upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/selftest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/selftest.cpp')
-rw-r--r--src/selftest.cpp77
1 files changed, 76 insertions, 1 deletions
diff --git a/src/selftest.cpp b/src/selftest.cpp
index 034e1f7..9f7106f 100644
--- a/src/selftest.cpp
+++ b/src/selftest.cpp
@@ -6,10 +6,12 @@
6// and against a temporary data directory. 6// and against a temporary data directory.
7 7
8#include "headers_core.h" 8#include "headers_core.h"
9#include "btfaddr.h"
9#include "bip32.h" 10#include "bip32.h"
10#include "proxy.h" 11#include "proxy.h"
11#include "selftest.h" 12#include "selftest.h"
12#include "sockcount.h" 13#include "sockcount.h"
14#include "tor.h"
13#include "walletcmd.h" 15#include "walletcmd.h"
14 16
15extern int RunPoolStratumSelfTest(); 17extern int RunPoolStratumSelfTest();
@@ -587,6 +589,34 @@ static int RunWalletHDSelfTest()
587 "the recovery audit finds wallet.dat-only immature mining rewards") ? 0 : 1; 589 "the recovery audit finds wallet.dat-only immature mining rewards") ? 0 : 1;
588 nFail += Check(audit.nLegacyImmatureTx == 1, 590 nFail += Check(audit.nLegacyImmatureTx == 1,
589 "the recovery audit counts wallet.dat-only immature mining rewards") ? 0 : 1; 591 "the recovery audit counts wallet.dat-only immature mining rewards") ? 0 : 1;
592
593 CKey keyLegacyHD11;
594 nSchemaForLegacyAudit = nHDKeySchema;
595 nHDKeySchema = HD_SCHEMA_LEGACY;
596 if (!DeriveHDKey(11, keyLegacyHD11, strError))
597 throw std::runtime_error("legacy compatibility index 11 derivation failed: " + strError);
598 nHDKeySchema = nSchemaForLegacyAudit;
599 if (!AddKey(keyLegacyHD11))
600 throw std::runtime_error("could not store the legacy compatibility index 11 key");
601
602 std::map<unsigned int, std::vector<unsigned char> > mapLegacyProbe;
603 mapLegacyProbe[0] = keyAuditLegacyHD.GetPubKey();
604 mapLegacyProbe[11] = keyLegacyHD11.GetPubKey();
605
606 CWalletTx wtxLegacyHD11;
607 wtxLegacyHD11.vout.push_back(CTxOut(19 * COIN, CScript() << keyLegacyHD11.GetPubKey() << OP_CHECKSIG));
608 CRITICAL_BLOCK(cs_mapWallet)
609 {
610 mapWallet.clear();
611 mapWallet[wtxLegacyHD11.GetHash()] = wtxLegacyHD11;
612 }
613 nFail += Check(WalletLastUsedPubKeyIndexNext(mapLegacyProbe) == 12,
614 "restore collapses legacy hdnext to the last used compatibility index plus one") ? 0 : 1;
615
616 CRITICAL_BLOCK(cs_mapWallet)
617 mapWallet.clear();
618 nFail += Check(WalletLastUsedPubKeyIndexNext(mapLegacyProbe) == 0,
619 "restore does not persist legacy hdnext at the scan depth when no compatibility key was used") ? 0 : 1;
590 } 620 }
591 catch (const std::exception& e) 621 catch (const std::exception& e)
592 { 622 {
@@ -1505,6 +1535,14 @@ static int RunSocks5ProxySelfTest()
1505 !BtfIsTorOnionHost("relay.example") && 1535 !BtfIsTorOnionHost("relay.example") &&
1506 !BtfIsTorOnionHost(".onion"), 1536 !BtfIsTorOnionHost(".onion"),
1507 "detects .onion hosts without accepting lookalikes") ? 0 : 1; 1537 "detects .onion hosts without accepting lookalikes") ? 0 : 1;
1538 std::string onion;
1539 nFail += Check(btf::NormalizeOnionEndpoint("ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCD.onion.:8433", onion) &&
1540 onion == "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd.onion:8433",
1541 "normalizes v3 onion peer endpoint") ? 0 : 1;
1542 nFail += Check(!btf::NormalizeOnionEndpoint("short.onion:8433", onion) &&
1543 !btf::NormalizeOnionEndpoint("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd.onion:0", onion) &&
1544 !btf::NormalizeOnionEndpoint("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabc1.onion:8433", onion),
1545 "rejects malformed onion peer endpoints") ? 0 : 1;
1508 1546
1509 BtfClearSocks5Proxy(); 1547 BtfClearSocks5Proxy();
1510 nFail += Check(!BtfSocks5ProxyEnabled(), "starts disabled after clear") ? 0 : 1; 1548 nFail += Check(!BtfSocks5ProxyEnabled(), "starts disabled after clear") ? 0 : 1;
@@ -1554,6 +1592,41 @@ static int RunSocks5ProxySelfTest()
1554 return nFail == 0 ? 0 : 1; 1592 return nFail == 0 ? 0 : 1;
1555} 1593}
1556 1594
1595static int RunManagedTorSelfTest()
1596{
1597 printf("managed-tor self-test\n");
1598 int nFail = 0;
1599
1600 std::string torrc = BtfBuildManagedTorrcForTest("C:/Bitflash Managed Tor/data",
1601 "C:/Bitflash Managed Tor/onion-service",
1602 19050, 19051, 8433);
1603 nFail += Check(torrc.find("DataDirectory \"C:/Bitflash Managed Tor/data\"") != std::string::npos,
1604 "quotes the Tor data directory") ? 0 : 1;
1605 nFail += Check(torrc.find("SocksPort 127.0.0.1:19050") != std::string::npos,
1606 "binds SOCKS5 to localhost only") ? 0 : 1;
1607 nFail += Check(torrc.find("ControlPort 127.0.0.1:19051") != std::string::npos &&
1608 torrc.find("CookieAuthentication 1") != std::string::npos,
1609 "binds an authenticated Tor control port to localhost only") ? 0 : 1;
1610 nFail += Check(torrc.find("IsolateSOCKSAuth") != std::string::npos &&
1611 torrc.find("IsolateClientAddr") != std::string::npos &&
1612 torrc.find("IsolateDestAddr") != std::string::npos &&
1613 torrc.find("IsolateDestPort") != std::string::npos,
1614 "enables strict Tor stream isolation") ? 0 : 1;
1615 nFail += Check(torrc.find("HiddenServiceDir \"C:/Bitflash Managed Tor/onion-service\"") != std::string::npos,
1616 "writes a hidden service directory") ? 0 : 1;
1617 nFail += Check(torrc.find("HiddenServiceVersion 3") != std::string::npos,
1618 "requests a v3 onion service") ? 0 : 1;
1619 nFail += Check(torrc.find("HiddenServicePort 8433 127.0.0.1:8433") != std::string::npos,
1620 "maps the onion service to the Bitflash P2P listener") ? 0 : 1;
1621 nFail += Check(BtfManagedTorStatus() == "disabled",
1622 "managed Tor starts disabled") ? 0 : 1;
1623
1624 printf("%s (%d failure%s)\n", nFail == 0 ? "ALL TESTS PASSED" : "TESTS FAILED",
1625 nFail, nFail == 1 ? "" : "s");
1626 fflush(stdout);
1627 return nFail == 0 ? 0 : 1;
1628}
1629
1557int RunSelfTest(const std::string& name) 1630int RunSelfTest(const std::string& name)
1558{ 1631{
1559 AttachTerminal(); 1632 AttachTerminal();
@@ -1580,8 +1653,10 @@ int RunSelfTest(const std::string& name)
1580 return RunParseMoneySelfTest(); 1653 return RunParseMoneySelfTest();
1581 if (name == "socks5-proxy") 1654 if (name == "socks5-proxy")
1582 return RunSocks5ProxySelfTest(); 1655 return RunSocks5ProxySelfTest();
1656 if (name == "managed-tor")
1657 return RunManagedTorSelfTest();
1583 1658
1584 printf("Unknown self-test '%s'\n", name.c_str()); 1659 printf("Unknown self-test '%s'\n", name.c_str());
1585 printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-crypto, wallet-encrypt, net-message, consensus-limits, pool-stratum, payout-ledger, parse-money, socks5-proxy\n"); 1660 printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-crypto, wallet-encrypt, net-message, consensus-limits, pool-stratum, payout-ledger, parse-money, socks5-proxy, managed-tor\n");
1586 return 1; 1661 return 1;
1587} 1662}