Skip to content

Commit 5c3f7be

Browse files
committed
lnwire: add encoding/decoding unit tests for UpdateFulfillHTLC message
1 parent d247cc9 commit 5c3f7be

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

lnwire/update_fulfill_htlc_test.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package lnwire
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/lightningnetwork/lnd/tlv"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
// testCaseUpdateFulfill is a test case for the UpdateFulfillHTLC message.
12+
type testCaseUpdateFulfill struct {
13+
// Msg is the message to be encoded and decoded.
14+
Msg UpdateFulfillHTLC
15+
16+
// ExpectEncodeError is a flag that indicates whether we expect the
17+
// encoding of the message to fail.
18+
ExpectEncodeError bool
19+
}
20+
21+
// generateTestCases generates a set of UpdateFulfillHTLC message test cases.
22+
func generateUpdateFulfillTestCases(t *testing.T) []testCaseUpdateFulfill {
23+
// Firstly, we'll set basic values for the message fields.
24+
//
25+
// Generate random channel ID.
26+
chanIDBytes, err := generateRandomBytes(32)
27+
require.NoError(t, err)
28+
29+
var chanID ChannelID
30+
copy(chanID[:], chanIDBytes)
31+
32+
// Generate random payment preimage.
33+
paymentPreimageBytes, err := generateRandomBytes(32)
34+
require.NoError(t, err)
35+
36+
var paymentPreimage [32]byte
37+
copy(paymentPreimage[:], paymentPreimageBytes)
38+
39+
// Define custom records.
40+
recordKey1 := uint64(MinCustomRecordsTlvType + 1)
41+
recordValue1, err := generateRandomBytes(10)
42+
require.NoError(t, err)
43+
44+
recordKey2 := uint64(MinCustomRecordsTlvType + 2)
45+
recordValue2, err := generateRandomBytes(10)
46+
require.NoError(t, err)
47+
48+
customRecords := CustomRecords{
49+
recordKey1: recordValue1,
50+
recordKey2: recordValue2,
51+
}
52+
53+
// Construct an instance of extra data that contains records with TLV
54+
// types below the minimum custom records threshold and that lack
55+
// corresponding fields in the message struct. Content should persist in
56+
// the extra data field after encoding and decoding.
57+
var (
58+
recordBytes45 = []byte("recordBytes45")
59+
tlvRecord45 = tlv.NewPrimitiveRecord[tlv.TlvType45](
60+
recordBytes45,
61+
)
62+
63+
recordBytes55 = []byte("recordBytes55")
64+
tlvRecord55 = tlv.NewPrimitiveRecord[tlv.TlvType55](
65+
recordBytes55,
66+
)
67+
)
68+
69+
var extraData ExtraOpaqueData
70+
err = extraData.PackRecords(
71+
[]tlv.RecordProducer{&tlvRecord45, &tlvRecord55}...,
72+
)
73+
require.NoError(t, err)
74+
75+
// Define test cases.
76+
testCases := make([]testCaseUpdateFulfill, 0)
77+
78+
testCases = append(testCases, testCaseUpdateFulfill{
79+
Msg: UpdateFulfillHTLC{
80+
ChanID: chanID,
81+
ID: 42,
82+
PaymentPreimage: paymentPreimage,
83+
CustomRecords: customRecords,
84+
ExtraData: extraData,
85+
},
86+
})
87+
88+
return testCases
89+
}
90+
91+
// TestUpdateFulfillHtlcEncodeDecode tests UpdateFulfillHTLC message encoding
92+
// and decoding for all supported field values.
93+
func TestUpdateFulfillHtlcEncodeDecode(t *testing.T) {
94+
t.Parallel()
95+
96+
// Generate test cases.
97+
testCases := generateUpdateFulfillTestCases(t)
98+
99+
// Execute test cases.
100+
for tcIdx, tc := range testCases {
101+
t.Log("Running test case", tcIdx)
102+
103+
// Encode test case message.
104+
var buf bytes.Buffer
105+
err := tc.Msg.Encode(&buf, 0)
106+
107+
// Check if we expect an encoding error.
108+
if tc.ExpectEncodeError {
109+
require.Error(t, err)
110+
continue
111+
}
112+
require.NoError(t, err)
113+
114+
// Decode the encoded message bytes message.
115+
var actualMsg UpdateFulfillHTLC
116+
decodeReader := bytes.NewReader(buf.Bytes())
117+
err = actualMsg.Decode(decodeReader, 0)
118+
require.NoError(t, err)
119+
120+
// Compare the two messages to ensure equality one field at a
121+
// time.
122+
require.Equal(t, tc.Msg.ChanID, actualMsg.ChanID)
123+
require.Equal(t, tc.Msg.ID, actualMsg.ID)
124+
require.Equal(
125+
t, tc.Msg.PaymentPreimage, actualMsg.PaymentPreimage,
126+
)
127+
128+
// Check that the custom records field is as expected.
129+
if len(tc.Msg.CustomRecords) == 0 {
130+
require.Len(t, actualMsg.CustomRecords, 0)
131+
} else {
132+
require.Equal(
133+
t, tc.Msg.CustomRecords,
134+
actualMsg.CustomRecords,
135+
)
136+
}
137+
138+
// Check that the extra data field is as expected.
139+
if len(tc.Msg.ExtraData) == 0 {
140+
require.Len(t, actualMsg.ExtraData, 0)
141+
} else {
142+
require.Equal(
143+
t, tc.Msg.ExtraData,
144+
actualMsg.ExtraData,
145+
)
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)