Skip to content

Commit

Permalink
Test: Use specific testing setups for wallet_zkeys_tests tests
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Jul 21, 2021
1 parent d86cd4f commit 9ae619a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
58 changes: 31 additions & 27 deletions src/test/librust/wallet_zkeys_tests.cpp
Expand Up @@ -21,7 +21,7 @@
* LoadZKeyMetadata()
*/

BOOST_FIXTURE_TEST_SUITE(wallet_zkeys_tests, WalletTestingSetup)
BOOST_AUTO_TEST_SUITE(wallet_zkeys_tests)

/**
* This test covers Sapling methods on CWallet
Expand All @@ -30,9 +30,7 @@ BOOST_FIXTURE_TEST_SUITE(wallet_zkeys_tests, WalletTestingSetup)
* LoadSaplingZKey()
* LoadSaplingZKeyMetadata()
*/
BOOST_AUTO_TEST_CASE(StoreAndLoadSaplingZkeys) {
SelectParams(CBaseChainParams::MAIN);

BOOST_FIXTURE_TEST_CASE(StoreAndLoadSaplingZkeys, TestingSetup) {
CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
LOCK(wallet.cs_wallet);
// wallet should be empty
Expand Down Expand Up @@ -133,52 +131,54 @@ BOOST_AUTO_TEST_CASE(StoreAndLoadSaplingZkeys) {
/**
* This test covers methods on CWalletDB to load/save crypted sapling z keys.
*/
BOOST_AUTO_TEST_CASE(WriteCryptedSaplingZkeyDirectToDb) {
SelectParams(CBaseChainParams::TESTNET);

BOOST_FIXTURE_TEST_CASE(WriteCryptedSaplingZkeyDirectToDb, BasicTestingSetup) {
fs::path path = fs::absolute("testWallet1", GetWalletDir());
CWallet testWallet("testWallet1", CWalletDBWrapper::Create(path));
path.make_preferred();
std::unique_ptr<CWallet> testWallet = std::make_unique<CWallet>("testWallet1", CWalletDBWrapper::Create(path));
bool fFirstRun;
BOOST_CHECK_EQUAL(testWallet.LoadWallet(fFirstRun), DB_LOAD_OK);
BOOST_CHECK(!testWallet.HasSaplingSPKM());
assert(testWallet.SetupSPKM(true));
BOOST_CHECK_EQUAL(testWallet->LoadWallet(fFirstRun), DB_LOAD_OK);
BOOST_CHECK(!testWallet->HasSaplingSPKM());
assert(testWallet->SetupSPKM(true));

// wallet should be empty
std::set<libzcash::SaplingPaymentAddress> addrs;
testWallet.GetSaplingPaymentAddresses(addrs);
testWallet->GetSaplingPaymentAddresses(addrs);
BOOST_CHECK_EQUAL(0, addrs.size());

// Add random key to the wallet
auto address = testWallet.GenerateNewSaplingZKey();
auto address = testWallet->GenerateNewSaplingZKey();

// wallet should have one key
testWallet.GetSaplingPaymentAddresses(addrs);
testWallet->GetSaplingPaymentAddresses(addrs);
BOOST_CHECK_EQUAL(1, addrs.size());

// encrypt wallet
SecureString strWalletPass;
strWalletPass.reserve(100);
strWalletPass = "hello";
BOOST_CHECK(testWallet.EncryptWallet(strWalletPass));
BOOST_CHECK(testWallet->EncryptWallet(strWalletPass));

// adding a new key will fail as the wallet is locked
BOOST_CHECK_THROW(testWallet.GenerateNewSaplingZKey(), std::runtime_error);
BOOST_CHECK_THROW(testWallet->GenerateNewSaplingZKey(), std::runtime_error);

// unlock wallet and then add
testWallet.Unlock(strWalletPass);
libzcash::SaplingPaymentAddress address2 = testWallet.GenerateNewSaplingZKey();
testWallet->Unlock(strWalletPass);
libzcash::SaplingPaymentAddress address2 = testWallet->GenerateNewSaplingZKey();

// Force db close
testWallet->Flush(true);
testWallet.reset();

// Create a new wallet from the existing wallet path
fFirstRun = false;
CWallet wallet2("testWallet1", CWalletDBWrapper::Create(path));
BOOST_CHECK_EQUAL(DB_LOAD_OK, wallet2.LoadWallet(fFirstRun));
std::unique_ptr<CWallet> wallet2 = std::make_unique<CWallet>("testWallet1", CWalletDBWrapper::Create(path));
BOOST_CHECK_EQUAL(DB_LOAD_OK, wallet2->LoadWallet(fFirstRun));

// Confirm it's not the same as the other wallet
BOOST_CHECK(&testWallet != &wallet2);
BOOST_CHECK(wallet2.HasSaplingSPKM());
BOOST_CHECK(wallet2->HasSaplingSPKM());

// wallet should have two keys
wallet2.GetSaplingPaymentAddresses(addrs);
wallet2->GetSaplingPaymentAddresses(addrs);
BOOST_CHECK_EQUAL(2, addrs.size());

//check we have entries for our payment addresses
Expand All @@ -187,16 +187,20 @@ BOOST_AUTO_TEST_CASE(WriteCryptedSaplingZkeyDirectToDb) {

// spending key is crypted, so we can't extract valid payment address
libzcash::SaplingExtendedSpendingKey keyOut;
BOOST_CHECK(!wallet2.GetSaplingExtendedSpendingKey(address, keyOut));
BOOST_CHECK(!wallet2->GetSaplingExtendedSpendingKey(address, keyOut));

// unlock wallet to get spending keys and verify payment addresses
wallet2.Unlock(strWalletPass);
wallet2->Unlock(strWalletPass);

BOOST_CHECK(wallet2.GetSaplingExtendedSpendingKey(address, keyOut));
BOOST_CHECK(wallet2->GetSaplingExtendedSpendingKey(address, keyOut));
BOOST_CHECK(address == keyOut.DefaultAddress());

BOOST_CHECK(wallet2.GetSaplingExtendedSpendingKey(address2, keyOut));
BOOST_CHECK(wallet2->GetSaplingExtendedSpendingKey(address2, keyOut));
BOOST_CHECK(address2 == keyOut.DefaultAddress());

// Force db close
wallet2->Flush(true);
wallet2.reset();
}


Expand Down
6 changes: 6 additions & 0 deletions src/wallet/db.cpp
Expand Up @@ -796,3 +796,9 @@ void CWalletDBWrapper::Flush(bool shutdown)
env->Flush(shutdown);
}
}

void CWalletDBWrapper::CloseAndReset()
{
env->Close();
env->Reset();
}
4 changes: 4 additions & 0 deletions src/wallet/db.h
Expand Up @@ -144,6 +144,10 @@ class CWalletDBWrapper
*/
void Flush(bool shutdown);

/** Close and reset.
*/
void CloseAndReset();

void IncrementUpdateCounter();
std::atomic<unsigned int> nUpdateCounter;
unsigned int nLastSeen;
Expand Down
6 changes: 2 additions & 4 deletions src/wallet/wallet.h
Expand Up @@ -737,10 +737,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
/** Get database handle used by this wallet. Ideally this function would
* not be necessary.
*/
CWalletDBWrapper& GetDBHandle() const
{
return *dbw;
}
CWalletDBWrapper* GetDBHandlePtr() const { return dbw.get(); }
CWalletDBWrapper& GetDBHandle() const { return *dbw; }

/** Get a name for this wallet for logging/debugging purposes.
*/
Expand Down

0 comments on commit 9ae619a

Please sign in to comment.