diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index 322addc9f..00433efb6 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -1,543 +1,515 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "addrman.h" #include "test/test_bitcoin.h" -#include #include +#include #include "hash.h" #include "netbase.h" #include "random.h" -class CAddrManTest : public CAddrMan -{ +class CAddrManTest : public CAddrMan { uint64_t state; public: - CAddrManTest() - { - state = 1; - } + CAddrManTest() { state = 1; } //! Ensure that bucket placement is always the same for testing purposes. - void MakeDeterministic() - { + void MakeDeterministic() { nKey.SetNull(); insecure_rand = FastRandomContext(true); } - int RandomInt(int nMax) - { + int RandomInt(int nMax) { state = (CHashWriter(SER_GETHASH, 0) << state).GetHash().GetCheapHash(); return (unsigned int)(state % nMax); } - CAddrInfo* Find(const CNetAddr& addr, int* pnId = NULL) - { + CAddrInfo *Find(const CNetAddr &addr, int *pnId = NULL) { return CAddrMan::Find(addr, pnId); } - CAddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = NULL) - { + CAddrInfo *Create(const CAddress &addr, const CNetAddr &addrSource, + int *pnId = NULL) { return CAddrMan::Create(addr, addrSource, pnId); } - void Delete(int nId) - { - CAddrMan::Delete(nId); - } + void Delete(int nId) { CAddrMan::Delete(nId); } }; -static CNetAddr ResolveIP(const char* ip) -{ +static CNetAddr ResolveIP(const char *ip) { CNetAddr addr; - BOOST_CHECK_MESSAGE(LookupHost(ip, addr, false), strprintf("failed to resolve: %s", ip)); + BOOST_CHECK_MESSAGE(LookupHost(ip, addr, false), + strprintf("failed to resolve: %s", ip)); return addr; } -static CNetAddr ResolveIP(std::string ip) -{ +static CNetAddr ResolveIP(std::string ip) { return ResolveIP(ip.c_str()); } -static CService ResolveService(const char* ip, int port = 0) -{ +static CService ResolveService(const char *ip, int port = 0) { CService serv; - BOOST_CHECK_MESSAGE(Lookup(ip, serv, port, false), strprintf("failed to resolve: %s:%i", ip, port)); + BOOST_CHECK_MESSAGE(Lookup(ip, serv, port, false), + strprintf("failed to resolve: %s:%i", ip, port)); return serv; } -static CService ResolveService(std::string ip, int port = 0) -{ +static CService ResolveService(std::string ip, int port = 0) { return ResolveService(ip.c_str(), port); } BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(addrman_simple) -{ +BOOST_AUTO_TEST_CASE(addrman_simple) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); CNetAddr source = ResolveIP("252.2.2.2"); // Test 1: Does Addrman respond correctly when empty. BOOST_CHECK(addrman.size() == 0); CAddrInfo addr_null = addrman.Select(); BOOST_CHECK(addr_null.ToString() == "[::]:0"); // Test 2: Does Addrman::Add work as expected. CService addr1 = ResolveService("250.1.1.1", 8333); addrman.Add(CAddress(addr1, NODE_NONE), source); BOOST_CHECK(addrman.size() == 1); CAddrInfo addr_ret1 = addrman.Select(); BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333"); // Test 3: Does IP address deduplication work correctly. // Expected dup IP should not be added. CService addr1_dup = ResolveService("250.1.1.1", 8333); addrman.Add(CAddress(addr1_dup, NODE_NONE), source); BOOST_CHECK(addrman.size() == 1); - // Test 5: New table has one addr and we add a diff addr we should // have two addrs. CService addr2 = ResolveService("250.1.1.2", 8333); addrman.Add(CAddress(addr2, NODE_NONE), source); BOOST_CHECK(addrman.size() == 2); // Test 6: AddrMan::Clear() should empty the new table. addrman.Clear(); BOOST_CHECK(addrman.size() == 0); CAddrInfo addr_null2 = addrman.Select(); BOOST_CHECK(addr_null2.ToString() == "[::]:0"); } -BOOST_AUTO_TEST_CASE(addrman_ports) -{ +BOOST_AUTO_TEST_CASE(addrman_ports) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); CNetAddr source = ResolveIP("252.2.2.2"); BOOST_CHECK(addrman.size() == 0); // Test 7; Addr with same IP but diff port does not replace existing addr. CService addr1 = ResolveService("250.1.1.1", 8333); addrman.Add(CAddress(addr1, NODE_NONE), source); BOOST_CHECK(addrman.size() == 1); CService addr1_port = ResolveService("250.1.1.1", 8334); addrman.Add(CAddress(addr1_port, NODE_NONE), source); BOOST_CHECK(addrman.size() == 1); CAddrInfo addr_ret2 = addrman.Select(); BOOST_CHECK(addr_ret2.ToString() == "250.1.1.1:8333"); // Test 8: Add same IP but diff port to tried table, it doesn't get added. // Perhaps this is not ideal behavior but it is the current behavior. addrman.Good(CAddress(addr1_port, NODE_NONE)); BOOST_CHECK(addrman.size() == 1); bool newOnly = true; CAddrInfo addr_ret3 = addrman.Select(newOnly); BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333"); } - -BOOST_AUTO_TEST_CASE(addrman_select) -{ +BOOST_AUTO_TEST_CASE(addrman_select) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); CNetAddr source = ResolveIP("252.2.2.2"); // Test 9: Select from new with 1 addr in new. CService addr1 = ResolveService("250.1.1.1", 8333); addrman.Add(CAddress(addr1, NODE_NONE), source); BOOST_CHECK(addrman.size() == 1); bool newOnly = true; CAddrInfo addr_ret1 = addrman.Select(newOnly); BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333"); // Test 10: move addr to tried, select from new expected nothing returned. addrman.Good(CAddress(addr1, NODE_NONE)); BOOST_CHECK(addrman.size() == 1); CAddrInfo addr_ret2 = addrman.Select(newOnly); BOOST_CHECK(addr_ret2.ToString() == "[::]:0"); CAddrInfo addr_ret3 = addrman.Select(); BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333"); BOOST_CHECK(addrman.size() == 1); - // Add three addresses to new table. CService addr2 = ResolveService("250.3.1.1", 8333); CService addr3 = ResolveService("250.3.2.2", 9999); CService addr4 = ResolveService("250.3.3.3", 9999); addrman.Add(CAddress(addr2, NODE_NONE), ResolveService("250.3.1.1", 8333)); addrman.Add(CAddress(addr3, NODE_NONE), ResolveService("250.3.1.1", 8333)); addrman.Add(CAddress(addr4, NODE_NONE), ResolveService("250.4.1.1", 8333)); // Add three addresses to tried table. CService addr5 = ResolveService("250.4.4.4", 8333); CService addr6 = ResolveService("250.4.5.5", 7777); CService addr7 = ResolveService("250.4.6.6", 8333); addrman.Add(CAddress(addr5, NODE_NONE), ResolveService("250.3.1.1", 8333)); addrman.Good(CAddress(addr5, NODE_NONE)); addrman.Add(CAddress(addr6, NODE_NONE), ResolveService("250.3.1.1", 8333)); addrman.Good(CAddress(addr6, NODE_NONE)); addrman.Add(CAddress(addr7, NODE_NONE), ResolveService("250.1.1.3", 8333)); addrman.Good(CAddress(addr7, NODE_NONE)); // Test 11: 6 addrs + 1 addr from last test = 7. BOOST_CHECK(addrman.size() == 7); // Test 12: Select pulls from new and tried regardless of port number. BOOST_CHECK(addrman.Select().ToString() == "250.4.6.6:8333"); BOOST_CHECK(addrman.Select().ToString() == "250.3.2.2:9999"); BOOST_CHECK(addrman.Select().ToString() == "250.3.3.3:9999"); BOOST_CHECK(addrman.Select().ToString() == "250.4.4.4:8333"); } -BOOST_AUTO_TEST_CASE(addrman_new_collisions) -{ +BOOST_AUTO_TEST_CASE(addrman_new_collisions) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); CNetAddr source = ResolveIP("252.2.2.2"); BOOST_CHECK(addrman.size() == 0); for (unsigned int i = 1; i < 18; i++) { CService addr = ResolveService("250.1.1." + boost::to_string(i)); addrman.Add(CAddress(addr, NODE_NONE), source); - //Test 13: No collision in new table yet. + // Test 13: No collision in new table yet. BOOST_CHECK(addrman.size() == i); } - //Test 14: new table collision! + // Test 14: new table collision! CService addr1 = ResolveService("250.1.1.18"); addrman.Add(CAddress(addr1, NODE_NONE), source); BOOST_CHECK(addrman.size() == 17); CService addr2 = ResolveService("250.1.1.19"); addrman.Add(CAddress(addr2, NODE_NONE), source); BOOST_CHECK(addrman.size() == 18); } -BOOST_AUTO_TEST_CASE(addrman_tried_collisions) -{ +BOOST_AUTO_TEST_CASE(addrman_tried_collisions) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); CNetAddr source = ResolveIP("252.2.2.2"); BOOST_CHECK(addrman.size() == 0); for (unsigned int i = 1; i < 80; i++) { CService addr = ResolveService("250.1.1." + boost::to_string(i)); addrman.Add(CAddress(addr, NODE_NONE), source); addrman.Good(CAddress(addr, NODE_NONE)); - //Test 15: No collision in tried table yet. + // Test 15: No collision in tried table yet. BOOST_CHECK_EQUAL(addrman.size(), i); } - //Test 16: tried table collision! + // Test 16: tried table collision! CService addr1 = ResolveService("250.1.1.80"); addrman.Add(CAddress(addr1, NODE_NONE), source); BOOST_CHECK(addrman.size() == 79); CService addr2 = ResolveService("250.1.1.81"); addrman.Add(CAddress(addr2, NODE_NONE), source); BOOST_CHECK(addrman.size() == 80); } -BOOST_AUTO_TEST_CASE(addrman_find) -{ +BOOST_AUTO_TEST_CASE(addrman_find) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); BOOST_CHECK(addrman.size() == 0); CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE); CAddress addr3 = CAddress(ResolveService("251.255.2.1", 8333), NODE_NONE); CNetAddr source1 = ResolveIP("250.1.2.1"); CNetAddr source2 = ResolveIP("250.1.2.2"); addrman.Add(addr1, source1); addrman.Add(addr2, source2); addrman.Add(addr3, source1); // Test 17: ensure Find returns an IP matching what we searched on. - CAddrInfo* info1 = addrman.Find(addr1); + CAddrInfo *info1 = addrman.Find(addr1); BOOST_CHECK(info1); - if (info1) - BOOST_CHECK(info1->ToString() == "250.1.2.1:8333"); + if (info1) BOOST_CHECK(info1->ToString() == "250.1.2.1:8333"); // Test 18; Find does not discriminate by port number. - CAddrInfo* info2 = addrman.Find(addr2); + CAddrInfo *info2 = addrman.Find(addr2); BOOST_CHECK(info2); - if (info2 && info1) - BOOST_CHECK(info2->ToString() == info1->ToString()); + if (info2 && info1) BOOST_CHECK(info2->ToString() == info1->ToString()); // Test 19: Find returns another IP matching what we searched on. - CAddrInfo* info3 = addrman.Find(addr3); + CAddrInfo *info3 = addrman.Find(addr3); BOOST_CHECK(info3); - if (info3) - BOOST_CHECK(info3->ToString() == "251.255.2.1:8333"); + if (info3) BOOST_CHECK(info3->ToString() == "251.255.2.1:8333"); } -BOOST_AUTO_TEST_CASE(addrman_create) -{ +BOOST_AUTO_TEST_CASE(addrman_create) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); BOOST_CHECK(addrman.size() == 0); CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); CNetAddr source1 = ResolveIP("250.1.2.1"); int nId; - CAddrInfo* pinfo = addrman.Create(addr1, source1, &nId); + CAddrInfo *pinfo = addrman.Create(addr1, source1, &nId); // Test 20: The result should be the same as the input addr. BOOST_CHECK(pinfo->ToString() == "250.1.2.1:8333"); - CAddrInfo* info2 = addrman.Find(addr1); + CAddrInfo *info2 = addrman.Find(addr1); BOOST_CHECK(info2->ToString() == "250.1.2.1:8333"); } - -BOOST_AUTO_TEST_CASE(addrman_delete) -{ +BOOST_AUTO_TEST_CASE(addrman_delete) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); BOOST_CHECK(addrman.size() == 0); CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); CNetAddr source1 = ResolveIP("250.1.2.1"); int nId; addrman.Create(addr1, source1, &nId); // Test 21: Delete should actually delete the addr. BOOST_CHECK(addrman.size() == 1); addrman.Delete(nId); BOOST_CHECK(addrman.size() == 0); - CAddrInfo* info2 = addrman.Find(addr1); + CAddrInfo *info2 = addrman.Find(addr1); BOOST_CHECK(info2 == NULL); } -BOOST_AUTO_TEST_CASE(addrman_getaddr) -{ +BOOST_AUTO_TEST_CASE(addrman_getaddr) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); // Test 22: Sanity check, GetAddr should never return anything if addrman // is empty. BOOST_CHECK(addrman.size() == 0); std::vector vAddr1 = addrman.GetAddr(); BOOST_CHECK(vAddr1.size() == 0); CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE); addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE); addr2.nTime = GetAdjustedTime(); CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE); addr3.nTime = GetAdjustedTime(); CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE); addr4.nTime = GetAdjustedTime(); CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE); addr5.nTime = GetAdjustedTime(); CNetAddr source1 = ResolveIP("250.1.2.1"); CNetAddr source2 = ResolveIP("250.2.3.3"); // Test 23: Ensure GetAddr works with new addresses. addrman.Add(addr1, source1); addrman.Add(addr2, source2); addrman.Add(addr3, source1); addrman.Add(addr4, source2); addrman.Add(addr5, source1); // GetAddr returns 23% of addresses, 23% of 5 is 1 rounded down. - BOOST_CHECK(addrman.GetAddr().size() == 1); + BOOST_CHECK(addrman.GetAddr().size() == 1); // Test 24: Ensure GetAddr works with new and tried addresses. addrman.Good(CAddress(addr1, NODE_NONE)); addrman.Good(CAddress(addr2, NODE_NONE)); BOOST_CHECK(addrman.GetAddr().size() == 1); // Test 25: Ensure GetAddr still returns 23% when addrman has many addrs. for (unsigned int i = 1; i < (8 * 256); i++) { int octet1 = i % 256; int octet2 = (i / 256) % 256; int octet3 = (i / (256 * 2)) % 256; - std::string strAddr = boost::to_string(octet1) + "." + boost::to_string(octet2) + "." + boost::to_string(octet3) + ".23"; + std::string strAddr = boost::to_string(octet1) + "." + + boost::to_string(octet2) + "." + + boost::to_string(octet3) + ".23"; CAddress addr = CAddress(ResolveService(strAddr), NODE_NONE); - + // Ensure that for all addrs in addrman, isTerrible == false. addr.nTime = GetAdjustedTime(); addrman.Add(addr, ResolveIP(strAddr)); - if (i % 8 == 0) - addrman.Good(addr); + if (i % 8 == 0) addrman.Good(addr); } std::vector vAddr = addrman.GetAddr(); size_t percent23 = (addrman.size() * 23) / 100; BOOST_CHECK(vAddr.size() == percent23); BOOST_CHECK(vAddr.size() == 461); // (Addrman.size() < number of addresses added) due to address collisons. BOOST_CHECK(addrman.size() == 2007); } - -BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) -{ +BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE); CNetAddr source1 = ResolveIP("250.1.1.1"); - CAddrInfo info1 = CAddrInfo(addr1, source1); uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash(); uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash(); - BOOST_CHECK(info1.GetTriedBucket(nKey1) == 40); // Test 26: Make sure key actually randomizes bucket placement. A fail on // this test could be a security issue. BOOST_CHECK(info1.GetTriedBucket(nKey1) != info1.GetTriedBucket(nKey2)); // Test 27: Two addresses with same IP but different ports can map to // different buckets because they have different keys. CAddrInfo info2 = CAddrInfo(addr2, source1); BOOST_CHECK(info1.GetKey() != info2.GetKey()); BOOST_CHECK(info1.GetTriedBucket(nKey1) != info2.GetTriedBucket(nKey1)); std::set buckets; for (int i = 0; i < 255; i++) { - CAddrInfo infoi = CAddrInfo( - CAddress(ResolveService("250.1.1." + boost::to_string(i)), NODE_NONE), - ResolveIP("250.1.1." + boost::to_string(i))); + CAddrInfo infoi = + CAddrInfo(CAddress(ResolveService("250.1.1." + boost::to_string(i)), + NODE_NONE), + ResolveIP("250.1.1." + boost::to_string(i))); int bucket = infoi.GetTriedBucket(nKey1); buckets.insert(bucket); } // Test 28: IP addresses in the same group (\16 prefix for IPv4) should // never get more than 8 buckets BOOST_CHECK(buckets.size() == 8); buckets.clear(); for (int j = 0; j < 255; j++) { CAddrInfo infoj = CAddrInfo( - CAddress(ResolveService("250." + boost::to_string(j) + ".1.1"), NODE_NONE), + CAddress(ResolveService("250." + boost::to_string(j) + ".1.1"), + NODE_NONE), ResolveIP("250." + boost::to_string(j) + ".1.1")); int bucket = infoj.GetTriedBucket(nKey1); buckets.insert(bucket); } // Test 29: IP addresses in the different groups should map to more than // 8 buckets. BOOST_CHECK(buckets.size() == 160); } -BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) -{ +BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) { CAddrManTest addrman; // Set addrman addr placement to be deterministic. addrman.MakeDeterministic(); CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE); CNetAddr source1 = ResolveIP("250.1.2.1"); CAddrInfo info1 = CAddrInfo(addr1, source1); uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash(); uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash(); BOOST_CHECK(info1.GetNewBucket(nKey1) == 786); // Test 30: Make sure key actually randomizes bucket placement. A fail on // this test could be a security issue. BOOST_CHECK(info1.GetNewBucket(nKey1) != info1.GetNewBucket(nKey2)); // Test 31: Ports should not effect bucket placement in the addr CAddrInfo info2 = CAddrInfo(addr2, source1); BOOST_CHECK(info1.GetKey() != info2.GetKey()); BOOST_CHECK(info1.GetNewBucket(nKey1) == info2.GetNewBucket(nKey1)); std::set buckets; for (int i = 0; i < 255; i++) { - CAddrInfo infoi = CAddrInfo( - CAddress(ResolveService("250.1.1." + boost::to_string(i)), NODE_NONE), - ResolveIP("250.1.1." + boost::to_string(i))); + CAddrInfo infoi = + CAddrInfo(CAddress(ResolveService("250.1.1." + boost::to_string(i)), + NODE_NONE), + ResolveIP("250.1.1." + boost::to_string(i))); int bucket = infoi.GetNewBucket(nKey1); buckets.insert(bucket); } // Test 32: IP addresses in the same group (\16 prefix for IPv4) should // always map to the same bucket. BOOST_CHECK(buckets.size() == 1); buckets.clear(); for (int j = 0; j < 4 * 255; j++) { - CAddrInfo infoj = CAddrInfo(CAddress( - ResolveService( - boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1"), NODE_NONE), + CAddrInfo infoj = CAddrInfo( + CAddress(ResolveService(boost::to_string(250 + (j / 255)) + "." + + boost::to_string(j % 256) + ".1.1"), + NODE_NONE), ResolveIP("251.4.1.1")); int bucket = infoj.GetNewBucket(nKey1); buckets.insert(bucket); } // Test 33: IP addresses in the same source groups should map to no more // than 64 buckets. BOOST_CHECK(buckets.size() <= 64); buckets.clear(); for (int p = 0; p < 255; p++) { - CAddrInfo infoj = CAddrInfo( - CAddress(ResolveService("250.1.1.1"), NODE_NONE), - ResolveIP("250." + boost::to_string(p) + ".1.1")); + CAddrInfo infoj = + CAddrInfo(CAddress(ResolveService("250.1.1.1"), NODE_NONE), + ResolveIP("250." + boost::to_string(p) + ".1.1")); int bucket = infoj.GetNewBucket(nKey1); buckets.insert(bucket); } // Test 34: IP addresses in the different source groups should map to more // than 64 buckets. BOOST_CHECK(buckets.size() > 64); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/allocator_tests.cpp b/src/test/allocator_tests.cpp index 3f15a0dec..ef02a38f1 100644 --- a/src/test/allocator_tests.cpp +++ b/src/test/allocator_tests.cpp @@ -1,234 +1,232 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "util.h" #include "support/allocators/secure.h" #include "test/test_bitcoin.h" #include BOOST_FIXTURE_TEST_SUITE(allocator_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(arena_tests) -{ +BOOST_AUTO_TEST_CASE(arena_tests) { // Fake memory base address for testing // without actually using memory. - void *synth_base = reinterpret_cast(0x08000000); - const size_t synth_size = 1024*1024; + void *synth_base = reinterpret_cast(0x08000000); + const size_t synth_size = 1024 * 1024; Arena b(synth_base, synth_size, 16); void *chunk = b.alloc(1000); #ifdef ARENA_DEBUG b.walk(); #endif BOOST_CHECK(chunk != nullptr); - BOOST_CHECK(b.stats().used == 1008); // Aligned to 16 - BOOST_CHECK(b.stats().total == synth_size); // Nothing has disappeared? + // Aligned to 16 + BOOST_CHECK(b.stats().used == 1008); + // Nothing has disappeared? + BOOST_CHECK(b.stats().total == synth_size); b.free(chunk); #ifdef ARENA_DEBUG b.walk(); #endif BOOST_CHECK(b.stats().used == 0); BOOST_CHECK(b.stats().free == synth_size); - try { // Test exception on double-free + try { + // Test exception on double-free b.free(chunk); BOOST_CHECK(0); - } catch(std::runtime_error &) - { + } catch (std::runtime_error &) { } void *a0 = b.alloc(128); void *a1 = b.alloc(256); void *a2 = b.alloc(512); BOOST_CHECK(b.stats().used == 896); BOOST_CHECK(b.stats().total == synth_size); #ifdef ARENA_DEBUG b.walk(); #endif b.free(a0); #ifdef ARENA_DEBUG b.walk(); #endif BOOST_CHECK(b.stats().used == 768); b.free(a1); BOOST_CHECK(b.stats().used == 512); void *a3 = b.alloc(128); #ifdef ARENA_DEBUG b.walk(); #endif BOOST_CHECK(b.stats().used == 640); b.free(a2); BOOST_CHECK(b.stats().used == 128); b.free(a3); BOOST_CHECK(b.stats().used == 0); BOOST_CHECK_EQUAL(b.stats().chunks_used, 0); BOOST_CHECK(b.stats().total == synth_size); BOOST_CHECK(b.stats().free == synth_size); BOOST_CHECK_EQUAL(b.stats().chunks_free, 1); - std::vector addr; - BOOST_CHECK(b.alloc(0) == nullptr); // allocating 0 always returns nullptr + std::vector addr; + // allocating 0 always returns nullptr + BOOST_CHECK(b.alloc(0) == nullptr); #ifdef ARENA_DEBUG b.walk(); #endif // Sweeping allocate all memory - for (int x=0; x<1024; ++x) + for (int x = 0; x < 1024; ++x) addr.push_back(b.alloc(1024)); BOOST_CHECK(b.stats().free == 0); - BOOST_CHECK(b.alloc(1024) == nullptr); // memory is full, this must return nullptr + // memory is full, this must return nullptr + BOOST_CHECK(b.alloc(1024) == nullptr); BOOST_CHECK(b.alloc(0) == nullptr); - for (int x=0; x<1024; ++x) + for (int x = 0; x < 1024; ++x) b.free(addr[x]); addr.clear(); BOOST_CHECK(b.stats().total == synth_size); BOOST_CHECK(b.stats().free == synth_size); // Now in the other direction... - for (int x=0; x<1024; ++x) + for (int x = 0; x < 1024; ++x) addr.push_back(b.alloc(1024)); - for (int x=0; x<1024; ++x) - b.free(addr[1023-x]); + for (int x = 0; x < 1024; ++x) + b.free(addr[1023 - x]); addr.clear(); // Now allocate in smaller unequal chunks, then deallocate haphazardly // Not all the chunks will succeed allocating, but freeing nullptr is // allowed so that is no problem. - for (int x=0; x<2048; ++x) - addr.push_back(b.alloc(x+1)); - for (int x=0; x<2048; ++x) - b.free(addr[((x*23)%2048)^242]); + for (int x = 0; x < 2048; ++x) + addr.push_back(b.alloc(x + 1)); + for (int x = 0; x < 2048; ++x) + b.free(addr[((x * 23) % 2048) ^ 242]); addr.clear(); - // Go entirely wild: free and alloc interleaved, - // generate targets and sizes using pseudo-randomness. - for (int x=0; x<2048; ++x) + // Go entirely wild: free and alloc interleaved, generate targets and sizes + // using pseudo-randomness. + for (int x = 0; x < 2048; ++x) addr.push_back(0); uint32_t s = 0x12345678; - for (int x=0; x<5000; ++x) { - int idx = s & (addr.size()-1); + for (int x = 0; x < 5000; ++x) { + int idx = s & (addr.size() - 1); if (s & 0x80000000) { b.free(addr[idx]); addr[idx] = 0; - } else if(!addr[idx]) { + } else if (!addr[idx]) { addr[idx] = b.alloc((s >> 16) & 2047); } bool lsb = s & 1; s >>= 1; - if (lsb) - s ^= 0xf00f00f0; // LFSR period 0xf7ffffe0 + // LFSR period 0xf7ffffe0 + if (lsb) s ^= 0xf00f00f0; } - for (void *ptr: addr) + for (void *ptr : addr) b.free(ptr); addr.clear(); BOOST_CHECK(b.stats().total == synth_size); BOOST_CHECK(b.stats().free == synth_size); } /** Mock LockedPageAllocator for testing */ -class TestLockedPageAllocator: public LockedPageAllocator -{ +class TestLockedPageAllocator : public LockedPageAllocator { public: - TestLockedPageAllocator(int count_in, int lockedcount_in): count(count_in), lockedcount(lockedcount_in) {} - void* AllocateLocked(size_t len, bool *lockingSuccess) - { + TestLockedPageAllocator(int count_in, int lockedcount_in) + : count(count_in), lockedcount(lockedcount_in) {} + void *AllocateLocked(size_t len, bool *lockingSuccess) { *lockingSuccess = false; if (count > 0) { --count; if (lockedcount > 0) { --lockedcount; *lockingSuccess = true; } - return reinterpret_cast(0x08000000 + (count<<24)); // Fake address, do not actually use this memory + // Fake address, do not actually use this memory + return reinterpret_cast(0x08000000 + (count << 24)); } return 0; } - void FreeLocked(void* addr, size_t len) - { - } - size_t GetLimit() - { - return std::numeric_limits::max(); - } + void FreeLocked(void *addr, size_t len) {} + size_t GetLimit() { return std::numeric_limits::max(); } + private: int count; int lockedcount; }; -BOOST_AUTO_TEST_CASE(lockedpool_tests_mock) -{ +BOOST_AUTO_TEST_CASE(lockedpool_tests_mock) { // Test over three virtual arenas, of which one will succeed being locked std::unique_ptr x(new TestLockedPageAllocator(3, 1)); LockedPool pool(std::move(x)); BOOST_CHECK(pool.stats().total == 0); BOOST_CHECK(pool.stats().locked == 0); // Ensure unreasonable requests are refused without allocating anything void *invalid_toosmall = pool.alloc(0); BOOST_CHECK(invalid_toosmall == nullptr); BOOST_CHECK(pool.stats().used == 0); BOOST_CHECK(pool.stats().free == 0); - void *invalid_toobig = pool.alloc(LockedPool::ARENA_SIZE+1); + void *invalid_toobig = pool.alloc(LockedPool::ARENA_SIZE + 1); BOOST_CHECK(invalid_toobig == nullptr); BOOST_CHECK(pool.stats().used == 0); BOOST_CHECK(pool.stats().free == 0); void *a0 = pool.alloc(LockedPool::ARENA_SIZE / 2); BOOST_CHECK(a0); BOOST_CHECK(pool.stats().locked == LockedPool::ARENA_SIZE); void *a1 = pool.alloc(LockedPool::ARENA_SIZE / 2); BOOST_CHECK(a1); void *a2 = pool.alloc(LockedPool::ARENA_SIZE / 2); BOOST_CHECK(a2); void *a3 = pool.alloc(LockedPool::ARENA_SIZE / 2); BOOST_CHECK(a3); void *a4 = pool.alloc(LockedPool::ARENA_SIZE / 2); BOOST_CHECK(a4); void *a5 = pool.alloc(LockedPool::ARENA_SIZE / 2); BOOST_CHECK(a5); // We've passed a count of three arenas, so this allocation should fail void *a6 = pool.alloc(16); BOOST_CHECK(!a6); pool.free(a0); pool.free(a2); pool.free(a4); pool.free(a1); pool.free(a3); pool.free(a5); - BOOST_CHECK(pool.stats().total == 3*LockedPool::ARENA_SIZE); + BOOST_CHECK(pool.stats().total == 3 * LockedPool::ARENA_SIZE); BOOST_CHECK(pool.stats().locked == LockedPool::ARENA_SIZE); BOOST_CHECK(pool.stats().used == 0); } -// These tests used the live LockedPoolManager object, this is also used -// by other tests so the conditions are somewhat less controllable and thus the +// These tests used the live LockedPoolManager object, this is also used by +// other tests so the conditions are somewhat less controllable and thus the // tests are somewhat more error-prone. -BOOST_AUTO_TEST_CASE(lockedpool_tests_live) -{ +BOOST_AUTO_TEST_CASE(lockedpool_tests_live) { LockedPoolManager &pool = LockedPoolManager::Instance(); LockedPool::Stats initial = pool.stats(); void *a0 = pool.alloc(16); BOOST_CHECK(a0); // Test reading and writing the allocated memory - *((uint32_t*)a0) = 0x1234; - BOOST_CHECK(*((uint32_t*)a0) == 0x1234); + *((uint32_t *)a0) = 0x1234; + BOOST_CHECK(*((uint32_t *)a0) == 0x1234); pool.free(a0); - try { // Test exception on double-free + try { + // Test exception on double-free pool.free(a0); BOOST_CHECK(0); - } catch(std::runtime_error &) - { + } catch (std::runtime_error &) { } - // If more than one new arena was allocated for the above tests, something is wrong + // If more than one new arena was allocated for the above tests, something + // is wrong BOOST_CHECK(pool.stats().total <= (initial.total + LockedPool::ARENA_SIZE)); // Usage must be back to where it started BOOST_CHECK(pool.stats().used == initial.used); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/amount_tests.cpp b/src/test/amount_tests.cpp index fd6f88b36..150078ce0 100644 --- a/src/test/amount_tests.cpp +++ b/src/test/amount_tests.cpp @@ -1,71 +1,72 @@ // Copyright (c) 2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "amount.h" #include "test/test_bitcoin.h" #include BOOST_FIXTURE_TEST_SUITE(amount_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(GetFeeTest) -{ +BOOST_AUTO_TEST_CASE(GetFeeTest) { CFeeRate feeRate; feeRate = CFeeRate(0); // Must always return 0 BOOST_CHECK_EQUAL(feeRate.GetFee(0), 0); BOOST_CHECK_EQUAL(feeRate.GetFee(1e5), 0); feeRate = CFeeRate(1000); // Must always just return the arg BOOST_CHECK_EQUAL(feeRate.GetFee(0), 0); BOOST_CHECK_EQUAL(feeRate.GetFee(1), 1); BOOST_CHECK_EQUAL(feeRate.GetFee(121), 121); BOOST_CHECK_EQUAL(feeRate.GetFee(999), 999); BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), 1e3); BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), 9e3); feeRate = CFeeRate(-1000); // Must always just return -1 * arg BOOST_CHECK_EQUAL(feeRate.GetFee(0), 0); BOOST_CHECK_EQUAL(feeRate.GetFee(1), -1); BOOST_CHECK_EQUAL(feeRate.GetFee(121), -121); BOOST_CHECK_EQUAL(feeRate.GetFee(999), -999); BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), -1e3); BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), -9e3); feeRate = CFeeRate(123); // Truncates the result, if not integer BOOST_CHECK_EQUAL(feeRate.GetFee(0), 0); - BOOST_CHECK_EQUAL(feeRate.GetFee(8), 1); // Special case: returns 1 instead of 0 + BOOST_CHECK_EQUAL(feeRate.GetFee(8), + 1); // Special case: returns 1 instead of 0 BOOST_CHECK_EQUAL(feeRate.GetFee(9), 1); BOOST_CHECK_EQUAL(feeRate.GetFee(121), 14); BOOST_CHECK_EQUAL(feeRate.GetFee(122), 15); BOOST_CHECK_EQUAL(feeRate.GetFee(999), 122); BOOST_CHECK_EQUAL(feeRate.GetFee(1e3), 123); BOOST_CHECK_EQUAL(feeRate.GetFee(9e3), 1107); feeRate = CFeeRate(-123); // Truncates the result, if not integer BOOST_CHECK_EQUAL(feeRate.GetFee(0), 0); - BOOST_CHECK_EQUAL(feeRate.GetFee(8), -1); // Special case: returns -1 instead of 0 + BOOST_CHECK_EQUAL(feeRate.GetFee(8), + -1); // Special case: returns -1 instead of 0 BOOST_CHECK_EQUAL(feeRate.GetFee(9), -1); // Check full constructor // default value BOOST_CHECK(CFeeRate(CAmount(-1), 1000) == CFeeRate(-1)); BOOST_CHECK(CFeeRate(CAmount(0), 1000) == CFeeRate(0)); BOOST_CHECK(CFeeRate(CAmount(1), 1000) == CFeeRate(1)); // lost precision (can only resolve satoshis per kB) BOOST_CHECK(CFeeRate(CAmount(1), 1001) == CFeeRate(0)); BOOST_CHECK(CFeeRate(CAmount(2), 1001) == CFeeRate(1)); // some more integer checks BOOST_CHECK(CFeeRate(CAmount(26), 789) == CFeeRate(32)); BOOST_CHECK(CFeeRate(CAmount(27), 789) == CFeeRate(34)); // Maximum size in bytes, should not crash CFeeRate(MAX_MONEY, std::numeric_limits::max() >> 1).GetFeePerK(); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/arith_uint256_tests.cpp b/src/test/arith_uint256_tests.cpp index 45ae7d463..269b299d1 100644 --- a/src/test/arith_uint256_tests.cpp +++ b/src/test/arith_uint256_tests.cpp @@ -1,567 +1,668 @@ // Copyright (c) 2011-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "arith_uint256.h" +#include "test/test_bitcoin.h" +#include "uint256.h" +#include "version.h" #include -#include -#include +#include #include #include -#include -#include "uint256.h" -#include "arith_uint256.h" +#include +#include #include -#include "version.h" -#include "test/test_bitcoin.h" BOOST_FIXTURE_TEST_SUITE(arith_uint256_tests, BasicTestingSetup) /// Convert vector to arith_uint256, via uint256 blob -inline arith_uint256 arith_uint256V(const std::vector& vch) -{ +inline arith_uint256 arith_uint256V(const std::vector &vch) { return UintToArith256(uint256(vch)); } const unsigned char R1Array[] = "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2" "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"; -const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; -const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256 -const arith_uint256 R1L = arith_uint256V(std::vector(R1Array,R1Array+32)); +const char R1ArrayHex[] = + "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; +const double R1Ldouble = + 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256 +const arith_uint256 R1L = + arith_uint256V(std::vector(R1Array, R1Array + 32)); const uint64_t R1LLow64 = 0x121156cfdb4a529cULL; const unsigned char R2Array[] = "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf" "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7"; -const arith_uint256 R2L = arith_uint256V(std::vector(R2Array,R2Array+32)); +const arith_uint256 R2L = + arith_uint256V(std::vector(R2Array, R2Array + 32)); -const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C"; +const char R1LplusR2L[] = + "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C"; const unsigned char ZeroArray[] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; -const arith_uint256 ZeroL = arith_uint256V(std::vector(ZeroArray,ZeroArray+32)); +const arith_uint256 ZeroL = + arith_uint256V(std::vector(ZeroArray, ZeroArray + 32)); const unsigned char OneArray[] = "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; -const arith_uint256 OneL = arith_uint256V(std::vector(OneArray,OneArray+32)); +const arith_uint256 OneL = + arith_uint256V(std::vector(OneArray, OneArray + 32)); const unsigned char MaxArray[] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; -const arith_uint256 MaxL = arith_uint256V(std::vector(MaxArray,MaxArray+32)); +const arith_uint256 MaxL = + arith_uint256V(std::vector(MaxArray, MaxArray + 32)); const arith_uint256 HalfL = (OneL << 255); -std::string ArrayToString(const unsigned char A[], unsigned int width) -{ +std::string ArrayToString(const unsigned char A[], unsigned int width) { std::stringstream Stream; Stream << std::hex; - for (unsigned int i = 0; i < width; ++i) - { - Stream<): - BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32)); - BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32)); - BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32)); - BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32)); - BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32)); - BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32)); + BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array, 32)); + BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array, 32)); + BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray, 32)); + BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray, 32)); + BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray, 32)); + BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray, 32)); // == and != BOOST_CHECK(R1L != R2L); BOOST_CHECK(ZeroL != OneL); BOOST_CHECK(OneL != ZeroL); BOOST_CHECK(MaxL != ZeroL); BOOST_CHECK(~MaxL == ZeroL); - BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L); + BOOST_CHECK(((R1L ^ R2L) ^ R1L) == R2L); uint64_t Tmp64 = 0xc4dab720d9c7acaaULL; - for (unsigned int i = 0; i < 256; ++i) - { + for (unsigned int i = 0; i < 256; ++i) { BOOST_CHECK(ZeroL != (OneL << i)); BOOST_CHECK((OneL << i) != ZeroL); BOOST_CHECK(R1L != (R1L ^ (OneL << i))); - BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 )); + BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i)) != Tmp64)); } BOOST_CHECK(ZeroL == (OneL << 256)); // String Constructor and Copy Constructor - BOOST_CHECK(arith_uint256("0x"+R1L.ToString()) == R1L); - BOOST_CHECK(arith_uint256("0x"+R2L.ToString()) == R2L); - BOOST_CHECK(arith_uint256("0x"+ZeroL.ToString()) == ZeroL); - BOOST_CHECK(arith_uint256("0x"+OneL.ToString()) == OneL); - BOOST_CHECK(arith_uint256("0x"+MaxL.ToString()) == MaxL); + BOOST_CHECK(arith_uint256("0x" + R1L.ToString()) == R1L); + BOOST_CHECK(arith_uint256("0x" + R2L.ToString()) == R2L); + BOOST_CHECK(arith_uint256("0x" + ZeroL.ToString()) == ZeroL); + BOOST_CHECK(arith_uint256("0x" + OneL.ToString()) == OneL); + BOOST_CHECK(arith_uint256("0x" + MaxL.ToString()) == MaxL); BOOST_CHECK(arith_uint256(R1L.ToString()) == R1L); - BOOST_CHECK(arith_uint256(" 0x"+R1L.ToString()+" ") == R1L); + BOOST_CHECK(arith_uint256(" 0x" + R1L.ToString() + " ") == R1L); BOOST_CHECK(arith_uint256("") == ZeroL); BOOST_CHECK(R1L == arith_uint256(R1ArrayHex)); BOOST_CHECK(arith_uint256(R1L) == R1L); - BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L); + BOOST_CHECK((arith_uint256(R1L ^ R2L) ^ R2L) == R1L); BOOST_CHECK(arith_uint256(ZeroL) == ZeroL); BOOST_CHECK(arith_uint256(OneL) == OneL); // uint64_t constructor - BOOST_CHECK( (R1L & arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64)); + BOOST_CHECK((R1L & arith_uint256("0xffffffffffffffff")) == + arith_uint256(R1LLow64)); BOOST_CHECK(ZeroL == arith_uint256(0)); BOOST_CHECK(OneL == arith_uint256(1)); - BOOST_CHECK(arith_uint256("0xffffffffffffffff") == arith_uint256(0xffffffffffffffffULL)); + BOOST_CHECK(arith_uint256("0xffffffffffffffff") == + arith_uint256(0xffffffffffffffffULL)); // Assignment (from base_uint) - arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL); - tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL); - tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L); - tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L); - tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL); + arith_uint256 tmpL = ~ZeroL; + BOOST_CHECK(tmpL == ~ZeroL); + tmpL = ~OneL; + BOOST_CHECK(tmpL == ~OneL); + tmpL = ~R1L; + BOOST_CHECK(tmpL == ~R1L); + tmpL = ~R2L; + BOOST_CHECK(tmpL == ~R2L); + tmpL = ~MaxL; + BOOST_CHECK(tmpL == ~MaxL); } -void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) -{ - for (unsigned int T=0; T < arrayLength; ++T) - { - unsigned int F = (T+bitsToShift/8); +void shiftArrayRight(unsigned char *to, const unsigned char *from, + unsigned int arrayLength, unsigned int bitsToShift) { + for (unsigned int T = 0; T < arrayLength; ++T) { + unsigned int F = (T + bitsToShift / 8); if (F < arrayLength) - to[T] = from[F] >> (bitsToShift%8); + to[T] = from[F] >> (bitsToShift % 8); else to[T] = 0; if (F + 1 < arrayLength) - to[T] |= from[(F+1)] << (8-bitsToShift%8); + to[T] |= from[(F + 1)] << (8 - bitsToShift % 8); } } -void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) -{ - for (unsigned int T=0; T < arrayLength; ++T) - { - if (T >= bitsToShift/8) - { - unsigned int F = T-bitsToShift/8; - to[T] = from[F] << (bitsToShift%8); - if (T >= bitsToShift/8+1) - to[T] |= from[F-1] >> (8-bitsToShift%8); - } - else { +void shiftArrayLeft(unsigned char *to, const unsigned char *from, + unsigned int arrayLength, unsigned int bitsToShift) { + for (unsigned int T = 0; T < arrayLength; ++T) { + if (T >= bitsToShift / 8) { + unsigned int F = T - bitsToShift / 8; + to[T] = from[F] << (bitsToShift % 8); + if (T >= bitsToShift / 8 + 1) + to[T] |= from[F - 1] >> (8 - bitsToShift % 8); + } else { to[T] = 0; } } } -BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>=" +BOOST_AUTO_TEST_CASE(shifts) { // "<<" ">>" "<<=" ">>=" unsigned char TmpArray[32]; arith_uint256 TmpL; - for (unsigned int i = 0; i < 256; ++i) - { + for (unsigned int i = 0; i < 256; ++i) { shiftArrayLeft(TmpArray, OneArray, 32, i); - BOOST_CHECK(arith_uint256V(std::vector(TmpArray,TmpArray+32)) == (OneL << i)); - TmpL = OneL; TmpL <<= i; + BOOST_CHECK(arith_uint256V(std::vector( + TmpArray, TmpArray + 32)) == (OneL << i)); + TmpL = OneL; + TmpL <<= i; BOOST_CHECK(TmpL == (OneL << i)); - BOOST_CHECK((HalfL >> (255-i)) == (OneL << i)); - TmpL = HalfL; TmpL >>= (255-i); + BOOST_CHECK((HalfL >> (255 - i)) == (OneL << i)); + TmpL = HalfL; + TmpL >>= (255 - i); BOOST_CHECK(TmpL == (OneL << i)); shiftArrayLeft(TmpArray, R1Array, 32, i); - BOOST_CHECK(arith_uint256V(std::vector(TmpArray,TmpArray+32)) == (R1L << i)); - TmpL = R1L; TmpL <<= i; + BOOST_CHECK(arith_uint256V(std::vector( + TmpArray, TmpArray + 32)) == (R1L << i)); + TmpL = R1L; + TmpL <<= i; BOOST_CHECK(TmpL == (R1L << i)); shiftArrayRight(TmpArray, R1Array, 32, i); - BOOST_CHECK(arith_uint256V(std::vector(TmpArray,TmpArray+32)) == (R1L >> i)); - TmpL = R1L; TmpL >>= i; + BOOST_CHECK(arith_uint256V(std::vector( + TmpArray, TmpArray + 32)) == (R1L >> i)); + TmpL = R1L; + TmpL >>= i; BOOST_CHECK(TmpL == (R1L >> i)); shiftArrayLeft(TmpArray, MaxArray, 32, i); - BOOST_CHECK(arith_uint256V(std::vector(TmpArray,TmpArray+32)) == (MaxL << i)); - TmpL = MaxL; TmpL <<= i; + BOOST_CHECK(arith_uint256V(std::vector( + TmpArray, TmpArray + 32)) == (MaxL << i)); + TmpL = MaxL; + TmpL <<= i; BOOST_CHECK(TmpL == (MaxL << i)); shiftArrayRight(TmpArray, MaxArray, 32, i); - BOOST_CHECK(arith_uint256V(std::vector(TmpArray,TmpArray+32)) == (MaxL >> i)); - TmpL = MaxL; TmpL >>= i; + BOOST_CHECK(arith_uint256V(std::vector( + TmpArray, TmpArray + 32)) == (MaxL >> i)); + TmpL = MaxL; + TmpL >>= i; BOOST_CHECK(TmpL == (MaxL >> i)); } arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL); arith_uint256 c2L = c1L << 128; for (unsigned int i = 0; i < 128; ++i) { - BOOST_CHECK((c1L << i) == (c2L >> (128-i))); + BOOST_CHECK((c1L << i) == (c2L >> (128 - i))); } for (unsigned int i = 128; i < 256; ++i) { - BOOST_CHECK((c1L << i) == (c2L << (i-128))); + BOOST_CHECK((c1L << i) == (c2L << (i - 128))); } } -BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ - +BOOST_AUTO_TEST_CASE(unaryOperators) // ! ~ - { BOOST_CHECK(!ZeroL); BOOST_CHECK(!(!OneL)); for (unsigned int i = 0; i < 256; ++i) - BOOST_CHECK(!(!(OneL<(TmpArray,TmpArray+32)) == (~R1L)); + for (unsigned int i = 0; i < 32; ++i) { + TmpArray[i] = ~R1Array[i]; + } + BOOST_CHECK(arith_uint256V(std::vector( + TmpArray, TmpArray + 32)) == (~R1L)); BOOST_CHECK(-ZeroL == ZeroL); - BOOST_CHECK(-R1L == (~R1L)+1); + BOOST_CHECK(-R1L == (~R1L) + 1); for (unsigned int i = 0; i < 256; ++i) - BOOST_CHECK(-(OneL<(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L)); - -#define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \ - TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); - -BOOST_AUTO_TEST_CASE( bitwiseOperators ) -{ +// element of Aarray and Barray, and then converting the result into a +// arith_uint256. +#define CHECKBITWISEOPERATOR(_A_, _B_, _OP_) \ + for (unsigned int i = 0; i < 32; ++i) { \ + TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; \ + } \ + BOOST_CHECK(arith_uint256V(std::vector( \ + TmpArray, TmpArray + 32)) == (_A_##L _OP_ _B_##L)); + +#define CHECKASSIGNMENTOPERATOR(_A_, _B_, _OP_) \ + TmpL = _A_##L; \ + TmpL _OP_## = _B_##L; \ + BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); + +BOOST_AUTO_TEST_CASE(bitwiseOperators) { unsigned char TmpArray[32]; - CHECKBITWISEOPERATOR(R1,R2,|) - CHECKBITWISEOPERATOR(R1,R2,^) - CHECKBITWISEOPERATOR(R1,R2,&) - CHECKBITWISEOPERATOR(R1,Zero,|) - CHECKBITWISEOPERATOR(R1,Zero,^) - CHECKBITWISEOPERATOR(R1,Zero,&) - CHECKBITWISEOPERATOR(R1,Max,|) - CHECKBITWISEOPERATOR(R1,Max,^) - CHECKBITWISEOPERATOR(R1,Max,&) - CHECKBITWISEOPERATOR(Zero,R1,|) - CHECKBITWISEOPERATOR(Zero,R1,^) - CHECKBITWISEOPERATOR(Zero,R1,&) - CHECKBITWISEOPERATOR(Max,R1,|) - CHECKBITWISEOPERATOR(Max,R1,^) - CHECKBITWISEOPERATOR(Max,R1,&) + CHECKBITWISEOPERATOR(R1, R2, |) + CHECKBITWISEOPERATOR(R1, R2, ^) + CHECKBITWISEOPERATOR(R1, R2, &) + CHECKBITWISEOPERATOR(R1, Zero, |) + CHECKBITWISEOPERATOR(R1, Zero, ^) + CHECKBITWISEOPERATOR(R1, Zero, &) + CHECKBITWISEOPERATOR(R1, Max, |) + CHECKBITWISEOPERATOR(R1, Max, ^) + CHECKBITWISEOPERATOR(R1, Max, &) + CHECKBITWISEOPERATOR(Zero, R1, |) + CHECKBITWISEOPERATOR(Zero, R1, ^) + CHECKBITWISEOPERATOR(Zero, R1, &) + CHECKBITWISEOPERATOR(Max, R1, |) + CHECKBITWISEOPERATOR(Max, R1, ^) + CHECKBITWISEOPERATOR(Max, R1, &) arith_uint256 TmpL; - CHECKASSIGNMENTOPERATOR(R1,R2,|) - CHECKASSIGNMENTOPERATOR(R1,R2,^) - CHECKASSIGNMENTOPERATOR(R1,R2,&) - CHECKASSIGNMENTOPERATOR(R1,Zero,|) - CHECKASSIGNMENTOPERATOR(R1,Zero,^) - CHECKASSIGNMENTOPERATOR(R1,Zero,&) - CHECKASSIGNMENTOPERATOR(R1,Max,|) - CHECKASSIGNMENTOPERATOR(R1,Max,^) - CHECKASSIGNMENTOPERATOR(R1,Max,&) - CHECKASSIGNMENTOPERATOR(Zero,R1,|) - CHECKASSIGNMENTOPERATOR(Zero,R1,^) - CHECKASSIGNMENTOPERATOR(Zero,R1,&) - CHECKASSIGNMENTOPERATOR(Max,R1,|) - CHECKASSIGNMENTOPERATOR(Max,R1,^) - CHECKASSIGNMENTOPERATOR(Max,R1,&) + CHECKASSIGNMENTOPERATOR(R1, R2, |) + CHECKASSIGNMENTOPERATOR(R1, R2, ^) + CHECKASSIGNMENTOPERATOR(R1, R2, &) + CHECKASSIGNMENTOPERATOR(R1, Zero, |) + CHECKASSIGNMENTOPERATOR(R1, Zero, ^) + CHECKASSIGNMENTOPERATOR(R1, Zero, &) + CHECKASSIGNMENTOPERATOR(R1, Max, |) + CHECKASSIGNMENTOPERATOR(R1, Max, ^) + CHECKASSIGNMENTOPERATOR(R1, Max, &) + CHECKASSIGNMENTOPERATOR(Zero, R1, |) + CHECKASSIGNMENTOPERATOR(Zero, R1, ^) + CHECKASSIGNMENTOPERATOR(Zero, R1, &) + CHECKASSIGNMENTOPERATOR(Max, R1, |) + CHECKASSIGNMENTOPERATOR(Max, R1, ^) + CHECKASSIGNMENTOPERATOR(Max, R1, &) uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL; - TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64))); - TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L); - TmpL ^= 0; BOOST_CHECK(TmpL == R1L); - TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64))); + TmpL = R1L; + TmpL |= Tmp64; + BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64))); + TmpL = R1L; + TmpL |= 0; + BOOST_CHECK(TmpL == R1L); + TmpL ^= 0; + BOOST_CHECK(TmpL == R1L); + TmpL ^= Tmp64; + BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64))); } -BOOST_AUTO_TEST_CASE( comparison ) // <= >= < > +BOOST_AUTO_TEST_CASE(comparison) // <= >= < > { arith_uint256 TmpL; for (unsigned int i = 0; i < 256; ++i) { - TmpL= OneL<< i; - BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL); - BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL); + TmpL = OneL << i; + BOOST_CHECK(TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && + ZeroL <= TmpL); + BOOST_CHECK(TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL); TmpL |= R1L; - BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L)); - BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL)); - BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL)); + BOOST_CHECK(TmpL >= R1L); + BOOST_CHECK((TmpL == R1L) != (TmpL > R1L)); + BOOST_CHECK((TmpL == R1L) || !(TmpL <= R1L)); + BOOST_CHECK(R1L <= TmpL); + BOOST_CHECK((R1L == TmpL) != (R1L < TmpL)); + BOOST_CHECK((TmpL == R1L) || !(R1L >= TmpL)); + BOOST_CHECK(!(TmpL < R1L)); + BOOST_CHECK(!(R1L > TmpL)); } } -BOOST_AUTO_TEST_CASE( plusMinus ) -{ +BOOST_AUTO_TEST_CASE(plusMinus) { arith_uint256 TmpL = 0; - BOOST_CHECK(R1L+R2L == arith_uint256(R1LplusR2L)); + BOOST_CHECK(R1L + R2L == arith_uint256(R1LplusR2L)); TmpL += R1L; BOOST_CHECK(TmpL == R1L); TmpL += R2L; BOOST_CHECK(TmpL == R1L + R2L); - BOOST_CHECK(OneL+MaxL == ZeroL); - BOOST_CHECK(MaxL+OneL == ZeroL); + BOOST_CHECK(OneL + MaxL == ZeroL); + BOOST_CHECK(MaxL + OneL == ZeroL); for (unsigned int i = 1; i < 256; ++i) { - BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) ); - BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) ); - TmpL = (MaxL>>i); TmpL += OneL; - BOOST_CHECK( TmpL == (HalfL >> (i-1)) ); - TmpL = (MaxL>>i); TmpL += 1; - BOOST_CHECK( TmpL == (HalfL >> (i-1)) ); - TmpL = (MaxL>>i); - BOOST_CHECK( TmpL++ == (MaxL>>i) ); - BOOST_CHECK( TmpL == (HalfL >> (i-1))); + BOOST_CHECK((MaxL >> i) + OneL == (HalfL >> (i - 1))); + BOOST_CHECK(OneL + (MaxL >> i) == (HalfL >> (i - 1))); + TmpL = (MaxL >> i); + TmpL += OneL; + BOOST_CHECK(TmpL == (HalfL >> (i - 1))); + TmpL = (MaxL >> i); + TmpL += 1; + BOOST_CHECK(TmpL == (HalfL >> (i - 1))); + TmpL = (MaxL >> i); + BOOST_CHECK(TmpL++ == (MaxL >> i)); + BOOST_CHECK(TmpL == (HalfL >> (i - 1))); } - BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); - TmpL = arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL; - BOOST_CHECK(TmpL == arith_uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL)); - TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL); + BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == + arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); + TmpL = arith_uint256(0xbedc77e27940a7ULL); + TmpL += 0xee8d836fce66fbULL; + BOOST_CHECK(TmpL == + arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); + TmpL -= 0xee8d836fce66fbULL; + BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL); TmpL = R1L; - BOOST_CHECK(++TmpL == R1L+1); + BOOST_CHECK(++TmpL == R1L + 1); - BOOST_CHECK(R1L -(-R2L) == R1L+R2L); - BOOST_CHECK(R1L -(-OneL) == R1L+OneL); - BOOST_CHECK(R1L - OneL == R1L+(-OneL)); + BOOST_CHECK(R1L - (-R2L) == R1L + R2L); + BOOST_CHECK(R1L - (-OneL) == R1L + OneL); + BOOST_CHECK(R1L - OneL == R1L + (-OneL)); for (unsigned int i = 1; i < 256; ++i) { - BOOST_CHECK((MaxL>>i) - (-OneL) == (HalfL >> (i-1))); - BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i)); - TmpL = (HalfL >> (i-1)); - BOOST_CHECK(TmpL-- == (HalfL >> (i-1))); + BOOST_CHECK((MaxL >> i) - (-OneL) == (HalfL >> (i - 1))); + BOOST_CHECK((HalfL >> (i - 1)) - OneL == (MaxL >> i)); + TmpL = (HalfL >> (i - 1)); + BOOST_CHECK(TmpL-- == (HalfL >> (i - 1))); BOOST_CHECK(TmpL == (MaxL >> i)); - TmpL = (HalfL >> (i-1)); + TmpL = (HalfL >> (i - 1)); BOOST_CHECK(--TmpL == (MaxL >> i)); } TmpL = R1L; - BOOST_CHECK(--TmpL == R1L-1); + BOOST_CHECK(--TmpL == R1L - 1); } -BOOST_AUTO_TEST_CASE( multiply ) -{ - BOOST_CHECK((R1L * R1L).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10"); - BOOST_CHECK((R1L * R2L).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40"); +BOOST_AUTO_TEST_CASE(multiply) { + BOOST_CHECK( + (R1L * R1L).ToString() == + "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10"); + BOOST_CHECK( + (R1L * R2L).ToString() == + "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40"); BOOST_CHECK((R1L * ZeroL) == ZeroL); BOOST_CHECK((R1L * OneL) == R1L); BOOST_CHECK((R1L * MaxL) == -R1L); BOOST_CHECK((R2L * R1L) == (R1L * R2L)); - BOOST_CHECK((R2L * R2L).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100"); + BOOST_CHECK( + (R2L * R2L).ToString() == + "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100"); BOOST_CHECK((R2L * ZeroL) == ZeroL); BOOST_CHECK((R2L * OneL) == R2L); BOOST_CHECK((R2L * MaxL) == -R2L); BOOST_CHECK(MaxL * MaxL == OneL); BOOST_CHECK((R1L * 0) == 0); BOOST_CHECK((R1L * 1) == R1L); - BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4"); - BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070"); + BOOST_CHECK( + (R1L * 3).ToString() == + "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4"); + BOOST_CHECK( + (R2L * 0x87654321UL).ToString() == + "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070"); } -BOOST_AUTO_TEST_CASE( divide ) -{ +BOOST_AUTO_TEST_CASE(divide) { arith_uint256 D1L("AD7133AC1977FA2B7"); arith_uint256 D2L("ECD751716"); - BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a"); - BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a"); + BOOST_CHECK( + (R1L / D1L).ToString() == + "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a"); + BOOST_CHECK( + (R1L / D2L).ToString() == + "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a"); BOOST_CHECK(R1L / OneL == R1L); BOOST_CHECK(R1L / MaxL == ZeroL); BOOST_CHECK(MaxL / R1L == 2); BOOST_CHECK_THROW(R1L / ZeroL, uint_error); - BOOST_CHECK((R2L / D1L).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5"); - BOOST_CHECK((R2L / D2L).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4"); + BOOST_CHECK( + (R2L / D1L).ToString() == + "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5"); + BOOST_CHECK( + (R2L / D2L).ToString() == + "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4"); BOOST_CHECK(R2L / OneL == R2L); BOOST_CHECK(R2L / MaxL == ZeroL); BOOST_CHECK(MaxL / R2L == 1); BOOST_CHECK_THROW(R2L / ZeroL, uint_error); } - -bool almostEqual(double d1, double d2) -{ - return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits::epsilon(); +bool almostEqual(double d1, double d2) { + return fabs(d1 - d2) <= + 4 * fabs(d1) * std::numeric_limits::epsilon(); } -BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex size() GetLow64 GetSerializeSize, Serialize, Unserialize +BOOST_AUTO_TEST_CASE(methods) // GetHex SetHex size() GetLow64 GetSerializeSize, + // Serialize, Unserialize { BOOST_CHECK(R1L.GetHex() == R1L.ToString()); BOOST_CHECK(R2L.GetHex() == R2L.ToString()); BOOST_CHECK(OneL.GetHex() == OneL.ToString()); BOOST_CHECK(MaxL.GetHex() == MaxL.ToString()); arith_uint256 TmpL(R1L); BOOST_CHECK(TmpL == R1L); - TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L); - TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0); - TmpL.SetHex(HalfL.ToString()); BOOST_CHECK(TmpL == HalfL); + TmpL.SetHex(R2L.ToString()); + BOOST_CHECK(TmpL == R2L); + TmpL.SetHex(ZeroL.ToString()); + BOOST_CHECK(TmpL == 0); + TmpL.SetHex(HalfL.ToString()); + BOOST_CHECK(TmpL == HalfL); TmpL.SetHex(R1L.ToString()); BOOST_CHECK(R1L.size() == 32); BOOST_CHECK(R2L.size() == 32); BOOST_CHECK(ZeroL.size() == 32); BOOST_CHECK(MaxL.size() == 32); - BOOST_CHECK(R1L.GetLow64() == R1LLow64); - BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL); - BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL); + BOOST_CHECK(R1L.GetLow64() == R1LLow64); + BOOST_CHECK(HalfL.GetLow64() == 0x0000000000000000ULL); + BOOST_CHECK(OneL.GetLow64() == 0x0000000000000001ULL); - for (unsigned int i = 0; i < 255; ++i) - { - BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i)); + for (unsigned int i = 0; i < 255; ++i) { + BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0, i)); } BOOST_CHECK(ZeroL.getdouble() == 0.0); for (int i = 256; i > 53; --i) - BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i))); - uint64_t R1L64part = (R1L>>192).GetLow64(); - for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly + BOOST_CHECK( + almostEqual((R1L >> (256 - i)).getdouble(), ldexp(R1Ldouble, i))); + uint64_t R1L64part = (R1L >> 192).GetLow64(); + for (int i = 53; i > 0; + --i) // doubles can store all integers in {0,...,2^54-1} exactly { - BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i))); + BOOST_CHECK((R1L >> (256 - i)).getdouble() == + (double)(R1L64part >> (64 - i))); } } -BOOST_AUTO_TEST_CASE(bignum_SetCompact) -{ +BOOST_AUTO_TEST_CASE(bignum_SetCompact) { arith_uint256 num; bool fNegative; bool fOverflow; num.SetCompact(0, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x00123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x01003456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x02000056, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x03000000, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x04000000, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x00923456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x01803456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x02800056, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x03800000, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x04800000, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x01123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000000012"); BOOST_CHECK_EQUAL(num.GetCompact(), 0x01120000U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); // Make sure that we don't generate compacts with the 0x00800000 bit set num = 0x80; BOOST_CHECK_EQUAL(num.GetCompact(), 0x02008000U); num.SetCompact(0x01fedcba, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "000000000000000000000000000000000000000000000000000000000000007e"); BOOST_CHECK_EQUAL(num.GetCompact(true), 0x01fe0000U); BOOST_CHECK_EQUAL(fNegative, true); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x02123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000001234"); BOOST_CHECK_EQUAL(num.GetCompact(), 0x02123400U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x03123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000000123456"); BOOST_CHECK_EQUAL(num.GetCompact(), 0x03123456U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x04123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000012345600"); BOOST_CHECK_EQUAL(num.GetCompact(), 0x04123456U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x04923456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000012345600"); BOOST_CHECK_EQUAL(num.GetCompact(true), 0x04923456U); BOOST_CHECK_EQUAL(fNegative, true); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x05009234, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "0000000000000000000000000000000000000000000000000000000092340000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0x05009234U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0x20123456, &fNegative, &fOverflow); - BOOST_CHECK_EQUAL(num.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000"); + BOOST_CHECK_EQUAL( + num.GetHex(), + "1234560000000000000000000000000000000000000000000000000000000000"); BOOST_CHECK_EQUAL(num.GetCompact(), 0x20123456U); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, false); num.SetCompact(0xff123456, &fNegative, &fOverflow); BOOST_CHECK_EQUAL(fNegative, false); BOOST_CHECK_EQUAL(fOverflow, true); } - -BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage +BOOST_AUTO_TEST_CASE( + getmaxcoverage) // some more tests just to get 100% coverage { // ~R1L give a base_uint<256> BOOST_CHECK((~~R1L >> 10) == (R1L >> 10)); BOOST_CHECK((~~R1L << 10) == (R1L << 10)); BOOST_CHECK(!(~~R1L < R1L)); BOOST_CHECK(~~R1L <= R1L); BOOST_CHECK(!(~~R1L > R1L)); BOOST_CHECK(~~R1L >= R1L); BOOST_CHECK(!(R1L < ~~R1L)); BOOST_CHECK(R1L <= ~~R1L); BOOST_CHECK(!(R1L > ~~R1L)); BOOST_CHECK(R1L >= ~~R1L); BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L); BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L); - BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L); + BOOST_CHECK(~R1L != R1L); + BOOST_CHECK(R1L != ~R1L); unsigned char TmpArray[32]; - CHECKBITWISEOPERATOR(~R1,R2,|) - CHECKBITWISEOPERATOR(~R1,R2,^) - CHECKBITWISEOPERATOR(~R1,R2,&) - CHECKBITWISEOPERATOR(R1,~R2,|) - CHECKBITWISEOPERATOR(R1,~R2,^) - CHECKBITWISEOPERATOR(R1,~R2,&) + CHECKBITWISEOPERATOR(~R1, R2, |) + CHECKBITWISEOPERATOR(~R1, R2, ^) + CHECKBITWISEOPERATOR(~R1, R2, &) + CHECKBITWISEOPERATOR(R1, ~R2, |) + CHECKBITWISEOPERATOR(R1, ~R2, ^) + CHECKBITWISEOPERATOR(R1, ~R2, &) } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/base32_tests.cpp b/src/test/base32_tests.cpp index 6422b3a88..17bdab41c 100644 --- a/src/test/base32_tests.cpp +++ b/src/test/base32_tests.cpp @@ -1,25 +1,26 @@ // Copyright (c) 2012-2015 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "utilstrencodings.h" #include "test/test_bitcoin.h" +#include "utilstrencodings.h" #include BOOST_FIXTURE_TEST_SUITE(base32_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(base32_testvectors) -{ - static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"}; - static const std::string vstrOut[] = {"","my======","mzxq====","mzxw6===","mzxw6yq=","mzxw6ytb","mzxw6ytboi======"}; - for (unsigned int i=0; i BOOST_FIXTURE_TEST_SUITE(base64_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(base64_testvectors) -{ - static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"}; - static const std::string vstrOut[] = {"","Zg==","Zm8=","Zm9v","Zm9vYg==","Zm9vYmE=","Zm9vYmFy"}; - for (unsigned int i=0; i #include #include BOOST_FIXTURE_TEST_SUITE(bloom_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize) -{ +BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize) { CBloomFilter filter(3, 0.01, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")); - BOOST_CHECK_MESSAGE( filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter doesn't contain just-inserted object!"); + BOOST_CHECK_MESSAGE( + filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), + "Bloom filter doesn't contain just-inserted object!"); // One bit different in first byte - BOOST_CHECK_MESSAGE(!filter.contains(ParseHex("19108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter contains something it shouldn't!"); + BOOST_CHECK_MESSAGE( + !filter.contains(ParseHex("19108ad8ed9bb6274d3980bab5a85c048f0950c8")), + "Bloom filter contains something it shouldn't!"); filter.insert(ParseHex("b5a2c786d9ef4658287ced5914b37a1b4aa32eee")); - BOOST_CHECK_MESSAGE(filter.contains(ParseHex("b5a2c786d9ef4658287ced5914b37a1b4aa32eee")), "Bloom filter doesn't contain just-inserted object (2)!"); + BOOST_CHECK_MESSAGE( + filter.contains(ParseHex("b5a2c786d9ef4658287ced5914b37a1b4aa32eee")), + "Bloom filter doesn't contain just-inserted object (2)!"); filter.insert(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")); - BOOST_CHECK_MESSAGE(filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), "Bloom filter doesn't contain just-inserted object (3)!"); + BOOST_CHECK_MESSAGE( + filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), + "Bloom filter doesn't contain just-inserted object (3)!"); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << filter; std::vector vch = ParseHex("03614e9b050000000000000001"); std::vector expected(vch.size()); for (unsigned int i = 0; i < vch.size(); i++) expected[i] = (char)vch[i]; - BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end()); + BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), + expected.begin(), expected.end()); - BOOST_CHECK_MESSAGE( filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter doesn't contain just-inserted object!"); + BOOST_CHECK_MESSAGE( + filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), + "Bloom filter doesn't contain just-inserted object!"); filter.clear(); - BOOST_CHECK_MESSAGE( !filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter should be empty!"); + BOOST_CHECK_MESSAGE( + !filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), + "Bloom filter should be empty!"); } -BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize_with_tweak) -{ +BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize_with_tweak) { // Same test as bloom_create_insert_serialize, but we add a nTweak of 100 CBloomFilter filter(3, 0.01, 2147483649UL, BLOOM_UPDATE_ALL); filter.insert(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")); - BOOST_CHECK_MESSAGE( filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter doesn't contain just-inserted object!"); + BOOST_CHECK_MESSAGE( + filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), + "Bloom filter doesn't contain just-inserted object!"); // One bit different in first byte - BOOST_CHECK_MESSAGE(!filter.contains(ParseHex("19108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter contains something it shouldn't!"); + BOOST_CHECK_MESSAGE( + !filter.contains(ParseHex("19108ad8ed9bb6274d3980bab5a85c048f0950c8")), + "Bloom filter contains something it shouldn't!"); filter.insert(ParseHex("b5a2c786d9ef4658287ced5914b37a1b4aa32eee")); - BOOST_CHECK_MESSAGE(filter.contains(ParseHex("b5a2c786d9ef4658287ced5914b37a1b4aa32eee")), "Bloom filter doesn't contain just-inserted object (2)!"); + BOOST_CHECK_MESSAGE( + filter.contains(ParseHex("b5a2c786d9ef4658287ced5914b37a1b4aa32eee")), + "Bloom filter doesn't contain just-inserted object (2)!"); filter.insert(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")); - BOOST_CHECK_MESSAGE(filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), "Bloom filter doesn't contain just-inserted object (3)!"); + BOOST_CHECK_MESSAGE( + filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), + "Bloom filter doesn't contain just-inserted object (3)!"); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << filter; std::vector vch = ParseHex("03ce4299050000000100008001"); std::vector expected(vch.size()); for (unsigned int i = 0; i < vch.size(); i++) expected[i] = (char)vch[i]; - BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end()); + BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), + expected.begin(), expected.end()); } -BOOST_AUTO_TEST_CASE(bloom_create_insert_key) -{ - std::string strSecret = std::string("5Kg1gnAjaLfKiwhhPpGS3QfRg2m6awQvaj98JCZBZQ5SuS2F15C"); +BOOST_AUTO_TEST_CASE(bloom_create_insert_key) { + std::string strSecret = + std::string("5Kg1gnAjaLfKiwhhPpGS3QfRg2m6awQvaj98JCZBZQ5SuS2F15C"); CBitcoinSecret vchSecret; BOOST_CHECK(vchSecret.SetString(strSecret)); CKey key = vchSecret.GetKey(); CPubKey pubkey = key.GetPubKey(); std::vector vchPubKey(pubkey.begin(), pubkey.end()); CBloomFilter filter(2, 0.001, 0, BLOOM_UPDATE_ALL); filter.insert(vchPubKey); uint160 hash = pubkey.GetID(); filter.insert(std::vector(hash.begin(), hash.end())); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << filter; std::vector vch = ParseHex("038fc16b080000000000000001"); std::vector expected(vch.size()); for (unsigned int i = 0; i < vch.size(); i++) expected[i] = (char)vch[i]; - BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end()); + BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), + expected.begin(), expected.end()); } -BOOST_AUTO_TEST_CASE(bloom_match) -{ - // Random real transaction (b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b) - CDataStream stream(ParseHex("01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339ffffffff021bff3d11000000001976a91404943fdd508053c75000106d3bc6e2754dbcff1988ac2f15de00000000001976a914a266436d2965547608b9e15d9032a7b9d64fa43188ac00000000"), SER_DISK, CLIENT_VERSION); +BOOST_AUTO_TEST_CASE(bloom_match) { + // Random real transaction + // (b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b) + CDataStream stream( + ParseHex("01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e" + "88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7c" + "3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d" + "8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee51b" + "0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef8" + "7e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339ffff" + "ffff021bff3d11000000001976a91404943fdd508053c75000106d3bc6e27" + "54dbcff1988ac2f15de00000000001976a914a266436d2965547608b9e15d" + "9032a7b9d64fa43188ac00000000"), + SER_DISK, CLIENT_VERSION); CTransaction tx(deserialize, stream); - // and one which spends it (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436) - unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00}; - std::vector vch(ch, ch + sizeof(ch) -1); + // and one which spends it + // (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436) + unsigned char ch[] = { + 0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, + 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, + 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, + 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, + 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, + 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, + 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, + 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, + 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, + 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, + 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, + 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, + 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, + 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, + 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, + 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, + 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, + 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, + 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, + 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, + 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, + 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00}; + std::vector vch(ch, ch + sizeof(ch) - 1); CDataStream spendStream(vch, SER_DISK, CLIENT_VERSION); CTransaction spendingTx(deserialize, spendStream); CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(uint256S("0xb4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b")); - BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match tx hash"); + filter.insert(uint256S( + "0xb4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b")); + BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter didn't match tx hash"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // byte-reversed tx hash - filter.insert(ParseHex("6bff7fcd4f8565ef406dd5d63d4ff94f318fe82027fd4dc451b04474019f74b4")); - BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match manually serialized tx hash"); + filter.insert(ParseHex( + "6bff7fcd4f8565ef406dd5d63d4ff94f318fe82027fd4dc451b04474019f74b4")); + BOOST_CHECK_MESSAGE( + filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter didn't match manually serialized tx hash"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(ParseHex("30450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a01")); - BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match input signature"); + filter.insert(ParseHex("30450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e" + "1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba7" + "1e5aef6405b8643ac4cb7cb3c462aced7f14711a01")); + BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter didn't match input signature"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(ParseHex("046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339")); - BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match input pub key"); + filter.insert(ParseHex("046d11fee51b0e60666d5049a9101a72741df480b96ee26488a" + "4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c54" + "7957b7700ff4dfbdefe76036c339")); + BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter didn't match input pub key"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("04943fdd508053c75000106d3bc6e2754dbcff19")); - BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match output address"); - BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(spendingTx), "Simple Bloom filter didn't add output"); + BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter didn't match output address"); + BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(spendingTx), + "Simple Bloom filter didn't add output"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("a266436d2965547608b9e15d9032a7b9d64fa431")); - BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match output address"); + BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter didn't match output address"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(COutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0)); - BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match COutPoint"); + filter.insert(COutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb" + "6ba58f3bdaab65e73b7e9260b"), + 0)); + BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter didn't match COutPoint"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - COutPoint prevOutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0); + COutPoint prevOutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6" + "ba58f3bdaab65e73b7e9260b"), + 0); { std::vector data(32 + sizeof(unsigned int)); memcpy(&data[0], prevOutPoint.hash.begin(), 32); memcpy(&data[32], &prevOutPoint.n, sizeof(unsigned int)); filter.insert(data); } - BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match manually serialized COutPoint"); + BOOST_CHECK_MESSAGE( + filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter didn't match manually serialized COutPoint"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(uint256S("00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436")); - BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random tx hash"); + filter.insert(uint256S( + "00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436")); + BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter matched random tx hash"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("0000006d2965547608b9e15d9032a7b9d64fa431")); - BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random address"); + BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter matched random address"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(COutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 1)); - BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about"); + filter.insert(COutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb" + "6ba58f3bdaab65e73b7e9260b"), + 1)); + BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter matched COutPoint for an output " + "we didn't care about"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(COutPoint(uint256S("0x000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0)); - BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about"); + filter.insert(COutPoint(uint256S("0x000000d70786e899529d71dbeba91ba216982fb" + "6ba58f3bdaab65e73b7e9260b"), + 0)); + BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), + "Simple Bloom filter matched COutPoint for an output " + "we didn't care about"); } -BOOST_AUTO_TEST_CASE(merkle_block_1) -{ - // Random real block (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af) +BOOST_AUTO_TEST_CASE(merkle_block_1) { + // Random real block + // (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af) // With 9 txes CBlock block; - CDataStream stream(ParseHex("0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf930901000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0146ffffffff0100f2052a01000000434104e18f7afbe4721580e81e8414fc8c24d7cfacf254bb5c7b949450c3e997c2dc1242487a8169507b631eb3771f2b425483fb13102c4eb5d858eef260fe70fbfae0ac00000000010000000196608ccbafa16abada902780da4dc35dafd7af05fa0da08cf833575f8cf9e836000000004a493046022100dab24889213caf43ae6adc41cf1c9396c08240c199f5225acf45416330fd7dbd022100fe37900e0644bf574493a07fc5edba06dbc07c311b947520c2d514bc5725dcb401ffffffff0100f2052a010000001976a914f15d1921f52e4007b146dfa60f369ed2fc393ce288ac000000000100000001fb766c1288458c2bafcfec81e48b24d98ec706de6b8af7c4e3c29419bfacb56d000000008c493046022100f268ba165ce0ad2e6d93f089cfcd3785de5c963bb5ea6b8c1b23f1ce3e517b9f022100da7c0f21adc6c401887f2bfd1922f11d76159cbc597fbd756a23dcbb00f4d7290141042b4e8625a96127826915a5b109852636ad0da753c9e1d5606a50480cd0c40f1f8b8d898235e571fe9357d9ec842bc4bba1827daaf4de06d71844d0057707966affffffff0280969800000000001976a9146963907531db72d0ed1a0cfb471ccb63923446f388ac80d6e34c000000001976a914f0688ba1c0d1ce182c7af6741e02658c7d4dfcd388ac000000000100000002c40297f730dd7b5a99567eb8d27b78758f607507c52292d02d4031895b52f2ff010000008b483045022100f7edfd4b0aac404e5bab4fd3889e0c6c41aa8d0e6fa122316f68eddd0a65013902205b09cc8b2d56e1cd1f7f2fafd60a129ed94504c4ac7bdc67b56fe67512658b3e014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffffca5065ff9617cbcba45eb23726df6498a9b9cafed4f54cbab9d227b0035ddefb000000008a473044022068010362a13c7f9919fa832b2dee4e788f61f6f5d344a7c2a0da6ae740605658022006d1af525b9a14a35c003b78b72bd59738cd676f845d1ff3fc25049e01003614014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffff01001ec4110200000043410469ab4181eceb28985b9b4e895c13fa5e68d85761b7eee311db5addef76fa8621865134a221bd01f28ec9999ee3e021e60766e9d1f3458c115fb28650605f11c9ac000000000100000001cdaf2f758e91c514655e2dc50633d1e4c84989f8aa90a0dbc883f0d23ed5c2fa010000008b48304502207ab51be6f12a1962ba0aaaf24a20e0b69b27a94fac5adf45aa7d2d18ffd9236102210086ae728b370e5329eead9accd880d0cb070aea0c96255fae6c4f1ddcce1fd56e014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff02404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac002d3101000000001976a9141befba0cdc1ad56529371864d9f6cb042faa06b588ac000000000100000001b4a47603e71b61bc3326efd90111bf02d2f549b067f4c4a8fa183b57a0f800cb010000008a4730440220177c37f9a505c3f1a1f0ce2da777c339bd8339ffa02c7cb41f0a5804f473c9230220585b25a2ee80eb59292e52b987dad92acb0c64eced92ed9ee105ad153cdb12d001410443bd44f683467e549dae7d20d1d79cbdb6df985c6e9c029c8d0c6cb46cc1a4d3cf7923c5021b27f7a0b562ada113bc85d5fda5a1b41e87fe6e8802817cf69996ffffffff0280651406000000001976a9145505614859643ab7b547cd7f1f5e7e2a12322d3788ac00aa0271000000001976a914ea4720a7a52fc166c55ff2298e07baf70ae67e1b88ac00000000010000000586c62cd602d219bb60edb14a3e204de0705176f9022fe49a538054fb14abb49e010000008c493046022100f2bc2aba2534becbdf062eb993853a42bbbc282083d0daf9b4b585bd401aa8c9022100b1d7fd7ee0b95600db8535bbf331b19eed8d961f7a8e54159c53675d5f69df8c014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff03ad0e58ccdac3df9dc28a218bcf6f1997b0a93306faaa4b3a28ae83447b2179010000008b483045022100be12b2937179da88599e27bb31c3525097a07cdb52422d165b3ca2f2020ffcf702200971b51f853a53d644ebae9ec8f3512e442b1bcb6c315a5b491d119d10624c83014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff2acfcab629bbc8685792603762c921580030ba144af553d271716a95089e107b010000008b483045022100fa579a840ac258871365dd48cd7552f96c8eea69bd00d84f05b283a0dab311e102207e3c0ee9234814cfbb1b659b83671618f45abc1326b9edcc77d552a4f2a805c0014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffffdcdc6023bbc9944a658ddc588e61eacb737ddf0a3cd24f113b5a8634c517fcd2000000008b4830450221008d6df731df5d32267954bd7d2dda2302b74c6c2a6aa5c0ca64ecbabc1af03c75022010e55c571d65da7701ae2da1956c442df81bbf076cdbac25133f99d98a9ed34c014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffffe15557cd5ce258f479dfd6dc6514edf6d7ed5b21fcfa4a038fd69f06b83ac76e010000008b483045022023b3e0ab071eb11de2eb1cc3a67261b866f86bf6867d4558165f7c8c8aca2d86022100dc6e1f53a91de3efe8f63512850811f26284b62f850c70ca73ed5de8771fb451014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff01404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac00000000010000000166d7577163c932b4f9690ca6a80b6e4eb001f0a2fa9023df5595602aae96ed8d000000008a4730440220262b42546302dfb654a229cefc86432b89628ff259dc87edd1154535b16a67e102207b4634c020a97c3e7bbd0d4d19da6aa2269ad9dded4026e896b213d73ca4b63f014104979b82d02226b3a4597523845754d44f13639e3bf2df5e82c6aab2bdc79687368b01b1ab8b19875ae3c90d661a3d0a33161dab29934edeb36aa01976be3baf8affffffff02404b4c00000000001976a9144854e695a02af0aeacb823ccbc272134561e0a1688ac40420f00000000001976a914abee93376d6b37b5c2940655a6fcaf1c8e74237988ac0000000001000000014e3f8ef2e91349a9059cb4f01e54ab2597c1387161d3da89919f7ea6acdbb371010000008c49304602210081f3183471a5ca22307c0800226f3ef9c353069e0773ac76bb580654d56aa523022100d4c56465bdc069060846f4fbf2f6b20520b2a80b08b168b31e66ddb9c694e240014104976c79848e18251612f8940875b2b08d06e6dc73b9840e8860c066b7e87432c477e9a59a453e71e6d76d5fe34058b800a098fc1740ce3012e8fc8a00c96af966ffffffff02c0e1e400000000001976a9144134e75a6fcb6042034aab5e18570cf1f844f54788ac404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac00000000"), SER_NETWORK, PROTOCOL_VERSION); + CDataStream stream( + ParseHex( + "0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb680000000" + "0000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558" + "da2fdb261b4d4c86041b1ab1bf9309010000000100000000000000000000000000" + "00000000000000000000000000000000000000ffffffff07044c86041b0146ffff" + "ffff0100f2052a01000000434104e18f7afbe4721580e81e8414fc8c24d7cfacf2" + "54bb5c7b949450c3e997c2dc1242487a8169507b631eb3771f2b425483fb13102c" + "4eb5d858eef260fe70fbfae0ac00000000010000000196608ccbafa16abada9027" + "80da4dc35dafd7af05fa0da08cf833575f8cf9e836000000004a493046022100da" + "b24889213caf43ae6adc41cf1c9396c08240c199f5225acf45416330fd7dbd0221" + "00fe37900e0644bf574493a07fc5edba06dbc07c311b947520c2d514bc5725dcb4" + "01ffffffff0100f2052a010000001976a914f15d1921f52e4007b146dfa60f369e" + "d2fc393ce288ac000000000100000001fb766c1288458c2bafcfec81e48b24d98e" + "c706de6b8af7c4e3c29419bfacb56d000000008c493046022100f268ba165ce0ad" + "2e6d93f089cfcd3785de5c963bb5ea6b8c1b23f1ce3e517b9f022100da7c0f21ad" + "c6c401887f2bfd1922f11d76159cbc597fbd756a23dcbb00f4d7290141042b4e86" + "25a96127826915a5b109852636ad0da753c9e1d5606a50480cd0c40f1f8b8d8982" + "35e571fe9357d9ec842bc4bba1827daaf4de06d71844d0057707966affffffff02" + "80969800000000001976a9146963907531db72d0ed1a0cfb471ccb63923446f388" + "ac80d6e34c000000001976a914f0688ba1c0d1ce182c7af6741e02658c7d4dfcd3" + "88ac000000000100000002c40297f730dd7b5a99567eb8d27b78758f607507c522" + "92d02d4031895b52f2ff010000008b483045022100f7edfd4b0aac404e5bab4fd3" + "889e0c6c41aa8d0e6fa122316f68eddd0a65013902205b09cc8b2d56e1cd1f7f2f" + "afd60a129ed94504c4ac7bdc67b56fe67512658b3e014104732012cb962afa90d3" + "1b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83aba" + "f975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffffca5065ff9617cb" + "cba45eb23726df6498a9b9cafed4f54cbab9d227b0035ddefb000000008a473044" + "022068010362a13c7f9919fa832b2dee4e788f61f6f5d344a7c2a0da6ae7406056" + "58022006d1af525b9a14a35c003b78b72bd59738cd676f845d1ff3fc25049e0100" + "3614014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c342" + "3e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc" + "2d68ecffffffff01001ec4110200000043410469ab4181eceb28985b9b4e895c13" + "fa5e68d85761b7eee311db5addef76fa8621865134a221bd01f28ec9999ee3e021" + "e60766e9d1f3458c115fb28650605f11c9ac000000000100000001cdaf2f758e91" + "c514655e2dc50633d1e4c84989f8aa90a0dbc883f0d23ed5c2fa010000008b4830" + "4502207ab51be6f12a1962ba0aaaf24a20e0b69b27a94fac5adf45aa7d2d18ffd9" + "236102210086ae728b370e5329eead9accd880d0cb070aea0c96255fae6c4f1ddc" + "ce1fd56e014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d78990" + "4f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8" + "ebbb12dcd4ffffffff02404b4c00000000001976a9142b6ba7c9d796b75eef7942" + "fc9288edd37c32f5c388ac002d3101000000001976a9141befba0cdc1ad5652937" + "1864d9f6cb042faa06b588ac000000000100000001b4a47603e71b61bc3326efd9" + "0111bf02d2f549b067f4c4a8fa183b57a0f800cb010000008a4730440220177c37" + "f9a505c3f1a1f0ce2da777c339bd8339ffa02c7cb41f0a5804f473c9230220585b" + "25a2ee80eb59292e52b987dad92acb0c64eced92ed9ee105ad153cdb12d0014104" + "43bd44f683467e549dae7d20d1d79cbdb6df985c6e9c029c8d0c6cb46cc1a4d3cf" + "7923c5021b27f7a0b562ada113bc85d5fda5a1b41e87fe6e8802817cf69996ffff" + "ffff0280651406000000001976a9145505614859643ab7b547cd7f1f5e7e2a1232" + "2d3788ac00aa0271000000001976a914ea4720a7a52fc166c55ff2298e07baf70a" + "e67e1b88ac00000000010000000586c62cd602d219bb60edb14a3e204de0705176" + "f9022fe49a538054fb14abb49e010000008c493046022100f2bc2aba2534becbdf" + "062eb993853a42bbbc282083d0daf9b4b585bd401aa8c9022100b1d7fd7ee0b956" + "00db8535bbf331b19eed8d961f7a8e54159c53675d5f69df8c014104462e76fd40" + "67b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c6" + "9b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff03ad0e" + "58ccdac3df9dc28a218bcf6f1997b0a93306faaa4b3a28ae83447b217901000000" + "8b483045022100be12b2937179da88599e27bb31c3525097a07cdb52422d165b3c" + "a2f2020ffcf702200971b51f853a53d644ebae9ec8f3512e442b1bcb6c315a5b49" + "1d119d10624c83014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33" + "d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312e" + "f1c0e8ebbb12dcd4ffffffff2acfcab629bbc8685792603762c921580030ba144a" + "f553d271716a95089e107b010000008b483045022100fa579a840ac258871365dd" + "48cd7552f96c8eea69bd00d84f05b283a0dab311e102207e3c0ee9234814cfbb1b" + "659b83671618f45abc1326b9edcc77d552a4f2a805c0014104462e76fd4067b3a0" + "aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc3" + "1895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffffdcdc6023bbc9" + "944a658ddc588e61eacb737ddf0a3cd24f113b5a8634c517fcd2000000008b4830" + "450221008d6df731df5d32267954bd7d2dda2302b74c6c2a6aa5c0ca64ecbabc1a" + "f03c75022010e55c571d65da7701ae2da1956c442df81bbf076cdbac25133f99d9" + "8a9ed34c014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d78990" + "4f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8" + "ebbb12dcd4ffffffffe15557cd5ce258f479dfd6dc6514edf6d7ed5b21fcfa4a03" + "8fd69f06b83ac76e010000008b483045022023b3e0ab071eb11de2eb1cc3a67261" + "b866f86bf6867d4558165f7c8c8aca2d86022100dc6e1f53a91de3efe8f6351285" + "0811f26284b62f850c70ca73ed5de8771fb451014104462e76fd4067b3a0aa4207" + "0082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0" + "c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff01404b4c0000000000" + "1976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac00000000010000" + "000166d7577163c932b4f9690ca6a80b6e4eb001f0a2fa9023df5595602aae96ed" + "8d000000008a4730440220262b42546302dfb654a229cefc86432b89628ff259dc" + "87edd1154535b16a67e102207b4634c020a97c3e7bbd0d4d19da6aa2269ad9dded" + "4026e896b213d73ca4b63f014104979b82d02226b3a4597523845754d44f13639e" + "3bf2df5e82c6aab2bdc79687368b01b1ab8b19875ae3c90d661a3d0a33161dab29" + "934edeb36aa01976be3baf8affffffff02404b4c00000000001976a9144854e695" + "a02af0aeacb823ccbc272134561e0a1688ac40420f00000000001976a914abee93" + "376d6b37b5c2940655a6fcaf1c8e74237988ac0000000001000000014e3f8ef2e9" + "1349a9059cb4f01e54ab2597c1387161d3da89919f7ea6acdbb371010000008c49" + "304602210081f3183471a5ca22307c0800226f3ef9c353069e0773ac76bb580654" + "d56aa523022100d4c56465bdc069060846f4fbf2f6b20520b2a80b08b168b31e66" + "ddb9c694e240014104976c79848e18251612f8940875b2b08d06e6dc73b9840e88" + "60c066b7e87432c477e9a59a453e71e6d76d5fe34058b800a098fc1740ce3012e8" + "fc8a00c96af966ffffffff02c0e1e400000000001976a9144134e75a6fcb604203" + "4aab5e18570cf1f844f54788ac404b4c00000000001976a9142b6ba7c9d796b75e" + "ef7942fc9288edd37c32f5c388ac00000000"), + SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the last transaction - filter.insert(uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); + filter.insert(uint256S( + "0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); std::pair pair = merkleBlock.vMatchedTxn[0]; - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == + uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b" + "586ec87451f20")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 8); std::vector vMatched; std::vector vIndex; - BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); + BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == + block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (unsigned int i = 0; i < vMatched.size(); i++) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); // Also match the 8th transaction - filter.insert(uint256S("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); + filter.insert(uint256S( + "0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 2); BOOST_CHECK(merkleBlock.vMatchedTxn[1] == pair); - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == + uint256S("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f16" + "8809cdfae1053")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 7); - BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); + BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == + block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (unsigned int i = 0; i < vMatched.size(); i++) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); } -BOOST_AUTO_TEST_CASE(merkle_block_2) -{ - // Random real block (000000005a4ded781e667e06ceefafb71410b511fe0d5adc3e5a27ecbec34ae6) +BOOST_AUTO_TEST_CASE(merkle_block_2) { + // Random real block + // (000000005a4ded781e667e06ceefafb71410b511fe0d5adc3e5a27ecbec34ae6) // With 4 txes CBlock block; - CDataStream stream(ParseHex("0100000075616236cc2126035fadb38deb65b9102cc2c41c09cdf29fc051906800000000fe7d5e12ef0ff901f6050211249919b1c0653771832b3a80c66cea42847f0ae1d4d26e49ffff001d00f0a4410401000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d029105ffffffff0100f2052a010000004341046d8709a041d34357697dfcb30a9d05900a6294078012bf3bb09c6f9b525f1d16d5503d7905db1ada9501446ea00728668fc5719aa80be2fdfc8a858a4dbdd4fbac00000000010000000255605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c268d28350000000049483045022100aa46504baa86df8a33b1192b1b9367b4d729dc41e389f2c04f3e5c7f0559aae702205e82253a54bf5c4f65b7428551554b2045167d6d206dfe6a2e198127d3f7df1501ffffffff55605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c268d2835010000004847304402202329484c35fa9d6bb32a55a70c0982f606ce0e3634b69006138683bcd12cbb6602200c28feb1e2555c3210f1dddb299738b4ff8bbe9667b68cb8764b5ac17b7adf0001ffffffff0200e1f505000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac0000000001000000025f9a06d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c2028000000004847304402205d6058484157235b06028c30736c15613a28bdb768ee628094ca8b0030d4d6eb0220328789c9a2ec27ddaec0ad5ef58efded42e6ea17c2e1ce838f3d6913f5e95db601ffffffff5f9a06d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c2028010000004a493046022100c45af050d3cea806cedd0ab22520c53ebe63b987b8954146cdca42487b84bdd6022100b9b027716a6b59e640da50a864d6dd8a0ef24c76ce62391fa3eabaf4d2886d2d01ffffffff0200e1f505000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac000000000100000002e2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b0000000048473044022016e7a727a061ea2254a6c358376aaa617ac537eb836c77d646ebda4c748aac8b0220192ce28bf9f2c06a6467e6531e27648d2b3e2e2bae85159c9242939840295ba501ffffffffe2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b010000004a493046022100b7a1a755588d4190118936e15cd217d133b0e4a53c3c15924010d5648d8925c9022100aaef031874db2114f2d869ac2de4ae53908fbfea5b2b1862e181626bb9005c9f01ffffffff0200e1f505000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac00180d8f000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00000000"), SER_NETWORK, PROTOCOL_VERSION); + CDataStream stream( + ParseHex( + "0100000075616236cc2126035fadb38deb65b9102cc2c41c09cdf29fc051906800" + "000000fe7d5e12ef0ff901f6050211249919b1c0653771832b3a80c66cea42847f" + "0ae1d4d26e49ffff001d00f0a44104010000000100000000000000000000000000" + "00000000000000000000000000000000000000ffffffff0804ffff001d029105ff" + "ffffff0100f2052a010000004341046d8709a041d34357697dfcb30a9d05900a62" + "94078012bf3bb09c6f9b525f1d16d5503d7905db1ada9501446ea00728668fc571" + "9aa80be2fdfc8a858a4dbdd4fbac00000000010000000255605dc6f5c3dc148b6d" + "a58442b0b2cd422be385eab2ebea4119ee9c268d28350000000049483045022100" + "aa46504baa86df8a33b1192b1b9367b4d729dc41e389f2c04f3e5c7f0559aae702" + "205e82253a54bf5c4f65b7428551554b2045167d6d206dfe6a2e198127d3f7df15" + "01ffffffff55605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c" + "268d2835010000004847304402202329484c35fa9d6bb32a55a70c0982f606ce0e" + "3634b69006138683bcd12cbb6602200c28feb1e2555c3210f1dddb299738b4ff8b" + "be9667b68cb8764b5ac17b7adf0001ffffffff0200e1f505000000004341046a07" + "65b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a" + "68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d" + "8f000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad7" + "69f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cf" + "c617c0ea45afac0000000001000000025f9a06d3acdceb56be1bfeaa3e8a25e62d" + "182fa24fefe899d1c17f1dad4c2028000000004847304402205d6058484157235b" + "06028c30736c15613a28bdb768ee628094ca8b0030d4d6eb0220328789c9a2ec27" + "ddaec0ad5ef58efded42e6ea17c2e1ce838f3d6913f5e95db601ffffffff5f9a06" + "d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c202801000000" + "4a493046022100c45af050d3cea806cedd0ab22520c53ebe63b987b8954146cdca" + "42487b84bdd6022100b9b027716a6b59e640da50a864d6dd8a0ef24c76ce62391f" + "a3eabaf4d2886d2d01ffffffff0200e1f505000000004341046a0765b5865641ce" + "08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d484" + "8b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f0000000043" + "41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef1" + "70e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0c" + "ac000000000100000002e2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f1" + "19b5046ca5738a0f6b0000000048473044022016e7a727a061ea2254a6c358376a" + "aa617ac537eb836c77d646ebda4c748aac8b0220192ce28bf9f2c06a6467e6531e" + "27648d2b3e2e2bae85159c9242939840295ba501ffffffffe2274e5fea1bf29d96" + "3914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b010000004a4930460221" + "00b7a1a755588d4190118936e15cd217d133b0e4a53c3c15924010d5648d8925c9" + "022100aaef031874db2114f2d869ac2de4ae53908fbfea5b2b1862e181626bb900" + "5c9f01ffffffff0200e1f505000000004341044a656f065871a353f216ca26cef8" + "dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8d" + "d2c875a390f67c1f6c94cfc617c0ea45afac00180d8f000000004341046a0765b5" + "865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68ae" + "e3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00000000"), + SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the first transaction - filter.insert(uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); + filter.insert(uint256S( + "0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); std::pair pair = merkleBlock.vMatchedTxn[0]; - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == + uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df" + "5b47aecb93b70")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); std::vector vMatched; std::vector vIndex; - BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); + BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == + block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (unsigned int i = 0; i < vMatched.size(); i++) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); - // Match an output from the second transaction (the pubkey for address 1DZTzaBHUDM7T3QvUKBz4qXMRpkg8jsfB5) - // This should match the third transaction because it spends the output matched + // Match an output from the second transaction (the pubkey for address + // 1DZTzaBHUDM7T3QvUKBz4qXMRpkg8jsfB5) + // This should match the third transaction because it spends the output + // matched // It also matches the fourth transaction, which spends to the pubkey again - filter.insert(ParseHex("044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45af")); + filter.insert(ParseHex("044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad" + "769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875" + "a390f67c1f6c94cfc617c0ea45af")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 4); BOOST_CHECK(pair == merkleBlock.vMatchedTxn[0]); - BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == uint256S("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56ebdcacd3069a5f")); + BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == + uint256S("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56e" + "bdcacd3069a5f")); BOOST_CHECK(merkleBlock.vMatchedTxn[1].first == 1); - BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == uint256S("0x6b0f8a73a56c04b519f1883e8aafda643ba61a30bd1439969df21bea5f4e27e2")); + BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == + uint256S("0x6b0f8a73a56c04b519f1883e8aafda643ba61a30bd1439969df" + "21bea5f4e27e2")); BOOST_CHECK(merkleBlock.vMatchedTxn[2].first == 2); - BOOST_CHECK(merkleBlock.vMatchedTxn[3].second == uint256S("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d7007663ace63cddb23")); + BOOST_CHECK(merkleBlock.vMatchedTxn[3].second == + uint256S("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d70076" + "63ace63cddb23")); BOOST_CHECK(merkleBlock.vMatchedTxn[3].first == 3); - BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); + BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == + block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (unsigned int i = 0; i < vMatched.size(); i++) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); } -BOOST_AUTO_TEST_CASE(merkle_block_2_with_update_none) -{ - // Random real block (000000005a4ded781e667e06ceefafb71410b511fe0d5adc3e5a27ecbec34ae6) +BOOST_AUTO_TEST_CASE(merkle_block_2_with_update_none) { + // Random real block + // (000000005a4ded781e667e06ceefafb71410b511fe0d5adc3e5a27ecbec34ae6) // With 4 txes CBlock block; - CDataStream stream(ParseHex("0100000075616236cc2126035fadb38deb65b9102cc2c41c09cdf29fc051906800000000fe7d5e12ef0ff901f6050211249919b1c0653771832b3a80c66cea42847f0ae1d4d26e49ffff001d00f0a4410401000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d029105ffffffff0100f2052a010000004341046d8709a041d34357697dfcb30a9d05900a6294078012bf3bb09c6f9b525f1d16d5503d7905db1ada9501446ea00728668fc5719aa80be2fdfc8a858a4dbdd4fbac00000000010000000255605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c268d28350000000049483045022100aa46504baa86df8a33b1192b1b9367b4d729dc41e389f2c04f3e5c7f0559aae702205e82253a54bf5c4f65b7428551554b2045167d6d206dfe6a2e198127d3f7df1501ffffffff55605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c268d2835010000004847304402202329484c35fa9d6bb32a55a70c0982f606ce0e3634b69006138683bcd12cbb6602200c28feb1e2555c3210f1dddb299738b4ff8bbe9667b68cb8764b5ac17b7adf0001ffffffff0200e1f505000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac0000000001000000025f9a06d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c2028000000004847304402205d6058484157235b06028c30736c15613a28bdb768ee628094ca8b0030d4d6eb0220328789c9a2ec27ddaec0ad5ef58efded42e6ea17c2e1ce838f3d6913f5e95db601ffffffff5f9a06d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c2028010000004a493046022100c45af050d3cea806cedd0ab22520c53ebe63b987b8954146cdca42487b84bdd6022100b9b027716a6b59e640da50a864d6dd8a0ef24c76ce62391fa3eabaf4d2886d2d01ffffffff0200e1f505000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac000000000100000002e2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b0000000048473044022016e7a727a061ea2254a6c358376aaa617ac537eb836c77d646ebda4c748aac8b0220192ce28bf9f2c06a6467e6531e27648d2b3e2e2bae85159c9242939840295ba501ffffffffe2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b010000004a493046022100b7a1a755588d4190118936e15cd217d133b0e4a53c3c15924010d5648d8925c9022100aaef031874db2114f2d869ac2de4ae53908fbfea5b2b1862e181626bb9005c9f01ffffffff0200e1f505000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac00180d8f000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00000000"), SER_NETWORK, PROTOCOL_VERSION); + CDataStream stream( + ParseHex( + "0100000075616236cc2126035fadb38deb65b9102cc2c41c09cdf29fc051906800" + "000000fe7d5e12ef0ff901f6050211249919b1c0653771832b3a80c66cea42847f" + "0ae1d4d26e49ffff001d00f0a44104010000000100000000000000000000000000" + "00000000000000000000000000000000000000ffffffff0804ffff001d029105ff" + "ffffff0100f2052a010000004341046d8709a041d34357697dfcb30a9d05900a62" + "94078012bf3bb09c6f9b525f1d16d5503d7905db1ada9501446ea00728668fc571" + "9aa80be2fdfc8a858a4dbdd4fbac00000000010000000255605dc6f5c3dc148b6d" + "a58442b0b2cd422be385eab2ebea4119ee9c268d28350000000049483045022100" + "aa46504baa86df8a33b1192b1b9367b4d729dc41e389f2c04f3e5c7f0559aae702" + "205e82253a54bf5c4f65b7428551554b2045167d6d206dfe6a2e198127d3f7df15" + "01ffffffff55605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c" + "268d2835010000004847304402202329484c35fa9d6bb32a55a70c0982f606ce0e" + "3634b69006138683bcd12cbb6602200c28feb1e2555c3210f1dddb299738b4ff8b" + "be9667b68cb8764b5ac17b7adf0001ffffffff0200e1f505000000004341046a07" + "65b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a" + "68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d" + "8f000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad7" + "69f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cf" + "c617c0ea45afac0000000001000000025f9a06d3acdceb56be1bfeaa3e8a25e62d" + "182fa24fefe899d1c17f1dad4c2028000000004847304402205d6058484157235b" + "06028c30736c15613a28bdb768ee628094ca8b0030d4d6eb0220328789c9a2ec27" + "ddaec0ad5ef58efded42e6ea17c2e1ce838f3d6913f5e95db601ffffffff5f9a06" + "d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c202801000000" + "4a493046022100c45af050d3cea806cedd0ab22520c53ebe63b987b8954146cdca" + "42487b84bdd6022100b9b027716a6b59e640da50a864d6dd8a0ef24c76ce62391f" + "a3eabaf4d2886d2d01ffffffff0200e1f505000000004341046a0765b5865641ce" + "08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d484" + "8b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f0000000043" + "41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef1" + "70e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0c" + "ac000000000100000002e2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f1" + "19b5046ca5738a0f6b0000000048473044022016e7a727a061ea2254a6c358376a" + "aa617ac537eb836c77d646ebda4c748aac8b0220192ce28bf9f2c06a6467e6531e" + "27648d2b3e2e2bae85159c9242939840295ba501ffffffffe2274e5fea1bf29d96" + "3914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b010000004a4930460221" + "00b7a1a755588d4190118936e15cd217d133b0e4a53c3c15924010d5648d8925c9" + "022100aaef031874db2114f2d869ac2de4ae53908fbfea5b2b1862e181626bb900" + "5c9f01ffffffff0200e1f505000000004341044a656f065871a353f216ca26cef8" + "dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8d" + "d2c875a390f67c1f6c94cfc617c0ea45afac00180d8f000000004341046a0765b5" + "865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68ae" + "e3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00000000"), + SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_NONE); // Match the first transaction - filter.insert(uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); + filter.insert(uint256S( + "0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); std::pair pair = merkleBlock.vMatchedTxn[0]; - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == + uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df" + "5b47aecb93b70")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); std::vector vMatched; std::vector vIndex; - BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); + BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == + block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (unsigned int i = 0; i < vMatched.size(); i++) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); - // Match an output from the second transaction (the pubkey for address 1DZTzaBHUDM7T3QvUKBz4qXMRpkg8jsfB5) - // This should not match the third transaction though it spends the output matched - // It will match the fourth transaction, which has another pay-to-pubkey output to the same address - filter.insert(ParseHex("044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45af")); + // Match an output from the second transaction (the pubkey for address + // 1DZTzaBHUDM7T3QvUKBz4qXMRpkg8jsfB5) + // This should not match the third transaction though it spends the output + // matched + // It will match the fourth transaction, which has another pay-to-pubkey + // output to the same address + filter.insert(ParseHex("044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad" + "769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875" + "a390f67c1f6c94cfc617c0ea45af")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 3); BOOST_CHECK(pair == merkleBlock.vMatchedTxn[0]); - BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == uint256S("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56ebdcacd3069a5f")); + BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == + uint256S("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56e" + "bdcacd3069a5f")); BOOST_CHECK(merkleBlock.vMatchedTxn[1].first == 1); - BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == uint256S("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d7007663ace63cddb23")); + BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == + uint256S("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d70076" + "63ace63cddb23")); BOOST_CHECK(merkleBlock.vMatchedTxn[2].first == 3); - BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); + BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == + block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (unsigned int i = 0; i < vMatched.size(); i++) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); } -BOOST_AUTO_TEST_CASE(merkle_block_3_and_serialize) -{ - // Random real block (000000000000dab0130bbcc991d3d7ae6b81aa6f50a798888dfe62337458dc45) +BOOST_AUTO_TEST_CASE(merkle_block_3_and_serialize) { + // Random real block + // (000000000000dab0130bbcc991d3d7ae6b81aa6f50a798888dfe62337458dc45) // With one tx CBlock block; - CDataStream stream(ParseHex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d630101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08044c86041b020a02ffffffff0100f2052a01000000434104ecd3229b0571c3be876feaac0442a9f13c5a572742927af1dc623353ecf8c202225f64868137a18cdd85cbbb4c74fbccfd4f49639cf1bdc94a5672bb15ad5d4cac00000000"), SER_NETWORK, PROTOCOL_VERSION); + CDataStream stream( + ParseHex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b00" + "00000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3f" + "f60abe184f196367291b4d4c86041b8fa45d6301010000000100000000000" + "00000000000000000000000000000000000000000000000000000ffffffff" + "08044c86041b020a02ffffffff0100f2052a01000000434104ecd3229b057" + "1c3be876feaac0442a9f13c5a572742927af1dc623353ecf8c202225f6486" + "8137a18cdd85cbbb4c74fbccfd4f49639cf1bdc94a5672bb15ad5d4cac000" + "00000"), + SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the only transaction - filter.insert(uint256S("0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5")); + filter.insert(uint256S( + "0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == + uint256S("0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee" + "3a3d669c00cb5")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); std::vector vMatched; std::vector vIndex; - BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); + BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == + block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (unsigned int i = 0; i < vMatched.size(); i++) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); CDataStream merkleStream(SER_NETWORK, PROTOCOL_VERSION); merkleStream << merkleBlock; - std::vector vch = ParseHex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f19630101"); + std::vector vch = + ParseHex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b00" + "00000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3f" + "f60abe184f196367291b4d4c86041b8fa45d630100000001b50cc069d6a3e" + "33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f19630101"); std::vector expected(vch.size()); for (unsigned int i = 0; i < vch.size(); i++) expected[i] = (char)vch[i]; - BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), merkleStream.begin(), merkleStream.end()); + BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), + merkleStream.begin(), merkleStream.end()); } -BOOST_AUTO_TEST_CASE(merkle_block_4) -{ - // Random real block (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) +BOOST_AUTO_TEST_CASE(merkle_block_4) { + // Random real block + // (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) // With 7 txes CBlock block; - CDataStream stream(ParseHex("0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc880670100000000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9728776381b4d4c86041b554b85290701000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0136ffffffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea10221009253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58ccb3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9dee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf49e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fcad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff309e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af83000000004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb5428f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c810ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff0100714460030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d2253d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a473044022078124c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e930220691d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a3601410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95fffffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4dafdaa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab023abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e00847147cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd701410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c385d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758df616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11eeb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac00000000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1ac1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f00000000001976a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f00000000001976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e280007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a33ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf04176b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf80125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff0100093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888ac00000000"), SER_NETWORK, PROTOCOL_VERSION); + CDataStream stream( + ParseHex( + "0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc88067010000" + "0000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9" + "728776381b4d4c86041b554b852907010000000100000000000000000000000000" + "00000000000000000000000000000000000000ffffffff07044c86041b0136ffff" + "ffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1" + "f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad" + "1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08" + "989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e" + "834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea1022100" + "9253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901" + "ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643" + "207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58cc" + "b3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9d" + "ee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca" + "0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8" + "acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf4" + "9e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fc" + "ad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff30" + "9e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af830000" + "00004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b" + "51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb542" + "8f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6" + "d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c8" + "10ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d" + "31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff01007144" + "60030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000" + "000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72" + "a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6" + "f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423" + "746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6" + "fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd62" + "38f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d225" + "3d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a47304402207812" + "4c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e93022069" + "1d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a360141" + "0462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab8" + "44c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ff" + "fffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4daf" + "daa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab0" + "23abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7" + "fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561" + "f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d8" + "7270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9" + "e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e0084714" + "7cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b" + "2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd7014104" + "62bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844" + "c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffff" + "ffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c38" + "5d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758d" + "f616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11e" + "eb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a" + "0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f" + "464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e" + "88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636" + "030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c" + "00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac0000" + "0000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d" + "850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1a" + "c1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90" + "f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1" + "e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c" + "4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f000000000019" + "76a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f0000000000" + "1976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000" + "000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3" + "f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e2" + "80007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a" + "690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2" + "b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a3" + "3ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf041" + "76b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b" + "903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb" + "87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf8" + "0125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06" + "820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff01" + "00093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888" + "ac00000000"), + SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the last transaction - filter.insert(uint256S("0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154")); + filter.insert(uint256S( + "0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); std::pair pair = merkleBlock.vMatchedTxn[0]; - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == + uint256S("0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f" + "19b15df0ac154")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 6); std::vector vMatched; std::vector vIndex; - BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); + BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == + block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (unsigned int i = 0; i < vMatched.size(); i++) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); // Also match the 4th transaction - filter.insert(uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); + filter.insert(uint256S( + "0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 2); - BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); + BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == + uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de673" + "26471df5bc041")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 3); BOOST_CHECK(merkleBlock.vMatchedTxn[1] == pair); - BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); + BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == + block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (unsigned int i = 0; i < vMatched.size(); i++) BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); } -BOOST_AUTO_TEST_CASE(merkle_block_4_test_p2pubkey_only) -{ - // Random real block (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) +BOOST_AUTO_TEST_CASE(merkle_block_4_test_p2pubkey_only) { + // Random real block + // (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) // With 7 txes CBlock block; - CDataStream stream(ParseHex("0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc880670100000000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9728776381b4d4c86041b554b85290701000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0136ffffffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea10221009253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58ccb3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9dee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf49e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fcad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff309e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af83000000004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb5428f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c810ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff0100714460030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d2253d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a473044022078124c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e930220691d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a3601410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95fffffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4dafdaa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab023abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e00847147cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd701410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c385d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758df616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11eeb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac00000000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1ac1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f00000000001976a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f00000000001976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e280007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a33ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf04176b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf80125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff0100093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888ac00000000"), SER_NETWORK, PROTOCOL_VERSION); + CDataStream stream( + ParseHex( + "0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc88067010000" + "0000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9" + "728776381b4d4c86041b554b852907010000000100000000000000000000000000" + "00000000000000000000000000000000000000ffffffff07044c86041b0136ffff" + "ffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1" + "f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad" + "1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08" + "989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e" + "834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea1022100" + "9253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901" + "ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643" + "207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58cc" + "b3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9d" + "ee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca" + "0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8" + "acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf4" + "9e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fc" + "ad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff30" + "9e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af830000" + "00004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b" + "51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb542" + "8f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6" + "d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c8" + "10ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d" + "31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff01007144" + "60030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000" + "000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72" + "a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6" + "f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423" + "746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6" + "fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd62" + "38f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d225" + "3d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a47304402207812" + "4c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e93022069" + "1d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a360141" + "0462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab8" + "44c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ff" + "fffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4daf" + "daa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab0" + "23abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7" + "fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561" + "f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d8" + "7270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9" + "e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e0084714" + "7cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b" + "2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd7014104" + "62bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844" + "c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffff" + "ffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c38" + "5d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758d" + "f616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11e" + "eb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a" + "0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f" + "464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e" + "88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636" + "030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c" + "00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac0000" + "0000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d" + "850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1a" + "c1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90" + "f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1" + "e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c" + "4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f000000000019" + "76a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f0000000000" + "1976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000" + "000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3" + "f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e2" + "80007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a" + "690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2" + "b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a3" + "3ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf041" + "76b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b" + "903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb" + "87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf8" + "0125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06" + "820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff01" + "00093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888" + "ac00000000"), + SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_P2PUBKEY_ONLY); // Match the generation pubkey - filter.insert(ParseHex("04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a2252247d97a46a91")); + filter.insert(ParseHex("04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f" + "134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce" + "13ad1357231a2252247d97a46a91")); // ...and the output address of the 4th transaction filter.insert(ParseHex("b6efd80d99179f4f4ff6f4dd0a007d018c385d21")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); // We should match the generation outpoint - BOOST_CHECK(filter.contains(COutPoint(uint256S("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); + BOOST_CHECK( + filter.contains(COutPoint(uint256S("0x147caa76786596590baa4e98f5d9f48b8" + "6c7765e489f7a6ff3360fe5c674360b"), + 0))); // ... but not the 4th transaction's output (its not pay-2-pubkey) - BOOST_CHECK(!filter.contains(COutPoint(uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0))); + BOOST_CHECK( + !filter.contains(COutPoint(uint256S("0x02981fa052f0481dbc5868f4fc216603" + "5a10f27a03cfd2de67326471df5bc041"), + 0))); } -BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none) -{ - // Random real block (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) +BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none) { + // Random real block + // (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) // With 7 txes CBlock block; - CDataStream stream(ParseHex("0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc880670100000000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9728776381b4d4c86041b554b85290701000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0136ffffffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea10221009253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58ccb3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9dee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf49e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fcad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff309e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af83000000004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb5428f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c810ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff0100714460030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d2253d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a473044022078124c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e930220691d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a3601410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95fffffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4dafdaa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab023abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e00847147cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd701410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c385d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758df616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11eeb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac00000000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1ac1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f00000000001976a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f00000000001976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e280007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a33ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf04176b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf80125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff0100093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888ac00000000"), SER_NETWORK, PROTOCOL_VERSION); + CDataStream stream( + ParseHex( + "0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc88067010000" + "0000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9" + "728776381b4d4c86041b554b852907010000000100000000000000000000000000" + "00000000000000000000000000000000000000ffffffff07044c86041b0136ffff" + "ffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1" + "f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad" + "1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08" + "989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e" + "834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea1022100" + "9253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901" + "ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643" + "207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58cc" + "b3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9d" + "ee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca" + "0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8" + "acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf4" + "9e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fc" + "ad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff30" + "9e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af830000" + "00004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b" + "51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb542" + "8f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6" + "d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c8" + "10ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d" + "31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff01007144" + "60030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000" + "000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72" + "a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6" + "f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423" + "746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6" + "fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd62" + "38f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d225" + "3d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a47304402207812" + "4c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e93022069" + "1d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a360141" + "0462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab8" + "44c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ff" + "fffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4daf" + "daa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab0" + "23abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7" + "fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561" + "f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d8" + "7270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9" + "e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e0084714" + "7cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b" + "2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd7014104" + "62bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844" + "c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffff" + "ffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c38" + "5d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758d" + "f616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11e" + "eb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a" + "0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f" + "464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e" + "88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636" + "030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c" + "00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac0000" + "0000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d" + "850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1a" + "c1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90" + "f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1" + "e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c" + "4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f000000000019" + "76a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f0000000000" + "1976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000" + "000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3" + "f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e2" + "80007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a" + "690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2" + "b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a3" + "3ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf041" + "76b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b" + "903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb" + "87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf8" + "0125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06" + "820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff01" + "00093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888" + "ac00000000"), + SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_NONE); // Match the generation pubkey - filter.insert(ParseHex("04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a2252247d97a46a91")); + filter.insert(ParseHex("04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f" + "134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce" + "13ad1357231a2252247d97a46a91")); // ...and the output address of the 4th transaction filter.insert(ParseHex("b6efd80d99179f4f4ff6f4dd0a007d018c385d21")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); // We shouldn't match any outpoints (UPDATE_NONE) - BOOST_CHECK(!filter.contains(COutPoint(uint256S("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); - BOOST_CHECK(!filter.contains(COutPoint(uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0))); + BOOST_CHECK( + !filter.contains(COutPoint(uint256S("0x147caa76786596590baa4e98f5d9f48b" + "86c7765e489f7a6ff3360fe5c674360b"), + 0))); + BOOST_CHECK( + !filter.contains(COutPoint(uint256S("0x02981fa052f0481dbc5868f4fc216603" + "5a10f27a03cfd2de67326471df5bc041"), + 0))); } -static std::vector RandomData() -{ +static std::vector RandomData() { uint256 r = GetRandHash(); return std::vector(r.begin(), r.end()); } -BOOST_AUTO_TEST_CASE(rolling_bloom) -{ +BOOST_AUTO_TEST_CASE(rolling_bloom) { // last-100-entry, 1% false positive: CRollingBloomFilter rb1(100, 0.01); // Overfill: - static const int DATASIZE=399; + static const int DATASIZE = 399; std::vector data[DATASIZE]; for (int i = 0; i < DATASIZE; i++) { data[i] = RandomData(); rb1.insert(data[i]); } // Last 100 guaranteed to be remembered: for (int i = 299; i < DATASIZE; i++) { BOOST_CHECK(rb1.contains(data[i])); } // false positive rate is 1%, so we should get about 100 hits if // testing 10,000 random keys. We get worst-case false positive // behavior when the filter is as full as possible, which is // when we've inserted one minus an integer multiple of nElement*2. unsigned int nHits = 0; for (int i = 0; i < 10000; i++) { - if (rb1.contains(RandomData())) - ++nHits; + if (rb1.contains(RandomData())) ++nHits; } // Run test_bitcoin with --log_level=message to see BOOST_TEST_MESSAGEs: - BOOST_TEST_MESSAGE("RollingBloomFilter got " << nHits << " false positives (~100 expected)"); + BOOST_TEST_MESSAGE("RollingBloomFilter got " + << nHits << " false positives (~100 expected)"); // Insanely unlikely to get a fp count outside this range: BOOST_CHECK(nHits > 25); BOOST_CHECK(nHits < 175); - BOOST_CHECK(rb1.contains(data[DATASIZE-1])); + BOOST_CHECK(rb1.contains(data[DATASIZE - 1])); rb1.reset(); - BOOST_CHECK(!rb1.contains(data[DATASIZE-1])); + BOOST_CHECK(!rb1.contains(data[DATASIZE - 1])); // Now roll through data, make sure last 100 entries // are always remembered: for (int i = 0; i < DATASIZE; i++) { - if (i >= 100) - BOOST_CHECK(rb1.contains(data[i-100])); + if (i >= 100) BOOST_CHECK(rb1.contains(data[i - 100])); rb1.insert(data[i]); BOOST_CHECK(rb1.contains(data[i])); } // Insert 999 more random entries: for (int i = 0; i < 999; i++) { std::vector d = RandomData(); rb1.insert(d); BOOST_CHECK(rb1.contains(d)); } // Sanity check to make sure the filter isn't just filling up: nHits = 0; for (int i = 0; i < DATASIZE; i++) { - if (rb1.contains(data[i])) - ++nHits; + if (rb1.contains(data[i])) ++nHits; } // Expect about 5 false positives, more than 100 means // something is definitely broken. - BOOST_TEST_MESSAGE("RollingBloomFilter got " << nHits << " false positives (~5 expected)"); + BOOST_TEST_MESSAGE("RollingBloomFilter got " + << nHits << " false positives (~5 expected)"); BOOST_CHECK(nHits < 100); // last-1000-entry, 0.01% false positive: CRollingBloomFilter rb2(1000, 0.001); for (int i = 0; i < DATASIZE; i++) { rb2.insert(data[i]); } // ... room for all of them: for (int i = 0; i < DATASIZE; i++) { BOOST_CHECK(rb2.contains(data[i])); } } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/bswap_tests.cpp b/src/test/bswap_tests.cpp index 7b3134d32..f6ccec9d9 100644 --- a/src/test/bswap_tests.cpp +++ b/src/test/bswap_tests.cpp @@ -1,26 +1,25 @@ // Copyright (c) 2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "compat/byteswap.h" #include "test/test_bitcoin.h" #include BOOST_FIXTURE_TEST_SUITE(bswap_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(bswap_tests) -{ - // Sibling in bitcoin/src/qt/test/compattests.cpp - uint16_t u1 = 0x1234; - uint32_t u2 = 0x56789abc; - uint64_t u3 = 0xdef0123456789abc; - uint16_t e1 = 0x3412; - uint32_t e2 = 0xbc9a7856; - uint64_t e3 = 0xbc9a78563412f0de; - BOOST_CHECK(bswap_16(u1) == e1); - BOOST_CHECK(bswap_32(u2) == e2); - BOOST_CHECK(bswap_64(u3) == e3); +BOOST_AUTO_TEST_CASE(bswap_tests) { + // Sibling in bitcoin/src/qt/test/compattests.cpp + uint16_t u1 = 0x1234; + uint32_t u2 = 0x56789abc; + uint64_t u3 = 0xdef0123456789abc; + uint16_t e1 = 0x3412; + uint32_t e2 = 0xbc9a7856; + uint64_t e3 = 0xbc9a78563412f0de; + BOOST_CHECK(bswap_16(u1) == e1); + BOOST_CHECK(bswap_32(u2) == e2); + BOOST_CHECK(bswap_64(u3) == e3); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/compress_tests.cpp b/src/test/compress_tests.cpp index 35e4458bb..29aa137de 100644 --- a/src/test/compress_tests.cpp +++ b/src/test/compress_tests.cpp @@ -1,65 +1,66 @@ // Copyright (c) 2012-2015 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "compressor.h" -#include "util.h" #include "test/test_bitcoin.h" +#include "util.h" #include #include // amounts 0.00000001 .. 0.00100000 #define NUM_MULTIPLES_UNIT 100000 // amounts 0.01 .. 100.00 #define NUM_MULTIPLES_CENT 10000 // amounts 1 .. 10000 #define NUM_MULTIPLES_1BTC 10000 // amounts 50 .. 21000000 #define NUM_MULTIPLES_50BTC 420000 BOOST_FIXTURE_TEST_SUITE(compress_tests, BasicTestingSetup) bool static TestEncode(uint64_t in) { - return in == CTxOutCompressor::DecompressAmount(CTxOutCompressor::CompressAmount(in)); + return in == CTxOutCompressor::DecompressAmount( + CTxOutCompressor::CompressAmount(in)); } bool static TestDecode(uint64_t in) { - return in == CTxOutCompressor::CompressAmount(CTxOutCompressor::DecompressAmount(in)); + return in == CTxOutCompressor::CompressAmount( + CTxOutCompressor::DecompressAmount(in)); } bool static TestPair(uint64_t dec, uint64_t enc) { return CTxOutCompressor::CompressAmount(dec) == enc && CTxOutCompressor::DecompressAmount(enc) == dec; } -BOOST_AUTO_TEST_CASE(compress_amounts) -{ - BOOST_CHECK(TestPair( 0, 0x0)); - BOOST_CHECK(TestPair( 1, 0x1)); - BOOST_CHECK(TestPair( CENT, 0x7)); - BOOST_CHECK(TestPair( COIN, 0x9)); - BOOST_CHECK(TestPair( 50*COIN, 0x32)); - BOOST_CHECK(TestPair(21000000*COIN, 0x1406f40)); +BOOST_AUTO_TEST_CASE(compress_amounts) { + BOOST_CHECK(TestPair(0, 0x0)); + BOOST_CHECK(TestPair(1, 0x1)); + BOOST_CHECK(TestPair(CENT, 0x7)); + BOOST_CHECK(TestPair(COIN, 0x9)); + BOOST_CHECK(TestPair(50 * COIN, 0x32)); + BOOST_CHECK(TestPair(21000000 * COIN, 0x1406f40)); for (uint64_t i = 1; i <= NUM_MULTIPLES_UNIT; i++) BOOST_CHECK(TestEncode(i)); for (uint64_t i = 1; i <= NUM_MULTIPLES_CENT; i++) BOOST_CHECK(TestEncode(i * CENT)); for (uint64_t i = 1; i <= NUM_MULTIPLES_1BTC; i++) BOOST_CHECK(TestEncode(i * COIN)); for (uint64_t i = 1; i <= NUM_MULTIPLES_50BTC; i++) BOOST_CHECK(TestEncode(i * 50 * COIN)); for (uint64_t i = 0; i < 100000; i++) BOOST_CHECK(TestDecode(i)); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/crypto_tests.cpp b/src/test/crypto_tests.cpp index 4d1741717..a1347f522 100644 --- a/src/test/crypto_tests.cpp +++ b/src/test/crypto_tests.cpp @@ -1,442 +1,556 @@ // Copyright (c) 2014-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "crypto/aes.h" +#include "crypto/hmac_sha256.h" +#include "crypto/hmac_sha512.h" #include "crypto/ripemd160.h" #include "crypto/sha1.h" #include "crypto/sha256.h" #include "crypto/sha512.h" -#include "crypto/hmac_sha256.h" -#include "crypto/hmac_sha512.h" -#include "utilstrencodings.h" #include "test/test_bitcoin.h" #include "test/test_random.h" +#include "utilstrencodings.h" #include #include #include #include #include BOOST_FIXTURE_TEST_SUITE(crypto_tests, BasicTestingSetup) -template +template void TestVector(const Hasher &h, const In &in, const Out &out) { Out hash; BOOST_CHECK(out.size() == h.OUTPUT_SIZE); hash.resize(out.size()); { // Test that writing the whole input string at once works. - Hasher(h).Write((unsigned char*)&in[0], in.size()).Finalize(&hash[0]); + Hasher(h).Write((unsigned char *)&in[0], in.size()).Finalize(&hash[0]); BOOST_CHECK(hash == out); } - for (int i=0; i<32; i++) { + for (int i = 0; i < 32; i++) { // Test that writing the string broken up in random pieces works. Hasher hasher(h); size_t pos = 0; while (pos < in.size()) { size_t len = insecure_rand() % ((in.size() - pos + 1) / 2 + 1); - hasher.Write((unsigned char*)&in[pos], len); + hasher.Write((unsigned char *)&in[pos], len); pos += len; - if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) { - // Test that writing the rest at once to a copy of a hasher works. - Hasher(hasher).Write((unsigned char*)&in[pos], in.size() - pos).Finalize(&hash[0]); + if (pos > 0 && pos + 2 * out.size() > in.size() && + pos < in.size()) { + // Test that writing the rest at once to a copy of a hasher + // works. + Hasher(hasher) + .Write((unsigned char *)&in[pos], in.size() - pos) + .Finalize(&hash[0]); BOOST_CHECK(hash == out); } } hasher.Finalize(&hash[0]); BOOST_CHECK(hash == out); } } -void TestSHA1(const std::string &in, const std::string &hexout) { TestVector(CSHA1(), in, ParseHex(hexout));} -void TestSHA256(const std::string &in, const std::string &hexout) { TestVector(CSHA256(), in, ParseHex(hexout));} -void TestSHA512(const std::string &in, const std::string &hexout) { TestVector(CSHA512(), in, ParseHex(hexout));} -void TestRIPEMD160(const std::string &in, const std::string &hexout) { TestVector(CRIPEMD160(), in, ParseHex(hexout));} +void TestSHA1(const std::string &in, const std::string &hexout) { + TestVector(CSHA1(), in, ParseHex(hexout)); +} +void TestSHA256(const std::string &in, const std::string &hexout) { + TestVector(CSHA256(), in, ParseHex(hexout)); +} +void TestSHA512(const std::string &in, const std::string &hexout) { + TestVector(CSHA512(), in, ParseHex(hexout)); +} +void TestRIPEMD160(const std::string &in, const std::string &hexout) { + TestVector(CRIPEMD160(), in, ParseHex(hexout)); +} -void TestHMACSHA256(const std::string &hexkey, const std::string &hexin, const std::string &hexout) { +void TestHMACSHA256(const std::string &hexkey, const std::string &hexin, + const std::string &hexout) { std::vector key = ParseHex(hexkey); - TestVector(CHMAC_SHA256(&key[0], key.size()), ParseHex(hexin), ParseHex(hexout)); + TestVector(CHMAC_SHA256(&key[0], key.size()), ParseHex(hexin), + ParseHex(hexout)); } -void TestHMACSHA512(const std::string &hexkey, const std::string &hexin, const std::string &hexout) { +void TestHMACSHA512(const std::string &hexkey, const std::string &hexin, + const std::string &hexout) { std::vector key = ParseHex(hexkey); - TestVector(CHMAC_SHA512(&key[0], key.size()), ParseHex(hexin), ParseHex(hexout)); + TestVector(CHMAC_SHA512(&key[0], key.size()), ParseHex(hexin), + ParseHex(hexout)); } -void TestAES128(const std::string &hexkey, const std::string &hexin, const std::string &hexout) -{ +void TestAES128(const std::string &hexkey, const std::string &hexin, + const std::string &hexout) { std::vector key = ParseHex(hexkey); std::vector in = ParseHex(hexin); std::vector correctout = ParseHex(hexout); std::vector buf, buf2; assert(key.size() == 16); assert(in.size() == 16); assert(correctout.size() == 16); AES128Encrypt enc(&key[0]); buf.resize(correctout.size()); buf2.resize(correctout.size()); enc.Encrypt(&buf[0], &in[0]); BOOST_CHECK_EQUAL(HexStr(buf), HexStr(correctout)); AES128Decrypt dec(&key[0]); dec.Decrypt(&buf2[0], &buf[0]); BOOST_CHECK_EQUAL(HexStr(buf2), HexStr(in)); } -void TestAES256(const std::string &hexkey, const std::string &hexin, const std::string &hexout) -{ +void TestAES256(const std::string &hexkey, const std::string &hexin, + const std::string &hexout) { std::vector key = ParseHex(hexkey); std::vector in = ParseHex(hexin); std::vector correctout = ParseHex(hexout); std::vector buf; assert(key.size() == 32); assert(in.size() == 16); assert(correctout.size() == 16); AES256Encrypt enc(&key[0]); buf.resize(correctout.size()); enc.Encrypt(&buf[0], &in[0]); BOOST_CHECK(buf == correctout); AES256Decrypt dec(&key[0]); dec.Decrypt(&buf[0], &buf[0]); BOOST_CHECK(buf == in); } -void TestAES128CBC(const std::string &hexkey, const std::string &hexiv, bool pad, const std::string &hexin, const std::string &hexout) -{ +void TestAES128CBC(const std::string &hexkey, const std::string &hexiv, + bool pad, const std::string &hexin, + const std::string &hexout) { std::vector key = ParseHex(hexkey); std::vector iv = ParseHex(hexiv); std::vector in = ParseHex(hexin); std::vector correctout = ParseHex(hexout); std::vector realout(in.size() + AES_BLOCKSIZE); // Encrypt the plaintext and verify that it equals the cipher AES128CBCEncrypt enc(&key[0], &iv[0], pad); int size = enc.Encrypt(&in[0], in.size(), &realout[0]); realout.resize(size); BOOST_CHECK(realout.size() == correctout.size()); - BOOST_CHECK_MESSAGE(realout == correctout, HexStr(realout) + std::string(" != ") + hexout); + BOOST_CHECK_MESSAGE(realout == correctout, + HexStr(realout) + std::string(" != ") + hexout); // Decrypt the cipher and verify that it equals the plaintext std::vector decrypted(correctout.size()); AES128CBCDecrypt dec(&key[0], &iv[0], pad); size = dec.Decrypt(&correctout[0], correctout.size(), &decrypted[0]); decrypted.resize(size); BOOST_CHECK(decrypted.size() == in.size()); - BOOST_CHECK_MESSAGE(decrypted == in, HexStr(decrypted) + std::string(" != ") + hexin); + BOOST_CHECK_MESSAGE(decrypted == in, + HexStr(decrypted) + std::string(" != ") + hexin); - // Encrypt and re-decrypt substrings of the plaintext and verify that they equal each-other - for(std::vector::iterator i(in.begin()); i != in.end(); ++i) - { + // Encrypt and re-decrypt substrings of the plaintext and verify that they + // equal each-other + for (std::vector::iterator i(in.begin()); i != in.end(); + ++i) { std::vector sub(i, in.end()); std::vector subout(sub.size() + AES_BLOCKSIZE); int _size = enc.Encrypt(&sub[0], sub.size(), &subout[0]); - if (_size != 0) - { + if (_size != 0) { subout.resize(_size); std::vector subdecrypted(subout.size()); _size = dec.Decrypt(&subout[0], subout.size(), &subdecrypted[0]); subdecrypted.resize(_size); BOOST_CHECK(decrypted.size() == in.size()); - BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + std::string(" != ") + HexStr(sub)); + BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + + std::string(" != ") + + HexStr(sub)); } } } -void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, bool pad, const std::string &hexin, const std::string &hexout) -{ +void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, + bool pad, const std::string &hexin, + const std::string &hexout) { std::vector key = ParseHex(hexkey); std::vector iv = ParseHex(hexiv); std::vector in = ParseHex(hexin); std::vector correctout = ParseHex(hexout); std::vector realout(in.size() + AES_BLOCKSIZE); // Encrypt the plaintext and verify that it equals the cipher AES256CBCEncrypt enc(&key[0], &iv[0], pad); int size = enc.Encrypt(&in[0], in.size(), &realout[0]); realout.resize(size); BOOST_CHECK(realout.size() == correctout.size()); - BOOST_CHECK_MESSAGE(realout == correctout, HexStr(realout) + std::string(" != ") + hexout); + BOOST_CHECK_MESSAGE(realout == correctout, + HexStr(realout) + std::string(" != ") + hexout); // Decrypt the cipher and verify that it equals the plaintext std::vector decrypted(correctout.size()); AES256CBCDecrypt dec(&key[0], &iv[0], pad); size = dec.Decrypt(&correctout[0], correctout.size(), &decrypted[0]); decrypted.resize(size); BOOST_CHECK(decrypted.size() == in.size()); - BOOST_CHECK_MESSAGE(decrypted == in, HexStr(decrypted) + std::string(" != ") + hexin); + BOOST_CHECK_MESSAGE(decrypted == in, + HexStr(decrypted) + std::string(" != ") + hexin); - // Encrypt and re-decrypt substrings of the plaintext and verify that they equal each-other - for(std::vector::iterator i(in.begin()); i != in.end(); ++i) - { + // Encrypt and re-decrypt substrings of the plaintext and verify that they + // equal each-other + for (std::vector::iterator i(in.begin()); i != in.end(); + ++i) { std::vector sub(i, in.end()); std::vector subout(sub.size() + AES_BLOCKSIZE); int _size = enc.Encrypt(&sub[0], sub.size(), &subout[0]); - if (_size != 0) - { + if (_size != 0) { subout.resize(_size); std::vector subdecrypted(subout.size()); _size = dec.Decrypt(&subout[0], subout.size(), &subdecrypted[0]); subdecrypted.resize(_size); BOOST_CHECK(decrypted.size() == in.size()); - BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + std::string(" != ") + HexStr(sub)); + BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + + std::string(" != ") + + HexStr(sub)); } } } std::string LongTestString(void) { std::string ret; - for (int i=0; i<200000; i++) { + for (int i = 0; i < 200000; i++) { ret += (unsigned char)(i); ret += (unsigned char)(i >> 4); ret += (unsigned char)(i >> 8); ret += (unsigned char)(i >> 12); ret += (unsigned char)(i >> 16); } return ret; } const std::string test1 = LongTestString(); BOOST_AUTO_TEST_CASE(ripemd160_testvectors) { TestRIPEMD160("", "9c1185a5c5e9fc54612808977ee8f548b2258d31"); TestRIPEMD160("abc", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"); TestRIPEMD160("message digest", "5d0689ef49d2fae572b881b123a85ffa21595f36"); - TestRIPEMD160("secure hash algorithm", "20397528223b6a5f4cbc2808aba0464e645544f9"); - TestRIPEMD160("RIPEMD160 is considered to be safe", "a7d78608c7af8a8e728778e81576870734122b66"); + TestRIPEMD160("secure hash algorithm", + "20397528223b6a5f4cbc2808aba0464e645544f9"); + TestRIPEMD160("RIPEMD160 is considered to be safe", + "a7d78608c7af8a8e728778e81576870734122b66"); TestRIPEMD160("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "12a053384a9c0c88e405a06c27dcf49ada62eb2b"); - TestRIPEMD160("For this sample, this 63-byte string will be used as input data", - "de90dbfee14b63fb5abf27c2ad4a82aaa5f27a11"); - TestRIPEMD160("This is exactly 64 bytes long, not counting the terminating byte", - "eda31d51d3a623b81e19eb02e24ff65d27d67b37"); - TestRIPEMD160(std::string(1000000, 'a'), "52783243c1697bdbe16d37f97f68f08325dc1528"); + TestRIPEMD160( + "For this sample, this 63-byte string will be used as input data", + "de90dbfee14b63fb5abf27c2ad4a82aaa5f27a11"); + TestRIPEMD160( + "This is exactly 64 bytes long, not counting the terminating byte", + "eda31d51d3a623b81e19eb02e24ff65d27d67b37"); + TestRIPEMD160(std::string(1000000, 'a'), + "52783243c1697bdbe16d37f97f68f08325dc1528"); TestRIPEMD160(test1, "464243587bd146ea835cdf57bdae582f25ec45f1"); } BOOST_AUTO_TEST_CASE(sha1_testvectors) { TestSHA1("", "da39a3ee5e6b4b0d3255bfef95601890afd80709"); TestSHA1("abc", "a9993e364706816aba3e25717850c26c9cd0d89d"); TestSHA1("message digest", "c12252ceda8be8994d5fa0290a47231c1d16aae3"); - TestSHA1("secure hash algorithm", "d4d6d2f0ebe317513bbd8d967d89bac5819c2f60"); - TestSHA1("SHA1 is considered to be safe", "f2b6650569ad3a8720348dd6ea6c497dee3a842a"); + TestSHA1("secure hash algorithm", + "d4d6d2f0ebe317513bbd8d967d89bac5819c2f60"); + TestSHA1("SHA1 is considered to be safe", + "f2b6650569ad3a8720348dd6ea6c497dee3a842a"); TestSHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "84983e441c3bd26ebaae4aa1f95129e5e54670f1"); TestSHA1("For this sample, this 63-byte string will be used as input data", "4f0ea5cd0585a23d028abdc1a6684e5a8094dc49"); TestSHA1("This is exactly 64 bytes long, not counting the terminating byte", "fb679f23e7d1ce053313e66e127ab1b444397057"); - TestSHA1(std::string(1000000, 'a'), "34aa973cd4c4daa4f61eeb2bdbad27316534016f"); + TestSHA1(std::string(1000000, 'a'), + "34aa973cd4c4daa4f61eeb2bdbad27316534016f"); TestSHA1(test1, "b7755760681cbfd971451668f32af5774f4656b5"); } BOOST_AUTO_TEST_CASE(sha256_testvectors) { - TestSHA256("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); - TestSHA256("abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); - TestSHA256("message digest", - "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650"); - TestSHA256("secure hash algorithm", - "f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d"); - TestSHA256("SHA256 is considered to be safe", - "6819d915c73f4d1e77e4e1b52d1fa0f9cf9beaead3939f15874bd988e2a23630"); - TestSHA256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); - TestSHA256("For this sample, this 63-byte string will be used as input data", - "f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342"); - TestSHA256("This is exactly 64 bytes long, not counting the terminating byte", - "ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8"); - TestSHA256("As Bitcoin relies on 80 byte header hashes, we want to have an example for that.", - "7406e8de7d6e4fffc573daef05aefb8806e7790f55eab5576f31349743cca743"); - TestSHA256(std::string(1000000, 'a'), - "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"); - TestSHA256(test1, "a316d55510b49662420f49d145d42fb83f31ef8dc016aa4e32df049991a91e26"); + TestSHA256( + "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); + TestSHA256( + "abc", + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); + TestSHA256( + "message digest", + "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650"); + TestSHA256( + "secure hash algorithm", + "f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d"); + TestSHA256( + "SHA256 is considered to be safe", + "6819d915c73f4d1e77e4e1b52d1fa0f9cf9beaead3939f15874bd988e2a23630"); + TestSHA256( + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); + TestSHA256( + "For this sample, this 63-byte string will be used as input data", + "f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342"); + TestSHA256( + "This is exactly 64 bytes long, not counting the terminating byte", + "ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8"); + TestSHA256( + "As Bitcoin relies on 80 byte header hashes, we want to have an " + "example for that.", + "7406e8de7d6e4fffc573daef05aefb8806e7790f55eab5576f31349743cca743"); + TestSHA256( + std::string(1000000, 'a'), + "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"); + TestSHA256( + test1, + "a316d55510b49662420f49d145d42fb83f31ef8dc016aa4e32df049991a91e26"); } BOOST_AUTO_TEST_CASE(sha512_testvectors) { - TestSHA512("", - "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce" - "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); - TestSHA512("abc", - "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" - "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); - TestSHA512("message digest", - "107dbf389d9e9f71a3a95f6c055b9251bc5268c2be16d6c13492ea45b0199f33" - "09e16455ab1e96118e8a905d5597b72038ddb372a89826046de66687bb420e7c"); - TestSHA512("secure hash algorithm", - "7746d91f3de30c68cec0dd693120a7e8b04d8073cb699bdce1a3f64127bca7a3" - "d5db502e814bb63c063a7a5043b2df87c61133395f4ad1edca7fcf4b30c3236e"); - TestSHA512("SHA512 is considered to be safe", - "099e6468d889e1c79092a89ae925a9499b5408e01b66cb5b0a3bd0dfa51a9964" - "6b4a3901caab1318189f74cd8cf2e941829012f2449df52067d3dd5b978456c2"); - TestSHA512("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c335" - "96fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445"); - TestSHA512("For this sample, this 63-byte string will be used as input data", - "b3de4afbc516d2478fe9b518d063bda6c8dd65fc38402dd81d1eb7364e72fb6e" - "6663cf6d2771c8f5a6da09601712fb3d2a36c6ffea3e28b0818b05b0a8660766"); - TestSHA512("This is exactly 64 bytes long, not counting the terminating byte", - "70aefeaa0e7ac4f8fe17532d7185a289bee3b428d950c14fa8b713ca09814a38" - "7d245870e007a80ad97c369d193e41701aa07f3221d15f0e65a1ff970cedf030"); - TestSHA512("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno" - "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" - "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"); - TestSHA512(std::string(1000000, 'a'), - "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb" - "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"); - TestSHA512(test1, - "40cac46c147e6131c5193dd5f34e9d8bb4951395f27b08c558c65ff4ba2de594" - "37de8c3ef5459d76a52cedc02dc499a3c9ed9dedbfb3281afd9653b8a112fafc"); + TestSHA512( + "", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce" + "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); + TestSHA512( + "abc", + "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); + TestSHA512( + "message digest", + "107dbf389d9e9f71a3a95f6c055b9251bc5268c2be16d6c13492ea45b0199f33" + "09e16455ab1e96118e8a905d5597b72038ddb372a89826046de66687bb420e7c"); + TestSHA512( + "secure hash algorithm", + "7746d91f3de30c68cec0dd693120a7e8b04d8073cb699bdce1a3f64127bca7a3" + "d5db502e814bb63c063a7a5043b2df87c61133395f4ad1edca7fcf4b30c3236e"); + TestSHA512( + "SHA512 is considered to be safe", + "099e6468d889e1c79092a89ae925a9499b5408e01b66cb5b0a3bd0dfa51a9964" + "6b4a3901caab1318189f74cd8cf2e941829012f2449df52067d3dd5b978456c2"); + TestSHA512( + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c335" + "96fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445"); + TestSHA512( + "For this sample, this 63-byte string will be used as input data", + "b3de4afbc516d2478fe9b518d063bda6c8dd65fc38402dd81d1eb7364e72fb6e" + "6663cf6d2771c8f5a6da09601712fb3d2a36c6ffea3e28b0818b05b0a8660766"); + TestSHA512( + "This is exactly 64 bytes long, not counting the terminating byte", + "70aefeaa0e7ac4f8fe17532d7185a289bee3b428d950c14fa8b713ca09814a38" + "7d245870e007a80ad97c369d193e41701aa07f3221d15f0e65a1ff970cedf030"); + TestSHA512( + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno" + "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"); + TestSHA512( + std::string(1000000, 'a'), + "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb" + "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"); + TestSHA512( + test1, + "40cac46c147e6131c5193dd5f34e9d8bb4951395f27b08c558c65ff4ba2de594" + "37de8c3ef5459d76a52cedc02dc499a3c9ed9dedbfb3281afd9653b8a112fafc"); } BOOST_AUTO_TEST_CASE(hmac_sha256_testvectors) { // test cases 1, 2, 3, 4, 6 and 7 of RFC 4231 - TestHMACSHA256("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", - "4869205468657265", - "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"); - TestHMACSHA256("4a656665", - "7768617420646f2079612077616e7420666f72206e6f7468696e673f", - "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"); - TestHMACSHA256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" - "dddddddddddddddddddddddddddddddddddd", - "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe"); - TestHMACSHA256("0102030405060708090a0b0c0d0e0f10111213141516171819", - "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" - "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", - "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"); - TestHMACSHA256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaa", - "54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a" - "65204b6579202d2048617368204b6579204669727374", - "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"); - TestHMACSHA256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaa", - "5468697320697320612074657374207573696e672061206c6172676572207468" - "616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074" - "68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565" - "647320746f20626520686173686564206265666f7265206265696e6720757365" - "642062792074686520484d414320616c676f726974686d2e", - "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2"); + TestHMACSHA256( + "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", "4869205468657265", + "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"); + TestHMACSHA256( + "4a656665", "7768617420646f2079612077616e7420666f72206e6f7468696e673f", + "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"); + TestHMACSHA256( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" + "dddddddddddddddddddddddddddddddddddd", + "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe"); + TestHMACSHA256( + "0102030405060708090a0b0c0d0e0f10111213141516171819", + "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" + "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", + "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"); + TestHMACSHA256( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaa", + "54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a" + "65204b6579202d2048617368204b6579204669727374", + "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"); + TestHMACSHA256( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaa", + "5468697320697320612074657374207573696e672061206c6172676572207468" + "616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074" + "68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565" + "647320746f20626520686173686564206265666f7265206265696e6720757365" + "642062792074686520484d414320616c676f726974686d2e", + "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2"); } BOOST_AUTO_TEST_CASE(hmac_sha512_testvectors) { // test cases 1, 2, 3, 4, 6 and 7 of RFC 4231 - TestHMACSHA512("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", - "4869205468657265", - "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde" - "daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854"); - TestHMACSHA512("4a656665", - "7768617420646f2079612077616e7420666f72206e6f7468696e673f", - "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554" - "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737"); - TestHMACSHA512("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" - "dddddddddddddddddddddddddddddddddddd", - "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39" - "bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb"); - TestHMACSHA512("0102030405060708090a0b0c0d0e0f10111213141516171819", - "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" - "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", - "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3db" - "a91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd"); - TestHMACSHA512("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaa", - "54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a" - "65204b6579202d2048617368204b6579204669727374", - "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352" - "6b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598"); - TestHMACSHA512("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaa", - "5468697320697320612074657374207573696e672061206c6172676572207468" - "616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074" - "68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565" - "647320746f20626520686173686564206265666f7265206265696e6720757365" - "642062792074686520484d414320616c676f726974686d2e", - "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944" - "b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58"); + TestHMACSHA512( + "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", "4869205468657265", + "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde" + "daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854"); + TestHMACSHA512( + "4a656665", "7768617420646f2079612077616e7420666f72206e6f7468696e673f", + "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554" + "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737"); + TestHMACSHA512( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" + "dddddddddddddddddddddddddddddddddddd", + "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39" + "bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb"); + TestHMACSHA512( + "0102030405060708090a0b0c0d0e0f10111213141516171819", + "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" + "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", + "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3db" + "a91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd"); + TestHMACSHA512( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaa", + "54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a" + "65204b6579202d2048617368204b6579204669727374", + "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352" + "6b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598"); + TestHMACSHA512( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaa", + "5468697320697320612074657374207573696e672061206c6172676572207468" + "616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074" + "68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565" + "647320746f20626520686173686564206265666f7265206265696e6720757365" + "642062792074686520484d414320616c676f726974686d2e", + "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944" + "b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58"); } BOOST_AUTO_TEST_CASE(aes_testvectors) { // AES test vectors from FIPS 197. - TestAES128("000102030405060708090a0b0c0d0e0f", "00112233445566778899aabbccddeeff", "69c4e0d86a7b0430d8cdb78070b4c55a"); - TestAES256("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "00112233445566778899aabbccddeeff", "8ea2b7ca516745bfeafc49904b496089"); + TestAES128("000102030405060708090a0b0c0d0e0f", + "00112233445566778899aabbccddeeff", + "69c4e0d86a7b0430d8cdb78070b4c55a"); + TestAES256( + "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "00112233445566778899aabbccddeeff", "8ea2b7ca516745bfeafc49904b496089"); // AES-ECB test vectors from NIST sp800-38a. - TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "6bc1bee22e409f96e93d7e117393172a", "3ad77bb40d7a3660a89ecaf32466ef97"); - TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "ae2d8a571e03ac9c9eb76fac45af8e51", "f5d3d58503b9699de785895a96fdbaaf"); - TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "30c81c46a35ce411e5fbc1191a0a52ef", "43b1cd7f598ece23881b00e3ed030688"); - TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "f69f2445df4f9b17ad2b417be66c3710", "7b0c785e27e8ad3f8223207104725dd4"); - TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "6bc1bee22e409f96e93d7e117393172a", "f3eed1bdb5d2a03c064b5a7e3db181f8"); - TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "ae2d8a571e03ac9c9eb76fac45af8e51", "591ccb10d410ed26dc5ba74a31362870"); - TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "30c81c46a35ce411e5fbc1191a0a52ef", "b6ed21b99ca6f4f9f153e7b1beafed1d"); - TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "f69f2445df4f9b17ad2b417be66c3710", "23304b7a39f9f3ff067d8d8f9e24ecc7"); + TestAES128("2b7e151628aed2a6abf7158809cf4f3c", + "6bc1bee22e409f96e93d7e117393172a", + "3ad77bb40d7a3660a89ecaf32466ef97"); + TestAES128("2b7e151628aed2a6abf7158809cf4f3c", + "ae2d8a571e03ac9c9eb76fac45af8e51", + "f5d3d58503b9699de785895a96fdbaaf"); + TestAES128("2b7e151628aed2a6abf7158809cf4f3c", + "30c81c46a35ce411e5fbc1191a0a52ef", + "43b1cd7f598ece23881b00e3ed030688"); + TestAES128("2b7e151628aed2a6abf7158809cf4f3c", + "f69f2445df4f9b17ad2b417be66c3710", + "7b0c785e27e8ad3f8223207104725dd4"); + TestAES256( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "6bc1bee22e409f96e93d7e117393172a", "f3eed1bdb5d2a03c064b5a7e3db181f8"); + TestAES256( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "ae2d8a571e03ac9c9eb76fac45af8e51", "591ccb10d410ed26dc5ba74a31362870"); + TestAES256( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "30c81c46a35ce411e5fbc1191a0a52ef", "b6ed21b99ca6f4f9f153e7b1beafed1d"); + TestAES256( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "f69f2445df4f9b17ad2b417be66c3710", "23304b7a39f9f3ff067d8d8f9e24ecc7"); } BOOST_AUTO_TEST_CASE(aes_cbc_testvectors) { // NIST AES CBC 128-bit encryption test-vectors - TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "000102030405060708090A0B0C0D0E0F", false, \ - "6bc1bee22e409f96e93d7e117393172a", "7649abac8119b246cee98e9b12e9197d"); - TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "7649ABAC8119B246CEE98E9B12E9197D", false, \ - "ae2d8a571e03ac9c9eb76fac45af8e51", "5086cb9b507219ee95db113a917678b2"); - TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "5086cb9b507219ee95db113a917678b2", false, \ - "30c81c46a35ce411e5fbc1191a0a52ef", "73bed6b8e3c1743b7116e69e22229516"); - TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "73bed6b8e3c1743b7116e69e22229516", false, \ - "f69f2445df4f9b17ad2b417be66c3710", "3ff1caa1681fac09120eca307586e1a7"); + TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", + "000102030405060708090A0B0C0D0E0F", false, + "6bc1bee22e409f96e93d7e117393172a", + "7649abac8119b246cee98e9b12e9197d"); + TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", + "7649ABAC8119B246CEE98E9B12E9197D", false, + "ae2d8a571e03ac9c9eb76fac45af8e51", + "5086cb9b507219ee95db113a917678b2"); + TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", + "5086cb9b507219ee95db113a917678b2", false, + "30c81c46a35ce411e5fbc1191a0a52ef", + "73bed6b8e3c1743b7116e69e22229516"); + TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", + "73bed6b8e3c1743b7116e69e22229516", false, + "f69f2445df4f9b17ad2b417be66c3710", + "3ff1caa1681fac09120eca307586e1a7"); // The same vectors with padding enabled - TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "000102030405060708090A0B0C0D0E0F", true, \ - "6bc1bee22e409f96e93d7e117393172a", "7649abac8119b246cee98e9b12e9197d8964e0b149c10b7b682e6e39aaeb731c"); - TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "7649ABAC8119B246CEE98E9B12E9197D", true, \ - "ae2d8a571e03ac9c9eb76fac45af8e51", "5086cb9b507219ee95db113a917678b255e21d7100b988ffec32feeafaf23538"); - TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "5086cb9b507219ee95db113a917678b2", true, \ - "30c81c46a35ce411e5fbc1191a0a52ef", "73bed6b8e3c1743b7116e69e22229516f6eccda327bf8e5ec43718b0039adceb"); - TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "73bed6b8e3c1743b7116e69e22229516", true, \ - "f69f2445df4f9b17ad2b417be66c3710", "3ff1caa1681fac09120eca307586e1a78cb82807230e1321d3fae00d18cc2012"); + TestAES128CBC( + "2b7e151628aed2a6abf7158809cf4f3c", "000102030405060708090A0B0C0D0E0F", + true, "6bc1bee22e409f96e93d7e117393172a", + "7649abac8119b246cee98e9b12e9197d8964e0b149c10b7b682e6e39aaeb731c"); + TestAES128CBC( + "2b7e151628aed2a6abf7158809cf4f3c", "7649ABAC8119B246CEE98E9B12E9197D", + true, "ae2d8a571e03ac9c9eb76fac45af8e51", + "5086cb9b507219ee95db113a917678b255e21d7100b988ffec32feeafaf23538"); + TestAES128CBC( + "2b7e151628aed2a6abf7158809cf4f3c", "5086cb9b507219ee95db113a917678b2", + true, "30c81c46a35ce411e5fbc1191a0a52ef", + "73bed6b8e3c1743b7116e69e22229516f6eccda327bf8e5ec43718b0039adceb"); + TestAES128CBC( + "2b7e151628aed2a6abf7158809cf4f3c", "73bed6b8e3c1743b7116e69e22229516", + true, "f69f2445df4f9b17ad2b417be66c3710", + "3ff1caa1681fac09120eca307586e1a78cb82807230e1321d3fae00d18cc2012"); // NIST AES CBC 256-bit encryption test-vectors - TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \ - "000102030405060708090A0B0C0D0E0F", false, "6bc1bee22e409f96e93d7e117393172a", \ - "f58c4c04d6e5f1ba779eabfb5f7bfbd6"); - TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \ - "F58C4C04D6E5F1BA779EABFB5F7BFBD6", false, "ae2d8a571e03ac9c9eb76fac45af8e51", \ - "9cfc4e967edb808d679f777bc6702c7d"); - TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \ - "9CFC4E967EDB808D679F777BC6702C7D", false, "30c81c46a35ce411e5fbc1191a0a52ef", - "39f23369a9d9bacfa530e26304231461"); - TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \ - "39F23369A9D9BACFA530E26304231461", false, "f69f2445df4f9b17ad2b417be66c3710", \ - "b2eb05e2c39be9fcda6c19078c6a9d1b"); + TestAES256CBC( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "000102030405060708090A0B0C0D0E0F", false, + "6bc1bee22e409f96e93d7e117393172a", "f58c4c04d6e5f1ba779eabfb5f7bfbd6"); + TestAES256CBC( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "F58C4C04D6E5F1BA779EABFB5F7BFBD6", false, + "ae2d8a571e03ac9c9eb76fac45af8e51", "9cfc4e967edb808d679f777bc6702c7d"); + TestAES256CBC( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "9CFC4E967EDB808D679F777BC6702C7D", false, + "30c81c46a35ce411e5fbc1191a0a52ef", "39f23369a9d9bacfa530e26304231461"); + TestAES256CBC( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "39F23369A9D9BACFA530E26304231461", false, + "f69f2445df4f9b17ad2b417be66c3710", "b2eb05e2c39be9fcda6c19078c6a9d1b"); // The same vectors with padding enabled - TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \ - "000102030405060708090A0B0C0D0E0F", true, "6bc1bee22e409f96e93d7e117393172a", \ - "f58c4c04d6e5f1ba779eabfb5f7bfbd6485a5c81519cf378fa36d42b8547edc0"); - TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \ - "F58C4C04D6E5F1BA779EABFB5F7BFBD6", true, "ae2d8a571e03ac9c9eb76fac45af8e51", \ - "9cfc4e967edb808d679f777bc6702c7d3a3aa5e0213db1a9901f9036cf5102d2"); - TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \ - "9CFC4E967EDB808D679F777BC6702C7D", true, "30c81c46a35ce411e5fbc1191a0a52ef", - "39f23369a9d9bacfa530e263042314612f8da707643c90a6f732b3de1d3f5cee"); - TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \ - "39F23369A9D9BACFA530E26304231461", true, "f69f2445df4f9b17ad2b417be66c3710", \ - "b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644"); + TestAES256CBC( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "000102030405060708090A0B0C0D0E0F", true, + "6bc1bee22e409f96e93d7e117393172a", + "f58c4c04d6e5f1ba779eabfb5f7bfbd6485a5c81519cf378fa36d42b8547edc0"); + TestAES256CBC( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "F58C4C04D6E5F1BA779EABFB5F7BFBD6", true, + "ae2d8a571e03ac9c9eb76fac45af8e51", + "9cfc4e967edb808d679f777bc6702c7d3a3aa5e0213db1a9901f9036cf5102d2"); + TestAES256CBC( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "9CFC4E967EDB808D679F777BC6702C7D", true, + "30c81c46a35ce411e5fbc1191a0a52ef", + "39f23369a9d9bacfa530e263042314612f8da707643c90a6f732b3de1d3f5cee"); + TestAES256CBC( + "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", + "39F23369A9D9BACFA530E26304231461", true, + "f69f2445df4f9b17ad2b417be66c3710", + "b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644"); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/cuckoocache_tests.cpp b/src/test/cuckoocache_tests.cpp index 00446aa11..e131a6389 100644 --- a/src/test/cuckoocache_tests.cpp +++ b/src/test/cuckoocache_tests.cpp @@ -1,394 +1,380 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include #include "cuckoocache.h" -#include "test/test_bitcoin.h" #include "random.h" -#include +#include "test/test_bitcoin.h" +#include #include - +#include /** Test Suite for CuckooCache * * 1) All tests should have a deterministic result (using insecure rand * with deterministic seeds) * 2) Some test methods are templated to allow for easier testing * against new versions / comparing * 3) Results should be treated as a regression test, i.e., did the behavior * change significantly from what was expected. This can be OK, depending on * the nature of the change, but requires updating the tests to reflect the new * expected behavior. For example improving the hit rate may cause some tests * using BOOST_CHECK_CLOSE to fail. - * */ FastRandomContext insecure_rand(true); BOOST_AUTO_TEST_SUITE(cuckoocache_tests); - /** insecure_GetRandHash fills in a uint256 from insecure_rand */ -void insecure_GetRandHash(uint256& t) -{ - uint32_t* ptr = (uint32_t*)t.begin(); +void insecure_GetRandHash(uint256 &t) { + uint32_t *ptr = (uint32_t *)t.begin(); for (uint8_t j = 0; j < 8; ++j) *(ptr++) = insecure_rand.rand32(); } /** Definition copied from /src/script/sigcache.cpp */ -class uint256Hasher -{ +class uint256Hasher { public: template - uint32_t operator()(const uint256& key) const - { - static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available."); + uint32_t operator()(const uint256 &key) const { + static_assert(hash_select < 8, + "SignatureCacheHasher only has 8 hashes available."); uint32_t u; std::memcpy(&u, key.begin() + 4 * hash_select, 4); return u; } }; - -/* Test that no values not inserted into the cache are read out of it. +/** + * Test that no values not inserted into the cache are read out of it. * * There are no repeats in the first 200000 insecure_GetRandHash calls */ -BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes) -{ +BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes) { insecure_rand = FastRandomContext(true); CuckooCache::cache cc{}; cc.setup_bytes(32 << 20); uint256 v; for (int x = 0; x < 100000; ++x) { insecure_GetRandHash(v); cc.insert(v); } for (int x = 0; x < 100000; ++x) { insecure_GetRandHash(v); BOOST_CHECK(!cc.contains(v, false)); } }; -/** This helper returns the hit rate when megabytes*load worth of entries are +/** + * This helper returns the hit rate when megabytes*load worth of entries are * inserted into a megabytes sized cache */ -template -double test_cache(size_t megabytes, double load) -{ +template double test_cache(size_t megabytes, double load) { insecure_rand = FastRandomContext(true); std::vector hashes; Cache set{}; size_t bytes = megabytes * (1 << 20); set.setup_bytes(bytes); uint32_t n_insert = static_cast(load * (bytes / sizeof(uint256))); hashes.resize(n_insert); for (uint32_t i = 0; i < n_insert; ++i) { - uint32_t* ptr = (uint32_t*)hashes[i].begin(); + uint32_t *ptr = (uint32_t *)hashes[i].begin(); for (uint8_t j = 0; j < 8; ++j) *(ptr++) = insecure_rand.rand32(); } - /** We make a copy of the hashes because future optimizations of the - * cuckoocache may overwrite the inserted element, so the test is - * "future proofed". + /** + * We make a copy of the hashes because future optimizations of the + * cuckoocache may overwrite the inserted element, so the test is "future + * proofed". */ std::vector hashes_insert_copy = hashes; /** Do the insert */ - for (uint256& h : hashes_insert_copy) + for (uint256 &h : hashes_insert_copy) set.insert(h); /** Count the hits */ uint32_t count = 0; - for (uint256& h : hashes) + for (uint256 &h : hashes) count += set.contains(h, false); double hit_rate = ((double)count) / ((double)n_insert); return hit_rate; } /** The normalized hit rate for a given load. * * The semantics are a little confusing, so please see the below * explanation. * * Examples: * * 1) at load 0.5, we expect a perfect hit rate, so we multiply by * 1.0 * 2) at load 2.0, we expect to see half the entries, so a perfect hit rate * would be 0.5. Therefore, if we see a hit rate of 0.4, 0.4*2.0 = 0.8 is the * normalized hit rate. * * This is basically the right semantics, but has a bit of a glitch depending on * how you measure around load 1.0 as after load 1.0 your normalized hit rate * becomes effectively perfect, ignoring freshness. */ -double normalize_hit_rate(double hits, double load) -{ +double normalize_hit_rate(double hits, double load) { return hits * std::max(load, 1.0); } /** Check the hit rate on loads ranging from 0.1 to 2.0 */ -BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok) -{ - /** Arbitrarily selected Hit Rate threshold that happens to work for this test - * as a lower bound on performance. +BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok) { + /** + * Arbitrarily selected Hit Rate threshold that happens to work for this + * test as a lower bound on performance. */ double HitRateThresh = 0.98; size_t megabytes = 32; for (double load = 0.1; load < 2; load *= 2) { - double hits = test_cache>(megabytes, load); + double hits = test_cache>( + megabytes, load); BOOST_CHECK(normalize_hit_rate(hits, load) > HitRateThresh); } } - /** This helper checks that erased elements are preferentially inserted onto and * that the hit rate of "fresher" keys is reasonable*/ -template -void test_cache_erase(size_t megabytes) -{ +template void test_cache_erase(size_t megabytes) { double load = 1; insecure_rand = FastRandomContext(true); std::vector hashes; Cache set{}; size_t bytes = megabytes * (1 << 20); set.setup_bytes(bytes); uint32_t n_insert = static_cast(load * (bytes / sizeof(uint256))); hashes.resize(n_insert); for (uint32_t i = 0; i < n_insert; ++i) { - uint32_t* ptr = (uint32_t*)hashes[i].begin(); + uint32_t *ptr = (uint32_t *)hashes[i].begin(); for (uint8_t j = 0; j < 8; ++j) *(ptr++) = insecure_rand.rand32(); } /** We make a copy of the hashes because future optimizations of the * cuckoocache may overwrite the inserted element, so the test is * "future proofed". */ std::vector hashes_insert_copy = hashes; /** Insert the first half */ for (uint32_t i = 0; i < (n_insert / 2); ++i) set.insert(hashes_insert_copy[i]); /** Erase the first quarter */ for (uint32_t i = 0; i < (n_insert / 4); ++i) set.contains(hashes[i], true); /** Insert the second half */ for (uint32_t i = (n_insert / 2); i < n_insert; ++i) set.insert(hashes_insert_copy[i]); /** elements that we marked erased but that are still there */ size_t count_erased_but_contained = 0; /** elements that we did not erase but are older */ size_t count_stale = 0; /** elements that were most recently inserted */ size_t count_fresh = 0; for (uint32_t i = 0; i < (n_insert / 4); ++i) count_erased_but_contained += set.contains(hashes[i], false); for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i) count_stale += set.contains(hashes[i], false); for (uint32_t i = (n_insert / 2); i < n_insert; ++i) count_fresh += set.contains(hashes[i], false); - double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0); + double hit_rate_erased_but_contained = + double(count_erased_but_contained) / (double(n_insert) / 4.0); double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0); double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0); // Check that our hit_rate_fresh is perfect BOOST_CHECK_EQUAL(hit_rate_fresh, 1.0); // Check that we have a more than 2x better hit rate on stale elements than // erased elements. BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained); } -BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok) -{ +BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok) { size_t megabytes = 32; test_cache_erase>(megabytes); } -template -void test_cache_erase_parallel(size_t megabytes) -{ +template void test_cache_erase_parallel(size_t megabytes) { double load = 1; insecure_rand = FastRandomContext(true); std::vector hashes; Cache set{}; size_t bytes = megabytes * (1 << 20); set.setup_bytes(bytes); uint32_t n_insert = static_cast(load * (bytes / sizeof(uint256))); hashes.resize(n_insert); for (uint32_t i = 0; i < n_insert; ++i) { - uint32_t* ptr = (uint32_t*)hashes[i].begin(); + uint32_t *ptr = (uint32_t *)hashes[i].begin(); for (uint8_t j = 0; j < 8; ++j) *(ptr++) = insecure_rand.rand32(); } /** We make a copy of the hashes because future optimizations of the * cuckoocache may overwrite the inserted element, so the test is * "future proofed". */ std::vector hashes_insert_copy = hashes; boost::shared_mutex mtx; { /** Grab lock to make sure we release inserts */ boost::unique_lock l(mtx); /** Insert the first half */ for (uint32_t i = 0; i < (n_insert / 2); ++i) set.insert(hashes_insert_copy[i]); } /** Spin up 3 threads to run contains with erase. */ std::vector threads; /** Erase the first quarter */ for (uint32_t x = 0; x < 3; ++x) - /** Each thread is emplaced with x copy-by-value - */ + /** Each thread is emplaced with x copy-by-value */ threads.emplace_back([&, x] { boost::shared_lock l(mtx); - size_t ntodo = (n_insert/4)/3; - size_t start = ntodo*x; - size_t end = ntodo*(x+1); + size_t ntodo = (n_insert / 4) / 3; + size_t start = ntodo * x; + size_t end = ntodo * (x + 1); for (uint32_t i = start; i < end; ++i) set.contains(hashes[i], true); }); - /** Wait for all threads to finish - */ - for (std::thread& t : threads) + /** Wait for all threads to finish */ + for (std::thread &t : threads) t.join(); /** Grab lock to make sure we observe erases */ boost::unique_lock l(mtx); /** Insert the second half */ for (uint32_t i = (n_insert / 2); i < n_insert; ++i) set.insert(hashes_insert_copy[i]); /** elements that we marked erased but that are still there */ size_t count_erased_but_contained = 0; /** elements that we did not erase but are older */ size_t count_stale = 0; /** elements that were most recently inserted */ size_t count_fresh = 0; for (uint32_t i = 0; i < (n_insert / 4); ++i) count_erased_but_contained += set.contains(hashes[i], false); for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i) count_stale += set.contains(hashes[i], false); for (uint32_t i = (n_insert / 2); i < n_insert; ++i) count_fresh += set.contains(hashes[i], false); - double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0); + double hit_rate_erased_but_contained = + double(count_erased_but_contained) / (double(n_insert) / 4.0); double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0); double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0); // Check that our hit_rate_fresh is perfect BOOST_CHECK_EQUAL(hit_rate_fresh, 1.0); // Check that we have a more than 2x better hit rate on stale elements than // erased elements. BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained); } -BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok) -{ +BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok) { size_t megabytes = 32; - test_cache_erase_parallel>(megabytes); + test_cache_erase_parallel>( + megabytes); } - -template -void test_cache_generations() -{ +template void test_cache_generations() { // This test checks that for a simulation of network activity, the fresh hit // rate is never below 99%, and the number of times that it is worse than // 99.9% are less than 1% of the time. double min_hit_rate = 0.99; double tight_hit_rate = 0.999; double max_rate_less_than_tight_hit_rate = 0.01; // A cache that meets this specification is therefore shown to have a hit - // rate of at least tight_hit_rate * (1 - max_rate_less_than_tight_hit_rate) + - // min_hit_rate*max_rate_less_than_tight_hit_rate = 0.999*99%+0.99*1% == 99.89% + // rate of at least tight_hit_rate * (1 - max_rate_less_than_tight_hit_rate) + // + + // min_hit_rate*max_rate_less_than_tight_hit_rate = 0.999*99%+0.99*1% == + // 99.89% // hit rate with low variance. // We use deterministic values, but this test has also passed on many // iterations with non-deterministic values, so it isn't "overfit" to the // specific entropy in FastRandomContext(true) and implementation of the // cache. insecure_rand = FastRandomContext(true); // block_activity models a chunk of network activity. n_insert elements are // adde to the cache. The first and last n/4 are stored for removal later // and the middle n/2 are not stored. This models a network which uses half // the signatures of recently (since the last block) added transactions // immediately and never uses the other half. struct block_activity { std::vector reads; - block_activity(uint32_t n_insert, Cache& c) : reads() - { + block_activity(uint32_t n_insert, Cache &c) : reads() { std::vector inserts; inserts.resize(n_insert); reads.reserve(n_insert / 2); for (uint32_t i = 0; i < n_insert; ++i) { - uint32_t* ptr = (uint32_t*)inserts[i].begin(); + uint32_t *ptr = (uint32_t *)inserts[i].begin(); for (uint8_t j = 0; j < 8; ++j) *(ptr++) = insecure_rand.rand32(); } for (uint32_t i = 0; i < n_insert / 4; ++i) reads.push_back(inserts[i]); for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i) reads.push_back(inserts[i]); for (auto h : inserts) c.insert(h); } }; const uint32_t BLOCK_SIZE = 10000; // We expect window size 60 to perform reasonably given that each epoch // stores 45% of the cache size (~472k). const uint32_t WINDOW_SIZE = 60; const uint32_t POP_AMOUNT = (BLOCK_SIZE / WINDOW_SIZE) / 2; const double load = 10; const size_t megabytes = 32; const size_t bytes = megabytes * (1 << 20); - const uint32_t n_insert = static_cast(load * (bytes / sizeof(uint256))); + const uint32_t n_insert = + static_cast(load * (bytes / sizeof(uint256))); std::vector hashes; Cache set{}; set.setup_bytes(bytes); hashes.reserve(n_insert / BLOCK_SIZE); std::deque last_few; uint32_t out_of_tight_tolerance = 0; uint32_t total = n_insert / BLOCK_SIZE; // we use the deque last_few to model a sliding window of blocks. at each // step, each of the last WINDOW_SIZE block_activities checks the cache for // POP_AMOUNT of the hashes that they inserted, and marks these erased. for (uint32_t i = 0; i < total; ++i) { - if (last_few.size() == WINDOW_SIZE) - last_few.pop_front(); + if (last_few.size() == WINDOW_SIZE) last_few.pop_front(); last_few.emplace_back(BLOCK_SIZE, set); uint32_t count = 0; - for (auto& act : last_few) + for (auto &act : last_few) for (uint32_t k = 0; k < POP_AMOUNT; ++k) { count += set.contains(act.reads.back(), true); act.reads.pop_back(); } // We use last_few.size() rather than WINDOW_SIZE for the correct // behavior on the first WINDOW_SIZE iterations where the deque is not // full yet. double hit = (double(count)) / (last_few.size() * POP_AMOUNT); // Loose Check that hit rate is above min_hit_rate BOOST_CHECK(hit > min_hit_rate); // Tighter check, count number of times we are less than tight_hit_rate // (and implicityly, greater than min_hit_rate) out_of_tight_tolerance += hit < tight_hit_rate; } // Check that being out of tolerance happens less than // max_rate_less_than_tight_hit_rate of the time - BOOST_CHECK(double(out_of_tight_tolerance) / double(total) < max_rate_less_than_tight_hit_rate); + BOOST_CHECK(double(out_of_tight_tolerance) / double(total) < + max_rate_less_than_tight_hit_rate); } -BOOST_AUTO_TEST_CASE(cuckoocache_generations) -{ +BOOST_AUTO_TEST_CASE(cuckoocache_generations) { test_cache_generations>(); } BOOST_AUTO_TEST_SUITE_END(); diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp index d5d158027..d1d4c9913 100644 --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -1,323 +1,331 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "dbwrapper.h" -#include "uint256.h" #include "random.h" #include "test/test_bitcoin.h" +#include "uint256.h" -#include // for 'operator+=()' #include +#include // for 'operator+=()' #include // Test if a string consists entirely of null characters -bool is_null_key(const std::vector& key) { +bool is_null_key(const std::vector &key) { bool isnull = true; for (unsigned int i = 0; i < key.size(); i++) isnull &= (key[i] == '\x00'); return isnull; } - + BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup) - -BOOST_AUTO_TEST_CASE(dbwrapper) -{ + +BOOST_AUTO_TEST_CASE(dbwrapper) { // Perform tests both obfuscated and non-obfuscated. for (int i = 0; i < 2; i++) { bool obfuscate = (bool)i; - boost::filesystem::path ph = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); + boost::filesystem::path ph = boost::filesystem::temp_directory_path() / + boost::filesystem::unique_path(); CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); char key = 'k'; uint256 in = GetRandHash(); uint256 res; // Ensure that we're doing real obfuscation when obfuscate=true - BOOST_CHECK(obfuscate != is_null_key(dbwrapper_private::GetObfuscateKey(dbw))); + BOOST_CHECK(obfuscate != + is_null_key(dbwrapper_private::GetObfuscateKey(dbw))); BOOST_CHECK(dbw.Write(key, in)); BOOST_CHECK(dbw.Read(key, res)); BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); } } // Test batch operations -BOOST_AUTO_TEST_CASE(dbwrapper_batch) -{ +BOOST_AUTO_TEST_CASE(dbwrapper_batch) { // Perform tests both obfuscated and non-obfuscated. for (int i = 0; i < 2; i++) { bool obfuscate = (bool)i; - boost::filesystem::path ph = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); + boost::filesystem::path ph = boost::filesystem::temp_directory_path() / + boost::filesystem::unique_path(); CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); char key = 'i'; uint256 in = GetRandHash(); char key2 = 'j'; uint256 in2 = GetRandHash(); char key3 = 'k'; uint256 in3 = GetRandHash(); uint256 res; CDBBatch batch(dbw); batch.Write(key, in); batch.Write(key2, in2); batch.Write(key3, in3); // Remove key3 before it's even been written batch.Erase(key3); dbw.WriteBatch(batch); BOOST_CHECK(dbw.Read(key, res)); BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); BOOST_CHECK(dbw.Read(key2, res)); BOOST_CHECK_EQUAL(res.ToString(), in2.ToString()); // key3 should've never been written BOOST_CHECK(dbw.Read(key3, res) == false); } } -BOOST_AUTO_TEST_CASE(dbwrapper_iterator) -{ +BOOST_AUTO_TEST_CASE(dbwrapper_iterator) { // Perform tests both obfuscated and non-obfuscated. for (int i = 0; i < 2; i++) { bool obfuscate = (bool)i; - boost::filesystem::path ph = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); + boost::filesystem::path ph = boost::filesystem::temp_directory_path() / + boost::filesystem::unique_path(); CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); // The two keys are intentionally chosen for ordering char key = 'j'; uint256 in = GetRandHash(); BOOST_CHECK(dbw.Write(key, in)); char key2 = 'k'; uint256 in2 = GetRandHash(); BOOST_CHECK(dbw.Write(key2, in2)); - std::unique_ptr it(const_cast(&dbw)->NewIterator()); + std::unique_ptr it( + const_cast(&dbw)->NewIterator()); // Be sure to seek past the obfuscation key (if it exists) it->Seek(key); char key_res; uint256 val_res; it->GetKey(key_res); it->GetValue(val_res); BOOST_CHECK_EQUAL(key_res, key); BOOST_CHECK_EQUAL(val_res.ToString(), in.ToString()); it->Next(); it->GetKey(key_res); it->GetValue(val_res); BOOST_CHECK_EQUAL(key_res, key2); BOOST_CHECK_EQUAL(val_res.ToString(), in2.ToString()); it->Next(); BOOST_CHECK_EQUAL(it->Valid(), false); } } // Test that we do not obfuscation if there is existing data. -BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate) -{ +BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate) { // We're going to share this boost::filesystem::path between two wrappers - boost::filesystem::path ph = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); + boost::filesystem::path ph = boost::filesystem::temp_directory_path() / + boost::filesystem::unique_path(); create_directories(ph); // Set up a non-obfuscated wrapper to write some initial data. - CDBWrapper* dbw = new CDBWrapper(ph, (1 << 10), false, false, false); + CDBWrapper *dbw = new CDBWrapper(ph, (1 << 10), false, false, false); char key = 'k'; uint256 in = GetRandHash(); uint256 res; BOOST_CHECK(dbw->Write(key, in)); BOOST_CHECK(dbw->Read(key, res)); BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); // Call the destructor to free leveldb LOCK delete dbw; // Now, set up another wrapper that wants to obfuscate the same directory CDBWrapper odbw(ph, (1 << 10), false, false, true); - // Check that the key/val we wrote with unobfuscated wrapper exists and + // Check that the key/val we wrote with unobfuscated wrapper exists and // is readable. uint256 res2; BOOST_CHECK(odbw.Read(key, res2)); BOOST_CHECK_EQUAL(res2.ToString(), in.ToString()); - BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data - BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string + // There should be existing data + BOOST_CHECK(!odbw.IsEmpty()); + // The key should be an empty string + BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); uint256 in2 = GetRandHash(); uint256 res3; - + // Check that we can write successfully BOOST_CHECK(odbw.Write(key, in2)); BOOST_CHECK(odbw.Read(key, res3)); BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString()); } - + // Ensure that we start obfuscating during a reindex. -BOOST_AUTO_TEST_CASE(existing_data_reindex) -{ +BOOST_AUTO_TEST_CASE(existing_data_reindex) { // We're going to share this boost::filesystem::path between two wrappers - boost::filesystem::path ph = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); + boost::filesystem::path ph = boost::filesystem::temp_directory_path() / + boost::filesystem::unique_path(); create_directories(ph); // Set up a non-obfuscated wrapper to write some initial data. - CDBWrapper* dbw = new CDBWrapper(ph, (1 << 10), false, false, false); + CDBWrapper *dbw = new CDBWrapper(ph, (1 << 10), false, false, false); char key = 'k'; uint256 in = GetRandHash(); uint256 res; BOOST_CHECK(dbw->Write(key, in)); BOOST_CHECK(dbw->Read(key, res)); BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); // Call the destructor to free leveldb LOCK delete dbw; // Simulate a -reindex by wiping the existing data store CDBWrapper odbw(ph, (1 << 10), false, true, true); // Check that the key/val we wrote with unobfuscated wrapper doesn't exist uint256 res2; BOOST_CHECK(!odbw.Read(key, res2)); BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); uint256 in2 = GetRandHash(); uint256 res3; - + // Check that we can write successfully BOOST_CHECK(odbw.Write(key, in2)); BOOST_CHECK(odbw.Read(key, res3)); BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString()); } -BOOST_AUTO_TEST_CASE(iterator_ordering) -{ - boost::filesystem::path ph = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); +BOOST_AUTO_TEST_CASE(iterator_ordering) { + boost::filesystem::path ph = boost::filesystem::temp_directory_path() / + boost::filesystem::unique_path(); CDBWrapper dbw(ph, (1 << 20), true, false, false); - for (int x=0x00; x<256; ++x) { + for (int x = 0x00; x < 256; ++x) { uint8_t key = x; - uint32_t value = x*x; + uint32_t value = x * x; BOOST_CHECK(dbw.Write(key, value)); } - std::unique_ptr it(const_cast(&dbw)->NewIterator()); - for (int c=0; c<2; ++c) { + std::unique_ptr it( + const_cast(&dbw)->NewIterator()); + for (int c = 0; c < 2; ++c) { int seek_start; if (c == 0) seek_start = 0x00; else seek_start = 0x80; it->Seek((uint8_t)seek_start); - for (int x=seek_start; x<256; ++x) { + for (int x = seek_start; x < 256; ++x) { uint8_t key; uint32_t value; BOOST_CHECK(it->Valid()); - if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure - break; + // Avoid spurious errors about invalid iterator's key and value in + // case of failure + if (!it->Valid()) break; BOOST_CHECK(it->GetKey(key)); BOOST_CHECK(it->GetValue(value)); BOOST_CHECK_EQUAL(key, x); - BOOST_CHECK_EQUAL(value, x*x); + BOOST_CHECK_EQUAL(value, x * x); it->Next(); } BOOST_CHECK(!it->Valid()); } } struct StringContentsSerializer { - // Used to make two serialized objects the same while letting them have a different lengths - // This is a terrible idea + // Used to make two serialized objects the same while letting them have a + // different lengths. This is a terrible idea. std::string str; StringContentsSerializer() {} - StringContentsSerializer(const std::string& inp) : str(inp) {} + StringContentsSerializer(const std::string &inp) : str(inp) {} - StringContentsSerializer& operator+=(const std::string& s) { + StringContentsSerializer &operator+=(const std::string &s) { str += s; return *this; } - StringContentsSerializer& operator+=(const StringContentsSerializer& s) { return *this += s.str; } + StringContentsSerializer &operator+=(const StringContentsSerializer &s) { + return *this += s.str; + } ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action) { + inline void SerializationOp(Stream &s, Operation ser_action) { if (ser_action.ForRead()) { str.clear(); char c = 0; while (true) { try { READWRITE(c); str.push_back(c); - } catch (const std::ios_base::failure& e) { + } catch (const std::ios_base::failure &e) { break; } } } else { for (size_t i = 0; i < str.size(); i++) READWRITE(str[i]); } } }; -BOOST_AUTO_TEST_CASE(iterator_string_ordering) -{ +BOOST_AUTO_TEST_CASE(iterator_string_ordering) { char buf[10]; - boost::filesystem::path ph = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); + boost::filesystem::path ph = boost::filesystem::temp_directory_path() / + boost::filesystem::unique_path(); CDBWrapper dbw(ph, (1 << 20), true, false, false); - for (int x=0x00; x<10; ++x) { + for (int x = 0x00; x < 10; ++x) { for (int y = 0; y < 10; y++) { sprintf(buf, "%d", x); StringContentsSerializer key(buf); for (int z = 0; z < y; z++) key += key; - uint32_t value = x*x; + uint32_t value = x * x; BOOST_CHECK(dbw.Write(key, value)); } } - std::unique_ptr it(const_cast(&dbw)->NewIterator()); - for (int c=0; c<2; ++c) { + std::unique_ptr it( + const_cast(&dbw)->NewIterator()); + for (int c = 0; c < 2; ++c) { int seek_start; if (c == 0) seek_start = 0; else seek_start = 5; sprintf(buf, "%d", seek_start); StringContentsSerializer seek_key(buf); it->Seek(seek_key); - for (int x=seek_start; x<10; ++x) { + for (int x = seek_start; x < 10; ++x) { for (int y = 0; y < 10; y++) { sprintf(buf, "%d", x); std::string exp_key(buf); for (int z = 0; z < y; z++) exp_key += exp_key; StringContentsSerializer key; uint32_t value; BOOST_CHECK(it->Valid()); - if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure - break; + // Avoid spurious errors about invalid iterator's key and value + // in case of failure + if (!it->Valid()) break; BOOST_CHECK(it->GetKey(key)); BOOST_CHECK(it->GetValue(value)); BOOST_CHECK_EQUAL(key.str, exp_key); - BOOST_CHECK_EQUAL(value, x*x); + BOOST_CHECK_EQUAL(value, x * x); it->Next(); } } BOOST_CHECK(!it->Valid()); } } - - BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/hash_tests.cpp b/src/test/hash_tests.cpp index d8de765db..4b4ddaba6 100644 --- a/src/test/hash_tests.cpp +++ b/src/test/hash_tests.cpp @@ -1,133 +1,142 @@ // Copyright (c) 2013-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "hash.h" -#include "utilstrencodings.h" #include "test/test_bitcoin.h" +#include "utilstrencodings.h" #include #include BOOST_FIXTURE_TEST_SUITE(hash_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(murmurhash3) -{ +BOOST_AUTO_TEST_CASE(murmurhash3) { -#define T(expected, seed, data) BOOST_CHECK_EQUAL(MurmurHash3(seed, ParseHex(data)), expected) +#define T(expected, seed, data) \ + BOOST_CHECK_EQUAL(MurmurHash3(seed, ParseHex(data)), expected) // Test MurmurHash3 with various inputs. Of course this is retested in the // bloom filter tests - they would fail if MurmurHash3() had any problems - // but is useful for those trying to implement Bitcoin libraries as a // source of test data for their MurmurHash3() primitive during // development. // // The magic number 0xFBA4C795 comes from CBloomFilter::Hash() T(0x00000000, 0x00000000, ""); T(0x6a396f08, 0xFBA4C795, ""); T(0x81f16f39, 0xffffffff, ""); T(0x514e28b7, 0x00000000, "00"); T(0xea3f0b17, 0xFBA4C795, "00"); T(0xfd6cf10d, 0x00000000, "ff"); T(0x16c6b7ab, 0x00000000, "0011"); T(0x8eb51c3d, 0x00000000, "001122"); T(0xb4471bf8, 0x00000000, "00112233"); T(0xe2301fa8, 0x00000000, "0011223344"); T(0xfc2e4a15, 0x00000000, "001122334455"); T(0xb074502c, 0x00000000, "00112233445566"); T(0x8034d2a0, 0x00000000, "0011223344556677"); T(0xb4698def, 0x00000000, "001122334455667788"); #undef T } /* SipHash-2-4 output with k = 00 01 02 ... and in = (empty string) in = 00 (1 byte) in = 00 01 (2 bytes) in = 00 01 02 (3 bytes) ... in = 00 01 02 ... 3e (63 bytes) from: https://131002.net/siphash/siphash24.c */ uint64_t siphash_4_2_testvec[] = { - 0x726fdb47dd0e0e31, 0x74f839c593dc67fd, 0x0d6c8009d9a94f5a, 0x85676696d7fb7e2d, - 0xcf2794e0277187b7, 0x18765564cd99a68d, 0xcbc9466e58fee3ce, 0xab0200f58b01d137, - 0x93f5f5799a932462, 0x9e0082df0ba9e4b0, 0x7a5dbbc594ddb9f3, 0xf4b32f46226bada7, - 0x751e8fbc860ee5fb, 0x14ea5627c0843d90, 0xf723ca908e7af2ee, 0xa129ca6149be45e5, - 0x3f2acc7f57c29bdb, 0x699ae9f52cbe4794, 0x4bc1b3f0968dd39c, 0xbb6dc91da77961bd, - 0xbed65cf21aa2ee98, 0xd0f2cbb02e3b67c7, 0x93536795e3a33e88, 0xa80c038ccd5ccec8, - 0xb8ad50c6f649af94, 0xbce192de8a85b8ea, 0x17d835b85bbb15f3, 0x2f2e6163076bcfad, - 0xde4daaaca71dc9a5, 0xa6a2506687956571, 0xad87a3535c49ef28, 0x32d892fad841c342, - 0x7127512f72f27cce, 0xa7f32346f95978e3, 0x12e0b01abb051238, 0x15e034d40fa197ae, - 0x314dffbe0815a3b4, 0x027990f029623981, 0xcadcd4e59ef40c4d, 0x9abfd8766a33735c, - 0x0e3ea96b5304a7d0, 0xad0c42d6fc585992, 0x187306c89bc215a9, 0xd4a60abcf3792b95, - 0xf935451de4f21df2, 0xa9538f0419755787, 0xdb9acddff56ca510, 0xd06c98cd5c0975eb, - 0xe612a3cb9ecba951, 0xc766e62cfcadaf96, 0xee64435a9752fe72, 0xa192d576b245165a, - 0x0a8787bf8ecb74b2, 0x81b3e73d20b49b6f, 0x7fa8220ba3b2ecea, 0x245731c13ca42499, - 0xb78dbfaf3a8d83bd, 0xea1ad565322a1a0b, 0x60e61c23a3795013, 0x6606d7e446282b93, - 0x6ca4ecb15c5f91e1, 0x9f626da15c9625f3, 0xe51b38608ef25f57, 0x958a324ceb064572 -}; - -BOOST_AUTO_TEST_CASE(siphash) -{ + 0x726fdb47dd0e0e31, 0x74f839c593dc67fd, 0x0d6c8009d9a94f5a, + 0x85676696d7fb7e2d, 0xcf2794e0277187b7, 0x18765564cd99a68d, + 0xcbc9466e58fee3ce, 0xab0200f58b01d137, 0x93f5f5799a932462, + 0x9e0082df0ba9e4b0, 0x7a5dbbc594ddb9f3, 0xf4b32f46226bada7, + 0x751e8fbc860ee5fb, 0x14ea5627c0843d90, 0xf723ca908e7af2ee, + 0xa129ca6149be45e5, 0x3f2acc7f57c29bdb, 0x699ae9f52cbe4794, + 0x4bc1b3f0968dd39c, 0xbb6dc91da77961bd, 0xbed65cf21aa2ee98, + 0xd0f2cbb02e3b67c7, 0x93536795e3a33e88, 0xa80c038ccd5ccec8, + 0xb8ad50c6f649af94, 0xbce192de8a85b8ea, 0x17d835b85bbb15f3, + 0x2f2e6163076bcfad, 0xde4daaaca71dc9a5, 0xa6a2506687956571, + 0xad87a3535c49ef28, 0x32d892fad841c342, 0x7127512f72f27cce, + 0xa7f32346f95978e3, 0x12e0b01abb051238, 0x15e034d40fa197ae, + 0x314dffbe0815a3b4, 0x027990f029623981, 0xcadcd4e59ef40c4d, + 0x9abfd8766a33735c, 0x0e3ea96b5304a7d0, 0xad0c42d6fc585992, + 0x187306c89bc215a9, 0xd4a60abcf3792b95, 0xf935451de4f21df2, + 0xa9538f0419755787, 0xdb9acddff56ca510, 0xd06c98cd5c0975eb, + 0xe612a3cb9ecba951, 0xc766e62cfcadaf96, 0xee64435a9752fe72, + 0xa192d576b245165a, 0x0a8787bf8ecb74b2, 0x81b3e73d20b49b6f, + 0x7fa8220ba3b2ecea, 0x245731c13ca42499, 0xb78dbfaf3a8d83bd, + 0xea1ad565322a1a0b, 0x60e61c23a3795013, 0x6606d7e446282b93, + 0x6ca4ecb15c5f91e1, 0x9f626da15c9625f3, 0xe51b38608ef25f57, + 0x958a324ceb064572}; + +BOOST_AUTO_TEST_CASE(siphash) { CSipHasher hasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL); - BOOST_CHECK_EQUAL(hasher.Finalize(), 0x726fdb47dd0e0e31ull); + BOOST_CHECK_EQUAL(hasher.Finalize(), 0x726fdb47dd0e0e31ull); static const unsigned char t0[1] = {0}; hasher.Write(t0, 1); - BOOST_CHECK_EQUAL(hasher.Finalize(), 0x74f839c593dc67fdull); - static const unsigned char t1[7] = {1,2,3,4,5,6,7}; + BOOST_CHECK_EQUAL(hasher.Finalize(), 0x74f839c593dc67fdull); + static const unsigned char t1[7] = {1, 2, 3, 4, 5, 6, 7}; hasher.Write(t1, 7); - BOOST_CHECK_EQUAL(hasher.Finalize(), 0x93f5f5799a932462ull); + BOOST_CHECK_EQUAL(hasher.Finalize(), 0x93f5f5799a932462ull); hasher.Write(0x0F0E0D0C0B0A0908ULL); - BOOST_CHECK_EQUAL(hasher.Finalize(), 0x3f2acc7f57c29bdbull); - static const unsigned char t2[2] = {16,17}; + BOOST_CHECK_EQUAL(hasher.Finalize(), 0x3f2acc7f57c29bdbull); + static const unsigned char t2[2] = {16, 17}; hasher.Write(t2, 2); - BOOST_CHECK_EQUAL(hasher.Finalize(), 0x4bc1b3f0968dd39cull); - static const unsigned char t3[9] = {18,19,20,21,22,23,24,25,26}; + BOOST_CHECK_EQUAL(hasher.Finalize(), 0x4bc1b3f0968dd39cull); + static const unsigned char t3[9] = {18, 19, 20, 21, 22, 23, 24, 25, 26}; hasher.Write(t3, 9); - BOOST_CHECK_EQUAL(hasher.Finalize(), 0x2f2e6163076bcfadull); - static const unsigned char t4[5] = {27,28,29,30,31}; + BOOST_CHECK_EQUAL(hasher.Finalize(), 0x2f2e6163076bcfadull); + static const unsigned char t4[5] = {27, 28, 29, 30, 31}; hasher.Write(t4, 5); - BOOST_CHECK_EQUAL(hasher.Finalize(), 0x7127512f72f27cceull); + BOOST_CHECK_EQUAL(hasher.Finalize(), 0x7127512f72f27cceull); hasher.Write(0x2726252423222120ULL); - BOOST_CHECK_EQUAL(hasher.Finalize(), 0x0e3ea96b5304a7d0ull); + BOOST_CHECK_EQUAL(hasher.Finalize(), 0x0e3ea96b5304a7d0ull); hasher.Write(0x2F2E2D2C2B2A2928ULL); - BOOST_CHECK_EQUAL(hasher.Finalize(), 0xe612a3cb9ecba951ull); + BOOST_CHECK_EQUAL(hasher.Finalize(), 0xe612a3cb9ecba951ull); - BOOST_CHECK_EQUAL(SipHashUint256(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL, uint256S("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100")), 0x7127512f72f27cceull); + BOOST_CHECK_EQUAL( + SipHashUint256(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL, + uint256S("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09" + "080706050403020100")), + 0x7127512f72f27cceull); // Check test vectors from spec, one byte at a time CSipHasher hasher2(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL); - for (uint8_t x=0; x #include #include -static const std::string strSecret1 ("5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj"); -static const std::string strSecret2 ("5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3"); -static const std::string strSecret1C ("Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw"); -static const std::string strSecret2C ("L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g"); -static const CBitcoinAddress addr1 ("1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ"); -static const CBitcoinAddress addr2 ("1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ"); +static const std::string + strSecret1("5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj"); +static const std::string + strSecret2("5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3"); +static const std::string + strSecret1C("Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw"); +static const std::string + strSecret2C("L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g"); +static const CBitcoinAddress addr1("1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ"); +static const CBitcoinAddress addr2("1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ"); static const CBitcoinAddress addr1C("1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs"); static const CBitcoinAddress addr2C("1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs"); - static const std::string strAddressBad("1HV9Lc3sNHZxwj4Zk6fB38tEmBryq2cBiF"); - #ifdef KEY_TESTS_DUMPINFO -void dumpKeyInfo(uint256 privkey) -{ +void dumpKeyInfo(uint256 privkey) { CKey key; key.resize(32); memcpy(&secret[0], &privkey, 32); std::vector sec; sec.resize(32); memcpy(&sec[0], &secret[0], 32); printf(" * secret (hex): %s\n", HexStr(sec).c_str()); - for (int nCompressed=0; nCompressed<2; nCompressed++) - { + for (int nCompressed = 0; nCompressed < 2; nCompressed++) { bool fCompressed = nCompressed == 1; printf(" * %s:\n", fCompressed ? "compressed" : "uncompressed"); CBitcoinSecret bsecret; bsecret.SetSecret(secret, fCompressed); printf(" * secret (base58): %s\n", bsecret.ToString().c_str()); CKey key; key.SetSecret(secret, fCompressed); std::vector vchPubKey = key.GetPubKey(); printf(" * pubkey (hex): %s\n", HexStr(vchPubKey).c_str()); - printf(" * address (base58): %s\n", CBitcoinAddress(vchPubKey).ToString().c_str()); + printf(" * address (base58): %s\n", + CBitcoinAddress(vchPubKey).ToString().c_str()); } } #endif - BOOST_FIXTURE_TEST_SUITE(key_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(key_test1) -{ +BOOST_AUTO_TEST_CASE(key_test1) { CBitcoinSecret bsecret1, bsecret2, bsecret1C, bsecret2C, baddress1; - BOOST_CHECK( bsecret1.SetString (strSecret1)); - BOOST_CHECK( bsecret2.SetString (strSecret2)); - BOOST_CHECK( bsecret1C.SetString(strSecret1C)); - BOOST_CHECK( bsecret2C.SetString(strSecret2C)); + BOOST_CHECK(bsecret1.SetString(strSecret1)); + BOOST_CHECK(bsecret2.SetString(strSecret2)); + BOOST_CHECK(bsecret1C.SetString(strSecret1C)); + BOOST_CHECK(bsecret2C.SetString(strSecret2C)); BOOST_CHECK(!baddress1.SetString(strAddressBad)); - CKey key1 = bsecret1.GetKey(); + CKey key1 = bsecret1.GetKey(); BOOST_CHECK(key1.IsCompressed() == false); - CKey key2 = bsecret2.GetKey(); + CKey key2 = bsecret2.GetKey(); BOOST_CHECK(key2.IsCompressed() == false); CKey key1C = bsecret1C.GetKey(); BOOST_CHECK(key1C.IsCompressed() == true); CKey key2C = bsecret2C.GetKey(); BOOST_CHECK(key2C.IsCompressed() == true); - CPubKey pubkey1 = key1. GetPubKey(); - CPubKey pubkey2 = key2. GetPubKey(); + CPubKey pubkey1 = key1.GetPubKey(); + CPubKey pubkey2 = key2.GetPubKey(); CPubKey pubkey1C = key1C.GetPubKey(); CPubKey pubkey2C = key2C.GetPubKey(); BOOST_CHECK(key1.VerifyPubKey(pubkey1)); BOOST_CHECK(!key1.VerifyPubKey(pubkey1C)); BOOST_CHECK(!key1.VerifyPubKey(pubkey2)); BOOST_CHECK(!key1.VerifyPubKey(pubkey2C)); BOOST_CHECK(!key1C.VerifyPubKey(pubkey1)); BOOST_CHECK(key1C.VerifyPubKey(pubkey1C)); BOOST_CHECK(!key1C.VerifyPubKey(pubkey2)); BOOST_CHECK(!key1C.VerifyPubKey(pubkey2C)); BOOST_CHECK(!key2.VerifyPubKey(pubkey1)); BOOST_CHECK(!key2.VerifyPubKey(pubkey1C)); BOOST_CHECK(key2.VerifyPubKey(pubkey2)); BOOST_CHECK(!key2.VerifyPubKey(pubkey2C)); BOOST_CHECK(!key2C.VerifyPubKey(pubkey1)); BOOST_CHECK(!key2C.VerifyPubKey(pubkey1C)); BOOST_CHECK(!key2C.VerifyPubKey(pubkey2)); BOOST_CHECK(key2C.VerifyPubKey(pubkey2C)); - BOOST_CHECK(addr1.Get() == CTxDestination(pubkey1.GetID())); - BOOST_CHECK(addr2.Get() == CTxDestination(pubkey2.GetID())); + BOOST_CHECK(addr1.Get() == CTxDestination(pubkey1.GetID())); + BOOST_CHECK(addr2.Get() == CTxDestination(pubkey2.GetID())); BOOST_CHECK(addr1C.Get() == CTxDestination(pubkey1C.GetID())); BOOST_CHECK(addr2C.Get() == CTxDestination(pubkey2C.GetID())); - for (int n=0; n<16; n++) - { + for (int n = 0; n < 16; n++) { std::string strMsg = strprintf("Very secret message %i: 11", n); uint256 hashMsg = Hash(strMsg.begin(), strMsg.end()); // normal signatures std::vector sign1, sign2, sign1C, sign2C; - BOOST_CHECK(key1.Sign (hashMsg, sign1)); - BOOST_CHECK(key2.Sign (hashMsg, sign2)); + BOOST_CHECK(key1.Sign(hashMsg, sign1)); + BOOST_CHECK(key2.Sign(hashMsg, sign2)); BOOST_CHECK(key1C.Sign(hashMsg, sign1C)); BOOST_CHECK(key2C.Sign(hashMsg, sign2C)); - BOOST_CHECK( pubkey1.Verify(hashMsg, sign1)); + BOOST_CHECK(pubkey1.Verify(hashMsg, sign1)); BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2)); - BOOST_CHECK( pubkey1.Verify(hashMsg, sign1C)); + BOOST_CHECK(pubkey1.Verify(hashMsg, sign1C)); BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2C)); BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1)); - BOOST_CHECK( pubkey2.Verify(hashMsg, sign2)); + BOOST_CHECK(pubkey2.Verify(hashMsg, sign2)); BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1C)); - BOOST_CHECK( pubkey2.Verify(hashMsg, sign2C)); + BOOST_CHECK(pubkey2.Verify(hashMsg, sign2C)); - BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1)); + BOOST_CHECK(pubkey1C.Verify(hashMsg, sign1)); BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2)); - BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1C)); + BOOST_CHECK(pubkey1C.Verify(hashMsg, sign1C)); BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2C)); BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1)); - BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2)); + BOOST_CHECK(pubkey2C.Verify(hashMsg, sign2)); BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1C)); - BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2C)); + BOOST_CHECK(pubkey2C.Verify(hashMsg, sign2C)); // compact signatures (with key recovery) std::vector csign1, csign2, csign1C, csign2C; - BOOST_CHECK(key1.SignCompact (hashMsg, csign1)); - BOOST_CHECK(key2.SignCompact (hashMsg, csign2)); + BOOST_CHECK(key1.SignCompact(hashMsg, csign1)); + BOOST_CHECK(key2.SignCompact(hashMsg, csign2)); BOOST_CHECK(key1C.SignCompact(hashMsg, csign1C)); BOOST_CHECK(key2C.SignCompact(hashMsg, csign2C)); CPubKey rkey1, rkey2, rkey1C, rkey2C; - BOOST_CHECK(rkey1.RecoverCompact (hashMsg, csign1)); - BOOST_CHECK(rkey2.RecoverCompact (hashMsg, csign2)); + BOOST_CHECK(rkey1.RecoverCompact(hashMsg, csign1)); + BOOST_CHECK(rkey2.RecoverCompact(hashMsg, csign2)); BOOST_CHECK(rkey1C.RecoverCompact(hashMsg, csign1C)); BOOST_CHECK(rkey2C.RecoverCompact(hashMsg, csign2C)); - BOOST_CHECK(rkey1 == pubkey1); - BOOST_CHECK(rkey2 == pubkey2); + BOOST_CHECK(rkey1 == pubkey1); + BOOST_CHECK(rkey2 == pubkey2); BOOST_CHECK(rkey1C == pubkey1C); BOOST_CHECK(rkey2C == pubkey2C); } // test deterministic signing std::vector detsig, detsigc; std::string strMsg = "Very deterministic message"; uint256 hashMsg = Hash(strMsg.begin(), strMsg.end()); BOOST_CHECK(key1.Sign(hashMsg, detsig)); BOOST_CHECK(key1C.Sign(hashMsg, detsigc)); BOOST_CHECK(detsig == detsigc); - BOOST_CHECK(detsig == ParseHex("304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6")); + BOOST_CHECK(detsig == + ParseHex("304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a3" + "94b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924" + "c43fa43c0ad57dccdaa11f81a6bd4582f6")); BOOST_CHECK(key2.Sign(hashMsg, detsig)); BOOST_CHECK(key2C.Sign(hashMsg, detsigc)); BOOST_CHECK(detsig == detsigc); - BOOST_CHECK(detsig == ParseHex("3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d")); + BOOST_CHECK(detsig == + ParseHex("3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33d" + "c129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd" + "70e404b6428ab9a69566962e8771b5944d")); BOOST_CHECK(key1.SignCompact(hashMsg, detsig)); BOOST_CHECK(key1C.SignCompact(hashMsg, detsigc)); - BOOST_CHECK(detsig == ParseHex("1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6")); - BOOST_CHECK(detsigc == ParseHex("205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6")); + BOOST_CHECK(detsig == + ParseHex("1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2" + "d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0a" + "d57dccdaa11f81a6bd4582f6")); + BOOST_CHECK(detsigc == + ParseHex("205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2" + "d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0a" + "d57dccdaa11f81a6bd4582f6")); BOOST_CHECK(key2.SignCompact(hashMsg, detsig)); BOOST_CHECK(key2C.SignCompact(hashMsg, detsigc)); - BOOST_CHECK(detsig == ParseHex("1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d")); - BOOST_CHECK(detsigc == ParseHex("2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d")); + BOOST_CHECK(detsig == + ParseHex("1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2" + "bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b642" + "8ab9a69566962e8771b5944d")); + BOOST_CHECK(detsigc == + ParseHex("2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2" + "bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b642" + "8ab9a69566962e8771b5944d")); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/limitedmap_tests.cpp b/src/test/limitedmap_tests.cpp index b071ab117..8233de094 100644 --- a/src/test/limitedmap_tests.cpp +++ b/src/test/limitedmap_tests.cpp @@ -1,101 +1,100 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "limitedmap.h" #include "test/test_bitcoin.h" #include BOOST_FIXTURE_TEST_SUITE(limitedmap_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(limitedmap_test) -{ +BOOST_AUTO_TEST_CASE(limitedmap_test) { // create a limitedmap capped at 10 items limitedmap map(10); // check that the max size is 10 BOOST_CHECK(map.max_size() == 10); // check that it's empty BOOST_CHECK(map.size() == 0); // insert (-1, -1) map.insert(std::pair(-1, -1)); // make sure that the size is updated BOOST_CHECK(map.size() == 1); // make sure that the new item is in the map BOOST_CHECK(map.count(-1) == 1); // insert 10 new items for (int i = 0; i < 10; i++) { map.insert(std::pair(i, i + 1)); } // make sure that the map now contains 10 items... BOOST_CHECK(map.size() == 10); // ...and that the first item has been discarded BOOST_CHECK(map.count(-1) == 0); // iterate over the map, both with an index and an iterator limitedmap::const_iterator it = map.begin(); for (int i = 0; i < 10; i++) { // make sure the item is present BOOST_CHECK(map.count(i) == 1); // use the iterator to check for the expected key and value BOOST_CHECK(it->first == i); BOOST_CHECK(it->second == i + 1); - + // use find to check for the value BOOST_CHECK(map.find(i)->second == i + 1); - + // update and recheck map.update(it, i + 2); BOOST_CHECK(map.find(i)->second == i + 2); it++; } // check that we've exhausted the iterator BOOST_CHECK(it == map.end()); // resize the map to 5 items map.max_size(5); // check that the max size and size are now 5 BOOST_CHECK(map.max_size() == 5); BOOST_CHECK(map.size() == 5); // check that items less than 5 have been discarded // and items greater than 5 are retained for (int i = 0; i < 10; i++) { if (i < 5) { BOOST_CHECK(map.count(i) == 0); } else { BOOST_CHECK(map.count(i) == 1); } } // erase some items not in the map for (int i = 100; i < 1000; i += 100) { map.erase(i); } // check that the size is unaffected BOOST_CHECK(map.size() == 5); // erase the remaining elements for (int i = 5; i < 10; i++) { map.erase(i); } // check that the map is now empty BOOST_CHECK(map.empty()); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/main_tests.cpp b/src/test/main_tests.cpp index d52104b4c..4f618f9dc 100644 --- a/src/test/main_tests.cpp +++ b/src/test/main_tests.cpp @@ -1,76 +1,80 @@ // Copyright (c) 2014-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "chainparams.h" -#include "validation.h" #include "net.h" +#include "validation.h" #include "test/test_bitcoin.h" #include #include BOOST_FIXTURE_TEST_SUITE(main_tests, TestingSetup) -static void TestBlockSubsidyHalvings(const Consensus::Params& consensusParams) -{ +static void TestBlockSubsidyHalvings(const Consensus::Params &consensusParams) { int maxHalvings = 64; CAmount nInitialSubsidy = 50 * COIN; CAmount nPreviousSubsidy = nInitialSubsidy * 2; // for height == 0 BOOST_CHECK_EQUAL(nPreviousSubsidy, nInitialSubsidy * 2); for (int nHalvings = 0; nHalvings < maxHalvings; nHalvings++) { int nHeight = nHalvings * consensusParams.nSubsidyHalvingInterval; CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); BOOST_CHECK(nSubsidy <= nInitialSubsidy); BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2); nPreviousSubsidy = nSubsidy; } - BOOST_CHECK_EQUAL(GetBlockSubsidy(maxHalvings * consensusParams.nSubsidyHalvingInterval, consensusParams), 0); + BOOST_CHECK_EQUAL( + GetBlockSubsidy(maxHalvings * consensusParams.nSubsidyHalvingInterval, + consensusParams), + 0); } -static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval) -{ +static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval) { Consensus::Params consensusParams; consensusParams.nSubsidyHalvingInterval = nSubsidyHalvingInterval; TestBlockSubsidyHalvings(consensusParams); } -BOOST_AUTO_TEST_CASE(block_subsidy_test) -{ - TestBlockSubsidyHalvings(Params(CBaseChainParams::MAIN).GetConsensus()); // As in main - TestBlockSubsidyHalvings(150); // As in regtest - TestBlockSubsidyHalvings(1000); // Just another interval +BOOST_AUTO_TEST_CASE(block_subsidy_test) { + TestBlockSubsidyHalvings( + Params(CBaseChainParams::MAIN).GetConsensus()); // As in main + TestBlockSubsidyHalvings(150); // As in regtest + TestBlockSubsidyHalvings(1000); // Just another interval } -BOOST_AUTO_TEST_CASE(subsidy_limit_test) -{ - const Consensus::Params& consensusParams = Params(CBaseChainParams::MAIN).GetConsensus(); +BOOST_AUTO_TEST_CASE(subsidy_limit_test) { + const Consensus::Params &consensusParams = + Params(CBaseChainParams::MAIN).GetConsensus(); CAmount nSum = 0; for (int nHeight = 0; nHeight < 14000000; nHeight += 1000) { CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); BOOST_CHECK(nSubsidy <= 50 * COIN); nSum += nSubsidy * 1000; BOOST_CHECK(MoneyRange(nSum)); } BOOST_CHECK_EQUAL(nSum, 2099999997690000ULL); } -bool ReturnFalse() { return false; } -bool ReturnTrue() { return true; } +bool ReturnFalse() { + return false; +} +bool ReturnTrue() { + return true; +} -BOOST_AUTO_TEST_CASE(test_combiner_all) -{ - boost::signals2::signal Test; +BOOST_AUTO_TEST_CASE(test_combiner_all) { + boost::signals2::signal Test; BOOST_CHECK(Test()); Test.connect(&ReturnFalse); BOOST_CHECK(!Test()); Test.connect(&ReturnTrue); BOOST_CHECK(!Test()); Test.disconnect(&ReturnFalse); BOOST_CHECK(Test()); Test.disconnect(&ReturnTrue); BOOST_CHECK(Test()); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/merkle_tests.cpp b/src/test/merkle_tests.cpp index 77dc521ab..2f0e29807 100644 --- a/src/test/merkle_tests.cpp +++ b/src/test/merkle_tests.cpp @@ -1,136 +1,156 @@ // Copyright (c) 2015-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "consensus/merkle.h" #include "test/test_bitcoin.h" #include "test/test_random.h" #include BOOST_FIXTURE_TEST_SUITE(merkle_tests, TestingSetup) // Older version of the merkle root computation code, for comparison. -static uint256 BlockBuildMerkleTree(const CBlock& block, bool* fMutated, std::vector& vMerkleTree) -{ +static uint256 BlockBuildMerkleTree(const CBlock &block, bool *fMutated, + std::vector &vMerkleTree) { vMerkleTree.clear(); - vMerkleTree.reserve(block.vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes. - for (std::vector::const_iterator it(block.vtx.begin()); it != block.vtx.end(); ++it) + // Safe upper bound for the number of total nodes. + vMerkleTree.reserve(block.vtx.size() * 2 + 16); + for (std::vector::const_iterator it(block.vtx.begin()); + it != block.vtx.end(); ++it) vMerkleTree.push_back((*it)->GetId()); int j = 0; bool mutated = false; - for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) - { - for (int i = 0; i < nSize; i += 2) - { - int i2 = std::min(i+1, nSize-1); - if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) { - // Two identical hashes at the end of the list at a particular level. + for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) { + for (int i = 0; i < nSize; i += 2) { + int i2 = std::min(i + 1, nSize - 1); + if (i2 == i + 1 && i2 + 1 == nSize && + vMerkleTree[j + i] == vMerkleTree[j + i2]) { + // Two identical hashes at the end of the list at a particular + // level. mutated = true; } - vMerkleTree.push_back(Hash(vMerkleTree[j+i].begin(), vMerkleTree[j+i].end(), - vMerkleTree[j+i2].begin(), vMerkleTree[j+i2].end())); + vMerkleTree.push_back( + Hash(vMerkleTree[j + i].begin(), vMerkleTree[j + i].end(), + vMerkleTree[j + i2].begin(), vMerkleTree[j + i2].end())); } j += nSize; } if (fMutated) { *fMutated = mutated; } return (vMerkleTree.empty() ? uint256() : vMerkleTree.back()); } // Older version of the merkle branch computation code, for comparison. -static std::vector BlockGetMerkleBranch(const CBlock& block, const std::vector& vMerkleTree, int nIndex) -{ +static std::vector +BlockGetMerkleBranch(const CBlock &block, + const std::vector &vMerkleTree, int nIndex) { std::vector vMerkleBranch; int j = 0; - for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) - { - int i = std::min(nIndex^1, nSize-1); - vMerkleBranch.push_back(vMerkleTree[j+i]); + for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) { + int i = std::min(nIndex ^ 1, nSize - 1); + vMerkleBranch.push_back(vMerkleTree[j + i]); nIndex >>= 1; j += nSize; } return vMerkleBranch; } static inline int ctz(uint32_t i) { if (i == 0) return 0; int j = 0; while (!(i & 1)) { j++; i >>= 1; } return j; } -BOOST_AUTO_TEST_CASE(merkle_test) -{ +BOOST_AUTO_TEST_CASE(merkle_test) { for (int i = 0; i < 32; i++) { - // Try 32 block sizes: all sizes from 0 to 16 inclusive, and then 15 random sizes. + // Try 32 block sizes: all sizes from 0 to 16 inclusive, and then 15 + // random sizes. int ntx = (i <= 16) ? i : 17 + (insecure_rand() % 4000); // Try up to 3 mutations. for (int mutate = 0; mutate <= 3; mutate++) { - int duplicate1 = mutate >= 1 ? 1 << ctz(ntx) : 0; // The last how many transactions to duplicate first. - if (duplicate1 >= ntx) break; // Duplication of the entire tree results in a different root (it adds a level). - int ntx1 = ntx + duplicate1; // The resulting number of transactions after the first duplication. - int duplicate2 = mutate >= 2 ? 1 << ctz(ntx1) : 0; // Likewise for the second mutation. + // The last how many transactions to duplicate first. + int duplicate1 = mutate >= 1 ? 1 << ctz(ntx) : 0; + if (duplicate1 >= ntx) { + // Duplication of the entire tree results in a different root + // (it adds a level). + break; + } + + // The resulting number of transactions after the first duplication. + int ntx1 = ntx + duplicate1; + // Likewise for the second mutation. + int duplicate2 = mutate >= 2 ? 1 << ctz(ntx1) : 0; if (duplicate2 >= ntx1) break; int ntx2 = ntx1 + duplicate2; - int duplicate3 = mutate >= 3 ? 1 << ctz(ntx2) : 0; // And for the third mutation. + // And for the third mutation. + int duplicate3 = mutate >= 3 ? 1 << ctz(ntx2) : 0; if (duplicate3 >= ntx2) break; int ntx3 = ntx2 + duplicate3; // Build a block with ntx different transactions. CBlock block; block.vtx.resize(ntx); for (int j = 0; j < ntx; j++) { CMutableTransaction mtx; mtx.nLockTime = j; block.vtx[j] = MakeTransactionRef(std::move(mtx)); } // Compute the root of the block before mutating it. bool unmutatedMutated = false; uint256 unmutatedRoot = BlockMerkleRoot(block, &unmutatedMutated); BOOST_CHECK(unmutatedMutated == false); - // Optionally mutate by duplicating the last transactions, resulting in the same merkle root. + // Optionally mutate by duplicating the last transactions, resulting + // in the same merkle root. block.vtx.resize(ntx3); for (int j = 0; j < duplicate1; j++) { block.vtx[ntx + j] = block.vtx[ntx + j - duplicate1]; } for (int j = 0; j < duplicate2; j++) { block.vtx[ntx1 + j] = block.vtx[ntx1 + j - duplicate2]; } for (int j = 0; j < duplicate3; j++) { block.vtx[ntx2 + j] = block.vtx[ntx2 + j - duplicate3]; } // Compute the merkle root and merkle tree using the old mechanism. bool oldMutated = false; std::vector merkleTree; - uint256 oldRoot = BlockBuildMerkleTree(block, &oldMutated, merkleTree); + uint256 oldRoot = + BlockBuildMerkleTree(block, &oldMutated, merkleTree); // Compute the merkle root using the new mechanism. bool newMutated = false; uint256 newRoot = BlockMerkleRoot(block, &newMutated); BOOST_CHECK(oldRoot == newRoot); BOOST_CHECK(newRoot == unmutatedRoot); BOOST_CHECK((newRoot == uint256()) == (ntx == 0)); BOOST_CHECK(oldMutated == newMutated); BOOST_CHECK(newMutated == !!mutate); - // If no mutation was done (once for every ntx value), try up to 16 branches. + // If no mutation was done (once for every ntx value), try up to 16 + // branches. if (mutate == 0) { for (int loop = 0; loop < std::min(ntx, 16); loop++) { - // If ntx <= 16, try all branches. Otherise, try 16 random ones. + // If ntx <= 16, try all branches. Otherise, try 16 random + // ones. int mtx = loop; if (ntx > 16) { mtx = insecure_rand() % ntx; } - std::vector newBranch = BlockMerkleBranch(block, mtx); - std::vector oldBranch = BlockGetMerkleBranch(block, merkleTree, mtx); + std::vector newBranch = + BlockMerkleBranch(block, mtx); + std::vector oldBranch = + BlockGetMerkleBranch(block, merkleTree, mtx); BOOST_CHECK(oldBranch == newBranch); - BOOST_CHECK(ComputeMerkleRootFromBranch(block.vtx[mtx]->GetId(), newBranch, mtx) == oldRoot); + BOOST_CHECK( + ComputeMerkleRootFromBranch(block.vtx[mtx]->GetId(), + newBranch, mtx) == oldRoot); } } } } } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp index 1afef5b1c..b3d1a5bb6 100644 --- a/src/test/netbase_tests.cpp +++ b/src/test/netbase_tests.cpp @@ -1,287 +1,313 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "netbase.h" #include "test/test_bitcoin.h" #include #include #include BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup) -static CNetAddr ResolveIP(const char* ip) -{ +static CNetAddr ResolveIP(const char *ip) { CNetAddr addr; LookupHost(ip, addr, false); return addr; } -static CSubNet ResolveSubNet(const char* subnet) -{ +static CSubNet ResolveSubNet(const char *subnet) { CSubNet ret; LookupSubNet(subnet, ret); return ret; } -BOOST_AUTO_TEST_CASE(netbase_networks) -{ - BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE); - BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE); - BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4); - BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6); - BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetNetwork() == NET_TOR); - +BOOST_AUTO_TEST_CASE(netbase_networks) { + BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE); + BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE); + BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4); + BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6); + BOOST_CHECK( + ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetNetwork() == + NET_TOR); } -BOOST_AUTO_TEST_CASE(netbase_properties) -{ +BOOST_AUTO_TEST_CASE(netbase_properties) { BOOST_CHECK(ResolveIP("127.0.0.1").IsIPv4()); BOOST_CHECK(ResolveIP("::FFFF:192.168.1.1").IsIPv4()); BOOST_CHECK(ResolveIP("::1").IsIPv6()); BOOST_CHECK(ResolveIP("10.0.0.1").IsRFC1918()); BOOST_CHECK(ResolveIP("192.168.1.1").IsRFC1918()); BOOST_CHECK(ResolveIP("172.31.255.255").IsRFC1918()); BOOST_CHECK(ResolveIP("2001:0DB8::").IsRFC3849()); BOOST_CHECK(ResolveIP("169.254.1.1").IsRFC3927()); BOOST_CHECK(ResolveIP("2002::1").IsRFC3964()); BOOST_CHECK(ResolveIP("FC00::").IsRFC4193()); BOOST_CHECK(ResolveIP("2001::2").IsRFC4380()); BOOST_CHECK(ResolveIP("2001:10::").IsRFC4843()); BOOST_CHECK(ResolveIP("FE80::").IsRFC4862()); BOOST_CHECK(ResolveIP("64:FF9B::").IsRFC6052()); BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").IsTor()); BOOST_CHECK(ResolveIP("127.0.0.1").IsLocal()); BOOST_CHECK(ResolveIP("::1").IsLocal()); BOOST_CHECK(ResolveIP("8.8.8.8").IsRoutable()); BOOST_CHECK(ResolveIP("2001::1").IsRoutable()); BOOST_CHECK(ResolveIP("127.0.0.1").IsValid()); - } -bool static TestSplitHost(std::string test, std::string host, int port) -{ +bool static TestSplitHost(std::string test, std::string host, int port) { std::string hostOut; int portOut = -1; SplitHostPort(test, portOut, hostOut); return hostOut == host && port == portOut; } -BOOST_AUTO_TEST_CASE(netbase_splithost) -{ +BOOST_AUTO_TEST_CASE(netbase_splithost) { BOOST_CHECK(TestSplitHost("www.bitcoin.org", "www.bitcoin.org", -1)); BOOST_CHECK(TestSplitHost("[www.bitcoin.org]", "www.bitcoin.org", -1)); BOOST_CHECK(TestSplitHost("www.bitcoin.org:80", "www.bitcoin.org", 80)); BOOST_CHECK(TestSplitHost("[www.bitcoin.org]:80", "www.bitcoin.org", 80)); BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", -1)); BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333)); BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", -1)); BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333)); BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", -1)); - BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333)); + BOOST_CHECK( + TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333)); BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333)); BOOST_CHECK(TestSplitHost("::8333", "::8333", -1)); BOOST_CHECK(TestSplitHost(":8333", "", 8333)); BOOST_CHECK(TestSplitHost("[]:8333", "", 8333)); BOOST_CHECK(TestSplitHost("", "", -1)); } -bool static TestParse(std::string src, std::string canon) -{ +bool static TestParse(std::string src, std::string canon) { CService addr(LookupNumeric(src.c_str(), 65535)); return canon == addr.ToString(); } -BOOST_AUTO_TEST_CASE(netbase_lookupnumeric) -{ +BOOST_AUTO_TEST_CASE(netbase_lookupnumeric) { BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535")); BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333")); BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535")); BOOST_CHECK(TestParse("::", "[::]:65535")); BOOST_CHECK(TestParse("[::]:8333", "[::]:8333")); BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535")); BOOST_CHECK(TestParse(":::", "[::]:0")); } -BOOST_AUTO_TEST_CASE(onioncat_test) -{ +BOOST_AUTO_TEST_CASE(onioncat_test) { - // values from https://web.archive.org/web/20121122003543/http://www.cypherpunk.at/onioncat/wiki/OnionCat + // values from + // https://web.archive.org/web/20121122003543/http://www.cypherpunk.at/onioncat/wiki/OnionCat CNetAddr addr1(ResolveIP("5wyqrzbvrdsumnok.onion")); CNetAddr addr2(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca")); BOOST_CHECK(addr1 == addr2); BOOST_CHECK(addr1.IsTor()); BOOST_CHECK(addr1.ToStringIP() == "5wyqrzbvrdsumnok.onion"); BOOST_CHECK(addr1.IsRoutable()); - } -BOOST_AUTO_TEST_CASE(subnet_test) -{ +BOOST_AUTO_TEST_CASE(subnet_test) { - BOOST_CHECK(ResolveSubNet("1.2.3.0/24") == ResolveSubNet("1.2.3.0/255.255.255.0")); - BOOST_CHECK(ResolveSubNet("1.2.3.0/24") != ResolveSubNet("1.2.4.0/255.255.255.0")); + BOOST_CHECK(ResolveSubNet("1.2.3.0/24") == + ResolveSubNet("1.2.3.0/255.255.255.0")); + BOOST_CHECK(ResolveSubNet("1.2.3.0/24") != + ResolveSubNet("1.2.4.0/255.255.255.0")); BOOST_CHECK(ResolveSubNet("1.2.3.0/24").Match(ResolveIP("1.2.3.4"))); BOOST_CHECK(!ResolveSubNet("1.2.2.0/24").Match(ResolveIP("1.2.3.4"))); BOOST_CHECK(ResolveSubNet("1.2.3.4").Match(ResolveIP("1.2.3.4"))); BOOST_CHECK(ResolveSubNet("1.2.3.4/32").Match(ResolveIP("1.2.3.4"))); BOOST_CHECK(!ResolveSubNet("1.2.3.4").Match(ResolveIP("5.6.7.8"))); BOOST_CHECK(!ResolveSubNet("1.2.3.4/32").Match(ResolveIP("5.6.7.8"))); - BOOST_CHECK(ResolveSubNet("::ffff:127.0.0.1").Match(ResolveIP("127.0.0.1"))); - BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:8"))); - BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:9"))); - BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:0/112").Match(ResolveIP("1:2:3:4:5:6:7:1234"))); - BOOST_CHECK(ResolveSubNet("192.168.0.1/24").Match(ResolveIP("192.168.0.2"))); - BOOST_CHECK(ResolveSubNet("192.168.0.20/29").Match(ResolveIP("192.168.0.18"))); + BOOST_CHECK( + ResolveSubNet("::ffff:127.0.0.1").Match(ResolveIP("127.0.0.1"))); + BOOST_CHECK( + ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:8"))); + BOOST_CHECK( + !ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:9"))); + BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:0/112") + .Match(ResolveIP("1:2:3:4:5:6:7:1234"))); + BOOST_CHECK( + ResolveSubNet("192.168.0.1/24").Match(ResolveIP("192.168.0.2"))); + BOOST_CHECK( + ResolveSubNet("192.168.0.20/29").Match(ResolveIP("192.168.0.18"))); BOOST_CHECK(ResolveSubNet("1.2.2.1/24").Match(ResolveIP("1.2.2.4"))); BOOST_CHECK(ResolveSubNet("1.2.2.110/31").Match(ResolveIP("1.2.2.111"))); BOOST_CHECK(ResolveSubNet("1.2.2.20/26").Match(ResolveIP("1.2.2.63"))); // All-Matching IPv6 Matches arbitrary IPv4 and IPv6 BOOST_CHECK(ResolveSubNet("::/0").Match(ResolveIP("1:2:3:4:5:6:7:1234"))); BOOST_CHECK(ResolveSubNet("::/0").Match(ResolveIP("1.2.3.4"))); // All-Matching IPv4 does not Match IPv6 - BOOST_CHECK(!ResolveSubNet("0.0.0.0/0").Match(ResolveIP("1:2:3:4:5:6:7:1234"))); + BOOST_CHECK( + !ResolveSubNet("0.0.0.0/0").Match(ResolveIP("1:2:3:4:5:6:7:1234"))); // Invalid subnets Match nothing (not even invalid addresses) BOOST_CHECK(!CSubNet().Match(ResolveIP("1.2.3.4"))); BOOST_CHECK(!ResolveSubNet("").Match(ResolveIP("4.5.6.7"))); BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("0.0.0.0"))); BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("hab"))); // Check valid/invalid BOOST_CHECK(ResolveSubNet("1.2.3.0/0").IsValid()); BOOST_CHECK(!ResolveSubNet("1.2.3.0/-1").IsValid()); BOOST_CHECK(ResolveSubNet("1.2.3.0/32").IsValid()); BOOST_CHECK(!ResolveSubNet("1.2.3.0/33").IsValid()); BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/0").IsValid()); BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/33").IsValid()); BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/-1").IsValid()); BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/128").IsValid()); BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/129").IsValid()); BOOST_CHECK(!ResolveSubNet("fuzzy").IsValid()); - //CNetAddr constructor test + // CNetAddr constructor test BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).IsValid()); BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.1"))); BOOST_CHECK(!CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.2"))); BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).ToString() == "127.0.0.1/32"); CSubNet subnet = CSubNet(ResolveIP("1.2.3.4"), 32); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32"); subnet = CSubNet(ResolveIP("1.2.3.4"), 8); BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8"); subnet = CSubNet(ResolveIP("1.2.3.4"), 0); BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0"); subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.255.255.255")); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32"); subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.0.0.0")); BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8"); subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("0.0.0.0")); BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0"); BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).IsValid()); - BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:8"))); - BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:9"))); - BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/128"); + BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")) + .Match(ResolveIP("1:2:3:4:5:6:7:8"))); + BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")) + .Match(ResolveIP("1:2:3:4:5:6:7:9"))); + BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).ToString() == + "1:2:3:4:5:6:7:8/128"); subnet = ResolveSubNet("1.2.3.4/255.255.255.255"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32"); subnet = ResolveSubNet("1.2.3.4/255.255.255.254"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/31"); subnet = ResolveSubNet("1.2.3.4/255.255.255.252"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/30"); subnet = ResolveSubNet("1.2.3.4/255.255.255.248"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/29"); subnet = ResolveSubNet("1.2.3.4/255.255.255.240"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/28"); subnet = ResolveSubNet("1.2.3.4/255.255.255.224"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/27"); subnet = ResolveSubNet("1.2.3.4/255.255.255.192"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/26"); subnet = ResolveSubNet("1.2.3.4/255.255.255.128"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/25"); subnet = ResolveSubNet("1.2.3.4/255.255.255.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/24"); subnet = ResolveSubNet("1.2.3.4/255.255.254.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.2.0/23"); subnet = ResolveSubNet("1.2.3.4/255.255.252.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/22"); subnet = ResolveSubNet("1.2.3.4/255.255.248.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/21"); subnet = ResolveSubNet("1.2.3.4/255.255.240.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/20"); subnet = ResolveSubNet("1.2.3.4/255.255.224.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/19"); subnet = ResolveSubNet("1.2.3.4/255.255.192.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/18"); subnet = ResolveSubNet("1.2.3.4/255.255.128.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/17"); subnet = ResolveSubNet("1.2.3.4/255.255.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/16"); subnet = ResolveSubNet("1.2.3.4/255.254.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/15"); subnet = ResolveSubNet("1.2.3.4/255.252.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/14"); subnet = ResolveSubNet("1.2.3.4/255.248.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/13"); subnet = ResolveSubNet("1.2.3.4/255.240.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/12"); subnet = ResolveSubNet("1.2.3.4/255.224.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/11"); subnet = ResolveSubNet("1.2.3.4/255.192.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/10"); subnet = ResolveSubNet("1.2.3.4/255.128.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/9"); subnet = ResolveSubNet("1.2.3.4/255.0.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8"); subnet = ResolveSubNet("1.2.3.4/254.0.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/7"); subnet = ResolveSubNet("1.2.3.4/252.0.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/6"); subnet = ResolveSubNet("1.2.3.4/248.0.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/5"); subnet = ResolveSubNet("1.2.3.4/240.0.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/4"); subnet = ResolveSubNet("1.2.3.4/224.0.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/3"); subnet = ResolveSubNet("1.2.3.4/192.0.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/2"); subnet = ResolveSubNet("1.2.3.4/128.0.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/1"); subnet = ResolveSubNet("1.2.3.4/0.0.0.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0"); - subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); + subnet = ResolveSubNet( + "1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/128"); - subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:0000:0000:0000:0000:0000:0000:0000"); + subnet = ResolveSubNet( + "1:2:3:4:5:6:7:8/ffff:0000:0000:0000:0000:0000:0000:0000"); BOOST_CHECK_EQUAL(subnet.ToString(), "1::/16"); - subnet = ResolveSubNet("1:2:3:4:5:6:7:8/0000:0000:0000:0000:0000:0000:0000:0000"); + subnet = ResolveSubNet( + "1:2:3:4:5:6:7:8/0000:0000:0000:0000:0000:0000:0000:0000"); BOOST_CHECK_EQUAL(subnet.ToString(), "::/0"); subnet = ResolveSubNet("1.2.3.4/255.255.232.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/255.255.232.0"); - subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f"); - BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f"); - + subnet = ResolveSubNet( + "1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f"); + BOOST_CHECK_EQUAL( + subnet.ToString(), + "1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f"); } -BOOST_AUTO_TEST_CASE(netbase_getgroup) -{ - - BOOST_CHECK(ResolveIP("127.0.0.1").GetGroup() == boost::assign::list_of(0)); // Local -> !Routable() - BOOST_CHECK(ResolveIP("257.0.0.1").GetGroup() == boost::assign::list_of(0)); // !Valid -> !Routable() - BOOST_CHECK(ResolveIP("10.0.0.1").GetGroup() == boost::assign::list_of(0)); // RFC1918 -> !Routable() - BOOST_CHECK(ResolveIP("169.254.1.1").GetGroup() == boost::assign::list_of(0)); // RFC3927 -> !Routable() - BOOST_CHECK(ResolveIP("1.2.3.4").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // IPv4 - BOOST_CHECK(ResolveIP("::FFFF:0:102:304").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC6145 - BOOST_CHECK(ResolveIP("64:FF9B::102:304").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC6052 - BOOST_CHECK(ResolveIP("2002:102:304:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC3964 - BOOST_CHECK(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC4380 - BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup() == boost::assign::list_of((unsigned char)NET_TOR)(239)); // Tor - BOOST_CHECK(ResolveIP("2001:470:abcd:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV6)(32)(1)(4)(112)(175)); //he.net - BOOST_CHECK(ResolveIP("2001:2001:9999:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV6)(32)(1)(32)(1)); //IPv6 +BOOST_AUTO_TEST_CASE(netbase_getgroup) { + BOOST_CHECK(ResolveIP("127.0.0.1").GetGroup() == + boost::assign::list_of(0)); // Local -> !Routable() + BOOST_CHECK(ResolveIP("257.0.0.1").GetGroup() == + boost::assign::list_of(0)); // !Valid -> !Routable() + BOOST_CHECK(ResolveIP("10.0.0.1").GetGroup() == + boost::assign::list_of(0)); // RFC1918 -> !Routable() + BOOST_CHECK(ResolveIP("169.254.1.1").GetGroup() == + boost::assign::list_of(0)); // RFC3927 -> !Routable() + BOOST_CHECK(ResolveIP("1.2.3.4").GetGroup() == + boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // IPv4 + BOOST_CHECK( + ResolveIP("::FFFF:0:102:304").GetGroup() == + boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC6145 + BOOST_CHECK( + ResolveIP("64:FF9B::102:304").GetGroup() == + boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC6052 + BOOST_CHECK( + ResolveIP("2002:102:304:9999:9999:9999:9999:9999").GetGroup() == + boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC3964 + BOOST_CHECK( + ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup() == + boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC4380 + BOOST_CHECK( + ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup() == + boost::assign::list_of((unsigned char)NET_TOR)(239)); // Tor + BOOST_CHECK( + ResolveIP("2001:470:abcd:9999:9999:9999:9999:9999").GetGroup() == + boost::assign::list_of((unsigned char)NET_IPV6)(32)(1)(4)(112)( + 175)); // he.net + BOOST_CHECK( + ResolveIP("2001:2001:9999:9999:9999:9999:9999:9999").GetGroup() == + boost::assign::list_of((unsigned char)NET_IPV6)(32)(1)(32)(1)); // IPv6 } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/pmt_tests.cpp b/src/test/pmt_tests.cpp index e74cf4b6a..8c245a23b 100644 --- a/src/test/pmt_tests.cpp +++ b/src/test/pmt_tests.cpp @@ -1,127 +1,129 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "arith_uint256.h" #include "consensus/merkle.h" #include "merkleblock.h" #include "serialize.h" #include "streams.h" -#include "uint256.h" -#include "arith_uint256.h" -#include "version.h" #include "test/test_bitcoin.h" #include "test/test_random.h" +#include "uint256.h" +#include "version.h" #include #include #include -class CPartialMerkleTreeTester : public CPartialMerkleTree -{ +class CPartialMerkleTreeTester : public CPartialMerkleTree { public: // flip one bit in one of the hashes - this should break the authentication void Damage() { unsigned int n = insecure_rand() % vHash.size(); int bit = insecure_rand() % 256; - *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7); + *(vHash[n].begin() + (bit >> 3)) ^= 1 << (bit & 7); } }; BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(pmt_test1) -{ +BOOST_AUTO_TEST_CASE(pmt_test1) { seed_insecure_rand(false); - static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095}; + static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, + 127, 256, 312, 513, 1000, 4095}; for (int i = 0; i < 12; i++) { unsigned int nTx = nTxCounts[i]; // build a block with some dummy transactions CBlock block; - for (unsigned int j=0; j vTxid(nTx, uint256()); - for (unsigned int j=0; jGetId(); int nHeight = 1, nTx_ = nTx; while (nTx_ > 1) { - nTx_ = (nTx_+1)/2; + nTx_ = (nTx_ + 1) / 2; nHeight++; } - // check with random subsets with inclusion chances 1, 1/2, 1/4, ..., 1/128 + // check with random subsets with inclusion chances 1, 1/2, 1/4, ..., + // 1/128 for (int att = 1; att < 15; att++) { // build random subset of txid's std::vector vMatch(nTx, false); std::vector vMatchTxid1; - for (unsigned int j=0; j(nTx, 1 + vMatchTxid1.size()*nHeight); - BOOST_CHECK(ss.size() <= 10 + (258*n+7)/8); + unsigned int n = + std::min(nTx, 1 + vMatchTxid1.size() * nHeight); + BOOST_CHECK(ss.size() <= 10 + (258 * n + 7) / 8); // deserialize into a tester copy CPartialMerkleTreeTester pmt2; ss >> pmt2; // extract merkle root and matched txids from copy std::vector vMatchTxid2; std::vector vIndex; uint256 merkleRoot2 = pmt2.ExtractMatches(vMatchTxid2, vIndex); - // check that it has the same merkle root as the original, and a valid one + // check that it has the same merkle root as the original, and a + // valid one BOOST_CHECK(merkleRoot1 == merkleRoot2); BOOST_CHECK(!merkleRoot2.IsNull()); - // check that it contains the matched transactions (in the same order!) + // check that it contains the matched transactions (in the same + // order!) BOOST_CHECK(vMatchTxid1 == vMatchTxid2); // check that random bit flips break the authentication - for (int j=0; j<4; j++) { + for (int j = 0; j < 4; j++) { CPartialMerkleTreeTester pmt3(pmt2); pmt3.Damage(); std::vector vMatchTxid3; uint256 merkleRoot3 = pmt3.ExtractMatches(vMatchTxid3, vIndex); BOOST_CHECK(merkleRoot3 != merkleRoot1); } } } } -BOOST_AUTO_TEST_CASE(pmt_malleability) -{ - std::vector vTxid = boost::assign::list_of - (ArithToUint256(1))(ArithToUint256(2)) - (ArithToUint256(3))(ArithToUint256(4)) - (ArithToUint256(5))(ArithToUint256(6)) - (ArithToUint256(7))(ArithToUint256(8)) - (ArithToUint256(9))(ArithToUint256(10)) - (ArithToUint256(9))(ArithToUint256(10)); - std::vector vMatch = boost::assign::list_of(false)(false)(false)(false)(false)(false)(false)(false)(false)(true)(true)(false); +BOOST_AUTO_TEST_CASE(pmt_malleability) { + std::vector vTxid = boost::assign::list_of(ArithToUint256(1))( + ArithToUint256(2))(ArithToUint256(3))(ArithToUint256(4))( + ArithToUint256(5))(ArithToUint256(6))(ArithToUint256(7))( + ArithToUint256(8))(ArithToUint256(9))(ArithToUint256(10))( + ArithToUint256(9))(ArithToUint256(10)); + std::vector vMatch = boost::assign::list_of(false)(false)(false)( + false)(false)(false)(false)(false)(false)(true)(true)(false); CPartialMerkleTree tree(vTxid, vMatch); std::vector vIndex; BOOST_CHECK(tree.ExtractMatches(vTxid, vIndex).IsNull()); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/policyestimator_tests.cpp b/src/test/policyestimator_tests.cpp index cede6c8bd..d0744e988 100644 --- a/src/test/policyestimator_tests.cpp +++ b/src/test/policyestimator_tests.cpp @@ -1,200 +1,240 @@ // Copyright (c) 2011-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "policy/policy.h" #include "policy/fees.h" #include "txmempool.h" #include "uint256.h" #include "util.h" #include "test/test_bitcoin.h" #include BOOST_FIXTURE_TEST_SUITE(policyestimator_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(BlockPolicyEstimates) -{ +BOOST_AUTO_TEST_CASE(BlockPolicyEstimates) { CTxMemPool mpool(CFeeRate(1000)); TestMemPoolEntryHelper entry; CAmount basefee(2000); CAmount deltaFee(100); std::vector feeV; // Populate vectors of increasing fees for (int j = 0; j < 10; j++) { - feeV.push_back(basefee * (j+1)); + feeV.push_back(basefee * (j + 1)); } - // Store the hashes of transactions that have been - // added to the mempool by their associate fee - // txHashes[j] is populated with transactions either of + // Store the hashes of transactions that have been added to the mempool by + // their associate fee txHashes[j] is populated with transactions either of // fee = basefee * (j+1) std::vector txHashes[10]; // Create a transaction template CScript garbage; for (unsigned int i = 0; i < 128; i++) garbage.push_back('X'); CMutableTransaction tx; tx.vin.resize(1); tx.vin[0].scriptSig = garbage; tx.vout.resize(1); - tx.vout[0].nValue=0LL; + tx.vout[0].nValue = 0LL; CFeeRate baseRate(basefee, GetTransactionSize(tx)); // Create a fake block std::vector block; int blocknum = 0; // Loop through 200 blocks // At a decay .998 and 4 fee transactions per block // This makes the tx count about 1.33 per bucket, above the 1 threshold while (blocknum < 200) { - for (int j = 0; j < 10; j++) { // For each fee - for (int k = 0; k < 4; k++) { // add 4 fee txs - tx.vin[0].prevout.n = 10000*blocknum+100*j+k; // make transaction unique + // For each fee + for (int j = 0; j < 10; j++) { + // add 4 fee txs + for (int k = 0; k < 4; k++) { + // make transaction unique + tx.vin[0].prevout.n = 10000 * blocknum + 100 * j + k; uint256 hash = tx.GetId(); - mpool.addUnchecked(hash, entry.Fee(feeV[j]).Time(GetTime()).Priority(0).Height(blocknum).FromTx(tx, &mpool)); + mpool.addUnchecked(hash, entry.Fee(feeV[j]) + .Time(GetTime()) + .Priority(0) + .Height(blocknum) + .FromTx(tx, &mpool)); txHashes[j].push_back(hash); } } - //Create blocks where higher fee txs are included more often - for (int h = 0; h <= blocknum%10; h++) { + // Create blocks where higher fee txs are included more often + for (int h = 0; h <= blocknum % 10; h++) { // 10/10 blocks add highest fee transactions // 9/10 blocks add 2nd highest and so on until ... // 1/10 blocks add lowest fee transactions - while (txHashes[9-h].size()) { - CTransactionRef ptx = mpool.get(txHashes[9-h].back()); - if (ptx) - block.push_back(ptx); - txHashes[9-h].pop_back(); + while (txHashes[9 - h].size()) { + CTransactionRef ptx = mpool.get(txHashes[9 - h].back()); + if (ptx) block.push_back(ptx); + txHashes[9 - h].pop_back(); } } mpool.removeForBlock(block, ++blocknum); block.clear(); if (blocknum == 30) { - // At this point we should need to combine 5 buckets to get enough data points - // So estimateFee(1,2,3) should fail and estimateFee(4) should return somewhere around - // 8*baserate. estimateFee(4) %'s are 100,100,100,100,90 = average 98% + // At this point we should need to combine 5 buckets to get enough + // data points. So estimateFee(1,2,3) should fail and estimateFee(4) + // should return somewhere around 8*baserate. estimateFee(4) %'s + // are 100,100,100,100,90 = average 98% BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0)); BOOST_CHECK(mpool.estimateFee(2) == CFeeRate(0)); BOOST_CHECK(mpool.estimateFee(3) == CFeeRate(0)); - BOOST_CHECK(mpool.estimateFee(4).GetFeePerK() < 8*baseRate.GetFeePerK() + deltaFee); - BOOST_CHECK(mpool.estimateFee(4).GetFeePerK() > 8*baseRate.GetFeePerK() - deltaFee); + BOOST_CHECK(mpool.estimateFee(4).GetFeePerK() < + 8 * baseRate.GetFeePerK() + deltaFee); + BOOST_CHECK(mpool.estimateFee(4).GetFeePerK() > + 8 * baseRate.GetFeePerK() - deltaFee); int answerFound; - BOOST_CHECK(mpool.estimateSmartFee(1, &answerFound) == mpool.estimateFee(4) && answerFound == 4); - BOOST_CHECK(mpool.estimateSmartFee(3, &answerFound) == mpool.estimateFee(4) && answerFound == 4); - BOOST_CHECK(mpool.estimateSmartFee(4, &answerFound) == mpool.estimateFee(4) && answerFound == 4); - BOOST_CHECK(mpool.estimateSmartFee(8, &answerFound) == mpool.estimateFee(8) && answerFound == 8); + BOOST_CHECK(mpool.estimateSmartFee(1, &answerFound) == + mpool.estimateFee(4) && + answerFound == 4); + BOOST_CHECK(mpool.estimateSmartFee(3, &answerFound) == + mpool.estimateFee(4) && + answerFound == 4); + BOOST_CHECK(mpool.estimateSmartFee(4, &answerFound) == + mpool.estimateFee(4) && + answerFound == 4); + BOOST_CHECK(mpool.estimateSmartFee(8, &answerFound) == + mpool.estimateFee(8) && + answerFound == 8); } } std::vector origFeeEst; - // Highest feerate is 10*baseRate and gets in all blocks, - // second highest feerate is 9*baseRate and gets in 9/10 blocks = 90%, - // third highest feerate is 8*base rate, and gets in 8/10 blocks = 80%, - // so estimateFee(1) would return 10*baseRate but is hardcoded to return failure - // Second highest feerate has 100% chance of being included by 2 blocks, - // so estimateFee(2) should return 9*baseRate etc... - for (int i = 1; i < 10;i++) { + // Highest feerate is 10*baseRate and gets in all blocks, second highest + // feerate is 9*baseRate and gets in 9/10 blocks = 90%, third highest + // feerate is 8*base rate, and gets in 8/10 blocks = 80%, so estimateFee(1) + // would return 10*baseRate but is hardcoded to return failure. Second + // highest feerate has 100% chance of being included by 2 blocks, so + // estimateFee(2) should return 9*baseRate etc... + for (int i = 1; i < 10; i++) { origFeeEst.push_back(mpool.estimateFee(i).GetFeePerK()); - if (i > 2) { // Fee estimates should be monotonically decreasing - BOOST_CHECK(origFeeEst[i-1] <= origFeeEst[i-2]); + // Fee estimates should be monotonically decreasing + if (i > 2) { + BOOST_CHECK(origFeeEst[i - 1] <= origFeeEst[i - 2]); } - int mult = 11-i; + int mult = 11 - i; if (i > 1) { - BOOST_CHECK(origFeeEst[i-1] < mult*baseRate.GetFeePerK() + deltaFee); - BOOST_CHECK(origFeeEst[i-1] > mult*baseRate.GetFeePerK() - deltaFee); - } - else { - BOOST_CHECK(origFeeEst[i-1] == CFeeRate(0).GetFeePerK()); + BOOST_CHECK(origFeeEst[i - 1] < + mult * baseRate.GetFeePerK() + deltaFee); + BOOST_CHECK(origFeeEst[i - 1] > + mult * baseRate.GetFeePerK() - deltaFee); + } else { + BOOST_CHECK(origFeeEst[i - 1] == CFeeRate(0).GetFeePerK()); } } - // Mine 50 more blocks with no transactions happening, estimates shouldn't change - // We haven't decayed the moving average enough so we still have enough data points in every bucket + // Mine 50 more blocks with no transactions happening, estimates shouldn't + // change. We haven't decayed the moving average enough so we still have + // enough data points in every bucket while (blocknum < 250) mpool.removeForBlock(block, ++blocknum); BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0)); - for (int i = 2; i < 10;i++) { - BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < origFeeEst[i-1] + deltaFee); - BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee); + for (int i = 2; i < 10; i++) { + BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < + origFeeEst[i - 1] + deltaFee); + BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > + origFeeEst[i - 1] - deltaFee); } - - // Mine 15 more blocks with lots of transactions happening and not getting mined - // Estimates should go up + // Mine 15 more blocks with lots of transactions happening and not getting + // mined. Estimates should go up while (blocknum < 265) { - for (int j = 0; j < 10; j++) { // For each fee multiple - for (int k = 0; k < 4; k++) { // add 4 fee txs - tx.vin[0].prevout.n = 10000*blocknum+100*j+k; + // For each fee multiple + for (int j = 0; j < 10; j++) { + // add 4 fee txs + for (int k = 0; k < 4; k++) { + tx.vin[0].prevout.n = 10000 * blocknum + 100 * j + k; uint256 txid = tx.GetId(); - mpool.addUnchecked(txid, entry.Fee(feeV[j]).Time(GetTime()).Priority(0).Height(blocknum).FromTx(tx, &mpool)); + mpool.addUnchecked(txid, entry.Fee(feeV[j]) + .Time(GetTime()) + .Priority(0) + .Height(blocknum) + .FromTx(tx, &mpool)); txHashes[j].push_back(txid); } } mpool.removeForBlock(block, ++blocknum); } int answerFound; - for (int i = 1; i < 10;i++) { - BOOST_CHECK(mpool.estimateFee(i) == CFeeRate(0) || mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee); - BOOST_CHECK(mpool.estimateSmartFee(i, &answerFound).GetFeePerK() > origFeeEst[answerFound-1] - deltaFee); + for (int i = 1; i < 10; i++) { + BOOST_CHECK(mpool.estimateFee(i) == CFeeRate(0) || + mpool.estimateFee(i).GetFeePerK() > + origFeeEst[i - 1] - deltaFee); + BOOST_CHECK(mpool.estimateSmartFee(i, &answerFound).GetFeePerK() > + origFeeEst[answerFound - 1] - deltaFee); } // Mine all those transactions // Estimates should still not be below original for (int j = 0; j < 10; j++) { - while(txHashes[j].size()) { + while (txHashes[j].size()) { CTransactionRef ptx = mpool.get(txHashes[j].back()); - if (ptx) - block.push_back(ptx); + if (ptx) block.push_back(ptx); txHashes[j].pop_back(); } } mpool.removeForBlock(block, 265); block.clear(); BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0)); - for (int i = 2; i < 10;i++) { - BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee); + for (int i = 2; i < 10; i++) { + BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > + origFeeEst[i - 1] - deltaFee); } // Mine 200 more blocks where everything is mined every block // Estimates should be below original estimates while (blocknum < 465) { - for (int j = 0; j < 10; j++) { // For each fee multiple - for (int k = 0; k < 4; k++) { // add 4 fee txs - tx.vin[0].prevout.n = 10000*blocknum+100*j+k; + // For each fee multiple + for (int j = 0; j < 10; j++) { + // add 4 fee txs + for (int k = 0; k < 4; k++) { + tx.vin[0].prevout.n = 10000 * blocknum + 100 * j + k; uint256 txid = tx.GetId(); - mpool.addUnchecked(txid, entry.Fee(feeV[j]).Time(GetTime()).Priority(0).Height(blocknum).FromTx(tx, &mpool)); + mpool.addUnchecked(txid, entry.Fee(feeV[j]) + .Time(GetTime()) + .Priority(0) + .Height(blocknum) + .FromTx(tx, &mpool)); CTransactionRef ptx = mpool.get(txid); - if (ptx) - block.push_back(ptx); - + if (ptx) block.push_back(ptx); } } mpool.removeForBlock(block, ++blocknum); block.clear(); } BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0)); for (int i = 2; i < 10; i++) { - BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < origFeeEst[i-1] - deltaFee); + BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < + origFeeEst[i - 1] - deltaFee); } - // Test that if the mempool is limited, estimateSmartFee won't return a value below the mempool min fee - // and that estimateSmartPriority returns essentially an infinite value - mpool.addUnchecked(tx.GetId(), entry.Fee(feeV[5]).Time(GetTime()).Priority(0).Height(blocknum).FromTx(tx, &mpool)); - // evict that transaction which should set a mempool min fee of minRelayTxFee + feeV[5] + // Test that if the mempool is limited, estimateSmartFee won't return a + // value below the mempool min fee and that estimateSmartPriority returns + // essentially an infinite value + mpool.addUnchecked( + tx.GetId(), + entry.Fee(feeV[5]).Time(GetTime()).Priority(0).Height(blocknum).FromTx( + tx, &mpool)); + // evict that transaction which should set a mempool min fee of + // minRelayTxFee + feeV[5] mpool.TrimToSize(1); BOOST_CHECK(mpool.GetMinFee(1).GetFeePerK() > feeV[5]); for (int i = 1; i < 10; i++) { - BOOST_CHECK(mpool.estimateSmartFee(i).GetFeePerK() >= mpool.estimateFee(i).GetFeePerK()); - BOOST_CHECK(mpool.estimateSmartFee(i).GetFeePerK() >= mpool.GetMinFee(1).GetFeePerK()); + BOOST_CHECK(mpool.estimateSmartFee(i).GetFeePerK() >= + mpool.estimateFee(i).GetFeePerK()); + BOOST_CHECK(mpool.estimateSmartFee(i).GetFeePerK() >= + mpool.GetMinFee(1).GetFeePerK()); BOOST_CHECK(mpool.estimateSmartPriority(i) == INF_PRIORITY); } } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/pow_tests.cpp b/src/test/pow_tests.cpp index 4ca6f1caf..ccf8c9e49 100644 --- a/src/test/pow_tests.cpp +++ b/src/test/pow_tests.cpp @@ -1,96 +1,101 @@ // Copyright (c) 2015 The Bitcoin Core developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "pow.h" #include "chain.h" #include "chainparams.h" -#include "pow.h" #include "random.h" -#include "util.h" #include "test/test_bitcoin.h" +#include "util.h" #include BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup) /* Test calculation of next difficulty target with no constraints applying */ -BOOST_AUTO_TEST_CASE(get_next_work) -{ +BOOST_AUTO_TEST_CASE(get_next_work) { SelectParams(CBaseChainParams::MAIN); - const Consensus::Params& params = Params().GetConsensus(); + const Consensus::Params ¶ms = Params().GetConsensus(); int64_t nLastRetargetTime = 1261130161; // Block #30240 CBlockIndex pindexLast; pindexLast.nHeight = 32255; - pindexLast.nTime = 1262152739; // Block #32255 + pindexLast.nTime = 1262152739; // Block #32255 pindexLast.nBits = 0x1d00ffff; - BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00d86a); + BOOST_CHECK_EQUAL( + CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), + 0x1d00d86a); } /* Test the constraint on the upper bound for next work */ -BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) -{ +BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) { SelectParams(CBaseChainParams::MAIN); - const Consensus::Params& params = Params().GetConsensus(); + const Consensus::Params ¶ms = Params().GetConsensus(); int64_t nLastRetargetTime = 1231006505; // Block #0 CBlockIndex pindexLast; pindexLast.nHeight = 2015; - pindexLast.nTime = 1233061996; // Block #2015 + pindexLast.nTime = 1233061996; // Block #2015 pindexLast.nBits = 0x1d00ffff; - BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00ffff); + BOOST_CHECK_EQUAL( + CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), + 0x1d00ffff); } /* Test the constraint on the lower bound for actual time taken */ -BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) -{ +BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) { SelectParams(CBaseChainParams::MAIN); - const Consensus::Params& params = Params().GetConsensus(); + const Consensus::Params ¶ms = Params().GetConsensus(); int64_t nLastRetargetTime = 1279008237; // Block #66528 CBlockIndex pindexLast; pindexLast.nHeight = 68543; - pindexLast.nTime = 1279297671; // Block #68543 + pindexLast.nTime = 1279297671; // Block #68543 pindexLast.nBits = 0x1c05a3f4; - BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c0168fd); + BOOST_CHECK_EQUAL( + CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), + 0x1c0168fd); } /* Test the constraint on the upper bound for actual time taken */ -BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) -{ +BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) { SelectParams(CBaseChainParams::MAIN); - const Consensus::Params& params = Params().GetConsensus(); + const Consensus::Params ¶ms = Params().GetConsensus(); int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time CBlockIndex pindexLast; pindexLast.nHeight = 46367; - pindexLast.nTime = 1269211443; // Block #46367 + pindexLast.nTime = 1269211443; // Block #46367 pindexLast.nBits = 0x1c387f6f; - BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00e1fd); + BOOST_CHECK_EQUAL( + CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), + 0x1d00e1fd); } -BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test) -{ +BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test) { SelectParams(CBaseChainParams::MAIN); - const Consensus::Params& params = Params().GetConsensus(); + const Consensus::Params ¶ms = Params().GetConsensus(); std::vector blocks(10000); for (int i = 0; i < 10000; i++) { blocks[i].pprev = i ? &blocks[i - 1] : NULL; blocks[i].nHeight = i; blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing; blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */ - blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0); + blocks[i].nChainWork = + i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) + : arith_uint256(0); } for (int j = 0; j < 1000; j++) { CBlockIndex *p1 = &blocks[GetRand(10000)]; CBlockIndex *p2 = &blocks[GetRand(10000)]; CBlockIndex *p3 = &blocks[GetRand(10000)]; int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, params); BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime()); } } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/raii_event_tests.cpp b/src/test/raii_event_tests.cpp index 0f40874f5..6930d4e65 100644 --- a/src/test/raii_event_tests.cpp +++ b/src/test/raii_event_tests.cpp @@ -1,94 +1,94 @@ // Copyright (c) 2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED -// It would probably be ideal to define dummy test(s) that report skipped, but boost::test doesn't seem to make that practical (at least not in versions available with common distros) +// It would probably be ideal to define dummy test(s) that report skipped, but +// boost::test doesn't seem to make that practical (at least not in versions +// available with common distros) #include #include #include "support/events.h" #include "test/test_bitcoin.h" #include #include -static std::map tags; -static std::map orders; +static std::map tags; +static std::map orders; static uint16_t tagSequence = 0; -static void* tag_malloc(size_t sz) { - void* mem = malloc(sz); +static void *tag_malloc(size_t sz) { + void *mem = malloc(sz); if (!mem) return mem; tags[mem]++; orders[mem] = tagSequence++; return mem; } -static void tag_free(void* mem) { +static void tag_free(void *mem) { tags[mem]--; orders[mem] = tagSequence++; free(mem); } BOOST_FIXTURE_TEST_SUITE(raii_event_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(raii_event_creation) -{ +BOOST_AUTO_TEST_CASE(raii_event_creation) { event_set_mem_functions(tag_malloc, realloc, tag_free); - - void* base_ptr = NULL; + + void *base_ptr = NULL; { auto base = obtain_event_base(); - base_ptr = (void*)base.get(); + base_ptr = (void *)base.get(); BOOST_CHECK(tags[base_ptr] == 1); } BOOST_CHECK(tags[base_ptr] == 0); - - void* event_ptr = NULL; + + void *event_ptr = NULL; { auto base = obtain_event_base(); auto event = obtain_event(base.get(), -1, 0, NULL, NULL); - base_ptr = (void*)base.get(); - event_ptr = (void*)event.get(); + base_ptr = (void *)base.get(); + event_ptr = (void *)event.get(); BOOST_CHECK(tags[base_ptr] == 1); BOOST_CHECK(tags[event_ptr] == 1); } BOOST_CHECK(tags[base_ptr] == 0); BOOST_CHECK(tags[event_ptr] == 0); - + event_set_mem_functions(malloc, realloc, free); } -BOOST_AUTO_TEST_CASE(raii_event_order) -{ +BOOST_AUTO_TEST_CASE(raii_event_order) { event_set_mem_functions(tag_malloc, realloc, tag_free); - - void* base_ptr = NULL; - void* event_ptr = NULL; + + void *base_ptr = NULL; + void *event_ptr = NULL; { auto base = obtain_event_base(); auto event = obtain_event(base.get(), -1, 0, NULL, NULL); - base_ptr = (void*)base.get(); - event_ptr = (void*)event.get(); + base_ptr = (void *)base.get(); + event_ptr = (void *)event.get(); // base should have allocated before event BOOST_CHECK(orders[base_ptr] < orders[event_ptr]); } // base should be freed after event BOOST_CHECK(orders[base_ptr] > orders[event_ptr]); event_set_mem_functions(malloc, realloc, free); } BOOST_AUTO_TEST_SUITE_END() -#endif // EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED +#endif // EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED diff --git a/src/test/reverselock_tests.cpp b/src/test/reverselock_tests.cpp index 00dc47e13..c33b16adc 100644 --- a/src/test/reverselock_tests.cpp +++ b/src/test/reverselock_tests.cpp @@ -1,60 +1,58 @@ // Copyright (c) 2015-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "reverselock.h" #include "test/test_bitcoin.h" #include BOOST_FIXTURE_TEST_SUITE(reverselock_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(reverselock_basics) -{ +BOOST_AUTO_TEST_CASE(reverselock_basics) { boost::mutex mutex; boost::unique_lock lock(mutex); BOOST_CHECK(lock.owns_lock()); { - reverse_lock > rlock(lock); + reverse_lock> rlock(lock); BOOST_CHECK(!lock.owns_lock()); } BOOST_CHECK(lock.owns_lock()); } -BOOST_AUTO_TEST_CASE(reverselock_errors) -{ +BOOST_AUTO_TEST_CASE(reverselock_errors) { boost::mutex mutex; boost::unique_lock lock(mutex); // Make sure trying to reverse lock an unlocked lock fails lock.unlock(); BOOST_CHECK(!lock.owns_lock()); bool failed = false; try { - reverse_lock > rlock(lock); - } catch(...) { + reverse_lock> rlock(lock); + } catch (...) { failed = true; } BOOST_CHECK(failed); BOOST_CHECK(!lock.owns_lock()); // Locking the original lock after it has been taken by a reverse lock // makes no sense. Ensure that the original lock no longer owns the lock // after giving it to a reverse one. lock.lock(); BOOST_CHECK(lock.owns_lock()); { - reverse_lock > rlock(lock); + reverse_lock> rlock(lock); BOOST_CHECK(!lock.owns_lock()); } BOOST_CHECK(failed); BOOST_CHECK(lock.owns_lock()); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/sanity_tests.cpp b/src/test/sanity_tests.cpp index 51f9e9f39..eaf74d020 100644 --- a/src/test/sanity_tests.cpp +++ b/src/test/sanity_tests.cpp @@ -1,20 +1,19 @@ // Copyright (c) 2012-2015 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "compat/sanity.h" #include "key.h" #include "test/test_bitcoin.h" #include BOOST_FIXTURE_TEST_SUITE(sanity_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(basic_sanity) -{ - BOOST_CHECK_MESSAGE(glibc_sanity_test() == true, "libc sanity test"); - BOOST_CHECK_MESSAGE(glibcxx_sanity_test() == true, "stdlib sanity test"); - BOOST_CHECK_MESSAGE(ECC_InitSanityCheck() == true, "openssl ECC test"); +BOOST_AUTO_TEST_CASE(basic_sanity) { + BOOST_CHECK_MESSAGE(glibc_sanity_test() == true, "libc sanity test"); + BOOST_CHECK_MESSAGE(glibcxx_sanity_test() == true, "stdlib sanity test"); + BOOST_CHECK_MESSAGE(ECC_InitSanityCheck() == true, "openssl ECC test"); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/scheduler_tests.cpp b/src/test/scheduler_tests.cpp index e4ddf9d61..69f7baea5 100644 --- a/src/test/scheduler_tests.cpp +++ b/src/test/scheduler_tests.cpp @@ -1,117 +1,129 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "random.h" #include "scheduler.h" +#include "random.h" #include "test/test_bitcoin.h" #include #include #include -#include #include +#include BOOST_AUTO_TEST_SUITE(scheduler_tests) -static void microTask(CScheduler& s, boost::mutex& mutex, int& counter, int delta, boost::chrono::system_clock::time_point rescheduleTime) -{ +static void microTask(CScheduler &s, boost::mutex &mutex, int &counter, + int delta, + boost::chrono::system_clock::time_point rescheduleTime) { { boost::unique_lock lock(mutex); counter += delta; } - boost::chrono::system_clock::time_point noTime = boost::chrono::system_clock::time_point::min(); + boost::chrono::system_clock::time_point noTime = + boost::chrono::system_clock::time_point::min(); if (rescheduleTime != noTime) { - CScheduler::Function f = boost::bind(µTask, boost::ref(s), boost::ref(mutex), boost::ref(counter), -delta + 1, noTime); + CScheduler::Function f = + boost::bind(µTask, boost::ref(s), boost::ref(mutex), + boost::ref(counter), -delta + 1, noTime); s.schedule(f, rescheduleTime); } } -static void MicroSleep(uint64_t n) -{ +static void MicroSleep(uint64_t n) { #if defined(HAVE_WORKING_BOOST_SLEEP_FOR) boost::this_thread::sleep_for(boost::chrono::microseconds(n)); #elif defined(HAVE_WORKING_BOOST_SLEEP) boost::this_thread::sleep(boost::posix_time::microseconds(n)); #else - //should never get here - #error missing boost sleep implementation +// should never get here +#error missing boost sleep implementation #endif } -BOOST_AUTO_TEST_CASE(manythreads) -{ +BOOST_AUTO_TEST_CASE(manythreads) { // Stress test: hundreds of microsecond-scheduled tasks, // serviced by 10 threads. // // So... ten shared counters, which if all the tasks execute // properly will sum to the number of tasks done. // Each task adds or subtracts a random amount from one of the // counters, and then schedules another task 0-1000 // microseconds in the future to subtract or add from // the counter -random_amount+1, so in the end the shared // counters should sum to the number of initial tasks performed. CScheduler microTasks; boost::mutex counterMutex[10]; - int counter[10] = { 0 }; + int counter[10] = {0}; boost::random::mt19937 rng(42); boost::random::uniform_int_distribution<> zeroToNine(0, 9); boost::random::uniform_int_distribution<> randomMsec(-11, 1000); boost::random::uniform_int_distribution<> randomDelta(-1000, 1000); - boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now(); + boost::chrono::system_clock::time_point start = + boost::chrono::system_clock::now(); boost::chrono::system_clock::time_point now = start; boost::chrono::system_clock::time_point first, last; size_t nTasks = microTasks.getQueueInfo(first, last); BOOST_CHECK(nTasks == 0); for (int i = 0; i < 100; i++) { - boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng)); - boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng)); + boost::chrono::system_clock::time_point t = + now + boost::chrono::microseconds(randomMsec(rng)); + boost::chrono::system_clock::time_point tReschedule = + now + boost::chrono::microseconds(500 + randomMsec(rng)); int whichCounter = zeroToNine(rng); - CScheduler::Function f = boost::bind(µTask, boost::ref(microTasks), - boost::ref(counterMutex[whichCounter]), boost::ref(counter[whichCounter]), - randomDelta(rng), tReschedule); + CScheduler::Function f = boost::bind( + µTask, boost::ref(microTasks), + boost::ref(counterMutex[whichCounter]), + boost::ref(counter[whichCounter]), randomDelta(rng), tReschedule); microTasks.schedule(f, t); } nTasks = microTasks.getQueueInfo(first, last); BOOST_CHECK(nTasks == 100); BOOST_CHECK(first < last); BOOST_CHECK(last > now); - // As soon as these are created they will start running and servicing the queue + // As soon as these are created they will start running and servicing the + // queue boost::thread_group microThreads; for (int i = 0; i < 5; i++) - microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, µTasks)); + microThreads.create_thread( + boost::bind(&CScheduler::serviceQueue, µTasks)); MicroSleep(600); now = boost::chrono::system_clock::now(); // More threads and more tasks: for (int i = 0; i < 5; i++) - microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, µTasks)); + microThreads.create_thread( + boost::bind(&CScheduler::serviceQueue, µTasks)); for (int i = 0; i < 100; i++) { - boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng)); - boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng)); + boost::chrono::system_clock::time_point t = + now + boost::chrono::microseconds(randomMsec(rng)); + boost::chrono::system_clock::time_point tReschedule = + now + boost::chrono::microseconds(500 + randomMsec(rng)); int whichCounter = zeroToNine(rng); - CScheduler::Function f = boost::bind(µTask, boost::ref(microTasks), - boost::ref(counterMutex[whichCounter]), boost::ref(counter[whichCounter]), - randomDelta(rng), tReschedule); + CScheduler::Function f = boost::bind( + µTask, boost::ref(microTasks), + boost::ref(counterMutex[whichCounter]), + boost::ref(counter[whichCounter]), randomDelta(rng), tReschedule); microTasks.schedule(f, t); } // Drain the task queue then exit threads microTasks.stop(true); microThreads.join_all(); // ... wait until all the threads are done int counterSum = 0; for (int i = 0; i < 10; i++) { BOOST_CHECK(counter[i] != 0); counterSum += counter[i]; } BOOST_CHECK_EQUAL(counterSum, 200); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/script_P2SH_tests.cpp b/src/test/script_P2SH_tests.cpp index 7587edc9b..32771c870 100644 --- a/src/test/script_P2SH_tests.cpp +++ b/src/test/script_P2SH_tests.cpp @@ -1,368 +1,429 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "script/script.h" #include "core_io.h" #include "key.h" #include "keystore.h" -#include "validation.h" #include "policy/policy.h" -#include "script/script.h" +#include "script/ismine.h" #include "script/script_error.h" #include "script/sign.h" -#include "script/ismine.h" #include "test/test_bitcoin.h" +#include "validation.h" #include #include // Helpers: -static std::vector -Serialize(const CScript& s) -{ +static std::vector Serialize(const CScript &s) { std::vector sSerialized(s.begin(), s.end()); return sSerialized; } -static bool -Verify(const CScript& scriptSig, const CScript& scriptPubKey, bool fStrict, ScriptError& err) -{ +static bool Verify(const CScript &scriptSig, const CScript &scriptPubKey, + bool fStrict, ScriptError &err) { // Create dummy to/from transactions: CMutableTransaction txFrom; txFrom.vout.resize(1); txFrom.vout[0].scriptPubKey = scriptPubKey; CMutableTransaction txTo; txTo.vin.resize(1); txTo.vout.resize(1); txTo.vin[0].prevout.n = 0; txTo.vin[0].prevout.hash = txFrom.GetId(); txTo.vin[0].scriptSig = scriptSig; txTo.vout[0].nValue = 1; - return VerifyScript(scriptSig, scriptPubKey, fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE, MutableTransactionSignatureChecker(&txTo, 0, txFrom.vout[0].nValue), &err); + return VerifyScript( + scriptSig, scriptPubKey, + fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE, + MutableTransactionSignatureChecker(&txTo, 0, txFrom.vout[0].nValue), + &err); } - BOOST_FIXTURE_TEST_SUITE(script_P2SH_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(sign) -{ +BOOST_AUTO_TEST_CASE(sign) { LOCK(cs_main); // Pay-to-script-hash looks like this: // scriptSig: // scriptPubKey: HASH160 EQUAL - // Test SignSignature() (and therefore the version of Solver() that signs transactions) + // Test SignSignature() (and therefore the version of Solver() that signs + // transactions) CBasicKeyStore keystore; CKey key[4]; - for (int i = 0; i < 4; i++) - { + for (int i = 0; i < 4; i++) { key[i].MakeNewKey(true); keystore.AddKey(key[i]); } // 8 Scripts: checking all combinations of // different keys, straight/P2SH, pubkey/pubkeyhash CScript standardScripts[4]; standardScripts[0] << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG; standardScripts[1] = GetScriptForDestination(key[1].GetPubKey().GetID()); standardScripts[2] << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG; standardScripts[3] = GetScriptForDestination(key[2].GetPubKey().GetID()); CScript evalScripts[4]; - for (int i = 0; i < 4; i++) - { + for (int i = 0; i < 4; i++) { keystore.AddCScript(standardScripts[i]); evalScripts[i] = GetScriptForDestination(CScriptID(standardScripts[i])); } - CMutableTransaction txFrom; // Funding transaction: + CMutableTransaction txFrom; // Funding transaction: std::string reason; txFrom.vout.resize(8); - for (int i = 0; i < 4; i++) - { + for (int i = 0; i < 4; i++) { txFrom.vout[i].scriptPubKey = evalScripts[i]; txFrom.vout[i].nValue = COIN; - txFrom.vout[i+4].scriptPubKey = standardScripts[i]; - txFrom.vout[i+4].nValue = COIN; + txFrom.vout[i + 4].scriptPubKey = standardScripts[i]; + txFrom.vout[i + 4].nValue = COIN; } BOOST_CHECK(IsStandardTx(txFrom, reason)); CMutableTransaction txTo[8]; // Spending transactions - for (int i = 0; i < 8; i++) - { + for (int i = 0; i < 8; i++) { txTo[i].vin.resize(1); txTo[i].vout.resize(1); txTo[i].vin[0].prevout.n = i; txTo[i].vin[0].prevout.hash = txFrom.GetId(); txTo[i].vout[0].nValue = 1; - BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i)); + BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), + strprintf("IsMine %d", i)); } - for (int i = 0; i < 8; i++) - { - BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0, SIGHASH_ALL), strprintf("SignSignature %d", i)); + for (int i = 0; i < 8; i++) { + BOOST_CHECK_MESSAGE( + SignSignature(keystore, txFrom, txTo[i], 0, SIGHASH_ALL), + strprintf("SignSignature %d", i)); } // All of the above should be OK, and the txTos have valid signatures - // Check to make sure signature verification fails if we use the wrong ScriptSig: + // Check to make sure signature verification fails if we use the wrong + // ScriptSig: for (int i = 0; i < 8; i++) { PrecomputedTransactionData txdata(txTo[i]); - for (int j = 0; j < 8; j++) - { + for (int j = 0; j < 8; j++) { CScript sigSave = txTo[i].vin[0].scriptSig; txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig; - bool sigOK = CScriptCheck(CCoins(txFrom, 0), txTo[i], 0, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, false, &txdata)(); + bool sigOK = CScriptCheck( + CCoins(txFrom, 0), txTo[i], 0, + SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, false, &txdata)(); if (i == j) - BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j)); + BOOST_CHECK_MESSAGE(sigOK, + strprintf("VerifySignature %d %d", i, j)); else - BOOST_CHECK_MESSAGE(!sigOK, strprintf("VerifySignature %d %d", i, j)); + BOOST_CHECK_MESSAGE(!sigOK, + strprintf("VerifySignature %d %d", i, j)); txTo[i].vin[0].scriptSig = sigSave; } } } -BOOST_AUTO_TEST_CASE(norecurse) -{ +BOOST_AUTO_TEST_CASE(norecurse) { ScriptError err; // Make sure only the outer pay-to-script-hash does the // extra-validation thing: CScript invalidAsScript; invalidAsScript << OP_INVALIDOPCODE << OP_INVALIDOPCODE; CScript p2sh = GetScriptForDestination(CScriptID(invalidAsScript)); CScript scriptSig; scriptSig << Serialize(invalidAsScript); // Should not verify, because it will try to execute OP_INVALIDOPCODE BOOST_CHECK(!Verify(scriptSig, p2sh, true, err)); BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_BAD_OPCODE, ScriptErrorString(err)); // Try to recur, and verification should succeed because // the inner HASH160 <> EQUAL should only check the hash: CScript p2sh2 = GetScriptForDestination(CScriptID(p2sh)); CScript scriptSig2; scriptSig2 << Serialize(invalidAsScript) << Serialize(p2sh); BOOST_CHECK(Verify(scriptSig2, p2sh2, true, err)); BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err)); } -BOOST_AUTO_TEST_CASE(set) -{ +BOOST_AUTO_TEST_CASE(set) { LOCK(cs_main); // Test the CScript::Set* methods CBasicKeyStore keystore; CKey key[4]; std::vector keys; - for (int i = 0; i < 4; i++) - { + for (int i = 0; i < 4; i++) { key[i].MakeNewKey(true); keystore.AddKey(key[i]); keys.push_back(key[i].GetPubKey()); } CScript inner[4]; inner[0] = GetScriptForDestination(key[0].GetPubKey().GetID()); - inner[1] = GetScriptForMultisig(2, std::vector(keys.begin(), keys.begin()+2)); - inner[2] = GetScriptForMultisig(1, std::vector(keys.begin(), keys.begin()+2)); - inner[3] = GetScriptForMultisig(2, std::vector(keys.begin(), keys.begin()+3)); + inner[1] = GetScriptForMultisig( + 2, std::vector(keys.begin(), keys.begin() + 2)); + inner[2] = GetScriptForMultisig( + 1, std::vector(keys.begin(), keys.begin() + 2)); + inner[3] = GetScriptForMultisig( + 2, std::vector(keys.begin(), keys.begin() + 3)); CScript outer[4]; - for (int i = 0; i < 4; i++) - { + for (int i = 0; i < 4; i++) { outer[i] = GetScriptForDestination(CScriptID(inner[i])); keystore.AddCScript(inner[i]); } - CMutableTransaction txFrom; // Funding transaction: + CMutableTransaction txFrom; // Funding transaction: std::string reason; txFrom.vout.resize(4); - for (int i = 0; i < 4; i++) - { + for (int i = 0; i < 4; i++) { txFrom.vout[i].scriptPubKey = outer[i]; txFrom.vout[i].nValue = CENT; } BOOST_CHECK(IsStandardTx(txFrom, reason)); CMutableTransaction txTo[4]; // Spending transactions - for (int i = 0; i < 4; i++) - { + for (int i = 0; i < 4; i++) { txTo[i].vin.resize(1); txTo[i].vout.resize(1); txTo[i].vin[0].prevout.n = i; txTo[i].vin[0].prevout.hash = txFrom.GetId(); - txTo[i].vout[0].nValue = 1*CENT; + txTo[i].vout[0].nValue = 1 * CENT; txTo[i].vout[0].scriptPubKey = inner[i]; - BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i)); + BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), + strprintf("IsMine %d", i)); } - for (int i = 0; i < 4; i++) - { - BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0, SIGHASH_ALL), strprintf("SignSignature %d", i)); - BOOST_CHECK_MESSAGE(IsStandardTx(txTo[i], reason), strprintf("txTo[%d].IsStandard", i)); + for (int i = 0; i < 4; i++) { + BOOST_CHECK_MESSAGE( + SignSignature(keystore, txFrom, txTo[i], 0, SIGHASH_ALL), + strprintf("SignSignature %d", i)); + BOOST_CHECK_MESSAGE(IsStandardTx(txTo[i], reason), + strprintf("txTo[%d].IsStandard", i)); } } -BOOST_AUTO_TEST_CASE(is) -{ +BOOST_AUTO_TEST_CASE(is) { // Test CScript::IsPayToScriptHash() uint160 dummy; CScript p2sh; p2sh << OP_HASH160 << ToByteVector(dummy) << OP_EQUAL; BOOST_CHECK(p2sh.IsPayToScriptHash()); - // Not considered pay-to-script-hash if using one of the OP_PUSHDATA opcodes: - static const unsigned char direct[] = { OP_HASH160, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL }; - BOOST_CHECK(CScript(direct, direct+sizeof(direct)).IsPayToScriptHash()); - static const unsigned char pushdata1[] = { OP_HASH160, OP_PUSHDATA1, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL }; - BOOST_CHECK(!CScript(pushdata1, pushdata1+sizeof(pushdata1)).IsPayToScriptHash()); - static const unsigned char pushdata2[] = { OP_HASH160, OP_PUSHDATA2, 20,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL }; - BOOST_CHECK(!CScript(pushdata2, pushdata2+sizeof(pushdata2)).IsPayToScriptHash()); - static const unsigned char pushdata4[] = { OP_HASH160, OP_PUSHDATA4, 20,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL }; - BOOST_CHECK(!CScript(pushdata4, pushdata4+sizeof(pushdata4)).IsPayToScriptHash()); + // Not considered pay-to-script-hash if using one of the OP_PUSHDATA + // opcodes: + static const unsigned char direct[] = { + OP_HASH160, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, OP_EQUAL}; + BOOST_CHECK(CScript(direct, direct + sizeof(direct)).IsPayToScriptHash()); + static const unsigned char pushdata1[] = {OP_HASH160, OP_PUSHDATA1, + 20, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, OP_EQUAL}; + BOOST_CHECK( + !CScript(pushdata1, pushdata1 + sizeof(pushdata1)).IsPayToScriptHash()); + static const unsigned char pushdata2[] = {OP_HASH160, OP_PUSHDATA2, + 20, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + OP_EQUAL}; + BOOST_CHECK( + !CScript(pushdata2, pushdata2 + sizeof(pushdata2)).IsPayToScriptHash()); + static const unsigned char pushdata4[] = {OP_HASH160, OP_PUSHDATA4, + 20, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + OP_EQUAL}; + BOOST_CHECK( + !CScript(pushdata4, pushdata4 + sizeof(pushdata4)).IsPayToScriptHash()); CScript not_p2sh; BOOST_CHECK(!not_p2sh.IsPayToScriptHash()); - not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << ToByteVector(dummy) << OP_EQUAL; + not_p2sh.clear(); + not_p2sh << OP_HASH160 << ToByteVector(dummy) << ToByteVector(dummy) + << OP_EQUAL; BOOST_CHECK(!not_p2sh.IsPayToScriptHash()); - not_p2sh.clear(); not_p2sh << OP_NOP << ToByteVector(dummy) << OP_EQUAL; + not_p2sh.clear(); + not_p2sh << OP_NOP << ToByteVector(dummy) << OP_EQUAL; BOOST_CHECK(!not_p2sh.IsPayToScriptHash()); - not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << OP_CHECKSIG; + not_p2sh.clear(); + not_p2sh << OP_HASH160 << ToByteVector(dummy) << OP_CHECKSIG; BOOST_CHECK(!not_p2sh.IsPayToScriptHash()); } -BOOST_AUTO_TEST_CASE(switchover) -{ +BOOST_AUTO_TEST_CASE(switchover) { // Test switch over code CScript notValid; ScriptError err; notValid << OP_11 << OP_12 << OP_EQUALVERIFY; CScript scriptSig; scriptSig << Serialize(notValid); CScript fund = GetScriptForDestination(CScriptID(notValid)); - // Validation should succeed under old rules (hash is correct): BOOST_CHECK(Verify(scriptSig, fund, false, err)); BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err)); // Fail under new: BOOST_CHECK(!Verify(scriptSig, fund, true, err)); BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EQUALVERIFY, ScriptErrorString(err)); } -BOOST_AUTO_TEST_CASE(AreInputsStandard) -{ +BOOST_AUTO_TEST_CASE(AreInputsStandard) { LOCK(cs_main); CCoinsView coinsDummy; CCoinsViewCache coins(&coinsDummy); CBasicKeyStore keystore; CKey key[6]; std::vector keys; - for (int i = 0; i < 6; i++) - { + for (int i = 0; i < 6; i++) { key[i].MakeNewKey(true); keystore.AddKey(key[i]); } for (int i = 0; i < 3; i++) keys.push_back(key[i].GetPubKey()); CMutableTransaction txFrom; txFrom.vout.resize(7); // First three are standard: CScript pay1 = GetScriptForDestination(key[0].GetPubKey().GetID()); keystore.AddCScript(pay1); CScript pay1of3 = GetScriptForMultisig(1, keys); - txFrom.vout[0].scriptPubKey = GetScriptForDestination(CScriptID(pay1)); // P2SH (OP_CHECKSIG) + txFrom.vout[0].scriptPubKey = + GetScriptForDestination(CScriptID(pay1)); // P2SH (OP_CHECKSIG) txFrom.vout[0].nValue = 1000; txFrom.vout[1].scriptPubKey = pay1; // ordinary OP_CHECKSIG txFrom.vout[1].nValue = 2000; txFrom.vout[2].scriptPubKey = pay1of3; // ordinary OP_CHECKMULTISIG txFrom.vout[2].nValue = 3000; // vout[3] is complicated 1-of-3 AND 2-of-3 // ... that is OK if wrapped in P2SH: CScript oneAndTwo; - oneAndTwo << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()); + oneAndTwo << OP_1 << ToByteVector(key[0].GetPubKey()) + << ToByteVector(key[1].GetPubKey()) + << ToByteVector(key[2].GetPubKey()); oneAndTwo << OP_3 << OP_CHECKMULTISIGVERIFY; - oneAndTwo << OP_2 << ToByteVector(key[3].GetPubKey()) << ToByteVector(key[4].GetPubKey()) << ToByteVector(key[5].GetPubKey()); + oneAndTwo << OP_2 << ToByteVector(key[3].GetPubKey()) + << ToByteVector(key[4].GetPubKey()) + << ToByteVector(key[5].GetPubKey()); oneAndTwo << OP_3 << OP_CHECKMULTISIG; keystore.AddCScript(oneAndTwo); txFrom.vout[3].scriptPubKey = GetScriptForDestination(CScriptID(oneAndTwo)); txFrom.vout[3].nValue = 4000; // vout[4] is max sigops: - CScript fifteenSigops; fifteenSigops << OP_1; + CScript fifteenSigops; + fifteenSigops << OP_1; for (unsigned i = 0; i < MAX_P2SH_SIGOPS; i++) - fifteenSigops << ToByteVector(key[i%3].GetPubKey()); + fifteenSigops << ToByteVector(key[i % 3].GetPubKey()); fifteenSigops << OP_15 << OP_CHECKMULTISIG; keystore.AddCScript(fifteenSigops); - txFrom.vout[4].scriptPubKey = GetScriptForDestination(CScriptID(fifteenSigops)); + txFrom.vout[4].scriptPubKey = + GetScriptForDestination(CScriptID(fifteenSigops)); txFrom.vout[4].nValue = 5000; // vout[5/6] are non-standard because they exceed MAX_P2SH_SIGOPS - CScript sixteenSigops; sixteenSigops << OP_16 << OP_CHECKMULTISIG; + CScript sixteenSigops; + sixteenSigops << OP_16 << OP_CHECKMULTISIG; keystore.AddCScript(sixteenSigops); - txFrom.vout[5].scriptPubKey = GetScriptForDestination(CScriptID(fifteenSigops)); + txFrom.vout[5].scriptPubKey = + GetScriptForDestination(CScriptID(fifteenSigops)); txFrom.vout[5].nValue = 5000; - CScript twentySigops; twentySigops << OP_CHECKMULTISIG; + CScript twentySigops; + twentySigops << OP_CHECKMULTISIG; keystore.AddCScript(twentySigops); - txFrom.vout[6].scriptPubKey = GetScriptForDestination(CScriptID(twentySigops)); + txFrom.vout[6].scriptPubKey = + GetScriptForDestination(CScriptID(twentySigops)); txFrom.vout[6].nValue = 6000; coins.ModifyCoins(txFrom.GetId())->FromTx(txFrom, 0); CMutableTransaction txTo; txTo.vout.resize(1); - txTo.vout[0].scriptPubKey = GetScriptForDestination(key[1].GetPubKey().GetID()); + txTo.vout[0].scriptPubKey = + GetScriptForDestination(key[1].GetPubKey().GetID()); txTo.vin.resize(5); - for (int i = 0; i < 5; i++) - { + for (int i = 0; i < 5; i++) { txTo.vin[i].prevout.n = i; txTo.vin[i].prevout.hash = txFrom.GetId(); } BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 0, SIGHASH_ALL)); BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 1, SIGHASH_ALL)); BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 2, SIGHASH_ALL)); // SignSignature doesn't know how to sign these. We're // not testing validating signatures, so just create // dummy signatures that DO include the correct P2SH scripts: - txTo.vin[3].scriptSig << OP_11 << OP_11 << std::vector(oneAndTwo.begin(), oneAndTwo.end()); - txTo.vin[4].scriptSig << std::vector(fifteenSigops.begin(), fifteenSigops.end()); + txTo.vin[3].scriptSig << OP_11 << OP_11 + << std::vector(oneAndTwo.begin(), + oneAndTwo.end()); + txTo.vin[4].scriptSig << std::vector(fifteenSigops.begin(), + fifteenSigops.end()); BOOST_CHECK(::AreInputsStandard(txTo, coins)); // 22 P2SH sigops for all inputs (1 for vin[0], 6 for vin[3], 15 for vin[4] BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txTo, coins), 22U); CMutableTransaction txToNonStd1; txToNonStd1.vout.resize(1); - txToNonStd1.vout[0].scriptPubKey = GetScriptForDestination(key[1].GetPubKey().GetID()); + txToNonStd1.vout[0].scriptPubKey = + GetScriptForDestination(key[1].GetPubKey().GetID()); txToNonStd1.vout[0].nValue = 1000; txToNonStd1.vin.resize(1); txToNonStd1.vin[0].prevout.n = 5; txToNonStd1.vin[0].prevout.hash = txFrom.GetId(); - txToNonStd1.vin[0].scriptSig << std::vector(sixteenSigops.begin(), sixteenSigops.end()); + txToNonStd1.vin[0].scriptSig << std::vector( + sixteenSigops.begin(), sixteenSigops.end()); BOOST_CHECK(!::AreInputsStandard(txToNonStd1, coins)); BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txToNonStd1, coins), 16U); CMutableTransaction txToNonStd2; txToNonStd2.vout.resize(1); - txToNonStd2.vout[0].scriptPubKey = GetScriptForDestination(key[1].GetPubKey().GetID()); + txToNonStd2.vout[0].scriptPubKey = + GetScriptForDestination(key[1].GetPubKey().GetID()); txToNonStd2.vout[0].nValue = 1000; txToNonStd2.vin.resize(1); txToNonStd2.vin[0].prevout.n = 6; txToNonStd2.vin[0].prevout.hash = txFrom.GetId(); - txToNonStd2.vin[0].scriptSig << std::vector(twentySigops.begin(), twentySigops.end()); + txToNonStd2.vin[0].scriptSig + << std::vector(twentySigops.begin(), twentySigops.end()); BOOST_CHECK(!::AreInputsStandard(txToNonStd2, coins)); BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txToNonStd2, coins), 20U); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/scriptnum10.h b/src/test/scriptnum10.h index 94dd58526..8174ab976 100644 --- a/src/test/scriptnum10.h +++ b/src/test/scriptnum10.h @@ -1,183 +1,196 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2015 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_TEST_SCRIPTNUM10_H #define BITCOIN_TEST_SCRIPTNUM10_H +#include "assert.h" #include #include #include #include #include #include -#include "assert.h" -class scriptnum10_error : public std::runtime_error -{ +class scriptnum10_error : public std::runtime_error { public: - explicit scriptnum10_error(const std::string& str) : std::runtime_error(str) {} + explicit scriptnum10_error(const std::string &str) + : std::runtime_error(str) {} }; -class CScriptNum10 -{ -/** - * The ScriptNum implementation from Bitcoin Core 0.10.0, for cross-comparison. - */ +class CScriptNum10 { + /** + * The ScriptNum implementation from Bitcoin Core 0.10.0, for + * cross-comparison. + */ public: - - explicit CScriptNum10(const int64_t& n) - { - m_value = n; - } + explicit CScriptNum10(const int64_t &n) { m_value = n; } static const size_t nDefaultMaxNumSize = 4; - explicit CScriptNum10(const std::vector& vch, bool fRequireMinimal, - const size_t nMaxNumSize = nDefaultMaxNumSize) - { + explicit CScriptNum10(const std::vector &vch, + bool fRequireMinimal, + const size_t nMaxNumSize = nDefaultMaxNumSize) { if (vch.size() > nMaxNumSize) { throw scriptnum10_error("script number overflow"); } if (fRequireMinimal && vch.size() > 0) { - // Check that the number is encoded with the minimum possible - // number of bytes. + // Check that the number is encoded with the minimum possible number + // of bytes. // // If the most-significant-byte - excluding the sign bit - is zero // then we're not minimal. Note how this test also rejects the // negative-zero encoding, 0x80. if ((vch.back() & 0x7f) == 0) { // One exception: if there's more than one byte and the most - // significant bit of the second-most-significant-byte is set - // it would conflict with the sign bit. An example of this case - // is +-255, which encode to 0xff00 and 0xff80 respectively. + // significant bit of the second-most-significant-byte is set it + // would conflict with the sign bit. An example of this case is + // +-255, which encode to 0xff00 and 0xff80 respectively. // (big-endian). if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) { - throw scriptnum10_error("non-minimally encoded script number"); + throw scriptnum10_error( + "non-minimally encoded script number"); } } } m_value = set_vch(vch); } - inline bool operator==(const int64_t& rhs) const { return m_value == rhs; } - inline bool operator!=(const int64_t& rhs) const { return m_value != rhs; } - inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; } - inline bool operator< (const int64_t& rhs) const { return m_value < rhs; } - inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; } - inline bool operator> (const int64_t& rhs) const { return m_value > rhs; } + inline bool operator==(const int64_t &rhs) const { return m_value == rhs; } + inline bool operator!=(const int64_t &rhs) const { return m_value != rhs; } + inline bool operator<=(const int64_t &rhs) const { return m_value <= rhs; } + inline bool operator<(const int64_t &rhs) const { return m_value < rhs; } + inline bool operator>=(const int64_t &rhs) const { return m_value >= rhs; } + inline bool operator>(const int64_t &rhs) const { return m_value > rhs; } - inline bool operator==(const CScriptNum10& rhs) const { return operator==(rhs.m_value); } - inline bool operator!=(const CScriptNum10& rhs) const { return operator!=(rhs.m_value); } - inline bool operator<=(const CScriptNum10& rhs) const { return operator<=(rhs.m_value); } - inline bool operator< (const CScriptNum10& rhs) const { return operator< (rhs.m_value); } - inline bool operator>=(const CScriptNum10& rhs) const { return operator>=(rhs.m_value); } - inline bool operator> (const CScriptNum10& rhs) const { return operator> (rhs.m_value); } + inline bool operator==(const CScriptNum10 &rhs) const { + return operator==(rhs.m_value); + } + inline bool operator!=(const CScriptNum10 &rhs) const { + return operator!=(rhs.m_value); + } + inline bool operator<=(const CScriptNum10 &rhs) const { + return operator<=(rhs.m_value); + } + inline bool operator<(const CScriptNum10 &rhs) const { + return operator<(rhs.m_value); + } + inline bool operator>=(const CScriptNum10 &rhs) const { + return operator>=(rhs.m_value); + } + inline bool operator>(const CScriptNum10 &rhs) const { + return operator>(rhs.m_value); + } - inline CScriptNum10 operator+( const int64_t& rhs) const { return CScriptNum10(m_value + rhs);} - inline CScriptNum10 operator-( const int64_t& rhs) const { return CScriptNum10(m_value - rhs);} - inline CScriptNum10 operator+( const CScriptNum10& rhs) const { return operator+(rhs.m_value); } - inline CScriptNum10 operator-( const CScriptNum10& rhs) const { return operator-(rhs.m_value); } + inline CScriptNum10 operator+(const int64_t &rhs) const { + return CScriptNum10(m_value + rhs); + } + inline CScriptNum10 operator-(const int64_t &rhs) const { + return CScriptNum10(m_value - rhs); + } + inline CScriptNum10 operator+(const CScriptNum10 &rhs) const { + return operator+(rhs.m_value); + } + inline CScriptNum10 operator-(const CScriptNum10 &rhs) const { + return operator-(rhs.m_value); + } - inline CScriptNum10& operator+=( const CScriptNum10& rhs) { return operator+=(rhs.m_value); } - inline CScriptNum10& operator-=( const CScriptNum10& rhs) { return operator-=(rhs.m_value); } + inline CScriptNum10 &operator+=(const CScriptNum10 &rhs) { + return operator+=(rhs.m_value); + } + inline CScriptNum10 &operator-=(const CScriptNum10 &rhs) { + return operator-=(rhs.m_value); + } - inline CScriptNum10 operator-() const - { + inline CScriptNum10 operator-() const { assert(m_value != std::numeric_limits::min()); return CScriptNum10(-m_value); } - inline CScriptNum10& operator=( const int64_t& rhs) - { + inline CScriptNum10 &operator=(const int64_t &rhs) { m_value = rhs; return *this; } - inline CScriptNum10& operator+=( const int64_t& rhs) - { - assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits::max() - rhs) || - (rhs < 0 && m_value >= std::numeric_limits::min() - rhs)); + inline CScriptNum10 &operator+=(const int64_t &rhs) { + assert( + rhs == 0 || + (rhs > 0 && m_value <= std::numeric_limits::max() - rhs) || + (rhs < 0 && m_value >= std::numeric_limits::min() - rhs)); m_value += rhs; return *this; } - inline CScriptNum10& operator-=( const int64_t& rhs) - { - assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits::min() + rhs) || - (rhs < 0 && m_value <= std::numeric_limits::max() + rhs)); + inline CScriptNum10 &operator-=(const int64_t &rhs) { + assert( + rhs == 0 || + (rhs > 0 && m_value >= std::numeric_limits::min() + rhs) || + (rhs < 0 && m_value <= std::numeric_limits::max() + rhs)); m_value -= rhs; return *this; } - int getint() const - { + int getint() const { if (m_value > std::numeric_limits::max()) return std::numeric_limits::max(); else if (m_value < std::numeric_limits::min()) return std::numeric_limits::min(); return m_value; } - std::vector getvch() const - { - return serialize(m_value); - } + std::vector getvch() const { return serialize(m_value); } - static std::vector serialize(const int64_t& value) - { - if(value == 0) - return std::vector(); + static std::vector serialize(const int64_t &value) { + if (value == 0) return std::vector(); std::vector result; const bool neg = value < 0; uint64_t absvalue = neg ? -value : value; - while(absvalue) - { + while (absvalue) { result.push_back(absvalue & 0xff); absvalue >>= 8; } -// - If the most significant byte is >= 0x80 and the value is positive, push a -// new zero-byte to make the significant byte < 0x80 again. + // - If the most significant byte is >= 0x80 and the value is + // positive, push a new zero-byte to make the significant byte < 0x80 + // again. -// - If the most significant byte is >= 0x80 and the value is negative, push a -// new 0x80 byte that will be popped off when converting to an integral. + // - If the most significant byte is >= 0x80 and the value is + // negative, push a new 0x80 byte that will be popped off when + // converting to an integral. -// - If the most significant byte is < 0x80 and the value is negative, add -// 0x80 to it, since it will be subtracted and interpreted as a negative when -// converting to an integral. + // - If the most significant byte is < 0x80 and the value is + // negative, add 0x80 to it, since it will be subtracted and + // interpreted as a negative when converting to an integral. if (result.back() & 0x80) result.push_back(neg ? 0x80 : 0); else if (neg) result.back() |= 0x80; return result; } private: - static int64_t set_vch(const std::vector& vch) - { - if (vch.empty()) - return 0; + static int64_t set_vch(const std::vector &vch) { + if (vch.empty()) return 0; - int64_t result = 0; - for (size_t i = 0; i != vch.size(); ++i) - result |= static_cast(vch[i]) << 8*i; + int64_t result = 0; + for (size_t i = 0; i != vch.size(); ++i) + result |= static_cast(vch[i]) << 8 * i; - // If the input vector's most significant byte is 0x80, remove it from - // the result's msb and return a negative. - if (vch.back() & 0x80) - return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1))))); + // If the input vector's most significant byte is 0x80, remove it from + // the result's msb and return a negative. + if (vch.back() & 0x80) + return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1))))); - return result; + return result; } int64_t m_value; }; - #endif // BITCOIN_TEST_BIGNUM_H diff --git a/src/test/scriptnum_tests.cpp b/src/test/scriptnum_tests.cpp index 1d5893bdc..e462f11d6 100644 --- a/src/test/scriptnum_tests.cpp +++ b/src/test/scriptnum_tests.cpp @@ -1,204 +1,205 @@ // Copyright (c) 2012-2015 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "scriptnum10.h" #include "script/script.h" +#include "scriptnum10.h" #include "test/test_bitcoin.h" #include #include #include BOOST_FIXTURE_TEST_SUITE(scriptnum_tests, BasicTestingSetup) /** A selection of numbers that do not trigger int64_t overflow * when added/subtracted. */ -static const int64_t values[] = { 0, 1, -2, 127, 128, -255, 256, (1LL << 15) - 1, -(1LL << 16), (1LL << 24) - 1, (1LL << 31), 1 - (1LL << 32), 1LL << 40 }; - -static const int64_t offsets[] = { 1, 0x79, 0x80, 0x81, 0xFF, 0x7FFF, 0x8000, 0xFFFF, 0x10000}; - -static bool verify(const CScriptNum10& bignum, const CScriptNum& scriptnum) -{ - return bignum.getvch() == scriptnum.getvch() && bignum.getint() == scriptnum.getint(); +static const int64_t values[] = {0, + 1, + -2, + 127, + 128, + -255, + 256, + (1LL << 15) - 1, + -(1LL << 16), + (1LL << 24) - 1, + (1LL << 31), + 1 - (1LL << 32), + 1LL << 40}; + +static const int64_t offsets[] = {1, 0x79, 0x80, 0x81, 0xFF, + 0x7FFF, 0x8000, 0xFFFF, 0x10000}; + +static bool verify(const CScriptNum10 &bignum, const CScriptNum &scriptnum) { + return bignum.getvch() == scriptnum.getvch() && + bignum.getint() == scriptnum.getint(); } -static void CheckCreateVch(const int64_t& num) -{ +static void CheckCreateVch(const int64_t &num) { CScriptNum10 bignum(num); CScriptNum scriptnum(num); BOOST_CHECK(verify(bignum, scriptnum)); std::vector vch = bignum.getvch(); CScriptNum10 bignum2(bignum.getvch(), false); vch = scriptnum.getvch(); CScriptNum scriptnum2(scriptnum.getvch(), false); BOOST_CHECK(verify(bignum2, scriptnum2)); CScriptNum10 bignum3(scriptnum2.getvch(), false); CScriptNum scriptnum3(bignum2.getvch(), false); BOOST_CHECK(verify(bignum3, scriptnum3)); } -static void CheckCreateInt(const int64_t& num) -{ +static void CheckCreateInt(const int64_t &num) { CScriptNum10 bignum(num); CScriptNum scriptnum(num); BOOST_CHECK(verify(bignum, scriptnum)); - BOOST_CHECK(verify(CScriptNum10(bignum.getint()), CScriptNum(scriptnum.getint()))); - BOOST_CHECK(verify(CScriptNum10(scriptnum.getint()), CScriptNum(bignum.getint()))); - BOOST_CHECK(verify(CScriptNum10(CScriptNum10(scriptnum.getint()).getint()), CScriptNum(CScriptNum(bignum.getint()).getint()))); + BOOST_CHECK( + verify(CScriptNum10(bignum.getint()), CScriptNum(scriptnum.getint()))); + BOOST_CHECK( + verify(CScriptNum10(scriptnum.getint()), CScriptNum(bignum.getint()))); + BOOST_CHECK(verify(CScriptNum10(CScriptNum10(scriptnum.getint()).getint()), + CScriptNum(CScriptNum(bignum.getint()).getint()))); } - -static void CheckAdd(const int64_t& num1, const int64_t& num2) -{ +static void CheckAdd(const int64_t &num1, const int64_t &num2) { const CScriptNum10 bignum1(num1); const CScriptNum10 bignum2(num2); const CScriptNum scriptnum1(num1); const CScriptNum scriptnum2(num2); CScriptNum10 bignum3(num1); CScriptNum10 bignum4(num1); CScriptNum scriptnum3(num1); CScriptNum scriptnum4(num1); // int64_t overflow is undefined. - bool invalid = (((num2 > 0) && (num1 > (std::numeric_limits::max() - num2))) || - ((num2 < 0) && (num1 < (std::numeric_limits::min() - num2)))); - if (!invalid) - { + bool invalid = + (((num2 > 0) && + (num1 > (std::numeric_limits::max() - num2))) || + ((num2 < 0) && (num1 < (std::numeric_limits::min() - num2)))); + if (!invalid) { BOOST_CHECK(verify(bignum1 + bignum2, scriptnum1 + scriptnum2)); BOOST_CHECK(verify(bignum1 + bignum2, scriptnum1 + num2)); BOOST_CHECK(verify(bignum1 + bignum2, scriptnum2 + num1)); } } -static void CheckNegate(const int64_t& num) -{ +static void CheckNegate(const int64_t &num) { const CScriptNum10 bignum(num); const CScriptNum scriptnum(num); // -INT64_MIN is undefined if (num != std::numeric_limits::min()) BOOST_CHECK(verify(-bignum, -scriptnum)); } -static void CheckSubtract(const int64_t& num1, const int64_t& num2) -{ +static void CheckSubtract(const int64_t &num1, const int64_t &num2) { const CScriptNum10 bignum1(num1); const CScriptNum10 bignum2(num2); const CScriptNum scriptnum1(num1); const CScriptNum scriptnum2(num2); bool invalid = false; // int64_t overflow is undefined. - invalid = ((num2 > 0 && num1 < std::numeric_limits::min() + num2) || - (num2 < 0 && num1 > std::numeric_limits::max() + num2)); - if (!invalid) - { + invalid = + ((num2 > 0 && num1 < std::numeric_limits::min() + num2) || + (num2 < 0 && num1 > std::numeric_limits::max() + num2)); + if (!invalid) { BOOST_CHECK(verify(bignum1 - bignum2, scriptnum1 - scriptnum2)); BOOST_CHECK(verify(bignum1 - bignum2, scriptnum1 - num2)); } - invalid = ((num1 > 0 && num2 < std::numeric_limits::min() + num1) || - (num1 < 0 && num2 > std::numeric_limits::max() + num1)); - if (!invalid) - { + invalid = + ((num1 > 0 && num2 < std::numeric_limits::min() + num1) || + (num1 < 0 && num2 > std::numeric_limits::max() + num1)); + if (!invalid) { BOOST_CHECK(verify(bignum2 - bignum1, scriptnum2 - scriptnum1)); BOOST_CHECK(verify(bignum2 - bignum1, scriptnum2 - num1)); } } -static void CheckCompare(const int64_t& num1, const int64_t& num2) -{ +static void CheckCompare(const int64_t &num1, const int64_t &num2) { const CScriptNum10 bignum1(num1); const CScriptNum10 bignum2(num2); const CScriptNum scriptnum1(num1); const CScriptNum scriptnum2(num2); BOOST_CHECK((bignum1 == bignum1) == (scriptnum1 == scriptnum1)); - BOOST_CHECK((bignum1 != bignum1) == (scriptnum1 != scriptnum1)); - BOOST_CHECK((bignum1 < bignum1) == (scriptnum1 < scriptnum1)); - BOOST_CHECK((bignum1 > bignum1) == (scriptnum1 > scriptnum1)); - BOOST_CHECK((bignum1 >= bignum1) == (scriptnum1 >= scriptnum1)); - BOOST_CHECK((bignum1 <= bignum1) == (scriptnum1 <= scriptnum1)); + BOOST_CHECK((bignum1 != bignum1) == (scriptnum1 != scriptnum1)); + BOOST_CHECK((bignum1 < bignum1) == (scriptnum1 < scriptnum1)); + BOOST_CHECK((bignum1 > bignum1) == (scriptnum1 > scriptnum1)); + BOOST_CHECK((bignum1 >= bignum1) == (scriptnum1 >= scriptnum1)); + BOOST_CHECK((bignum1 <= bignum1) == (scriptnum1 <= scriptnum1)); BOOST_CHECK((bignum1 == bignum1) == (scriptnum1 == num1)); - BOOST_CHECK((bignum1 != bignum1) == (scriptnum1 != num1)); - BOOST_CHECK((bignum1 < bignum1) == (scriptnum1 < num1)); - BOOST_CHECK((bignum1 > bignum1) == (scriptnum1 > num1)); - BOOST_CHECK((bignum1 >= bignum1) == (scriptnum1 >= num1)); - BOOST_CHECK((bignum1 <= bignum1) == (scriptnum1 <= num1)); - - BOOST_CHECK((bignum1 == bignum2) == (scriptnum1 == scriptnum2)); - BOOST_CHECK((bignum1 != bignum2) == (scriptnum1 != scriptnum2)); - BOOST_CHECK((bignum1 < bignum2) == (scriptnum1 < scriptnum2)); - BOOST_CHECK((bignum1 > bignum2) == (scriptnum1 > scriptnum2)); - BOOST_CHECK((bignum1 >= bignum2) == (scriptnum1 >= scriptnum2)); - BOOST_CHECK((bignum1 <= bignum2) == (scriptnum1 <= scriptnum2)); - - BOOST_CHECK((bignum1 == bignum2) == (scriptnum1 == num2)); - BOOST_CHECK((bignum1 != bignum2) == (scriptnum1 != num2)); - BOOST_CHECK((bignum1 < bignum2) == (scriptnum1 < num2)); - BOOST_CHECK((bignum1 > bignum2) == (scriptnum1 > num2)); - BOOST_CHECK((bignum1 >= bignum2) == (scriptnum1 >= num2)); - BOOST_CHECK((bignum1 <= bignum2) == (scriptnum1 <= num2)); + BOOST_CHECK((bignum1 != bignum1) == (scriptnum1 != num1)); + BOOST_CHECK((bignum1 < bignum1) == (scriptnum1 < num1)); + BOOST_CHECK((bignum1 > bignum1) == (scriptnum1 > num1)); + BOOST_CHECK((bignum1 >= bignum1) == (scriptnum1 >= num1)); + BOOST_CHECK((bignum1 <= bignum1) == (scriptnum1 <= num1)); + + BOOST_CHECK((bignum1 == bignum2) == (scriptnum1 == scriptnum2)); + BOOST_CHECK((bignum1 != bignum2) == (scriptnum1 != scriptnum2)); + BOOST_CHECK((bignum1 < bignum2) == (scriptnum1 < scriptnum2)); + BOOST_CHECK((bignum1 > bignum2) == (scriptnum1 > scriptnum2)); + BOOST_CHECK((bignum1 >= bignum2) == (scriptnum1 >= scriptnum2)); + BOOST_CHECK((bignum1 <= bignum2) == (scriptnum1 <= scriptnum2)); + + BOOST_CHECK((bignum1 == bignum2) == (scriptnum1 == num2)); + BOOST_CHECK((bignum1 != bignum2) == (scriptnum1 != num2)); + BOOST_CHECK((bignum1 < bignum2) == (scriptnum1 < num2)); + BOOST_CHECK((bignum1 > bignum2) == (scriptnum1 > num2)); + BOOST_CHECK((bignum1 >= bignum2) == (scriptnum1 >= num2)); + BOOST_CHECK((bignum1 <= bignum2) == (scriptnum1 <= num2)); } -static void RunCreate(const int64_t& num) -{ +static void RunCreate(const int64_t &num) { CheckCreateInt(num); CScriptNum scriptnum(num); if (scriptnum.getvch().size() <= CScriptNum::nDefaultMaxNumSize) CheckCreateVch(num); - else - { - BOOST_CHECK_THROW (CheckCreateVch(num), scriptnum10_error); + else { + BOOST_CHECK_THROW(CheckCreateVch(num), scriptnum10_error); } } -static void RunOperators(const int64_t& num1, const int64_t& num2) -{ +static void RunOperators(const int64_t &num1, const int64_t &num2) { CheckAdd(num1, num2); CheckSubtract(num1, num2); CheckNegate(num1); CheckCompare(num1, num2); } -BOOST_AUTO_TEST_CASE(creation) -{ - for(size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) - { - for(size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j) - { +BOOST_AUTO_TEST_CASE(creation) { + for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) { + for (size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j) { RunCreate(values[i]); RunCreate(values[i] + offsets[j]); RunCreate(values[i] - offsets[j]); } } } -BOOST_AUTO_TEST_CASE(operators) -{ - for(size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) - { - for(size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j) - { +BOOST_AUTO_TEST_CASE(operators) { + for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i) { + for (size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j) { RunOperators(values[i], values[i]); RunOperators(values[i], -values[i]); RunOperators(values[i], values[j]); RunOperators(values[i], -values[j]); RunOperators(values[i] + values[j], values[j]); RunOperators(values[i] + values[j], -values[j]); RunOperators(values[i] - values[j], values[j]); RunOperators(values[i] - values[j], -values[j]); RunOperators(values[i] + values[j], values[i] + values[j]); RunOperators(values[i] + values[j], values[i] - values[j]); RunOperators(values[i] - values[j], values[i] + values[j]); RunOperators(values[i] - values[j], values[i] - values[j]); } } } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/skiplist_tests.cpp b/src/test/skiplist_tests.cpp index 0b2fe0ef9..a8cba827b 100644 --- a/src/test/skiplist_tests.cpp +++ b/src/test/skiplist_tests.cpp @@ -1,146 +1,163 @@ // Copyright (c) 2014-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "chain.h" -#include "util.h" #include "test/test_bitcoin.h" #include "test/test_random.h" +#include "util.h" #include #include #define SKIPLIST_LENGTH 300000 BOOST_FIXTURE_TEST_SUITE(skiplist_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(skiplist_test) -{ +BOOST_AUTO_TEST_CASE(skiplist_test) { std::vector vIndex(SKIPLIST_LENGTH); - for (int i=0; i 0) { BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]); BOOST_CHECK(vIndex[i].pskip->nHeight < i); } else { BOOST_CHECK(vIndex[i].pskip == NULL); } } - for (int i=0; i < 1000; i++) { + for (int i = 0; i < 1000; i++) { int from = insecure_rand() % (SKIPLIST_LENGTH - 1); int to = insecure_rand() % (from + 1); - BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]); + BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == + &vIndex[from]); BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]); BOOST_CHECK(vIndex[from].GetAncestor(0) == &vIndex[0]); } } -BOOST_AUTO_TEST_CASE(getlocator_test) -{ +BOOST_AUTO_TEST_CASE(getlocator_test) { // Build a main chain 100000 blocks long. std::vector vHashMain(100000); std::vector vBlocksMain(100000); - for (unsigned int i=0; inHeight + 1); + BOOST_CHECK_EQUAL( + (int)UintToArith256(vBlocksMain[i].GetBlockHash()).GetLow64(), + vBlocksMain[i].nHeight); + BOOST_CHECK(vBlocksMain[i].pprev == NULL || + vBlocksMain[i].nHeight == + vBlocksMain[i].pprev->nHeight + 1); } // Build a branch that splits off at block 49999, 50000 blocks long. std::vector vHashSide(50000); std::vector vBlocksSide(50000); - for (unsigned int i=0; inHeight + 1); + BOOST_CHECK_EQUAL( + (int)UintToArith256(vBlocksSide[i].GetBlockHash()).GetLow64(), + vBlocksSide[i].nHeight); + BOOST_CHECK(vBlocksSide[i].pprev == NULL || + vBlocksSide[i].nHeight == + vBlocksSide[i].pprev->nHeight + 1); } // Build a CChain for the main branch. CChain chain; chain.SetTip(&vBlocksMain.back()); // Test 100 random starting points for locators. - for (int n=0; n<100; n++) { + for (int n = 0; n < 100; n++) { int r = insecure_rand() % 150000; - CBlockIndex* tip = (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000]; + CBlockIndex *tip = + (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000]; CBlockLocator locator = chain.GetLocator(tip); - // The first result must be the block itself, the last one must be genesis. + // The first result must be the block itself, the last one must be + // genesis. BOOST_CHECK(locator.vHave.front() == tip->GetBlockHash()); BOOST_CHECK(locator.vHave.back() == vBlocksMain[0].GetBlockHash()); // Entries 1 through 11 (inclusive) go back one step each. for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) { - BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i]).GetLow64(), tip->nHeight - i); + BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i]).GetLow64(), + tip->nHeight - i); } - // The further ones (excluding the last one) go back with exponential steps. + // The further ones (excluding the last one) go back with exponential + // steps. unsigned int dist = 2; for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) { - BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i - 1]).GetLow64() - UintToArith256(locator.vHave[i]).GetLow64(), dist); + BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i - 1]).GetLow64() - + UintToArith256(locator.vHave[i]).GetLow64(), + dist); dist *= 2; } } } -BOOST_AUTO_TEST_CASE(findearliestatleast_test) -{ +BOOST_AUTO_TEST_CASE(findearliestatleast_test) { std::vector vHashMain(100000); std::vector vBlocksMain(100000); - for (unsigned int i=0; inTimeMax >= test_time); - BOOST_CHECK((ret->pprev==NULL) || ret->pprev->nTimeMax < test_time); + BOOST_CHECK((ret->pprev == NULL) || ret->pprev->nTimeMax < test_time); BOOST_CHECK(vBlocksMain[r].GetAncestor(ret->nHeight) == ret); } } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index 94b5cc119..659b79b20 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -1,124 +1,121 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "streams.h" #include "support/allocators/zeroafterfree.h" #include "test/test_bitcoin.h" -#include // for 'operator+=()' #include +#include // for 'operator+=()' #include using namespace boost::assign; // bring 'operator+=()' into scope BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(streams_vector_writer) -{ +BOOST_AUTO_TEST_CASE(streams_vector_writer) { unsigned char a(1); unsigned char b(2); - unsigned char bytes[] = { 3, 4, 5, 6 }; + unsigned char bytes[] = {3, 4, 5, 6}; std::vector vch; // Each test runs twice. Serializing a second time at the same starting // point should yield the same results, even if the first test grew the // vector. CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, a, b); BOOST_CHECK((vch == std::vector{{1, 2}})); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, a, b); BOOST_CHECK((vch == std::vector{{1, 2}})); vch.clear(); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b); BOOST_CHECK((vch == std::vector{{0, 0, 1, 2}})); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b); BOOST_CHECK((vch == std::vector{{0, 0, 1, 2}})); vch.clear(); vch.resize(5, 0); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b); BOOST_CHECK((vch == std::vector{{0, 0, 1, 2, 0}})); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b); BOOST_CHECK((vch == std::vector{{0, 0, 1, 2, 0}})); vch.clear(); vch.resize(4, 0); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 3, a, b); BOOST_CHECK((vch == std::vector{{0, 0, 0, 1, 2}})); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 3, a, b); BOOST_CHECK((vch == std::vector{{0, 0, 0, 1, 2}})); vch.clear(); vch.resize(4, 0); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 4, a, b); BOOST_CHECK((vch == std::vector{{0, 0, 0, 0, 1, 2}})); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 4, a, b); BOOST_CHECK((vch == std::vector{{0, 0, 0, 0, 1, 2}})); vch.clear(); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, FLATDATA(bytes)); BOOST_CHECK((vch == std::vector{{3, 4, 5, 6}})); CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, FLATDATA(bytes)); BOOST_CHECK((vch == std::vector{{3, 4, 5, 6}})); vch.clear(); vch.resize(4, 8); - CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, FLATDATA(bytes), b); + CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, FLATDATA(bytes), + b); BOOST_CHECK((vch == std::vector{{8, 8, 1, 3, 4, 5, 6, 2}})); - CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, FLATDATA(bytes), b); + CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, FLATDATA(bytes), + b); BOOST_CHECK((vch == std::vector{{8, 8, 1, 3, 4, 5, 6, 2}})); vch.clear(); } -BOOST_AUTO_TEST_CASE(streams_serializedata_xor) -{ +BOOST_AUTO_TEST_CASE(streams_serializedata_xor) { std::vector in; std::vector expected_xor; std::vector key; CDataStream ds(in, 0, 0); // Degenerate case - - key += '\x00','\x00'; + + key += '\x00', '\x00'; ds.Xor(key); - BOOST_CHECK_EQUAL( - std::string(expected_xor.begin(), expected_xor.end()), - std::string(ds.begin(), ds.end())); + BOOST_CHECK_EQUAL(std::string(expected_xor.begin(), expected_xor.end()), + std::string(ds.begin(), ds.end())); + + in += '\x0f', '\xf0'; + expected_xor += '\xf0', '\x0f'; - in += '\x0f','\xf0'; - expected_xor += '\xf0','\x0f'; - // Single character key ds.clear(); ds.insert(ds.begin(), in.begin(), in.end()); key.clear(); key += '\xff'; ds.Xor(key); - BOOST_CHECK_EQUAL( - std::string(expected_xor.begin(), expected_xor.end()), - std::string(ds.begin(), ds.end())); - + BOOST_CHECK_EQUAL(std::string(expected_xor.begin(), expected_xor.end()), + std::string(ds.begin(), ds.end())); + // Multi character key in.clear(); expected_xor.clear(); - in += '\xf0','\x0f'; - expected_xor += '\x0f','\x00'; - + in += '\xf0', '\x0f'; + expected_xor += '\x0f', '\x00'; + ds.clear(); ds.insert(ds.begin(), in.begin(), in.end()); key.clear(); - key += '\xff','\x0f'; + key += '\xff', '\x0f'; ds.Xor(key); - BOOST_CHECK_EQUAL( - std::string(expected_xor.begin(), expected_xor.end()), - std::string(ds.begin(), ds.end())); -} + BOOST_CHECK_EQUAL(std::string(expected_xor.begin(), expected_xor.end()), + std::string(ds.begin(), ds.end())); +} BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/test_bitcoin_fuzzy.cpp b/src/test/test_bitcoin_fuzzy.cpp index c4983f6f5..24c35fad4 100644 --- a/src/test/test_bitcoin_fuzzy.cpp +++ b/src/test/test_bitcoin_fuzzy.cpp @@ -1,258 +1,259 @@ // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #if defined(HAVE_CONFIG_H) #include "config/bitcoin-config.h" #endif -#include "consensus/merkle.h" -#include "primitives/block.h" -#include "script/script.h" #include "addrman.h" #include "chain.h" #include "coins.h" #include "compressor.h" +#include "consensus/merkle.h" #include "net.h" +#include "primitives/block.h" #include "protocol.h" +#include "pubkey.h" +#include "script/script.h" #include "streams.h" #include "undo.h" #include "version.h" -#include "pubkey.h" #include #include #include #include enum TEST_ID { - CBLOCK_DESERIALIZE=0, + CBLOCK_DESERIALIZE = 0, CTRANSACTION_DESERIALIZE, CBLOCKLOCATOR_DESERIALIZE, CBLOCKMERKLEROOT, CADDRMAN_DESERIALIZE, CBLOCKHEADER_DESERIALIZE, CBANENTRY_DESERIALIZE, CTXUNDO_DESERIALIZE, CBLOCKUNDO_DESERIALIZE, CCOINS_DESERIALIZE, CNETADDR_DESERIALIZE, CSERVICE_DESERIALIZE, CMESSAGEHEADER_DESERIALIZE, CADDRESS_DESERIALIZE, CINV_DESERIALIZE, CBLOOMFILTER_DESERIALIZE, CDISKBLOCKINDEX_DESERIALIZE, CTXOUTCOMPRESSOR_DESERIALIZE, TEST_ID_END }; bool read_stdin(std::vector &data) { char buffer[1024]; - ssize_t length=0; - while((length = read(STDIN_FILENO, buffer, 1024)) > 0) { - data.insert(data.end(), buffer, buffer+length); + ssize_t length = 0; + while ((length = read(STDIN_FILENO, buffer, 1024)) > 0) { + data.insert(data.end(), buffer, buffer + length); - if (data.size() > (1<<20)) return false; + if (data.size() > (1 << 20)) return false; } - return length==0; + return length == 0; } -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { ECCVerifyHandle globalVerifyHandle; std::vector buffer; if (!read_stdin(buffer)) return 0; if (buffer.size() < sizeof(uint32_t)) return 0; uint32_t test_id = 0xffffffff; memcpy(&test_id, &buffer[0], sizeof(uint32_t)); buffer.erase(buffer.begin(), buffer.begin() + sizeof(uint32_t)); if (test_id >= TEST_ID_END) return 0; CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION); try { int nVersion; ds >> nVersion; ds.SetVersion(nVersion); - } catch (const std::ios_base::failure& e) { + } catch (const std::ios_base::failure &e) { return 0; } - switch(test_id) { - case CBLOCK_DESERIALIZE: - { - try - { + switch (test_id) { + case CBLOCK_DESERIALIZE: { + try { CBlock block; ds >> block; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CTRANSACTION_DESERIALIZE: - { - try - { + case CTRANSACTION_DESERIALIZE: { + try { CTransaction tx(deserialize, ds); - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CBLOCKLOCATOR_DESERIALIZE: - { - try - { + case CBLOCKLOCATOR_DESERIALIZE: { + try { CBlockLocator bl; ds >> bl; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CBLOCKMERKLEROOT: - { - try - { + case CBLOCKMERKLEROOT: { + try { CBlock block; ds >> block; bool mutated; BlockMerkleRoot(block, &mutated); - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CADDRMAN_DESERIALIZE: - { - try - { + case CADDRMAN_DESERIALIZE: { + try { CAddrMan am; ds >> am; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CBLOCKHEADER_DESERIALIZE: - { - try - { + case CBLOCKHEADER_DESERIALIZE: { + try { CBlockHeader bh; ds >> bh; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CBANENTRY_DESERIALIZE: - { - try - { + case CBANENTRY_DESERIALIZE: { + try { CBanEntry be; ds >> be; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CTXUNDO_DESERIALIZE: - { - try - { + case CTXUNDO_DESERIALIZE: { + try { CTxUndo tu; ds >> tu; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CBLOCKUNDO_DESERIALIZE: - { - try - { + case CBLOCKUNDO_DESERIALIZE: { + try { CBlockUndo bu; ds >> bu; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CCOINS_DESERIALIZE: - { - try - { + case CCOINS_DESERIALIZE: { + try { CCoins block; ds >> block; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CNETADDR_DESERIALIZE: - { - try - { + case CNETADDR_DESERIALIZE: { + try { CNetAddr na; ds >> na; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CSERVICE_DESERIALIZE: - { - try - { + case CSERVICE_DESERIALIZE: { + try { CService s; ds >> s; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CMESSAGEHEADER_DESERIALIZE: - { - CMessageHeader::MessageStartChars pchMessageStart = {0x00, 0x00, 0x00, 0x00}; - try - { + case CMESSAGEHEADER_DESERIALIZE: { + CMessageHeader::MessageStartChars pchMessageStart = {0x00, 0x00, + 0x00, 0x00}; + try { CMessageHeader mh(pchMessageStart); ds >> mh; - if (!mh.IsValid(pchMessageStart)) {return 0;} - } catch (const std::ios_base::failure& e) {return 0;} + if (!mh.IsValid(pchMessageStart)) { + return 0; + } + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CADDRESS_DESERIALIZE: - { - try - { + case CADDRESS_DESERIALIZE: { + try { CAddress a; ds >> a; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CINV_DESERIALIZE: - { - try - { + case CINV_DESERIALIZE: { + try { CInv i; ds >> i; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CBLOOMFILTER_DESERIALIZE: - { - try - { + case CBLOOMFILTER_DESERIALIZE: { + try { CBloomFilter bf; ds >> bf; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CDISKBLOCKINDEX_DESERIALIZE: - { - try - { + case CDISKBLOCKINDEX_DESERIALIZE: { + try { CDiskBlockIndex dbi; ds >> dbi; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } - case CTXOUTCOMPRESSOR_DESERIALIZE: - { + case CTXOUTCOMPRESSOR_DESERIALIZE: { CTxOut to; CTxOutCompressor toc(to); - try - { + try { ds >> toc; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure &e) { + return 0; + } break; } default: return 0; } return 0; } - diff --git a/src/test/test_random.h b/src/test/test_random.h index 4a1637ac7..9532221d9 100644 --- a/src/test/test_random.h +++ b/src/test/test_random.h @@ -1,23 +1,21 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_TEST_RANDOM_H #define BITCOIN_TEST_RANDOM_H #include "random.h" extern FastRandomContext insecure_rand_ctx; -static inline void seed_insecure_rand(bool fDeterministic = false) -{ +static inline void seed_insecure_rand(bool fDeterministic = false) { insecure_rand_ctx = FastRandomContext(fDeterministic); } -static inline uint32_t insecure_rand(void) -{ +static inline uint32_t insecure_rand(void) { return insecure_rand_ctx.rand32(); } #endif diff --git a/src/test/timedata_tests.cpp b/src/test/timedata_tests.cpp index 34863fd9d..12eae05d3 100644 --- a/src/test/timedata_tests.cpp +++ b/src/test/timedata_tests.cpp @@ -1,37 +1,36 @@ // Copyright (c) 2011-2015 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. // #include "timedata.h" #include "test/test_bitcoin.h" #include BOOST_FIXTURE_TEST_SUITE(timedata_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(util_MedianFilter) -{ +BOOST_AUTO_TEST_CASE(util_MedianFilter) { CMedianFilter filter(5, 15); BOOST_CHECK_EQUAL(filter.median(), 15); filter.input(20); // [15 20] BOOST_CHECK_EQUAL(filter.median(), 17); filter.input(30); // [15 20 30] BOOST_CHECK_EQUAL(filter.median(), 20); filter.input(3); // [3 15 20 30] BOOST_CHECK_EQUAL(filter.median(), 17); filter.input(7); // [3 7 15 20 30] BOOST_CHECK_EQUAL(filter.median(), 15); filter.input(18); // [3 7 18 20 30] BOOST_CHECK_EQUAL(filter.median(), 18); filter.input(0); // [0 3 7 18 30] BOOST_CHECK_EQUAL(filter.median(), 7); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp index 70d83a2e5..50be5ac51 100644 --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -1,269 +1,277 @@ // Copyright (c) 2011-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "arith_uint256.h" #include "uint256.h" -#include "version.h" +#include "arith_uint256.h" #include "test/test_bitcoin.h" +#include "version.h" #include -#include -#include +#include +#include +#include #include #include -#include +#include #include -#include BOOST_FIXTURE_TEST_SUITE(uint256_tests, BasicTestingSetup) const unsigned char R1Array[] = "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2" "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"; -const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; -const uint256 R1L = uint256(std::vector(R1Array,R1Array+32)); -const uint160 R1S = uint160(std::vector(R1Array,R1Array+20)); +const char R1ArrayHex[] = + "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; +const uint256 R1L = uint256(std::vector(R1Array, R1Array + 32)); +const uint160 R1S = uint160(std::vector(R1Array, R1Array + 20)); const unsigned char R2Array[] = "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf" "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7"; -const uint256 R2L = uint256(std::vector(R2Array,R2Array+32)); -const uint160 R2S = uint160(std::vector(R2Array,R2Array+20)); +const uint256 R2L = uint256(std::vector(R2Array, R2Array + 32)); +const uint160 R2S = uint160(std::vector(R2Array, R2Array + 20)); const unsigned char ZeroArray[] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; -const uint256 ZeroL = uint256(std::vector(ZeroArray,ZeroArray+32)); -const uint160 ZeroS = uint160(std::vector(ZeroArray,ZeroArray+20)); +const uint256 ZeroL = + uint256(std::vector(ZeroArray, ZeroArray + 32)); +const uint160 ZeroS = + uint160(std::vector(ZeroArray, ZeroArray + 20)); const unsigned char OneArray[] = "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; -const uint256 OneL = uint256(std::vector(OneArray,OneArray+32)); -const uint160 OneS = uint160(std::vector(OneArray,OneArray+20)); +const uint256 OneL = + uint256(std::vector(OneArray, OneArray + 32)); +const uint160 OneS = + uint160(std::vector(OneArray, OneArray + 20)); const unsigned char MaxArray[] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; -const uint256 MaxL = uint256(std::vector(MaxArray,MaxArray+32)); -const uint160 MaxS = uint160(std::vector(MaxArray,MaxArray+20)); +const uint256 MaxL = + uint256(std::vector(MaxArray, MaxArray + 32)); +const uint160 MaxS = + uint160(std::vector(MaxArray, MaxArray + 20)); -std::string ArrayToString(const unsigned char A[], unsigned int width) -{ +std::string ArrayToString(const unsigned char A[], unsigned int width) { std::stringstream Stream; Stream << std::hex; - for (unsigned int i = 0; i < width; ++i) - { - Stream<): - BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32)); - BOOST_CHECK(R1S.ToString() == ArrayToString(R1Array,20)); - BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32)); - BOOST_CHECK(R2S.ToString() == ArrayToString(R2Array,20)); - BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32)); - BOOST_CHECK(ZeroS.ToString() == ArrayToString(ZeroArray,20)); - BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32)); - BOOST_CHECK(OneS.ToString() == ArrayToString(OneArray,20)); - BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32)); - BOOST_CHECK(MaxS.ToString() == ArrayToString(MaxArray,20)); - BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32)); - BOOST_CHECK(OneS.ToString() != ArrayToString(ZeroArray,20)); + BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array, 32)); + BOOST_CHECK(R1S.ToString() == ArrayToString(R1Array, 20)); + BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array, 32)); + BOOST_CHECK(R2S.ToString() == ArrayToString(R2Array, 20)); + BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray, 32)); + BOOST_CHECK(ZeroS.ToString() == ArrayToString(ZeroArray, 20)); + BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray, 32)); + BOOST_CHECK(OneS.ToString() == ArrayToString(OneArray, 20)); + BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray, 32)); + BOOST_CHECK(MaxS.ToString() == ArrayToString(MaxArray, 20)); + BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray, 32)); + BOOST_CHECK(OneS.ToString() != ArrayToString(ZeroArray, 20)); // == and != BOOST_CHECK(R1L != R2L && R1S != R2S); BOOST_CHECK(ZeroL != OneL && ZeroS != OneS); BOOST_CHECK(OneL != ZeroL && OneS != ZeroS); BOOST_CHECK(MaxL != ZeroL && MaxS != ZeroS); // String Constructor and Copy Constructor - BOOST_CHECK(uint256S("0x"+R1L.ToString()) == R1L); - BOOST_CHECK(uint256S("0x"+R2L.ToString()) == R2L); - BOOST_CHECK(uint256S("0x"+ZeroL.ToString()) == ZeroL); - BOOST_CHECK(uint256S("0x"+OneL.ToString()) == OneL); - BOOST_CHECK(uint256S("0x"+MaxL.ToString()) == MaxL); + BOOST_CHECK(uint256S("0x" + R1L.ToString()) == R1L); + BOOST_CHECK(uint256S("0x" + R2L.ToString()) == R2L); + BOOST_CHECK(uint256S("0x" + ZeroL.ToString()) == ZeroL); + BOOST_CHECK(uint256S("0x" + OneL.ToString()) == OneL); + BOOST_CHECK(uint256S("0x" + MaxL.ToString()) == MaxL); BOOST_CHECK(uint256S(R1L.ToString()) == R1L); - BOOST_CHECK(uint256S(" 0x"+R1L.ToString()+" ") == R1L); + BOOST_CHECK(uint256S(" 0x" + R1L.ToString() + " ") == R1L); BOOST_CHECK(uint256S("") == ZeroL); BOOST_CHECK(R1L == uint256S(R1ArrayHex)); BOOST_CHECK(uint256(R1L) == R1L); BOOST_CHECK(uint256(ZeroL) == ZeroL); BOOST_CHECK(uint256(OneL) == OneL); - BOOST_CHECK(uint160S("0x"+R1S.ToString()) == R1S); - BOOST_CHECK(uint160S("0x"+R2S.ToString()) == R2S); - BOOST_CHECK(uint160S("0x"+ZeroS.ToString()) == ZeroS); - BOOST_CHECK(uint160S("0x"+OneS.ToString()) == OneS); - BOOST_CHECK(uint160S("0x"+MaxS.ToString()) == MaxS); + BOOST_CHECK(uint160S("0x" + R1S.ToString()) == R1S); + BOOST_CHECK(uint160S("0x" + R2S.ToString()) == R2S); + BOOST_CHECK(uint160S("0x" + ZeroS.ToString()) == ZeroS); + BOOST_CHECK(uint160S("0x" + OneS.ToString()) == OneS); + BOOST_CHECK(uint160S("0x" + MaxS.ToString()) == MaxS); BOOST_CHECK(uint160S(R1S.ToString()) == R1S); - BOOST_CHECK(uint160S(" 0x"+R1S.ToString()+" ") == R1S); + BOOST_CHECK(uint160S(" 0x" + R1S.ToString() + " ") == R1S); BOOST_CHECK(uint160S("") == ZeroS); BOOST_CHECK(R1S == uint160S(R1ArrayHex)); BOOST_CHECK(uint160(R1S) == R1S); BOOST_CHECK(uint160(ZeroS) == ZeroS); BOOST_CHECK(uint160(OneS) == OneS); } -BOOST_AUTO_TEST_CASE( comparison ) // <= >= < > -{ +// <= >= < > +BOOST_AUTO_TEST_CASE(comparison) { uint256 LastL; for (int i = 255; i >= 0; --i) { uint256 TmpL; - *(TmpL.begin() + (i>>3)) |= 1<<(7-(i&7)); - BOOST_CHECK( LastL < TmpL ); + *(TmpL.begin() + (i >> 3)) |= 1 << (7 - (i & 7)); + BOOST_CHECK(LastL < TmpL); LastL = TmpL; } - BOOST_CHECK( ZeroL < R1L ); - BOOST_CHECK( R2L < R1L ); - BOOST_CHECK( ZeroL < OneL ); - BOOST_CHECK( OneL < MaxL ); - BOOST_CHECK( R1L < MaxL ); - BOOST_CHECK( R2L < MaxL ); + BOOST_CHECK(ZeroL < R1L); + BOOST_CHECK(R2L < R1L); + BOOST_CHECK(ZeroL < OneL); + BOOST_CHECK(OneL < MaxL); + BOOST_CHECK(R1L < MaxL); + BOOST_CHECK(R2L < MaxL); uint160 LastS; for (int i = 159; i >= 0; --i) { uint160 TmpS; - *(TmpS.begin() + (i>>3)) |= 1<<(7-(i&7)); - BOOST_CHECK( LastS < TmpS ); + *(TmpS.begin() + (i >> 3)) |= 1 << (7 - (i & 7)); + BOOST_CHECK(LastS < TmpS); LastS = TmpS; } - BOOST_CHECK( ZeroS < R1S ); - BOOST_CHECK( R2S < R1S ); - BOOST_CHECK( ZeroS < OneS ); - BOOST_CHECK( OneS < MaxS ); - BOOST_CHECK( R1S < MaxS ); - BOOST_CHECK( R2S < MaxS ); + BOOST_CHECK(ZeroS < R1S); + BOOST_CHECK(R2S < R1S); + BOOST_CHECK(ZeroS < OneS); + BOOST_CHECK(OneS < MaxS); + BOOST_CHECK(R1S < MaxS); + BOOST_CHECK(R2S < MaxS); } -BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize -{ +// GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, +// Unserialize +BOOST_AUTO_TEST_CASE(methods) { BOOST_CHECK(R1L.GetHex() == R1L.ToString()); BOOST_CHECK(R2L.GetHex() == R2L.ToString()); BOOST_CHECK(OneL.GetHex() == OneL.ToString()); BOOST_CHECK(MaxL.GetHex() == MaxL.ToString()); uint256 TmpL(R1L); BOOST_CHECK(TmpL == R1L); - TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L); - TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == uint256()); + TmpL.SetHex(R2L.ToString()); + BOOST_CHECK(TmpL == R2L); + TmpL.SetHex(ZeroL.ToString()); + BOOST_CHECK(TmpL == uint256()); TmpL.SetHex(R1L.ToString()); - BOOST_CHECK(memcmp(R1L.begin(), R1Array, 32)==0); - BOOST_CHECK(memcmp(TmpL.begin(), R1Array, 32)==0); - BOOST_CHECK(memcmp(R2L.begin(), R2Array, 32)==0); - BOOST_CHECK(memcmp(ZeroL.begin(), ZeroArray, 32)==0); - BOOST_CHECK(memcmp(OneL.begin(), OneArray, 32)==0); + BOOST_CHECK(memcmp(R1L.begin(), R1Array, 32) == 0); + BOOST_CHECK(memcmp(TmpL.begin(), R1Array, 32) == 0); + BOOST_CHECK(memcmp(R2L.begin(), R2Array, 32) == 0); + BOOST_CHECK(memcmp(ZeroL.begin(), ZeroArray, 32) == 0); + BOOST_CHECK(memcmp(OneL.begin(), OneArray, 32) == 0); BOOST_CHECK(R1L.size() == sizeof(R1L)); BOOST_CHECK(sizeof(R1L) == 32); BOOST_CHECK(R1L.size() == 32); BOOST_CHECK(R2L.size() == 32); BOOST_CHECK(ZeroL.size() == 32); BOOST_CHECK(MaxL.size() == 32); BOOST_CHECK(R1L.begin() + 32 == R1L.end()); BOOST_CHECK(R2L.begin() + 32 == R2L.end()); BOOST_CHECK(OneL.begin() + 32 == OneL.end()); BOOST_CHECK(MaxL.begin() + 32 == MaxL.end()); BOOST_CHECK(TmpL.begin() + 32 == TmpL.end()); BOOST_CHECK(GetSerializeSize(R1L, 0, PROTOCOL_VERSION) == 32); BOOST_CHECK(GetSerializeSize(ZeroL, 0, PROTOCOL_VERSION) == 32); CDataStream ss(0, PROTOCOL_VERSION); ss << R1L; - BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32)); + BOOST_CHECK(ss.str() == std::string(R1Array, R1Array + 32)); ss >> TmpL; BOOST_CHECK(R1L == TmpL); ss.clear(); ss << ZeroL; - BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32)); + BOOST_CHECK(ss.str() == std::string(ZeroArray, ZeroArray + 32)); ss >> TmpL; BOOST_CHECK(ZeroL == TmpL); ss.clear(); ss << MaxL; - BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32)); + BOOST_CHECK(ss.str() == std::string(MaxArray, MaxArray + 32)); ss >> TmpL; BOOST_CHECK(MaxL == TmpL); ss.clear(); BOOST_CHECK(R1S.GetHex() == R1S.ToString()); BOOST_CHECK(R2S.GetHex() == R2S.ToString()); BOOST_CHECK(OneS.GetHex() == OneS.ToString()); BOOST_CHECK(MaxS.GetHex() == MaxS.ToString()); uint160 TmpS(R1S); BOOST_CHECK(TmpS == R1S); - TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S); - TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == uint160()); + TmpS.SetHex(R2S.ToString()); + BOOST_CHECK(TmpS == R2S); + TmpS.SetHex(ZeroS.ToString()); + BOOST_CHECK(TmpS == uint160()); TmpS.SetHex(R1S.ToString()); - BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20)==0); - BOOST_CHECK(memcmp(TmpS.begin(), R1Array, 20)==0); - BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20)==0); - BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20)==0); - BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20)==0); + BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20) == 0); + BOOST_CHECK(memcmp(TmpS.begin(), R1Array, 20) == 0); + BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20) == 0); + BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20) == 0); + BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20) == 0); BOOST_CHECK(R1S.size() == sizeof(R1S)); BOOST_CHECK(sizeof(R1S) == 20); BOOST_CHECK(R1S.size() == 20); BOOST_CHECK(R2S.size() == 20); BOOST_CHECK(ZeroS.size() == 20); BOOST_CHECK(MaxS.size() == 20); BOOST_CHECK(R1S.begin() + 20 == R1S.end()); BOOST_CHECK(R2S.begin() + 20 == R2S.end()); BOOST_CHECK(OneS.begin() + 20 == OneS.end()); BOOST_CHECK(MaxS.begin() + 20 == MaxS.end()); BOOST_CHECK(TmpS.begin() + 20 == TmpS.end()); BOOST_CHECK(GetSerializeSize(R1S, 0, PROTOCOL_VERSION) == 20); BOOST_CHECK(GetSerializeSize(ZeroS, 0, PROTOCOL_VERSION) == 20); ss << R1S; - BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20)); + BOOST_CHECK(ss.str() == std::string(R1Array, R1Array + 20)); ss >> TmpS; BOOST_CHECK(R1S == TmpS); ss.clear(); ss << ZeroS; - BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20)); + BOOST_CHECK(ss.str() == std::string(ZeroArray, ZeroArray + 20)); ss >> TmpS; BOOST_CHECK(ZeroS == TmpS); ss.clear(); ss << MaxS; - BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20)); + BOOST_CHECK(ss.str() == std::string(MaxArray, MaxArray + 20)); ss >> TmpS; BOOST_CHECK(MaxS == TmpS); ss.clear(); } -BOOST_AUTO_TEST_CASE( conversion ) -{ +BOOST_AUTO_TEST_CASE(conversion) { BOOST_CHECK(ArithToUint256(UintToArith256(ZeroL)) == ZeroL); BOOST_CHECK(ArithToUint256(UintToArith256(OneL)) == OneL); BOOST_CHECK(ArithToUint256(UintToArith256(R1L)) == R1L); BOOST_CHECK(ArithToUint256(UintToArith256(R2L)) == R2L); BOOST_CHECK(UintToArith256(ZeroL) == 0); BOOST_CHECK(UintToArith256(OneL) == 1); BOOST_CHECK(ArithToUint256(0) == ZeroL); BOOST_CHECK(ArithToUint256(1) == OneL); BOOST_CHECK(arith_uint256(R1L.GetHex()) == UintToArith256(R1L)); BOOST_CHECK(arith_uint256(R2L.GetHex()) == UintToArith256(R2L)); BOOST_CHECK(R1L.GetHex() == UintToArith256(R1L).GetHex()); BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex()); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/univalue_tests.cpp b/src/test/univalue_tests.cpp index dffe8e55a..16d2df402 100644 --- a/src/test/univalue_tests.cpp +++ b/src/test/univalue_tests.cpp @@ -1,333 +1,327 @@ // Copyright (c) 2014 BitPay Inc. // Copyright (c) 2014-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "test/test_bitcoin.h" +#include #include -#include #include -#include #include -#include "test/test_bitcoin.h" +#include #include BOOST_FIXTURE_TEST_SUITE(univalue_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(univalue_constructor) -{ +BOOST_AUTO_TEST_CASE(univalue_constructor) { UniValue v1; BOOST_CHECK(v1.isNull()); UniValue v2(UniValue::VSTR); BOOST_CHECK(v2.isStr()); UniValue v3(UniValue::VSTR, "foo"); BOOST_CHECK(v3.isStr()); BOOST_CHECK_EQUAL(v3.getValStr(), "foo"); UniValue numTest; BOOST_CHECK(numTest.setNumStr("82")); BOOST_CHECK(numTest.isNum()); BOOST_CHECK_EQUAL(numTest.getValStr(), "82"); uint64_t vu64 = 82; UniValue v4(vu64); BOOST_CHECK(v4.isNum()); BOOST_CHECK_EQUAL(v4.getValStr(), "82"); int64_t vi64 = -82; UniValue v5(vi64); BOOST_CHECK(v5.isNum()); BOOST_CHECK_EQUAL(v5.getValStr(), "-82"); int vi = -688; UniValue v6(vi); BOOST_CHECK(v6.isNum()); BOOST_CHECK_EQUAL(v6.getValStr(), "-688"); double vd = -7.21; UniValue v7(vd); BOOST_CHECK(v7.isNum()); BOOST_CHECK_EQUAL(v7.getValStr(), "-7.21"); std::string vs("yawn"); UniValue v8(vs); BOOST_CHECK(v8.isStr()); BOOST_CHECK_EQUAL(v8.getValStr(), "yawn"); const char *vcs = "zappa"; UniValue v9(vcs); BOOST_CHECK(v9.isStr()); BOOST_CHECK_EQUAL(v9.getValStr(), "zappa"); } -BOOST_AUTO_TEST_CASE(univalue_typecheck) -{ +BOOST_AUTO_TEST_CASE(univalue_typecheck) { UniValue v1; BOOST_CHECK(v1.setNumStr("1")); BOOST_CHECK(v1.isNum()); BOOST_CHECK_THROW(v1.get_bool(), std::runtime_error); UniValue v2; BOOST_CHECK(v2.setBool(true)); BOOST_CHECK_EQUAL(v2.get_bool(), true); BOOST_CHECK_THROW(v2.get_int(), std::runtime_error); UniValue v3; BOOST_CHECK(v3.setNumStr("32482348723847471234")); BOOST_CHECK_THROW(v3.get_int64(), std::runtime_error); BOOST_CHECK(v3.setNumStr("1000")); BOOST_CHECK_EQUAL(v3.get_int64(), 1000); UniValue v4; BOOST_CHECK(v4.setNumStr("2147483648")); BOOST_CHECK_EQUAL(v4.get_int64(), 2147483648); BOOST_CHECK_THROW(v4.get_int(), std::runtime_error); BOOST_CHECK(v4.setNumStr("1000")); BOOST_CHECK_EQUAL(v4.get_int(), 1000); BOOST_CHECK_THROW(v4.get_str(), std::runtime_error); BOOST_CHECK_EQUAL(v4.get_real(), 1000); BOOST_CHECK_THROW(v4.get_array(), std::runtime_error); BOOST_CHECK_THROW(v4.getKeys(), std::runtime_error); BOOST_CHECK_THROW(v4.getValues(), std::runtime_error); BOOST_CHECK_THROW(v4.get_obj(), std::runtime_error); UniValue v5; BOOST_CHECK(v5.read("[true, 10]")); BOOST_CHECK_NO_THROW(v5.get_array()); std::vector vals = v5.getValues(); BOOST_CHECK_THROW(vals[0].get_int(), std::runtime_error); BOOST_CHECK_EQUAL(vals[0].get_bool(), true); BOOST_CHECK_EQUAL(vals[1].get_int(), 10); BOOST_CHECK_THROW(vals[1].get_bool(), std::runtime_error); } -BOOST_AUTO_TEST_CASE(univalue_set) -{ +BOOST_AUTO_TEST_CASE(univalue_set) { UniValue v(UniValue::VSTR, "foo"); v.clear(); BOOST_CHECK(v.isNull()); BOOST_CHECK_EQUAL(v.getValStr(), ""); BOOST_CHECK(v.setObject()); BOOST_CHECK(v.isObject()); BOOST_CHECK_EQUAL(v.size(), 0); BOOST_CHECK_EQUAL(v.getType(), UniValue::VOBJ); BOOST_CHECK(v.empty()); BOOST_CHECK(v.setArray()); BOOST_CHECK(v.isArray()); BOOST_CHECK_EQUAL(v.size(), 0); BOOST_CHECK(v.setStr("zum")); BOOST_CHECK(v.isStr()); BOOST_CHECK_EQUAL(v.getValStr(), "zum"); BOOST_CHECK(v.setFloat(-1.01)); BOOST_CHECK(v.isNum()); BOOST_CHECK_EQUAL(v.getValStr(), "-1.01"); BOOST_CHECK(v.setInt((int)1023)); BOOST_CHECK(v.isNum()); BOOST_CHECK_EQUAL(v.getValStr(), "1023"); BOOST_CHECK(v.setInt((int64_t)-1023LL)); BOOST_CHECK(v.isNum()); BOOST_CHECK_EQUAL(v.getValStr(), "-1023"); BOOST_CHECK(v.setInt((uint64_t)1023ULL)); BOOST_CHECK(v.isNum()); BOOST_CHECK_EQUAL(v.getValStr(), "1023"); BOOST_CHECK(v.setNumStr("-688")); BOOST_CHECK(v.isNum()); BOOST_CHECK_EQUAL(v.getValStr(), "-688"); BOOST_CHECK(v.setBool(false)); BOOST_CHECK_EQUAL(v.isBool(), true); BOOST_CHECK_EQUAL(v.isTrue(), false); BOOST_CHECK_EQUAL(v.isFalse(), true); BOOST_CHECK_EQUAL(v.getBool(), false); BOOST_CHECK(v.setBool(true)); BOOST_CHECK_EQUAL(v.isBool(), true); BOOST_CHECK_EQUAL(v.isTrue(), true); BOOST_CHECK_EQUAL(v.isFalse(), false); BOOST_CHECK_EQUAL(v.getBool(), true); BOOST_CHECK(!v.setNumStr("zombocom")); BOOST_CHECK(v.setNull()); BOOST_CHECK(v.isNull()); } -BOOST_AUTO_TEST_CASE(univalue_array) -{ +BOOST_AUTO_TEST_CASE(univalue_array) { UniValue arr(UniValue::VARR); UniValue v((int64_t)1023LL); BOOST_CHECK(arr.push_back(v)); std::string vStr("zippy"); BOOST_CHECK(arr.push_back(vStr)); const char *s = "pippy"; BOOST_CHECK(arr.push_back(s)); std::vector vec; v.setStr("boing"); vec.push_back(v); v.setStr("going"); vec.push_back(v); BOOST_CHECK(arr.push_backV(vec)); BOOST_CHECK_EQUAL(arr.empty(), false); BOOST_CHECK_EQUAL(arr.size(), 5); BOOST_CHECK_EQUAL(arr[0].getValStr(), "1023"); BOOST_CHECK_EQUAL(arr[1].getValStr(), "zippy"); BOOST_CHECK_EQUAL(arr[2].getValStr(), "pippy"); BOOST_CHECK_EQUAL(arr[3].getValStr(), "boing"); BOOST_CHECK_EQUAL(arr[4].getValStr(), "going"); BOOST_CHECK_EQUAL(arr[999].getValStr(), ""); arr.clear(); BOOST_CHECK(arr.empty()); BOOST_CHECK_EQUAL(arr.size(), 0); } -BOOST_AUTO_TEST_CASE(univalue_object) -{ +BOOST_AUTO_TEST_CASE(univalue_object) { UniValue obj(UniValue::VOBJ); std::string strKey, strVal; UniValue v; strKey = "age"; v.setInt(100); BOOST_CHECK(obj.pushKV(strKey, v)); strKey = "first"; strVal = "John"; BOOST_CHECK(obj.pushKV(strKey, strVal)); strKey = "last"; const char *cVal = "Smith"; BOOST_CHECK(obj.pushKV(strKey, cVal)); strKey = "distance"; - BOOST_CHECK(obj.pushKV(strKey, (int64_t) 25)); + BOOST_CHECK(obj.pushKV(strKey, (int64_t)25)); strKey = "time"; - BOOST_CHECK(obj.pushKV(strKey, (uint64_t) 3600)); + BOOST_CHECK(obj.pushKV(strKey, (uint64_t)3600)); strKey = "calories"; - BOOST_CHECK(obj.pushKV(strKey, (int) 12)); + BOOST_CHECK(obj.pushKV(strKey, (int)12)); strKey = "temperature"; - BOOST_CHECK(obj.pushKV(strKey, (double) 90.012)); + BOOST_CHECK(obj.pushKV(strKey, (double)90.012)); UniValue obj2(UniValue::VOBJ); BOOST_CHECK(obj2.pushKV("cat1", 9000)); BOOST_CHECK(obj2.pushKV("cat2", 12345)); BOOST_CHECK(obj.pushKVs(obj2)); BOOST_CHECK_EQUAL(obj.empty(), false); BOOST_CHECK_EQUAL(obj.size(), 9); BOOST_CHECK_EQUAL(obj["age"].getValStr(), "100"); BOOST_CHECK_EQUAL(obj["first"].getValStr(), "John"); BOOST_CHECK_EQUAL(obj["last"].getValStr(), "Smith"); BOOST_CHECK_EQUAL(obj["distance"].getValStr(), "25"); BOOST_CHECK_EQUAL(obj["time"].getValStr(), "3600"); BOOST_CHECK_EQUAL(obj["calories"].getValStr(), "12"); BOOST_CHECK_EQUAL(obj["temperature"].getValStr(), "90.012"); BOOST_CHECK_EQUAL(obj["cat1"].getValStr(), "9000"); BOOST_CHECK_EQUAL(obj["cat2"].getValStr(), "12345"); BOOST_CHECK_EQUAL(obj["nyuknyuknyuk"].getValStr(), ""); BOOST_CHECK(obj.exists("age")); BOOST_CHECK(obj.exists("first")); BOOST_CHECK(obj.exists("last")); BOOST_CHECK(obj.exists("distance")); BOOST_CHECK(obj.exists("time")); BOOST_CHECK(obj.exists("calories")); BOOST_CHECK(obj.exists("temperature")); BOOST_CHECK(obj.exists("cat1")); BOOST_CHECK(obj.exists("cat2")); BOOST_CHECK(!obj.exists("nyuknyuknyuk")); std::map objTypes; objTypes["age"] = UniValue::VNUM; objTypes["first"] = UniValue::VSTR; objTypes["last"] = UniValue::VSTR; objTypes["distance"] = UniValue::VNUM; objTypes["time"] = UniValue::VNUM; objTypes["calories"] = UniValue::VNUM; objTypes["temperature"] = UniValue::VNUM; objTypes["cat1"] = UniValue::VNUM; objTypes["cat2"] = UniValue::VNUM; BOOST_CHECK(obj.checkObject(objTypes)); objTypes["cat2"] = UniValue::VSTR; BOOST_CHECK(!obj.checkObject(objTypes)); obj.clear(); BOOST_CHECK(obj.empty()); BOOST_CHECK_EQUAL(obj.size(), 0); } -static const char *json1 = -"[1.10000000,{\"key1\":\"str\\u0000\",\"key2\":800,\"key3\":{\"name\":\"martian http://test.com\"}}]"; +static const char *json1 = "[1.10000000,{\"key1\":\"str\\u0000\",\"key2\":800," + "\"key3\":{\"name\":\"martian http://test.com\"}}]"; -BOOST_AUTO_TEST_CASE(univalue_readwrite) -{ +BOOST_AUTO_TEST_CASE(univalue_readwrite) { UniValue v; BOOST_CHECK(v.read(json1)); std::string strJson1(json1); BOOST_CHECK(v.read(strJson1)); BOOST_CHECK(v.isArray()); BOOST_CHECK_EQUAL(v.size(), 2); BOOST_CHECK_EQUAL(v[0].getValStr(), "1.10000000"); UniValue obj = v[1]; BOOST_CHECK(obj.isObject()); BOOST_CHECK_EQUAL(obj.size(), 3); BOOST_CHECK(obj["key1"].isStr()); std::string correctValue("str"); correctValue.push_back('\0'); BOOST_CHECK_EQUAL(obj["key1"].getValStr(), correctValue); BOOST_CHECK(obj["key2"].isNum()); BOOST_CHECK_EQUAL(obj["key2"].getValStr(), "800"); BOOST_CHECK(obj["key3"].isObject()); BOOST_CHECK_EQUAL(strJson1, v.write()); /* Check for (correctly reporting) a parsing error if the initial JSON construct is followed by more stuff. Note that whitespace is, of course, exempt. */ BOOST_CHECK(v.read(" {}\n ")); BOOST_CHECK(v.isObject()); BOOST_CHECK(v.read(" []\n ")); BOOST_CHECK(v.isArray()); BOOST_CHECK(!v.read("@{}")); BOOST_CHECK(!v.read("{} garbage")); BOOST_CHECK(!v.read("[]{}")); BOOST_CHECK(!v.read("{}[]")); BOOST_CHECK(!v.read("{} 42")); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 641655621..d8d3e909c 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1,569 +1,623 @@ // Copyright (c) 2011-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "util.h" #include "clientversion.h" #include "primitives/transaction.h" #include "sync.h" -#include "utilstrencodings.h" -#include "utilmoneystr.h" #include "test/test_bitcoin.h" #include "test/test_random.h" +#include "utilmoneystr.h" +#include "utilstrencodings.h" #include #include #include extern std::map mapArgs; BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup) -BOOST_AUTO_TEST_CASE(util_criticalsection) -{ +BOOST_AUTO_TEST_CASE(util_criticalsection) { CCriticalSection cs; do { LOCK(cs); break; BOOST_ERROR("break was swallowed!"); - } while(0); + } while (0); do { TRY_LOCK(cs, lockTest); - if (lockTest) - break; + if (lockTest) break; BOOST_ERROR("break was swallowed!"); - } while(0); + } while (0); } static const unsigned char ParseHex_expected[65] = { - 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, - 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, - 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, - 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, - 0x5f -}; -BOOST_AUTO_TEST_CASE(util_ParseHex) -{ + 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, + 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, + 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, + 0xe5, 0x1e, 0xc1, 0x12, 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, + 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f}; +BOOST_AUTO_TEST_CASE(util_ParseHex) { std::vector result; - std::vector expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)); + std::vector expected( + ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)); // Basic test vector - result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"); - BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); + result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0" + "ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d" + "578a4c702b6bf11d5f"); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), + expected.begin(), expected.end()); // Spaces between bytes must be supported result = ParseHex("12 34 56 78"); - BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78); + BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && + result[2] == 0x56 && result[3] == 0x78); // Leading space must be supported (used in CDBEnv::Salvage) result = ParseHex(" 89 34 56 78"); - BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78); + BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && + result[2] == 0x56 && result[3] == 0x78); // Stop parsing at invalid value result = ParseHex("1234 invalid 1234"); BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34); } -BOOST_AUTO_TEST_CASE(util_HexStr) -{ - BOOST_CHECK_EQUAL( - HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)), - "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"); +BOOST_AUTO_TEST_CASE(util_HexStr) { + BOOST_CHECK_EQUAL(HexStr(ParseHex_expected, + ParseHex_expected + sizeof(ParseHex_expected)), + "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0" + "ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d" + "578a4c702b6bf11d5f"); - BOOST_CHECK_EQUAL( - HexStr(ParseHex_expected, ParseHex_expected + 5, true), - "04 67 8a fd b0"); + BOOST_CHECK_EQUAL(HexStr(ParseHex_expected, ParseHex_expected + 5, true), + "04 67 8a fd b0"); - BOOST_CHECK_EQUAL( - HexStr(ParseHex_expected, ParseHex_expected, true), - ""); + BOOST_CHECK_EQUAL(HexStr(ParseHex_expected, ParseHex_expected, true), ""); - std::vector ParseHex_vec(ParseHex_expected, ParseHex_expected + 5); + std::vector ParseHex_vec(ParseHex_expected, + ParseHex_expected + 5); - BOOST_CHECK_EQUAL( - HexStr(ParseHex_vec, true), - "04 67 8a fd b0"); + BOOST_CHECK_EQUAL(HexStr(ParseHex_vec, true), "04 67 8a fd b0"); } - -BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat) -{ - BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0), "1970-01-01 00:00:00"); - BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0x7FFFFFFF), "2038-01-19 03:14:07"); - BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 1317425777), "2011-09-30 23:36:17"); - BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M", 1317425777), "2011-09-30 23:36"); - BOOST_CHECK_EQUAL(DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", 1317425777), "Fri, 30 Sep 2011 23:36:17 +0000"); +BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat) { + BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0), + "1970-01-01 00:00:00"); + BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0x7FFFFFFF), + "2038-01-19 03:14:07"); + BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 1317425777), + "2011-09-30 23:36:17"); + BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M", 1317425777), + "2011-09-30 23:36"); + BOOST_CHECK_EQUAL( + DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", 1317425777), + "Fri, 30 Sep 2011 23:36:17 +0000"); } -BOOST_AUTO_TEST_CASE(util_ParseParameters) -{ - const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"}; +BOOST_AUTO_TEST_CASE(util_ParseParameters) { + const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", + "-ccc=multiple", "f", "-d=e"}; - ParseParameters(0, (char**)argv_test); + ParseParameters(0, (char **)argv_test); BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty()); - ParseParameters(1, (char**)argv_test); + ParseParameters(1, (char **)argv_test); BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty()); - ParseParameters(5, (char**)argv_test); + ParseParameters(5, (char **)argv_test); // expectation: -ignored is ignored (program name argument), // -a, -b and -ccc end up in map, -d ignored because it is after // a non-option argument (non-GNU option parsing) BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3); - BOOST_CHECK(IsArgSet("-a") && IsArgSet("-b") && IsArgSet("-ccc") - && !IsArgSet("f") && !IsArgSet("-d")); - BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc") - && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d")); + BOOST_CHECK(IsArgSet("-a") && IsArgSet("-b") && IsArgSet("-ccc") && + !IsArgSet("f") && !IsArgSet("-d")); + BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && + mapMultiArgs.count("-ccc") && !mapMultiArgs.count("f") && + !mapMultiArgs.count("-d")); BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple"); BOOST_CHECK(mapMultiArgs.at("-ccc").size() == 2); } -BOOST_AUTO_TEST_CASE(util_GetArg) -{ +BOOST_AUTO_TEST_CASE(util_GetArg) { mapArgs.clear(); mapArgs["strtest1"] = "string..."; // strtest2 undefined on purpose mapArgs["inttest1"] = "12345"; mapArgs["inttest2"] = "81985529216486895"; // inttest3 undefined on purpose mapArgs["booltest1"] = ""; // booltest2 undefined on purpose mapArgs["booltest3"] = "0"; mapArgs["booltest4"] = "1"; BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string..."); BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default"); BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345); BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL); BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1); BOOST_CHECK_EQUAL(GetBoolArg("booltest1", false), true); BOOST_CHECK_EQUAL(GetBoolArg("booltest2", false), false); BOOST_CHECK_EQUAL(GetBoolArg("booltest3", false), false); BOOST_CHECK_EQUAL(GetBoolArg("booltest4", false), true); } -BOOST_AUTO_TEST_CASE(util_FormatMoney) -{ +BOOST_AUTO_TEST_CASE(util_FormatMoney) { BOOST_CHECK_EQUAL(FormatMoney(0), "0.00"); - BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789), "12345.6789"); + BOOST_CHECK_EQUAL(FormatMoney((COIN / 10000) * 123456789), "12345.6789"); BOOST_CHECK_EQUAL(FormatMoney(-COIN), "-1.00"); - BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000), "100000000.00"); - BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000), "10000000.00"); - BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000), "1000000.00"); - BOOST_CHECK_EQUAL(FormatMoney(COIN*100000), "100000.00"); - BOOST_CHECK_EQUAL(FormatMoney(COIN*10000), "10000.00"); - BOOST_CHECK_EQUAL(FormatMoney(COIN*1000), "1000.00"); - BOOST_CHECK_EQUAL(FormatMoney(COIN*100), "100.00"); - BOOST_CHECK_EQUAL(FormatMoney(COIN*10), "10.00"); + BOOST_CHECK_EQUAL(FormatMoney(COIN * 100000000), "100000000.00"); + BOOST_CHECK_EQUAL(FormatMoney(COIN * 10000000), "10000000.00"); + BOOST_CHECK_EQUAL(FormatMoney(COIN * 1000000), "1000000.00"); + BOOST_CHECK_EQUAL(FormatMoney(COIN * 100000), "100000.00"); + BOOST_CHECK_EQUAL(FormatMoney(COIN * 10000), "10000.00"); + BOOST_CHECK_EQUAL(FormatMoney(COIN * 1000), "1000.00"); + BOOST_CHECK_EQUAL(FormatMoney(COIN * 100), "100.00"); + BOOST_CHECK_EQUAL(FormatMoney(COIN * 10), "10.00"); BOOST_CHECK_EQUAL(FormatMoney(COIN), "1.00"); - BOOST_CHECK_EQUAL(FormatMoney(COIN/10), "0.10"); - BOOST_CHECK_EQUAL(FormatMoney(COIN/100), "0.01"); - BOOST_CHECK_EQUAL(FormatMoney(COIN/1000), "0.001"); - BOOST_CHECK_EQUAL(FormatMoney(COIN/10000), "0.0001"); - BOOST_CHECK_EQUAL(FormatMoney(COIN/100000), "0.00001"); - BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000), "0.000001"); - BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000), "0.0000001"); - BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000), "0.00000001"); + BOOST_CHECK_EQUAL(FormatMoney(COIN / 10), "0.10"); + BOOST_CHECK_EQUAL(FormatMoney(COIN / 100), "0.01"); + BOOST_CHECK_EQUAL(FormatMoney(COIN / 1000), "0.001"); + BOOST_CHECK_EQUAL(FormatMoney(COIN / 10000), "0.0001"); + BOOST_CHECK_EQUAL(FormatMoney(COIN / 100000), "0.00001"); + BOOST_CHECK_EQUAL(FormatMoney(COIN / 1000000), "0.000001"); + BOOST_CHECK_EQUAL(FormatMoney(COIN / 10000000), "0.0000001"); + BOOST_CHECK_EQUAL(FormatMoney(COIN / 100000000), "0.00000001"); } -BOOST_AUTO_TEST_CASE(util_ParseMoney) -{ +BOOST_AUTO_TEST_CASE(util_ParseMoney) { CAmount ret = 0; BOOST_CHECK(ParseMoney("0.0", ret)); BOOST_CHECK_EQUAL(ret, 0); BOOST_CHECK(ParseMoney("12345.6789", ret)); - BOOST_CHECK_EQUAL(ret, (COIN/10000)*123456789); + BOOST_CHECK_EQUAL(ret, (COIN / 10000) * 123456789); BOOST_CHECK(ParseMoney("100000000.00", ret)); - BOOST_CHECK_EQUAL(ret, COIN*100000000); + BOOST_CHECK_EQUAL(ret, COIN * 100000000); BOOST_CHECK(ParseMoney("10000000.00", ret)); - BOOST_CHECK_EQUAL(ret, COIN*10000000); + BOOST_CHECK_EQUAL(ret, COIN * 10000000); BOOST_CHECK(ParseMoney("1000000.00", ret)); - BOOST_CHECK_EQUAL(ret, COIN*1000000); + BOOST_CHECK_EQUAL(ret, COIN * 1000000); BOOST_CHECK(ParseMoney("100000.00", ret)); - BOOST_CHECK_EQUAL(ret, COIN*100000); + BOOST_CHECK_EQUAL(ret, COIN * 100000); BOOST_CHECK(ParseMoney("10000.00", ret)); - BOOST_CHECK_EQUAL(ret, COIN*10000); + BOOST_CHECK_EQUAL(ret, COIN * 10000); BOOST_CHECK(ParseMoney("1000.00", ret)); - BOOST_CHECK_EQUAL(ret, COIN*1000); + BOOST_CHECK_EQUAL(ret, COIN * 1000); BOOST_CHECK(ParseMoney("100.00", ret)); - BOOST_CHECK_EQUAL(ret, COIN*100); + BOOST_CHECK_EQUAL(ret, COIN * 100); BOOST_CHECK(ParseMoney("10.00", ret)); - BOOST_CHECK_EQUAL(ret, COIN*10); + BOOST_CHECK_EQUAL(ret, COIN * 10); BOOST_CHECK(ParseMoney("1.00", ret)); BOOST_CHECK_EQUAL(ret, COIN); BOOST_CHECK(ParseMoney("1", ret)); BOOST_CHECK_EQUAL(ret, COIN); BOOST_CHECK(ParseMoney("0.1", ret)); - BOOST_CHECK_EQUAL(ret, COIN/10); + BOOST_CHECK_EQUAL(ret, COIN / 10); BOOST_CHECK(ParseMoney("0.01", ret)); - BOOST_CHECK_EQUAL(ret, COIN/100); + BOOST_CHECK_EQUAL(ret, COIN / 100); BOOST_CHECK(ParseMoney("0.001", ret)); - BOOST_CHECK_EQUAL(ret, COIN/1000); + BOOST_CHECK_EQUAL(ret, COIN / 1000); BOOST_CHECK(ParseMoney("0.0001", ret)); - BOOST_CHECK_EQUAL(ret, COIN/10000); + BOOST_CHECK_EQUAL(ret, COIN / 10000); BOOST_CHECK(ParseMoney("0.00001", ret)); - BOOST_CHECK_EQUAL(ret, COIN/100000); + BOOST_CHECK_EQUAL(ret, COIN / 100000); BOOST_CHECK(ParseMoney("0.000001", ret)); - BOOST_CHECK_EQUAL(ret, COIN/1000000); + BOOST_CHECK_EQUAL(ret, COIN / 1000000); BOOST_CHECK(ParseMoney("0.0000001", ret)); - BOOST_CHECK_EQUAL(ret, COIN/10000000); + BOOST_CHECK_EQUAL(ret, COIN / 10000000); BOOST_CHECK(ParseMoney("0.00000001", ret)); - BOOST_CHECK_EQUAL(ret, COIN/100000000); + BOOST_CHECK_EQUAL(ret, COIN / 100000000); // Attempted 63 bit overflow should fail BOOST_CHECK(!ParseMoney("92233720368.54775808", ret)); // Parsing negative amounts must fail BOOST_CHECK(!ParseMoney("-1", ret)); } -BOOST_AUTO_TEST_CASE(util_IsHex) -{ +BOOST_AUTO_TEST_CASE(util_IsHex) { BOOST_CHECK(IsHex("00")); BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF")); BOOST_CHECK(IsHex("ff")); BOOST_CHECK(IsHex("FF")); BOOST_CHECK(!IsHex("")); BOOST_CHECK(!IsHex("0")); BOOST_CHECK(!IsHex("a")); BOOST_CHECK(!IsHex("eleven")); BOOST_CHECK(!IsHex("00xx00")); BOOST_CHECK(!IsHex("0x0000")); } -BOOST_AUTO_TEST_CASE(util_seed_insecure_rand) -{ +BOOST_AUTO_TEST_CASE(util_seed_insecure_rand) { seed_insecure_rand(true); - for (int mod=2;mod<11;mod++) - { + for (int mod = 2; mod < 11; mod++) { int mask = 1; // Really rough binomal confidence approximation. - int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.); - //mask is 2^ceil(log2(mod))-1 - while(mask=(uint32_t)mod); - count += rval==0; + do { + rval = insecure_rand() & mask; + } while (rval >= (uint32_t)mod); + count += rval == 0; } - BOOST_CHECK(count<=10000/mod+err); - BOOST_CHECK(count>=10000/mod-err); + BOOST_CHECK(count <= 10000 / mod + err); + BOOST_CHECK(count >= 10000 / mod - err); } } -BOOST_AUTO_TEST_CASE(util_TimingResistantEqual) -{ +BOOST_AUTO_TEST_CASE(util_TimingResistantEqual) { BOOST_CHECK(TimingResistantEqual(std::string(""), std::string(""))); BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string(""))); BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc"))); BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa"))); BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a"))); BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc"))); BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba"))); } /* Test strprintf formatting directives. * Put a string before and after to ensure sanity of element sizes on stack. */ #define B "check_prefix" #define E "check_postfix" -BOOST_AUTO_TEST_CASE(strprintf_numbers) -{ - int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */ +BOOST_AUTO_TEST_CASE(strprintf_numbers) { + int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */ uint64_t u64t = 18446744073709551615ULL; /* unsigned 64 bit test value */ - BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B" -9223372036854775807 " E); - BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B" 18446744073709551615 " E); - BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B" ffffffffffffffff " E); + BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B + " -9223372036854775807 " E); + BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B + " 18446744073709551615 " E); + BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B " ffffffffffffffff " E); - size_t st = 12345678; /* unsigned size_t test value */ + size_t st = 12345678; /* unsigned size_t test value */ ssize_t sst = -12345678; /* signed size_t test value */ - BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B" -12345678 " E); - BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B" 12345678 " E); - BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B" bc614e " E); + BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B " -12345678 " E); + BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B " 12345678 " E); + BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B " bc614e " E); - ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */ + ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */ ptrdiff_t spt = -87654321; /* negative ptrdiff_t test value */ - BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B" -87654321 " E); - BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B" 87654321 " E); - BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B" 5397fb1 " E); + BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B " -87654321 " E); + BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B " 87654321 " E); + BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B " 5397fb1 " E); } #undef B #undef E /* Check for mingw/wine issue #3494 * Remove this test before time.ctime(0xffffffff) == 'Sun Feb 7 07:28:15 2106' */ -BOOST_AUTO_TEST_CASE(gettime) -{ +BOOST_AUTO_TEST_CASE(gettime) { BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0); } -BOOST_AUTO_TEST_CASE(test_ParseInt32) -{ +BOOST_AUTO_TEST_CASE(test_ParseInt32) { int32_t n; // Valid values BOOST_CHECK(ParseInt32("1234", NULL)); BOOST_CHECK(ParseInt32("0", &n) && n == 0); BOOST_CHECK(ParseInt32("1234", &n) && n == 1234); BOOST_CHECK(ParseInt32("01234", &n) && n == 1234); // no octal BOOST_CHECK(ParseInt32("2147483647", &n) && n == 2147483647); BOOST_CHECK(ParseInt32("-2147483648", &n) && n == -2147483648); BOOST_CHECK(ParseInt32("-1234", &n) && n == -1234); // Invalid values BOOST_CHECK(!ParseInt32("", &n)); BOOST_CHECK(!ParseInt32(" 1", &n)); // no padding inside BOOST_CHECK(!ParseInt32("1 ", &n)); BOOST_CHECK(!ParseInt32("1a", &n)); BOOST_CHECK(!ParseInt32("aap", &n)); BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex const char test_bytes[] = {'1', 0, '1'}; std::string teststr(test_bytes, sizeof(test_bytes)); BOOST_CHECK(!ParseInt32(teststr, &n)); // no embedded NULs // Overflow and underflow BOOST_CHECK(!ParseInt32("-2147483649", NULL)); BOOST_CHECK(!ParseInt32("2147483648", NULL)); BOOST_CHECK(!ParseInt32("-32482348723847471234", NULL)); BOOST_CHECK(!ParseInt32("32482348723847471234", NULL)); } -BOOST_AUTO_TEST_CASE(test_ParseInt64) -{ +BOOST_AUTO_TEST_CASE(test_ParseInt64) { int64_t n; // Valid values BOOST_CHECK(ParseInt64("1234", NULL)); BOOST_CHECK(ParseInt64("0", &n) && n == 0LL); BOOST_CHECK(ParseInt64("1234", &n) && n == 1234LL); BOOST_CHECK(ParseInt64("01234", &n) && n == 1234LL); // no octal BOOST_CHECK(ParseInt64("2147483647", &n) && n == 2147483647LL); BOOST_CHECK(ParseInt64("-2147483648", &n) && n == -2147483648LL); - BOOST_CHECK(ParseInt64("9223372036854775807", &n) && n == (int64_t)9223372036854775807); - BOOST_CHECK(ParseInt64("-9223372036854775808", &n) && n == (int64_t)-9223372036854775807-1); + BOOST_CHECK(ParseInt64("9223372036854775807", &n) && + n == (int64_t)9223372036854775807); + BOOST_CHECK(ParseInt64("-9223372036854775808", &n) && + n == (int64_t)-9223372036854775807 - 1); BOOST_CHECK(ParseInt64("-1234", &n) && n == -1234LL); // Invalid values BOOST_CHECK(!ParseInt64("", &n)); BOOST_CHECK(!ParseInt64(" 1", &n)); // no padding inside BOOST_CHECK(!ParseInt64("1 ", &n)); BOOST_CHECK(!ParseInt64("1a", &n)); BOOST_CHECK(!ParseInt64("aap", &n)); BOOST_CHECK(!ParseInt64("0x1", &n)); // no hex const char test_bytes[] = {'1', 0, '1'}; std::string teststr(test_bytes, sizeof(test_bytes)); BOOST_CHECK(!ParseInt64(teststr, &n)); // no embedded NULs // Overflow and underflow BOOST_CHECK(!ParseInt64("-9223372036854775809", NULL)); BOOST_CHECK(!ParseInt64("9223372036854775808", NULL)); BOOST_CHECK(!ParseInt64("-32482348723847471234", NULL)); BOOST_CHECK(!ParseInt64("32482348723847471234", NULL)); } -BOOST_AUTO_TEST_CASE(test_ParseUInt32) -{ +BOOST_AUTO_TEST_CASE(test_ParseUInt32) { uint32_t n; // Valid values BOOST_CHECK(ParseUInt32("1234", NULL)); BOOST_CHECK(ParseUInt32("0", &n) && n == 0); BOOST_CHECK(ParseUInt32("1234", &n) && n == 1234); BOOST_CHECK(ParseUInt32("01234", &n) && n == 1234); // no octal BOOST_CHECK(ParseUInt32("2147483647", &n) && n == 2147483647); BOOST_CHECK(ParseUInt32("2147483648", &n) && n == (uint32_t)2147483648); BOOST_CHECK(ParseUInt32("4294967295", &n) && n == (uint32_t)4294967295); // Invalid values BOOST_CHECK(!ParseUInt32("", &n)); BOOST_CHECK(!ParseUInt32(" 1", &n)); // no padding inside BOOST_CHECK(!ParseUInt32(" -1", &n)); BOOST_CHECK(!ParseUInt32("1 ", &n)); BOOST_CHECK(!ParseUInt32("1a", &n)); BOOST_CHECK(!ParseUInt32("aap", &n)); BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex const char test_bytes[] = {'1', 0, '1'}; std::string teststr(test_bytes, sizeof(test_bytes)); BOOST_CHECK(!ParseUInt32(teststr, &n)); // no embedded NULs // Overflow and underflow BOOST_CHECK(!ParseUInt32("-2147483648", &n)); BOOST_CHECK(!ParseUInt32("4294967296", &n)); BOOST_CHECK(!ParseUInt32("-1234", &n)); BOOST_CHECK(!ParseUInt32("-32482348723847471234", NULL)); BOOST_CHECK(!ParseUInt32("32482348723847471234", NULL)); } -BOOST_AUTO_TEST_CASE(test_ParseUInt64) -{ +BOOST_AUTO_TEST_CASE(test_ParseUInt64) { uint64_t n; // Valid values BOOST_CHECK(ParseUInt64("1234", NULL)); BOOST_CHECK(ParseUInt64("0", &n) && n == 0LL); BOOST_CHECK(ParseUInt64("1234", &n) && n == 1234LL); BOOST_CHECK(ParseUInt64("01234", &n) && n == 1234LL); // no octal BOOST_CHECK(ParseUInt64("2147483647", &n) && n == 2147483647LL); - BOOST_CHECK(ParseUInt64("9223372036854775807", &n) && n == 9223372036854775807ULL); - BOOST_CHECK(ParseUInt64("9223372036854775808", &n) && n == 9223372036854775808ULL); - BOOST_CHECK(ParseUInt64("18446744073709551615", &n) && n == 18446744073709551615ULL); + BOOST_CHECK(ParseUInt64("9223372036854775807", &n) && + n == 9223372036854775807ULL); + BOOST_CHECK(ParseUInt64("9223372036854775808", &n) && + n == 9223372036854775808ULL); + BOOST_CHECK(ParseUInt64("18446744073709551615", &n) && + n == 18446744073709551615ULL); // Invalid values BOOST_CHECK(!ParseUInt64("", &n)); BOOST_CHECK(!ParseUInt64(" 1", &n)); // no padding inside BOOST_CHECK(!ParseUInt64(" -1", &n)); BOOST_CHECK(!ParseUInt64("1 ", &n)); BOOST_CHECK(!ParseUInt64("1a", &n)); BOOST_CHECK(!ParseUInt64("aap", &n)); BOOST_CHECK(!ParseUInt64("0x1", &n)); // no hex const char test_bytes[] = {'1', 0, '1'}; std::string teststr(test_bytes, sizeof(test_bytes)); BOOST_CHECK(!ParseUInt64(teststr, &n)); // no embedded NULs // Overflow and underflow BOOST_CHECK(!ParseUInt64("-9223372036854775809", NULL)); BOOST_CHECK(!ParseUInt64("18446744073709551616", NULL)); BOOST_CHECK(!ParseUInt64("-32482348723847471234", NULL)); BOOST_CHECK(!ParseUInt64("-2147483648", &n)); BOOST_CHECK(!ParseUInt64("-9223372036854775808", &n)); BOOST_CHECK(!ParseUInt64("-1234", &n)); } -BOOST_AUTO_TEST_CASE(test_ParseDouble) -{ +BOOST_AUTO_TEST_CASE(test_ParseDouble) { double n; // Valid values BOOST_CHECK(ParseDouble("1234", NULL)); BOOST_CHECK(ParseDouble("0", &n) && n == 0.0); BOOST_CHECK(ParseDouble("1234", &n) && n == 1234.0); BOOST_CHECK(ParseDouble("01234", &n) && n == 1234.0); // no octal BOOST_CHECK(ParseDouble("2147483647", &n) && n == 2147483647.0); BOOST_CHECK(ParseDouble("-2147483648", &n) && n == -2147483648.0); BOOST_CHECK(ParseDouble("-1234", &n) && n == -1234.0); BOOST_CHECK(ParseDouble("1e6", &n) && n == 1e6); BOOST_CHECK(ParseDouble("-1e6", &n) && n == -1e6); // Invalid values BOOST_CHECK(!ParseDouble("", &n)); BOOST_CHECK(!ParseDouble(" 1", &n)); // no padding inside BOOST_CHECK(!ParseDouble("1 ", &n)); BOOST_CHECK(!ParseDouble("1a", &n)); BOOST_CHECK(!ParseDouble("aap", &n)); BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex const char test_bytes[] = {'1', 0, '1'}; std::string teststr(test_bytes, sizeof(test_bytes)); BOOST_CHECK(!ParseDouble(teststr, &n)); // no embedded NULs // Overflow and underflow BOOST_CHECK(!ParseDouble("-1e10000", NULL)); BOOST_CHECK(!ParseDouble("1e10000", NULL)); } -BOOST_AUTO_TEST_CASE(test_FormatParagraph) -{ +BOOST_AUTO_TEST_CASE(test_FormatParagraph) { BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), ""); BOOST_CHECK_EQUAL(FormatParagraph("test", 79, 0), "test"); BOOST_CHECK_EQUAL(FormatParagraph(" test", 79, 0), " test"); BOOST_CHECK_EQUAL(FormatParagraph("test test", 79, 0), "test test"); BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 0), "test\ntest"); BOOST_CHECK_EQUAL(FormatParagraph("testerde test", 4, 0), "testerde\ntest"); BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 4), "test\n test"); - // Make sure we don't indent a fully-new line following a too-long line ending - BOOST_CHECK_EQUAL(FormatParagraph("test test\nabc", 4, 4), "test\n test\nabc"); + // Make sure we don't indent a fully-new line following a too-long line + // ending + BOOST_CHECK_EQUAL(FormatParagraph("test test\nabc", 4, 4), + "test\n test\nabc"); - BOOST_CHECK_EQUAL(FormatParagraph("This_is_a_very_long_test_string_without_any_spaces_so_it_should_just_get_returned_as_is_despite_the_length until it gets here", 79), "This_is_a_very_long_test_string_without_any_spaces_so_it_should_just_get_returned_as_is_despite_the_length\nuntil it gets here"); + BOOST_CHECK_EQUAL( + FormatParagraph("This_is_a_very_long_test_string_without_any_spaces_so_" + "it_should_just_get_returned_as_is_despite_the_length " + "until it gets here", + 79), + "This_is_a_very_long_test_string_without_any_spaces_so_it_should_just_" + "get_returned_as_is_despite_the_length\nuntil it gets here"); // Test wrap length is exact - BOOST_CHECK_EQUAL(FormatParagraph("a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p", 79), "a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\nf g h i j k l m n o p"); - BOOST_CHECK_EQUAL(FormatParagraph("x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p", 79), "x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\nf g h i j k l m n o p"); + BOOST_CHECK_EQUAL( + FormatParagraph("a b c d e f g h i j k l m n o p q r s t u v w x y z 1 " + "2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p", + 79), + "a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 " + "a b c de\nf g h i j k l m n o p"); + BOOST_CHECK_EQUAL( + FormatParagraph("x\na b c d e f g h i j k l m n o p q r s t u v w x y " + "z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p", + 79), + "x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 " + "8 9 a b c de\nf g h i j k l m n o p"); // Indent should be included in length of lines - BOOST_CHECK_EQUAL(FormatParagraph("x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e fg h i j k", 79, 4), "x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\n f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e fg\n h i j k"); + BOOST_CHECK_EQUAL( + FormatParagraph("x\na b c d e f g h i j k l m n o p q r s t u v w x y " + "z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p q " + "r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e fg h " + "i j k", + 79, 4), + "x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 " + "8 9 a b c de\n f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 " + "5 6 7 8 9 a b c d e fg\n h i j k"); - BOOST_CHECK_EQUAL(FormatParagraph("This is a very long test string. This is a second sentence in the very long test string.", 79), "This is a very long test string. This is a second sentence in the very long\ntest string."); - BOOST_CHECK_EQUAL(FormatParagraph("This is a very long test string.\nThis is a second sentence in the very long test string. This is a third sentence in the very long test string.", 79), "This is a very long test string.\nThis is a second sentence in the very long test string. This is a third\nsentence in the very long test string."); - BOOST_CHECK_EQUAL(FormatParagraph("This is a very long test string.\n\nThis is a second sentence in the very long test string. This is a third sentence in the very long test string.", 79), "This is a very long test string.\n\nThis is a second sentence in the very long test string. This is a third\nsentence in the very long test string."); - BOOST_CHECK_EQUAL(FormatParagraph("Testing that normal newlines do not get indented.\nLike here.", 79), "Testing that normal newlines do not get indented.\nLike here."); + BOOST_CHECK_EQUAL( + FormatParagraph("This is a very long test string. This is a second " + "sentence in the very long test string.", + 79), + "This is a very long test string. This is a second sentence in the " + "very long\ntest string."); + BOOST_CHECK_EQUAL( + FormatParagraph("This is a very long test string.\nThis is a second " + "sentence in the very long test string. This is a " + "third sentence in the very long test string.", + 79), + "This is a very long test string.\nThis is a second sentence in the " + "very long test string. This is a third\nsentence in the very long " + "test string."); + BOOST_CHECK_EQUAL( + FormatParagraph("This is a very long test string.\n\nThis is a second " + "sentence in the very long test string. This is a " + "third sentence in the very long test string.", + 79), + "This is a very long test string.\n\nThis is a second sentence in the " + "very long test string. This is a third\nsentence in the very long " + "test string."); + BOOST_CHECK_EQUAL( + FormatParagraph( + "Testing that normal newlines do not get indented.\nLike here.", + 79), + "Testing that normal newlines do not get indented.\nLike here."); } -BOOST_AUTO_TEST_CASE(test_FormatSubVersion) -{ +BOOST_AUTO_TEST_CASE(test_FormatSubVersion) { std::vector comments; comments.push_back(std::string("comment1")); std::vector comments2; comments2.push_back(std::string("comment1")); - comments2.push_back(SanitizeString(std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), SAFE_CHARS_UA_COMMENT)); // Semicolon is discouraged but not forbidden by BIP-0014 - BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector()),std::string("/Test:0.9.99/")); - BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments),std::string("/Test:0.9.99(comment1)/")); - BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2),std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/")); + comments2.push_back(SanitizeString( + std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), + SAFE_CHARS_UA_COMMENT)); // Semicolon is discouraged but not forbidden + // by BIP-0014 + BOOST_CHECK_EQUAL( + FormatSubVersion("Test", 99900, std::vector()), + std::string("/Test:0.9.99/")); + BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments), + std::string("/Test:0.9.99(comment1)/")); + BOOST_CHECK_EQUAL( + FormatSubVersion("Test", 99900, comments2), + std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/")); } -BOOST_AUTO_TEST_CASE(test_ParseFixedPoint) -{ +BOOST_AUTO_TEST_CASE(test_ParseFixedPoint) { int64_t amount = 0; BOOST_CHECK(ParseFixedPoint("0", 8, &amount)); BOOST_CHECK_EQUAL(amount, 0LL); BOOST_CHECK(ParseFixedPoint("1", 8, &amount)); BOOST_CHECK_EQUAL(amount, 100000000LL); BOOST_CHECK(ParseFixedPoint("0.0", 8, &amount)); BOOST_CHECK_EQUAL(amount, 0LL); BOOST_CHECK(ParseFixedPoint("-0.1", 8, &amount)); BOOST_CHECK_EQUAL(amount, -10000000LL); BOOST_CHECK(ParseFixedPoint("1.1", 8, &amount)); BOOST_CHECK_EQUAL(amount, 110000000LL); BOOST_CHECK(ParseFixedPoint("1.10000000000000000", 8, &amount)); BOOST_CHECK_EQUAL(amount, 110000000LL); BOOST_CHECK(ParseFixedPoint("1.1e1", 8, &amount)); BOOST_CHECK_EQUAL(amount, 1100000000LL); BOOST_CHECK(ParseFixedPoint("1.1e-1", 8, &amount)); BOOST_CHECK_EQUAL(amount, 11000000LL); BOOST_CHECK(ParseFixedPoint("1000", 8, &amount)); BOOST_CHECK_EQUAL(amount, 100000000000LL); BOOST_CHECK(ParseFixedPoint("-1000", 8, &amount)); BOOST_CHECK_EQUAL(amount, -100000000000LL); BOOST_CHECK(ParseFixedPoint("0.00000001", 8, &amount)); BOOST_CHECK_EQUAL(amount, 1LL); BOOST_CHECK(ParseFixedPoint("0.0000000100000000", 8, &amount)); BOOST_CHECK_EQUAL(amount, 1LL); BOOST_CHECK(ParseFixedPoint("-0.00000001", 8, &amount)); BOOST_CHECK_EQUAL(amount, -1LL); BOOST_CHECK(ParseFixedPoint("1000000000.00000001", 8, &amount)); BOOST_CHECK_EQUAL(amount, 100000000000000001LL); BOOST_CHECK(ParseFixedPoint("9999999999.99999999", 8, &amount)); BOOST_CHECK_EQUAL(amount, 999999999999999999LL); BOOST_CHECK(ParseFixedPoint("-9999999999.99999999", 8, &amount)); BOOST_CHECK_EQUAL(amount, -999999999999999999LL); BOOST_CHECK(!ParseFixedPoint("", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("a-1000", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-a1000", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-1000a", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-01000", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("00.1", 8, &amount)); BOOST_CHECK(!ParseFixedPoint(".1", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("--0.1", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("0.000000001", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-0.000000001", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("0.00000001000000001", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-10000000000.00000000", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("10000000000.00000000", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-10000000000.00000001", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("10000000000.00000001", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-10000000000.00000009", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("10000000000.00000009", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-99999999999.99999999", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("99999909999.09999999", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("92233720368.54775807", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("92233720368.54775808", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-92233720368.54775808", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("-92233720368.54775809", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("1.1e", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("1.1e-", 8, &amount)); BOOST_CHECK(!ParseFixedPoint("1.", 8, &amount)); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index e2b5573ab..9ce06638b 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -1,338 +1,537 @@ // Copyright (c) 2014-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "chain.h" #include "versionbits.h" +#include "chain.h" +#include "chainparams.h" +#include "consensus/params.h" #include "test/test_bitcoin.h" #include "test/test_random.h" -#include "chainparams.h" #include "validation.h" -#include "consensus/params.h" #include -/* Define a virtual block time, one block per 10 minutes after Nov 14 2014, 0:55:36am */ -int32_t TestTime(int nHeight) { return 1415926536 + 600 * nHeight; } +/* Define a virtual block time, one block per 10 minutes after Nov 14 2014, + * 0:55:36am */ +int32_t TestTime(int nHeight) { + return 1415926536 + 600 * nHeight; +} static const Consensus::Params paramsDummy = Consensus::Params(); -class TestConditionChecker : public AbstractThresholdConditionChecker -{ +class TestConditionChecker : public AbstractThresholdConditionChecker { private: mutable ThresholdConditionCache cache; public: - int64_t BeginTime(const Consensus::Params& params) const { return TestTime(10000); } - int64_t EndTime(const Consensus::Params& params) const { return TestTime(20000); } - int Period(const Consensus::Params& params) const { return 1000; } - int Threshold(const Consensus::Params& params) const { return 900; } - bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const { return (pindex->nVersion & 0x100); } - - ThresholdState GetStateFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateFor(pindexPrev, paramsDummy, cache); } - int GetStateSinceHeightFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateSinceHeightFor(pindexPrev, paramsDummy, cache); } + int64_t BeginTime(const Consensus::Params ¶ms) const { + return TestTime(10000); + } + int64_t EndTime(const Consensus::Params ¶ms) const { + return TestTime(20000); + } + int Period(const Consensus::Params ¶ms) const { return 1000; } + int Threshold(const Consensus::Params ¶ms) const { return 900; } + bool Condition(const CBlockIndex *pindex, + const Consensus::Params ¶ms) const { + return (pindex->nVersion & 0x100); + } + + ThresholdState GetStateFor(const CBlockIndex *pindexPrev) const { + return AbstractThresholdConditionChecker::GetStateFor( + pindexPrev, paramsDummy, cache); + } + int GetStateSinceHeightFor(const CBlockIndex *pindexPrev) const { + return AbstractThresholdConditionChecker::GetStateSinceHeightFor( + pindexPrev, paramsDummy, cache); + } }; #define CHECKERS 6 -class VersionBitsTester -{ +class VersionBitsTester { // A fake blockchain - std::vector vpblock; + std::vector vpblock; // 6 independent checkers for the same bit. - // The first one performs all checks, the second only 50%, the third only 25%, etc... - // This is to test whether lack of cached information leads to the same results. + // The first one performs all checks, the second only 50%, the third only + // 25%, etc... + // This is to test whether lack of cached information leads to the same + // results. TestConditionChecker checker[CHECKERS]; // Test counter (to identify failures) int num; public: VersionBitsTester() : num(0) {} - VersionBitsTester& Reset() { + VersionBitsTester &Reset() { for (unsigned int i = 0; i < vpblock.size(); i++) { delete vpblock[i]; } - for (unsigned int i = 0; i < CHECKERS; i++) { + for (unsigned int i = 0; i < CHECKERS; i++) { checker[i] = TestConditionChecker(); } vpblock.clear(); return *this; } - ~VersionBitsTester() { - Reset(); - } + ~VersionBitsTester() { Reset(); } - VersionBitsTester& Mine(unsigned int height, int32_t nTime, int32_t nVersion) { + VersionBitsTester &Mine(unsigned int height, int32_t nTime, + int32_t nVersion) { while (vpblock.size() < height) { - CBlockIndex* pindex = new CBlockIndex(); + CBlockIndex *pindex = new CBlockIndex(); pindex->nHeight = vpblock.size(); pindex->pprev = vpblock.size() > 0 ? vpblock.back() : NULL; pindex->nTime = nTime; pindex->nVersion = nVersion; pindex->BuildSkip(); vpblock.push_back(pindex); } return *this; } - VersionBitsTester& TestStateSinceHeight(int height) { + VersionBitsTester &TestStateSinceHeight(int height) { for (int i = 0; i < CHECKERS; i++) { if ((insecure_rand() & ((1 << i) - 1)) == 0) { - BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(vpblock.empty() ? NULL : vpblock.back()) == height, strprintf("Test %i for StateSinceHeight", num)); + BOOST_CHECK_MESSAGE( + checker[i].GetStateSinceHeightFor( + vpblock.empty() ? NULL : vpblock.back()) == height, + strprintf("Test %i for StateSinceHeight", num)); } } num++; return *this; } - VersionBitsTester& TestDefined() { + VersionBitsTester &TestDefined() { for (int i = 0; i < CHECKERS; i++) { if ((insecure_rand() & ((1 << i) - 1)) == 0) { - BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_DEFINED, strprintf("Test %i for DEFINED", num)); + BOOST_CHECK_MESSAGE( + checker[i].GetStateFor(vpblock.empty() ? NULL + : vpblock.back()) == + THRESHOLD_DEFINED, + strprintf("Test %i for DEFINED", num)); } } num++; return *this; } - VersionBitsTester& TestStarted() { + VersionBitsTester &TestStarted() { for (int i = 0; i < CHECKERS; i++) { if ((insecure_rand() & ((1 << i) - 1)) == 0) { - BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_STARTED, strprintf("Test %i for STARTED", num)); + BOOST_CHECK_MESSAGE( + checker[i].GetStateFor(vpblock.empty() ? NULL + : vpblock.back()) == + THRESHOLD_STARTED, + strprintf("Test %i for STARTED", num)); } } num++; return *this; } - VersionBitsTester& TestLockedIn() { + VersionBitsTester &TestLockedIn() { for (int i = 0; i < CHECKERS; i++) { if ((insecure_rand() & ((1 << i) - 1)) == 0) { - BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_LOCKED_IN, strprintf("Test %i for LOCKED_IN", num)); + BOOST_CHECK_MESSAGE( + checker[i].GetStateFor(vpblock.empty() ? NULL + : vpblock.back()) == + THRESHOLD_LOCKED_IN, + strprintf("Test %i for LOCKED_IN", num)); } } num++; return *this; } - VersionBitsTester& TestActive() { + VersionBitsTester &TestActive() { for (int i = 0; i < CHECKERS; i++) { if ((insecure_rand() & ((1 << i) - 1)) == 0) { - BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_ACTIVE, strprintf("Test %i for ACTIVE", num)); + BOOST_CHECK_MESSAGE( + checker[i].GetStateFor(vpblock.empty() ? NULL + : vpblock.back()) == + THRESHOLD_ACTIVE, + strprintf("Test %i for ACTIVE", num)); } } num++; return *this; } - VersionBitsTester& TestFailed() { + VersionBitsTester &TestFailed() { for (int i = 0; i < CHECKERS; i++) { if ((insecure_rand() & ((1 << i) - 1)) == 0) { - BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_FAILED, strprintf("Test %i for FAILED", num)); + BOOST_CHECK_MESSAGE( + checker[i].GetStateFor(vpblock.empty() ? NULL + : vpblock.back()) == + THRESHOLD_FAILED, + strprintf("Test %i for FAILED", num)); } } num++; return *this; } - CBlockIndex * Tip() { return vpblock.size() ? vpblock.back() : NULL; } + CBlockIndex *Tip() { return vpblock.size() ? vpblock.back() : NULL; } }; BOOST_FIXTURE_TEST_SUITE(versionbits_tests, TestingSetup) -BOOST_AUTO_TEST_CASE(versionbits_test) -{ +BOOST_AUTO_TEST_CASE(versionbits_test) { for (int i = 0; i < 64; i++) { // DEFINED -> FAILED - VersionBitsTester().TestDefined().TestStateSinceHeight(0) - .Mine(1, TestTime(1), 0x100).TestDefined().TestStateSinceHeight(0) - .Mine(11, TestTime(11), 0x100).TestDefined().TestStateSinceHeight(0) - .Mine(989, TestTime(989), 0x100).TestDefined().TestStateSinceHeight(0) - .Mine(999, TestTime(20000), 0x100).TestDefined().TestStateSinceHeight(0) - .Mine(1000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(1000) - .Mine(1999, TestTime(30001), 0x100).TestFailed().TestStateSinceHeight(1000) - .Mine(2000, TestTime(30002), 0x100).TestFailed().TestStateSinceHeight(1000) - .Mine(2001, TestTime(30003), 0x100).TestFailed().TestStateSinceHeight(1000) - .Mine(2999, TestTime(30004), 0x100).TestFailed().TestStateSinceHeight(1000) - .Mine(3000, TestTime(30005), 0x100).TestFailed().TestStateSinceHeight(1000) - - // DEFINED -> STARTED -> FAILED - .Reset().TestDefined().TestStateSinceHeight(0) - .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0) - .Mine(1000, TestTime(10000) - 1, 0x100).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined - .Mine(2000, TestTime(10000), 0x100).TestStarted().TestStateSinceHeight(2000) // So that's what happens the next period - .Mine(2051, TestTime(10010), 0).TestStarted().TestStateSinceHeight(2000) // 51 old blocks - .Mine(2950, TestTime(10020), 0x100).TestStarted().TestStateSinceHeight(2000) // 899 new blocks - .Mine(3000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(3000) // 50 old blocks (so 899 out of the past 1000) - .Mine(4000, TestTime(20010), 0x100).TestFailed().TestStateSinceHeight(3000) - - // DEFINED -> STARTED -> FAILED while threshold reached - .Reset().TestDefined().TestStateSinceHeight(0) - .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0) - .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined - .Mine(2000, TestTime(10000), 0x101).TestStarted().TestStateSinceHeight(2000) // So that's what happens the next period - .Mine(2999, TestTime(30000), 0x100).TestStarted().TestStateSinceHeight(2000) // 999 new blocks - .Mine(3000, TestTime(30000), 0x100).TestFailed().TestStateSinceHeight(3000) // 1 new block (so 1000 out of the past 1000 are new) - .Mine(3999, TestTime(30001), 0).TestFailed().TestStateSinceHeight(3000) - .Mine(4000, TestTime(30002), 0).TestFailed().TestStateSinceHeight(3000) - .Mine(14333, TestTime(30003), 0).TestFailed().TestStateSinceHeight(3000) - .Mine(24000, TestTime(40000), 0).TestFailed().TestStateSinceHeight(3000) - - // DEFINED -> STARTED -> LOCKEDIN at the last minute -> ACTIVE - .Reset().TestDefined() - .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0) - .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined - .Mine(2000, TestTime(10000), 0x101).TestStarted().TestStateSinceHeight(2000) // So that's what happens the next period - .Mine(2050, TestTime(10010), 0x200).TestStarted().TestStateSinceHeight(2000) // 50 old blocks - .Mine(2950, TestTime(10020), 0x100).TestStarted().TestStateSinceHeight(2000) // 900 new blocks - .Mine(2999, TestTime(19999), 0x200).TestStarted().TestStateSinceHeight(2000) // 49 old blocks - .Mine(3000, TestTime(29999), 0x200).TestLockedIn().TestStateSinceHeight(3000) // 1 old block (so 900 out of the past 1000) - .Mine(3999, TestTime(30001), 0).TestLockedIn().TestStateSinceHeight(3000) - .Mine(4000, TestTime(30002), 0).TestActive().TestStateSinceHeight(4000) - .Mine(14333, TestTime(30003), 0).TestActive().TestStateSinceHeight(4000) - .Mine(24000, TestTime(40000), 0).TestActive().TestStateSinceHeight(4000) - - // DEFINED multiple periods -> STARTED multiple periods -> FAILED - .Reset().TestDefined().TestStateSinceHeight(0) - .Mine(999, TestTime(999), 0).TestDefined().TestStateSinceHeight(0) - .Mine(1000, TestTime(1000), 0).TestDefined().TestStateSinceHeight(0) - .Mine(2000, TestTime(2000), 0).TestDefined().TestStateSinceHeight(0) - .Mine(3000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) - .Mine(4000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) - .Mine(5000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) - .Mine(6000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(6000) - .Mine(7000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(6000); + VersionBitsTester() + .TestDefined() + .TestStateSinceHeight(0) + .Mine(1, TestTime(1), 0x100) + .TestDefined() + .TestStateSinceHeight(0) + .Mine(11, TestTime(11), 0x100) + .TestDefined() + .TestStateSinceHeight(0) + .Mine(989, TestTime(989), 0x100) + .TestDefined() + .TestStateSinceHeight(0) + .Mine(999, TestTime(20000), 0x100) + .TestDefined() + .TestStateSinceHeight(0) + .Mine(1000, TestTime(20000), 0x100) + .TestFailed() + .TestStateSinceHeight(1000) + .Mine(1999, TestTime(30001), 0x100) + .TestFailed() + .TestStateSinceHeight(1000) + .Mine(2000, TestTime(30002), 0x100) + .TestFailed() + .TestStateSinceHeight(1000) + .Mine(2001, TestTime(30003), 0x100) + .TestFailed() + .TestStateSinceHeight(1000) + .Mine(2999, TestTime(30004), 0x100) + .TestFailed() + .TestStateSinceHeight(1000) + .Mine(3000, TestTime(30005), 0x100) + .TestFailed() + .TestStateSinceHeight(1000) + + // DEFINED -> STARTED -> FAILED + .Reset() + .TestDefined() + .TestStateSinceHeight(0) + .Mine(1, TestTime(1), 0) + .TestDefined() + .TestStateSinceHeight(0) + .Mine(1000, TestTime(10000) - 1, 0x100) + .TestDefined() + // One second more and it would be defined + .TestStateSinceHeight(0) + .Mine(2000, TestTime(10000), 0x100) + .TestStarted() + // So that's what happens the next period + .TestStateSinceHeight(2000) + .Mine(2051, TestTime(10010), 0) + .TestStarted() + // 51 old blocks + .TestStateSinceHeight(2000) + .Mine(2950, TestTime(10020), 0x100) + .TestStarted() + // 899 new blocks + .TestStateSinceHeight(2000) + .Mine(3000, TestTime(20000), 0) + .TestFailed() + // 50 old blocks (so 899 out of the past 1000) + .TestStateSinceHeight(3000) + .Mine(4000, TestTime(20010), 0x100) + .TestFailed() + .TestStateSinceHeight(3000) + + // DEFINED -> STARTED -> FAILED while threshold reached + .Reset() + .TestDefined() + .TestStateSinceHeight(0) + .Mine(1, TestTime(1), 0) + .TestDefined() + .TestStateSinceHeight(0) + .Mine(1000, TestTime(10000) - 1, 0x101) + .TestDefined() + // One second more and it would be defined + .TestStateSinceHeight(0) + .Mine(2000, TestTime(10000), 0x101) + .TestStarted() + // So that's what happens the next period + .TestStateSinceHeight(2000) + .Mine(2999, TestTime(30000), 0x100) + .TestStarted() + // 999 new blocks + .TestStateSinceHeight(2000) + .Mine(3000, TestTime(30000), 0x100) + .TestFailed() + // 1 new block (so 1000 out of the past 1000 are new) + .TestStateSinceHeight(3000) + .Mine(3999, TestTime(30001), 0) + .TestFailed() + .TestStateSinceHeight(3000) + .Mine(4000, TestTime(30002), 0) + .TestFailed() + .TestStateSinceHeight(3000) + .Mine(14333, TestTime(30003), 0) + .TestFailed() + .TestStateSinceHeight(3000) + .Mine(24000, TestTime(40000), 0) + .TestFailed() + .TestStateSinceHeight(3000) + + // DEFINED -> STARTED -> LOCKEDIN at the last minute -> ACTIVE + .Reset() + .TestDefined() + .Mine(1, TestTime(1), 0) + .TestDefined() + .TestStateSinceHeight(0) + .Mine(1000, TestTime(10000) - 1, 0x101) + .TestDefined() + // One second more and it would be defined + .TestStateSinceHeight(0) + .Mine(2000, TestTime(10000), 0x101) + .TestStarted() + // So that's what happens the next period + .TestStateSinceHeight(2000) + .Mine(2050, TestTime(10010), 0x200) + .TestStarted() + // 50 old blocks + .TestStateSinceHeight(2000) + .Mine(2950, TestTime(10020), 0x100) + .TestStarted() + // 900 new blocks + .TestStateSinceHeight(2000) + .Mine(2999, TestTime(19999), 0x200) + .TestStarted() + // 49 old blocks + .TestStateSinceHeight(2000) + .Mine(3000, TestTime(29999), 0x200) + .TestLockedIn() + // 1 old block (so 900 out of the past 1000) + .TestStateSinceHeight(3000) + .Mine(3999, TestTime(30001), 0) + .TestLockedIn() + .TestStateSinceHeight(3000) + .Mine(4000, TestTime(30002), 0) + .TestActive() + .TestStateSinceHeight(4000) + .Mine(14333, TestTime(30003), 0) + .TestActive() + .TestStateSinceHeight(4000) + .Mine(24000, TestTime(40000), 0) + .TestActive() + .TestStateSinceHeight(4000) + + // DEFINED multiple periods -> STARTED multiple periods -> FAILED + .Reset() + .TestDefined() + .TestStateSinceHeight(0) + .Mine(999, TestTime(999), 0) + .TestDefined() + .TestStateSinceHeight(0) + .Mine(1000, TestTime(1000), 0) + .TestDefined() + .TestStateSinceHeight(0) + .Mine(2000, TestTime(2000), 0) + .TestDefined() + .TestStateSinceHeight(0) + .Mine(3000, TestTime(10000), 0) + .TestStarted() + .TestStateSinceHeight(3000) + .Mine(4000, TestTime(10000), 0) + .TestStarted() + .TestStateSinceHeight(3000) + .Mine(5000, TestTime(10000), 0) + .TestStarted() + .TestStateSinceHeight(3000) + .Mine(6000, TestTime(20000), 0) + .TestFailed() + .TestStateSinceHeight(6000) + .Mine(7000, TestTime(20000), 0x100) + .TestFailed() + .TestStateSinceHeight(6000); } // Sanity checks of version bit deployments - const Consensus::Params &mainnetParams = Params(CBaseChainParams::MAIN).GetConsensus(); - for (int i=0; i<(int) Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) { - uint32_t bitmask = VersionBitsMask(mainnetParams, (Consensus::DeploymentPos)i); + const Consensus::Params &mainnetParams = + Params(CBaseChainParams::MAIN).GetConsensus(); + for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) { + uint32_t bitmask = + VersionBitsMask(mainnetParams, (Consensus::DeploymentPos)i); // Make sure that no deployment tries to set an invalid bit. BOOST_CHECK_EQUAL(bitmask & ~(uint32_t)VERSIONBITS_TOP_MASK, bitmask); // Verify that the deployment windows of different deployment using the - // same bit are disjoint. - // This test may need modification at such time as a new deployment - // is proposed that reuses the bit of an activated soft fork, before the - // end time of that soft fork. (Alternatively, the end time of that - // activated soft fork could be later changed to be earlier to avoid - // overlap.) - for (int j=i+1; j<(int) Consensus::MAX_VERSION_BITS_DEPLOYMENTS; j++) { - if (VersionBitsMask(mainnetParams, (Consensus::DeploymentPos)j) == bitmask) { - BOOST_CHECK(mainnetParams.vDeployments[j].nStartTime > mainnetParams.vDeployments[i].nTimeout || - mainnetParams.vDeployments[i].nStartTime > mainnetParams.vDeployments[j].nTimeout); + // same bit are disjoint. This test may need modification at such time + // as a new deployment is proposed that reuses the bit of an activated + // soft fork, before the end time of that soft fork. (Alternatively, + // the end time of that activated soft fork could be later changed to be + // earlier to avoid overlap.) + for (int j = i + 1; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; + j++) { + if (VersionBitsMask(mainnetParams, (Consensus::DeploymentPos)j) == + bitmask) { + BOOST_CHECK(mainnetParams.vDeployments[j].nStartTime > + mainnetParams.vDeployments[i].nTimeout || + mainnetParams.vDeployments[i].nStartTime > + mainnetParams.vDeployments[j].nTimeout); } } } } -BOOST_AUTO_TEST_CASE(versionbits_computeblockversion) -{ +BOOST_AUTO_TEST_CASE(versionbits_computeblockversion) { // Check that ComputeBlockVersion will set the appropriate bit correctly // on mainnet. - const Consensus::Params &mainnetParams = Params(CBaseChainParams::MAIN).GetConsensus(); + const Consensus::Params &mainnetParams = + Params(CBaseChainParams::MAIN).GetConsensus(); // Use the TESTDUMMY deployment for testing purposes. - int64_t bit = mainnetParams.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit; - int64_t nStartTime = mainnetParams.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime; - int64_t nTimeout = mainnetParams.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout; + int64_t bit = + mainnetParams.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit; + int64_t nStartTime = + mainnetParams.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime; + int64_t nTimeout = + mainnetParams.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout; assert(nStartTime < nTimeout); // In the first chain, test that the bit is set by CBV until it has failed. // In the second chain, test the bit is set by CBV while STARTED and // LOCKED-IN, and then no longer set while ACTIVE. VersionBitsTester firstChain, secondChain; // Start generating blocks before nStartTime int64_t nTime = nStartTime - 1; // Before MedianTimePast of the chain has crossed nStartTime, the bit // should not be set. CBlockIndex *lastBlock = NULL; - lastBlock = firstChain.Mine(2016, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip(); - BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, mainnetParams) & (1< 0) { - lastBlock = firstChain.Mine(nHeight+1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip(); - BOOST_CHECK((ComputeBlockVersion(lastBlock, mainnetParams) & (1<