Skip to content

Commit edf37ae

Browse files
committed
lnwire: patch unit tests for encoding/decoding
This commit adds unit tests for `AnnounceSignatures1`, `AnnounceSignatures2`, and `ChannelAnnouncement1`. This further assures all the encoding and decoding methods are behaved as expected.
1 parent b230501 commit edf37ae

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package lnwire
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
// TestAnnounceSignatures2EncodeDecode tests the encode and decode methods for
11+
// the AnnounceSignatures2 message.
12+
func TestAnnounceSignatures2EncodeDecode(t *testing.T) {
13+
t.Parallel()
14+
15+
// We'll create a raw byte stream that represents a valid
16+
// AnnounceSignatures2 message. This includes the fixed-size fields and
17+
// a TLV stream with both known and unknown records.
18+
var rawBytes []byte
19+
20+
// ChannelID.
21+
rawBytes = append(rawBytes, make([]byte, 32)...)
22+
23+
// ShortChannelID.
24+
rawBytes = append(rawBytes, []byte{0, 0, 1, 0, 0, 2, 0, 3}...)
25+
26+
// PartialSignature.
27+
rawBytes = append(rawBytes, make([]byte, 32)...)
28+
29+
// Extra Opaque Data.
30+
rawBytes = append(rawBytes, []byte{
31+
// Unknown odd-type TLV record.
32+
0x3, // type
33+
0x2, // length
34+
0xab, 0xcd, // value
35+
}...)
36+
37+
// Extra Opaque Data.
38+
rawBytes = append(rawBytes, []byte{
39+
// Unknown odd-type TLV record.
40+
0x6f, // type
41+
0x2, // length
42+
0x79, 0x79, // value
43+
}...)
44+
45+
// Now, create a new empty message and decode the raw bytes into it.
46+
msg := &AnnounceSignatures2{}
47+
r := bytes.NewReader(rawBytes)
48+
err := msg.Decode(r, 0)
49+
require.NoError(t, err)
50+
51+
// Next, encode the message back into a new byte buffer.
52+
var b bytes.Buffer
53+
err = msg.Encode(&b, 0)
54+
require.NoError(t, err)
55+
56+
// The re-encoded bytes should be exactly the same as the original raw
57+
// bytes.
58+
require.Equal(t, rawBytes, b.Bytes())
59+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package lnwire
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
// TestAnnounceSignatures1EncodeDecode tests the encode and decode methods for
11+
// the AnnounceSignatures1 message.
12+
func TestAnnounceSignatures1EncodeDecode(t *testing.T) {
13+
t.Parallel()
14+
15+
// We'll create a raw byte stream that represents a valid
16+
// AnnounceSignatures1 message. This includes the fixed-size fields and
17+
// a TLV stream with both known and unknown records.
18+
var rawBytes []byte
19+
20+
// ChannelID.
21+
rawBytes = append(rawBytes, make([]byte, 32)...)
22+
23+
// ShortChannelID.
24+
rawBytes = append(rawBytes, []byte{0, 0, 1, 0, 0, 2, 0, 3}...)
25+
26+
// NodeSignature.
27+
rawBytes = append(rawBytes, make([]byte, 64)...)
28+
29+
// BitcoinSignature.
30+
rawBytes = append(rawBytes, make([]byte, 64)...)
31+
32+
// Extra Opaque Data.
33+
rawBytes = append(rawBytes, []byte{
34+
// Unknown odd-type TLV record.
35+
0x3, // type
36+
0x2, // length
37+
0xab, 0xcd, // value
38+
}...)
39+
40+
// Extra Opaque Data.
41+
rawBytes = append(rawBytes, []byte{
42+
// Unknown odd-type TLV record.
43+
0x6f, // type
44+
0x2, // length
45+
0x79, 0x79, // value
46+
}...)
47+
48+
// Now, create a new empty message and decode the raw bytes into it.
49+
msg := &AnnounceSignatures1{}
50+
r := bytes.NewReader(rawBytes)
51+
err := msg.Decode(r, 0)
52+
require.NoError(t, err)
53+
54+
// Next, encode the message back into a new byte buffer.
55+
var b bytes.Buffer
56+
err = msg.Encode(&b, 0)
57+
require.NoError(t, err)
58+
59+
// The re-encoded bytes should be exactly the same as the original raw
60+
// bytes.
61+
require.Equal(t, rawBytes, b.Bytes())
62+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package lnwire
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
// TestChannelAnnouncement1EncodeDecode asserts that a raw byte stream can be
11+
// decoded, then re-encoded to the same exact byte stream.
12+
func TestChannelAnnouncement1EncodeDecode(t *testing.T) {
13+
t.Parallel()
14+
15+
// We'll create a raw byte stream that represents a valid
16+
// ChannelAnnouncement1 message. This includes valid values for all
17+
// fields and some extra opaque data to test for forward-compatibility.
18+
// The exact values are not important, only that they are of the
19+
// correct size.
20+
var rawBytes []byte
21+
rawBytes = append(rawBytes, make([]byte, 64)...) // NodeSig1
22+
rawBytes = append(rawBytes, make([]byte, 64)...) // NodeSig2
23+
rawBytes = append(rawBytes, make([]byte, 64)...) // BitcoinSig1
24+
rawBytes = append(rawBytes, make([]byte, 64)...) // BitcoinSig2
25+
rawBytes = append(rawBytes, []byte{
26+
0x0, 0x1, 0x2, // Features
27+
}...)
28+
rawBytes = append(rawBytes, make([]byte, 32)...) // ChainHash
29+
rawBytes = append(rawBytes, []byte{
30+
0x0, 0x0, 0x1, // BlockHeight
31+
0x0, 0x0, 0x2, // TxIndex
32+
0x0, 0x3, // TxPosition
33+
}...)
34+
rawBytes = append(rawBytes, make([]byte, 33)...) // NodeID1
35+
rawBytes = append(rawBytes, make([]byte, 33)...) // NodeID2
36+
rawBytes = append(rawBytes, make([]byte, 33)...) // BitcoinKey1
37+
rawBytes = append(rawBytes, make([]byte, 33)...) // BitcoinKey2
38+
rawBytes = append(rawBytes, []byte{
39+
// Extra Opaque Data - Unknown Record 1
40+
0x6d, // type
41+
0x2, // length
42+
0xab, 0xcd, // value
43+
44+
// Extra Opaque Data - Unknown Record 2
45+
0x6f, // type
46+
0x2, // length
47+
0x79, 0x79, // value
48+
}...)
49+
50+
// Now, create a new empty message and decode the raw bytes into it.
51+
msg := &ChannelAnnouncement1{}
52+
r := bytes.NewReader(rawBytes)
53+
err := msg.Decode(r, 0)
54+
require.NoError(t, err)
55+
56+
// Next, encode the message back into a new byte buffer.
57+
var b bytes.Buffer
58+
err = msg.Encode(&b, 0)
59+
require.NoError(t, err)
60+
61+
// The re-encoded bytes should be exactly the same as the original raw
62+
// bytes.
63+
require.Equal(t, rawBytes, b.Bytes())
64+
}

0 commit comments

Comments
 (0)