@@ -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
755756type 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