Skip to content

Commit c82e359

Browse files
MarcoFalkeFuzzbawls
authored andcommitted
test: Make bloom tests deterministic
1 parent 7b33223 commit c82e359

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

src/random.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,11 @@ void GetRandBytes(unsigned char* buf, int num) noexcept { ProcRand(buf, num, RNG
660660
void GetStrongRandBytes(unsigned char* buf, int num) noexcept { ProcRand(buf, num, RNGLevel::SLOW); }
661661
void RandAddSeedSleep() { ProcRand(nullptr, 0, RNGLevel::SLEEP); }
662662

663+
bool g_mock_deterministic_tests{false};
664+
663665
uint64_t GetRand(uint64_t nMax) noexcept
664666
{
665-
return FastRandomContext().randrange(nMax);
667+
return FastRandomContext(g_mock_deterministic_tests).randrange(nMax);
666668
}
667669

668670
int GetRandInt(int nMax) noexcept

src/test/bloom_tests.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ static std::vector<unsigned char> RandomData()
463463

464464
BOOST_AUTO_TEST_CASE(rolling_bloom)
465465
{
466+
SeedInsecureRand(/* deterministic */ true);
467+
g_mock_deterministic_tests = true;
468+
466469
// last-100-entry, 1% false positive:
467470
CRollingBloomFilter rb1(100, 0.01);
468471

@@ -487,12 +490,8 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
487490
if (rb1.contains(RandomData()))
488491
++nHits;
489492
}
490-
// Run test_bitcoin with --log_level=message to see BOOST_TEST_MESSAGEs:
491-
BOOST_TEST_MESSAGE("RollingBloomFilter got " << nHits << " false positives (~100 expected)");
492-
493-
// Insanely unlikely to get a fp count outside this range:
494-
BOOST_CHECK(nHits > 25);
495-
BOOST_CHECK(nHits < 175);
493+
// Expect about 100 hits
494+
BOOST_CHECK_EQUAL(nHits, 75);
496495

497496
BOOST_CHECK(rb1.contains(data[DATASIZE - 1]));
498497
rb1.reset();
@@ -516,10 +515,8 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
516515
if (rb1.contains(data[i]))
517516
++nHits;
518517
}
519-
// Expect about 5 false positives, more than 100 means
520-
// something is definitely broken.
521-
BOOST_TEST_MESSAGE("RollingBloomFilter got " << nHits << " false positives (~5 expected)");
522-
BOOST_CHECK(nHits < 100);
518+
// Expect about 5 false positives
519+
BOOST_CHECK_EQUAL(nHits, 6);
523520

524521
// last-1000-entry, 0.01% false positive:
525522
CRollingBloomFilter rb2(1000, 0.001);
@@ -530,6 +527,7 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
530527
for (int i = 0; i < DATASIZE; i++) {
531528
BOOST_CHECK(rb2.contains(data[i]));
532529
}
530+
g_mock_deterministic_tests = false;
533531
}
534532

535533
BOOST_AUTO_TEST_SUITE_END()

src/test/random_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ BOOST_AUTO_TEST_CASE(osrandom_tests)
2121
BOOST_AUTO_TEST_CASE(fastrandom_tests)
2222
{
2323
// Check that deterministic FastRandomContexts are deterministic
24+
g_mock_deterministic_tests = true;
2425
FastRandomContext ctx1(true);
2526
FastRandomContext ctx2(true);
2627

28+
for (int i = 10; i > 0; --i) {
29+
BOOST_CHECK_EQUAL(GetRand(std::numeric_limits<uint64_t>::max()), uint64_t{10393729187455219830U});
30+
BOOST_CHECK_EQUAL(GetRandInt(std::numeric_limits<int>::max()), int{769702006});
31+
}
2732
BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
2833
BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
2934
BOOST_CHECK_EQUAL(ctx1.rand64(), ctx2.rand64());
@@ -38,6 +43,11 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
3843
BOOST_CHECK(ctx1.randbytes(50) == ctx2.randbytes(50));
3944

4045
// Check that a nondeterministic ones are not
46+
g_mock_deterministic_tests = false;
47+
for (int i = 10; i > 0; --i) {
48+
BOOST_CHECK(GetRand(std::numeric_limits<uint64_t>::max()) != uint64_t{10393729187455219830U});
49+
BOOST_CHECK(GetRandInt(std::numeric_limits<int>::max()) != int{769702006});
50+
}
4151
{
4252
FastRandomContext ctx3, ctx4;
4353
BOOST_CHECK(ctx3.rand64() != ctx4.rand64()); // extremely unlikely to be equal

src/test/test_pivx.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
extern FastRandomContext insecure_rand_ctx;
1515

16+
/**
17+
* Flag to make GetRand in random.h return the same number
18+
*/
19+
extern bool g_mock_deterministic_tests;
20+
1621
static inline void SeedInsecureRand(bool deterministic = false)
1722
{
1823
insecure_rand_ctx = FastRandomContext(deterministic);

0 commit comments

Comments
 (0)