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

randombit / botan / 21794448852

08 Feb 2026 12:09AM UTC coverage: 90.065% (-0.008%) from 90.073%
21794448852

push

github

web-flow
Merge pull request #5295 from randombit/jack/header-patrol-3

Reduce header dependencies in tests and cli

102230 of 113507 relevant lines covered (90.06%)

11492365.41 hits per line

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

88.95
/src/tests/test_hash.cpp
1
/*
2
* (C) 2014,2015,2018,2019 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_HASH)
10
   #include <botan/exceptn.h>
11
   #include <botan/hash.h>
12
   #include <botan/rng.h>
13
   #include <botan/internal/fmt.h>
14
#endif
15

16
namespace Botan_Tests {
17

18
#if defined(BOTAN_HAS_HASH)
19

20
namespace {
21

22
class Invalid_Hash_Name_Tests final : public Test {
1✔
23
   public:
24
      std::vector<Test::Result> run() override {
1✔
25
         Test::Result result("Invalid HashFunction names");
1✔
26
         test_invalid_name(result, "NonExistentHash");
2✔
27
         test_invalid_name(result, "Blake2b(9)", "Bad output bits size for BLAKE2b");
2✔
28
         test_invalid_name(result, "Comb4P(MD5,MD5)", "Comb4P: Must use two distinct hashes");
2✔
29
         test_invalid_name(result, "Comb4P(MD5,SHA-256)", "Comb4P: Incompatible hashes MD5 and SHA-256");
2✔
30
         test_invalid_name(result, "Keccak-1600(160)", "Keccak_1600: Invalid output length 160");
2✔
31
         test_invalid_name(result, "SHA-3(160)", "SHA_3: Invalid output length 160");
2✔
32

33
         return {result};
3✔
34
      }
2✔
35

36
   private:
37
      static void test_invalid_name(Result& result, const std::string& name, const std::string& expected_msg = "") {
6✔
38
         try {
6✔
39
            auto hash = Botan::HashFunction::create_or_throw(name);
6✔
40
            result.test_failure("Was successfully able to create " + name);
×
41
         } catch(Botan::Invalid_Argument& e) {
6✔
42
            const std::string msg = e.what();
5✔
43
            const std::string full_msg = "" + expected_msg;
5✔
44
            result.test_eq("expected error message", msg, full_msg);
10✔
45
         } catch(Botan::Lookup_Error& e) {
6✔
46
            const std::string algo_not_found_msg = "Unavailable Hash " + name;
1✔
47
            const std::string msg = e.what();
1✔
48
            result.test_eq("expected error message", msg, algo_not_found_msg);
2✔
49
         } catch(std::exception& e) {
1✔
50
            result.test_failure("some unknown exception", e.what());
×
51
         } catch(...) {
×
52
            result.test_failure("some unknown exception");
×
53
         }
×
54
      }
6✔
55
};
56

57
BOTAN_REGISTER_TEST("hash", "invalid_name_hash", Invalid_Hash_Name_Tests);
58

59
class Hash_Function_Tests final : public Text_Based_Test {
×
60
   public:
61
      Hash_Function_Tests() : Text_Based_Test("hash", "In,Out") {}
2✔
62

63
      std::vector<std::string> possible_providers(const std::string& algo) override {
7,579✔
64
         return provider_filter(Botan::HashFunction::providers(algo));
7,579✔
65
      }
66

67
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override {
7,579✔
68
         const std::vector<uint8_t> input = vars.get_req_bin("In");
7,579✔
69
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
7,579✔
70

71
         Test::Result result(algo);
15,158✔
72

73
         const std::vector<std::string> providers = possible_providers(algo);
7,579✔
74

75
         if(providers.empty()) {
7,579✔
76
            result.note_missing("hash " + algo);
×
77
            return result;
×
78
         }
79

80
         for(const auto& provider_ask : providers) {
15,158✔
81
            auto hash = Botan::HashFunction::create(algo, provider_ask);
7,579✔
82

83
            if(!hash) {
7,579✔
84
               result.test_failure(Botan::fmt("Hash {} supported by {} but not found", algo, provider_ask));
×
85
               continue;
×
86
            }
87

88
            auto clone = hash->new_object();
7,579✔
89

90
            const std::string provider(hash->provider());
7,579✔
91
            result.test_is_nonempty("provider", provider);
7,579✔
92
            result.test_eq(provider, hash->name(), algo);
7,579✔
93
            result.test_eq(provider, hash->name(), clone->name());
15,158✔
94

95
            for(size_t i = 0; i != 3; ++i) {
30,316✔
96
               hash->update(input);
22,737✔
97
               result.test_eq(provider, "hashing", hash->final(), expected);
68,211✔
98
            }
99

100
            clone->update(input);
7,579✔
101
            result.test_eq(provider, "hashing (clone)", clone->final(), expected);
15,158✔
102

103
            // Test to make sure clear() resets what we need it to
104
            hash->update("some discarded input");
7,579✔
105
            hash->clear();
7,579✔
106
            hash->update(nullptr, 0);  // this should be effectively ignored
7,579✔
107
            hash->update(input);
7,579✔
108

109
            result.test_eq(provider, "hashing after clear", hash->final(), expected);
15,158✔
110

111
            // Test that misaligned inputs work
112

113
            if(!input.empty()) {
7,579✔
114
               std::vector<uint8_t> misaligned = input;
7,520✔
115
               const size_t current_alignment = reinterpret_cast<uintptr_t>(misaligned.data()) % 16;
7,520✔
116

117
               const size_t bytes_to_misalign = 15 - current_alignment;
7,520✔
118

119
               for(size_t i = 0; i != bytes_to_misalign; ++i) {
120,320✔
120
                  misaligned.insert(misaligned.begin(), 0x23);
112,800✔
121
               }
122

123
               hash->update(&misaligned[bytes_to_misalign], input.size());
7,520✔
124
               result.test_eq(provider, "hashing misaligned data", hash->final(), expected);
22,560✔
125
            }
7,520✔
126

127
            if(input.size() > 5) {
7,579✔
128
               hash->update(input[0]);
7,257✔
129

130
               auto fork = hash->copy_state();
7,257✔
131
               // verify fork copy doesn't affect original computation
132
               fork->update(&input[1], input.size() - 2);
7,257✔
133

134
               size_t so_far = 1;
7,257✔
135
               while(so_far < input.size()) {
59,265✔
136
                  size_t take = this->rng().next_byte() % (input.size() - so_far);
52,008✔
137

138
                  if(input.size() - so_far == 1) {
52,008✔
139
                     take = 1;
7,257✔
140
                  }
141

142
                  hash->update(&input[so_far], take);
52,008✔
143
                  so_far += take;
52,008✔
144
               }
145
               result.test_eq(provider, "hashing split", hash->final(), expected);
14,514✔
146

147
               fork->update(&input[input.size() - 1], 1);
7,257✔
148
               result.test_eq(provider, "hashing split", fork->final(), expected);
21,771✔
149
            }
7,257✔
150

151
            if(hash->hash_block_size() > 0) {
7,579✔
152
               // GOST-34.11 uses 32 byte block
153
               result.test_gte("If hash_block_size is set, it is large", hash->hash_block_size(), 32);
14,734✔
154
            }
155
         }
22,737✔
156

157
         return result;
158
      }
22,737✔
159
};
160

161
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("hash", "hash_algos", Hash_Function_Tests);
162

163
class Hash_NIST_MonteCarlo_Tests final : public Text_Based_Test {
×
164
   public:
165
      Hash_NIST_MonteCarlo_Tests() : Text_Based_Test("hash_mc.vec", "Seed,Count,Output") {}
2✔
166

167
      std::vector<std::string> possible_providers(const std::string& algo) override {
7✔
168
         return provider_filter(Botan::HashFunction::providers(algo));
7✔
169
      }
170

171
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override {
7✔
172
         const std::vector<uint8_t> seed = vars.get_req_bin("Seed");
7✔
173
         const size_t count = vars.get_req_sz("Count");
7✔
174
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
7✔
175

176
         Test::Result result("NIST Monte Carlo " + algo);
7✔
177

178
         const std::vector<std::string> providers = possible_providers(algo);
7✔
179

180
         if(providers.empty()) {
7✔
181
            result.note_missing("hash " + algo);
1✔
182
            return result;
1✔
183
         }
184

185
         for(const auto& provider_ask : providers) {
12✔
186
            auto hash = Botan::HashFunction::create(algo, provider_ask);
6✔
187

188
            if(!hash) {
6✔
189
               result.test_failure(Botan::fmt("Hash {} supported by {} but not found", algo, provider_ask));
×
190
               continue;
×
191
            }
192

193
            std::vector<std::vector<uint8_t>> input;
6✔
194
            input.push_back(seed);
6✔
195
            input.push_back(seed);
6✔
196
            input.push_back(seed);
6✔
197

198
            for(size_t j = 0; j <= count; ++j) {
606✔
199
               for(size_t i = 3; i != 1003; ++i) {
600,600✔
200
                  hash->update(input[0]);
600,000✔
201
                  hash->update(input[1]);
600,000✔
202
                  hash->update(input[2]);
600,000✔
203

204
                  hash->final(input[0].data());
600,000✔
205
                  input[0].swap(input[1]);
600,000✔
206
                  input[1].swap(input[2]);
600,000✔
207
               }
208

209
               if(j < count) {
600✔
210
                  input[0] = input[2];
594✔
211
                  input[1] = input[2];
594✔
212
               }
213
            }
214

215
            result.test_eq("Output is expected", input[2], expected);
18✔
216
         }
12✔
217

218
         return result;
219
      }
21✔
220
};
221

222
BOTAN_REGISTER_TEST("hash", "hash_nist_mc", Hash_NIST_MonteCarlo_Tests);
223

224
class Hash_LongRepeat_Tests final : public Text_Based_Test {
×
225
   public:
226
      Hash_LongRepeat_Tests() : Text_Based_Test("hash_rep.vec", "Input,TotalLength,Digest") {}
2✔
227

228
      std::vector<std::string> possible_providers(const std::string& algo) override {
18✔
229
         return provider_filter(Botan::HashFunction::providers(algo));
18✔
230
      }
231

232
      // repeating the output several times reduces buffering overhead during processing
233
      static std::vector<uint8_t> expand_input(const std::vector<uint8_t>& input, size_t min_len) {
18✔
234
         std::vector<uint8_t> output;
18✔
235
         output.reserve(min_len);
18✔
236

237
         while(output.size() < min_len) {
2,358✔
238
            output.insert(output.end(), input.begin(), input.end());
2,340✔
239
         }
240

241
         return output;
18✔
242
      }
×
243

244
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override {
18✔
245
         const std::vector<uint8_t> input = expand_input(vars.get_req_bin("Input"), 256);
36✔
246
         const size_t total_len = vars.get_req_sz("TotalLength");
18✔
247
         const std::vector<uint8_t> expected = vars.get_req_bin("Digest");
18✔
248

249
         Test::Result result("Long input " + algo);
18✔
250

251
         const std::vector<std::string> providers = possible_providers(algo);
18✔
252

253
         if(total_len > 1000000 && Test::run_long_tests() == false) {
18✔
254
            return result;
255
         }
256

257
         if(providers.empty()) {
18✔
258
            result.note_missing("hash " + algo);
×
259
            return result;
×
260
         }
261

262
         for(const auto& provider_ask : providers) {
36✔
263
            auto hash = Botan::HashFunction::create(algo, provider_ask);
18✔
264

265
            if(!hash) {
18✔
266
               result.test_failure(Botan::fmt("Hash {} supported by {} but not found", algo, provider_ask));
×
267
               continue;
×
268
            }
269

270
            const size_t full_inputs = total_len / input.size();
18✔
271
            const size_t leftover = total_len % input.size();
18✔
272

273
            for(size_t i = 0; i != full_inputs; ++i) {
37,783,908✔
274
               hash->update(input);
37,783,890✔
275
            }
276

277
            if(leftover > 0) {
18✔
278
               hash->update(input.data(), leftover);
9✔
279
            }
280

281
            std::vector<uint8_t> output(hash->output_length());
18✔
282
            hash->final(output.data());
18✔
283
            result.test_eq("Output is expected", output, expected);
36✔
284
         }
36✔
285

286
         return result;
287
      }
54✔
288
};
289

290
BOTAN_REGISTER_TEST("hash", "hash_rep", Hash_LongRepeat_Tests);
291

292
   #if defined(BOTAN_HAS_TRUNCATED_HASH) && defined(BOTAN_HAS_SHA2_32)
293

294
/// negative tests for Truncated_Hash, positive tests are implemented in hash/truncated.vec
295
Test::Result hash_truncation_negative_tests() {
1✔
296
   Test::Result result("hash truncation parameter validation");
1✔
297
   result.test_throws<Botan::Invalid_Argument>("truncation to zero",
2✔
298
                                               [] { Botan::HashFunction::create("Truncated(SHA-256,0)"); });
1✔
299
   result.test_throws<Botan::Invalid_Argument>("cannot output more bits than the underlying hash",
2✔
300
                                               [] { Botan::HashFunction::create("Truncated(SHA-256,257)"); });
1✔
301
   auto unobtainable = Botan::HashFunction::create("Truncated(NonExistentHash-256,128)");
1✔
302
   result.confirm("non-existent hashes are not created", unobtainable == nullptr);
2✔
303
   return result;
1✔
304
}
1✔
305

306
BOTAN_REGISTER_TEST_FN("hash", "hash_truncation", hash_truncation_negative_tests);
307

308
   #endif
309

310
}  // namespace
311

312
#endif
313

314
}  // 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