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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 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/hash.h>
11
   #include <botan/rng.h>
12
   #include <botan/internal/fmt.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
#if defined(BOTAN_HAS_HASH)
18

19
namespace {
20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

110
            // Test that misaligned inputs work
111

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

240
         return output;
18✔
241
      }
×
242

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

307
   #endif
308

309
}  // namespace
310

311
#endif
312

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