1 // Copyright 2011 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.
13 func TestDNSParseSRVReply(t
*testing
.T
) {
14 data
, err
:= hex
.DecodeString(dnsSRVReply
)
19 ok
:= msg
.Unpack(data
)
21 t
.Fatalf("unpacking packet failed")
23 msg
.String() // exercise this code path
24 if g
, e
:= len(msg
.answer
), 5; g
!= e
{
25 t
.Errorf("len(msg.answer) = %d; want %d", g
, e
)
27 for idx
, rr
:= range msg
.answer
{
28 if g
, e
:= rr
.Header().Rrtype
, uint16(dnsTypeSRV
); g
!= e
{
29 t
.Errorf("rr[%d].Header().Rrtype = %d; want %d", idx
, g
, e
)
31 if _
, ok
:= rr
.(*dnsRR_SRV
); !ok
{
32 t
.Errorf("answer[%d] = %T; want *dnsRR_SRV", idx
, rr
)
35 _
, addrs
, err
:= answer("_xmpp-server._tcp.google.com.", "foo:53", msg
, uint16(dnsTypeSRV
))
37 t
.Fatalf("answer: %v", err
)
39 if g
, e
:= len(addrs
), 5; g
!= e
{
40 t
.Errorf("len(addrs) = %d; want %d", g
, e
)
41 t
.Logf("addrs = %#v", addrs
)
44 data2
, ok
:= msg
.Pack()
49 t
.Errorf("failed to repack message")
50 case !reflect
.DeepEqual(msg
, msg2
):
51 t
.Errorf("repacked message differs from original")
55 func TestDNSParseCorruptSRVReply(t
*testing
.T
) {
56 data
, err
:= hex
.DecodeString(dnsSRVCorruptReply
)
61 ok
:= msg
.Unpack(data
)
63 t
.Fatalf("unpacking packet failed")
65 msg
.String() // exercise this code path
66 if g
, e
:= len(msg
.answer
), 5; g
!= e
{
67 t
.Errorf("len(msg.answer) = %d; want %d", g
, e
)
69 for idx
, rr
:= range msg
.answer
{
70 if g
, e
:= rr
.Header().Rrtype
, uint16(dnsTypeSRV
); g
!= e
{
71 t
.Errorf("rr[%d].Header().Rrtype = %d; want %d", idx
, g
, e
)
74 if _
, ok
:= rr
.(*dnsRR_Header
); !ok
{
75 t
.Errorf("answer[%d] = %T; want *dnsRR_Header", idx
, rr
)
78 if _
, ok
:= rr
.(*dnsRR_SRV
); !ok
{
79 t
.Errorf("answer[%d] = %T; want *dnsRR_SRV", idx
, rr
)
83 _
, addrs
, err
:= answer("_xmpp-server._tcp.google.com.", "foo:53", msg
, uint16(dnsTypeSRV
))
85 t
.Fatalf("answer: %v", err
)
87 if g
, e
:= len(addrs
), 4; g
!= e
{
88 t
.Errorf("len(addrs) = %d; want %d", g
, e
)
89 t
.Logf("addrs = %#v", addrs
)
93 // Valid DNS SRV reply
94 const dnsSRVReply
= "0901818000010005000000000c5f786d70702d736572766572045f74637006676f6f67" +
95 "6c6503636f6d0000210001c00c002100010000012c00210014000014950c786d70702d" +
96 "73657276657234016c06676f6f676c6503636f6d00c00c002100010000012c00210014" +
97 "000014950c786d70702d73657276657232016c06676f6f676c6503636f6d00c00c0021" +
98 "00010000012c00210014000014950c786d70702d73657276657233016c06676f6f676c" +
99 "6503636f6d00c00c002100010000012c00200005000014950b786d70702d7365727665" +
100 "72016c06676f6f676c6503636f6d00c00c002100010000012c00210014000014950c78" +
101 "6d70702d73657276657231016c06676f6f676c6503636f6d00"
103 // Corrupt DNS SRV reply, with its final RR having a bogus length
104 // (perhaps it was truncated, or it's malicious) The mutation is the
105 // capital "FF" below, instead of the proper "21".
106 const dnsSRVCorruptReply
= "0901818000010005000000000c5f786d70702d736572766572045f74637006676f6f67" +
107 "6c6503636f6d0000210001c00c002100010000012c00210014000014950c786d70702d" +
108 "73657276657234016c06676f6f676c6503636f6d00c00c002100010000012c00210014" +
109 "000014950c786d70702d73657276657232016c06676f6f676c6503636f6d00c00c0021" +
110 "00010000012c00210014000014950c786d70702d73657276657233016c06676f6f676c" +
111 "6503636f6d00c00c002100010000012c00200005000014950b786d70702d7365727665" +
112 "72016c06676f6f676c6503636f6d00c00c002100010000012c00FF0014000014950c78" +
113 "6d70702d73657276657231016c06676f6f676c6503636f6d00"