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

randombit / botan / 19586471228

21 Nov 2025 11:36PM UTC coverage: 90.802% (+0.2%) from 90.627%
19586471228

Pull #5167

github

web-flow
Merge dc170f795 into f8eb34002
Pull Request #5167: Changes to reduce unnecessary inclusions

100849 of 111065 relevant lines covered (90.8%)

12790156.99 hits per line

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

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

23
#include <bit>
24

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

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

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

37
namespace Botan_Tests {
38

39
namespace {
40

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

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

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

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

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

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

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

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

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

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

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

93
         return result;
1✔
94
      }
×
95

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

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

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

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

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

114
         return result;
1✔
115
      }
×
116

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

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

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

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

131
         return result;
1✔
132
      }
×
133

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

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

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

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

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

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

165
         return result;
1✔
166
      }
1✔
167

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

239
         for(size_t offset = 0; offset != 7; ++offset) {
8✔
240
            uint8_t* out = outbuf + offset;
7✔
241

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

476
         return result;
2✔
477
      }
11✔
478

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

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

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

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

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

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

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

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

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

533
         return result;
1✔
534
      }
×
535

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

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

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

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

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

559
         return result;
1✔
560
      }
×
561

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

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

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

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

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

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

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

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

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

632
         // store_le/be with multiple integers, both as a parameter pack and a range (std::array for constexpr)
633
         constexpr std::array<uint8_t, 16> cex_store_le16s =
1✔
634
            Botan::store_le(in16, in16, in16, in16, in16, in16, in16, in16);
635
         constexpr std::array<uint8_t, 16> cex_store_le16s2 =
1✔
636
            Botan::store_le(std::array{in16, in16, in16, in16, in16, in16, in16, in16});
637
         result.test_is_eq(
1✔
638
            cex_store_le16s,
639
            {0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12});
640
         result.test_is_eq(cex_store_le16s, cex_store_le16s2);
1✔
641
         constexpr std::array<uint8_t, 16> cex_store_le32s = Botan::store_le(in32, in32, in32, in32);
1✔
642
         constexpr std::array<uint8_t, 16> cex_store_le32s2 = Botan::store_le(std::array{in32, in32, in32, in32});
1✔
643
         result.test_is_eq(
1✔
644
            cex_store_le32s,
645
            {0xD0, 0xC0, 0xB0, 0xA0, 0xD0, 0xC0, 0xB0, 0xA0, 0xD0, 0xC0, 0xB0, 0xA0, 0xD0, 0xC0, 0xB0, 0xA0});
646
         result.test_is_eq(cex_store_le32s, cex_store_le32s2);
1✔
647
         constexpr std::array<uint8_t, 16> cex_store_le64s = Botan::store_le(in64, in64);
1✔
648
         constexpr std::array<uint8_t, 16> cex_store_le64s2 = Botan::store_le(std::array{in64, in64});
1✔
649
         result.test_is_eq(
1✔
650
            cex_store_le64s,
651
            {0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB});
652
         result.test_is_eq(cex_store_le64s, cex_store_le64s2);
1✔
653

654
         constexpr std::array<uint8_t, 16> cex_store_be16s =
1✔
655
            Botan::store_be(in16, in16, in16, in16, in16, in16, in16, in16);
656
         constexpr std::array<uint8_t, 16> cex_store_be16s2 =
1✔
657
            Botan::store_be(std::array{in16, in16, in16, in16, in16, in16, in16, in16});
658
         result.test_is_eq(
1✔
659
            cex_store_be16s,
660
            {0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34});
661
         result.test_is_eq(cex_store_be16s, cex_store_be16s2);
1✔
662
         constexpr std::array<uint8_t, 16> cex_store_be32s = Botan::store_be(in32, in32, in32, in32);
1✔
663
         constexpr std::array<uint8_t, 16> cex_store_be32s2 = Botan::store_be(std::array{in32, in32, in32, in32});
1✔
664
         result.test_is_eq(
1✔
665
            cex_store_be32s,
666
            {0xA0, 0xB0, 0xC0, 0xD0, 0xA0, 0xB0, 0xC0, 0xD0, 0xA0, 0xB0, 0xC0, 0xD0, 0xA0, 0xB0, 0xC0, 0xD0});
667
         result.test_is_eq(cex_store_be32s, cex_store_be32s2);
1✔
668
         constexpr std::array<uint8_t, 16> cex_store_be64s = Botan::store_be(in64, in64);
1✔
669
         constexpr std::array<uint8_t, 16> cex_store_be64s2 = Botan::store_be(std::array{in64, in64});
1✔
670
         result.test_is_eq(
1✔
671
            cex_store_be64s,
672
            {0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89});
673
         result.test_is_eq(cex_store_be64s, cex_store_be64s2);
1✔
674

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

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

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

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

705
         return result;
1✔
706
      }
×
707

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

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

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

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

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

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

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

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

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

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

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

790
         return results;
1✔
791
      }
×
792

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

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

808
         return result;
1✔
809
      }
×
810

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

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

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

834
         return result;
1✔
835
      }
×
836

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

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

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

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

862
         return result;
1✔
863
      }
×
864

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

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

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

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

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

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

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

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

904
         return result;
1✔
905
      }
×
906

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

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

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

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

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

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

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

938
#if defined(BOTAN_HAS_POLY_DBL)
939

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

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

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

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

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

959
#endif
960

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1051
         return result;
22✔
1052
      }
11✔
1053

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

1063
BOTAN_REGISTER_TEST("utils", "util_dates", Date_Format_Tests);
1064

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

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

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

1075
         std::string converted;
8✔
1076

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

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

1090
         return result;
8✔
1091
      }
24✔
1092
};
1093

1094
BOTAN_REGISTER_TEST("utils", "charset", Charset_Tests);
1095

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

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

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

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

1110
         return result;
44✔
1111
      }
44✔
1112
};
1113

1114
BOTAN_REGISTER_TEST("utils", "hostname", Hostname_Tests);
1115

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

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

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

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

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

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

1147
BOTAN_REGISTER_TEST("utils", "dns_check", DNS_Check_Tests);
1148

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

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

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

1159
         auto ipv4 = Botan::string_to_ipv4(input);
47✔
1160

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

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

1168
         return result;
47✔
1169
      }
47✔
1170
};
1171

1172
BOTAN_REGISTER_TEST("utils", "ipv4_parse", IPv4_Parsing_Tests);
1173

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

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

1181
         const bool is_valid = (status == "Valid");
16✔
1182

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

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

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

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

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

1216
         return elems;
6✔
1217
      }
6✔
1218

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

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

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

1237
BOTAN_REGISTER_TEST("utils", "util_read_kv", ReadKV_Tests);
1238

1239
#if defined(BOTAN_HAS_CPUID)
1240

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

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

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

1255
               const std::string feat_str = feat.to_string();
32✔
1256

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

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

1270
   #if defined(BOTAN_TARGET_ARCH_IS_X86_FAMILY)
1271

1272
         const auto bit = Botan::CPUID::Feature::SSE2;
1✔
1273

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

1277
            Botan::CPUID::clear_cpuid_bit(bit);
1✔
1278

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

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

1290
         return {result};
3✔
1291
      }
2✔
1292
};
1293

1294
BOTAN_REGISTER_SERIALIZED_TEST("utils", "cpuid", CPUID_Tests);
1295

1296
#endif
1297

1298
#if defined(BOTAN_HAS_UUID)
1299

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

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

1310
         result.test_throws("Cannot load wrong number of bytes", []() { Botan::UUID u(std::vector<uint8_t>(15)); });
3✔
1311

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

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

1317
         result.test_eq("Random UUID not empty", random_uuid1.is_valid(), true);
1✔
1318
         result.test_eq("Random UUID not empty", random_uuid2.is_valid(), true);
1✔
1319

1320
         result.confirm("Random UUIDs are distinct", random_uuid1 != random_uuid2);
2✔
1321
         result.confirm("Random UUIDs not equal to empty", random_uuid1 != empty_uuid);
2✔
1322

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

1326
         const std::string uuid_r1_str = random_uuid1.to_string();
1✔
1327
         result.confirm("UUID from string matches", Botan::UUID(uuid_r1_str) == random_uuid1);
2✔
1328

1329
         class AllSame_RNG : public Botan::RandomNumberGenerator {
×
1330
            public:
1331
               explicit AllSame_RNG(uint8_t b) : m_val(b) {}
2✔
1332

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

1337
               std::string name() const override { return "zeros"; }
×
1338

1339
               bool accepts_input() const override { return false; }
×
1340

1341
               void clear() override {}
×
1342

1343
               bool is_seeded() const override { return true; }
×
1344

1345
            private:
1346
               uint8_t m_val;
1347
         };
1348

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

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

1357
         return {result};
3✔
1358
      }
6✔
1359
};
1360

1361
BOTAN_REGISTER_TEST("utils", "uuid", UUID_Tests);
1362

1363
#endif
1364

1365
class Formatter_Tests : public Test {
1✔
1366
   public:
1367
      std::vector<Test::Result> run() override {
1✔
1368
         Test::Result result("Format utility");
1✔
1369

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

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

1383
         return {result};
3✔
1384
      }
2✔
1385
};
1386

1387
BOTAN_REGISTER_TEST("utils", "fmt", Formatter_Tests);
1388

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

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

1411
                     result.confirm("cleanup not yet ran", !ran);
2✔
1412
                     fn();
1✔
1413
                     result.confirm("fn called", fn_called);
2✔
1414
                     result.confirm("cleanup ran", ran);
2✔
1415
                  }),
1✔
1416

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

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

1435
                     result.confirm("fn called", fn_called);
2✔
1436
                     result.confirm("cleanup ran", ran);
2✔
1437
                     result.confirm("exception caught", exception_caught);
2✔
1438
                  }),
1✔
1439

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

1450
         };
5✔
1451
      }
1✔
1452
};
1453

1454
BOTAN_REGISTER_TEST("utils", "scoped_cleanup", ScopedCleanup_Tests);
1455

1456
}  // namespace
1457

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