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

randombit / botan / 5356685938

23 Jun 2023 01:39PM UTC coverage: 91.746% (+0.02%) from 91.728%
5356685938

push

github

randombit
Merge GH #3595 Apply clang-tidy more universally

Previously clang-tidy ruleset disabled certain rules for the cli and
tests. Remove these exceptions, and fix the relevant warnings.

Also fix compile_commands.json which had previously not provided information for
the examples, BoGo shim, or fuzzers. As a result, clang-tidy was effectively
blind to them. Fix various clang-tidy findings in these files.

Additionally fix clang-tidy warnings that were in Boost or Sqlite3 specific
code, which had been accidentally omitted in past checks.

Modify the nightly clang-tidy run to additionally check the examples, fuzzers,
shim, and Sqlite3/Boost specific code.

78183 of 85217 relevant lines covered (91.75%)

12364366.11 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
      }
1✔
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) {
11✔
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) {
2✔
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 {
6,170✔
62
         return provider_filter(Botan::HashFunction::providers(algo));
6,170✔
63
      }
64

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

69
         Test::Result result(algo);
12,340✔
70

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

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

78
         for(const auto& provider_ask : providers) {
12,340✔
79
            auto hash = Botan::HashFunction::create(algo, provider_ask);
6,170✔
80

81
            if(!hash) {
6,170✔
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();
6,170✔
87

88
            const std::string provider(hash->provider());
6,170✔
89
            result.test_is_nonempty("provider", provider);
6,170✔
90
            result.test_eq(provider, hash->name(), algo);
6,170✔
91
            result.test_eq(provider, hash->name(), clone->name());
12,340✔
92

93
            for(size_t i = 0; i != 3; ++i) {
24,680✔
94
               hash->update(input);
18,510✔
95
               result.test_eq(provider, "hashing", hash->final(), expected);
55,530✔
96
            }
97

98
            clone->update(input);
6,170✔
99
            result.test_eq(provider, "hashing (clone)", clone->final(), expected);
12,340✔
100

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

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

109
            // Test that misaligned inputs work
110

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

115
               const size_t bytes_to_misalign = 15 - current_alignment;
6,121✔
116

117
               for(size_t i = 0; i != bytes_to_misalign; ++i) {
97,936✔
118
                  misaligned.insert(misaligned.begin(), 0x23);
91,815✔
119
               }
120

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

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

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

132
               size_t so_far = 1;
133
               while(so_far < input.size()) {
49,402✔
134
                  size_t take = Test::rng().next_byte() % (input.size() - so_far);
43,497✔
135

136
                  if(input.size() - so_far == 1) {
43,497✔
137
                     take = 1;
5,905✔
138
                  }
139

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

145
               fork->update(&input[input.size() - 1], 1);
5,905✔
146
               result.test_eq(provider, "hashing split", fork->final(), expected);
17,715✔
147
            }
5,905✔
148

149
            if(hash->hash_block_size() > 0) {
6,170✔
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);
12,128✔
152
            }
153
         }
18,510✔
154

155
         return result;
156
      }
18,510✔
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
            std::vector<uint8_t> buf(hash->output_length());
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);
12✔
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") {}
3✔
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

© 2025 Coveralls, Inc