rdns_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package adapters
  2. import (
  3. "context"
  4. "errors"
  5. "net/netip"
  6. "sync"
  7. "testing"
  8. "time"
  9. )
  10. type fakeRDNS struct {
  11. mu sync.Mutex
  12. ptr map[string][]string
  13. forward map[string][]string
  14. calls map[string]int
  15. }
  16. func (f *fakeRDNS) LookupAddr(_ context.Context, addr string) ([]string, error) {
  17. f.mu.Lock()
  18. defer f.mu.Unlock()
  19. if f.calls == nil {
  20. f.calls = map[string]int{}
  21. }
  22. f.calls[addr]++
  23. if names, ok := f.ptr[addr]; ok {
  24. return names, nil
  25. }
  26. return nil, errors.New("no such host")
  27. }
  28. func (f *fakeRDNS) LookupNetIP(_ context.Context, _, host string) ([]netip.Addr, error) {
  29. ips, ok := f.forward[host]
  30. if !ok {
  31. return nil, errors.New("no such host")
  32. }
  33. out := []netip.Addr{}
  34. for _, s := range ips {
  35. out = append(out, netip.MustParseAddr(s))
  36. }
  37. return out, nil
  38. }
  39. func (f *fakeRDNS) count(addr string) int {
  40. f.mu.Lock()
  41. defer f.mu.Unlock()
  42. return f.calls[addr]
  43. }
  44. func newTestRDNS(f *fakeRDNS, now func() time.Time) *rdnsCache {
  45. c := newRDNSCache()
  46. c.enabled = true
  47. c.resolver = f
  48. c.now = now
  49. return c
  50. }
  51. // resolved asks once (queues the lookup), waits for it, and asks again.
  52. func resolved(c *rdnsCache, ip string) string {
  53. if n := c.Name(ip); n != "" {
  54. return n
  55. }
  56. c.wg.Wait()
  57. return c.Name(ip)
  58. }
  59. func TestRDNSName(t *testing.T) {
  60. f := &fakeRDNS{
  61. ptr: map[string][]string{
  62. "172.16.16.4": {"switch-garage.home.arpa."},
  63. "172.16.17.202": {"pihole.home.arpa."},
  64. "192.168.8.217": {"192-168-8-217.0a1b2c3d4e5f.stremio.rocks."},
  65. "10.0.0.5": {"ip-10-0-0-5.ec2.internal."},
  66. "172.16.16.30": {"stale.home.arpa."},
  67. "172.16.16.31": {"printer-ptr-only."},
  68. "172.16.16.40": {"192-168-8-217.example.net.", "nas.home.arpa."},
  69. "fd00::10": {"nas6.home.arpa."},
  70. "203.0.113.1": {"gateway.isp.example."},
  71. },
  72. forward: map[string][]string{
  73. "switch-garage.home.arpa": {"172.16.16.4"},
  74. "pihole.home.arpa": {"172.16.17.202"},
  75. "192-168-8-217.0a1b2c3d4e5f.stremio.rocks": {"192.168.8.217"},
  76. "ip-10-0-0-5.ec2.internal": {"10.0.0.5"},
  77. "stale.home.arpa": {"172.16.16.99"},
  78. "nas.home.arpa": {"172.16.16.40"},
  79. "nas6.home.arpa": {"fd00::10"},
  80. "gateway.isp.example": {"203.0.113.1"},
  81. },
  82. }
  83. c := newTestRDNS(f, time.Now)
  84. cases := []struct {
  85. ip, want, why string
  86. }{
  87. {"172.16.16.4", "switch-garage", "static switch, forward-confirmed"},
  88. {"172.16.17.202", "pihole", "static container"},
  89. {"192.168.8.217", "", "wildcard PTR that embeds the address"},
  90. {"10.0.0.5", "", "cloud-style ip-a-b-c-d name"},
  91. {"172.16.16.30", "", "PTR that does not resolve back"},
  92. {"172.16.16.31", "", "PTR target with no forward record"},
  93. {"172.16.16.40", "nas", "skips the synthesized name, takes the real one"},
  94. {"fd00::10", "nas6", "IPv6 ULA"},
  95. {"203.0.113.1", "", "public address is never looked up"},
  96. {"172.16.16.50", "", "no PTR at all"},
  97. {"not-an-ip", "", "garbage input"},
  98. }
  99. for _, tc := range cases {
  100. if got := resolved(c, tc.ip); got != tc.want {
  101. t.Errorf("%s (%s): got %q, want %q", tc.ip, tc.why, got, tc.want)
  102. }
  103. }
  104. if n := f.count("203.0.113.1"); n != 0 {
  105. t.Errorf("public address was looked up %d times", n)
  106. }
  107. }
  108. func TestRDNSIPv4MappedAddress(t *testing.T) {
  109. f := &fakeRDNS{
  110. ptr: map[string][]string{
  111. "172.16.16.6": {"ap-hall.home.arpa."},
  112. },
  113. forward: map[string][]string{
  114. "ap-hall.home.arpa": {"172.16.16.6"},
  115. },
  116. }
  117. c := newTestRDNS(f, time.Now)
  118. // A mapped address has to be unmapped before the forward comparison,
  119. // otherwise LookupNetIP returns a plain IPv4 and the match never happens.
  120. if got := resolved(c, "::ffff:172.16.16.6"); got != "ap-hall" {
  121. t.Fatalf("mapped address: got %q, want ap-hall", got)
  122. }
  123. if n := f.count("172.16.16.6"); n != 1 {
  124. t.Fatalf("lookup used %v, want the unmapped 172.16.16.6", f.calls)
  125. }
  126. }
  127. func TestRDNSDoesNotQueueBeyondConcurrency(t *testing.T) {
  128. f := &fakeRDNS{ptr: map[string][]string{}, forward: map[string][]string{}}
  129. c := newTestRDNS(f, time.Now)
  130. for i := 0; i < rdnsConcurrency; i++ {
  131. c.sem <- struct{}{}
  132. }
  133. c.Name("172.16.16.61")
  134. c.mu.Lock()
  135. queued := len(c.inflight)
  136. c.mu.Unlock()
  137. if queued != 0 {
  138. t.Fatalf("queued %d lookups while every worker was busy", queued)
  139. }
  140. for i := 0; i < rdnsConcurrency; i++ {
  141. <-c.sem
  142. }
  143. // With a slot free the next poll queues it normally.
  144. c.Name("172.16.16.61")
  145. c.wg.Wait()
  146. c.mu.Lock()
  147. queued = len(c.inflight)
  148. c.mu.Unlock()
  149. if queued != 0 {
  150. t.Fatalf("%d lookups still in flight after the worker finished", queued)
  151. }
  152. if _, ok := c.entries["172.16.16.61"]; !ok {
  153. t.Fatal("lookup was never resolved once a slot was free")
  154. }
  155. }
  156. func TestRDNSCachesAndExpires(t *testing.T) {
  157. f := &fakeRDNS{
  158. ptr: map[string][]string{"172.16.16.4": {"switch-garage.home.arpa."}},
  159. forward: map[string][]string{"switch-garage.home.arpa": {"172.16.16.4"}},
  160. }
  161. now := time.Unix(1_800_000_000, 0)
  162. c := newTestRDNS(f, func() time.Time { return now })
  163. if got := c.Name("172.16.16.4"); got != "" {
  164. t.Fatalf("first call must not block on DNS, got %q", got)
  165. }
  166. c.wg.Wait()
  167. for i := 0; i < 5; i++ {
  168. if got := c.Name("172.16.16.4"); got != "switch-garage" {
  169. t.Fatalf("cached name: got %q", got)
  170. }
  171. }
  172. if n := f.count("172.16.16.4"); n != 1 {
  173. t.Fatalf("lookups within TTL: got %d, want 1", n)
  174. }
  175. // After the TTL the stale name keeps showing while one refresh runs.
  176. now = now.Add(rdnsTTL + time.Second)
  177. if got := c.Name("172.16.16.4"); got != "switch-garage" {
  178. t.Fatalf("stale name during refresh: got %q", got)
  179. }
  180. c.wg.Wait()
  181. if n := f.count("172.16.16.4"); n != 2 {
  182. t.Fatalf("lookups after TTL: got %d, want 2", n)
  183. }
  184. }
  185. func TestRDNSNegativeCache(t *testing.T) {
  186. f := &fakeRDNS{}
  187. now := time.Unix(1_800_000_000, 0)
  188. c := newTestRDNS(f, func() time.Time { return now })
  189. resolved(c, "172.16.16.50")
  190. resolved(c, "172.16.16.50")
  191. if n := f.count("172.16.16.50"); n != 1 {
  192. t.Fatalf("misses within negative TTL: got %d lookups, want 1", n)
  193. }
  194. now = now.Add(rdnsNegativeTTL + time.Second)
  195. resolved(c, "172.16.16.50")
  196. if n := f.count("172.16.16.50"); n != 2 {
  197. t.Fatalf("after negative TTL: got %d lookups, want 2", n)
  198. }
  199. }
  200. func TestRDNSDisabled(t *testing.T) {
  201. t.Setenv("NETPULSE_RDNS", "0")
  202. c := newRDNSCache()
  203. f := &fakeRDNS{ptr: map[string][]string{"172.16.16.4": {"switch-garage.home.arpa."}}}
  204. c.resolver = f
  205. if got := c.Name("172.16.16.4"); got != "" {
  206. t.Fatalf("disabled cache returned %q", got)
  207. }
  208. c.wg.Wait()
  209. if n := f.count("172.16.16.4"); n != 0 {
  210. t.Fatalf("disabled cache looked up %d times", n)
  211. }
  212. var nilCache *rdnsCache
  213. if got := nilCache.Name("172.16.16.4"); got != "" {
  214. t.Fatalf("nil cache returned %q", got)
  215. }
  216. }