LCOV - code coverage report
Current view: top level - src/test/util - random.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 4 12 33.3 %
Date: 2023-11-10 23:46:46 Functions: 1 4 25.0 %
Branches: 1 2 50.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2023 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #ifndef BITCOIN_TEST_UTIL_RANDOM_H
       6                 :            : #define BITCOIN_TEST_UTIL_RANDOM_H
       7                 :            : 
       8                 :            : #include <consensus/amount.h>
       9                 :            : #include <random.h>
      10                 :            : #include <uint256.h>
      11                 :            : 
      12                 :            : #include <cstdint>
      13                 :            : 
      14                 :            : /**
      15                 :            :  * This global and the helpers that use it are not thread-safe.
      16                 :            :  *
      17                 :            :  * If thread-safety is needed, the global could be made thread_local (given
      18                 :            :  * that thread_local is supported on all architectures we support) or a
      19                 :            :  * per-thread instance could be used in the multi-threaded test.
      20                 :            :  */
      21                 :            : extern FastRandomContext g_insecure_rand_ctx;
      22                 :            : 
      23                 :            : /**
      24                 :            :  * Flag to make GetRand in random.h return the same number
      25                 :            :  */
      26                 :            : extern bool g_mock_deterministic_tests;
      27                 :            : 
      28                 :            : enum class SeedRand {
      29                 :            :     ZEROS, //!< Seed with a compile time constant of zeros
      30                 :            :     SEED,  //!< Call the Seed() helper
      31                 :            : };
      32                 :            : 
      33                 :            : /** Seed the given random ctx or use the seed passed in via an environment var */
      34                 :            : void Seed(FastRandomContext& ctx);
      35                 :            : 
      36                 :          1 : static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
      37                 :            : {
      38         [ -  + ]:          1 :     if (seed == SeedRand::ZEROS) {
      39                 :          0 :         g_insecure_rand_ctx = FastRandomContext(/*fDeterministic=*/true);
      40                 :          0 :     } else {
      41                 :          1 :         Seed(g_insecure_rand_ctx);
      42                 :            :     }
      43                 :          1 : }
      44                 :            : 
      45                 :            : static inline uint32_t InsecureRand32()
      46                 :            : {
      47                 :            :     return g_insecure_rand_ctx.rand32();
      48                 :            : }
      49                 :            : 
      50                 :          0 : static inline uint256 InsecureRand256()
      51                 :            : {
      52                 :          0 :     return g_insecure_rand_ctx.rand256();
      53                 :            : }
      54                 :            : 
      55                 :            : static inline uint64_t InsecureRandBits(int bits)
      56                 :            : {
      57                 :            :     return g_insecure_rand_ctx.randbits(bits);
      58                 :            : }
      59                 :            : 
      60                 :          0 : static inline uint64_t InsecureRandRange(uint64_t range)
      61                 :            : {
      62                 :          0 :     return g_insecure_rand_ctx.randrange(range);
      63                 :            : }
      64                 :            : 
      65                 :            : static inline bool InsecureRandBool()
      66                 :            : {
      67                 :            :     return g_insecure_rand_ctx.randbool();
      68                 :            : }
      69                 :            : 
      70                 :          0 : static inline CAmount InsecureRandMoneyAmount()
      71                 :            : {
      72                 :          0 :     return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1));
      73                 :            : }
      74                 :            : 
      75                 :            : #endif // BITCOIN_TEST_UTIL_RANDOM_H

Generated by: LCOV version 1.14