added support for string type
[govpp.git] / vendor / github.com / google / gopacket / layers / udp_test.go
1 // Copyright 2012, Google, Inc. All rights reserved.
2 // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style license
5 // that can be found in the LICENSE file in the root of the source
6 // tree.
7
8 package layers
9
10 import (
11         "reflect"
12         "testing"
13
14         "github.com/google/gopacket"
15 )
16
17 // testUDPPacketDNS is the packet:
18 //   10:33:07.883637 IP 172.16.255.1.53 > 172.29.20.15.35181: 47320 7/0/0 MX ALT2.ASPMX.L.GOOGLE.com. 20, MX ASPMX2.GOOGLEMAIL.com. 30, MX ASPMX3.GOOGLEMAIL.com. 30, MX ASPMX4.GOOGLEMAIL.com. 30, MX ASPMX5.GOOGLEMAIL.com. 30, MX ASPMX.L.GOOGLE.com. 10, MX ALT1.ASPMX.L.GOOGLE.com. 20 (202)
19 //      0x0000:  24be 0527 0b17 001f cab3 75c0 0800 4500  $..'......u...E.
20 //      0x0010:  00e6 68cf 0000 3f11 a6f9 ac10 ff01 ac1d  ..h...?.........
21 //      0x0020:  140f 0035 896d 00d2 754a b8d8 8180 0001  ...5.m..uJ......
22 //      0x0030:  0007 0000 0000 0478 6b63 6403 636f 6d00  .......xkcd.com.
23 //      0x0040:  000f 0001 c00c 000f 0001 0000 0258 0018  .............X..
24 //      0x0050:  0014 0441 4c54 3205 4153 504d 5801 4c06  ...ALT2.ASPMX.L.
25 //      0x0060:  474f 4f47 4c45 c011 c00c 000f 0001 0000  GOOGLE..........
26 //      0x0070:  0258 0016 001e 0641 5350 4d58 320a 474f  .X.....ASPMX2.GO
27 //      0x0080:  4f47 4c45 4d41 494c c011 c00c 000f 0001  OGLEMAIL........
28 //      0x0090:  0000 0258 000b 001e 0641 5350 4d58 33c0  ...X.....ASPMX3.
29 //      0x00a0:  53c0 0c00 0f00 0100 0002 5800 0b00 1e06  S.........X.....
30 //      0x00b0:  4153 504d 5834 c053 c00c 000f 0001 0000  ASPMX4.S........
31 //      0x00c0:  0258 000b 001e 0641 5350 4d58 35c0 53c0  .X.....ASPMX5.S.
32 //      0x00d0:  0c00 0f00 0100 0002 5800 0400 0ac0 2dc0  ........X.....-.
33 //      0x00e0:  0c00 0f00 0100 0002 5800 0900 1404 414c  ........X.....AL
34 //      0x00f0:  5431 c02d                                T1.-
35 // Packet generated by doing DNS query for 'xkcd.com'
36 var testUDPPacketDNS = []byte{
37         0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x75, 0xc0, 0x08, 0x00, 0x45, 0x00,
38         0x00, 0xe6, 0x68, 0xcf, 0x00, 0x00, 0x3f, 0x11, 0xa6, 0xf9, 0xac, 0x10, 0xff, 0x01, 0xac, 0x1d,
39         0x14, 0x0f, 0x00, 0x35, 0x89, 0x6d, 0x00, 0xd2, 0x75, 0x4a, 0xb8, 0xd8, 0x81, 0x80, 0x00, 0x01,
40         0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x04, 0x78, 0x6b, 0x63, 0x64, 0x03, 0x63, 0x6f, 0x6d, 0x00,
41         0x00, 0x0f, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x02, 0x58, 0x00, 0x18,
42         0x00, 0x14, 0x04, 0x41, 0x4c, 0x54, 0x32, 0x05, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x01, 0x4c, 0x06,
43         0x47, 0x4f, 0x4f, 0x47, 0x4c, 0x45, 0xc0, 0x11, 0xc0, 0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00,
44         0x02, 0x58, 0x00, 0x16, 0x00, 0x1e, 0x06, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x32, 0x0a, 0x47, 0x4f,
45         0x4f, 0x47, 0x4c, 0x45, 0x4d, 0x41, 0x49, 0x4c, 0xc0, 0x11, 0xc0, 0x0c, 0x00, 0x0f, 0x00, 0x01,
46         0x00, 0x00, 0x02, 0x58, 0x00, 0x0b, 0x00, 0x1e, 0x06, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x33, 0xc0,
47         0x53, 0xc0, 0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x02, 0x58, 0x00, 0x0b, 0x00, 0x1e, 0x06,
48         0x41, 0x53, 0x50, 0x4d, 0x58, 0x34, 0xc0, 0x53, 0xc0, 0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00,
49         0x02, 0x58, 0x00, 0x0b, 0x00, 0x1e, 0x06, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x35, 0xc0, 0x53, 0xc0,
50         0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x02, 0x58, 0x00, 0x04, 0x00, 0x0a, 0xc0, 0x2d, 0xc0,
51         0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x02, 0x58, 0x00, 0x09, 0x00, 0x14, 0x04, 0x41, 0x4c,
52         0x54, 0x31, 0xc0, 0x2d,
53 }
54
55 func TestUDPPacketDNS(t *testing.T) {
56         p := gopacket.NewPacket(testUDPPacketDNS, LinkTypeEthernet, gopacket.Default)
57         if p.ErrorLayer() != nil {
58                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
59         }
60         checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeUDP, LayerTypeDNS}, t)
61         if got, ok := p.TransportLayer().(*UDP); ok {
62                 want := &UDP{
63                         BaseLayer: BaseLayer{
64                                 Contents: []byte{0x0, 0x35, 0x89, 0x6d, 0x0, 0xd2, 0x75, 0x4a},
65                                 Payload: []byte{0xb8, 0xd8, 0x81, 0x80, 0x0, 0x1, 0x0,
66                                         0x7, 0x0, 0x0, 0x0, 0x0, 0x4, 0x78, 0x6b, 0x63, 0x64, 0x3, 0x63, 0x6f,
67                                         0x6d, 0x0, 0x0, 0xf, 0x0, 0x1, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0,
68                                         0x2, 0x58, 0x0, 0x18, 0x0, 0x14, 0x4, 0x41, 0x4c, 0x54, 0x32, 0x5, 0x41,
69                                         0x53, 0x50, 0x4d, 0x58, 0x1, 0x4c, 0x6, 0x47, 0x4f, 0x4f, 0x47, 0x4c,
70                                         0x45, 0xc0, 0x11, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x58, 0x0,
71                                         0x16, 0x0, 0x1e, 0x6, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x32, 0xa, 0x47, 0x4f,
72                                         0x4f, 0x47, 0x4c, 0x45, 0x4d, 0x41, 0x49, 0x4c, 0xc0, 0x11, 0xc0, 0xc,
73                                         0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x58, 0x0, 0xb, 0x0, 0x1e, 0x6, 0x41,
74                                         0x53, 0x50, 0x4d, 0x58, 0x33, 0xc0, 0x53, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1,
75                                         0x0, 0x0, 0x2, 0x58, 0x0, 0xb, 0x0, 0x1e, 0x6, 0x41, 0x53, 0x50, 0x4d,
76                                         0x58, 0x34, 0xc0, 0x53, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2,
77                                         0x58, 0x0, 0xb, 0x0, 0x1e, 0x6, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x35, 0xc0,
78                                         0x53, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x58, 0x0, 0x4, 0x0,
79                                         0xa, 0xc0, 0x2d, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x58, 0x0,
80                                         0x9, 0x0, 0x14, 0x4, 0x41, 0x4c, 0x54, 0x31, 0xc0, 0x2d},
81                         },
82                         SrcPort:  53,
83                         DstPort:  35181,
84                         Length:   210,
85                         Checksum: 30026,
86                         sPort:    []byte{0x0, 0x35},
87                         dPort:    []byte{0x89, 0x6d},
88                 }
89                 if !reflect.DeepEqual(got, want) {
90                         t.Errorf("UDP packet mismatch:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
91                 }
92         } else {
93                 t.Error("Transport layer packet not UDP")
94         }
95 }
96
97 func loadDNS(dnspacket []byte, t *testing.T) *DNS {
98         p := gopacket.NewPacket(dnspacket, LinkTypeEthernet, gopacket.Default)
99         if p.ErrorLayer() != nil {
100                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
101         }
102         checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4,
103                 LayerTypeUDP, LayerTypeDNS}, t)
104
105         dnsL := p.Layer(LayerTypeDNS)
106         if dnsL == nil {
107                 t.Error("No DNS Layer found")
108         }
109
110         dns, ok := dnsL.(*DNS)
111         if !ok {
112                 return nil
113         }
114         return dns
115 }
116
117 var testDNSQueryA = []byte{
118         0xfe, 0x54, 0x00, 0x3e, 0x00, 0x96, 0x52, 0x54, /* .T.>..RT */
119         0x00, 0xbd, 0x1c, 0x70, 0x08, 0x00, 0x45, 0x00, /* ...p..E. */
120         0x00, 0x3c, 0x22, 0xe0, 0x00, 0x00, 0x40, 0x11, /* .<"...@. */
121         0xe2, 0x38, 0xc0, 0xa8, 0x7a, 0x46, 0xc0, 0xa8, /* .8..zF.. */
122         0x7a, 0x01, 0xc3, 0x35, 0x00, 0x35, 0x00, 0x28, /* z..5.5.( */
123         0x75, 0xd2, 0x52, 0x41, 0x01, 0x00, 0x00, 0x01, /* u.RA.... */
124         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x77, /* .......w */
125         0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, /* ww.googl */
126         0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, /* e.com... */
127         0x00, 0x01, /* .. */
128 }
129
130 func TestDNSQueryA(t *testing.T) {
131         dns := loadDNS(testDNSQueryA, t)
132         if dns == nil {
133                 t.Fatal("Failed to get a pointer to DNS struct")
134         }
135
136         if uint16(len(dns.Questions)) != dns.QDCount {
137                 t.Fatal("Invalid query decoding, not the right number of questions")
138         }
139
140         if string(dns.Questions[0].Name) != "www.google.com" {
141                 t.Errorf("Invalid query decoding, expecting 'www.google.com', got '%s'",
142                         dns.Questions[0].Name)
143         }
144         if dns.Questions[0].Class != DNSClassIN {
145                 t.Errorf("Invalid query decoding, expecting Class IN, got '%d'",
146                         dns.Questions[0].Class)
147         }
148
149         if dns.Questions[0].Type != DNSTypeA {
150                 t.Errorf("Invalid query decoding, expecting Type A, got '%d'",
151                         dns.Questions[0].Type)
152         }
153 }
154
155 var testDNSRRA = []byte{
156         0x52, 0x54, 0x00, 0xbd, 0x1c, 0x70, 0xfe, 0x54, /* RT...p.T */
157         0x00, 0x3e, 0x00, 0x96, 0x08, 0x00, 0x45, 0x00, /* .>....E. */
158         0x01, 0x24, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11, /* .$..@.@. */
159         0xc4, 0x30, 0xc0, 0xa8, 0x7a, 0x01, 0xc0, 0xa8, /* .0..z... */
160         0x7a, 0x46, 0x00, 0x35, 0xc3, 0x35, 0x01, 0x10, /* zF.5.5.. */
161         0x76, 0xba, 0x52, 0x41, 0x81, 0x80, 0x00, 0x01, /* v.RA.... */
162         0x00, 0x06, 0x00, 0x04, 0x00, 0x04, 0x03, 0x77, /* .......w */
163         0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, /* ww.googl */
164         0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, /* e.com... */
165         0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* ........ */
166         0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
167         0xc3, 0x67, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* .g...... */
168         0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
169         0xc3, 0x68, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* .h...... */
170         0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
171         0xc3, 0x69, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* .i...... */
172         0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
173         0xc3, 0x6a, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* .j...... */
174         0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
175         0xc3, 0x93, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* ........ */
176         0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
177         0xc3, 0x63, 0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, /* .c...... */
178         0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, /* .......n */
179         0x73, 0x32, 0xc0, 0x10, 0xc0, 0x10, 0x00, 0x02, /* s2...... */
180         0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, /* ........ */
181         0x03, 0x6e, 0x73, 0x33, 0xc0, 0x10, 0xc0, 0x10, /* .ns3.... */
182         0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
183         0x00, 0x06, 0x03, 0x6e, 0x73, 0x31, 0xc0, 0x10, /* ...ns1.. */
184         0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, /* ........ */
185         0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, 0x73, 0x34, /* .....ns4 */
186         0xc0, 0x10, 0xc0, 0xb0, 0x00, 0x01, 0x00, 0x01, /* ........ */
187         0x00, 0x02, 0xa3, 0x00, 0x00, 0x04, 0xd8, 0xef, /* ........ */
188         0x20, 0x0a, 0xc0, 0x8c, 0x00, 0x01, 0x00, 0x01, /*  ....... */
189         0x00, 0x02, 0xa3, 0x00, 0x00, 0x04, 0xd8, 0xef, /* ........ */
190         0x22, 0x0a, 0xc0, 0x9e, 0x00, 0x01, 0x00, 0x01, /* "....... */
191         0x00, 0x02, 0xa3, 0x00, 0x00, 0x04, 0xd8, 0xef, /* ........ */
192         0x24, 0x0a, 0xc0, 0xc2, 0x00, 0x01, 0x00, 0x01, /* $....... */
193         0x00, 0x02, 0xa3, 0x00, 0x00, 0x04, 0xd8, 0xef, /* ........ */
194         0x26, 0x0a, /* &. */
195 }
196
197 func TestDNSRRA(t *testing.T) {
198         dns := loadDNS(testDNSRRA, t)
199         if dns == nil {
200                 t.Fatal("Failed to get a pointer to DNS struct")
201                 return
202         }
203
204         if uint16(len(dns.Questions)) != dns.QDCount {
205                 t.Fatal("Invalid query decoding, not the right number of questions")
206         } else if uint16(len(dns.Answers)) != dns.ANCount {
207                 t.Fatal("Invalid query decoding, not the right number of answers")
208         } else if uint16(len(dns.Authorities)) != dns.NSCount {
209                 t.Fatal("Invalid query decoding, not the right number of authorities")
210         } else if uint16(len(dns.Additionals)) != dns.ARCount {
211                 t.Fatal("Invalid query decoding, not the right number of additionals info")
212         }
213
214         if string(dns.Questions[0].Name) != "www.google.com" {
215                 t.Errorf("Invalid query decoding, expecting 'www.google.com', got '%s'",
216                         dns.Questions[0].Name)
217         }
218         if string(dns.Answers[0].Name) != "www.google.com" {
219                 t.Errorf("Invalid query decoding, expecting 'www.google.com', got '%d'",
220                         dns.Questions[0].Class)
221         }
222         if dns.Answers[0].Class != DNSClassIN {
223                 t.Errorf("Invalid query decoding, expecting Class IN, got '%d'",
224                         dns.Questions[0].Class)
225         }
226         if dns.Answers[0].Type != DNSTypeA {
227                 t.Errorf("Invalid query decoding, expecting Type A, got '%d'",
228                         dns.Questions[0].Type)
229         }
230         if !dns.Answers[0].IP.Equal([]byte{74, 125, 195, 103}) {
231                 t.Errorf("Invalid query decoding, invalid IP address,"+
232                         " expecting '74.125.195.103', got '%s'",
233                         dns.Answers[0].IP.String())
234         }
235         if len(dns.Answers) != 6 {
236                 t.Errorf("No correct number of answers, expecting 6, go '%d'",
237                         len(dns.Answers))
238         }
239         if len(dns.Authorities) != 4 {
240                 t.Errorf("No correct number of answers, expecting 4, go '%d'",
241                         len(dns.Answers))
242         }
243         if len(dns.Additionals) != 4 {
244                 t.Errorf("No correct number of answers, expecting 4, go '%d'",
245                         len(dns.Answers))
246         } else {
247                 for i, want := range []string{
248                         "ns1.google.com",
249                         "ns2.google.com",
250                         "ns3.google.com",
251                         "ns4.google.com",
252                 } {
253                         if got := string(dns.Additionals[i].Name); got != want {
254                                 t.Errorf("got %q want %q", got, want)
255                         }
256                 }
257         }
258 }
259
260 var testDNSAAAA = []byte{
261         0x52, 0x54, 0x00, 0xbd, 0x1c, 0x70, 0xfe, 0x54, /* RT...p.T */
262         0x00, 0x3e, 0x00, 0x96, 0x08, 0x00, 0x45, 0x00, /* .>....E. */
263         0x00, 0xe0, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11, /* ....@.@. */
264         0xc4, 0x74, 0xc0, 0xa8, 0x7a, 0x01, 0xc0, 0xa8, /* .t..z... */
265         0x7a, 0x46, 0x00, 0x35, 0xdb, 0x13, 0x00, 0xcc, /* zF.5.... */
266         0x76, 0x76, 0xf3, 0x03, 0x81, 0x80, 0x00, 0x01, /* vv...... */
267         0x00, 0x01, 0x00, 0x04, 0x00, 0x04, 0x03, 0x77, /* .......w */
268         0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, /* ww.googl */
269         0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x1c, /* e.com... */
270         0x00, 0x01, 0xc0, 0x0c, 0x00, 0x1c, 0x00, 0x01, /* ........ */
271         0x00, 0x00, 0x01, 0x2c, 0x00, 0x10, 0x2a, 0x00, /* ...,..*. */
272         0x14, 0x50, 0x40, 0x0c, 0x0c, 0x01, 0x00, 0x00, /* .P@..... */
273         0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xc0, 0x10, /* .....i.. */
274         0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
275         0x00, 0x06, 0x03, 0x6e, 0x73, 0x34, 0xc0, 0x10, /* ...ns4.. */
276         0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, /* ........ */
277         0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, 0x73, 0x32, /* .....ns2 */
278         0xc0, 0x10, 0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, /* ........ */
279         0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, /* .......n */
280         0x73, 0x31, 0xc0, 0x10, 0xc0, 0x10, 0x00, 0x02, /* s1...... */
281         0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, /* ........ */
282         0x03, 0x6e, 0x73, 0x33, 0xc0, 0x10, 0xc0, 0x6c, /* .ns3...l */
283         0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
284         0x00, 0x04, 0xd8, 0xef, 0x20, 0x0a, 0xc0, 0x5a, /* .... ..Z */
285         0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
286         0x00, 0x04, 0xd8, 0xef, 0x22, 0x0a, 0xc0, 0x7e, /* ...."..~ */
287         0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
288         0x00, 0x04, 0xd8, 0xef, 0x24, 0x0a, 0xc0, 0x48, /* ....$..H */
289         0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
290         0x00, 0x04, 0xd8, 0xef, 0x26, 0x0a, /* ....&. */
291 }
292
293 func TestDNSAAAA(t *testing.T) {
294         dns := loadDNS(testDNSAAAA, t)
295         if dns == nil {
296                 t.Error("Failed to get a pointer to DNS struct")
297                 return
298         }
299
300         if len(dns.Questions) != 1 {
301                 t.Error("Invalid number of question")
302                 return
303         }
304         if dns.Questions[0].Type != DNSTypeAAAA {
305                 t.Errorf("Invalid question, Type is not AAAA, found %d",
306                         dns.Questions[0].Type)
307         }
308
309         if len(dns.Answers) != 1 {
310                 t.Error("Invalid number of answers")
311         }
312         if !dns.Answers[0].IP.Equal([]byte{0x2a, 0x00, 0x14, 0x50, 0x40,
313                 0x0c, 0x0c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69}) {
314                 t.Error("Invalid IP address, found ",
315                         dns.Answers[0].IP.String())
316         }
317 }
318
319 var testDNSMXSOA = []byte{
320         0x52, 0x54, 0x00, 0xbd, 0x1c, 0x70, 0xfe, 0x54, /* RT...p.T */
321         0x00, 0x3e, 0x00, 0x96, 0x08, 0x00, 0x45, 0x00, /* .>....E. */
322         0x00, 0x6e, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11, /* .n..@.@. */
323         0xc4, 0xe6, 0xc0, 0xa8, 0x7a, 0x01, 0xc0, 0xa8, /* ....z... */
324         0x7a, 0x46, 0x00, 0x35, 0x9c, 0x60, 0x00, 0x5a, /* zF.5.`.Z */
325         0x76, 0x04, 0xfc, 0x7a, 0x81, 0x80, 0x00, 0x01, /* v..z.... */
326         0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0x77, /* .......w */
327         0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, /* ww.googl */
328         0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x0f, /* e.com... */
329         0x00, 0x01, 0xc0, 0x10, 0x00, 0x06, 0x00, 0x01, /* ........ */
330         0x00, 0x00, 0x00, 0x3c, 0x00, 0x26, 0x03, 0x6e, /* ...<.&.n */
331         0x73, 0x31, 0xc0, 0x10, 0x09, 0x64, 0x6e, 0x73, /* s1...dns */
332         0x2d, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0xc0, 0x10, /* -admin.. */
333         0x00, 0x17, 0x9f, 0x64, 0x00, 0x00, 0x1c, 0x20, /* ...d...  */
334         0x00, 0x00, 0x07, 0x08, 0x00, 0x12, 0x75, 0x00, /* ......u. */
335         0x00, 0x00, 0x01, 0x2c, /* ..., */
336 }
337
338 func TestDNSMXSOA(t *testing.T) {
339         dns := loadDNS(testDNSMXSOA, t)
340         if dns == nil {
341                 t.Error("Failed to get a pointer to DNS struct")
342                 return
343         }
344
345         if len(dns.Authorities) != 1 {
346                 t.Error("Invalid number of authoritative answers, found ",
347                         len(dns.Authorities))
348                 return
349         }
350 }
351
352 func BenchmarkDecodeDNS(b *testing.B) {
353         for i := 0; i < b.N; i++ {
354                 gopacket.NewPacket(testDNSQueryA, LinkTypeEthernet, gopacket.NoCopy)
355         }
356 }
357 func BenchmarkDecodeDNSLayer(b *testing.B) {
358         var dns DNS
359         for i := 0; i < b.N; i++ {
360                 dns.DecodeFromBytes(testDNSAAAA[ /*eth*/ 14+ /*ipv4*/ 20+ /*udp*/ 8:], gopacket.NilDecodeFeedback)
361         }
362 }
363 func TestDNSDoesNotMalloc(t *testing.T) {
364         var dns DNS
365         if n := testing.AllocsPerRun(1000, func() {
366                 if err := dns.DecodeFromBytes(testDNSAAAA[ /*eth*/ 14+ /*ipv4*/ 20+ /*udp*/ 8:], gopacket.NilDecodeFeedback); err != nil {
367                         t.Fatal(err)
368                 }
369         }); n > 0 {
370                 t.Error(n, "mallocs decoding DNS")
371         }
372 }