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

randombit / botan / 15808738489

22 Jun 2025 04:44PM UTC coverage: 90.56% (-0.001%) from 90.561%
15808738489

push

github

web-flow
Merge pull request #4942 from KaganCanSit/cppcheck_warning_message_in_tests

Fix/reduce Cppcheck warnings in test code

98795 of 109094 relevant lines covered (90.56%)

12461219.98 hits per line

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

88.37
/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/internal/fmt.h>
12
#endif
13

14
namespace Botan_Tests {
15

16
#if defined(BOTAN_HAS_HASH)
17

18
namespace {
19

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

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

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

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

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

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

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

69
         Test::Result result(algo);
14,234✔
70

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

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

78
         for(const auto& provider_ask : providers) {
14,234✔
79
            auto hash = Botan::HashFunction::create(algo, provider_ask);
7,117✔
80

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

86
            auto clone = hash->new_object();
7,117✔
87

88
            const std::string provider(hash->provider());
7,117✔
89
            result.test_is_nonempty("provider", provider);
7,117✔
90
            result.test_eq(provider, hash->name(), algo);
7,117✔
91
            result.test_eq(provider, hash->name(), clone->name());
14,234✔
92

93
            for(size_t i = 0; i != 3; ++i) {
28,468✔
94
               hash->update(input);
21,351✔
95
               result.test_eq(provider, "hashing", hash->final(), expected);
64,053✔
96
            }
97

98
            clone->update(input);
7,117✔
99
            result.test_eq(provider, "hashing (clone)", clone->final(), expected);
14,234✔
100

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

107
            result.test_eq(provider, "hashing after clear", hash->final(), expected);
14,234✔
108

109
            // Test that misaligned inputs work
110

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

115
               const size_t bytes_to_misalign = 15 - current_alignment;
7,062✔
116

117
               for(size_t i = 0; i != bytes_to_misalign; ++i) {
112,992✔
118
                  misaligned.insert(misaligned.begin(), 0x23);
105,930✔
119
               }
120

121
               hash->update(&misaligned[bytes_to_misalign], input.size());
7,062✔
122
               result.test_eq(provider, "hashing misaligned data", hash->final(), expected);
21,186✔
123
            }
7,062✔
124

125
            if(input.size() > 5) {
7,117✔
126
               hash->update(input[0]);
6,818✔
127

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

132
               size_t so_far = 1;
6,818✔
133
               while(so_far < input.size()) {
56,426✔
134
                  size_t take = this->rng().next_byte() % (input.size() - so_far);
49,608✔
135

136
                  if(input.size() - so_far == 1) {
49,608✔
137
                     take = 1;
6,818✔
138
                  }
139

140
                  hash->update(&input[so_far], take);
49,608✔
141
                  so_far += take;
49,608✔
142
               }
143
               result.test_eq(provider, "hashing split", hash->final(), expected);
13,636✔
144

145
               fork->update(&input[input.size() - 1], 1);
6,818✔
146
               result.test_eq(provider, "hashing split", fork->final(), expected);
20,454✔
147
            }
6,818✔
148

149
            if(hash->hash_block_size() > 0) {
7,117✔
150
               // GOST-34.11 uses 32 byte block
151
               result.test_gte("If hash_block_size is set, it is large", hash->hash_block_size(), 32);
14,022✔
152
            }
153
         }
21,351✔
154

155
         return result;
156
      }
21,351✔
157
};
158

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

239
         return output;
18✔
240
      }
×
241

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

306
   #endif
307

308
}  // namespace
309

310
#endif
311

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