Skip to content

Commit 3dc527f

Browse files
ryanofskyMarcoFalke
authored andcommitted
test: refactor: Give unit test functions access to test state
Add unit test subclasses as needed so unit test functions that need to access members like m_rng can reference it directly.
1 parent fab023e commit 3dc527f

16 files changed

+145
-81
lines changed

src/test/bip324_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace {
2222

23+
struct BIP324Test : BasicTestingSetup {
2324
void TestBIP324PacketVector(
2425
uint32_t in_idx,
2526
const std::string& in_priv_ours_hex,
@@ -155,10 +156,11 @@ void TestBIP324PacketVector(
155156
}
156157
}
157158
}
159+
}; // struct BIP324Test
158160

159161
} // namespace
160162

161-
BOOST_FIXTURE_TEST_SUITE(bip324_tests, BasicTestingSetup)
163+
BOOST_FIXTURE_TEST_SUITE(bip324_tests, BIP324Test)
162164

163165
BOOST_AUTO_TEST_CASE(packet_test_vectors) {
164166
// BIP324 key derivation uses network magic in the HKDF process. We use mainnet params here

src/test/bloom_tests.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222

2323
#include <boost/test/unit_test.hpp>
2424

25-
BOOST_FIXTURE_TEST_SUITE(bloom_tests, BasicTestingSetup)
25+
namespace bloom_tests {
26+
struct BloomTest : public BasicTestingSetup {
27+
std::vector<unsigned char> RandomData();
28+
};
29+
} // namespace bloom_tests
30+
31+
BOOST_FIXTURE_TEST_SUITE(bloom_tests, BloomTest)
2632

2733
BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize)
2834
{
@@ -455,7 +461,7 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none)
455461
BOOST_CHECK(!filter.contains(COutPoint(Txid::FromHex("02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041").value(), 0)));
456462
}
457463

458-
static std::vector<unsigned char> RandomData()
464+
std::vector<unsigned char> BloomTest::RandomData()
459465
{
460466
uint256 r = InsecureRand256();
461467
return std::vector<unsigned char>(r.begin(), r.end());

src/test/checkqueue_tests.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ struct NoLockLoggingTestingSetup : public TestingSetup {
3434
#endif
3535
};
3636

37-
BOOST_FIXTURE_TEST_SUITE(checkqueue_tests, NoLockLoggingTestingSetup)
37+
struct CheckQueueTest : NoLockLoggingTestingSetup {
38+
void Correct_Queue_range(std::vector<size_t> range);
39+
};
3840

3941
static const unsigned int QUEUE_BATCH_SIZE = 128;
4042
static const int SCRIPT_CHECK_THREADS = 3;
@@ -156,7 +158,7 @@ typedef CCheckQueue<FrozenCleanupCheck> FrozenCleanup_Queue;
156158
/** This test case checks that the CCheckQueue works properly
157159
* with each specified size_t Checks pushed.
158160
*/
159-
static void Correct_Queue_range(std::vector<size_t> range)
161+
void CheckQueueTest::Correct_Queue_range(std::vector<size_t> range)
160162
{
161163
auto small_queue = std::make_unique<Correct_Queue>(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS);
162164
// Make vChecks here to save on malloc (this test can be slow...)
@@ -177,6 +179,8 @@ static void Correct_Queue_range(std::vector<size_t> range)
177179
}
178180
}
179181

182+
BOOST_FIXTURE_TEST_SUITE(checkqueue_tests, CheckQueueTest)
183+
180184
/** Test that 0 checks is correct
181185
*/
182186
BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Zero)

src/test/coins_tests.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
105105

106106
static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;
107107

108+
struct CacheTest : BasicTestingSetup {
108109
// This is a large randomized insert/remove simulation test on a variable-size
109110
// stack of caches on top of CCoinsViewTest.
110111
//
@@ -277,9 +278,10 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
277278
BOOST_CHECK(uncached_an_entry);
278279
BOOST_CHECK(flushed_without_erase);
279280
}
281+
}; // struct CacheTest
280282

281283
// Run the above simulation for multiple base types.
282-
BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
284+
BOOST_FIXTURE_TEST_CASE(coins_cache_simulation_test, CacheTest)
283285
{
284286
CCoinsViewTest base;
285287
SimulationTest(&base, false);
@@ -288,6 +290,7 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
288290
SimulationTest(&db_base, true);
289291
}
290292

293+
struct UpdateTest : BasicTestingSetup {
291294
// Store of all necessary tx and undo data for next test
292295
typedef std::map<COutPoint, std::tuple<CTransaction,CTxUndo,Coin>> UtxoData;
293296
UtxoData utxoData;
@@ -302,14 +305,15 @@ UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
302305
assert(utxoDataIt != utxoData.end());
303306
return utxoDataIt;
304307
}
308+
}; // struct UpdateTest
305309

306310

307311
// This test is similar to the previous test
308312
// except the emphasis is on testing the functionality of UpdateCoins
309313
// random txs are created and UpdateCoins is used to update the cache stack
310314
// In particular it is tested that spending a duplicate coinbase tx
311315
// has the expected effect (the other duplicate is overwritten at all cache levels)
312-
BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
316+
BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
313317
{
314318
SeedRandomForTest(SeedRand::ZEROS);
315319

@@ -888,6 +892,7 @@ BOOST_AUTO_TEST_CASE(ccoins_write)
888892
}
889893

890894

895+
struct FlushTest : BasicTestingSetup {
891896
Coin MakeCoin()
892897
{
893898
Coin coin;
@@ -919,7 +924,7 @@ void TestFlushBehavior(
919924
size_t cache_usage;
920925
size_t cache_size;
921926

922-
auto flush_all = [&all_caches](bool erase) {
927+
auto flush_all = [this, &all_caches](bool erase) {
923928
// Flush in reverse order to ensure that flushes happen from children up.
924929
for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
925930
auto& cache = *i;
@@ -1074,8 +1079,9 @@ void TestFlushBehavior(
10741079
BOOST_CHECK(!all_caches[0]->HaveCoinInCache(outp));
10751080
BOOST_CHECK(!base.HaveCoin(outp));
10761081
}
1082+
}; // struct FlushTest
10771083

1078-
BOOST_AUTO_TEST_CASE(ccoins_flush_behavior)
1084+
BOOST_FIXTURE_TEST_CASE(ccoins_flush_behavior, FlushTest)
10791085
{
10801086
// Create two in-memory caches atop a leveldb view.
10811087
CCoinsViewDB base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};

src/test/crypto_tests.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525

2626
#include <boost/test/unit_test.hpp>
2727

28-
BOOST_FIXTURE_TEST_SUITE(crypto_tests, BasicTestingSetup)
28+
namespace crypto_tests {
29+
struct CryptoTest : BasicTestingSetup {
2930

3031
template<typename Hasher, typename In, typename Out>
31-
static void TestVector(const Hasher &h, const In &in, const Out &out) {
32+
void TestVector(const Hasher &h, const In &in, const Out &out) {
3233
Out hash;
3334
BOOST_CHECK(out.size() == h.OUTPUT_SIZE);
3435
hash.resize(out.size());
@@ -56,22 +57,22 @@ static void TestVector(const Hasher &h, const In &in, const Out &out) {
5657
}
5758
}
5859

59-
static void TestSHA1(const std::string &in, const std::string &hexout) { TestVector(CSHA1(), in, ParseHex(hexout));}
60-
static void TestSHA256(const std::string &in, const std::string &hexout) { TestVector(CSHA256(), in, ParseHex(hexout));}
61-
static void TestSHA512(const std::string &in, const std::string &hexout) { TestVector(CSHA512(), in, ParseHex(hexout));}
62-
static void TestRIPEMD160(const std::string &in, const std::string &hexout) { TestVector(CRIPEMD160(), in, ParseHex(hexout));}
60+
void TestSHA1(const std::string &in, const std::string &hexout) { TestVector(CSHA1(), in, ParseHex(hexout));}
61+
void TestSHA256(const std::string &in, const std::string &hexout) { TestVector(CSHA256(), in, ParseHex(hexout));}
62+
void TestSHA512(const std::string &in, const std::string &hexout) { TestVector(CSHA512(), in, ParseHex(hexout));}
63+
void TestRIPEMD160(const std::string &in, const std::string &hexout) { TestVector(CRIPEMD160(), in, ParseHex(hexout));}
6364

64-
static void TestHMACSHA256(const std::string &hexkey, const std::string &hexin, const std::string &hexout) {
65+
void TestHMACSHA256(const std::string &hexkey, const std::string &hexin, const std::string &hexout) {
6566
std::vector<unsigned char> key = ParseHex(hexkey);
6667
TestVector(CHMAC_SHA256(key.data(), key.size()), ParseHex(hexin), ParseHex(hexout));
6768
}
6869

69-
static void TestHMACSHA512(const std::string &hexkey, const std::string &hexin, const std::string &hexout) {
70+
void TestHMACSHA512(const std::string &hexkey, const std::string &hexin, const std::string &hexout) {
7071
std::vector<unsigned char> key = ParseHex(hexkey);
7172
TestVector(CHMAC_SHA512(key.data(), key.size()), ParseHex(hexin), ParseHex(hexout));
7273
}
7374

74-
static void TestAES256(const std::string &hexkey, const std::string &hexin, const std::string &hexout)
75+
void TestAES256(const std::string &hexkey, const std::string &hexin, const std::string &hexout)
7576
{
7677
std::vector<unsigned char> key = ParseHex(hexkey);
7778
std::vector<unsigned char> in = ParseHex(hexin);
@@ -90,7 +91,7 @@ static void TestAES256(const std::string &hexkey, const std::string &hexin, cons
9091
BOOST_CHECK(buf == in);
9192
}
9293

93-
static void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, bool pad, const std::string &hexin, const std::string &hexout)
94+
void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, bool pad, const std::string &hexin, const std::string &hexout)
9495
{
9596
std::vector<unsigned char> key = ParseHex(hexkey);
9697
std::vector<unsigned char> iv = ParseHex(hexiv);
@@ -131,7 +132,7 @@ static void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, b
131132
}
132133
}
133134

134-
static void TestChaCha20(const std::string &hex_message, const std::string &hexkey, ChaCha20::Nonce96 nonce, uint32_t seek, const std::string& hexout)
135+
void TestChaCha20(const std::string &hex_message, const std::string &hexkey, ChaCha20::Nonce96 nonce, uint32_t seek, const std::string& hexout)
135136
{
136137
auto key = ParseHex<std::byte>(hexkey);
137138
assert(key.size() == 32);
@@ -182,7 +183,7 @@ static void TestChaCha20(const std::string &hex_message, const std::string &hexk
182183
}
183184
}
184185

185-
static void TestFSChaCha20(const std::string& hex_plaintext, const std::string& hexkey, uint32_t rekey_interval, const std::string& ciphertext_after_rotation)
186+
void TestFSChaCha20(const std::string& hex_plaintext, const std::string& hexkey, uint32_t rekey_interval, const std::string& ciphertext_after_rotation)
186187
{
187188
auto key = ParseHex<std::byte>(hexkey);
188189
BOOST_CHECK_EQUAL(FSChaCha20::KEYLEN, key.size());
@@ -222,7 +223,7 @@ static void TestFSChaCha20(const std::string& hex_plaintext, const std::string&
222223
BOOST_CHECK_EQUAL(HexStr(fsc20_output), ciphertext_after_rotation);
223224
}
224225

225-
static void TestPoly1305(const std::string &hexmessage, const std::string &hexkey, const std::string& hextag)
226+
void TestPoly1305(const std::string &hexmessage, const std::string &hexkey, const std::string& hextag)
226227
{
227228
auto key = ParseHex<std::byte>(hexkey);
228229
auto m = ParseHex<std::byte>(hexmessage);
@@ -247,7 +248,7 @@ static void TestPoly1305(const std::string &hexmessage, const std::string &hexke
247248
}
248249
}
249250

250-
static void TestChaCha20Poly1305(const std::string& plain_hex, const std::string& aad_hex, const std::string& key_hex, ChaCha20::Nonce96 nonce, const std::string& cipher_hex)
251+
void TestChaCha20Poly1305(const std::string& plain_hex, const std::string& aad_hex, const std::string& key_hex, ChaCha20::Nonce96 nonce, const std::string& cipher_hex)
251252
{
252253
auto plain = ParseHex<std::byte>(plain_hex);
253254
auto aad = ParseHex<std::byte>(aad_hex);
@@ -288,7 +289,7 @@ static void TestChaCha20Poly1305(const std::string& plain_hex, const std::string
288289
}
289290
}
290291

291-
static void TestFSChaCha20Poly1305(const std::string& plain_hex, const std::string& aad_hex, const std::string& key_hex, uint64_t msg_idx, const std::string& cipher_hex)
292+
void TestFSChaCha20Poly1305(const std::string& plain_hex, const std::string& aad_hex, const std::string& key_hex, uint64_t msg_idx, const std::string& cipher_hex)
292293
{
293294
auto plain = ParseHex<std::byte>(plain_hex);
294295
auto aad = ParseHex<std::byte>(aad_hex);
@@ -334,7 +335,7 @@ static void TestFSChaCha20Poly1305(const std::string& plain_hex, const std::stri
334335
}
335336
}
336337

337-
static void TestHKDF_SHA256_32(const std::string &ikm_hex, const std::string &salt_hex, const std::string &info_hex, const std::string &okm_check_hex) {
338+
void TestHKDF_SHA256_32(const std::string &ikm_hex, const std::string &salt_hex, const std::string &info_hex, const std::string &okm_check_hex) {
338339
std::vector<unsigned char> initial_key_material = ParseHex(ikm_hex);
339340
std::vector<unsigned char> salt = ParseHex(salt_hex);
340341
std::vector<unsigned char> info = ParseHex(info_hex);
@@ -350,6 +351,10 @@ static void TestHKDF_SHA256_32(const std::string &ikm_hex, const std::string &sa
350351
BOOST_CHECK(HexStr(out) == okm_check_hex);
351352
}
352353

354+
void TestSHA3_256(const std::string& input, const std::string& output);
355+
}; // struct CryptoTests
356+
} // namespace crypto_tests
357+
353358
static std::string LongTestString()
354359
{
355360
std::string ret;
@@ -365,6 +370,8 @@ static std::string LongTestString()
365370

366371
const std::string test1 = LongTestString();
367372

373+
BOOST_FIXTURE_TEST_SUITE(crypto_tests, CryptoTest)
374+
368375
BOOST_AUTO_TEST_CASE(ripemd160_testvectors) {
369376
TestRIPEMD160("", "9c1185a5c5e9fc54612808977ee8f548b2258d31");
370377
TestRIPEMD160("abc", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc");
@@ -1076,7 +1083,7 @@ BOOST_AUTO_TEST_CASE(sha256d64)
10761083
}
10771084
}
10781085

1079-
static void TestSHA3_256(const std::string& input, const std::string& output)
1086+
void CryptoTest::TestSHA3_256(const std::string& input, const std::string& output)
10801087
{
10811088
const auto in_bytes = ParseHex(input);
10821089
const auto out_bytes = ParseHex(output);

src/test/cuckoocache_tests.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* using BOOST_CHECK_CLOSE to fail.
3030
*
3131
*/
32-
BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
32+
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup);
3333

3434
/* Test that no values not inserted into the cache are read out of it.
3535
*
@@ -49,11 +49,12 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
4949
}
5050
};
5151

52+
struct HitRateTest : BasicTestingSetup {
5253
/** This helper returns the hit rate when megabytes*load worth of entries are
5354
* inserted into a megabytes sized cache
5455
*/
5556
template <typename Cache>
56-
static double test_cache(size_t megabytes, double load)
57+
double test_cache(size_t megabytes, double load)
5758
{
5859
SeedRandomForTest(SeedRand::ZEROS);
5960
std::vector<uint256> hashes;
@@ -104,9 +105,10 @@ static double normalize_hit_rate(double hits, double load)
104105
{
105106
return hits * std::max(load, 1.0);
106107
}
108+
}; // struct HitRateTest
107109

108110
/** Check the hit rate on loads ranging from 0.1 to 1.6 */
109-
BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)
111+
BOOST_FIXTURE_TEST_CASE(cuckoocache_hit_rate_ok, HitRateTest)
110112
{
111113
/** Arbitrarily selected Hit Rate threshold that happens to work for this test
112114
* as a lower bound on performance.
@@ -120,10 +122,11 @@ BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)
120122
}
121123

122124

125+
struct EraseTest : BasicTestingSetup {
123126
/** This helper checks that erased elements are preferentially inserted onto and
124127
* that the hit rate of "fresher" keys is reasonable*/
125128
template <typename Cache>
126-
static void test_cache_erase(size_t megabytes)
129+
void test_cache_erase(size_t megabytes)
127130
{
128131
double load = 1;
129132
SeedRandomForTest(SeedRand::ZEROS);
@@ -178,15 +181,17 @@ static void test_cache_erase(size_t megabytes)
178181
// erased elements.
179182
BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
180183
}
184+
}; // struct EraseTest
181185

182-
BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok)
186+
BOOST_FIXTURE_TEST_CASE(cuckoocache_erase_ok, EraseTest)
183187
{
184188
size_t megabytes = 4;
185189
test_cache_erase<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
186190
}
187191

192+
struct EraseParallelTest : BasicTestingSetup {
188193
template <typename Cache>
189-
static void test_cache_erase_parallel(size_t megabytes)
194+
void test_cache_erase_parallel(size_t megabytes)
190195
{
191196
double load = 1;
192197
SeedRandomForTest(SeedRand::ZEROS);
@@ -268,15 +273,17 @@ static void test_cache_erase_parallel(size_t megabytes)
268273
// erased elements.
269274
BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
270275
}
271-
BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok)
276+
}; // struct EraseParallelTest
277+
BOOST_FIXTURE_TEST_CASE(cuckoocache_erase_parallel_ok, EraseParallelTest)
272278
{
273279
size_t megabytes = 4;
274280
test_cache_erase_parallel<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
275281
}
276282

277283

284+
struct GenerationsTest : BasicTestingSetup {
278285
template <typename Cache>
279-
static void test_cache_generations()
286+
void test_cache_generations()
280287
{
281288
// This test checks that for a simulation of network activity, the fresh hit
282289
// rate is never below 99%, and the number of times that it is worse than
@@ -365,7 +372,8 @@ static void test_cache_generations()
365372
// max_rate_less_than_tight_hit_rate of the time
366373
BOOST_CHECK(double(out_of_tight_tolerance) / double(total) < max_rate_less_than_tight_hit_rate);
367374
}
368-
BOOST_AUTO_TEST_CASE(cuckoocache_generations)
375+
}; // struct GenerationsTest
376+
BOOST_FIXTURE_TEST_CASE(cuckoocache_generations, GenerationsTest)
369377
{
370378
test_cache_generations<CuckooCache::cache<uint256, SignatureCacheHasher>>();
371379
}

0 commit comments

Comments
 (0)