blob: 3355e4694894974fc1d23a3ffb407c4adcb31027 [file] [log] [blame]
Alex Brainman5da14d12011-08-05 10:27:51 +10001// 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
8package net
9
10import (
Mikio Haraadbadf42012-02-23 12:16:43 +090011 "flag"
Mikio Harab7376442012-09-09 23:53:48 +090012 "strings"
Alex Brainman5da14d12011-08-05 10:27:51 +100013 "testing"
14)
15
Mikio Hara73010652012-03-01 07:39:03 +090016var testExternal = flag.Bool("external", true, "allow use of external networks during long test")
Alex Brainman5da14d12011-08-05 10:27:51 +100017
18func TestGoogleSRV(t *testing.T) {
Mikio Haraadbadf42012-02-23 12:16:43 +090019 if testing.Short() || !*testExternal {
Dave Cheney6a9e9562013-01-24 17:32:10 +110020 t.Skip("skipping test to avoid external network")
Alex Brainman5da14d12011-08-05 10:27:51 +100021 }
22 _, addrs, err := LookupSRV("xmpp-server", "tcp", "google.com")
23 if err != nil {
24 t.Errorf("failed: %s", err)
25 }
26 if len(addrs) == 0 {
27 t.Errorf("no results")
28 }
Russ Cox48bb3e82011-10-18 13:57:04 -040029
30 // Non-standard back door.
31 _, addrs, err = LookupSRV("", "", "_xmpp-server._tcp.google.com")
32 if err != nil {
33 t.Errorf("back door failed: %s", err)
34 }
35 if len(addrs) == 0 {
36 t.Errorf("back door no results")
37 }
Alex Brainman5da14d12011-08-05 10:27:51 +100038}
39
40func TestGmailMX(t *testing.T) {
Mikio Haraadbadf42012-02-23 12:16:43 +090041 if testing.Short() || !*testExternal {
Dave Cheney6a9e9562013-01-24 17:32:10 +110042 t.Skip("skipping test to avoid external network")
Alex Brainman5da14d12011-08-05 10:27:51 +100043 }
44 mx, err := LookupMX("gmail.com")
45 if err != nil {
46 t.Errorf("failed: %s", err)
47 }
48 if len(mx) == 0 {
49 t.Errorf("no results")
50 }
51}
52
Stephen McQuaya5b0c672012-10-18 15:39:04 +090053func TestGmailNS(t *testing.T) {
54 if testing.Short() || !*testExternal {
Dave Cheney6a9e9562013-01-24 17:32:10 +110055 t.Skip("skipping test to avoid external network")
Stephen McQuaya5b0c672012-10-18 15:39:04 +090056 }
57 ns, err := LookupNS("gmail.com")
58 if err != nil {
59 t.Errorf("failed: %s", err)
60 }
61 if len(ns) == 0 {
62 t.Errorf("no results")
63 }
64}
65
Nigel Tao40d85fb2011-09-13 13:05:33 +100066func TestGmailTXT(t *testing.T) {
Mikio Haraadbadf42012-02-23 12:16:43 +090067 if testing.Short() || !*testExternal {
Dave Cheney6a9e9562013-01-24 17:32:10 +110068 t.Skip("skipping test to avoid external network")
Nigel Tao40d85fb2011-09-13 13:05:33 +100069 }
70 txt, err := LookupTXT("gmail.com")
71 if err != nil {
72 t.Errorf("failed: %s", err)
73 }
74 if len(txt) == 0 || len(txt[0]) == 0 {
75 t.Errorf("no results")
76 }
77}
78
Alex Brainman5da14d12011-08-05 10:27:51 +100079func TestGoogleDNSAddr(t *testing.T) {
Mikio Haraadbadf42012-02-23 12:16:43 +090080 if testing.Short() || !*testExternal {
Dave Cheney6a9e9562013-01-24 17:32:10 +110081 t.Skip("skipping test to avoid external network")
Alex Brainman5da14d12011-08-05 10:27:51 +100082 }
83 names, err := LookupAddr("8.8.8.8")
84 if err != nil {
85 t.Errorf("failed: %s", err)
86 }
87 if len(names) == 0 {
88 t.Errorf("no results")
89 }
90}
Mikio Hara705ebf12012-02-24 11:58:30 +090091
Mikio Harab7376442012-09-09 23:53:48 +090092func TestLookupIANACNAME(t *testing.T) {
93 if testing.Short() || !*testExternal {
Dave Cheney6a9e9562013-01-24 17:32:10 +110094 t.Skip("skipping test to avoid external network")
Mikio Harab7376442012-09-09 23:53:48 +090095 }
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)
99 }
100}
101
Mikio Hara705ebf12012-02-24 11:58:30 +0900102var revAddrTests = []struct {
103 Addr string
104 Reverse string
105 ErrPrefix string
106}{
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"},
118}
119
120func 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)
125 continue
126 }
127 if len(tt.ErrPrefix) == 0 && err != nil {
128 t.Errorf("#%d: expected <nil>, got %q (error)", i, err)
129 }
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)
132 }
133 if a != tt.Reverse {
134 t.Errorf("#%d: expected %q, got %q (reverse address)", i, tt.Reverse, a)
135 }
136 }
137}