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

randombit / botan / 11846426392

14 Nov 2024 10:14PM UTC coverage: 91.075% (+0.003%) from 91.072%
11846426392

push

github

web-flow
Merge pull request #4434 from jiep/patch-1

Fix typos

90566 of 99441 relevant lines covered (91.08%)

9426126.71 hits per line

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

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

9
#include "tests.h"
10
#include <botan/version.h>
11
#include <botan/internal/bit_ops.h>
12
#include <botan/internal/calendar.h>
13
#include <botan/internal/charset.h>
14
#include <botan/internal/cpuid.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 <ctime>
22
#include <functional>
23

24
#if defined(BOTAN_HAS_POLY_DBL)
25
   #include <botan/internal/poly_dbl.h>
26
#endif
27

28
#if defined(BOTAN_HAS_UUID)
29
   #include <botan/uuid.h>
30
#endif
31

32
namespace Botan_Tests {
33

34
namespace {
35

36
class Utility_Function_Tests final : public Test {
×
37
   public:
38
      std::vector<Test::Result> run() override {
1✔
39
         std::vector<Test::Result> results;
1✔
40

41
         results.push_back(test_checked_add());
2✔
42
         results.push_back(test_checked_mul());
2✔
43
         results.push_back(test_checked_cast());
2✔
44
         results.push_back(test_round_up());
2✔
45
         results.push_back(test_loadstore());
2✔
46
         results.push_back(test_loadstore_ambiguity());
2✔
47
         results.push_back(test_loadstore_fallback());
2✔
48
         results.push_back(test_loadstore_constexpr());
2✔
49
         return Botan::concat(results, test_copy_out_be_le());
3✔
50
      }
1✔
51

52
   private:
53
      Test::Result test_checked_add() {
1✔
54
         Test::Result result("checked_add");
1✔
55

56
         const size_t large = static_cast<size_t>(-5);
1✔
57
         const size_t zero = 0;
1✔
58

59
         for(int si = -15; si != 15; ++si) {
31✔
60
            const size_t i = static_cast<size_t>(si);
30✔
61
            auto sum1 = Botan::checked_add<size_t>(i, zero, zero, zero, large);
30✔
62
            auto sum2 = Botan::checked_add<size_t>(large, zero, zero, zero, i);
30✔
63

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

66
            if(i < 5) {
30✔
67
               result.test_eq("checked_add worked", sum1.value(), i + large);
10✔
68
            } else {
69
               result.confirm("checked_add did not return a result", !sum1.has_value());
50✔
70
            }
71
         }
72

73
         auto& rng = Test::rng();
1✔
74

75
         for(size_t i = 0; i != 100; ++i) {
101✔
76
            const uint16_t x = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100✔
77
            const uint16_t y = Botan::make_uint16(rng.next_byte(), rng.next_byte());
100✔
78

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

81
            if(auto z = Botan::checked_add(x, y)) {
200✔
82
               result.test_int_eq("checked_add adds", z.value(), ref);
96✔
83
            } else {
84
               result.confirm("checked_add checks", (ref >> 16) > 0);
104✔
85
            }
86
         }
87

88
         return result;
1✔
89
      }
×
90

91
      Test::Result test_checked_mul() {
1✔
92
         Test::Result result("checked_mul");
1✔
93

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

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

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

102
            if(auto z = Botan::checked_mul(x, y)) {
200✔
103
               result.test_int_eq("checked_mul multiplies", z.value(), ref);
×
104
            } else {
105
               result.confirm("checked_mul checks", (ref >> 16) > 0);
200✔
106
            }
107
         }
108

109
         return result;
1✔
110
      }
×
111

112
      Test::Result test_checked_cast() {
1✔
113
         Test::Result result("checked_cast");
1✔
114

115
         const uint32_t large = static_cast<uint32_t>(-1);
1✔
116
         const uint32_t is_16_bits = 0x8123;
1✔
117
         const uint32_t is_8_bits = 0x89;
1✔
118

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

122
         result.test_int_eq("checked_cast converts", Botan::checked_cast_to<uint32_t>(large), large);
2✔
123
         result.test_int_eq("checked_cast converts", Botan::checked_cast_to<uint16_t>(is_16_bits), 0x8123);
2✔
124
         result.test_int_eq("checked_cast converts", Botan::checked_cast_to<uint8_t>(is_8_bits), 0x89);
2✔
125

126
         return result;
1✔
127
      }
×
128

129
      Test::Result test_round_up() {
1✔
130
         Test::Result result("Util round_up");
1✔
131

132
         // clang-format off
133
         const std::vector<size_t> inputs = {
1✔
134
            0, 1, 2, 3, 4, 9, 10, 32, 99, 100, 101, 255, 256, 1000, 10000,
135
            65535, 65536, 65537,
136
         };
1✔
137

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

144
         for(size_t i : inputs) {
19✔
145
            for(size_t m : alignments) {
450✔
146
               try {
432✔
147
                  const size_t z = Botan::round_up(i, m);
432✔
148

149
                  result.confirm("z % m == 0", z % m == 0);
864✔
150
                  result.confirm("z >= i", z >= i);
864✔
151
                  result.confirm("z <= i + m", z <= i + m);
864✔
152
               } catch(Botan::Exception& e) {
×
153
                  result.test_failure(Botan::fmt("round_up({},{})", i, m), e.what());
×
154
               }
×
155
            }
156
         }
157

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

160
         return result;
1✔
161
      }
1✔
162

163
      using TestInt64 = Botan::Strong<uint64_t, struct TestInt64_>;
164
      using TestInt32 = Botan::Strong<uint32_t, struct TestInt64_>;
165
      using TestVectorSink = Botan::Strong<std::vector<uint8_t>, struct TestVectorSink_>;
166

167
      enum class TestEnum64 : uint64_t {
168
         _1 = 0x1234567890ABCDEF,
169
         _2 = 0xEFCDAB9078563412,
170
      };
171

172
      enum class TestEnum32 : uint32_t {
173
         _1 = 0x12345678,
174
         _2 = 0x78563412,
175
      };
176

177
      static Test::Result test_loadstore() {
1✔
178
         Test::Result result("Util load/store");
1✔
179

180
         const std::vector<uint8_t> membuf = Botan::hex_decode("00112233445566778899AABBCCDDEEFF");
1✔
181
         const uint8_t* mem = membuf.data();
1✔
182

183
         const uint16_t in16 = 0x1234;
1✔
184
         const uint32_t in32 = 0xA0B0C0D0;
1✔
185
         const uint64_t in64 = 0xABCDEF0123456789;
1✔
186

187
         result.test_is_eq<uint8_t>(Botan::get_byte<0>(in32), 0xA0);
1✔
188
         result.test_is_eq<uint8_t>(Botan::get_byte<1>(in32), 0xB0);
1✔
189
         result.test_is_eq<uint8_t>(Botan::get_byte<2>(in32), 0xC0);
1✔
190
         result.test_is_eq<uint8_t>(Botan::get_byte<3>(in32), 0xD0);
1✔
191

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

195
         result.test_is_eq<uint16_t>(Botan::load_be<uint16_t>(mem, 0), 0x0011);
1✔
196
         result.test_is_eq<uint16_t>(Botan::load_be<uint16_t>(mem, 1), 0x2233);
1✔
197
         result.test_is_eq<uint16_t>(Botan::load_be<uint16_t>(mem, 2), 0x4455);
1✔
198
         result.test_is_eq<uint16_t>(Botan::load_be<uint16_t>(mem, 3), 0x6677);
1✔
199

200
         result.test_is_eq<uint16_t>(Botan::load_le<uint16_t>(mem, 0), 0x1100);
1✔
201
         result.test_is_eq<uint16_t>(Botan::load_le<uint16_t>(mem, 1), 0x3322);
1✔
202
         result.test_is_eq<uint16_t>(Botan::load_le<uint16_t>(mem, 2), 0x5544);
1✔
203
         result.test_is_eq<uint16_t>(Botan::load_le<uint16_t>(mem, 3), 0x7766);
1✔
204

205
         result.test_is_eq<uint32_t>(Botan::load_be<uint32_t>(mem, 0), 0x00112233);
1✔
206
         result.test_is_eq<uint32_t>(Botan::load_be<uint32_t>(mem, 1), 0x44556677);
1✔
207
         result.test_is_eq<uint32_t>(Botan::load_be<uint32_t>(mem, 2), 0x8899AABB);
1✔
208
         result.test_is_eq<uint32_t>(Botan::load_be<uint32_t>(mem, 3), 0xCCDDEEFF);
1✔
209

210
         result.test_is_eq<uint32_t>(Botan::load_le<uint32_t>(mem, 0), 0x33221100);
1✔
211
         result.test_is_eq<uint32_t>(Botan::load_le<uint32_t>(mem, 1), 0x77665544);
1✔
212
         result.test_is_eq<uint32_t>(Botan::load_le<uint32_t>(mem, 2), 0xBBAA9988);
1✔
213
         result.test_is_eq<uint32_t>(Botan::load_le<uint32_t>(mem, 3), 0xFFEEDDCC);
1✔
214

215
         result.test_is_eq<uint64_t>(Botan::load_be<uint64_t>(mem, 0), 0x0011223344556677);
1✔
216
         result.test_is_eq<uint64_t>(Botan::load_be<uint64_t>(mem, 1), 0x8899AABBCCDDEEFF);
1✔
217

218
         result.test_is_eq<uint64_t>(Botan::load_le<uint64_t>(mem, 0), 0x7766554433221100);
1✔
219
         result.test_is_eq<uint64_t>(Botan::load_le<uint64_t>(mem, 1), 0xFFEEDDCCBBAA9988);
1✔
220

221
         // Check misaligned loads:
222
         result.test_is_eq<uint16_t>(Botan::load_be<uint16_t>(mem + 1, 0), 0x1122);
1✔
223
         result.test_is_eq<uint16_t>(Botan::load_le<uint16_t>(mem + 3, 0), 0x4433);
1✔
224

225
         result.test_is_eq<uint32_t>(Botan::load_be<uint32_t>(mem + 1, 1), 0x55667788);
1✔
226
         result.test_is_eq<uint32_t>(Botan::load_le<uint32_t>(mem + 3, 1), 0xAA998877);
1✔
227

228
         result.test_is_eq<uint64_t>(Botan::load_be<uint64_t>(mem + 1, 0), 0x1122334455667788);
1✔
229
         result.test_is_eq<uint64_t>(Botan::load_le<uint64_t>(mem + 7, 0), 0xEEDDCCBBAA998877);
1✔
230
         result.test_is_eq<uint64_t>(Botan::load_le<uint64_t>(mem + 5, 0), 0xCCBBAA9988776655);
1✔
231

232
         uint8_t outbuf[16] = {0};
1✔
233

234
         for(size_t offset = 0; offset != 7; ++offset) {
8✔
235
            uint8_t* out = outbuf + offset;
7✔
236

237
            Botan::store_be(in16, out);
7✔
238
            result.test_is_eq<uint8_t>(out[0], 0x12);
7✔
239
            result.test_is_eq<uint8_t>(out[1], 0x34);
7✔
240

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

245
            Botan::store_be(in32, out);
7✔
246
            result.test_is_eq<uint8_t>(out[0], 0xA0);
7✔
247
            result.test_is_eq<uint8_t>(out[1], 0xB0);
7✔
248
            result.test_is_eq<uint8_t>(out[2], 0xC0);
7✔
249
            result.test_is_eq<uint8_t>(out[3], 0xD0);
7✔
250

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

257
            Botan::store_be(in64, out);
7✔
258
            result.test_is_eq<uint8_t>(out[0], 0xAB);
7✔
259
            result.test_is_eq<uint8_t>(out[1], 0xCD);
7✔
260
            result.test_is_eq<uint8_t>(out[2], 0xEF);
7✔
261
            result.test_is_eq<uint8_t>(out[3], 0x01);
7✔
262
            result.test_is_eq<uint8_t>(out[4], 0x23);
7✔
263
            result.test_is_eq<uint8_t>(out[5], 0x45);
7✔
264
            result.test_is_eq<uint8_t>(out[6], 0x67);
7✔
265
            result.test_is_eq<uint8_t>(out[7], 0x89);
7✔
266

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

278
         std::array<uint8_t, 8> outarr;
1✔
279
         uint16_t i0, i1, i2, i3;
1✔
280
         Botan::store_be(in64, outarr);
1✔
281

282
         Botan::load_be(outarr, i0, i1, i2, i3);
1✔
283
         result.test_is_eq<uint16_t>(i0, 0xABCD);
1✔
284
         result.test_is_eq<uint16_t>(i1, 0xEF01);
1✔
285
         result.test_is_eq<uint16_t>(i2, 0x2345);
1✔
286
         result.test_is_eq<uint16_t>(i3, 0x6789);
1✔
287

288
         Botan::load_le(std::span{outarr}.first<6>(), i0, i1, i2);
1✔
289
         result.test_is_eq<uint16_t>(i0, 0xCDAB);
1✔
290
         result.test_is_eq<uint16_t>(i1, 0x01EF);
1✔
291
         result.test_is_eq<uint16_t>(i2, 0x4523);
1✔
292
         result.test_is_eq<uint16_t>(i3, 0x6789);  // remains unchanged
1✔
293

294
         Botan::store_le(in64, outarr);
1✔
295

296
         Botan::load_le(outarr, i0, i1, i2, i3);
1✔
297
         result.test_is_eq<uint16_t>(i0, 0x6789);
1✔
298
         result.test_is_eq<uint16_t>(i1, 0x2345);
1✔
299
         result.test_is_eq<uint16_t>(i2, 0xEF01);
1✔
300
         result.test_is_eq<uint16_t>(i3, 0xABCD);
1✔
301

302
         Botan::load_be(std::span{outarr}.first<6>(), i0, i1, i2);
1✔
303
         result.test_is_eq<uint16_t>(i0, 0x8967);
1✔
304
         result.test_is_eq<uint16_t>(i1, 0x4523);
1✔
305
         result.test_is_eq<uint16_t>(i2, 0x01EF);
1✔
306
         result.test_is_eq<uint16_t>(i3, 0xABCD);  // remains unchanged
1✔
307

308
         i0 = 0xAA11;
1✔
309
         i1 = 0xBB22;
1✔
310
         i2 = 0xCC33;
1✔
311
         i3 = 0xDD44;
1✔
312
         Botan::store_be(outarr, i0, i1, i2, i3);
1✔
313
         result.test_is_eq(outarr, {0xAA, 0x11, 0xBB, 0x22, 0xCC, 0x33, 0xDD, 0x44});
1✔
314
         std::vector<uint8_t> outvec(8);
1✔
315
         Botan::store_be(outvec, i0, i1, i2, i3);
1✔
316
         result.test_is_eq(outvec, Botan::hex_decode("AA11BB22CC33DD44"));
1✔
317

318
         Botan::store_le(outarr, i0, i1, i2, i3);
1✔
319
         result.test_is_eq(outarr, {0x11, 0xAA, 0x22, 0xBB, 0x33, 0xCC, 0x44, 0xDD});
1✔
320
         Botan::store_le(outvec, i0, i1, i2, i3);
1✔
321
         result.test_is_eq(outvec, Botan::hex_decode("11AA22BB33CC44DD"));
1✔
322

323
#if !defined(BOTAN_TERMINATE_ON_ASSERTS)
324
         std::vector<uint8_t> sink56bits(7);
325
         std::vector<uint8_t> sink72bits(9);
326
         result.test_throws("store_le with a buffer that is too small",
327
                            [&] { Botan::store_le(sink56bits, i0, i1, i2, i3); });
328
         result.test_throws("store_le with a buffer that is too big",
329
                            [&] { Botan::store_le(sink72bits, i0, i1, i2, i3); });
330
         result.test_throws("store_be with a buffer that is too small",
331
                            [&] { Botan::store_be(sink56bits, i0, i1, i2, i3); });
332
         result.test_throws("store_be with a buffer that is too big",
333
                            [&] { Botan::store_be(sink72bits, i0, i1, i2, i3); });
334
#endif
335

336
         // can store multiple values straight into a collection
337
         auto out64_array_be = Botan::store_be(i0, i1, i2, i3);
1✔
338
         auto out64_vec_be = Botan::store_be<std::vector<uint8_t>>(i0, i1, i2, i3);
1✔
339
         auto out64_strong_be = Botan::store_be<TestVectorSink>(i0, i1, i2, i3);
1✔
340
         result.test_is_eq(out64_array_be, {0xAA, 0x11, 0xBB, 0x22, 0xCC, 0x33, 0xDD, 0x44});
1✔
341
         result.test_is_eq(out64_vec_be, Botan::hex_decode("AA11BB22CC33DD44"));
1✔
342
         result.test_is_eq(out64_strong_be, TestVectorSink(Botan::hex_decode("AA11BB22CC33DD44")));
2✔
343
         auto out64_array_le = Botan::store_le(i0, i1, i2, i3);
1✔
344
         auto out64_vec_le = Botan::store_le<std::vector<uint8_t>>(i0, i1, i2, i3);
1✔
345
         auto out64_strong_le = Botan::store_le<TestVectorSink>(i0, i1, i2, i3);
1✔
346
         result.test_is_eq(out64_array_le, {0x11, 0xAA, 0x22, 0xBB, 0x33, 0xCC, 0x44, 0xDD});
1✔
347
         result.test_is_eq(out64_vec_le, Botan::hex_decode("11AA22BB33CC44DD"));
1✔
348
         result.test_is_eq(out64_strong_le, TestVectorSink(Botan::hex_decode("11AA22BB33CC44DD")));
2✔
349

350
         result.test_is_eq(in16, Botan::load_be(Botan::store_be(in16)));
1✔
351
         result.test_is_eq(in32, Botan::load_be(Botan::store_be(in32)));
1✔
352
         result.test_is_eq(in64, Botan::load_be(Botan::store_be(in64)));
1✔
353

354
         result.test_is_eq(in16, Botan::load_le(Botan::store_le(in16)));
1✔
355
         result.test_is_eq(in32, Botan::load_le(Botan::store_le(in32)));
1✔
356
         result.test_is_eq(in64, Botan::load_le(Botan::store_le(in64)));
1✔
357

358
         // Test that the runtime detects incompatible range sizes
359
#if !defined(BOTAN_TERMINATE_ON_ASSERTS)
360
         std::vector<uint16_t> too_big16(4);
361
         std::vector<uint16_t> too_small16(1);
362
         result.test_throws("load_le with incompatible buffers",
363
                            [&] { Botan::load_le(too_big16, Botan::hex_decode("BAADB00B")); });
364
         result.test_throws("load_le with incompatible buffers",
365
                            [&] { Botan::load_le(too_small16, Botan::hex_decode("BAADB00B")); });
366
         result.test_throws("load_be with incompatible buffers",
367
                            [&] { Botan::load_be(too_big16, Botan::hex_decode("BAADB00B")); });
368
         result.test_throws("load_be with incompatible buffers",
369
                            [&] { Botan::load_be(too_small16, Botan::hex_decode("BAADB00B")); });
370

371
         std::vector<uint8_t> too_big8(4);
372
         std::vector<uint8_t> too_small8(1);
373
         result.test_throws("store_le with incompatible buffers",
374
                            [&] { Botan::store_le(too_big8, std::array<uint16_t, 1>{}); });
375
         result.test_throws("store_le with incompatible buffers",
376
                            [&] { Botan::store_le(too_small8, std::array<uint16_t, 1>{}); });
377
         result.test_throws("store_be with incompatible buffers",
378
                            [&] { Botan::store_be(too_big8, std::array<uint16_t, 1>{}); });
379
         result.test_throws("store_be with incompatible buffers",
380
                            [&] { Botan::store_be(too_small8, std::array<uint16_t, 1>{}); });
381
#endif
382

383
         // Test store of entire ranges
384
         std::array<uint16_t, 2> in16_array = {0x0A0B, 0x0C0D};
1✔
385
         result.test_is_eq(Botan::store_be<std::vector<uint8_t>>(in16_array), Botan::hex_decode("0A0B0C0D"));
3✔
386
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(in16_array), Botan::hex_decode("0B0A0D0C"));
3✔
387

388
         std::vector<uint16_t> in16_vector = {0x0A0B, 0x0C0D};
1✔
389
         result.test_is_eq(Botan::store_be<std::vector<uint8_t>>(in16_vector), Botan::hex_decode("0A0B0C0D"));
3✔
390
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(in16_vector), Botan::hex_decode("0B0A0D0C"));
3✔
391

392
         std::array<uint8_t, 4> out_array;
1✔
393
         Botan::store_be(out_array, in16_array);
1✔
394
         result.test_is_eq(out_array, std::array<uint8_t, 4>{0x0A, 0x0B, 0x0C, 0x0D});
1✔
395
         Botan::store_le(out_array, in16_array);
1✔
396
         result.test_is_eq(out_array, std::array<uint8_t, 4>{0x0B, 0x0A, 0x0D, 0x0C});
1✔
397

398
         const auto be_inferred = Botan::store_be(in16_array);
1✔
399
         result.test_is_eq(be_inferred, std::array<uint8_t, 4>{0x0A, 0x0B, 0x0C, 0x0D});
1✔
400
         const auto le_inferred = Botan::store_le(in16_array);
1✔
401
         result.test_is_eq(le_inferred, std::array<uint8_t, 4>{0x0B, 0x0A, 0x0D, 0x0C});
1✔
402

403
         // Test load of entire ranges
404
         const auto in_buffer = Botan::hex_decode("AABBCCDD");
1✔
405
         auto out16_array_be = Botan::load_be<std::array<uint16_t, 2>>(in_buffer);
1✔
406
         result.test_is_eq<uint16_t>(out16_array_be[0], 0xAABB);
1✔
407
         result.test_is_eq<uint16_t>(out16_array_be[1], 0xCCDD);
1✔
408
         auto out16_vec_be = Botan::load_be<std::vector<uint16_t>>(in_buffer);
1✔
409
         result.test_eq_sz("be-vector has expected size", out16_vec_be.size(), 2);
1✔
410
         result.test_is_eq<uint16_t>(out16_vec_be[0], 0xAABB);
1✔
411
         result.test_is_eq<uint16_t>(out16_vec_be[1], 0xCCDD);
1✔
412

413
         auto out16_array_le = Botan::load_le<std::array<uint16_t, 2>>(in_buffer);
1✔
414
         result.test_is_eq<uint16_t>(out16_array_le[0], 0xBBAA);
1✔
415
         result.test_is_eq<uint16_t>(out16_array_le[1], 0xDDCC);
1✔
416
         auto out16_vec_le = Botan::load_le<Botan::secure_vector<uint16_t>>(in_buffer);
1✔
417
         result.test_eq_sz("le-vector has expected size", out16_vec_be.size(), 2);
1✔
418
         result.test_is_eq<uint16_t>(out16_vec_le[0], 0xBBAA);
1✔
419
         result.test_is_eq<uint16_t>(out16_vec_le[1], 0xDDCC);
1✔
420

421
         // Test loading/storing of strong type integers
422
         const TestInt64 in64_strong{0xABCDEF0123456789};
1✔
423
         const TestInt32 in32_strong{0xABCDEF01};
1✔
424

425
         result.test_is_eq(Botan::store_be<std::vector<uint8_t>>(in64_strong), Botan::hex_decode("ABCDEF0123456789"));
3✔
426
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(in64_strong), Botan::hex_decode("8967452301EFCDAB"));
3✔
427
         result.test_is_eq(Botan::store_be<std::vector<uint8_t>>(in32_strong), Botan::hex_decode("ABCDEF01"));
3✔
428
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(in32_strong), Botan::hex_decode("01EFCDAB"));
3✔
429

430
         result.test_is_eq(Botan::load_be<TestInt64>(Botan::hex_decode("ABCDEF0123456789")), in64_strong);
2✔
431
         result.test_is_eq(Botan::load_le<TestInt64>(Botan::hex_decode("8967452301EFCDAB")), in64_strong);
2✔
432
         result.test_is_eq(Botan::load_be<TestInt32>(Botan::hex_decode("ABCDEF01")), in32_strong);
2✔
433
         result.test_is_eq(Botan::load_le<TestInt32>(Botan::hex_decode("01EFCDAB")), in32_strong);
2✔
434

435
         std::vector<TestInt64> some_in64_strongs{TestInt64{0xABCDEF0123456789}, TestInt64{0x0123456789ABCDEF}};
1✔
436
         result.test_is_eq(Botan::store_be<std::vector<uint8_t>>(some_in64_strongs),
3✔
437
                           Botan::hex_decode("ABCDEF01234567890123456789ABCDEF"));
1✔
438
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(some_in64_strongs),
3✔
439
                           Botan::hex_decode("8967452301EFCDABEFCDAB8967452301"));
1✔
440

441
         const auto in64_strongs_le =
1✔
442
            Botan::load_le<std::array<TestInt64, 2>>(Botan::hex_decode("8967452301EFCDABEFCDAB8967452301"));
2✔
443
         result.test_is_eq(in64_strongs_le[0], TestInt64{0xABCDEF0123456789});
1✔
444
         result.test_is_eq(in64_strongs_le[1], TestInt64{0x0123456789ABCDEF});
1✔
445

446
         const auto in64_strongs_be =
1✔
447
            Botan::load_be<std::vector<TestInt64>>(Botan::hex_decode("ABCDEF01234567890123456789ABCDEF"));
2✔
448
         result.test_is_eq(in64_strongs_be[0], TestInt64{0xABCDEF0123456789});
1✔
449
         result.test_is_eq(in64_strongs_be[1], TestInt64{0x0123456789ABCDEF});
1✔
450

451
         // Test loading/storing of enum types with different endianness
452
         const auto in64_enum_le = Botan::load_le<TestEnum64>(Botan::hex_decode("1234567890ABCDEF"));
2✔
453
         result.test_is_eq(in64_enum_le, TestEnum64::_2);
1✔
454
         const auto in64_enum_be = Botan::load_be<TestEnum64>(Botan::hex_decode("1234567890ABCDEF"));
2✔
455
         result.test_is_eq(in64_enum_be, TestEnum64::_1);
1✔
456
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(TestEnum64::_1),
2✔
457
                           Botan::hex_decode("EFCDAB9078563412"));
1✔
458
         result.test_is_eq<std::array<uint8_t, 8>>(Botan::store_be(TestEnum64::_2),
1✔
459
                                                   {0xEF, 0xCD, 0xAB, 0x90, 0x78, 0x56, 0x34, 0x12});
460

461
         const auto in32_enum_le = Botan::load_le<TestEnum32>(Botan::hex_decode("78563412"));
2✔
462
         result.test_is_eq(in32_enum_le, TestEnum32::_1);
1✔
463
         const auto in32_enum_be = Botan::load_be<TestEnum32>(Botan::hex_decode("78563412"));
2✔
464
         result.test_is_eq(in32_enum_be, TestEnum32::_2);
1✔
465
         result.test_is_eq(Botan::store_le<std::vector<uint8_t>>(TestEnum32::_1), Botan::hex_decode("78563412"));
2✔
466
         result.test_is_eq<std::array<uint8_t, 4>>(Botan::store_be(TestEnum32::_2), {0x78, 0x56, 0x34, 0x12});
1✔
467

468
         return result;
2✔
469
      }
11✔
470

471
      template <std::unsigned_integral T>
472
      static T fb_load_be(std::array<const uint8_t, sizeof(T)> in) {
3✔
473
         return Botan::detail::fallback_load_any<Botan::detail::Endianness::Big, T>(in);
3✔
474
      }
475

476
      template <std::unsigned_integral T>
477
      static T fb_load_le(std::array<const uint8_t, sizeof(T)> in) {
3✔
478
         return Botan::detail::fallback_load_any<Botan::detail::Endianness::Little, T>(in);
3✔
479
      }
480

481
      template <std::unsigned_integral T>
482
      static decltype(auto) fb_store_be(const T in) {
3✔
483
         std::array<uint8_t, sizeof(T)> out;
484
         Botan::detail::fallback_store_any<Botan::detail::Endianness::Big, T>(in, out);
1✔
485
         return out;
2✔
486
      }
487

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

495
      template <size_t N>
496
      using a = std::array<uint8_t, N>;
497

498
      static Test::Result test_loadstore_ambiguity() {
1✔
499
         // This is a regression test for a (probable) compiler bug in Xcode 15
500
         // where it would fail to compile the load/store functions for size_t
501
         //
502
         // It seems that this platform defines uint64_t as "unsigned long long"
503
         // and size_t as "unsigned long". Both are 64-bits but the compiler
504
         // was unable to disambiguate the two in reverse_bytes in bswap.h
505

506
         const uint32_t in32 = 0x01234567;
1✔
507
         const uint64_t in64 = 0x0123456789ABCDEF;
1✔
508
         const size_t inszt = 0x87654321;
1✔
509

510
         Test::Result result("Util load/store ambiguity");
1✔
511
         const auto out_be_32 = Botan::store_be(in32);
1✔
512
         const auto out_le_32 = Botan::store_le(in32);
1✔
513
         const auto out_be_64 = Botan::store_be(in64);
1✔
514
         const auto out_le_64 = Botan::store_le(in64);
1✔
515
         const auto out_be_szt = Botan::store_be(inszt);
1✔
516
         const auto out_le_szt = Botan::store_le(inszt);
1✔
517

518
         result.test_is_eq<uint32_t>("be 32", Botan::load_be<uint32_t>(out_be_32), in32);
1✔
519
         result.test_is_eq<uint32_t>("le 32", Botan::load_le<uint32_t>(out_le_32), in32);
1✔
520
         result.test_is_eq<uint64_t>("be 64", Botan::load_be<uint64_t>(out_be_64), in64);
1✔
521
         result.test_is_eq<uint64_t>("le 64", Botan::load_le<uint64_t>(out_le_64), in64);
1✔
522
         result.test_is_eq<size_t>("be szt", Botan::load_be<size_t>(out_be_szt), inszt);
1✔
523
         result.test_is_eq<size_t>("le szt", Botan::load_le<size_t>(out_le_szt), inszt);
1✔
524

525
         return result;
1✔
526
      }
×
527

528
      static Test::Result test_loadstore_fallback() {
1✔
529
         // The fallback implementation is only used if we don't know the
530
         // endianness of the target at compile time. This makes sure that the
531
         // fallback implementation is correct. On all typical platforms it
532
         // won't be called in production.
533
         Test::Result result("Util load/store fallback");
1✔
534

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

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

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

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

551
         return result;
1✔
552
      }
×
553

554
      static Test::Result test_loadstore_constexpr() {
1✔
555
         Test::Result result("Util load/store constexpr");
1✔
556

557
         constexpr uint16_t in16 = 0x1234;
1✔
558
         constexpr uint32_t in32 = 0xA0B0C0D0;
1✔
559
         constexpr uint64_t in64 = 0xABCDEF0123456789;
1✔
560

561
         // clang-format off
562
         constexpr std::array<uint8_t, 16> cex_mem = {
1✔
563
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
564
         };
565
         // clang-format on
566

567
         // get_byte<> w/ 16bit
568
         constexpr auto cex_byte_16_0 = Botan::get_byte<0>(in16);
1✔
569
         result.test_is_eq<uint8_t>(cex_byte_16_0, 0x12);
1✔
570
         constexpr auto cex_byte_16_1 = Botan::get_byte<1>(in16);
1✔
571
         result.test_is_eq<uint8_t>(cex_byte_16_1, 0x34);
1✔
572

573
         // get_byte<> w/ 32bit
574
         constexpr auto cex_byte_32_0 = Botan::get_byte<0>(in32);
1✔
575
         result.test_is_eq<uint8_t>(cex_byte_32_0, 0xA0);
1✔
576
         constexpr auto cex_byte_32_1 = Botan::get_byte<1>(in32);
1✔
577
         result.test_is_eq<uint8_t>(cex_byte_32_1, 0xB0);
1✔
578
         constexpr auto cex_byte_32_2 = Botan::get_byte<2>(in32);
1✔
579
         result.test_is_eq<uint8_t>(cex_byte_32_2, 0xC0);
1✔
580
         constexpr auto cex_byte_32_3 = Botan::get_byte<3>(in32);
1✔
581
         result.test_is_eq<uint8_t>(cex_byte_32_3, 0xD0);
1✔
582

583
         // get_byte<> w/ 64bit
584
         constexpr auto cex_byte_64_0 = Botan::get_byte<0>(in64);
1✔
585
         result.test_is_eq<uint8_t>(cex_byte_64_0, 0xAB);
1✔
586
         constexpr auto cex_byte_64_1 = Botan::get_byte<1>(in64);
1✔
587
         result.test_is_eq<uint8_t>(cex_byte_64_1, 0xCD);
1✔
588
         constexpr auto cex_byte_64_2 = Botan::get_byte<2>(in64);
1✔
589
         result.test_is_eq<uint8_t>(cex_byte_64_2, 0xEF);
1✔
590
         constexpr auto cex_byte_64_3 = Botan::get_byte<3>(in64);
1✔
591
         result.test_is_eq<uint8_t>(cex_byte_64_3, 0x01);
1✔
592
         constexpr auto cex_byte_64_4 = Botan::get_byte<4>(in64);
1✔
593
         result.test_is_eq<uint8_t>(cex_byte_64_4, 0x23);
1✔
594
         constexpr auto cex_byte_64_5 = Botan::get_byte<5>(in64);
1✔
595
         result.test_is_eq<uint8_t>(cex_byte_64_5, 0x45);
1✔
596
         constexpr auto cex_byte_64_6 = Botan::get_byte<6>(in64);
1✔
597
         result.test_is_eq<uint8_t>(cex_byte_64_6, 0x67);
1✔
598
         constexpr auto cex_byte_64_7 = Botan::get_byte<7>(in64);
1✔
599
         result.test_is_eq<uint8_t>(cex_byte_64_7, 0x89);
1✔
600

601
         // make_uintXX()
602
         constexpr auto cex_uint16_t = Botan::make_uint16(0x12, 0x34);
1✔
603
         result.test_is_eq<uint16_t>(cex_uint16_t, in16);
1✔
604
         constexpr auto cex_uint32_t = Botan::make_uint32(0xA0, 0xB0, 0xC0, 0xD0);
1✔
605
         result.test_is_eq<uint32_t>(cex_uint32_t, in32);
1✔
606
         constexpr auto cex_uint64_t = Botan::make_uint64(0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89);
1✔
607
         result.test_is_eq<uint64_t>(cex_uint64_t, in64);
1✔
608

609
         // store_le/be with a single integer
610
         constexpr std::array<uint8_t, 2> cex_store_le16 = Botan::store_le(in16);
1✔
611
         result.test_is_eq(cex_store_le16, std::array<uint8_t, 2>{0x34, 0x12});
1✔
612
         constexpr std::array<uint8_t, 4> cex_store_le32 = Botan::store_le(in32);
1✔
613
         result.test_is_eq(cex_store_le32, std::array<uint8_t, 4>{0xD0, 0xC0, 0xB0, 0xA0});
1✔
614
         constexpr std::array<uint8_t, 8> cex_store_le64 = Botan::store_le(in64);
1✔
615
         result.test_is_eq(cex_store_le64, std::array<uint8_t, 8>{0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB});
1✔
616

617
         constexpr std::array<uint8_t, 2> cex_store_be16 = Botan::store_be(in16);
1✔
618
         result.test_is_eq(cex_store_be16, std::array<uint8_t, 2>{0x12, 0x34});
1✔
619
         constexpr std::array<uint8_t, 4> cex_store_be32 = Botan::store_be(in32);
1✔
620
         result.test_is_eq(cex_store_be32, std::array<uint8_t, 4>{0xA0, 0xB0, 0xC0, 0xD0});
1✔
621
         constexpr std::array<uint8_t, 8> cex_store_be64 = Botan::store_be(in64);
1✔
622
         result.test_is_eq(cex_store_be64, std::array<uint8_t, 8>{0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89});
1✔
623

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

646
         constexpr std::array<uint8_t, 16> cex_store_be16s =
1✔
647
            Botan::store_be(in16, in16, in16, in16, in16, in16, in16, in16);
648
         constexpr std::array<uint8_t, 16> cex_store_be16s2 =
1✔
649
            Botan::store_be(std::array{in16, in16, in16, in16, in16, in16, in16, in16});
650
         result.test_is_eq(
1✔
651
            cex_store_be16s,
652
            {0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34});
653
         result.test_is_eq(cex_store_be16s, cex_store_be16s2);
1✔
654
         constexpr std::array<uint8_t, 16> cex_store_be32s = Botan::store_be(in32, in32, in32, in32);
1✔
655
         constexpr std::array<uint8_t, 16> cex_store_be32s2 = Botan::store_be(std::array{in32, in32, in32, in32});
1✔
656
         result.test_is_eq(
1✔
657
            cex_store_be32s,
658
            {0xA0, 0xB0, 0xC0, 0xD0, 0xA0, 0xB0, 0xC0, 0xD0, 0xA0, 0xB0, 0xC0, 0xD0, 0xA0, 0xB0, 0xC0, 0xD0});
659
         result.test_is_eq(cex_store_be32s, cex_store_be32s2);
1✔
660
         constexpr std::array<uint8_t, 16> cex_store_be64s = Botan::store_be(in64, in64);
1✔
661
         constexpr std::array<uint8_t, 16> cex_store_be64s2 = Botan::store_be(std::array{in64, in64});
1✔
662
         result.test_is_eq(
1✔
663
            cex_store_be64s,
664
            {0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89});
665
         result.test_is_eq(cex_store_be64s, cex_store_be64s2);
1✔
666

667
         // load_le/be a single integer
668
         constexpr uint16_t cex_load_le16 = Botan::load_le<uint16_t>(cex_store_le16);
1✔
669
         result.test_is_eq(cex_load_le16, in16);
1✔
670
         constexpr uint32_t cex_load_le32 = Botan::load_le<uint32_t>(cex_store_le32);
1✔
671
         result.test_is_eq(cex_load_le32, in32);
1✔
672
         constexpr uint64_t cex_load_le64 = Botan::load_le<uint64_t>(cex_store_le64);
1✔
673
         result.test_is_eq(cex_load_le64, in64);
1✔
674

675
         constexpr uint16_t cex_load_be16 = Botan::load_be<uint16_t>(cex_store_be16);
1✔
676
         result.test_is_eq(cex_load_be16, in16);
1✔
677
         constexpr uint32_t cex_load_be32 = Botan::load_be<uint32_t>(cex_store_be32);
1✔
678
         result.test_is_eq(cex_load_be32, in32);
1✔
679
         constexpr uint64_t cex_load_be64 = Botan::load_be<uint64_t>(cex_store_be64);
1✔
680
         result.test_is_eq(cex_load_be64, in64);
1✔
681

682
         // load_le/be multiple integers into a std::array for constexpr
683
         constexpr auto cex_load_le16s = Botan::load_le<std::array<uint16_t, cex_mem.size() / 2>>(cex_mem);
1✔
684
         result.test_is_eq(cex_load_le16s, {0x1100, 0x3322, 0x5544, 0x7766, 0x9988, 0xBBAA, 0xDDCC, 0xFFEE});
1✔
685
         constexpr auto cex_load_le32s = Botan::load_le<std::array<uint32_t, cex_mem.size() / 4>>(cex_mem);
1✔
686
         result.test_is_eq(cex_load_le32s, {0x33221100, 0x77665544, 0xBBAA9988, 0xFFEEDDCC});
1✔
687
         constexpr auto cex_load_le64s = Botan::load_le<std::array<uint64_t, cex_mem.size() / 8>>(cex_mem);
1✔
688
         result.test_is_eq(cex_load_le64s, {0x7766554433221100, 0xFFEEDDCCBBAA9988});
1✔
689

690
         constexpr auto cex_load_be16s = Botan::load_be<std::array<uint16_t, cex_mem.size() / 2>>(cex_mem);
1✔
691
         result.test_is_eq(cex_load_be16s, {0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xAABB, 0xCCDD, 0xEEFF});
1✔
692
         constexpr auto cex_load_be32s = Botan::load_be<std::array<uint32_t, cex_mem.size() / 4>>(cex_mem);
1✔
693
         result.test_is_eq(cex_load_be32s, {0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF});
1✔
694
         constexpr auto cex_load_be64s = Botan::load_be<std::array<uint64_t, cex_mem.size() / 8>>(cex_mem);
1✔
695
         result.test_is_eq(cex_load_be64s, {0x0011223344556677, 0x8899AABBCCDDEEFF});
1✔
696

697
         return result;
1✔
698
      }
×
699

700
      static std::vector<Test::Result> test_copy_out_be_le() {
1✔
701
         return {
1✔
702
            CHECK("copy_out_be with 16bit input (word aligned)",
703
                  [&](auto& result) {
1✔
704
                     std::vector<uint8_t> out_vector(4);
1✔
705
                     const std::array<uint16_t, 2> in_array = {0x0A0B, 0x0C0D};
1✔
706
                     Botan::copy_out_be(out_vector, in_array);
1✔
707
                     result.test_is_eq(out_vector, Botan::hex_decode("0A0B0C0D"));
2✔
708
                  }),
1✔
709

710
            CHECK("copy_out_be with 16bit input (partial words)",
711
                  [&](auto& result) {
1✔
712
                     std::vector<uint8_t> out_vector(3);
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("0A0B0C"));
2✔
716
                  }),
1✔
717

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

726
            CHECK("copy_out_le with 16bit input (partial words)",
727
                  [&](auto& result) {
1✔
728
                     std::vector<uint8_t> out_vector(3);
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("0B0A0D"));
2✔
732
                  }),
1✔
733

734
            CHECK("copy_out_be with 64bit input (word aligned)",
735
                  [&](auto& result) {
1✔
736
                     std::vector<uint8_t> out_vector(16);
1✔
737
                     const std::array<uint64_t, 2> in_array = {0x0A0B0C0D0E0F1011, 0x1213141516171819};
1✔
738
                     Botan::copy_out_be(out_vector, in_array);
1✔
739
                     result.test_is_eq(out_vector, Botan::hex_decode("0A0B0C0D0E0F10111213141516171819"));
2✔
740
                  }),
1✔
741

742
            CHECK("copy_out_le 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_le(out_vector, in_array);
1✔
747
                     result.test_is_eq(out_vector, Botan::hex_decode("11100F0E0D0C0B0A1918171615141312"));
2✔
748
                  }),
1✔
749

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

758
            CHECK("copy_out_le 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_le(out_vector, in_array);
1✔
763
                     result.test_is_eq(out_vector, Botan::hex_decode("11100F0E0D0C0B0A19181716151413"));
2✔
764
                  }),
1✔
765
         };
9✔
766
      }
1✔
767
};
768

769
BOTAN_REGISTER_SMOKE_TEST("utils", "util", Utility_Function_Tests);
770

771
class BitOps_Tests final : public Test {
×
772
   public:
773
      std::vector<Test::Result> run() override {
1✔
774
         std::vector<Test::Result> results;
1✔
775

776
         results.push_back(test_power_of_2());
2✔
777
         results.push_back(test_ctz());
2✔
778
         results.push_back(test_sig_bytes());
2✔
779

780
         return results;
1✔
781
      }
×
782

783
   private:
784
      template <typename T>
785
      void test_ctz(Test::Result& result, T val, size_t expected) {
6✔
786
         result.test_eq("ctz(" + std::to_string(val) + ")", Botan::ctz<T>(val), expected);
24✔
787
      }
6✔
788

789
      Test::Result test_ctz() {
1✔
790
         Test::Result result("ctz");
1✔
791
         test_ctz<uint32_t>(result, 0, 32);
1✔
792
         test_ctz<uint32_t>(result, 1, 0);
1✔
793
         test_ctz<uint32_t>(result, 0x80, 7);
1✔
794
         test_ctz<uint32_t>(result, 0x8000000, 27);
1✔
795
         test_ctz<uint32_t>(result, 0x8100000, 20);
1✔
796
         test_ctz<uint32_t>(result, 0x80000000, 31);
1✔
797

798
         return result;
1✔
799
      }
×
800

801
      template <typename T>
802
      void test_sig_bytes(Test::Result& result, T val, size_t expected) {
14✔
803
         result.test_eq("significant_bytes(" + std::to_string(val) + ")", Botan::significant_bytes<T>(val), expected);
70✔
804
      }
14✔
805

806
      Test::Result test_sig_bytes() {
1✔
807
         Test::Result result("significant_bytes");
1✔
808
         test_sig_bytes<uint32_t>(result, 0, 0);
1✔
809
         test_sig_bytes<uint32_t>(result, 1, 1);
1✔
810
         test_sig_bytes<uint32_t>(result, 0x80, 1);
1✔
811
         test_sig_bytes<uint32_t>(result, 255, 1);
1✔
812
         test_sig_bytes<uint32_t>(result, 256, 2);
1✔
813
         test_sig_bytes<uint32_t>(result, 65535, 2);
1✔
814
         test_sig_bytes<uint32_t>(result, 65536, 3);
1✔
815
         test_sig_bytes<uint32_t>(result, 0x80000000, 4);
1✔
816

817
         test_sig_bytes<uint64_t>(result, 0, 0);
1✔
818
         test_sig_bytes<uint64_t>(result, 1, 1);
1✔
819
         test_sig_bytes<uint64_t>(result, 0x80, 1);
1✔
820
         test_sig_bytes<uint64_t>(result, 256, 2);
1✔
821
         test_sig_bytes<uint64_t>(result, 0x80000000, 4);
1✔
822
         test_sig_bytes<uint64_t>(result, 0x100000000, 5);
1✔
823

824
         return result;
1✔
825
      }
×
826

827
      template <typename T>
828
      void test_power_of_2(Test::Result& result, T val, bool expected) {
15✔
829
         result.test_eq("power_of_2(" + std::to_string(val) + ")", Botan::is_power_of_2<T>(val), expected);
75✔
830
      }
15✔
831

832
      Test::Result test_power_of_2() {
1✔
833
         Test::Result result("is_power_of_2");
1✔
834

835
         test_power_of_2<uint32_t>(result, 0, false);
1✔
836
         test_power_of_2<uint32_t>(result, 1, false);
1✔
837
         test_power_of_2<uint32_t>(result, 2, true);
1✔
838
         test_power_of_2<uint32_t>(result, 3, false);
1✔
839
         test_power_of_2<uint32_t>(result, 0x8000, true);
1✔
840
         test_power_of_2<uint32_t>(result, 0x8001, false);
1✔
841
         test_power_of_2<uint32_t>(result, 0x8000000, true);
1✔
842

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

852
         return result;
1✔
853
      }
×
854
};
855

856
BOTAN_REGISTER_TEST("utils", "bit_ops", BitOps_Tests);
857

858
#if defined(BOTAN_HAS_POLY_DBL)
859

860
class Poly_Double_Tests final : public Text_Based_Test {
×
861
   public:
862
      Poly_Double_Tests() : Text_Based_Test("poly_dbl.vec", "In,Out") {}
2✔
863

864
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
82✔
865
         Test::Result result("Polynomial doubling");
82✔
866
         const std::vector<uint8_t> in = vars.get_req_bin("In");
82✔
867
         const std::vector<uint8_t> out = vars.get_req_bin("Out");
82✔
868

869
         std::vector<uint8_t> b = in;
82✔
870
         Botan::poly_double_n(b.data(), b.size());
82✔
871

872
         result.test_eq("Expected value", b, out);
82✔
873
         return result;
82✔
874
      }
246✔
875
};
876

877
BOTAN_REGISTER_TEST("utils", "poly_dbl", Poly_Double_Tests);
878

879
#endif
880

881
class Version_Tests final : public Test {
×
882
   public:
883
      std::vector<Test::Result> run() override {
1✔
884
         Test::Result result("Versions");
1✔
885

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

888
         const char* version_cstr = Botan::version_cstr();
1✔
889
         std::string version_str = Botan::version_string();
1✔
890
         result.test_eq("Same version string", version_str, std::string(version_cstr));
2✔
891

892
         const char* sversion_cstr = Botan::short_version_cstr();
1✔
893
         std::string sversion_str = Botan::short_version_string();
1✔
894
         result.test_eq("Same short version string", sversion_str, std::string(sversion_cstr));
2✔
895

896
         std::string expected_sversion = std::to_string(BOTAN_VERSION_MAJOR) + "." +
3✔
897
                                         std::to_string(BOTAN_VERSION_MINOR) + "." +
3✔
898
                                         std::to_string(BOTAN_VERSION_PATCH);
2✔
899

900
#if defined(BOTAN_VERSION_SUFFIX)
901
         expected_sversion += BOTAN_VERSION_SUFFIX_STR;
902
#endif
903

904
         result.test_eq("Short version string has expected format", sversion_str, expected_sversion);
1✔
905

906
         const std::string version_check_ok =
1✔
907
            Botan::runtime_version_check(BOTAN_VERSION_MAJOR, BOTAN_VERSION_MINOR, BOTAN_VERSION_PATCH);
1✔
908

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

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

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

916
         result.test_eq("Expected warning text", version_check_bad, expected_error);
1✔
917

918
         return {result};
3✔
919
      }
2✔
920
};
921

922
BOTAN_REGISTER_TEST("utils", "versioning", Version_Tests);
923

924
class Date_Format_Tests final : public Text_Based_Test {
×
925
   public:
926
      Date_Format_Tests() : Text_Based_Test("dates.vec", "Date") {}
2✔
927

928
      static std::vector<uint32_t> parse_date(const std::string& s) {
8✔
929
         const std::vector<std::string> parts = Botan::split_on(s, ',');
8✔
930
         if(parts.size() != 6) {
8✔
931
            throw Test_Error("Bad date format '" + s + "'");
×
932
         }
933

934
         std::vector<uint32_t> u32s;
8✔
935
         u32s.reserve(parts.size());
8✔
936
         for(const auto& sub : parts) {
56✔
937
            u32s.push_back(Botan::to_u32bit(sub));
48✔
938
         }
939
         return u32s;
8✔
940
      }
8✔
941

942
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
8✔
943
         const std::string date_str = vars.get_req_str("Date");
8✔
944
         Test::Result result("Date parsing");
8✔
945

946
         const std::vector<uint32_t> d = parse_date(date_str);
8✔
947

948
         if(type == "valid" || type == "valid.not_std" || type == "valid.64_bit_time_t") {
8✔
949
            Botan::calendar_point c(d[0], d[1], d[2], d[3], d[4], d[5]);
8✔
950
            result.test_is_eq(date_str + " year", c.year(), d[0]);
8✔
951
            result.test_is_eq(date_str + " month", c.month(), d[1]);
8✔
952
            result.test_is_eq(date_str + " day", c.day(), d[2]);
8✔
953
            result.test_is_eq(date_str + " hour", c.hour(), d[3]);
8✔
954
            result.test_is_eq(date_str + " minute", c.minutes(), d[4]);
8✔
955
            result.test_is_eq(date_str + " second", c.seconds(), d[5]);
8✔
956

957
            if(type == "valid.not_std" ||
8✔
958
               (type == "valid.64_bit_time_t" && c.year() > 2037 && sizeof(std::time_t) == 4)) {
959
               result.test_throws("valid but out of std::timepoint range", [c]() { c.to_std_timepoint(); });
12✔
960
            } else {
961
               Botan::calendar_point c2(c.to_std_timepoint());
5✔
962
               result.test_is_eq(date_str + " year", c2.year(), d[0]);
5✔
963
               result.test_is_eq(date_str + " month", c2.month(), d[1]);
5✔
964
               result.test_is_eq(date_str + " day", c2.day(), d[2]);
5✔
965
               result.test_is_eq(date_str + " hour", c2.hour(), d[3]);
5✔
966
               result.test_is_eq(date_str + " minute", c2.minutes(), d[4]);
5✔
967
               result.test_is_eq(date_str + " second", c2.seconds(), d[5]);
10✔
968
            }
969
         } else if(type == "invalid") {
×
970
            result.test_throws("invalid date", [d]() { Botan::calendar_point c(d[0], d[1], d[2], d[3], d[4], d[5]); });
×
971
         } else {
972
            throw Test_Error("Unexpected header '" + type + "' in date format tests");
×
973
         }
974

975
         return result;
16✔
976
      }
8✔
977

978
      std::vector<Test::Result> run_final_tests() override {
1✔
979
         Test::Result result("calendar_point::to_string");
1✔
980
         Botan::calendar_point d(2008, 5, 15, 9, 30, 33);
1✔
981
         // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
982
         result.test_eq("calendar_point::to_string", d.to_string(), "2008-05-15T09:30:33");
2✔
983
         return {result};
3✔
984
      }
2✔
985
};
986

987
BOTAN_REGISTER_TEST("utils", "util_dates", Date_Format_Tests);
988

989
class Charset_Tests final : public Text_Based_Test {
×
990
   public:
991
      Charset_Tests() : Text_Based_Test("charset.vec", "In,Out") {}
2✔
992

993
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
8✔
994
         Test::Result result("Charset");
8✔
995

996
         const std::vector<uint8_t> in = vars.get_req_bin("In");
8✔
997
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
8✔
998

999
         std::string converted;
8✔
1000

1001
         if(type == "UCS2-UTF8") {
8✔
1002
            converted = Botan::ucs2_to_utf8(in.data(), in.size());
4✔
1003
         } else if(type == "UCS4-UTF8") {
4✔
1004
            converted = Botan::ucs4_to_utf8(in.data(), in.size());
1✔
1005
         } else if(type == "LATIN1-UTF8") {
3✔
1006
            converted = Botan::latin1_to_utf8(in.data(), in.size());
3✔
1007
         } else {
1008
            throw Test_Error("Unexpected header '" + type + "' in charset tests");
×
1009
         }
1010

1011
         result.test_eq(
16✔
1012
            "string converted successfully", std::vector<uint8_t>(converted.begin(), converted.end()), expected);
8✔
1013

1014
         return result;
8✔
1015
      }
24✔
1016
};
1017

1018
BOTAN_REGISTER_TEST("utils", "charset", Charset_Tests);
1019

1020
class Hostname_Tests final : public Text_Based_Test {
×
1021
   public:
1022
      Hostname_Tests() : Text_Based_Test("hostnames.vec", "Issued,Hostname") {}
2✔
1023

1024
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
44✔
1025
         Test::Result result("Hostname Matching");
44✔
1026

1027
         const std::string issued = vars.get_req_str("Issued");
44✔
1028
         const std::string hostname = vars.get_req_str("Hostname");
44✔
1029
         const bool expected = (type == "Invalid") ? false : true;
44✔
1030

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

1034
         return result;
44✔
1035
      }
44✔
1036
};
1037

1038
BOTAN_REGISTER_TEST("utils", "hostname", Hostname_Tests);
1039

1040
class IPv4_Parsing_Tests final : public Text_Based_Test {
×
1041
   public:
1042
      IPv4_Parsing_Tests() : Text_Based_Test("utils/ipv4.vec", "IPv4") {}
2✔
1043

1044
      Test::Result run_one_test(const std::string& status, const VarMap& vars) override {
47✔
1045
         Test::Result result("IPv4 parsing");
47✔
1046

1047
         const std::string input = vars.get_req_str("IPv4");
47✔
1048
         const bool valid = (status == "Valid");
47✔
1049

1050
         auto ipv4 = Botan::string_to_ipv4(input);
47✔
1051

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

1054
         if(ipv4) {
47✔
1055
            const std::string rt = Botan::ipv4_to_string(ipv4.value());
13✔
1056
            result.test_eq("ipv4_to_string and string_to_ipv4 round trip", input, rt);
26✔
1057
         }
13✔
1058

1059
         return result;
47✔
1060
      }
47✔
1061
};
1062

1063
BOTAN_REGISTER_TEST("utils", "ipv4_parse", IPv4_Parsing_Tests);
1064

1065
class ReadKV_Tests final : public Text_Based_Test {
×
1066
   public:
1067
      ReadKV_Tests() : Text_Based_Test("utils/read_kv.vec", "Input,Expected") {}
2✔
1068

1069
      Test::Result run_one_test(const std::string& status, const VarMap& vars) override {
16✔
1070
         Test::Result result("read_kv");
16✔
1071

1072
         const bool is_valid = (status == "Valid");
16✔
1073

1074
         const std::string input = vars.get_req_str("Input");
16✔
1075
         const std::string expected = vars.get_req_str("Expected");
16✔
1076

1077
         if(is_valid) {
16✔
1078
            confirm_kv(result, Botan::read_kv(input), split_group(expected));
14✔
1079
         } else {
1080
            // In this case "expected" is the expected exception message
1081
            result.test_throws("Invalid key value input throws exception", expected, [&]() { Botan::read_kv(input); });
36✔
1082
         }
1083
         return result;
16✔
1084
      }
16✔
1085

1086
   private:
1087
      static std::vector<std::string> split_group(const std::string& str) {
7✔
1088
         std::vector<std::string> elems;
7✔
1089
         if(str.empty()) {
7✔
1090
            return elems;
1091
         }
1092

1093
         std::string substr;
6✔
1094
         for(auto i = str.begin(); i != str.end(); ++i) {
115✔
1095
            if(*i == '|') {
109✔
1096
               elems.push_back(substr);
16✔
1097
               substr.clear();
16✔
1098
            } else {
1099
               substr += *i;
202✔
1100
            }
1101
         }
1102

1103
         if(!substr.empty()) {
6✔
1104
            elems.push_back(substr);
6✔
1105
         }
1106

1107
         return elems;
6✔
1108
      }
6✔
1109

1110
      static void confirm_kv(Test::Result& result,
7✔
1111
                             const std::map<std::string, std::string>& kv,
1112
                             const std::vector<std::string>& expected) {
1113
         if(!result.test_eq("expected size", expected.size() % 2, size_t(0))) {
7✔
1114
            return;
1115
         }
1116

1117
         for(size_t i = 0; i != expected.size(); i += 2) {
18✔
1118
            auto j = kv.find(expected[i]);
11✔
1119
            if(result.confirm("Found key", j != kv.end())) {
22✔
1120
               result.test_eq("Matching value", j->second, expected[i + 1]);
22✔
1121
            }
1122
         }
1123

1124
         result.test_eq("KV has same size as expected", kv.size(), expected.size() / 2);
14✔
1125
      }
1126
};
1127

1128
BOTAN_REGISTER_TEST("utils", "util_read_kv", ReadKV_Tests);
1129

1130
class CPUID_Tests final : public Test {
×
1131
   public:
1132
      std::vector<Test::Result> run() override {
1✔
1133
         Test::Result result("CPUID");
1✔
1134

1135
         result.confirm("Endian is either little or big",
2✔
1136
                        Botan::CPUID::is_big_endian() || Botan::CPUID::is_little_endian());
1137

1138
         if(Botan::CPUID::is_little_endian()) {
1✔
1139
            result.test_eq("If endian is little, it is not also big endian", Botan::CPUID::is_big_endian(), false);
1✔
1140
         } else {
1141
            result.test_eq("If endian is big, it is not also little endian", Botan::CPUID::is_little_endian(), false);
1142
         }
1143

1144
         const std::string cpuid_string = Botan::CPUID::to_string();
1✔
1145
         result.test_success("CPUID::to_string doesn't crash");
1✔
1146

1147
#if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
1148

1149
         if(Botan::CPUID::has_sse2()) {
1✔
1150
            result.confirm("Output string includes sse2", cpuid_string.find("sse2") != std::string::npos);
2✔
1151

1152
            Botan::CPUID::clear_cpuid_bit(Botan::CPUID::CPUID_SSE2_BIT);
1✔
1153

1154
            result.test_eq("After clearing cpuid bit, has_sse2 returns false", Botan::CPUID::has_sse2(), false);
1✔
1155

1156
            Botan::CPUID::initialize();  // reset state
1✔
1157
            result.test_eq("After reinitializing, has_sse2 returns true", Botan::CPUID::has_sse2(), true);
2✔
1158
         }
1159
#endif
1160

1161
         return {result};
3✔
1162
      }
2✔
1163
};
1164

1165
BOTAN_REGISTER_SERIALIZED_TEST("utils", "cpuid", CPUID_Tests);
1166

1167
#if defined(BOTAN_HAS_UUID)
1168

1169
class UUID_Tests : public Test {
×
1170
   public:
1171
      std::vector<Test::Result> run() override {
1✔
1172
         Test::Result result("UUID");
1✔
1173

1174
         const Botan::UUID empty_uuid;
1✔
1175
         const Botan::UUID random_uuid1(this->rng());
1✔
1176
         const Botan::UUID random_uuid2(this->rng());
1✔
1177
         const Botan::UUID loaded_uuid(std::vector<uint8_t>(16, 4));
1✔
1178

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

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

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

1186
         result.test_eq("Random UUID not empty", random_uuid1.is_valid(), true);
1✔
1187
         result.test_eq("Random UUID not empty", random_uuid2.is_valid(), true);
1✔
1188

1189
         result.confirm("Random UUIDs are distinct", random_uuid1 != random_uuid2);
2✔
1190
         result.confirm("Random UUIDs not equal to empty", random_uuid1 != empty_uuid);
2✔
1191

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

1195
         const std::string uuid_r1_str = random_uuid1.to_string();
1✔
1196
         result.confirm("UUID from string matches", Botan::UUID(uuid_r1_str) == random_uuid1);
2✔
1197

1198
         class AllSame_RNG : public Botan::RandomNumberGenerator {
×
1199
            public:
1200
               explicit AllSame_RNG(uint8_t b) : m_val(b) {}
2✔
1201

1202
               void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> /* ignored */) override {
2✔
1203
                  for(auto& byte : output) {
34✔
1204
                     byte = m_val;
32✔
1205
                  }
1206
               }
2✔
1207

1208
               std::string name() const override { return "zeros"; }
×
1209

1210
               bool accepts_input() const override { return false; }
×
1211

1212
               void clear() override {}
×
1213

1214
               bool is_seeded() const override { return true; }
×
1215

1216
            private:
1217
               uint8_t m_val;
1218
         };
1219

1220
         AllSame_RNG zeros(0x00);
1✔
1221
         const Botan::UUID zero_uuid(zeros);
1✔
1222
         result.test_eq("Zero UUID matches expected", zero_uuid.to_string(), "00000000-0000-4000-8000-000000000000");
2✔
1223

1224
         AllSame_RNG ones(0xFF);
1✔
1225
         const Botan::UUID ones_uuid(ones);
1✔
1226
         result.test_eq("Ones UUID matches expected", ones_uuid.to_string(), "FFFFFFFF-FFFF-4FFF-BFFF-FFFFFFFFFFFF");
2✔
1227

1228
         return {result};
3✔
1229
      }
6✔
1230
};
1231

1232
BOTAN_REGISTER_TEST("utils", "uuid", UUID_Tests);
1233

1234
#endif
1235

1236
class Formatter_Tests : public Test {
×
1237
   public:
1238
      std::vector<Test::Result> run() override {
1✔
1239
         Test::Result result("Format utility");
1✔
1240

1241
         /*
1242
         In a number of these tests, we are not strictly depending on the
1243
         behavior, for instance checking `fmt("{}") == "{}"` is more about
1244
         checking that we don't crash, rather than we return that precise string.
1245
         */
1246

1247
         result.test_eq("test 1", Botan::fmt("hi"), "hi");
2✔
1248
         result.test_eq("test 2", Botan::fmt("ignored", 5), "ignored");
2✔
1249
         result.test_eq("test 3", Botan::fmt("answer is {}", 42), "answer is 42");
2✔
1250
         result.test_eq("test 4", Botan::fmt("{", 5), "{");
2✔
1251
         result.test_eq("test 4", Botan::fmt("{}"), "{}");
2✔
1252
         result.test_eq("test 5", Botan::fmt("{} == '{}'", 5, "five"), "5 == 'five'");
2✔
1253

1254
         return {result};
3✔
1255
      }
2✔
1256
};
1257

1258
BOTAN_REGISTER_TEST("utils", "fmt", Formatter_Tests);
1259

1260
class ScopedCleanup_Tests : public Test {
×
1261
   public:
1262
      std::vector<Test::Result> run() override {
1✔
1263
         return {
1✔
1264
            CHECK("leaving a scope results in cleanup",
1265
                  [](Test::Result& result) {
1✔
1266
                     bool ran = false;
1✔
1267
                     {
1✔
1268
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1✔
1269
                     }
1✔
1270
                     result.confirm("cleanup ran", ran);
2✔
1271
                  }),
1✔
1272

1273
            CHECK("leaving a function, results in cleanup",
1274
                  [](Test::Result& result) {
1✔
1275
                     bool ran = false;
1✔
1276
                     bool fn_called = false;
1✔
1277
                     auto fn = [&] {
2✔
1278
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1✔
1279
                        fn_called = true;
1✔
1280
                     };
2✔
1281

1282
                     result.confirm("cleanup not yet ran", !ran);
2✔
1283
                     fn();
1✔
1284
                     result.confirm("fn called", fn_called);
2✔
1285
                     result.confirm("cleanup ran", ran);
2✔
1286
                  }),
1✔
1287

1288
            CHECK("stack unwinding results in cleanup",
1289
                  [](Test::Result& result) {
1✔
1290
                     bool ran = false;
1✔
1291
                     bool fn_called = false;
1✔
1292
                     bool exception_caught = false;
1✔
1293
                     auto fn = [&] {
2✔
1294
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1✔
1295
                        fn_called = true;
1✔
1296
                        throw std::runtime_error("test");
1✔
1297
                     };
2✔
1298

1299
                     result.confirm("cleanup not yet ran", !ran);
2✔
1300
                     try {
1✔
1301
                        fn();
1✔
1302
                     } catch(const std::exception&) {
1✔
1303
                        exception_caught = true;
1✔
1304
                     }
1✔
1305

1306
                     result.confirm("fn called", fn_called);
2✔
1307
                     result.confirm("cleanup ran", ran);
2✔
1308
                     result.confirm("exception caught", exception_caught);
2✔
1309
                  }),
1✔
1310

1311
            CHECK("cleanup isn't called after disengaging",
1312
                  [](Test::Result& result) {
1✔
1313
                     bool ran = false;
1✔
1314
                     {
1✔
1315
                        auto clean = Botan::scoped_cleanup([&] { ran = true; });
1✔
1316
                        clean.disengage();
1✔
1317
                     }
1✔
1318
                     result.confirm("cleanup not ran", !ran);
2✔
1319
                  }),
1✔
1320

1321
         };
5✔
1322
      }
1✔
1323
};
1324

1325
BOTAN_REGISTER_TEST("utils", "scoped_cleanup", ScopedCleanup_Tests);
1326

1327
}  // namespace
1328

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

© 2025 Coveralls, Inc