Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zpay32: improve fuzz tests #7723

Merged
merged 5 commits into from
Jul 19, 2023

Conversation

morehouse
Copy link
Collaborator

Even after 200+ CPU hours, the fuzzer was unable to generate any serialized invoices with a valid checksum (see lightninglabs/lnd-fuzz#5 (comment)). This PR helps the fuzzer out by appending the required checksum manually. As a result, the fuzz tests now have pretty decent coverage of both invoice decoding and encoding.

The PR also contains other minor improvements and simplifications of the fuzz tests.

Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, LGTM 🎉

zpay32/fuzz_test.go Outdated Show resolved Hide resolved
zpay32/fuzz_test.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@Crypt-iQ Crypt-iQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good need to test


// Call these functions as a sanity check to make sure the
// invoice is well-formed.
_ = inv.MinFinalCLTVExpiry()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should stay since it can let us know if the function breaks

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like there's no present benefit to doing the function calls here. There's nothing in the functions that can return an error or panic, and we don't check the return values anyway.

But its easy to drop this commit if you want.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can get rid of it, though sometimes I find this pattern useful to see if a member function gives an unexpected side effect

// invoice is well-formed.
_ = inv.MinFinalCLTVExpiry()
_ = inv.Expiry()
_, _ = Decode(data, &chaincfg.TestNet3Params)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be a good idea to get the fuzzer to randomize the chaincfg

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 7390e6d23b6fd8329956a278892451ea6f0edb5f.

zpay32/fuzz_test.go Outdated Show resolved Hide resolved
zpay32/fuzz_test.go Outdated Show resolved Hide resolved
@morehouse
Copy link
Collaborator Author

I've added 7390e6d, which lets the fuzzer choose the invoice network.

Note that this is a backwards-incompatible change with the existing seeds in lnd-fuzz. So we'll need to merge a PR over there at the same time we merge this PR, or else CI will break.

@morehouse morehouse requested a review from Crypt-iQ June 22, 2023 16:11
@lightninglabs-deploy
Copy link

@Crypt-iQ: review reminder

@Crypt-iQ
Copy link
Collaborator

can merge when tests fixed

morehouse added a commit to morehouse/lnd-fuzz that referenced this pull request Jul 18, 2023
lightningnetwork/lnd#7723 adds a byte parameter
to FuzzDecode that selects the chain to use for decoding the invoice. We
add that parameter to the existing seeds so that they continue to run
for TestNet3.
morehouse added a commit to morehouse/lnd-fuzz that referenced this pull request Jul 18, 2023
lightningnetwork/lnd#7723 changed FuzzEncode
significantly, such that the old seeds are no longer useful even if we
modified them to use the new parameters.
morehouse added a commit to morehouse/lnd-fuzz that referenced this pull request Jul 18, 2023
Generated from fuzzing with the updated FuzzDecode target from
lightningnetwork/lnd#7723.
morehouse added a commit to morehouse/lnd-fuzz that referenced this pull request Jul 18, 2023
Seeds generated after the overhaul of FuzzEncode in
lightningnetwork/lnd#7723.
@morehouse
Copy link
Collaborator Author

can merge when tests fixed

Companion PR: lightninglabs/lnd-fuzz#11

These need to be merged together to keep tests passing.

@guggero
Copy link
Collaborator

guggero commented Jul 19, 2023

@morehouse can you rebase to re-trigger the unit tests please? I merged lightninglabs/lnd-fuzz#11.

The fuzz tests call inv.MinFinalCLTVExpiry() and inv.Expiry() supposedly
to ensure the invoice is well-formed. However, those methods can never
panic or return errors and therefore provide no benefit for this
purpose.
The message signer from invoice_test.go is identical to the one created
in the fuzz test. We're already using the private key from
invoice_test.go, so we may as well use the complete message signer for
simplicity.
It is very difficult for the fuzzer to create a valid checksum for each
serialized invoice, and we were therefore unable to fuzz deeper than
invoice decoding. We can help the fuzzer generate valid serialized
invoices by calculating and appending the checksum ourselves.

We also switch to using mainnet invoices to make it easier to find valid
invoices for seeding the fuzzer. We prepend the required "lnbc" prefix
ourselves to further help the fuzzer generate valid invoices.
We add a parameter to select which network will be used for the fuzz
tests, rather than hardcoding the network.
@morehouse
Copy link
Collaborator Author

Rebased.

@guggero guggero merged commit c3cd93c into lightningnetwork:master Jul 19, 2023
17 checks passed
@morehouse morehouse deleted the fuzz_zpay32_checksum branch July 19, 2023 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants