-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_rle.c
430 lines (361 loc) · 11.7 KB
/
test_rle.c
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
RLE Zoo Encode & Decode Tests
Copyright (c) 2022, Eddy L O Jansson. Licensed under The MIT License.
See https://github.com/eloj/rle-zoo
*/
#define _GNU_SOURCE
#define UTILITY_IMPLEMENTATION
#include "utility.h"
#define RLE_ZOO_IMPLEMENTATION
#include "rle_goldbox.h"
#include "rle_packbits.h"
#include "rle_pcx.h"
#include "rle_icns.h"
#include "rle-variant-selection.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <ctype.h>
#include <sys/mman.h>
#define RED "\e[1;31m"
#define GREEN "\e[0;32m"
#define YELLOW "\e[1;33m"
#define NC "\e[0m"
static int debug = 1; // Output debug hex dumps for failed tests.
static int hex_always = 0;
static int hex_show_offset = 1;
static int flag_roundtrip = 1;
static int num_roundtrip = 0;
struct test {
uint8_t *input;
size_t len;
char *actions;
ssize_t expected_size;
uint32_t expected_hash; // CRC32c for now
};
/*
TODO: Should use some other digest with a simple plain-c implementation.
*/
__attribute__ ((target ("sse4.2")))
static uint32_t crc32c(uint32_t crc, const void *data, size_t len) {
const uint8_t *src = data;
for (size_t i=0 ; i < len ; ++i) {
crc = __builtin_ia32_crc32qi(crc, *src++);
}
return crc;
}
#define TEST_ERRMSG(fmt, ...) \
fprintf(stderr, "%s:%zu:" RED " error: " NC fmt "\n", filename, line_no __VA_OPT__(,) __VA_ARGS__)
#define TEST_WARNMSG(fmt, ...) \
fprintf(stderr, "%s:%zu:" YELLOW " warning: " NC fmt "\n", filename, line_no __VA_OPT__(,) __VA_ARGS__)
// This either compress or decompress the output of a test to check it against the original input.
static int roundtrip(struct rle_t *rle, struct test *te, uint8_t *inbuf, size_t inbuf_len, int compress) {
rle_fp rle_func = compress ? rle->compress : rle->decompress;
uint8_t *tmp_buf = malloc(te->len);
ssize_t res = rle_func(inbuf, inbuf_len, tmp_buf, te->len);
int cmp = memcmp(tmp_buf, te->input, te->len);
if (cmp != 0) {
printf("expected from %scompressed test input:\n", compress ? "" : "de");
fprint_hex(stdout, te->input, te->len, 32, "\n", hex_show_offset);
fflush(stdout);
printf("\n");
printf("got");
if (res <= 0) {
fprintf(stdout, " error: %zd -- buffer length unknown!\n", res);
fprint_hex(stdout, tmp_buf, te->len, 32, "\n", hex_show_offset);
fprintf(stdout, " ...<truncated>");
} else {
fprintf(stdout, " %zd bytes:\n", res);
fprint_hex(stdout, tmp_buf, res, 32, "\n", hex_show_offset);
}
fflush(stdout);
printf("\n");
} else {
cmp = res != (ssize_t)te->len;
}
free(tmp_buf);
++num_roundtrip;
return cmp;
}
static int run_rle_test(struct rle_t *rle, struct test *te, const char *filename, size_t line_no) {
// Take the max of the input and expected sizes as base estimate for temporary buffer.
size_t tmp_size = te->len;
if (te->expected_size > (ssize_t)tmp_size)
tmp_size = te->expected_size;
tmp_size *= 4;
assert(tmp_size < 1L << 24);
uint8_t *tmp_buf = malloc(tmp_size);
int retval = 0;
char *action = te->actions;
int no_roundtrip = strchr(action, '-') != NULL;
if (*action == 'c') {
// COMPRESS
// First do a length-determination check on the input.
ssize_t len_check = rle->compress(te->input, te->len, NULL, 0);
if (len_check != te->expected_size) {
TEST_ERRMSG("expected compressed size %zd, got %zd.", te->expected_size, len_check);
retval = 1;
}
if (len_check >= 0) {
// Next compress the input into the oversized buffer, and verify length remains the same.
assert(len_check <= (ssize_t)tmp_size);
ssize_t res = rle->compress(te->input, te->len, tmp_buf, tmp_size);
if (res != len_check) {
TEST_ERRMSG("compressed output length differs from determined value %zd, got %zd.", len_check, res);
retval = 1;
}
uint32_t res_hash = crc32c((uint32_t)~0, tmp_buf, res) ^ (uint32_t)~0;
// Now decompress with the output byte-tight, to check for dest range-check errors.
ssize_t res_tight = rle->compress(te->input, te->len, tmp_buf, len_check);
if (res_tight != len_check) {
TEST_ERRMSG("compressed output length for tight buffer differs from determined value %zd, got %zd.", len_check, res_tight);
retval = 1;
}
uint32_t res_tight_hash = crc32c((uint32_t)~0, tmp_buf, res_tight) ^ (uint32_t)~0;
if (res_hash != te->expected_hash) {
TEST_ERRMSG("expected compressed hash 0x%08x, got 0x%08x.", te->expected_hash, res_hash);
retval = 1;
}
// Verify there's no content diff between the oversized output buffer and the tight one.
if (res_tight_hash != res_hash) {
TEST_ERRMSG("compressed hash mismatch; 0x%08x vs 0x%08x.", res_tight_hash, res_hash);
retval = 1;
}
if (flag_roundtrip && !no_roundtrip && roundtrip(rle, te, tmp_buf, te->expected_size, 0) != 0) {
TEST_ERRMSG("re-decompressed data does not match original input!");
retval = 1;
}
if ((debug && retval != 0) || hex_always) {
if (res < 0) {
fprintf(stdout, "error: %zd -- hexdump unavailable, buffer length unknown!", res);
} else {
fprint_hex(stdout, tmp_buf, res, 32, "\n", hex_show_offset);
}
printf("\n");
fflush(stdout);
}
} else {
// end length check/input validation
}
} else if (*action == 'd') {
// DECOMPRESS
// First do a length-determination check on the input.
ssize_t len_check = rle->decompress(te->input, te->len, NULL, 0);
if (len_check != te->expected_size) {
TEST_ERRMSG("expected decompressed size %zd, got %zd.", te->expected_size, len_check);
retval = 1;
}
if (len_check > 0) {
// Next decompress the input into the oversized buffer, and verify length remains the same.
assert(len_check <= (ssize_t)tmp_size);
ssize_t res = rle->decompress(te->input, te->len, tmp_buf, tmp_size);
if (res != len_check) {
TEST_ERRMSG("decompressed output length differs from determined value %zd, got %zd.", len_check, res);
retval = 1;
}
uint32_t res_hash = crc32c((uint32_t)~0, tmp_buf, res) ^ (uint32_t)~0;
// Now decompress with the output byte-tight, to check for dest range-check errors.
ssize_t res_tight = rle->decompress(te->input, te->len, tmp_buf, len_check);
if (res_tight != len_check) {
TEST_ERRMSG("decompressed output length for tight buffer differs from determined value %zd, got %zd.", len_check, res_tight);
retval = 1;
}
uint32_t res_tight_hash = crc32c((uint32_t)~0, tmp_buf, res_tight) ^ (uint32_t)~0;
if (res_tight_hash != te->expected_hash) {
TEST_ERRMSG("expected decompressed hash 0x%08x, got 0x%08x.", te->expected_hash, res_hash);
retval = 1;
}
// Verify there's no content diff between the oversized output buffer and the tight one.
if (res_tight_hash != res_hash) {
TEST_ERRMSG("decompressed hash mismatch; 0x%08x vs 0x%08x.", res_tight_hash, res_hash);
retval = 1;
}
if (flag_roundtrip && !no_roundtrip && roundtrip(rle, te, tmp_buf, te->expected_size, 1) != 0) {
TEST_ERRMSG("re-compressed data does not match original input!");
retval = 1;
}
if ((debug && retval != 0) || hex_always) {
if (res < 0) {
fprintf(stdout, "error: %zd -- hexdump unavailable, buffer length unknown!", res);
} else {
fprint_hex(stdout, tmp_buf, res, 32, "\n", hex_show_offset);
}
printf("\n");
fflush(stdout);
}
} else {
// end length check/input validation
}
} else {
TEST_ERRMSG("Invalid action");
retval = 1;
}
free(tmp_buf);
return retval;
}
static int map_file(const char *filename, size_t ofs, ssize_t len, void **data, size_t *size) {
FILE *f = fopen(filename, "rb");
if (!f) {
return 1;
}
int res = fseek(f, 0, SEEK_END);
if (res != 0) {
fprintf(stderr, "System has broken fseek() -- guess we should have used fstat instead, huh.");
exit(1);
}
size_t flen = ftell(f);
if (len == 0) {
len = flen - ofs;
} else if (len < 0) {
len = -len;
ofs = flen - len;
}
// printf("map: seek ofs:%zu, len:%zd, flen:%zu\n", ofs, len, flen);
fseek(f, ofs, SEEK_SET);
void *base = NULL;
if (ofs + len <= flen) {
base = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(f), 0);
if (base == MAP_FAILED) {
fclose(f);
return 2;
}
*data = base;
*size = len;
} else {
fclose(f);
return 3;
}
fclose(f);
return 0;
}
static int process_file(const char *filename, int depth) {
if (depth > 3) {
fprintf(stderr, "Maximum include depth reached -- loop or just silly?\n");
return -1;
}
FILE *f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "ERROR: Could not open input file '%s': %m\n", filename);
return -1;
}
printf("<< Processing '%s':\n", filename);
char *line = NULL;
size_t failed_tests = 0;
size_t line_len = 0;
size_t line_no = 0;
ssize_t nread;
while ((nread = getline(&line, &line_len, f)) != -1) {
if (nread)
line[nread - 1] = 0; // Overwrite \n
++line_no;
// Skip comments
if (nread < 3 || line[0] == '#' || line[0] == ';') {
continue;
}
if (strncmp(line, "---", 3) == 0) {
TEST_WARNMSG("end-marker hit");
break;
}
if (strncmp(line, "include", 7) == 0) {
int res = process_file(line + 8, depth + 1);
if (res < 0) {
failed_tests = -1;
break;
}
failed_tests += res;
continue;
}
// Parse input line
// goldbox c "AAAAAAAAAAAAAAAA" 2 0xhash
char *method = NULL;
char *input = NULL;
struct test te = {};
int exsize = 0;
unsigned int exhash = 0;
// TODO: Parsing the hex this way is bad, e.g adding a hex digit up front still pass.
int parsed = sscanf(line, "%ms %ms %ms %i %x", &method, &te.actions, &input, &exsize, &exhash);
if (parsed >= 3) {
printf("<< %s\n", line);
struct rle_t * rle = get_rle_by_name(method);
if (rle) {
te.expected_size = exsize;
te.expected_hash = exhash;
if (input[0] == '@') {
// Read input from file.
void *raw = NULL;
size_t raw_len = 0;
ssize_t at_ofs = 0;
ssize_t at_len = 0;
int fn_ofs = 1;
if (input[fn_ofs] == '[') {
// parse offset + len
int advance = parse_ofs_len(input + fn_ofs, &at_ofs, &at_len);
if (advance < 0) {
TEST_WARNMSG("parse error in range: %d", advance);
goto nexttest;
}
fn_ofs += advance;
}
if (map_file(input + fn_ofs, at_ofs, at_len, &raw, &raw_len) != 0) {
TEST_WARNMSG("file error reading '%s': %m", input+fn_ofs);
goto nexttest;
} else {
te.len = raw_len;
te.input = malloc(te.len);
memcpy(te.input, raw, te.len);
munmap(raw, raw_len);
}
} else if (input[0] == '"') {
int err;
te.len = expand_escapes(input + 1, strlen(input + 1) - 1, NULL, 0, &err);
if (err == 0) {
// NOTE: I intentionally malloc the data, to give valgrind the best chance to detect OOB reads.
te.input = malloc(te.len);
te.len = expand_escapes(input + 1, strlen(input + 1) - 1, (char*)te.input, te.len, &err);
assert(err == 0);
} else {
TEST_WARNMSG("invalid escape sequence at position %zu, err %d\n", te.len, err);
goto nexttest;
}
} else {
TEST_WARNMSG("invalid input format");
goto nexttest;
}
if (run_rle_test(rle, &te, filename, line_no) != 0) {
++failed_tests;
}
} else {
TEST_WARNMSG("unknown method '%s'", method);
}
nexttest:
free(te.input);
free(te.actions);
}
free(input);
free(method);
}
free(line);
fclose(f);
return failed_tests;
}
int main(int argc, char *argv[]) {
const char *filename = argc > 1 ? argv[1] : "all-tests.suite";
int res = process_file(filename, 1);
if (res < 0) {
fprintf(stderr, RED "Test error." NC "\n");
exit(1);
}
if (flag_roundtrip == 0) {
printf(YELLOW "Warning: Roundtripping disabled -- test coverage decreased!" NC "\n");
}
if (res == 0) {
printf(GREEN "All tests of '%s' passed. (incl. %d roundtrip checks)" NC "\n", filename, num_roundtrip);
} else {
fprintf(stderr, RED "%d test failures in suite '%s'." NC "\n", res, filename);
exit(1);
}
return 0;
}