From 10ed7528c312e51562b45679472e5706d9b48f0f Mon Sep 17 00:00:00 2001 From: Oliver Herms <oliver.herms@exaring.de> Date: Sat, 6 Oct 2018 22:14:39 +0200 Subject: [PATCH] More tests --- protocols/isis/packet/tlv_padding.go | 17 +++-- protocols/isis/packet/tlv_padding_test.go | 86 +++++++++++++++++++++++ protocols/isis/packet/tlv_unknown.go | 2 +- protocols/isis/packet/tlv_unknown_test.go | 16 +++++ 4 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 protocols/isis/packet/tlv_padding_test.go diff --git a/protocols/isis/packet/tlv_padding.go b/protocols/isis/packet/tlv_padding.go index a5ff5d69..6e6bafb3 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 00000000..9aa8fc10 --- /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 b89b7f3b..9488ac1f 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 5b0f76dc..85bd5447 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()) +} -- GitLab