|
| 1 | +package lnwire |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/lightningnetwork/lnd/lnutils" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// TestDynCommitEncodeDecode checks that the Encode and Decode methods for |
| 12 | +// DynCommit work as expected. |
| 13 | +func TestDynCommitEncodeDecode(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + // Generate random channel ID. |
| 17 | + chanIDBytes, err := generateRandomBytes(32) |
| 18 | + require.NoError(t, err) |
| 19 | + |
| 20 | + var chanID ChannelID |
| 21 | + copy(chanID[:], chanIDBytes) |
| 22 | + |
| 23 | + // Generate random sig. |
| 24 | + sigBytes, err := generateRandomBytes(64) |
| 25 | + require.NoError(t, err) |
| 26 | + |
| 27 | + var sig Sig |
| 28 | + copy(sig.bytes[:], sigBytes) |
| 29 | + |
| 30 | + // Create test data for the TLVs. The actual value doesn't matter, as we |
| 31 | + // only care about that the raw bytes can be decoded into a msg, and the |
| 32 | + // msg can be encoded into the exact same raw bytes. |
| 33 | + testTlvData := []byte{ |
| 34 | + // DustLimit tlv. |
| 35 | + 0x0, // type. |
| 36 | + 0x5, // length. |
| 37 | + 0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 1_000_000). |
| 38 | + |
| 39 | + // ExtraData - unknown tlv record. |
| 40 | + // |
| 41 | + // NOTE: This record is optional and occupies the type 1. |
| 42 | + 0x1, // type. |
| 43 | + 0x2, // length. |
| 44 | + 0x79, 0x79, // value. |
| 45 | + |
| 46 | + // MaxValueInFlight tlv. |
| 47 | + 0x2, // type. |
| 48 | + 0x5, // length. |
| 49 | + 0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 1_000_000). |
| 50 | + |
| 51 | + // HtlcMinimum tlv. |
| 52 | + 0x4, // type. |
| 53 | + 0x5, // length. |
| 54 | + 0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 1_000_000). |
| 55 | + // |
| 56 | + // ChannelReserve tlv. |
| 57 | + 0x6, // type. |
| 58 | + 0x5, // length. |
| 59 | + 0xfe, 0x0, 0xf, 0x42, 0x40, // value (BigSize: 1_000_000). |
| 60 | + |
| 61 | + // CsvDelay tlv. |
| 62 | + 0x8, // type. |
| 63 | + 0x2, // length. |
| 64 | + 0x0, 0x8, // value. |
| 65 | + |
| 66 | + // MaxAcceptedHTLCs tlv. |
| 67 | + 0xa, // type. |
| 68 | + 0x2, // length. |
| 69 | + 0x0, 0x8, // value. |
| 70 | + |
| 71 | + // ChannelType tlv is empty. |
| 72 | + // |
| 73 | + // LocalNonce tlv. |
| 74 | + 0x14, // type. |
| 75 | + 0x42, // length. |
| 76 | + 0x2c, 0xd4, 0x53, 0x7d, 0xaa, 0x7b, // value. |
| 77 | + 0x7e, 0xae, 0x18, 0x32, 0xa6, 0xc4, 0x29, 0xe9, 0xe0, 0x91, |
| 78 | + 0x32, 0x7a, 0xaf, 0xd1, 0x1c, 0x2b, 0x04, 0xa0, 0x4d, 0xb5, |
| 79 | + 0x6a, 0x6f, 0x8b, 0x6c, 0xdc, 0xd1, 0x80, 0x2d, 0xff, 0x72, |
| 80 | + 0xd8, 0x3c, 0xfc, 0x01, 0x6e, 0x7c, 0x1a, 0xc8, 0x5e, 0x3a, |
| 81 | + 0x16, 0x98, 0xbc, 0x9b, 0x6e, 0x22, 0x58, 0x96, 0x96, 0xad, |
| 82 | + 0x88, 0xbf, 0xff, 0x59, 0x90, 0xbd, 0x36, 0x0b, 0x0b, 0x4d, |
| 83 | + |
| 84 | + // ExtraData - unknown tlv record. |
| 85 | + 0x6f, // type. |
| 86 | + 0x2, // length. |
| 87 | + 0x79, 0x79, // value. |
| 88 | + } |
| 89 | + |
| 90 | + msg := &DynCommit{} |
| 91 | + |
| 92 | + // Pre-allocate a new slice with enough capacity for all three parts for |
| 93 | + // efficiency. |
| 94 | + totalLen := len(chanIDBytes) + len(sigBytes) + len(testTlvData) |
| 95 | + rawBytes := make([]byte, 0, totalLen) |
| 96 | + |
| 97 | + // Append each slice to the new rawBytes slice. |
| 98 | + rawBytes = append(rawBytes, chanIDBytes...) |
| 99 | + rawBytes = append(rawBytes, sigBytes...) |
| 100 | + rawBytes = append(rawBytes, testTlvData...) |
| 101 | + |
| 102 | + // Decode the raw bytes. |
| 103 | + r := bytes.NewBuffer(rawBytes) |
| 104 | + err = msg.Decode(r, 0) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + t.Logf("Encoded msg is %v", lnutils.SpewLogClosure(msg)) |
| 108 | + |
| 109 | + // Encode the msg into raw bytes and assert the encoded bytes equal to |
| 110 | + // the rawBytes. |
| 111 | + w := new(bytes.Buffer) |
| 112 | + err = msg.Encode(w, 0) |
| 113 | + require.NoError(t, err) |
| 114 | + |
| 115 | + require.Equal(t, rawBytes, w.Bytes()) |
| 116 | +} |
0 commit comments