github.com/osrg/gobgp/v3@v3.30.0/pkg/packet/rtr/rtr_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 rtr
    17  
    18  import (
    19  	"encoding/hex"
    20  	"math/rand"
    21  	"net"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func verifyRTRMessage(t *testing.T, m1 RTRMessage) {
    30  	buf1, _ := m1.Serialize()
    31  	m2, err := ParseRTR(buf1)
    32  	require.NoError(t, err)
    33  
    34  	buf2, err := m2.Serialize()
    35  	require.NoError(t, err)
    36  
    37  	assert.Equal(t, buf1, buf2, "buf1: %v buf2: %v", hex.EncodeToString(buf1), hex.EncodeToString(buf2))
    38  }
    39  
    40  func randUint32() uint32 {
    41  	return rand.Uint32()
    42  }
    43  
    44  func Test_RTRSerialNotify(t *testing.T) {
    45  	id := uint16(time.Now().Unix())
    46  	sn := randUint32()
    47  	verifyRTRMessage(t, NewRTRSerialNotify(id, sn))
    48  }
    49  
    50  func Test_RTRSerialQuery(t *testing.T) {
    51  	id := uint16(time.Now().Unix())
    52  	sn := randUint32()
    53  	verifyRTRMessage(t, NewRTRSerialQuery(id, sn))
    54  }
    55  
    56  func Test_RTRResetQuery(t *testing.T) {
    57  	verifyRTRMessage(t, NewRTRResetQuery())
    58  }
    59  
    60  func Test_RTRCacheResponse(t *testing.T) {
    61  	id := uint16(time.Now().Unix())
    62  	verifyRTRMessage(t, NewRTRCacheResponse(id))
    63  }
    64  
    65  type rtrIPPrefixTestCase struct {
    66  	pString string
    67  	pLen    uint8
    68  	mLen    uint8
    69  	asn     uint32
    70  	flags   uint8
    71  }
    72  
    73  var rtrIPPrefixTestCases = []rtrIPPrefixTestCase{
    74  	{"192.168.0.0", 16, 32, 65001, ANNOUNCEMENT},
    75  	{"192.168.0.0", 16, 32, 65001, WITHDRAWAL},
    76  	{"2001:db8::", 32, 128, 65001, ANNOUNCEMENT},
    77  	{"2001:db8::", 32, 128, 65001, WITHDRAWAL},
    78  	{"::ffff:0.0.0.0", 96, 128, 65001, ANNOUNCEMENT},
    79  	{"::ffff:0.0.0.0", 96, 128, 65001, WITHDRAWAL},
    80  }
    81  
    82  func Test_RTRIPPrefix(t *testing.T) {
    83  	for i := range rtrIPPrefixTestCases {
    84  		test := &rtrIPPrefixTestCases[i]
    85  		addr := net.ParseIP(test.pString)
    86  		verifyRTRMessage(t, NewRTRIPPrefix(addr, test.pLen, test.mLen, test.asn, test.flags))
    87  	}
    88  }
    89  
    90  func Test_RTREndOfData(t *testing.T) {
    91  	id := uint16(time.Now().Unix())
    92  	sn := randUint32()
    93  	verifyRTRMessage(t, NewRTREndOfData(id, sn))
    94  }
    95  
    96  func Test_RTRCacheReset(t *testing.T) {
    97  	verifyRTRMessage(t, NewRTRCacheReset())
    98  }
    99  
   100  func Test_RTRErrorReport(t *testing.T) {
   101  	errPDU, _ := NewRTRResetQuery().Serialize()
   102  	errText1 := []byte("Couldn't send CacheResponce PDU")
   103  	errText2 := []byte("Wrong Length of PDU: 10 bytes")
   104  
   105  	// See 5.10 ErrorReport in RFC6810
   106  	// when it doesn't have both "erroneous PDU" and "Arbitrary Text"
   107  	verifyRTRMessage(t, NewRTRErrorReport(NO_DATA_AVAILABLE, nil, nil))
   108  
   109  	// when it has "erroneous PDU"
   110  	verifyRTRMessage(t, NewRTRErrorReport(UNSUPPORTED_PROTOCOL_VERSION, errPDU, nil))
   111  
   112  	// when it has "ArbitaryText"
   113  	verifyRTRMessage(t, NewRTRErrorReport(INTERNAL_ERROR, nil, errText1))
   114  
   115  	// when it has both "erroneous PDU" and "Arbitrary Text"
   116  	verifyRTRMessage(t, NewRTRErrorReport(CORRUPT_DATA, errPDU, errText2))
   117  }
   118  
   119  func FuzzParseRTR(f *testing.F) {
   120  
   121  	f.Fuzz(func(t *testing.T, data []byte) {
   122  		ParseRTR(data)
   123  	})
   124  }