github.com/osrg/gobgp/v3@v3.30.0/internal/pkg/table/message_test.go (about)

     1  // Copyright (C) 2014 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 table
    17  
    18  import (
    19  	"fmt"
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/osrg/gobgp/v3/pkg/packet/bgp"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  // before:
    29  //
    30  //	as-path  : 65000, 4000, 400000, 300000, 40001
    31  //
    32  // expected result:
    33  //
    34  //	as-path  : 65000, 4000, 23456, 23456, 40001
    35  //	as4-path : 65000, 4000, 400000, 300000, 40001
    36  func TestAsPathAs2Trans1(t *testing.T) {
    37  	as := []uint32{65000, 4000, 400000, 300000, 40001}
    38  	params := []bgp.AsPathParamInterface{bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as)}
    39  	aspath := bgp.NewPathAttributeAsPath(params)
    40  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath}, nil).Body.(*bgp.BGPUpdate)
    41  	UpdatePathAttrs2ByteAs(msg)
    42  	assert.Equal(t, len(msg.PathAttributes), 2)
    43  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 1)
    44  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS), 5)
    45  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS[0], uint16(65000))
    46  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS[1], uint16(4000))
    47  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS[2], uint16(bgp.AS_TRANS))
    48  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS[3], uint16(bgp.AS_TRANS))
    49  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS[4], uint16(40001))
    50  	assert.Equal(t, len(msg.PathAttributes[1].(*bgp.PathAttributeAs4Path).Value), 1)
    51  	assert.Equal(t, len(msg.PathAttributes[1].(*bgp.PathAttributeAs4Path).Value[0].AS), 5)
    52  	assert.Equal(t, msg.PathAttributes[1].(*bgp.PathAttributeAs4Path).Value[0].AS[0], uint32(65000))
    53  	assert.Equal(t, msg.PathAttributes[1].(*bgp.PathAttributeAs4Path).Value[0].AS[1], uint32(4000))
    54  	assert.Equal(t, msg.PathAttributes[1].(*bgp.PathAttributeAs4Path).Value[0].AS[2], uint32(400000))
    55  	assert.Equal(t, msg.PathAttributes[1].(*bgp.PathAttributeAs4Path).Value[0].AS[3], uint32(300000))
    56  	assert.Equal(t, msg.PathAttributes[1].(*bgp.PathAttributeAs4Path).Value[0].AS[4], uint32(40001))
    57  }
    58  
    59  // before:
    60  //
    61  //	as-path  : 65000, 4000, 40000, 30000, 40001
    62  //
    63  // expected result:
    64  //
    65  //	as-path  : 65000, 4000, 40000, 30000, 40001
    66  func TestAsPathAs2Trans2(t *testing.T) {
    67  	as := []uint32{65000, 4000, 40000, 30000, 40001}
    68  	params := []bgp.AsPathParamInterface{bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as)}
    69  	aspath := bgp.NewPathAttributeAsPath(params)
    70  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath}, nil).Body.(*bgp.BGPUpdate)
    71  	UpdatePathAttrs2ByteAs(msg)
    72  	assert.Equal(t, len(msg.PathAttributes), 1)
    73  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 1)
    74  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS), 5)
    75  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS[0], uint16(65000))
    76  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS[1], uint16(4000))
    77  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS[2], uint16(40000))
    78  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS[3], uint16(30000))
    79  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.AsPathParam).AS[4], uint16(40001))
    80  }
    81  
    82  // before:
    83  //
    84  //	as-path  : 65000, 4000, 23456, 23456, 40001
    85  //	as4-path : 400000, 300000, 40001
    86  //
    87  // expected result:
    88  //
    89  //	as-path  : 65000, 4000, 400000, 300000, 40001
    90  func TestAsPathAs4Trans1(t *testing.T) {
    91  	as := []uint16{65000, 4000, bgp.AS_TRANS, bgp.AS_TRANS, 40001}
    92  	params := []bgp.AsPathParamInterface{bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as)}
    93  	aspath := bgp.NewPathAttributeAsPath(params)
    94  
    95  	as4 := []uint32{400000, 300000, 40001}
    96  	param4s := []*bgp.As4PathParam{bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as4)}
    97  	as4path := bgp.NewPathAttributeAs4Path(param4s)
    98  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath, as4path}, nil).Body.(*bgp.BGPUpdate)
    99  	UpdatePathAttrs4ByteAs(logger, msg)
   100  	assert.Equal(t, len(msg.PathAttributes), 1)
   101  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 1)
   102  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS), 5)
   103  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[0], uint32(65000))
   104  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[1], uint32(4000))
   105  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[2], uint32(400000))
   106  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[3], uint32(300000))
   107  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[4], uint32(40001))
   108  }
   109  
   110  // before:
   111  //
   112  //	as-path  : 65000, 4000, {10, 20, 30}, 23456, 23456, 40001
   113  //	as4-path : 400000, 300000, 40001
   114  //
   115  // expected result:
   116  //
   117  //	as-path  : 65000, 4000, {10, 20, 30}, 400000, 300000, 40001
   118  func TestAsPathAs4Trans2(t *testing.T) {
   119  	as1 := []uint16{65000, 4000}
   120  	param1 := bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as1)
   121  	as2 := []uint16{10, 20, 30}
   122  	param2 := bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, as2)
   123  	as3 := []uint16{bgp.AS_TRANS, bgp.AS_TRANS, 40001}
   124  	param3 := bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as3)
   125  	params := []bgp.AsPathParamInterface{param1, param2, param3}
   126  	aspath := bgp.NewPathAttributeAsPath(params)
   127  
   128  	as4 := []uint32{400000, 300000, 40001}
   129  	param4s := []*bgp.As4PathParam{bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as4)}
   130  	as4path := bgp.NewPathAttributeAs4Path(param4s)
   131  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath, as4path}, nil).Body.(*bgp.BGPUpdate)
   132  	UpdatePathAttrs4ByteAs(logger, msg)
   133  	assert.Equal(t, len(msg.PathAttributes), 1)
   134  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 3)
   135  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS), 2)
   136  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[0], uint32(65000))
   137  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[1], uint32(4000))
   138  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS), 3)
   139  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS[0], uint32(10))
   140  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS[1], uint32(20))
   141  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS[2], uint32(30))
   142  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[2].(*bgp.As4PathParam).AS), 3)
   143  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[2].(*bgp.As4PathParam).AS[0], uint32(400000))
   144  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[2].(*bgp.As4PathParam).AS[1], uint32(300000))
   145  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[2].(*bgp.As4PathParam).AS[2], uint32(40001))
   146  }
   147  
   148  // before:
   149  //
   150  //	as-path  : 65000, 4000, {10, 20, 30}, 23456, 23456, 40001
   151  //	as4-path : 3000, 400000, 300000, 40001
   152  //
   153  // expected result:
   154  //
   155  //	as-path  : 65000, 4000, 3000, 400000, 300000, 40001
   156  func TestAsPathAs4Trans3(t *testing.T) {
   157  	as1 := []uint16{65000, 4000}
   158  	param1 := bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as1)
   159  	as2 := []uint16{10, 20, 30}
   160  	param2 := bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, as2)
   161  	as3 := []uint16{bgp.AS_TRANS, bgp.AS_TRANS, 40001}
   162  	param3 := bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as3)
   163  	params := []bgp.AsPathParamInterface{param1, param2, param3}
   164  	aspath := bgp.NewPathAttributeAsPath(params)
   165  
   166  	as4 := []uint32{3000, 400000, 300000, 40001}
   167  	param4s := []*bgp.As4PathParam{bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as4)}
   168  	as4path := bgp.NewPathAttributeAs4Path(param4s)
   169  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath, as4path}, nil).Body.(*bgp.BGPUpdate)
   170  	UpdatePathAttrs4ByteAs(logger, msg)
   171  	assert.Equal(t, len(msg.PathAttributes), 1)
   172  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 1)
   173  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS), 6)
   174  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[0], uint32(65000))
   175  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[1], uint32(4000))
   176  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[2], uint32(3000))
   177  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[3], uint32(400000))
   178  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[4], uint32(300000))
   179  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[5], uint32(40001))
   180  }
   181  
   182  // before:
   183  //
   184  //	as-path  : 65000, 4000, 23456, 23456, 40001
   185  //	as4-path : 400000, 300000, 40001, {10, 20, 30}
   186  //
   187  // expected result:
   188  //
   189  //	as-path  : 65000, 400000, 300000, 40001, {10, 20, 30}
   190  func TestAsPathAs4Trans4(t *testing.T) {
   191  	as := []uint16{65000, 4000, bgp.AS_TRANS, bgp.AS_TRANS, 40001}
   192  	params := []bgp.AsPathParamInterface{bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as)}
   193  	aspath := bgp.NewPathAttributeAsPath(params)
   194  
   195  	as4 := []uint32{400000, 300000, 40001}
   196  	as4param1 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as4)
   197  	as5 := []uint32{10, 20, 30}
   198  	as4param2 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, as5)
   199  	param4s := []*bgp.As4PathParam{as4param1, as4param2}
   200  	as4path := bgp.NewPathAttributeAs4Path(param4s)
   201  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath, as4path}, nil).Body.(*bgp.BGPUpdate)
   202  	UpdatePathAttrs4ByteAs(logger, msg)
   203  	assert.Equal(t, len(msg.PathAttributes), 1)
   204  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 2)
   205  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS), 4)
   206  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[0], uint32(65000))
   207  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[1], uint32(400000))
   208  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[2], uint32(300000))
   209  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[3], uint32(40001))
   210  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS), 3)
   211  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS[0], uint32(10))
   212  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS[1], uint32(20))
   213  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS[2], uint32(30))
   214  }
   215  
   216  // before:
   217  //
   218  //	as-path  : 65000, 4000, 23456, 23456, 40001
   219  //	as4-path : {10, 20, 30} 400000, 300000, 40001
   220  //
   221  // expected result:
   222  //
   223  //	as-path  : 65000, {10, 20, 30}, 400000, 300000, 40001
   224  func TestAsPathAs4Trans5(t *testing.T) {
   225  	as := []uint16{65000, 4000, bgp.AS_TRANS, bgp.AS_TRANS, 40001}
   226  	params := []bgp.AsPathParamInterface{bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as)}
   227  	aspath := bgp.NewPathAttributeAsPath(params)
   228  
   229  	as4 := []uint32{400000, 300000, 40001}
   230  	as4param1 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as4)
   231  	as5 := []uint32{10, 20, 30}
   232  	as4param2 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, as5)
   233  	param4s := []*bgp.As4PathParam{as4param2, as4param1}
   234  	as4path := bgp.NewPathAttributeAs4Path(param4s)
   235  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath, as4path}, nil).Body.(*bgp.BGPUpdate)
   236  	UpdatePathAttrs4ByteAs(logger, msg)
   237  	assert.Equal(t, len(msg.PathAttributes), 1)
   238  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 3)
   239  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS), 1)
   240  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[0], uint32(65000))
   241  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS), 3)
   242  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS[0], uint32(10))
   243  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS[1], uint32(20))
   244  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[1].(*bgp.As4PathParam).AS[2], uint32(30))
   245  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[2].(*bgp.As4PathParam).AS), 3)
   246  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[2].(*bgp.As4PathParam).AS[0], uint32(400000))
   247  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[2].(*bgp.As4PathParam).AS[1], uint32(300000))
   248  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[2].(*bgp.As4PathParam).AS[2], uint32(40001))
   249  }
   250  
   251  // before:
   252  //
   253  //	as-path  : 65000, 4000, 23456, 23456, 40001
   254  //	as4-path : 100000, 65000, 4000, 400000, 300000, 40001
   255  //
   256  // expected result:
   257  //
   258  //	as-path  : 65000, 4000, 23456, 23456, 40001
   259  func TestAsPathAs4TransInvalid1(t *testing.T) {
   260  	as := []uint16{65000, 4000, bgp.AS_TRANS, bgp.AS_TRANS, 40001}
   261  	params := []bgp.AsPathParamInterface{bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as)}
   262  	aspath := bgp.NewPathAttributeAsPath(params)
   263  
   264  	as4 := []uint32{100000, 65000, 4000, 400000, 300000, 40001}
   265  	as4param1 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as4)
   266  	param4s := []*bgp.As4PathParam{as4param1}
   267  	as4path := bgp.NewPathAttributeAs4Path(param4s)
   268  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath, as4path}, nil).Body.(*bgp.BGPUpdate)
   269  	UpdatePathAttrs4ByteAs(logger, msg)
   270  	assert.Equal(t, len(msg.PathAttributes), 1)
   271  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 1)
   272  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS), 5)
   273  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[0], uint32(65000))
   274  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[1], uint32(4000))
   275  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[2], uint32(bgp.AS_TRANS))
   276  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[3], uint32(bgp.AS_TRANS))
   277  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[4], uint32(40001))
   278  }
   279  
   280  // before:
   281  //
   282  //	as-path  : 65000, 4000, 23456, 23456, 40001
   283  //	as4-path : 300000, 40001
   284  //
   285  // expected result:
   286  //
   287  //	as-path  : 65000, 4000, 23456, 300000, 40001
   288  func TestAsPathAs4TransInvalid2(t *testing.T) {
   289  	as := []uint16{65000, 4000, bgp.AS_TRANS, bgp.AS_TRANS, 40001}
   290  	params := []bgp.AsPathParamInterface{bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as)}
   291  	aspath := bgp.NewPathAttributeAsPath(params)
   292  
   293  	as4 := []uint32{300000, 40001}
   294  	as4param1 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as4)
   295  	param4s := []*bgp.As4PathParam{as4param1}
   296  	as4path := bgp.NewPathAttributeAs4Path(param4s)
   297  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath, as4path}, nil).Body.(*bgp.BGPUpdate)
   298  	UpdatePathAttrs4ByteAs(logger, msg)
   299  	assert.Equal(t, len(msg.PathAttributes), 1)
   300  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 1)
   301  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS), 5)
   302  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[0], uint32(65000))
   303  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[1], uint32(4000))
   304  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[2], uint32(bgp.AS_TRANS))
   305  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[3], uint32(300000))
   306  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[4], uint32(40001))
   307  }
   308  
   309  // before:
   310  //
   311  //	as-path  : 65000, 4000, 23456, 23456, 40001
   312  //	as4-path : nil
   313  //
   314  // expected result:
   315  //
   316  //	as-path  : 65000, 4000, 23456, 23456, 40001
   317  func TestAsPathAs4TransInvalid3(t *testing.T) {
   318  	as := []uint16{65000, 4000, bgp.AS_TRANS, bgp.AS_TRANS, 40001}
   319  	params := []bgp.AsPathParamInterface{bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as)}
   320  	aspath := bgp.NewPathAttributeAsPath(params)
   321  
   322  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath}, nil).Body.(*bgp.BGPUpdate)
   323  	UpdatePathAttrs4ByteAs(logger, msg)
   324  	assert.Equal(t, len(msg.PathAttributes), 1)
   325  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 1)
   326  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS), 5)
   327  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[0], uint32(65000))
   328  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[1], uint32(4000))
   329  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[2], uint32(bgp.AS_TRANS))
   330  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[3], uint32(bgp.AS_TRANS))
   331  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[4], uint32(40001))
   332  }
   333  
   334  // before:
   335  //
   336  //	as-path  : 65000, 4000, 23456, 23456, 40001
   337  //	as4-path : empty
   338  //
   339  // expected result:
   340  //
   341  //	as-path  : 65000, 4000, 23456, 23456, 40001
   342  func TestAsPathAs4TransInvalid4(t *testing.T) {
   343  	as := []uint16{65000, 4000, bgp.AS_TRANS, bgp.AS_TRANS, 40001}
   344  	params := []bgp.AsPathParamInterface{bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as)}
   345  	aspath := bgp.NewPathAttributeAsPath(params)
   346  
   347  	as4 := []uint32{}
   348  	as4param1 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as4)
   349  	param4s := []*bgp.As4PathParam{as4param1}
   350  	as4path := bgp.NewPathAttributeAs4Path(param4s)
   351  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath, as4path}, nil).Body.(*bgp.BGPUpdate)
   352  	UpdatePathAttrs4ByteAs(logger, msg)
   353  	assert.Equal(t, len(msg.PathAttributes), 1)
   354  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value), 1)
   355  	assert.Equal(t, len(msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS), 5)
   356  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[0], uint32(65000))
   357  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[1], uint32(4000))
   358  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[2], uint32(bgp.AS_TRANS))
   359  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[3], uint32(bgp.AS_TRANS))
   360  	assert.Equal(t, msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value[0].(*bgp.As4PathParam).AS[4], uint32(40001))
   361  }
   362  
   363  func TestASPathAs4TransMultipleParams(t *testing.T) {
   364  	as1 := []uint16{17676, 2914, 174, 50607}
   365  	as2 := []uint16{bgp.AS_TRANS, bgp.AS_TRANS}
   366  	params := []bgp.AsPathParamInterface{bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as1), bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as2)}
   367  	aspath := bgp.NewPathAttributeAsPath(params)
   368  
   369  	as41 := []uint32{2914, 174, 50607}
   370  	as42 := []uint32{198035, 198035}
   371  	as4param1 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as41)
   372  	as4param2 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as42)
   373  	param4s := []*bgp.As4PathParam{as4param1, as4param2}
   374  	as4path := bgp.NewPathAttributeAs4Path(param4s)
   375  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath, as4path}, nil).Body.(*bgp.BGPUpdate)
   376  	UpdatePathAttrs4ByteAs(logger, msg)
   377  	for _, param := range msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value {
   378  		p := param.(*bgp.As4PathParam)
   379  		assert.Equal(t, p.Num, uint8(len(p.AS)))
   380  	}
   381  }
   382  
   383  func TestASPathAs4TransMultipleLargeParams(t *testing.T) {
   384  	as1 := make([]uint16, 0, 255)
   385  	for i := 0; i < 255-5; i++ {
   386  		as1 = append(as1, uint16(i+1))
   387  	}
   388  	as1 = append(as1, []uint16{17676, 2914, 174, 50607}...)
   389  	as2 := []uint16{bgp.AS_TRANS, bgp.AS_TRANS}
   390  	params := []bgp.AsPathParamInterface{bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as1), bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as2)}
   391  	aspath := bgp.NewPathAttributeAsPath(params)
   392  
   393  	as41 := []uint32{2914, 174, 50607}
   394  	as42 := []uint32{198035, 198035}
   395  	as4param1 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as41)
   396  	as4param2 := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, as42)
   397  	param4s := []*bgp.As4PathParam{as4param1, as4param2}
   398  	as4path := bgp.NewPathAttributeAs4Path(param4s)
   399  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{aspath, as4path}, nil).Body.(*bgp.BGPUpdate)
   400  	UpdatePathAttrs4ByteAs(logger, msg)
   401  	for _, param := range msg.PathAttributes[0].(*bgp.PathAttributeAsPath).Value {
   402  		p := param.(*bgp.As4PathParam)
   403  		assert.Equal(t, p.Num, uint8(len(p.AS)))
   404  	}
   405  }
   406  
   407  func TestAggregator4BytesASes(t *testing.T) {
   408  	getAggr := func(msg *bgp.BGPUpdate) *bgp.PathAttributeAggregator {
   409  		for _, attr := range msg.PathAttributes {
   410  			switch a := attr.(type) {
   411  			case *bgp.PathAttributeAggregator:
   412  				return a
   413  			}
   414  		}
   415  		return nil
   416  	}
   417  
   418  	getAggr4 := func(msg *bgp.BGPUpdate) *bgp.PathAttributeAs4Aggregator {
   419  		for _, attr := range msg.PathAttributes {
   420  			switch a := attr.(type) {
   421  			case *bgp.PathAttributeAs4Aggregator:
   422  				return a
   423  			}
   424  		}
   425  		return nil
   426  	}
   427  
   428  	addr := "192.168.0.1"
   429  	as4 := uint32(100000)
   430  	as := uint32(1000)
   431  	msg := bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{bgp.NewPathAttributeAggregator(as4, addr)}, nil).Body.(*bgp.BGPUpdate)
   432  
   433  	// 4byte capable to 4byte capable for 4 bytes AS
   434  	assert.Equal(t, UpdatePathAggregator4ByteAs(msg), nil)
   435  	assert.Equal(t, getAggr(msg).Value.AS, as4)
   436  	assert.Equal(t, getAggr(msg).Value.Address.String(), addr)
   437  
   438  	// 4byte capable to 2byte capable for 4 bytes AS
   439  	UpdatePathAggregator2ByteAs(msg)
   440  	assert.Equal(t, getAggr(msg).Value.AS, uint32(bgp.AS_TRANS))
   441  	assert.Equal(t, getAggr(msg).Value.Askind, reflect.Uint16)
   442  	assert.Equal(t, getAggr4(msg).Value.AS, as4)
   443  	assert.Equal(t, getAggr4(msg).Value.Address.String(), addr)
   444  
   445  	msg = bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{bgp.NewPathAttributeAggregator(uint16(bgp.AS_TRANS), addr), bgp.NewPathAttributeAs4Aggregator(as4, addr)}, nil).Body.(*bgp.BGPUpdate)
   446  	assert.Equal(t, getAggr(msg).Value.AS, uint32(bgp.AS_TRANS))
   447  	assert.Equal(t, getAggr(msg).Value.Askind, reflect.Uint16)
   448  
   449  	// non 4byte capable to 4byte capable for 4 bytes AS
   450  	assert.Equal(t, UpdatePathAggregator4ByteAs(msg), nil)
   451  	assert.Equal(t, getAggr(msg).Value.AS, as4)
   452  	assert.Equal(t, getAggr(msg).Value.Askind, reflect.Uint32)
   453  	assert.Equal(t, getAggr(msg).Value.Address.String(), addr)
   454  	assert.Equal(t, getAggr4(msg), (*bgp.PathAttributeAs4Aggregator)(nil))
   455  
   456  	// non 4byte capable to non 4byte capable for 4 bytes AS
   457  	UpdatePathAggregator2ByteAs(msg)
   458  	assert.Equal(t, getAggr(msg).Value.AS, uint32(bgp.AS_TRANS))
   459  	assert.Equal(t, getAggr(msg).Value.Askind, reflect.Uint16)
   460  	assert.Equal(t, getAggr4(msg).Value.AS, as4)
   461  	assert.Equal(t, getAggr4(msg).Value.Address.String(), addr)
   462  
   463  	msg = bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{bgp.NewPathAttributeAggregator(uint32(as), addr)}, nil).Body.(*bgp.BGPUpdate)
   464  	// 4byte capable to 4byte capable for 2 bytes AS
   465  	assert.Equal(t, getAggr(msg).Value.AS, as)
   466  	assert.Equal(t, getAggr(msg).Value.Askind, reflect.Uint32)
   467  	assert.Equal(t, UpdatePathAggregator4ByteAs(msg), nil)
   468  	assert.Equal(t, getAggr(msg).Value.AS, as)
   469  	assert.Equal(t, getAggr(msg).Value.Askind, reflect.Uint32)
   470  
   471  	// 4byte capable to non 4byte capable for 2 bytes AS
   472  	UpdatePathAggregator2ByteAs(msg)
   473  	assert.Equal(t, getAggr4(msg), (*bgp.PathAttributeAs4Aggregator)(nil))
   474  	assert.Equal(t, getAggr(msg).Value.Askind, reflect.Uint16)
   475  	assert.Equal(t, getAggr(msg).Value.AS, as)
   476  
   477  	msg = bgp.NewBGPUpdateMessage(nil, []bgp.PathAttributeInterface{bgp.NewPathAttributeAggregator(uint16(as), addr)}, nil).Body.(*bgp.BGPUpdate)
   478  	// non 4byte capable to 4byte capable for 2 bytes AS
   479  	assert.Equal(t, getAggr(msg).Value.AS, as)
   480  	assert.Equal(t, getAggr(msg).Value.Askind, reflect.Uint16)
   481  	assert.Equal(t, UpdatePathAggregator4ByteAs(msg), nil)
   482  
   483  	assert.Equal(t, getAggr(msg).Value.AS, as)
   484  	assert.Equal(t, getAggr(msg).Value.Askind, reflect.Uint32)
   485  
   486  	// non 4byte capable to non 4byte capable for 2 bytes AS
   487  	UpdatePathAggregator2ByteAs(msg)
   488  	assert.Equal(t, getAggr(msg).Value.AS, as)
   489  	assert.Equal(t, getAggr(msg).Value.Askind, reflect.Uint16)
   490  	assert.Equal(t, getAggr4(msg), (*bgp.PathAttributeAs4Aggregator)(nil))
   491  }
   492  
   493  func TestBMP(t *testing.T) {
   494  	aspath1 := []bgp.AsPathParamInterface{
   495  		bgp.NewAs4PathParam(2, []uint32{1000000}),
   496  		bgp.NewAs4PathParam(1, []uint32{1000001, 1002}),
   497  		bgp.NewAs4PathParam(2, []uint32{1003, 100004}),
   498  	}
   499  	mp_nlri := []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(100,
   500  		"fe80:1234:1234:5667:8967:af12:8912:1023")}
   501  
   502  	p := []bgp.PathAttributeInterface{
   503  		bgp.NewPathAttributeOrigin(3),
   504  		bgp.NewPathAttributeAsPath(aspath1),
   505  		bgp.NewPathAttributeMpUnreachNLRI(mp_nlri),
   506  	}
   507  	w := []*bgp.IPAddrPrefix{}
   508  	n := []*bgp.IPAddrPrefix{}
   509  
   510  	msg := bgp.NewBGPUpdateMessage(w, p, n)
   511  	pList := ProcessMessage(msg, peerR1(), time.Now())
   512  	CreateUpdateMsgFromPaths(pList)
   513  }
   514  
   515  func unreachIndex(msgs []*bgp.BGPMessage) int {
   516  	for i := range msgs {
   517  		for _, a := range msgs[i].Body.(*bgp.BGPUpdate).PathAttributes {
   518  			if a.GetType() == bgp.BGP_ATTR_TYPE_MP_UNREACH_NLRI {
   519  				return i
   520  			}
   521  		}
   522  	}
   523  	// should not be here
   524  	return -1
   525  }
   526  
   527  func TestMixedMPReachMPUnreach(t *testing.T) {
   528  	aspath1 := []bgp.AsPathParamInterface{
   529  		bgp.NewAs4PathParam(2, []uint32{100}),
   530  	}
   531  	nlri1 := []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(32, "2222::")}
   532  	nlri2 := []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(32, "1111::")}
   533  
   534  	p := []bgp.PathAttributeInterface{
   535  		bgp.NewPathAttributeOrigin(0),
   536  		bgp.NewPathAttributeAsPath(aspath1),
   537  		bgp.NewPathAttributeMpReachNLRI("1::1", nlri1),
   538  		bgp.NewPathAttributeMpUnreachNLRI(nlri2),
   539  	}
   540  	msg := bgp.NewBGPUpdateMessage(nil, p, nil)
   541  	pList := ProcessMessage(msg, peerR1(), time.Now())
   542  	assert.Equal(t, len(pList), 2)
   543  	assert.Equal(t, pList[0].IsWithdraw, false)
   544  	assert.Equal(t, pList[1].IsWithdraw, true)
   545  	msgs := CreateUpdateMsgFromPaths(pList)
   546  	assert.Equal(t, len(msgs), 2)
   547  
   548  	uIndex := unreachIndex(msgs)
   549  	rIndex := 0
   550  	if uIndex == 0 {
   551  		rIndex = 1
   552  	}
   553  	assert.Equal(t, len(msgs[uIndex].Body.(*bgp.BGPUpdate).PathAttributes), 1)
   554  	assert.Equal(t, len(msgs[rIndex].Body.(*bgp.BGPUpdate).PathAttributes), 3)
   555  }
   556  
   557  func TestMixedNLRIAndMPUnreach(t *testing.T) {
   558  	aspath1 := []bgp.AsPathParamInterface{
   559  		bgp.NewAs4PathParam(2, []uint32{100}),
   560  	}
   561  	nlri1 := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(24, "10.0.0.0")}
   562  	nlri2 := []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(32, "1111::")}
   563  
   564  	p := []bgp.PathAttributeInterface{
   565  		bgp.NewPathAttributeOrigin(0),
   566  		bgp.NewPathAttributeAsPath(aspath1),
   567  		bgp.NewPathAttributeNextHop("1.1.1.1"),
   568  		bgp.NewPathAttributeMpUnreachNLRI(nlri2),
   569  	}
   570  	msg := bgp.NewBGPUpdateMessage(nil, p, nlri1)
   571  	pList := ProcessMessage(msg, peerR1(), time.Now())
   572  
   573  	assert.Equal(t, len(pList), 2)
   574  	assert.Equal(t, pList[0].IsWithdraw, false)
   575  	assert.Equal(t, pList[1].IsWithdraw, true)
   576  	msgs := CreateUpdateMsgFromPaths(pList)
   577  	assert.Equal(t, len(msgs), 2)
   578  
   579  	uIndex := unreachIndex(msgs)
   580  	rIndex := 0
   581  	if uIndex == 0 {
   582  		rIndex = 1
   583  	}
   584  	assert.Equal(t, len(msgs[uIndex].Body.(*bgp.BGPUpdate).PathAttributes), 1)
   585  	assert.Equal(t, len(msgs[rIndex].Body.(*bgp.BGPUpdate).PathAttributes), 3)
   586  }
   587  
   588  func TestMergeV4NLRIs(t *testing.T) {
   589  	aspath1 := []bgp.AsPathParamInterface{
   590  		bgp.NewAs4PathParam(2, []uint32{100}),
   591  	}
   592  	attrs := []bgp.PathAttributeInterface{
   593  		bgp.NewPathAttributeOrigin(0),
   594  		bgp.NewPathAttributeAsPath(aspath1),
   595  		bgp.NewPathAttributeNextHop("1.1.1.1"),
   596  	}
   597  
   598  	nr := 1024
   599  	paths := make([]*Path, 0, nr)
   600  	addrs := make([]string, 0, nr)
   601  	for i := 0; i < nr; i++ {
   602  		addrs = append(addrs, fmt.Sprintf("1.1.%d.%d", i>>8&0xff, i&0xff))
   603  		nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(32, addrs[i])}
   604  		msg := bgp.NewBGPUpdateMessage(nil, attrs, nlri)
   605  		paths = append(paths, ProcessMessage(msg, peerR1(), time.Now())...)
   606  	}
   607  	msgs := CreateUpdateMsgFromPaths(paths)
   608  	assert.Equal(t, len(msgs), 2)
   609  
   610  	l := make([]*bgp.IPAddrPrefix, 0, nr)
   611  	for _, msg := range msgs {
   612  		u := msg.Body.(*bgp.BGPUpdate)
   613  		assert.Equal(t, len(u.PathAttributes), 3)
   614  		l = append(l, u.NLRI...)
   615  	}
   616  
   617  	assert.Equal(t, len(l), nr)
   618  	for i, addr := range addrs {
   619  		assert.Equal(t, addr, l[i].Prefix.String())
   620  	}
   621  	for _, msg := range msgs {
   622  		d, _ := msg.Serialize()
   623  		assert.True(t, len(d) < bgp.BGP_MAX_MESSAGE_LENGTH)
   624  	}
   625  }
   626  
   627  func TestNotMergeV4NLRIs(t *testing.T) {
   628  	paths := make([]*Path, 0, 2)
   629  
   630  	aspath1 := []bgp.AsPathParamInterface{
   631  		bgp.NewAs4PathParam(2, []uint32{100}),
   632  	}
   633  	attrs1 := []bgp.PathAttributeInterface{
   634  		bgp.NewPathAttributeOrigin(0),
   635  		bgp.NewPathAttributeAsPath(aspath1),
   636  		bgp.NewPathAttributeNextHop("1.1.1.1"),
   637  	}
   638  	nlri1 := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(32, "1.1.1.1")}
   639  	paths = append(paths, ProcessMessage(bgp.NewBGPUpdateMessage(nil, attrs1, nlri1), peerR1(), time.Now())...)
   640  
   641  	attrs2 := []bgp.PathAttributeInterface{
   642  		bgp.NewPathAttributeOrigin(0),
   643  		bgp.NewPathAttributeAsPath(aspath1),
   644  		bgp.NewPathAttributeNextHop("2.2.2.2"),
   645  	}
   646  	nlri2 := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(32, "2.2.2.2")}
   647  	paths = append(paths, ProcessMessage(bgp.NewBGPUpdateMessage(nil, attrs2, nlri2), peerR1(), time.Now())...)
   648  
   649  	assert.NotEmpty(t, paths[0].GetHash(), paths[1].GetHash())
   650  
   651  	msgs := CreateUpdateMsgFromPaths(paths)
   652  	assert.Equal(t, len(msgs), 2)
   653  
   654  	paths[1].SetHash(paths[0].GetHash())
   655  	msgs = CreateUpdateMsgFromPaths(paths)
   656  	assert.Equal(t, len(msgs), 2)
   657  }
   658  
   659  func TestMergeV4Withdraw(t *testing.T) {
   660  	nr := 1024
   661  	paths := make([]*Path, 0, nr)
   662  	addrs := make([]string, 0, nr)
   663  	for i := 0; i < nr; i++ {
   664  		addrs = append(addrs, fmt.Sprintf("1.1.%d.%d", i>>8&0xff, i&0xff))
   665  		nlri := []*bgp.IPAddrPrefix{bgp.NewIPAddrPrefix(32, addrs[i])}
   666  		// use different attribute for each nlri
   667  		aspath1 := []bgp.AsPathParamInterface{
   668  			bgp.NewAs4PathParam(2, []uint32{uint32(i)}),
   669  		}
   670  		attrs := []bgp.PathAttributeInterface{
   671  			bgp.NewPathAttributeOrigin(0),
   672  			bgp.NewPathAttributeAsPath(aspath1),
   673  			bgp.NewPathAttributeNextHop("1.1.1.1"),
   674  		}
   675  		msg := bgp.NewBGPUpdateMessage(nlri, attrs, nil)
   676  		paths = append(paths, ProcessMessage(msg, peerR1(), time.Now())...)
   677  	}
   678  	msgs := CreateUpdateMsgFromPaths(paths)
   679  	assert.Equal(t, len(msgs), 2)
   680  
   681  	l := make([]*bgp.IPAddrPrefix, 0, nr)
   682  	for _, msg := range msgs {
   683  		u := msg.Body.(*bgp.BGPUpdate)
   684  		assert.Equal(t, len(u.PathAttributes), 0)
   685  		l = append(l, u.WithdrawnRoutes...)
   686  	}
   687  	assert.Equal(t, len(l), nr)
   688  	for i, addr := range addrs {
   689  		assert.Equal(t, addr, l[i].Prefix.String())
   690  	}
   691  
   692  	for _, msg := range msgs {
   693  		d, _ := msg.Serialize()
   694  		assert.True(t, len(d) < bgp.BGP_MAX_MESSAGE_LENGTH)
   695  	}
   696  }