1
0
mirror of https://github.com/golang/go synced 2024-11-18 23:34:45 -07:00

net: add more cases to lookup API test

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/126450043
This commit is contained in:
Mikio Hara 2014-08-29 12:28:31 +09:00
parent 597b266eaf
commit b930b433b0

View File

@ -15,77 +15,129 @@ import (
var testExternal = flag.Bool("external", true, "allow use of external networks during long test")
func TestGoogleSRV(t *testing.T) {
var lookupGoogleSRVTests = []struct {
service, proto, name string
cname, target string
}{
{
"xmpp-server", "tcp", "google.com",
".google.com", ".google.com",
},
{
"", "", "_xmpp-server._tcp.google.com", // non-standard back door
".google.com", ".google.com",
},
}
func TestLookupGoogleSRV(t *testing.T) {
if testing.Short() || !*testExternal {
t.Skip("skipping test to avoid external network")
}
_, addrs, err := LookupSRV("xmpp-server", "tcp", "google.com")
if err != nil {
t.Errorf("failed: %s", err)
}
if len(addrs) == 0 {
t.Errorf("no results")
}
// Non-standard back door.
_, addrs, err = LookupSRV("", "", "_xmpp-server._tcp.google.com")
if err != nil {
t.Errorf("back door failed: %s", err)
}
if len(addrs) == 0 {
t.Errorf("back door no results")
for _, tt := range lookupGoogleSRVTests {
cname, srvs, err := LookupSRV(tt.service, tt.proto, tt.name)
if err != nil {
t.Fatal(err)
}
if len(srvs) == 0 {
t.Error("got no record")
}
if !strings.Contains(cname, tt.cname) {
t.Errorf("got %q; want %q", cname, tt.cname)
}
for _, srv := range srvs {
if !strings.Contains(srv.Target, tt.target) {
t.Errorf("got %v; want a record containing %q", srv, tt.target)
}
}
}
}
func TestGmailMX(t *testing.T) {
func TestLookupGmailMX(t *testing.T) {
if testing.Short() || !*testExternal {
t.Skip("skipping test to avoid external network")
}
mx, err := LookupMX("gmail.com")
mxs, err := LookupMX("gmail.com")
if err != nil {
t.Errorf("failed: %s", err)
t.Fatal(err)
}
if len(mx) == 0 {
t.Errorf("no results")
if len(mxs) == 0 {
t.Error("got no record")
}
for _, mx := range mxs {
if !strings.Contains(mx.Host, ".google.com") {
t.Errorf("got %v; want a record containing .google.com.", mx)
}
}
}
func TestGmailNS(t *testing.T) {
func TestLookupGmailNS(t *testing.T) {
if testing.Short() || !*testExternal {
t.Skip("skipping test to avoid external network")
}
ns, err := LookupNS("gmail.com")
nss, err := LookupNS("gmail.com")
if err != nil {
t.Errorf("failed: %s", err)
t.Fatal(err)
}
if len(ns) == 0 {
t.Errorf("no results")
if len(nss) == 0 {
t.Error("got no record")
}
for _, ns := range nss {
if !strings.Contains(ns.Host, ".google.com") {
t.Errorf("got %v; want a record containing .google.com.", ns)
}
}
}
func TestGmailTXT(t *testing.T) {
func TestLookupGmailTXT(t *testing.T) {
if testing.Short() || !*testExternal {
t.Skip("skipping test to avoid external network")
}
txt, err := LookupTXT("gmail.com")
txts, err := LookupTXT("gmail.com")
if err != nil {
t.Errorf("failed: %s", err)
t.Fatal(err)
}
if len(txt) == 0 || len(txt[0]) == 0 {
t.Errorf("no results")
if len(txts) == 0 {
t.Error("got no record")
}
for _, txt := range txts {
if !strings.Contains(txt, "spf") {
t.Errorf("got %q; want a spf record", txt)
}
}
}
func TestGoogleDNSAddr(t *testing.T) {
var lookupGooglePublicDNSAddrs = []struct {
addr string
name string
}{
{"8.8.8.8", ".google.com."},
{"8.8.4.4", ".google.com."},
{"2001:4860:4860::8888", ".google.com."},
{"2001:4860:4860::8844", ".google.com."},
}
func TestLookupGooglePublicDNSAddr(t *testing.T) {
if testing.Short() || !*testExternal {
t.Skip("skipping test to avoid external network")
}
names, err := LookupAddr("8.8.8.8")
if err != nil {
t.Errorf("failed: %s", err)
}
if len(names) == 0 {
t.Errorf("no results")
for _, tt := range lookupGooglePublicDNSAddrs {
names, err := LookupAddr(tt.addr)
if err != nil {
t.Fatal(err)
}
if len(names) == 0 {
t.Error("got no record")
}
for _, name := range names {
if !strings.HasSuffix(name, tt.name) {
t.Errorf("got %q; want a record containing %q", name, tt.name)
}
}
}
}
@ -93,9 +145,51 @@ func TestLookupIANACNAME(t *testing.T) {
if testing.Short() || !*testExternal {
t.Skip("skipping test to avoid external network")
}
cname, err := LookupCNAME("www.iana.org")
if !strings.HasSuffix(cname, ".icann.org.") || err != nil {
t.Errorf(`LookupCNAME("www.iana.org.") = %q, %v, want "*.icann.org.", nil`, cname, err)
if err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(cname, ".icann.org.") {
t.Errorf("got %q; want a record containing .icann.org.", cname)
}
}
func TestLookupGoogleHost(t *testing.T) {
if testing.Short() || !*testExternal {
t.Skip("skipping test to avoid external network")
}
addrs, err := LookupHost("google.com")
if err != nil {
t.Fatal(err)
}
if len(addrs) == 0 {
t.Error("got no record")
}
for _, addr := range addrs {
if ParseIP(addr) == nil {
t.Errorf("got %q; want a literal ip address", addr)
}
}
}
func TestLookupGoogleIP(t *testing.T) {
if testing.Short() || !*testExternal {
t.Skip("skipping test to avoid external network")
}
ips, err := LookupIP("google.com")
if err != nil {
t.Fatal(err)
}
if len(ips) == 0 {
t.Error("got no record")
}
for _, ip := range ips {
if ip.To4() == nil && ip.To16() == nil {
t.Errorf("got %v; want an ip address", ip)
}
}
}