-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathdecode_test.go
222 lines (180 loc) · 5.35 KB
/
decode_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package tappsbt
import (
"bytes"
"encoding/base64"
"reflect"
"strings"
"testing"
"github.com/lightninglabs/taproot-assets/address"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/internal/test"
"github.com/stretchr/testify/require"
)
var (
generatedTestVectorName = "psbt_encoding_generated.json"
allTestVectorFiles = []string{
generatedTestVectorName,
"psbt_encoding_error_cases.json",
}
)
// assertEqualPackets asserts that two packets are equal and prints a nice diff
// if they are not.
func assertEqualPackets(t *testing.T, expected, actual *VPacket) {
if !reflect.DeepEqual(expected.ChainParams, actual.ChainParams) {
require.Equal(t, expected.ChainParams, actual.ChainParams)
require.Fail(t, "ChainParams not equal")
}
require.Len(t, expected.Inputs, len(actual.Inputs))
for idx := range expected.Inputs {
e := expected.Inputs[idx]
a := actual.Inputs[idx]
if !reflect.DeepEqual(e, a) {
require.Equal(t, e, a, "input %d not equal", idx)
require.Fail(t, "input not equal")
}
}
require.Len(t, expected.Outputs, len(actual.Outputs))
for idx := range expected.Outputs {
e := expected.Outputs[idx]
a := actual.Outputs[idx]
if !reflect.DeepEqual(e, a) {
require.Equalf(t, e, a, "output %d not equal", idx)
require.Fail(t, "output not equal")
}
}
}
// TestEncodingDecoding tests the decoding of a virtual packet from raw bytes.
func TestEncodingDecoding(t *testing.T) {
t.Parallel()
testVectors := &TestVectors{}
assertEncodingDecoding := func(comment string, pkg *VPacket) {
// Encode the packet as a PSBT packet then as base64.
packet, err := pkg.EncodeAsPsbt()
require.NoError(t, err)
var buf bytes.Buffer
err = packet.Serialize(&buf)
require.NoError(t, err)
testVectors.ValidTestCases = append(
testVectors.ValidTestCases, &ValidTestCase{
Packet: NewTestFromVPacket(t, pkg),
Expected: base64.StdEncoding.EncodeToString(
buf.Bytes(),
),
Comment: comment,
},
)
// Make sure we can read the packet back from the raw bytes.
decoded, err := NewFromRawBytes(&buf, false)
require.NoError(t, err)
assertEqualPackets(t, pkg, decoded)
// Also make sure we can decode the packet from the base PSBT.
decoded, err = NewFromPsbt(packet)
require.NoError(t, err)
assertEqualPackets(t, pkg, decoded)
}
testCases := []struct {
name string
pkg func(t *testing.T) *VPacket
}{{
name: "minimal packet",
pkg: func(t *testing.T) *VPacket {
proofCourierAddr := address.RandProofCourierAddr(t)
addr, _, _ := address.RandAddr(
t, testParams, proofCourierAddr,
)
pkg, _, err := FromAddresses([]*address.Tap{addr.Tap}, 1)
require.NoError(t, err)
pkg.Outputs = append(pkg.Outputs, &VOutput{
ScriptKey: asset.RandScriptKey(t),
})
return pkg
},
}, {
name: "random packet",
pkg: func(t *testing.T) *VPacket {
return RandPacket(t)
},
}}
for _, testCase := range testCases {
testCase := testCase
success := t.Run(testCase.name, func(t *testing.T) {
pkg := testCase.pkg(t)
assertEncodingDecoding(testCase.name, pkg)
})
if !success {
return
}
}
// Write test vectors to file. This is a no-op if the "gen_test_vectors"
// build tag is not set.
test.WriteTestVectors(t, generatedTestVectorName, testVectors)
}
// TestBIPTestVectors tests that the BIP test vectors are passing.
func TestBIPTestVectors(t *testing.T) {
t.Parallel()
for idx := range allTestVectorFiles {
var (
fileName = allTestVectorFiles[idx]
testVectors = &TestVectors{}
)
test.ParseTestVectors(t, fileName, &testVectors)
t.Run(fileName, func(tt *testing.T) {
tt.Parallel()
runBIPTestVector(tt, testVectors)
})
}
}
// runBIPTestVector runs the tests in a single BIP test vector file.
func runBIPTestVector(t *testing.T, testVectors *TestVectors) {
for _, validCase := range testVectors.ValidTestCases {
validCase := validCase
t.Run(validCase.Comment, func(tt *testing.T) {
tt.Parallel()
p := validCase.Packet.ToVPacket(t)
packetString, err := p.B64Encode()
require.NoError(tt, err)
areEqual := validCase.Expected == packetString
// Create nice diff if things don't match.
if !areEqual {
expectedPacket, err := NewFromRawBytes(
strings.NewReader(validCase.Expected),
true,
)
require.NoError(tt, err)
require.Equal(tt, p, expectedPacket)
// Make sure we still fail the test.
require.Equal(
tt, validCase.Expected, packetString,
)
}
// We also want to make sure that the address is decoded
// correctly from the encoded TLV stream.
decoded, err := NewFromRawBytes(
strings.NewReader(validCase.Expected), true,
)
require.NoError(tt, err)
require.Equal(tt, p, decoded)
// And finally, we want to make sure that if we get a
// raw byte blob we can also decode the packet and the
// result is the same.
rawBytes, err := base64.StdEncoding.DecodeString(
validCase.Expected,
)
require.NoError(tt, err)
decodedFromBytes, err := NewFromRawBytes(
bytes.NewReader(rawBytes), false,
)
require.NoError(tt, err)
require.Equal(tt, p, decodedFromBytes)
})
}
for _, invalidCase := range testVectors.ErrorTestCases {
invalidCase := invalidCase
t.Run(invalidCase.Comment, func(tt *testing.T) {
tt.Parallel()
require.PanicsWithValue(tt, invalidCase.Error, func() {
invalidCase.Packet.ToVPacket(tt)
})
})
}
}