lookup_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // TODO It would be nice to use a mock DNS server, to eliminate
  5. // external dependencies.
  6. package net
  7. import (
  8. "flag"
  9. "strings"
  10. "testing"
  11. )
  12. var testExternal = flag.Bool("external", true, "allow use of external networks during long test")
  13. var lookupGoogleSRVTests = []struct {
  14. service, proto, name string
  15. cname, target string
  16. }{
  17. {
  18. "xmpp-server", "tcp", "google.com",
  19. ".google.com", ".google.com",
  20. },
  21. {
  22. "", "", "_xmpp-server._tcp.google.com", // non-standard back door
  23. ".google.com", ".google.com",
  24. },
  25. }
  26. func TestLookupGoogleSRV(t *testing.T) {
  27. if testing.Short() || !*testExternal {
  28. t.Skip("skipping test to avoid external network")
  29. }
  30. for _, tt := range lookupGoogleSRVTests {
  31. cname, srvs, err := LookupSRV(tt.service, tt.proto, tt.name)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. if len(srvs) == 0 {
  36. t.Error("got no record")
  37. }
  38. if !strings.Contains(cname, tt.cname) {
  39. t.Errorf("got %q; want %q", cname, tt.cname)
  40. }
  41. for _, srv := range srvs {
  42. if !strings.Contains(srv.Target, tt.target) {
  43. t.Errorf("got %v; want a record containing %q", srv, tt.target)
  44. }
  45. }
  46. }
  47. }
  48. func TestLookupGmailMX(t *testing.T) {
  49. if testing.Short() || !*testExternal {
  50. t.Skip("skipping test to avoid external network")
  51. }
  52. mxs, err := LookupMX("gmail.com")
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. if len(mxs) == 0 {
  57. t.Error("got no record")
  58. }
  59. for _, mx := range mxs {
  60. if !strings.Contains(mx.Host, ".google.com") {
  61. t.Errorf("got %v; want a record containing .google.com.", mx)
  62. }
  63. }
  64. }
  65. func TestLookupGmailNS(t *testing.T) {
  66. if testing.Short() || !*testExternal {
  67. t.Skip("skipping test to avoid external network")
  68. }
  69. nss, err := LookupNS("gmail.com")
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if len(nss) == 0 {
  74. t.Error("got no record")
  75. }
  76. for _, ns := range nss {
  77. if !strings.Contains(ns.Host, ".google.com") {
  78. t.Errorf("got %v; want a record containing .google.com.", ns)
  79. }
  80. }
  81. }
  82. func TestLookupGmailTXT(t *testing.T) {
  83. if testing.Short() || !*testExternal {
  84. t.Skip("skipping test to avoid external network")
  85. }
  86. txts, err := LookupTXT("gmail.com")
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. if len(txts) == 0 {
  91. t.Error("got no record")
  92. }
  93. for _, txt := range txts {
  94. if !strings.Contains(txt, "spf") {
  95. t.Errorf("got %q; want a spf record", txt)
  96. }
  97. }
  98. }
  99. var lookupGooglePublicDNSAddrs = []struct {
  100. addr string
  101. name string
  102. }{
  103. {"8.8.8.8", ".google.com."},
  104. {"8.8.4.4", ".google.com."},
  105. {"2001:4860:4860::8888", ".google.com."},
  106. {"2001:4860:4860::8844", ".google.com."},
  107. }
  108. func TestLookupGooglePublicDNSAddr(t *testing.T) {
  109. if testing.Short() || !*testExternal {
  110. t.Skip("skipping test to avoid external network")
  111. }
  112. for _, tt := range lookupGooglePublicDNSAddrs {
  113. names, err := LookupAddr(tt.addr)
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. if len(names) == 0 {
  118. t.Error("got no record")
  119. }
  120. for _, name := range names {
  121. if !strings.HasSuffix(name, tt.name) {
  122. t.Errorf("got %q; want a record containing %q", name, tt.name)
  123. }
  124. }
  125. }
  126. }
  127. func TestLookupIANACNAME(t *testing.T) {
  128. if testing.Short() || !*testExternal {
  129. t.Skip("skipping test to avoid external network")
  130. }
  131. cname, err := LookupCNAME("www.iana.org")
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. if !strings.HasSuffix(cname, ".icann.org.") {
  136. t.Errorf("got %q; want a record containing .icann.org.", cname)
  137. }
  138. }
  139. func TestLookupGoogleHost(t *testing.T) {
  140. if testing.Short() || !*testExternal {
  141. t.Skip("skipping test to avoid external network")
  142. }
  143. addrs, err := LookupHost("google.com")
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. if len(addrs) == 0 {
  148. t.Error("got no record")
  149. }
  150. for _, addr := range addrs {
  151. if ParseIP(addr) == nil {
  152. t.Errorf("got %q; want a literal ip address", addr)
  153. }
  154. }
  155. }
  156. func TestLookupGoogleIP(t *testing.T) {
  157. if testing.Short() || !*testExternal {
  158. t.Skip("skipping test to avoid external network")
  159. }
  160. ips, err := LookupIP("google.com")
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. if len(ips) == 0 {
  165. t.Error("got no record")
  166. }
  167. for _, ip := range ips {
  168. if ip.To4() == nil && ip.To16() == nil {
  169. t.Errorf("got %v; want an ip address", ip)
  170. }
  171. }
  172. }
  173. var revAddrTests = []struct {
  174. Addr string
  175. Reverse string
  176. ErrPrefix string
  177. }{
  178. {"1.2.3.4", "4.3.2.1.in-addr.arpa.", ""},
  179. {"245.110.36.114", "114.36.110.245.in-addr.arpa.", ""},
  180. {"::ffff:12.34.56.78", "78.56.34.12.in-addr.arpa.", ""},
  181. {"::1", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.", ""},
  182. {"1::", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.ip6.arpa.", ""},
  183. {"1234:567::89a:bcde", "e.d.c.b.a.9.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.7.6.5.0.4.3.2.1.ip6.arpa.", ""},
  184. {"1234:567:fefe:bcbc:adad:9e4a:89a:bcde", "e.d.c.b.a.9.8.0.a.4.e.9.d.a.d.a.c.b.c.b.e.f.e.f.7.6.5.0.4.3.2.1.ip6.arpa.", ""},
  185. {"1.2.3", "", "unrecognized address"},
  186. {"1.2.3.4.5", "", "unrecognized address"},
  187. {"1234:567:bcbca::89a:bcde", "", "unrecognized address"},
  188. {"1234:567::bcbc:adad::89a:bcde", "", "unrecognized address"},
  189. }
  190. func TestReverseAddress(t *testing.T) {
  191. for i, tt := range revAddrTests {
  192. a, err := reverseaddr(tt.Addr)
  193. if len(tt.ErrPrefix) > 0 && err == nil {
  194. t.Errorf("#%d: expected %q, got <nil> (error)", i, tt.ErrPrefix)
  195. continue
  196. }
  197. if len(tt.ErrPrefix) == 0 && err != nil {
  198. t.Errorf("#%d: expected <nil>, got %q (error)", i, err)
  199. }
  200. if err != nil && err.(*DNSError).Err != tt.ErrPrefix {
  201. t.Errorf("#%d: expected %q, got %q (mismatched error)", i, tt.ErrPrefix, err.(*DNSError).Err)
  202. }
  203. if a != tt.Reverse {
  204. t.Errorf("#%d: expected %q, got %q (reverse address)", i, tt.Reverse, a)
  205. }
  206. }
  207. }