| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- package wire
- import (
- "testing"
- "github.com/stretchr/testify/assert"
- )
- func TestTLVList_Append(t *testing.T) {
- want := TLVList{
- {
- Tag: 0,
- Value: []byte(`0`),
- },
- {
- Tag: 1,
- Value: []byte(`1`),
- },
- {
- Tag: 2,
- Value: []byte(`2`),
- },
- }
- have := TLVList{}
- have.Append(NewTLVBE(0, []byte(`0`)))
- have.Append(NewTLVBE(1, []byte(`1`)))
- have.Append(NewTLVBE(2, []byte(`2`)))
- assert.Equal(t, want, have)
- }
- func TestTLVList_HasTag(t *testing.T) {
- list := TLVList{
- {
- Tag: 0,
- Value: []byte(`0`),
- },
- {
- Tag: 1,
- Value: []byte(`1`),
- },
- {
- Tag: 2,
- Value: []byte(`2`),
- },
- }
- assert.True(t, list.HasTag(0))
- assert.False(t, list.HasTag(3))
- }
- func TestTLVList_AppendList(t *testing.T) {
- want := TLVList{
- {
- Tag: 0,
- Value: []byte(`0`),
- },
- {
- Tag: 1,
- Value: []byte(`1`),
- },
- {
- Tag: 2,
- Value: []byte(`2`),
- },
- }
- have := TLVList{}
- have.AppendList([]TLV{
- NewTLVBE(0, []byte(`0`)),
- NewTLVBE(1, []byte(`1`)),
- NewTLVBE(2, []byte(`2`)),
- })
- assert.Equal(t, want, have)
- }
- func TestTLVList_Getters(t *testing.T) {
- tests := []struct {
- name string
- given []TLV
- ttype any
- lookup func(TLVList) (any, bool)
- expect any
- found bool
- panic bool
- }{
- {
- name: "given a TLV of big-endian uint32, expect found value",
- given: []TLV{
- NewTLVBE(0, uint32(12)),
- NewTLVBE(1, uint32(34)),
- NewTLVBE(2, uint32(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint32BE(1)
- },
- expect: uint32(34),
- found: true,
- },
- {
- name: "given a TLV of big-endian uint32, expect not found value",
- given: []TLV{
- NewTLVBE(0, uint32(12)),
- NewTLVBE(1, uint32(34)),
- NewTLVBE(2, uint32(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint32BE(3)
- },
- expect: uint32(0),
- found: false,
- },
- {
- name: "given a TLV of big-endian uint16, expect found value",
- given: []TLV{
- NewTLVBE(0, uint16(12)),
- NewTLVBE(1, uint16(34)),
- NewTLVBE(2, uint16(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint16BE(1)
- },
- expect: uint16(34),
- found: true,
- },
- {
- name: "given a TLV of big-endian uint16, expect not found value",
- given: []TLV{
- NewTLVBE(0, uint16(12)),
- NewTLVBE(1, uint16(34)),
- NewTLVBE(2, uint16(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint16BE(3)
- },
- expect: uint16(0),
- found: false,
- },
- {
- name: "given a TLV of little-endian uint32, expect found value",
- given: []TLV{
- NewTLVLE(0, uint32(12)),
- NewTLVLE(1, uint32(34)),
- NewTLVLE(2, uint32(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint32LE(1)
- },
- expect: uint32(34),
- found: true,
- },
- {
- name: "given a TLV of little-endian uint32, expect not found value",
- given: []TLV{
- NewTLVLE(0, uint32(12)),
- NewTLVLE(1, uint32(34)),
- NewTLVLE(2, uint32(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint32LE(3)
- },
- expect: uint32(0),
- found: false,
- },
- {
- name: "given a TLV of little-endian uint16, expect found value",
- given: []TLV{
- NewTLVLE(0, uint16(12)),
- NewTLVLE(1, uint16(34)),
- NewTLVLE(2, uint16(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint16LE(1)
- },
- expect: uint16(34),
- found: true,
- },
- {
- name: "given a TLV of little-endian uint16, expect not found value",
- given: []TLV{
- NewTLVLE(0, uint16(12)),
- NewTLVLE(1, uint16(34)),
- NewTLVLE(2, uint16(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint16LE(3)
- },
- expect: uint16(0),
- found: false,
- },
- {
- name: "given a TLV of uint8, expect found value",
- given: []TLV{
- NewTLVLE(0, uint8(12)),
- NewTLVLE(1, uint8(34)),
- NewTLVLE(2, uint8(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint8(1)
- },
- expect: uint8(34),
- found: true,
- },
- {
- name: "given a TLV of uint8, expect not found value",
- given: []TLV{
- NewTLVLE(0, uint8(12)),
- NewTLVLE(1, uint8(34)),
- NewTLVLE(2, uint8(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint8(3)
- },
- expect: uint8(0),
- found: false,
- },
- {
- name: "given a TLV of string, expect found value",
- given: []TLV{
- NewTLVBE(0, "12"),
- NewTLVBE(1, "34"),
- NewTLVBE(2, "56"),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.String(1)
- },
- expect: "34",
- found: true,
- },
- {
- name: "given a TLV of string, expect not found value",
- given: []TLV{
- NewTLVBE(0, "12"),
- NewTLVBE(1, "34"),
- NewTLVBE(2, "56"),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.String(3)
- },
- expect: "",
- found: false,
- },
- {
- name: "given a TLV of slice, expect found value",
- given: []TLV{
- NewTLVBE(0, []byte(`12`)),
- NewTLVBE(1, []byte(`34`)),
- NewTLVBE(2, []byte(`56`)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Bytes(1)
- },
- expect: []byte(`34`),
- found: true,
- },
- {
- name: "given a TLV of string, expect not found value",
- given: []TLV{
- NewTLVBE(0, []byte(`12`)),
- NewTLVBE(1, []byte(`34`)),
- NewTLVBE(2, []byte(`56`)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Bytes(3)
- },
- expect: []byte(nil),
- found: false,
- },
- {
- name: "expect a panic when there's a type mismatch between big-endian uint16 and uint32",
- given: []TLV{
- NewTLVBE(0, uint16(12)),
- NewTLVBE(1, uint16(34)),
- NewTLVBE(2, uint16(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint32BE(1)
- },
- panic: true,
- },
- {
- name: "expect a panic when there's a type mismatch between little-endian uint16 and uint32",
- given: []TLV{
- NewTLVLE(0, uint16(12)),
- NewTLVLE(1, uint16(34)),
- NewTLVLE(2, uint16(56)),
- },
- lookup: func(l TLVList) (any, bool) {
- return l.Uint32LE(1)
- },
- panic: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if tt.panic {
- assert.Panics(t, func() { tt.lookup(tt.given) })
- return
- }
- have, found := tt.lookup(tt.given)
- assert.Equal(t, tt.expect, have)
- assert.Equal(t, tt.found, found)
- })
- }
- }
- func TestTLVList_NewTLVBEPanic(t *testing.T) {
- // make sure NewTLVBE panics when it encounters an unsupported type, in this
- // case it's int.
- assert.Panics(t, func() {
- NewTLVBE(1, 30)
- })
- }
- func TestTLVList_NewTLVLEPanic(t *testing.T) {
- // make sure NewTLVLE panics when it encounters an unsupported type, in this
- // case it's int.
- assert.Panics(t, func() {
- NewTLVLE(1, 30)
- })
- }
- func TestTLVList_ICQString(t *testing.T) {
- // Create a new TLV list
- tlv := TLVList{}
- // Add a valid ICQ string TLV entry to the list
- tlv.Append(NewTLVLE(0x01, []byte{0x09, 0x00, 'k', 'n', 'i', 't', 't', 'i', 'n', 'g', '\x00'}))
- t.Run("Valid ICQString", func(t *testing.T) {
- // Test retrieving a valid ICQ string
- str, ok := tlv.ICQString(0x01)
- assert.True(t, ok)
- assert.Equal(t, "knitting", str)
- })
- t.Run("Non-existent Tag", func(t *testing.T) {
- // Test retrieving an ICQ string for a non-existent tag
- str, ok := tlv.ICQString(0x02)
- assert.False(t, ok)
- assert.Empty(t, str)
- })
- t.Run("Malformed ICQString", func(t *testing.T) {
- // Add a malformed TLV entry (length prefix too short)
- tlvMalformed := TLVList{}
- tlvMalformed.Append(NewTLVLE(0x03, []byte{0x02, 0x00, 'a'})) // Length 2 but only 1 character and no null terminator
- str, ok := tlvMalformed.ICQString(0x03)
- assert.False(t, ok)
- assert.Empty(t, str)
- })
- t.Run("Incorrect Length Prefix", func(t *testing.T) {
- // Add an incorrect length prefix (does not match actual string length)
- tlvIncorrectLength := TLVList{}
- tlvIncorrectLength.Append(NewTLVLE(0x04, []byte{0x0A, 0x00, 'k', 'n', 'i', 't', 't', 'i', 'n', 'g', '\x00'})) // Length prefix is 9 but actual length is 7 + 1 (null terminator)
- str, ok := tlvIncorrectLength.ICQString(0x04)
- assert.False(t, ok)
- assert.Empty(t, str)
- })
- t.Run("Short Length Prefix", func(t *testing.T) {
- // Add a TLV with a length prefix, but the data is too short to contain a valid ICQ string
- tlvShortLength := TLVList{}
- tlvShortLength.Append(NewTLVLE(0x05, []byte{0x05, 0x00})) // Length prefix is 5 but no data
- str, ok := tlvShortLength.ICQString(0x05)
- assert.False(t, ok)
- assert.Empty(t, str)
- })
- t.Run("Empty String", func(t *testing.T) {
- // Add a TLV with an empty ICQ string (just the length prefix and null terminator)
- tlvEmptyString := TLVList{}
- tlvEmptyString.Append(NewTLVLE(0x06, []byte{0x01, 0x00, '\x00'})) // Length prefix is 1 with just null terminator
- str, ok := tlvEmptyString.ICQString(0x06)
- assert.True(t, ok)
- assert.Empty(t, str)
- })
- }
- func TestTLVList_Replace(t *testing.T) {
- tests := []struct {
- name string
- given TLVList
- want TLVList
- replacement TLV
- }{
- {
- name: "replace multiple TLVs",
- given: TLVList{
- NewTLVLE(0x01, []byte{0x01}),
- NewTLVLE(0x02, []byte{0x02}),
- NewTLVLE(0x01, []byte{0x03}),
- NewTLVLE(0x03, []byte{0x04}),
- },
- replacement: NewTLVLE(0x01, []byte{0xAA}),
- want: TLVList{
- NewTLVLE(0x01, []byte{0xAA}),
- NewTLVLE(0x02, []byte{0x02}),
- NewTLVLE(0x01, []byte{0xAA}),
- NewTLVLE(0x03, []byte{0x04}),
- },
- },
- {
- name: "no matching tags",
- given: TLVList{
- NewTLVLE(0x01, []byte{0x01}),
- NewTLVLE(0x02, []byte{0x02}),
- NewTLVLE(0x01, []byte{0x03}),
- NewTLVLE(0x03, []byte{0x04}),
- },
- replacement: NewTLVLE(0x07, []byte{0xAA}),
- want: TLVList{
- NewTLVLE(0x01, []byte{0x01}),
- NewTLVLE(0x02, []byte{0x02}),
- NewTLVLE(0x01, []byte{0x03}),
- NewTLVLE(0x03, []byte{0x04}),
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- tt.given.Replace(tt.replacement)
- assert.Equal(t, tt.want, tt.given)
- })
- }
- }
|