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.
5 // TODO It would be nice to use a mock DNS server, to eliminate
6 // external dependencies.
16 var testExternal
= flag
.Bool("external", true, "allow use of external networks during long test")
18 var lookupGoogleSRVTests
= []struct {
19 service
, proto
, name
string
23 "xmpp-server", "tcp", "google.com",
24 ".google.com", ".google.com",
27 "", "", "_xmpp-server._tcp.google.com", // non-standard back door
28 ".google.com", ".google.com",
32 func TestLookupGoogleSRV(t
*testing
.T
) {
33 if testing
.Short() ||
!*testExternal
{
34 t
.Skip("skipping test to avoid external network")
37 for _
, tt
:= range lookupGoogleSRVTests
{
38 cname
, srvs
, err
:= LookupSRV(tt
.service
, tt
.proto
, tt
.name
)
43 t
.Error("got no record")
45 if !strings
.Contains(cname
, tt
.cname
) {
46 t
.Errorf("got %q; want %q", cname
, tt
.cname
)
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
)
56 func TestLookupGmailMX(t
*testing
.T
) {
57 if testing
.Short() ||
!*testExternal
{
58 t
.Skip("skipping test to avoid external network")
61 mxs
, err
:= LookupMX("gmail.com")
66 t
.Error("got no record")
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
)
75 func TestLookupGmailNS(t
*testing
.T
) {
76 if testing
.Short() ||
!*testExternal
{
77 t
.Skip("skipping test to avoid external network")
80 nss
, err
:= LookupNS("gmail.com")
85 t
.Error("got no record")
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
)
94 func TestLookupGmailTXT(t
*testing
.T
) {
95 if testing
.Short() ||
!*testExternal
{
96 t
.Skip("skipping test to avoid external network")
99 txts
, err
:= LookupTXT("gmail.com")
104 t
.Error("got no record")
106 for _
, txt
:= range txts
{
107 if !strings
.Contains(txt
, "spf") {
108 t
.Errorf("got %q; want a spf record", txt
)
113 var lookupGooglePublicDNSAddrs
= []struct {
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."},
123 func TestLookupGooglePublicDNSAddr(t
*testing
.T
) {
124 if testing
.Short() ||
!*testExternal
{
125 t
.Skip("skipping test to avoid external network")
128 for _
, tt
:= range lookupGooglePublicDNSAddrs
{
129 names
, err
:= LookupAddr(tt
.addr
)
134 t
.Error("got no record")
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
)
144 func TestLookupIANACNAME(t
*testing
.T
) {
145 if testing
.Short() ||
!*testExternal
{
146 t
.Skip("skipping test to avoid external network")
149 cname
, err
:= LookupCNAME("www.iana.org")
153 if !strings
.HasSuffix(cname
, ".icann.org.") {
154 t
.Errorf("got %q; want a record containing .icann.org.", cname
)
158 func TestLookupGoogleHost(t
*testing
.T
) {
159 if testing
.Short() ||
!*testExternal
{
160 t
.Skip("skipping test to avoid external network")
163 addrs
, err
:= LookupHost("google.com")
168 t
.Error("got no record")
170 for _
, addr
:= range addrs
{
171 if ParseIP(addr
) == nil {
172 t
.Errorf("got %q; want a literal ip address", addr
)
177 func TestLookupGoogleIP(t
*testing
.T
) {
178 if testing
.Short() ||
!*testExternal
{
179 t
.Skip("skipping test to avoid external network")
182 ips
, err
:= LookupIP("google.com")
187 t
.Error("got no record")
189 for _
, ip
:= range ips
{
190 if ip
.To4() == nil && ip
.To16() == nil {
191 t
.Errorf("got %v; want an ip address", ip
)
196 var revAddrTests
= []struct {
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"},
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
)
221 if len(tt
.ErrPrefix
) == 0 && err
!= nil {
222 t
.Errorf("#%d: expected <nil>, got %q (error)", i
, err
)
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
)
228 t
.Errorf("#%d: expected %q, got %q (reverse address)", i
, tt
.Reverse
, a
)