diff options
Diffstat (limited to 'libgo/go/net/dnsmsg_test.go')
-rw-r--r-- | libgo/go/net/dnsmsg_test.go | 219 |
1 files changed, 217 insertions, 2 deletions
diff --git a/libgo/go/net/dnsmsg_test.go b/libgo/go/net/dnsmsg_test.go index 1078d77ceb..2a25a21d4c 100644 --- a/libgo/go/net/dnsmsg_test.go +++ b/libgo/go/net/dnsmsg_test.go @@ -10,6 +10,103 @@ import ( "testing" ) +func TestStructPackUnpack(t *testing.T) { + want := dnsQuestion{ + Name: ".", + Qtype: dnsTypeA, + Qclass: dnsClassINET, + } + buf := make([]byte, 50) + n, ok := packStruct(&want, buf, 0) + if !ok { + t.Fatal("packing failed") + } + buf = buf[:n] + got := dnsQuestion{} + n, ok = unpackStruct(&got, buf, 0) + if !ok { + t.Fatal("unpacking failed") + } + if n != len(buf) { + t.Errorf("unpacked different amount than packed: got n = %d, want = %d", n, len(buf)) + } + if !reflect.DeepEqual(got, want) { + t.Errorf("got = %+v, want = %+v", got, want) + } +} + +func TestDomainNamePackUnpack(t *testing.T) { + tests := []struct { + in string + want string + ok bool + }{ + {"", ".", true}, + {".", ".", true}, + {"google..com", "", false}, + {"google.com", "google.com.", true}, + {"google..com.", "", false}, + {"google.com.", "google.com.", true}, + {".google.com.", "", false}, + {"www..google.com.", "", false}, + {"www.google.com.", "www.google.com.", true}, + } + + for _, test := range tests { + buf := make([]byte, 30) + n, ok := packDomainName(test.in, buf, 0) + if ok != test.ok { + t.Errorf("packing of %s: got ok = %t, want = %t", test.in, ok, test.ok) + continue + } + if !test.ok { + continue + } + buf = buf[:n] + got, n, ok := unpackDomainName(buf, 0) + if !ok { + t.Errorf("unpacking for %s failed", test.in) + continue + } + if n != len(buf) { + t.Errorf( + "unpacked different amount than packed for %s: got n = %d, want = %d", + test.in, + n, + len(buf), + ) + } + if got != test.want { + t.Errorf("unpacking packing of %s: got = %s, want = %s", test.in, got, test.want) + } + } +} + +func TestDNSPackUnpack(t *testing.T) { + want := dnsMsg{ + question: []dnsQuestion{{ + Name: ".", + Qtype: dnsTypeAAAA, + Qclass: dnsClassINET, + }}, + answer: []dnsRR{}, + ns: []dnsRR{}, + extra: []dnsRR{}, + } + b, ok := want.Pack() + if !ok { + t.Fatal("packing failed") + } + var got dnsMsg + ok = got.Unpack(b) + if !ok { + t.Fatal("unpacking failed") + } + if !reflect.DeepEqual(got, want) { + t.Errorf("got = %+v, want = %+v", got, want) + } +} + func TestDNSParseSRVReply(t *testing.T) { data, err := hex.DecodeString(dnsSRVReply) if err != nil { @@ -20,7 +117,7 @@ func TestDNSParseSRVReply(t *testing.T) { if !ok { t.Fatal("unpacking packet failed") } - msg.String() // exercise this code path + _ = msg.String() // exercise this code path if g, e := len(msg.answer), 5; g != e { t.Errorf("len(msg.answer) = %d; want %d", g, e) } @@ -68,7 +165,7 @@ func TestDNSParseCorruptSRVReply(t *testing.T) { if !ok { t.Fatal("unpacking packet failed") } - msg.String() // exercise this code path + _ = msg.String() // exercise this code path if g, e := len(msg.answer), 5; g != e { t.Errorf("len(msg.answer) = %d; want %d", g, e) } @@ -183,6 +280,124 @@ func TestDNSParseTXTCorruptTXTLengthReply(t *testing.T) { } } +func TestIsResponseTo(t *testing.T) { + // Sample DNS query. + query := dnsMsg{ + dnsMsgHdr: dnsMsgHdr{ + id: 42, + }, + question: []dnsQuestion{ + { + Name: "www.example.com.", + Qtype: dnsTypeA, + Qclass: dnsClassINET, + }, + }, + } + + resp := query + resp.response = true + if !resp.IsResponseTo(&query) { + t.Error("got false, want true") + } + + badResponses := []dnsMsg{ + // Different ID. + { + dnsMsgHdr: dnsMsgHdr{ + id: 43, + response: true, + }, + question: []dnsQuestion{ + { + Name: "www.example.com.", + Qtype: dnsTypeA, + Qclass: dnsClassINET, + }, + }, + }, + + // Different query name. + { + dnsMsgHdr: dnsMsgHdr{ + id: 42, + response: true, + }, + question: []dnsQuestion{ + { + Name: "www.google.com.", + Qtype: dnsTypeA, + Qclass: dnsClassINET, + }, + }, + }, + + // Different query type. + { + dnsMsgHdr: dnsMsgHdr{ + id: 42, + response: true, + }, + question: []dnsQuestion{ + { + Name: "www.example.com.", + Qtype: dnsTypeAAAA, + Qclass: dnsClassINET, + }, + }, + }, + + // Different query class. + { + dnsMsgHdr: dnsMsgHdr{ + id: 42, + response: true, + }, + question: []dnsQuestion{ + { + Name: "www.example.com.", + Qtype: dnsTypeA, + Qclass: dnsClassCSNET, + }, + }, + }, + + // No questions. + { + dnsMsgHdr: dnsMsgHdr{ + id: 42, + response: true, + }, + }, + + // Extra questions. + { + dnsMsgHdr: dnsMsgHdr{ + id: 42, + response: true, + }, + question: []dnsQuestion{ + { + Name: "www.example.com.", + Qtype: dnsTypeA, + Qclass: dnsClassINET, + }, + { + Name: "www.golang.org.", + Qtype: dnsTypeAAAA, + Qclass: dnsClassINET, + }, + }, + }, + } + + for i := range badResponses { + if badResponses[i].IsResponseTo(&query) { + t.Errorf("%v: got true, want false", i) + } + } +} + // Valid DNS SRV reply const dnsSRVReply = "0901818000010005000000000c5f786d70702d736572766572045f74637006676f6f67" + "6c6503636f6d0000210001c00c002100010000012c00210014000014950c786d70702d" + |