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