diff options
author | Eiichiro Watanabe <a16tochjp@gmail.com> | 2015-12-09 01:00:27 +0900 |
---|---|---|
committer | Eiichiro Watanabe <a16tochjp@gmail.com> | 2015-12-09 12:30:49 +0900 |
commit | d0455ea10dc0670d948f68645dc7e6eb305cf14a (patch) | |
tree | 5ddbc0563c12bd6c4ddd2a2af72c2254c99fe903 | |
parent | 1ebbcee68b4850dfa46548cfa7facb0fb761a0e3 (diff) |
packet: add test examples for rtr
-rw-r--r-- | packet/rtr_test.go | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/packet/rtr_test.go b/packet/rtr_test.go new file mode 100644 index 00000000..3451b862 --- /dev/null +++ b/packet/rtr_test.go @@ -0,0 +1,108 @@ +// Copyright (C) 2015 Nippon Telegraph and Telephone Corporation. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +// implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package bgp + +import ( + "encoding/hex" + "math/rand" + "net" + "reflect" + "testing" + "time" +) + +func verifyRTRMessage(t *testing.T, m1 RTRMessage) { + buf1, _ := m1.Serialize() + m2, err := ParseRTR(buf1) + if err != nil { + t.Error(err) + } + buf2, _ := m2.Serialize() + + if reflect.DeepEqual(buf1, buf2) == true { + t.Log("OK") + } else { + t.Errorf("Something wrong") + t.Error(len(buf1), m1, hex.EncodeToString(buf1)) + t.Error(len(buf2), m2, hex.EncodeToString(buf2)) + } +} + +func randUint32() uint32 { + rand.Seed(time.Now().UnixNano()) + return rand.Uint32() +} + +func Test_RTRSerialNotify(t *testing.T) { + id := uint16(time.Now().Unix()) + sn := randUint32() + verifyRTRMessage(t, NewRTRSerialNotify(id, sn)) +} + +func Test_RTRSerialQuery(t *testing.T) { + id := uint16(time.Now().Unix()) + sn := randUint32() + verifyRTRMessage(t, NewRTRSerialQuery(id, sn)) +} + +func Test_RTRResetQuery(t *testing.T) { + verifyRTRMessage(t, NewRTRResetQuery()) +} + +func Test_RTRCacheResponse(t *testing.T) { + id := uint16(time.Now().Unix()) + verifyRTRMessage(t, NewRTRCacheResponse(id)) +} + +type rtrIPPrefixTestCase struct { + pString string + pLen uint8 + mLen uint8 + asn uint32 + flags uint8 +} + +var rtrIPPrefixTestCases = []rtrIPPrefixTestCase{ + {"192.168.0.0", 16, 32, 65001, ANNOUNCEMENT}, + {"192.168.0.0", 16, 32, 65001, WITHDRAWAL}, + {"2001:db8::", 32, 128, 65001, ANNOUNCEMENT}, + {"2001:db8::", 32, 128, 65001, WITHDRAWAL}, + {"::ffff:0.0.0.0", 96, 128, 65001, ANNOUNCEMENT}, + {"::ffff:0.0.0.0", 96, 128, 65001, WITHDRAWAL}, +} + +func Test_RTRIPPrefix(t *testing.T) { + for i := range rtrIPPrefixTestCases { + test := &rtrIPPrefixTestCases[i] + addr := net.ParseIP(test.pString) + verifyRTRMessage(t, NewRTRIPPrefix(addr, test.pLen, test.mLen, test.asn, test.flags)) + } +} + +func Test_RTREndOfData(t *testing.T) { + id := uint16(time.Now().Unix()) + sn := randUint32() + verifyRTRMessage(t, NewRTREndOfData(id, sn)) +} + +func Test_RTRCacheReset(t *testing.T) { + verifyRTRMessage(t, NewRTRCacheReset()) +} + +//TO BE IMPLEMENTED: +//func Test_RTRErrorReport(t *testing.T) { +// verifyRTRMessage(t, NewRTRErrorReport()) +//} |