github.com/decred/dcrlnd@v0.7.6/tlv/truncated_test.go (about) 1 package tlv_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "testing" 7 8 "github.com/decred/dcrlnd/tlv" 9 ) 10 11 var tuint16Tests = []struct { 12 value uint16 13 size uint64 14 bytes []byte 15 }{ 16 { 17 value: 0x0000, 18 size: 0, 19 bytes: []byte{}, 20 }, 21 { 22 value: 0x0001, 23 size: 1, 24 bytes: []byte{0x01}, 25 }, 26 { 27 value: 0x00ff, 28 size: 1, 29 bytes: []byte{0xff}, 30 }, 31 { 32 value: 0x0100, 33 size: 2, 34 bytes: []byte{0x01, 0x00}, 35 }, 36 { 37 value: 0xffff, 38 size: 2, 39 bytes: []byte{0xff, 0xff}, 40 }, 41 } 42 43 // TestSizeTUint16 asserts that SizeTUint16 computes the proper truncated size 44 // along boundary conditions of the input space. 45 func TestSizeTUint16(t *testing.T) { 46 for _, test := range tuint16Tests { 47 name := fmt.Sprintf("0x%x", test.value) 48 t.Run(name, func(t *testing.T) { 49 size := tlv.SizeTUint16(test.value) 50 if test.size != size { 51 t.Fatalf("size mismatch, expected: %d got: %d", 52 test.size, size) 53 } 54 }) 55 } 56 } 57 58 // TestTUint16 asserts that ETUint16 outputs the proper encoding of a truncated 59 // uint16, and that DTUint16 is able to parse the output. 60 func TestTUint16(t *testing.T) { 61 var buf [8]byte 62 for _, test := range tuint16Tests { 63 test := test 64 65 if len(test.bytes) != int(test.size) { 66 t.Fatalf("invalid test case, "+ 67 "len(bytes)[%d] != size[%d]", 68 len(test.bytes), test.size) 69 } 70 71 name := fmt.Sprintf("0x%x", test.value) 72 t.Run(name, func(t *testing.T) { 73 // Test generic encoder. 74 var b bytes.Buffer 75 err := tlv.ETUint16(&b, &test.value, &buf) 76 if err != nil { 77 t.Fatalf("unable to encode tuint16: %v", err) 78 } 79 80 if !bytes.Equal(b.Bytes(), test.bytes) { 81 t.Fatalf("encoding mismatch, "+ 82 "expected: %x, got: %x", 83 test.bytes, b.Bytes()) 84 } 85 86 // Test non-generic encoder. 87 var b2 bytes.Buffer 88 err = tlv.ETUint16T(&b2, test.value, &buf) 89 if err != nil { 90 t.Fatalf("unable to encode tuint16: %v", err) 91 } 92 93 if !bytes.Equal(b2.Bytes(), test.bytes) { 94 t.Fatalf("encoding mismatch, "+ 95 "expected: %x, got: %x", 96 test.bytes, b2.Bytes()) 97 } 98 99 var value uint16 100 r := bytes.NewReader(b.Bytes()) 101 err = tlv.DTUint16(r, &value, &buf, test.size) 102 if err != nil { 103 t.Fatalf("unable to decode tuint16: %v", err) 104 } 105 106 if value != test.value { 107 t.Fatalf("decoded value mismatch, "+ 108 "expected: %d, got: %d", 109 test.value, value) 110 } 111 }) 112 } 113 } 114 115 var tuint32Tests = []struct { 116 value uint32 117 size uint64 118 bytes []byte 119 }{ 120 { 121 value: 0x00000000, 122 size: 0, 123 bytes: []byte{}, 124 }, 125 { 126 value: 0x00000001, 127 size: 1, 128 bytes: []byte{0x01}, 129 }, 130 { 131 value: 0x000000ff, 132 size: 1, 133 bytes: []byte{0xff}, 134 }, 135 { 136 value: 0x00000100, 137 size: 2, 138 bytes: []byte{0x01, 0x00}, 139 }, 140 { 141 value: 0x0000ffff, 142 size: 2, 143 bytes: []byte{0xff, 0xff}, 144 }, 145 { 146 value: 0x00010000, 147 size: 3, 148 bytes: []byte{0x01, 0x00, 0x00}, 149 }, 150 { 151 value: 0x00ffffff, 152 size: 3, 153 bytes: []byte{0xff, 0xff, 0xff}, 154 }, 155 { 156 value: 0x01000000, 157 size: 4, 158 bytes: []byte{0x01, 0x00, 0x00, 0x00}, 159 }, 160 { 161 value: 0xffffffff, 162 size: 4, 163 bytes: []byte{0xff, 0xff, 0xff, 0xff}, 164 }, 165 } 166 167 // TestSizeTUint32 asserts that SizeTUint32 computes the proper truncated size 168 // along boundary conditions of the input space. 169 func TestSizeTUint32(t *testing.T) { 170 for _, test := range tuint32Tests { 171 name := fmt.Sprintf("0x%x", test.value) 172 t.Run(name, func(t *testing.T) { 173 size := tlv.SizeTUint32(test.value) 174 if test.size != size { 175 t.Fatalf("size mismatch, expected: %d got: %d", 176 test.size, size) 177 } 178 }) 179 } 180 } 181 182 // TestTUint32 asserts that ETUint32 outputs the proper encoding of a truncated 183 // uint32, and that DTUint32 is able to parse the output. 184 func TestTUint32(t *testing.T) { 185 var buf [8]byte 186 for _, test := range tuint32Tests { 187 test := test 188 189 if len(test.bytes) != int(test.size) { 190 t.Fatalf("invalid test case, "+ 191 "len(bytes)[%d] != size[%d]", 192 len(test.bytes), test.size) 193 } 194 195 name := fmt.Sprintf("0x%x", test.value) 196 t.Run(name, func(t *testing.T) { 197 // Test generic encoder. 198 var b bytes.Buffer 199 err := tlv.ETUint32(&b, &test.value, &buf) 200 if err != nil { 201 t.Fatalf("unable to encode tuint32: %v", err) 202 } 203 204 if !bytes.Equal(b.Bytes(), test.bytes) { 205 t.Fatalf("encoding mismatch, "+ 206 "expected: %x, got: %x", 207 test.bytes, b.Bytes()) 208 } 209 210 // Test non-generic encoder. 211 var b2 bytes.Buffer 212 err = tlv.ETUint32T(&b2, test.value, &buf) 213 if err != nil { 214 t.Fatalf("unable to encode tuint32: %v", err) 215 } 216 217 if !bytes.Equal(b2.Bytes(), test.bytes) { 218 t.Fatalf("encoding mismatch, "+ 219 "expected: %x, got: %x", 220 test.bytes, b2.Bytes()) 221 } 222 223 var value uint32 224 r := bytes.NewReader(b.Bytes()) 225 err = tlv.DTUint32(r, &value, &buf, test.size) 226 if err != nil { 227 t.Fatalf("unable to decode tuint32: %v", err) 228 } 229 230 if value != test.value { 231 t.Fatalf("decoded value mismatch, "+ 232 "expected: %d, got: %d", 233 test.value, value) 234 } 235 }) 236 } 237 } 238 239 var tuint64Tests = []struct { 240 value uint64 241 size uint64 242 bytes []byte 243 }{ 244 { 245 value: 0x0000000000000000, 246 size: 0, 247 bytes: []byte{}, 248 }, 249 { 250 value: 0x0000000000000001, 251 size: 1, 252 bytes: []byte{0x01}, 253 }, 254 { 255 value: 0x00000000000000ff, 256 size: 1, 257 bytes: []byte{0xff}, 258 }, 259 { 260 value: 0x0000000000000100, 261 size: 2, 262 bytes: []byte{0x01, 0x00}, 263 }, 264 { 265 value: 0x000000000000ffff, 266 size: 2, 267 bytes: []byte{0xff, 0xff}, 268 }, 269 { 270 value: 0x0000000000010000, 271 size: 3, 272 bytes: []byte{0x01, 0x00, 0x00}, 273 }, 274 { 275 value: 0x0000000000ffffff, 276 size: 3, 277 bytes: []byte{0xff, 0xff, 0xff}, 278 }, 279 { 280 value: 0x0000000001000000, 281 size: 4, 282 bytes: []byte{0x01, 0x00, 0x00, 0x00}, 283 }, 284 { 285 value: 0x00000000ffffffff, 286 size: 4, 287 bytes: []byte{0xff, 0xff, 0xff, 0xff}, 288 }, 289 { 290 value: 0x0000000100000000, 291 size: 5, 292 bytes: []byte{0x01, 0x00, 0x00, 0x00, 0x00}, 293 }, 294 { 295 value: 0x000000ffffffffff, 296 size: 5, 297 bytes: []byte{0xff, 0xff, 0xff, 0xff, 0xff}, 298 }, 299 { 300 value: 0x0000010000000000, 301 size: 6, 302 bytes: []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00}, 303 }, 304 { 305 value: 0x0000ffffffffffff, 306 size: 6, 307 bytes: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 308 }, 309 { 310 value: 0x0001000000000000, 311 size: 7, 312 bytes: []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 313 }, 314 { 315 value: 0x00ffffffffffffff, 316 size: 7, 317 bytes: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 318 }, 319 { 320 value: 0x0100000000000000, 321 size: 8, 322 bytes: []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 323 }, 324 { 325 value: 0xffffffffffffffff, 326 size: 8, 327 bytes: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 328 }, 329 } 330 331 // TestSizeTUint64 asserts that SizeTUint64 computes the proper truncated size 332 // along boundary conditions of the input space. 333 func TestSizeTUint64(t *testing.T) { 334 for _, test := range tuint64Tests { 335 if len(test.bytes) != int(test.size) { 336 t.Fatalf("invalid test case, "+ 337 "len(bytes)[%d] != size[%d]", 338 len(test.bytes), test.size) 339 } 340 341 name := fmt.Sprintf("0x%x", test.value) 342 t.Run(name, func(t *testing.T) { 343 size := tlv.SizeTUint64(test.value) 344 if test.size != size { 345 t.Fatalf("size mismatch, expected: %d got: %d", 346 test.size, size) 347 } 348 }) 349 } 350 } 351 352 // TestTUint64 asserts that ETUint64 outputs the proper encoding of a truncated 353 // uint64, and that DTUint64 is able to parse the output. 354 func TestTUint64(t *testing.T) { 355 var buf [8]byte 356 for _, test := range tuint64Tests { 357 test := test 358 359 if len(test.bytes) != int(test.size) { 360 t.Fatalf("invalid test case, "+ 361 "len(bytes)[%d] != size[%d]", 362 len(test.bytes), test.size) 363 } 364 365 name := fmt.Sprintf("0x%x", test.value) 366 t.Run(name, func(t *testing.T) { 367 // Test generic encoder. 368 var b bytes.Buffer 369 err := tlv.ETUint64(&b, &test.value, &buf) 370 if err != nil { 371 t.Fatalf("unable to encode tuint64: %v", err) 372 } 373 374 if !bytes.Equal(b.Bytes(), test.bytes) { 375 t.Fatalf("encoding mismatch, "+ 376 "expected: %x, got: %x", 377 test.bytes, b.Bytes()) 378 } 379 380 // Test non-generic encoder. 381 var b2 bytes.Buffer 382 err = tlv.ETUint64T(&b2, test.value, &buf) 383 if err != nil { 384 t.Fatalf("unable to encode tuint64: %v", err) 385 } 386 387 if !bytes.Equal(b2.Bytes(), test.bytes) { 388 t.Fatalf("encoding mismatch, "+ 389 "expected: %x, got: %x", 390 test.bytes, b2.Bytes()) 391 } 392 393 var value uint64 394 r := bytes.NewReader(b.Bytes()) 395 err = tlv.DTUint64(r, &value, &buf, test.size) 396 if err != nil { 397 t.Fatalf("unable to decode tuint64: %v", err) 398 } 399 400 if value != test.value { 401 t.Fatalf("decoded value mismatch, "+ 402 "expected: %d, got: %d", 403 test.value, value) 404 } 405 }) 406 } 407 }