diff --git a/protocols/isis/packet/BUILD.bazel b/protocols/isis/packet/BUILD.bazel index 51b35c52bfa922705c255ddcf13b41c039a46f9a..adf49290c1c6eac647759f16c83bb0013c550525 100644 --- a/protocols/isis/packet/BUILD.bazel +++ b/protocols/isis/packet/BUILD.bazel @@ -46,7 +46,13 @@ go_test( "lsp_test.go", "psnp_test.go", "tlv_area_addresses_test.go", + "tlv_checksum_test.go", + "tlv_dynamic_hostname_test.go", + "tlv_ip_interface_address_test.go", + "tlv_is_neighbors_test.go", + "tlv_p2p_adj_state_test.go", "tlv_padding_test.go", + "tlv_protocols_supported_test.go", "tlv_unknown_test.go", ], embed = [":go_default_library"], diff --git a/protocols/isis/packet/hello_test.go b/protocols/isis/packet/hello_test.go index a7b224356995896160fe1abd816303ab86f75025..661c489a236203be3d586d692541902af1486ceb 100644 --- a/protocols/isis/packet/hello_test.go +++ b/protocols/isis/packet/hello_test.go @@ -238,7 +238,7 @@ func TestDecodeISISHello(t *testing.T) { &ProtocolsSupportedTLV{ TLVType: 129, TLVLength: 2, - NerworkLayerProtocolIDs: []byte{0xcc, 0x8e}, + NetworkLayerProtocolIDs: []byte{0xcc, 0x8e}, }, &IPInterfaceAddressTLV{ TLVType: 132, diff --git a/protocols/isis/packet/isis_test.go b/protocols/isis/packet/isis_test.go index 21a9495c506dc991bb5b9650162b870940214520..4da6163a2ab758d49d5986e583caceef8ddd57ea 100644 --- a/protocols/isis/packet/isis_test.go +++ b/protocols/isis/packet/isis_test.go @@ -74,7 +74,7 @@ func TestDecode(t *testing.T) { &ProtocolsSupportedTLV{ TLVType: 129, TLVLength: 2, - NerworkLayerProtocolIDs: []uint8{0xcc, 0x8e}, + NetworkLayerProtocolIDs: []uint8{0xcc, 0x8e}, }, &IPInterfaceAddressTLV{ TLVType: 132, diff --git a/protocols/isis/packet/tlv.go b/protocols/isis/packet/tlv.go index 2b8becaf5f59bde7abbbdbbd448fcbd218697d02..6fcd8796e8cb5c9db845a91a3f1e3898567949e8 100644 --- a/protocols/isis/packet/tlv.go +++ b/protocols/isis/packet/tlv.go @@ -55,15 +55,15 @@ func readTLVs(buf *bytes.Buffer) ([]TLV, error) { case ChecksumTLVType: tlv, err = readChecksumTLV(buf, tlvType, tlvLength) case ProtocolsSupportedTLVType: - tlv, _, err = readProtocolsSupportedTLV(buf, tlvType, tlvLength) + tlv, err = readProtocolsSupportedTLV(buf, tlvType, tlvLength) case IPInterfaceAddressTLVType: - tlv, _, err = readIPInterfaceAddressTLV(buf, tlvType, tlvLength) + tlv, err = readIPInterfaceAddressTLV(buf, tlvType, tlvLength) case AreaAddressesTLVType: tlv, err = readAreaAddressesTLV(buf, tlvType, tlvLength) case P2PAdjacencyStateTLVType: - tlv, _, err = readP2PAdjacencyStateTLV(buf, tlvType, tlvLength) + tlv, err = readP2PAdjacencyStateTLV(buf, tlvType, tlvLength) case ISNeighborsTLVType: - tlv, _, err = readISNeighborsTLV(buf, tlvType, tlvLength) + tlv, err = readISNeighborsTLV(buf, tlvType, tlvLength) default: tlv, err = readUnknownTLV(buf, tlvType, tlvLength) } diff --git a/protocols/isis/packet/tlv_area_addresses_test.go b/protocols/isis/packet/tlv_area_addresses_test.go index f9965df3c1fe23c961a8192405a620a50e773f5b..72a93315732c9261750509e3aedaf62247e5586d 100644 --- a/protocols/isis/packet/tlv_area_addresses_test.go +++ b/protocols/isis/packet/tlv_area_addresses_test.go @@ -8,6 +8,58 @@ import ( "github.com/stretchr/testify/assert" ) +func TestReadAreaAddressesTLV(t *testing.T) { + tests := []struct { + name string + input []byte + wantFail bool + expected *AreaAddressesTLV + }{ + { + name: "Full", + input: []byte{ + 3, 1, 2, 3, + }, + wantFail: false, + expected: &AreaAddressesTLV{ + TLVType: 8, + TLVLength: 4, + AreaIDs: []types.AreaID{ + { + 1, 2, 3, + }, + }, + }, + }, + { + name: "Incomplete", + input: []byte{ + 1, 2, 3, + }, + wantFail: true, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(test.input) + tlv, err := readAreaAddressesTLV(buf, 8, uint8(len(test.input))) + if err != nil { + if test.wantFail { + continue + } + t.Errorf("Unexpected failure for test %q: %v", test.name, err) + continue + } + + if test.wantFail { + t.Errorf("Unexpected success for test %q", test.name) + continue + } + + assert.Equalf(t, test.expected, tlv, "Test %q", test.name) + } +} + func TestNewAreaAddressesTLV(t *testing.T) { tests := []struct { name string diff --git a/protocols/isis/packet/tlv_authentication.go b/protocols/isis/packet/tlv_authentication.go index a4c49aa134bdaef1b62fae67a158cb7f388bc4d1..14179399fc1af6805d91a48f8ac5874685ca1698 100644 --- a/protocols/isis/packet/tlv_authentication.go +++ b/protocols/isis/packet/tlv_authentication.go @@ -1,7 +1,9 @@ package packet +// AuthenticationType is the type value of an authentication TLV const AuthenticationType = 10 +// AuthenticationTLV represents an authentication TLV type AuthenticationTLV struct { TLVType uint8 TLVLength uint8 diff --git a/protocols/isis/packet/tlv_checksum.go b/protocols/isis/packet/tlv_checksum.go index f3ef8b85b8ec7182d3d545e646e99476fb31ed19..7adc5e8624b7cef19cf944e0657b4c72013d7705 100644 --- a/protocols/isis/packet/tlv_checksum.go +++ b/protocols/isis/packet/tlv_checksum.go @@ -8,6 +8,7 @@ import ( "github.com/taktv6/tflow2/convert" ) +// ChecksumTLVType is the type value of a checksum TLV const ChecksumTLVType = 12 // ChecksumTLV represents a checksum TLV @@ -18,18 +19,18 @@ type ChecksumTLV struct { } // Type gets the type of the TLV -func (c *ChecksumTLV) Type() uint8 { +func (c ChecksumTLV) Type() uint8 { return c.TLVType } // Length gets the length of the TLV -func (c *ChecksumTLV) Length() uint8 { +func (c ChecksumTLV) Length() uint8 { return c.TLVLength } -// Value returns the checksum -func (c *ChecksumTLV) Value() interface{} { - return c.Checksum +// Value returns the TLV itself +func (c ChecksumTLV) Value() interface{} { + return c } func readChecksumTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*ChecksumTLV, error) { diff --git a/protocols/isis/packet/tlv_checksum_test.go b/protocols/isis/packet/tlv_checksum_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5300a59e8258ecd3be08adfef19f90464ab1e603 --- /dev/null +++ b/protocols/isis/packet/tlv_checksum_test.go @@ -0,0 +1,95 @@ +package packet + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestChecksumTLV(t *testing.T) { + tlv := &ChecksumTLV{ + TLVType: 12, + TLVLength: 2, + Checksum: 123, + } + + assert.Equal(t, uint8(12), tlv.Type()) + assert.Equal(t, uint8(2), tlv.Length()) + assert.Equal(t, ChecksumTLV{ + TLVType: 12, + TLVLength: 2, + Checksum: 123, + }, tlv.Value()) +} + +func TestChecksumSerialize(t *testing.T) { + tests := []struct { + name string + input *ChecksumTLV + expected []byte + }{ + { + name: "A", + input: &ChecksumTLV{ + TLVType: 12, + TLVLength: 2, + Checksum: 123, + }, + expected: []byte{ + 12, 2, 0, 123, + }, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(nil) + test.input.Serialize(buf) + + assert.Equalf(t, test.expected, buf.Bytes(), "Test %q", test.name) + } +} + +func TestReadChecksumTLV(t *testing.T) { + tests := []struct { + name string + input []byte + wantFail bool + expected *ChecksumTLV + }{ + { + name: "Full", + input: []byte{0, 123}, + wantFail: false, + expected: &ChecksumTLV{ + TLVType: 8, + TLVLength: 2, + Checksum: 123, + }, + }, + { + name: "Incomplete", + input: []byte{0}, + wantFail: true, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(test.input) + tlv, err := readChecksumTLV(buf, 8, uint8(len(test.input))) + if err != nil { + if test.wantFail { + continue + } + t.Errorf("Unexpected failure for test %q: %v", test.name, err) + continue + } + + if test.wantFail { + t.Errorf("Unexpected success for test %q", test.name) + continue + } + + assert.Equalf(t, test.expected, tlv, "Test %q", test.name) + } +} diff --git a/protocols/isis/packet/tlv_dynamic_hostname.go b/protocols/isis/packet/tlv_dynamic_hostname.go index edc647e6a591198669464147876408615f397358..2b5114e948aef784d74bdf51642619d582722433 100644 --- a/protocols/isis/packet/tlv_dynamic_hostname.go +++ b/protocols/isis/packet/tlv_dynamic_hostname.go @@ -7,6 +7,7 @@ import ( "github.com/bio-routing/bio-rd/util/decode" ) +// DynamicHostNameTLVType is the type value of dynamic hostname TLV const DynamicHostNameTLVType = 137 // DynamicHostNameTLV represents a dynamic Hostname TLV @@ -26,9 +27,9 @@ func (d *DynamicHostNameTLV) Length() uint8 { return d.TLVLength } -// Value returns the dynamic hostname +// Value returns the TLV itself func (d *DynamicHostNameTLV) Value() interface{} { - return d.Hostname + return d } func readDynamicHostnameTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*DynamicHostNameTLV, error) { diff --git a/protocols/isis/packet/tlv_dynamic_hostname_test.go b/protocols/isis/packet/tlv_dynamic_hostname_test.go new file mode 100644 index 0000000000000000000000000000000000000000..cee8d43d08431e9a7b0d4e148eeebd167cd33129 --- /dev/null +++ b/protocols/isis/packet/tlv_dynamic_hostname_test.go @@ -0,0 +1,84 @@ +package packet + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDynamicHostnameTLVSerialize(t *testing.T) { + tests := []struct { + name string + input *DynamicHostNameTLV + expected []byte + }{ + { + name: "Full", + input: &DynamicHostNameTLV{ + TLVType: 137, + TLVLength: 5, + Hostname: []byte{1, 2, 3, 4, 5}, + }, + expected: []byte{137, 5, 1, 2, 3, 4, 5}, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(nil) + test.input.Serialize(buf) + + assert.Equalf(t, test.expected, buf.Bytes(), "Test %q", test.name) + } +} + +func TestReadDynamicHostnameTLV(t *testing.T) { + tests := []struct { + name string + input []byte + tlvLen uint8 + wantFail bool + expected *DynamicHostNameTLV + }{ + { + name: "Full", + input: []byte{ + 1, 2, 3, 4, 5, + }, + tlvLen: 5, + expected: &DynamicHostNameTLV{ + TLVType: 137, + TLVLength: 5, + Hostname: []byte{1, 2, 3, 4, 5}, + }, + }, + { + name: "Incomplete", + input: []byte{ + 1, 2, 3, 4, + }, + tlvLen: 5, + wantFail: true, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(test.input) + tlv, err := readDynamicHostnameTLV(buf, 137, test.tlvLen) + + if err != nil { + if test.wantFail { + continue + } + t.Errorf("Unexpected failure for test %q: %v", test.name, err) + continue + } + + if test.wantFail { + t.Errorf("Unexpected success for test %q", test.name) + continue + } + + assert.Equalf(t, test.expected, tlv, "Test %q", test.name) + } +} diff --git a/protocols/isis/packet/tlv_ip_interface_address.go b/protocols/isis/packet/tlv_ip_interface_address.go index c2d5f4573093c226396fef8369544dcc5ef65b26..012688a8b9e049513fa4398e1897b288a22fc5b4 100644 --- a/protocols/isis/packet/tlv_ip_interface_address.go +++ b/protocols/isis/packet/tlv_ip_interface_address.go @@ -18,17 +18,16 @@ type IPInterfaceAddressTLV struct { IPv4Address uint32 } -const ipv4AddressLength = 4 - -func NewIPInterfaceAddressTLV(addr uint32) IPInterfaceAddressTLV { - return IPInterfaceAddressTLV{ +// NewIPInterfaceAddressTLV creates a new IPInterfaceAddressTLV +func NewIPInterfaceAddressTLV(addr uint32) *IPInterfaceAddressTLV { + return &IPInterfaceAddressTLV{ TLVType: IPInterfaceAddressTLVType, TLVLength: 4, IPv4Address: addr, } } -func readIPInterfaceAddressTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*IPInterfaceAddressTLV, uint8, error) { +func readIPInterfaceAddressTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*IPInterfaceAddressTLV, error) { pdu := &IPInterfaceAddressTLV{ TLVType: tlvType, TLVLength: tlvLength, @@ -40,10 +39,10 @@ func readIPInterfaceAddressTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8 err := decode.Decode(buf, fields) if err != nil { - return nil, 0, fmt.Errorf("Unable to decode fields: %v", err) + return nil, fmt.Errorf("Unable to decode fields: %v", err) } - return pdu, ipv4AddressLength, nil + return pdu, nil } // Type returns the type of the TLV diff --git a/protocols/isis/packet/tlv_ip_interface_address_test.go b/protocols/isis/packet/tlv_ip_interface_address_test.go new file mode 100644 index 0000000000000000000000000000000000000000..432437ce0b943adec0947b3d247afdad411d15d2 --- /dev/null +++ b/protocols/isis/packet/tlv_ip_interface_address_test.go @@ -0,0 +1,82 @@ +package packet + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewIPInterfaceAddressTLV(t *testing.T) { + tests := []struct { + name string + addr uint32 + expected *IPInterfaceAddressTLV + }{ + { + name: "Test #1", + addr: 100, + expected: &IPInterfaceAddressTLV{ + TLVType: 132, + TLVLength: 4, + IPv4Address: 100, + }, + }, + } + + for _, test := range tests { + tlv := NewIPInterfaceAddressTLV(test.addr) + assert.Equalf(t, test.expected, tlv, "Test %q", test.name) + } +} + +func TestReadIPInterfaceAddressTLV(t *testing.T) { + tests := []struct { + name string + input []byte + tlvLength uint8 + wantFail bool + expected *IPInterfaceAddressTLV + }{ + { + name: "Full", + input: []byte{ + 0, 0, 0, 100, + }, + tlvLength: 4, + expected: &IPInterfaceAddressTLV{ + TLVType: 132, + TLVLength: 4, + IPv4Address: 100, + }, + }, + { + name: "Incomplete", + input: []byte{ + 0, 0, 0, + }, + tlvLength: 4, + wantFail: true, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(test.input) + tlv, err := readIPInterfaceAddressTLV(buf, 132, test.tlvLength) + + if err != nil { + if test.wantFail { + continue + } + t.Errorf("Unexpected failure for test %q: %v", test.name, err) + continue + } + + if test.wantFail { + t.Errorf("Unexpected success for test %q", test.name) + continue + } + + assert.Equalf(t, test.expected, tlv, "Test %q", test.name) + } +} diff --git a/protocols/isis/packet/tlv_is_neighbors.go b/protocols/isis/packet/tlv_is_neighbors.go index 96b260caaca5432c8f776c43f586d4f327a4d922..1296fee65da74f6dc3ca707521583bf83dee0253 100644 --- a/protocols/isis/packet/tlv_is_neighbors.go +++ b/protocols/isis/packet/tlv_is_neighbors.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" + "github.com/bio-routing/bio-rd/protocols/isis/types" "github.com/bio-routing/bio-rd/util/decode" ) @@ -14,13 +15,13 @@ const ISNeighborsTLVType = 6 type ISNeighborsTLV struct { TLVType uint8 TLVLength uint8 - NeighborSNPA [6]byte + NeighborSNPA types.SystemID } // ISNeighborsTLVLength is the length of an IS Neighbor TLV const ISNeighborsTLVLength = 8 -func readISNeighborsTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*ISNeighborsTLV, uint8, error) { +func readISNeighborsTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*ISNeighborsTLV, error) { pdu := &ISNeighborsTLV{ TLVType: tlvType, TLVLength: tlvLength, @@ -31,10 +32,10 @@ func readISNeighborsTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*ISN err := decode.Decode(buf, fields) if err != nil { - return nil, 0, fmt.Errorf("Unable to decode fields: %v", err) + return nil, fmt.Errorf("Unable to decode fields: %v", err) } - return pdu, ISNeighborsTLVLength, nil + return pdu, nil } // Type returns the type of the TLV @@ -47,8 +48,9 @@ func (i ISNeighborsTLV) Length() uint8 { return i.TLVLength } +// Value returns the TLV itself func (i ISNeighborsTLV) Value() interface{} { - return i.NeighborSNPA + return i } // Serialize serializes an WriteByte into a buffer diff --git a/protocols/isis/packet/tlv_is_neighbors_test.go b/protocols/isis/packet/tlv_is_neighbors_test.go new file mode 100644 index 0000000000000000000000000000000000000000..fa238877b2e5781c0a606d8530b4b3b1c5d9ad3a --- /dev/null +++ b/protocols/isis/packet/tlv_is_neighbors_test.go @@ -0,0 +1,92 @@ +package packet + +import ( + "bytes" + "testing" + + "github.com/bio-routing/bio-rd/protocols/isis/types" + "github.com/stretchr/testify/assert" +) + +func TestIsNeighborsTLV(t *testing.T) { + tlv := &ISNeighborsTLV{ + TLVType: 6, + TLVLength: 6, + NeighborSNPA: types.SystemID{1, 2, 3, 4, 5, 6}, + } + + assert.Equal(t, uint8(6), tlv.Type()) + assert.Equal(t, uint8(6), tlv.Length()) + assert.Equal(t, ISNeighborsTLV{ + TLVType: 6, + TLVLength: 6, + NeighborSNPA: types.SystemID{1, 2, 3, 4, 5, 6}, + }, tlv.Value()) +} + +func TestReadISNeighborsTLV(t *testing.T) { + tests := []struct { + name string + input []byte + tlvLength uint8 + wantFail bool + expected *ISNeighborsTLV + }{ + { + name: "Full", + input: []byte{1, 2, 3, 4, 5, 6}, + tlvLength: 6, + wantFail: false, + expected: &ISNeighborsTLV{ + TLVType: 6, + TLVLength: 6, + NeighborSNPA: types.SystemID{1, 2, 3, 4, 5, 6}, + }, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(test.input) + tlv, err := readISNeighborsTLV(buf, 6, test.tlvLength) + + if err != nil { + if test.wantFail { + continue + } + t.Errorf("Unexpected failure for test %q: %v", test.name, err) + continue + } + + if test.wantFail { + t.Errorf("Unexpected success for test %q", test.name) + continue + } + + assert.Equalf(t, test.expected, tlv, "Test %q", test.name) + } +} + +func TestISNeighborsTLVSerialize(t *testing.T) { + tests := []struct { + name string + input *ISNeighborsTLV + expected []byte + }{ + { + name: "Test #1", + input: &ISNeighborsTLV{ + TLVType: 6, + TLVLength: 6, + NeighborSNPA: types.SystemID{1, 2, 3, 4, 5, 6}, + }, + expected: []byte{6, 6, 1, 2, 3, 4, 5, 6}, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(nil) + test.input.Serialize(buf) + + assert.Equalf(t, test.expected, buf.Bytes(), "Test %q", test.name) + } +} diff --git a/protocols/isis/packet/tlv_is_reachability.go b/protocols/isis/packet/tlv_is_reachability.go index 25b4ec99b29786adb71e85f9e5793418db604067..7fa792d9c4e88380b47d33504f508bcb4c7e2b91 100644 --- a/protocols/isis/packet/tlv_is_reachability.go +++ b/protocols/isis/packet/tlv_is_reachability.go @@ -1,7 +1,9 @@ package packet +// ISReachabilityTLVType is the type value of an IS reachability TLV const ISReachabilityTLVType = 2 +// ISReachabilityTLV represents an IS reachability TLV type ISReachabilityTLV struct { TLVType uint8 TLVLength uint8 diff --git a/protocols/isis/packet/tlv_p2p_adj_state.go b/protocols/isis/packet/tlv_p2p_adj_state.go index 2e4ce589debcaca3a087e786432d140c5593fa3d..e1e9976923a6d46c1613d54d81afb3a1f4d2ef20 100644 --- a/protocols/isis/packet/tlv_p2p_adj_state.go +++ b/protocols/isis/packet/tlv_p2p_adj_state.go @@ -9,8 +9,10 @@ import ( "github.com/taktv6/tflow2/convert" ) +// P2PAdjacencyStateTLVType is the type value of an P2P adjacency state TLV const P2PAdjacencyStateTLVType = 240 +// P2PAdjacencyStateTLV represents an P2P adjacency state TLV type P2PAdjacencyStateTLV struct { TLVType uint8 TLVLength uint8 @@ -20,23 +22,20 @@ type P2PAdjacencyStateTLV struct { NeighborExtendedLocalCircuitID uint32 } -func readP2PAdjacencyStateTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*P2PAdjacencyStateTLV, uint8, error) { +func readP2PAdjacencyStateTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*P2PAdjacencyStateTLV, error) { pdu := &P2PAdjacencyStateTLV{ TLVType: tlvType, TLVLength: tlvLength, } - read := uint8(0) fields := make([]interface{}, 0) switch pdu.TLVLength { case 5: - read = 5 fields = []interface{}{ &pdu.AdjacencyState, &pdu.ExtendedLocalCircuitID, } case 15: - read = 15 fields = []interface{}{ &pdu.AdjacencyState, &pdu.ExtendedLocalCircuitID, @@ -47,14 +46,15 @@ func readP2PAdjacencyStateTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) err := decode.Decode(buf, fields) if err != nil { - return nil, 0, fmt.Errorf("Unable to decode fields: %v", err) + return nil, fmt.Errorf("Unable to decode fields: %v", err) } - return pdu, read, nil + return pdu, nil } -func NewP2PAdjacencyStateTLV(adjacencyState uint8, extendedLocalCircuitID uint32) P2PAdjacencyStateTLV { - return P2PAdjacencyStateTLV{ +// NewP2PAdjacencyStateTLV creates a new P2PAdjacencyStateTLV +func NewP2PAdjacencyStateTLV(adjacencyState uint8, extendedLocalCircuitID uint32) *P2PAdjacencyStateTLV { + return &P2PAdjacencyStateTLV{ TLVType: P2PAdjacencyStateTLVType, TLVLength: 5, AdjacencyState: adjacencyState, diff --git a/protocols/isis/packet/tlv_p2p_adj_state_test.go b/protocols/isis/packet/tlv_p2p_adj_state_test.go new file mode 100644 index 0000000000000000000000000000000000000000..609ab9b1d3da2b755c56178ef28e02969db8aeea --- /dev/null +++ b/protocols/isis/packet/tlv_p2p_adj_state_test.go @@ -0,0 +1,146 @@ +package packet + +import ( + "bytes" + "testing" + + "github.com/bio-routing/bio-rd/protocols/isis/types" + "github.com/stretchr/testify/assert" +) + +func TestReadP2PAdjacencyStateTLV(t *testing.T) { + tests := []struct { + name string + input []byte + tlvLength uint8 + wantFail bool + expected *P2PAdjacencyStateTLV + }{ + { + name: "Full", + input: []byte{ + 1, + 0, 0, 0, 100, + 1, 2, 3, 4, 5, 6, + 0, 0, 0, 200, + }, + tlvLength: 15, + wantFail: false, + expected: &P2PAdjacencyStateTLV{ + TLVType: 240, + TLVLength: 15, + AdjacencyState: 1, + ExtendedLocalCircuitID: 100, + NeighborSystemID: types.SystemID{1, 2, 3, 4, 5, 6}, + NeighborExtendedLocalCircuitID: 200, + }, + }, + { + name: "Incomplete", + input: []byte{ + 1, + 0, 0, 0, 100, + 1, 2, 3, 4, 5, 6, + 0, 0, + }, + tlvLength: 15, + wantFail: true, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(test.input) + tlv, err := readP2PAdjacencyStateTLV(buf, 240, test.tlvLength) + + if err != nil { + if test.wantFail { + continue + } + t.Errorf("Unexpected failure for test %q: %v", test.name, err) + continue + } + + if test.wantFail { + t.Errorf("Unexpected success for test %q", test.name) + continue + } + + assert.Equalf(t, test.expected, tlv, "Test %q", test.name) + } +} + +func TestNewP2PAdjacencyStateTLV(t *testing.T) { + tests := []struct { + name string + adjacencyState uint8 + extendedLocalCircuitID uint32 + expected *P2PAdjacencyStateTLV + }{ + { + name: "Test #1", + adjacencyState: 2, + extendedLocalCircuitID: 100, + expected: &P2PAdjacencyStateTLV{ + TLVType: 240, + TLVLength: 5, + AdjacencyState: 2, + ExtendedLocalCircuitID: 100, + }, + }, + } + + for _, test := range tests { + tlv := NewP2PAdjacencyStateTLV(test.adjacencyState, test.extendedLocalCircuitID) + + assert.Equalf(t, test.expected, tlv, "Test %q", test.name) + } +} + +func TestP2PAdjacencyStateTLV(t *testing.T) { + tlv := &P2PAdjacencyStateTLV{ + TLVType: 240, + TLVLength: 15, + } + + assert.Equal(t, uint8(240), tlv.Type()) + assert.Equal(t, uint8(15), tlv.Length()) + assert.Equal(t, P2PAdjacencyStateTLV{ + TLVType: 240, + TLVLength: 15, + }, tlv.Value()) +} + +func TestP2PAdjacencyStateTLVSerialize(t *testing.T) { + tests := []struct { + name string + input *P2PAdjacencyStateTLV + expected []byte + }{ + { + name: "Test #1", + input: &P2PAdjacencyStateTLV{ + TLVType: 240, + TLVLength: 15, + AdjacencyState: 1, + ExtendedLocalCircuitID: 100, + NeighborSystemID: types.SystemID{1, 2, 3, 4, 5, 6}, + NeighborExtendedLocalCircuitID: 200, + }, + expected: []byte{ + 240, + 15, + 1, + 0, 0, 0, 100, + 1, 2, 3, 4, 5, 6, + 0, 0, 0, 200, + }, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(nil) + test.input.Serialize(buf) + + assert.Equalf(t, test.expected, buf.Bytes(), "Test %q", test.name) + } +} diff --git a/protocols/isis/packet/tlv_protocols_supported.go b/protocols/isis/packet/tlv_protocols_supported.go index 20a30d12348362536f0bf0a0f7ea9bbba3afadae..a9597e155d539245a2c0e7fc158bb81311ac8c45 100644 --- a/protocols/isis/packet/tlv_protocols_supported.go +++ b/protocols/isis/packet/tlv_protocols_supported.go @@ -14,14 +14,14 @@ const ProtocolsSupportedTLVType = 129 type ProtocolsSupportedTLV struct { TLVType uint8 TLVLength uint8 - NerworkLayerProtocolIDs []uint8 + NetworkLayerProtocolIDs []uint8 } -func readProtocolsSupportedTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*ProtocolsSupportedTLV, uint8, error) { +func readProtocolsSupportedTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8) (*ProtocolsSupportedTLV, error) { pdu := &ProtocolsSupportedTLV{ TLVType: tlvType, TLVLength: tlvLength, - NerworkLayerProtocolIDs: make([]uint8, tlvLength), + NetworkLayerProtocolIDs: make([]uint8, tlvLength), } protoID := uint8(0) @@ -29,24 +29,22 @@ func readProtocolsSupportedTLV(buf *bytes.Buffer, tlvType uint8, tlvLength uint8 &protoID, } - read := uint8(2) for i := uint8(0); i < tlvLength; i++ { err := decode.Decode(buf, fields) if err != nil { - return nil, 0, fmt.Errorf("Unable to decode fields: %v", err) + return nil, fmt.Errorf("Unable to decode fields: %v", err) } - pdu.NerworkLayerProtocolIDs[i] = protoID - read++ + pdu.NetworkLayerProtocolIDs[i] = protoID } - return pdu, read, nil + return pdu, nil } func NewProtocolsSupportedTLV(protocols []uint8) ProtocolsSupportedTLV { return ProtocolsSupportedTLV{ TLVType: ProtocolsSupportedTLVType, TLVLength: uint8(len(protocols)), - NerworkLayerProtocolIDs: protocols, + NetworkLayerProtocolIDs: protocols, } } @@ -69,5 +67,5 @@ func (p ProtocolsSupportedTLV) Value() interface{} { func (p ProtocolsSupportedTLV) Serialize(buf *bytes.Buffer) { buf.WriteByte(p.TLVType) buf.WriteByte(p.TLVLength) - buf.Write(p.NerworkLayerProtocolIDs) + buf.Write(p.NetworkLayerProtocolIDs) } diff --git a/protocols/isis/packet/tlv_protocols_supported_test.go b/protocols/isis/packet/tlv_protocols_supported_test.go new file mode 100644 index 0000000000000000000000000000000000000000..d80192d3cbcb195ee1776a1080e90638698f5272 --- /dev/null +++ b/protocols/isis/packet/tlv_protocols_supported_test.go @@ -0,0 +1,90 @@ +package packet + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestProtocolsSupportedTLV(t *testing.T) { + tlv := &ProtocolsSupportedTLV{ + TLVType: 12, + TLVLength: 2, + NetworkLayerProtocolIDs: []uint8{100, 200}, + } + + assert.Equal(t, uint8(12), tlv.Type()) + assert.Equal(t, uint8(2), tlv.Length()) + assert.Equal(t, ProtocolsSupportedTLV{ + TLVType: 12, + TLVLength: 2, + NetworkLayerProtocolIDs: []uint8{100, 200}, + }, tlv.Value()) +} + +func TestProtocolsSupportedTLVSerialize(t *testing.T) { + tests := []struct { + name string + input *ProtocolsSupportedTLV + expected []byte + }{ + { + name: "A", + input: &ProtocolsSupportedTLV{ + TLVType: 129, + TLVLength: 2, + NetworkLayerProtocolIDs: []uint8{100, 200}, + }, + expected: []byte{ + 129, 2, 100, 200, + }, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(nil) + test.input.Serialize(buf) + + assert.Equalf(t, test.expected, buf.Bytes(), "Test %q", test.name) + } +} + +func TestReadProtocolsSupportedTLV(t *testing.T) { + tests := []struct { + name string + input []byte + wantFail bool + expected *ProtocolsSupportedTLV + }{ + { + name: "Full", + input: []byte{100, 200}, + wantFail: false, + expected: &ProtocolsSupportedTLV{ + TLVType: 129, + TLVLength: 2, + NetworkLayerProtocolIDs: []uint8{100, 200}, + }, + }, + } + + for _, test := range tests { + buf := bytes.NewBuffer(test.input) + tlv, err := readProtocolsSupportedTLV(buf, 129, uint8(len(test.input))) + if err != nil { + if test.wantFail { + continue + } + t.Errorf("Unexpected failure for test %q: %v", test.name, err) + continue + } + + if test.wantFail { + t.Errorf("Unexpected success for test %q", test.name) + continue + } + + assert.Equalf(t, test.expected, tlv, "Test %q", test.name) + } +}