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 func TestGoogleSRV(t
*testing
.T
) {
19 if testing
.Short() ||
!*testExternal
{
20 t
.Skip("skipping test to avoid external network")
22 _
, addrs
, err
:= LookupSRV("xmpp-server", "tcp", "google.com")
24 t
.Errorf("failed: %s", err
)
27 t
.Errorf("no results")
30 // Non-standard back door.
31 _
, addrs
, err
= LookupSRV("", "", "_xmpp-server._tcp.google.com")
33 t
.Errorf("back door failed: %s", err
)
36 t
.Errorf("back door no results")
40 func TestGmailMX(t
*testing
.T
) {
41 if testing
.Short() ||
!*testExternal
{
42 t
.Skip("skipping test to avoid external network")
44 mx
, err
:= LookupMX("gmail.com")
46 t
.Errorf("failed: %s", err
)
49 t
.Errorf("no results")
53 func TestGmailNS(t
*testing
.T
) {
54 if testing
.Short() ||
!*testExternal
{
55 t
.Skip("skipping test to avoid external network")
57 ns
, err
:= LookupNS("gmail.com")
59 t
.Errorf("failed: %s", err
)
62 t
.Errorf("no results")
66 func TestGmailTXT(t
*testing
.T
) {
67 if testing
.Short() ||
!*testExternal
{
68 t
.Skip("skipping test to avoid external network")
70 txt
, err
:= LookupTXT("gmail.com")
72 t
.Errorf("failed: %s", err
)
74 if len(txt
) == 0 ||
len(txt
[0]) == 0 {
75 t
.Errorf("no results")
79 func TestGoogleDNSAddr(t
*testing
.T
) {
80 if testing
.Short() ||
!*testExternal
{
81 t
.Skip("skipping test to avoid external network")
83 names
, err
:= LookupAddr("8.8.8.8")
85 t
.Errorf("failed: %s", err
)
88 t
.Errorf("no results")
92 func TestLookupIANACNAME(t
*testing
.T
) {
93 if testing
.Short() ||
!*testExternal
{
94 t
.Skip("skipping test to avoid external network")
96 cname
, err
:= LookupCNAME("www.iana.org")
97 if !strings
.HasSuffix(cname
, ".icann.org.") || err
!= nil {
98 t
.Errorf(`LookupCNAME("www.iana.org.") = %q, %v, want "*.icann.org.", nil`, cname
, err
)
102 var revAddrTests
= []struct {
107 {"1.2.3.4", "4.3.2.1.in-addr.arpa.", ""},
108 {"245.110.36.114", "114.36.110.245.in-addr.arpa.", ""},
109 {"::ffff:12.34.56.78", "78.56.34.12.in-addr.arpa.", ""},
110 {"::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.", ""},
111 {"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.", ""},
112 {"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.", ""},
113 {"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.", ""},
114 {"1.2.3", "", "unrecognized address"},
115 {"1.2.3.4.5", "", "unrecognized address"},
116 {"1234:567:bcbca::89a:bcde", "", "unrecognized address"},
117 {"1234:567::bcbc:adad::89a:bcde", "", "unrecognized address"},
120 func TestReverseAddress(t
*testing
.T
) {
121 for i
, tt
:= range revAddrTests
{
122 a
, err
:= reverseaddr(tt
.Addr
)
123 if len(tt
.ErrPrefix
) > 0 && err
== nil {
124 t
.Errorf("#%d: expected %q, got <nil> (error)", i
, tt
.ErrPrefix
)
127 if len(tt
.ErrPrefix
) == 0 && err
!= nil {
128 t
.Errorf("#%d: expected <nil>, got %q (error)", i
, err
)
130 if err
!= nil && err
.(*DNSError
).Err
!= tt
.ErrPrefix
{
131 t
.Errorf("#%d: expected %q, got %q (mismatched error)", i
, tt
.ErrPrefix
, err
.(*DNSError
).Err
)
134 t
.Errorf("#%d: expected %q, got %q (reverse address)", i
, tt
.Reverse
, a
)