dnscache_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package dnscache
  2. import (
  3. "context"
  4. "net"
  5. "strings"
  6. "testing"
  7. "time"
  8. "golang.org/x/net/dns/dnsmessage"
  9. )
  10. // wireA600 es una respuesta DNS A fija: router.example.com -> 192.0.2.10, TTL 600.
  11. // Generada con dnsmessage.Builder y usada por el fake UDP server.
  12. var wireA600 = []byte{
  13. 0x04, 0xd2, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  14. 0x06, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x07, 0x65, 0x78, 0x61, 0x6d,
  15. 0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
  16. 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x58, 0x00, 0x04,
  17. 0xc0, 0x00, 0x02, 0x0a,
  18. }
  19. // fakeDNS responde siempre wireA600 y cuenta las consultas recibidas.
  20. type fakeDNS struct {
  21. udp *net.UDPConn
  22. mu chan struct{}
  23. quit chan struct{}
  24. }
  25. func startFakeDNS(t *testing.T) *fakeDNS {
  26. t.Helper()
  27. addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. conn, err := net.ListenUDP("udp", addr)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. f := &fakeDNS{udp: conn, mu: make(chan struct{}, 64), quit: make(chan struct{})}
  36. go func() {
  37. buf := make([]byte, 512)
  38. for {
  39. n, src, err := conn.ReadFromUDP(buf)
  40. if err != nil {
  41. return
  42. }
  43. select {
  44. case <-f.quit:
  45. return
  46. default:
  47. }
  48. _ = n
  49. f.mu <- struct{}{}
  50. _, _ = conn.WriteToUDP(wireA600, src)
  51. }
  52. }()
  53. t.Cleanup(func() {
  54. close(f.quit)
  55. _ = conn.Close()
  56. })
  57. return f
  58. }
  59. func (f *fakeDNS) queries() int {
  60. n := 0
  61. for {
  62. select {
  63. case <-f.mu:
  64. n++
  65. default:
  66. return n
  67. }
  68. }
  69. }
  70. func TestLookupCacheHonorsTTL(t *testing.T) {
  71. f := startFakeDNS(t)
  72. r := &Resolver{}
  73. ctx := context.Background()
  74. // queryOne directo: parsea la respuesta fija y lee TTL 600.
  75. server := f.udp.LocalAddr().String()
  76. ips, ttl, err := queryOne(ctx, server, "router.example.com", dnsmessage.TypeA)
  77. if err != nil {
  78. t.Fatalf("queryOne: %v", err)
  79. }
  80. if len(ips) != 1 || !ips[0].Equal(net.ParseIP("192.0.2.10")) {
  81. t.Fatalf("ips: %v", ips)
  82. }
  83. if ttl != 600*time.Second {
  84. t.Fatalf("ttl: %v (want 600s)", ttl)
  85. }
  86. // Lookup con IP literal no consulta nada.
  87. if got := r.Lookup(ctx, "192.0.2.99"); len(got) != 1 || !got[0].Equal(net.ParseIP("192.0.2.99")) {
  88. t.Fatalf("literal: %v", got)
  89. }
  90. // Caché: store + expiry.
  91. r.store("h.example", []net.IP{net.ParseIP("192.0.2.1")}, 600*time.Second)
  92. for i := 0; i < 50; i++ {
  93. if got := r.Lookup(ctx, "h.example"); len(got) != 1 {
  94. t.Fatalf("caché rota en la iteración %d: %v", i, got)
  95. }
  96. }
  97. if f.queries() != 1 {
  98. t.Fatalf("la caché no debería consultar DNS tras el primer miss: %d", f.queries())
  99. }
  100. }
  101. func TestParseResolvConf(t *testing.T) {
  102. servers, err := resolvConfServers()
  103. if err != nil {
  104. t.Skipf("sin resolv.conf legible en este entorno: %v", err)
  105. }
  106. for _, s := range servers {
  107. if net.ParseIP(s) == nil {
  108. t.Fatalf("nameserver no IP: %q", s)
  109. }
  110. }
  111. }
  112. func TestDialContextResuelveYSiFallaInvalida(t *testing.T) {
  113. r := &Resolver{}
  114. r.store("nunca-escucha.example", []net.IP{net.ParseIP("127.0.0.1")}, 600*time.Second)
  115. _, err := r.DialContext(context.Background(), "tcp", "nunca-escucha.example:59999")
  116. if err == nil {
  117. t.Fatal("debe fallar: nada escucha en 59999")
  118. }
  119. r.mu.Lock()
  120. _, cached := r.cache["nunca-escucha.example"]
  121. r.mu.Unlock()
  122. if cached {
  123. t.Fatal("tras fallo de dial la entrada cacheada debe invalidarse")
  124. }
  125. if !strings.Contains(err.Error(), "refused") && !strings.Contains(err.Error(), "connect") {
  126. t.Logf("error de dial inesperado (no fatal): %v", err)
  127. }
  128. }