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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#define INVALID(n) { #n, -1, 0 }
#define VALID(n) { #n, 0, n }
/* always pads with leading zeros to a size of 9 digits */
static int crappy_uintmax_to_str(char *into, uintmax_t val)
{
#define BIGBASE 1000000000ull
#define STRINGIFY(s) #s
#define STRINGIFY2(s) STRINGIFY(s)
int len = 0;
if(val >= BIGBASE) {
len = crappy_uintmax_to_str(into, val/BIGBASE);
}
i_snprintf(into + len, 10, "%09llu",
(unsigned long long)(val % BIGBASE));
return len + strlen(STRINGIFY2(BIGBASE))-4;
#undef STRINGIFY2
#undef STRINGIFY
#undef BIGBASE
}
static void test_str_to_uintmax(void)
{
unsigned int i=0;
int randrange = i_rand_minmax(1, 15); /* when 1, will max out on 1s */
uintmax_t value = 0, valbase = i_rand() * 1000ull;
int len, ret;
char buff[50]; /* totally assumes < 159 bits */
test_begin("str_to_uintmax in range");
while (i < sizeof(uintmax_t)*CHAR_BIT) {
uintmax_t value_back;
const char *endp;
value = (value << 1) + 1;
if (value >= 64)
value -= i_rand_limit(randrange); /* don't always test the same numbers */
len = crappy_uintmax_to_str(buff, value);
ret = str_to_uintmax(buff, &value_back);
test_assert_idx(ret == 0, i);
test_assert_idx(value == value_back, i);
/* test with trailing noise */
buff[len] = 'x'; /* don't even null-terminate, let's be evil */
value_back = 0x1234567890123456;
ret = str_to_uintmax(buff, &value_back);
test_assert_idx(ret < 0, i);
test_assert_idx(value_back == 0x1234567890123456, i);
ret = str_parse_uintmax(buff, &value_back, &endp);
test_assert_idx(ret == 0, i);
test_assert_idx(value_back == value, i);
test_assert_idx(endp == &buff[len], i);
i++;
}
test_end();
/* not knowing exactly how large a uintmax_t is, we have to construct
the troublesome near-10/9*MAX strings manually by appending digits
to a MAX/9 string which we can easily create. Do a wider range
of 30 rather than the obvious 10, just in case - all are too large.*/
test_begin("str_to_uintmax overflow corner case");
value = UINTMAX_MAX/9-1;
len = crappy_uintmax_to_str(buff, value);
buff[len] = '0';
buff[len+1] = '\0';
for(i = 0; i <= 30; ++i) {
int j = len + 1;
while (buff[--j] == '9')
buff[j] = '0';
buff[j]++;
value = valbase + i;
ret = str_to_uintmax(buff, &value);
test_assert_idx(ret < 0 && value == valbase + i, i);
}
test_end();
}
/* always pads with leading zeros to a size of 9 digits */
static int crappy_uintmax_to_str_hex(char *into, uintmax_t val)
{
#define BIGBASE 0x1000000000ull
#define STRINGIFY(s) #s
#define STRINGIFY2(s) STRINGIFY(s)
int len = 0;
if(val >= BIGBASE) {
len = crappy_uintmax_to_str_hex(into, val/BIGBASE);
}
i_snprintf(into + len, 10, "%09llx",
(unsigned long long)(val % BIGBASE));
return len + strlen(STRINGIFY2(BIGBASE))-6;
#undef STRINGIFY2
#undef STRINGIFY
#undef BIGBASE
}
static void test_str_to_uintmax_hex(void)
{
unsigned int i=0;
int randrange = i_rand_minmax(1, 15); /* when 1, will max out on 1s */
uintmax_t value = 0, valbase = i_rand() * 1000ull;
int len, ret;
char buff[52]; /* totally assumes < 200 bits */
test_begin("str_to_uintmax_hex in range");
while (i < sizeof(uintmax_t)*CHAR_BIT) {
uintmax_t value_back;
const char *endp;
value = (value << 1) + 1;
if (value >= 64)
value -= i_rand_limit(randrange); /* don't always test the same numbers */
len = crappy_uintmax_to_str_hex(buff, value);
ret = str_to_uintmax_hex(buff, &value_back);
test_assert_idx(ret == 0, i);
test_assert_idx(value == value_back, i);
/* test with trailing noise */
buff[len] = 'x'; /* don't even null-terminate, let's be evil */
value_back = 0x1234567890123456;
ret = str_to_uintmax_hex(buff, &value_back);
test_assert_idx(ret < 0, i);
test_assert_idx(value_back == 0x1234567890123456, i);
ret = str_parse_uintmax_hex(buff, &value_back, &endp);
test_assert_idx(ret == 0, i);
test_assert_idx(value_back == value, i);
test_assert_idx(endp == &buff[len], i);
i++;
}
test_end();
/* not knowing exactly how large a uintmax_t is, we have to construct
the troublesome near-0x10/0x0F*MAX strings manually by appending digits
to a MAX/0x0f string which we can easily create. Do a wider range
of 0x30 rather than the obvious 0x10, just in case - all are too large.*/
test_begin("str_to_uintmax_hex overflow corner case");
value = (UINTMAX_MAX/0x0f)-1;
len = crappy_uintmax_to_str_hex(buff, value);
buff[len] = '0';
buff[len+1] = '\0';
for(i = 0; i <= 0x30; ++i) {
int j = len + 1;
while (buff[--j] == 'f')
buff[j] = '0';
if (buff[j] == '9')
buff[j] = 'a';
else
buff[j]++;
value = valbase + i;
ret = str_to_uintmax_hex(buff, &value);
test_assert_idx(ret < 0 && value == valbase + i, i);
}
test_end();
}
/* always pads with leading zeros to a size of 9 digits */
static int crappy_uintmax_to_str_oct(char *into, uintmax_t val)
{
#define BIGBASE 01000000000ull
#define STRINGIFY(s) #s
#define STRINGIFY2(s) STRINGIFY(s)
int len = 0;
if(val >= BIGBASE) {
len = crappy_uintmax_to_str_oct(into, val/BIGBASE);
}
i_snprintf(into + len, 10, "%09llo",
(unsigned long long)(val % BIGBASE));
return len + strlen(STRINGIFY2(BIGBASE))-5;
#undef STRINGIFY2
#undef STRINGIFY
#undef BIGBASE
}
static void test_str_to_uintmax_oct(void)
{
unsigned int i=0;
int randrange = i_rand_minmax(1, 15); /* when 1, will max out on 1s */
uintmax_t value = 0, valbase = i_rand() * 1000ull;
int len, ret;
char buff[69]; /* totally assumes < 200 bits */
test_begin("str_to_uintmax_oct in range");
while (i < sizeof(uintmax_t)*CHAR_BIT) {
uintmax_t value_back;
const char *endp = NULL;
value = (value << 1) + 1;
if (value >= 64)
value -= i_rand_limit(randrange); /* don't always test the same numbers */
len = crappy_uintmax_to_str_oct(buff, value);
ret = str_to_uintmax_oct(buff, &value_back);
test_assert_idx(ret == 0, i);
test_assert_idx(value == value_back, i);
/* test with trailing noise */
buff[len] = 'x'; /* don't even null-terminate, let's be evil */
value_back = 0x1234567890123456;
ret = str_to_uintmax_oct(buff, &value_back);
test_assert_idx(ret < 0, i);
test_assert_idx(value_back == 0x1234567890123456, i);
ret = str_parse_uintmax_oct(buff, &value_back, &endp);
test_assert_idx(ret == 0, i);
test_assert_idx(value_back == value, i);
test_assert_idx(endp == &buff[len], i);
i++;
}
test_end();
/* not knowing exactly how large a uintmax_t is, we have to construct
the troublesome near-010/007*MAX strings manually by appending digits
to a MAX/007 string which we can easily create. Do a wider range
of 030 rather than the obvious 010, just in case - all are too large.*/
test_begin("str_to_uintmax_oct overflow corner case");
value = (UINTMAX_MAX/007)-1;
len = crappy_uintmax_to_str_oct(buff, value);
buff[len] = '0';
buff[len+1] = '\0';
for(i = 0; i <= 030; ++i) {
int j = len + 1;
while (buff[--j] == '7')
buff[j] = '0';
buff[j]++;
value = valbase + i;
ret = str_to_uintmax_oct(buff, &value);
test_assert_idx(ret < 0 && value == valbase + i, i);
}
test_end();
}
static void test_str_to_u64(void)
{
unsigned int i;
const struct {
const char *input;
int ret;
uint64_t val;
} u64tests[] = {
INVALID(-1),
INVALID(foo),
VALID(0),
VALID(000000000000000000000000000000000000000000000000000000000000000),
{ "000000000000000000000000000000000000000000000000000001000000001", 0, 1000000001 },
{ "18446744073709551615", 0, 18446744073709551615ULL },
INVALID(18446744073709551616),
INVALID(20496382304121724010), /* 2^64*10/9 doesn't wrap */
INVALID(20496382304121724017), /* 2^64*10/9 wraps only after addition */
INVALID(20496382304121724020), /* 2^64*10/9 wraps on multiply*/
};
test_begin("str_to_uint64");
for (i = 0; i < N_ELEMENTS(u64tests); ++i) {
uint64_t val = 0xBADBEEF15BADF00D;
int ret = str_to_uint64(u64tests[i].input, &val);
test_assert_idx(ret == u64tests[i].ret, i);
if (ret == 0)
test_assert_idx(val == u64tests[i].val, i);
else
test_assert_idx(val == 0xBADBEEF15BADF00D, i);
if (ret == 0)
T_BEGIN {
const char *longer = t_strconcat(u64tests[i].input, "x", NULL);
ret = str_to_uint64(longer, &val);
test_assert_idx(ret < 0, i);
} T_END;
}
test_end();
}
static void test_str_to_u32(void)
{
unsigned int i;
const struct {
const char *input;
int ret;
uint32_t val;
} u32tests[] = {
VALID(0),
INVALID(-0),
VALID(4294967295),
INVALID(4294967296),
INVALID(4772185880),
INVALID(4772185884),
INVALID(4772185890),
};
test_begin("str_to_uint32");
for (i = 0; i < N_ELEMENTS(u32tests); ++i) {
uint32_t val = 0xDEADF00D;
int ret = str_to_uint32(u32tests[i].input, &val);
test_assert_idx(ret == u32tests[i].ret, i);
if (ret == 0)
test_assert_idx(val == u32tests[i].val, i);
else
test_assert_idx(val == 0xDEADF00D, i);
}
test_end();
}
/* Assumes long long is 64 bit, 2's complement */
static void test_str_to_llong(void)
{
unsigned int i;
const struct {
const char *input;
int ret;
long long val;
} i64tests[] = {
VALID(0),
VALID(-0),
INVALID(--0),
VALID(2147483648),
VALID(-2147483649),
VALID(9223372036854775807),
{ "-9223372036854775808", 0, -9223372036854775807-1 },
INVALID(9223372036854775808),
INVALID(-9223372036854775809),
};
test_begin("str_to_llong");
for (i = 0; i < N_ELEMENTS(i64tests); ++i) {
long long val = 123456789;
int ret = str_to_llong(i64tests[i].input, &val);
test_assert_idx(ret == i64tests[i].ret, i);
if (ret == 0)
test_assert_idx(val == i64tests[i].val, i);
else
test_assert_idx(val == 123456789, i);
}
test_end();
}
/* Assumes int is 32 bit, 2's complement */
static void test_str_to_i32(void)
{
unsigned int i;
const struct {
const char *input;
int ret;
int val;
} i32tests[] = {
VALID(0),
VALID(-0),
INVALID(--0),
VALID(2147483647),
VALID(-2147483648),
INVALID(2147483648),
INVALID(-2147483649),
};
test_begin("str_to_int");
for (i = 0; i < N_ELEMENTS(i32tests); ++i) {
int val = 123456789;
int ret = str_to_int(i32tests[i].input, &val);
test_assert_idx(ret == i32tests[i].ret, i);
if (ret == 0)
test_assert_idx(val == i32tests[i].val, i);
else
test_assert_idx(val == 123456789, i);
}
test_end();
}
static void test_str_is_float(void)
{
test_begin("str_is_float accepts integer");
/* accepts integer */
test_assert(str_is_float("0",'\0'));
test_assert(str_is_float("1234",'\0'));
test_end();
test_begin("str_is_float accepts float");
test_assert(str_is_float("0.0",'\0'));
test_assert(str_is_float("1234.0",'\0'));
test_assert(str_is_float("0.1234",'\0'));
test_assert(str_is_float("1234.1234",'\0'));
test_assert(str_is_float("0.1234 ",' '));
test_assert(str_is_float("1234.1234",'.'));
test_end();
test_begin("str_is_float refuses invalid values");
test_assert(!str_is_float(".",'\0'));
test_assert(!str_is_float(".1234",'\0'));
test_assert(!str_is_float("1234.",'\0'));
test_assert(!str_is_float("i am not a float at all",'\0'));
test_assert(!str_is_float("0x1234.0x1234",'\0'));
test_assert(!str_is_float(".0",'\0'));
test_assert(!str_is_float("0.",'\0'));
test_end();
}
void test_strnum(void)
{
/* If the above isn't true, then we do expect some failures possibly */
test_str_to_uintmax();
test_str_to_uintmax_hex();
test_str_to_uintmax_oct();
test_str_to_u64();
test_str_to_u32();
test_str_to_llong();
test_str_to_i32();
test_str_is_float();
}
|