blob: 4448f23cb6b8130c0da02b526897281b1bd60e8b [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 Colletd7883a22016-08-12 16:48:02 +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++);
193 { size_t const s = ZSTD_sizeofCStream(zc);
194 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 Collet53e17fb2016-08-17 01:39:22 +0200215 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200216 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 +0200217 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
218 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200219 DISPLAYLEVEL(4, "OK \n");
220
221 /* check regenerated data is byte exact */
222 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
223 { size_t i;
224 for (i=0; i<CNBufferSize; i++) {
225 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
226 } }
227 DISPLAYLEVEL(4, "OK \n");
228
Yann Colletcb327632016-08-23 00:30:31 +0200229 DISPLAYLEVEL(4, "test%3i : check DStream size : ", testNb++);
230 { size_t const s = ZSTD_sizeofDStream(zd);
231 if (ZSTD_isError(s)) goto _output_error;
232 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
233 }
234
Yann Colletd7883a22016-08-12 16:48:02 +0200235 /* Byte-by-byte decompression test */
236 DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++);
237 { size_t r = 1;
238 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200239 inBuff.src = compressedBuffer;
240 outBuff.dst = decodedBuffer;
241 inBuff.pos = 0;
242 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200243 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200244 inBuff.size = inBuff.pos + 1;
245 outBuff.size = outBuff.pos + 1;
246 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200247 if (ZSTD_isError(r)) goto _output_error;
248 }
249 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
250 r=1;
251 while (r) { /* normal frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200252 inBuff.size = inBuff.pos + 1;
253 outBuff.size = outBuff.pos + 1;
254 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200255 if (ZSTD_isError(r)) goto _output_error;
256 }
257 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200258 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
259 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200260 DISPLAYLEVEL(4, "OK \n");
261
262 /* check regenerated data is byte exact */
263 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
264 { size_t i;
265 for (i=0; i<CNBufferSize; i++) {
266 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
267 } }
268 DISPLAYLEVEL(4, "OK \n");
269
270_end:
271 ZSTD_freeCStream(zc);
272 ZSTD_freeDStream(zd);
273 free(CNBuffer);
274 free(compressedBuffer);
275 free(decodedBuffer);
276 return testResult;
277
278_output_error:
279 testResult = 1;
280 DISPLAY("Error detected in Unit tests ! \n");
281 goto _end;
282}
283
284
285static size_t findDiff(const void* buf1, const void* buf2, size_t max)
286{
287 const BYTE* b1 = (const BYTE*)buf1;
288 const BYTE* b2 = (const BYTE*)buf2;
289 size_t u;
290 for (u=0; u<max; u++) {
291 if (b1[u] != b2[u]) break;
292 }
293 return u;
294}
295
296static size_t FUZ_rLogLength(U32* seed, U32 logLength)
297{
298 size_t const lengthMask = ((size_t)1 << logLength) - 1;
299 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
300}
301
302static size_t FUZ_randomLength(U32* seed, U32 maxLog)
303{
304 U32 const logLength = FUZ_rand(seed) % maxLog;
305 return FUZ_rLogLength(seed, logLength);
306}
307
308#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
309
310#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
311 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
312
313static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
314{
315 static const U32 maxSrcLog = 24;
316 static const U32 maxSampleLog = 19;
317 BYTE* cNoiseBuffer[5];
318 size_t srcBufferSize = (size_t)1<<maxSrcLog;
319 BYTE* copyBuffer;
320 size_t copyBufferSize= srcBufferSize + (1<<maxSampleLog);
321 BYTE* cBuffer;
322 size_t cBufferSize = ZSTD_compressBound(srcBufferSize);
323 BYTE* dstBuffer;
324 size_t dstBufferSize = srcBufferSize;
325 U32 result = 0;
326 U32 testNb = 0;
327 U32 coreSeed = seed;
328 ZSTD_CStream* zc;
329 ZSTD_DStream* zd;
330 U32 startTime = FUZ_GetMilliStart();
331
332 /* allocations */
333 zc = ZSTD_createCStream();
334 zd = ZSTD_createDStream();
335 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
336 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
337 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
338 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
339 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
340 copyBuffer= (BYTE*)malloc (copyBufferSize);
341 dstBuffer = (BYTE*)malloc (dstBufferSize);
342 cBuffer = (BYTE*)malloc (cBufferSize);
343 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
344 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd,
345 "Not enough memory, fuzzer tests cancelled");
346
347 /* Create initial samples */
348 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
349 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
350 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
351 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
352 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
353 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
354
355 /* catch up testNb */
356 for (testNb=1; testNb < startTest; testNb++)
357 FUZ_rand(&coreSeed);
358
359 /* test loop */
360 for ( ; (testNb <= nbTests) || (FUZ_GetMilliSpan(startTime) < g_testTime) ; testNb++ ) {
361 U32 lseed;
362 const BYTE* srcBuffer;
363 const BYTE* dict;
364 size_t maxTestSize, dictSize;
Yann Collet53e17fb2016-08-17 01:39:22 +0200365 size_t cSize, totalTestSize, totalGenSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200366 U32 n, nbChunks;
367 XXH64_state_t xxhState;
368 U64 crcOrig;
369
370 /* init */
371 DISPLAYUPDATE(2, "\r%6u", testNb);
372 if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests);
373 FUZ_rand(&coreSeed);
374 lseed = coreSeed ^ prime1;
375
376 /* states full reset (unsynchronized) */
377 /* some issues only happen when reusing states in a specific sequence of parameters */
378 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); }
379 if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZSTD_freeDStream(zd); zd = ZSTD_createDStream(); }
380
381 /* srcBuffer selection [0-4] */
382 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
383 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
384 else {
385 buffNb >>= 3;
386 if (buffNb & 7) {
387 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
388 buffNb = tnb[buffNb >> 3];
389 } else {
390 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
391 buffNb = tnb[buffNb >> 3];
392 } }
393 srcBuffer = cNoiseBuffer[buffNb];
394 }
395
396 /* compression init */
397 { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
398 U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
399 maxTestSize = FUZ_rLogLength(&lseed, testLog);
400 dictSize = (FUZ_rand(&lseed)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
401 /* random dictionary selection */
402 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
403 dict = srcBuffer + dictStart;
404 }
405 { ZSTD_parameters params = ZSTD_getParams(cLevel, 0, dictSize);
406 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
407 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
408 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, 0);
409 CHECK (ZSTD_isError(initError),"init error : %s", ZSTD_getErrorName(initError));
410 } } }
411
412 /* multi-segments compression test */
413 XXH64_reset(&xxhState, 0);
414 nbChunks = (FUZ_rand(&lseed) & 127) + 2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200415 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Colletd7883a22016-08-12 16:48:02 +0200416 for (n=0, cSize=0, totalTestSize=0 ; (n<nbChunks) && (totalTestSize < maxTestSize) ; n++) {
417 /* compress random chunk into random size dst buffer */
Yann Collet53e17fb2016-08-17 01:39:22 +0200418 { size_t const readChunkSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Colletd7883a22016-08-12 16:48:02 +0200419 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - readChunkSize);
420 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
421 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200422 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, readChunkSize, 0 };
423 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200424
Yann Collet53e17fb2016-08-17 01:39:22 +0200425 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200426 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200427
Yann Collet53e17fb2016-08-17 01:39:22 +0200428 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
429 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
430 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200431 }
432
433 /* random flush operation, to mess around */
434 if ((FUZ_rand(&lseed) & 15) == 0) {
435 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200436 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
437 outBuff.size = outBuff.pos + adjustedDstSize;
438 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200439 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
440 } } }
441
442 /* final frame epilogue */
443 { size_t remainingToFlush = (size_t)(-1);
444 while (remainingToFlush) {
445 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
446 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
447 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200448 outBuff.size = outBuff.pos + adjustedDstSize;
449 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200450 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
451 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
452 } }
453 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200454 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200455 }
456
457 /* multi - fragments decompression test */
458 ZSTD_initDStream_usingDict(zd, dict, dictSize);
459 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200460 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
461 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
462 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200463 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
464 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
465 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200466 inBuff.size = inBuff.pos + readCSrcSize;
467 outBuff.size = inBuff.pos + dstBuffSize;
468 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200469 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200470 }
471 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200472 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
473 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200474 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
475 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
476 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
477 } }
478
479 /*===== noisy/erroneous src decompression test =====*/
480
481 /* add some noise */
482 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
483 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
484 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
485 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
486 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
487 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
488 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
489 } }
490
491 /* try decompression on noisy data */
492 ZSTD_initDStream(zd);
Yann Collet53e17fb2016-08-17 01:39:22 +0200493 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
494 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
495 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200496 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
497 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200498 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
499 outBuff.size = outBuff.pos + adjustedDstSize;
500 inBuff.size = inBuff.pos + randomCSrcSize;
501 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200502 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
503 } } } }
504 DISPLAY("\r%u fuzzer tests completed \n", testNb);
505
506_cleanup:
507 ZSTD_freeCStream(zc);
508 ZSTD_freeDStream(zd);
509 free(cNoiseBuffer[0]);
510 free(cNoiseBuffer[1]);
511 free(cNoiseBuffer[2]);
512 free(cNoiseBuffer[3]);
513 free(cNoiseBuffer[4]);
514 free(copyBuffer);
515 free(cBuffer);
516 free(dstBuffer);
517 return result;
518
519_output_error:
520 result = 1;
521 goto _cleanup;
522}
523
524
525/*-*******************************************************
526* Command line
527*********************************************************/
528int FUZ_usage(const char* programName)
529{
530 DISPLAY( "Usage :\n");
531 DISPLAY( " %s [args]\n", programName);
532 DISPLAY( "\n");
533 DISPLAY( "Arguments :\n");
534 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
535 DISPLAY( " -s# : Select seed (default:prompt user)\n");
536 DISPLAY( " -t# : Select starting test number (default:0)\n");
537 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
538 DISPLAY( " -v : verbose\n");
539 DISPLAY( " -p : pause at the end\n");
540 DISPLAY( " -h : display help and exit\n");
541 return 0;
542}
543
544
545int main(int argc, const char** argv)
546{
547 U32 seed=0;
548 int seedset=0;
549 int argNb;
550 int nbTests = nbTestsDefault;
551 int testNb = 0;
552 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
553 int result=0;
554 U32 mainPause = 0;
555 const char* programName = argv[0];
556 ZSTD_customMem customMem = { allocFunction, freeFunction, NULL };
557 ZSTD_customMem customNULL = { NULL, NULL, NULL };
558
559 /* Check command line */
560 for(argNb=1; argNb<argc; argNb++) {
561 const char* argument = argv[argNb];
562 if(!argument) continue; /* Protection if argument empty */
563
564 /* Parsing commands. Aggregated commands are allowed */
565 if (argument[0]=='-') {
566 argument++;
567
568 while (*argument!=0) {
569 switch(*argument)
570 {
571 case 'h':
572 return FUZ_usage(programName);
573 case 'v':
574 argument++;
575 g_displayLevel=4;
576 break;
577 case 'q':
578 argument++;
579 g_displayLevel--;
580 break;
581 case 'p': /* pause at the end */
582 argument++;
583 mainPause = 1;
584 break;
585
586 case 'i':
587 argument++;
588 nbTests=0; g_testTime=0;
589 while ((*argument>='0') && (*argument<='9')) {
590 nbTests *= 10;
591 nbTests += *argument - '0';
592 argument++;
593 }
594 break;
595
596 case 'T':
597 argument++;
598 nbTests=0; g_testTime=0;
599 while ((*argument>='0') && (*argument<='9')) {
600 g_testTime *= 10;
601 g_testTime += *argument - '0';
602 argument++;
603 }
604 if (*argument=='m') g_testTime *=60, argument++;
605 if (*argument=='n') argument++;
606 g_testTime *= 1000;
607 break;
608
609 case 's':
610 argument++;
611 seed=0;
612 seedset=1;
613 while ((*argument>='0') && (*argument<='9')) {
614 seed *= 10;
615 seed += *argument - '0';
616 argument++;
617 }
618 break;
619
620 case 't':
621 argument++;
622 testNb=0;
623 while ((*argument>='0') && (*argument<='9')) {
624 testNb *= 10;
625 testNb += *argument - '0';
626 argument++;
627 }
628 break;
629
630 case 'P': /* compressibility % */
631 argument++;
632 proba=0;
633 while ((*argument>='0') && (*argument<='9')) {
634 proba *= 10;
635 proba += *argument - '0';
636 argument++;
637 }
638 if (proba<0) proba=0;
639 if (proba>100) proba=100;
640 break;
641
642 default:
643 return FUZ_usage(programName);
644 }
645 } } } /* for(argNb=1; argNb<argc; argNb++) */
646
647 /* Get Seed */
648 DISPLAY("Starting zstd_buffered tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
649
650 if (!seedset) seed = FUZ_GetMilliStart() % 10000;
651 DISPLAY("Seed = %u\n", seed);
652 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
653
654 if (nbTests<=0) nbTests=1;
655
656 if (testNb==0) {
657 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
658 if (!result) {
659 DISPLAYLEVEL(4, "Unit tests using customMem :\n")
660 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
661 } }
662
663 if (!result)
664 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
665
666 if (mainPause) {
667 int unused;
668 DISPLAY("Press Enter \n");
669 unused = getchar();
670 (void)unused;
671 }
672 return result;
673}