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

[test] Rewrite rpc_wallet_encrypted_wallet_sapzkeys unit test as functional test #2725

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions src/test/librust/sapling_rpc_wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,77 +498,6 @@ BOOST_AUTO_TEST_CASE(rpc_shieldsendmany_taddr_to_sapling)
vpwallets.erase(vpwallets.begin());
}

struct RealWalletTestingSetup : public WalletTestingSetupBase
{
RealWalletTestingSetup() : WalletTestingSetupBase(CBaseChainParams::MAIN,
"test_wallet",
WalletDatabase::Create(fs::absolute("test_wallet", GetWalletDir()))) {};
};

BOOST_FIXTURE_TEST_CASE(rpc_wallet_encrypted_wallet_sapzkeys, RealWalletTestingSetup)
{
UniValue retValue;
int n = 100;

{
LOCK(m_wallet.cs_wallet);
m_wallet.SetMinVersion(FEATURE_SAPLING);
m_wallet.SetupSPKM(false);
}
vpwallets.insert(vpwallets.begin(), &m_wallet);

// wallet should currently be empty
std::set<libzcash::SaplingPaymentAddress> addrs;
m_wallet.GetSaplingPaymentAddresses(addrs);
BOOST_CHECK(addrs.empty());

// create keys
for (int i = 0; i < n; i++) {
CallRPC("getnewshieldaddress");
}

// Verify we can list the keys imported
BOOST_CHECK_NO_THROW(retValue = CallRPC("listshieldaddresses"));
UniValue arr = retValue.get_array();
BOOST_CHECK((int) arr.size() == n);

// Verify that the wallet encryption RPC is disabled
// TODO: We don't have the experimental mode to disable the encryptwallet disable.
//BOOST_CHECK_THROW(CallRPC("encryptwallet passphrase"), std::runtime_error);

// Encrypt the wallet (we can't call RPC encryptwallet as that shuts down node)
SecureString strWalletPass;
strWalletPass.reserve(100);
strWalletPass = "hello";

PushCurrentDirectory push_dir(gArgs.GetArg("-datadir","/tmp/thisshouldnothappen"));
BOOST_CHECK(m_wallet.EncryptWallet(strWalletPass));
BOOST_CHECK(m_wallet.IsCrypted());

// Verify we can still list the keys imported
BOOST_CHECK_NO_THROW(retValue = CallRPC("listshieldaddresses"));
arr = retValue.get_array();
BOOST_CHECK((int) arr.size() == n);

// Try to add a new key, but we can't as the wallet is locked
BOOST_CHECK_THROW(CallRPC("getnewshieldaddress"), std::runtime_error);

// We can't call RPC walletpassphrase as that invokes RPCRunLater which breaks tests.
// So we manually unlock.
BOOST_CHECK(m_wallet.Unlock(strWalletPass));
BOOST_CHECK(m_wallet.IsCrypted());

// Now add a key
BOOST_CHECK_NO_THROW(CallRPC("getnewshieldaddress"));

// Verify the key has been added
BOOST_CHECK_NO_THROW(retValue = CallRPC("listshieldaddresses"));
arr = retValue.get_array();
BOOST_CHECK((int) arr.size() == n+1);

vpwallets.erase(vpwallets.begin());
}

BOOST_AUTO_TEST_CASE(rpc_listshieldunspent_parameters)
{
{
Expand Down
62 changes: 62 additions & 0 deletions test/functional/sapling_wallet_encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
# Copyright (c) 2021 The PIVX developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://www.opensource.org/licenses/mit-license.php .

from test_framework.test_framework import PivxTestFramework
from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
assert_true
)

# Test encrypted wallet behaviour with Sapling addresses
class WalletSaplingTest(PivxTestFramework):

def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True

def run_test(self):
node = self.nodes[0]
node.generate(1)

# wallet should currently be empty
assert_equal(len(node.listshieldaddresses()), 0)
# create 100 keys
keys_to_generate = 100
for _ in range (keys_to_generate):
node.getnewshieldaddress()
# Verify we can list the created addresses
addr_list = node.listshieldaddresses()
assert_equal(len(addr_list), keys_to_generate)

password = "hello"
node.encryptwallet(password)
# assert that the wallet is encrypted
assert_raises_rpc_error(-15, "Error: running with an encrypted wallet, but encryptwallet was called.", node.encryptwallet, 'ff')

# now verify addresses again
addr_list_post_encryption = node.listshieldaddresses()
assert_equal(len(addr_list_post_encryption), keys_to_generate)
assert_equal(addr_list, addr_list_post_encryption)

# try to add a new key, but we can't as the wallet is locked
assert_raises_rpc_error(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.", node.getnewshieldaddress)

# now unlock the wallet and try to generate the key again
node.walletpassphrase(password, 12000)
new_addr = node.getnewshieldaddress()
assert(new_addr is not None)

# and verify that the key has been added
found = False
addr_list_post_encryption = node.listshieldaddresses()
assert_equal(len(addr_list_post_encryption), keys_to_generate + 1)
for addr in addr_list_post_encryption:
if addr == new_addr:
found = True
assert_true(found, "error: shield address not found in encrypted wallet")

if __name__ == '__main__':
WalletSaplingTest().main()
2 changes: 2 additions & 0 deletions test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
'rpc_deprecated.py', # ~ 80 sec
'interface_bitcoin_cli.py', # ~ 80 sec
'mempool_packages.py', # ~ 63 sec
'sapling_wallet_encryption.py',

# vv Tests less than 60s vv
'rpc_users.py',
Expand Down Expand Up @@ -237,6 +238,7 @@
'wallet_importmulti.py',
'wallet_import_rescan.py',
'wallet_multiwallet.py',
'sapling_wallet_encryption.py'
]

# Place the lists with the longest tests (on average) first
Expand Down