diff --git a/protocols/isis/packet/tlv_padding.go b/protocols/isis/packet/tlv_padding.go index a5ff5d698b2b6bf8fb2d8cf203f0d88c5d2e5c42..6e6bafb33b3f8ce33e24f502cb2819c3c0d0a793 100644 --- a/protocols/isis/packet/tlv_padding.go +++ b/protocols/isis/packet/tlv_padding.go @@ -1,39 +1,46 @@ package packet -import( +import ( "bytes" ) +// PaddingType is the type value of a padding TLV const PaddingType = 8 +// PaddingTLV represents a padding TLV type PaddingTLV struct { TLVType uint8 TLVLength uint8 PaddingData []byte } +// NewPaddingTLV creates a new padding TLV func NewPaddingTLV(length uint8) *PaddingTLV { return &PaddingTLV{ - TLVType: PaddingType, - TLVLength: length, + TLVType: PaddingType, + TLVLength: length, PaddingData: make([]byte, length), } } +// Type gets the type of the TLV func (p *PaddingTLV) Type() uint8 { return p.TLVType } +// Length gets the length of the TLV func (p *PaddingTLV) Length() uint8 { return p.TLVLength } +// Value gets the TLV itself func (p *PaddingTLV) Value() interface{} { - return p.PaddingData + return p } +// Serialize serializes a padding TLV func (p *PaddingTLV) Serialize(buf *bytes.Buffer) { buf.WriteByte(p.TLVType) buf.WriteByte(p.TLVLength) buf.Write(p.PaddingData) -} \ No newline at end of file +} diff --git a/protocols/isis/packet/tlv_padding_test.go b/protocols/isis/packet/tlv_padding_test.go new file mode 100644 index 0000000000000000000000000000000000000000..9aa8fc10bbd5ce543c021eca914d8b3c1aeaa2bc --- /dev/null +++ b/protocols/isis/packet/tlv_padding_test.go @@ -0,0 +1,86 @@ +package packet + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewPaddingTLV(t *testing.T) { + tests := []struct { + name string + input uint8 + expected *PaddingTLV + }{ + { + name: "A", + input: 2, + expected: &PaddingTLV{ + TLVType: 8, + TLVLength: 2, + PaddingData: []byte{0, 0}, + }, + }, + { + name: "B", + input: 4, + expected: &PaddingTLV{ + TLVType: 8, + TLVLength: 4, + PaddingData: []byte{0, 0, 0, 0}, + }, + }, + } + + for _, test := range tests { + tlv := NewPaddingTLV(test.input) + assert.Equalf(t, test.expected, tlv, "Test %q", test.name) + } +} + +func TestPaddingTLV(t *testing.T) { + tlv := NewPaddingTLV(2) + + assert.Equal(t, uint8(8), tlv.Type()) + assert.Equal(t, uint8(2), tlv.Length()) + assert.Equal(t, &PaddingTLV{ + TLVType: 8, + TLVLength: 2, + PaddingData: []byte{0, 0}, + }, tlv.Value()) +} + +func TestPaddingTLVSerialize(t *testing.T) { + tests := []struct { + name string + input *PaddingTLV + expected []byte + }{ + { + name: "Full", + input: &PaddingTLV{ + TLVType: 8, + TLVLength: 2, + PaddingData: []byte{0, 0}, + }, + expected: []byte{8, 2, 0, 0}, + }, + { + name: "Full", + input: &PaddingTLV{ + TLVType: 8, + TLVLength: 3, + PaddingData: []byte{0, 0, 0}, + }, + expected: []byte{8, 3, 0, 0, 0}, + }, + } + + 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_unknown.go b/protocols/isis/packet/tlv_unknown.go index b89b7f3bae13cab6727ed3ac1ec98cf9e713d1f2..9488ac1ff700e6e15ee8c06256b4b16af206f2a9 100644 --- a/protocols/isis/packet/tlv_unknown.go +++ b/protocols/isis/packet/tlv_unknown.go @@ -46,7 +46,7 @@ func (u *UnknownTLV) Value() interface{} { return u } -// Serialize serializes a protocols supported TLV +// Serialize serializes an unknown TLV func (u UnknownTLV) Serialize(buf *bytes.Buffer) { buf.WriteByte(u.TLVType) buf.WriteByte(u.TLVLength) diff --git a/protocols/isis/packet/tlv_unknown_test.go b/protocols/isis/packet/tlv_unknown_test.go index 5b0f76dca6c0654cc243b9d71cbd141a75ad870c..85bd5447434a245bf75f856c82204dc52d802446 100644 --- a/protocols/isis/packet/tlv_unknown_test.go +++ b/protocols/isis/packet/tlv_unknown_test.go @@ -83,3 +83,19 @@ func TestUnknownTLVSerialize(t *testing.T) { assert.Equalf(t, test.expected, buf.Bytes(), "Test %q", test.name) } } + +func TestUnknownTLV(t *testing.T) { + tlv := &UnknownTLV{ + TLVType: 100, + TLVLength: 1, + TLVValue: []byte{1}, + } + + assert.Equal(t, uint8(100), tlv.Type()) + assert.Equal(t, uint8(1), tlv.Length()) + assert.Equal(t, &UnknownTLV{ + TLVType: 100, + TLVLength: 1, + TLVValue: []byte{1}, + }, tlv.Value()) +}