github.com/osrg/gobgp@v2.0.0+incompatible/pkg/packet/bmp/bmp_test.go (about)

     1  // Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package bmp
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/osrg/gobgp/pkg/packet/bgp"
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func verify(t *testing.T, m1 *BMPMessage) {
    27  	buf1, _ := m1.Serialize()
    28  	m2, err := ParseBMPMessage(buf1)
    29  	require.NoError(t, err)
    30  
    31  	assert.Equal(t, m1, m2)
    32  }
    33  
    34  func Test_Initiation(t *testing.T) {
    35  	verify(t, NewBMPInitiation(nil))
    36  	m := NewBMPInitiation([]BMPInfoTLVInterface{
    37  		NewBMPInfoTLVString(BMP_INIT_TLV_TYPE_STRING, "free-form UTF-8 string"),
    38  		NewBMPInfoTLVUnknown(0xff, []byte{0x01, 0x02, 0x03, 0x04}),
    39  	})
    40  	verify(t, m)
    41  }
    42  
    43  func Test_Termination(t *testing.T) {
    44  	verify(t, NewBMPTermination(nil))
    45  	m := NewBMPTermination([]BMPTermTLVInterface{
    46  		NewBMPTermTLVString(BMP_TERM_TLV_TYPE_STRING, "free-form UTF-8 string"),
    47  		NewBMPTermTLV16(BMP_TERM_TLV_TYPE_REASON, BMP_TERM_REASON_ADMIN),
    48  		NewBMPTermTLVUnknown(0xff, []byte{0x01, 0x02, 0x03, 0x04}),
    49  	})
    50  	verify(t, m)
    51  }
    52  
    53  func Test_PeerUpNotification(t *testing.T) {
    54  	m := bgp.NewTestBGPOpenMessage()
    55  	p0 := NewBMPPeerHeader(0, 0, 1000, "10.0.0.1", 70000, "10.0.0.2", 1)
    56  	verify(t, NewBMPPeerUpNotification(*p0, "10.0.0.3", 10, 100, m, m))
    57  	p1 := NewBMPPeerHeader(0, 0, 1000, "fe80::6e40:8ff:feab:2c2a", 70000, "10.0.0.2", 1)
    58  	verify(t, NewBMPPeerUpNotification(*p1, "fe80::6e40:8ff:feab:2c2a", 10, 100, m, m))
    59  }
    60  
    61  func Test_PeerDownNotification(t *testing.T) {
    62  	p0 := NewBMPPeerHeader(0, 0, 1000, "10.0.0.1", 70000, "10.0.0.2", 1)
    63  	verify(t, NewBMPPeerDownNotification(*p0, BMP_PEER_DOWN_REASON_LOCAL_NO_NOTIFICATION, nil, []byte{0x3, 0xb}))
    64  	m := bgp.NewBGPNotificationMessage(1, 2, nil)
    65  	verify(t, NewBMPPeerDownNotification(*p0, BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION, m, nil))
    66  }
    67  
    68  func Test_RouteMonitoring(t *testing.T) {
    69  	m := bgp.NewTestBGPUpdateMessage()
    70  	p0 := NewBMPPeerHeader(0, 0, 1000, "fe80::6e40:8ff:feab:2c2a", 70000, "10.0.0.2", 1)
    71  	verify(t, NewBMPRouteMonitoring(*p0, m))
    72  }
    73  
    74  func Test_StatisticsReport(t *testing.T) {
    75  	p0 := NewBMPPeerHeader(0, 0, 1000, "10.0.0.1", 70000, "10.0.0.2", 1)
    76  	s0 := NewBMPStatisticsReport(
    77  		*p0,
    78  		[]BMPStatsTLVInterface{
    79  			NewBMPStatsTLV32(BMP_STAT_TYPE_REJECTED, 100),
    80  			NewBMPStatsTLV64(BMP_STAT_TYPE_ADJ_RIB_IN, 200),
    81  			NewBMPStatsTLVPerAfiSafi64(BMP_STAT_TYPE_PER_AFI_SAFI_LOC_RIB, bgp.AFI_IP, bgp.SAFI_UNICAST, 300),
    82  		},
    83  	)
    84  	verify(t, s0)
    85  }
    86  
    87  func Test_RouteMirroring(t *testing.T) {
    88  	p0 := NewBMPPeerHeader(0, 0, 1000, "10.0.0.1", 70000, "10.0.0.2", 1)
    89  	s0 := NewBMPRouteMirroring(
    90  		*p0,
    91  		[]BMPRouteMirrTLVInterface{
    92  			NewBMPRouteMirrTLV16(BMP_ROUTE_MIRRORING_TLV_TYPE_INFO, BMP_ROUTE_MIRRORING_INFO_MSG_LOST),
    93  			NewBMPRouteMirrTLVUnknown(0xff, []byte{0x01, 0x02, 0x03, 0x04}),
    94  			// RFC7854: BGP Message TLV MUST occur last in the list of TLVs
    95  			NewBMPRouteMirrTLVBGPMsg(BMP_ROUTE_MIRRORING_TLV_TYPE_BGP_MSG, bgp.NewTestBGPOpenMessage()),
    96  		},
    97  	)
    98  	verify(t, s0)
    99  }
   100  
   101  func Test_BogusHeader(t *testing.T) {
   102  	h, err := ParseBMPMessage(make([]byte, 10))
   103  	assert.Nil(t, h)
   104  	assert.NotNil(t, err)
   105  }