Skip to content

Commit

Permalink
Add explicit negative-test for state block and epoch block signature …
Browse files Browse the repository at this point in the history
…checking
  • Loading branch information
clemahieu committed Nov 6, 2023
1 parent 11c846d commit c9e2073
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,54 @@ TEST (ledger, fail_change_gap_previous)
ASSERT_EQ (nano::process_result::gap_previous, result1.code);
}

TEST (ledger, fail_state_bad_signature)
{
auto ctx = nano::test::context::ledger_empty ();
auto & ledger = ctx.ledger ();
auto & store = ctx.store ();
auto transaction = store.tx_begin_write ();
nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits<unsigned>::max () };
nano::block_builder builder;
auto block = builder
.state ()
.account (nano::dev::genesis_key.pub)
.previous (nano::dev::genesis->hash ())
.representative (nano::dev::genesis_key.pub)
.balance (0)
.link (nano::dev::genesis_key.pub)
.sign (nano::keypair ().prv, 0)
.work (*pool.generate (nano::dev::genesis->hash ()))
.build ();
auto result1 = ledger.process (transaction, *block);
ASSERT_EQ (nano::process_result::bad_signature, result1.code);
}

TEST (ledger, fail_epoch_bad_signature)
{
auto ctx = nano::test::context::ledger_empty ();
auto & ledger = ctx.ledger ();
auto & store = ctx.store ();
auto transaction = store.tx_begin_write ();
nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits<unsigned>::max () };
nano::block_builder builder;
auto block = builder
.state ()
.account (nano::dev::genesis_key.pub)
.previous (nano::dev::genesis->hash ())
.representative (nano::dev::genesis_key.pub)
.balance (nano::dev::constants.genesis_amount)
.link (ledger.epoch_link (nano::epoch::epoch_1))
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*pool.generate (nano::dev::genesis->hash ()))
.build_shared ();
block->signature.bytes[0] ^= 1;
auto result1 = ledger.process (transaction, *block);
ASSERT_EQ (nano::process_result::bad_signature, result1.code); // Fails epoch signature
block->signature.bytes[0] ^= 1;
auto result2 = ledger.process (transaction, *block);
ASSERT_EQ (nano::process_result::progress, result2.code); // Succeeds with epoch signature
}

TEST (ledger, fail_change_bad_signature)
{
auto ctx = nano::test::context::ledger_empty ();
Expand Down

0 comments on commit c9e2073

Please sign in to comment.