LCOV - code coverage report
Current view: top level - src/test/util - random.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 71.4 % 14 10
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 2 2
Branches: 52.9 % 17 9

             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                 :             : #include <test/util/random.h>
       6                 :             : 
       7                 :             : #include <logging.h>
       8                 :             : #include <random.h>
       9                 :             : #include <uint256.h>
      10                 :             : #include <util/check.h>
      11                 :             : 
      12                 :             : #include <cstdlib>
      13                 :             : #include <iostream>
      14                 :             : 
      15                 :             : extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
      16                 :             : 
      17                 :        1827 : void SeedRandomStateForTest(SeedRand seedtype)
      18                 :             : {
      19                 :        1827 :     constexpr auto RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
      20                 :             : 
      21                 :             :     // Do this once, on the first call, regardless of seedtype, because once
      22                 :             :     // MakeRandDeterministicDANGEROUS is called, the output of GetRandHash is
      23                 :             :     // no longer truly random. It should be enough to get the seed once for the
      24                 :             :     // process.
      25   [ +  +  -  +  :        1828 :     static const uint256 ctx_seed = []() {
                   +  - ]
      26                 :             :         // If RANDOM_CTX_SEED is set, use that as seed.
      27   [ -  +  -  -  :           1 :         if (const char* num{std::getenv(RANDOM_CTX_SEED)}) {
                      + ]
      28         [ #  # ]:           0 :             if (auto num_parsed{uint256::FromUserHex(num)}) {
      29                 :           0 :                 return *num_parsed;
      30                 :             :             } else {
      31                 :           0 :                 std::cerr << RANDOM_CTX_SEED << " must consist of up to " << uint256::size() * 2 << " hex digits (\"0x\" prefix allowed), it was set to: '" << num << "'.\n";
      32                 :           0 :                 std::abort();
      33                 :             :             }
      34                 :             :         }
      35                 :             :         // Otherwise use a (truly) random value.
      36                 :           1 :         return GetRandHash();
      37                 :           1 :     }();
      38                 :             : 
      39         [ +  + ]:        1827 :     const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO};
      40         [ +  - ]:        1827 :     LogInfo("Setting random seed for current tests to %s=%s\n", RANDOM_CTX_SEED, seed.GetHex());
      41                 :        1827 :     MakeRandDeterministicDANGEROUS(seed);
      42                 :        1827 : }
        

Generated by: LCOV version 2.0-1