Commit b930b433 authored by Mikio Hara's avatar Mikio Hara

net: add more cases to lookup API test

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/126450043
parent 597b266e
......@@ -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")
for _, tt := range lookupGoogleSRVTests {
cname, srvs, err := LookupSRV(tt.service, tt.proto, tt.name)
if err != nil {
t.Errorf("failed: %s", err)
t.Fatal(err)
}
if len(addrs) == 0 {
t.Errorf("no results")
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)
}
// 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")
}
}
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(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)
}
if len(mx) == 0 {
t.Errorf("no results")
}
}
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(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)
}
if len(ns) == 0 {
t.Errorf("no results")
}
}
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(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)
}
if len(txt) == 0 || len(txt[0]) == 0 {
t.Errorf("no results")
}
}
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")
for _, tt := range lookupGooglePublicDNSAddrs {
names, err := LookupAddr(tt.addr)
if err != nil {
t.Errorf("failed: %s", err)
t.Fatal(err)
}
if len(names) == 0 {
t.Errorf("no results")
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)
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment