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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
/*
Unix SMB/CIFS implementation.
test suite for the compression functions
Copyright (C) Jelmer Vernooij 2007
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "torture/torture.h"
#include "torture/local/proto.h"
#include "talloc.h"
#include "lzxpress.h"
#include "lib/util/base64.h"
/* Tests based on [MS-XCA] 3.1 Examples */
static bool test_msft_data1(
struct torture_context *test
)
{
TALLOC_CTX *tmp_ctx = talloc_new(test);
const char *fixed_data = "abcdefghijklmnopqrstuvwxyz";
const uint8_t fixed_out[] = {
0x3f, 0x00, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64,
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7a };
ssize_t c_size;
uint8_t *out, *out2;
out = talloc_size(tmp_ctx, 2048);
memset(out, 0x42, talloc_get_size(out));
torture_comment(test, "lzxpress fixed compression\n");
c_size = lzxpress_compress((const uint8_t *)fixed_data,
strlen(fixed_data),
out,
talloc_get_size(out));
torture_assert_int_equal(test, c_size, sizeof(fixed_out),
"fixed lzxpress_compress size");
torture_assert_mem_equal(test, out, fixed_out, c_size,
"fixed lzxpress_compress data");
torture_comment(test, "lzxpress fixed decompression\n");
out2 = talloc_size(tmp_ctx, strlen(fixed_data));
c_size = lzxpress_decompress(out,
sizeof(fixed_out),
out2,
talloc_get_size(out2));
torture_assert_int_equal(test, c_size, strlen(fixed_data),
"fixed lzxpress_decompress size");
torture_assert_mem_equal(test, out2, fixed_data, c_size,
"fixed lzxpress_decompress data");
talloc_free(tmp_ctx);
return true;
}
static bool test_msft_data2(
struct torture_context *test
)
{
TALLOC_CTX *tmp_ctx = talloc_new(test);
const char *fixed_data =
"abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
"abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
"abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
"abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
"abcabcabcabcabcabcabcabc";
const uint8_t fixed_out[] = {
0xff, 0xff, 0xff, 0x1f, 0x61, 0x62, 0x63, 0x17,
0x00, 0x0f, 0xff, 0x26, 0x01};
ssize_t c_size;
uint8_t *out, *out2;
out = talloc_size(tmp_ctx, 2048);
memset(out, 0x42, talloc_get_size(out));
torture_comment(test, "lzxpress fixed compression\n");
c_size = lzxpress_compress((const uint8_t *)fixed_data,
strlen(fixed_data),
out,
talloc_get_size(out));
torture_assert_int_equal(test, c_size, sizeof(fixed_out),
"fixed lzxpress_compress size");
torture_assert_mem_equal(test, out, fixed_out, c_size,
"fixed lzxpress_compress data");
torture_comment(test, "lzxpress fixed decompression\n");
out2 = talloc_size(tmp_ctx, strlen(fixed_data));
c_size = lzxpress_decompress(out,
sizeof(fixed_out),
out2,
talloc_get_size(out2));
torture_comment(test, "out2: %.*s\n", (int)c_size, (char *)out2);
torture_assert_int_equal(test, c_size, strlen(fixed_data),
"fixed lzxpress_decompress size");
torture_assert_mem_equal(test, out2, fixed_data, c_size,
"fixed lzxpress_decompress data");
talloc_free(tmp_ctx);
return true;
}
/*
test lzxpress
*/
static bool test_lzxpress(struct torture_context *test)
{
TALLOC_CTX *tmp_ctx = talloc_new(test);
const char *fixed_data = "this is a test. and this is a test too";
const uint8_t fixed_out[] = {
0xff, 0x21, 0x00, 0x04, 0x74, 0x68, 0x69, 0x73,
0x20, 0x10, 0x00, 0x61, 0x20, 0x74, 0x65, 0x73,
0x74, 0x2E, 0x20, 0x61, 0x6E, 0x64, 0x20, 0x9F,
0x00, 0x04, 0x20, 0x74, 0x6F, 0x6F };
const uint8_t fixed_out_old_version[] = {
0x00, 0x20, 0x00, 0x04, 0x74, 0x68, 0x69, 0x73,
0x20, 0x10, 0x00, 0x61, 0x20, 0x74, 0x65, 0x73,
0x74, 0x2E, 0x20, 0x61, 0x6E, 0x64, 0x20, 0x9F,
0x00, 0x04, 0x20, 0x74, 0x6F, 0x6F, 0x00, 0x00,
0x00, 0x00 };
ssize_t c_size;
uint8_t *out, *out2, *out3;
out = talloc_size(tmp_ctx, 2048);
memset(out, 0x42, talloc_get_size(out));
torture_comment(test, "lzxpress fixed compression\n");
c_size = lzxpress_compress((const uint8_t *)fixed_data,
strlen(fixed_data),
out,
talloc_get_size(out));
torture_assert_int_equal(test, c_size, sizeof(fixed_out),
"fixed lzxpress_compress size");
torture_assert_mem_equal(test, out, fixed_out, c_size,
"fixed lzxpress_compress data");
torture_comment(test, "lzxpress fixed decompression\n");
out2 = talloc_size(tmp_ctx, strlen(fixed_data));
c_size = lzxpress_decompress(out,
sizeof(fixed_out),
out2,
talloc_get_size(out2));
torture_assert_int_equal(test, c_size, strlen(fixed_data),
"fixed lzxpress_decompress size");
torture_assert_mem_equal(test, out2, fixed_data, c_size,
"fixed lzxpress_decompress data");
torture_comment(test, "lzxpress fixed decompression (old data)\n");
out3 = talloc_size(tmp_ctx, strlen(fixed_data));
c_size = lzxpress_decompress(fixed_out_old_version,
sizeof(fixed_out_old_version),
out3,
talloc_get_size(out3));
torture_assert_int_equal(test, c_size, strlen(fixed_data),
"fixed lzxpress_decompress size");
torture_assert_mem_equal(test, out3, fixed_data, c_size,
"fixed lzxpress_decompress data");
talloc_free(tmp_ctx);
return true;
}
static bool test_lzxpress2(struct torture_context *test)
{
/*
* Use two matches, separated by a literal, and each with a length
* greater than 10, to test the use of nibble_index. Both length values
* (less ten) should be stored as adjacent nibbles to form the 0x21
* byte.
*/
TALLOC_CTX *tmp_ctx = talloc_new(test);
const char *fixed_data = "aaaaaaaaaaaabaaaaaaaaaaaa";
const uint8_t fixed_out[] = {
0xff, 0xff, 0xff, 0x5f, 0x61, 0x07, 0x00, 0x21,
0x62, 0x67, 0x00};
ssize_t c_size;
uint8_t *out, *out2;
out = talloc_size(tmp_ctx, 2048);
memset(out, 0x42, talloc_get_size(out));
torture_comment(test, "lzxpress fixed compression\n");
c_size = lzxpress_compress((const uint8_t *)fixed_data,
strlen(fixed_data),
out,
talloc_get_size(out));
torture_assert_int_equal(test, c_size, sizeof(fixed_out),
"fixed lzxpress_compress size");
torture_assert_mem_equal(test, out, fixed_out, c_size,
"fixed lzxpress_compress data");
torture_comment(test, "lzxpress fixed decompression\n");
out2 = talloc_size(tmp_ctx, strlen(fixed_data));
c_size = lzxpress_decompress(out,
sizeof(fixed_out),
out2,
talloc_get_size(out2));
torture_assert_int_equal(test, c_size, strlen(fixed_data),
"fixed lzxpress_decompress size");
torture_assert_mem_equal(test, out2, fixed_data, c_size,
"fixed lzxpress_decompress data");
talloc_free(tmp_ctx);
return true;
}
static bool test_lzxpress3(struct torture_context *test)
{
/*
* Use a series of 31 literals, followed by a single minimum-length
* match (and a terminating literal), to test setting indic_pos when the
* 32-bit flags value overflows after a match.
*/
TALLOC_CTX *tmp_ctx = talloc_new(test);
const char *fixed_data = "abcdefghijklmnopqrstuvwxyz01234abca";
const uint8_t fixed_out[] = {
0x01, 0x00, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64,
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31,
0x32, 0x33, 0x34, 0xf0, 0x00, 0xff, 0xff, 0xff,
0x7f, 0x61};
ssize_t c_size;
uint8_t *out, *out2;
out = talloc_size(tmp_ctx, 2048);
memset(out, 0x42, talloc_get_size(out));
torture_comment(test, "lzxpress fixed compression\n");
c_size = lzxpress_compress((const uint8_t *)fixed_data,
strlen(fixed_data),
out,
talloc_get_size(out));
torture_assert_int_equal(test, c_size, sizeof(fixed_out),
"fixed lzxpress_compress size");
torture_assert_mem_equal(test, out, fixed_out, c_size,
"fixed lzxpress_compress data");
torture_comment(test, "lzxpress fixed decompression\n");
out2 = talloc_size(tmp_ctx, strlen(fixed_data));
c_size = lzxpress_decompress(out,
sizeof(fixed_out),
out2,
talloc_get_size(out2));
torture_assert_int_equal(test, c_size, strlen(fixed_data),
"fixed lzxpress_decompress size");
torture_assert_mem_equal(test, out2, fixed_data, c_size,
"fixed lzxpress_decompress data");
talloc_free(tmp_ctx);
return true;
}
static bool test_lzxpress4(struct torture_context *test)
{
/*
* Use a series of 31 literals, followed by a single minimum-length
* match, to test that the final set of 32-bit flags is written
* correctly when it is empty.
*/
TALLOC_CTX *tmp_ctx = talloc_new(test);
const char *fixed_data = "abcdefghijklmnopqrstuvwxyz01234abc";
const uint8_t fixed_out[] = {
0x01, 0x00, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64,
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31,
0x32, 0x33, 0x34, 0xf0, 0x00, 0xff, 0xff, 0xff,
0xff};
ssize_t c_size;
uint8_t *out, *out2;
out = talloc_size(tmp_ctx, 2048);
memset(out, 0x42, talloc_get_size(out));
torture_comment(test, "lzxpress fixed compression\n");
c_size = lzxpress_compress((const uint8_t *)fixed_data,
strlen(fixed_data),
out,
talloc_get_size(out));
torture_assert_int_equal(test, c_size, sizeof(fixed_out),
"fixed lzxpress_compress size");
torture_assert_mem_equal(test, out, fixed_out, c_size,
"fixed lzxpress_compress data");
torture_comment(test, "lzxpress fixed decompression\n");
out2 = talloc_size(tmp_ctx, strlen(fixed_data));
c_size = lzxpress_decompress(out,
sizeof(fixed_out),
out2,
talloc_get_size(out2));
torture_assert_int_equal(test, c_size, strlen(fixed_data),
"fixed lzxpress_decompress size");
torture_assert_mem_equal(test, out2, fixed_data, c_size,
"fixed lzxpress_decompress data");
talloc_free(tmp_ctx);
return true;
}
static bool test_lzxpress_many_zeros(struct torture_context *test)
{
/*
* Repeated values (zero is convenient but not special) will lead to
* very long substring searches in compression, which can be very slow
* if we're not careful.
*
* This test makes a very loose assertion about how long it should
* take to compress a million zeros.
*
* Wall clock time *should* be < 0.1 seconds with the fix and around a
* minute without it. We try for CLOCK_THREAD_CPUTIME_ID which should
* filter out some noise on the machine, and set the threshold at 5
* seconds.
*/
TALLOC_CTX *tmp_ctx = talloc_new(test);
const size_t N_ZEROS = 1000000;
const uint8_t *zeros = talloc_zero_size(tmp_ctx, N_ZEROS);
const ssize_t expected_c_size = 93;
ssize_t c_size;
uint8_t *comp, *decomp;
static struct timespec t_start, t_end;
uint64_t elapsed_ns;
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t_start) != 0) {
if (clock_gettime(CUSTOM_CLOCK_MONOTONIC, &t_start) != 0) {
clock_gettime(CLOCK_REALTIME, &t_start);
}
}
comp = talloc_zero_size(tmp_ctx, 2048);
c_size = lzxpress_compress(zeros,
N_ZEROS,
comp,
talloc_get_size(comp));
torture_assert_int_equal(test, c_size, expected_c_size,
"fixed lzxpress_compress size");
decomp = talloc_size(tmp_ctx, N_ZEROS * 2);
c_size = lzxpress_decompress(comp,
c_size,
decomp,
N_ZEROS * 2);
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t_end) != 0) {
if (clock_gettime(CUSTOM_CLOCK_MONOTONIC, &t_end) != 0) {
clock_gettime(CLOCK_REALTIME, &t_end);
}
}
elapsed_ns = (
(t_end.tv_sec - t_start.tv_sec) * 1000U * 1000U * 1000U) +
(t_end.tv_nsec - t_start.tv_nsec);
torture_comment(test, "round-trip time: %"PRIu64" ns\n", elapsed_ns);
torture_assert(test, elapsed_ns < 3 * 1000U * 1000U * 1000U,
"million zeros round trip tool > 3 seconds");
torture_assert_mem_equal(test, decomp, zeros, N_ZEROS,
"fixed lzxpress_decompress data");
talloc_free(tmp_ctx);
return true;
}
static bool test_lzxpress_round_trip(struct torture_context *test)
{
/*
* Examples found using via fuzzing.
*/
TALLOC_CTX *tmp_ctx = talloc_new(test);
size_t i;
struct b64_pair {
const char *uncompressed;
const char *compressed;
} pairs[] = {
{ /* this results in a trailing flags block */
"AAICAmq/EKdP785YU2Ddh7d4vUtdlQyLeHV09LHpUBw=",
"AAAAAAACAgJqvxCnT+/OWFNg3Ye3eL1LXZUMi3h1dPSx6VAc/////w==",
},
{ /* empty string compresses to empty string */
"", ""
},
};
const size_t alloc_size = 1000;
uint8_t *data = talloc_array(tmp_ctx, uint8_t, alloc_size);
for (i = 0; i < ARRAY_SIZE(pairs); i++) {
ssize_t len;
DATA_BLOB uncomp = base64_decode_data_blob_talloc(
tmp_ctx,
pairs[i].uncompressed);
DATA_BLOB comp = base64_decode_data_blob_talloc(
tmp_ctx,
pairs[i].compressed);
len = lzxpress_compress(uncomp.data,
uncomp.length,
data,
alloc_size);
torture_assert_int_equal(test, len, comp.length,
"lzexpress compression size");
torture_assert_mem_equal(test, comp.data, data, len,
"lzxpress compression data");
len = lzxpress_decompress(comp.data,
comp.length,
data,
alloc_size);
torture_assert_int_equal(test, len, uncomp.length,
"lzexpress decompression size");
torture_assert_mem_equal(test, uncomp.data, data, len,
"lzxpress decompression data");
}
talloc_free(tmp_ctx);
return true;
}
struct torture_suite *torture_local_compression(TALLOC_CTX *mem_ctx)
{
struct torture_suite *suite = torture_suite_create(mem_ctx, "compression");
torture_suite_add_simple_test(suite, "lzxpress", test_lzxpress);
torture_suite_add_simple_test(suite, "lzxpress_msft_data1", test_msft_data1);
torture_suite_add_simple_test(suite, "lzxpress_msft_data2", test_msft_data2);
torture_suite_add_simple_test(suite, "lzxpress2", test_lzxpress2);
torture_suite_add_simple_test(suite, "lzxpress3", test_lzxpress3);
torture_suite_add_simple_test(suite, "lzxpress4", test_lzxpress4);
torture_suite_add_simple_test(suite, "lzxpress_many_zeros",
test_lzxpress_many_zeros);
torture_suite_add_simple_test(suite, "lzxpress_round_trip",
test_lzxpress_round_trip);
return suite;
}
|