github.com/osrg/gobgp/v3@v3.30.0/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/v3/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_RouteMonitoringAdjRIBOut(t *testing.T) { 75 m := bgp.NewTestBGPUpdateMessage() 76 p0 := NewBMPPeerHeader(0, 16, 1000, "10.0.0.1", 12345, "10.0.0.2", 1) 77 assert.True(t, p0.IsAdjRIBOut()) 78 verify(t, NewBMPRouteMonitoring(*p0, m)) 79 } 80 81 func Test_RouteMonitoringAddPath(t *testing.T) { 82 opt := &bgp.MarshallingOption{ 83 AddPath: map[bgp.RouteFamily]bgp.BGPAddPathMode{bgp.RF_IPv4_UC: bgp.BGP_ADD_PATH_BOTH}, 84 } 85 p1 := bgp.NewIPAddrPrefix(24, "10.10.10.0") 86 p1.SetPathLocalIdentifier(10) 87 p := []bgp.PathAttributeInterface{ 88 bgp.NewPathAttributeOrigin(3), 89 bgp.NewPathAttributeNextHop("129.1.1.2"), 90 } 91 m := bgp.NewBGPUpdateMessage([]*bgp.IPAddrPrefix{}, p, []*bgp.IPAddrPrefix{p1}) 92 p0 := NewBMPPeerHeader(0, 0, 1000, "fe80::6e40:8ff:feab:2c2a", 70000, "10.0.0.2", 1) 93 94 m1 := NewBMPRouteMonitoring(*p0, m) 95 buf1, _ := m1.Serialize(opt) 96 m2, err := ParseBMPMessageWithOptions(buf1, func(BMPPeerHeader) []*bgp.MarshallingOption { 97 return []*bgp.MarshallingOption{opt} 98 }) 99 require.NoError(t, err) 100 101 // We need to fix tha path identifier (local/remote) 102 u2 := m2.Body.(*BMPRouteMonitoring).BGPUpdate.Body.(*bgp.BGPUpdate).NLRI[0] 103 assert.Equal(t, u2.PathIdentifier(), uint32(10)) 104 assert.Equal(t, u2.PathLocalIdentifier(), uint32(0)) 105 u2.SetPathIdentifier(0) 106 u2.SetPathLocalIdentifier(10) 107 108 assert.Equal(t, m1, m2) 109 } 110 111 func Test_StatisticsReport(t *testing.T) { 112 p0 := NewBMPPeerHeader(0, 0, 1000, "10.0.0.1", 70000, "10.0.0.2", 1) 113 s0 := NewBMPStatisticsReport( 114 *p0, 115 []BMPStatsTLVInterface{ 116 NewBMPStatsTLV32(BMP_STAT_TYPE_REJECTED, 100), 117 NewBMPStatsTLV64(BMP_STAT_TYPE_ADJ_RIB_IN, 200), 118 NewBMPStatsTLVPerAfiSafi64(BMP_STAT_TYPE_PER_AFI_SAFI_LOC_RIB, bgp.AFI_IP, bgp.SAFI_UNICAST, 300), 119 }, 120 ) 121 verify(t, s0) 122 } 123 124 func Test_StatisticsReportAdjRIBOut(t *testing.T) { 125 p0 := NewBMPPeerHeader(0, 8, 1000, "10.0.0.1", 12345, "10.0.0.2", 1) 126 s0 := NewBMPStatisticsReport( 127 *p0, 128 []BMPStatsTLVInterface{ 129 NewBMPStatsTLV64(BMP_STAT_TYPE_ADJ_RIB_OUT_POST_POLICY, 200), 130 NewBMPStatsTLVPerAfiSafi64(BMP_STAT_TYPE_PER_AFI_SAFI_ADJ_RIB_OUT_POST_POLICY, bgp.AFI_IP, bgp.SAFI_UNICAST, 300), 131 }, 132 ) 133 verify(t, s0) 134 } 135 136 func Test_RouteMirroring(t *testing.T) { 137 p0 := NewBMPPeerHeader(0, 0, 1000, "10.0.0.1", 70000, "10.0.0.2", 1) 138 s0 := NewBMPRouteMirroring( 139 *p0, 140 []BMPRouteMirrTLVInterface{ 141 NewBMPRouteMirrTLV16(BMP_ROUTE_MIRRORING_TLV_TYPE_INFO, BMP_ROUTE_MIRRORING_INFO_MSG_LOST), 142 NewBMPRouteMirrTLVUnknown(0xff, []byte{0x01, 0x02, 0x03, 0x04}), 143 // RFC7854: BGP Message TLV MUST occur last in the list of TLVs 144 NewBMPRouteMirrTLVBGPMsg(BMP_ROUTE_MIRRORING_TLV_TYPE_BGP_MSG, bgp.NewTestBGPOpenMessage()), 145 }, 146 ) 147 verify(t, s0) 148 } 149 150 func Test_BogusHeader(t *testing.T) { 151 h, err := ParseBMPMessage(make([]byte, 10)) 152 assert.Nil(t, h) 153 assert.NotNil(t, err) 154 } 155 156 func Test_RouteMonitoringUnknownType(t *testing.T) { 157 data := []byte{0x03, 0x00, 0x00, 0x00, 0xe4, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x01, 0x04, 0x70, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x1b, 0x1b, 0xd8, 0xda, 0xfc, 0xa4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2b, 0xb5, 0x00, 0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2b, 0xb5, 0x7f, 0xff, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x80, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xeb, 0x80, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x02, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xeb, 0x80, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x80, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x54, 0x80, 0x05, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x80, 0x06, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xa0} 158 _, err := ParseBMPMessage(data) 159 require.NoError(t, err) 160 } 161 162 func FuzzParseBMPMessage(f *testing.F) { 163 164 f.Fuzz(func(t *testing.T, data []byte) { 165 ParseBMPMessage(data) 166 }) 167 }