container: root: test: benchmark/unit: add bitcoin plugin support

- Adds test cases and benchmark for bitcoin's RNG via `dfi`'s `Random`
  * Currently only supports one of bitcoin's RNG's (`FastRandomContext`)
This commit is contained in:
2025-12-17 14:16:48 -08:00
parent 0bf4c359b1
commit c3d128ea62
3 changed files with 70 additions and 3 deletions

View File

@@ -128,6 +128,30 @@ BENCHMARK_F(RandomLibsodium, generate)(::benchmark::State& state) // NOLINT
random.generate();
}
}
//
// Bitcoin
//
#ifdef __DFI_PLUGIN_BITCOIN__
//! \brief Bitcoin Random fixture w/ real implementation
//! \since docker-finance 1.1.0
struct RandomBitcoin : public ::benchmark::Fixture,
public tests::RandomBitcoin_Impl
{
void SetUp(const ::benchmark::State& state) {}
void TearDown(const ::benchmark::State& state) {}
};
BENCHMARK_F(RandomBitcoin, generate)(::benchmark::State& state) // NOLINT
{
for (auto st : state)
{
random.generate();
}
}
#endif
} // namespace benchmarks
} // namespace tests
} // namespace dfi

View File

@@ -90,6 +90,18 @@ struct RandomLibsodium_Impl
using Random = ::dfi::crypto::libsodium::Random;
Random random;
};
#ifdef __DFI_PLUGIN_BITCOIN__
//! \brief Bitcoin random implementation fixture
//! \since docker-finance 1.1.0
struct RandomBitcoin_Impl
{
protected:
using Random = ::dfi::crypto::bitcoin::Random;
Random random;
};
#endif
} // namespace tests
} // namespace dfi

View File

@@ -70,6 +70,11 @@ TEST_F(RandomInterface, generate_uint32_t)
ASSERT_EQ(random.generate<uint32_t>(), std::numeric_limits<uint32_t>::max());
}
TEST_F(RandomInterface, generate_uint64_t)
{
ASSERT_EQ(random.generate<uint64_t>(), std::numeric_limits<uint64_t>::max());
}
TEST_F(RandomInterface, generate_int16_t)
{
ASSERT_EQ(random.generate<int16_t>(), std::numeric_limits<int16_t>::max());
@@ -96,9 +101,13 @@ struct RandomFixture : public ::testing::Test, protected t_impl
auto two = t_impl::random.template generate<t_random>();
static_assert(std::is_same_v<decltype(two), t_random>);
// NOTE: t_random limits are implementation-specific
ASSERT_NEAR(0, one, std::numeric_limits<t_random>::max());
ASSERT_NEAR(0, two, std::numeric_limits<t_random>::max());
// TODO(unassigned): gtest implicit conversion of uint64_t with ASSERT_NEAR
if constexpr (!std::is_same_v<t_random, uint64_t>)
{
// NOTE: t_random limits are implementation-specific
ASSERT_NEAR(0, one, std::numeric_limits<t_random>::max());
ASSERT_NEAR(0, two, std::numeric_limits<t_random>::max());
}
// Exceedingly rare (tests the obvious, but is not an accurate entropy test)
// If fails, re-run tests to confirm
@@ -176,6 +185,28 @@ TEST_F(RandomLibsodium, generate_uint32_t)
ASSERT_NO_THROW(generate<uint32_t>());
}
#ifdef __DFI_PLUGIN_BITCOIN__
//
// Bitcoin
//
//! \brief Bitcoin random number fixture
//! \since docker-finance 1.1.0
struct RandomBitcoin : public RandomFixture<tests::RandomBitcoin_Impl>
{
};
TEST_F(RandomBitcoin, generate_uint32_t)
{
ASSERT_NO_THROW(generate<uint32_t>());
}
TEST_F(RandomBitcoin, generate_uint64_t)
{
ASSERT_NO_THROW(generate<uint64_t>());
}
#endif
} // namespace unit
} // namespace tests
} // namespace dfi