From e15d3e898ed5f4226ff46a41ef839a5f08cc70ad Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Mon, 7 Nov 2022 16:38:16 -0600 Subject: [PATCH] brontide,lnwire,wtwire,zpay32: appease linter Address all linter complaints for the newly migrated fuzz tests. --- brontide/fuzz_test.go | 126 ++++++++++++++++++--------------- lnwire/fuzz_test.go | 86 ++++++++++++++++------ watchtower/wtwire/fuzz_test.go | 3 + zpay32/fuzz_test.go | 6 +- 4 files changed, 140 insertions(+), 81 deletions(-) diff --git a/brontide/fuzz_test.go b/brontide/fuzz_test.go index 2cea13c10..91b5cd8dc 100644 --- a/brontide/fuzz_test.go +++ b/brontide/fuzz_test.go @@ -36,6 +36,7 @@ var ( } priv, _ := btcec.PrivKeyFromBytes(eBytes) + return priv, nil }) @@ -49,6 +50,7 @@ var ( } priv, _ := btcec.PrivKeyFromBytes(eBytes) + return priv, nil }) ) @@ -56,9 +58,11 @@ var ( // completeHandshake takes two brontide machines (initiator, responder) // and completes the brontide handshake between them. If any part of the // handshake fails, this function will panic. -func completeHandshake(initiator, responder *Machine, t *testing.T) { +func completeHandshake(t *testing.T, initiator, responder *Machine) { + t.Helper() + if err := handshake(initiator, responder); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } } @@ -96,7 +100,9 @@ func handshake(initiator, responder *Machine) error { // nilAndPanic first nils the initiator and responder's Curve fields and then // panics. -func nilAndPanic(initiator, responder *Machine, err error, t *testing.T) { +func nilAndPanic(t *testing.T, initiator, responder *Machine, err error) { + t.Helper() + t.Fatalf("error: %v, initiator: %v, responder: %v", err, spew.Sdump(initiator), spew.Sdump(responder)) } @@ -153,7 +159,7 @@ func FuzzRandomActOne(f *testing.F) { // Responder receives ActOne, should fail on the MAC check. if err := responder.RecvActOne(actOne); err == nil { - nilAndPanic(nil, responder, nil, t) + nilAndPanic(t, nil, responder, nil) } }) } @@ -172,12 +178,12 @@ func FuzzRandomActThree(f *testing.F) { // Generate ActOne and send to the responder. actOne, err := initiator.GenActOne() if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Receiving ActOne should succeed, so we panic on error. if err := responder.RecvActOne(actOne); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Generate ActTwo - this is not sent to the initiator because @@ -186,7 +192,7 @@ func FuzzRandomActThree(f *testing.F) { // the appropriate state in the responder machine. _, err = responder.GenActTwo() if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Copy data into [ActThreeSize]byte. @@ -195,7 +201,7 @@ func FuzzRandomActThree(f *testing.F) { // Responder receives ActThree, should fail on the MAC check. if err := responder.RecvActThree(actThree); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -217,7 +223,7 @@ func FuzzRandomActTwo(f *testing.F) { // appropriate state in the initiator machine. _, err := initiator.GenActOne() if err != nil { - nilAndPanic(initiator, nil, err, t) + nilAndPanic(t, initiator, nil, err) } // Copy data into [ActTwoSize]byte. @@ -226,7 +232,7 @@ func FuzzRandomActTwo(f *testing.F) { // Initiator receives ActTwo, should fail. if err := initiator.RecvActTwo(actTwo); err == nil { - nilAndPanic(initiator, nil, nil, t) + nilAndPanic(t, initiator, nil, nil) } }) } @@ -239,7 +245,7 @@ func FuzzRandomInitDecrypt(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) // Create a reader with the byte array. r := bytes.NewReader(data) @@ -247,7 +253,7 @@ func FuzzRandomInitDecrypt(f *testing.F) { // Decrypt the encrypted message using ReadMessage w/ initiator // machine. if _, err := initiator.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -266,30 +272,31 @@ func FuzzRandomInitEncDec(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ initiator machine. if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ initiator machine. if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } - // Decrypt the ciphertext using ReadMessage w/ responder machine. + // Decrypt the ciphertext using ReadMessage w/ responder + // machine. plaintext, err := responder.ReadMessage(&b) if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Check that the decrypted message and the original message are // equal. if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -308,18 +315,18 @@ func FuzzRandomInitEncrypt(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ initiator machine. if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ initiator machine. if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } }) } @@ -332,7 +339,7 @@ func FuzzRandomRespDecrypt(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) // Create a reader with the byte array. r := bytes.NewReader(data) @@ -340,7 +347,7 @@ func FuzzRandomRespDecrypt(f *testing.F) { // Decrypt the encrypted message using ReadMessage w/ responder // machine. if _, err := responder.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -359,30 +366,31 @@ func FuzzRandomRespEncDec(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ responder machine. if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ responder machine. if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } - // Decrypt the ciphertext using ReadMessage w/ initiator machine. + // Decrypt the ciphertext using ReadMessage w/ initiator + // machine. plaintext, err := initiator.ReadMessage(&b) if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Check that the decrypted message and the original message are // equal. if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -401,18 +409,18 @@ func FuzzRandomRespEncrypt(f *testing.F) { initiator, responder := getBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ responder machine. if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ responder machine. if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } }) } @@ -434,7 +442,7 @@ func FuzzStaticActOne(f *testing.F) { // Responder receives ActOne, should fail. if err := responder.RecvActOne(actOne); err == nil { - nilAndPanic(nil, responder, nil, t) + nilAndPanic(t, nil, responder, nil) } }) } @@ -453,12 +461,12 @@ func FuzzStaticActThree(f *testing.F) { // Generate ActOne and send to the responder. actOne, err := initiator.GenActOne() if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Receiving ActOne should succeed, so we panic on error. if err := responder.RecvActOne(actOne); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Generate ActTwo - this is not sent to the initiator because @@ -467,7 +475,7 @@ func FuzzStaticActThree(f *testing.F) { // the appropriate state in the responder machine. _, err = responder.GenActTwo() if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Copy data into [ActThreeSize]byte. @@ -476,7 +484,7 @@ func FuzzStaticActThree(f *testing.F) { // Responder receives ActThree, should fail. if err := responder.RecvActThree(actThree); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -498,7 +506,7 @@ func FuzzStaticActTwo(f *testing.F) { // appropriate state in the initiator machine. _, err := initiator.GenActOne() if err != nil { - nilAndPanic(initiator, nil, err, t) + nilAndPanic(t, initiator, nil, err) } // Copy data into [ActTwoSize]byte. @@ -507,7 +515,7 @@ func FuzzStaticActTwo(f *testing.F) { // Initiator receives ActTwo, should fail. if err := initiator.RecvActTwo(actTwo); err == nil { - nilAndPanic(initiator, nil, nil, t) + nilAndPanic(t, initiator, nil, nil) } }) } @@ -520,7 +528,7 @@ func FuzzStaticInitDecrypt(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) // Create a reader with the byte array. r := bytes.NewReader(data) @@ -528,7 +536,7 @@ func FuzzStaticInitDecrypt(f *testing.F) { // Decrypt the encrypted message using ReadMessage w/ initiator // machine. if _, err := initiator.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -547,31 +555,31 @@ func FuzzStaticInitEncDec(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ initiator machine. if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ initiator machine. if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Decrypt the ciphertext using ReadMessage w/ responder // machine. plaintext, err := responder.ReadMessage(&b) if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Check that the decrypted message and the original message are // equal. if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -590,18 +598,18 @@ func FuzzStaticInitEncrypt(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ initiator machine. if err := initiator.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ initiator machine. if _, err := initiator.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } }) } @@ -614,7 +622,7 @@ func FuzzStaticRespDecrypt(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) // Create a reader with the byte array. r := bytes.NewReader(data) @@ -622,7 +630,7 @@ func FuzzStaticRespDecrypt(f *testing.F) { // Decrypt the encrypted message using ReadMessage w/ responder // machine. if _, err := responder.ReadMessage(r); err == nil { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -641,31 +649,31 @@ func FuzzStaticRespEncDec(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ responder machine. if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ responder machine. if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Decrypt the ciphertext using ReadMessage w/ initiator // machine. plaintext, err := initiator.ReadMessage(&b) if err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Check that the decrypted message and the original message are // equal. if !bytes.Equal(data, plaintext) { - nilAndPanic(initiator, responder, nil, t) + nilAndPanic(t, initiator, responder, nil) } }) } @@ -684,18 +692,18 @@ func FuzzStaticRespEncrypt(f *testing.F) { initiator, responder := getStaticBrontideMachines() // Complete the brontide handshake. - completeHandshake(initiator, responder, t) + completeHandshake(t, initiator, responder) var b bytes.Buffer // Encrypt the message using WriteMessage w/ responder machine. if err := responder.WriteMessage(data); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } // Flush the encrypted message w/ responder machine. if _, err := responder.Flush(&b); err != nil { - nilAndPanic(initiator, responder, err, t) + nilAndPanic(t, initiator, responder, err) } }) } diff --git a/lnwire/fuzz_test.go b/lnwire/fuzz_test.go index 95f6b5314..ed7088280 100644 --- a/lnwire/fuzz_test.go +++ b/lnwire/fuzz_test.go @@ -14,6 +14,7 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { var prefixBytes [2]byte binary.BigEndian.PutUint16(prefixBytes[:], uint16(prefix)) data = append(prefixBytes[:], data...) + return data } @@ -22,6 +23,8 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { // checks, is a valid message once deserialized, and passes a sequence of // serialization and deserialization checks. func harness(t *testing.T, data []byte) { + t.Helper() + // Create a reader with the byte array. r := bytes.NewReader(data) @@ -55,7 +58,8 @@ func harness(t *testing.T, data []byte) { if !reflect.DeepEqual(msg, newMsg) { // Deserialized message and original message are not deeply // equal. - t.Fatal("original message and deserialized message are not deeply equal") + t.Fatal("original message and deserialized message are not " + + "deeply equal") } } @@ -99,10 +103,18 @@ func FuzzAcceptChannel(f *testing.F) { // probably takes up more branches than necessary, but that's // fine for now. var shouldPanic bool - first := msg.(*AcceptChannel) - second := newMsg.(*AcceptChannel) + first, ok := msg.(*AcceptChannel) + if !ok { + t.Fatal("message was not AcceptChannel") + } + second, ok := newMsg.(*AcceptChannel) + if !ok { + t.Fatal("new message was not AcceptChannel") + } + + if !bytes.Equal(first.PendingChannelID[:], + second.PendingChannelID[:]) { - if !bytes.Equal(first.PendingChannelID[:], second.PendingChannelID[:]) { shouldPanic = true } @@ -146,7 +158,9 @@ func FuzzAcceptChannel(f *testing.F) { shouldPanic = true } - if !first.DelayedPaymentPoint.IsEqual(second.DelayedPaymentPoint) { + if !first.DelayedPaymentPoint.IsEqual( + second.DelayedPaymentPoint) { + shouldPanic = true } @@ -154,16 +168,21 @@ func FuzzAcceptChannel(f *testing.F) { shouldPanic = true } - if !first.FirstCommitmentPoint.IsEqual(second.FirstCommitmentPoint) { + if !first.FirstCommitmentPoint.IsEqual( + second.FirstCommitmentPoint) { + shouldPanic = true } - if !bytes.Equal(first.UpfrontShutdownScript, second.UpfrontShutdownScript) { + if !bytes.Equal(first.UpfrontShutdownScript, + second.UpfrontShutdownScript) { + shouldPanic = true } if shouldPanic { - t.Fatalf("original message and deseralized message are not equal") + t.Fatal("original message and deseralized message " + + "are not equal") } }) } @@ -344,8 +363,15 @@ func FuzzNodeAnnouncement(f *testing.F) { // Now compare every field instead of using reflect.DeepEqual // for the Addresses field. var shouldPanic bool - first := msg.(*NodeAnnouncement) - second := newMsg.(*NodeAnnouncement) + first, ok := msg.(*NodeAnnouncement) + if !ok { + t.Fatal("message was not NodeAnnouncement") + } + second, ok := newMsg.(*NodeAnnouncement) + if !ok { + t.Fatal("new message was not NodeAnnouncement") + } + if !bytes.Equal(first.Signature[:], second.Signature[:]) { shouldPanic = true } @@ -375,18 +401,23 @@ func FuzzNodeAnnouncement(f *testing.F) { } for i := range first.Addresses { - if first.Addresses[i].String() != second.Addresses[i].String() { + if first.Addresses[i].String() != + second.Addresses[i].String() { + shouldPanic = true break } } - if !reflect.DeepEqual(first.ExtraOpaqueData, second.ExtraOpaqueData) { + if !reflect.DeepEqual(first.ExtraOpaqueData, + second.ExtraOpaqueData) { + shouldPanic = true } if shouldPanic { - t.Fatal("original message and deserialized message are not equal") + t.Fatal("original message and deserialized message " + + "are not equal") } }) } @@ -439,14 +470,22 @@ func FuzzOpenChannel(f *testing.F) { // probably takes up more branches than necessary, but that's // fine for now. var shouldPanic bool - first := msg.(*OpenChannel) - second := newMsg.(*OpenChannel) + first, ok := msg.(*OpenChannel) + if !ok { + t.Fatal("message was not OpenChannel") + } + second, ok := newMsg.(*OpenChannel) + if !ok { + t.Fatal("new message was not OpenChannel") + } if !first.ChainHash.IsEqual(&second.ChainHash) { shouldPanic = true } - if !bytes.Equal(first.PendingChannelID[:], second.PendingChannelID[:]) { + if !bytes.Equal(first.PendingChannelID[:], + second.PendingChannelID[:]) { + shouldPanic = true } @@ -498,7 +537,9 @@ func FuzzOpenChannel(f *testing.F) { shouldPanic = true } - if !first.DelayedPaymentPoint.IsEqual(second.DelayedPaymentPoint) { + if !first.DelayedPaymentPoint.IsEqual( + second.DelayedPaymentPoint) { + shouldPanic = true } @@ -506,7 +547,9 @@ func FuzzOpenChannel(f *testing.F) { shouldPanic = true } - if !first.FirstCommitmentPoint.IsEqual(second.FirstCommitmentPoint) { + if !first.FirstCommitmentPoint.IsEqual( + second.FirstCommitmentPoint) { + shouldPanic = true } @@ -514,12 +557,15 @@ func FuzzOpenChannel(f *testing.F) { shouldPanic = true } - if !bytes.Equal(first.UpfrontShutdownScript, second.UpfrontShutdownScript) { + if !bytes.Equal(first.UpfrontShutdownScript, + second.UpfrontShutdownScript) { + shouldPanic = true } if shouldPanic { - t.Fatal("original message and deserialized message are not equal") + t.Fatal("original message and deserialized message " + + "are not equal") } }) } diff --git a/watchtower/wtwire/fuzz_test.go b/watchtower/wtwire/fuzz_test.go index b3ca44b45..06b0b3ffd 100644 --- a/watchtower/wtwire/fuzz_test.go +++ b/watchtower/wtwire/fuzz_test.go @@ -13,6 +13,7 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { var prefixBytes [2]byte binary.BigEndian.PutUint16(prefixBytes[:], uint16(prefix)) data = append(prefixBytes[:], data...) + return data } @@ -22,6 +23,8 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte { // serialization and deserialization checks. Returns an int that determines // whether the input is unique or not. func harness(t *testing.T, data []byte, emptyMsg Message) { + t.Helper() + // Create a reader with the byte array. r := bytes.NewReader(data) diff --git a/zpay32/fuzz_test.go b/zpay32/fuzz_test.go index b008a2c3c..4d3a19a9c 100644 --- a/zpay32/fuzz_test.go +++ b/zpay32/fuzz_test.go @@ -39,7 +39,7 @@ func FuzzEncode(f *testing.F) { // Initialize the static key we will be using for this fuzz // test. - testPrivKeyBytes, _ := hex.DecodeString("e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734") + testPrivKeyBytes, _ := hex.DecodeString("e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734") // nolint:lll testPrivKey, _ := btcec.PrivKeyFromBytes(testPrivKeyBytes) // Then, initialize the testMessageSigner so we can encode out @@ -47,12 +47,14 @@ func FuzzEncode(f *testing.F) { testMessageSigner := MessageSigner{ SignCompact: func(msg []byte) ([]byte, error) { hash := chainhash.HashB(msg) - sig, err := ecdsa.SignCompact(testPrivKey, hash, true) + sig, err := ecdsa.SignCompact(testPrivKey, hash, + true) if err != nil { return nil, fmt.Errorf("can't sign the "+ "message: %v", err) } + return sig, nil }, }