rdns_builddevices_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package adapters
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. // TestBuildDevicesNamesFromReverseDNS: a static-IP host seen only in ARP
  7. // takes its PTR name once the background lookup lands; a device with a
  8. // DHCP hostname keeps it and is never looked up.
  9. func TestBuildDevicesNamesFromReverseDNS(t *testing.T) {
  10. f := &fakeRDNS{
  11. ptr: map[string][]string{
  12. "172.16.16.4": {"switch-garage.home.arpa."},
  13. "172.16.16.70": {"should-not-win.home.arpa."},
  14. },
  15. forward: map[string][]string{
  16. "switch-garage.home.arpa": {"172.16.16.4"},
  17. "should-not-win.home.arpa": {"172.16.16.70"},
  18. },
  19. }
  20. l := NewLive(nil, nil, nil, nil)
  21. l.rdns = newTestRDNS(f, time.Now)
  22. polled := map[string]*routerPolled{
  23. "gw": {
  24. cfg: RouterConfig{ID: "gw", Name: "Gateway", Host: "172.16.16.1", IsGateway: true},
  25. arp: map[string]string{
  26. "5C:E2:8C:00:00:04": "172.16.16.4",
  27. },
  28. leases: []DhcpLease{
  29. {MAC: "AA:BB:CC:00:00:70", IP: "172.16.16.70", Hostname: "laptop"},
  30. },
  31. },
  32. }
  33. byMAC := func(devs []Device) map[string]Device {
  34. m := map[string]Device{}
  35. for _, d := range devs {
  36. m[d.MAC] = d
  37. }
  38. return m
  39. }
  40. first := byMAC(l.buildDevices(polled, ""))
  41. if got := first["5C:E2:8C:00:00:04"].Name; got != "5C:E2:8C:00:00:04" {
  42. t.Fatalf("first poll must not wait on DNS: got %q", got)
  43. }
  44. l.rdns.wg.Wait()
  45. devs := byMAC(l.buildDevices(polled, ""))
  46. if got := devs["5C:E2:8C:00:00:04"].Name; got != "switch-garage" {
  47. t.Fatalf("static host: got %q, want switch-garage", got)
  48. }
  49. // The type is guessed again after the PTR name lands: "switch-garage"
  50. // matches the switch rule, while a bare MAC can only be "desconocido".
  51. if got := devs["5C:E2:8C:00:00:04"].Type; got != "switch" {
  52. t.Fatalf("static host type: got %q, want switch", got)
  53. }
  54. if got := devs["AA:BB:CC:00:00:70"].Name; got != "laptop" {
  55. t.Fatalf("leased host: got %q, want laptop", got)
  56. }
  57. if n := f.count("172.16.16.70"); n != 0 {
  58. t.Fatalf("named device was looked up %d times", n)
  59. }
  60. }