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

randombit / botan / 44860445550

20 Nov 2026 12:17PM UTC coverage: 92.497% (+0.007%) from 92.49%
44860445550

push

github

web-flow
Merge pull request #6145 from randombit/jack/fuzzer-work

Fix various bugs and issues in the fuzzers

145799 of 157625 relevant lines covered (92.5%)

8640547.93 hits per line

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

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

7
#include "tests.h"
8

9
#include <botan/exceptn.h>
10
#include <botan/hex.h>
11
#include <botan/mem_ops.h>
12
#include <algorithm>
13
#include <optional>
14

15
#if defined(BOTAN_HAS_BASE64_CODEC)
16
   #include <botan/base64.h>
17
#endif
18

19
#if defined(BOTAN_HAS_BASE32_CODEC)
20
   #include <botan/base32.h>
21
#endif
22

23
#if defined(BOTAN_HAS_BASE58_CODEC)
24
   #include <botan/base58.h>
25
#endif
26

27
namespace Botan_Tests {
28

29
namespace {
30

31
#if defined(BOTAN_HAS_BASE32_CODEC)
32

33
class Base32_Tests final : public Text_Based_Test {
×
34
   public:
35
      Base32_Tests() : Text_Based_Test("codec/base32.vec", "Base32", "Binary") {}
2 ✔
36

37
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
33 ✔
38
         Test::Result result("Base32");
33 ✔
39

40
         const bool is_valid = (type == "valid");
33 ✔
41
         const std::string base32 = vars.get_req_str("Base32");
33 ✔
42

43
         try {
33 ✔
44
            if(is_valid) {
33 ✔
45
               const std::vector<uint8_t> binary = vars.get_req_bin("Binary");
24 ✔
46
               result.test_bin_eq("base32 decoding", Botan::base32_decode(base32), binary);
24 ✔
47
               result.test_str_eq("base32 encoding", Botan::base32_encode(binary), base32);
24 ✔
48
            } else {
24 ✔
49
               auto res = Botan::base32_decode(base32);
9 ✔
50
               result.test_failure("decoded invalid base32 to " + Botan::hex_encode(res));
×
51
            }
×
52
         } catch(std::exception& e) {
9 ✔
53
            if(is_valid) {
9 ✔
54
               result.test_failure("rejected valid base32", e.what());
×
55
            } else {
56
               result.test_note("rejected invalid base32");
9 ✔
57
            }
58
         }
9 ✔
59

60
         return result;
33 ✔
61
      }
33 ✔
62

63
      std::vector<Test::Result> run_final_tests() override {
1 ✔
64
         Test::Result result("Base32");
1 ✔
65
         const std::string valid_b32 = "MY======";
1 ✔
66

67
         for(const char ws_char : {' ', '\t', '\r', '\n'}) {
5 ✔
68
            for(size_t i = 0; i <= valid_b32.size(); ++i) {
40 ✔
69
               std::string b32_ws = valid_b32;
36 ✔
70
               b32_ws.insert(i, 1, ws_char);
36 ✔
71

72
               try {
36 ✔
73
                  result.test_failure("decoded whitespace base32", Botan::base32_decode(b32_ws, false));
36 ✔
74
               } catch(std::exception&) {}
36 ✔
75

76
               try {
36 ✔
77
                  result.test_bin_eq("base32 decoding with whitespace", Botan::base32_decode(b32_ws, true), "66");
72 ✔
78
               } catch(std::exception& e) {
×
79
                  result.test_failure(b32_ws, e.what());
×
80
               }
×
81
            }
36 ✔
82
         }
83

84
         return {result};
3 ✔
85
      }
2 ✔
86
};
87

88
BOTAN_REGISTER_TEST("codec", "base32", Base32_Tests);
89

90
#endif
91

92
#if defined(BOTAN_HAS_BASE58_CODEC)
93

94
class Base58_Tests final : public Text_Based_Test {
×
95
   public:
96
      Base58_Tests() : Text_Based_Test("codec/base58.vec", "Base58", "Binary") {}
2 ✔
97

98
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
60 ✔
99
         Test::Result result("Base58");
60 ✔
100

101
         const bool is_valid = (type == "valid");
60 ✔
102
         const std::string base58 = vars.get_req_str("Base58");
60 ✔
103

104
         try {
60 ✔
105
            if(is_valid) {
60 ✔
106
               const std::vector<uint8_t> binary = vars.get_req_bin("Binary");
36 ✔
107
               result.test_bin_eq("base58 decoding", Botan::base58_decode(base58), binary);
36 ✔
108
               result.test_str_eq("base58 encoding", Botan::base58_encode(binary), base58);
36 ✔
109
            } else {
36 ✔
110
               auto res = Botan::base58_decode(base58);
24 ✔
111
               result.test_failure("decoded invalid base58 to " + Botan::hex_encode(res));
×
112
            }
×
113
         } catch(std::exception& e) {
24 ✔
114
            if(is_valid) {
24 ✔
115
               result.test_failure("rejected valid base58", e.what());
×
116
            } else {
117
               result.test_note("rejected invalid base58");
24 ✔
118
            }
119
         }
24 ✔
120

121
         return result;
60 ✔
122
      }
60 ✔
123

124
      std::vector<Test::Result> run_final_tests() override {
1 ✔
125
         Test::Result result("Base58");
1 ✔
126

127
         // Whitespace is ignored wherever it appears, including inside a block of 8 characters
128
         const std::vector<uint8_t> expected =
1 ✔
129
            Botan::hex_decode("6162636465666768696a6b6c6d6e6f707172737475767778797a");
1 ✔
130

131
         const std::vector<std::string> with_ws = {
1 ✔
132
            "3yxU3u1igY8WkgtjK92fbJQCd4BZiiT1v25f\n",
133
            " 3yxU3u1igY8WkgtjK92fbJQCd4BZiiT1v25f",
134
            "3yxU 3u1igY8WkgtjK92fbJQCd4BZiiT1v25f",
135
            "3yxU3u1i\ngY8Wkgtj\nK92fbJQC\nd4BZiiT1\nv25f",
136
            "3yxU3u1igY8Wkgtj K92fbJQCd4BZiiT1v25f",
137
            "3yxU3u1igY8WkgtjK92fbJ  QCd4BZiiT1v25f",
138
            "3 y x U 3 u 1 i g Y 8 W k g t j K 9 2 f b J Q C d 4 B Z i i T 1 v 2 5 f",
139
         };
1 ✔
140

141
         for(const auto& b58 : with_ws) {
8 ✔
142
            try {
7 ✔
143
               result.test_bin_eq("base58 decoding with whitespace", Botan::base58_decode(b58), expected);
14 ✔
144
            } catch(std::exception& e) {
×
145
               result.test_failure("rejected valid base58 with whitespace", e.what());
×
146
            }
×
147
         }
148

149
         return {result};
3 ✔
150
      }
3 ✔
151
};
152

153
BOTAN_REGISTER_TEST("codec", "base58", Base58_Tests);
154

155
class Base58_Check_Tests final : public Text_Based_Test {
×
156
   public:
157
      Base58_Check_Tests() : Text_Based_Test("codec/base58c.vec", "Base58", "Binary") {}
2 ✔
158

159
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
9 ✔
160
         Test::Result result("Base58 Check");
9 ✔
161

162
         const bool is_valid = (type == "valid");
9 ✔
163
         const std::string base58 = vars.get_req_str("Base58");
9 ✔
164

165
         try {
9 ✔
166
            if(is_valid) {
9 ✔
167
               const std::vector<uint8_t> binary = vars.get_req_bin("Binary");
4 ✔
168
               result.test_bin_eq("base58 decoding", Botan::base58_check_decode(base58), binary);
4 ✔
169
               result.test_str_eq("base58 encoding", Botan::base58_check_encode(binary), base58);
4 ✔
170
            } else {
4 ✔
171
               auto res = Botan::base58_check_decode(base58);
5 ✔
172
               result.test_failure("decoded invalid base58c to " + Botan::hex_encode(res));
×
173
            }
×
174
         } catch(std::exception& e) {
5 ✔
175
            if(is_valid) {
5 ✔
176
               result.test_failure("rejected valid base58c", e.what());
×
177
            } else {
178
               result.test_note("rejected invalid base58c");
5 ✔
179
            }
180
         }
5 ✔
181

182
         return result;
9 ✔
183
      }
9 ✔
184
};
185

186
BOTAN_REGISTER_TEST("codec", "base58c", Base58_Check_Tests);
187

188
#endif
189

190
#if defined(BOTAN_HAS_BASE64_CODEC)
191

192
class Base64_Tests final : public Text_Based_Test {
×
193
   public:
194
      Base64_Tests() : Text_Based_Test("codec/base64.vec", "Base64", "Binary") {}
2 ✔
195

196
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
39 ✔
197
         Test::Result result("Base64");
39 ✔
198

199
         const bool is_valid = (type == "valid");
39 ✔
200
         const std::string base64 = vars.get_req_str("Base64");
39 ✔
201

202
         try {
39 ✔
203
            if(is_valid) {
39 ✔
204
               const std::vector<uint8_t> binary = vars.get_req_bin("Binary");
27 ✔
205
               result.test_bin_eq("base64 decoding", Botan::base64_decode(base64), binary);
27 ✔
206
               result.test_str_eq("base64 encoding", Botan::base64_encode(binary), base64);
27 ✔
207
            } else {
27 ✔
208
               auto res = Botan::base64_decode(base64);
12 ✔
209
               result.test_failure("decoded invalid base64 to " + Botan::hex_encode(res));
×
210
            }
×
211
         } catch(std::exception& e) {
12 ✔
212
            if(is_valid) {
12 ✔
213
               result.test_failure("rejected valid base64", e.what());
×
214
            } else {
215
               result.test_note("rejected invalid base64");
12 ✔
216
            }
217
         }
12 ✔
218

219
         return result;
39 ✔
220
      }
39 ✔
221

222
      std::vector<Test::Result> run_final_tests() override {
1 ✔
223
         Test::Result result("Base64");
1 ✔
224
         const std::string valid_b64 = "Zg==";
1 ✔
225

226
         for(const char ws_char : {' ', '\t', '\r', '\n'}) {
5 ✔
227
            for(size_t i = 0; i <= valid_b64.size(); ++i) {
24 ✔
228
               std::string b64_ws = valid_b64;
20 ✔
229
               b64_ws.insert(i, 1, ws_char);
20 ✔
230

231
               try {
20 ✔
232
                  result.test_failure("decoded whitespace base64", Botan::base64_decode(b64_ws, false));
20 ✔
233
               } catch(std::exception&) {}
20 ✔
234

235
               try {
20 ✔
236
                  result.test_bin_eq("base64 decoding with whitespace", Botan::base64_decode(b64_ws, true), "66");
40 ✔
237
               } catch(std::exception& e) {
×
238
                  result.test_failure(b64_ws, e.what());
×
239
               }
×
240
            }
20 ✔
241
         }
242

243
         // Long inputs exercise the SIMD bulk paths in some build configurations
244
         std::vector<uint8_t> long_bin(3000);
1 ✔
245
         for(size_t i = 0; i != long_bin.size(); ++i) {
3,001 ✔
246
            long_bin[i] = static_cast<uint8_t>(i * 7 + i / 251);
3,000 ✔
247
         }
248

249
         const std::string long_b64 = Botan::base64_encode(long_bin);
1 ✔
250

251
         // PEM style line wrapping
252
         std::string long_b64_ws;
1 ✔
253
         for(size_t i = 0; i != long_b64.size(); ++i) {
4,001 ✔
254
            if(i > 0 && i % 64 == 0) {
4,000 ✔
255
               long_b64_ws += '\n';
62 ✔
256
            }
257
            long_b64_ws += long_b64[i];
8,000 ✔
258
         }
259

260
         try {
1 ✔
261
            result.test_bin_eq("long base64 decoding", Botan::base64_decode(long_b64, false), long_bin);
1 ✔
262
            result.test_bin_eq("long base64 decoding with newlines", Botan::base64_decode(long_b64_ws, true), long_bin);
2 ✔
263
         } catch(std::exception& e) {
×
264
            result.test_failure("long base64 roundtrip", e.what());
×
265
         }
×
266

267
         try {
1 ✔
268
            std::string corrupted = long_b64;
1 ✔
269
            corrupted[corrupted.size() / 2] = '!';
1 ✔
270
            result.test_failure("decoded corrupted base64", Botan::base64_decode(corrupted));
1 ✔
271
         } catch(std::exception&) {
1 ✔
272
            result.test_note("rejected corrupted base64");
1 ✔
273
         }
1 ✔
274

275
         test_buffer_bounds(result);
1 ✔
276

277
         return {result};
3 ✔
278
      }
3 ✔
279

280
   private:
281
      static constexpr size_t CanaryLen = 64;
282
      static constexpr uint8_t Canary = 0xA5;
283

284
      static bool canaries_intact(std::span<const uint8_t> buf) {
15,729 ✔
285
         const auto is_canary = [](uint8_t b) { return b == Canary; };
15,729 ✔
286
         const auto head = buf.first(CanaryLen);
15,729 ✔
287
         const auto tail = buf.last(CanaryLen);
15,729 ✔
288
         return std::all_of(head.begin(), head.end(), is_canary) && std::all_of(tail.begin(), tail.end(), is_canary);
15,729 ✔
289
      }
290

291
      static void check_decode_bounds(Test::Result& result, std::string_view input, bool ignore_ws) {
15,408 ✔
292
         // An exact size allocation lets sanitizers see any read past the end
293
         const std::vector<char> in(input.begin(), input.end());
15,408 ✔
294
         const size_t max_output = Botan::base64_decode_max_output(in.size());
15,408 ✔
295

296
         // The streaming decoder never uses the SIMD bulk paths
297
         std::vector<uint8_t> ref(max_output);
15,408 ✔
298
         std::optional<size_t> ref_len;
15,408 ✔
299
         try {
15,408 ✔
300
            size_t consumed = 0;
15,408 ✔
301
            const size_t written = Botan::base64_decode(ref.data(), in.data(), in.size(), consumed, true, ignore_ws);
15,408 ✔
302
            if(consumed == in.size()) {
5,154 ✔
303
               ref_len = written;
3,210 ✔
304
            }
305
         } catch(Botan::Invalid_Argument&) {}
10,254 ✔
306

307
         std::vector<uint8_t> buf(CanaryLen + max_output + CanaryLen, Canary);
15,408 ✔
308
         const auto out = std::span{buf}.subspan(CanaryLen, max_output);
15,408 ✔
309
         std::optional<size_t> out_len;
15,408 ✔
310
         try {
15,408 ✔
311
            out_len = Botan::base64_decode(out.data(), in.data(), in.size(), ignore_ws);
27,606 ✔
312
         } catch(Botan::Invalid_Argument&) {}
12,198 ✔
313

314
         const std::string what = "base64 decoding " + std::to_string(in.size()) + " chars";
46,224 ✔
315

316
         if(!canaries_intact(buf)) {
15,408 ✔
317
            result.test_failure(what + " wrote outside of the output buffer");
×
318
         } else if(out_len.has_value() != ref_len.has_value()) {
15,408 ✔
319
            result.test_failure(what + " disagrees with the streaming decoder on validity");
×
320
         } else if(out_len.has_value()) {
15,408 ✔
321
            if(result.test_sz_eq(what + " length", *out_len, *ref_len)) {
3,210 ✔
322
               result.test_bin_eq(what, out.first(*out_len), std::span{ref}.first(*ref_len));
3,210 ✔
323
            }
324
         } else {
325
            result.test_success();
12,198 ✔
326
         }
327
      }
49,426 ✔
328

329
      static void check_encode_bounds(Test::Result& result, size_t len) {
321 ✔
330
         std::vector<uint8_t> bin(len);
321 ✔
331
         for(size_t i = 0; i != len; ++i) {
51,681 ✔
332
            bin[i] = static_cast<uint8_t>(i * 7 + len);
51,360 ✔
333
         }
334

335
         const size_t max_output = Botan::base64_encode_max_output(len);
321 ✔
336
         std::vector<uint8_t> buf(CanaryLen + max_output + CanaryLen, Canary);
321 ✔
337
         char* out = Botan::cast_uint8_ptr_to_char(&buf[CanaryLen]);
321 ✔
338

339
         size_t consumed = 0;
321 ✔
340
         const size_t produced = Botan::base64_encode(out, bin.data(), bin.size(), consumed, true);
321 ✔
341

342
         const std::string what = "base64 encoding " + std::to_string(len) + " bytes";
963 ✔
343

344
         if(!canaries_intact(buf)) {
321 ✔
345
            result.test_failure(what + " wrote outside of the output buffer");
×
346
         } else if(produced != max_output || consumed != len) {
321 ✔
347
            result.test_failure(what + " returned unexpected lengths");
×
348
         } else {
349
            std::vector<uint8_t> decoded(Botan::base64_decode_max_output(produced));
321 ✔
350
            size_t decode_consumed = 0;
321 ✔
351
            decoded.resize(Botan::base64_decode(decoded.data(), out, produced, decode_consumed, true, false));
321 ✔
352
            result.test_bin_eq(what, decoded, bin);
321 ✔
353
         }
321 ✔
354
      }
962 ✔
355

356
      // Every buffer is exactly the documented size, and the outputs are
357
      // surrounded by canaries, so any write outside of them is detected
358
      static void test_buffer_bounds(Test::Result& result) {
1 ✔
359
         constexpr size_t max_len = 320;
1 ✔
360
         const std::string_view alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1 ✔
361

362
         for(size_t len = 0; len <= max_len; ++len) {
322 ✔
363
            check_encode_bounds(result, len);
321 ✔
364

365
            for(const bool all_zero : {true, false}) {
963 ✔
366
               std::string body(len, 'A');
642 ✔
367
               if(!all_zero) {
642 ✔
368
                  for(size_t i = 0; i != len; ++i) {
51,681 ✔
369
                     body[i] = alphabet[(i * 7 + len) % alphabet.size()];
51,360 ✔
370
                  }
371
               }
372

373
               // Includes excess padding, and padding following complete groups
374
               for(size_t pads = 0; pads <= 3; ++pads) {
3,210 ✔
375
                  const std::string padded = body + std::string(pads, '=');
2,568 ✔
376

377
                  // PEM style line wrapping
378
                  std::string wrapped;
2,568 ✔
379
                  for(size_t i = 0; i < padded.size(); i += 64) {
10,314 ✔
380
                     wrapped += padded.substr(i, 64);
15,492 ✔
381
                     wrapped += '\n';
15,492 ✔
382
                  }
383

384
                  for(const auto& input : {padded, wrapped, " " + padded}) {
25,680 ✔
385
                     for(const bool ignore_ws : {true, false}) {
23,112 ✔
386
                        check_decode_bounds(result, input, ignore_ws);
15,408 ✔
387
                     }
388
                  }
10,272 ✔
389
               }
2,568 ✔
390
            }
642 ✔
391
         }
392
      }
1 ✔
393
};
394

395
BOTAN_REGISTER_TEST("codec", "base64", Base64_Tests);
396

397
#endif
398

399
class Codec_Error_Char_Display_Tests final : public Test {
1 ✔
400
   public:
401
      std::vector<Test::Result> run() override {
1 ✔
402
         Test::Result result("Codec error message character display");
1 ✔
403

404
         const std::vector<std::pair<char, std::string>> cases = {
1 ✔
405
            {'\x1b', "\\x1B"},
1 ✔
406
            {'\x00', "\\x00"},
1 ✔
407
            {'\n', "\\n"},
1 ✔
408
            {'\r', "\\r"},
1 ✔
409
            {'\t', "\\t"},
1 ✔
410
            {'\x7f', "\\x7F"},
1 ✔
411
            {static_cast<char>(0xFF), "\\xFF"},
1 ✔
412
         };
8 ✔
413

414
         for(const auto& [bad, expect] : cases) {
8 ✔
415
            try {
7 ✔
416
               Botan::hex_decode(std::string(1, bad), false);
14 ✔
417
               result.test_failure("hex_decode accepted invalid byte");
×
418
            } catch(const Botan::Invalid_Argument& e) {
7 ✔
419
               const std::string msg = e.what();
7 ✔
420
               result.test_is_true("message uses escaped string", msg.find(expect) != std::string::npos);
7 ✔
421
            }
7 ✔
422
         }
423

424
         return {result};
3 ✔
425
      }
3 ✔
426
};
427

428
BOTAN_REGISTER_TEST("codec", "codec_error_char_display", Codec_Error_Char_Display_Tests);
429

430
}  // namespace
431

432
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc