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

randombit / botan / 20160266155

12 Dec 2025 08:01AM UTC coverage: 90.357% (-0.001%) from 90.358%
20160266155

push

github

web-flow
Merge pull request #5172 from randombit/jack/fix-clang-tidy-misc-const-correctness

Fix and enable clang-tidy warning misc-const-correctness

100951 of 111725 relevant lines covered (90.36%)

12817908.54 hits per line

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

94.99
/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
#include <botan/version.h>
11
#include <botan/internal/bit_ops.h>
12
#include <botan/internal/calendar.h>
13
#include <botan/internal/charset.h>
14
#include <botan/internal/fmt.h>
15
#include <botan/internal/int_utils.h>
16
#include <botan/internal/loadstor.h>
17
#include <botan/internal/parsing.h>
18
#include <botan/internal/rounding.h>
19
#include <botan/internal/stl_util.h>
20
#include <botan/internal/target_info.h>
21

22
#include <bit>
23
#include <ctime>
24
#include <functional>
25

26
#if defined(BOTAN_HAS_CPUID)
27
   #include <botan/internal/cpuid.h>
28
#endif
29

30
#if defined(BOTAN_HAS_POLY_DBL)
31
   #include <botan/internal/poly_dbl.h>
32
#endif
33

34
#if defined(BOTAN_HAS_UUID)
35
   #include <botan/uuid.h>
36
#endif
37

38
namespace Botan_Tests {
39

40
namespace {
41

42
class Utility_Function_Tests final : public Test {
×
43
   public:
44
      std::vector<Test::Result> run() override {
1 ✔
45
         std::vector<Test::Result> results;
1 ✔
46

47
         results.push_back(test_checked_add());
2 ✔
48
         results.push_back(test_checked_mul());
2 ✔
49
         results.push_back(test_checked_cast());
2 ✔
50
         results.push_back(test_round_up());
2 ✔
51
         results.push_back(test_loadstore());
2 ✔
52
         results.push_back(test_loadstore_ambiguity());
2 ✔
53
         results.push_back(test_loadstore_fallback());
2 ✔
54
         results.push_back(test_loadstore_constexpr());
2 ✔
55
         return Botan::concat(results, test_copy_out_be_le());
3 ✔
56
      }
1 ✔
57

58
   private:
59
      Test::Result test_checked_add() {
1 ✔
60
         Test::Result result("checked_add");
1 ✔
61

62
         const size_t large = static_cast<size_t>(-5);
1 ✔
63
         const size_t zero = 0;
1 ✔
64

65
         for(int si = -15; si != 15; ++si) {
31 ✔
66
            const size_t i = static_cast<size_t>(si);
30 ✔
67
            auto sum1 = Botan::checked_add<size_t>(i, zero, zero, zero, large);
30 ✔
68
            auto sum2 = Botan::checked_add<size_t>(large, zero, zero, zero, i);
30 ✔
69

70
            result.confirm("checked_add looks at all args", sum1 == sum2);
90 ✔
71

72
            if(i < 5) {
30 ✔
73
               result.test_eq("checked_add worked", sum1.value(), i + large);
10 ✔
74
            } else {
75
               result.confirm("checked_add did not return a result", !sum1.has_value());
50 ✔
76
            }
77
         }
78

79
         auto& rng = Test::rng();
1 ✔
80

81
         for(size_t i = 0; i != 100; ++i) {
101 ✔
82
            const uint16_t x = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100 ✔
83
            const uint16_t y = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100 ✔
84

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

87
            if(auto z = Botan::checked_add(x, y)) {
200 ✔
88
               result.test_int_eq("checked_add adds", z.value(), ref);
88 ✔
89
            } else {
90
               result.confirm("checked_add checks", (ref >> 16) > 0);
112 ✔
91
            }
92
         }
93

94
         return result;
1 ✔
95
      }
×
96

97
      Test::Result test_checked_mul() {
1 ✔
98
         Test::Result result("checked_mul");
1 ✔
99

100
         auto& rng = Test::rng();
1 ✔
101

102
         for(size_t i = 0; i != 100; ++i) {
101 ✔
103
            const uint16_t x = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100 ✔
104
            const uint16_t y = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100 ✔
105

106
            const uint32_t ref = static_cast<uint32_t>(x) * y;
100 ✔
107

108
            if(auto z = Botan::checked_mul(x, y)) {
200 ✔
109
               result.test_int_eq("checked_mul multiplies", z.value(), ref);
×
110
            } else {
111
               result.confirm("checked_mul checks", (ref >> 16) > 0);
200 ✔
112
            }
113
         }
114

115
         return result;
1 ✔
116
      }
×
117

118
      Test::Result test_checked_cast() {
1 ✔
119
         Test::Result result("checked_cast");
1 ✔
120

121
         const uint32_t large = static_cast<uint32_t>(-1);
1 ✔
122
         const uint32_t is_16_bits = 0x8123;
1 ✔
123
         const uint32_t is_8_bits = 0x89;
1 ✔
124

125
         result.test_throws("checked_cast checks", [&] { Botan::checked_cast_to<uint16_t>(large); });
3 ✔
126
         result.test_throws("checked_cast checks", [&] { Botan::checked_cast_to<uint8_t>(large); });
3 ✔
127

128
         result.test_int_eq("checked_cast converts", Botan::checked_cast_to<uint32_t>(large), large);
2 ✔
129
         result.test_int_eq("checked_cast converts", Botan::checked_cast_to<uint16_t>(is_16_bits), 0x8123);
2 ✔
130
         result.test_int_eq("checked_cast converts", Botan::checked_cast_to<uint8_t>(is_8_bits), 0x89);
2 ✔
131

132
         return result;
1 ✔
133
      }
×
134

135
      Test::Result test_round_up() {
1 ✔
136
         Test::Result result("Util round_up");
1 ✔
137

138
         // clang-format off
139
         const std::vector<size_t> inputs = {
1 ✔
140
            0, 1, 2, 3, 4, 9, 10, 32, 99, 100, 101, 255, 256, 1000, 10000,
141
            65535, 65536, 65537,
142
         };
1 ✔
143

144
         const std::vector<size_t> alignments = {
1 ✔
145
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 32, 50, 64, 100, 512, 521,
146
            1000, 1023, 1024, 1025, 10000, 65535, 65536
147
         };
1 ✔
148
         // clang-format on
149

150
         for(const size_t i : inputs) {
19 ✔
151
            for(const size_t m : alignments) {
450 ✔
152
               try {
432 ✔
153
                  const size_t z = Botan::round_up(i, m);
432 ✔
154

155
                  result.confirm("z % m == 0", z % m == 0);
864 ✔
156
                  result.confirm("z >= i", z >= i);
864 ✔
157
                  result.confirm("z <= i + m", z <= i + m);
864 ✔
158
               } catch(Botan::Exception& e) {
×
159
                  result.test_failure(Botan::fmt("round_up({},{})", i, m), e.what());
×
160
               }
×
161
            }
162
         }
163

164
         result.test_throws("Integer overflow is detected", []() { Botan::round_up(static_cast<size_t>(-1), 1024); });
2 ✔
165

166
         return result;
1 ✔
167
      }
1 ✔
168

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

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

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

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

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

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

193
         result.test_is_eq<uint8_t>(Botan::get_byte<0>(in32), 0xA0);
1 ✔
194
         result.test_is_eq<uint8_t>(Botan::get_byte<1>(in32), 0xB0);
1 ✔
195
         result.test_is_eq<uint8_t>(Botan::get_byte<2>(in32), 0xC0);
1 ✔
196
         result.test_is_eq<uint8_t>(Botan::get_byte<3>(in32), 0xD0);
1 ✔
197

198
         result.test_is_eq<uint16_t>(Botan::make_uint16(0xAA, 0xBB), 0xAABB);
1 ✔
199
         result.test_is_eq<uint32_t>(Botan::make_uint32(0x01, 0x02, 0x03, 0x04), 0x01020304);
1 ✔
200

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

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

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

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

221
         result.test_is_eq<uint64_t>(Botan::load_be<uint64_t>(mem, 0), 0x0011223344556677);
1 ✔
222
         result.test_is_eq<uint64_t>(Botan::load_be<uint64_t>(mem, 1), 0x8899AABBCCDDEEFF);
1 ✔
223

224
         result.test_is_eq<uint64_t>(Botan::load_le<uint64_t>(mem, 0), 0x7766554433221100);
1 ✔
225
         result.test_is_eq<uint64_t>(Botan::load_le<uint64_t>(mem, 1), 0xFFEEDDCCBBAA9988);
1 ✔
226

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

231
         result.test_is_eq<uint32_t>(Botan::load_be<uint32_t>(mem + 1, 1), 0x55667788);
1 ✔
232
         result.test_is_eq<uint32_t>(Botan::load_le<uint32_t>(mem + 3, 1), 0xAA998877);
1 ✔
233

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

238
         uint8_t outbuf[16] = {0};
1 ✔
239

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

243
            Botan::store_be(in16, out);
7 ✔
244
            result.test_is_eq<uint8_t>(out[0], 0x12);
7 ✔
245
            result.test_is_eq<uint8_t>(out[1], 0x34);
7 ✔
246

247
            Botan::store_le(in16, out);
7 ✔
248
            result.test_is_eq<uint8_t>(out[0], 0x34);
7 ✔
249
            result.test_is_eq<uint8_t>(out[1], 0x12);
7 ✔
250

251
            Botan::store_be(in32, out);
7 ✔
252
            result.test_is_eq<uint8_t>(out[0], 0xA0);
7 ✔
253
            result.test_is_eq<uint8_t>(out[1], 0xB0);
7 ✔
254
            result.test_is_eq<uint8_t>(out[2], 0xC0);
7 ✔
255
            result.test_is_eq<uint8_t>(out[3], 0xD0);
7 ✔
256

257
            Botan::store_le(in32, out);
7 ✔
258
            result.test_is_eq<uint8_t>(out[0], 0xD0);
7 ✔
259
            result.test_is_eq<uint8_t>(out[1], 0xC0);
7 ✔
260
            result.test_is_eq<uint8_t>(out[2], 0xB0);
7 ✔
261
            result.test_is_eq<uint8_t>(out[3], 0xA0);
7 ✔
262

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

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

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

291
         Botan::load_be(outarr, i0, i1, i2, i3);
1 ✔
292
         result.test_is_eq<uint16_t>(i0, 0xABCD);
1 ✔
293
         result.test_is_eq<uint16_t>(i1, 0xEF01);
1 ✔
294
         result.test_is_eq<uint16_t>(i2, 0x2345);
1 ✔
295
         result.test_is_eq<uint16_t>(i3, 0x6789);
1 ✔
296

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

303
         Botan::store_le(in64, outarr);
1 ✔
304

305
         Botan::load_le(outarr, i0, i1, i2, i3);
1 ✔
306
         result.test_is_eq<uint16_t>(i0, 0x6789);
1 ✔
307
         result.test_is_eq<uint16_t>(i1, 0x2345);
1 ✔
308
         result.test_is_eq<uint16_t>(i2, 0xEF01);
1 ✔
309
         result.test_is_eq<uint16_t>(i3, 0xABCD);
1 ✔
310

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

317
         i0 = 0xAA11;
1 ✔
318
         i1 = 0xBB22;
1 ✔
319
         i2 = 0xCC33;
1 ✔
320
         i3 = 0xDD44;
1 ✔
321
         Botan::store_be(outarr, i0, i1, i2, i3);
1 ✔
322
         result.test_is_eq(outarr, {0xAA, 0x11, 0xBB, 0x22, 0xCC, 0x33, 0xDD, 0x44});
1 ✔
323
         std::vector<uint8_t> outvec(8);
1 ✔
324
         Botan::store_be(outvec, i0, i1, i2, i3);
1 ✔
325
         result.test_is_eq(outvec, Botan::hex_decode("AA11BB22CC33DD44"));
1 ✔
326

327
         Botan::store_le(outarr, i0, i1, i2, i3);
1 ✔
328
         result.test_is_eq(outarr, {0x11, 0xAA, 0x22, 0xBB, 0x33, 0xCC, 0x44, 0xDD});
1 ✔
329
         Botan::store_le(outvec, i0, i1, i2, i3);
1 ✔
330
         result.test_is_eq(outvec, Botan::hex_decode("11AA22BB33CC44DD"));
1 ✔
331

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

345
         // can store multiple values straight into a collection
346
         auto out64_array_be = Botan::store_be(i0, i1, i2, i3);
1 ✔
347
         auto out64_vec_be = Botan::store_be<std::vector<uint8_t>>(i0, i1, i2, i3);
1 ✔
348
         auto out64_strong_be = Botan::store_be<TestVectorSink>(i0, i1, i2, i3);
1 ✔
349
         result.test_is_eq(out64_array_be, {0xAA, 0x11, 0xBB, 0x22, 0xCC, 0x33, 0xDD, 0x44});
1 ✔
350
         result.test_is_eq(out64_vec_be, Botan::hex_decode("AA11BB22CC33DD44"));
1 ✔
351
         result.test_is_eq(out64_strong_be, TestVectorSink(Botan::hex_decode("AA11BB22CC33DD44")));
2 ✔
352
         auto out64_array_le = Botan::store_le(i0, i1, i2, i3);
1 ✔
353
         auto out64_vec_le = Botan::store_le<std::vector<uint8_t>>(i0, i1, i2, i3);
1 ✔
354
         auto out64_strong_le = Botan::store_le<TestVectorSink>(i0, i1, i2, i3);
1 ✔
355
         result.test_is_eq(out64_array_le, {0x11, 0xAA, 0x22, 0xBB, 0x33, 0xCC, 0x44, 0xDD});
1 ✔
356
         result.test_is_eq(out64_vec_le, Botan::hex_decode("11AA22BB33CC44DD"));
1 ✔
357
         result.test_is_eq(out64_strong_le, TestVectorSink(Botan::hex_decode("11AA22BB33CC44DD")));
2 ✔
358

359
         result.test_is_eq(in16, Botan::load_be(Botan::store_be(in16)));
1 ✔
360
         result.test_is_eq(in32, Botan::load_be(Botan::store_be(in32)));
1 ✔
361
         result.test_is_eq(in64, Botan::load_be(Botan::store_be(in64)));
1 ✔
362

363
         result.test_is_eq(in16, Botan::load_le(Botan::store_le(in16)));
1 ✔
364
         result.test_is_eq(in32, Botan::load_le(Botan::store_le(in32)));
1 ✔
365
         result.test_is_eq(in64, Botan::load_le(Botan::store_le(in64)));
1 ✔
366

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

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

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

397
         const std::vector<uint16_t> in16_vector = {0x0A0B, 0x0C0D};
1 ✔
398
         result.test_is_eq(Botan::store_be<std::vector<uint8_t>>(in16_vector), Botan::hex_decode("0A0B0C0D"));
3 ✔
399
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(in16_vector), Botan::hex_decode("0B0A0D0C"));
3 ✔
400

401
         std::array<uint8_t, 4> out_array{};
1 ✔
402
         Botan::store_be(out_array, in16_array);
1 ✔
403
         result.test_is_eq(out_array, std::array<uint8_t, 4>{0x0A, 0x0B, 0x0C, 0x0D});
1 ✔
404
         Botan::store_le(out_array, in16_array);
1 ✔
405
         result.test_is_eq(out_array, std::array<uint8_t, 4>{0x0B, 0x0A, 0x0D, 0x0C});
1 ✔
406

407
         const auto be_inferred = Botan::store_be(in16_array);
1 ✔
408
         result.test_is_eq(be_inferred, std::array<uint8_t, 4>{0x0A, 0x0B, 0x0C, 0x0D});
1 ✔
409
         const auto le_inferred = Botan::store_le(in16_array);
1 ✔
410
         result.test_is_eq(le_inferred, std::array<uint8_t, 4>{0x0B, 0x0A, 0x0D, 0x0C});
1 ✔
411

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

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

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

434
         result.test_is_eq(Botan::store_be<std::vector<uint8_t>>(in64_strong), Botan::hex_decode("ABCDEF0123456789"));
3 ✔
435
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(in64_strong), Botan::hex_decode("8967452301EFCDAB"));
3 ✔
436
         result.test_is_eq(Botan::store_be<std::vector<uint8_t>>(in32_strong), Botan::hex_decode("ABCDEF01"));
3 ✔
437
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(in32_strong), Botan::hex_decode("01EFCDAB"));
3 ✔
438

439
         result.test_is_eq(Botan::load_be<TestInt64>(Botan::hex_decode("ABCDEF0123456789")), in64_strong);
2 ✔
440
         result.test_is_eq(Botan::load_le<TestInt64>(Botan::hex_decode("8967452301EFCDAB")), in64_strong);
2 ✔
441
         result.test_is_eq(Botan::load_be<TestInt32>(Botan::hex_decode("ABCDEF01")), in32_strong);
2 ✔
442
         result.test_is_eq(Botan::load_le<TestInt32>(Botan::hex_decode("01EFCDAB")), in32_strong);
2 ✔
443

444
         const std::vector<TestInt64> some_in64_strongs{TestInt64{0xABCDEF0123456789}, TestInt64{0x0123456789ABCDEF}};
1 ✔
445
         result.test_is_eq(Botan::store_be<std::vector<uint8_t>>(some_in64_strongs),
3 ✔
446
                           Botan::hex_decode("ABCDEF01234567890123456789ABCDEF"));
1 ✔
447
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(some_in64_strongs),
3 ✔
448
                           Botan::hex_decode("8967452301EFCDABEFCDAB8967452301"));
1 ✔
449

450
         const auto in64_strongs_le =
1 ✔
451
            Botan::load_le<std::array<TestInt64, 2>>(Botan::hex_decode("8967452301EFCDABEFCDAB8967452301"));
2 ✔
452
         result.test_is_eq(in64_strongs_le[0], TestInt64{0xABCDEF0123456789});
1 ✔
453
         result.test_is_eq(in64_strongs_le[1], TestInt64{0x0123456789ABCDEF});
1 ✔
454

455
         const auto in64_strongs_be =
1 ✔
456
            Botan::load_be<std::vector<TestInt64>>(Botan::hex_decode("ABCDEF01234567890123456789ABCDEF"));
2 ✔
457
         result.test_is_eq(in64_strongs_be[0], TestInt64{0xABCDEF0123456789});
1 ✔
458
         result.test_is_eq(in64_strongs_be[1], TestInt64{0x0123456789ABCDEF});
1 ✔
459

460
         // Test loading/storing of enum types with different endianness
461
         const auto in64_enum_le = Botan::load_le<TestEnum64>(Botan::hex_decode("1234567890ABCDEF"));
2 ✔
462
         result.test_is_eq(in64_enum_le, TestEnum64::_2);
1 ✔
463
         const auto in64_enum_be = Botan::load_be<TestEnum64>(Botan::hex_decode("1234567890ABCDEF"));
2 ✔
464
         result.test_is_eq(in64_enum_be, TestEnum64::_1);
1 ✔
465
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(TestEnum64::_1),
2 ✔
466
                           Botan::hex_decode("EFCDAB9078563412"));
1 ✔
467
         result.test_is_eq<std::array<uint8_t, 8>>(Botan::store_be(TestEnum64::_2),
1 ✔
468
                                                   {0xEF, 0xCD, 0xAB, 0x90, 0x78, 0x56, 0x34, 0x12});
469

470
         const auto in32_enum_le = Botan::load_le<TestEnum32>(Botan::hex_decode("78563412"));
2 ✔
471
         result.test_is_eq(in32_enum_le, TestEnum32::_1);
1 ✔
472
         const auto in32_enum_be = Botan::load_be<TestEnum32>(Botan::hex_decode("78563412"));
2 ✔
473
         result.test_is_eq(in32_enum_be, TestEnum32::_2);
1 ✔
474
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(TestEnum32::_1), Botan::hex_decode("78563412"));
2 ✔
475
         result.test_is_eq<std::array<uint8_t, 4>>(Botan::store_be(TestEnum32::_2), {0x78, 0x56, 0x34, 0x12});
1 ✔
476

477
         return result;
2 ✔
478
      }
11 ✔
479

480
      template <std::unsigned_integral T>
481
      static T fb_load_be(std::array<const uint8_t, sizeof(T)> in) {
3 ✔
482
         return Botan::detail::fallback_load_any<std::endian::big, T>(in);
3 ✔
483
      }
484

485
      template <std::unsigned_integral T>
486
      static T fb_load_le(std::array<const uint8_t, sizeof(T)> in) {
3 ✔
487
         return Botan::detail::fallback_load_any<std::endian::little, T>(in);
3 ✔
488
      }
489

490
      template <std::unsigned_integral T>
491
      static decltype(auto) fb_store_be(const T in) {
3 ✔
492
         std::array<uint8_t, sizeof(T)> out{};
3 ✔
493
         Botan::detail::fallback_store_any<std::endian::big, T>(in, out);
1 ✔
494
         return out;
2 ✔
495
      }
496

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

504
      template <size_t N>
505
      using a = std::array<uint8_t, N>;
506

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

515
         const uint32_t in32 = 0x01234567;
1 ✔
516
         const uint64_t in64 = 0x0123456789ABCDEF;
1 ✔
517
         const size_t inszt = 0x87654321;
1 ✔
518

519
         Test::Result result("Util load/store ambiguity");
1 ✔
520
         const auto out_be_32 = Botan::store_be(in32);
1 ✔
521
         const auto out_le_32 = Botan::store_le(in32);
1 ✔
522
         const auto out_be_64 = Botan::store_be(in64);
1 ✔
523
         const auto out_le_64 = Botan::store_le(in64);
1 ✔
524
         const auto out_be_szt = Botan::store_be(inszt);
1 ✔
525
         const auto out_le_szt = Botan::store_le(inszt);
1 ✔
526

527
         result.test_is_eq<uint32_t>("be 32", Botan::load_be<uint32_t>(out_be_32), in32);
1 ✔
528
         result.test_is_eq<uint32_t>("le 32", Botan::load_le<uint32_t>(out_le_32), in32);
1 ✔
529
         result.test_is_eq<uint64_t>("be 64", Botan::load_be<uint64_t>(out_be_64), in64);
1 ✔
530
         result.test_is_eq<uint64_t>("le 64", Botan::load_le<uint64_t>(out_le_64), in64);
1 ✔
531
         result.test_is_eq<size_t>("be szt", Botan::load_be<size_t>(out_be_szt), inszt);
1 ✔
532
         result.test_is_eq<size_t>("le szt", Botan::load_le<size_t>(out_le_szt), inszt);
1 ✔
533

534
         return result;
1 ✔
535
      }
×
536

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

544
         result.test_is_eq<uint16_t>("lLE 16", fb_load_le<uint16_t>({1, 2}), 0x0201);
1 ✔
545
         result.test_is_eq<uint32_t>("lLE 32", fb_load_le<uint32_t>({1, 2, 3, 4}), 0x04030201);
1 ✔
546
         result.test_is_eq<uint64_t>("lLE 64", fb_load_le<uint64_t>({1, 2, 3, 4, 5, 6, 7, 8}), 0x0807060504030201);
1 ✔
547

548
         result.test_is_eq<uint16_t>("lBE 16", fb_load_be<uint16_t>({1, 2}), 0x0102);
1 ✔
549
         result.test_is_eq<uint32_t>("lBE 32", fb_load_be<uint32_t>({1, 2, 3, 4}), 0x01020304);
1 ✔
550
         result.test_is_eq<uint64_t>("lBE 64", fb_load_be<uint64_t>({1, 2, 3, 4, 5, 6, 7, 8}), 0x0102030405060708);
1 ✔
551

552
         result.test_is_eq<a<2>>("sLE 16", fb_store_le<uint16_t>(0x0201), {1, 2});
1 ✔
553
         result.test_is_eq<a<4>>("sLE 32", fb_store_le<uint32_t>(0x04030201), {1, 2, 3, 4});
1 ✔
554
         result.test_is_eq<a<8>>("sLE 64", fb_store_le<uint64_t>(0x0807060504030201), {1, 2, 3, 4, 5, 6, 7, 8});
1 ✔
555

556
         result.test_is_eq<a<2>>("sBE 16", fb_store_be<uint16_t>(0x0102), {1, 2});
1 ✔
557
         result.test_is_eq<a<4>>("sBE 32", fb_store_be<uint32_t>(0x01020304), {1, 2, 3, 4});
1 ✔
558
         result.test_is_eq<a<8>>("sBE 64", fb_store_be<uint64_t>(0x0102030405060708), {1, 2, 3, 4, 5, 6, 7, 8});
1 ✔
559

560
         return result;
1 ✔
561
      }
×
562

563
      static Test::Result test_loadstore_constexpr() {
1 ✔
564
         Test::Result result("Util load/store constexpr");
1 ✔
565

566
         constexpr uint16_t in16 = 0x1234;
1 ✔
567
         constexpr uint32_t in32 = 0xA0B0C0D0;
1 ✔
568
         constexpr uint64_t in64 = 0xABCDEF0123456789;
1 ✔
569

570
         // clang-format off
571
         constexpr std::array<uint8_t, 16> cex_mem = {
1 ✔
572
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
573
         };
574
         // clang-format on
575

576
         // get_byte<> w/ 16bit
577
         constexpr auto cex_byte_16_0 = Botan::get_byte<0>(in16);
1 ✔
578
         result.test_is_eq<uint8_t>(cex_byte_16_0, 0x12);
1 ✔
579
         constexpr auto cex_byte_16_1 = Botan::get_byte<1>(in16);
1 ✔
580
         result.test_is_eq<uint8_t>(cex_byte_16_1, 0x34);
1 ✔
581

582
         // get_byte<> w/ 32bit
583
         constexpr auto cex_byte_32_0 = Botan::get_byte<0>(in32);
1 ✔
584
         result.test_is_eq<uint8_t>(cex_byte_32_0, 0xA0);
1 ✔
585
         constexpr auto cex_byte_32_1 = Botan::get_byte<1>(in32);
1 ✔
586
         result.test_is_eq<uint8_t>(cex_byte_32_1, 0xB0);
1 ✔
587
         constexpr auto cex_byte_32_2 = Botan::get_byte<2>(in32);
1 ✔
588
         result.test_is_eq<uint8_t>(cex_byte_32_2, 0xC0);
1 ✔
589
         constexpr auto cex_byte_32_3 = Botan::get_byte<3>(in32);
1 ✔
590
         result.test_is_eq<uint8_t>(cex_byte_32_3, 0xD0);
1 ✔
591

592
         // get_byte<> w/ 64bit
593
         constexpr auto cex_byte_64_0 = Botan::get_byte<0>(in64);
1 ✔
594
         result.test_is_eq<uint8_t>(cex_byte_64_0, 0xAB);
1 ✔
595
         constexpr auto cex_byte_64_1 = Botan::get_byte<1>(in64);
1 ✔
596
         result.test_is_eq<uint8_t>(cex_byte_64_1, 0xCD);
1 ✔
597
         constexpr auto cex_byte_64_2 = Botan::get_byte<2>(in64);
1 ✔
598
         result.test_is_eq<uint8_t>(cex_byte_64_2, 0xEF);
1 ✔
599
         constexpr auto cex_byte_64_3 = Botan::get_byte<3>(in64);
1 ✔
600
         result.test_is_eq<uint8_t>(cex_byte_64_3, 0x01);
1 ✔
601
         constexpr auto cex_byte_64_4 = Botan::get_byte<4>(in64);
1 ✔
602
         result.test_is_eq<uint8_t>(cex_byte_64_4, 0x23);
1 ✔
603
         constexpr auto cex_byte_64_5 = Botan::get_byte<5>(in64);
1 ✔
604
         result.test_is_eq<uint8_t>(cex_byte_64_5, 0x45);
1 ✔
605
         constexpr auto cex_byte_64_6 = Botan::get_byte<6>(in64);
1 ✔
606
         result.test_is_eq<uint8_t>(cex_byte_64_6, 0x67);
1 ✔
607
         constexpr auto cex_byte_64_7 = Botan::get_byte<7>(in64);
1 ✔
608
         result.test_is_eq<uint8_t>(cex_byte_64_7, 0x89);
1 ✔
609

610
         // make_uintXX()
611
         constexpr auto cex_uint16_t = Botan::make_uint16(0x12, 0x34);
1 ✔
612
         result.test_is_eq<uint16_t>(cex_uint16_t, in16);
1 ✔
613
         constexpr auto cex_uint32_t = Botan::make_uint32(0xA0, 0xB0, 0xC0, 0xD0);
1 ✔
614
         result.test_is_eq<uint32_t>(cex_uint32_t, in32);
1 ✔
615
         constexpr auto cex_uint64_t = Botan::make_uint64(0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89);
1 ✔
616
         result.test_is_eq<uint64_t>(cex_uint64_t, in64);
1 ✔
617

618
         // store_le/be with a single integer
619
         constexpr std::array<uint8_t, 2> cex_store_le16 = Botan::store_le(in16);
1 ✔
620
         result.test_is_eq(cex_store_le16, std::array<uint8_t, 2>{0x34, 0x12});
1 ✔
621
         constexpr std::array<uint8_t, 4> cex_store_le32 = Botan::store_le(in32);
1 ✔
622
         result.test_is_eq(cex_store_le32, std::array<uint8_t, 4>{0xD0, 0xC0, 0xB0, 0xA0});
1 ✔
623
         constexpr std::array<uint8_t, 8> cex_store_le64 = Botan::store_le(in64);
1 ✔
624
         result.test_is_eq(cex_store_le64, std::array<uint8_t, 8>{0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB});
1 ✔
625

626
         constexpr std::array<uint8_t, 2> cex_store_be16 = Botan::store_be(in16);
1 ✔
627
         result.test_is_eq(cex_store_be16, std::array<uint8_t, 2>{0x12, 0x34});
1 ✔
628
         constexpr std::array<uint8_t, 4> cex_store_be32 = Botan::store_be(in32);
1 ✔
629
         result.test_is_eq(cex_store_be32, std::array<uint8_t, 4>{0xA0, 0xB0, 0xC0, 0xD0});
1 ✔
630
         constexpr std::array<uint8_t, 8> cex_store_be64 = Botan::store_be(in64);
1 ✔
631
         result.test_is_eq(cex_store_be64, std::array<uint8_t, 8>{0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89});
1 ✔
632

633
         // store_le/be with multiple integers, both as a parameter pack and a range (std::array for constexpr)
634
         constexpr std::array<uint8_t, 16> cex_store_le16s =
1 ✔
635
            Botan::store_le(in16, in16, in16, in16, in16, in16, in16, in16);
636
         constexpr std::array<uint8_t, 16> cex_store_le16s2 =
1 ✔
637
            Botan::store_le(std::array{in16, in16, in16, in16, in16, in16, in16, in16});
638
         result.test_is_eq(
1 ✔
639
            cex_store_le16s,
640
            {0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12});
641
         result.test_is_eq(cex_store_le16s, cex_store_le16s2);
1 ✔
642
         constexpr std::array<uint8_t, 16> cex_store_le32s = Botan::store_le(in32, in32, in32, in32);
1 ✔
643
         constexpr std::array<uint8_t, 16> cex_store_le32s2 = Botan::store_le(std::array{in32, in32, in32, in32});
1 ✔
644
         result.test_is_eq(
1 ✔
645
            cex_store_le32s,
646
            {0xD0, 0xC0, 0xB0, 0xA0, 0xD0, 0xC0, 0xB0, 0xA0, 0xD0, 0xC0, 0xB0, 0xA0, 0xD0, 0xC0, 0xB0, 0xA0});
647
         result.test_is_eq(cex_store_le32s, cex_store_le32s2);
1 ✔
648
         constexpr std::array<uint8_t, 16> cex_store_le64s = Botan::store_le(in64, in64);
1 ✔
649
         constexpr std::array<uint8_t, 16> cex_store_le64s2 = Botan::store_le(std::array{in64, in64});
1 ✔
650
         result.test_is_eq(
1 ✔
651
            cex_store_le64s,
652
            {0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB});
653
         result.test_is_eq(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_is_eq(
1 ✔
660
            cex_store_be16s,
661
            {0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34});
662
         result.test_is_eq(cex_store_be16s, cex_store_be16s2);
1 ✔
663
         constexpr std::array<uint8_t, 16> cex_store_be32s = Botan::store_be(in32, in32, in32, in32);
1 ✔
664
         constexpr std::array<uint8_t, 16> cex_store_be32s2 = Botan::store_be(std::array{in32, in32, in32, in32});
1 ✔
665
         result.test_is_eq(
1 ✔
666
            cex_store_be32s,
667
            {0xA0, 0xB0, 0xC0, 0xD0, 0xA0, 0xB0, 0xC0, 0xD0, 0xA0, 0xB0, 0xC0, 0xD0, 0xA0, 0xB0, 0xC0, 0xD0});
668
         result.test_is_eq(cex_store_be32s, cex_store_be32s2);
1 ✔
669
         constexpr std::array<uint8_t, 16> cex_store_be64s = Botan::store_be(in64, in64);
1 ✔
670
         constexpr std::array<uint8_t, 16> cex_store_be64s2 = Botan::store_be(std::array{in64, in64});
1 ✔
671
         result.test_is_eq(
1 ✔
672
            cex_store_be64s,
673
            {0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89});
674
         result.test_is_eq(cex_store_be64s, cex_store_be64s2);
1 ✔
675

676
         // load_le/be a single integer
677
         constexpr uint16_t cex_load_le16 = Botan::load_le<uint16_t>(cex_store_le16);
1 ✔
678
         result.test_is_eq(cex_load_le16, in16);
1 ✔
679
         constexpr uint32_t cex_load_le32 = Botan::load_le<uint32_t>(cex_store_le32);
1 ✔
680
         result.test_is_eq(cex_load_le32, in32);
1 ✔
681
         constexpr uint64_t cex_load_le64 = Botan::load_le<uint64_t>(cex_store_le64);
1 ✔
682
         result.test_is_eq(cex_load_le64, in64);
1 ✔
683

684
         constexpr uint16_t cex_load_be16 = Botan::load_be<uint16_t>(cex_store_be16);
1 ✔
685
         result.test_is_eq(cex_load_be16, in16);
1 ✔
686
         constexpr uint32_t cex_load_be32 = Botan::load_be<uint32_t>(cex_store_be32);
1 ✔
687
         result.test_is_eq(cex_load_be32, in32);
1 ✔
688
         constexpr uint64_t cex_load_be64 = Botan::load_be<uint64_t>(cex_store_be64);
1 ✔
689
         result.test_is_eq(cex_load_be64, in64);
1 ✔
690

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

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

706
         return result;
1 ✔
707
      }
×
708

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

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

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

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

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

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

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

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

778
BOTAN_REGISTER_SMOKE_TEST("utils", "util", Utility_Function_Tests);
779

780
class BitOps_Tests final : public Test {
×
781
   public:
782
      std::vector<Test::Result> run() override {
1 ✔
783
         std::vector<Test::Result> results;
1 ✔
784

785
         results.push_back(test_power_of_2());
2 ✔
786
         results.push_back(test_ctz());
2 ✔
787
         results.push_back(test_sig_bytes());
2 ✔
788
         results.push_back(test_popcount());
2 ✔
789
         results.push_back(test_reverse_bits());
2 ✔
790

791
         return results;
1 ✔
792
      }
×
793

794
   private:
795
      template <typename T>
796
      void test_ctz(Test::Result& result, T val, size_t expected) {
6 ✔
797
         result.test_eq("ctz(" + std::to_string(val) + ")", Botan::ctz<T>(val), expected);
24 ✔
798
      }
6 ✔
799

800
      Test::Result test_ctz() {
1 ✔
801
         Test::Result result("ctz");
1 ✔
802
         test_ctz<uint32_t>(result, 0, 32);
1 ✔
803
         test_ctz<uint32_t>(result, 1, 0);
1 ✔
804
         test_ctz<uint32_t>(result, 0x80, 7);
1 ✔
805
         test_ctz<uint32_t>(result, 0x8000000, 27);
1 ✔
806
         test_ctz<uint32_t>(result, 0x8100000, 20);
1 ✔
807
         test_ctz<uint32_t>(result, 0x80000000, 31);
1 ✔
808

809
         return result;
1 ✔
810
      }
×
811

812
      template <typename T>
813
      void test_sig_bytes(Test::Result& result, T val, size_t expected) {
14 ✔
814
         result.test_eq("significant_bytes(" + std::to_string(val) + ")", Botan::significant_bytes<T>(val), expected);
56 ✔
815
      }
14 ✔
816

817
      Test::Result test_sig_bytes() {
1 ✔
818
         Test::Result result("significant_bytes");
1 ✔
819
         test_sig_bytes<uint32_t>(result, 0, 0);
1 ✔
820
         test_sig_bytes<uint32_t>(result, 1, 1);
1 ✔
821
         test_sig_bytes<uint32_t>(result, 0x80, 1);
1 ✔
822
         test_sig_bytes<uint32_t>(result, 255, 1);
1 ✔
823
         test_sig_bytes<uint32_t>(result, 256, 2);
1 ✔
824
         test_sig_bytes<uint32_t>(result, 65535, 2);
1 ✔
825
         test_sig_bytes<uint32_t>(result, 65536, 3);
1 ✔
826
         test_sig_bytes<uint32_t>(result, 0x80000000, 4);
1 ✔
827

828
         test_sig_bytes<uint64_t>(result, 0, 0);
1 ✔
829
         test_sig_bytes<uint64_t>(result, 1, 1);
1 ✔
830
         test_sig_bytes<uint64_t>(result, 0x80, 1);
1 ✔
831
         test_sig_bytes<uint64_t>(result, 256, 2);
1 ✔
832
         test_sig_bytes<uint64_t>(result, 0x80000000, 4);
1 ✔
833
         test_sig_bytes<uint64_t>(result, 0x100000000, 5);
1 ✔
834

835
         return result;
1 ✔
836
      }
×
837

838
      template <typename T>
839
      void test_power_of_2(Test::Result& result, T val, bool expected) {
15 ✔
840
         result.test_eq("power_of_2(" + std::to_string(val) + ")", Botan::is_power_of_2<T>(val), expected);
75 ✔
841
      }
15 ✔
842

843
      Test::Result test_power_of_2() {
1 ✔
844
         Test::Result result("is_power_of_2");
1 ✔
845

846
         test_power_of_2<uint32_t>(result, 0, false);
1 ✔
847
         test_power_of_2<uint32_t>(result, 1, false);
1 ✔
848
         test_power_of_2<uint32_t>(result, 2, true);
1 ✔
849
         test_power_of_2<uint32_t>(result, 3, false);
1 ✔
850
         test_power_of_2<uint32_t>(result, 0x8000, true);
1 ✔
851
         test_power_of_2<uint32_t>(result, 0x8001, false);
1 ✔
852
         test_power_of_2<uint32_t>(result, 0x8000000, true);
1 ✔
853

854
         test_power_of_2<uint64_t>(result, 0, false);
1 ✔
855
         test_power_of_2<uint64_t>(result, 1, false);
1 ✔
856
         test_power_of_2<uint64_t>(result, 2, true);
1 ✔
857
         test_power_of_2<uint64_t>(result, 3, false);
1 ✔
858
         test_power_of_2<uint64_t>(result, 0x8000, true);
1 ✔
859
         test_power_of_2<uint64_t>(result, 0x8001, false);
1 ✔
860
         test_power_of_2<uint64_t>(result, 0x8000000, true);
1 ✔
861
         test_power_of_2<uint64_t>(result, 0x100000000000, true);
1 ✔
862

863
         return result;
1 ✔
864
      }
×
865

866
      template <typename T>
867
      auto pc(T val) -> decltype(Botan::ct_popcount(val)) {
2 ✔
868
         return Botan::ct_popcount(val);
10 ✔
869
      }
870

871
      template <typename T>
872
      auto random_pc(Test::Result& result) {
4 ✔
873
         auto n = Botan::load_le<T>(Test::rng().random_array<sizeof(T)>());
4 ✔
874
         result.test_is_eq<size_t>(Botan::fmt("popcount({}) == {}", n, std::popcount(n)), pc(n), std::popcount(n));
4 ✔
875
      }
4 ✔
876

877
      Test::Result test_popcount() {
1 ✔
878
         Test::Result result("popcount");
1 ✔
879

880
         result.test_is_eq<uint8_t>("popcount<uint8_t>(0)", pc<uint8_t>(0), 0);
1 ✔
881
         result.test_is_eq<uint8_t>("popcount<uint16_t>(0)", pc<uint16_t>(0), 0);
1 ✔
882
         result.test_is_eq<uint8_t>("popcount<uint32_t>(0)", pc<uint32_t>(0), 0);
1 ✔
883
         result.test_is_eq<uint8_t>("popcount<uint64_t>(0)", pc<uint64_t>(0), 0);
1 ✔
884

885
         result.test_is_eq<uint8_t>("popcount<uint8_t>(1)", pc<uint8_t>(1), 1);
1 ✔
886
         result.test_is_eq<uint8_t>("popcount<uint16_t>(1)", pc<uint16_t>(1), 1);
1 ✔
887
         result.test_is_eq<uint8_t>("popcount<uint32_t>(1)", pc<uint32_t>(1), 1);
1 ✔
888
         result.test_is_eq<uint8_t>("popcount<uint64_t>(1)", pc<uint64_t>(1), 1);
1 ✔
889

890
         result.test_is_eq<uint8_t>("popcount<uint8_t>(0xAA)", pc<uint8_t>(0xAA), 4);
1 ✔
891
         result.test_is_eq<uint8_t>("popcount<uint16_t>(0xAAAA)", pc<uint16_t>(0xAAAA), 8);
1 ✔
892
         result.test_is_eq<uint8_t>("popcount<uint32_t>(0xAAAA...)", pc<uint32_t>(0xAAAAAAAA), 16);
1 ✔
893
         result.test_is_eq<uint8_t>("popcount<uint64_t>(0xAAAA...)", pc<uint64_t>(0xAAAAAAAAAAAAAAAA), 32);
1 ✔
894

895
         result.test_is_eq<uint8_t>("popcount<uint8_t>(0xFF)", pc<uint8_t>(0xFF), 8);
1 ✔
896
         result.test_is_eq<uint8_t>("popcount<uint16_t>(0xFFFF)", pc<uint16_t>(0xFFFF), 16);
1 ✔
897
         result.test_is_eq<uint8_t>("popcount<uint32_t>(0xFFFF...)", pc<uint32_t>(0xFFFFFFFF), 32);
1 ✔
898
         result.test_is_eq<uint8_t>("popcount<uint64_t>(0xFFFF...)", pc<uint64_t>(0xFFFFFFFFFFFFFFFF), 64);
1 ✔
899

900
         random_pc<uint8_t>(result);
1 ✔
901
         random_pc<uint16_t>(result);
1 ✔
902
         random_pc<uint32_t>(result);
1 ✔
903
         random_pc<uint64_t>(result);
1 ✔
904

905
         return result;
1 ✔
906
      }
×
907

908
      Test::Result test_reverse_bits() {
1 ✔
909
         Test::Result result("reverse_bits");
1 ✔
910

911
         result.test_is_eq<uint8_t>("rev(0u8)", Botan::ct_reverse_bits<uint8_t>(0b00000000), 0b00000000);
1 ✔
912
         result.test_is_eq<uint8_t>("rev(1u8)", Botan::ct_reverse_bits<uint8_t>(0b01010101), 0b10101010);
1 ✔
913
         result.test_is_eq<uint8_t>("rev(2u8)", Botan::ct_reverse_bits<uint8_t>(0b01001011), 0b11010010);
1 ✔
914

915
         result.test_is_eq<uint16_t>(
1 ✔
916
            "rev(0u16)", Botan::ct_reverse_bits<uint16_t>(0b0000000000000000), 0b0000000000000000);
1 ✔
917
         result.test_is_eq<uint16_t>(
1 ✔
918
            "rev(1u16)", Botan::ct_reverse_bits<uint16_t>(0b0101010101010101), 0b1010101010101010);
1 ✔
919
         result.test_is_eq<uint16_t>(
1 ✔
920
            "rev(2u16)", Botan::ct_reverse_bits<uint16_t>(0b0100101101011010), 0b0101101011010010);
1 ✔
921

922
         result.test_is_eq<uint32_t>("rev(0u32)", Botan::ct_reverse_bits<uint32_t>(0xFFFFFFFF), 0xFFFFFFFF);
1 ✔
923
         result.test_is_eq<uint32_t>("rev(1u32)", Botan::ct_reverse_bits<uint32_t>(0x55555555), 0xAAAAAAAA);
1 ✔
924
         result.test_is_eq<uint32_t>("rev(2u32)", Botan::ct_reverse_bits<uint32_t>(0x4B6A2C1D), 0xB83456D2);
1 ✔
925

926
         result.test_is_eq<uint64_t>(
1 ✔
927
            "rev(0u64)", Botan::ct_reverse_bits<uint64_t>(0xF0E0D0C005040302), 0x40C020A0030B070F);
1 ✔
928
         result.test_is_eq<uint64_t>(
1 ✔
929
            "rev(1u64)", Botan::ct_reverse_bits<uint64_t>(0x5555555555555555), 0xAAAAAAAAAAAAAAAA);
1 ✔
930
         result.test_is_eq<uint64_t>(
1 ✔
931
            "rev(2u64)", Botan::ct_reverse_bits<uint64_t>(0x4B6A2C1D5E7F8A90), 0x951FE7AB83456D2);
1 ✔
932

933
         return result;
1 ✔
934
      }
×
935
};
936

937
BOTAN_REGISTER_TEST("utils", "bit_ops", BitOps_Tests);
938

939
#if defined(BOTAN_HAS_POLY_DBL)
940

941
class Poly_Double_Tests final : public Text_Based_Test {
×
942
   public:
943
      Poly_Double_Tests() : Text_Based_Test("poly_dbl.vec", "In,Out") {}
2 ✔
944

945
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
82 ✔
946
         Test::Result result("Polynomial doubling");
82 ✔
947
         const std::vector<uint8_t> in = vars.get_req_bin("In");
82 ✔
948
         const std::vector<uint8_t> out = vars.get_req_bin("Out");
82 ✔
949

950
         std::vector<uint8_t> b = in;
82 ✔
951
         Botan::poly_double_n(b.data(), b.size());
82 ✔
952

953
         result.test_eq("Expected value", b, out);
164 ✔
954
         return result;
82 ✔
955
      }
246 ✔
956
};
957

958
BOTAN_REGISTER_TEST("utils", "poly_dbl", Poly_Double_Tests);
959

960
#endif
961

962
class Version_Tests final : public Test {
×
963
   public:
964
      std::vector<Test::Result> run() override {
1 ✔
965
         Test::Result result("Versions");
1 ✔
966

967
         result.confirm("Version datestamp matches macro", Botan::version_datestamp() == BOTAN_VERSION_DATESTAMP);
2 ✔
968

969
         const char* version_cstr = Botan::version_cstr();
1 ✔
970
         const std::string version_str = Botan::version_string();
1 ✔
971
         result.test_eq("Same version string", version_str, std::string(version_cstr));
2 ✔
972

973
         const char* sversion_cstr = Botan::short_version_cstr();
1 ✔
974
         const std::string sversion_str = Botan::short_version_string();
1 ✔
975
         result.test_eq("Same short version string", sversion_str, std::string(sversion_cstr));
2 ✔
976

977
         const auto expected_sversion =
1 ✔
978
            Botan::fmt("{}.{}.{}", BOTAN_VERSION_MAJOR, BOTAN_VERSION_MINOR, BOTAN_VERSION_PATCH);
1 ✔
979

980
         // May have a suffix eg 4.0.0-rc2
981
         result.confirm("Short version string has expected format", sversion_str.starts_with(expected_sversion));
2 ✔
982

983
         const std::string version_check_ok =
1 ✔
984
            Botan::runtime_version_check(BOTAN_VERSION_MAJOR, BOTAN_VERSION_MINOR, BOTAN_VERSION_PATCH);
1 ✔
985

986
         result.confirm("Correct version no warning", version_check_ok.empty());
2 ✔
987

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

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

993
         result.test_eq("Expected warning text", version_check_bad, expected_error);
1 ✔
994

995
         return {result};
3 ✔
996
      }
2 ✔
997
};
998

999
BOTAN_REGISTER_TEST("utils", "versioning", Version_Tests);
1000

1001
class Date_Format_Tests final : public Text_Based_Test {
×
1002
   public:
1003
      Date_Format_Tests() : Text_Based_Test("dates.vec", "Date") {}
2 ✔
1004

1005
      static std::vector<uint32_t> parse_date(const std::string& s) {
11 ✔
1006
         const std::vector<std::string> parts = Botan::split_on(s, ',');
11 ✔
1007
         if(parts.size() != 6) {
11 ✔
1008
            throw Test_Error("Bad date format '" + s + "'");
×
1009
         }
1010

1011
         std::vector<uint32_t> u32s;
11 ✔
1012
         u32s.reserve(parts.size());
11 ✔
1013
         for(const auto& sub : parts) {
77 ✔
1014
            u32s.push_back(Botan::to_u32bit(sub));
66 ✔
1015
         }
1016
         return u32s;
11 ✔
1017
      }
11 ✔
1018

1019
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
11 ✔
1020
         const std::string date_str = vars.get_req_str("Date");
11 ✔
1021
         Test::Result result("Date parsing");
11 ✔
1022

1023
         const std::vector<uint32_t> d = parse_date(date_str);
11 ✔
1024

1025
         if(type == "valid" || type == "valid.not_std" || type == "valid.64_bit_time_t") {
11 ✔
1026
            const Botan::calendar_point c(d[0], d[1], d[2], d[3], d[4], d[5]);
11 ✔
1027
            result.test_is_eq(date_str + " year", c.year(), d[0]);
11 ✔
1028
            result.test_is_eq(date_str + " month", c.month(), d[1]);
11 ✔
1029
            result.test_is_eq(date_str + " day", c.day(), d[2]);
11 ✔
1030
            result.test_is_eq(date_str + " hour", c.hour(), d[3]);
11 ✔
1031
            result.test_is_eq(date_str + " minute", c.minutes(), d[4]);
11 ✔
1032
            result.test_is_eq(date_str + " second", c.seconds(), d[5]);
11 ✔
1033

1034
            if(type == "valid.not_std" ||
11 ✔
1035
               (type == "valid.64_bit_time_t" && c.year() > 2037 && sizeof(std::time_t) == 4)) {
1036
               result.test_throws("valid but out of std::timepoint range", [c]() { c.to_std_timepoint(); });
12 ✔
1037
            } else {
1038
               const Botan::calendar_point c2(c.to_std_timepoint());
8 ✔
1039
               result.test_is_eq(date_str + " year", c2.year(), d[0]);
8 ✔
1040
               result.test_is_eq(date_str + " month", c2.month(), d[1]);
8 ✔
1041
               result.test_is_eq(date_str + " day", c2.day(), d[2]);
8 ✔
1042
               result.test_is_eq(date_str + " hour", c2.hour(), d[3]);
8 ✔
1043
               result.test_is_eq(date_str + " minute", c2.minutes(), d[4]);
8 ✔
1044
               result.test_is_eq(date_str + " second", c2.seconds(), d[5]);
16 ✔
1045
            }
1046
         } else if(type == "invalid") {
×
1047
            result.test_throws("invalid date",
×
1048
                               [d]() { const Botan::calendar_point c(d[0], d[1], d[2], d[3], d[4], d[5]); });
×
1049
         } else {
1050
            throw Test_Error("Unexpected header '" + type + "' in date format tests");
×
1051
         }
1052

1053
         return result;
22 ✔
1054
      }
11 ✔
1055

1056
      std::vector<Test::Result> run_final_tests() override {
1 ✔
1057
         Test::Result result("calendar_point::to_string");
1 ✔
1058
         const Botan::calendar_point d(2008, 5, 15, 9, 30, 33);
1 ✔
1059
         // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
1060
         result.test_eq("calendar_point::to_string", d.to_string(), "2008-05-15T09:30:33");
2 ✔
1061
         return {result};
3 ✔
1062
      }
2 ✔
1063
};
1064

1065
BOTAN_REGISTER_TEST("utils", "util_dates", Date_Format_Tests);
1066

1067
class Charset_Tests final : public Text_Based_Test {
×
1068
   public:
1069
      Charset_Tests() : Text_Based_Test("charset.vec", "In,Out") {}
2 ✔
1070

1071
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
8 ✔
1072
         Test::Result result("Charset");
8 ✔
1073

1074
         const std::vector<uint8_t> in = vars.get_req_bin("In");
8 ✔
1075
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
8 ✔
1076

1077
         std::string converted;
8 ✔
1078

1079
         if(type == "UCS2-UTF8") {
8 ✔
1080
            converted = Botan::ucs2_to_utf8(in.data(), in.size());
4 ✔
1081
         } else if(type == "UCS4-UTF8") {
4 ✔
1082
            converted = Botan::ucs4_to_utf8(in.data(), in.size());
1 ✔
1083
         } else if(type == "LATIN1-UTF8") {
3 ✔
1084
            converted = Botan::latin1_to_utf8(in.data(), in.size());
3 ✔
1085
         } else {
1086
            throw Test_Error("Unexpected header '" + type + "' in charset tests");
×
1087
         }
1088

1089
         result.test_eq(
16 ✔
1090
            "string converted successfully", std::vector<uint8_t>(converted.begin(), converted.end()), expected);
8 ✔
1091

1092
         return result;
8 ✔
1093
      }
24 ✔
1094
};
1095

1096
BOTAN_REGISTER_TEST("utils", "charset", Charset_Tests);
1097

1098
class Hostname_Tests final : public Text_Based_Test {
×
1099
   public:
1100
      Hostname_Tests() : Text_Based_Test("hostnames.vec", "Issued,Hostname") {}
2 ✔
1101

1102
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
44 ✔
1103
         Test::Result result("Hostname Matching");
44 ✔
1104

1105
         const std::string issued = vars.get_req_str("Issued");
44 ✔
1106
         const std::string hostname = vars.get_req_str("Hostname");
44 ✔
1107
         const bool expected = (type == "Invalid") ? false : true;
44 ✔
1108

1109
         const std::string what = hostname + ((expected == true) ? " matches " : " does not match ") + issued;
88 ✔
1110
         result.test_eq(what, Botan::host_wildcard_match(issued, hostname), expected);
44 ✔
1111

1112
         return result;
44 ✔
1113
      }
44 ✔
1114
};
1115

1116
BOTAN_REGISTER_TEST("utils", "hostname", Hostname_Tests);
1117

1118
class DNS_Check_Tests final : public Text_Based_Test {
×
1119
   public:
1120
      DNS_Check_Tests() : Text_Based_Test("utils/dns.vec", "DNS") {}
2 ✔
1121

1122
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
59 ✔
1123
         Test::Result result("DNS name validation");
59 ✔
1124

1125
         const std::string name = vars.get_req_str("DNS");
59 ✔
1126
         const bool valid = (type == "Invalid") ? false : true;
59 ✔
1127

1128
         try {
59 ✔
1129
            const auto canonicalized = Botan::check_and_canonicalize_dns_name(name);
59 ✔
1130
            BOTAN_UNUSED(canonicalized);
25 ✔
1131

1132
            if(valid) {
25 ✔
1133
               result.test_success("Accepted valid name");
50 ✔
1134
            } else {
1135
               result.test_failure("Accepted invalid name");
×
1136
            }
1137
         } catch(Botan::Decoding_Error&) {
59 ✔
1138
            if(valid) {
34 ✔
1139
               result.test_failure("Rejected valid name");
×
1140
            } else {
1141
               result.test_success("Rejected invalid name");
68 ✔
1142
            }
1143
         }
34 ✔
1144

1145
         return result;
59 ✔
1146
      }
59 ✔
1147
};
1148

1149
BOTAN_REGISTER_TEST("utils", "dns_check", DNS_Check_Tests);
1150

1151
class IPv4_Parsing_Tests final : public Text_Based_Test {
×
1152
   public:
1153
      IPv4_Parsing_Tests() : Text_Based_Test("utils/ipv4.vec", "IPv4") {}
2 ✔
1154

1155
      Test::Result run_one_test(const std::string& status, const VarMap& vars) override {
47 ✔
1156
         Test::Result result("IPv4 parsing");
47 ✔
1157

1158
         const std::string input = vars.get_req_str("IPv4");
47 ✔
1159
         const bool valid = (status == "Valid");
47 ✔
1160

1161
         auto ipv4 = Botan::string_to_ipv4(input);
47 ✔
1162

1163
         result.test_eq("string_to_ipv4 accepts only valid", valid, ipv4.has_value());
47 ✔
1164

1165
         if(ipv4) {
47 ✔
1166
            const std::string rt = Botan::ipv4_to_string(ipv4.value());
13 ✔
1167
            result.test_eq("ipv4_to_string and string_to_ipv4 round trip", input, rt);
26 ✔
1168
         }
13 ✔
1169

1170
         return result;
47 ✔
1171
      }
47 ✔
1172
};
1173

1174
BOTAN_REGISTER_TEST("utils", "ipv4_parse", IPv4_Parsing_Tests);
1175

1176
class ReadKV_Tests final : public Text_Based_Test {
×
1177
   public:
1178
      ReadKV_Tests() : Text_Based_Test("utils/read_kv.vec", "Input,Expected") {}
2 ✔
1179

1180
      Test::Result run_one_test(const std::string& status, const VarMap& vars) override {
16 ✔
1181
         Test::Result result("read_kv");
16 ✔
1182

1183
         const bool is_valid = (status == "Valid");
16 ✔
1184

1185
         const std::string input = vars.get_req_str("Input");
16 ✔
1186
         const std::string expected = vars.get_req_str("Expected");
16 ✔
1187

1188
         if(is_valid) {
16 ✔
1189
            confirm_kv(result, Botan::read_kv(input), split_group(expected));
14 ✔
1190
         } else {
1191
            // In this case "expected" is the expected exception message
1192
            result.test_throws("Invalid key value input throws exception", expected, [&]() { Botan::read_kv(input); });
36 ✔
1193
         }
1194
         return result;
16 ✔
1195
      }
16 ✔
1196

1197
   private:
1198
      static std::vector<std::string> split_group(const std::string& str) {
7 ✔
1199
         std::vector<std::string> elems;
7 ✔
1200
         if(str.empty()) {
7 ✔
1201
            return elems;
1202
         }
1203

1204
         std::string substr;
6 ✔
1205
         for(const char c : str) {
115 ✔
1206
            if(c == '|') {
109 ✔
1207
               elems.push_back(substr);
16 ✔
1208
               substr.clear();
16 ✔
1209
            } else {
1210
               substr += c;
202 ✔
1211
            }
1212
         }
1213

1214
         if(!substr.empty()) {
6 ✔
1215
            elems.push_back(substr);
6 ✔
1216
         }
1217

1218
         return elems;
6 ✔
1219
      }
6 ✔
1220

1221
      static void confirm_kv(Test::Result& result,
7 ✔
1222
                             const std::map<std::string, std::string>& kv,
1223
                             const std::vector<std::string>& expected) {
1224
         if(!result.test_eq("expected size", expected.size() % 2, size_t(0))) {
7 ✔
1225
            return;
1226
         }
1227

1228
         for(size_t i = 0; i != expected.size(); i += 2) {
18 ✔
1229
            auto j = kv.find(expected[i]);
11 ✔
1230
            if(result.confirm("Found key", j != kv.end())) {
22 ✔
1231
               result.test_eq("Matching value", j->second, expected[i + 1]);
22 ✔
1232
            }
1233
         }
1234

1235
         result.test_eq("KV has same size as expected", kv.size(), expected.size() / 2);
14 ✔
1236
      }
1237
};
1238

1239
BOTAN_REGISTER_TEST("utils", "util_read_kv", ReadKV_Tests);
1240

1241
#if defined(BOTAN_HAS_CPUID)
1242

1243
class CPUID_Tests final : public Test {
×
1244
   public:
1245
      std::vector<Test::Result> run() override {
1 ✔
1246
         Test::Result result("CPUID");
1 ✔
1247

1248
         const std::string cpuid_string = Botan::CPUID::to_string();
1 ✔
1249
         result.test_success("CPUID::to_string doesn't crash");
1 ✔
1250

1251
         for(size_t b = 0; b != 32; ++b) {
33 ✔
1252
            try {
32 ✔
1253
               const auto bit = static_cast<uint32_t>(1) << b;
32 ✔
1254
               // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
1255
               const auto feat = Botan::CPUID::Feature(static_cast<Botan::CPUID::Feature::Bit>(bit));
32 ✔
1256

1257
               const std::string feat_str = feat.to_string();
32 ✔
1258

1259
               result.confirm("Feature string is not empty", !feat_str.empty());
40 ✔
1260

1261
               if(auto from_str = Botan::CPUID::Feature::from_string(feat_str)) {
20 ✔
1262
                  result.test_int_eq("Feature::from_string returns expected bit", from_str->as_u32(), bit);
40 ✔
1263
               } else {
1264
                  result.test_failure(
×
1265
                     Botan::fmt("Feature::from_string didn't recognize its own output ({})", feat_str));
×
1266
               }
1267
            } catch(Botan::Invalid_State&) {
32 ✔
1268
               // This will thrown if the bit is not a valid one
1269
            }
12 ✔
1270
         }
1271

1272
   #if defined(BOTAN_TARGET_ARCH_IS_X86_FAMILY)
1273

1274
         const auto bit = Botan::CPUID::Feature::SSE2;
1 ✔
1275

1276
         if(Botan::CPUID::has(bit)) {
1 ✔
1277
            result.confirm("Output string includes sse2", cpuid_string.find("sse2") != std::string::npos);
2 ✔
1278

1279
            Botan::CPUID::clear_cpuid_bit(bit);
1 ✔
1280

1281
            result.test_eq(
2 ✔
1282
               "After clearing cpuid bit, CPUID::has for SSE2 returns false", Botan::CPUID::has(bit), false);
1 ✔
1283

1284
            Botan::CPUID::initialize();  // reset state
1 ✔
1285
            result.test_eq(
3 ✔
1286
               "After reinitializing, CPUID::has for SSE2 returns true again", Botan::CPUID::has(bit), true);
1 ✔
1287
         }
1288
   #else
1289
         BOTAN_UNUSED(cpuid_string);
1290
   #endif
1291

1292
         return {result};
3 ✔
1293
      }
2 ✔
1294
};
1295

1296
BOTAN_REGISTER_SERIALIZED_TEST("utils", "cpuid", CPUID_Tests);
1297

1298
#endif
1299

1300
#if defined(BOTAN_HAS_UUID)
1301

1302
class UUID_Tests : public Test {
×
1303
   public:
1304
      std::vector<Test::Result> run() override {
1 ✔
1305
         Test::Result result("UUID");
1 ✔
1306

1307
         const Botan::UUID empty_uuid;
1 ✔
1308
         const Botan::UUID random_uuid1(this->rng());
1 ✔
1309
         const Botan::UUID random_uuid2(this->rng());
1 ✔
1310
         const Botan::UUID loaded_uuid(std::vector<uint8_t>(16, 4));
1 ✔
1311

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

1315
         result.test_eq("Empty UUID is empty", empty_uuid.is_valid(), false);
1 ✔
1316
         result.confirm("Empty UUID equals another empty UUID", empty_uuid == Botan::UUID());
2 ✔
1317

1318
         result.test_throws("Empty UUID cannot become a string", [&]() { empty_uuid.to_string(); });
3 ✔
1319

1320
         result.test_eq("Random UUID not empty", random_uuid1.is_valid(), true);
1 ✔
1321
         result.test_eq("Random UUID not empty", random_uuid2.is_valid(), true);
1 ✔
1322

1323
         result.confirm("Random UUIDs are distinct", random_uuid1 != random_uuid2);
2 ✔
1324
         result.confirm("Random UUIDs not equal to empty", random_uuid1 != empty_uuid);
2 ✔
1325

1326
         const std::string uuid4_str = loaded_uuid.to_string();
1 ✔
1327
         result.test_eq("String matches expected", uuid4_str, "04040404-0404-0404-0404-040404040404");
2 ✔
1328

1329
         const std::string uuid_r1_str = random_uuid1.to_string();
1 ✔
1330
         result.confirm("UUID from string matches", Botan::UUID(uuid_r1_str) == random_uuid1);
2 ✔
1331

1332
         class AllSame_RNG : public Botan::RandomNumberGenerator {
×
1333
            public:
1334
               explicit AllSame_RNG(uint8_t b) : m_val(b) {}
2 ✔
1335

1336
               void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> /* ignored */) override {
2 ✔
1337
                  std::fill(output.begin(), output.end(), m_val);
2 ✔
1338
               }
2 ✔
1339

1340
               std::string name() const override { return "zeros"; }
×
1341

1342
               bool accepts_input() const override { return false; }
×
1343

1344
               void clear() override {}
×
1345

1346
               bool is_seeded() const override { return true; }
×
1347

1348
            private:
1349
               uint8_t m_val;
1350
         };
1351

1352
         AllSame_RNG zeros(0x00);
1 ✔
1353
         const Botan::UUID zero_uuid(zeros);
1 ✔
1354
         result.test_eq("Zero UUID matches expected", zero_uuid.to_string(), "00000000-0000-4000-8000-000000000000");
2 ✔
1355

1356
         AllSame_RNG ones(0xFF);
1 ✔
1357
         const Botan::UUID ones_uuid(ones);
1 ✔
1358
         result.test_eq("Ones UUID matches expected", ones_uuid.to_string(), "FFFFFFFF-FFFF-4FFF-BFFF-FFFFFFFFFFFF");
2 ✔
1359

1360
         return {result};
3 ✔
1361
      }
6 ✔
1362
};
1363

1364
BOTAN_REGISTER_TEST("utils", "uuid", UUID_Tests);
1365

1366
#endif
1367

1368
class Formatter_Tests : public Test {
×
1369
   public:
1370
      std::vector<Test::Result> run() override {
1 ✔
1371
         Test::Result result("Format utility");
1 ✔
1372

1373
         /*
1374
         In a number of these tests, we are not strictly depending on the
1375
         behavior, for instance checking `fmt("{}") == "{}"` is more about
1376
         checking that we don't crash, rather than we return that precise string.
1377
         */
1378

1379
         result.test_eq("test 1", Botan::fmt("hi"), "hi");
2 ✔
1380
         result.test_eq("test 2", Botan::fmt("ignored", 5), "ignored");
2 ✔
1381
         result.test_eq("test 3", Botan::fmt("answer is {}", 42), "answer is 42");
2 ✔
1382
         result.test_eq("test 4", Botan::fmt("{", 5), "{");
2 ✔
1383
         result.test_eq("test 4", Botan::fmt("{}"), "{}");
2 ✔
1384
         result.test_eq("test 5", Botan::fmt("{} == '{}'", 5, "five"), "5 == 'five'");
2 ✔
1385

1386
         return {result};
3 ✔
1387
      }
2 ✔
1388
};
1389

1390
BOTAN_REGISTER_TEST("utils", "fmt", Formatter_Tests);
1391

1392
class ScopedCleanup_Tests : public Test {
×
1393
   public:
1394
      std::vector<Test::Result> run() override {
1 ✔
1395
         return {
1 ✔
1396
            CHECK("leaving a scope results in cleanup",
1397
                  [](Test::Result& result) {
1 ✔
1398
                     bool ran = false;
1 ✔
1399
                     {
1 ✔
1400
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1 ✔
1401
                     }
1 ✔
1402
                     result.confirm("cleanup ran", ran);
2 ✔
1403
                  }),
1 ✔
1404

1405
            CHECK("leaving a function, results in cleanup",
1406
                  [](Test::Result& result) {
1 ✔
1407
                     bool ran = false;
1 ✔
1408
                     bool fn_called = false;
1 ✔
1409
                     auto fn = [&] {
2 ✔
1410
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1 ✔
1411
                        fn_called = true;
1 ✔
1412
                     };
2 ✔
1413

1414
                     result.confirm("cleanup not yet ran", !ran);
2 ✔
1415
                     fn();
1 ✔
1416
                     result.confirm("fn called", fn_called);
2 ✔
1417
                     result.confirm("cleanup ran", ran);
2 ✔
1418
                  }),
1 ✔
1419

1420
            CHECK("stack unwinding results in cleanup",
1421
                  [](Test::Result& result) {
1 ✔
1422
                     bool ran = false;
1 ✔
1423
                     bool fn_called = false;
1 ✔
1424
                     bool exception_caught = false;
1 ✔
1425
                     auto fn = [&] {
2 ✔
1426
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1 ✔
1427
                        fn_called = true;
1 ✔
1428
                        throw std::runtime_error("test");
1 ✔
1429
                     };
2 ✔
1430

1431
                     result.confirm("cleanup not yet ran", !ran);
2 ✔
1432
                     try {
1 ✔
1433
                        fn();
1 ✔
1434
                     } catch(const std::exception&) {
1 ✔
1435
                        exception_caught = true;
1 ✔
1436
                     }
1 ✔
1437

1438
                     result.confirm("fn called", fn_called);
2 ✔
1439
                     result.confirm("cleanup ran", ran);
2 ✔
1440
                     result.confirm("exception caught", exception_caught);
2 ✔
1441
                  }),
1 ✔
1442

1443
            CHECK("cleanup isn't called after disengaging",
1444
                  [](Test::Result& result) {
1 ✔
1445
                     bool ran = false;
1 ✔
1446
                     {
1 ✔
1447
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1 ✔
1448
                        clean.disengage();
1 ✔
1449
                     }
1 ✔
1450
                     result.confirm("cleanup not ran", !ran);
2 ✔
1451
                  }),
1 ✔
1452

1453
         };
5 ✔
1454
      }
1 ✔
1455
};
1456

1457
BOTAN_REGISTER_TEST("utils", "scoped_cleanup", ScopedCleanup_Tests);
1458

1459
}  // namespace
1460

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