Skip to content

Commit e1546f6

Browse files
bensarifathigconnell
authored andcommitted
add unit tests to stp protocol implementation
1 parent 17c430c commit e1546f6

File tree

2 files changed

+189
-5
lines changed

2 files changed

+189
-5
lines changed

layers/stp.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ func (stp *STP) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
6666
stp.ProtocolID = binary.BigEndian.Uint16(data[:2])
6767
stp.Version = uint8(data[2])
6868
stp.Type = uint8(data[3])
69-
stp.TCA = data[4]&0x01 != 0
70-
stp.TC = data[4]&0x80 != 0
69+
stp.TC = data[4]&0x01 != 0
70+
stp.TCA = data[4]&0x80 != 0
7171
stp.RouteID.Priority = STPPriority(binary.BigEndian.Uint16(data[5:7]) & 0xf000)
7272
stp.RouteID.SysID = binary.BigEndian.Uint16(data[5:7]) & 0x0fff
7373
stp.RouteID.HwAddr = net.HardwareAddr(data[7:13])
7474
stp.Cost = binary.BigEndian.Uint32(data[13:17])
7575
stp.BridgeID.Priority = STPPriority(binary.BigEndian.Uint16(data[17:19]) & 0xf000)
7676
stp.BridgeID.SysID = binary.BigEndian.Uint16(data[17:19]) & 0x0fff
77-
stp.RouteID.HwAddr = net.HardwareAddr(data[19:25])
77+
stp.BridgeID.HwAddr = net.HardwareAddr(data[19:25])
7878
stp.PortID = binary.BigEndian.Uint16(data[25:27])
7979
stp.MessageAge = binary.BigEndian.Uint16(data[27:29])
8080
stp.MaxAge = binary.BigEndian.Uint16(data[29:31])
@@ -119,10 +119,10 @@ func (s *STP) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOpt
119119
binary.BigEndian.PutUint16(bytes, s.ProtocolID)
120120
bytes[2] = s.Version
121121
bytes[3] = s.Type
122-
if s.TCA {
122+
if s.TC {
123123
flags |= 0x01
124124
}
125-
if s.TC {
125+
if s.TCA {
126126
flags |= 0x80
127127
}
128128
bytes[4] = flags

layers/stp_test.go

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Copyright 2021 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+
var testPacketSTPRDATA = []byte{
18+
0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x01, 0xAA, 0xBB, 0xCC,
19+
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0xAA,
20+
0xBB, 0xCC, 0x00, 0x01, 0x00, 0x80, 0x01, 0x00, 0x00, 0x14,
21+
0x00, 0x02, 0x00, 0x0F, 0x00,
22+
}
23+
24+
// real stp packet data with the following value :
25+
// {
26+
// ProtocolID: 0,
27+
// Version: 0,
28+
// Type: 0,
29+
// TCA: false,
30+
// TC: true,
31+
// RooteID : {
32+
// Priority: 32768,
33+
// SysID: 1,
34+
// HwAddr: aa bb cc 00 01 00
35+
// },
36+
// Cost: 0,
37+
// BridgeID: {
38+
// Priority: 32768,
39+
// SysID: 1,
40+
// HwAddr: aa bb cc 00 01 00
41+
// },
42+
// PortID : 0x8001,
43+
// MessageAge: 0,
44+
// MaxAge: 5120,
45+
// HelloTime: 512, // we must divide by 256 to have the value in seconds
46+
// FDelay : 3840,
47+
// }
48+
49+
// 00 00 00 00 01 80 01 AA BB CC 00 01 00 00 00 00 ................
50+
// 00 80 01 AA BB CC 00 01 00 80 01 00 00 14 00 02 ................
51+
// 00 0F 00 ...
52+
53+
func TestPacketSTPNilRdata(t *testing.T) {
54+
p := gopacket.NewPacket(testPacketSTPRDATA, LayerTypeSTP, testDecodeOptions)
55+
if p.ErrorLayer() != nil {
56+
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
57+
}
58+
checkLayers(p, []gopacket.LayerType{LayerTypeSTP}, t)
59+
}
60+
61+
// test decoding stp layer on real packet data
62+
func TestDecodeSTPRData(t *testing.T) {
63+
p := gopacket.NewPacket(testPacketSTPRDATA, LayerTypeSTP, testDecodeOptions)
64+
if p.ErrorLayer() != nil {
65+
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
66+
}
67+
expectedSTP := &STP{
68+
ProtocolID: 0,
69+
Version: 0,
70+
Type: 0,
71+
TCA: false,
72+
TC: true,
73+
RouteID: STPSwitchID{
74+
Priority: 32768,
75+
SysID: 1,
76+
HwAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0x00, 0x01, 0x00},
77+
},
78+
Cost: 0,
79+
BridgeID: STPSwitchID{
80+
Priority: 32768,
81+
SysID: 1,
82+
HwAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0x00, 0x01, 0x00},
83+
},
84+
PortID: 0x8001,
85+
MessageAge: 0,
86+
MaxAge: 5120,
87+
HelloTime: 512, // we must divide by 256 to have the value in seconds
88+
FDelay: 3840,
89+
}
90+
91+
decodedSTP := p.Layer(LayerTypeSTP).(*STP)
92+
decodedSTP.BaseLayer = BaseLayer{}
93+
94+
if !reflect.DeepEqual(expectedSTP, decodedSTP) {
95+
t.Error("Expect ", expectedSTP, "actual ", decodedSTP)
96+
}
97+
98+
}
99+
100+
// test harness to ensure the stp layer can be encoded/decoded properly
101+
// return error if decoded data not match.
102+
func testEncodeDecodeSTP(stp *STP) error {
103+
buf := gopacket.NewSerializeBuffer()
104+
opts := gopacket.SerializeOptions{
105+
// ComputeChecksums: true,
106+
// FixLengths: true,
107+
}
108+
expectedSTP := stp
109+
110+
err := stp.SerializeTo(buf, opts)
111+
if err != nil {
112+
return err
113+
}
114+
115+
newSTP := &STP{}
116+
err = newSTP.DecodeFromBytes(buf.Bytes(), gopacket.NilDecodeFeedback)
117+
if err != nil {
118+
return err
119+
}
120+
newSTP.BaseLayer = BaseLayer{}
121+
122+
if !reflect.DeepEqual(expectedSTP, newSTP) {
123+
return fmt.Errorf("Expect %v actual %v", expectedSTP, newSTP)
124+
}
125+
return nil
126+
127+
}
128+
129+
// Test to ensure what has been encode can be decoded
130+
func TestEncodeDecodeSTP(t *testing.T) {
131+
STPs := []*STP{
132+
&STP{
133+
ProtocolID: 0,
134+
Version: 0,
135+
Type: 0,
136+
RouteID: STPSwitchID{
137+
Priority: 32768,
138+
SysID: 1,
139+
HwAddr: net.HardwareAddr{0x64, 0x5a, 0x04, 0xaf, 0x33, 0xdc},
140+
},
141+
Cost: 0,
142+
BridgeID: STPSwitchID{
143+
Priority: 32768,
144+
SysID: 1,
145+
HwAddr: net.HardwareAddr{0x64, 0x5a, 0x04, 0xaf, 0x33, 0xdc},
146+
},
147+
PortID: 0x8001,
148+
MessageAge: 0,
149+
MaxAge: 20 * 256,
150+
HelloTime: 2 * 256,
151+
FDelay: 15 * 256,
152+
},
153+
&STP{
154+
ProtocolID: 0,
155+
Version: 0,
156+
Type: 0,
157+
RouteID: STPSwitchID{
158+
Priority: 32768,
159+
SysID: 1,
160+
HwAddr: net.HardwareAddr{0x64, 0x5a, 0x04, 0xaf, 0x33, 0xdc},
161+
},
162+
TC: true,
163+
TCA: true,
164+
Cost: 0,
165+
BridgeID: STPSwitchID{
166+
Priority: 32768,
167+
SysID: 1,
168+
HwAddr: net.HardwareAddr{0x64, 0x5a, 0x04, 0xaf, 0x33, 0xdc},
169+
},
170+
PortID: 0x8001,
171+
MessageAge: 0,
172+
MaxAge: 20 * 256,
173+
HelloTime: 2 * 256,
174+
FDelay: 15 * 256,
175+
},
176+
}
177+
178+
for i, curTest := range STPs {
179+
err := testEncodeDecodeSTP(curTest)
180+
if err != nil {
181+
t.Error("Error with item ", i, " with error message :", err)
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)