@@ -131,7 +131,7 @@ func generateCommitSigTestCases(t *testing.T) []commitSigTestCase {
131131
132132// TestCommitSigEncodeDecode tests CommitSig message encoding and decoding for
133133// all supported field values.
134- func TestCommitSigEncodeDecode (t * testing.T ) {
134+ func TestCommitSigEncodeDecodeFields (t * testing.T ) {
135135 t .Parallel ()
136136
137137 // Generate test cases.
@@ -166,3 +166,56 @@ func TestCommitSigEncodeDecode(t *testing.T) {
166166 })
167167 }
168168}
169+
170+ // TestCommitSigEncodeDecode tests that a raw byte stream can be decoded, then
171+ // re-encoded to the same exact byte stream.
172+ func TestCommitSigEncodeDecode (t * testing.T ) {
173+ t .Parallel ()
174+
175+ // We'll create a raw byte stream that represents a valid CommitSig
176+ // message. This includes the fixed-size fields and a TLV stream with
177+ // both known and unknown records.
178+ var rawBytes []byte
179+
180+ // ChanID
181+ rawBytes = append (rawBytes , make ([]byte , 32 )... )
182+
183+ // CommitSig
184+ rawBytes = append (rawBytes , make ([]byte , 64 )... )
185+
186+ // HtlcSigs
187+ rawBytes = append (rawBytes , []byte {0 , 0 }... )
188+
189+ // Add TLV data, including known and unknown records.
190+ tlvData := []byte {
191+ // PartialSig record.
192+ 2 , 98 ,
193+ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ,
194+ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ,
195+ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ,
196+ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ,
197+ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,
198+
199+ // Unknown odd record: Type=3, Length=1, Value=0.
200+ 3 , 1 , 0 ,
201+
202+ // CustomRecords: Type=65536, Length=1, Value=0.
203+ 0xfe , 0x00 , 0x01 , 0x00 , 0x00 , 1 , 0 ,
204+ }
205+ rawBytes = append (rawBytes , tlvData ... )
206+
207+ // Now, create a new empty message and decode the raw bytes into it.
208+ msg := & CommitSig {}
209+ r := bytes .NewReader (rawBytes )
210+ err := msg .Decode (r , 0 )
211+ require .NoError (t , err )
212+
213+ // Next, encode the message back into a new byte buffer.
214+ var b bytes.Buffer
215+ err = msg .Encode (& b , 0 )
216+ require .NoError (t , err )
217+
218+ // The re-encoded bytes should be exactly the same as the original raw
219+ // bytes.
220+ require .Equal (t , rawBytes , b .Bytes ())
221+ }
0 commit comments