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

randombit / botan / 5134090420

31 May 2023 03:12PM UTC coverage: 91.721% (-0.3%) from 91.995%
5134090420

push

github

randombit
Merge GH #3565 Disable noisy/pointless pylint warnings

76048 of 82912 relevant lines covered (91.72%)

11755290.1 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
#endif
12

13
namespace Botan_Tests {
14

15
#if defined(BOTAN_HAS_HASH)
16

17
namespace {
18

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

30
         return {result};
3✔
31
      }
1✔
32

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

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

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

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

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

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

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

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

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

80
            if(!hash) {
6,170✔
81
               result.test_failure("Hash " + algo + " supported by " + provider_ask + " but not found");
×
82
               continue;
×
83
            }
84

85
            auto clone = hash->new_object();
6,170✔
86

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

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

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

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

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

108
            // Test that misaligned inputs work
109

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

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

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

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

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

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

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

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

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

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

148
            if(hash->hash_block_size() > 0) {
6,170✔
149
               // GOST-34.11 uses 32 byte block
150
               result.test_gte("If hash_block_size is set, it is large", hash->hash_block_size(), 32);
12,128✔
151
            }
152
         }
18,510✔
153

154
         return result;
155
      }
18,510✔
156
};
157

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

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

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

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

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

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

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

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

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

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

195
            std::vector<uint8_t> buf(hash->output_length());
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") {}
3✔
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("Hash " + algo + " supported by " + provider_ask + " but not found");
×
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

© 2025 Coveralls, Inc