• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 12732895979

12 Jan 2025 11:00AM UTC coverage: 91.237% (-0.003%) from 91.24%
12732895979

push

github

web-flow
Merge pull request #4525 from randombit/jack/entropy-is-optional

Let the entropy module be optional

93458 of 102434 relevant lines covered (91.24%)

11406941.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.71
/src/tests/test_entropy.cpp
1
/*
2
* (C) 2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "test_rng.h"
8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_ENTROPY_SOURCE)
11
   #include <botan/entropy_src.h>
12
#endif
13

14
#if defined(BOTAN_HAS_COMPRESSION)
15
   #include <botan/compression.h>
16
#endif
17

18
namespace Botan_Tests {
19

20
#if defined(BOTAN_HAS_ENTROPY_SOURCE)
21

22
namespace {
23

24
class Entropy_Source_Tests final : public Test {
×
25
   public:
26
      std::vector<Test::Result> run() override {
1✔
27
         Botan::Entropy_Sources& srcs = Botan::Entropy_Sources::global_sources();
1✔
28

29
         std::vector<std::string> src_names = srcs.enabled_sources();
1✔
30

31
         std::vector<Test::Result> results;
1✔
32

33
         for(const auto& src_name : src_names) {
5✔
34
            Test::Result result("Entropy source " + src_name);
4✔
35

36
            result.start_timer();
4✔
37

38
            try {
4✔
39
               SeedCapturing_RNG rng;
4✔
40

41
               size_t bits = srcs.poll_just(rng, src_name);
4✔
42

43
               result.test_gte("Entropy estimate", rng.seed_material().size() * 8, bits);
4✔
44

45
               if(rng.samples() > 0) {
4✔
46
                  result.test_gte("Seed material bytes", rng.seed_material().size(), 1);
4✔
47
                  result.test_gte("Samples", rng.samples(), 1);
8✔
48
               }
49

50
               result.test_note("poll result", rng.seed_material());
4✔
51

52
   #if defined(BOTAN_HAS_COMPRESSION)
53
               if(!rng.seed_material().empty()) {
4✔
54
                  /*
55
                  * Skip bzip2 both due to OS X problem (GH #394) and because bzip2's
56
                  * large blocks cause the entropy differential test to fail sometimes.
57
                  */
58
                  for(const std::string comp_algo : {"zlib", "lzma"}) {
12✔
59
                     auto comp = Botan::Compression_Algorithm::create(comp_algo);
8✔
60

61
                     if(comp) {
8✔
62
                        size_t comp1_size = 0;
8✔
63

64
                        try {
8✔
65
                           Botan::secure_vector<uint8_t> compressed;
8✔
66
                           compressed.assign(rng.seed_material().begin(), rng.seed_material().end());
8✔
67
                           comp->start(9);
8✔
68
                           comp->finish(compressed);
8✔
69

70
                           comp1_size = compressed.size();
8✔
71

72
                           result.test_gte(
8✔
73
                              comp_algo + " compressed entropy better than advertised", compressed.size() * 8, bits);
16✔
74
                        } catch(std::exception& e) {
8✔
75
                           result.test_failure(comp_algo + " exception while compressing", e.what());
×
76
                        }
×
77

78
                        SeedCapturing_RNG rng2;
8✔
79

80
                        size_t bits2 = srcs.poll_just(rng2, src_name);
8✔
81

82
                        result.test_note("poll 2 result", rng2.seed_material());
8✔
83

84
                        if(!rng.seed_material().empty() && !rng2.seed_material().empty()) {
8✔
85
                           try {
8✔
86
                              Botan::secure_vector<uint8_t> compressed;
8✔
87
                              compressed.insert(
16✔
88
                                 compressed.end(), rng.seed_material().begin(), rng.seed_material().end());
8✔
89
                              compressed.insert(
16✔
90
                                 compressed.end(), rng2.seed_material().begin(), rng2.seed_material().end());
8✔
91

92
                              comp->start();
8✔
93
                              comp->finish(compressed);
8✔
94

95
                              size_t comp2_size = compressed.size();
8✔
96

97
                              result.test_lt("Two blocks of entropy are larger than one", comp1_size, comp2_size);
8✔
98

99
                              size_t comp_diff = comp2_size - comp1_size;
8✔
100

101
                              result.test_gte(
8✔
102
                                 comp_algo + " diff compressed entropy better than advertised", comp_diff * 8, bits2);
16✔
103
                           } catch(std::exception& e) {
8✔
104
                              result.test_failure(comp_algo + " exception while compressing", e.what());
×
105
                           }
×
106
                        }
107
                     }
8✔
108
                  }
8✔
109
               }
110
   #endif
111
            } catch(std::exception& e) {
4✔
112
               result.test_failure("during entropy collection test", e.what());
×
113
            }
×
114

115
            result.end_timer();
4✔
116
            results.push_back(result);
4✔
117
         }
4✔
118

119
         return results;
1✔
120
      }
1✔
121
};
122

123
BOTAN_REGISTER_TEST("rng", "entropy", Entropy_Source_Tests);
124

125
}  // namespace
126

127
#endif
128

129
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc