github.com/osrg/gobgp/v3@v3.30.0/pkg/packet/mrt/mrt_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 mrt
    17  
    18  import (
    19  	"bufio"
    20  	"bytes"
    21  	"reflect"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/osrg/gobgp/v3/pkg/packet/bgp"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestMrtHdr(t *testing.T) {
    30  	h1, err := NewMRTHeader(10, TABLE_DUMPv2, RIB_IPV4_MULTICAST, 20)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	b1, err := h1.Serialize()
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	h2 := &MRTHeader{}
    39  	err = h2.DecodeFromBytes(b1)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	assert.Equal(t, reflect.DeepEqual(h1, h2), true)
    44  }
    45  
    46  func TestMrtHdrTime(t *testing.T) {
    47  	h1, err := NewMRTHeader(10, TABLE_DUMPv2, RIB_IPV4_MULTICAST, 20)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	ttime := time.Unix(10, 0)
    52  	htime := h1.GetTime()
    53  	t.Logf("this timestamp should be 10s after epoch:%v", htime)
    54  	assert.Equal(t, h1.GetTime(), ttime)
    55  }
    56  
    57  func testPeer(t *testing.T, p1 *Peer) {
    58  	b1, err := p1.Serialize()
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	p2 := &Peer{}
    63  	rest, err := p2.DecodeFromBytes(b1)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	assert.Equal(t, len(rest), 0)
    68  	assert.Equal(t, reflect.DeepEqual(p1, p2), true)
    69  }
    70  
    71  func TestMrtPeer(t *testing.T) {
    72  	p := NewPeer("192.168.0.1", "10.0.0.1", 65000, false)
    73  	testPeer(t, p)
    74  }
    75  
    76  func TestMrtPeerv6(t *testing.T) {
    77  	p := NewPeer("192.168.0.1", "2001::1", 65000, false)
    78  	testPeer(t, p)
    79  }
    80  
    81  func TestMrtPeerAS4(t *testing.T) {
    82  	p := NewPeer("192.168.0.1", "2001::1", 135500, true)
    83  	testPeer(t, p)
    84  }
    85  
    86  func TestMrtPeerIndexTable(t *testing.T) {
    87  	p1 := NewPeer("192.168.0.1", "10.0.0.1", 65000, false)
    88  	p2 := NewPeer("192.168.0.1", "2001::1", 65000, false)
    89  	p3 := NewPeer("192.168.0.1", "2001::1", 135500, true)
    90  	pt1 := NewPeerIndexTable("192.168.0.1", "test", []*Peer{p1, p2, p3})
    91  	b1, err := pt1.Serialize()
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  	pt2 := &PeerIndexTable{}
    96  	err = pt2.DecodeFromBytes(b1)
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	assert.Equal(t, reflect.DeepEqual(pt1, pt2), true)
   101  }
   102  
   103  func TestMrtRibEntry(t *testing.T) {
   104  	aspath1 := []bgp.AsPathParamInterface{
   105  		bgp.NewAsPathParam(2, []uint16{1000}),
   106  		bgp.NewAsPathParam(1, []uint16{1001, 1002}),
   107  		bgp.NewAsPathParam(2, []uint16{1003, 1004}),
   108  	}
   109  
   110  	p := []bgp.PathAttributeInterface{
   111  		bgp.NewPathAttributeOrigin(3),
   112  		bgp.NewPathAttributeAsPath(aspath1),
   113  		bgp.NewPathAttributeNextHop("129.1.1.2"),
   114  		bgp.NewPathAttributeMultiExitDisc(1 << 20),
   115  		bgp.NewPathAttributeLocalPref(1 << 22),
   116  	}
   117  
   118  	e1 := NewRibEntry(1, uint32(time.Now().Unix()), 0, p, false)
   119  	b1, err := e1.Serialize()
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	e2 := &RibEntry{}
   125  	rest, err := e2.DecodeFromBytes(b1)
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	assert.Equal(t, len(rest), 0)
   130  	assert.Equal(t, reflect.DeepEqual(e1, e2), true)
   131  }
   132  
   133  func TestMrtRibEntryWithAddPath(t *testing.T) {
   134  	aspath1 := []bgp.AsPathParamInterface{
   135  		bgp.NewAsPathParam(2, []uint16{1000}),
   136  		bgp.NewAsPathParam(1, []uint16{1001, 1002}),
   137  		bgp.NewAsPathParam(2, []uint16{1003, 1004}),
   138  	}
   139  
   140  	p := []bgp.PathAttributeInterface{
   141  		bgp.NewPathAttributeOrigin(3),
   142  		bgp.NewPathAttributeAsPath(aspath1),
   143  		bgp.NewPathAttributeNextHop("129.1.1.2"),
   144  		bgp.NewPathAttributeMultiExitDisc(1 << 20),
   145  		bgp.NewPathAttributeLocalPref(1 << 22),
   146  	}
   147  	e1 := NewRibEntry(1, uint32(time.Now().Unix()), 200, p, true)
   148  	b1, err := e1.Serialize()
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  
   153  	e2 := &RibEntry{isAddPath: true}
   154  	rest, err := e2.DecodeFromBytes(b1)
   155  	if err != nil {
   156  		t.Fatal(err)
   157  	}
   158  	assert.Equal(t, len(rest), 0)
   159  	assert.Equal(t, reflect.DeepEqual(e1, e2), true)
   160  }
   161  
   162  func TestMrtRib(t *testing.T) {
   163  	aspath1 := []bgp.AsPathParamInterface{
   164  		bgp.NewAsPathParam(2, []uint16{1000}),
   165  		bgp.NewAsPathParam(1, []uint16{1001, 1002}),
   166  		bgp.NewAsPathParam(2, []uint16{1003, 1004}),
   167  	}
   168  
   169  	p := []bgp.PathAttributeInterface{
   170  		bgp.NewPathAttributeOrigin(3),
   171  		bgp.NewPathAttributeAsPath(aspath1),
   172  		bgp.NewPathAttributeNextHop("129.1.1.2"),
   173  		bgp.NewPathAttributeMultiExitDisc(1 << 20),
   174  		bgp.NewPathAttributeLocalPref(1 << 22),
   175  	}
   176  
   177  	e1 := NewRibEntry(1, uint32(time.Now().Unix()), 0, p, false)
   178  	e2 := NewRibEntry(2, uint32(time.Now().Unix()), 0, p, false)
   179  	e3 := NewRibEntry(3, uint32(time.Now().Unix()), 0, p, false)
   180  
   181  	r1 := NewRib(1, bgp.NewIPAddrPrefix(24, "192.168.0.0"), []*RibEntry{e1, e2, e3})
   182  	b1, err := r1.Serialize()
   183  	if err != nil {
   184  		t.Fatal(err)
   185  	}
   186  	r2 := &Rib{
   187  		RouteFamily: bgp.RF_IPv4_UC,
   188  	}
   189  	err = r2.DecodeFromBytes(b1)
   190  	if err != nil {
   191  		t.Fatal(err)
   192  	}
   193  	assert.Equal(t, reflect.DeepEqual(r1, r2), true)
   194  }
   195  
   196  func TestMrtRibWithAddPath(t *testing.T) {
   197  	aspath1 := []bgp.AsPathParamInterface{
   198  		bgp.NewAsPathParam(2, []uint16{1000}),
   199  		bgp.NewAsPathParam(1, []uint16{1001, 1002}),
   200  		bgp.NewAsPathParam(2, []uint16{1003, 1004}),
   201  	}
   202  
   203  	p := []bgp.PathAttributeInterface{
   204  		bgp.NewPathAttributeOrigin(3),
   205  		bgp.NewPathAttributeAsPath(aspath1),
   206  		bgp.NewPathAttributeNextHop("129.1.1.2"),
   207  		bgp.NewPathAttributeMultiExitDisc(1 << 20),
   208  		bgp.NewPathAttributeLocalPref(1 << 22),
   209  	}
   210  
   211  	e1 := NewRibEntry(1, uint32(time.Now().Unix()), 100, p, true)
   212  	e2 := NewRibEntry(2, uint32(time.Now().Unix()), 200, p, true)
   213  	e3 := NewRibEntry(3, uint32(time.Now().Unix()), 300, p, true)
   214  
   215  	r1 := NewRib(1, bgp.NewIPAddrPrefix(24, "192.168.0.0"), []*RibEntry{e1, e2, e3})
   216  	b1, err := r1.Serialize()
   217  	if err != nil {
   218  		t.Fatal(err)
   219  	}
   220  	r2 := &Rib{
   221  		RouteFamily: bgp.RF_IPv4_UC,
   222  		isAddPath:   true,
   223  	}
   224  	err = r2.DecodeFromBytes(b1)
   225  	if err != nil {
   226  		t.Fatal(err)
   227  	}
   228  	assert.Equal(t, reflect.DeepEqual(r1, r2), true)
   229  }
   230  
   231  func TestMrtGeoPeerTable(t *testing.T) {
   232  	p1 := NewGeoPeer("192.168.0.1", 28.031157, 86.899684)
   233  	p2 := NewGeoPeer("192.168.0.1", 35.360556, 138.727778)
   234  	pt1 := NewGeoPeerTable("192.168.0.1", 12.345678, 98.765432, []*GeoPeer{p1, p2})
   235  	b1, err := pt1.Serialize()
   236  	if err != nil {
   237  		t.Fatal(err)
   238  	}
   239  	pt2 := &GeoPeerTable{}
   240  	err = pt2.DecodeFromBytes(b1)
   241  	if err != nil {
   242  		t.Fatal(err)
   243  	}
   244  	assert.Equal(t, reflect.DeepEqual(pt1, pt2), true)
   245  }
   246  
   247  func TestMrtBgp4mpStateChange(t *testing.T) {
   248  	c1 := NewBGP4MPStateChange(65000, 65001, 1, "192.168.0.1", "192.168.0.2", false, ACTIVE, ESTABLISHED)
   249  	b1, err := c1.Serialize()
   250  	if err != nil {
   251  		t.Fatal(err)
   252  	}
   253  	c2 := &BGP4MPStateChange{BGP4MPHeader: &BGP4MPHeader{}}
   254  	err = c2.DecodeFromBytes(b1)
   255  	if err != nil {
   256  		t.Fatal(err)
   257  	}
   258  	_, err = c2.Serialize()
   259  	if err != nil {
   260  		t.Fatal(err)
   261  	}
   262  	assert.Equal(t, reflect.DeepEqual(c1, c2), true)
   263  }
   264  
   265  func TestMrtBgp4mpMessage(t *testing.T) {
   266  	msg := bgp.NewBGPKeepAliveMessage()
   267  	m1 := NewBGP4MPMessage(65000, 65001, 1, "192.168.0.1", "192.168.0.2", false, msg)
   268  	b1, err := m1.Serialize()
   269  	if err != nil {
   270  		t.Fatal(err)
   271  	}
   272  	m2 := &BGP4MPMessage{BGP4MPHeader: &BGP4MPHeader{}}
   273  	err = m2.DecodeFromBytes(b1)
   274  	if err != nil {
   275  		t.Fatal(err)
   276  	}
   277  	assert.Equal(t, reflect.DeepEqual(m1, m2), true)
   278  }
   279  
   280  func TestMrtSplit(t *testing.T) {
   281  	var b bytes.Buffer
   282  	numwrite, numread := 10, 0
   283  	for i := 0; i < numwrite; i++ {
   284  		msg := bgp.NewBGPKeepAliveMessage()
   285  		m1 := NewBGP4MPMessage(65000, 65001, 1, "192.168.0.1", "192.168.0.2", false, msg)
   286  		mm, _ := NewMRTMessage(1234, BGP4MP, MESSAGE, m1)
   287  		b1, err := mm.Serialize()
   288  		if err != nil {
   289  			t.Fatal(err)
   290  		}
   291  		b.Write(b1)
   292  	}
   293  	t.Logf("wrote %d serialized MRT keepalives in the buffer", numwrite)
   294  	r := bytes.NewReader(b.Bytes())
   295  	scanner := bufio.NewScanner(r)
   296  	scanner.Split(SplitMrt)
   297  	for scanner.Scan() {
   298  		numread += 1
   299  	}
   300  	t.Logf("scanner scanned %d serialized keepalives from the buffer", numread)
   301  	assert.Equal(t, numwrite, numread)
   302  }
   303  
   304  func FuzzMRT(f *testing.F) {
   305  
   306  	f.Fuzz(func(t *testing.T, data []byte) {
   307  		if len(data) < 16 {
   308  			return
   309  		}
   310  
   311  		hdr := &MRTHeader{}
   312  		err := hdr.DecodeFromBytes(data[:MRT_COMMON_HEADER_LEN])
   313  
   314  		if err != nil {
   315  			return
   316  		}
   317  
   318  		ParseMRTBody(hdr, data[MRT_COMMON_HEADER_LEN:])
   319  	})
   320  }