1 // Copyright 2017 Google, Inc. All rights reserved.
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
7 //******************************************************************************
12 "github.com/google/gopacket"
17 //******************************************************************************
19 // checkBFD() uses the bfd.go code to analyse the packet bytes as an BFD Control
20 // packet and generate an BFD object. It then compares the generated BFD object
21 // with the one provided and throws an error if there is any difference.
22 // The desc argument is output with any failure message to identify the test.
23 func checkBFD(desc string, t *testing.T, packetBytes []byte, pExpectedBFD *BFD) {
25 // Analyse the packet bytes, yielding a new packet object p.
26 p := gopacket.NewPacket(packetBytes, LinkTypeEthernet, gopacket.Default)
27 if p.ErrorLayer() != nil {
28 t.Errorf("Failed to decode packet %s: %v", desc, p.ErrorLayer().Error())
31 // Ensure that the packet analysis yielded the correct set of layers:
32 // Link Layer = Ethernet.
33 // Network Layer = IPv4.
34 // Transport Layer = UDP.
35 // Application Layer = BFD.
36 checkLayers(p, []gopacket.LayerType{
42 // Select the Application (BFD) layer.
43 pResultBFD, ok := p.ApplicationLayer().(*BFD)
45 t.Error("No BFD layer type found in packet in " + desc + ".")
48 // Compare the generated BFD object with the expected BFD object.
49 if !reflect.DeepEqual(pResultBFD, pExpectedBFD) {
50 t.Errorf("BFD packet processing failed for packet "+desc+
51 ":\ngot :\n%#v\n\nwant :\n%#v\n\n", pResultBFD, pExpectedBFD)
53 buf := gopacket.NewSerializeBuffer()
54 opts := gopacket.SerializeOptions{}
55 err := pResultBFD.SerializeTo(buf, opts)
59 if !reflect.DeepEqual(pResultBFD.Contents, buf.Bytes()) {
60 t.Errorf("BFD packet serialization failed for packet "+desc+
61 ":\ngot :\n%+v\n\nwant :\n%+v\n\n", buf.Bytes(), pResultBFD.Contents)
66 func TestBFDNoAuth(t *testing.T) {
67 // This test packet is based off of the first BFD packet in the BFD sample capture
68 // pcap file bfd-raw-auth-simple.pcap on the Wireshark sample captures page:
70 // https://wiki.wireshark.org/SampleCaptures
71 // https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=bfd-raw-auth-simple.pcap
73 // Changed to remove the authentication header, and adjust all of the lengths
74 var testPacketBFD = []byte{
75 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x10, 0x94, 0x00, 0x00, 0x02, 0x08, 0x00, 0x45, 0x00,
76 0x00, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x11, 0x2f, 0x58, 0xc0, 0x55, 0x01, 0x02, 0xc0, 0x00,
77 0x00, 0x01, 0xc0, 0x00, 0x0e, 0xc8, 0x00, 0x20, 0x72, 0x31, 0x20, 0x40, 0x05, 0x18, 0x00, 0x00,
78 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00,
79 0x00, 0x00, 0x01, 0x4e, 0x0a, 0x90, 0x40,
82 // Assemble the BFD object that we expect to emerge from this test.
86 0x20, 0x40, 0x05, 0x18, 0x00, 0x00, 0x00, 0x01,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40,
88 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00, 0x00, 0x00,
93 Diagnostic: BFDDiagnosticNone,
97 ControlPlaneIndependent: false,
103 YourDiscriminator: 0,
104 DesiredMinTxInterval: 1000000,
105 RequiredMinRxInterval: 1000000,
106 RequiredMinEchoRxInterval: 0,
110 checkBFD("testNoAuth", t, testPacketBFD, pExpectedBFD)
113 //******************************************************************************
115 func TestBFDAuthTypePassword(t *testing.T) {
117 // This test packet is the first BFD packet in the BFD sample capture
118 // pcap file bfd-raw-auth-simple.pcap on the Wireshark sample captures page:
120 // https://wiki.wireshark.org/SampleCaptures
121 // https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=bfd-raw-auth-simple.pcap
122 var testPacketBFD = []byte{
123 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x10, 0x94, 0x00, 0x00, 0x02, 0x08, 0x00, 0x45, 0x00,
124 0x00, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x11, 0x2f, 0x58, 0xc0, 0x55, 0x01, 0x02, 0xc0, 0x00,
125 0x00, 0x01, 0xc0, 0x00, 0x0e, 0xc8, 0x00, 0x29, 0x72, 0x31, 0x20, 0x44, 0x05, 0x21, 0x00, 0x00,
126 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00,
127 0x00, 0x00, 0x01, 0x09, 0x02, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4e, 0x0a, 0x90, 0x40,
130 // Assemble the BFD object that we expect to emerge from this test.
131 pExpectedBFD := &BFD{
132 BaseLayer: BaseLayer{
134 0x20, 0x44, 0x05, 0x21, 0x00, 0x00, 0x00, 0x01,
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40,
136 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00, 0x00, 0x00,
137 0x01, 0x09, 0x02, 0x73, 0x65, 0x63, 0x72, 0x65,
143 Diagnostic: BFDDiagnosticNone,
147 ControlPlaneIndependent: false,
153 YourDiscriminator: 0,
154 DesiredMinTxInterval: 1000000,
155 RequiredMinRxInterval: 1000000,
156 RequiredMinEchoRxInterval: 0,
157 AuthHeader: &BFDAuthHeader{
158 AuthType: BFDAuthTypePassword,
161 Data: []byte{'s', 'e', 'c', 'r', 'e', 't'},
165 checkBFD("testBFDAuthTypePassword", t, testPacketBFD, pExpectedBFD)
168 //******************************************************************************
170 func TestBFDAuthTypeKeyedMD5(t *testing.T) {
172 // This test packet is the first BFD packet in the BFD sample capture
173 // pcap file bfd-raw-auth-md5.pcap on the Wireshark sample captures page:
175 // https://wiki.wireshark.org/SampleCaptures
176 // https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=bfd-raw-auth-md5.pcap
177 var testPacketBFD = []byte{
178 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x10, 0x94, 0x00, 0x00, 0x02, 0x08, 0x00, 0x45, 0x00,
179 0x00, 0x4c, 0x00, 0x01, 0x00, 0x00, 0x0a, 0x11, 0x2f, 0x48, 0xc0, 0x55, 0x01, 0x02, 0xc0, 0x00,
180 0x00, 0x01, 0x04, 0x00, 0x0e, 0xc8, 0x00, 0x38, 0x6a, 0xcc, 0x20, 0x44, 0x05, 0x30, 0x00, 0x00,
181 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00,
182 0x00, 0x00, 0x02, 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
183 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x3c, 0xc3, 0xf8, 0x21,
186 // Assemble the BFD object that we expect to emerge from this test.
187 pExpectedBFD := &BFD{
188 BaseLayer: BaseLayer{
190 0x20, 0x44, 0x05, 0x30, 0x00, 0x00, 0x00, 0x01,
191 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40,
192 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00, 0x00, 0x00,
193 0x02, 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05,
194 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
195 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
200 Diagnostic: BFDDiagnosticNone,
204 ControlPlaneIndependent: false,
210 YourDiscriminator: 0,
211 DesiredMinTxInterval: 1000000,
212 RequiredMinRxInterval: 1000000,
213 RequiredMinEchoRxInterval: 0,
214 AuthHeader: &BFDAuthHeader{
215 AuthType: BFDAuthTypeKeyedMD5,
219 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
220 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
225 checkBFD("testBFDAuthTypeKeyedMD5", t, testPacketBFD, pExpectedBFD)
228 //******************************************************************************
230 func TestBFDAuthTypeMeticulousKeyedSHA1(t *testing.T) {
232 // This test packet is the first BFD packet in the BFD sample capture
233 // pcap file bfd-raw-auth-sha1.pcap on the Wireshark sample captures page:
235 // https://wiki.wireshark.org/SampleCaptures
236 // https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=bfd-raw-auth-sha1.pcap
237 var testPacketBFD = []byte{
238 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x10, 0x94, 0x00, 0x00, 0x02, 0x08, 0x00, 0x45, 0x00,
239 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x11, 0x2f, 0x45, 0xc0, 0x55, 0x01, 0x02, 0xc0, 0x00,
240 0x00, 0x01, 0x04, 0x00, 0x0e, 0xc8, 0x00, 0x3c, 0x37, 0x8a, 0x20, 0x44, 0x05, 0x34, 0x00, 0x00,
241 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00,
242 0x00, 0x00, 0x05, 0x1c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
243 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0xea, 0x6d,
247 // Assemble the BFD object that we expect to emerge from this test.
248 pExpectedBFD := &BFD{
249 BaseLayer: BaseLayer{
251 0x20, 0x44, 0x05, 0x34, 0x00, 0x00, 0x00, 0x01,
252 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40,
253 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00, 0x00, 0x00,
254 0x05, 0x1c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05,
255 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
256 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
257 0x17, 0x18, 0x19, 0x1a,
262 Diagnostic: BFDDiagnosticNone,
266 ControlPlaneIndependent: false,
272 YourDiscriminator: 0,
273 DesiredMinTxInterval: 1000000,
274 RequiredMinRxInterval: 1000000,
275 RequiredMinEchoRxInterval: 0,
276 AuthHeader: &BFDAuthHeader{
277 AuthType: BFDAuthTypeMeticulousKeyedSHA1,
281 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
282 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
283 0x17, 0x18, 0x19, 0x1a,
288 checkBFD("TestBFDAuthTypeMeticulousKeyedSHA1", t, testPacketBFD, pExpectedBFD)