Skip to content

Commit f30c0a7

Browse files
committed
go.mod+test: modernise the TestVariablePayloadOnion test
Update the TestVariablePayloadOnion test to make use of the `require` package.
1 parent 921494f commit f30c0a7

File tree

3 files changed

+41
-42
lines changed

3 files changed

+41
-42
lines changed

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ require (
88
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
99
github.com/davecgh/go-spew v1.1.1
1010
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
11+
github.com/stretchr/testify v1.8.2
1112
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
1213
)
1314

14-
require golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed // indirect
15+
require (
16+
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed // indirect
18+
gopkg.in/yaml.v3 v3.0.1 // indirect
19+
)
1520

1621
go 1.19

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg
2121
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
2222
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
2323
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2425
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2526
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2627
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
@@ -53,6 +54,15 @@ github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5
5354
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
5455
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
5556
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
57+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
58+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
59+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
60+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
61+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
62+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
63+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
64+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
65+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
5666
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
5767
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
5868
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
@@ -85,9 +95,13 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ
8595
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
8696
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
8797
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
98+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8899
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
89100
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
90101
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
91102
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
92103
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
93104
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
105+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
106+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
107+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

sphinx_test.go

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55
"encoding/hex"
66
"encoding/json"
77
"fmt"
8-
"io/ioutil"
8+
"os"
99
"reflect"
1010
"testing"
1111

1212
"github.com/btcsuite/btcd/btcec/v2"
1313
"github.com/btcsuite/btcd/chaincfg"
1414
"github.com/davecgh/go-spew/spew"
15+
"github.com/stretchr/testify/require"
1516
)
1617

1718
// BOLT 4 Test Vectors
@@ -749,8 +750,8 @@ func TestSphinxHopVariableSizedPayloads(t *testing.T) {
749750
}
750751
}
751752

752-
// testFileName is the name of the multi-frame onion test file.
753-
const testFileName = "testdata/onion-test-multi-frame.json"
753+
// testMultiFrameFileName is the name of the multi-frame onion test file.
754+
const testMultiFrameFileName = "testdata/onion-test-multi-frame.json"
754755

755756
type jsonHop struct {
756757
Type string `json:"type"`
@@ -802,35 +803,26 @@ func TestVariablePayloadOnion(t *testing.T) {
802803
t.Parallel()
803804

804805
// First, we'll read out the raw JSOn file at the target location.
805-
jsonBytes, err := ioutil.ReadFile(testFileName)
806-
if err != nil {
807-
t.Fatalf("unable to read json file: %v", err)
808-
}
806+
jsonBytes, err := os.ReadFile(testMultiFrameFileName)
807+
require.NoError(t, err)
809808

810809
// Once we have the raw file, we'll unpack it into our jsonTestCase
811810
// struct defined above.
812811
testCase := &jsonTestCase{}
813-
if err := json.Unmarshal(jsonBytes, testCase); err != nil {
814-
t.Fatalf("unable to parse spec json file: %v", err)
815-
}
812+
require.NoError(t, json.Unmarshal(jsonBytes, testCase))
816813

817814
// Next, we'll populate a new OnionHop using the information included
818815
// in this test case.
819816
var route PaymentPath
820817
for i, hop := range testCase.Generate.Hops {
821818
pubKeyBytes, err := hex.DecodeString(hop.Pubkey)
822-
if err != nil {
823-
t.Fatalf("unable to decode pubkey: %v", err)
824-
}
819+
require.NoError(t, err)
820+
825821
pubKey, err := btcec.ParsePubKey(pubKeyBytes)
826-
if err != nil {
827-
t.Fatalf("unable to parse BOLT 4 pubkey #%d: %v", i, err)
828-
}
822+
require.NoError(t, err)
829823

830824
payload, err := hex.DecodeString(hop.Payload)
831-
if err != nil {
832-
t.Fatalf("unable to decode payload: %v", err)
833-
}
825+
require.NoError(t, err)
834826

835827
payloadType := jsonTypeToPayloadType(hop.Type)
836828
route[i] = OnionHop{
@@ -854,39 +846,27 @@ func TestVariablePayloadOnion(t *testing.T) {
854846
}
855847

856848
finalPacket, err := hex.DecodeString(testCase.Onion)
857-
if err != nil {
858-
t.Fatalf("unable to decode packet: %v", err)
859-
}
849+
require.NoError(t, err)
860850

861851
sessionKeyBytes, err := hex.DecodeString(testCase.Generate.SessionKey)
862-
if err != nil {
863-
t.Fatalf("unable to generate session key: %v", err)
864-
}
852+
require.NoError(t, err)
865853

866-
associatedData, err := hex.DecodeString(testCase.Generate.AssociatedData)
867-
if err != nil {
868-
t.Fatalf("unable to decode AD: %v", err)
869-
}
854+
assocData, err := hex.DecodeString(testCase.Generate.AssociatedData)
855+
require.NoError(t, err)
870856

871857
// With all the required data assembled, we'll craft a new packet.
872858
sessionKey, _ := btcec.PrivKeyFromBytes(sessionKeyBytes)
873859
pkt, err := NewOnionPacket(
874-
&route, sessionKey, associatedData, DeterministicPacketFiller,
860+
&route, sessionKey, assocData, DeterministicPacketFiller,
875861
)
876-
if err != nil {
877-
t.Fatalf("unable to construct onion packet: %v", err)
878-
}
862+
require.NoError(t, err)
879863

880864
var b bytes.Buffer
881-
if err := pkt.Encode(&b); err != nil {
882-
t.Fatalf("unable to decode onion packet: %v", err)
883-
}
865+
require.NoError(t, pkt.Encode(&b))
884866

885867
// Finally, we expect that our packet matches the packet included in
886868
// the spec's test vectors.
887-
if bytes.Compare(b.Bytes(), finalPacket) != 0 {
888-
t.Fatalf("final packet does not match expected BOLT 4 packet, "+
889-
"want: %s, got %s", hex.EncodeToString(finalPacket),
890-
hex.EncodeToString(b.Bytes()))
891-
}
869+
require.Equalf(t, b.Bytes(), finalPacket, "final packet does not "+
870+
"match expected BOLT 4 packet, want: %s, got %s",
871+
hex.EncodeToString(finalPacket), hex.EncodeToString(b.Bytes()))
892872
}

0 commit comments

Comments
 (0)