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

randombit / botan / 26013989535

17 May 2026 03:06PM UTC coverage: 89.316% (+0.01%) from 89.306%
26013989535

push

github

web-flow
Merge pull request #5604 from randombit/jack/add-timeout

Add timeout to running apt-get update on Github Actions

107912 of 120820 relevant lines covered (89.32%)

11136705.36 hits per line

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

95.34
/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/hex.h>
13
#include <botan/rng.h>
14
#include <botan/version.h>
15
#include <botan/internal/bit_ops.h>
16
#include <botan/internal/calendar.h>
17
#include <botan/internal/charset.h>
18
#include <botan/internal/concat_util.h>
19
#include <botan/internal/ct_utils.h>
20
#include <botan/internal/fmt.h>
21
#include <botan/internal/int_utils.h>
22
#include <botan/internal/loadstor.h>
23
#include <botan/internal/parsing.h>
24
#include <botan/internal/rounding.h>
25
#include <botan/internal/target_info.h>
26

27
#include <bit>
28

29
#if defined(BOTAN_HAS_CPUID)
30
   #include <botan/internal/cpuid.h>
31
#endif
32

33
#if defined(BOTAN_HAS_IPV6_ADDRESS)
34
   #include <botan/ipv6_address.h>
35
#endif
36

37
#if defined(BOTAN_HAS_POLY_DBL)
38
   #include <botan/internal/poly_dbl.h>
39
#endif
40

41
#if defined(BOTAN_HAS_UUID)
42
   #include <botan/uuid.h>
43
#endif
44

45
namespace Botan_Tests {
46

47
namespace {
48

49
class Utility_Function_Tests final : public Test {
1✔
50
   public:
51
      std::vector<Test::Result> run() override {
1✔
52
         std::vector<Test::Result> results;
1✔
53

54
         results.push_back(test_checked_add());
2✔
55
         results.push_back(test_checked_mul());
2✔
56
         results.push_back(test_checked_cast());
2✔
57
         results.push_back(test_round_up());
2✔
58
         results.push_back(test_loadstore());
2✔
59
         results.push_back(test_loadstore_ambiguity());
2✔
60
         results.push_back(test_loadstore_fallback());
2✔
61
         results.push_back(test_loadstore_constexpr());
2✔
62
         return Botan::concat(results, test_copy_out_be_le());
3✔
63
      }
1✔
64

65
   private:
66
      Test::Result test_checked_add() {
1✔
67
         Test::Result result("checked_add");
1✔
68

69
         const size_t large = static_cast<size_t>(-5);
1✔
70
         const size_t zero = 0;
1✔
71

72
         for(int si = -15; si != 15; ++si) {
31✔
73
            const size_t i = static_cast<size_t>(si);
30✔
74
            auto sum1 = Botan::checked_add<size_t>(i, zero, zero, zero, large);
30✔
75
            auto sum2 = Botan::checked_add<size_t>(large, zero, zero, zero, i);
30✔
76

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

79
            if(i < 5) {
30✔
80
               result.test_sz_eq("checked_add worked", sum1.value(), i + large);
5✔
81
            } else {
82
               result.test_is_true("checked_add did not return a result", !sum1.has_value());
25✔
83
            }
84
         }
85

86
         auto& rng = Test::rng();
1✔
87

88
         for(size_t i = 0; i != 100; ++i) {
101✔
89
            const uint16_t x = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100✔
90
            const uint16_t y = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100✔
91

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

94
            if(auto z = Botan::checked_add(x, y)) {
200✔
95
               result.test_u32_eq("checked_add adds", static_cast<uint32_t>(z.value()), ref);
47✔
96
            } else {
97
               result.test_is_true("checked_add checks", (ref >> 16) > 0);
53✔
98
            }
99
         }
100

101
         return result;
1✔
102
      }
×
103

104
      Test::Result test_checked_mul() {
1✔
105
         Test::Result result("checked_mul");
1✔
106

107
         auto& rng = Test::rng();
1✔
108

109
         for(size_t i = 0; i != 100; ++i) {
101✔
110
            const uint16_t x = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100✔
111
            const uint16_t y = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100✔
112

113
            const uint32_t ref = static_cast<uint32_t>(x) * y;
100✔
114

115
            if(auto z = Botan::checked_mul(x, y)) {
200✔
116
               result.test_u32_eq("checked_mul multiplies", static_cast<uint32_t>(z.value()), ref);
×
117
            } else {
118
               result.test_is_true("checked_mul checks", (ref >> 16) > 0);
100✔
119
            }
120
         }
121

122
         return result;
1✔
123
      }
×
124

125
      Test::Result test_checked_cast() {
1✔
126
         Test::Result result("checked_cast");
1✔
127

128
         const uint32_t large = static_cast<uint32_t>(-1);
1✔
129
         const uint32_t is_16_bits = 0x8123;
1✔
130
         const uint32_t is_8_bits = 0x89;
1✔
131

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

135
         result.test_u32_eq("checked_cast converts", Botan::checked_cast_to<uint32_t>(large), large);
1✔
136
         result.test_u16_eq("checked_cast converts", Botan::checked_cast_to<uint16_t>(is_16_bits), 0x8123);
1✔
137
         result.test_u8_eq("checked_cast converts", Botan::checked_cast_to<uint8_t>(is_8_bits), 0x89);
1✔
138

139
         return result;
1✔
140
      }
×
141

142
      Test::Result test_round_up() {
1✔
143
         Test::Result result("Util round_up");
1✔
144

145
         // clang-format off
146
         const std::vector<size_t> inputs = {
1✔
147
            0, 1, 2, 3, 4, 9, 10, 32, 99, 100, 101, 255, 256, 1000, 10000,
148
            65535, 65536, 65537,
149
         };
1✔
150

151
         const std::vector<size_t> alignments = {
1✔
152
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 32, 50, 64, 100, 512, 521,
153
            1000, 1023, 1024, 1025, 10000, 65535, 65536
154
         };
1✔
155
         // clang-format on
156

157
         for(const size_t i : inputs) {
19✔
158
            for(const size_t m : alignments) {
450✔
159
               try {
432✔
160
                  const size_t z = Botan::round_up(i, m);
432✔
161

162
                  result.test_is_true("z % m == 0", z % m == 0);
432✔
163
                  result.test_is_true("z >= i", z >= i);
432✔
164
                  result.test_is_true("z <= i + m", z <= i + m);
432✔
165
               } catch(Botan::Exception& e) {
×
166
                  result.test_failure(Botan::fmt("round_up({},{})", i, m), e.what());
×
167
               }
×
168
            }
169
         }
170

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

173
         return result;
1✔
174
      }
1✔
175

176
      using TestInt64 = Botan::Strong<uint64_t, struct TestInt64_>;
177
      using TestInt32 = Botan::Strong<uint32_t, struct TestInt64_>;
178
      using TestVectorSink = Botan::Strong<std::vector<uint8_t>, struct TestVectorSink_>;
179

180
      enum class TestEnum64 : uint64_t {
181
         _1 = 0x1234567890ABCDEF,
182
         _2 = 0xEFCDAB9078563412,
183
      };
184

185
      enum class TestEnum32 : uint32_t {
186
         _1 = 0x12345678,
187
         _2 = 0x78563412,
188
      };
189

190
      static Test::Result test_loadstore() {
1✔
191
         Test::Result result("Util load/store");
1✔
192

193
         const std::vector<uint8_t> membuf = Botan::hex_decode("00112233445566778899AABBCCDDEEFF");
1✔
194
         const uint8_t* mem = membuf.data();
1✔
195

196
         const uint16_t in16 = 0x1234;
1✔
197
         const uint32_t in32 = 0xA0B0C0D0;
1✔
198
         const uint64_t in64 = 0xABCDEF0123456789;
1✔
199

200
         result.test_u8_eq(Botan::get_byte<0>(in32), 0xA0);
1✔
201
         result.test_u8_eq(Botan::get_byte<1>(in32), 0xB0);
1✔
202
         result.test_u8_eq(Botan::get_byte<2>(in32), 0xC0);
1✔
203
         result.test_u8_eq(Botan::get_byte<3>(in32), 0xD0);
1✔
204

205
         result.test_u16_eq(Botan::make_uint16(0xAA, 0xBB), 0xAABB);
1✔
206
         result.test_u32_eq(Botan::make_uint32(0x01, 0x02, 0x03, 0x04), 0x01020304);
1✔
207

208
         result.test_u16_eq(Botan::load_be<uint16_t>(mem, 0), 0x0011);
1✔
209
         result.test_u16_eq(Botan::load_be<uint16_t>(mem, 1), 0x2233);
1✔
210
         result.test_u16_eq(Botan::load_be<uint16_t>(mem, 2), 0x4455);
1✔
211
         result.test_u16_eq(Botan::load_be<uint16_t>(mem, 3), 0x6677);
1✔
212

213
         result.test_u16_eq(Botan::load_le<uint16_t>(mem, 0), 0x1100);
1✔
214
         result.test_u16_eq(Botan::load_le<uint16_t>(mem, 1), 0x3322);
1✔
215
         result.test_u16_eq(Botan::load_le<uint16_t>(mem, 2), 0x5544);
1✔
216
         result.test_u16_eq(Botan::load_le<uint16_t>(mem, 3), 0x7766);
1✔
217

218
         result.test_u32_eq(Botan::load_be<uint32_t>(mem, 0), 0x00112233);
1✔
219
         result.test_u32_eq(Botan::load_be<uint32_t>(mem, 1), 0x44556677);
1✔
220
         result.test_u32_eq(Botan::load_be<uint32_t>(mem, 2), 0x8899AABB);
1✔
221
         result.test_u32_eq(Botan::load_be<uint32_t>(mem, 3), 0xCCDDEEFF);
1✔
222

223
         result.test_u32_eq(Botan::load_le<uint32_t>(mem, 0), 0x33221100);
1✔
224
         result.test_u32_eq(Botan::load_le<uint32_t>(mem, 1), 0x77665544);
1✔
225
         result.test_u32_eq(Botan::load_le<uint32_t>(mem, 2), 0xBBAA9988);
1✔
226
         result.test_u32_eq(Botan::load_le<uint32_t>(mem, 3), 0xFFEEDDCC);
1✔
227

228
         result.test_u64_eq(Botan::load_be<uint64_t>(mem, 0), 0x0011223344556677);
1✔
229
         result.test_u64_eq(Botan::load_be<uint64_t>(mem, 1), 0x8899AABBCCDDEEFF);
1✔
230

231
         result.test_u64_eq(Botan::load_le<uint64_t>(mem, 0), 0x7766554433221100);
1✔
232
         result.test_u64_eq(Botan::load_le<uint64_t>(mem, 1), 0xFFEEDDCCBBAA9988);
1✔
233

234
         // Check misaligned loads:
235
         result.test_u16_eq(Botan::load_be<uint16_t>(mem + 1, 0), 0x1122);
1✔
236
         result.test_u16_eq(Botan::load_le<uint16_t>(mem + 3, 0), 0x4433);
1✔
237

238
         result.test_u32_eq(Botan::load_be<uint32_t>(mem + 1, 1), 0x55667788);
1✔
239
         result.test_u32_eq(Botan::load_le<uint32_t>(mem + 3, 1), 0xAA998877);
1✔
240

241
         result.test_u64_eq(Botan::load_be<uint64_t>(mem + 1, 0), 0x1122334455667788);
1✔
242
         result.test_u64_eq(Botan::load_le<uint64_t>(mem + 7, 0), 0xEEDDCCBBAA998877);
1✔
243
         result.test_u64_eq(Botan::load_le<uint64_t>(mem + 5, 0), 0xCCBBAA9988776655);
1✔
244

245
         uint8_t outbuf[16] = {0};
1✔
246

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

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

254
            Botan::store_le(in16, out);
7✔
255
            result.test_u8_eq(out[0], 0x34);
7✔
256
            result.test_u8_eq(out[1], 0x12);
7✔
257

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

264
            Botan::store_le(in32, out);
7✔
265
            result.test_u8_eq(out[0], 0xD0);
7✔
266
            result.test_u8_eq(out[1], 0xC0);
7✔
267
            result.test_u8_eq(out[2], 0xB0);
7✔
268
            result.test_u8_eq(out[3], 0xA0);
7✔
269

270
            Botan::store_be(in64, out);
7✔
271
            result.test_u8_eq(out[0], 0xAB);
7✔
272
            result.test_u8_eq(out[1], 0xCD);
7✔
273
            result.test_u8_eq(out[2], 0xEF);
7✔
274
            result.test_u8_eq(out[3], 0x01);
7✔
275
            result.test_u8_eq(out[4], 0x23);
7✔
276
            result.test_u8_eq(out[5], 0x45);
7✔
277
            result.test_u8_eq(out[6], 0x67);
7✔
278
            result.test_u8_eq(out[7], 0x89);
7✔
279

280
            Botan::store_le(in64, out);
7✔
281
            result.test_u8_eq(out[0], 0x89);
7✔
282
            result.test_u8_eq(out[1], 0x67);
7✔
283
            result.test_u8_eq(out[2], 0x45);
7✔
284
            result.test_u8_eq(out[3], 0x23);
7✔
285
            result.test_u8_eq(out[4], 0x01);
7✔
286
            result.test_u8_eq(out[5], 0xEF);
7✔
287
            result.test_u8_eq(out[6], 0xCD);
7✔
288
            result.test_u8_eq(out[7], 0xAB);
7✔
289
         }
290

291
         std::array<uint8_t, 8> outarr{};
1✔
292
         uint16_t i0 = 0;
1✔
293
         uint16_t i1 = 0;
1✔
294
         uint16_t i2 = 0;
1✔
295
         uint16_t i3 = 0;
1✔
296
         Botan::store_be(in64, outarr);
1✔
297

298
         Botan::load_be(outarr, i0, i1, i2, i3);
1✔
299
         result.test_u16_eq(i0, 0xABCD);
1✔
300
         result.test_u16_eq(i1, 0xEF01);
1✔
301
         result.test_u16_eq(i2, 0x2345);
1✔
302
         result.test_u16_eq(i3, 0x6789);
1✔
303

304
         Botan::load_le(std::span{outarr}.first<6>(), i0, i1, i2);
1✔
305
         result.test_u16_eq(i0, 0xCDAB);
1✔
306
         result.test_u16_eq(i1, 0x01EF);
1✔
307
         result.test_u16_eq(i2, 0x4523);
1✔
308
         result.test_u16_eq(i3, 0x6789);  // remains unchanged
1✔
309

310
         Botan::store_le(in64, outarr);
1✔
311

312
         Botan::load_le(outarr, i0, i1, i2, i3);
1✔
313
         result.test_u16_eq(i0, 0x6789);
1✔
314
         result.test_u16_eq(i1, 0x2345);
1✔
315
         result.test_u16_eq(i2, 0xEF01);
1✔
316
         result.test_u16_eq(i3, 0xABCD);
1✔
317

318
         Botan::load_be(std::span{outarr}.first<6>(), i0, i1, i2);
1✔
319
         result.test_u16_eq(i0, 0x8967);
1✔
320
         result.test_u16_eq(i1, 0x4523);
1✔
321
         result.test_u16_eq(i2, 0x01EF);
1✔
322
         result.test_u16_eq(i3, 0xABCD);  // remains unchanged
1✔
323

324
         i0 = 0xAA11;
1✔
325
         i1 = 0xBB22;
1✔
326
         i2 = 0xCC33;
1✔
327
         i3 = 0xDD44;
1✔
328
         Botan::store_be(outarr, i0, i1, i2, i3);
1✔
329
         result.test_bin_eq("store_be", outarr, "AA11BB22CC33DD44");
1✔
330
         std::vector<uint8_t> outvec(8);
1✔
331
         Botan::store_be(outvec, i0, i1, i2, i3);
1✔
332
         result.test_bin_eq("store_be", outvec, "AA11BB22CC33DD44");
1✔
333

334
         Botan::store_le(outarr, i0, i1, i2, i3);
1✔
335
         result.test_bin_eq("store_le(arr)", outarr, "11AA22BB33CC44DD");
1✔
336
         Botan::store_le(outvec, i0, i1, i2, i3);
1✔
337
         result.test_bin_eq("store_le", outvec, "11AA22BB33CC44DD");
1✔
338

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

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

366
         result.test_u16_eq(in16, Botan::load_be(Botan::store_be(in16)));
1✔
367
         result.test_u32_eq(in32, Botan::load_be(Botan::store_be(in32)));
1✔
368
         result.test_u64_eq(in64, Botan::load_be(Botan::store_be(in64)));
1✔
369

370
         result.test_u16_eq(in16, Botan::load_le(Botan::store_le(in16)));
1✔
371
         result.test_u32_eq(in32, Botan::load_le(Botan::store_le(in32)));
1✔
372
         result.test_u64_eq(in64, Botan::load_le(Botan::store_le(in64)));
1✔
373

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

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

399
         // Test store of entire ranges
400
         const std::array<uint16_t, 2> in16_array = {0x0A0B, 0x0C0D};
1✔
401
         result.test_bin_eq("store_be(vec)", Botan::store_be<std::vector<uint8_t>>(in16_array), "0A0B0C0D");
1✔
402
         result.test_bin_eq("store_le(vec)", Botan::store_le<std::vector<uint8_t>>(in16_array), "0B0A0D0C");
1✔
403

404
         const std::vector<uint16_t> in16_vector = {0x0A0B, 0x0C0D};
1✔
405
         result.test_bin_eq("store_be(vec)", Botan::store_be<std::vector<uint8_t>>(in16_vector), "0A0B0C0D");
1✔
406
         result.test_bin_eq("store_le(vec)", Botan::store_le<std::vector<uint8_t>>(in16_vector), "0B0A0D0C");
1✔
407

408
         std::array<uint8_t, 4> out_array{};
1✔
409
         Botan::store_be(out_array, in16_array);
1✔
410
         result.test_bin_eq("store_be(arr)", out_array, "0A0B0C0D");
1✔
411
         Botan::store_le(out_array, in16_array);
1✔
412
         result.test_bin_eq("store_le(arr)", out_array, "0B0A0D0C");
1✔
413

414
         const auto be_inferred = Botan::store_be(in16_array);
1✔
415
         result.test_bin_eq("store_be(arr)", be_inferred, "0A0B0C0D");
1✔
416
         const auto le_inferred = Botan::store_le(in16_array);
1✔
417
         result.test_bin_eq("store_le(arr)", le_inferred, "0B0A0D0C");
1✔
418

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

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

437
         // Test loading/storing of strong type integers
438
         const TestInt64 in64_strong{0xABCDEF0123456789};
1✔
439
         const TestInt32 in32_strong{0xABCDEF01};
1✔
440

441
         result.test_bin_eq(
1✔
442
            "store_be(u64,strong)", Botan::store_be<std::vector<uint8_t>>(in64_strong), "ABCDEF0123456789");
×
443
         result.test_bin_eq(
1✔
444
            "store_le(u64,strong)", Botan::store_le<std::vector<uint8_t>>(in64_strong), "8967452301EFCDAB");
×
445
         result.test_bin_eq("store_be(u32,strong)", Botan::store_be<std::vector<uint8_t>>(in32_strong), "ABCDEF01");
1✔
446
         result.test_bin_eq("store_le(u32,strong)", Botan::store_le<std::vector<uint8_t>>(in32_strong), "01EFCDAB");
1✔
447

448
         test_arb_eq(
2✔
449
            result, "load_be(strong64)", Botan::load_be<TestInt64>(Botan::hex_decode("ABCDEF0123456789")), in64_strong);
2✔
450
         test_arb_eq(
2✔
451
            result, "load_le(strong64)", Botan::load_le<TestInt64>(Botan::hex_decode("8967452301EFCDAB")), in64_strong);
2✔
452
         test_arb_eq(
2✔
453
            result, "load_be(strong32)", Botan::load_be<TestInt32>(Botan::hex_decode("ABCDEF01")), in32_strong);
2✔
454
         test_arb_eq(
2✔
455
            result, "load_le(strong32)", Botan::load_le<TestInt32>(Botan::hex_decode("01EFCDAB")), in32_strong);
2✔
456

457
         const std::vector<TestInt64> some_in64_strongs{TestInt64{0xABCDEF0123456789}, TestInt64{0x0123456789ABCDEF}};
1✔
458
         result.test_bin_eq("store_be(vector,strong)",
1✔
459
                            Botan::store_be<std::vector<uint8_t>>(some_in64_strongs),
×
460
                            "ABCDEF01234567890123456789ABCDEF");
461
         result.test_bin_eq("store_le(vector,strong)",
1✔
462
                            Botan::store_le<std::vector<uint8_t>>(some_in64_strongs),
×
463
                            "8967452301EFCDABEFCDAB8967452301");
464

465
         const auto in64_strongs_le =
1✔
466
            Botan::load_le<std::array<TestInt64, 2>>(Botan::hex_decode("8967452301EFCDABEFCDAB8967452301"));
2✔
467
         test_arb_eq(result, "load_le(strong arr)", in64_strongs_le[0], TestInt64{0xABCDEF0123456789});
1✔
468
         test_arb_eq(result, "load_le(strong arr)", in64_strongs_le[1], TestInt64{0x0123456789ABCDEF});
1✔
469

470
         const auto in64_strongs_be =
1✔
471
            Botan::load_be<std::vector<TestInt64>>(Botan::hex_decode("ABCDEF01234567890123456789ABCDEF"));
2✔
472
         test_arb_eq(result, "load_be(strong arr)", in64_strongs_be[0], TestInt64{0xABCDEF0123456789});
1✔
473
         test_arb_eq(result, "load_be(strong arr)", in64_strongs_be[1], TestInt64{0x0123456789ABCDEF});
1✔
474

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

484
         const auto in32_enum_le = Botan::load_le<TestEnum32>(Botan::hex_decode("78563412"));
2✔
485
         result.test_enum_eq("load_le(enum32)", in32_enum_le, TestEnum32::_1);
1✔
486
         const auto in32_enum_be = Botan::load_be<TestEnum32>(Botan::hex_decode("78563412"));
2✔
487
         result.test_enum_eq("load_be(enum32)", in32_enum_be, TestEnum32::_2);
1✔
488
         result.test_bin_eq("store_le(enum32)", Botan::store_le<std::vector<uint8_t>>(TestEnum32::_1), "78563412");
1✔
489
         result.test_bin_eq("store_be(enum32)", Botan::store_be(TestEnum32::_2), "78563412");
1✔
490

491
         return result;
2✔
492
      }
10✔
493

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

499
      template <std::unsigned_integral T>
500
      static T fb_load_le(std::array<const uint8_t, sizeof(T)> in) {
3✔
501
         return Botan::detail::fallback_load_any<std::endian::little, T>(in);
1✔
502
      }
503

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

511
      template <std::unsigned_integral T>
512
      static decltype(auto) fb_store_le(const T in) {
3✔
513
         std::array<uint8_t, sizeof(T)> out{};
2✔
514
         Botan::detail::fallback_store_any<std::endian::little, T>(in, out);
1✔
515
         return out;
2✔
516
      }
517

518
      template <size_t N>
519
      using a = std::array<uint8_t, N>;
520

521
      static Test::Result test_loadstore_ambiguity() {
1✔
522
         // This is a regression test for a (probable) compiler bug in Xcode 15
523
         // where it would fail to compile the load/store functions for size_t
524
         //
525
         // It seems that this platform defines uint64_t as "unsigned long long"
526
         // and size_t as "unsigned long". Both are 64-bits but the compiler
527
         // was unable to disambiguate the two in reverse_bytes in bswap.h
528

529
         const uint32_t in32 = 0x01234567;
1✔
530
         const uint64_t in64 = 0x0123456789ABCDEF;
1✔
531
         const size_t inszt = 0x87654321;
1✔
532

533
         Test::Result result("Util load/store ambiguity");
1✔
534
         const auto out_be_32 = Botan::store_be(in32);
1✔
535
         const auto out_le_32 = Botan::store_le(in32);
1✔
536
         const auto out_be_64 = Botan::store_be(in64);
1✔
537
         const auto out_le_64 = Botan::store_le(in64);
1✔
538
         const auto out_be_szt = Botan::store_be(inszt);
1✔
539
         const auto out_le_szt = Botan::store_le(inszt);
1✔
540

541
         result.test_u32_eq("be 32", Botan::load_be<uint32_t>(out_be_32), in32);
1✔
542
         result.test_u32_eq("le 32", Botan::load_le<uint32_t>(out_le_32), in32);
1✔
543
         result.test_u64_eq("be 64", Botan::load_be<uint64_t>(out_be_64), in64);
1✔
544
         result.test_u64_eq("le 64", Botan::load_le<uint64_t>(out_le_64), in64);
1✔
545
         result.test_sz_eq("be sz", Botan::load_be<size_t>(out_be_szt), inszt);
1✔
546
         result.test_sz_eq("le sz", Botan::load_le<size_t>(out_le_szt), inszt);
1✔
547

548
         return result;
1✔
549
      }
×
550

551
      static Test::Result test_loadstore_fallback() {
1✔
552
         // The fallback implementation is only used if we don't know the
553
         // endianness of the target at compile time. This makes sure that the
554
         // fallback implementation is correct. On all typical platforms it
555
         // won't be called in production.
556
         Test::Result result("Util load/store fallback");
1✔
557

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

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

566
         result.test_bin_eq("sLE 16", fb_store_le<uint16_t>(0x0201), "0102");
1✔
567
         result.test_bin_eq("sLE 32", fb_store_le<uint32_t>(0x04030201), "01020304");
1✔
568
         result.test_bin_eq("sLE 64", fb_store_le<uint64_t>(0x0807060504030201), "0102030405060708");
1✔
569

570
         result.test_bin_eq("sBE 16", fb_store_be<uint16_t>(0x0102), "0102");
1✔
571
         result.test_bin_eq("sBE 32", fb_store_be<uint32_t>(0x01020304), "01020304");
1✔
572
         result.test_bin_eq("sBE 64", fb_store_be<uint64_t>(0x0102030405060708), "0102030405060708");
1✔
573

574
         return result;
1✔
575
      }
×
576

577
      static Test::Result test_loadstore_constexpr() {
1✔
578
         Test::Result result("Util load/store constexpr");
1✔
579

580
         constexpr uint16_t in16 = 0x1234;
1✔
581
         constexpr uint32_t in32 = 0xA0B0C0D0;
1✔
582
         constexpr uint64_t in64 = 0xABCDEF0123456789;
1✔
583

584
         // clang-format off
585
         constexpr std::array<uint8_t, 16> cex_mem = {
1✔
586
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
587
         };
588
         // clang-format on
589

590
         // get_byte<> w/ 16bit
591
         constexpr auto cex_byte_16_0 = Botan::get_byte<0>(in16);
1✔
592
         result.test_u8_eq(cex_byte_16_0, 0x12);
1✔
593
         constexpr auto cex_byte_16_1 = Botan::get_byte<1>(in16);
1✔
594
         result.test_u8_eq(cex_byte_16_1, 0x34);
1✔
595

596
         // get_byte<> w/ 32bit
597
         constexpr auto cex_byte_32_0 = Botan::get_byte<0>(in32);
1✔
598
         result.test_u8_eq(cex_byte_32_0, 0xA0);
1✔
599
         constexpr auto cex_byte_32_1 = Botan::get_byte<1>(in32);
1✔
600
         result.test_u8_eq(cex_byte_32_1, 0xB0);
1✔
601
         constexpr auto cex_byte_32_2 = Botan::get_byte<2>(in32);
1✔
602
         result.test_u8_eq(cex_byte_32_2, 0xC0);
1✔
603
         constexpr auto cex_byte_32_3 = Botan::get_byte<3>(in32);
1✔
604
         result.test_u8_eq(cex_byte_32_3, 0xD0);
1✔
605

606
         // get_byte<> w/ 64bit
607
         constexpr auto cex_byte_64_0 = Botan::get_byte<0>(in64);
1✔
608
         result.test_u8_eq(cex_byte_64_0, 0xAB);
1✔
609
         constexpr auto cex_byte_64_1 = Botan::get_byte<1>(in64);
1✔
610
         result.test_u8_eq(cex_byte_64_1, 0xCD);
1✔
611
         constexpr auto cex_byte_64_2 = Botan::get_byte<2>(in64);
1✔
612
         result.test_u8_eq(cex_byte_64_2, 0xEF);
1✔
613
         constexpr auto cex_byte_64_3 = Botan::get_byte<3>(in64);
1✔
614
         result.test_u8_eq(cex_byte_64_3, 0x01);
1✔
615
         constexpr auto cex_byte_64_4 = Botan::get_byte<4>(in64);
1✔
616
         result.test_u8_eq(cex_byte_64_4, 0x23);
1✔
617
         constexpr auto cex_byte_64_5 = Botan::get_byte<5>(in64);
1✔
618
         result.test_u8_eq(cex_byte_64_5, 0x45);
1✔
619
         constexpr auto cex_byte_64_6 = Botan::get_byte<6>(in64);
1✔
620
         result.test_u8_eq(cex_byte_64_6, 0x67);
1✔
621
         constexpr auto cex_byte_64_7 = Botan::get_byte<7>(in64);
1✔
622
         result.test_u8_eq(cex_byte_64_7, 0x89);
1✔
623

624
         // make_uintXX()
625
         constexpr auto cex_uint16_t = Botan::make_uint16(0x12, 0x34);
1✔
626
         result.test_u16_eq(cex_uint16_t, in16);
1✔
627
         constexpr auto cex_uint32_t = Botan::make_uint32(0xA0, 0xB0, 0xC0, 0xD0);
1✔
628
         result.test_u32_eq(cex_uint32_t, in32);
1✔
629
         constexpr auto cex_uint64_t = Botan::make_uint64(0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89);
1✔
630
         result.test_u64_eq(cex_uint64_t, in64);
1✔
631

632
         // store_le/be with a single integer
633
         constexpr std::array<uint8_t, 2> cex_store_le16 = Botan::store_le(in16);
1✔
634
         result.test_bin_eq("store_le(u16 arr)", cex_store_le16, "3412");
1✔
635
         constexpr std::array<uint8_t, 4> cex_store_le32 = Botan::store_le(in32);
1✔
636
         result.test_bin_eq("store_le(u32 arr)", cex_store_le32, "D0C0B0A0");
1✔
637
         constexpr std::array<uint8_t, 8> cex_store_le64 = Botan::store_le(in64);
1✔
638
         result.test_bin_eq("store_le(u32,arr)", cex_store_le64, "8967452301EFCDAB");
1✔
639

640
         constexpr std::array<uint8_t, 2> cex_store_be16 = Botan::store_be(in16);
1✔
641
         result.test_bin_eq("store_be(u16 arr)", cex_store_be16, "1234");
1✔
642
         constexpr std::array<uint8_t, 4> cex_store_be32 = Botan::store_be(in32);
1✔
643
         result.test_bin_eq("store_be(u32 arr)", cex_store_be32, "A0B0C0D0");
1✔
644
         constexpr std::array<uint8_t, 8> cex_store_be64 = Botan::store_be(in64);
1✔
645
         result.test_bin_eq("store_be(u64 arr)", cex_store_be64, "ABCDEF0123456789");
1✔
646

647
         // store_le/be with multiple integers, both as a parameter pack and a range (std::array for constexpr)
648
         constexpr std::array<uint8_t, 16> cex_store_le16s =
1✔
649
            Botan::store_le(in16, in16, in16, in16, in16, in16, in16, in16);
650
         constexpr std::array<uint8_t, 16> cex_store_le16s2 =
1✔
651
            Botan::store_le(std::array{in16, in16, in16, in16, in16, in16, in16, in16});
652
         result.test_bin_eq("store_le", cex_store_le16s, "34123412341234123412341234123412");
1✔
653
         result.test_bin_eq("cex_store_le16s", cex_store_le16s, cex_store_le16s2);
1✔
654
         constexpr std::array<uint8_t, 16> cex_store_le32s = Botan::store_le(in32, in32, in32, in32);
1✔
655
         constexpr std::array<uint8_t, 16> cex_store_le32s2 = Botan::store_le(std::array{in32, in32, in32, in32});
1✔
656
         result.test_bin_eq("cex_store_le32s", cex_store_le32s, "D0C0B0A0D0C0B0A0D0C0B0A0D0C0B0A0");
1✔
657
         result.test_bin_eq("cex_store_le32s2", cex_store_le32s, cex_store_le32s2);
1✔
658
         constexpr std::array<uint8_t, 16> cex_store_le64s = Botan::store_le(in64, in64);
1✔
659
         constexpr std::array<uint8_t, 16> cex_store_le64s2 = Botan::store_le(std::array{in64, in64});
1✔
660
         result.test_bin_eq("cex_store_le64s", cex_store_le64s, "8967452301EFCDAB8967452301EFCDAB");
1✔
661
         result.test_bin_eq("cex_store_le64s2", cex_store_le64s, cex_store_le64s2);
1✔
662

663
         constexpr std::array<uint8_t, 16> cex_store_be16s =
1✔
664
            Botan::store_be(in16, in16, in16, in16, in16, in16, in16, in16);
665
         constexpr std::array<uint8_t, 16> cex_store_be16s2 =
1✔
666
            Botan::store_be(std::array{in16, in16, in16, in16, in16, in16, in16, in16});
667
         result.test_bin_eq("cex_store_be16s", cex_store_be16s, "12341234123412341234123412341234");
1✔
668
         result.test_bin_eq("cex_store_be16s2", cex_store_be16s, cex_store_be16s2);
1✔
669
         constexpr std::array<uint8_t, 16> cex_store_be32s = Botan::store_be(in32, in32, in32, in32);
1✔
670
         constexpr std::array<uint8_t, 16> cex_store_be32s2 = Botan::store_be(std::array{in32, in32, in32, in32});
1✔
671
         result.test_bin_eq("cex_store_be32s", cex_store_be32s, "A0B0C0D0A0B0C0D0A0B0C0D0A0B0C0D0");
1✔
672
         result.test_bin_eq("cex_store_be32s2", cex_store_be32s, cex_store_be32s2);
1✔
673
         constexpr std::array<uint8_t, 16> cex_store_be64s = Botan::store_be(in64, in64);
1✔
674
         constexpr std::array<uint8_t, 16> cex_store_be64s2 = Botan::store_be(std::array{in64, in64});
1✔
675
         result.test_bin_eq("cex_store_be64s", cex_store_be64s, "ABCDEF0123456789ABCDEF0123456789");
1✔
676
         result.test_bin_eq("cex_store_be64s2", cex_store_be64s, cex_store_be64s2);
1✔
677

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

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

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

705
         constexpr auto cex_load_be16s = Botan::load_be<std::array<uint16_t, cex_mem.size() / 2>>(cex_mem);
1✔
706
         test_arb_eq(result,
1✔
707
                     "constexpr load_be(u16)",
708
                     cex_load_be16s,
709
                     {0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xAABB, 0xCCDD, 0xEEFF});
710
         constexpr auto cex_load_be32s = Botan::load_be<std::array<uint32_t, cex_mem.size() / 4>>(cex_mem);
1✔
711
         test_arb_eq(
1✔
712
            result, "constexpr load_be(u32)", cex_load_be32s, {0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF});
713
         constexpr auto cex_load_be64s = Botan::load_be<std::array<uint64_t, cex_mem.size() / 8>>(cex_mem);
1✔
714
         test_arb_eq(result, "constexpr load_be(u64)", cex_load_be64s, {0x0011223344556677, 0x8899AABBCCDDEEFF});
1✔
715

716
         return result;
1✔
717
      }
×
718

719
      static std::vector<Test::Result> test_copy_out_be_le() {
1✔
720
         return {
1✔
721
            CHECK("copy_out_be with 16bit input (word aligned)",
722
                  [&](auto& result) {
1✔
723
                     std::vector<uint8_t> out_vector(4);
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", out_vector, "0A0B0C0D");
1✔
727
                  }),
1✔
728

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

737
            CHECK("copy_out_le with 16bit input (word aligned)",
738
                  [&](auto& result) {
1✔
739
                     std::vector<uint8_t> out_vector(4);
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, "0B0A0D0C");
1✔
743
                  }),
1✔
744

745
            CHECK("copy_out_le with 16bit input (partial words)",
746
                  [&](auto& result) {
1✔
747
                     std::vector<uint8_t> out_vector(3);
1✔
748
                     const std::array<uint16_t, 2> in_array = {0x0A0B, 0x0C0D};
1✔
749
                     Botan::copy_out_le(out_vector, in_array);
1✔
750
                     result.test_bin_eq("copy_out_le(u16)", out_vector, "0B0A0D");
1✔
751
                  }),
1✔
752

753
            CHECK("copy_out_be 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_be(out_vector, in_array);
1✔
758
                     result.test_bin_eq("copy_out_be(u64)", out_vector, "0A0B0C0D0E0F10111213141516171819");
1✔
759
                  }),
1✔
760

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

769
            CHECK("copy_out_be 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_be(out_vector, in_array);
1✔
774
                     result.test_bin_eq("copy_out_be(u64)", out_vector, "0A0B0C0D0E0F101112131415161718");
1✔
775
                  }),
1✔
776

777
            CHECK("copy_out_le with 64bit input (partial words)",
778
                  [&](auto& result) {
1✔
779
                     std::vector<uint8_t> out_vector(15);
1✔
780
                     const std::array<uint64_t, 2> in_array = {0x0A0B0C0D0E0F1011, 0x1213141516171819};
1✔
781
                     Botan::copy_out_le(out_vector, in_array);
1✔
782
                     result.test_bin_eq("copy_out_le(u64)", out_vector, "11100F0E0D0C0B0A19181716151413");
1✔
783
                  }),
1✔
784
         };
9✔
785
      }
1✔
786
};
787

788
BOTAN_REGISTER_SMOKE_TEST("utils", "util", Utility_Function_Tests);
789

790
class BitOps_Tests final : public Test {
1✔
791
   public:
792
      std::vector<Test::Result> run() override {
1✔
793
         std::vector<Test::Result> results;
1✔
794

795
         results.push_back(test_power_of_2());
2✔
796
         results.push_back(test_ctz());
2✔
797
         results.push_back(test_sig_bytes());
2✔
798
         results.push_back(test_popcount());
2✔
799
         results.push_back(test_reverse_bits());
2✔
800

801
         return results;
1✔
802
      }
×
803

804
   private:
805
      template <typename T>
806
      void test_ctz(Test::Result& result, T val, size_t expected) {
6✔
807
         Botan::CT::poison(val);
808
         const size_t computed = Botan::ctz<T>(val);
6✔
809
         Botan::CT::unpoison_all(computed, val);
810
         result.test_sz_eq("ctz(" + std::to_string(val) + ")", computed, expected);
24✔
811
      }
6✔
812

813
      Test::Result test_ctz() {
1✔
814
         Test::Result result("ctz");
1✔
815
         test_ctz<uint32_t>(result, 0, 32);
1✔
816
         test_ctz<uint32_t>(result, 1, 0);
1✔
817
         test_ctz<uint32_t>(result, 0x80, 7);
1✔
818
         test_ctz<uint32_t>(result, 0x8000000, 27);
1✔
819
         test_ctz<uint32_t>(result, 0x8100000, 20);
1✔
820
         test_ctz<uint32_t>(result, 0x80000000, 31);
1✔
821

822
         return result;
1✔
823
      }
×
824

825
      template <typename T>
826
      void test_sig_bytes(Test::Result& result, T val, size_t expected) {
14✔
827
         Botan::CT::poison(val);
828
         const size_t computed = Botan::significant_bytes<T>(val);
14✔
829
         Botan::CT::unpoison_all(computed, val);
830
         result.test_sz_eq("significant_bytes(" + std::to_string(val) + ")", computed, expected);
56✔
831
      }
14✔
832

833
      Test::Result test_sig_bytes() {
1✔
834
         Test::Result result("significant_bytes");
1✔
835
         test_sig_bytes<uint32_t>(result, 0, 0);
1✔
836
         test_sig_bytes<uint32_t>(result, 1, 1);
1✔
837
         test_sig_bytes<uint32_t>(result, 0x80, 1);
1✔
838
         test_sig_bytes<uint32_t>(result, 255, 1);
1✔
839
         test_sig_bytes<uint32_t>(result, 256, 2);
1✔
840
         test_sig_bytes<uint32_t>(result, 65535, 2);
1✔
841
         test_sig_bytes<uint32_t>(result, 65536, 3);
1✔
842
         test_sig_bytes<uint32_t>(result, 0x80000000, 4);
1✔
843

844
         test_sig_bytes<uint64_t>(result, 0, 0);
1✔
845
         test_sig_bytes<uint64_t>(result, 1, 1);
1✔
846
         test_sig_bytes<uint64_t>(result, 0x80, 1);
1✔
847
         test_sig_bytes<uint64_t>(result, 256, 2);
1✔
848
         test_sig_bytes<uint64_t>(result, 0x80000000, 4);
1✔
849
         test_sig_bytes<uint64_t>(result, 0x100000000, 5);
1✔
850

851
         return result;
1✔
852
      }
×
853

854
      template <typename T>
855
      void test_power_of_2(Test::Result& result, T val, bool expected) {
15✔
856
         result.test_bool_eq("power_of_2(" + std::to_string(val) + ")", Botan::is_power_of_2<T>(val), expected);
75✔
857
      }
15✔
858

859
      Test::Result test_power_of_2() {
1✔
860
         Test::Result result("is_power_of_2");
1✔
861

862
         test_power_of_2<uint32_t>(result, 0, false);
1✔
863
         test_power_of_2<uint32_t>(result, 1, false);
1✔
864
         test_power_of_2<uint32_t>(result, 2, true);
1✔
865
         test_power_of_2<uint32_t>(result, 3, false);
1✔
866
         test_power_of_2<uint32_t>(result, 0x8000, true);
1✔
867
         test_power_of_2<uint32_t>(result, 0x8001, false);
1✔
868
         test_power_of_2<uint32_t>(result, 0x8000000, true);
1✔
869

870
         test_power_of_2<uint64_t>(result, 0, false);
1✔
871
         test_power_of_2<uint64_t>(result, 1, false);
1✔
872
         test_power_of_2<uint64_t>(result, 2, true);
1✔
873
         test_power_of_2<uint64_t>(result, 3, false);
1✔
874
         test_power_of_2<uint64_t>(result, 0x8000, true);
1✔
875
         test_power_of_2<uint64_t>(result, 0x8001, false);
1✔
876
         test_power_of_2<uint64_t>(result, 0x8000000, true);
1✔
877
         test_power_of_2<uint64_t>(result, 0x100000000000, true);
1✔
878

879
         return result;
1✔
880
      }
×
881

882
      template <typename T>
883
      auto pc(T val) -> decltype(Botan::ct_popcount(val)) {
2✔
884
         return Botan::ct_popcount(val);
6✔
885
      }
886

887
      template <typename T>
888
      auto random_pc(Test::Result& result) {
4✔
889
         auto n = Botan::load_le<T>(Test::rng().random_array<sizeof(T)>());
4✔
890
         result.test_sz_eq(Botan::fmt("popcount({}) == {}", n, std::popcount(n)), pc(n), std::popcount(n));
4✔
891
      }
4✔
892

893
      Test::Result test_popcount() {
1✔
894
         Test::Result result("popcount");
1✔
895

896
         result.test_u8_eq("popcount<uint8_t>(0)", pc<uint8_t>(0), 0);
1✔
897
         result.test_u8_eq("popcount<uint16_t>(0)", pc<uint16_t>(0), 0);
1✔
898
         result.test_u8_eq("popcount<uint32_t>(0)", pc<uint32_t>(0), 0);
1✔
899
         result.test_u8_eq("popcount<uint64_t>(0)", pc<uint64_t>(0), 0);
1✔
900

901
         result.test_u8_eq("popcount<uint8_t>(1)", pc<uint8_t>(1), 1);
1✔
902
         result.test_u8_eq("popcount<uint16_t>(1)", pc<uint16_t>(1), 1);
1✔
903
         result.test_u8_eq("popcount<uint32_t>(1)", pc<uint32_t>(1), 1);
1✔
904
         result.test_u8_eq("popcount<uint64_t>(1)", pc<uint64_t>(1), 1);
1✔
905

906
         result.test_u8_eq("popcount<uint8_t>(0xAA)", pc<uint8_t>(0xAA), 4);
1✔
907
         result.test_u8_eq("popcount<uint16_t>(0xAAAA)", pc<uint16_t>(0xAAAA), 8);
1✔
908
         result.test_u8_eq("popcount<uint32_t>(0xAAAA...)", pc<uint32_t>(0xAAAAAAAA), 16);
1✔
909
         result.test_u8_eq("popcount<uint64_t>(0xAAAA...)", pc<uint64_t>(0xAAAAAAAAAAAAAAAA), 32);
1✔
910

911
         result.test_u8_eq("popcount<uint8_t>(0xFF)", pc<uint8_t>(0xFF), 8);
1✔
912
         result.test_u8_eq("popcount<uint16_t>(0xFFFF)", pc<uint16_t>(0xFFFF), 16);
1✔
913
         result.test_u8_eq("popcount<uint32_t>(0xFFFF...)", pc<uint32_t>(0xFFFFFFFF), 32);
1✔
914
         result.test_u8_eq("popcount<uint64_t>(0xFFFF...)", pc<uint64_t>(0xFFFFFFFFFFFFFFFF), 64);
1✔
915

916
         random_pc<uint8_t>(result);
1✔
917
         random_pc<uint16_t>(result);
1✔
918
         random_pc<uint32_t>(result);
1✔
919
         random_pc<uint64_t>(result);
1✔
920

921
         return result;
1✔
922
      }
×
923

924
      Test::Result test_reverse_bits() {
1✔
925
         Test::Result result("reverse_bits");
1✔
926

927
         result.test_u8_eq("rev(0u8)", Botan::ct_reverse_bits<uint8_t>(0b00000000), 0b00000000);
1✔
928
         result.test_u8_eq("rev(1u8)", Botan::ct_reverse_bits<uint8_t>(0b01010101), 0b10101010);
1✔
929
         result.test_u8_eq("rev(2u8)", Botan::ct_reverse_bits<uint8_t>(0b01001011), 0b11010010);
1✔
930

931
         result.test_u16_eq("rev(0u16)", Botan::ct_reverse_bits<uint16_t>(0b0000000000000000), 0b0000000000000000);
1✔
932
         result.test_u16_eq("rev(1u16)", Botan::ct_reverse_bits<uint16_t>(0b0101010101010101), 0b1010101010101010);
1✔
933
         result.test_u16_eq("rev(2u16)", Botan::ct_reverse_bits<uint16_t>(0b0100101101011010), 0b0101101011010010);
1✔
934

935
         result.test_u32_eq("rev(0u32)", Botan::ct_reverse_bits<uint32_t>(0xFFFFFFFF), 0xFFFFFFFF);
1✔
936
         result.test_u32_eq("rev(1u32)", Botan::ct_reverse_bits<uint32_t>(0x55555555), 0xAAAAAAAA);
1✔
937
         result.test_u32_eq("rev(2u32)", Botan::ct_reverse_bits<uint32_t>(0x4B6A2C1D), 0xB83456D2);
1✔
938

939
         result.test_u64_eq("rev(0u64)", Botan::ct_reverse_bits<uint64_t>(0xF0E0D0C005040302), 0x40C020A0030B070F);
1✔
940
         result.test_u64_eq("rev(1u64)", Botan::ct_reverse_bits<uint64_t>(0x5555555555555555), 0xAAAAAAAAAAAAAAAA);
1✔
941
         result.test_u64_eq("rev(2u64)", Botan::ct_reverse_bits<uint64_t>(0x4B6A2C1D5E7F8A90), 0x951FE7AB83456D2);
1✔
942

943
         return result;
1✔
944
      }
×
945
};
946

947
BOTAN_REGISTER_TEST("utils", "bit_ops", BitOps_Tests);
948

949
#if defined(BOTAN_HAS_POLY_DBL)
950

951
class Poly_Double_Tests final : public Text_Based_Test {
×
952
   public:
953
      Poly_Double_Tests() : Text_Based_Test("poly_dbl.vec", "In,Out") {}
2✔
954

955
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
82✔
956
         Test::Result result("Polynomial doubling");
82✔
957
         const std::vector<uint8_t> in = vars.get_req_bin("In");
82✔
958
         const std::vector<uint8_t> out = vars.get_req_bin("Out");
82✔
959

960
         std::vector<uint8_t> b = in;
82✔
961
         Botan::poly_double_n(b.data(), b.size());
82✔
962

963
         result.test_bin_eq("Expected value", b, out);
82✔
964
         return result;
82✔
965
      }
246✔
966
};
967

968
BOTAN_REGISTER_TEST("utils", "poly_dbl", Poly_Double_Tests);
969

970
#endif
971

972
class Version_Tests final : public Test {
1✔
973
   public:
974
      std::vector<Test::Result> run() override {
1✔
975
         Test::Result result("Versions");
1✔
976

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

979
         const char* version_cstr = Botan::version_cstr();
1✔
980
         const std::string version_str = Botan::version_string();
1✔
981
         result.test_str_eq("Same version string", version_str, std::string(version_cstr));
1✔
982

983
         const char* sversion_cstr = Botan::short_version_cstr();
1✔
984
         const std::string sversion_str = Botan::short_version_string();
1✔
985
         result.test_str_eq("Same short version string", sversion_str, std::string(sversion_cstr));
1✔
986

987
         const auto expected_sversion =
1✔
988
            Botan::fmt("{}.{}.{}", BOTAN_VERSION_MAJOR, BOTAN_VERSION_MINOR, BOTAN_VERSION_PATCH);
1✔
989

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

993
         const std::string version_check_ok =
1✔
994
            Botan::runtime_version_check(BOTAN_VERSION_MAJOR, BOTAN_VERSION_MINOR, BOTAN_VERSION_PATCH);
1✔
995

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

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

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

1003
         result.test_str_eq("Expected warning text", version_check_bad, expected_error);
1✔
1004

1005
         return {result};
3✔
1006
      }
2✔
1007
};
1008

1009
BOTAN_REGISTER_TEST("utils", "versioning", Version_Tests);
1010

1011
class Date_Format_Tests final : public Text_Based_Test {
×
1012
   public:
1013
      Date_Format_Tests() : Text_Based_Test("dates.vec", "Date") {}
2✔
1014

1015
      static std::vector<uint32_t> parse_date(const std::string& s) {
11✔
1016
         const std::vector<std::string> parts = Botan::split_on(s, ',');
11✔
1017
         if(parts.size() != 6) {
11✔
1018
            throw Test_Error("Bad date format '" + s + "'");
×
1019
         }
1020

1021
         std::vector<uint32_t> u32s;
11✔
1022
         u32s.reserve(parts.size());
11✔
1023
         for(const auto& sub : parts) {
77✔
1024
            u32s.push_back(Botan::to_u32bit(sub));
66✔
1025
         }
1026
         return u32s;
11✔
1027
      }
11✔
1028

1029
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
11✔
1030
         const std::string date_str = vars.get_req_str("Date");
11✔
1031
         Test::Result result("Date parsing");
11✔
1032

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

1035
         if(type == "valid" || type == "valid.not_std" || type == "valid.64_bit_time_t") {
11✔
1036
            const Botan::calendar_point c(d[0], d[1], d[2], d[3], d[4], d[5]);
11✔
1037
            result.test_u32_eq(date_str + " year", c.year(), d[0]);
11✔
1038
            result.test_u32_eq(date_str + " month", c.month(), d[1]);
11✔
1039
            result.test_u32_eq(date_str + " day", c.day(), d[2]);
11✔
1040
            result.test_u32_eq(date_str + " hour", c.hour(), d[3]);
11✔
1041
            result.test_u32_eq(date_str + " minute", c.minutes(), d[4]);
11✔
1042
            result.test_u32_eq(date_str + " second", c.seconds(), d[5]);
11✔
1043

1044
            if(type == "valid.not_std" ||
11✔
1045
               (type == "valid.64_bit_time_t" && c.year() > 2037 && sizeof(std::time_t) == 4)) {
1046
               result.test_throws("valid but out of std::timepoint range", [c]() { c.to_std_timepoint(); });
6✔
1047
            } else {
1048
               const Botan::calendar_point c2(c.to_std_timepoint());
8✔
1049
               result.test_u32_eq(date_str + " year", c2.year(), d[0]);
8✔
1050
               result.test_u32_eq(date_str + " month", c2.month(), d[1]);
8✔
1051
               result.test_u32_eq(date_str + " day", c2.day(), d[2]);
8✔
1052
               result.test_u32_eq(date_str + " hour", c2.hour(), d[3]);
8✔
1053
               result.test_u32_eq(date_str + " minute", c2.minutes(), d[4]);
8✔
1054
               result.test_u32_eq(date_str + " second", c2.seconds(), d[5]);
16✔
1055
            }
1056
         } else if(type == "invalid") {
×
1057
            result.test_throws("invalid date",
×
1058
                               [d]() { const Botan::calendar_point c(d[0], d[1], d[2], d[3], d[4], d[5]); });
×
1059
         } else {
1060
            throw Test_Error("Unexpected header '" + type + "' in date format tests");
×
1061
         }
1062

1063
         return result;
22✔
1064
      }
125✔
1065

1066
      std::vector<Test::Result> run_final_tests() override {
1✔
1067
         Test::Result result("calendar_point::to_string");
1✔
1068
         const Botan::calendar_point d(2008, 5, 15, 9, 30, 33);
1✔
1069
         // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
1070
         result.test_str_eq("calendar_point::to_string", d.to_string(), "2008-05-15T09:30:33");
1✔
1071
         return {result};
3✔
1072
      }
2✔
1073
};
1074

1075
BOTAN_REGISTER_TEST("utils", "util_dates", Date_Format_Tests);
1076

1077
class Charset_Tests final : public Text_Based_Test {
×
1078
   public:
1079
      Charset_Tests() : Text_Based_Test("charset.vec", "In,Out") {}
2✔
1080

1081
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
53✔
1082
         Test::Result result("Charset");
53✔
1083

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

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

1088
         if(type == "UTF8-UCS2-INVALID") {
53✔
1089
            result.test_throws<Botan::Decoding_Error>("utf8_to_ucs2 rejects invalid input",
13✔
1090
                                                      [&] { Botan::utf8_to_ucs2(in_sv); });
26✔
1091
            return result;
13✔
1092
         }
1093

1094
         if(type == "UTF8-UCS4-INVALID") {
40✔
1095
            result.test_throws<Botan::Decoding_Error>("utf8_to_ucs4 rejects invalid input",
10✔
1096
                                                      [&] { Botan::utf8_to_ucs4(in_sv); });
20✔
1097
            return result;
10✔
1098
         }
1099

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

1102
         std::string converted;
30✔
1103

1104
         if(type == "UCS2-UTF8") {
30✔
1105
            converted = Botan::ucs2_to_utf8(in);
4✔
1106
         } else if(type == "UCS4-UTF8") {
26✔
1107
            converted = Botan::ucs4_to_utf8(in);
1✔
1108
         } else if(type == "UTF8-UCS2") {
25✔
1109
            std::vector<uint8_t> ucs2 = Botan::utf8_to_ucs2(in_sv);
11✔
1110
            converted = std::string(ucs2.begin(), ucs2.end());
22✔
1111
         } else if(type == "UTF8-UCS4") {
25✔
1112
            std::vector<uint8_t> ucs4 = Botan::utf8_to_ucs4(in_sv);
11✔
1113
            converted = std::string(ucs4.begin(), ucs4.end());
22✔
1114
         } else if(type == "LATIN1-UTF8") {
14✔
1115
            converted = Botan::latin1_to_utf8(in);
3✔
1116
         } else {
1117
            throw Test_Error("Unexpected header '" + type + "' in charset tests");
×
1118
         }
1119

1120
         result.test_bin_eq(
30✔
1121
            "string converted successfully", std::vector<uint8_t>(converted.begin(), converted.end()), expected);
30✔
1122

1123
         return result;
30✔
1124
      }
113✔
1125
};
1126

1127
BOTAN_REGISTER_TEST("utils", "charset", Charset_Tests);
1128

1129
class Hostname_Tests final : public Text_Based_Test {
×
1130
   public:
1131
      Hostname_Tests() : Text_Based_Test("hostnames.vec", "Issued,Hostname") {}
2✔
1132

1133
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
50✔
1134
         Test::Result result("Hostname Matching");
50✔
1135

1136
         const std::string issued = vars.get_req_str("Issued");
50✔
1137
         const std::string hostname = vars.get_req_str("Hostname");
50✔
1138
         const bool expected = (type == "Invalid") ? false : true;
50✔
1139

1140
         const std::string what = hostname + ((expected == true) ? " matches " : " does not match ") + issued;
100✔
1141
         result.test_bool_eq(what, Botan::host_wildcard_match(issued, hostname), expected);
50✔
1142

1143
         return result;
50✔
1144
      }
50✔
1145
};
1146

1147
BOTAN_REGISTER_TEST("utils", "hostname", Hostname_Tests);
1148

1149
class DNS_Check_Tests final : public Text_Based_Test {
×
1150
   public:
1151
      DNS_Check_Tests() : Text_Based_Test("utils/dns.vec", "DNS") {}
2✔
1152

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

1156
         const std::string name = vars.get_req_str("DNS");
59✔
1157
         const bool valid = (type == "Invalid") ? false : true;
59✔
1158

1159
         try {
59✔
1160
            const auto canonicalized = Botan::check_and_canonicalize_dns_name(name);
59✔
1161
            BOTAN_UNUSED(canonicalized);
25✔
1162

1163
            if(valid) {
25✔
1164
               result.test_success("Accepted valid name");
25✔
1165
            } else {
1166
               result.test_failure("Accepted invalid name");
×
1167
            }
1168
         } catch(Botan::Decoding_Error&) {
59✔
1169
            if(valid) {
34✔
1170
               result.test_failure("Rejected valid name");
×
1171
            } else {
1172
               result.test_success("Rejected invalid name");
34✔
1173
            }
1174
         }
34✔
1175

1176
         return result;
59✔
1177
      }
59✔
1178
};
1179

1180
BOTAN_REGISTER_TEST("utils", "dns_check", DNS_Check_Tests);
1181

1182
class IPv4_Parsing_Tests final : public Text_Based_Test {
×
1183
   public:
1184
      IPv4_Parsing_Tests() : Text_Based_Test("utils/ipv4.vec", "IPv4") {}
2✔
1185

1186
      Test::Result run_one_test(const std::string& status, const VarMap& vars) override {
47✔
1187
         Test::Result result("IPv4 parsing");
47✔
1188

1189
         const std::string input = vars.get_req_str("IPv4");
47✔
1190
         const bool valid = (status == "Valid");
47✔
1191

1192
         auto ipv4 = Botan::string_to_ipv4(input);
47✔
1193

1194
         result.test_bool_eq("string_to_ipv4 accepts only valid", ipv4.has_value(), valid);
47✔
1195

1196
         if(ipv4) {
47✔
1197
            const std::string rt = Botan::ipv4_to_string(ipv4.value());
13✔
1198
            result.test_str_eq("ipv4_to_string and string_to_ipv4 round trip", input, rt);
13✔
1199
         }
13✔
1200

1201
         return result;
47✔
1202
      }
47✔
1203
};
1204

1205
BOTAN_REGISTER_TEST("utils", "ipv4_parse", IPv4_Parsing_Tests);
1206

1207
#if defined(BOTAN_HAS_IPV6_ADDRESS)
1208

1209
class IPv6_Parsing_Tests final : public Text_Based_Test {
×
1210
   public:
1211
      IPv6_Parsing_Tests() : Text_Based_Test("utils/ipv6.vec", "IPv6") {}
2✔
1212

1213
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
39✔
1214
         Test::Result result("IPv6 parsing");
39✔
1215

1216
         const std::string input = vars.get_req_str("IPv6");
39✔
1217
         const bool valid = (header == "Valid");
39✔
1218

1219
         auto ipv6 = Botan::IPv6Address::from_string(input);
39✔
1220

1221
         result.test_bool_eq("IPv6Address::from_string accepts only valid", ipv6.has_value(), valid);
39✔
1222

1223
         if(ipv6) {
39✔
1224
            const std::string rt = ipv6->to_string();
10✔
1225
            result.test_str_eq("IPv6Address to_string and from_string round trip", input, rt);
10✔
1226
         }
10✔
1227

1228
         return result;
39✔
1229
      }
39✔
1230
};
1231

1232
BOTAN_REGISTER_TEST("utils", "ipv6_parse", IPv6_Parsing_Tests);
1233

1234
class IPv6_Noncanonical_Parsing_Tests final : public Text_Based_Test {
×
1235
   public:
1236
      IPv6_Noncanonical_Parsing_Tests() : Text_Based_Test("utils/ipv6_nc.vec", "Input,Canonical") {}
2✔
1237

1238
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
14✔
1239
         Test::Result result("IPv6 parsing of non-canonical form");
14✔
1240

1241
         const std::string input_str = vars.get_req_str("Input");
14✔
1242
         const std::string canonical_str = vars.get_req_str("Canonical");
14✔
1243

1244
         const auto ipv6 = Botan::IPv6Address::from_string(input_str);
14✔
1245
         const auto canonical = Botan::IPv6Address::from_string(canonical_str);
14✔
1246

1247
         result.test_is_true("IPv6 non-canonical parsing worked", ipv6.has_value());
14✔
1248
         result.test_is_true("IPv6 canonical parsing worked", canonical.has_value());
14✔
1249

1250
         if(ipv6.has_value() && canonical.has_value()) {
14✔
1251
            result.test_is_true("IPv6 non-canonical decoding", *ipv6 == *canonical);
14✔
1252
         }
1253

1254
         return result;
14✔
1255
      }
14✔
1256
};
1257

1258
BOTAN_REGISTER_TEST("utils", "ipv6_parse_non_canonical", IPv6_Noncanonical_Parsing_Tests);
1259

1260
#endif
1261

1262
class ReadKV_Tests final : public Text_Based_Test {
×
1263
   public:
1264
      ReadKV_Tests() : Text_Based_Test("utils/read_kv.vec", "Input,Expected") {}
2✔
1265

1266
      Test::Result run_one_test(const std::string& status, const VarMap& vars) override {
16✔
1267
         Test::Result result("read_kv");
16✔
1268

1269
         const bool is_valid = (status == "Valid");
16✔
1270

1271
         const std::string input = vars.get_req_str("Input");
16✔
1272
         const std::string expected = vars.get_req_str("Expected");
16✔
1273

1274
         if(is_valid) {
16✔
1275
            confirm_kv(result, Botan::read_kv(input), split_group(expected));
14✔
1276
         } else {
1277
            // In this case "expected" is the expected exception message
1278
            result.test_throws("Invalid key value input throws exception", expected, [&]() { Botan::read_kv(input); });
18✔
1279
         }
1280
         return result;
16✔
1281
      }
16✔
1282

1283
   private:
1284
      static std::vector<std::string> split_group(const std::string& str) {
7✔
1285
         std::vector<std::string> elems;
7✔
1286
         if(str.empty()) {
7✔
1287
            return elems;
1288
         }
1289

1290
         std::string substr;
6✔
1291
         for(const char c : str) {
115✔
1292
            if(c == '|') {
109✔
1293
               elems.push_back(substr);
16✔
1294
               substr.clear();
16✔
1295
            } else {
1296
               substr += c;
202✔
1297
            }
1298
         }
1299

1300
         if(!substr.empty()) {
6✔
1301
            elems.push_back(substr);
6✔
1302
         }
1303

1304
         return elems;
6✔
1305
      }
6✔
1306

1307
      static void confirm_kv(Test::Result& result,
7✔
1308
                             const std::map<std::string, std::string>& kv,
1309
                             const std::vector<std::string>& expected) {
1310
         if(!result.test_sz_eq("expected size", expected.size() % 2, size_t(0))) {
7✔
1311
            return;
1312
         }
1313

1314
         for(size_t i = 0; i != expected.size(); i += 2) {
18✔
1315
            auto j = kv.find(expected[i]);
11✔
1316
            if(result.test_is_true("Found key", j != kv.end())) {
11✔
1317
               result.test_str_eq("Matching value", j->second, expected[i + 1]);
11✔
1318
            }
1319
         }
1320

1321
         result.test_sz_eq("KV has same size as expected", kv.size(), expected.size() / 2);
7✔
1322
      }
1323
};
1324

1325
BOTAN_REGISTER_TEST("utils", "util_read_kv", ReadKV_Tests);
1326

1327
#if defined(BOTAN_HAS_CPUID)
1328

1329
class CPUID_Tests final : public Test {
1✔
1330
   public:
1331
      std::vector<Test::Result> run() override {
1✔
1332
         Test::Result result("CPUID");
1✔
1333

1334
         const std::string cpuid_string = Botan::CPUID::to_string();
1✔
1335
         result.test_success("CPUID::to_string doesn't crash");
1✔
1336

1337
         for(size_t b = 0; b != 32; ++b) {
33✔
1338
            try {
32✔
1339
               const auto bit = static_cast<uint32_t>(1) << b;
32✔
1340
               // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
1341
               const auto feat = Botan::CPUID::Feature(static_cast<Botan::CPUID::Feature::Bit>(bit));
32✔
1342

1343
               const std::string feat_str = feat.to_string();
32✔
1344

1345
               result.test_is_true("Feature string is not empty", !feat_str.empty());
20✔
1346

1347
               if(auto from_str = Botan::CPUID::Feature::from_string(feat_str)) {
20✔
1348
                  result.test_u32_eq("Feature::from_string returns expected bit", from_str->as_u32(), bit);
20✔
1349
               } else {
1350
                  result.test_failure(
×
1351
                     Botan::fmt("Feature::from_string didn't recognize its own output ({})", feat_str));
×
1352
               }
1353
            } catch(Botan::Invalid_State&) {
32✔
1354
               // This will thrown if the bit is not a valid one
1355
            }
12✔
1356
         }
1357

1358
   #if defined(BOTAN_TARGET_ARCH_IS_X86_FAMILY)
1359

1360
         const auto bit = Botan::CPUID::Feature::SSE2;
1✔
1361

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

1365
            Botan::CPUID::clear_cpuid_bit(bit);
1✔
1366

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

1369
            Botan::CPUID::initialize();  // reset state
1✔
1370
            result.test_is_true("After reinitializing, CPUID::has for SSE2 returns true again", Botan::CPUID::has(bit));
1✔
1371
         }
1372
   #else
1373
         BOTAN_UNUSED(cpuid_string);
1374
   #endif
1375

1376
         return {result};
3✔
1377
      }
2✔
1378
};
1379

1380
BOTAN_REGISTER_SERIALIZED_TEST("utils", "cpuid", CPUID_Tests);
1381

1382
#endif
1383

1384
#if defined(BOTAN_HAS_UUID)
1385

1386
class UUID_Tests : public Test {
1✔
1387
   public:
1388
      std::vector<Test::Result> run() override {
1✔
1389
         Test::Result result("UUID");
1✔
1390

1391
         const Botan::UUID empty_uuid;
1✔
1392
         const Botan::UUID random_uuid1(this->rng());
1✔
1393
         const Botan::UUID random_uuid2(this->rng());
1✔
1394
         const Botan::UUID loaded_uuid(std::vector<uint8_t>(16, 4));
1✔
1395

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

1399
         result.test_is_false("Empty UUID is empty", empty_uuid.is_valid());
1✔
1400
         result.test_is_true("Empty UUID equals another empty UUID", empty_uuid == Botan::UUID());
1✔
1401

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

1404
         result.test_is_true("Random UUID not empty", random_uuid1.is_valid());
1✔
1405
         result.test_is_true("Random UUID not empty", random_uuid2.is_valid());
1✔
1406

1407
         result.test_is_true("Random UUIDs are distinct", random_uuid1 != random_uuid2);
1✔
1408
         result.test_is_true("Random UUIDs not equal to empty", random_uuid1 != empty_uuid);
1✔
1409

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

1413
         const std::string uuid_r1_str = random_uuid1.to_string();
1✔
1414
         result.test_is_true("UUID from string matches", Botan::UUID(uuid_r1_str) == random_uuid1);
1✔
1415

1416
         class AllSame_RNG : public Botan::RandomNumberGenerator {
×
1417
            public:
1418
               explicit AllSame_RNG(uint8_t b) : m_val(b) {}
2✔
1419

1420
               void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> /* ignored */) override {
2✔
1421
                  std::fill(output.begin(), output.end(), m_val);
2✔
1422
               }
2✔
1423

1424
               std::string name() const override { return "zeros"; }
×
1425

1426
               bool accepts_input() const override { return false; }
×
1427

1428
               void clear() override {}
×
1429

1430
               bool is_seeded() const override { return true; }
×
1431

1432
            private:
1433
               uint8_t m_val;
1434
         };
1435

1436
         AllSame_RNG zeros(0x00);
1✔
1437
         const Botan::UUID zero_uuid(zeros);
1✔
1438
         result.test_str_eq(
1✔
1439
            "Zero UUID matches expected", zero_uuid.to_string(), "00000000-0000-4000-8000-000000000000");
1✔
1440

1441
         AllSame_RNG ones(0xFF);
1✔
1442
         const Botan::UUID ones_uuid(ones);
1✔
1443
         result.test_str_eq(
1✔
1444
            "Ones UUID matches expected", ones_uuid.to_string(), "FFFFFFFF-FFFF-4FFF-BFFF-FFFFFFFFFFFF");
1✔
1445

1446
         return {result};
3✔
1447
      }
6✔
1448
};
1449

1450
BOTAN_REGISTER_TEST("utils", "uuid", UUID_Tests);
1451

1452
#endif
1453

1454
class Formatter_Tests : public Test {
1✔
1455
   public:
1456
      std::vector<Test::Result> run() override {
1✔
1457
         Test::Result result("Format utility");
1✔
1458

1459
         /*
1460
         In a number of these tests, we are not strictly depending on the
1461
         behavior, for instance checking `fmt("{}") == "{}"` is more about
1462
         checking that we don't crash, rather than we return that precise string.
1463
         */
1464

1465
         result.test_str_eq("test 1", Botan::fmt("hi"), "hi");
1✔
1466
         result.test_str_eq("test 2", Botan::fmt("ignored", 5), "ignored");
1✔
1467
         result.test_str_eq("test 3", Botan::fmt("answer is {}", 42), "answer is 42");
1✔
1468
         result.test_str_eq("test 4", Botan::fmt("{", 5), "{");
1✔
1469
         result.test_str_eq("test 4", Botan::fmt("{}"), "{}");
1✔
1470
         result.test_str_eq("test 5", Botan::fmt("{} == '{}'", 5, "five"), "5 == 'five'");
1✔
1471

1472
         return {result};
3✔
1473
      }
2✔
1474
};
1475

1476
BOTAN_REGISTER_TEST("utils", "fmt", Formatter_Tests);
1477

1478
class ScopedCleanup_Tests : public Test {
1✔
1479
   public:
1480
      std::vector<Test::Result> run() override {
1✔
1481
         return {
1✔
1482
            CHECK("leaving a scope results in cleanup",
1483
                  [](Test::Result& result) {
1✔
1484
                     bool ran = false;
1✔
1485
                     {
1✔
1486
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1✔
1487
                     }
1✔
1488
                     result.test_is_true("cleanup ran", ran);
1✔
1489
                  }),
1✔
1490

1491
            CHECK("leaving a function, results in cleanup",
1492
                  [](Test::Result& result) {
1✔
1493
                     bool ran = false;
1✔
1494
                     bool fn_called = false;
1✔
1495
                     auto fn = [&] {
2✔
1496
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1✔
1497
                        fn_called = true;
1✔
1498
                     };
2✔
1499

1500
                     result.test_is_true("cleanup not yet ran", !ran);
1✔
1501
                     fn();
1✔
1502
                     result.test_is_true("fn called", fn_called);
1✔
1503
                     result.test_is_true("cleanup ran", ran);
1✔
1504
                  }),
1✔
1505

1506
            CHECK("stack unwinding results in cleanup",
1507
                  [](Test::Result& result) {
1✔
1508
                     bool ran = false;
1✔
1509
                     bool fn_called = false;
1✔
1510
                     bool exception_caught = false;
1✔
1511
                     auto fn = [&] {
2✔
1512
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1✔
1513
                        fn_called = true;
1✔
1514
                        throw std::runtime_error("test");
1✔
1515
                     };
2✔
1516

1517
                     result.test_is_true("cleanup not yet ran", !ran);
1✔
1518
                     try {
1✔
1519
                        fn();
1✔
1520
                     } catch(const std::exception&) {
1✔
1521
                        exception_caught = true;
1✔
1522
                     }
1✔
1523

1524
                     result.test_is_true("fn called", fn_called);
1✔
1525
                     result.test_is_true("cleanup ran", ran);
1✔
1526
                     result.test_is_true("exception caught", exception_caught);
1✔
1527
                  }),
1✔
1528

1529
            CHECK("cleanup isn't called after disengaging",
1530
                  [](Test::Result& result) {
1✔
1531
                     bool ran = false;
1✔
1532
                     {
1✔
1533
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1✔
1534
                        clean.disengage();
1✔
1535
                     }
1✔
1536
                     result.test_is_true("cleanup not ran", !ran);
1✔
1537
                  }),
1✔
1538

1539
         };
5✔
1540
      }
1✔
1541
};
1542

1543
BOTAN_REGISTER_TEST("utils", "scoped_cleanup", ScopedCleanup_Tests);
1544

1545
}  // namespace
1546

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

© 2026 Coveralls, Inc