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

     1  // Copyright (C) 2016 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 bgp
    17  
    18  import (
    19  	"bytes"
    20  	"encoding/binary"
    21  	"net"
    22  	"reflect"
    23  	"strconv"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func keepalive() *BGPMessage {
    31  	return NewBGPKeepAliveMessage()
    32  }
    33  
    34  func notification() *BGPMessage {
    35  	return NewBGPNotificationMessage(1, 2, nil)
    36  }
    37  
    38  func refresh() *BGPMessage {
    39  	return NewBGPRouteRefreshMessage(1, 2, 10)
    40  }
    41  
    42  var result []string
    43  
    44  func BenchmarkNormalizeFlowSpecOpValues(b *testing.B) {
    45  	var r []string
    46  	for n := 0; n < b.N; n++ {
    47  		r = normalizeFlowSpecOpValues([]string{"&<=80"})
    48  	}
    49  	result = r
    50  }
    51  
    52  func Test_Message(t *testing.T) {
    53  	l := []*BGPMessage{keepalive(), notification(), refresh(), NewTestBGPOpenMessage(), NewTestBGPUpdateMessage()}
    54  
    55  	for _, m1 := range l {
    56  		buf1, err := m1.Serialize()
    57  		assert.NoError(t, err)
    58  
    59  		t.Log("LEN =", len(buf1))
    60  		m2, err := ParseBGPMessage(buf1)
    61  		assert.NoError(t, err)
    62  
    63  		// FIXME: shouldn't but workaround for some structs.
    64  		_, err = m2.Serialize()
    65  		assert.NoError(t, err)
    66  
    67  		assert.True(t, reflect.DeepEqual(m1, m2))
    68  	}
    69  }
    70  
    71  func Test_IPAddrPrefixString(t *testing.T) {
    72  	ipv4 := NewIPAddrPrefix(24, "129.6.10.0")
    73  	assert.Equal(t, "129.6.10.0/24", ipv4.String())
    74  	ipv4 = NewIPAddrPrefix(24, "129.6.10.1")
    75  	assert.Equal(t, "129.6.10.0/24", ipv4.String())
    76  	ipv4 = NewIPAddrPrefix(22, "129.6.129.0")
    77  	assert.Equal(t, "129.6.128.0/22", ipv4.String())
    78  
    79  	ipv6 := NewIPv6AddrPrefix(64, "3343:faba:3903::0")
    80  	assert.Equal(t, "3343:faba:3903::/64", ipv6.String())
    81  	ipv6 = NewIPv6AddrPrefix(64, "3343:faba:3903::1")
    82  	assert.Equal(t, "3343:faba:3903::/64", ipv6.String())
    83  	ipv6 = NewIPv6AddrPrefix(63, "3343:faba:3903:129::0")
    84  	assert.Equal(t, "3343:faba:3903:128::/63", ipv6.String())
    85  }
    86  
    87  func Test_RouteTargetMembershipNLRIString(t *testing.T) {
    88  	assert := assert.New(t)
    89  
    90  	// TwoOctetAsSpecificExtended
    91  	buf := make([]byte, 13)
    92  	buf[0] = 96 // in bit length
    93  	binary.BigEndian.PutUint32(buf[1:5], 65546)
    94  	buf[5] = byte(EC_TYPE_TRANSITIVE_TWO_OCTET_AS_SPECIFIC) // typehigh
    95  	binary.BigEndian.PutUint16(buf[7:9], 65000)
    96  	binary.BigEndian.PutUint32(buf[9:], 65546)
    97  	r := &RouteTargetMembershipNLRI{}
    98  	err := r.DecodeFromBytes(buf)
    99  	assert.Equal(nil, err)
   100  	assert.Equal("65546:65000:65546", r.String())
   101  	buf, err = r.Serialize()
   102  	assert.Equal(nil, err)
   103  	err = r.DecodeFromBytes(buf)
   104  	assert.Equal(nil, err)
   105  	assert.Equal("65546:65000:65546", r.String())
   106  
   107  	// IPv4AddressSpecificExtended
   108  	buf = make([]byte, 13)
   109  	buf[0] = 96 // in bit length
   110  	binary.BigEndian.PutUint32(buf[1:5], 65546)
   111  	buf[5] = byte(EC_TYPE_TRANSITIVE_IP4_SPECIFIC) // typehigh
   112  	ip := net.ParseIP("10.0.0.1").To4()
   113  	copy(buf[7:11], []byte(ip))
   114  	binary.BigEndian.PutUint16(buf[11:], 65000)
   115  	r = &RouteTargetMembershipNLRI{}
   116  	err = r.DecodeFromBytes(buf)
   117  	assert.Equal(nil, err)
   118  	assert.Equal("65546:10.0.0.1:65000", r.String())
   119  	buf, err = r.Serialize()
   120  	assert.Equal(nil, err)
   121  	err = r.DecodeFromBytes(buf)
   122  	assert.Equal(nil, err)
   123  	assert.Equal("65546:10.0.0.1:65000", r.String())
   124  
   125  	// FourOctetAsSpecificExtended
   126  	buf = make([]byte, 13)
   127  	buf[0] = 96 // in bit length
   128  	binary.BigEndian.PutUint32(buf[1:5], 65546)
   129  	buf[5] = byte(EC_TYPE_TRANSITIVE_FOUR_OCTET_AS_SPECIFIC) // typehigh
   130  	buf[6] = byte(EC_SUBTYPE_ROUTE_TARGET)                   // subtype
   131  	binary.BigEndian.PutUint32(buf[7:], 65546)
   132  	binary.BigEndian.PutUint16(buf[11:], 65000)
   133  	r = &RouteTargetMembershipNLRI{}
   134  	err = r.DecodeFromBytes(buf)
   135  	assert.Equal(nil, err)
   136  	assert.Equal("65546:1.10:65000", r.String())
   137  	buf, err = r.Serialize()
   138  	assert.Equal(nil, err)
   139  	err = r.DecodeFromBytes(buf)
   140  	assert.Equal(nil, err)
   141  	assert.Equal("65546:1.10:65000", r.String())
   142  
   143  	// OpaqueExtended
   144  	buf = make([]byte, 13)
   145  	buf[0] = 96 // in bit length
   146  	binary.BigEndian.PutUint32(buf[1:5], 65546)
   147  	buf[5] = byte(EC_TYPE_TRANSITIVE_OPAQUE) // typehigh
   148  	binary.BigEndian.PutUint32(buf[9:], 1000000)
   149  	r = &RouteTargetMembershipNLRI{}
   150  	err = r.DecodeFromBytes(buf)
   151  	assert.Equal(nil, err)
   152  	assert.Equal("65546:1000000", r.String())
   153  	buf, err = r.Serialize()
   154  	assert.Equal(nil, err)
   155  	err = r.DecodeFromBytes(buf)
   156  	assert.Equal(nil, err)
   157  	assert.Equal("65546:1000000", r.String())
   158  
   159  	// Unknown
   160  	buf = make([]byte, 13)
   161  	buf[0] = 96 // in bit length
   162  	binary.BigEndian.PutUint32(buf[1:5], 65546)
   163  	buf[5] = 0x04 // typehigh
   164  	binary.BigEndian.PutUint32(buf[9:], 1000000)
   165  	r = &RouteTargetMembershipNLRI{}
   166  	err = r.DecodeFromBytes(buf)
   167  	assert.Equal(nil, err)
   168  	assert.Equal("65546:1000000", r.String())
   169  	buf, err = r.Serialize()
   170  	assert.Equal(nil, err)
   171  	err = r.DecodeFromBytes(buf)
   172  	assert.Equal(nil, err)
   173  	assert.Equal("65546:1000000", r.String())
   174  
   175  }
   176  
   177  func Test_MalformedUpdateMsg(t *testing.T) {
   178  	assert := assert.New(t)
   179  
   180  	// Invalid AGGREGATOR
   181  	bufin := []byte{
   182  		0x00, 0x00, // Withdraws(0)
   183  		0x00, 0x16, // Attrs Len(22)
   184  		0xc0, 0x07, 0x05, // Flag, Type(7), Length(5)
   185  		0x00, 0x00, 0x00, 0x64, // aggregator - invalid length
   186  		0x00,
   187  		0x40, 0x01, 0x01, 0x00, // Attr(ORIGIN)
   188  		0x40, 0x03, 0x04, 0xc0, // Attr(NEXT_HOP)
   189  		0xa8, 0x01, 0x64,
   190  		0x40, 0x02, 0x00, // Attr(AS_PATH)
   191  	}
   192  
   193  	u := &BGPUpdate{}
   194  	err := u.DecodeFromBytes(bufin)
   195  	assert.Error(err)
   196  	assert.Equal(ERROR_HANDLING_ATTRIBUTE_DISCARD, err.(*MessageError).ErrorHandling)
   197  
   198  	// Invalid MP_REACH_NLRI
   199  	bufin = []byte{
   200  		0x00, 0x00, // Withdraws(0)
   201  		0x00, 0x27, // Attrs Len(39)
   202  		0x80, 0x0e, 0x1d, // Flag, Type(14), Length(29)
   203  		0x00, 0x02, 0x01, // afi(2), safi(1)
   204  		0x0f, 0x00, 0x00, 0x00, // nexthop - invalid nexthop length
   205  		0x00, 0x00, 0x00, 0x00,
   206  		0x00, 0x00, 0x00, 0xff,
   207  		0xff, 0x0a, 0x00, 0x00,
   208  		0x00,                   // SNPA(0)
   209  		0x40, 0x20, 0x01, 0x0d, // NLRI
   210  		0xb8, 0x00, 0x01, 0x00,
   211  		0x00,
   212  		0x40, 0x01, 0x01, 0x00, // Attr(ORIGIN)
   213  		0x40, 0x02, 0x00, // Attr(AS_PATH)
   214  	}
   215  
   216  	err = u.DecodeFromBytes(bufin)
   217  	assert.Error(err)
   218  	assert.Equal(ERROR_HANDLING_AFISAFI_DISABLE, err.(*MessageError).ErrorHandling)
   219  
   220  	// Invalid flag
   221  	bufin = []byte{
   222  		0x00, 0x00, // Withdraws(0)
   223  		0x00, 0x0e, // Attrs Len(14)
   224  		0xc0, 0x01, 0x01, 0x00, // Attr(ORIGIN) - invalid flag
   225  		0x40, 0x03, 0x04, 0xc0, // Attr(NEXT_HOP)
   226  		0xa8, 0x01, 0x64,
   227  		0x40, 0x02, 0x00, // Attr(AS_PATH)
   228  	}
   229  
   230  	err = u.DecodeFromBytes(bufin)
   231  	assert.Error(err)
   232  	assert.Equal(ERROR_HANDLING_TREAT_AS_WITHDRAW, err.(*MessageError).ErrorHandling)
   233  
   234  	// Invalid AGGREGATOR and MULTI_EXIT_DESC
   235  	bufin = []byte{
   236  		0x00, 0x00, // Withdraws(0)
   237  		0x00, 0x1e, // Attrs Len(30)
   238  		0xc0, 0x07, 0x05, 0x00, // Attr(AGGREGATOR) - invalid length
   239  		0x00, 0x00, 0x64, 0x00,
   240  		0x80, 0x04, 0x05, 0x00, // Attr(MULTI_EXIT_DESC)  - invalid length
   241  		0x00, 0x00, 0x00, 0x64,
   242  		0x40, 0x01, 0x01, 0x00, // Attr(ORIGIN)
   243  		0x40, 0x02, 0x00, // Attr(AS_PATH)
   244  		0x40, 0x03, 0x04, 0xc0, // Attr(NEXT_HOP)
   245  		0xa8, 0x01, 0x64,
   246  		0x20, 0xc8, 0xc8, 0xc8, // NLRI
   247  		0xc8,
   248  	}
   249  
   250  	err = u.DecodeFromBytes(bufin)
   251  	assert.Error(err)
   252  	assert.Equal(ERROR_HANDLING_TREAT_AS_WITHDRAW, err.(*MessageError).ErrorHandling)
   253  }
   254  
   255  func Test_RFC5512(t *testing.T) {
   256  	assert := assert.New(t)
   257  
   258  	buf := make([]byte, 8)
   259  	buf[0] = byte(EC_TYPE_TRANSITIVE_OPAQUE)
   260  	buf[1] = byte(EC_SUBTYPE_COLOR)
   261  	binary.BigEndian.PutUint32(buf[4:], 1000000)
   262  	ec, err := ParseExtended(buf)
   263  	assert.Equal(nil, err)
   264  	assert.Equal("1000000", ec.String())
   265  	buf, err = ec.Serialize()
   266  	assert.Equal(nil, err)
   267  	assert.Equal([]byte{0x3, 0xb, 0x0, 0x0, 0x0, 0xf, 0x42, 0x40}, buf)
   268  
   269  	buf = make([]byte, 8)
   270  	buf[0] = byte(EC_TYPE_TRANSITIVE_OPAQUE)
   271  	buf[1] = byte(EC_SUBTYPE_ENCAPSULATION)
   272  	binary.BigEndian.PutUint16(buf[6:], uint16(TUNNEL_TYPE_VXLAN))
   273  	ec, err = ParseExtended(buf)
   274  	assert.Equal(nil, err)
   275  	assert.Equal("VXLAN", ec.String())
   276  	buf, err = ec.Serialize()
   277  	assert.Equal(nil, err)
   278  	assert.Equal([]byte{0x3, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8}, buf)
   279  
   280  	tlv := NewTunnelEncapTLV(TUNNEL_TYPE_VXLAN, []TunnelEncapSubTLVInterface{NewTunnelEncapSubTLVColor(10)})
   281  	attr := NewPathAttributeTunnelEncap([]*TunnelEncapTLV{tlv})
   282  
   283  	buf1, err := attr.Serialize()
   284  	assert.Equal(nil, err)
   285  
   286  	p, err := GetPathAttribute(buf1)
   287  	assert.Equal(nil, err)
   288  
   289  	err = p.DecodeFromBytes(buf1)
   290  	assert.Equal(nil, err)
   291  
   292  	buf2, err := p.Serialize()
   293  	assert.Equal(nil, err)
   294  	assert.Equal(buf1, buf2)
   295  
   296  	n1 := NewEncapNLRI("10.0.0.1")
   297  	buf1, err = n1.Serialize()
   298  	assert.Equal(nil, err)
   299  
   300  	n2 := NewEncapNLRI("")
   301  	err = n2.DecodeFromBytes(buf1)
   302  	assert.Equal(nil, err)
   303  	assert.Equal("10.0.0.1", n2.String())
   304  
   305  	n3 := NewEncapv6NLRI("2001::1")
   306  	buf1, err = n3.Serialize()
   307  	assert.Equal(nil, err)
   308  
   309  	n4 := NewEncapv6NLRI("")
   310  	err = n4.DecodeFromBytes(buf1)
   311  	assert.Equal(nil, err)
   312  	assert.Equal("2001::1", n4.String())
   313  }
   314  
   315  func Test_ASLen(t *testing.T) {
   316  	assert := assert.New(t)
   317  
   318  	aspath := AsPathParam{
   319  		Num: 2,
   320  		AS:  []uint16{65000, 65001},
   321  	}
   322  	aspath.Type = BGP_ASPATH_ATTR_TYPE_SEQ
   323  	assert.Equal(2, aspath.ASLen())
   324  
   325  	aspath.Type = BGP_ASPATH_ATTR_TYPE_SET
   326  	assert.Equal(1, aspath.ASLen())
   327  
   328  	aspath.Type = BGP_ASPATH_ATTR_TYPE_CONFED_SEQ
   329  	assert.Equal(0, aspath.ASLen())
   330  
   331  	aspath.Type = BGP_ASPATH_ATTR_TYPE_CONFED_SET
   332  	assert.Equal(0, aspath.ASLen())
   333  
   334  	as4path := As4PathParam{
   335  		Num: 2,
   336  		AS:  []uint32{65000, 65001},
   337  	}
   338  	as4path.Type = BGP_ASPATH_ATTR_TYPE_SEQ
   339  	assert.Equal(2, as4path.ASLen())
   340  
   341  	as4path.Type = BGP_ASPATH_ATTR_TYPE_SET
   342  	assert.Equal(1, as4path.ASLen())
   343  
   344  	as4path.Type = BGP_ASPATH_ATTR_TYPE_CONFED_SEQ
   345  	assert.Equal(0, as4path.ASLen())
   346  
   347  	as4path.Type = BGP_ASPATH_ATTR_TYPE_CONFED_SET
   348  	assert.Equal(0, as4path.ASLen())
   349  
   350  }
   351  
   352  func Test_MPLSLabelStack(t *testing.T) {
   353  	assert := assert.New(t)
   354  	mpls := NewMPLSLabelStack()
   355  	buf, err := mpls.Serialize()
   356  	assert.Nil(err)
   357  	assert.Equal(true, bytes.Equal(buf, []byte{0, 0, 1}))
   358  
   359  	mpls = &MPLSLabelStack{}
   360  	assert.Nil(mpls.DecodeFromBytes(buf))
   361  	assert.Equal(1, len(mpls.Labels))
   362  	assert.Equal(uint32(0), mpls.Labels[0])
   363  
   364  	mpls = NewMPLSLabelStack(WITHDRAW_LABEL)
   365  	buf, err = mpls.Serialize()
   366  	assert.Nil(err)
   367  	assert.Equal(true, bytes.Equal(buf, []byte{128, 0, 0}))
   368  
   369  	mpls = &MPLSLabelStack{}
   370  	assert.Nil(mpls.DecodeFromBytes(buf))
   371  	assert.Equal(1, len(mpls.Labels))
   372  	assert.Equal(WITHDRAW_LABEL, mpls.Labels[0])
   373  }
   374  
   375  func Test_FlowSpecNlri(t *testing.T) {
   376  	assert := assert.New(t)
   377  	cmp := make([]FlowSpecComponentInterface, 0)
   378  	cmp = append(cmp, NewFlowSpecDestinationPrefix(NewIPAddrPrefix(24, "10.0.0.0")))
   379  	cmp = append(cmp, NewFlowSpecSourcePrefix(NewIPAddrPrefix(24, "10.0.0.0")))
   380  	item1 := NewFlowSpecComponentItem(DEC_NUM_OP_EQ, TCP)
   381  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_IP_PROTO, []*FlowSpecComponentItem{item1}))
   382  	item2 := NewFlowSpecComponentItem(DEC_NUM_OP_GT_EQ, 20)
   383  	item3 := NewFlowSpecComponentItem(DEC_NUM_OP_AND|DEC_NUM_OP_LT_EQ, 30)
   384  	item4 := NewFlowSpecComponentItem(DEC_NUM_OP_GT_EQ, 10)
   385  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_PORT, []*FlowSpecComponentItem{item2, item3, item4}))
   386  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_DST_PORT, []*FlowSpecComponentItem{item2, item3, item4}))
   387  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_SRC_PORT, []*FlowSpecComponentItem{item2, item3, item4}))
   388  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ICMP_TYPE, []*FlowSpecComponentItem{item2, item3, item4}))
   389  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ICMP_CODE, []*FlowSpecComponentItem{item2, item3, item4}))
   390  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_PKT_LEN, []*FlowSpecComponentItem{item2, item3, item4}))
   391  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_DSCP, []*FlowSpecComponentItem{item2, item3, item4}))
   392  	isFragment := uint64(0x02)
   393  	lastFragment := uint64(0x08)
   394  	item5 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_MATCH, isFragment)
   395  	item6 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_AND, lastFragment)
   396  
   397  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_FRAGMENT, []*FlowSpecComponentItem{item5, item6}))
   398  	item7 := NewFlowSpecComponentItem(0, TCP_FLAG_ACK)
   399  	item8 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_AND|BITMASK_FLAG_OP_NOT, TCP_FLAG_URGENT)
   400  
   401  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_TCP_FLAG, []*FlowSpecComponentItem{item7, item8}))
   402  	n1 := NewFlowSpecIPv4Unicast(cmp)
   403  
   404  	buf1, err := n1.Serialize()
   405  	assert.Nil(err)
   406  
   407  	n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_IPv4_UC))
   408  	assert.Nil(err)
   409  
   410  	err = n2.DecodeFromBytes(buf1)
   411  	assert.Nil(err)
   412  	// should be equal
   413  	assert.Equal(n1, n2)
   414  }
   415  
   416  func Test_FlowSpecExtended(t *testing.T) {
   417  	assert := assert.New(t)
   418  	exts := make([]ExtendedCommunityInterface, 0)
   419  	exts = append(exts, NewTrafficRateExtended(100, 9600.0))
   420  	exts = append(exts, NewTrafficActionExtended(true, false))
   421  	exts = append(exts, NewRedirectTwoOctetAsSpecificExtended(1000, 1000))
   422  	exts = append(exts, NewRedirectIPv4AddressSpecificExtended("10.0.0.1", 1000))
   423  	exts = append(exts, NewRedirectFourOctetAsSpecificExtended(10000000, 1000))
   424  	exts = append(exts, NewTrafficRemarkExtended(10))
   425  	m1 := NewPathAttributeExtendedCommunities(exts)
   426  	buf1, err := m1.Serialize()
   427  	require.NoError(t, err)
   428  
   429  	m2 := NewPathAttributeExtendedCommunities(nil)
   430  	err = m2.DecodeFromBytes(buf1)
   431  	require.NoError(t, err)
   432  
   433  	_, err = m2.Serialize()
   434  	require.NoError(t, err)
   435  
   436  	assert.Equal(m1, m2)
   437  }
   438  
   439  func Test_IP6FlowSpecExtended(t *testing.T) {
   440  	exts := make([]ExtendedCommunityInterface, 0)
   441  	exts = append(exts, NewRedirectIPv6AddressSpecificExtended("2001:db8::68", 1000))
   442  	m1 := NewPathAttributeIP6ExtendedCommunities(exts)
   443  	buf1, err := m1.Serialize()
   444  	require.NoError(t, err)
   445  
   446  	m2 := NewPathAttributeIP6ExtendedCommunities(nil)
   447  	err = m2.DecodeFromBytes(buf1)
   448  	require.NoError(t, err)
   449  
   450  	_, err = m2.Serialize()
   451  	require.NoError(t, err)
   452  
   453  	assert.Equal(t, m1, m2)
   454  }
   455  
   456  func Test_FlowSpecNlriv6(t *testing.T) {
   457  	cmp := make([]FlowSpecComponentInterface, 0)
   458  	cmp = append(cmp, NewFlowSpecDestinationPrefix6(NewIPv6AddrPrefix(64, "2001::"), 12))
   459  	cmp = append(cmp, NewFlowSpecSourcePrefix6(NewIPv6AddrPrefix(64, "2001::"), 12))
   460  	item1 := NewFlowSpecComponentItem(DEC_NUM_OP_EQ, TCP)
   461  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_IP_PROTO, []*FlowSpecComponentItem{item1}))
   462  	item2 := NewFlowSpecComponentItem(DEC_NUM_OP_GT_EQ, 20)
   463  	item3 := NewFlowSpecComponentItem(DEC_NUM_OP_AND|DEC_NUM_OP_LT_EQ, 30)
   464  	item4 := NewFlowSpecComponentItem(DEC_NUM_OP_EQ, 10)
   465  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_PORT, []*FlowSpecComponentItem{item2, item3, item4}))
   466  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_DST_PORT, []*FlowSpecComponentItem{item2, item3, item4}))
   467  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_SRC_PORT, []*FlowSpecComponentItem{item2, item3, item4}))
   468  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ICMP_TYPE, []*FlowSpecComponentItem{item2, item3, item4}))
   469  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ICMP_CODE, []*FlowSpecComponentItem{item2, item3, item4}))
   470  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_PKT_LEN, []*FlowSpecComponentItem{item2, item3, item4}))
   471  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_DSCP, []*FlowSpecComponentItem{item2, item3, item4}))
   472  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_LABEL, []*FlowSpecComponentItem{item2, item3, item4}))
   473  	isFragment := uint64(0x02)
   474  	item5 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_MATCH, isFragment)
   475  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_FRAGMENT, []*FlowSpecComponentItem{item5}))
   476  	item6 := NewFlowSpecComponentItem(0, TCP_FLAG_ACK)
   477  	item7 := NewFlowSpecComponentItem(BITMASK_FLAG_OP_AND|BITMASK_FLAG_OP_NOT, TCP_FLAG_URGENT)
   478  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_TCP_FLAG, []*FlowSpecComponentItem{item6, item7}))
   479  	n1 := NewFlowSpecIPv6Unicast(cmp)
   480  	buf1, err := n1.Serialize()
   481  	require.NoError(t, err)
   482  
   483  	n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_IPv6_UC))
   484  	require.NoError(t, err)
   485  
   486  	err = n2.DecodeFromBytes(buf1)
   487  	require.NoError(t, err)
   488  
   489  	_, err = n2.Serialize()
   490  	require.NoError(t, err)
   491  
   492  	assert.Equal(t, n1, n2)
   493  }
   494  
   495  func Test_Aigp(t *testing.T) {
   496  	assert := assert.New(t)
   497  	m := NewAigpTLVIgpMetric(1000)
   498  	a1 := NewPathAttributeAigp([]AigpTLVInterface{m})
   499  	buf1, err := a1.Serialize()
   500  	require.NoError(t, err)
   501  
   502  	a2 := NewPathAttributeAigp(nil)
   503  	err = a2.DecodeFromBytes(buf1)
   504  	require.NoError(t, err)
   505  
   506  	assert.Equal(a1, a2)
   507  }
   508  
   509  func Test_FlowSpecNlriL2(t *testing.T) {
   510  	assert := assert.New(t)
   511  	mac, _ := net.ParseMAC("01:23:45:67:89:ab")
   512  	cmp := make([]FlowSpecComponentInterface, 0)
   513  	cmp = append(cmp, NewFlowSpecDestinationMac(mac))
   514  	cmp = append(cmp, NewFlowSpecSourceMac(mac))
   515  	item1 := NewFlowSpecComponentItem(DEC_NUM_OP_EQ, uint64(IPv4))
   516  	cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ETHERNET_TYPE, []*FlowSpecComponentItem{item1}))
   517  	rd, _ := ParseRouteDistinguisher("100:100")
   518  	n1 := NewFlowSpecL2VPN(rd, cmp)
   519  	buf1, err := n1.Serialize()
   520  	assert.Nil(err)
   521  	n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_L2_VPN))
   522  	assert.Nil(err)
   523  	err = n2.DecodeFromBytes(buf1)
   524  	assert.Nil(err)
   525  
   526  	assert.Equal(n1, n2)
   527  }
   528  
   529  func Test_NotificationErrorCode(t *testing.T) {
   530  	// boundary check
   531  	t.Log(NewNotificationErrorCode(BGP_ERROR_MESSAGE_HEADER_ERROR, BGP_ERROR_SUB_BAD_MESSAGE_TYPE).String())
   532  	t.Log(NewNotificationErrorCode(BGP_ERROR_MESSAGE_HEADER_ERROR, BGP_ERROR_SUB_BAD_MESSAGE_TYPE+1).String())
   533  	t.Log(NewNotificationErrorCode(BGP_ERROR_MESSAGE_HEADER_ERROR, 0).String())
   534  	t.Log(NewNotificationErrorCode(0, BGP_ERROR_SUB_BAD_MESSAGE_TYPE).String())
   535  	t.Log(NewNotificationErrorCode(BGP_ERROR_ROUTE_REFRESH_MESSAGE_ERROR+1, 0).String())
   536  }
   537  
   538  func Test_FlowSpecNlriVPN(t *testing.T) {
   539  	assert := assert.New(t)
   540  	cmp := make([]FlowSpecComponentInterface, 0)
   541  	cmp = append(cmp, NewFlowSpecDestinationPrefix(NewIPAddrPrefix(24, "10.0.0.0")))
   542  	cmp = append(cmp, NewFlowSpecSourcePrefix(NewIPAddrPrefix(24, "10.0.0.0")))
   543  	rd, _ := ParseRouteDistinguisher("100:100")
   544  	n1 := NewFlowSpecIPv4VPN(rd, cmp)
   545  	buf1, err := n1.Serialize()
   546  	assert.Nil(err)
   547  	n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_FS_IPv4_VPN))
   548  	assert.Nil(err)
   549  	err = n2.DecodeFromBytes(buf1)
   550  	require.NoError(t, err)
   551  
   552  	assert.Equal(n1, n2)
   553  }
   554  
   555  func Test_EVPNIPPrefixRoute(t *testing.T) {
   556  	assert := assert.New(t)
   557  	rd, _ := ParseRouteDistinguisher("100:100")
   558  	r := &EVPNIPPrefixRoute{
   559  		RD: rd,
   560  		ESI: EthernetSegmentIdentifier{
   561  			Type:  ESI_ARBITRARY,
   562  			Value: make([]byte, 9),
   563  		},
   564  		ETag:           10,
   565  		IPPrefixLength: 24,
   566  		IPPrefix:       net.IP{10, 10, 10, 0},
   567  		GWIPAddress:    net.IP{10, 10, 10, 10},
   568  		Label:          1000,
   569  	}
   570  	n1 := NewEVPNNLRI(EVPN_IP_PREFIX, r)
   571  	buf1, err := n1.Serialize()
   572  	assert.Nil(err)
   573  	n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_EVPN))
   574  	assert.Nil(err)
   575  	err = n2.DecodeFromBytes(buf1)
   576  	assert.Nil(err)
   577  
   578  	assert.Equal(n1, n2)
   579  }
   580  
   581  func Test_CapExtendedNexthop(t *testing.T) {
   582  	assert := assert.New(t)
   583  	tuple := NewCapExtendedNexthopTuple(RF_IPv4_UC, AFI_IP6)
   584  	n1 := NewCapExtendedNexthop([]*CapExtendedNexthopTuple{tuple})
   585  	buf1, err := n1.Serialize()
   586  	assert.Nil(err)
   587  	n2, err := DecodeCapability(buf1)
   588  	assert.Nil(err)
   589  
   590  	assert.Equal(n1, n2)
   591  }
   592  
   593  func Test_AddPath(t *testing.T) {
   594  	assert := assert.New(t)
   595  	opt := &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_IPv4_UC: BGP_ADD_PATH_BOTH}}
   596  	{
   597  		n1 := NewIPAddrPrefix(24, "10.10.10.0")
   598  		assert.Equal(n1.PathIdentifier(), uint32(0))
   599  		n1.SetPathLocalIdentifier(10)
   600  		assert.Equal(n1.PathLocalIdentifier(), uint32(10))
   601  		bits, err := n1.Serialize(opt)
   602  		assert.Nil(err)
   603  		n2 := &IPAddrPrefix{}
   604  		err = n2.DecodeFromBytes(bits, opt)
   605  		assert.Nil(err)
   606  		assert.Equal(n2.PathIdentifier(), uint32(10))
   607  	}
   608  	{
   609  		n1 := NewIPv6AddrPrefix(64, "2001::")
   610  		n1.SetPathIdentifier(10)
   611  		bits, err := n1.Serialize(opt)
   612  		assert.Nil(err)
   613  		n2 := NewIPv6AddrPrefix(0, "")
   614  		err = n2.DecodeFromBytes(bits, opt)
   615  		assert.Nil(err)
   616  		assert.Equal(n2.PathIdentifier(), uint32(0))
   617  	}
   618  	opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_IPv4_UC: BGP_ADD_PATH_BOTH, RF_IPv6_UC: BGP_ADD_PATH_BOTH}}
   619  	{
   620  		n1 := NewIPv6AddrPrefix(64, "2001::")
   621  		n1.SetPathLocalIdentifier(10)
   622  		bits, err := n1.Serialize(opt)
   623  		assert.Nil(err)
   624  		n2 := NewIPv6AddrPrefix(0, "")
   625  		err = n2.DecodeFromBytes(bits, opt)
   626  		assert.Nil(err)
   627  		assert.Equal(n2.PathIdentifier(), uint32(10))
   628  	}
   629  	opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_IPv4_VPN: BGP_ADD_PATH_BOTH, RF_IPv6_VPN: BGP_ADD_PATH_BOTH}}
   630  	{
   631  		rd, _ := ParseRouteDistinguisher("100:100")
   632  		labels := NewMPLSLabelStack(100, 200)
   633  		n1 := NewLabeledVPNIPAddrPrefix(24, "10.10.10.0", *labels, rd)
   634  		n1.SetPathLocalIdentifier(20)
   635  		bits, err := n1.Serialize(opt)
   636  		assert.Nil(err)
   637  		n2 := NewLabeledVPNIPAddrPrefix(0, "", MPLSLabelStack{}, nil)
   638  		err = n2.DecodeFromBytes(bits, opt)
   639  		assert.Nil(err)
   640  		assert.Equal(n2.PathIdentifier(), uint32(20))
   641  	}
   642  	{
   643  		rd, _ := ParseRouteDistinguisher("100:100")
   644  		labels := NewMPLSLabelStack(100, 200)
   645  		n1 := NewLabeledVPNIPv6AddrPrefix(64, "2001::", *labels, rd)
   646  		n1.SetPathLocalIdentifier(20)
   647  		bits, err := n1.Serialize(opt)
   648  		assert.Nil(err)
   649  		n2 := NewLabeledVPNIPAddrPrefix(0, "", MPLSLabelStack{}, nil)
   650  		err = n2.DecodeFromBytes(bits, opt)
   651  		assert.Nil(err)
   652  		assert.Equal(n2.PathIdentifier(), uint32(20))
   653  	}
   654  	opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_IPv4_MPLS: BGP_ADD_PATH_BOTH, RF_IPv6_MPLS: BGP_ADD_PATH_BOTH}}
   655  	{
   656  		labels := NewMPLSLabelStack(100, 200)
   657  		n1 := NewLabeledIPAddrPrefix(24, "10.10.10.0", *labels)
   658  		n1.SetPathLocalIdentifier(20)
   659  		bits, err := n1.Serialize(opt)
   660  		assert.Nil(err)
   661  		n2 := NewLabeledIPAddrPrefix(0, "", MPLSLabelStack{})
   662  		err = n2.DecodeFromBytes(bits, opt)
   663  		assert.Nil(err)
   664  		assert.Equal(n2.PathIdentifier(), uint32(20))
   665  	}
   666  	{
   667  		labels := NewMPLSLabelStack(100, 200)
   668  		n1 := NewLabeledIPv6AddrPrefix(64, "2001::", *labels)
   669  		n1.SetPathLocalIdentifier(20)
   670  		bits, err := n1.Serialize(opt)
   671  		assert.Nil(err)
   672  		n2 := NewLabeledIPAddrPrefix(0, "", MPLSLabelStack{})
   673  		err = n2.DecodeFromBytes(bits, opt)
   674  		assert.Nil(err)
   675  		assert.Equal(n2.PathIdentifier(), uint32(20))
   676  	}
   677  	opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_RTC_UC: BGP_ADD_PATH_BOTH}}
   678  	{
   679  		rt, _ := ParseRouteTarget("100:100")
   680  		n1 := NewRouteTargetMembershipNLRI(65000, rt)
   681  		n1.SetPathLocalIdentifier(30)
   682  		bits, err := n1.Serialize(opt)
   683  		assert.Nil(err)
   684  		n2 := NewRouteTargetMembershipNLRI(0, nil)
   685  		err = n2.DecodeFromBytes(bits, opt)
   686  		assert.Nil(err)
   687  		assert.Equal(n2.PathIdentifier(), uint32(30))
   688  	}
   689  	opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_EVPN: BGP_ADD_PATH_BOTH}}
   690  	{
   691  		n1 := NewEVPNNLRI(EVPN_ROUTE_TYPE_ETHERNET_AUTO_DISCOVERY,
   692  			&EVPNEthernetAutoDiscoveryRoute{NewRouteDistinguisherFourOctetAS(5, 6),
   693  				EthernetSegmentIdentifier{ESI_ARBITRARY, make([]byte, 9)}, 2, 2})
   694  		n1.SetPathLocalIdentifier(40)
   695  		bits, err := n1.Serialize(opt)
   696  		assert.Nil(err)
   697  		n2 := NewEVPNNLRI(0, nil)
   698  		err = n2.DecodeFromBytes(bits, opt)
   699  		assert.Nil(err)
   700  		assert.Equal(n2.PathIdentifier(), uint32(40))
   701  	}
   702  	opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_IPv4_ENCAP: BGP_ADD_PATH_BOTH}}
   703  	{
   704  		n1 := NewEncapNLRI("10.10.10.0")
   705  		n1.SetPathLocalIdentifier(50)
   706  		bits, err := n1.Serialize(opt)
   707  		assert.Nil(err)
   708  		n2 := NewEncapNLRI("")
   709  		err = n2.DecodeFromBytes(bits, opt)
   710  		assert.Nil(err)
   711  		assert.Equal(n2.PathIdentifier(), uint32(50))
   712  	}
   713  	opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_FS_IPv4_UC: BGP_ADD_PATH_BOTH}}
   714  	{
   715  		n1 := NewFlowSpecIPv4Unicast([]FlowSpecComponentInterface{NewFlowSpecDestinationPrefix(NewIPAddrPrefix(24, "10.0.0.0"))})
   716  		n1.SetPathLocalIdentifier(60)
   717  		bits, err := n1.Serialize(opt)
   718  		assert.Nil(err)
   719  		n2 := NewFlowSpecIPv4Unicast(nil)
   720  		err = n2.DecodeFromBytes(bits, opt)
   721  		assert.Nil(err)
   722  		assert.Equal(n2.PathIdentifier(), uint32(60))
   723  	}
   724  	opt = &MarshallingOption{AddPath: map[RouteFamily]BGPAddPathMode{RF_OPAQUE: BGP_ADD_PATH_BOTH}}
   725  	{
   726  		n1 := NewOpaqueNLRI([]byte("key"), []byte("value"))
   727  		n1.SetPathLocalIdentifier(70)
   728  		bits, err := n1.Serialize(opt)
   729  		assert.Nil(err)
   730  		n2 := &OpaqueNLRI{}
   731  		err = n2.DecodeFromBytes(bits, opt)
   732  		assert.Nil(err)
   733  		assert.Equal(n2.PathIdentifier(), uint32(70))
   734  	}
   735  
   736  }
   737  
   738  func Test_CompareFlowSpecNLRI(t *testing.T) {
   739  	assert := assert.New(t)
   740  	cmp, err := ParseFlowSpecComponents(RF_FS_IPv4_UC, "destination 10.0.0.2/32 source 10.0.0.1/32 destination-port ==3128 protocol tcp")
   741  	assert.Nil(err)
   742  	// Note: Use NewFlowSpecIPv4Unicast() for the consistent ordered rules.
   743  	n1 := NewFlowSpecIPv4Unicast(cmp).FlowSpecNLRI
   744  	cmp, err = ParseFlowSpecComponents(RF_FS_IPv4_UC, "source 10.0.0.0/24 destination-port ==3128 protocol tcp")
   745  	assert.Nil(err)
   746  	n2 := NewFlowSpecIPv4Unicast(cmp).FlowSpecNLRI
   747  	r, err := CompareFlowSpecNLRI(&n1, &n2)
   748  	assert.Nil(err)
   749  	assert.True(r > 0)
   750  	cmp, err = ParseFlowSpecComponents(RF_FS_IPv4_UC, "source 10.0.0.9/32 port ==80 ==8080 destination-port >8080&<8080 ==3128 source-port >1024 protocol ==udp ==tcp")
   751  	n3 := NewFlowSpecIPv4Unicast(cmp).FlowSpecNLRI
   752  	assert.Nil(err)
   753  	cmp, err = ParseFlowSpecComponents(RF_FS_IPv4_UC, "destination 192.168.0.2/32")
   754  	n4 := NewFlowSpecIPv4Unicast(cmp).FlowSpecNLRI
   755  	assert.Nil(err)
   756  	r, err = CompareFlowSpecNLRI(&n3, &n4)
   757  	assert.Nil(err)
   758  	assert.True(r < 0)
   759  }
   760  
   761  func Test_MpReachNLRIWithIPv4MappedIPv6Prefix(t *testing.T) {
   762  	assert := assert.New(t)
   763  	n1 := NewIPv6AddrPrefix(120, "::ffff:10.0.0.0")
   764  	buf1, err := n1.Serialize()
   765  	assert.Nil(err)
   766  	n2, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_IPv6_UC))
   767  	assert.Nil(err)
   768  	err = n2.DecodeFromBytes(buf1)
   769  	assert.Nil(err)
   770  
   771  	assert.Equal(n1, n2)
   772  
   773  	label := NewMPLSLabelStack(2)
   774  
   775  	n3 := NewLabeledIPv6AddrPrefix(120, "::ffff:10.0.0.0", *label)
   776  	buf1, err = n3.Serialize()
   777  	assert.Nil(err)
   778  	n4, err := NewPrefixFromRouteFamily(RouteFamilyToAfiSafi(RF_IPv6_MPLS))
   779  	assert.Nil(err)
   780  	err = n4.DecodeFromBytes(buf1)
   781  	assert.Nil(err)
   782  
   783  	assert.Equal(n3, n4)
   784  }
   785  
   786  func Test_MpReachNLRIWithIPv6PrefixWithIPv4Peering(t *testing.T) {
   787  	assert := assert.New(t)
   788  	bufin := []byte{
   789  		0x80, 0x0e, 0x1e, // flags(1), type(1), length(1)
   790  		0x00, 0x02, 0x01, 0x10, // afi(2), safi(1), nexthoplen(1)
   791  		0x00, 0x00, 0x00, 0x00, // nexthop(16)
   792  		0x00, 0x00, 0x00, 0x00, // = "::ffff:172.20.0.1"
   793  		0x00, 0x00, 0xff, 0xff,
   794  		0xac, 0x14, 0x00, 0x01,
   795  		0x00,                   // reserved(1)
   796  		0x40, 0x20, 0x01, 0x0d, // nlri(9)
   797  		0xb8, 0x00, 0x01, 0x00, // = "2001:db8:1:1::/64"
   798  		0x01,
   799  	}
   800  	// Test DecodeFromBytes()
   801  	p := &PathAttributeMpReachNLRI{}
   802  	err := p.DecodeFromBytes(bufin)
   803  	assert.Nil(err)
   804  	// Test decoded values
   805  	assert.Equal(BGPAttrFlag(0x80), p.Flags)
   806  	assert.Equal(BGPAttrType(0xe), p.Type)
   807  	assert.Equal(uint16(0x1e), p.Length)
   808  	assert.Equal(uint16(AFI_IP6), p.AFI)
   809  	assert.Equal(uint8(SAFI_UNICAST), p.SAFI)
   810  	assert.Equal(net.ParseIP("::ffff:172.20.0.1"), p.Nexthop)
   811  	assert.Equal(net.ParseIP(""), p.LinkLocalNexthop)
   812  	value := []AddrPrefixInterface{
   813  		NewIPv6AddrPrefix(64, "2001:db8:1:1::"),
   814  	}
   815  	assert.Equal(value, p.Value)
   816  	// Set NextHop as IPv4 address (because IPv4 peering)
   817  	p.Nexthop = net.ParseIP("172.20.0.1")
   818  	// Test Serialize()
   819  	bufout, err := p.Serialize()
   820  	assert.Nil(err)
   821  	// Test serialised value
   822  	assert.Equal(bufin, bufout)
   823  }
   824  
   825  func Test_MpReachNLRIWithIPv6(t *testing.T) {
   826  	assert := assert.New(t)
   827  	bufin := []byte{
   828  		0x90, 0x0e, 0x00, 0x1e, // flags(1), type(1), length(2),
   829  		0x00, 0x02, 0x01, 0x10, // afi(2), safi(1), nexthoplen(1)
   830  		0x20, 0x01, 0x0d, 0xb8, // nexthop(16)
   831  		0x00, 0x01, 0x00, 0x00, // = "2001:db8:1::1"
   832  		0x00, 0x00, 0x00, 0x00,
   833  		0x00, 0x00, 0x00, 0x01,
   834  		0x00,                   // reserved(1)
   835  		0x40, 0x20, 0x01, 0x0d, // nlri(9)
   836  		0xb8, 0x00, 0x53, 0x00, // = "2001:db8:53::/64"
   837  		0x00,
   838  	}
   839  	// Test DecodeFromBytes()
   840  	p := &PathAttributeMpReachNLRI{}
   841  	err := p.DecodeFromBytes(bufin)
   842  	assert.Nil(err)
   843  	// Test decoded values
   844  	assert.Equal(BGPAttrFlag(0x90), p.Flags)
   845  	assert.Equal(BGPAttrType(0xe), p.Type)
   846  	assert.Equal(uint16(0x1e), p.Length)
   847  	assert.Equal(uint16(AFI_IP6), p.AFI)
   848  	assert.Equal(uint8(SAFI_UNICAST), p.SAFI)
   849  	assert.Equal(net.ParseIP("2001:db8:1::1"), p.Nexthop)
   850  	value := []AddrPrefixInterface{
   851  		NewIPv6AddrPrefix(64, "2001:db8:53::"),
   852  	}
   853  	assert.Equal(value, p.Value)
   854  }
   855  
   856  func Test_MpUnreachNLRIWithIPv6(t *testing.T) {
   857  	assert := assert.New(t)
   858  	bufin := []byte{
   859  		0x90, 0x0f, 0x00, 0x0c, // flags(1), type(1), length(2),
   860  		0x00, 0x02, 0x01, // afi(2), safi(1),
   861  		0x40, 0x20, 0x01, 0x0d, // nlri(9)
   862  		0xb8, 0x00, 0x53, 0x00, // = "2001:db8:53::/64"
   863  		0x00,
   864  	}
   865  	// Test DecodeFromBytes()
   866  	p := &PathAttributeMpUnreachNLRI{}
   867  	err := p.DecodeFromBytes(bufin)
   868  	assert.Nil(err)
   869  	// Test decoded values
   870  	assert.Equal(BGPAttrFlag(0x90), p.Flags)
   871  	assert.Equal(BGPAttrType(0xf), p.Type)
   872  	assert.Equal(uint16(0x0c), p.Length)
   873  	assert.Equal(uint16(AFI_IP6), p.AFI)
   874  	assert.Equal(uint8(SAFI_UNICAST), p.SAFI)
   875  	value := []AddrPrefixInterface{
   876  		NewIPv6AddrPrefix(64, "2001:db8:53::"),
   877  	}
   878  	assert.Equal(value, p.Value)
   879  }
   880  
   881  func Test_MpReachNLRIWithIPv6PrefixWithLinkLocalNexthop(t *testing.T) {
   882  	assert := assert.New(t)
   883  	bufin := []byte{
   884  		0x80, 0x0e, 0x2c, // flags(1), type(1), length(1)
   885  		0x00, 0x02, 0x01, 0x20, // afi(2), safi(1), nexthoplen(1)
   886  		0x20, 0x01, 0x0d, 0xb8, // nexthop(32)
   887  		0x00, 0x01, 0x00, 0x00, // = "2001:db8:1::1"
   888  		0x00, 0x00, 0x00, 0x00,
   889  		0x00, 0x00, 0x00, 0x01,
   890  		0xfe, 0x80, 0x00, 0x00, // + "fe80::1" (link local)
   891  		0x00, 0x00, 0x00, 0x00,
   892  		0x00, 0x00, 0x00, 0x00,
   893  		0x00, 0x00, 0x00, 0x01,
   894  		0x00,                   // reserved(1)
   895  		0x30, 0x20, 0x10, 0x0a, // nlri(7)
   896  		0xb8, 0x00, 0x01, // = "2010:ab8:1::/48"
   897  	}
   898  	// Test DecodeFromBytes()
   899  	p := &PathAttributeMpReachNLRI{}
   900  	err := p.DecodeFromBytes(bufin)
   901  	assert.Nil(err)
   902  	// Test decoded values
   903  	assert.Equal(BGPAttrFlag(0x80), p.Flags)
   904  	assert.Equal(BGPAttrType(0xe), p.Type)
   905  	assert.Equal(uint16(0x2c), p.Length)
   906  	assert.Equal(uint16(AFI_IP6), p.AFI)
   907  	assert.Equal(uint8(SAFI_UNICAST), p.SAFI)
   908  	assert.Equal(net.ParseIP("2001:db8:1::1"), p.Nexthop)
   909  	assert.Equal(net.ParseIP("fe80::1"), p.LinkLocalNexthop)
   910  	value := []AddrPrefixInterface{
   911  		NewIPv6AddrPrefix(48, "2010:ab8:1::"),
   912  	}
   913  	assert.Equal(value, p.Value)
   914  	// Test Serialize()
   915  	bufout, err := p.Serialize()
   916  	assert.Nil(err)
   917  	// Test serialised value
   918  	assert.Equal(bufin, bufout)
   919  }
   920  
   921  func Test_MpReachNLRIWithVPNv4Prefix(t *testing.T) {
   922  	assert := assert.New(t)
   923  	bufin := []byte{
   924  		0x80, 0x0e, 0x20, // flags(1), type(1), length(1)
   925  		0x00, 0x01, 0x80, 0x0c, // afi(2), safi(1), nexthoplen(1)
   926  		0x00, 0x00, 0x00, 0x00, // nexthop(12)
   927  		0x00, 0x00, 0x00, 0x00, // = (rd:"0:0",) "172.20.0.1"
   928  		0xac, 0x14, 0x00, 0x01,
   929  		0x00,                   // reserved(1)
   930  		0x70, 0x00, 0x01, 0x01, // nlri(15)
   931  		0x00, 0x00, 0xfd, 0xe8, // = label:16, rd:"65000:100", prefix:"10.1.1.0/24"
   932  		0x00, 0x00, 0x00, 0x64,
   933  		0x0a, 0x01, 0x01,
   934  	}
   935  	// Test DecodeFromBytes()
   936  	p := &PathAttributeMpReachNLRI{}
   937  	err := p.DecodeFromBytes(bufin)
   938  	assert.Nil(err)
   939  	// Test decoded values
   940  	assert.Equal(BGPAttrFlag(0x80), p.Flags)
   941  	assert.Equal(BGPAttrType(0xe), p.Type)
   942  	assert.Equal(uint16(0x20), p.Length)
   943  	assert.Equal(uint16(AFI_IP), p.AFI)
   944  	assert.Equal(uint8(SAFI_MPLS_VPN), p.SAFI)
   945  	assert.Equal(net.ParseIP("172.20.0.1").To4(), p.Nexthop)
   946  	assert.Equal(net.ParseIP(""), p.LinkLocalNexthop)
   947  	value := []AddrPrefixInterface{
   948  		NewLabeledVPNIPAddrPrefix(24, "10.1.1.0", *NewMPLSLabelStack(16),
   949  			NewRouteDistinguisherTwoOctetAS(65000, 100)),
   950  	}
   951  	assert.Equal(value, p.Value)
   952  	// Test Serialize()
   953  	bufout, err := p.Serialize()
   954  	assert.Nil(err)
   955  	// Test serialised value
   956  	assert.Equal(bufin, bufout)
   957  }
   958  
   959  func Test_MpReachNLRIWithVPNv6Prefix(t *testing.T) {
   960  	assert := assert.New(t)
   961  	bufin := []byte{
   962  		0x80, 0x0e, 0x39, // flags(1), type(1), length(1)
   963  		0x00, 0x02, 0x80, 0x18, // afi(2), safi(1), nexthoplen(1)
   964  		0x00, 0x00, 0x00, 0x00, // nexthop(24)
   965  		0x00, 0x00, 0x00, 0x00, // = (rd:"0:0",) "2001:db8:1::1"
   966  		0x20, 0x01, 0x0d, 0xb8,
   967  		0x00, 0x01, 0x00, 0x00,
   968  		0x00, 0x00, 0x00, 0x00,
   969  		0x00, 0x00, 0x00, 0x01,
   970  		0x00,                   // reserved(1)
   971  		0xd4, 0x00, 0x01, 0x01, // nlri(28)
   972  		0x00, 0x00, 0xfd, 0xe8, // = label:16, rd:"65000:100", prefix:"2001:1::/124"
   973  		0x00, 0x00, 0x00, 0x64,
   974  		0x20, 0x01, 0x00, 0x01,
   975  		0x00, 0x00, 0x00, 0x00,
   976  		0x00, 0x00, 0x00, 0x00,
   977  		0x00, 0x00, 0x00, 0x00,
   978  	}
   979  	// Test DecodeFromBytes()
   980  	p := &PathAttributeMpReachNLRI{}
   981  	err := p.DecodeFromBytes(bufin)
   982  	assert.Nil(err)
   983  	// Test decoded values
   984  	assert.Equal(BGPAttrFlag(0x80), p.Flags)
   985  	assert.Equal(BGPAttrType(0xe), p.Type)
   986  	assert.Equal(uint16(0x39), p.Length)
   987  	assert.Equal(uint16(AFI_IP6), p.AFI)
   988  	assert.Equal(uint8(SAFI_MPLS_VPN), p.SAFI)
   989  	assert.Equal(net.ParseIP("2001:db8:1::1"), p.Nexthop)
   990  	assert.Equal(net.ParseIP(""), p.LinkLocalNexthop)
   991  	value := []AddrPrefixInterface{
   992  		NewLabeledVPNIPv6AddrPrefix(124, "2001:1::", *NewMPLSLabelStack(16),
   993  			NewRouteDistinguisherTwoOctetAS(65000, 100)),
   994  	}
   995  	assert.Equal(value, p.Value)
   996  	// Test Serialize()
   997  	bufout, err := p.Serialize()
   998  	assert.Nil(err)
   999  	// Test serialised value
  1000  	assert.Equal(bufin, bufout)
  1001  }
  1002  
  1003  func Test_MpReachNLRIWithIPv4PrefixWithIPv6Nexthop(t *testing.T) {
  1004  	assert := assert.New(t)
  1005  	bufin := []byte{
  1006  		0x80, 0x0e, 0x19, // flags(1), type(1), length(1)
  1007  		0x00, 0x01, 0x01, 0x10, // afi(1), safi(1), nexthoplen(1)
  1008  		0x20, 0x01, 0x0d, 0xb8, // nexthop(32)
  1009  		0x00, 0x01, 0x00, 0x00, // = "2001:db8:1::1"
  1010  		0x00, 0x00, 0x00, 0x00,
  1011  		0x00, 0x00, 0x00, 0x01,
  1012  		0x00,                   // reserved(1)
  1013  		0x18, 0xc0, 0xa8, 0x0a, // nlri(7)
  1014  	}
  1015  	// Test DecodeFromBytes()
  1016  	p := &PathAttributeMpReachNLRI{}
  1017  	err := p.DecodeFromBytes(bufin)
  1018  	assert.Nil(err)
  1019  	// Test decoded values
  1020  	assert.Equal(BGPAttrFlag(0x80), p.Flags)
  1021  	assert.Equal(BGPAttrType(0xe), p.Type)
  1022  	assert.Equal(uint16(0x19), p.Length)
  1023  	assert.Equal(uint16(AFI_IP), p.AFI)
  1024  	assert.Equal(uint8(SAFI_UNICAST), p.SAFI)
  1025  	assert.Equal(net.ParseIP("2001:db8:1::1"), p.Nexthop)
  1026  	value := []AddrPrefixInterface{
  1027  		NewIPAddrPrefix(24, "192.168.10.0"),
  1028  	}
  1029  	assert.Equal(value, p.Value)
  1030  	// Test Serialize()
  1031  	bufout, err := p.Serialize()
  1032  	assert.Nil(err)
  1033  	// Test serialised value
  1034  	assert.Equal(bufin, bufout)
  1035  }
  1036  
  1037  func Test_ParseRouteDistinguisher(t *testing.T) {
  1038  	assert := assert.New(t)
  1039  
  1040  	rd, _ := ParseRouteDistinguisher("100:1000")
  1041  	rdType0, ok := rd.(*RouteDistinguisherTwoOctetAS)
  1042  	if !ok {
  1043  		t.Fatal("Type of RD interface is not RouteDistinguisherTwoOctetAS")
  1044  	}
  1045  
  1046  	assert.Equal(uint16(100), rdType0.Admin)
  1047  	assert.Equal(uint32(1000), rdType0.Assigned)
  1048  
  1049  	rd, _ = ParseRouteDistinguisher("10.0.0.0:100")
  1050  	rdType1, ok := rd.(*RouteDistinguisherIPAddressAS)
  1051  	if !ok {
  1052  		t.Fatal("Type of RD interface is not RouteDistinguisherIPAddressAS")
  1053  	}
  1054  
  1055  	assert.Equal("10.0.0.0", rdType1.Admin.String())
  1056  	assert.Equal(uint16(100), rdType1.Assigned)
  1057  
  1058  	rd, _ = ParseRouteDistinguisher("100.1000:10000")
  1059  	rdType2, ok := rd.(*RouteDistinguisherFourOctetAS)
  1060  	if !ok {
  1061  		t.Fatal("Type of RD interface is not RouteDistinguisherFourOctetAS")
  1062  	}
  1063  
  1064  	assert.Equal(uint32((100<<16)|1000), rdType2.Admin)
  1065  	assert.Equal(uint16(10000), rdType2.Assigned)
  1066  }
  1067  
  1068  func Test_ParseEthernetSegmentIdentifier(t *testing.T) {
  1069  	assert := assert.New(t)
  1070  
  1071  	// "single-homed"
  1072  	esiZero := EthernetSegmentIdentifier{}
  1073  	args := make([]string, 0)
  1074  	esi, err := ParseEthernetSegmentIdentifier(args)
  1075  	assert.Nil(err)
  1076  	assert.Equal(esiZero, esi)
  1077  	args = []string{"single-homed"}
  1078  	esi, err = ParseEthernetSegmentIdentifier(args)
  1079  	assert.Nil(err)
  1080  	assert.Equal(esiZero, esi)
  1081  
  1082  	// ESI_ARBITRARY
  1083  	args = []string{"ARBITRARY", "11:22:33:44:55:66:77:88:99"} // omit "ESI_"
  1084  	esi, err = ParseEthernetSegmentIdentifier(args)
  1085  	assert.Nil(err)
  1086  	assert.Equal(EthernetSegmentIdentifier{
  1087  		Type:  ESI_ARBITRARY,
  1088  		Value: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
  1089  	}, esi)
  1090  
  1091  	// ESI_LACP
  1092  	args = []string{"lacp", "aa:bb:cc:dd:ee:ff", strconv.FormatInt(0x1122, 10)} // lower case
  1093  	esi, err = ParseEthernetSegmentIdentifier(args)
  1094  	assert.Nil(err)
  1095  	assert.Equal(EthernetSegmentIdentifier{
  1096  		Type:  ESI_LACP,
  1097  		Value: []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x00},
  1098  	}, esi)
  1099  
  1100  	// ESI_MSTP
  1101  	args = []string{"esi_mstp", "aa:bb:cc:dd:ee:ff", strconv.FormatInt(0x1122, 10)} // omit "ESI_" + lower case
  1102  	esi, err = ParseEthernetSegmentIdentifier(args)
  1103  	assert.Nil(err)
  1104  	assert.Equal(EthernetSegmentIdentifier{
  1105  		Type:  ESI_MSTP,
  1106  		Value: []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x00},
  1107  	}, esi)
  1108  
  1109  	// ESI_MAC
  1110  	args = []string{"ESI_MAC", "aa:bb:cc:dd:ee:ff", strconv.FormatInt(0x112233, 10)}
  1111  	esi, err = ParseEthernetSegmentIdentifier(args)
  1112  	assert.Nil(err)
  1113  	assert.Equal(EthernetSegmentIdentifier{
  1114  		Type:  ESI_MAC,
  1115  		Value: []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33},
  1116  	}, esi)
  1117  
  1118  	// ESI_ROUTERID
  1119  	args = []string{"ESI_ROUTERID", "1.1.1.1", strconv.FormatInt(0x11223344, 10)}
  1120  	esi, err = ParseEthernetSegmentIdentifier(args)
  1121  	assert.Nil(err)
  1122  	assert.Equal(EthernetSegmentIdentifier{
  1123  		Type:  ESI_ROUTERID,
  1124  		Value: []byte{0x01, 0x01, 0x01, 0x01, 0x11, 0x22, 0x33, 0x44, 0x00},
  1125  	}, esi)
  1126  
  1127  	// ESI_AS
  1128  	args = []string{"ESI_AS", strconv.FormatInt(0xaabbccdd, 10), strconv.FormatInt(0x11223344, 10)}
  1129  	esi, err = ParseEthernetSegmentIdentifier(args)
  1130  	assert.Nil(err)
  1131  	assert.Equal(EthernetSegmentIdentifier{
  1132  		Type:  ESI_AS,
  1133  		Value: []byte{0xaa, 0xbb, 0xcc, 0xdd, 0x11, 0x22, 0x33, 0x44, 0x00},
  1134  	}, esi)
  1135  
  1136  	// Other
  1137  	args = []string{"99", "11:22:33:44:55:66:77:88:99"}
  1138  	esi, err = ParseEthernetSegmentIdentifier(args)
  1139  	assert.Nil(err)
  1140  	assert.Equal(EthernetSegmentIdentifier{
  1141  		Type:  ESIType(99),
  1142  		Value: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
  1143  	}, esi)
  1144  }
  1145  
  1146  func TestParseBogusShortData(t *testing.T) {
  1147  	var bodies = []BGPBody{
  1148  		&BGPOpen{},
  1149  		&BGPUpdate{},
  1150  		&BGPNotification{},
  1151  		&BGPKeepAlive{},
  1152  		&BGPRouteRefresh{},
  1153  	}
  1154  
  1155  	for _, b := range bodies {
  1156  		b.DecodeFromBytes([]byte{0})
  1157  	}
  1158  }
  1159  
  1160  func TestFuzzCrashers(t *testing.T) {
  1161  	var crashers = []string{
  1162  		"000000000000000000\x01",
  1163  	}
  1164  
  1165  	for _, f := range crashers {
  1166  		ParseBGPMessage([]byte(f))
  1167  	}
  1168  }
  1169  
  1170  func TestNormalizeFlowSpecOpValues(t *testing.T) {
  1171  	tests := []struct {
  1172  		msg  string
  1173  		args []string
  1174  		want []string
  1175  	}{
  1176  		{
  1177  			msg:  "valid match",
  1178  			args: []string{"  &  <=80", " tcp  != udp ", " =!   SA   & =U!  F", " =  is-fragment+last-fragment"},
  1179  			want: []string{"<=80", "tcp", "!=udp", "=!SA", "&=U", "!F", "=is-fragment+last-fragment"},
  1180  		},
  1181  		{
  1182  			msg:  "RFC5575 trims & prefix",
  1183  			args: []string{"&<=80"},
  1184  			want: []string{"<=80"},
  1185  		},
  1186  	}
  1187  
  1188  	for _, tt := range tests {
  1189  		t.Run(tt.msg, func(t *testing.T) {
  1190  			got := normalizeFlowSpecOpValues(tt.args)
  1191  			assert.Equal(t, tt.want, got)
  1192  		})
  1193  	}
  1194  }
  1195  
  1196  func Test_PathAttributeNextHop(t *testing.T) {
  1197  	f := func(addr string) {
  1198  		b, _ := NewPathAttributeNextHop(addr).Serialize()
  1199  		p := PathAttributeNextHop{}
  1200  		p.DecodeFromBytes(b)
  1201  		assert.Equal(t, addr, p.Value.String())
  1202  	}
  1203  	f("192.0.2.1")
  1204  	f("2001:db8::68")
  1205  }