Skip to content
Snippets Groups Projects
Commit 10ed7528 authored by Oliver Herms's avatar Oliver Herms
Browse files

More tests

parent 6d354065
No related branches found
No related tags found
No related merge requests found
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
}
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)
}
}
......@@ -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)
......
......@@ -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())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment