blob: 990ade9e2104243901c6204ecddd7e6c2bc8406b [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 {
Alex Brainman5da14d12011-08-05 10:27:51 +100020 t.Logf("skipping test to avoid external network")
21 return
22 }
23 _, addrs, err := LookupSRV("xmpp-server", "tcp", "google.com")
24 if err != nil {
25 t.Errorf("failed: %s", err)
26 }
27 if len(addrs) == 0 {
28 t.Errorf("no results")
29 }
Russ Cox48bb3e82011-10-18 13:57:04 -040030
31 // Non-standard back door.
32 _, addrs, err = LookupSRV("", "", "_xmpp-server._tcp.google.com")
33 if err != nil {
34 t.Errorf("back door failed: %s", err)
35 }
36 if len(addrs) == 0 {
37 t.Errorf("back door no results")
38 }
Alex Brainman5da14d12011-08-05 10:27:51 +100039}
40
41func TestGmailMX(t *testing.T) {
Mikio Haraadbadf42012-02-23 12:16:43 +090042 if testing.Short() || !*testExternal {
Alex Brainman5da14d12011-08-05 10:27:51 +100043 t.Logf("skipping test to avoid external network")
44 return
45 }
46 mx, err := LookupMX("gmail.com")
47 if err != nil {
48 t.Errorf("failed: %s", err)
49 }
50 if len(mx) == 0 {
51 t.Errorf("no results")
52 }
53}
54
Stephen McQuaya5b0c672012-10-18 15:39:04 +090055func TestGmailNS(t *testing.T) {
56 if testing.Short() || !*testExternal {
57 t.Logf("skipping test to avoid external network")
58 return
59 }
60 ns, err := LookupNS("gmail.com")
61 if err != nil {
62 t.Errorf("failed: %s", err)
63 }
64 if len(ns) == 0 {
65 t.Errorf("no results")
66 }
67}
68
Nigel Tao40d85fb2011-09-13 13:05:33 +100069func TestGmailTXT(t *testing.T) {
Mikio Haraadbadf42012-02-23 12:16:43 +090070 if testing.Short() || !*testExternal {
Nigel Tao40d85fb2011-09-13 13:05:33 +100071 t.Logf("skipping test to avoid external network")
72 return
73 }
74 txt, err := LookupTXT("gmail.com")
75 if err != nil {
76 t.Errorf("failed: %s", err)
77 }
78 if len(txt) == 0 || len(txt[0]) == 0 {
79 t.Errorf("no results")
80 }
81}
82
Alex Brainman5da14d12011-08-05 10:27:51 +100083func TestGoogleDNSAddr(t *testing.T) {
Mikio Haraadbadf42012-02-23 12:16:43 +090084 if testing.Short() || !*testExternal {
Alex Brainman5da14d12011-08-05 10:27:51 +100085 t.Logf("skipping test to avoid external network")
86 return
87 }
88 names, err := LookupAddr("8.8.8.8")
89 if err != nil {
90 t.Errorf("failed: %s", err)
91 }
92 if len(names) == 0 {
93 t.Errorf("no results")
94 }
95}
Mikio Hara705ebf12012-02-24 11:58:30 +090096
Mikio Harab7376442012-09-09 23:53:48 +090097func TestLookupIANACNAME(t *testing.T) {
98 if testing.Short() || !*testExternal {
99 t.Logf("skipping test to avoid external network")
100 return
101 }
102 cname, err := LookupCNAME("www.iana.org")
103 if !strings.HasSuffix(cname, ".icann.org.") || err != nil {
104 t.Errorf(`LookupCNAME("www.iana.org.") = %q, %v, want "*.icann.org.", nil`, cname, err)
105 }
106}
107
Mikio Hara705ebf12012-02-24 11:58:30 +0900108var revAddrTests = []struct {
109 Addr string
110 Reverse string
111 ErrPrefix string
112}{
113 {"1.2.3.4", "4.3.2.1.in-addr.arpa.", ""},
114 {"245.110.36.114", "114.36.110.245.in-addr.arpa.", ""},
115 {"::ffff:12.34.56.78", "78.56.34.12.in-addr.arpa.", ""},
116 {"::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.", ""},
117 {"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.", ""},
118 {"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.", ""},
119 {"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.", ""},
120 {"1.2.3", "", "unrecognized address"},
121 {"1.2.3.4.5", "", "unrecognized address"},
122 {"1234:567:bcbca::89a:bcde", "", "unrecognized address"},
123 {"1234:567::bcbc:adad::89a:bcde", "", "unrecognized address"},
124}
125
126func TestReverseAddress(t *testing.T) {
127 for i, tt := range revAddrTests {
128 a, err := reverseaddr(tt.Addr)
129 if len(tt.ErrPrefix) > 0 && err == nil {
130 t.Errorf("#%d: expected %q, got <nil> (error)", i, tt.ErrPrefix)
131 continue
132 }
133 if len(tt.ErrPrefix) == 0 && err != nil {
134 t.Errorf("#%d: expected <nil>, got %q (error)", i, err)
135 }
136 if err != nil && err.(*DNSError).Err != tt.ErrPrefix {
137 t.Errorf("#%d: expected %q, got %q (mismatched error)", i, tt.ErrPrefix, err.(*DNSError).Err)
138 }
139 if a != tt.Reverse {
140 t.Errorf("#%d: expected %q, got %q (reverse address)", i, tt.Reverse, a)
141 }
142 }
143}