arp_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // arp_test.go — Fallback de resolución IP vía tabla ARP (#377).
  2. package adapters
  3. import (
  4. "context"
  5. "testing"
  6. "github.com/gnacho/netpulse/agent/probe"
  7. "github.com/gnacho/netpulse/server-go/internal/db"
  8. )
  9. func TestBuildDevicesArpFallback(t *testing.T) {
  10. l := NewLive(nil, nil, nil, nil)
  11. polled := map[string]*routerPolled{
  12. "patio": {
  13. cfg: RouterConfig{ID: "patio", Name: "Patio", Host: "192.168.1.2"},
  14. wireless: map[string]WirelessClient{
  15. "AA:BB:CC:DD:EE:FF": {SignalDbm: -55, Band: "5 GHz"},
  16. },
  17. arp: map[string]string{
  18. "AA:BB:CC:DD:EE:FF": "192.168.1.50",
  19. },
  20. },
  21. }
  22. devs := l.buildDevices(polled, "")
  23. if len(devs) != 1 {
  24. t.Fatalf("esperado 1 dispositivo, got %d: %+v", len(devs), devs)
  25. }
  26. if devs[0].IP != "192.168.1.50" {
  27. t.Fatalf("IP esperada 192.168.1.50, got %q (dispositivo %+v)", devs[0].IP, devs[0])
  28. }
  29. }
  30. // TestBuildDevicesArpDiscovery: una MAC visible SOLO por ARP (sin wireless,
  31. // sin FDB, sin lease, sin device_attrib) aparece como dispositivo cableado
  32. // online con la IP de la tabla ARP y el RouterID del router que la reportó
  33. // (#507).
  34. func TestBuildDevicesArpDiscovery(t *testing.T) {
  35. l := NewLive(nil, nil, nil, nil)
  36. polled := map[string]*routerPolled{
  37. "patio": {
  38. cfg: RouterConfig{ID: "patio", Name: "Patio", Host: "192.168.1.2"},
  39. arp: map[string]string{
  40. "AA:BB:CC:DD:EE:FF": "192.168.1.60",
  41. },
  42. },
  43. }
  44. devs := l.buildDevices(polled, "")
  45. if len(devs) != 1 {
  46. t.Fatalf("esperado 1 dispositivo, got %d: %+v", len(devs), devs)
  47. }
  48. d := devs[0]
  49. if d.MAC != "AA:BB:CC:DD:EE:FF" {
  50. t.Fatalf("MAC esperada AA:BB:CC:DD:EE:FF, got %q", d.MAC)
  51. }
  52. if d.IP != "192.168.1.60" {
  53. t.Fatalf("IP esperada 192.168.1.60, got %q", d.IP)
  54. }
  55. if d.Band != "cable" {
  56. t.Fatalf("Band esperada cable, got %q", d.Band)
  57. }
  58. if !d.Online {
  59. t.Fatalf("Online esperado true (presencia ARP = activo reciente)")
  60. }
  61. if d.RouterID != "patio" {
  62. t.Fatalf("RouterID esperado patio, got %q", d.RouterID)
  63. }
  64. }
  65. // TestBuildDevicesArpRouterExcluded: una MAC de bridge/router presente en la
  66. // tabla ARP NO debe aparecer como dispositivo (#507).
  67. func TestBuildDevicesArpRouterExcluded(t *testing.T) {
  68. l := NewLive(nil, nil, nil, nil)
  69. polled := map[string]*routerPolled{
  70. "patio": {
  71. cfg: RouterConfig{ID: "patio", Name: "Patio", Host: "192.168.1.2"},
  72. brMac: "AA:BB:CC:DD:EE:FF",
  73. arp: map[string]string{
  74. "AA:BB:CC:DD:EE:FF": "192.168.1.60",
  75. },
  76. },
  77. }
  78. devs := l.buildDevices(polled, "")
  79. if len(devs) != 0 {
  80. t.Fatalf("esperado 0 dispositivos (bridge MAC excluida), got %d: %+v", len(devs), devs)
  81. }
  82. }
  83. func TestPolledFromAgentArp(t *testing.T) {
  84. l := NewLive(nil, nil, nil, nil)
  85. pl := &probe.Payload{Router: "patio", Version: "0.1.0"}
  86. pl.Data.System = &probe.SystemData{SysInfo: &probe.SysInfo{Uptime: 100}}
  87. pl.Data.Arp = map[string]string{"AA:BB:CC:DD:EE:FF": "192.168.1.50"}
  88. p := l.polledFromAgent(RouterConfig{ID: "patio"}, pl)
  89. if p == nil {
  90. t.Fatal("polledFromAgent devolvió nil")
  91. }
  92. if p.arp["AA:BB:CC:DD:EE:FF"] != "192.168.1.50" {
  93. t.Fatalf("arp no propagada: %+v", p.arp)
  94. }
  95. }
  96. // A MAC whose only evidence is a neighbour entry the kernel has NOT
  97. // confirmed is not a device: the entry outlives the host by minutes, and
  98. // with no port either the map hung it off the gateway bubble.
  99. func TestBuildDevicesIgnoresStaleArpOnlyHosts(t *testing.T) {
  100. l := NewLive(nil, nil, nil, nil)
  101. polled := map[string]*routerPolled{
  102. "patio": {
  103. cfg: RouterConfig{ID: "patio", Name: "Patio", Host: "192.0.2.2"},
  104. arp: map[string]string{
  105. "02:00:00:00:00:10": "192.0.2.60", // confirmed
  106. "02:00:00:00:00:11": "192.0.2.61", // only remembered
  107. },
  108. arpStale: map[string]bool{"02:00:00:00:00:11": true},
  109. },
  110. }
  111. devs := l.buildDevices(polled, "")
  112. if len(devs) != 1 || devs[0].MAC != "02:00:00:00:00:10" {
  113. t.Fatalf("only the confirmed host is a device: %+v", devs)
  114. }
  115. }
  116. // Stale is about presence, never about other evidence: a host with a lease
  117. // stays, and its address still comes from the stale entry.
  118. func TestBuildDevicesStaleArpStillResolvesIPs(t *testing.T) {
  119. l := NewLive(nil, nil, nil, nil)
  120. polled := map[string]*routerPolled{
  121. "patio": {
  122. cfg: RouterConfig{ID: "patio", Name: "Patio", Host: "192.0.2.2"},
  123. wireless: map[string]WirelessClient{"02:00:00:00:00:11": {SignalDbm: -55, Band: "5 GHz"}},
  124. arp: map[string]string{"02:00:00:00:00:11": "192.0.2.61"},
  125. arpStale: map[string]bool{"02:00:00:00:00:11": true},
  126. },
  127. }
  128. devs := l.buildDevices(polled, "")
  129. if len(devs) != 1 || devs[0].IP != "192.0.2.61" {
  130. t.Fatalf("an associated client keeps its address: %+v", devs)
  131. }
  132. }
  133. // Two routers see the same host: confirmed anywhere is confirmed.
  134. func TestBuildDevicesStaleOnOneRouterOnly(t *testing.T) {
  135. l := NewLive(nil, nil, nil, nil)
  136. polled := map[string]*routerPolled{
  137. "patio": {
  138. cfg: RouterConfig{ID: "patio", Name: "Patio", Host: "192.0.2.2"},
  139. arp: map[string]string{"02:00:00:00:00:10": "192.0.2.60"},
  140. arpStale: map[string]bool{"02:00:00:00:00:10": true},
  141. },
  142. "salon": {
  143. cfg: RouterConfig{ID: "salon", Name: "Salon", Host: "192.0.2.3"},
  144. arp: map[string]string{"02:00:00:00:00:10": "192.0.2.60"},
  145. },
  146. }
  147. if devs := l.buildDevices(polled, ""); len(devs) != 1 {
  148. t.Fatalf("confirmed by one router is enough: %+v", devs)
  149. }
  150. }
  151. // A source that cannot report states (an older agent, or the /proc/net/arp
  152. // fallback) marks nothing stale, and the whole table counts as presence.
  153. func TestPolledFromAgentArpStale(t *testing.T) {
  154. l := NewLive(nil, nil, nil, nil)
  155. pl := &probe.Payload{Router: "patio", Version: "0.1.0"}
  156. pl.Data.System = &probe.SystemData{SysInfo: &probe.SysInfo{Uptime: 100}}
  157. pl.Data.Arp = map[string]string{"02:00:00:00:00:10": "192.0.2.60"}
  158. if p := l.polledFromAgent(RouterConfig{ID: "patio"}, pl); len(p.arpStale) != 0 {
  159. t.Fatalf("an agent without arpStale marks nothing stale: %+v", p.arpStale)
  160. }
  161. pl.Data.ArpStale = []string{"02:00:00:00:00:10"}
  162. p := l.polledFromAgent(RouterConfig{ID: "patio"}, pl)
  163. if !p.arpStale["02:00:00:00:00:10"] {
  164. t.Fatalf("arpStale not propagated: %+v", p.arpStale)
  165. }
  166. }
  167. // A device_attrib row outlives the router it names: deleting a router, or an
  168. // integration that registered one per AP, used to leave its clients
  169. // attributed to an id nothing resolves, so no router counted them.
  170. func TestBuildDevicesReattributesDeletedRouters(t *testing.T) {
  171. d := openLiveTestDB(t)
  172. l := NewLive(nil, d, nil, nil)
  173. l.SetRouters([]RouterConfig{
  174. {ID: "gateway", Name: "Gateway", Host: "192.0.2.1", IsGateway: true, AgentOnly: true},
  175. })
  176. // One client remembered on a router that still exists, one on a router
  177. // that was deleted.
  178. for _, r := range [][2]string{
  179. {"02:00:00:00:00:01", "gateway"},
  180. {"02:00:00:00:00:02", "ap-that-was-deleted"},
  181. } {
  182. if _, err := d.Exec(
  183. `INSERT INTO device_attrib (mac, router_id, band, signal_dbm, last_seen) VALUES (?,?,?,?,?)`,
  184. r[0], r[1], "5 GHz", -50, db.NowMS()); err != nil {
  185. t.Fatal(err)
  186. }
  187. }
  188. polled := map[string]*routerPolled{
  189. "gateway": {
  190. cfg: RouterConfig{ID: "gateway", Name: "Gateway", Host: "192.0.2.1", IsGateway: true},
  191. leases: []DhcpLease{
  192. {MAC: "02:00:00:00:00:01", IP: "192.0.2.51", Hostname: "one"},
  193. {MAC: "02:00:00:00:00:02", IP: "192.0.2.52", Hostname: "two"},
  194. },
  195. },
  196. }
  197. byMAC := map[string]Device{}
  198. for _, dev := range l.buildDevices(polled, "") {
  199. byMAC[dev.MAC] = dev
  200. }
  201. if len(byMAC) != 2 {
  202. t.Fatalf("devices: %+v", byMAC)
  203. }
  204. for mac, dev := range byMAC {
  205. if dev.RouterID != "gateway" {
  206. t.Fatalf("%s should fall back to the gateway, got %q", mac, dev.RouterID)
  207. }
  208. }
  209. }
  210. // countClientsPerRouter: online devices attributed to each router, and
  211. // nothing else -- offline devices and devices attributed to a router that is
  212. // not in the list do not count anywhere.
  213. func TestCountClientsPerRouter(t *testing.T) {
  214. routers := []Router{{ID: "gateway"}, {ID: "patio"}}
  215. devices := []Device{
  216. {MAC: "02:00:00:00:00:01", RouterID: "gateway", Band: "cable", Online: true},
  217. {MAC: "02:00:00:00:00:02", RouterID: "gateway", Band: "5 GHz", Online: true},
  218. {MAC: "02:00:00:00:00:03", RouterID: "gateway", Band: "2.4 GHz", Online: false},
  219. {MAC: "02:00:00:00:00:04", RouterID: "patio", Band: "2.4 GHz", Online: true},
  220. {MAC: "02:00:00:00:00:05", RouterID: "ghost", Band: "cable", Online: true},
  221. }
  222. countClientsPerRouter(routers, devices)
  223. if routers[0].Clients != 2 || routers[1].Clients != 1 {
  224. t.Fatalf("counts: %d %d", routers[0].Clients, routers[1].Clients)
  225. }
  226. if routers[0].BandSplit == nil || routers[0].BandSplit.Cable != 1 || routers[0].BandSplit.Band5 != 1 {
  227. t.Fatalf("band split: %+v", routers[0].BandSplit)
  228. }
  229. // The offline 2.4 GHz device is not in the split either.
  230. if routers[0].BandSplit.Band24 != 0 {
  231. t.Fatalf("an offline device was counted: %+v", routers[0].BandSplit)
  232. }
  233. }
  234. // GET /api/routers used to report the DHCP lease count while the overview
  235. // reported the real attribution, so the routers page and the map disagreed
  236. // about the same router.
  237. func TestGetRoutersCountsClientsNotLeases(t *testing.T) {
  238. d := openLiveTestDB(t)
  239. l := NewLive(nil, d, nil, nil)
  240. cfg := RouterConfig{ID: "gateway", Name: "Gateway", Host: "192.0.2.1", IsGateway: true, AgentOnly: true}
  241. l.SetRouters([]RouterConfig{cfg})
  242. // #1818: un cliente nuevo se retiene un minuto antes de listarse. Estos dos
  243. // ya estaban vistos, que es lo que este test mide (leases vs asociados).
  244. l.noteDevicesSeen([]Device{
  245. {MAC: "02:00:00:00:00:01", Online: true, Hostname: "one"},
  246. {MAC: "02:00:00:00:00:02", Online: true, Hostname: "two"},
  247. }, 1000)
  248. // Three leases, but only two of those hosts are associated right now.
  249. l.mu.Lock()
  250. l.lastPolled = map[string]*routerPolled{"gateway": {
  251. cfg: cfg,
  252. leases: []DhcpLease{
  253. {MAC: "02:00:00:00:00:01", IP: "192.0.2.51", Hostname: "one"},
  254. {MAC: "02:00:00:00:00:02", IP: "192.0.2.52", Hostname: "two"},
  255. {MAC: "02:00:00:00:00:03", IP: "192.0.2.53", Hostname: "gone"},
  256. },
  257. wireless: map[string]WirelessClient{
  258. "02:00:00:00:00:01": {SignalDbm: -50, Band: "5 GHz"},
  259. "02:00:00:00:00:02": {SignalDbm: -60, Band: "2.4 GHz"},
  260. },
  261. }}
  262. l.mu.Unlock()
  263. routers := l.GetRouters(context.Background())
  264. if len(routers) != 1 {
  265. t.Fatalf("routers: %+v", routers)
  266. }
  267. if routers[0].Clients != 2 {
  268. t.Fatalf("clients: got %d, want 2 (the associated ones, not the 3 leases)", routers[0].Clients)
  269. }
  270. // And the same number the overview would report.
  271. online := 0
  272. for _, dev := range l.attributedDevices() {
  273. if dev.Online && dev.RouterID == "gateway" {
  274. online++
  275. }
  276. }
  277. if online != routers[0].Clients {
  278. t.Fatalf("routers endpoint says %d, attribution says %d", routers[0].Clients, online)
  279. }
  280. }
  281. // #1485: el ARP aprendido en el puerto WAN del gateway cuenta como presencia
  282. // para el primer salto del enlace (ONT/modem en bridge tras PPPoE), que no
  283. // está en ninguna FDB. Solo en la boca WAN: el ARP de una boca LAN no se marca.
  284. func TestBuildDevicesWanSideArp(t *testing.T) {
  285. l := NewLive(nil, nil, nil, nil)
  286. l.gatewayCfg = &RouterConfig{ID: "gw", Name: "Gateway", IsGateway: true}
  287. polled := map[string]*routerPolled{
  288. "gw": {
  289. cfg: RouterConfig{ID: "gw", Name: "Gateway", IsGateway: true},
  290. arp: map[string]string{
  291. "C0:49:43:A3:D1:34": "192.168.1.1", // ONT en el puerto WAN
  292. "AA:BB:CC:00:00:01": "192.168.1.60", // cliente visto por LAN
  293. },
  294. arpDevs: map[string]string{
  295. "C0:49:43:A3:D1:34": "eth2",
  296. "AA:BB:CC:00:00:01": "br-lan",
  297. },
  298. },
  299. }
  300. devs := l.buildDevices(polled, "eth2")
  301. byMAC := map[string]Device{}
  302. for _, d := range devs {
  303. byMAC[d.MAC] = d
  304. }
  305. ont, ok := byMAC["C0:49:43:A3:D1:34"]
  306. if !ok {
  307. t.Fatalf("el ONT no aparece en devices: %+v", devs)
  308. }
  309. if !ont.WanSide {
  310. t.Fatalf("ONT: %+v, want WanSide (ARP en el puerto WAN)", ont)
  311. }
  312. if !ont.Online {
  313. t.Fatalf("ONT: %+v, want online (el ARP del puerto WAN es presencia)", ont)
  314. }
  315. lan, ok := byMAC["AA:BB:CC:00:00:01"]
  316. if !ok {
  317. t.Fatalf("el cliente LAN no aparece: %+v", devs)
  318. }
  319. if lan.WanSide {
  320. t.Fatalf("un ARP de boca LAN no puede ser WanSide: %+v", lan)
  321. }
  322. // Sin boca WAN (p.ej. rutas que no la pasan) no se marca nada.
  323. for _, d := range l.buildDevices(polled, "") {
  324. if d.WanSide {
  325. t.Fatalf("sin wanPort no debe haber WanSide: %+v", d)
  326. }
  327. }
  328. }
  329. // #1489: la MAC que responde por la IP de gestión de una unidad de flota es la
  330. // propia unidad (pila de mgmt del switch), no un cliente: se absorbe como las
  331. // MACs de bridge aunque el FDB vecino la aprenda en su br-lan.
  332. func TestBuildDevicesAbsorbsSwitchMgmtMac(t *testing.T) {
  333. l := NewLive(nil, nil, nil, nil)
  334. l.routers = []RouterConfig{{ID: "xs708t", Host: "192.168.2.6", Type: "managed-switch"}}
  335. polled := map[string]*routerPolled{
  336. "gw": {
  337. cfg: RouterConfig{ID: "gw", Host: "192.168.1.1", IsGateway: true},
  338. arp: map[string]string{
  339. "08:BD:43:75:50:CD": "192.168.2.6", // el propio switch
  340. "AA:BB:CC:00:00:09": "192.168.2.9", // cliente normal
  341. },
  342. },
  343. }
  344. devs := l.buildDevices(polled, "")
  345. for _, d := range devs {
  346. if d.MAC == "08:BD:43:75:50:CD" {
  347. t.Fatalf("la MAC de gestión del switch no es un cliente: %+v", d)
  348. }
  349. }
  350. if len(devs) != 1 || devs[0].MAC != "AA:BB:CC:00:00:09" {
  351. t.Fatalf("devices: %+v, want solo el cliente normal", devs)
  352. }
  353. }