github.com/gopacket/gopacket@v1.1.0/layers/bfd_test.go (about)

     1  // Copyright 2017 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  //
     7  //******************************************************************************
     8  
     9  package layers
    10  
    11  import (
    12  	"reflect"
    13  	"testing"
    14  
    15  	"github.com/gopacket/gopacket"
    16  )
    17  
    18  //******************************************************************************
    19  
    20  // checkBFD() uses the bfd.go code to analyse the packet bytes as an BFD Control
    21  // packet and generate an BFD object. It then compares the generated BFD object
    22  // with the one provided and throws an error if there is any difference.
    23  // The desc argument is output with any failure message to identify the test.
    24  func checkBFD(desc string, t *testing.T, packetBytes []byte, pExpectedBFD *BFD) {
    25  
    26  	// Analyse the packet bytes, yielding a new packet object p.
    27  	p := gopacket.NewPacket(packetBytes, LinkTypeEthernet, gopacket.Default)
    28  	if p.ErrorLayer() != nil {
    29  		t.Errorf("Failed to decode packet %s: %v", desc, p.ErrorLayer().Error())
    30  	}
    31  
    32  	// Ensure that the packet analysis yielded the correct set of layers:
    33  	//    Link Layer        = Ethernet.
    34  	//    Network Layer     = IPv4.
    35  	//    Transport Layer   = UDP.
    36  	//    Application Layer = BFD.
    37  	checkLayers(p, []gopacket.LayerType{
    38  		LayerTypeEthernet,
    39  		LayerTypeIPv4,
    40  		LayerTypeUDP,
    41  		LayerTypeBFD}, t)
    42  
    43  	// Select the Application (BFD) layer.
    44  	pResultBFD, ok := p.ApplicationLayer().(*BFD)
    45  	if !ok {
    46  		t.Error("No BFD layer type found in packet in " + desc + ".")
    47  	}
    48  
    49  	// Compare the generated BFD object with the expected BFD object.
    50  	if !reflect.DeepEqual(pResultBFD, pExpectedBFD) {
    51  		t.Errorf("BFD packet processing failed for packet "+desc+
    52  			":\ngot  :\n%#v\n\nwant :\n%#v\n\n", pResultBFD, pExpectedBFD)
    53  	}
    54  	buf := gopacket.NewSerializeBuffer()
    55  	opts := gopacket.SerializeOptions{}
    56  	err := pResultBFD.SerializeTo(buf, opts)
    57  	if err != nil {
    58  		t.Error(err)
    59  	}
    60  	if !reflect.DeepEqual(pResultBFD.Contents, buf.Bytes()) {
    61  		t.Errorf("BFD packet serialization failed for packet "+desc+
    62  			":\ngot  :\n%+v\n\nwant :\n%+v\n\n", buf.Bytes(), pResultBFD.Contents)
    63  	}
    64  
    65  }
    66  
    67  func TestBFDNoAuth(t *testing.T) {
    68  	// This test packet is based off of the first BFD packet in the BFD sample capture
    69  	// pcap file bfd-raw-auth-simple.pcap on the Wireshark sample captures page:
    70  	//
    71  	//    https://wiki.wireshark.org/SampleCaptures
    72  	//    https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=bfd-raw-auth-simple.pcap
    73  	//
    74  	// Changed to remove the authentication header, and adjust all of the lengths
    75  	var testPacketBFD = []byte{
    76  		0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x10, 0x94, 0x00, 0x00, 0x02, 0x08, 0x00, 0x45, 0x00,
    77  		0x00, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x11, 0x2f, 0x58, 0xc0, 0x55, 0x01, 0x02, 0xc0, 0x00,
    78  		0x00, 0x01, 0xc0, 0x00, 0x0e, 0xc8, 0x00, 0x20, 0x72, 0x31, 0x20, 0x40, 0x05, 0x18, 0x00, 0x00,
    79  		0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00,
    80  		0x00, 0x00, 0x01, 0x4e, 0x0a, 0x90, 0x40,
    81  	}
    82  
    83  	// Assemble the BFD object that we expect to emerge from this test.
    84  	pExpectedBFD := &BFD{
    85  		BaseLayer: BaseLayer{
    86  			Contents: []byte{
    87  				0x20, 0x40, 0x05, 0x18, 0x00, 0x00, 0x00, 0x01,
    88  				0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40,
    89  				0x00, 0x0f, 0x42, 0x40, 0x00, 0x00, 0x00, 0x00,
    90  			},
    91  			Payload: nil,
    92  		},
    93  		Version:                   1,
    94  		Diagnostic:                BFDDiagnosticNone,
    95  		State:                     BFDStateDown,
    96  		Poll:                      false,
    97  		Final:                     false,
    98  		ControlPlaneIndependent:   false,
    99  		AuthPresent:               false,
   100  		Demand:                    false,
   101  		Multipoint:                false,
   102  		DetectMultiplier:          5,
   103  		MyDiscriminator:           1,
   104  		YourDiscriminator:         0,
   105  		DesiredMinTxInterval:      1000000,
   106  		RequiredMinRxInterval:     1000000,
   107  		RequiredMinEchoRxInterval: 0,
   108  		AuthHeader:                nil,
   109  	}
   110  
   111  	checkBFD("testNoAuth", t, testPacketBFD, pExpectedBFD)
   112  }
   113  
   114  //******************************************************************************
   115  
   116  func TestBFDAuthTypePassword(t *testing.T) {
   117  
   118  	// This test packet is the first BFD packet in the BFD sample capture
   119  	// pcap file bfd-raw-auth-simple.pcap on the Wireshark sample captures page:
   120  	//
   121  	//    https://wiki.wireshark.org/SampleCaptures
   122  	//    https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=bfd-raw-auth-simple.pcap
   123  	var testPacketBFD = []byte{
   124  		0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x10, 0x94, 0x00, 0x00, 0x02, 0x08, 0x00, 0x45, 0x00,
   125  		0x00, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x11, 0x2f, 0x58, 0xc0, 0x55, 0x01, 0x02, 0xc0, 0x00,
   126  		0x00, 0x01, 0xc0, 0x00, 0x0e, 0xc8, 0x00, 0x29, 0x72, 0x31, 0x20, 0x44, 0x05, 0x21, 0x00, 0x00,
   127  		0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00,
   128  		0x00, 0x00, 0x01, 0x09, 0x02, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4e, 0x0a, 0x90, 0x40,
   129  	}
   130  
   131  	// Assemble the BFD object that we expect to emerge from this test.
   132  	pExpectedBFD := &BFD{
   133  		BaseLayer: BaseLayer{
   134  			Contents: []byte{
   135  				0x20, 0x44, 0x05, 0x21, 0x00, 0x00, 0x00, 0x01,
   136  				0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40,
   137  				0x00, 0x0f, 0x42, 0x40, 0x00, 0x00, 0x00, 0x00,
   138  				0x01, 0x09, 0x02, 0x73, 0x65, 0x63, 0x72, 0x65,
   139  				0x74,
   140  			},
   141  			Payload: nil,
   142  		},
   143  		Version:                   1,
   144  		Diagnostic:                BFDDiagnosticNone,
   145  		State:                     BFDStateDown,
   146  		Poll:                      false,
   147  		Final:                     false,
   148  		ControlPlaneIndependent:   false,
   149  		AuthPresent:               true,
   150  		Demand:                    false,
   151  		Multipoint:                false,
   152  		DetectMultiplier:          5,
   153  		MyDiscriminator:           1,
   154  		YourDiscriminator:         0,
   155  		DesiredMinTxInterval:      1000000,
   156  		RequiredMinRxInterval:     1000000,
   157  		RequiredMinEchoRxInterval: 0,
   158  		AuthHeader: &BFDAuthHeader{
   159  			AuthType:       BFDAuthTypePassword,
   160  			KeyID:          2,
   161  			SequenceNumber: 0,
   162  			Data:           []byte{'s', 'e', 'c', 'r', 'e', 't'},
   163  		},
   164  	}
   165  
   166  	checkBFD("testBFDAuthTypePassword", t, testPacketBFD, pExpectedBFD)
   167  }
   168  
   169  //******************************************************************************
   170  
   171  func TestBFDAuthTypeKeyedMD5(t *testing.T) {
   172  
   173  	// This test packet is the first BFD packet in the BFD sample capture
   174  	// pcap file bfd-raw-auth-md5.pcap on the Wireshark sample captures page:
   175  	//
   176  	//    https://wiki.wireshark.org/SampleCaptures
   177  	//    https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=bfd-raw-auth-md5.pcap
   178  	var testPacketBFD = []byte{
   179  		0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x10, 0x94, 0x00, 0x00, 0x02, 0x08, 0x00, 0x45, 0x00,
   180  		0x00, 0x4c, 0x00, 0x01, 0x00, 0x00, 0x0a, 0x11, 0x2f, 0x48, 0xc0, 0x55, 0x01, 0x02, 0xc0, 0x00,
   181  		0x00, 0x01, 0x04, 0x00, 0x0e, 0xc8, 0x00, 0x38, 0x6a, 0xcc, 0x20, 0x44, 0x05, 0x30, 0x00, 0x00,
   182  		0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00,
   183  		0x00, 0x00, 0x02, 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
   184  		0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x3c, 0xc3, 0xf8, 0x21,
   185  	}
   186  
   187  	// Assemble the BFD object that we expect to emerge from this test.
   188  	pExpectedBFD := &BFD{
   189  		BaseLayer: BaseLayer{
   190  			Contents: []byte{
   191  				0x20, 0x44, 0x05, 0x30, 0x00, 0x00, 0x00, 0x01,
   192  				0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40,
   193  				0x00, 0x0f, 0x42, 0x40, 0x00, 0x00, 0x00, 0x00,
   194  				0x02, 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05,
   195  				0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   196  				0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
   197  			},
   198  			Payload: nil,
   199  		},
   200  		Version:                   1,
   201  		Diagnostic:                BFDDiagnosticNone,
   202  		State:                     BFDStateDown,
   203  		Poll:                      false,
   204  		Final:                     false,
   205  		ControlPlaneIndependent:   false,
   206  		AuthPresent:               true,
   207  		Demand:                    false,
   208  		Multipoint:                false,
   209  		DetectMultiplier:          5,
   210  		MyDiscriminator:           1,
   211  		YourDiscriminator:         0,
   212  		DesiredMinTxInterval:      1000000,
   213  		RequiredMinRxInterval:     1000000,
   214  		RequiredMinEchoRxInterval: 0,
   215  		AuthHeader: &BFDAuthHeader{
   216  			AuthType:       BFDAuthTypeKeyedMD5,
   217  			KeyID:          2,
   218  			SequenceNumber: 5,
   219  			Data: []byte{
   220  				0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   221  				0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
   222  			},
   223  		},
   224  	}
   225  
   226  	checkBFD("testBFDAuthTypeKeyedMD5", t, testPacketBFD, pExpectedBFD)
   227  }
   228  
   229  //******************************************************************************
   230  
   231  func TestBFDAuthTypeMeticulousKeyedSHA1(t *testing.T) {
   232  
   233  	// This test packet is the first BFD packet in the BFD sample capture
   234  	// pcap file bfd-raw-auth-sha1.pcap on the Wireshark sample captures page:
   235  	//
   236  	//    https://wiki.wireshark.org/SampleCaptures
   237  	//    https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=bfd-raw-auth-sha1.pcap
   238  	var testPacketBFD = []byte{
   239  		0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x10, 0x94, 0x00, 0x00, 0x02, 0x08, 0x00, 0x45, 0x00,
   240  		0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x11, 0x2f, 0x45, 0xc0, 0x55, 0x01, 0x02, 0xc0, 0x00,
   241  		0x00, 0x01, 0x04, 0x00, 0x0e, 0xc8, 0x00, 0x3c, 0x37, 0x8a, 0x20, 0x44, 0x05, 0x34, 0x00, 0x00,
   242  		0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x0f, 0x42, 0x40, 0x00, 0x00,
   243  		0x00, 0x00, 0x05, 0x1c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
   244  		0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0xea, 0x6d,
   245  		0x1f, 0x21,
   246  	}
   247  
   248  	// Assemble the BFD object that we expect to emerge from this test.
   249  	pExpectedBFD := &BFD{
   250  		BaseLayer: BaseLayer{
   251  			Contents: []byte{
   252  				0x20, 0x44, 0x05, 0x34, 0x00, 0x00, 0x00, 0x01,
   253  				0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40,
   254  				0x00, 0x0f, 0x42, 0x40, 0x00, 0x00, 0x00, 0x00,
   255  				0x05, 0x1c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05,
   256  				0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   257  				0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
   258  				0x17, 0x18, 0x19, 0x1a,
   259  			},
   260  			Payload: nil,
   261  		},
   262  		Version:                   1,
   263  		Diagnostic:                BFDDiagnosticNone,
   264  		State:                     BFDStateDown,
   265  		Poll:                      false,
   266  		Final:                     false,
   267  		ControlPlaneIndependent:   false,
   268  		AuthPresent:               true,
   269  		Demand:                    false,
   270  		Multipoint:                false,
   271  		DetectMultiplier:          5,
   272  		MyDiscriminator:           1,
   273  		YourDiscriminator:         0,
   274  		DesiredMinTxInterval:      1000000,
   275  		RequiredMinRxInterval:     1000000,
   276  		RequiredMinEchoRxInterval: 0,
   277  		AuthHeader: &BFDAuthHeader{
   278  			AuthType:       BFDAuthTypeMeticulousKeyedSHA1,
   279  			KeyID:          2,
   280  			SequenceNumber: 5,
   281  			Data: []byte{
   282  				0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   283  				0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
   284  				0x17, 0x18, 0x19, 0x1a,
   285  			},
   286  		},
   287  	}
   288  
   289  	checkBFD("TestBFDAuthTypeMeticulousKeyedSHA1", t, testPacketBFD, pExpectedBFD)
   290  }