|
8 | 8 | "github.com/btcsuite/btcd/btcec/v2" |
9 | 9 | "github.com/lightningnetwork/lnd/lnwire" |
10 | 10 | "github.com/lightningnetwork/lnd/record" |
| 11 | + "github.com/lightningnetwork/lnd/tlv" |
11 | 12 | "github.com/stretchr/testify/require" |
12 | 13 | ) |
13 | 14 |
|
@@ -430,3 +431,57 @@ func TestBlindedHopFee(t *testing.T) { |
430 | 431 | require.Equal(t, lnwire.MilliSatoshi(0), route.HopFee(3)) |
431 | 432 | require.Equal(t, lnwire.MilliSatoshi(0), route.HopFee(4)) |
432 | 433 | } |
| 434 | + |
| 435 | +// makeVertex creates a test Vertex with sequential byte values for testing |
| 436 | +// TLV encoding/decoding. |
| 437 | +func makeVertex() Vertex { |
| 438 | + var v Vertex |
| 439 | + for i := range v { |
| 440 | + v[i] = byte(i) |
| 441 | + } |
| 442 | + |
| 443 | + return v |
| 444 | +} |
| 445 | + |
| 446 | +// TestVertexTLVEncodeDecode tests that we're able to properly encode and decode |
| 447 | +// Vertex within TLV streams. |
| 448 | +func TestVertexTLVEncodeDecode(t *testing.T) { |
| 449 | + t.Parallel() |
| 450 | + |
| 451 | + vertex := makeVertex() |
| 452 | + |
| 453 | + var extraData lnwire.ExtraOpaqueData |
| 454 | + require.NoError(t, extraData.PackRecords(&vertex)) |
| 455 | + |
| 456 | + var vertex2 Vertex |
| 457 | + tlvs, err := extraData.ExtractRecords(&vertex2) |
| 458 | + require.NoError(t, err) |
| 459 | + |
| 460 | + require.Contains(t, tlvs, tlv.Type(0)) |
| 461 | + require.Equal(t, vertex, vertex2) |
| 462 | +} |
| 463 | + |
| 464 | +// TestVertexTypeDecodeInvalidLength ensures that decoding a Vertex TLV |
| 465 | +// with an invalid length (anything other than 33) fails with an error. |
| 466 | +func TestVertexTypeDecodeInvalidLength(t *testing.T) { |
| 467 | + t.Parallel() |
| 468 | + |
| 469 | + vertex := makeVertex() |
| 470 | + |
| 471 | + var extraData lnwire.ExtraOpaqueData |
| 472 | + require.NoError(t, extraData.PackRecords(&vertex)) |
| 473 | + |
| 474 | + // Corrupt the TLV length field to simulate malformed input. |
| 475 | + // Byte 1 contains the varint size encoding. Since 33 bytes fits into |
| 476 | + // a single varint byte, we can directly modify extraData[1]. |
| 477 | + extraData[1] = VertexSize + 1 |
| 478 | + |
| 479 | + var out Vertex |
| 480 | + _, err := extraData.ExtractRecords(&out) |
| 481 | + require.Error(t, err) |
| 482 | + |
| 483 | + extraData[1] = VertexSize - 1 |
| 484 | + |
| 485 | + _, err = extraData.ExtractRecords(&out) |
| 486 | + require.Error(t, err) |
| 487 | +} |
0 commit comments