3355e4694894974fc1d23a3ffb407c4adcb31027
[platform/upstream/gcc48.git] / libgo / go / net / lookup_test.go
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 func TestGoogleSRV(t *testing.T) {
19         if testing.Short() || !*testExternal {
20                 t.Skip("skipping test to avoid external network")
21         }
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         }
29
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         }
38 }
39
40 func TestGmailMX(t *testing.T) {
41         if testing.Short() || !*testExternal {
42                 t.Skip("skipping test to avoid external network")
43         }
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
53 func TestGmailNS(t *testing.T) {
54         if testing.Short() || !*testExternal {
55                 t.Skip("skipping test to avoid external network")
56         }
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
66 func TestGmailTXT(t *testing.T) {
67         if testing.Short() || !*testExternal {
68                 t.Skip("skipping test to avoid external network")
69         }
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
79 func TestGoogleDNSAddr(t *testing.T) {
80         if testing.Short() || !*testExternal {
81                 t.Skip("skipping test to avoid external network")
82         }
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 }
91
92 func TestLookupIANACNAME(t *testing.T) {
93         if testing.Short() || !*testExternal {
94                 t.Skip("skipping test to avoid external network")
95         }
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
102 var 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
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)
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 }