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

randombit / botan / 41571428728

02 Nov 2026 10:24PM UTC coverage: 90.072% (+0.1%) from 89.939%
41571428728

push

github

web-flow
Merge pull request #6091 from randombit/jack/tls13-cert-pk

When parsing a TLS 1.3 Certificate list don't bother parsing keys we don't need

139779 of 155185 relevant lines covered (90.07%)

8705172.21 hits per line

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

94.89
/src/tests/test_utils.cpp
1
/*
2
* (C) 2015,2018,2024 Jack Lloyd
3
* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
4
* (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include "tests.h"
10

11
#include "test_arb_eq.h"
12
#include <botan/exceptn.h>
13
#include <botan/hex.h>
14
#include <botan/rng.h>
15
#include <botan/strong_type.h>
16
#include <botan/version.h>
17
#include <botan/internal/algorithm_spec.h>
18
#include <botan/internal/bit_ops.h>
19
#include <botan/internal/calendar.h>
20
#include <botan/internal/charset.h>
21
#include <botan/internal/concat_util.h>
22
#include <botan/internal/ct_utils.h>
23
#include <botan/internal/fmt.h>
24
#include <botan/internal/int_utils.h>
25
#include <botan/internal/loadstor.h>
26
#include <botan/internal/parsing.h>
27
#include <botan/internal/target_info.h>
28

29
#include <bit>
30
#include <limits>
31

32
#if defined(BOTAN_HAS_CPUID)
33
   #include <botan/internal/cpuid.h>
34
#endif
35

36
#if defined(BOTAN_HAS_IPV4_ADDRESS)
37
   #include <botan/ipv4_address.h>
38
#endif
39

40
#if defined(BOTAN_HAS_IPV6_ADDRESS)
41
   #include <botan/ipv6_address.h>
42
#endif
43

44
#if defined(BOTAN_HAS_POLY_DBL)
45
   #include <botan/internal/poly_dbl.h>
46
#endif
47

48
#if defined(BOTAN_HAS_UUID)
49
   #include <botan/uuid.h>
50
#endif
51

52
namespace Botan_Tests {
53

54
namespace {
55

56
class Utility_Function_Tests final : public Test {
1 ✔
57
   public:
58
      std::vector<Test::Result> run() override {
1 ✔
59
         std::vector<Test::Result> results;
1 ✔
60

61
         results.push_back(test_checked_add());
2 ✔
62
         results.push_back(test_checked_mul());
2 ✔
63
         results.push_back(test_checked_cast());
2 ✔
64
         results.push_back(test_ceil_division());
2 ✔
65
         results.push_back(test_loadstore());
2 ✔
66
         results.push_back(test_loadstore_ambiguity());
2 ✔
67
         results.push_back(test_loadstore_fallback());
2 ✔
68
         results.push_back(test_loadstore_constexpr());
2 ✔
69
         return Botan::concat(results, test_copy_out_be_le());
3 ✔
70
      }
1 ✔
71

72
   private:
73
      Test::Result test_checked_add() {
1 ✔
74
         Test::Result result("checked_add");
1 ✔
75

76
         const size_t large = static_cast<size_t>(-5);
1 ✔
77
         const size_t zero = 0;
1 ✔
78

79
         for(int si = -15; si != 15; ++si) {
31 ✔
80
            const size_t i = static_cast<size_t>(si);
30 ✔
81
            auto sum1 = Botan::checked_add<size_t>(i, zero, zero, zero, large);
30 ✔
82
            const auto sum2 = Botan::checked_add<size_t>(large, zero, zero, zero, i);
30 ✔
83

84
            result.test_is_true("checked_add looks at all args", sum1 == sum2);
60 ✔
85

86
            if(i < 5) {
30 ✔
87
               result.test_sz_eq("checked_add worked", sum1.value(), i + large);
5 ✔
88
            } else {
89
               result.test_is_true("checked_add did not return a result", !sum1.has_value());
25 ✔
90
            }
91
         }
92

93
         auto& rng = Test::rng();
1 ✔
94

95
         for(size_t i = 0; i != 100; ++i) {
101 ✔
96
            const uint16_t x = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100 ✔
97
            const uint16_t y = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100 ✔
98

99
            const uint32_t ref = static_cast<uint32_t>(x) + y;
100 ✔
100

101
            if(auto z = Botan::checked_add(x, y)) {
200 ✔
102
               result.test_u32_eq("checked_add adds", static_cast<uint32_t>(z.value()), ref);
52 ✔
103
            } else {
104
               result.test_is_true("checked_add checks", (ref >> 16) > 0);
48 ✔
105
            }
106
         }
107

108
         return result;
1 ✔
109
      }
×
110

111
      Test::Result test_checked_mul() {
1 ✔
112
         Test::Result result("checked_mul");
1 ✔
113

114
         auto& rng = Test::rng();
1 ✔
115

116
         for(size_t i = 0; i != 100; ++i) {
101 ✔
117
            const uint16_t x = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100 ✔
118
            const uint16_t y = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100 ✔
119

120
            const uint32_t ref = static_cast<uint32_t>(x) * y;
100 ✔
121

122
            if(auto z = Botan::checked_mul(x, y)) {
200 ✔
123
               result.test_u32_eq("checked_mul multiplies", static_cast<uint32_t>(z.value()), ref);
×
124
            } else {
125
               result.test_is_true("checked_mul checks", (ref >> 16) > 0);
100 ✔
126
            }
127
         }
128

129
         return result;
1 ✔
130
      }
×
131

132
      Test::Result test_checked_cast() {
1 ✔
133
         Test::Result result("checked_cast");
1 ✔
134

135
         const uint32_t large = static_cast<uint32_t>(-1);
1 ✔
136
         const uint32_t is_16_bits = 0x8123;
1 ✔
137
         const uint32_t is_8_bits = 0x89;
1 ✔
138

139
         result.test_throws("checked_cast checks", [&] { Botan::checked_cast_to<uint16_t>(large); });
2 ✔
140
         result.test_throws("checked_cast checks", [&] { Botan::checked_cast_to<uint8_t>(large); });
2 ✔
141

142
         result.test_u32_eq("checked_cast converts", Botan::checked_cast_to<uint32_t>(large), large);
1 ✔
143
         result.test_u16_eq("checked_cast converts", Botan::checked_cast_to<uint16_t>(is_16_bits), 0x8123);
1 ✔
144
         result.test_u8_eq("checked_cast converts", Botan::checked_cast_to<uint8_t>(is_8_bits), 0x89);
1 ✔
145

146
         return result;
1 ✔
147
      }
×
148

149
      Test::Result test_ceil_division() {
1 ✔
150
         Test::Result result("Util ceil_division");
1 ✔
151

152
         for(size_t a = 0; a != 1000; ++a) {
1,001 ✔
153
            for(size_t b = 1; b != 70; ++b) {
70,000 ✔
154
               const size_t q = Botan::ceil_division(a, b);
69,000 ✔
155
               result.test_is_true("q*b >= a", q * b >= a);
69,000 ✔
156
               result.test_is_true("(q-1)*b < a", q == 0 || (q - 1) * b < a);
138,000 ✔
157
            }
158
         }
159

160
         // These would wrap if the computation added b - 1 to a
161
         const size_t max = std::numeric_limits<size_t>::max();
1 ✔
162
         result.test_sz_eq("ceil_division(max, 1)", Botan::ceil_division(max, size_t(1)), max);
1 ✔
163
         result.test_sz_eq("ceil_division(max, 2)", Botan::ceil_division(max, size_t(2)), max / 2 + 1);
1 ✔
164
         result.test_sz_eq("ceil_division(max - 1, 2)", Botan::ceil_division(max - 1, size_t(2)), max / 2);
1 ✔
165
         result.test_sz_eq("ceil_division(max, max - 1)", Botan::ceil_division(max, max - 1), 2);
1 ✔
166
         result.test_sz_eq("ceil_division(max, max)", Botan::ceil_division(max, max), 1);
1 ✔
167

168
         return result;
1 ✔
169
      }
×
170

171
      using TestInt64 = Botan::Strong<uint64_t, struct TestInt64_>;
172
      using TestInt32 = Botan::Strong<uint32_t, struct TestInt64_>;
173
      using TestVectorSink = Botan::Strong<std::vector<uint8_t>, struct TestVectorSink_>;
174

175
      enum class TestEnum64 : uint64_t {
176
         _1 = 0x1234567890ABCDEF,
177
         _2 = 0xEFCDAB9078563412,
178
      };
179

180
      enum class TestEnum32 : uint32_t {
181
         _1 = 0x12345678,
182
         _2 = 0x78563412,
183
      };
184

185
      static Test::Result test_loadstore() {
1 ✔
186
         Test::Result result("Util load/store");
1 ✔
187

188
         const std::vector<uint8_t> membuf = Botan::hex_decode("00112233445566778899AABBCCDDEEFF");
1 ✔
189
         const uint8_t* mem = membuf.data();
1 ✔
190

191
         const uint16_t in16 = 0x1234;
1 ✔
192
         const uint32_t in32 = 0xA0B0C0D0;
1 ✔
193
         const uint64_t in64 = 0xABCDEF0123456789;
1 ✔
194

195
         result.test_u8_eq(Botan::get_byte<0>(in32), 0xA0);
1 ✔
196
         result.test_u8_eq(Botan::get_byte<1>(in32), 0xB0);
1 ✔
197
         result.test_u8_eq(Botan::get_byte<2>(in32), 0xC0);
1 ✔
198
         result.test_u8_eq(Botan::get_byte<3>(in32), 0xD0);
1 ✔
199

200
         result.test_u16_eq(Botan::make_uint16(0xAA, 0xBB), 0xAABB);
1 ✔
201
         result.test_u32_eq(Botan::make_uint32(0x01, 0x02, 0x03, 0x04), 0x01020304);
1 ✔
202

203
         result.test_u16_eq(Botan::load_be<uint16_t>(mem, 0), 0x0011);
1 ✔
204
         result.test_u16_eq(Botan::load_be<uint16_t>(mem, 1), 0x2233);
1 ✔
205
         result.test_u16_eq(Botan::load_be<uint16_t>(mem, 2), 0x4455);
1 ✔
206
         result.test_u16_eq(Botan::load_be<uint16_t>(mem, 3), 0x6677);
1 ✔
207

208
         result.test_u16_eq(Botan::load_le<uint16_t>(mem, 0), 0x1100);
1 ✔
209
         result.test_u16_eq(Botan::load_le<uint16_t>(mem, 1), 0x3322);
1 ✔
210
         result.test_u16_eq(Botan::load_le<uint16_t>(mem, 2), 0x5544);
1 ✔
211
         result.test_u16_eq(Botan::load_le<uint16_t>(mem, 3), 0x7766);
1 ✔
212

213
         result.test_u32_eq(Botan::load_be<uint32_t>(mem, 0), 0x00112233);
1 ✔
214
         result.test_u32_eq(Botan::load_be<uint32_t>(mem, 1), 0x44556677);
1 ✔
215
         result.test_u32_eq(Botan::load_be<uint32_t>(mem, 2), 0x8899AABB);
1 ✔
216
         result.test_u32_eq(Botan::load_be<uint32_t>(mem, 3), 0xCCDDEEFF);
1 ✔
217

218
         result.test_u32_eq(Botan::load_le<uint32_t>(mem, 0), 0x33221100);
1 ✔
219
         result.test_u32_eq(Botan::load_le<uint32_t>(mem, 1), 0x77665544);
1 ✔
220
         result.test_u32_eq(Botan::load_le<uint32_t>(mem, 2), 0xBBAA9988);
1 ✔
221
         result.test_u32_eq(Botan::load_le<uint32_t>(mem, 3), 0xFFEEDDCC);
1 ✔
222

223
         result.test_u64_eq(Botan::load_be<uint64_t>(mem, 0), 0x0011223344556677);
1 ✔
224
         result.test_u64_eq(Botan::load_be<uint64_t>(mem, 1), 0x8899AABBCCDDEEFF);
1 ✔
225

226
         result.test_u64_eq(Botan::load_le<uint64_t>(mem, 0), 0x7766554433221100);
1 ✔
227
         result.test_u64_eq(Botan::load_le<uint64_t>(mem, 1), 0xFFEEDDCCBBAA9988);
1 ✔
228

229
         // Check misaligned loads:
230
         result.test_u16_eq(Botan::load_be<uint16_t>(mem + 1, 0), 0x1122);
1 ✔
231
         result.test_u16_eq(Botan::load_le<uint16_t>(mem + 3, 0), 0x4433);
1 ✔
232

233
         result.test_u32_eq(Botan::load_be<uint32_t>(mem + 1, 1), 0x55667788);
1 ✔
234
         result.test_u32_eq(Botan::load_le<uint32_t>(mem + 3, 1), 0xAA998877);
1 ✔
235

236
         result.test_u64_eq(Botan::load_be<uint64_t>(mem + 1, 0), 0x1122334455667788);
1 ✔
237
         result.test_u64_eq(Botan::load_le<uint64_t>(mem + 7, 0), 0xEEDDCCBBAA998877);
1 ✔
238
         result.test_u64_eq(Botan::load_le<uint64_t>(mem + 5, 0), 0xCCBBAA9988776655);
1 ✔
239

240
         uint8_t outbuf[16] = {0};
1 ✔
241

242
         for(size_t offset = 0; offset != 7; ++offset) {
8 ✔
243
            uint8_t* out = outbuf + offset;  // NOLINT(*-const-correctness) clang-tidy bug
7 ✔
244

245
            Botan::store_be(in16, out);
7 ✔
246
            result.test_u8_eq(out[0], 0x12);
7 ✔
247
            result.test_u8_eq(out[1], 0x34);
7 ✔
248

249
            Botan::store_le(in16, out);
7 ✔
250
            result.test_u8_eq(out[0], 0x34);
7 ✔
251
            result.test_u8_eq(out[1], 0x12);
7 ✔
252

253
            Botan::store_be(in32, out);
7 ✔
254
            result.test_u8_eq(out[0], 0xA0);
7 ✔
255
            result.test_u8_eq(out[1], 0xB0);
7 ✔
256
            result.test_u8_eq(out[2], 0xC0);
7 ✔
257
            result.test_u8_eq(out[3], 0xD0);
7 ✔
258

259
            Botan::store_le(in32, out);
7 ✔
260
            result.test_u8_eq(out[0], 0xD0);
7 ✔
261
            result.test_u8_eq(out[1], 0xC0);
7 ✔
262
            result.test_u8_eq(out[2], 0xB0);
7 ✔
263
            result.test_u8_eq(out[3], 0xA0);
7 ✔
264

265
            Botan::store_be(in64, out);
7 ✔
266
            result.test_u8_eq(out[0], 0xAB);
7 ✔
267
            result.test_u8_eq(out[1], 0xCD);
7 ✔
268
            result.test_u8_eq(out[2], 0xEF);
7 ✔
269
            result.test_u8_eq(out[3], 0x01);
7 ✔
270
            result.test_u8_eq(out[4], 0x23);
7 ✔
271
            result.test_u8_eq(out[5], 0x45);
7 ✔
272
            result.test_u8_eq(out[6], 0x67);
7 ✔
273
            result.test_u8_eq(out[7], 0x89);
7 ✔
274

275
            Botan::store_le(in64, out);
7 ✔
276
            result.test_u8_eq(out[0], 0x89);
7 ✔
277
            result.test_u8_eq(out[1], 0x67);
7 ✔
278
            result.test_u8_eq(out[2], 0x45);
7 ✔
279
            result.test_u8_eq(out[3], 0x23);
7 ✔
280
            result.test_u8_eq(out[4], 0x01);
7 ✔
281
            result.test_u8_eq(out[5], 0xEF);
7 ✔
282
            result.test_u8_eq(out[6], 0xCD);
7 ✔
283
            result.test_u8_eq(out[7], 0xAB);
7 ✔
284
         }
285

286
         std::array<uint8_t, 8> outarr{};
1 ✔
287
         uint16_t i0 = 0;
1 ✔
288
         uint16_t i1 = 0;
1 ✔
289
         uint16_t i2 = 0;
1 ✔
290
         uint16_t i3 = 0;
1 ✔
291
         Botan::store_be(in64, outarr);
1 ✔
292

293
         Botan::load_be(outarr, i0, i1, i2, i3);
1 ✔
294
         result.test_u16_eq(i0, 0xABCD);
1 ✔
295
         result.test_u16_eq(i1, 0xEF01);
1 ✔
296
         result.test_u16_eq(i2, 0x2345);
1 ✔
297
         result.test_u16_eq(i3, 0x6789);
1 ✔
298

299
         Botan::load_le(std::span{outarr}.first<6>(), i0, i1, i2);
1 ✔
300
         result.test_u16_eq(i0, 0xCDAB);
1 ✔
301
         result.test_u16_eq(i1, 0x01EF);
1 ✔
302
         result.test_u16_eq(i2, 0x4523);
1 ✔
303
         result.test_u16_eq(i3, 0x6789);  // remains unchanged
1 ✔
304

305
         Botan::store_le(in64, outarr);
1 ✔
306

307
         Botan::load_le(outarr, i0, i1, i2, i3);
1 ✔
308
         result.test_u16_eq(i0, 0x6789);
1 ✔
309
         result.test_u16_eq(i1, 0x2345);
1 ✔
310
         result.test_u16_eq(i2, 0xEF01);
1 ✔
311
         result.test_u16_eq(i3, 0xABCD);
1 ✔
312

313
         Botan::load_be(std::span{outarr}.first<6>(), i0, i1, i2);
1 ✔
314
         result.test_u16_eq(i0, 0x8967);
1 ✔
315
         result.test_u16_eq(i1, 0x4523);
1 ✔
316
         result.test_u16_eq(i2, 0x01EF);
1 ✔
317
         result.test_u16_eq(i3, 0xABCD);  // remains unchanged
1 ✔
318

319
         i0 = 0xAA11;
1 ✔
320
         i1 = 0xBB22;
1 ✔
321
         i2 = 0xCC33;
1 ✔
322
         i3 = 0xDD44;
1 ✔
323
         Botan::store_be(outarr, i0, i1, i2, i3);
1 ✔
324
         result.test_bin_eq("store_be", outarr, "AA11BB22CC33DD44");
1 ✔
325
         std::vector<uint8_t> outvec(8);
1 ✔
326
         Botan::store_be(outvec, i0, i1, i2, i3);
1 ✔
327
         result.test_bin_eq("store_be", outvec, "AA11BB22CC33DD44");
1 ✔
328

329
         Botan::store_le(outarr, i0, i1, i2, i3);
1 ✔
330
         result.test_bin_eq("store_le(arr)", outarr, "11AA22BB33CC44DD");
1 ✔
331
         Botan::store_le(outvec, i0, i1, i2, i3);
1 ✔
332
         result.test_bin_eq("store_le", outvec, "11AA22BB33CC44DD");
1 ✔
333

334
#if !defined(BOTAN_TERMINATE_ON_ASSERTS)
335
         std::vector<uint8_t> sink56bits(7);
336
         std::vector<uint8_t> sink72bits(9);
337
         result.test_throws("store_le with a buffer that is too small",
338
                            [&] { Botan::store_le(sink56bits, i0, i1, i2, i3); });
339
         result.test_throws("store_le with a buffer that is too big",
340
                            [&] { Botan::store_le(sink72bits, i0, i1, i2, i3); });
341
         result.test_throws("store_be with a buffer that is too small",
342
                            [&] { Botan::store_be(sink56bits, i0, i1, i2, i3); });
343
         result.test_throws("store_be with a buffer that is too big",
344
                            [&] { Botan::store_be(sink72bits, i0, i1, i2, i3); });
345
#endif
346

347
         // can store multiple values straight into a collection
348
         auto out64_array_be = Botan::store_be(i0, i1, i2, i3);
1 ✔
349
         auto out64_vec_be = Botan::store_be<std::vector<uint8_t>>(i0, i1, i2, i3);
1 ✔
350
         auto out64_strong_be = Botan::store_be<TestVectorSink>(i0, i1, i2, i3);
1 ✔
351
         result.test_bin_eq("store_be(arr)", out64_array_be, "AA11BB22CC33DD44");
1 ✔
352
         result.test_bin_eq("store_be(vec)", out64_vec_be, "AA11BB22CC33DD44");
1 ✔
353
         result.test_bin_eq("store_be(strong)", out64_strong_be, "AA11BB22CC33DD44");
1 ✔
354
         auto out64_array_le = Botan::store_le(i0, i1, i2, i3);
1 ✔
355
         auto out64_vec_le = Botan::store_le<std::vector<uint8_t>>(i0, i1, i2, i3);
1 ✔
356
         auto out64_strong_le = Botan::store_le<TestVectorSink>(i0, i1, i2, i3);
1 ✔
357
         result.test_bin_eq("store_le(arr)", out64_array_le, "11AA22BB33CC44DD");
1 ✔
358
         result.test_bin_eq("store_le(vec)", out64_vec_le, "11AA22BB33CC44DD");
1 ✔
359
         result.test_bin_eq("store_le(strong)", out64_strong_le, "11AA22BB33CC44DD");
1 ✔
360

361
         result.test_u16_eq(in16, Botan::load_be(Botan::store_be(in16)));
1 ✔
362
         result.test_u32_eq(in32, Botan::load_be(Botan::store_be(in32)));
1 ✔
363
         result.test_u64_eq(in64, Botan::load_be(Botan::store_be(in64)));
1 ✔
364

365
         result.test_u16_eq(in16, Botan::load_le(Botan::store_le(in16)));
1 ✔
366
         result.test_u32_eq(in32, Botan::load_le(Botan::store_le(in32)));
1 ✔
367
         result.test_u64_eq(in64, Botan::load_le(Botan::store_le(in64)));
1 ✔
368

369
         // Test that the runtime detects incompatible range sizes
370
#if !defined(BOTAN_TERMINATE_ON_ASSERTS)
371
         std::vector<uint16_t> too_big16(4);
372
         std::vector<uint16_t> too_small16(1);
373
         result.test_throws("load_le with incompatible buffers",
374
                            [&] { Botan::load_le(too_big16, Botan::hex_decode("BAADB00B")); });
375
         result.test_throws("load_le with incompatible buffers",
376
                            [&] { Botan::load_le(too_small16, Botan::hex_decode("BAADB00B")); });
377
         result.test_throws("load_be with incompatible buffers",
378
                            [&] { Botan::load_be(too_big16, Botan::hex_decode("BAADB00B")); });
379
         result.test_throws("load_be with incompatible buffers",
380
                            [&] { Botan::load_be(too_small16, Botan::hex_decode("BAADB00B")); });
381

382
         std::vector<uint8_t> too_big8(4);
383
         std::vector<uint8_t> too_small8(1);
384
         result.test_throws("store_le with incompatible buffers",
385
                            [&] { Botan::store_le(too_big8, std::array<uint16_t, 1>{}); });
386
         result.test_throws("store_le with incompatible buffers",
387
                            [&] { Botan::store_le(too_small8, std::array<uint16_t, 1>{}); });
388
         result.test_throws("store_be with incompatible buffers",
389
                            [&] { Botan::store_be(too_big8, std::array<uint16_t, 1>{}); });
390
         result.test_throws("store_be with incompatible buffers",
391
                            [&] { Botan::store_be(too_small8, std::array<uint16_t, 1>{}); });
392
#endif
393

394
         // Test store of entire ranges
395
         const std::array<uint16_t, 2> in16_array = {0x0A0B, 0x0C0D};
1 ✔
396
         result.test_bin_eq("store_be(vec)", Botan::store_be<std::vector<uint8_t>>(in16_array), "0A0B0C0D");
1 ✔
397
         result.test_bin_eq("store_le(vec)", Botan::store_le<std::vector<uint8_t>>(in16_array), "0B0A0D0C");
1 ✔
398

399
         const std::vector<uint16_t> in16_vector = {0x0A0B, 0x0C0D};
1 ✔
400
         result.test_bin_eq("store_be(vec)", Botan::store_be<std::vector<uint8_t>>(in16_vector), "0A0B0C0D");
1 ✔
401
         result.test_bin_eq("store_le(vec)", Botan::store_le<std::vector<uint8_t>>(in16_vector), "0B0A0D0C");
1 ✔
402

403
         std::array<uint8_t, 4> out_array{};
1 ✔
404
         Botan::store_be(out_array, in16_array);
1 ✔
405
         result.test_bin_eq("store_be(arr)", out_array, "0A0B0C0D");
1 ✔
406
         Botan::store_le(out_array, in16_array);
1 ✔
407
         result.test_bin_eq("store_le(arr)", out_array, "0B0A0D0C");
1 ✔
408

409
         const auto be_inferred = Botan::store_be(in16_array);
1 ✔
410
         result.test_bin_eq("store_be(arr)", be_inferred, "0A0B0C0D");
1 ✔
411
         const auto le_inferred = Botan::store_le(in16_array);
1 ✔
412
         result.test_bin_eq("store_le(arr)", le_inferred, "0B0A0D0C");
1 ✔
413

414
         // Test load of entire ranges
415
         const auto in_buffer = Botan::hex_decode("AABBCCDD");
1 ✔
416
         auto out16_array_be = Botan::load_be<std::array<uint16_t, 2>>(in_buffer);
1 ✔
417
         result.test_u16_eq(out16_array_be[0], 0xAABB);
1 ✔
418
         result.test_u16_eq(out16_array_be[1], 0xCCDD);
1 ✔
419
         auto out16_vec_be = Botan::load_be<std::vector<uint16_t>>(in_buffer);
1 ✔
420
         result.test_sz_eq("be-vector has expected size", out16_vec_be.size(), 2);
1 ✔
421
         result.test_u16_eq(out16_vec_be[0], 0xAABB);
1 ✔
422
         result.test_u16_eq(out16_vec_be[1], 0xCCDD);
1 ✔
423

424
         auto out16_array_le = Botan::load_le<std::array<uint16_t, 2>>(in_buffer);
1 ✔
425
         result.test_u16_eq(out16_array_le[0], 0xBBAA);
1 ✔
426
         result.test_u16_eq(out16_array_le[1], 0xDDCC);
1 ✔
427
         auto out16_vec_le = Botan::load_le<Botan::secure_vector<uint16_t>>(in_buffer);
1 ✔
428
         result.test_sz_eq("le-vector has expected size", out16_vec_be.size(), 2);
1 ✔
429
         result.test_u16_eq(out16_vec_le[0], 0xBBAA);
1 ✔
430
         result.test_u16_eq(out16_vec_le[1], 0xDDCC);
1 ✔
431

432
         // Test loading/storing of strong type integers
433
         const TestInt64 in64_strong{0xABCDEF0123456789};
1 ✔
434
         const TestInt32 in32_strong{0xABCDEF01};
1 ✔
435

436
         result.test_bin_eq(
1 ✔
437
            "store_be(u64,strong)", Botan::store_be<std::vector<uint8_t>>(in64_strong), "ABCDEF0123456789");
×
438
         result.test_bin_eq(
1 ✔
439
            "store_le(u64,strong)", Botan::store_le<std::vector<uint8_t>>(in64_strong), "8967452301EFCDAB");
×
440
         result.test_bin_eq("store_be(u32,strong)", Botan::store_be<std::vector<uint8_t>>(in32_strong), "ABCDEF01");
1 ✔
441
         result.test_bin_eq("store_le(u32,strong)", Botan::store_le<std::vector<uint8_t>>(in32_strong), "01EFCDAB");
1 ✔
442

443
         test_arb_eq(
2 ✔
444
            result, "load_be(strong64)", Botan::load_be<TestInt64>(Botan::hex_decode("ABCDEF0123456789")), in64_strong);
2 ✔
445
         test_arb_eq(
2 ✔
446
            result, "load_le(strong64)", Botan::load_le<TestInt64>(Botan::hex_decode("8967452301EFCDAB")), in64_strong);
2 ✔
447
         test_arb_eq(
2 ✔
448
            result, "load_be(strong32)", Botan::load_be<TestInt32>(Botan::hex_decode("ABCDEF01")), in32_strong);
2 ✔
449
         test_arb_eq(
2 ✔
450
            result, "load_le(strong32)", Botan::load_le<TestInt32>(Botan::hex_decode("01EFCDAB")), in32_strong);
2 ✔
451

452
         const std::vector<TestInt64> some_in64_strongs{TestInt64{0xABCDEF0123456789}, TestInt64{0x0123456789ABCDEF}};
1 ✔
453
         result.test_bin_eq("store_be(vector,strong)",
1 ✔
454
                            Botan::store_be<std::vector<uint8_t>>(some_in64_strongs),
×
455
                            "ABCDEF01234567890123456789ABCDEF");
456
         result.test_bin_eq("store_le(vector,strong)",
1 ✔
457
                            Botan::store_le<std::vector<uint8_t>>(some_in64_strongs),
×
458
                            "8967452301EFCDABEFCDAB8967452301");
459

460
         const auto in64_strongs_le =
1 ✔
461
            Botan::load_le<std::array<TestInt64, 2>>(Botan::hex_decode("8967452301EFCDABEFCDAB8967452301"));
2 ✔
462
         test_arb_eq(result, "load_le(strong arr)", in64_strongs_le[0], TestInt64{0xABCDEF0123456789});
1 ✔
463
         test_arb_eq(result, "load_le(strong arr)", in64_strongs_le[1], TestInt64{0x0123456789ABCDEF});
1 ✔
464

465
         const auto in64_strongs_be =
1 ✔
466
            Botan::load_be<std::vector<TestInt64>>(Botan::hex_decode("ABCDEF01234567890123456789ABCDEF"));
2 ✔
467
         test_arb_eq(result, "load_be(strong arr)", in64_strongs_be[0], TestInt64{0xABCDEF0123456789});
1 ✔
468
         test_arb_eq(result, "load_be(strong arr)", in64_strongs_be[1], TestInt64{0x0123456789ABCDEF});
1 ✔
469

470
         // Test loading/storing of enum types with different endianness
471
         const auto in64_enum_le = Botan::load_le<TestEnum64>(Botan::hex_decode("1234567890ABCDEF"));
2 ✔
472
         result.test_enum_eq("load_le(enum64)", in64_enum_le, TestEnum64::_2);
1 ✔
473
         const auto in64_enum_be = Botan::load_be<TestEnum64>(Botan::hex_decode("1234567890ABCDEF"));
2 ✔
474
         result.test_enum_eq("load_be(enum64)", in64_enum_be, TestEnum64::_1);
1 ✔
475
         result.test_bin_eq(
1 ✔
476
            "store_be(enum64)", Botan::store_le<std::vector<uint8_t>>(TestEnum64::_1), "EFCDAB9078563412");
1 ✔
477
         result.test_bin_eq("store_be(enum64)", Botan::store_be(TestEnum64::_2), "EFCDAB9078563412");
1 ✔
478

479
         const auto in32_enum_le = Botan::load_le<TestEnum32>(Botan::hex_decode("78563412"));
2 ✔
480
         result.test_enum_eq("load_le(enum32)", in32_enum_le, TestEnum32::_1);
1 ✔
481
         const auto in32_enum_be = Botan::load_be<TestEnum32>(Botan::hex_decode("78563412"));
2 ✔
482
         result.test_enum_eq("load_be(enum32)", in32_enum_be, TestEnum32::_2);
1 ✔
483
         result.test_bin_eq("store_le(enum32)", Botan::store_le<std::vector<uint8_t>>(TestEnum32::_1), "78563412");
1 ✔
484
         result.test_bin_eq("store_be(enum32)", Botan::store_be(TestEnum32::_2), "78563412");
1 ✔
485

486
         return result;
2 ✔
487
      }
10 ✔
488

489
      template <std::unsigned_integral T>
490
      static T fb_load_be(std::array<const uint8_t, sizeof(T)> in) {
3 ✔
491
         return Botan::detail::fallback_load_any<std::endian::big, T>(in);
1 ✔
492
      }
493

494
      template <std::unsigned_integral T>
495
      static T fb_load_le(std::array<const uint8_t, sizeof(T)> in) {
3 ✔
496
         return Botan::detail::fallback_load_any<std::endian::little, T>(in);
1 ✔
497
      }
498

499
      template <std::unsigned_integral T>
500
      static decltype(auto) fb_store_be(const T in) {
3 ✔
501
         std::array<uint8_t, sizeof(T)> out{};
2 ✔
502
         Botan::detail::fallback_store_any<std::endian::big, T>(in, out);
1 ✔
503
         return out;
2 ✔
504
      }
505

506
      template <std::unsigned_integral T>
507
      static decltype(auto) fb_store_le(const T in) {
3 ✔
508
         std::array<uint8_t, sizeof(T)> out{};
2 ✔
509
         Botan::detail::fallback_store_any<std::endian::little, T>(in, out);
1 ✔
510
         return out;
2 ✔
511
      }
512

513
      static Test::Result test_loadstore_ambiguity() {
1 ✔
514
         // This is a regression test for a (probable) compiler bug in Xcode 15
515
         // where it would fail to compile the load/store functions for size_t
516
         //
517
         // It seems that this platform defines uint64_t as "unsigned long long"
518
         // and size_t as "unsigned long". Both are 64-bits but the compiler
519
         // was unable to disambiguate the two in reverse_bytes in bswap.h
520

521
         const uint32_t in32 = 0x01234567;
1 ✔
522
         const uint64_t in64 = 0x0123456789ABCDEF;
1 ✔
523
         const size_t inszt = 0x87654321;
1 ✔
524

525
         Test::Result result("Util load/store ambiguity");
1 ✔
526
         const auto out_be_32 = Botan::store_be(in32);
1 ✔
527
         const auto out_le_32 = Botan::store_le(in32);
1 ✔
528
         const auto out_be_64 = Botan::store_be(in64);
1 ✔
529
         const auto out_le_64 = Botan::store_le(in64);
1 ✔
530
         const auto out_be_szt = Botan::store_be(inszt);
1 ✔
531
         const auto out_le_szt = Botan::store_le(inszt);
1 ✔
532

533
         result.test_u32_eq("be 32", Botan::load_be<uint32_t>(out_be_32), in32);
1 ✔
534
         result.test_u32_eq("le 32", Botan::load_le<uint32_t>(out_le_32), in32);
1 ✔
535
         result.test_u64_eq("be 64", Botan::load_be<uint64_t>(out_be_64), in64);
1 ✔
536
         result.test_u64_eq("le 64", Botan::load_le<uint64_t>(out_le_64), in64);
1 ✔
537
         result.test_sz_eq("be sz", Botan::load_be<size_t>(out_be_szt), inszt);
1 ✔
538
         result.test_sz_eq("le sz", Botan::load_le<size_t>(out_le_szt), inszt);
1 ✔
539

540
         return result;
1 ✔
541
      }
×
542

543
      static Test::Result test_loadstore_fallback() {
1 ✔
544
         // The fallback implementation is only used if we don't know the
545
         // endianness of the target at compile time. This makes sure that the
546
         // fallback implementation is correct. On all typical platforms it
547
         // won't be called in production.
548
         Test::Result result("Util load/store fallback");
1 ✔
549

550
         result.test_u16_eq("lLE 16", fb_load_le<uint16_t>({1, 2}), 0x0201);
1 ✔
551
         result.test_u32_eq("lLE 32", fb_load_le<uint32_t>({1, 2, 3, 4}), 0x04030201);
1 ✔
552
         result.test_u64_eq("lLE 64", fb_load_le<uint64_t>({1, 2, 3, 4, 5, 6, 7, 8}), 0x0807060504030201);
1 ✔
553

554
         result.test_u16_eq("lBE 16", fb_load_be<uint16_t>({1, 2}), 0x0102);
1 ✔
555
         result.test_u32_eq("lBE 32", fb_load_be<uint32_t>({1, 2, 3, 4}), 0x01020304);
1 ✔
556
         result.test_u64_eq("lBE 64", fb_load_be<uint64_t>({1, 2, 3, 4, 5, 6, 7, 8}), 0x0102030405060708);
1 ✔
557

558
         result.test_bin_eq("sLE 16", fb_store_le<uint16_t>(0x0201), "0102");
1 ✔
559
         result.test_bin_eq("sLE 32", fb_store_le<uint32_t>(0x04030201), "01020304");
1 ✔
560
         result.test_bin_eq("sLE 64", fb_store_le<uint64_t>(0x0807060504030201), "0102030405060708");
1 ✔
561

562
         result.test_bin_eq("sBE 16", fb_store_be<uint16_t>(0x0102), "0102");
1 ✔
563
         result.test_bin_eq("sBE 32", fb_store_be<uint32_t>(0x01020304), "01020304");
1 ✔
564
         result.test_bin_eq("sBE 64", fb_store_be<uint64_t>(0x0102030405060708), "0102030405060708");
1 ✔
565

566
         return result;
1 ✔
567
      }
×
568

569
      static Test::Result test_loadstore_constexpr() {
1 ✔
570
         Test::Result result("Util load/store constexpr");
1 ✔
571

572
         constexpr uint16_t in16 = 0x1234;
1 ✔
573
         constexpr uint32_t in32 = 0xA0B0C0D0;
1 ✔
574
         constexpr uint64_t in64 = 0xABCDEF0123456789;
1 ✔
575

576
         // clang-format off
577
         constexpr std::array<uint8_t, 16> cex_mem = {
1 ✔
578
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
579
         };
580
         // clang-format on
581

582
         // get_byte<> w/ 16bit
583
         constexpr auto cex_byte_16_0 = Botan::get_byte<0>(in16);
1 ✔
584
         result.test_u8_eq(cex_byte_16_0, 0x12);
1 ✔
585
         constexpr auto cex_byte_16_1 = Botan::get_byte<1>(in16);
1 ✔
586
         result.test_u8_eq(cex_byte_16_1, 0x34);
1 ✔
587

588
         // get_byte<> w/ 32bit
589
         constexpr auto cex_byte_32_0 = Botan::get_byte<0>(in32);
1 ✔
590
         result.test_u8_eq(cex_byte_32_0, 0xA0);
1 ✔
591
         constexpr auto cex_byte_32_1 = Botan::get_byte<1>(in32);
1 ✔
592
         result.test_u8_eq(cex_byte_32_1, 0xB0);
1 ✔
593
         constexpr auto cex_byte_32_2 = Botan::get_byte<2>(in32);
1 ✔
594
         result.test_u8_eq(cex_byte_32_2, 0xC0);
1 ✔
595
         constexpr auto cex_byte_32_3 = Botan::get_byte<3>(in32);
1 ✔
596
         result.test_u8_eq(cex_byte_32_3, 0xD0);
1 ✔
597

598
         // get_byte<> w/ 64bit
599
         constexpr auto cex_byte_64_0 = Botan::get_byte<0>(in64);
1 ✔
600
         result.test_u8_eq(cex_byte_64_0, 0xAB);
1 ✔
601
         constexpr auto cex_byte_64_1 = Botan::get_byte<1>(in64);
1 ✔
602
         result.test_u8_eq(cex_byte_64_1, 0xCD);
1 ✔
603
         constexpr auto cex_byte_64_2 = Botan::get_byte<2>(in64);
1 ✔
604
         result.test_u8_eq(cex_byte_64_2, 0xEF);
1 ✔
605
         constexpr auto cex_byte_64_3 = Botan::get_byte<3>(in64);
1 ✔
606
         result.test_u8_eq(cex_byte_64_3, 0x01);
1 ✔
607
         constexpr auto cex_byte_64_4 = Botan::get_byte<4>(in64);
1 ✔
608
         result.test_u8_eq(cex_byte_64_4, 0x23);
1 ✔
609
         constexpr auto cex_byte_64_5 = Botan::get_byte<5>(in64);
1 ✔
610
         result.test_u8_eq(cex_byte_64_5, 0x45);
1 ✔
611
         constexpr auto cex_byte_64_6 = Botan::get_byte<6>(in64);
1 ✔
612
         result.test_u8_eq(cex_byte_64_6, 0x67);
1 ✔
613
         constexpr auto cex_byte_64_7 = Botan::get_byte<7>(in64);
1 ✔
614
         result.test_u8_eq(cex_byte_64_7, 0x89);
1 ✔
615

616
         // make_uintXX()
617
         constexpr auto cex_uint16_t = Botan::make_uint16(0x12, 0x34);
1 ✔
618
         result.test_u16_eq(cex_uint16_t, in16);
1 ✔
619
         constexpr auto cex_uint32_t = Botan::make_uint32(0xA0, 0xB0, 0xC0, 0xD0);
1 ✔
620
         result.test_u32_eq(cex_uint32_t, in32);
1 ✔
621
         constexpr auto cex_uint64_t = Botan::make_uint64(0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89);
1 ✔
622
         result.test_u64_eq(cex_uint64_t, in64);
1 ✔
623

624
         // store_le/be with a single integer
625
         constexpr std::array<uint8_t, 2> cex_store_le16 = Botan::store_le(in16);
1 ✔
626
         result.test_bin_eq("store_le(u16 arr)", cex_store_le16, "3412");
1 ✔
627
         constexpr std::array<uint8_t, 4> cex_store_le32 = Botan::store_le(in32);
1 ✔
628
         result.test_bin_eq("store_le(u32 arr)", cex_store_le32, "D0C0B0A0");
1 ✔
629
         constexpr std::array<uint8_t, 8> cex_store_le64 = Botan::store_le(in64);
1 ✔
630
         result.test_bin_eq("store_le(u32,arr)", cex_store_le64, "8967452301EFCDAB");
1 ✔
631

632
         constexpr std::array<uint8_t, 2> cex_store_be16 = Botan::store_be(in16);
1 ✔
633
         result.test_bin_eq("store_be(u16 arr)", cex_store_be16, "1234");
1 ✔
634
         constexpr std::array<uint8_t, 4> cex_store_be32 = Botan::store_be(in32);
1 ✔
635
         result.test_bin_eq("store_be(u32 arr)", cex_store_be32, "A0B0C0D0");
1 ✔
636
         constexpr std::array<uint8_t, 8> cex_store_be64 = Botan::store_be(in64);
1 ✔
637
         result.test_bin_eq("store_be(u64 arr)", cex_store_be64, "ABCDEF0123456789");
1 ✔
638

639
         // store_le/be with multiple integers, both as a parameter pack and a range (std::array for constexpr)
640
         constexpr std::array<uint8_t, 16> cex_store_le16s =
1 ✔
641
            Botan::store_le(in16, in16, in16, in16, in16, in16, in16, in16);
642
         constexpr std::array<uint8_t, 16> cex_store_le16s2 =
1 ✔
643
            Botan::store_le(std::array{in16, in16, in16, in16, in16, in16, in16, in16});
644
         result.test_bin_eq("store_le", cex_store_le16s, "34123412341234123412341234123412");
1 ✔
645
         result.test_bin_eq("cex_store_le16s", cex_store_le16s, cex_store_le16s2);
1 ✔
646
         constexpr std::array<uint8_t, 16> cex_store_le32s = Botan::store_le(in32, in32, in32, in32);
1 ✔
647
         constexpr std::array<uint8_t, 16> cex_store_le32s2 = Botan::store_le(std::array{in32, in32, in32, in32});
1 ✔
648
         result.test_bin_eq("cex_store_le32s", cex_store_le32s, "D0C0B0A0D0C0B0A0D0C0B0A0D0C0B0A0");
1 ✔
649
         result.test_bin_eq("cex_store_le32s2", cex_store_le32s, cex_store_le32s2);
1 ✔
650
         constexpr std::array<uint8_t, 16> cex_store_le64s = Botan::store_le(in64, in64);
1 ✔
651
         constexpr std::array<uint8_t, 16> cex_store_le64s2 = Botan::store_le(std::array{in64, in64});
1 ✔
652
         result.test_bin_eq("cex_store_le64s", cex_store_le64s, "8967452301EFCDAB8967452301EFCDAB");
1 ✔
653
         result.test_bin_eq("cex_store_le64s2", cex_store_le64s, cex_store_le64s2);
1 ✔
654

655
         constexpr std::array<uint8_t, 16> cex_store_be16s =
1 ✔
656
            Botan::store_be(in16, in16, in16, in16, in16, in16, in16, in16);
657
         constexpr std::array<uint8_t, 16> cex_store_be16s2 =
1 ✔
658
            Botan::store_be(std::array{in16, in16, in16, in16, in16, in16, in16, in16});
659
         result.test_bin_eq("cex_store_be16s", cex_store_be16s, "12341234123412341234123412341234");
1 ✔
660
         result.test_bin_eq("cex_store_be16s2", cex_store_be16s, cex_store_be16s2);
1 ✔
661
         constexpr std::array<uint8_t, 16> cex_store_be32s = Botan::store_be(in32, in32, in32, in32);
1 ✔
662
         constexpr std::array<uint8_t, 16> cex_store_be32s2 = Botan::store_be(std::array{in32, in32, in32, in32});
1 ✔
663
         result.test_bin_eq("cex_store_be32s", cex_store_be32s, "A0B0C0D0A0B0C0D0A0B0C0D0A0B0C0D0");
1 ✔
664
         result.test_bin_eq("cex_store_be32s2", cex_store_be32s, cex_store_be32s2);
1 ✔
665
         constexpr std::array<uint8_t, 16> cex_store_be64s = Botan::store_be(in64, in64);
1 ✔
666
         constexpr std::array<uint8_t, 16> cex_store_be64s2 = Botan::store_be(std::array{in64, in64});
1 ✔
667
         result.test_bin_eq("cex_store_be64s", cex_store_be64s, "ABCDEF0123456789ABCDEF0123456789");
1 ✔
668
         result.test_bin_eq("cex_store_be64s2", cex_store_be64s, cex_store_be64s2);
1 ✔
669

670
         // load_le/be a single integer
671
         constexpr uint16_t cex_load_le16 = Botan::load_le<uint16_t>(cex_store_le16);
1 ✔
672
         result.test_u16_eq(cex_load_le16, in16);
1 ✔
673
         constexpr uint32_t cex_load_le32 = Botan::load_le<uint32_t>(cex_store_le32);
1 ✔
674
         result.test_u32_eq(cex_load_le32, in32);
1 ✔
675
         constexpr uint64_t cex_load_le64 = Botan::load_le<uint64_t>(cex_store_le64);
1 ✔
676
         result.test_u64_eq(cex_load_le64, in64);
1 ✔
677

678
         constexpr uint16_t cex_load_be16 = Botan::load_be<uint16_t>(cex_store_be16);
1 ✔
679
         result.test_u16_eq(cex_load_be16, in16);
1 ✔
680
         constexpr uint32_t cex_load_be32 = Botan::load_be<uint32_t>(cex_store_be32);
1 ✔
681
         result.test_u32_eq(cex_load_be32, in32);
1 ✔
682
         constexpr uint64_t cex_load_be64 = Botan::load_be<uint64_t>(cex_store_be64);
1 ✔
683
         result.test_u64_eq(cex_load_be64, in64);
1 ✔
684

685
         // load_le/be multiple integers into a std::array for constexpr
686
         constexpr auto cex_load_le16s = Botan::load_le<std::array<uint16_t, cex_mem.size() / 2>>(cex_mem);
1 ✔
687
         test_arb_eq(result,
1 ✔
688
                     "constexpr load_le(u16)",
689
                     cex_load_le16s,
690
                     {0x1100, 0x3322, 0x5544, 0x7766, 0x9988, 0xBBAA, 0xDDCC, 0xFFEE});
691
         constexpr auto cex_load_le32s = Botan::load_le<std::array<uint32_t, cex_mem.size() / 4>>(cex_mem);
1 ✔
692
         test_arb_eq(
1 ✔
693
            result, "constexpr load_le(u32)", cex_load_le32s, {0x33221100, 0x77665544, 0xBBAA9988, 0xFFEEDDCC});
694
         constexpr auto cex_load_le64s = Botan::load_le<std::array<uint64_t, cex_mem.size() / 8>>(cex_mem);
1 ✔
695
         test_arb_eq(result, "constexpr load_le(u64)", cex_load_le64s, {0x7766554433221100, 0xFFEEDDCCBBAA9988});
1 ✔
696

697
         constexpr auto cex_load_be16s = Botan::load_be<std::array<uint16_t, cex_mem.size() / 2>>(cex_mem);
1 ✔
698
         test_arb_eq(result,
1 ✔
699
                     "constexpr load_be(u16)",
700
                     cex_load_be16s,
701
                     {0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xAABB, 0xCCDD, 0xEEFF});
702
         constexpr auto cex_load_be32s = Botan::load_be<std::array<uint32_t, cex_mem.size() / 4>>(cex_mem);
1 ✔
703
         test_arb_eq(
1 ✔
704
            result, "constexpr load_be(u32)", cex_load_be32s, {0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF});
705
         constexpr auto cex_load_be64s = Botan::load_be<std::array<uint64_t, cex_mem.size() / 8>>(cex_mem);
1 ✔
706
         test_arb_eq(result, "constexpr load_be(u64)", cex_load_be64s, {0x0011223344556677, 0x8899AABBCCDDEEFF});
1 ✔
707

708
         return result;
1 ✔
709
      }
×
710

711
      static std::vector<Test::Result> test_copy_out_be_le() {
1 ✔
712
         return {
1 ✔
713
            CHECK("copy_out_be with 16bit input (word aligned)",
714
                  [&](auto& result) {
1 ✔
715
                     std::vector<uint8_t> out_vector(4);
1 ✔
716
                     const std::array<uint16_t, 2> in_array = {0x0A0B, 0x0C0D};
1 ✔
717
                     Botan::copy_out_be(out_vector, in_array);
1 ✔
718
                     result.test_bin_eq("copy_out_be", out_vector, "0A0B0C0D");
1 ✔
719
                  }),
1 ✔
720

721
            CHECK("copy_out_be with 16bit input (partial words)",
722
                  [&](auto& result) {
1 ✔
723
                     std::vector<uint8_t> out_vector(3);
1 ✔
724
                     const std::array<uint16_t, 2> in_array = {0x0A0B, 0x0C0D};
1 ✔
725
                     Botan::copy_out_be(out_vector, in_array);
1 ✔
726
                     result.test_bin_eq("copy_out_be(u16)", out_vector, "0A0B0C");
1 ✔
727
                  }),
1 ✔
728

729
            CHECK("copy_out_le with 16bit input (word aligned)",
730
                  [&](auto& result) {
1 ✔
731
                     std::vector<uint8_t> out_vector(4);
1 ✔
732
                     const std::array<uint16_t, 2> in_array = {0x0A0B, 0x0C0D};
1 ✔
733
                     Botan::copy_out_le(out_vector, in_array);
1 ✔
734
                     result.test_bin_eq("copy_out_le(u16)", out_vector, "0B0A0D0C");
1 ✔
735
                  }),
1 ✔
736

737
            CHECK("copy_out_le with 16bit input (partial words)",
738
                  [&](auto& result) {
1 ✔
739
                     std::vector<uint8_t> out_vector(3);
1 ✔
740
                     const std::array<uint16_t, 2> in_array = {0x0A0B, 0x0C0D};
1 ✔
741
                     Botan::copy_out_le(out_vector, in_array);
1 ✔
742
                     result.test_bin_eq("copy_out_le(u16)", out_vector, "0B0A0D");
1 ✔
743
                  }),
1 ✔
744

745
            CHECK("copy_out_be with 64bit input (word aligned)",
746
                  [&](auto& result) {
1 ✔
747
                     std::vector<uint8_t> out_vector(16);
1 ✔
748
                     const std::array<uint64_t, 2> in_array = {0x0A0B0C0D0E0F1011, 0x1213141516171819};
1 ✔
749
                     Botan::copy_out_be(out_vector, in_array);
1 ✔
750
                     result.test_bin_eq("copy_out_be(u64)", out_vector, "0A0B0C0D0E0F10111213141516171819");
1 ✔
751
                  }),
1 ✔
752

753
            CHECK("copy_out_le with 64bit input (word aligned)",
754
                  [&](auto& result) {
1 ✔
755
                     std::vector<uint8_t> out_vector(16);
1 ✔
756
                     const std::array<uint64_t, 2> in_array = {0x0A0B0C0D0E0F1011, 0x1213141516171819};
1 ✔
757
                     Botan::copy_out_le(out_vector, in_array);
1 ✔
758
                     result.test_bin_eq("copy_out_le(u64)", out_vector, "11100F0E0D0C0B0A1918171615141312");
1 ✔
759
                  }),
1 ✔
760

761
            CHECK("copy_out_be with 64bit input (partial words)",
762
                  [&](auto& result) {
1 ✔
763
                     std::vector<uint8_t> out_vector(15);
1 ✔
764
                     const std::array<uint64_t, 2> in_array = {0x0A0B0C0D0E0F1011, 0x1213141516171819};
1 ✔
765
                     Botan::copy_out_be(out_vector, in_array);
1 ✔
766
                     result.test_bin_eq("copy_out_be(u64)", out_vector, "0A0B0C0D0E0F101112131415161718");
1 ✔
767
                  }),
1 ✔
768

769
            CHECK("copy_out_le with 64bit input (partial words)",
770
                  [&](auto& result) {
1 ✔
771
                     std::vector<uint8_t> out_vector(15);
1 ✔
772
                     const std::array<uint64_t, 2> in_array = {0x0A0B0C0D0E0F1011, 0x1213141516171819};
1 ✔
773
                     Botan::copy_out_le(out_vector, in_array);
1 ✔
774
                     result.test_bin_eq("copy_out_le(u64)", out_vector, "11100F0E0D0C0B0A19181716151413");
1 ✔
775
                  }),
1 ✔
776
         };
9 ✔
777
      }
1 ✔
778
};
779

780
BOTAN_REGISTER_SMOKE_TEST("utils", "util", Utility_Function_Tests);
781

782
class AlgorithmSpec_Tests final : public Test {
1 ✔
783
   public:
784
      std::vector<Test::Result> run() override {
1 ✔
785
         std::vector<Test::Result> results;
1 ✔
786

787
         results.push_back(parse_bare_name());
2 ✔
788
         results.push_back(parse_name_with_args());
2 ✔
789
         results.push_back(parse_cipher_mode_syntax());
2 ✔
790
         results.push_back(parse_invalid_names());
2 ✔
791
         results.push_back(parse_nesting_depth());
2 ✔
792
         results.push_back(match_exact());
2 ✔
793
         results.push_back(match_head_alternatives());
2 ✔
794
         results.push_back(match_wildcard_head());
2 ✔
795
         results.push_back(match_int_placeholders());
2 ✔
796
         results.push_back(match_optional_placeholders());
2 ✔
797
         results.push_back(match_str_and_name_placeholders());
2 ✔
798
         results.push_back(match_variadic());
2 ✔
799
         results.push_back(match_literals());
2 ✔
800
         results.push_back(match_optional_literals());
2 ✔
801
         results.push_back(match_accessor_errors());
2 ✔
802

803
         return results;
1 ✔
804
      }
×
805

806
   private:
807
      static Test::Result parse_bare_name() {
1 ✔
808
         Test::Result result("AlgorithmSpec bare name");
1 ✔
809
         const Botan::AlgorithmSpec spec("SHA-256");
1 ✔
810
         result.test_str_eq("head", spec.head(), "SHA-256");
1 ✔
811
         result.test_sz_eq("no args", spec.arg_count(), 0);
1 ✔
812
         result.test_str_eq("text", spec.to_string(), "SHA-256");
1 ✔
813
         result.test_str_eq("canonical", spec.canonical_form(), "SHA-256");
1 ✔
814

815
         // Without the cipher mode syntax a slash is an ordinary character
816
         const Botan::AlgorithmSpec slashed("AES-128/CBC/PKCS7");
1 ✔
817
         result.test_str_eq("slash is not a separator", slashed.head(), "AES-128/CBC/PKCS7");
1 ✔
818
         result.test_sz_eq("no args", slashed.arg_count(), 0);
1 ✔
819
         return result;
1 ✔
820
      }
1 ✔
821

822
      static Test::Result parse_name_with_args() {
1 ✔
823
         Test::Result result("AlgorithmSpec with arguments");
1 ✔
824

825
         const Botan::AlgorithmSpec hmac("HMAC(SHA-256)");
1 ✔
826
         result.test_str_eq("head", hmac.head(), "HMAC");
1 ✔
827
         result.test_sz_eq("one arg", hmac.arg_count(), 1);
1 ✔
828
         result.test_str_eq("arg", hmac.arg(0).to_string(), "SHA-256");
1 ✔
829
         result.test_throws<Botan::Invalid_Argument>("arg out of range", [&] { hmac.arg(1); });
2 ✔
830

831
         const Botan::AlgorithmSpec nested("Foo(A,B(C,D),E)");
1 ✔
832
         result.test_str_eq("head", nested.head(), "Foo");
1 ✔
833
         result.test_sz_eq("three args", nested.arg_count(), 3);
1 ✔
834
         result.test_str_eq("arg0", nested.arg(0).to_string(), "A");
1 ✔
835
         result.test_str_eq("arg1", nested.arg(1).to_string(), "B(C,D)");
1 ✔
836
         result.test_str_eq("arg1 head", nested.arg(1).head(), "B");
1 ✔
837
         result.test_sz_eq("arg1 args", nested.arg(1).arg_count(), 2);
1 ✔
838
         result.test_str_eq("arg1 arg1", nested.arg(1).arg(1).to_string(), "D");
1 ✔
839
         result.test_str_eq("arg2", nested.arg(2).to_string(), "E");
1 ✔
840
         result.test_str_eq("canonical", nested.canonical_form(), "Foo(A,B(C,D),E)");
1 ✔
841
         result.test_str_eq("with_head", nested.with_head("Bar").to_string(), "Bar(A,B(C,D),E)");
1 ✔
842

843
         return result;
1 ✔
844
      }
1 ✔
845

846
      static Test::Result parse_cipher_mode_syntax() {
1 ✔
847
         Test::Result result("AlgorithmSpec cipher mode syntax");
1 ✔
848

849
         using Syntax = Botan::AlgorithmSpec::Syntax;
1 ✔
850

851
         const Botan::AlgorithmSpec cbc("AES-128/CBC/PKCS7", Syntax::CipherMode);
1 ✔
852
         result.test_str_eq("head", cbc.head(), "CBC");
1 ✔
853
         result.test_sz_eq("two args", cbc.arg_count(), 2);
1 ✔
854
         result.test_str_eq("cipher", cbc.arg(0).to_string(), "AES-128");
1 ✔
855
         result.test_str_eq("padding", cbc.arg(1).to_string(), "PKCS7");
1 ✔
856
         result.test_str_eq("text", cbc.to_string(), "AES-128/CBC/PKCS7");
1 ✔
857
         result.test_str_eq("canonical", cbc.canonical_form(), "CBC(AES-128,PKCS7)");
1 ✔
858

859
         const Botan::AlgorithmSpec gcm("AES-128/GCM(12)", Syntax::CipherMode);
1 ✔
860
         result.test_str_eq("head", gcm.head(), "GCM");
1 ✔
861
         result.test_str_eq("canonical", gcm.canonical_form(), "GCM(AES-128,12)");
1 ✔
862

863
         const Botan::AlgorithmSpec gcm_pad("AES-128/GCM(12)/Foo", Syntax::CipherMode);
1 ✔
864
         result.test_str_eq("canonical", gcm_pad.canonical_form(), "GCM(AES-128,12,Foo)");
1 ✔
865

866
         const Botan::AlgorithmSpec plain("GCM(AES-128,12)", Syntax::CipherMode);
1 ✔
867
         result.test_str_eq("canonical", plain.canonical_form(), "GCM(AES-128,12)");
1 ✔
868

869
         // A slash below the top level is an ordinary character
870
         const Botan::AlgorithmSpec inner("AES-128/CBC(Foo/Bar)", Syntax::CipherMode);
1 ✔
871
         result.test_str_eq("canonical", inner.canonical_form(), "CBC(AES-128,Foo/Bar)");
1 ✔
872

873
         const Botan::AlgorithmSpec bare("SHA-256", Syntax::CipherMode);
1 ✔
874
         result.test_str_eq("head", bare.head(), "SHA-256");
1 ✔
875
         result.test_sz_eq("no args", bare.arg_count(), 0);
1 ✔
876

877
         return result;
1 ✔
878
      }
1 ✔
879

880
      static Test::Result parse_invalid_names() {
1 ✔
881
         Test::Result result("AlgorithmSpec invalid names rejected");
1 ✔
882

883
         using Syntax = Botan::AlgorithmSpec::Syntax;
1 ✔
884

885
         const auto verify_rejected = [&](std::string_view name, Syntax syntax) {
46 ✔
886
            result.test_throws<Botan::Invalid_Algorithm_Name>(Botan::fmt("Invalid name '{}' rejected", name), [&] {
90 ✔
887
               const Botan::AlgorithmSpec _parsed(name, syntax);
45 ✔
888
            });
×
889
         };
45 ✔
890

891
         const std::vector<std::string> invalid_in_any_syntax = {
1 ✔
892
            "",        "(",       ")",         ",",        "Foo((A))",         "Foo(A(((B))C))", "Foo()",
893
            "Foo,",    "Foo(",    "Foo)",      "Foo(,)",   "Foo(,A)",          "Foo(A,)",        "Foo(A,,B)",
894
            "Foo(A)B", "Foo(A))", "Foo(A)(B)", "Foo(A),B", "HMAC(SHA-256)Foo",
895
         };
1 ✔
896

897
         for(const auto& tc : invalid_in_any_syntax) {
20 ✔
898
            verify_rejected(tc, Syntax::Standard);
19 ✔
899
            verify_rejected(tc, Syntax::CipherMode);
19 ✔
900
         }
901

902
         const std::vector<std::string> invalid_cipher_modes = {
1 ✔
903
            "AES-128//CBC",
904
            "/Foo",
905
            "Foo/",
906
            "/",
907
            "AES-128/CBC(",
908
            "AES-128/CBC()",
909
            "AES-128/CBC(A)/",
910
         };
1 ✔
911

912
         for(const auto& tc : invalid_cipher_modes) {
8 ✔
913
            verify_rejected(tc, Syntax::CipherMode);
7 ✔
914
         }
915

916
         return result;
1 ✔
917
      }
1 ✔
918

919
      static Test::Result parse_nesting_depth() {
1 ✔
920
         Test::Result result("AlgorithmSpec nesting depth");
1 ✔
921

922
         auto nested = [](size_t depth) {
3 ✔
923
            std::string s = "X";
2 ✔
924
            for(size_t i = 0; i != depth; ++i) {
52 ✔
925
               s = Botan::fmt("F({})", s);
50 ✔
926
            }
927
            return s;
2 ✔
928
         };
×
929

930
         result.test_no_throw("moderate nesting accepted", [&] { const Botan::AlgorithmSpec _parsed(nested(10)); });
3 ✔
931
         result.test_throws<Botan::Invalid_Algorithm_Name>("excessive nesting rejected",
1 ✔
932
                                                           [&] { const Botan::AlgorithmSpec _parsed(nested(40)); });
2 ✔
933

934
         return result;
1 ✔
935
      }
×
936

937
      static Test::Result match_exact() {
1 ✔
938
         Test::Result result("AlgorithmSpec exact match");
1 ✔
939

940
         const Botan::AlgorithmSpec hmac("HMAC(SHA-512)");
1 ✔
941
         if(const auto m = hmac.match("HMAC({hash})")) {
1 ✔
942
            result.test_str_eq("head", m->head(), "HMAC");
1 ✔
943
            result.test_is_true("hash bound", m->has("hash"));
1 ✔
944
            result.test_str_eq("hash", m->str("hash"), "SHA-512");
1 ✔
945
            result.test_str_eq("hash as name", m->name("hash").head(), "SHA-512");
1 ✔
946
         } else {
947
            result.test_failure("HMAC(SHA-512) did not match");
×
948
         }
949

950
         // Extra or missing arguments must not match
951
         const Botan::AlgorithmSpec extra("HMAC(SHA-512,Foo)");
1 ✔
952
         result.test_is_false("extra argument rejected", extra.matches("HMAC({hash})"));
1 ✔
953

954
         const Botan::AlgorithmSpec none("HMAC");
1 ✔
955
         result.test_is_false("missing argument rejected", none.matches("HMAC({hash})"));
1 ✔
956

957
         const Botan::AlgorithmSpec other("CMAC(AES-128)");
1 ✔
958
         result.test_is_false("other head rejected", other.matches("HMAC({hash})"));
1 ✔
959

960
         // A pattern without elements only matches a bare name
961
         const Botan::AlgorithmSpec bare("SHA-256");
1 ✔
962
         result.test_is_true("bare name matches", bare.matches("SHA-256"));
1 ✔
963
         const Botan::AlgorithmSpec with_arg("SHA-256(1)");
1 ✔
964
         result.test_is_false("bare pattern rejects args", with_arg.matches("SHA-256"));
1 ✔
965

966
         return result;
1 ✔
967
      }
1 ✔
968

969
      static Test::Result match_head_alternatives() {
1 ✔
970
         Test::Result result("AlgorithmSpec head alternatives");
1 ✔
971

972
         const Botan::AlgorithmSpec emsa4("EMSA4(SHA-256)");
1 ✔
973
         if(const auto m = emsa4.match("PSS|EMSA-PSS|EMSA4({hash})")) {
1 ✔
974
            result.test_str_eq("matched head", m->head(), "EMSA4");
1 ✔
975
            result.test_str_eq("hash", m->str("hash"), "SHA-256");
1 ✔
976
         } else {
977
            result.test_failure("EMSA4(SHA-256) did not match");
×
978
         }
979

980
         const Botan::AlgorithmSpec sha2("SHA2-256");
1 ✔
981
         result.test_is_true("bare alternative", sha2.matches("SHA-256|SHA2-256"));
1 ✔
982
         const Botan::AlgorithmSpec sha3("SHA-3(256)");
1 ✔
983
         result.test_is_false("other head", sha3.matches("SHA-256|SHA2-256"));
1 ✔
984

985
         return result;
1 ✔
986
      }
1 ✔
987

988
      static Test::Result match_wildcard_head() {
1 ✔
989
         Test::Result result("AlgorithmSpec wildcard head");
1 ✔
990

991
         const Botan::AlgorithmPattern pattern("*({cipher})");
1 ✔
992

993
         const Botan::AlgorithmSpec cbc("CBC(AES-128)");
1 ✔
994
         if(const auto m = cbc.match(pattern)) {
1 ✔
995
            result.test_str_eq("head", m->head(), "CBC");
1 ✔
996
            result.test_str_eq("cipher", m->str("cipher"), "AES-128");
1 ✔
997
         } else {
998
            result.test_failure("CBC(AES-128) did not match");
×
999
         }
1000

1001
         const Botan::AlgorithmSpec other("Anything-At-All(AES-128)");
1 ✔
1002
         if(const auto m = other.match(pattern)) {
1 ✔
1003
            result.test_str_eq("head", m->head(), "Anything-At-All");
1 ✔
1004
         } else {
1005
            result.test_failure("Anything-At-All(AES-128) did not match");
×
1006
         }
1007

1008
         // The elements are still enforced
1009
         const Botan::AlgorithmSpec bare("CBC");
1 ✔
1010
         result.test_is_false("missing argument rejected", bare.matches(pattern));
1 ✔
1011
         const Botan::AlgorithmSpec extra("CBC(AES-128,x)");
1 ✔
1012
         result.test_is_false("extra argument rejected", extra.matches(pattern));
1 ✔
1013

1014
         const Botan::AlgorithmSpec name_only("SHA-256");
1 ✔
1015
         result.test_is_true("bare wildcard matches any bare name", name_only.matches("*"));
1 ✔
1016
         result.test_is_false("bare wildcard rejects arguments", cbc.matches("*"));
1 ✔
1017

1018
         return result;
1 ✔
1019
      }
1 ✔
1020

1021
      static Test::Result match_int_placeholders() {
1 ✔
1022
         Test::Result result("AlgorithmSpec int placeholders");
1 ✔
1023

1024
         const Botan::AlgorithmPattern ccm("CCM({cipher},{tag_len:int=16},{L:int=3})");
1 ✔
1025

1026
         const Botan::AlgorithmSpec defaults("CCM(AES-128)");
1 ✔
1027
         if(const auto m = defaults.match(ccm)) {
1 ✔
1028
            result.test_str_eq("cipher", m->str("cipher"), "AES-128");
1 ✔
1029
            result.test_is_true("tag_len has default", m->has("tag_len"));
1 ✔
1030
            result.test_sz_eq("tag_len default", m->integer("tag_len"), 16);
1 ✔
1031
            result.test_str_eq("tag_len default text", m->str("tag_len"), "16");
1 ✔
1032
            result.test_sz_eq("L default", m->integer("L"), 3);
1 ✔
1033
         } else {
1034
            result.test_failure("CCM(AES-128) did not match");
×
1035
         }
1036

1037
         const Botan::AlgorithmSpec partial("CCM(AES-128,8)");
1 ✔
1038
         if(const auto m = partial.match(ccm)) {
1 ✔
1039
            result.test_sz_eq("tag_len", m->integer("tag_len"), 8);
1 ✔
1040
            result.test_sz_eq("L default", m->integer("L"), 3);
1 ✔
1041
         } else {
1042
            result.test_failure("CCM(AES-128,8) did not match");
×
1043
         }
1044

1045
         const Botan::AlgorithmSpec full("CCM(AES-128,8,4)");
1 ✔
1046
         if(const auto m = full.match(ccm)) {
1 ✔
1047
            result.test_sz_eq("tag_len", m->integer("tag_len"), 8);
1 ✔
1048
            result.test_sz_eq("L", m->integer("L"), 4);
1 ✔
1049
         } else {
1050
            result.test_failure("CCM(AES-128,8,4) did not match");
×
1051
         }
1052

1053
         const std::vector<std::string> rejected = {
1 ✔
1054
            "CCM(AES-128,08)",
1055
            "CCM(AES-128,+8)",
1056
            "CCM(AES-128,-8)",
1057
            "CCM(AES-128,8 )",
1058
            "CCM(AES-128,x)",
1059
            "CCM(AES-128,8(1))",
1060
            "CCM(AES-128,8,4,5)",
1061
            "CCM(AES-128,99999999999999999999999)",
1062
         };
1 ✔
1063

1064
         for(const auto& tc : rejected) {
9 ✔
1065
            const Botan::AlgorithmSpec spec(tc);
8 ✔
1066
            result.test_is_false(Botan::fmt("'{}' rejected", tc), spec.matches(ccm));
8 ✔
1067
         }
8 ✔
1068

1069
         return result;
1 ✔
1070
      }
1 ✔
1071

1072
      static Test::Result match_optional_placeholders() {
1 ✔
1073
         Test::Result result("AlgorithmSpec optional placeholders");
1 ✔
1074

1075
         const Botan::AlgorithmPattern cfb("CFB({cipher},{feedback_bits:int?})");
1 ✔
1076

1077
         const Botan::AlgorithmSpec absent("CFB(AES-128)");
1 ✔
1078
         if(const auto m = absent.match(cfb)) {
1 ✔
1079
            result.test_str_eq("cipher", m->str("cipher"), "AES-128");
1 ✔
1080
            result.test_is_false("feedback_bits unbound", m->has("feedback_bits"));
1 ✔
1081
            result.test_throws<Botan::Internal_Error>("unbound integer() throws", [&] { m->integer("feedback_bits"); });
2 ✔
1082
            result.test_throws<Botan::Internal_Error>("unbound str() throws", [&] { m->str("feedback_bits"); });
2 ✔
1083
         } else {
1084
            result.test_failure("CFB(AES-128) did not match");
×
1085
         }
1086

1087
         const Botan::AlgorithmSpec present("CFB(AES-128,8)");
1 ✔
1088
         if(const auto m = present.match(cfb)) {
1 ✔
1089
            result.test_is_true("feedback_bits bound", m->has("feedback_bits"));
1 ✔
1090
            result.test_sz_eq("feedback_bits", m->integer("feedback_bits"), 8);
1 ✔
1091
         } else {
1092
            result.test_failure("CFB(AES-128,8) did not match");
×
1093
         }
1094

1095
         // An empty str default is permitted
1096
         const Botan::AlgorithmSpec no_pers("Skein-512(256)");
1 ✔
1097
         if(const auto m = no_pers.match("Skein-512({outbits:int=512},{personalization:str=})")) {
1 ✔
1098
            result.test_sz_eq("outbits", m->integer("outbits"), 256);
1 ✔
1099
            result.test_is_true("personalization bound to default", m->has("personalization"));
1 ✔
1100
            result.test_str_eq("personalization empty", m->str("personalization"), "");
1 ✔
1101
         } else {
1102
            result.test_failure("Skein-512(256) did not match");
×
1103
         }
1104

1105
         return result;
1 ✔
1106
      }
1 ✔
1107

1108
      static Test::Result match_str_and_name_placeholders() {
1 ✔
1109
         Test::Result result("AlgorithmSpec str and name placeholders");
1 ✔
1110

1111
         const Botan::AlgorithmSpec spec("Foo(Bar(Baz,Qux),Quux(1))");
1 ✔
1112
         if(const auto m = spec.match("Foo({s:str},{n})")) {
1 ✔
1113
            result.test_str_eq("str binds verbatim", m->str("s"), "Bar(Baz,Qux)");
1 ✔
1114
            result.test_throws<Botan::Internal_Error>("name() on str placeholder throws", [&] { m->name("s"); });
2 ✔
1115
            result.test_throws<Botan::Internal_Error>("integer() on str placeholder throws", [&] { m->integer("s"); });
2 ✔
1116

1117
            result.test_str_eq("name text", m->str("n"), "Quux(1)");
1 ✔
1118
            const auto& n = m->name("n");
1 ✔
1119
            result.test_str_eq("name head", n.head(), "Quux");
1 ✔
1120
            result.test_sz_eq("name args", n.arg_count(), 1);
1 ✔
1121
            result.test_is_true("name can be matched again", n.matches("Quux({x:int})"));
1 ✔
1122
         } else {
1123
            result.test_failure("Foo(Bar(Baz,Qux),Quux(1)) did not match");
×
1124
         }
1125

1126
         return result;
1 ✔
1127
      }
1 ✔
1128

1129
      static Test::Result match_variadic() {
1 ✔
1130
         Test::Result result("AlgorithmSpec variadic placeholders");
1 ✔
1131

1132
         const Botan::AlgorithmPattern hss("HSS-LMS({hash},{layer}...)");
1 ✔
1133

1134
         const Botan::AlgorithmSpec two("HSS-LMS(SHA-256,HW(5,1),HW(10,4))");
1 ✔
1135
         if(const auto m = two.match(hss)) {
1 ✔
1136
            result.test_str_eq("hash", m->str("hash"), "SHA-256");
1 ✔
1137
            const auto layers = m->rest("layer");
1 ✔
1138
            result.test_sz_eq("two layers", layers.size(), 2);
1 ✔
1139
            result.test_str_eq("layer 0", layers[0].to_string(), "HW(5,1)");
1 ✔
1140
            result.test_str_eq("layer 1", layers[1].to_string(), "HW(10,4)");
1 ✔
1141
            result.test_is_true("layer matches", layers[1].matches("HW({h:int},{w:int})"));
1 ✔
1142
            result.test_throws<Botan::Internal_Error>("str() on variadic throws", [&] { m->str("layer"); });
2 ✔
1143
            result.test_throws<Botan::Internal_Error>("rest() on non-variadic throws", [&] { m->rest("hash"); });
2 ✔
1144
         } else {
1145
            result.test_failure("HSS-LMS with two layers did not match");
×
1146
         }
1147

1148
         const Botan::AlgorithmSpec one("HSS-LMS(SHA-256,HW(5,1))");
1 ✔
1149
         if(const auto m = one.match(hss)) {
1 ✔
1150
            result.test_sz_eq("one layer", m->rest("layer").size(), 1);
1 ✔
1151
         } else {
1152
            result.test_failure("HSS-LMS with one layer did not match");
×
1153
         }
1154

1155
         // Variadic means one or more
1156
         const Botan::AlgorithmSpec zero("HSS-LMS(SHA-256)");
1 ✔
1157
         result.test_is_false("variadic requires at least one", zero.matches(hss));
1 ✔
1158

1159
         return result;
1 ✔
1160
      }
1 ✔
1161

1162
      static Test::Result match_literals() {
1 ✔
1163
         Test::Result result("AlgorithmSpec literals");
1 ✔
1164

1165
         const Botan::AlgorithmPattern plain("Foo(Bar,{x})");
1 ✔
1166
         const Botan::AlgorithmSpec ok("Foo(Bar,A)");
1 ✔
1167
         if(const auto m = ok.match(plain)) {
1 ✔
1168
            result.test_str_eq("x", m->str("x"), "A");
1 ✔
1169
         } else {
1170
            result.test_failure("Foo(Bar,A) did not match");
×
1171
         }
1172
         const Botan::AlgorithmSpec wrong_literal("Foo(Baz,A)");
1 ✔
1173
         result.test_is_false("wrong literal rejected", wrong_literal.matches(plain));
1 ✔
1174
         const Botan::AlgorithmSpec literal_with_args("Foo(Bar(Z),A)");
1 ✔
1175
         result.test_is_false("literal with args rejected", literal_with_args.matches(plain));
1 ✔
1176

1177
         const Botan::AlgorithmPattern nested("Foo(Bar({x}),{y})");
1 ✔
1178
         const Botan::AlgorithmSpec nested_ok("Foo(Bar(A),B)");
1 ✔
1179
         if(const auto m = nested_ok.match(nested)) {
1 ✔
1180
            result.test_str_eq("x", m->str("x"), "A");
1 ✔
1181
            result.test_str_eq("y", m->str("y"), "B");
1 ✔
1182
         } else {
1183
            result.test_failure("Foo(Bar(A),B) did not match");
×
1184
         }
1185

1186
         const std::vector<std::string> rejected = {
1 ✔
1187
            "Foo(Bar,B)",
1188
            "Foo(Baz(A),B)",
1189
            "Foo(Bar(A,C),B)",
1190
            "Foo(Bar(A))",
1191
            "Foo(Bar(A),B,C)",
1192
         };
1 ✔
1193
         for(const auto& tc : rejected) {
6 ✔
1194
            const Botan::AlgorithmSpec spec(tc);
5 ✔
1195
            result.test_is_false(Botan::fmt("'{}' rejected", tc), spec.matches(nested));
5 ✔
1196
         }
5 ✔
1197

1198
         return result;
1 ✔
1199
      }
1 ✔
1200

1201
      static Test::Result match_optional_literals() {
1 ✔
1202
         Test::Result result("AlgorithmSpec optional literals");
1 ✔
1203

1204
         const Botan::AlgorithmPattern pattern("Foo([Bar],{x?})");
1 ✔
1205

1206
         const Botan::AlgorithmSpec bare("Foo");
1 ✔
1207
         if(const auto m = bare.match(pattern)) {
1 ✔
1208
            result.test_is_false("x unbound", m->has("x"));
1 ✔
1209
         } else {
1210
            result.test_failure("Foo did not match");
×
1211
         }
1212

1213
         const Botan::AlgorithmSpec literal_only("Foo(Bar)");
1 ✔
1214
         if(const auto m = literal_only.match(pattern)) {
1 ✔
1215
            result.test_is_false("x unbound", m->has("x"));
1 ✔
1216
         } else {
1217
            result.test_failure("Foo(Bar) did not match");
×
1218
         }
1219

1220
         const Botan::AlgorithmSpec both("Foo(Bar,A)");
1 ✔
1221
         if(const auto m = both.match(pattern)) {
1 ✔
1222
            result.test_str_eq("x", m->str("x"), "A");
1 ✔
1223
         } else {
1224
            result.test_failure("Foo(Bar,A) did not match");
×
1225
         }
1226

1227
         // The literal is skipped if the argument is something else
1228
         const Botan::AlgorithmSpec skipped("Foo(A)");
1 ✔
1229
         if(const auto m = skipped.match(pattern)) {
1 ✔
1230
            result.test_str_eq("x", m->str("x"), "A");
1 ✔
1231
         } else {
1232
            result.test_failure("Foo(A) did not match");
×
1233
         }
1234

1235
         // Including if it has the same head but carries arguments
1236
         const Botan::AlgorithmSpec same_head("Foo(Bar(Z))");
1 ✔
1237
         if(const auto m = same_head.match(pattern)) {
1 ✔
1238
            result.test_str_eq("x", m->str("x"), "Bar(Z)");
1 ✔
1239
         } else {
1240
            result.test_failure("Foo(Bar(Z)) did not match");
×
1241
         }
1242

1243
         const Botan::AlgorithmSpec too_many("Foo(A,B)");
1 ✔
1244
         result.test_is_false("too many arguments rejected", too_many.matches(pattern));
1 ✔
1245
         const Botan::AlgorithmSpec too_many2("Foo(Bar,A,B)");
1 ✔
1246
         result.test_is_false("too many arguments rejected", too_many2.matches(pattern));
1 ✔
1247

1248
         return result;
1 ✔
1249
      }
1 ✔
1250

1251
      static Test::Result match_accessor_errors() {
1 ✔
1252
         Test::Result result("AlgorithmSpec accessor errors");
1 ✔
1253

1254
         const Botan::AlgorithmSpec spec("HMAC(SHA-256)");
1 ✔
1255
         if(const auto m = spec.match("HMAC({hash})")) {
1 ✔
1256
            result.test_throws<Botan::Internal_Error>("has() unknown variable", [&] { m->has("nope"); });
2 ✔
1257
            result.test_throws<Botan::Internal_Error>("str() unknown variable", [&] { m->str("nope"); });
2 ✔
1258
            result.test_throws<Botan::Internal_Error>("integer() unknown variable", [&] { m->integer("nope"); });
2 ✔
1259
            result.test_throws<Botan::Internal_Error>("name() unknown variable", [&] { m->name("nope"); });
2 ✔
1260
            result.test_throws<Botan::Internal_Error>("rest() unknown variable", [&] { m->rest("nope"); });
2 ✔
1261
            result.test_throws<Botan::Internal_Error>("integer() on name placeholder", [&] { m->integer("hash"); });
2 ✔
1262
            result.test_throws<Botan::Internal_Error>("rest() on name placeholder", [&] { m->rest("hash"); });
2 ✔
1263
         } else {
1264
            result.test_failure("HMAC(SHA-256) did not match");
×
1265
         }
1266

1267
         return result;
1 ✔
1268
      }
1 ✔
1269
};
1270

1271
BOTAN_REGISTER_TEST("utils", "algorithm_spec", AlgorithmSpec_Tests);
1272

1273
class BitOps_Tests final : public Test {
1 ✔
1274
   public:
1275
      std::vector<Test::Result> run() override {
1 ✔
1276
         std::vector<Test::Result> results;
1 ✔
1277

1278
         results.push_back(test_power_of_2());
2 ✔
1279
         results.push_back(test_ctz());
2 ✔
1280
         results.push_back(test_sig_bytes());
2 ✔
1281
         results.push_back(test_popcount());
2 ✔
1282
         results.push_back(test_reverse_bits());
2 ✔
1283

1284
         return results;
1 ✔
1285
      }
×
1286

1287
   private:
1288
      template <typename T>
1289
      void test_ctz(Test::Result& result, T val, size_t expected) {
6 ✔
1290
         Botan::CT::poison(val);
1291
         const size_t computed = Botan::ctz<T>(val);
6 ✔
1292
         Botan::CT::unpoison_all(computed, val);
1293
         result.test_sz_eq("ctz(" + std::to_string(val) + ")", computed, expected);
24 ✔
1294
      }
6 ✔
1295

1296
      Test::Result test_ctz() {
1 ✔
1297
         Test::Result result("ctz");
1 ✔
1298
         test_ctz<uint32_t>(result, 0, 32);
1 ✔
1299
         test_ctz<uint32_t>(result, 1, 0);
1 ✔
1300
         test_ctz<uint32_t>(result, 0x80, 7);
1 ✔
1301
         test_ctz<uint32_t>(result, 0x8000000, 27);
1 ✔
1302
         test_ctz<uint32_t>(result, 0x8100000, 20);
1 ✔
1303
         test_ctz<uint32_t>(result, 0x80000000, 31);
1 ✔
1304

1305
         return result;
1 ✔
1306
      }
×
1307

1308
      template <typename T>
1309
      void test_sig_bytes(Test::Result& result, T val, size_t expected) {
14 ✔
1310
         Botan::CT::poison(val);
1311
         const size_t computed = Botan::significant_bytes<T>(val);
14 ✔
1312
         Botan::CT::unpoison_all(computed, val);
1313
         result.test_sz_eq("significant_bytes(" + std::to_string(val) + ")", computed, expected);
56 ✔
1314
      }
14 ✔
1315

1316
      Test::Result test_sig_bytes() {
1 ✔
1317
         Test::Result result("significant_bytes");
1 ✔
1318
         test_sig_bytes<uint32_t>(result, 0, 0);
1 ✔
1319
         test_sig_bytes<uint32_t>(result, 1, 1);
1 ✔
1320
         test_sig_bytes<uint32_t>(result, 0x80, 1);
1 ✔
1321
         test_sig_bytes<uint32_t>(result, 255, 1);
1 ✔
1322
         test_sig_bytes<uint32_t>(result, 256, 2);
1 ✔
1323
         test_sig_bytes<uint32_t>(result, 65535, 2);
1 ✔
1324
         test_sig_bytes<uint32_t>(result, 65536, 3);
1 ✔
1325
         test_sig_bytes<uint32_t>(result, 0x80000000, 4);
1 ✔
1326

1327
         test_sig_bytes<uint64_t>(result, 0, 0);
1 ✔
1328
         test_sig_bytes<uint64_t>(result, 1, 1);
1 ✔
1329
         test_sig_bytes<uint64_t>(result, 0x80, 1);
1 ✔
1330
         test_sig_bytes<uint64_t>(result, 256, 2);
1 ✔
1331
         test_sig_bytes<uint64_t>(result, 0x80000000, 4);
1 ✔
1332
         test_sig_bytes<uint64_t>(result, 0x100000000, 5);
1 ✔
1333

1334
         return result;
1 ✔
1335
      }
×
1336

1337
      template <typename T>
1338
      void test_power_of_2(Test::Result& result, T val, bool expected) {
15 ✔
1339
         result.test_bool_eq("power_of_2(" + std::to_string(val) + ")", Botan::is_power_of_2<T>(val), expected);
75 ✔
1340
      }
15 ✔
1341

1342
      Test::Result test_power_of_2() {
1 ✔
1343
         Test::Result result("is_power_of_2");
1 ✔
1344

1345
         test_power_of_2<uint32_t>(result, 0, false);
1 ✔
1346
         test_power_of_2<uint32_t>(result, 1, false);
1 ✔
1347
         test_power_of_2<uint32_t>(result, 2, true);
1 ✔
1348
         test_power_of_2<uint32_t>(result, 3, false);
1 ✔
1349
         test_power_of_2<uint32_t>(result, 0x8000, true);
1 ✔
1350
         test_power_of_2<uint32_t>(result, 0x8001, false);
1 ✔
1351
         test_power_of_2<uint32_t>(result, 0x8000000, true);
1 ✔
1352

1353
         test_power_of_2<uint64_t>(result, 0, false);
1 ✔
1354
         test_power_of_2<uint64_t>(result, 1, false);
1 ✔
1355
         test_power_of_2<uint64_t>(result, 2, true);
1 ✔
1356
         test_power_of_2<uint64_t>(result, 3, false);
1 ✔
1357
         test_power_of_2<uint64_t>(result, 0x8000, true);
1 ✔
1358
         test_power_of_2<uint64_t>(result, 0x8001, false);
1 ✔
1359
         test_power_of_2<uint64_t>(result, 0x8000000, true);
1 ✔
1360
         test_power_of_2<uint64_t>(result, 0x100000000000, true);
1 ✔
1361

1362
         return result;
1 ✔
1363
      }
×
1364

1365
      template <typename T>
1366
      auto pc(T val) -> decltype(Botan::ct_popcount(val)) {
2 ✔
1367
         return Botan::ct_popcount(val);
6 ✔
1368
      }
1369

1370
      template <typename T>
1371
      auto random_pc(Test::Result& result) {
4 ✔
1372
         const auto n = Botan::load_le<T>(Test::rng().random_array<sizeof(T)>());
4 ✔
1373
         result.test_sz_eq(Botan::fmt("popcount({}) == {}", n, std::popcount(n)), pc(n), std::popcount(n));
4 ✔
1374
      }
4 ✔
1375

1376
      Test::Result test_popcount() {
1 ✔
1377
         Test::Result result("popcount");
1 ✔
1378

1379
         result.test_u8_eq("popcount<uint8_t>(0)", pc<uint8_t>(0), 0);
1 ✔
1380
         result.test_u8_eq("popcount<uint16_t>(0)", pc<uint16_t>(0), 0);
1 ✔
1381
         result.test_u8_eq("popcount<uint32_t>(0)", pc<uint32_t>(0), 0);
1 ✔
1382
         result.test_u8_eq("popcount<uint64_t>(0)", pc<uint64_t>(0), 0);
1 ✔
1383

1384
         result.test_u8_eq("popcount<uint8_t>(1)", pc<uint8_t>(1), 1);
1 ✔
1385
         result.test_u8_eq("popcount<uint16_t>(1)", pc<uint16_t>(1), 1);
1 ✔
1386
         result.test_u8_eq("popcount<uint32_t>(1)", pc<uint32_t>(1), 1);
1 ✔
1387
         result.test_u8_eq("popcount<uint64_t>(1)", pc<uint64_t>(1), 1);
1 ✔
1388

1389
         result.test_u8_eq("popcount<uint8_t>(0xAA)", pc<uint8_t>(0xAA), 4);
1 ✔
1390
         result.test_u8_eq("popcount<uint16_t>(0xAAAA)", pc<uint16_t>(0xAAAA), 8);
1 ✔
1391
         result.test_u8_eq("popcount<uint32_t>(0xAAAA...)", pc<uint32_t>(0xAAAAAAAA), 16);
1 ✔
1392
         result.test_u8_eq("popcount<uint64_t>(0xAAAA...)", pc<uint64_t>(0xAAAAAAAAAAAAAAAA), 32);
1 ✔
1393

1394
         result.test_u8_eq("popcount<uint8_t>(0xFF)", pc<uint8_t>(0xFF), 8);
1 ✔
1395
         result.test_u8_eq("popcount<uint16_t>(0xFFFF)", pc<uint16_t>(0xFFFF), 16);
1 ✔
1396
         result.test_u8_eq("popcount<uint32_t>(0xFFFF...)", pc<uint32_t>(0xFFFFFFFF), 32);
1 ✔
1397
         result.test_u8_eq("popcount<uint64_t>(0xFFFF...)", pc<uint64_t>(0xFFFFFFFFFFFFFFFF), 64);
1 ✔
1398

1399
         random_pc<uint8_t>(result);
1 ✔
1400
         random_pc<uint16_t>(result);
1 ✔
1401
         random_pc<uint32_t>(result);
1 ✔
1402
         random_pc<uint64_t>(result);
1 ✔
1403

1404
         return result;
1 ✔
1405
      }
×
1406

1407
      Test::Result test_reverse_bits() {
1 ✔
1408
         Test::Result result("reverse_bits");
1 ✔
1409

1410
         result.test_u8_eq("rev(0u8)", Botan::ct_reverse_bits<uint8_t>(0b00000000), 0b00000000);
1 ✔
1411
         result.test_u8_eq("rev(1u8)", Botan::ct_reverse_bits<uint8_t>(0b01010101), 0b10101010);
1 ✔
1412
         result.test_u8_eq("rev(2u8)", Botan::ct_reverse_bits<uint8_t>(0b01001011), 0b11010010);
1 ✔
1413

1414
         result.test_u16_eq("rev(0u16)", Botan::ct_reverse_bits<uint16_t>(0b0000000000000000), 0b0000000000000000);
1 ✔
1415
         result.test_u16_eq("rev(1u16)", Botan::ct_reverse_bits<uint16_t>(0b0101010101010101), 0b1010101010101010);
1 ✔
1416
         result.test_u16_eq("rev(2u16)", Botan::ct_reverse_bits<uint16_t>(0b0100101101011010), 0b0101101011010010);
1 ✔
1417

1418
         result.test_u32_eq("rev(0u32)", Botan::ct_reverse_bits<uint32_t>(0xFFFFFFFF), 0xFFFFFFFF);
1 ✔
1419
         result.test_u32_eq("rev(1u32)", Botan::ct_reverse_bits<uint32_t>(0x55555555), 0xAAAAAAAA);
1 ✔
1420
         result.test_u32_eq("rev(2u32)", Botan::ct_reverse_bits<uint32_t>(0x4B6A2C1D), 0xB83456D2);
1 ✔
1421

1422
         result.test_u64_eq("rev(0u64)", Botan::ct_reverse_bits<uint64_t>(0xF0E0D0C005040302), 0x40C020A0030B070F);
1 ✔
1423
         result.test_u64_eq("rev(1u64)", Botan::ct_reverse_bits<uint64_t>(0x5555555555555555), 0xAAAAAAAAAAAAAAAA);
1 ✔
1424
         result.test_u64_eq("rev(2u64)", Botan::ct_reverse_bits<uint64_t>(0x4B6A2C1D5E7F8A90), 0x951FE7AB83456D2);
1 ✔
1425

1426
         return result;
1 ✔
1427
      }
×
1428
};
1429

1430
BOTAN_REGISTER_TEST("utils", "bit_ops", BitOps_Tests);
1431

1432
#if defined(BOTAN_HAS_POLY_DBL)
1433

1434
class Poly_Double_Tests final : public Text_Based_Test {
×
1435
   public:
1436
      Poly_Double_Tests() : Text_Based_Test("poly_dbl.vec", "In,Out") {}
2 ✔
1437

1438
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
82 ✔
1439
         Test::Result result("Polynomial doubling");
82 ✔
1440
         const std::vector<uint8_t> in = vars.get_req_bin("In");
82 ✔
1441
         const std::vector<uint8_t> out = vars.get_req_bin("Out");
82 ✔
1442

1443
         std::vector<uint8_t> b = in;
82 ✔
1444
         Botan::poly_double_n(b.data(), b.size());
82 ✔
1445

1446
         result.test_bin_eq("Expected value", b, out);
82 ✔
1447
         return result;
82 ✔
1448
      }
246 ✔
1449
};
1450

1451
BOTAN_REGISTER_TEST("utils", "poly_dbl", Poly_Double_Tests);
1452

1453
#endif
1454

1455
class Version_Tests final : public Test {
1 ✔
1456
   public:
1457
      std::vector<Test::Result> run() override {
1 ✔
1458
         Test::Result result("Versions");
1 ✔
1459

1460
         result.test_u32_eq("Version datestamp matches macro", Botan::version_datestamp(), BOTAN_VERSION_DATESTAMP);
1 ✔
1461

1462
         const char* version_cstr = Botan::version_cstr();
1 ✔
1463
         const std::string version_str = Botan::version_string();
1 ✔
1464
         result.test_str_eq("Same version string", version_str, version_cstr);
1 ✔
1465

1466
         const char* sversion_cstr = Botan::short_version_cstr();
1 ✔
1467
         const std::string sversion_str = Botan::short_version_string();
1 ✔
1468
         result.test_str_eq("Same short version string", sversion_str, sversion_cstr);
1 ✔
1469

1470
         const auto expected_sversion =
1 ✔
1471
            Botan::fmt("{}.{}.{}", BOTAN_VERSION_MAJOR, BOTAN_VERSION_MINOR, BOTAN_VERSION_PATCH);
1 ✔
1472

1473
         // May have a suffix eg 4.0.0-rc2
1474
         result.test_is_true("Short version string has expected format", sversion_str.starts_with(expected_sversion));
1 ✔
1475

1476
         const std::string version_check_ok =
1 ✔
1477
            Botan::runtime_version_check(BOTAN_VERSION_MAJOR, BOTAN_VERSION_MINOR, BOTAN_VERSION_PATCH);
1 ✔
1478

1479
         result.test_is_true("Correct version no warning", version_check_ok.empty());
1 ✔
1480

1481
         const std::string version_check_bad = Botan::runtime_version_check(1, 19, 42);
1 ✔
1482

1483
         const std::string expected_error =
1 ✔
1484
            "Warning: linked version (" + sversion_str + ") does not match version built against (1.19.42)\n";
2 ✔
1485

1486
         result.test_str_eq("Expected warning text", version_check_bad, expected_error);
1 ✔
1487

1488
         return {result};
3 ✔
1489
      }
2 ✔
1490
};
1491

1492
BOTAN_REGISTER_TEST("utils", "versioning", Version_Tests);
1493

1494
class Date_Format_Tests final : public Text_Based_Test {
×
1495
   public:
1496
      Date_Format_Tests() : Text_Based_Test("dates.vec", "Date") {}
2 ✔
1497

1498
      static std::vector<uint32_t> parse_date(const std::string& s) {
16 ✔
1499
         const std::vector<std::string> parts = Botan::split_on(s, ',');
16 ✔
1500
         if(parts.size() != 6) {
16 ✔
1501
            throw Test_Error("Bad date format '" + s + "'");
×
1502
         }
1503

1504
         std::vector<uint32_t> u32s;
16 ✔
1505
         u32s.reserve(parts.size());
16 ✔
1506
         for(const auto& sub : parts) {
112 ✔
1507
            u32s.push_back(Botan::to_u32bit(sub));
96 ✔
1508
         }
1509
         return u32s;
16 ✔
1510
      }
16 ✔
1511

1512
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
16 ✔
1513
         const std::string date_str = vars.get_req_str("Date");
16 ✔
1514
         Test::Result result("Date parsing");
16 ✔
1515

1516
         const std::vector<uint32_t> d = parse_date(date_str);
16 ✔
1517

1518
         if(type == "valid" || type == "valid.not_std" || type == "valid.64_bit_time_t") {
16 ✔
1519
            const Botan::calendar_point c(d[0], d[1], d[2], d[3], d[4], d[5]);
16 ✔
1520
            result.test_u32_eq(date_str + " year", c.year(), d[0]);
16 ✔
1521
            result.test_u32_eq(date_str + " month", c.month(), d[1]);
16 ✔
1522
            result.test_u32_eq(date_str + " day", c.day(), d[2]);
16 ✔
1523
            result.test_u32_eq(date_str + " hour", c.hour(), d[3]);
16 ✔
1524
            result.test_u32_eq(date_str + " minute", c.minutes(), d[4]);
16 ✔
1525
            result.test_u32_eq(date_str + " second", c.seconds(), d[5]);
16 ✔
1526

1527
            if(type == "valid.not_std" ||
16 ✔
1528
               (type == "valid.64_bit_time_t" && c.year() > 2037 && sizeof(std::time_t) == 4)) {
1529
               result.test_throws("valid but out of std::timepoint range", [c]() { c.to_std_timepoint(); });
6 ✔
1530
            } else {
1531
               const Botan::calendar_point c2(c.to_std_timepoint());
13 ✔
1532
               result.test_u32_eq(date_str + " year", c2.year(), d[0]);
13 ✔
1533
               result.test_u32_eq(date_str + " month", c2.month(), d[1]);
13 ✔
1534
               result.test_u32_eq(date_str + " day", c2.day(), d[2]);
13 ✔
1535
               result.test_u32_eq(date_str + " hour", c2.hour(), d[3]);
13 ✔
1536
               result.test_u32_eq(date_str + " minute", c2.minutes(), d[4]);
13 ✔
1537
               result.test_u32_eq(date_str + " second", c2.seconds(), d[5]);
26 ✔
1538
            }
1539
         } else if(type == "invalid") {
×
1540
            result.test_throws("invalid date",
×
1541
                               [d]() { const Botan::calendar_point c(d[0], d[1], d[2], d[3], d[4], d[5]); });
×
1542
         } else {
1543
            throw Test_Error("Unexpected header '" + type + "' in date format tests");
×
1544
         }
1545

1546
         return result;
32 ✔
1547
      }
190 ✔
1548
};
1549

1550
BOTAN_REGISTER_TEST("utils", "util_dates", Date_Format_Tests);
1551

1552
class Charset_Tests final : public Text_Based_Test {
×
1553
   public:
1554
      Charset_Tests() : Text_Based_Test("charset.vec", "In,Out") {}
2 ✔
1555

1556
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
53 ✔
1557
         Test::Result result("Charset");
53 ✔
1558

1559
         const std::vector<uint8_t> in = vars.get_req_bin("In");
53 ✔
1560

1561
         const auto in_sv = std::string_view(reinterpret_cast<const char*>(in.data()), in.size());
53 ✔
1562

1563
         if(type == "UTF8-UCS2-INVALID") {
53 ✔
1564
            result.test_throws<Botan::Decoding_Error>("utf8_to_ucs2 rejects invalid input",
13 ✔
1565
                                                      [&] { Botan::utf8_to_ucs2(in_sv); });
26 ✔
1566
            return result;
13 ✔
1567
         }
1568

1569
         if(type == "UTF8-UCS4-INVALID") {
40 ✔
1570
            result.test_throws<Botan::Decoding_Error>("utf8_to_ucs4 rejects invalid input",
10 ✔
1571
                                                      [&] { Botan::utf8_to_ucs4(in_sv); });
20 ✔
1572
            return result;
10 ✔
1573
         }
1574

1575
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
30 ✔
1576

1577
         std::string converted;
30 ✔
1578

1579
         if(type == "UCS2-UTF8") {
30 ✔
1580
            converted = Botan::ucs2_to_utf8(in);
4 ✔
1581
         } else if(type == "UCS4-UTF8") {
26 ✔
1582
            converted = Botan::ucs4_to_utf8(in);
1 ✔
1583
         } else if(type == "UTF8-UCS2") {
25 ✔
1584
            std::vector<uint8_t> ucs2 = Botan::utf8_to_ucs2(in_sv);
11 ✔
1585
            converted = std::string(ucs2.begin(), ucs2.end());
22 ✔
1586
         } else if(type == "UTF8-UCS4") {
25 ✔
1587
            std::vector<uint8_t> ucs4 = Botan::utf8_to_ucs4(in_sv);
11 ✔
1588
            converted = std::string(ucs4.begin(), ucs4.end());
22 ✔
1589
         } else if(type == "LATIN1-UTF8") {
14 ✔
1590
            converted = Botan::latin1_to_utf8(in);
3 ✔
1591
         } else {
1592
            throw Test_Error("Unexpected header '" + type + "' in charset tests");
×
1593
         }
1594

1595
         result.test_bin_eq(
30 ✔
1596
            "string converted successfully", std::vector<uint8_t>(converted.begin(), converted.end()), expected);
30 ✔
1597

1598
         return result;
30 ✔
1599
      }
113 ✔
1600
};
1601

1602
BOTAN_REGISTER_TEST("utils", "charset", Charset_Tests);
1603

1604
class Escape_Control_Chars_Tests final : public Test {
1 ✔
1605
   public:
1606
      std::vector<Test::Result> run() override {
1 ✔
1607
         Test::Result result("escape_control_chars");
1 ✔
1608

1609
         result.test_str_eq("newline", Botan::escape_control_chars("a\nb"), "a\\x0Ab");
1 ✔
1610
         result.test_str_eq("escape", Botan::escape_control_chars("\x1b[0m"), "\\x1B[0m");
1 ✔
1611
         result.test_str_eq("embedded NUL", Botan::escape_control_chars(std::string("a\0b", 3)), "a\\x00b");
2 ✔
1612
         result.test_str_eq("DEL", Botan::escape_control_chars("\x7f"), "\\x7F");
1 ✔
1613
         result.test_str_eq("C1 control U+009B", Botan::escape_control_chars("\xc2\x9b"), "\\xC2\\x9B");
1 ✔
1614
         result.test_str_eq("printable ASCII ok", Botan::escape_control_chars("Hello.123"), "Hello.123");
1 ✔
1615
         result.test_str_eq("printable UTF-8 ok", Botan::escape_control_chars("Fräulein"), "Fräulein");
1 ✔
1616
         result.test_str_eq("U+00A0 is not a control", Botan::escape_control_chars("\xc2\xa0"), "\xc2\xa0");
1 ✔
1617

1618
         return {result};
3 ✔
1619
      }
3 ✔
1620
};
1621

1622
BOTAN_REGISTER_TEST("utils", "escape_control_chars", Escape_Control_Chars_Tests);
1623

1624
#if defined(BOTAN_HAS_IPV4_ADDRESS)
1625

1626
class IPv4_Parsing_Tests final : public Text_Based_Test {
×
1627
   public:
1628
      IPv4_Parsing_Tests() : Text_Based_Test("utils/ipv4.vec", "IPv4") {}
2 ✔
1629

1630
      Test::Result run_one_test(const std::string& status, const VarMap& vars) override {
47 ✔
1631
         Test::Result result("IPv4 parsing");
47 ✔
1632

1633
         const std::string input = vars.get_req_str("IPv4");
47 ✔
1634
         const bool valid = (status == "Valid");
47 ✔
1635

1636
         auto ipv4 = Botan::IPv4Address::from_string(input);
47 ✔
1637

1638
         result.test_bool_eq("IPv4Address::from_string accepts only valid", ipv4.has_value(), valid);
47 ✔
1639

1640
         if(ipv4) {
47 ✔
1641
            const std::string rt = ipv4->to_string();
13 ✔
1642
            result.test_str_eq("IPv4Address::from_string and IPv4Address::to_string round trip", input, rt);
13 ✔
1643
         }
13 ✔
1644

1645
         return result;
47 ✔
1646
      }
47 ✔
1647
};
1648

1649
BOTAN_REGISTER_TEST("utils", "ipv4_parse", IPv4_Parsing_Tests);
1650

1651
class IPv4_Subnet_Parsing_Tests final : public Text_Based_Test {
×
1652
   public:
1653
      IPv4_Subnet_Parsing_Tests() : Text_Based_Test("utils/ipv4_subnet.vec", "IPv4Subnet", "LastAddress") {}
2 ✔
1654

1655
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
19 ✔
1656
         Test::Result result("IPv4 subnet parsing");
19 ✔
1657

1658
         const std::string input = vars.get_req_str("IPv4Subnet");
19 ✔
1659
         const bool valid = (header == "Valid");
19 ✔
1660

1661
         const auto subnet = Botan::IPv4Subnet::from_string(input);
19 ✔
1662

1663
         result.test_bool_eq("IPv4Subnet::from_string accepts only valid", subnet.has_value(), valid);
19 ✔
1664

1665
         if(subnet) {
19 ✔
1666
            result.test_str_eq(
6 ✔
1667
               "IPv4Subnet::from_string and IPv4Subnet::to_string round trip", subnet->to_string(), input);
6 ✔
1668

1669
            const auto last = Botan::IPv4Address::from_string(vars.get_req_str("LastAddress"));
6 ✔
1670
            result.test_is_true("LastAddress parses", last.has_value());
6 ✔
1671
            if(last) {
6 ✔
1672
               result.test_str_eq("IPv4Subnet::last_address", subnet->last_address().to_string(), last->to_string());
6 ✔
1673
            }
1674
         }
1675

1676
         return result;
19 ✔
1677
      }
19 ✔
1678
};
1679

1680
BOTAN_REGISTER_TEST("utils", "ipv4_subnet_parse", IPv4_Subnet_Parsing_Tests);
1681

1682
#endif
1683

1684
#if defined(BOTAN_HAS_IPV6_ADDRESS)
1685

1686
class IPv6_Parsing_Tests final : public Text_Based_Test {
×
1687
   public:
1688
      IPv6_Parsing_Tests() : Text_Based_Test("utils/ipv6.vec", "IPv6") {}
2 ✔
1689

1690
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
50 ✔
1691
         Test::Result result("IPv6 parsing");
50 ✔
1692

1693
         const std::string input = vars.get_req_str("IPv6");
50 ✔
1694
         const bool valid = (header == "Valid");
50 ✔
1695

1696
         auto ipv6 = Botan::IPv6Address::from_string(input);
50 ✔
1697

1698
         result.test_bool_eq("IPv6Address::from_string accepts only valid", ipv6.has_value(), valid);
50 ✔
1699

1700
         if(ipv6) {
50 ✔
1701
            const std::string rt = ipv6->to_string();
15 ✔
1702
            result.test_str_eq("IPv6Address to_string and from_string round trip", input, rt);
15 ✔
1703
         }
15 ✔
1704

1705
         return result;
50 ✔
1706
      }
50 ✔
1707
};
1708

1709
BOTAN_REGISTER_TEST("utils", "ipv6_parse", IPv6_Parsing_Tests);
1710

1711
class IPv6_Noncanonical_Parsing_Tests final : public Text_Based_Test {
×
1712
   public:
1713
      IPv6_Noncanonical_Parsing_Tests() : Text_Based_Test("utils/ipv6_nc.vec", "Input,Canonical") {}
2 ✔
1714

1715
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
23 ✔
1716
         Test::Result result("IPv6 parsing of non-canonical form");
23 ✔
1717

1718
         const std::string input_str = vars.get_req_str("Input");
23 ✔
1719
         const std::string canonical_str = vars.get_req_str("Canonical");
23 ✔
1720

1721
         const auto ipv6 = Botan::IPv6Address::from_string(input_str);
23 ✔
1722
         const auto canonical = Botan::IPv6Address::from_string(canonical_str);
23 ✔
1723

1724
         result.test_is_true("IPv6 non-canonical parsing worked", ipv6.has_value());
23 ✔
1725
         result.test_is_true("IPv6 canonical parsing worked", canonical.has_value());
23 ✔
1726

1727
         if(ipv6.has_value() && canonical.has_value()) {
23 ✔
1728
            result.test_is_true("IPv6 non-canonical decoding", *ipv6 == *canonical);
23 ✔
1729
            result.test_str_eq("IPv6 to_string is canonical", ipv6->to_string(), canonical_str);
23 ✔
1730
         }
1731

1732
         return result;
23 ✔
1733
      }
23 ✔
1734
};
1735

1736
BOTAN_REGISTER_TEST("utils", "ipv6_parse_non_canonical", IPv6_Noncanonical_Parsing_Tests);
1737

1738
class IPv6_Subnet_Parsing_Tests final : public Text_Based_Test {
×
1739
   public:
1740
      IPv6_Subnet_Parsing_Tests() : Text_Based_Test("utils/ipv6_subnet.vec", "IPv6Subnet", "LastAddress") {}
2 ✔
1741

1742
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
25 ✔
1743
         Test::Result result("IPv6 subnet parsing");
25 ✔
1744

1745
         const std::string input = vars.get_req_str("IPv6Subnet");
25 ✔
1746
         const bool valid = (header == "Valid");
25 ✔
1747

1748
         const auto subnet = Botan::IPv6Subnet::from_string(input);
25 ✔
1749

1750
         result.test_bool_eq("IPv6Subnet::from_string accepts only valid", subnet.has_value(), valid);
25 ✔
1751

1752
         if(subnet) {
25 ✔
1753
            result.test_str_eq(
7 ✔
1754
               "IPv6Subnet::from_string and IPv6Subnet::to_string round trip", subnet->to_string(), input);
7 ✔
1755

1756
            const auto last = Botan::IPv6Address::from_string(vars.get_req_str("LastAddress"));
7 ✔
1757
            result.test_is_true("LastAddress parses", last.has_value());
7 ✔
1758
            if(last) {
7 ✔
1759
               result.test_str_eq("IPv6Subnet::last_address", subnet->last_address().to_string(), last->to_string());
7 ✔
1760
            }
1761
         }
1762

1763
         return result;
25 ✔
1764
      }
25 ✔
1765
};
1766

1767
BOTAN_REGISTER_TEST("utils", "ipv6_subnet_parse", IPv6_Subnet_Parsing_Tests);
1768

1769
#endif
1770

1771
class ReadKV_Tests final : public Text_Based_Test {
×
1772
   public:
1773
      ReadKV_Tests() : Text_Based_Test("utils/read_kv.vec", "Input,Expected") {}
2 ✔
1774

1775
      Test::Result run_one_test(const std::string& status, const VarMap& vars) override {
16 ✔
1776
         Test::Result result("read_kv");
16 ✔
1777

1778
         const bool is_valid = (status == "Valid");
16 ✔
1779

1780
         const std::string input = vars.get_req_str("Input");
16 ✔
1781
         const std::string expected = vars.get_req_str("Expected");
16 ✔
1782

1783
         if(is_valid) {
16 ✔
1784
            confirm_kv(result, Botan::read_kv(input), split_group(expected));
14 ✔
1785
         } else {
1786
            // In this case "expected" is the expected exception message
1787
            result.test_throws("Invalid key value input throws exception", expected, [&]() { Botan::read_kv(input); });
18 ✔
1788
         }
1789
         return result;
16 ✔
1790
      }
16 ✔
1791

1792
   private:
1793
      static std::vector<std::string> split_group(const std::string& str) {
7 ✔
1794
         std::vector<std::string> elems;
7 ✔
1795
         if(str.empty()) {
7 ✔
1796
            return elems;
1797
         }
1798

1799
         std::string substr;
6 ✔
1800
         for(const char c : str) {
115 ✔
1801
            if(c == '|') {
109 ✔
1802
               elems.push_back(substr);
16 ✔
1803
               substr.clear();
16 ✔
1804
            } else {
1805
               substr += c;
202 ✔
1806
            }
1807
         }
1808

1809
         if(!substr.empty()) {
6 ✔
1810
            elems.push_back(substr);
6 ✔
1811
         }
1812

1813
         return elems;
6 ✔
1814
      }
6 ✔
1815

1816
      static void confirm_kv(Test::Result& result,
7 ✔
1817
                             const std::map<std::string, std::string>& kv,
1818
                             const std::vector<std::string>& expected) {
1819
         if(!result.test_sz_eq("expected size", expected.size() % 2, size_t(0))) {
7 ✔
1820
            return;
1821
         }
1822

1823
         for(size_t i = 0; i != expected.size(); i += 2) {
18 ✔
1824
            const auto j = kv.find(expected[i]);
11 ✔
1825
            if(result.test_is_true("Found key", j != kv.end())) {
11 ✔
1826
               result.test_str_eq("Matching value", j->second, expected[i + 1]);
11 ✔
1827
            }
1828
         }
1829

1830
         result.test_sz_eq("KV has same size as expected", kv.size(), expected.size() / 2);
7 ✔
1831
      }
1832
};
1833

1834
BOTAN_REGISTER_TEST("utils", "util_read_kv", ReadKV_Tests);
1835

1836
#if defined(BOTAN_HAS_CPUID)
1837

1838
class CPUID_Tests final : public Test {
1 ✔
1839
   public:
1840
      std::vector<Test::Result> run() override {
1 ✔
1841
         Test::Result result("CPUID");
1 ✔
1842

1843
         const std::string cpuid_string = Botan::CPUID::to_string();
1 ✔
1844
         result.test_success("CPUID::to_string doesn't crash");
1 ✔
1845

1846
         for(size_t b = 0; b != 32; ++b) {
33 ✔
1847
            try {
32 ✔
1848
               const auto bit = static_cast<uint32_t>(1) << b;
32 ✔
1849
               // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
1850
               const auto feat = Botan::CPUID::Feature(static_cast<Botan::CPUID::Feature::Bit>(bit));
32 ✔
1851

1852
               const std::string feat_str = feat.to_string();
32 ✔
1853

1854
               result.test_is_true("Feature string is not empty", !feat_str.empty());
21 ✔
1855

1856
               if(auto from_str = Botan::CPUID::Feature::from_string(feat_str)) {
21 ✔
1857
                  result.test_u32_eq("Feature::from_string returns expected bit", from_str->as_u32(), bit);
21 ✔
1858
               } else {
1859
                  result.test_failure(
×
1860
                     Botan::fmt("Feature::from_string didn't recognize its own output ({})", feat_str));
×
1861
               }
1862
            } catch(Botan::Invalid_State&) {
32 ✔
1863
               // This will thrown if the bit is not a valid one
1864
            }
11 ✔
1865
         }
1866

1867
   #if defined(BOTAN_TARGET_ARCH_IS_X86_FAMILY)
1868

1869
         const auto bit = Botan::CPUID::Feature::SSE2;
1 ✔
1870

1871
         if(Botan::CPUID::has(bit)) {
1 ✔
1872
            result.test_is_true("Output string includes sse2", cpuid_string.find("sse2") != std::string::npos);
1 ✔
1873

1874
            Botan::CPUID::clear_cpuid_bit(bit);
1 ✔
1875

1876
            result.test_is_false("After clearing cpuid bit, CPUID::has for SSE2 returns false", Botan::CPUID::has(bit));
1 ✔
1877

1878
            Botan::CPUID::initialize();  // reset state
1 ✔
1879
            result.test_is_true("After reinitializing, CPUID::has for SSE2 returns true again", Botan::CPUID::has(bit));
1 ✔
1880
         }
1881
   #else
1882
         BOTAN_UNUSED(cpuid_string);
1883
   #endif
1884

1885
         return {result};
3 ✔
1886
      }
2 ✔
1887
};
1888

1889
BOTAN_REGISTER_SERIALIZED_TEST("utils", "cpuid", CPUID_Tests);
1890

1891
#endif
1892

1893
#if defined(BOTAN_HAS_UUID)
1894

1895
class UUID_Tests : public Test {
1 ✔
1896
   public:
1897
      std::vector<Test::Result> run() override {
1 ✔
1898
         Test::Result result("UUID");
1 ✔
1899

1900
         const Botan::UUID empty_uuid;
1 ✔
1901
         const Botan::UUID random_uuid1(this->rng());
1 ✔
1902
         const Botan::UUID random_uuid2(this->rng());
1 ✔
1903
         const Botan::UUID loaded_uuid(std::vector<uint8_t>(16, 4));
1 ✔
1904

1905
         result.test_throws("Cannot load wrong number of bytes",
1 ✔
1906
                            []() { const Botan::UUID u(std::vector<uint8_t>(15)); });
1 ✔
1907

1908
         result.test_is_false("Empty UUID is empty", empty_uuid.is_valid());
1 ✔
1909
         result.test_is_true("Empty UUID equals another empty UUID", empty_uuid == Botan::UUID());
1 ✔
1910

1911
         result.test_throws("Empty UUID cannot become a string", [&]() { empty_uuid.to_string(); });
2 ✔
1912

1913
         result.test_is_true("Random UUID not empty", random_uuid1.is_valid());
1 ✔
1914
         result.test_is_true("Random UUID not empty", random_uuid2.is_valid());
1 ✔
1915

1916
         result.test_is_true("Random UUIDs are distinct", random_uuid1 != random_uuid2);
1 ✔
1917
         result.test_is_true("Random UUIDs not equal to empty", random_uuid1 != empty_uuid);
1 ✔
1918

1919
         const std::string uuid4_str = loaded_uuid.to_string();
1 ✔
1920
         result.test_str_eq("String matches expected", uuid4_str, "04040404-0404-0404-0404-040404040404");
1 ✔
1921

1922
         const std::string uuid_r1_str = random_uuid1.to_string();
1 ✔
1923
         result.test_is_true("UUID from string matches", Botan::UUID(uuid_r1_str) == random_uuid1);
1 ✔
1924

1925
         class AllSame_RNG : public Botan::RandomNumberGenerator {
×
1926
            public:
1927
               explicit AllSame_RNG(uint8_t b) : m_val(b) {}
2 ✔
1928

1929
               void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> /* ignored */) override {
2 ✔
1930
                  std::fill(output.begin(), output.end(), m_val);
2 ✔
1931
               }
2 ✔
1932

1933
               std::string name() const override { return "zeros"; }
×
1934

1935
               bool accepts_input() const override { return false; }
×
1936

1937
               void clear() override {}
×
1938

1939
               bool is_seeded() const override { return true; }
×
1940

1941
            private:
1942
               uint8_t m_val;
1943
         };
1944

1945
         AllSame_RNG zeros(0x00);
1 ✔
1946
         const Botan::UUID zero_uuid(zeros);
1 ✔
1947
         result.test_str_eq(
1 ✔
1948
            "Zero UUID matches expected", zero_uuid.to_string(), "00000000-0000-4000-8000-000000000000");
1 ✔
1949

1950
         AllSame_RNG ones(0xFF);
1 ✔
1951
         const Botan::UUID ones_uuid(ones);
1 ✔
1952
         result.test_str_eq(
1 ✔
1953
            "Ones UUID matches expected", ones_uuid.to_string(), "FFFFFFFF-FFFF-4FFF-BFFF-FFFFFFFFFFFF");
1 ✔
1954

1955
         return {result};
3 ✔
1956
      }
6 ✔
1957
};
1958

1959
BOTAN_REGISTER_TEST("utils", "uuid", UUID_Tests);
1960

1961
#endif
1962

1963
class Formatter_Tests : public Test {
1 ✔
1964
   public:
1965
      std::vector<Test::Result> run() override {
1 ✔
1966
         Test::Result result("Format utility");
1 ✔
1967

1968
         /*
1969
         In a number of these tests, we are not strictly depending on the
1970
         behavior, for instance checking `fmt("{}") == "{}"` is more about
1971
         checking that we don't crash, rather than we return that precise string.
1972
         */
1973

1974
         result.test_str_eq("test 1", Botan::fmt("hi"), "hi");
1 ✔
1975
         result.test_str_eq("test 2", Botan::fmt("ignored", 5), "ignored");
1 ✔
1976
         result.test_str_eq("test 3", Botan::fmt("answer is {}", 42), "answer is 42");
1 ✔
1977
         result.test_str_eq("test 4", Botan::fmt("{", 5), "{");
1 ✔
1978
         result.test_str_eq("test 4", Botan::fmt("{}"), "{}");
1 ✔
1979
         result.test_str_eq("test 5", Botan::fmt("{} == '{}'", 5, "five"), "5 == 'five'");
1 ✔
1980

1981
         return {result};
3 ✔
1982
      }
2 ✔
1983
};
1984

1985
BOTAN_REGISTER_TEST("utils", "fmt", Formatter_Tests);
1986

1987
class ScopedCleanup_Tests : public Test {
1 ✔
1988
   public:
1989
      std::vector<Test::Result> run() override {
1 ✔
1990
         return {
1 ✔
1991
            CHECK("leaving a scope results in cleanup",
1992
                  [](Test::Result& result) {
1 ✔
1993
                     bool ran = false;
1 ✔
1994
                     {
1 ✔
1995
                        const auto clean = Botan::scoped_cleanup([&] { ran = true; });
1 ✔
1996
                     }
1 ✔
1997
                     result.test_is_true("cleanup ran", ran);
1 ✔
1998
                  }),
1 ✔
1999

2000
            CHECK("leaving a function, results in cleanup",
2001
                  [](Test::Result& result) {
1 ✔
2002
                     bool ran = false;
1 ✔
2003
                     bool fn_called = false;
1 ✔
2004
                     const auto fn = [&] {
2 ✔
2005
                        const auto clean = Botan::scoped_cleanup([&] { ran = true; });
1 ✔
2006
                        fn_called = true;
1 ✔
2007
                     };
2 ✔
2008

2009
                     result.test_is_true("cleanup not yet ran", !ran);
1 ✔
2010
                     fn();
1 ✔
2011
                     result.test_is_true("fn called", fn_called);
1 ✔
2012
                     result.test_is_true("cleanup ran", ran);
1 ✔
2013
                  }),
1 ✔
2014

2015
            CHECK("stack unwinding results in cleanup",
2016
                  [](Test::Result& result) {
1 ✔
2017
                     bool ran = false;
1 ✔
2018
                     bool fn_called = false;
1 ✔
2019
                     bool exception_caught = false;
1 ✔
2020
                     const auto fn = [&] {
2 ✔
2021
                        const auto clean = Botan::scoped_cleanup([&] { ran = true; });
1 ✔
2022
                        fn_called = true;
1 ✔
2023
                        throw std::runtime_error("test");
1 ✔
2024
                     };
2 ✔
2025

2026
                     result.test_is_true("cleanup not yet ran", !ran);
1 ✔
2027
                     try {
1 ✔
2028
                        fn();
1 ✔
2029
                     } catch(const std::exception&) {
1 ✔
2030
                        exception_caught = true;
1 ✔
2031
                     }
1 ✔
2032

2033
                     result.test_is_true("fn called", fn_called);
1 ✔
2034
                     result.test_is_true("cleanup ran", ran);
1 ✔
2035
                     result.test_is_true("exception caught", exception_caught);
1 ✔
2036
                  }),
1 ✔
2037

2038
            CHECK("cleanup isn't called after disengaging",
2039
                  [](Test::Result& result) {
1 ✔
2040
                     bool ran = false;
1 ✔
2041
                     {
1 ✔
2042
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1 ✔
2043
                        clean.disengage();
1 ✔
2044
                     }
1 ✔
2045
                     result.test_is_true("cleanup not ran", !ran);
1 ✔
2046
                  }),
1 ✔
2047

2048
         };
5 ✔
2049
      }
1 ✔
2050
};
2051

2052
BOTAN_REGISTER_TEST("utils", "scoped_cleanup", ScopedCleanup_Tests);
2053

2054
}  // namespace
2055

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