blob: dd00864d1a9ea86deed15ebdbf37256cf1db39f4 [file] [log] [blame]
Yann Colletd7883a22016-08-12 16:48:02 +02001/*
2 Fuzzer test tool for zstd streaming API
3 Copyright (C) Yann Collet 2016
4
5 GPL v2 License
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 You can contact the author at :
22 - ZSTD homepage : https://www.zstd.net/
23*/
24
25/*-************************************
26* Compiler specific
27**************************************/
28#ifdef _MSC_VER /* Visual Studio */
29# define _CRT_SECURE_NO_WARNINGS /* fgets */
30# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
31# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */
32#endif
33
34
35/*-************************************
36* Includes
37**************************************/
38#include <stdlib.h> /* free */
39#include <stdio.h> /* fgets, sscanf */
40#include <sys/timeb.h> /* timeb */
41#include <string.h> /* strcmp */
42#include "mem.h"
43#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel */
44#include "zstd.h" /* ZSTD_compressBound */
45#include "datagen.h" /* RDG_genBuffer */
46#define XXH_STATIC_LINKING_ONLY
47#include "xxhash.h" /* XXH64_* */
48
49
50/*-************************************
51* Constants
52**************************************/
53#define KB *(1U<<10)
54#define MB *(1U<<20)
55#define GB *(1U<<30)
56
57static const U32 nbTestsDefault = 10000;
58#define COMPRESSIBLE_NOISE_LENGTH (10 MB)
59#define FUZ_COMPRESSIBILITY_DEFAULT 50
60static const U32 prime1 = 2654435761U;
61static const U32 prime2 = 2246822519U;
62
63
Yann Colletd7883a22016-08-12 16:48:02 +020064/*-************************************
65* Display Macros
66**************************************/
67#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
68#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
69static U32 g_displayLevel = 2;
70
71#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
72 if ((FUZ_GetMilliSpan(g_displayTime) > g_refreshRate) || (g_displayLevel>=4)) \
73 { g_displayTime = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); \
74 if (g_displayLevel>=4) fflush(stdout); } }
75static const U32 g_refreshRate = 150;
76static U32 g_displayTime = 0;
77
78static U32 g_testTime = 0;
79
80
81/*-*******************************************************
82* Fuzzer functions
83*********************************************************/
84#define MAX(a,b) ((a)>(b)?(a):(b))
85
86static U32 FUZ_GetMilliStart(void)
87{
88 struct timeb tb;
89 U32 nCount;
90 ftime( &tb );
91 nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm);
92 return nCount;
93}
94
Yann Colletd7883a22016-08-12 16:48:02 +020095static U32 FUZ_GetMilliSpan(U32 nTimeStart)
96{
97 U32 const nCurrent = FUZ_GetMilliStart();
98 U32 nSpan = nCurrent - nTimeStart;
99 if (nTimeStart > nCurrent)
100 nSpan += 0x100000 * 1000;
101 return nSpan;
102}
103
104/*! FUZ_rand() :
105 @return : a 27 bits random value, from a 32-bits `seed`.
106 `seed` is also modified */
107# define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
108unsigned int FUZ_rand(unsigned int* seedPtr)
109{
110 U32 rand32 = *seedPtr;
111 rand32 *= prime1;
112 rand32 += prime2;
113 rand32 = FUZ_rotl32(rand32, 13);
114 *seedPtr = rand32;
115 return rand32 >> 5;
116}
117
Yann Colletd7883a22016-08-12 16:48:02 +0200118/*
119static unsigned FUZ_highbit32(U32 v32)
120{
121 unsigned nbBits = 0;
122 if (v32==0) return 0;
123 for ( ; v32 ; v32>>=1) nbBits++;
124 return nbBits;
125}
126*/
127
128static void* allocFunction(void* opaque, size_t size)
129{
130 void* address = malloc(size);
131 (void)opaque;
132 return address;
133}
134
135static void freeFunction(void* opaque, void* address)
136{
137 (void)opaque;
138 free(address);
139}
140
Yann Colletcb327632016-08-23 00:30:31 +0200141
142/*======================================================
143* Basic Unit tests
144======================================================*/
145
Yann Colletd7883a22016-08-12 16:48:02 +0200146static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem)
147{
148 int testResult = 0;
149 size_t CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
150 void* CNBuffer = malloc(CNBufferSize);
151 size_t const skippableFrameSize = 11;
152 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
153 void* compressedBuffer = malloc(compressedBufferSize);
154 size_t const decodedBufferSize = CNBufferSize;
155 void* decodedBuffer = malloc(decodedBufferSize);
156 size_t cSize;
157 U32 testNb=0;
158 ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem);
159 ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem);
Yann Collet53e17fb2016-08-17 01:39:22 +0200160 ZSTD_inBuffer inBuff;
161 ZSTD_outBuffer outBuff;
Yann Colletd7883a22016-08-12 16:48:02 +0200162
163 /* Create compressible test buffer */
164 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
165 DISPLAY("Not enough memory, aborting\n");
166 goto _output_error;
167 }
168 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
169
170 /* generate skippable frame */
171 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
172 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
173 cSize = skippableFrameSize + 8;
174
175 /* Basic compression test */
176 DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
177 ZSTD_initCStream_usingDict(zc, CNBuffer, 128 KB, 1);
Yann Collet53e17fb2016-08-17 01:39:22 +0200178 outBuff.dst = (char*)(compressedBuffer)+cSize;
179 outBuff.size = compressedBufferSize;
180 outBuff.pos = 0;
181 inBuff.src = CNBuffer;
182 inBuff.size = CNBufferSize;
183 inBuff.pos = 0;
184 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200185 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200186 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
187 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200188 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200189 cSize += outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200190 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
191
Yann Colletcb327632016-08-23 00:30:31 +0200192 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200193 { size_t const s = ZSTD_sizeof_CStream(zc);
Yann Colletcb327632016-08-23 00:30:31 +0200194 if (ZSTD_isError(s)) goto _output_error;
195 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
196 }
197
Yann Colletd7883a22016-08-12 16:48:02 +0200198 /* skippable frame test */
199 DISPLAYLEVEL(4, "test%3i : decompress skippable frame : ", testNb++);
200 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200201 inBuff.src = compressedBuffer;
202 inBuff.size = cSize;
203 inBuff.pos = 0;
204 outBuff.dst = decodedBuffer;
205 outBuff.size = CNBufferSize;
206 outBuff.pos = 0;
207 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200208 if (r != 0) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200209 if (outBuff.pos != 0) goto _output_error; /* skippable frame len is 0 */
Yann Colletd7883a22016-08-12 16:48:02 +0200210 DISPLAYLEVEL(4, "OK \n");
211
212 /* Basic decompression test */
213 DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
214 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet17e482e2016-08-23 16:58:10 +0200215 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000000000); /* large limit */
216 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200217 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200218 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
Yann Collet53e17fb2016-08-17 01:39:22 +0200219 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
220 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200221 DISPLAYLEVEL(4, "OK \n");
222
223 /* check regenerated data is byte exact */
224 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
225 { size_t i;
226 for (i=0; i<CNBufferSize; i++) {
227 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
228 } }
229 DISPLAYLEVEL(4, "OK \n");
230
Yann Colletcb327632016-08-23 00:30:31 +0200231 DISPLAYLEVEL(4, "test%3i : check DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200232 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200233 if (ZSTD_isError(s)) goto _output_error;
234 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
235 }
236
Yann Colletd7883a22016-08-12 16:48:02 +0200237 /* Byte-by-byte decompression test */
238 DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++);
239 { size_t r = 1;
240 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200241 inBuff.src = compressedBuffer;
242 outBuff.dst = decodedBuffer;
243 inBuff.pos = 0;
244 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200245 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200246 inBuff.size = inBuff.pos + 1;
247 outBuff.size = outBuff.pos + 1;
248 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200249 if (ZSTD_isError(r)) goto _output_error;
250 }
251 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
252 r=1;
253 while (r) { /* normal frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200254 inBuff.size = inBuff.pos + 1;
255 outBuff.size = outBuff.pos + 1;
256 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200257 if (ZSTD_isError(r)) goto _output_error;
258 }
259 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200260 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
261 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200262 DISPLAYLEVEL(4, "OK \n");
263
264 /* check regenerated data is byte exact */
265 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
266 { size_t i;
267 for (i=0; i<CNBufferSize; i++) {
268 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
269 } }
270 DISPLAYLEVEL(4, "OK \n");
271
Yann Collet17e482e2016-08-23 16:58:10 +0200272 DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
273 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
274 if (!ZSTD_isError(r)) goto _output_error; }
275 DISPLAYLEVEL(4, "OK \n");
276
277 /* Memory restriction */
278 DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++);
279 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
280 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */
281 if (ZSTD_isError(r)) goto _output_error; }
282 inBuff.src = compressedBuffer;
283 inBuff.size = cSize;
284 inBuff.pos = 0;
285 outBuff.dst = decodedBuffer;
286 outBuff.size = CNBufferSize;
287 outBuff.pos = 0;
288 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
289 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
290 DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); }
291
292
Yann Colletd7883a22016-08-12 16:48:02 +0200293_end:
294 ZSTD_freeCStream(zc);
295 ZSTD_freeDStream(zd);
296 free(CNBuffer);
297 free(compressedBuffer);
298 free(decodedBuffer);
299 return testResult;
300
301_output_error:
302 testResult = 1;
303 DISPLAY("Error detected in Unit tests ! \n");
304 goto _end;
305}
306
307
308static size_t findDiff(const void* buf1, const void* buf2, size_t max)
309{
310 const BYTE* b1 = (const BYTE*)buf1;
311 const BYTE* b2 = (const BYTE*)buf2;
312 size_t u;
313 for (u=0; u<max; u++) {
314 if (b1[u] != b2[u]) break;
315 }
316 return u;
317}
318
319static size_t FUZ_rLogLength(U32* seed, U32 logLength)
320{
321 size_t const lengthMask = ((size_t)1 << logLength) - 1;
322 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
323}
324
325static size_t FUZ_randomLength(U32* seed, U32 maxLog)
326{
327 U32 const logLength = FUZ_rand(seed) % maxLog;
328 return FUZ_rLogLength(seed, logLength);
329}
330
331#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
332
333#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
334 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
335
336static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
337{
338 static const U32 maxSrcLog = 24;
339 static const U32 maxSampleLog = 19;
340 BYTE* cNoiseBuffer[5];
341 size_t srcBufferSize = (size_t)1<<maxSrcLog;
342 BYTE* copyBuffer;
343 size_t copyBufferSize= srcBufferSize + (1<<maxSampleLog);
344 BYTE* cBuffer;
345 size_t cBufferSize = ZSTD_compressBound(srcBufferSize);
346 BYTE* dstBuffer;
347 size_t dstBufferSize = srcBufferSize;
348 U32 result = 0;
349 U32 testNb = 0;
350 U32 coreSeed = seed;
351 ZSTD_CStream* zc;
352 ZSTD_DStream* zd;
353 U32 startTime = FUZ_GetMilliStart();
354
355 /* allocations */
356 zc = ZSTD_createCStream();
357 zd = ZSTD_createDStream();
358 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
359 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
360 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
361 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
362 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
363 copyBuffer= (BYTE*)malloc (copyBufferSize);
364 dstBuffer = (BYTE*)malloc (dstBufferSize);
365 cBuffer = (BYTE*)malloc (cBufferSize);
366 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
367 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd,
368 "Not enough memory, fuzzer tests cancelled");
369
370 /* Create initial samples */
371 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
372 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
373 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
374 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
375 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
376 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
377
378 /* catch up testNb */
379 for (testNb=1; testNb < startTest; testNb++)
380 FUZ_rand(&coreSeed);
381
382 /* test loop */
383 for ( ; (testNb <= nbTests) || (FUZ_GetMilliSpan(startTime) < g_testTime) ; testNb++ ) {
384 U32 lseed;
385 const BYTE* srcBuffer;
386 const BYTE* dict;
387 size_t maxTestSize, dictSize;
Yann Collet53e17fb2016-08-17 01:39:22 +0200388 size_t cSize, totalTestSize, totalGenSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200389 U32 n, nbChunks;
390 XXH64_state_t xxhState;
391 U64 crcOrig;
392
393 /* init */
394 DISPLAYUPDATE(2, "\r%6u", testNb);
395 if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests);
396 FUZ_rand(&coreSeed);
397 lseed = coreSeed ^ prime1;
398
399 /* states full reset (unsynchronized) */
400 /* some issues only happen when reusing states in a specific sequence of parameters */
401 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); }
402 if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZSTD_freeDStream(zd); zd = ZSTD_createDStream(); }
403
404 /* srcBuffer selection [0-4] */
405 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
406 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
407 else {
408 buffNb >>= 3;
409 if (buffNb & 7) {
410 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
411 buffNb = tnb[buffNb >> 3];
412 } else {
413 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
414 buffNb = tnb[buffNb >> 3];
415 } }
416 srcBuffer = cNoiseBuffer[buffNb];
417 }
418
419 /* compression init */
420 { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
421 U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
422 maxTestSize = FUZ_rLogLength(&lseed, testLog);
423 dictSize = (FUZ_rand(&lseed)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
424 /* random dictionary selection */
425 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
426 dict = srcBuffer + dictStart;
427 }
428 { ZSTD_parameters params = ZSTD_getParams(cLevel, 0, dictSize);
429 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
430 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
431 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, 0);
432 CHECK (ZSTD_isError(initError),"init error : %s", ZSTD_getErrorName(initError));
433 } } }
434
435 /* multi-segments compression test */
436 XXH64_reset(&xxhState, 0);
437 nbChunks = (FUZ_rand(&lseed) & 127) + 2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200438 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Colletd7883a22016-08-12 16:48:02 +0200439 for (n=0, cSize=0, totalTestSize=0 ; (n<nbChunks) && (totalTestSize < maxTestSize) ; n++) {
440 /* compress random chunk into random size dst buffer */
Yann Collet53e17fb2016-08-17 01:39:22 +0200441 { size_t const readChunkSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Colletd7883a22016-08-12 16:48:02 +0200442 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - readChunkSize);
443 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
444 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200445 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, readChunkSize, 0 };
446 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200447
Yann Collet53e17fb2016-08-17 01:39:22 +0200448 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200449 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200450
Yann Collet53e17fb2016-08-17 01:39:22 +0200451 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
452 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
453 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200454 }
455
456 /* random flush operation, to mess around */
457 if ((FUZ_rand(&lseed) & 15) == 0) {
458 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200459 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
460 outBuff.size = outBuff.pos + adjustedDstSize;
461 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200462 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
463 } } }
464
465 /* final frame epilogue */
466 { size_t remainingToFlush = (size_t)(-1);
467 while (remainingToFlush) {
468 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
469 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
470 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200471 outBuff.size = outBuff.pos + adjustedDstSize;
472 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200473 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
474 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
475 } }
476 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200477 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200478 }
479
480 /* multi - fragments decompression test */
481 ZSTD_initDStream_usingDict(zd, dict, dictSize);
482 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200483 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
484 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
485 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200486 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
487 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
488 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200489 inBuff.size = inBuff.pos + readCSrcSize;
490 outBuff.size = inBuff.pos + dstBuffSize;
491 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200492 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200493 }
494 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200495 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
496 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200497 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
498 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
499 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
500 } }
501
502 /*===== noisy/erroneous src decompression test =====*/
503
504 /* add some noise */
505 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
506 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
507 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
508 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
509 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
510 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
511 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
512 } }
513
514 /* try decompression on noisy data */
515 ZSTD_initDStream(zd);
Yann Collet53e17fb2016-08-17 01:39:22 +0200516 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
517 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
518 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200519 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
520 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200521 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
522 outBuff.size = outBuff.pos + adjustedDstSize;
523 inBuff.size = inBuff.pos + randomCSrcSize;
524 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200525 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
526 } } } }
527 DISPLAY("\r%u fuzzer tests completed \n", testNb);
528
529_cleanup:
530 ZSTD_freeCStream(zc);
531 ZSTD_freeDStream(zd);
532 free(cNoiseBuffer[0]);
533 free(cNoiseBuffer[1]);
534 free(cNoiseBuffer[2]);
535 free(cNoiseBuffer[3]);
536 free(cNoiseBuffer[4]);
537 free(copyBuffer);
538 free(cBuffer);
539 free(dstBuffer);
540 return result;
541
542_output_error:
543 result = 1;
544 goto _cleanup;
545}
546
547
548/*-*******************************************************
549* Command line
550*********************************************************/
551int FUZ_usage(const char* programName)
552{
553 DISPLAY( "Usage :\n");
554 DISPLAY( " %s [args]\n", programName);
555 DISPLAY( "\n");
556 DISPLAY( "Arguments :\n");
557 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
558 DISPLAY( " -s# : Select seed (default:prompt user)\n");
559 DISPLAY( " -t# : Select starting test number (default:0)\n");
560 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
561 DISPLAY( " -v : verbose\n");
562 DISPLAY( " -p : pause at the end\n");
563 DISPLAY( " -h : display help and exit\n");
564 return 0;
565}
566
567
568int main(int argc, const char** argv)
569{
570 U32 seed=0;
571 int seedset=0;
572 int argNb;
573 int nbTests = nbTestsDefault;
574 int testNb = 0;
575 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
576 int result=0;
577 U32 mainPause = 0;
578 const char* programName = argv[0];
579 ZSTD_customMem customMem = { allocFunction, freeFunction, NULL };
580 ZSTD_customMem customNULL = { NULL, NULL, NULL };
581
582 /* Check command line */
583 for(argNb=1; argNb<argc; argNb++) {
584 const char* argument = argv[argNb];
585 if(!argument) continue; /* Protection if argument empty */
586
587 /* Parsing commands. Aggregated commands are allowed */
588 if (argument[0]=='-') {
589 argument++;
590
591 while (*argument!=0) {
592 switch(*argument)
593 {
594 case 'h':
595 return FUZ_usage(programName);
596 case 'v':
597 argument++;
598 g_displayLevel=4;
599 break;
600 case 'q':
601 argument++;
602 g_displayLevel--;
603 break;
604 case 'p': /* pause at the end */
605 argument++;
606 mainPause = 1;
607 break;
608
609 case 'i':
610 argument++;
611 nbTests=0; g_testTime=0;
612 while ((*argument>='0') && (*argument<='9')) {
613 nbTests *= 10;
614 nbTests += *argument - '0';
615 argument++;
616 }
617 break;
618
619 case 'T':
620 argument++;
621 nbTests=0; g_testTime=0;
622 while ((*argument>='0') && (*argument<='9')) {
623 g_testTime *= 10;
624 g_testTime += *argument - '0';
625 argument++;
626 }
627 if (*argument=='m') g_testTime *=60, argument++;
628 if (*argument=='n') argument++;
629 g_testTime *= 1000;
630 break;
631
632 case 's':
633 argument++;
634 seed=0;
635 seedset=1;
636 while ((*argument>='0') && (*argument<='9')) {
637 seed *= 10;
638 seed += *argument - '0';
639 argument++;
640 }
641 break;
642
643 case 't':
644 argument++;
645 testNb=0;
646 while ((*argument>='0') && (*argument<='9')) {
647 testNb *= 10;
648 testNb += *argument - '0';
649 argument++;
650 }
651 break;
652
653 case 'P': /* compressibility % */
654 argument++;
655 proba=0;
656 while ((*argument>='0') && (*argument<='9')) {
657 proba *= 10;
658 proba += *argument - '0';
659 argument++;
660 }
661 if (proba<0) proba=0;
662 if (proba>100) proba=100;
663 break;
664
665 default:
666 return FUZ_usage(programName);
667 }
668 } } } /* for(argNb=1; argNb<argc; argNb++) */
669
670 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +0200671 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +0200672
673 if (!seedset) seed = FUZ_GetMilliStart() % 10000;
674 DISPLAY("Seed = %u\n", seed);
675 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
676
677 if (nbTests<=0) nbTests=1;
678
679 if (testNb==0) {
680 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
681 if (!result) {
682 DISPLAYLEVEL(4, "Unit tests using customMem :\n")
683 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
684 } }
685
686 if (!result)
687 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
688
689 if (mainPause) {
690 int unused;
691 DISPLAY("Press Enter \n");
692 unused = getchar();
693 (void)unused;
694 }
695 return result;
696}