github.com/gopacket/gopacket@v1.1.0/layers/stp_test.go (about) 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/gopacket/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 }