From 795aff5f42b73e990df4f85c981c790ca1a6c4c7 Mon Sep 17 00:00:00 2001 From: ffranr Date: Fri, 3 May 2024 15:24:42 +0100 Subject: [PATCH] lnwire: add unit tests for `ExtraOpaqueData.PackRecords` --- lnwire/extra_bytes_test.go | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/lnwire/extra_bytes_test.go b/lnwire/extra_bytes_test.go index 97a908bce..b05b19db5 100644 --- a/lnwire/extra_bytes_test.go +++ b/lnwire/extra_bytes_test.go @@ -143,3 +143,66 @@ func TestExtraOpaqueDataPackUnpackRecords(t *testing.T) { t.Fatalf("type2 not found in typeMap") } } + +// TestPackRecords tests that we're able to pack a set of records into an +// ExtraOpaqueData instance, and then extract them back out. Crucially, we'll +// ensure that records can be packed in any order, and we'll ensure that the +// unpacked records are valid. +func TestPackRecords(t *testing.T) { + t.Parallel() + + // Create an empty ExtraOpaqueData instance. + extraBytes := ExtraOpaqueData{} + + var ( + // Record type 1. + tlvType1 tlv.TlvType1 + recordBytes1 = []byte("recordBytes1") + tlvRecord1 = tlv.NewPrimitiveRecord[tlv.TlvType1]( + recordBytes1, + ) + + // Record type 2. + tlvType2 tlv.TlvType2 + recordBytes2 = []byte("recordBytes2") + tlvRecord2 = tlv.NewPrimitiveRecord[tlv.TlvType2]( + recordBytes2, + ) + + // Record type 3. + tlvType3 tlv.TlvType3 + recordBytes3 = []byte("recordBytes3") + tlvRecord3 = tlv.NewPrimitiveRecord[tlv.TlvType3]( + recordBytes3, + ) + ) + + // Pack records 1 and 2 into the ExtraOpaqueData instance. + err := extraBytes.PackRecords( + []tlv.RecordProducer{&tlvRecord1, &tlvRecord2}..., + ) + require.NoError(t, err) + + // Examine the records that were packed into the ExtraOpaqueData. + extractedRecords, err := extraBytes.ExtractRecords() + require.NoError(t, err) + + require.Equal(t, 2, len(extractedRecords)) + require.Equal(t, recordBytes1, extractedRecords[tlvType1.TypeVal()]) + require.Equal(t, recordBytes2, extractedRecords[tlvType2.TypeVal()]) + + // Pack records 1, 2, and 3 into the ExtraOpaqueData instance. + err = extraBytes.PackRecords( + []tlv.RecordProducer{&tlvRecord3, &tlvRecord1, &tlvRecord2}..., + ) + require.NoError(t, err) + + // Examine the records that were packed into the ExtraOpaqueData. + extractedRecords, err = extraBytes.ExtractRecords() + require.NoError(t, err) + + require.Equal(t, 3, len(extractedRecords)) + require.Equal(t, recordBytes1, extractedRecords[tlvType1.TypeVal()]) + require.Equal(t, recordBytes2, extractedRecords[tlvType2.TypeVal()]) + require.Equal(t, recordBytes3, extractedRecords[tlvType3.TypeVal()]) +}