aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/catchy/test_utils.cpp
blob: eb94be0f4d2ef5a2b14455843003a41d8dbd0dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
(C) 2015 Simon Warta (Kullo GmbH)
(C) 2015 Jack Lloyd

Botan is released under the Simplified BSD License (see license.txt)
*/

#include "catchy_tests.h"

#include <botan/calendar.h>
#include <botan/parsing.h>
#include <botan/loadstor.h>
#include <botan/internal/rounding.h>

using namespace Botan;

TEST_CASE("round_up strictly positive", "[utils]")
   {
   CHECK_THAT(round_up( 1, 10), Equals(10));
   CHECK_THAT(round_up( 3, 10), Equals(10));
   CHECK_THAT(round_up( 9, 10), Equals(10));
   CHECK_THAT(round_up(10, 10), Equals(10));

   CHECK_THAT(round_up( 1, 4), Equals( 4));
   CHECK_THAT(round_up( 3, 4), Equals( 4));
   CHECK_THAT(round_up( 4, 4), Equals( 4));
   CHECK_THAT(round_up( 9, 4), Equals(12));
   CHECK_THAT(round_up(10, 4), Equals(12));
   }

TEST_CASE("round_up zero", "[utils]")
   {
   CHECK_THAT(round_up(0, 2),       Equals(0));
   CHECK_THAT(round_up(0, 10),      Equals(0));
   CHECK_THAT(round_up(0, 1000),    Equals(0));
   CHECK_THAT(round_up(0, 99999),   Equals(0));
   CHECK_THAT(round_up(0, 2222222), Equals(0));
   }

TEST_CASE("round_up invalid input", "[utils]")
   {
   CHECK_THROWS(round_up(3, 0));
   CHECK_THROWS(round_up(5, 0));
   }

TEST_CASE("calendar_point constructor works", "[utils]")
   {
      {
      auto point1 = calendar_point(1988, 04, 23, 14, 37, 28);
      CHECK_THAT(point1.year,    Equals(1988));
      CHECK_THAT(point1.month,   Equals(4));
      CHECK_THAT(point1.day,     Equals(23));
      CHECK_THAT(point1.hour,    Equals(14));
      CHECK_THAT(point1.minutes, Equals(37));
      CHECK_THAT(point1.seconds, Equals(28));
      }

      {
      auto point2 = calendar_point(1800, 01, 01, 0, 0, 0);
      CHECK_THAT(point2.year,    Equals(1800));
      CHECK_THAT(point2.month,   Equals(1));
      CHECK_THAT(point2.day,     Equals(1));
      CHECK_THAT(point2.hour,    Equals(0));
      CHECK_THAT(point2.minutes, Equals(0));
      CHECK_THAT(point2.seconds, Equals(0));
      }

      {
      auto point = calendar_point(2037, 12, 31, 24, 59, 59);
      CHECK_THAT(point.year,    Equals(2037));
      CHECK_THAT(point.month,   Equals(12));
      CHECK_THAT(point.day,     Equals(31));
      CHECK_THAT(point.hour,    Equals(24));
      CHECK_THAT(point.minutes, Equals(59));
      CHECK_THAT(point.seconds, Equals(59));
      }

      {
      auto point = calendar_point(2100, 5, 1, 0, 0, 0);
      CHECK_THAT(point.year,    Equals(2100));
      CHECK_THAT(point.month,   Equals(5));
      CHECK_THAT(point.day,     Equals(1));
      CHECK_THAT(point.hour,    Equals(0));
      CHECK_THAT(point.minutes, Equals(0));
      CHECK_THAT(point.seconds, Equals(0));
      }
   }

TEST_CASE("calendar_point to stl timepoint and back", "[utils]")
   {
   SECTION("default test")
      {
      auto in = calendar_point(1988, 04, 23, 14, 37, 28);
      auto out = calendar_value(in.to_std_timepoint());
      CHECK_THAT(out.year,    Equals(1988));
      CHECK_THAT(out.month,   Equals(4));
      CHECK_THAT(out.day,     Equals(23));
      CHECK_THAT(out.hour,    Equals(14));
      CHECK_THAT(out.minutes, Equals(37));
      CHECK_THAT(out.seconds, Equals(28));
      }

   // _mkgmtime on Windows does not work for dates before 1970
   SECTION("first possible time point")
      {
      auto in = calendar_point(1970, 01, 01, 00, 00, 00);
      auto out = calendar_value(in.to_std_timepoint());
      CHECK_THAT(out.year,    Equals(1970));
      CHECK_THAT(out.month,   Equals(01));
      CHECK_THAT(out.day,     Equals(01));
      CHECK_THAT(out.hour,    Equals(00));
      CHECK_THAT(out.minutes, Equals(00));
      CHECK_THAT(out.seconds, Equals(00));
      }

   SECTION("latest possible time point")
      {
      auto in = calendar_point(2037, 12, 31, 23, 59, 59);
      auto out = calendar_value(in.to_std_timepoint());
      CHECK_THAT(out.year,    Equals(2037));
      CHECK_THAT(out.month,   Equals(12));
      CHECK_THAT(out.day,     Equals(31));
      CHECK_THAT(out.hour,    Equals(23));
      CHECK_THAT(out.minutes, Equals(59));
      CHECK_THAT(out.seconds, Equals(59));
      }

   SECTION("year too early")
      {
         {
         auto in = calendar_point(1800, 01, 01, 0, 0, 0);
         CHECK_THROWS(in.to_std_timepoint());
         }

         {
         auto in = calendar_point(1899, 12, 31, 23, 59, 59);
         CHECK_THROWS(in.to_std_timepoint());
         }

         {
         auto in = calendar_point(1969, 12, 31, 23, 59, 58); // time_t = -2
         CHECK_THROWS(in.to_std_timepoint());
         }

         {
         auto in = calendar_point(1969, 12, 31, 23, 59, 59); // time_t = -1
         CHECK_THROWS(in.to_std_timepoint());
         }
      }

   SECTION("year too late")
      {
      auto in = calendar_point(2038, 01, 01, 0, 0, 0);
      CHECK_THROWS(in.to_std_timepoint());
      }
   }

TEST_CASE("load/store operations", "[utils]")
   {
   const byte mem[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                          0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };

   const u16bit in16 = 0x1234;
   const u32bit in32 = 0xA0B0C0D0;
   const u64bit in64 = 0xABCDEF0123456789;

   CHECK_THAT(get_byte(0, in32), Equals(0xA0));
   CHECK_THAT(get_byte(1, in32), Equals(0xB0));
   CHECK_THAT(get_byte(2, in32), Equals(0xC0));
   CHECK_THAT(get_byte(3, in32), Equals(0xD0));

   CHECK_THAT(make_u16bit(0xAA, 0xBB), Equals(0xAABB));
   CHECK_THAT(make_u32bit(0x01, 0x02, 0x03, 0x04), Equals(0x01020304));

   CHECK_THAT(load_be<u16bit>(mem, 0), Equals(0x0011));
   CHECK_THAT(load_be<u16bit>(mem, 1), Equals(0x2233));
   CHECK_THAT(load_be<u16bit>(mem, 2), Equals(0x4455));
   CHECK_THAT(load_be<u16bit>(mem, 3), Equals(0x6677));

   CHECK_THAT(load_le<u16bit>(mem, 0), Equals(0x1100));
   CHECK_THAT(load_le<u16bit>(mem, 1), Equals(0x3322));
   CHECK_THAT(load_le<u16bit>(mem, 2), Equals(0x5544));
   CHECK_THAT(load_le<u16bit>(mem, 3), Equals(0x7766));

   CHECK_THAT(load_be<u32bit>(mem, 0), Equals(0x00112233));
   CHECK_THAT(load_be<u32bit>(mem, 1), Equals(0x44556677));
   CHECK_THAT(load_be<u32bit>(mem, 2), Equals(0x8899AABB));
   CHECK_THAT(load_be<u32bit>(mem, 3), Equals(0xCCDDEEFF));

   CHECK_THAT(load_le<u32bit>(mem, 0), Equals(0x33221100));
   CHECK_THAT(load_le<u32bit>(mem, 1), Equals(0x77665544));
   CHECK_THAT(load_le<u32bit>(mem, 2), Equals(0xBBAA9988));
   CHECK_THAT(load_le<u32bit>(mem, 3), Equals(0xFFEEDDCC));

   CHECK_THAT(load_be<u64bit>(mem, 0), Equals(0x0011223344556677));
   CHECK_THAT(load_be<u64bit>(mem, 1), Equals(0x8899AABBCCDDEEFF));

   CHECK_THAT(load_le<u64bit>(mem, 0), Equals(0x7766554433221100));
   CHECK_THAT(load_le<u64bit>(mem, 1), Equals(0xFFEEDDCCBBAA9988));

   // Check misaligned loads:
   CHECK_THAT(load_be<u16bit>(mem + 1, 0), Equals(0x1122));
   CHECK_THAT(load_le<u16bit>(mem + 3, 0), Equals(0x4433));

   CHECK_THAT(load_be<u32bit>(mem + 1, 1), Equals(0x55667788));
   CHECK_THAT(load_le<u32bit>(mem + 3, 1), Equals(0xAA998877));

   CHECK_THAT(load_be<u64bit>(mem + 1, 0), Equals(0x1122334455667788));
   CHECK_THAT(load_le<u64bit>(mem + 7, 0), Equals(0xEEDDCCBBAA998877));
   CHECK_THAT(load_le<u64bit>(mem + 5, 0), Equals(0xCCBBAA9988776655));

   byte outbuf[16] = { 0 };

   for(size_t offset = 0; offset != 7; ++offset)
      {
      byte* out = outbuf + offset;

      store_be(in16, out);
      CHECK_THAT(out[0], Equals(0x12));
      CHECK_THAT(out[1], Equals(0x34));

      store_le(in16, out);
      CHECK_THAT(out[0], Equals(0x34));
      CHECK_THAT(out[1], Equals(0x12));

      store_be(in32, out);
      CHECK_THAT(out[0], Equals(0xA0));
      CHECK_THAT(out[1], Equals(0xB0));
      CHECK_THAT(out[2], Equals(0xC0));
      CHECK_THAT(out[3], Equals(0xD0));

      store_le(in32, out);
      CHECK_THAT(out[0], Equals(0xD0));
      CHECK_THAT(out[1], Equals(0xC0));
      CHECK_THAT(out[2], Equals(0xB0));
      CHECK_THAT(out[3], Equals(0xA0));

      store_be(in64, out);
      CHECK_THAT(out[0], Equals(0xAB));
      CHECK_THAT(out[1], Equals(0xCD));
      CHECK_THAT(out[2], Equals(0xEF));
      CHECK_THAT(out[3], Equals(0x01));
      CHECK_THAT(out[4], Equals(0x23));
      CHECK_THAT(out[5], Equals(0x45));
      CHECK_THAT(out[6], Equals(0x67));
      CHECK_THAT(out[7], Equals(0x89));

      store_le(in64, out);
      CHECK_THAT(out[0], Equals(0x89));
      CHECK_THAT(out[1], Equals(0x67));
      CHECK_THAT(out[2], Equals(0x45));
      CHECK_THAT(out[3], Equals(0x23));
      CHECK_THAT(out[4], Equals(0x01));
      CHECK_THAT(out[5], Equals(0xEF));
      CHECK_THAT(out[6], Equals(0xCD));
      CHECK_THAT(out[7], Equals(0xAB));
      }
}

TEST_CASE("uint32 parsing valid", "[utils]")
   {
   CHECK_THAT(to_u32bit("0"), Equals(0));
   CHECK_THAT(to_u32bit("1"), Equals(1));
   CHECK_THAT(to_u32bit("2"), Equals(2));
   CHECK_THAT(to_u32bit("10"), Equals(10));
   CHECK_THAT(to_u32bit("100"), Equals(100));
   CHECK_THAT(to_u32bit("1000"), Equals(1000));
   CHECK_THAT(to_u32bit("10000"), Equals(10000));
   CHECK_THAT(to_u32bit("100000"), Equals(100000));
   CHECK_THAT(to_u32bit("1000000"), Equals(1000000));
   // biggest allowed value
   CHECK_THAT(to_u32bit("4294967295"), Equals(4294967295));

   // leading zeros
   CHECK_THAT(to_u32bit("00"), Equals(0));
   CHECK_THAT(to_u32bit("01"), Equals(1));
   CHECK_THAT(to_u32bit("02"), Equals(2));
   CHECK_THAT(to_u32bit("010"), Equals(10));
   CHECK_THAT(to_u32bit("0000000000000000000000000010"), Equals(10));

   // leading and trailing whitespace
   CHECK_THROWS(to_u32bit(" 1"));
   CHECK_THROWS(to_u32bit(" 1 "));
   CHECK_THROWS(to_u32bit("\n1"));
   CHECK_THROWS(to_u32bit("1\n"));
   CHECK_THROWS(to_u32bit("1 5"));
   CHECK_THROWS(to_u32bit("1\t5"));
   CHECK_THROWS(to_u32bit("1\n5"));

   // Other stuff that is no digit
   CHECK_THROWS(to_u32bit("1Z"));

   // invalid input
   CHECK_THROWS(to_u32bit(""));
   CHECK_THROWS(to_u32bit(" "));
   CHECK_THROWS(to_u32bit("!"));
   //CHECK_THROWS(to_u32bit("1!"));
   CHECK_THROWS(to_u32bit("!1"));

   // Avoid overflow: value too big for uint32
   CHECK_THROWS(to_u32bit("4294967296"));
   }