b0299fc629538ea48560f4c293f5880dcc3da363
[govpp.git] / vendor / github.com / google / gopacket / layers / dot11_test.go
1 // Copyright 2014, Google, Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree.
6
7 package layers
8
9 import (
10         "bytes"
11         "net"
12         "reflect"
13         "testing"
14
15         "github.com/google/gopacket"
16 )
17
18 // Generator: python layers/test_creator.py --layerType=LayerTypeRadioTap --linkType=LinkTypeIEEE80211Radio --name=Dot11%s ~/Downloads/mesh.pcap
19 // http://wiki.wireshark.org/SampleCaptures#Sample_Captures
20
21 // testPacketDot11CtrlCTS is the packet:
22 //   09:28:41.830560 20604983us tsft short preamble 24.0 Mb/s 5240 MHz 11a -79dB signal -92dB noise antenna 1 Clear-To-Send RA:d8:a2:5e:97:61:c1
23 //      0x0000:  0000 1900 6f08 0000 3768 3a01 0000 0000  ....o...7h:.....
24 //      0x0010:  1230 7814 4001 b1a4 01c4 0094 00d8 a25e  .0x.@..........^
25 //      0x0020:  9761 c136 5095 8e                        .a.6P..
26
27 var testPacketDot11CtrlCTS = []byte{
28         0x00, 0x00, 0x19, 0x00, 0x6f, 0x08, 0x00, 0x00, 0x37, 0x68, 0x3a, 0x01, 0x00, 0x00, 0x00, 0x00,
29         0x12, 0x30, 0x78, 0x14, 0x40, 0x01, 0xb1, 0xa4, 0x01, 0xc4, 0x00, 0x94, 0x00, 0xd8, 0xa2, 0x5e,
30         0x97, 0x61, 0xc1, 0x36, 0x50, 0x95, 0x8e,
31 }
32
33 func TestPacketDot11CtrlCTS(t *testing.T) {
34         p := gopacket.NewPacket(testPacketDot11CtrlCTS, LinkTypeIEEE80211Radio, gopacket.Default)
35         if p.ErrorLayer() != nil {
36                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
37         }
38         checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11}, t)
39
40         if got, ok := p.Layer(LayerTypeRadioTap).(*RadioTap); ok {
41                 want := &RadioTap{
42                         BaseLayer: BaseLayer{
43                                 Contents: []uint8{0x0, 0x0, 0x19, 0x0, 0x6f, 0x8, 0x0, 0x0, 0x37, 0x68, 0x3a, 0x1, 0x0, 0x0, 0x0, 0x0, 0x12, 0x30, 0x78, 0x14, 0x40, 0x1, 0xb1, 0xa4, 0x1},
44                                 Payload:  []uint8{0xc4, 0x0, 0x94, 0x0, 0xd8, 0xa2, 0x5e, 0x97, 0x61, 0xc1, 0x36, 0x50, 0x95, 0x8e},
45                         },
46                         Version:          0x0,
47                         Length:           0x19,
48                         Present:          0x86f,
49                         TSFT:             0x13a6837,
50                         Flags:            0x12,
51                         Rate:             0x30,
52                         ChannelFrequency: 0x1478,
53                         ChannelFlags:     0x140,
54                         FHSS:             0x0,
55                         DBMAntennaSignal: -79,
56                         DBMAntennaNoise:  -92,
57                         LockQuality:      0x0,
58                         TxAttenuation:    0x0,
59                         DBTxAttenuation:  0x0,
60                         DBMTxPower:       0,
61                         Antenna:          1,
62                         DBAntennaSignal:  0x0,
63                         DBAntennaNoise:   0x0,
64                 }
65
66                 if !reflect.DeepEqual(got, want) {
67                         t.Errorf("RadioTap packet processing failed:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
68                 }
69         }
70
71         if got, ok := p.Layer(LayerTypeDot11).(*Dot11); ok {
72                 if !got.ChecksumValid() {
73                         t.Errorf("Dot11 packet processing failed:\nchecksum failed. got  :\n%#v\n\n", got)
74                 }
75
76                 want := &Dot11{
77                         BaseLayer: BaseLayer{
78                                 Contents: []uint8{0xc4, 0x0, 0x94, 0x0, 0xd8, 0xa2, 0x5e, 0x97, 0x61, 0xc1},
79                                 Payload:  []uint8{},
80                         },
81                         Type:       Dot11TypeCtrlCTS,
82                         Proto:      0x0,
83                         Flags:      0x0,
84                         DurationID: 0x94,
85                         Address1:   net.HardwareAddr{0xd8, 0xa2, 0x5e, 0x97, 0x61, 0xc1}, // check
86                         Address2:   net.HardwareAddr(nil),
87                         Address3:   net.HardwareAddr(nil),
88                         Address4:   net.HardwareAddr(nil),
89                         Checksum:   0x8e955036,
90                 }
91
92                 if !reflect.DeepEqual(got, want) {
93                         t.Errorf("Dot11 packet processing failed:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
94                 }
95         }
96 }
97
98 func BenchmarkDecodePacketDot11CtrlCTS(b *testing.B) {
99         for i := 0; i < b.N; i++ {
100                 gopacket.NewPacket(testPacketDot11CtrlCTS, LinkTypeIEEE80211Radio, gopacket.NoCopy)
101         }
102 }
103
104 // testPacketDot11MgmtBeacon is the packet:
105 //   15:44:56.531833 6.0 Mb/s 2412 MHz 11g -81dB signal antenna 5 Beacon (Wi2) [6.0* 9.0 12.0* 18.0 24.0* 36.0 48.0 54.0 Mbit] ESS CH: 1
106 //      0x0000:  0000 1200 2e48 0000 100c 6c09 c000 af05  .....H....l.....
107 //      0x0010:  0000 8000 0000 ffff ffff ffff c08a de01  ................
108 //      0x0020:  11b8 c08a de01 11b8 f097 80f1 30bc 1300  ............0...
109 //      0x0030:  0000 6400 2104 0003 5769 3201 088c 1298  ..d.!...Wi2.....
110 //      0x0040:  24b0 4860 6c03 0101 0504 0001 0000 2a01  $.H`l.........*.
111 //      0x0050:  00dd 1800 50f2 0201 0181 0007 a400 0023  ....P..........#
112 //      0x0060:  a400 0042 435e 0062 322f 00dd 1e00 904c  ...BC^.b2/.....L
113 //      0x0070:  338c 011b ffff 0000 0000 0000 0000 0000  3...............
114 //      0x0080:  1000 0000 0000 0000 0000 002d 1a8c 011b  ...........-....
115 //      0x0090:  ffff 0000 0000 0000 0000 0000 1000 0000  ................
116 //      0x00a0:  0000 0000 0000 00dd 1a00 904c 3401 0000  ...........L4...
117 //      0x00b0:  0000 0000 0000 0000 0000 0000 0000 0000  ................
118 //      0x00c0:  0000 003d 1601 0000 0000 0000 0000 0000  ...=............
119 //      0x00d0:  0000 0000 0000 0000 0000 007f 0400 0000  ................
120 //      0x00e0:  00dd 0800 1392 0100 0185 0094 0b90 15    ...............
121 var testPacketDot11MgmtBeacon = []byte{
122         0x00, 0x00, 0x12, 0x00, 0x2e, 0x48, 0x00, 0x00, 0x10, 0x0c, 0x6c, 0x09, 0xc0, 0x00, 0xaf, 0x05,
123         0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x8a, 0xde, 0x01,
124         0x11, 0xb8, 0xc0, 0x8a, 0xde, 0x01, 0x11, 0xb8, 0xf0, 0x97, 0x80, 0xf1, 0x30, 0xbc, 0x13, 0x00,
125         0x00, 0x00, 0x64, 0x00, 0x21, 0x04, 0x00, 0x03, 0x57, 0x69, 0x32, 0x01, 0x08, 0x8c, 0x12, 0x98,
126         0x24, 0xb0, 0x48, 0x60, 0x6c, 0x03, 0x01, 0x01, 0x05, 0x04, 0x00, 0x01, 0x00, 0x00, 0x2a, 0x01,
127         0x00, 0xdd, 0x18, 0x00, 0x50, 0xf2, 0x02, 0x01, 0x01, 0x81, 0x00, 0x07, 0xa4, 0x00, 0x00, 0x23,
128         0xa4, 0x00, 0x00, 0x42, 0x43, 0x5e, 0x00, 0x62, 0x32, 0x2f, 0x00, 0xdd, 0x1e, 0x00, 0x90, 0x4c,
129         0x33, 0x8c, 0x01, 0x1b, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130         0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x1a, 0x8c, 0x01, 0x1b,
131         0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
132         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdd, 0x1a, 0x00, 0x90, 0x4c, 0x34, 0x01, 0x00, 0x00,
133         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134         0x00, 0x00, 0x00, 0x3d, 0x16, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x04, 0x00, 0x00, 0x00,
136         0x00, 0xdd, 0x08, 0x00, 0x13, 0x92, 0x01, 0x00, 0x01, 0x85, 0x00, 0x94, 0x0b, 0x90, 0x15,
137 }
138
139 func TestPacketDot11MgmtBeacon(t *testing.T) {
140         p := gopacket.NewPacket(testPacketDot11MgmtBeacon, LinkTypeIEEE80211Radio, gopacket.Default)
141         if p.ErrorLayer() != nil {
142                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
143         }
144         expectedLayers := []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11MgmtBeacon}
145         for i := 0; i < 12; i++ {
146                 expectedLayers = append(expectedLayers, LayerTypeDot11InformationElement)
147         }
148         checkLayers(p, expectedLayers, t)
149
150         if p.Layer(LayerTypeDot11).(*Dot11).SequenceNumber != 2431 {
151                 t.Error("dot11 invalid sequence number")
152         }
153         if p.Layer(LayerTypeDot11).(*Dot11).FragmentNumber != 0 {
154                 t.Error("dot11 invalid fragment number")
155         }
156         if _, ok := p.Layer(LayerTypeDot11MgmtBeacon).(*Dot11MgmtBeacon); !ok {
157                 t.Errorf("dot11 management beacon frame was expected")
158         }
159 }
160
161 func BenchmarkDecodePacketDot11MgmtBeacon(b *testing.B) {
162         for i := 0; i < b.N; i++ {
163                 gopacket.NewPacket(testPacketDot11MgmtBeacon, LinkTypeIEEE80211Radio, gopacket.NoCopy)
164         }
165 }
166
167 // testPacketDot11DataQOSData is the packet:
168 //   06:14:27.838634 638790765us tsft short preamble 54.0 Mb/s -51dB signal -96dB noise antenna 2 5180 MHz 11a CF +QoS ARP, Request who-has 140.180.51.68 tell 169.254.247.0, length 28
169 //      0x0000:  0000 2000 6708 0400 6d2c 1326 0000 0000  ....g...m,.&....
170 //      0x0010:  226c cda0 0200 0000 4001 0000 3c14 2411  "l......@...<.$.
171 //      0x0020:  8801 2c00 0603 7f07 a016 0019 e3d3 5352  ..,...........SR
172 //      0x0030:  ffff ffff ffff 5064 0000 50aa aaaa 0300  ......Pd..P.....
173 //      0x0040:  0000 0806 0001 0800 0604 0001 0019 e3d3  ................
174 //      0x0050:  5352 a9fe f700 0000 0000 0000 8cb4 3344  SR............3D
175 var testPacketDot11DataQOSData = []byte{
176         0x00, 0x00, 0x20, 0x00, 0x67, 0x08, 0x04, 0x00, 0x6d, 0x2c, 0x13, 0x26, 0x00, 0x00, 0x00, 0x00,
177         0x22, 0x6c, 0xcd, 0xa0, 0x02, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x3c, 0x14, 0x24, 0x11,
178         0x88, 0x01, 0x2c, 0x00, 0x06, 0x03, 0x7f, 0x07, 0xa0, 0x16, 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52,
179         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x50, 0x64, 0x00, 0x00, 0x50, 0xaa, 0xaa, 0xaa, 0x03, 0x00,
180         0x00, 0x00, 0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x00, 0x19, 0xe3, 0xd3,
181         0x53, 0x52, 0xa9, 0xfe, 0xf7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8c, 0xb4, 0x33, 0x44,
182 }
183
184 func TestPacketDot11DataQOSData(t *testing.T) {
185         p := gopacket.NewPacket(testPacketDot11DataQOSData, LinkTypeIEEE80211Radio, gopacket.Default)
186         if p.ErrorLayer() != nil {
187                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
188         }
189         checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11DataQOSData, LayerTypeDot11Data, LayerTypeLLC, LayerTypeSNAP, LayerTypeARP}, t)
190
191         if got, ok := p.Layer(LayerTypeARP).(*ARP); ok {
192                 want := &ARP{BaseLayer: BaseLayer{
193                         Contents: []uint8{0x0, 0x1, 0x8, 0x0, 0x6, 0x4, 0x0, 0x1, 0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0xa9, 0xfe, 0xf7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8c, 0xb4, 0x33, 0x44},
194                         Payload:  []uint8{},
195                 },
196                         AddrType:          0x1,
197                         Protocol:          0x800,
198                         HwAddressSize:     0x6,
199                         ProtAddressSize:   0x4,
200                         Operation:         0x1,
201                         SourceHwAddress:   []uint8{0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52},
202                         SourceProtAddress: []uint8{0xa9, 0xfe, 0xf7, 0x0},
203                         DstHwAddress:      []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
204                         DstProtAddress:    []uint8{0x8c, 0xb4, 0x33, 0x44},
205                 }
206
207                 if !reflect.DeepEqual(got, want) {
208                         t.Errorf("ARP packet processing failed:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
209                 }
210         }
211 }
212 func BenchmarkDecodePacketDot11DataQOSData(b *testing.B) {
213         for i := 0; i < b.N; i++ {
214                 gopacket.NewPacket(testPacketDot11DataQOSData, LinkTypeIEEE80211Radio, gopacket.NoCopy)
215         }
216 }
217
218 // testPacketDot11MgmtAction is the packet:
219 //   15:54:43.236460 1.0 Mb/s 2412 MHz 11b -67dB signal antenna 5 Action (8e:3a:e3:44:ac:c6): Spectrum Management Act#4
220 //      0x0000:  0000 1200 2e48 0000 1002 6c09 a000 bd05  .....H....l.....
221 //      0x0010:  0000 d000 0000 ffff ffff ffff 8e3a e344  .............:.D
222 //      0x0020:  acc6 8e3a e344 acc6 001b 0004 2503 0001  ...:.D......%...
223 //      0x0030:  0055 39f0 33                             .U9.3
224 var testPacketDot11MgmtAction = []byte{
225         0x00, 0x00, 0x12, 0x00, 0x2e, 0x48, 0x00, 0x00, 0x10, 0x02, 0x6c, 0x09, 0xa0, 0x00, 0xbd, 0x05,
226         0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8e, 0x3a, 0xe3, 0x44,
227         0xac, 0xc6, 0x8e, 0x3a, 0xe3, 0x44, 0xac, 0xc6, 0x00, 0x1b, 0x00, 0x04, 0x25, 0x03, 0x00, 0x01,
228         0x00, 0x55, 0x39, 0xf0, 0x33,
229 }
230
231 func TestPacketDot11MgmtAction(t *testing.T) {
232         p := gopacket.NewPacket(testPacketDot11MgmtAction, LinkTypeIEEE80211Radio, gopacket.Default)
233         if p.ErrorLayer() != nil {
234                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
235         }
236         checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11MgmtAction}, t)
237         if got, ok := p.Layer(LayerTypeDot11).(*Dot11); !ok {
238                 t.Errorf("dot11 frame was not parsed")
239         } else if !got.ChecksumValid() {
240                 t.Errorf("Dot11 packet processing failed: checksum failed")
241         }
242         if got, ok := p.Layer(LayerTypeDot11MgmtAction).(*Dot11MgmtAction); !ok {
243                 t.Errorf("management action frame was not parsed")
244         } else if got.Contents[0] != 0 {
245                 t.Errorf("action category was not spectrum management")
246         }
247 }
248
249 func BenchmarkDecodePacketDot11MgmtAction(b *testing.B) {
250         for i := 0; i < b.N; i++ {
251                 gopacket.NewPacket(testPacketDot11MgmtAction, LinkTypeIEEE80211Radio, gopacket.NoCopy)
252         }
253 }
254
255 // testPacketDot11CtrlAck is the packet:
256 //   06:14:27.838669 638758038us tsft short preamble 24.0 Mb/s -39dB signal -96dB noise antenna 2 5180 MHz 11a Acknowledgment RA:00:19:e3:d3:53:52
257 //      0x0000:  0000 2000 6708 0400 96ac 1226 0000 0000  ....g......&....
258 //      0x0010:  2230 d9a0 0200 0000 4001 0000 3c14 2411  "0......@...<.$.
259 //      0x0020:  d400 0000 0019 e3d3 5352 46e9 7687       ........SRF.v.
260 var testPacketDot11CtrlAck = []byte{
261         0x00, 0x00, 0x20, 0x00, 0x67, 0x08, 0x04, 0x00, 0x96, 0xac, 0x12, 0x26, 0x00, 0x00, 0x00, 0x00,
262         0x32, 0x30, 0xd9, 0xa0, 0x02, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x3c, 0x14, 0x24, 0x11,
263         0xd4, 0x00, 0x00, 0x00, 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0x46, 0xe9, 0x76, 0x87,
264 }
265
266 func TestPacketDot11CtrlAck(t *testing.T) {
267         p := gopacket.NewPacket(testPacketDot11CtrlAck, LinkTypeIEEE80211Radio, gopacket.Default)
268         if p.ErrorLayer() != nil {
269                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
270         }
271         checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11}, t)
272
273         if got, ok := p.Layer(LayerTypeDot11).(*Dot11); ok {
274                 if !got.ChecksumValid() {
275                         t.Errorf("Dot11 packet processing failed:\nchecksum failed. got  :\n%#v\n\n", got)
276                 }
277         }
278
279         if got, ok := p.Layer(LayerTypeDot11).(*Dot11); ok {
280                 if !got.ChecksumValid() {
281                         t.Errorf("Dot11 packet processing failed:\nchecksum failed. got  :\n%#v\n\n", got)
282                 }
283                 want := &Dot11{
284                         BaseLayer: BaseLayer{
285                                 Contents: []uint8{0xd4, 0x0, 0x0, 0x0, 0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52},
286                                 Payload:  []uint8{},
287                         },
288                         Type:       Dot11TypeCtrlAck,
289                         Proto:      0x0,
290                         Flags:      0x0,
291                         DurationID: 0x0,
292                         Address1:   net.HardwareAddr{0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52},
293                         Address2:   net.HardwareAddr(nil),
294                         Address3:   net.HardwareAddr(nil),
295                         Address4:   net.HardwareAddr(nil),
296                         Checksum:   0x8776e946,
297                 }
298                 if !reflect.DeepEqual(got, want) {
299                         t.Errorf("Dot11 packet processing failed:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
300                 }
301         }
302 }
303 func BenchmarkDecodePacketDot11CtrlAck(b *testing.B) {
304         for i := 0; i < b.N; i++ {
305                 gopacket.NewPacket(testPacketDot11CtrlAck, LinkTypeIEEE80211Radio, gopacket.NoCopy)
306         }
307 }
308
309 // testPacketDot11DataARP is the packet:
310 //   06:14:11.512316 622463798us tsft short preamble 6.0 Mb/s -39dB signal -96dB noise antenna 2 5180 MHz 11a ARP, Request who-has 67.8.14.54 tell 169.254.247.0, length 28
311 //      0x0000:  0000 2000 6708 0400 360b 1a25 0000 0000  ....g...6..%....
312 //      0x0010:  220c d9a0 0200 0000 4001 0000 3c14 2411  ".......@...<.$.
313 //      0x0020:  0802 0000 ffff ffff ffff 0603 7f07 a016  ................
314 //      0x0030:  0019 e3d3 5352 e07f aaaa 0300 0000 0806  ....SR..........
315 //      0x0040:  0001 0800 0604 0001 0019 e3d3 5352 a9fe  ............SR..
316 //      0x0050:  f700 0000 0000 0000 4308 0e36            ........C..6
317 var testPacketDot11DataARP = []byte{
318         0x00, 0x00, 0x20, 0x00, 0x67, 0x08, 0x04, 0x00, 0x36, 0x0b, 0x1a, 0x25, 0x00, 0x00, 0x00, 0x00,
319         0x22, 0x0c, 0xd9, 0xa0, 0x02, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x3c, 0x14, 0x24, 0x11,
320         0x08, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x06, 0x03, 0x7f, 0x07, 0xa0, 0x16,
321         0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0xe0, 0x7f, 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x08, 0x06,
322         0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0xa9, 0xfe,
323         0xf7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x08, 0x0e, 0x36,
324 }
325
326 func TestPacketDot11DataARP(t *testing.T) {
327         p := gopacket.NewPacket(testPacketDot11DataARP, LinkTypeIEEE80211Radio, gopacket.Default)
328         if p.ErrorLayer() != nil {
329                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
330         }
331         checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11Data, LayerTypeLLC, LayerTypeSNAP, LayerTypeARP}, t)
332
333         if got, ok := p.Layer(LayerTypeARP).(*ARP); ok {
334                 want := &ARP{
335                         BaseLayer: BaseLayer{
336                                 Contents: []uint8{0x0, 0x1, 0x8, 0x0, 0x6, 0x4, 0x0, 0x1, 0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0xa9, 0xfe, 0xf7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x43, 0x8, 0xe, 0x36},
337                                 Payload:  []uint8{},
338                         },
339                         AddrType:          0x1,
340                         Protocol:          0x800,
341                         HwAddressSize:     0x6,
342                         ProtAddressSize:   0x4,
343                         Operation:         0x1,
344                         SourceHwAddress:   []uint8{0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52},
345                         SourceProtAddress: []uint8{0xa9, 0xfe, 0xf7, 0x0},
346                         DstHwAddress:      []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
347                         DstProtAddress:    []uint8{0x43, 0x8, 0xe, 0x36},
348                 }
349
350                 if !reflect.DeepEqual(got, want) {
351                         t.Errorf("ARP packet processing failed:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
352                 }
353         }
354 }
355
356 func BenchmarkDecodePacketDot11DataARP(b *testing.B) {
357         for i := 0; i < b.N; i++ {
358                 gopacket.NewPacket(testPacketDot11DataARP, LinkTypeIEEE80211Radio, gopacket.NoCopy)
359         }
360 }
361
362 // testPacketDot11DataIP is the packet:
363 //   06:14:21.388622 632340487us tsft short preamble 6.0 Mb/s -40dB signal -96dB noise antenna 1 5180 MHz 11a IP 0.0.0.0.68 > 255.255.255.255.67: BOOTP/DHCP, Request from 00:19:e3:d3:53:52, length 300
364 //      0x0000:  0000 2000 6708 0400 07c0 b025 0000 0000  ....g......%....
365 //      0x0010:  220c d8a0 0100 0000 4001 0000 3c14 2411  ".......@...<.$.
366 //      0x0020:  0802 0000 ffff ffff ffff 0603 7f07 a016  ................
367 //      0x0030:  0019 e3d3 5352 4095 aaaa 0300 0000 0800  ....SR@.........
368 //      0x0040:  4500 0148 c514 0000 ff11 f590 0000 0000  E..H............
369 //      0x0050:  ffff ffff 0044 0043 0134 2b39 0101 0600  .....D.C.4+9....
370 //      0x0060:  131f 8c43 003c 0000 0000 0000 0000 0000  ...C.<..........
371 //      0x0070:  0000 0000 0000 0000 0019 e3d3 5352 0000  ............SR..
372 //      0x0080:  0000 0000 0000 0000 0000 0000 0000 0000  ................
373 //      0x0090:  0000 0000 0000 0000 0000 0000 0000 0000  ................
374 //      0x00a0:  0000 0000 0000 0000 0000 0000 0000 0000  ................
375 //      0x00b0:  0000 0000 0000 0000 0000 0000 0000 0000  ................
376 //      0x00c0:  0000 0000 0000 0000 0000 0000 0000 0000  ................
377 //      0x00d0:  0000 0000 0000 0000 0000 0000 0000 0000  ................
378 //      0x00e0:  0000 0000 0000 0000 0000 0000 0000 0000  ................
379 //      0x00f0:  0000 0000 0000 0000 0000 0000 0000 0000  ................
380 //      0x0100:  0000 0000 0000 0000 0000 0000 0000 0000  ................
381 //      0x0110:  0000 0000 0000 0000 0000 0000 0000 0000  ................
382 //      0x0120:  0000 0000 0000 0000 0000 0000 0000 0000  ................
383 //      0x0130:  0000 0000 0000 0000 0000 0000 0000 0000  ................
384 //      0x0140:  0000 0000 0000 0000 6382 5363 3501 0137  ........c.Sc5..7
385 //      0x0150:  0a01 0306 0f77 5ffc 2c2e 2f39 0205 dc3d  .....w_.,./9...=
386 //      0x0160:  0701 0019 e3d3 5352 3304 0076 a700 0c0b  ......SR3..v....
387 //      0x0170:  4d61 6369 6e74 6f73 682d 34ff 0000 0000  Macintosh-4.....
388 //      0x0180:  0000 0000 0000 0000                      ........
389 var testPacketDot11DataIP = []byte{
390         0x00, 0x00, 0x20, 0x00, 0x67, 0x08, 0x04, 0x00, 0x07, 0xc0, 0xb0, 0x25, 0x00, 0x00, 0x00, 0x00,
391         0x22, 0x0c, 0xd8, 0xa0, 0x01, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x3c, 0x14, 0x24, 0x11,
392         0x08, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x06, 0x03, 0x7f, 0x07, 0xa0, 0x16,
393         0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0x40, 0x95, 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x08, 0x00,
394         0x45, 0x00, 0x01, 0x48, 0xc5, 0x14, 0x00, 0x00, 0xff, 0x11, 0xf5, 0x90, 0x00, 0x00, 0x00, 0x00,
395         0xff, 0xff, 0xff, 0xff, 0x00, 0x44, 0x00, 0x43, 0x01, 0x34, 0x2b, 0x39, 0x01, 0x01, 0x06, 0x00,
396         0x13, 0x1f, 0x8c, 0x43, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0x00, 0x00,
398         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
399         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
400         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
401         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
402         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
403         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
404         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
405         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
406         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
407         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
408         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
409         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
410         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x01, 0x37,
411         0x0a, 0x01, 0x03, 0x06, 0x0f, 0x77, 0x5f, 0xfc, 0x2c, 0x2e, 0x2f, 0x39, 0x02, 0x05, 0xdc, 0x3d,
412         0x07, 0x01, 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0x33, 0x04, 0x00, 0x76, 0xa7, 0x00, 0x0c, 0x0b,
413         0x4d, 0x61, 0x63, 0x69, 0x6e, 0x74, 0x6f, 0x73, 0x68, 0x2d, 0x34, 0xff, 0x00, 0x00, 0x00, 0x00,
414         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
415 }
416
417 func TestPacketDot11DataIP(t *testing.T) {
418         p := gopacket.NewPacket(testPacketDot11DataIP, LinkTypeIEEE80211Radio, gopacket.Default)
419         if p.ErrorLayer() != nil {
420                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
421         }
422         checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11Data, LayerTypeLLC, LayerTypeSNAP, LayerTypeIPv4, LayerTypeUDP, LayerTypeDHCPv4}, t)
423 }
424 func BenchmarkDecodePacketDot11DataIP(b *testing.B) {
425         for i := 0; i < b.N; i++ {
426                 gopacket.NewPacket(testPacketDot11DataIP, LinkTypeIEEE80211Radio, gopacket.NoCopy)
427         }
428 }
429
430 // Encrypted
431
432 /// testPacketP6196 is the packet:
433 //   09:28:41.830631 20605036us tsft wep -69dB signal -92dB noise antenna 1 5240 MHz 11a ht/40- 162.0 Mb/s MCS 12 40 MHz lon GI mixed BCC FEC [bit 20] CF +QoS Data IV:50a9 Pad 20 KeyID 0
434 //      0x0000:  0000 3000 6b08 1c00 6c68 3a01 0000 0000  ..0.k...lh:.....
435 //      0x0010:  1400 7814 4001 bba4 0160 0e1a 4001 0400  ..x.@....`..@...
436 //      0x0020:  7814 3022 1f01 0cff b10d 0000 0400 0000  x.0"............
437 //      0x0030:  8841 2c00 0025 9c42 c262 d8a2 5e97 61c1  .A,..%.B.b..^.a.
438 //      0x0040:  0025 9c42 c25f 10db 0000 a950 0020 0000  .%.B._.....P....
439 //      0x0050:  0000 f8ab a97e 3fbd d6e1 785b 0040 5f15  .....~?...x[.@_.
440 //      0x0060:  7123 8711 bd1f ffb9 e5b3 84bb ec2a 0a90  q#...........*..
441 //      0x0070:  d0a0 1a6f 9033 1083 5179 a0da f833 3a00  ...o.3..Qy...3:.
442 //      0x0080:  5471 f596 539b 1823 a33c 4908 545c 266a  Tq..S..#.<I.T\&j
443 //      0x0090:  8540 515a 1da9 c49e a85a fbf7 de09 7f9c  .@QZ.....Z......
444 //      0x00a0:  6f35 0b8b 6831 2c10 43dc 8983 b1d9 dd29  o5..h1,.C......)
445 //      0x00b0:  7395 65b9 4b43 b391 16ec 4201 86c9 ca    s.e.KC....B....
446 var testPacketP6196 = []byte{
447         0x00, 0x00, 0x30, 0x00, 0x6b, 0x08, 0x1c, 0x00, 0x6c, 0x68, 0x3a, 0x01, 0x00, 0x00, 0x00, 0x00,
448         0x14, 0x00, 0x78, 0x14, 0x40, 0x01, 0xbb, 0xa4, 0x01, 0x60, 0x0e, 0x1a, 0x40, 0x01, 0x04, 0x00,
449         0x78, 0x14, 0x30, 0x22, 0x1f, 0x01, 0x0c, 0xff, 0xb1, 0x0d, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
450         0x88, 0x41, 0x2c, 0x00, 0x00, 0x25, 0x9c, 0x42, 0xc2, 0x62, 0xd8, 0xa2, 0x5e, 0x97, 0x61, 0xc1,
451         0x00, 0x25, 0x9c, 0x42, 0xc2, 0x5f, 0x10, 0xdb, 0x00, 0x00, 0xa9, 0x50, 0x00, 0x20, 0x00, 0x00,
452         0x00, 0x00, 0xf8, 0xab, 0xa9, 0x7e, 0x3f, 0xbd, 0xd6, 0xe1, 0x78, 0x5b, 0x00, 0x40, 0x5f, 0x15,
453         0x71, 0x23, 0x87, 0x11, 0xbd, 0x1f, 0xff, 0xb9, 0xe5, 0xb3, 0x84, 0xbb, 0xec, 0x2a, 0x0a, 0x90,
454         0xd0, 0xa0, 0x1a, 0x6f, 0x90, 0x33, 0x10, 0x83, 0x51, 0x79, 0xa0, 0xda, 0xf8, 0x33, 0x3a, 0x00,
455         0x54, 0x71, 0xf5, 0x96, 0x53, 0x9b, 0x18, 0x23, 0xa3, 0x3c, 0x49, 0x08, 0x54, 0x5c, 0x26, 0x6a,
456         0x85, 0x40, 0x51, 0x5a, 0x1d, 0xa9, 0xc4, 0x9e, 0xa8, 0x5a, 0xfb, 0xf7, 0xde, 0x09, 0x7f, 0x9c,
457         0x6f, 0x35, 0x0b, 0x8b, 0x68, 0x31, 0x2c, 0x10, 0x43, 0xdc, 0x89, 0x83, 0xb1, 0xd9, 0xdd, 0x29,
458         0x73, 0x95, 0x65, 0xb9, 0x4b, 0x43, 0xb3, 0x91, 0x16, 0xec, 0x42, 0x01, 0x86, 0xc9, 0xca,
459 }
460
461 func TestPacketP6196(t *testing.T) {
462         p := gopacket.NewPacket(testPacketP6196, LinkTypeIEEE80211Radio, gopacket.Default)
463         if p.ErrorLayer() != nil {
464                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
465         }
466
467         checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11WEP}, t)
468 }
469
470 func BenchmarkDecodePacketP6196(b *testing.B) {
471         for i := 0; i < b.N; i++ {
472                 gopacket.NewPacket(testPacketP6196, LinkTypeIEEE80211Radio, gopacket.NoCopy)
473         }
474 }
475
476 func TestInformationElement(t *testing.T) {
477         bin := []byte{
478                 0, 0,
479                 0, 2, 1, 3,
480                 221, 5, 1, 2, 3, 4, 5,
481         }
482         pkt := gopacket.NewPacket(bin, LayerTypeDot11InformationElement, gopacket.NoCopy)
483
484         buf := gopacket.NewSerializeBuffer()
485         var sLayers []gopacket.SerializableLayer
486         for _, l := range pkt.Layers() {
487                 sLayers = append(sLayers, l.(*Dot11InformationElement))
488         }
489         if err := gopacket.SerializeLayers(buf, gopacket.SerializeOptions{}, sLayers...); err != nil {
490                 t.Error(err.Error())
491         }
492         if !bytes.Equal(bin, buf.Bytes()) {
493                 t.Error("build failed")
494         }
495 }