1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package layers
import (
"fmt"
"reflect"
"testing"
"github.com/google/gopacket"
)
// test harness to ensure the dot1q layer can be encoded/decoded properly
// return error if decoded data not match.
func testEncodeDecodeDot1Q(dot1Q *Dot1Q) error {
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{}
expectedDot1Q := dot1Q
err := dot1Q.SerializeTo(buf, opts)
if err != nil {
return err
}
newDot1q := &Dot1Q{}
err = newDot1q.DecodeFromBytes(buf.Bytes(), gopacket.NilDecodeFeedback)
if err != nil {
return err
}
newDot1q.BaseLayer = BaseLayer{}
if !reflect.DeepEqual(expectedDot1Q, newDot1q) {
return fmt.Errorf("Expect %v actual %v", expectedDot1Q, newDot1q)
}
return nil
}
// Test to ensure what has been encode can be decoded
func TestEncodeDecodeDot1Q(t *testing.T) {
dot1Qs := []*Dot1Q{
&Dot1Q{
Priority: uint8(3),
VLANIdentifier: uint16(30),
},
&Dot1Q{
Priority: uint8(0x07),
DropEligible: true,
VLANIdentifier: uint16(0xFFF),
},
}
for i, curTest := range dot1Qs {
err := testEncodeDecodeDot1Q(curTest)
if err != nil {
t.Error("Error with item ", i, " with error message :", err)
}
}
}
|