added support for string type
[govpp.git] / vendor / github.com / google / gopacket / layers / gre_test.go
1 // Copyright 2012 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 package layers
7
8 import (
9         "fmt"
10         "net"
11         "reflect"
12         "testing"
13
14         "github.com/google/gopacket"
15 )
16
17 // testPacketGRE is the packet:
18 //   15:08:08.003196 IP 192.168.1.1 > 192.168.1.2: GREv0, length 88: IP 172.16.1.1 > 172.16.2.1: ICMP echo request, id 4724, seq 1, length 64
19 //      0x0000:  3a56 6b69 595e 8e7a 12c3 a971 0800 4500  :VkiY^.z...q..E.
20 //      0x0010:  006c 843c 4000 402f 32d3 c0a8 0101 c0a8  .l.<@.@/2.......
21 //      0x0020:  0102 0000 0800 4500 0054 0488 4000 4001  ......E..T..@.@.
22 //      0x0030:  dafe ac10 0101 ac10 0201 0800 82c4 1274  ...............t
23 //      0x0040:  0001 c892 a354 0000 0000 380c 0000 0000  .....T....8.....
24 //      0x0050:  0000 1011 1213 1415 1617 1819 1a1b 1c1d  ................
25 //      0x0060:  1e1f 2021 2223 2425 2627 2829 2a2b 2c2d  ...!"#$%&'()*+,-
26 //      0x0070:  2e2f 3031 3233 3435 3637                 ./01234567
27 var testPacketGRE = []byte{
28         0x3a, 0x56, 0x6b, 0x69, 0x59, 0x5e, 0x8e, 0x7a, 0x12, 0xc3, 0xa9, 0x71, 0x08, 0x00, 0x45, 0x00,
29         0x00, 0x6c, 0x84, 0x3c, 0x40, 0x00, 0x40, 0x2f, 0x32, 0xd3, 0xc0, 0xa8, 0x01, 0x01, 0xc0, 0xa8,
30         0x01, 0x02, 0x00, 0x00, 0x08, 0x00, 0x45, 0x00, 0x00, 0x54, 0x04, 0x88, 0x40, 0x00, 0x40, 0x01,
31         0xda, 0xfe, 0xac, 0x10, 0x01, 0x01, 0xac, 0x10, 0x02, 0x01, 0x08, 0x00, 0x82, 0xc4, 0x12, 0x74,
32         0x00, 0x01, 0xc8, 0x92, 0xa3, 0x54, 0x00, 0x00, 0x00, 0x00, 0x38, 0x0c, 0x00, 0x00, 0x00, 0x00,
33         0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
34         0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
35         0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
36 }
37
38 func TestPacketGRE(t *testing.T) {
39         p := gopacket.NewPacket(testPacketGRE, LinkTypeEthernet, gopacket.Default)
40         if p.ErrorLayer() != nil {
41                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
42         }
43         checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeGRE, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
44         if got, ok := p.Layer(LayerTypeGRE).(*GRE); ok {
45                 want := &GRE{
46                         BaseLayer: BaseLayer{testPacketGRE[34:38], testPacketGRE[38:]},
47                         Protocol:  EthernetTypeIPv4,
48                 }
49                 if !reflect.DeepEqual(want, got) {
50                         t.Errorf("GRE layer mismatch, \nwant %#v\ngot  %#v\n", want, got)
51                 }
52         }
53 }
54
55 func BenchmarkDecodePacketGRE(b *testing.B) {
56         for i := 0; i < b.N; i++ {
57                 gopacket.NewPacket(testPacketGRE, LinkTypeEthernet, gopacket.NoCopy)
58         }
59 }
60
61 var testIPv4OverGRE = []gopacket.SerializableLayer{
62         &Ethernet{
63                 SrcMAC:       net.HardwareAddr{142, 122, 18, 195, 169, 113},
64                 DstMAC:       net.HardwareAddr{58, 86, 107, 105, 89, 94},
65                 EthernetType: EthernetTypeIPv4,
66         },
67         &IPv4{
68                 Version:  4,
69                 SrcIP:    net.IP{192, 168, 1, 1},
70                 DstIP:    net.IP{192, 168, 1, 2},
71                 Protocol: IPProtocolGRE,
72                 Flags:    IPv4DontFragment,
73                 TTL:      64,
74                 Id:       33852,
75                 IHL:      5,
76         },
77         &GRE{
78                 Protocol: EthernetTypeIPv4,
79         },
80         &IPv4{
81                 Version:  4,
82                 SrcIP:    net.IP{172, 16, 1, 1},
83                 DstIP:    net.IP{172, 16, 2, 1},
84                 Protocol: IPProtocolICMPv4,
85                 Flags:    IPv4DontFragment,
86                 TTL:      64,
87                 IHL:      5,
88                 Id:       1160,
89         },
90         &ICMPv4{
91                 TypeCode: CreateICMPv4TypeCode(ICMPv4TypeEchoRequest, 0),
92                 Id:       4724,
93                 Seq:      1,
94         },
95         gopacket.Payload{
96                 0xc8, 0x92, 0xa3, 0x54, 0x00, 0x00, 0x00, 0x00, 0x38, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97                 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
98                 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
99                 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
100         },
101 }
102
103 func TestIPv4OverGREEncode(t *testing.T) {
104         b := gopacket.NewSerializeBuffer()
105         opts := gopacket.SerializeOptions{
106                 ComputeChecksums: true,
107                 FixLengths:       true,
108         }
109         if err := gopacket.SerializeLayers(b, opts, testIPv4OverGRE...); err != nil {
110                 t.Errorf("Unable to serialize: %v", err)
111         }
112         p := gopacket.NewPacket(b.Bytes(), LinkTypeEthernet, gopacket.Default)
113         if p.ErrorLayer() != nil {
114                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
115         }
116         checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeGRE, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
117         if got, want := b.Bytes(), testPacketGRE; !reflect.DeepEqual(want, got) {
118                 t.Errorf("Encoding mismatch, \nwant: %v\ngot %v\n", want, got)
119         }
120 }
121
122 func BenchmarkEncodePacketGRE(b *testing.B) {
123         buf := gopacket.NewSerializeBuffer()
124         opts := gopacket.SerializeOptions{
125                 ComputeChecksums: true,
126                 FixLengths:       true,
127         }
128         for i := 0; i < b.N; i++ {
129                 gopacket.SerializeLayers(buf, opts, testIPv4OverGRE...)
130                 buf.Clear()
131         }
132 }
133
134 // testPacketEthernetOverGRE is the packet:
135 //   11:01:38.124768 IP 192.168.1.1 > 192.168.1.2: GREv0, length 102: IP 172.16.1.1 > 172.16.1.2: ICMP echo request, id 3842, seq 1, length 64
136 //      0x0000:  ea6b 4cd3 5513 d6b9 d880 56ef 0800 4500  .kL.U.....V...E.
137 //      0x0010:  007a 0acd 4000 402f ac34 c0a8 0101 c0a8  .z..@.@/.4......
138 //      0x0020:  0102 0000 6558 aa6a 36e6 c630 6e32 3ec7  ....eX.j6..0n2>.
139 //      0x0030:  9def 0800 4500 0054 d970 4000 4001 0715  ....E..T.p@.@...
140 //      0x0040:  ac10 0101 ac10 0102 0800 3f15 0f02 0001  ..........?.....
141 //      0x0050:  82d9 b154 0000 0000 b5e6 0100 0000 0000  ...T............
142 //      0x0060:  1011 1213 1415 1617 1819 1a1b 1c1d 1e1f  ................
143 //      0x0070:  2021 2223 2425 2627 2829 2a2b 2c2d 2e2f  .!"#$%&'()*+,-./
144 //      0x0080:  3031 3233 3435 3637                      01234567
145 var testPacketEthernetOverGRE = []byte{
146         0xea, 0x6b, 0x4c, 0xd3, 0x55, 0x13, 0xd6, 0xb9, 0xd8, 0x80, 0x56, 0xef, 0x08, 0x00, 0x45, 0x00,
147         0x00, 0x7a, 0x0a, 0xcd, 0x40, 0x00, 0x40, 0x2f, 0xac, 0x34, 0xc0, 0xa8, 0x01, 0x01, 0xc0, 0xa8,
148         0x01, 0x02, 0x00, 0x00, 0x65, 0x58, 0xaa, 0x6a, 0x36, 0xe6, 0xc6, 0x30, 0x6e, 0x32, 0x3e, 0xc7,
149         0x9d, 0xef, 0x08, 0x00, 0x45, 0x00, 0x00, 0x54, 0xd9, 0x70, 0x40, 0x00, 0x40, 0x01, 0x07, 0x15,
150         0xac, 0x10, 0x01, 0x01, 0xac, 0x10, 0x01, 0x02, 0x08, 0x00, 0x3f, 0x15, 0x0f, 0x02, 0x00, 0x01,
151         0x82, 0xd9, 0xb1, 0x54, 0x00, 0x00, 0x00, 0x00, 0xb5, 0xe6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
152         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
153         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
154         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
155 }
156
157 func TestPacketEthernetOverGRE(t *testing.T) {
158         p := gopacket.NewPacket(testPacketEthernetOverGRE, LinkTypeEthernet, gopacket.Default)
159         if p.ErrorLayer() != nil {
160                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
161         }
162         checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeGRE, LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
163         if got, ok := p.Layer(LayerTypeGRE).(*GRE); ok {
164                 want := &GRE{
165                         BaseLayer: BaseLayer{testPacketEthernetOverGRE[34:38], testPacketEthernetOverGRE[38:]},
166                         Protocol:  EthernetTypeTransparentEthernetBridging,
167                 }
168                 if !reflect.DeepEqual(want, got) {
169                         t.Errorf("GRE layer mismatch, \nwant %#v\ngot  %#v\n", want, got)
170                 }
171         }
172 }
173
174 func BenchmarkDecodePacketEthernetOverGRE(b *testing.B) {
175         for i := 0; i < b.N; i++ {
176                 gopacket.NewPacket(testPacketEthernetOverGRE, LinkTypeEthernet, gopacket.NoCopy)
177         }
178 }
179
180 var testEthernetOverGRE = []gopacket.SerializableLayer{
181         &Ethernet{
182                 SrcMAC:       net.HardwareAddr{0xd6, 0xb9, 0xd8, 0x80, 0x56, 0xef},
183                 DstMAC:       net.HardwareAddr{0xea, 0x6b, 0x4c, 0xd3, 0x55, 0x13},
184                 EthernetType: EthernetTypeIPv4,
185         },
186         &IPv4{
187                 Version:  4,
188                 SrcIP:    net.IP{192, 168, 1, 1},
189                 DstIP:    net.IP{192, 168, 1, 2},
190                 Protocol: IPProtocolGRE,
191                 Flags:    IPv4DontFragment,
192                 TTL:      64,
193                 Id:       2765,
194                 IHL:      5,
195         },
196         &GRE{
197                 Protocol: EthernetTypeTransparentEthernetBridging,
198         },
199         &Ethernet{
200                 SrcMAC:       net.HardwareAddr{0x6e, 0x32, 0x3e, 0xc7, 0x9d, 0xef},
201                 DstMAC:       net.HardwareAddr{0xaa, 0x6a, 0x36, 0xe6, 0xc6, 0x30},
202                 EthernetType: EthernetTypeIPv4,
203         },
204         &IPv4{
205                 Version:  4,
206                 SrcIP:    net.IP{172, 16, 1, 1},
207                 DstIP:    net.IP{172, 16, 1, 2},
208                 Protocol: IPProtocolICMPv4,
209                 Flags:    IPv4DontFragment,
210                 TTL:      64,
211                 IHL:      5,
212                 Id:       55664,
213         },
214         &ICMPv4{
215                 TypeCode: CreateICMPv4TypeCode(ICMPv4TypeEchoRequest, 0),
216                 Id:       3842,
217                 Seq:      1,
218         },
219         gopacket.Payload{
220                 0x82, 0xd9, 0xb1, 0x54, 0x00, 0x00, 0x00, 0x00, 0xb5, 0xe6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
221                 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
222                 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
223                 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
224         },
225 }
226
227 func TestEthernetOverGREEncode(t *testing.T) {
228         b := gopacket.NewSerializeBuffer()
229         opts := gopacket.SerializeOptions{
230                 ComputeChecksums: true,
231                 FixLengths:       true,
232         }
233         if err := gopacket.SerializeLayers(b, opts, testEthernetOverGRE...); err != nil {
234                 t.Errorf("Unable to serialize: %v", err)
235         }
236         p := gopacket.NewPacket(b.Bytes(), LinkTypeEthernet, gopacket.Default)
237         if p.ErrorLayer() != nil {
238                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
239         }
240         checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeGRE, LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
241         if got, want := b.Bytes(), testPacketEthernetOverGRE; !reflect.DeepEqual(want, got) {
242                 t.Errorf("Encoding mismatch, \nwant: %v\ngot %v\n", want, got)
243         }
244 }
245
246 func BenchmarkEncodePacketEthernetOverGRE(b *testing.B) {
247         buf := gopacket.NewSerializeBuffer()
248         opts := gopacket.SerializeOptions{
249                 ComputeChecksums: true,
250                 FixLengths:       true,
251         }
252         for i := 0; i < b.N; i++ {
253                 gopacket.SerializeLayers(buf, opts, testEthernetOverGRE...)
254                 buf.Clear()
255         }
256 }
257
258 var testGREChecksum = map[uint16][]gopacket.SerializableLayer{
259         0x77ff: {
260                 &Ethernet{
261                         SrcMAC:       net.HardwareAddr{0xc2, 0x00, 0x57, 0x75, 0x00, 0x00},
262                         DstMAC:       net.HardwareAddr{0xc2, 0x01, 0x57, 0x75, 0x00, 0x00},
263                         EthernetType: EthernetTypeIPv4,
264                 },
265                 &IPv4{
266                         Version:  4,
267                         SrcIP:    net.IP{10, 0, 0, 1},
268                         DstIP:    net.IP{10, 0, 0, 2},
269                         Protocol: IPProtocolGRE,
270                         TTL:      255,
271                         Id:       10,
272                         IHL:      5,
273                 },
274                 &GRE{
275                         Protocol:        EthernetTypeIPv4,
276                         ChecksumPresent: true,
277                 },
278                 &IPv4{
279                         Version:  4,
280                         SrcIP:    net.IP{1, 1, 1, 1},
281                         DstIP:    net.IP{2, 2, 2, 2},
282                         Protocol: IPProtocolICMPv4,
283                         TTL:      255,
284                         IHL:      5,
285                         Id:       10,
286                 },
287                 &ICMPv4{
288                         TypeCode: CreateICMPv4TypeCode(ICMPv4TypeEchoRequest, 0),
289                         Id:       2,
290                         Seq:      0,
291                 },
292                 gopacket.Payload{
293                         0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xbe, 0x70, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
294                         0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
295                         0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
296                         0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
297                         0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
298                 },
299         },
300         0x8475: {
301                 &Ethernet{
302                         SrcMAC:       net.HardwareAddr{0xc2, 0x00, 0x57, 0x75, 0x00, 0x00},
303                         DstMAC:       net.HardwareAddr{0xc2, 0x01, 0x57, 0x75, 0x00, 0x00},
304                         EthernetType: EthernetTypeIPv4,
305                 },
306                 &IPv4{
307                         Version:  4,
308                         SrcIP:    net.IP{10, 0, 0, 1},
309                         DstIP:    net.IP{10, 0, 0, 2},
310                         Protocol: IPProtocolGRE,
311                         TTL:      255,
312                         Id:       10,
313                         IHL:      5,
314                 },
315                 &GRE{
316                         Protocol:        EthernetTypeIPv4,
317                         ChecksumPresent: true,
318                 },
319                 &IPv4{
320                         Version:  4,
321                         SrcIP:    net.IP{2, 3, 4, 5},
322                         DstIP:    net.IP{2, 3, 4, 50},
323                         Protocol: IPProtocolUDP,
324                         TTL:      1,
325                         IHL:      5,
326                         Flags:    IPv4DontFragment,
327                         Id:       964,
328                 },
329                 &UDP{
330                         SrcPort: 41781,
331                         DstPort: 33434,
332                 },
333                 gopacket.Payload{
334                         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
335                         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
336                 },
337         },
338 }
339
340 func TestGREChecksum(t *testing.T) {
341         buf := gopacket.NewSerializeBuffer()
342         opts := gopacket.SerializeOptions{
343                 ComputeChecksums: true,
344                 FixLengths:       true,
345         }
346         for cksum, packet := range testGREChecksum {
347                 buf.Clear()
348                 if err := setNetworkLayer(packet); err != nil {
349                         t.Errorf("Failed to set network layer: %v", err)
350                         continue
351                 }
352                 if err := gopacket.SerializeLayers(buf, opts, packet...); err != nil {
353                         t.Errorf("Failed to serialize packet: %v", err)
354                         continue
355                 }
356                 p := gopacket.NewPacket(buf.Bytes(), LinkTypeEthernet, gopacket.Default)
357                 t.Log(p)
358                 if p.ErrorLayer() != nil {
359                         t.Error("Failed to decode packet:", p.ErrorLayer().Error())
360                         continue
361                 }
362                 if got, ok := p.Layer(LayerTypeGRE).(*GRE); ok {
363                         if got.Checksum != cksum {
364                                 t.Errorf("Incorrect checksum calculated for GRE packet: want %v, got %v", cksum, got.Checksum)
365                         }
366                 }
367         }
368 }
369
370 func setNetworkLayer(layers []gopacket.SerializableLayer) error {
371         type setNetworkLayerForChecksum interface {
372                 SetNetworkLayerForChecksum(gopacket.NetworkLayer) error
373         }
374         var l gopacket.NetworkLayer
375         for _, layer := range layers {
376                 if n, ok := layer.(gopacket.NetworkLayer); ok {
377                         l = n
378                 }
379                 if s, ok := layer.(setNetworkLayerForChecksum); ok {
380                         if l == nil {
381                                 return fmt.Errorf("no enclosing network layer found before: %v", s)
382                         }
383                         if err := s.SetNetworkLayerForChecksum(l); err != nil {
384                                 return fmt.Errorf("failed to set network layer(%v) on layer(%v): %v", l, s, err)
385                         }
386                 }
387         }
388         return nil
389 }