github.com/osrg/gobgp@v2.0.0+incompatible/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 rand.Seed(time.Now().UnixNano()) 42 return rand.Uint32() 43 } 44 45 func Test_RTRSerialNotify(t *testing.T) { 46 id := uint16(time.Now().Unix()) 47 sn := randUint32() 48 verifyRTRMessage(t, NewRTRSerialNotify(id, sn)) 49 } 50 51 func Test_RTRSerialQuery(t *testing.T) { 52 id := uint16(time.Now().Unix()) 53 sn := randUint32() 54 verifyRTRMessage(t, NewRTRSerialQuery(id, sn)) 55 } 56 57 func Test_RTRResetQuery(t *testing.T) { 58 verifyRTRMessage(t, NewRTRResetQuery()) 59 } 60 61 func Test_RTRCacheResponse(t *testing.T) { 62 id := uint16(time.Now().Unix()) 63 verifyRTRMessage(t, NewRTRCacheResponse(id)) 64 } 65 66 type rtrIPPrefixTestCase struct { 67 pString string 68 pLen uint8 69 mLen uint8 70 asn uint32 71 flags uint8 72 } 73 74 var rtrIPPrefixTestCases = []rtrIPPrefixTestCase{ 75 {"192.168.0.0", 16, 32, 65001, ANNOUNCEMENT}, 76 {"192.168.0.0", 16, 32, 65001, WITHDRAWAL}, 77 {"2001:db8::", 32, 128, 65001, ANNOUNCEMENT}, 78 {"2001:db8::", 32, 128, 65001, WITHDRAWAL}, 79 {"::ffff:0.0.0.0", 96, 128, 65001, ANNOUNCEMENT}, 80 {"::ffff:0.0.0.0", 96, 128, 65001, WITHDRAWAL}, 81 } 82 83 func Test_RTRIPPrefix(t *testing.T) { 84 for i := range rtrIPPrefixTestCases { 85 test := &rtrIPPrefixTestCases[i] 86 addr := net.ParseIP(test.pString) 87 verifyRTRMessage(t, NewRTRIPPrefix(addr, test.pLen, test.mLen, test.asn, test.flags)) 88 } 89 } 90 91 func Test_RTREndOfData(t *testing.T) { 92 id := uint16(time.Now().Unix()) 93 sn := randUint32() 94 verifyRTRMessage(t, NewRTREndOfData(id, sn)) 95 } 96 97 func Test_RTRCacheReset(t *testing.T) { 98 verifyRTRMessage(t, NewRTRCacheReset()) 99 } 100 101 func Test_RTRErrorReport(t *testing.T) { 102 errPDU, _ := NewRTRResetQuery().Serialize() 103 errText1 := []byte("Couldn't send CacheResponce PDU") 104 errText2 := []byte("Wrong Length of PDU: 10 bytes") 105 106 // See 5.10 ErrorReport in RFC6810 107 // when it doesn't have both "erroneous PDU" and "Arbitrary Text" 108 verifyRTRMessage(t, NewRTRErrorReport(NO_DATA_AVAILABLE, nil, nil)) 109 110 // when it has "erroneous PDU" 111 verifyRTRMessage(t, NewRTRErrorReport(UNSUPPORTED_PROTOCOL_VERSION, errPDU, nil)) 112 113 // when it has "ArbitaryText" 114 verifyRTRMessage(t, NewRTRErrorReport(INTERNAL_ERROR, nil, errText1)) 115 116 // when it has both "erroneous PDU" and "Arbitrary Text" 117 verifyRTRMessage(t, NewRTRErrorReport(CORRUPT_DATA, errPDU, errText2)) 118 }