Repository navigation
Expand file tree
/
Copy pathfdo_test.c
More file actions
451 lines (393 loc) · 13.6 KB
/
Copy pathfdo_test.c
File metadata and controls
451 lines (393 loc) · 13.6 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* fdo_test.c - unit and parity tests for the C89 FDO codec.
*
* Build (host): cc -I. fdo_test.c fdo.c -o fdo_test
* Run: ./fdo_test
*
* The tests prove:
* - encode (compiler) produces expected bytes for given frames
* - decode (decompiler) correctly parses styles and large atoms
* - roundtrips preserve logical content
* - for self-generated full-style output, byte parity after decode+encode
*
* For A/UX: this test builds and runs on the host to certify the code.
* A minimal version without file I/O can be run on vintage hardware after cross
* or native build.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "fdo.h"
static int tests = 0;
static int fails = 0;
#define CHECK(cond) do { \
tests++; \
if (!(cond)) { \
fails++; \
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
} \
} while (0)
/* A tiny known-good full-style vector (proto0 atom0 empty) */
static void test_basic_full(void)
{
unsigned char out[32];
unsigned long w;
FdoFrame f;
FdoFrame *dec = 0;
int n;
f.proto = 0;
f.atom = 0;
f.data = 0;
f.len = 0;
CHECK(fdo_compile_frames(&f, 1, out, sizeof out, &w) == 0);
CHECK(w == 3);
CHECK(out[0] == 0x00); /* FULL | 0 */
CHECK(out[1] == 0x00); /* atom 0 */
CHECK(out[2] == 0x00); /* len 0 */
CHECK(fdo_decode(out, w, &dec, &n) == 0);
CHECK(n == 1);
CHECK(dec[0].proto == 0);
CHECK(dec[0].atom == 0);
CHECK(dec[0].len == 0);
fdo_free_frames(dec, n);
}
/* Encode then decode then re-encode must yield identical bytes (full path) */
static void test_roundtrip_self(void)
{
unsigned char buf1[128], buf2[128];
unsigned long w1, w2;
FdoFrame frames[3];
unsigned char d2[2] = {0xAB, 0x01};
FdoFrame *dec = 0;
int n, i;
(void)d2; /* in case not needed for a minimal build */
frames[0].proto = 0; frames[0].atom = 1; frames[0].data = 0; frames[0].len = 0;
frames[1].proto = 0; frames[1].atom = 2; frames[1].data = d2; frames[1].len = 2;
frames[2].proto = 1; frames[2].atom = 0; frames[2].data = 0; frames[2].len = 0;
CHECK(fdo_compile_frames(frames, 3, buf1, sizeof buf1, &w1) == 0);
CHECK(fdo_decode(buf1, w1, &dec, &n) == 0);
CHECK(n == 3);
/* re-encode the decoded frames with full */
CHECK(fdo_compile_frames(dec, n, buf2, sizeof buf2, &w2) == 0);
CHECK(w1 == w2);
for (i = 0; i < (int)w1; i++) {
if (buf1[i] != buf2[i]) {
printf(" mismatch at %d: %02x vs %02x\n", i, buf1[i]&0xFF, buf2[i]&0xFF);
CHECK(0);
break;
}
}
fdo_free_frames(dec, n);
}
/* Selftest inside the lib */
static void test_lib_selftest(void)
{
CHECK(fdo_selftest() == 0);
}
/* Hardcoded 24-byte golden-ish vector from resources (ada32 32-572).
* We don't assert exact re-encode bytes (it may have used compact styles),
* but we assert that decode succeeds and logical frames are sensible,
* and that a re-decode of the re-encoded (full) form yields same atom count
* and matching data for the first frame.
*/
static void test_golden_like(void)
{
/* Exact bytes of target/test-classes/golden_ada32/32-572.fdo.compiled.bin */
static unsigned char golden[24] = {
0x00,0x01,0x00,0x0d,0x0f,0x0f,0x59,0x6f,0x75,0x27,0x76,0x65,
0x20,0x47,0x6f,0x74,0x20,0x4d,0x61,0x69,0x6c,0x00,0x02,0x00
};
FdoFrame *dec = 0;
int n;
unsigned char re[64];
unsigned long wr;
FdoFrame *dec2 = 0;
int n2;
CHECK(fdo_decode(golden, 24, &dec, &n) == 0);
CHECK(n >= 1);
/* This golden starts with a zero-len atom (proto0,atom1). We mainly
prove that a real mixed-style bin from the corpus decodes at all
without crashing, and that re-encoding the logical frames (in full
style) and decoding again produces a stream with the same atom count
and equivalent first frame. (Compact styles in the original mean the
full re-encode bytes will legitimately differ.) */
CHECK(dec[0].proto == 0);
CHECK(dec[0].atom == 1);
CHECK(dec[0].len == 0);
/* data may be NULL or empty buffer; both acceptable for len==0 */
if (dec[0].data != 0) {
/* if allocated, first byte should be whatever but we don't care */
}
/* Re-encode with full (will likely differ in style for later frames) */
CHECK(fdo_compile_frames(dec, n, re, sizeof re, &wr) == 0);
CHECK(wr > 0);
/* Re-decode the full version; should have same # of logical atoms */
CHECK(fdo_decode(re, wr, &dec2, &n2) == 0);
CHECK(n2 == n);
/* First frame should still be the zero-len (0,1) after round */
CHECK(dec2[0].proto == 0);
CHECK(dec2[0].atom == 1);
CHECK(dec2[0].len == 0);
fdo_free_frames(dec, n);
fdo_free_frames(dec2, n2);
}
static void test_compact_decode(void)
{
/* A hand-crafted compact example:
ATOM style first byte: (3<<5) | atom (style 3, atom<32)
*/
unsigned char compact[1];
FdoFrame *dec = 0;
int n;
/* current starts at 0, emit ATOM style for atom 3, no data */
compact[0] = (3 << 5) | 3; /* 0x60 | 3 = 0x63 */
CHECK(fdo_decode(compact, 1, &dec, &n) == 0);
CHECK(n == 1);
CHECK(dec[0].proto == 0); /* inherited current */
CHECK(dec[0].atom == 3);
CHECK(dec[0].len == 0);
fdo_free_frames(dec, n);
}
/* Direct 1:1 byte parity tests for the compiler (fdo_compile_frames).
* The expected arrays were produced by the Java BinaryEncoder (fullOnly=true)
* via jshell against the exact same logical frames. This proves the C
* compiler emits identical bytes to the reference for these cases.
*/
static void test_java_parity(void)
{
unsigned char out[300];
unsigned long w;
FdoFrame f;
/* CASE from jshell: (0,5) with 3 bytes -> 00 05 03 11 22 33 */
{
unsigned char data[3] = {0x11,0x22,0x33};
static const unsigned char expect[6] = {0x00,0x05,0x03,0x11,0x22,0x33};
f.proto=0; f.atom=5; f.data=data; f.len=3;
CHECK(fdo_compile_frames(&f, 1, out, sizeof out, &w) == 0);
CHECK(w == 6);
CHECK(memcmp(out, expect, 6) == 0);
}
/* CASE: two frames (0,3 empty) + (0,4 data0) */
{
unsigned char data0[1] = {0};
FdoFrame fs[2];
static const unsigned char expect[7] = {0x00,0x03,0x00, 0x00,0x04,0x01,0x00};
fs[0].proto=0; fs[0].atom=3; fs[0].data=0; fs[0].len=0;
fs[1].proto=0; fs[1].atom=4; fs[1].data=data0; fs[1].len=1;
CHECK(fdo_compile_frames(fs, 2, out, sizeof out, &w) == 0);
CHECK(w == 7);
CHECK(memcmp(out, expect, 7) == 0);
}
/* Extended proto (hits PREFIX path) */
{
unsigned char data[1] = {(unsigned char)0xAA};
static const unsigned char expect[5] = {0xe8, 0x08, 0x01, 0x01, 0xaa};
f.proto=40; f.atom=1; f.data=data; f.len=1;
CHECK(fdo_compile_frames(&f, 1, out, sizeof out, &w) == 0);
CHECK(w == 5);
CHECK(memcmp(out, expect, 5) == 0);
}
}
/* ---------------- Integration tests against real-life golden samples ---------------- */
/*
* This is the level the Java side does (GoldenTestRunner, AdaBinVerification,
* Fdo88* roundtrips on hundreds/thousands of real captured/compiled .bin files).
*
* Here we exercise the C core compiler + decompiler on the actual golden corpus:
* for each real .bin:
* decode (decompiler) -> frames
* compile_frames full style (compiler) -> bytes
* decode again
* verify the two frame lists are logically identical (proto, atom, data)
*
* This proves the C implementation handles real-world FDO data at scale.
* Exact byte match after full re-encode is also reported (only possible for
* goldens that were originally emitted in full style).
*/
static int load_file(const char *path, unsigned char **out_data, unsigned long *out_len)
{
FILE *f = fopen(path, "rb");
if (!f) return -1;
if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return -1; }
long sz = ftell(f);
if (sz < 0) { fclose(f); return -1; }
rewind(f);
unsigned char *buf = (unsigned char *) malloc((size_t)sz);
if (!buf) { fclose(f); return -1; }
size_t got = fread(buf, 1, (size_t)sz, f);
fclose(f);
if (got != (size_t)sz) {
free(buf);
return -1;
}
*out_data = buf;
*out_len = (unsigned long)sz;
return 0;
}
static void free_frames_local(FdoFrame *f, int n)
{
if (!f) return;
int i;
for (i = 0; i < n; i++) {
if (f[i].data) free(f[i].data);
}
free(f);
}
static int frames_logically_equal(const FdoFrame *a, int na, const FdoFrame *b, int nb)
{
int i;
if (na != nb) return 0;
for (i = 0; i < na; i++) {
if (a[i].proto != b[i].proto) return 0;
if (a[i].atom != b[i].atom) return 0;
if (a[i].len != b[i].len) return 0;
if (a[i].len > 0) {
if (!a[i].data || !b[i].data) return 0;
if (memcmp(a[i].data, b[i].data, a[i].len) != 0) return 0;
}
}
return 1;
}
static void test_golden_integration(void)
{
const char *candidates[] = {
"src/test/resources/golden",
"target/test-classes/golden",
"../src/test/resources/golden",
"../../src/test/resources/golden",
"golden", /* in case someone copies a subset */
0
};
const char *used_dir = 0;
int i;
for (i = 0; candidates[i]; i++) {
DIR *d = opendir(candidates[i]);
if (d) {
used_dir = candidates[i];
closedir(d);
break;
}
}
printf("\n--- Golden integration (real life samples) ---\n");
if (!used_dir) {
printf("No golden directory found in standard locations.\n");
printf("Skipping mass roundtrip regression (this is normal when building\n");
printf("just the c89/ tree on A/UX without the full Java test resources).\n");
printf("To run full integration: cd to project root and run from there,\n");
printf("or ensure src/test/resources/golden exists.\n");
return;
}
printf("Using golden dir: %s\n", used_dir);
DIR *dir = opendir(used_dir);
if (!dir) {
printf("Failed to open %s even though probe succeeded.\n", used_dir);
return;
}
int processed = 0;
int logical_ok = 0;
int exact_byte = 0;
int decode_fail = 0;
int encode_fail = 0;
int redecode_fail = 0;
unsigned long max_size = 0;
struct dirent *ent;
while ((ent = readdir(dir)) != 0) {
const char *name = ent->d_name;
size_t nl = strlen(name);
if (nl < 5 || strcmp(name + nl - 4, ".bin") != 0)
continue;
char fullpath[1024];
snprintf(fullpath, sizeof fullpath, "%s/%s", used_dir, name);
struct stat st;
if (stat(fullpath, &st) != 0 || !S_ISREG(st.st_mode))
continue;
unsigned char *orig = 0;
unsigned long orig_len = 0;
if (load_file(fullpath, &orig, &orig_len) != 0) {
printf(" SKIP (load): %s\n", name);
continue;
}
if (orig_len > max_size) max_size = orig_len;
FdoFrame *f1 = 0;
int n1 = 0;
if (fdo_decode(orig, orig_len, &f1, &n1) != 0) {
decode_fail++;
free(orig);
continue;
}
/* Allocate a buffer big enough even for large master lists */
unsigned long re_cap = (orig_len < 65536) ? 65536 : (orig_len + 65536);
unsigned char *re = (unsigned char *) malloc((size_t)re_cap);
if (!re) {
encode_fail++;
free_frames_local(f1, n1);
free(orig);
continue;
}
unsigned long wr = 0;
int enc_rc = fdo_compile_frames(f1, n1, re, re_cap, &wr);
if (enc_rc != 0) {
encode_fail++;
free(re);
free_frames_local(f1, n1);
free(orig);
continue;
}
if (wr == orig_len && memcmp(re, orig, wr) == 0)
exact_byte++;
FdoFrame *f2 = 0;
int n2 = 0;
if (fdo_decode(re, wr, &f2, &n2) != 0) {
redecode_fail++;
free(re);
free_frames_local(f1, n1);
free(orig);
continue;
}
if (frames_logically_equal(f1, n1, f2, n2))
logical_ok++;
else
printf(" LOGICAL MISMATCH: %s (n1=%d n2=%d)\n", name, n1, n2);
processed++;
free(re);
free_frames_local(f1, n1);
free_frames_local(f2, n2);
free(orig);
}
closedir(dir);
printf("Processed: %d real .bin files\n", processed);
printf("Logical roundtrips OK: %d / %d\n", logical_ok, processed);
printf("Exact byte match (full re-encode vs original): %d\n", exact_byte);
printf("Decode failures on golden: %d\n", decode_fail);
printf("Encode failures: %d Re-decode failures: %d\n", encode_fail, redecode_fail);
printf("Largest golden seen: %lu bytes\n", max_size);
/* Treat mass logical failures as test failures */
if (processed > 0) {
if (logical_ok != processed) {
fails++;
printf("FAIL: not all golden logical roundtrips succeeded\n");
}
tests++; /* count the integration run as one "test" */
}
}
int main(void)
{
printf("fdo-c89 tests (C89 codec for A/UX)\n");
test_lib_selftest();
test_basic_full();
test_roundtrip_self();
test_golden_like();
test_compact_decode();
test_java_parity();
test_golden_integration();
if (fails == 0) {
printf("PASS %d tests\n", tests);
return 0;
} else {
printf("FAIL %d/%d tests\n", fails, tests);
return 1;
}
}