tlv_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package wire
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestTLVList_Append(t *testing.T) {
  7. want := TLVList{
  8. {
  9. Tag: 0,
  10. Value: []byte(`0`),
  11. },
  12. {
  13. Tag: 1,
  14. Value: []byte(`1`),
  15. },
  16. {
  17. Tag: 2,
  18. Value: []byte(`2`),
  19. },
  20. }
  21. have := TLVList{}
  22. have.Append(NewTLV(0, []byte(`0`)))
  23. have.Append(NewTLV(1, []byte(`1`)))
  24. have.Append(NewTLV(2, []byte(`2`)))
  25. assert.Equal(t, want, have)
  26. }
  27. func TestTLVList_AppendList(t *testing.T) {
  28. want := TLVList{
  29. {
  30. Tag: 0,
  31. Value: []byte(`0`),
  32. },
  33. {
  34. Tag: 1,
  35. Value: []byte(`1`),
  36. },
  37. {
  38. Tag: 2,
  39. Value: []byte(`2`),
  40. },
  41. }
  42. have := TLVList{}
  43. have.AppendList([]TLV{
  44. NewTLV(0, []byte(`0`)),
  45. NewTLV(1, []byte(`1`)),
  46. NewTLV(2, []byte(`2`)),
  47. })
  48. assert.Equal(t, want, have)
  49. }
  50. func TestTLVList_Getters(t *testing.T) {
  51. type args struct {
  52. tType uint16
  53. }
  54. tests := []struct {
  55. name string
  56. given []TLV
  57. ttype any
  58. lookup func(TLVList) (any, bool)
  59. expect any
  60. found bool
  61. panic bool
  62. }{
  63. {
  64. name: "given a TLV of uint32, expect found value",
  65. given: []TLV{
  66. NewTLV(0, uint32(12)),
  67. NewTLV(1, uint32(34)),
  68. NewTLV(2, uint32(56)),
  69. },
  70. lookup: func(l TLVList) (any, bool) {
  71. return l.Uint32(1)
  72. },
  73. expect: uint32(34),
  74. found: true,
  75. },
  76. {
  77. name: "given a TLV of uint32, expect not found value",
  78. given: []TLV{
  79. NewTLV(0, uint32(12)),
  80. NewTLV(1, uint32(34)),
  81. NewTLV(2, uint32(56)),
  82. },
  83. lookup: func(l TLVList) (any, bool) {
  84. return l.Uint32(3)
  85. },
  86. expect: uint32(0),
  87. found: false,
  88. },
  89. {
  90. name: "given a TLV of uint16, expect found value",
  91. given: []TLV{
  92. NewTLV(0, uint16(12)),
  93. NewTLV(1, uint16(34)),
  94. NewTLV(2, uint16(56)),
  95. },
  96. lookup: func(l TLVList) (any, bool) {
  97. return l.Uint16(1)
  98. },
  99. expect: uint16(34),
  100. found: true,
  101. },
  102. {
  103. name: "given a TLV of uint16, expect not found value",
  104. given: []TLV{
  105. NewTLV(0, uint16(12)),
  106. NewTLV(1, uint16(34)),
  107. NewTLV(2, uint16(56)),
  108. },
  109. lookup: func(l TLVList) (any, bool) {
  110. return l.Uint16(3)
  111. },
  112. expect: uint16(0),
  113. found: false,
  114. },
  115. {
  116. name: "given a TLV of string, expect found value",
  117. given: []TLV{
  118. NewTLV(0, "12"),
  119. NewTLV(1, "34"),
  120. NewTLV(2, "56"),
  121. },
  122. lookup: func(l TLVList) (any, bool) {
  123. return l.String(1)
  124. },
  125. expect: "34",
  126. found: true,
  127. },
  128. {
  129. name: "given a TLV of string, expect not found value",
  130. given: []TLV{
  131. NewTLV(0, "12"),
  132. NewTLV(1, "34"),
  133. NewTLV(2, "56"),
  134. },
  135. lookup: func(l TLVList) (any, bool) {
  136. return l.String(3)
  137. },
  138. expect: "",
  139. found: false,
  140. },
  141. {
  142. name: "given a TLV of slice, expect found value",
  143. given: []TLV{
  144. NewTLV(0, []byte(`12`)),
  145. NewTLV(1, []byte(`34`)),
  146. NewTLV(2, []byte(`56`)),
  147. },
  148. lookup: func(l TLVList) (any, bool) {
  149. return l.Slice(1)
  150. },
  151. expect: []byte(`34`),
  152. found: true,
  153. },
  154. {
  155. name: "given a TLV of string, expect not found value",
  156. given: []TLV{
  157. NewTLV(0, []byte(`12`)),
  158. NewTLV(1, []byte(`34`)),
  159. NewTLV(2, []byte(`56`)),
  160. },
  161. lookup: func(l TLVList) (any, bool) {
  162. return l.Slice(3)
  163. },
  164. expect: []byte(nil),
  165. found: false,
  166. },
  167. {
  168. name: "expect a panic when there's a type mismatch",
  169. given: []TLV{
  170. NewTLV(0, uint16(12)),
  171. NewTLV(1, uint16(34)),
  172. NewTLV(2, uint16(56)),
  173. },
  174. lookup: func(l TLVList) (any, bool) {
  175. return l.Uint32(1)
  176. },
  177. panic: true,
  178. },
  179. }
  180. for _, tt := range tests {
  181. t.Run(tt.name, func(t *testing.T) {
  182. if tt.panic {
  183. assert.Panics(t, func() { tt.lookup(tt.given) })
  184. return
  185. }
  186. have, found := tt.lookup(tt.given)
  187. assert.Equal(t, tt.expect, have)
  188. assert.Equal(t, tt.found, found)
  189. })
  190. }
  191. }
  192. func TestTLVList_NewTLVPanic(t *testing.T) {
  193. // make sure NewTLV panics when it encounters an unsupported type, in this
  194. // case it's int.
  195. assert.Panics(t, func() {
  196. NewTLV(1, 30)
  197. })
  198. }