OSDN Git Service

net, syscall: Use native endianness for GNU/Linux netlink code.
[pf3gnuchains/gcc-fork.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         "testing"
13 )
14
15 var testExternal = flag.Bool("external", true, "allow use of external networks during long test")
16
17 func TestGoogleSRV(t *testing.T) {
18         if testing.Short() || !*testExternal {
19                 t.Logf("skipping test to avoid external network")
20                 return
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.Logf("skipping test to avoid external network")
43                 return
44         }
45         mx, err := LookupMX("gmail.com")
46         if err != nil {
47                 t.Errorf("failed: %s", err)
48         }
49         if len(mx) == 0 {
50                 t.Errorf("no results")
51         }
52 }
53
54 func TestGmailTXT(t *testing.T) {
55         if testing.Short() || !*testExternal {
56                 t.Logf("skipping test to avoid external network")
57                 return
58         }
59         txt, err := LookupTXT("gmail.com")
60         if err != nil {
61                 t.Errorf("failed: %s", err)
62         }
63         if len(txt) == 0 || len(txt[0]) == 0 {
64                 t.Errorf("no results")
65         }
66 }
67
68 func TestGoogleDNSAddr(t *testing.T) {
69         if testing.Short() || !*testExternal {
70                 t.Logf("skipping test to avoid external network")
71                 return
72         }
73         names, err := LookupAddr("8.8.8.8")
74         if err != nil {
75                 t.Errorf("failed: %s", err)
76         }
77         if len(names) == 0 {
78                 t.Errorf("no results")
79         }
80 }
81
82 var revAddrTests = []struct {
83         Addr      string
84         Reverse   string
85         ErrPrefix string
86 }{
87         {"1.2.3.4", "4.3.2.1.in-addr.arpa.", ""},
88         {"245.110.36.114", "114.36.110.245.in-addr.arpa.", ""},
89         {"::ffff:12.34.56.78", "78.56.34.12.in-addr.arpa.", ""},
90         {"::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.", ""},
91         {"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.", ""},
92         {"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.", ""},
93         {"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.", ""},
94         {"1.2.3", "", "unrecognized address"},
95         {"1.2.3.4.5", "", "unrecognized address"},
96         {"1234:567:bcbca::89a:bcde", "", "unrecognized address"},
97         {"1234:567::bcbc:adad::89a:bcde", "", "unrecognized address"},
98 }
99
100 func TestReverseAddress(t *testing.T) {
101         for i, tt := range revAddrTests {
102                 a, err := reverseaddr(tt.Addr)
103                 if len(tt.ErrPrefix) > 0 && err == nil {
104                         t.Errorf("#%d: expected %q, got <nil> (error)", i, tt.ErrPrefix)
105                         continue
106                 }
107                 if len(tt.ErrPrefix) == 0 && err != nil {
108                         t.Errorf("#%d: expected <nil>, got %q (error)", i, err)
109                 }
110                 if err != nil && err.(*DNSError).Err != tt.ErrPrefix {
111                         t.Errorf("#%d: expected %q, got %q (mismatched error)", i, tt.ErrPrefix, err.(*DNSError).Err)
112                 }
113                 if a != tt.Reverse {
114                         t.Errorf("#%d: expected %q, got %q (reverse address)", i, tt.Reverse, a)
115                 }
116         }
117 }