| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | Fuzzer test tool for zstd |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 3 | Copyright (C) Yann Collet 2014-2016 |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 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 : |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 22 | - ZSTD homepage : http://www.zstd.net |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 23 | */ |
| 24 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 25 | /*-************************************ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 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 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 34 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 35 | /*-************************************ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 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 "zstd_static.h" |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 43 | #include "datagen.h" /* RDG_genBuffer */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 44 | #include "xxhash.h" /* XXH64 */ |
| Yann Collet | 353c5d2 | 2015-10-21 14:39:26 +0100 | [diff] [blame] | 45 | #include "mem.h" |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 46 | |
| 47 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 48 | /*-************************************ |
| 49 | * Constants |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 50 | **************************************/ |
| 51 | #ifndef ZSTD_VERSION |
| 52 | # define ZSTD_VERSION "" |
| 53 | #endif |
| 54 | |
| 55 | #define KB *(1U<<10) |
| 56 | #define MB *(1U<<20) |
| 57 | #define GB *(1U<<30) |
| 58 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 59 | static const size_t COMPRESSIBLE_NOISE_LENGTH = 10 MB; /* capital, used to be a macro */ |
| 60 | static const U32 FUZ_compressibility_default = 50; |
| Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 61 | static const U32 nbTestsDefault = 30000; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 62 | |
| 63 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 64 | /*-************************************ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 65 | * Display Macros |
| 66 | **************************************/ |
| 67 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 68 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 69 | static U32 g_displayLevel = 2; |
| 70 | |
| 71 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 72 | if ((FUZ_GetMilliSpan(g_displayTime) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 73 | { g_displayTime = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); \ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 74 | if (g_displayLevel>=4) fflush(stdout); } } |
| 75 | static const U32 g_refreshRate = 150; |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 76 | static U32 g_displayTime = 0; |
| 77 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 78 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 79 | /*-******************************************************* |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 80 | * Fuzzer functions |
| 81 | *********************************************************/ |
| Yann Collet | e93add0 | 2016-02-15 17:44:14 +0100 | [diff] [blame] | 82 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
| Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 83 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
| 84 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 85 | static U32 FUZ_GetMilliStart(void) |
| 86 | { |
| 87 | struct timeb tb; |
| 88 | U32 nCount; |
| 89 | ftime( &tb ); |
| 90 | nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); |
| 91 | return nCount; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | static U32 FUZ_GetMilliSpan(U32 nTimeStart) |
| 96 | { |
| 97 | U32 nCurrent = FUZ_GetMilliStart(); |
| 98 | U32 nSpan = nCurrent - nTimeStart; |
| 99 | if (nTimeStart > nCurrent) |
| 100 | nSpan += 0x100000 * 1000; |
| 101 | return nSpan; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 106 | unsigned int FUZ_rand(unsigned int* src) |
| 107 | { |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 108 | static const U32 prime1 = 2654435761U; |
| 109 | static const U32 prime2 = 2246822519U; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 110 | U32 rand32 = *src; |
| 111 | rand32 *= prime1; |
| 112 | rand32 += prime2; |
| 113 | rand32 = FUZ_rotl32(rand32, 13); |
| 114 | *src = rand32; |
| 115 | return rand32 >> 5; |
| 116 | } |
| 117 | |
| 118 | |
| Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 119 | static unsigned FUZ_highbit32(U32 v32) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 120 | { |
| 121 | unsigned nbBits = 0; |
| 122 | if (v32==0) return 0; |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 123 | while (v32) { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 124 | v32 >>= 1; |
| 125 | nbBits ++; |
| 126 | } |
| 127 | return nbBits; |
| 128 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 129 | |
| 130 | |
| 131 | static int basicUnitTests(U32 seed, double compressibility) |
| 132 | { |
| 133 | int testResult = 0; |
| 134 | void* CNBuffer; |
| 135 | void* compressedBuffer; |
| 136 | void* decodedBuffer; |
| 137 | U32 randState = seed; |
| 138 | size_t result, cSize; |
| 139 | U32 testNb=0; |
| 140 | |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 141 | /* Create compressible test buffer */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 142 | CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); |
| 143 | compressedBuffer = malloc(ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH)); |
| 144 | decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 145 | if (!CNBuffer || !compressedBuffer || !decodedBuffer) { |
| Yann Collet | 94f998b | 2015-07-04 23:10:40 -0800 | [diff] [blame] | 146 | DISPLAY("Not enough memory, aborting\n"); |
| 147 | testResult = 1; |
| 148 | goto _end; |
| 149 | } |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 150 | RDG_genBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, 0., randState); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 151 | |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 152 | /* Basic tests */ |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 153 | DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, (U32)COMPRESSIBLE_NOISE_LENGTH); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 154 | result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 155 | if (ZSTD_isError(result)) goto _output_error; |
| 156 | cSize = result; |
| 157 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 158 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 159 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)COMPRESSIBLE_NOISE_LENGTH); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 160 | result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize); |
| 161 | if (ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 4ec2998 | 2016-03-04 19:09:28 +0100 | [diff] [blame] | 162 | if (result != COMPRESSIBLE_NOISE_LENGTH) goto _output_error; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 163 | DISPLAYLEVEL(4, "OK \n"); |
| 164 | |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 165 | { size_t i; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 166 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 167 | for (i=0; i<COMPRESSIBLE_NOISE_LENGTH; i++) { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 168 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; |
| 169 | } |
| 170 | DISPLAYLEVEL(4, "OK \n"); |
| 171 | } |
| 172 | |
| 173 | DISPLAYLEVEL(4, "test%3i : decompress with 1 missing byte : ", testNb++); |
| 174 | result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize-1); |
| 175 | if (!ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 176 | if (result != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 177 | DISPLAYLEVEL(4, "OK \n"); |
| 178 | |
| 179 | DISPLAYLEVEL(4, "test%3i : decompress with 1 too much byte : ", testNb++); |
| 180 | result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize+1); |
| 181 | if (!ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 182 | if (result != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 183 | DISPLAYLEVEL(4, "OK \n"); |
| 184 | |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 185 | /* Dictionary and Duplication tests */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 186 | { ZSTD_CCtx* ctxOrig = ZSTD_createCCtx(); |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 187 | ZSTD_CCtx* ctxDuplicated = ZSTD_createCCtx(); |
| 188 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
| 189 | const size_t dictSize = 500; |
| 190 | size_t cSizeOrig; |
| 191 | |
| 192 | DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++); |
| Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 193 | result = ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2); |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 194 | if (ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 195 | result = ZSTD_copyCCtx(ctxDuplicated, ctxOrig); |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 196 | if (ZSTD_isError(result)) goto _output_error; |
| 197 | DISPLAYLEVEL(4, "OK \n"); |
| 198 | |
| 199 | DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++); |
| 200 | cSize = 0; |
| 201 | result = ZSTD_compressContinue(ctxOrig, compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), (const char*)CNBuffer + dictSize, COMPRESSIBLE_NOISE_LENGTH - dictSize); |
| 202 | if (ZSTD_isError(result)) goto _output_error; |
| 203 | cSize += result; |
| 204 | result = ZSTD_compressEnd(ctxOrig, (char*)compressedBuffer+cSize, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH)-cSize); |
| 205 | if (ZSTD_isError(result)) goto _output_error; |
| 206 | cSize += result; |
| 207 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 208 | |
| 209 | DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++); |
| 210 | result = ZSTD_decompress_usingDict(dctx, |
| 211 | decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, |
| 212 | compressedBuffer, cSize, |
| 213 | CNBuffer, dictSize); |
| 214 | if (ZSTD_isError(result)) goto _output_error; |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 215 | if (result != COMPRESSIBLE_NOISE_LENGTH - dictSize) goto _output_error; |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 216 | ZSTD_freeCCtx(ctxOrig); /* if ctxOrig is read, will produce segfault */ |
| 217 | DISPLAYLEVEL(4, "OK \n"); |
| 218 | |
| 219 | DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++); |
| 220 | cSizeOrig = cSize; |
| 221 | cSize = 0; |
| 222 | result = ZSTD_compressContinue(ctxDuplicated, compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), (const char*)CNBuffer + dictSize, COMPRESSIBLE_NOISE_LENGTH - dictSize); |
| 223 | if (ZSTD_isError(result)) goto _output_error; |
| 224 | cSize += result; |
| 225 | result = ZSTD_compressEnd(ctxDuplicated, (char*)compressedBuffer+cSize, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH)-cSize); |
| 226 | if (ZSTD_isError(result)) goto _output_error; |
| 227 | cSize += result; |
| 228 | if (cSize != cSizeOrig) goto _output_error; /* should be identical == have same size */ |
| 229 | ZSTD_freeCCtx(ctxDuplicated); |
| 230 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 231 | |
| 232 | DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++); |
| 233 | result = ZSTD_decompress_usingDict(dctx, |
| 234 | decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, |
| 235 | compressedBuffer, cSize, |
| 236 | CNBuffer, dictSize); |
| 237 | if (ZSTD_isError(result)) goto _output_error; |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 238 | if (result != COMPRESSIBLE_NOISE_LENGTH - dictSize) goto _output_error; |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 239 | ZSTD_freeDCtx(dctx); |
| 240 | DISPLAYLEVEL(4, "OK \n"); |
| 241 | } |
| 242 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 243 | /* Decompression defense tests */ |
| 244 | DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++); |
| 245 | result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, CNBuffer, 3); |
| 246 | if (!ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 247 | if (result != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 248 | DISPLAYLEVEL(4, "OK \n"); |
| 249 | |
| 250 | DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++); |
| 251 | ((char*)(CNBuffer))[0] = 1; |
| 252 | result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, CNBuffer, 4); |
| 253 | if (!ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 254 | DISPLAYLEVEL(4, "OK \n"); |
| 255 | |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 256 | /* block API tests */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 257 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 258 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| 259 | const size_t blockSize = 100 KB; |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 260 | const size_t dictSize = 16 KB; |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 261 | |
| 262 | /* basic block compression */ |
| 263 | DISPLAYLEVEL(4, "test%3i : Block compression test : ", testNb++); |
| 264 | result = ZSTD_compressBegin(cctx, 5); |
| 265 | if (ZSTD_isError(result)) goto _output_error; |
| 266 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize); |
| 267 | if (ZSTD_isError(cSize)) goto _output_error; |
| 268 | DISPLAYLEVEL(4, "OK \n"); |
| 269 | |
| 270 | DISPLAYLEVEL(4, "test%3i : Block decompression test : ", testNb++); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 271 | result = ZSTD_decompressBegin(dctx); |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 272 | if (ZSTD_isError(result)) goto _output_error; |
| 273 | result = ZSTD_decompressBlock(dctx, decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize); |
| 274 | if (ZSTD_isError(result)) goto _output_error; |
| 275 | if (result != blockSize) goto _output_error; |
| 276 | DISPLAYLEVEL(4, "OK \n"); |
| 277 | |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 278 | /* dictionary block compression */ |
| 279 | DISPLAYLEVEL(4, "test%3i : Dictionary Block compression test : ", testNb++); |
| Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 280 | result = ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5); |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 281 | if (ZSTD_isError(result)) goto _output_error; |
| 282 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize); |
| 283 | if (ZSTD_isError(cSize)) goto _output_error; |
| 284 | DISPLAYLEVEL(4, "OK \n"); |
| 285 | |
| 286 | DISPLAYLEVEL(4, "test%3i : Dictionary Block decompression test : ", testNb++); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 287 | result = ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize); |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 288 | if (ZSTD_isError(result)) goto _output_error; |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 289 | result = ZSTD_decompressBlock(dctx, decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize); |
| 290 | if (ZSTD_isError(result)) goto _output_error; |
| 291 | if (result != blockSize) goto _output_error; |
| 292 | DISPLAYLEVEL(4, "OK \n"); |
| 293 | |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 294 | ZSTD_freeCCtx(cctx); |
| 295 | ZSTD_freeDCtx(dctx); |
| 296 | } |
| 297 | |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 298 | /* long rle test */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 299 | { size_t sampleSize = 0; |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 300 | DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++); |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 301 | RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., randState); |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 302 | memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1); |
| 303 | sampleSize += 256 KB - 1; |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 304 | RDG_genBuffer((char*)CNBuffer+sampleSize, 96 KB, compressibility, 0., randState); |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 305 | sampleSize += 96 KB; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 306 | cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1); |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 307 | if (ZSTD_isError(cSize)) goto _output_error; |
| 308 | result = ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize); |
| 309 | if (ZSTD_isError(result)) goto _output_error; |
| 310 | if (result!=sampleSize) goto _output_error; |
| 311 | DISPLAYLEVEL(4, "OK \n"); |
| 312 | } |
| 313 | |
| Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 314 | /* All zeroes test (#137 verif) */ |
| 315 | #define ZEROESLENGTH 100 |
| 316 | DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH); |
| 317 | memset(CNBuffer, 0, ZEROESLENGTH); |
| 318 | result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1); |
| 319 | if (ZSTD_isError(result)) goto _output_error; |
| 320 | cSize = result; |
| 321 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100); |
| 322 | |
| 323 | DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH); |
| 324 | result = ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize); |
| 325 | if (ZSTD_isError(result)) goto _output_error; |
| 326 | if (result != ZEROESLENGTH) goto _output_error; |
| 327 | DISPLAYLEVEL(4, "OK \n"); |
| 328 | |
| 329 | /* nbSeq limit test */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 330 | #define _3BYTESTESTLENGTH 131000 |
| 331 | #define NB3BYTESSEQLOG 9 |
| 332 | #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG) |
| 333 | #define NB3BYTESSEQMASK (NB3BYTESSEQ-1) |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 334 | /* creates a buffer full of 3-bytes sequences */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 335 | { BYTE _3BytesSeqs[NB3BYTESSEQ][3]; |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 336 | U32 rSeed = 1; |
| Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 337 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 338 | /* create batch of 3-bytes sequences */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 339 | { int i; for (i=0; i < NB3BYTESSEQ; i++) { |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 340 | _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 341 | _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 342 | _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 343 | }} |
| Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 344 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 345 | /* randomly fills CNBuffer with prepared 3-bytes sequences */ |
| 346 | { int i; for (i=0; i < _3BYTESTESTLENGTH; ) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */ |
| 347 | U32 id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK; |
| Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 348 | ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0]; |
| 349 | ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1]; |
| 350 | ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2]; |
| 351 | i += 3; |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 352 | } }} |
| 353 | DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++); |
| 354 | result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH), CNBuffer, _3BYTESTESTLENGTH, 19); |
| 355 | if (ZSTD_isError(result)) goto _output_error; |
| 356 | cSize = result; |
| 357 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100); |
| Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 358 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 359 | DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++); |
| 360 | result = ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize); |
| 361 | if (ZSTD_isError(result)) goto _output_error; |
| 362 | if (result != _3BYTESTESTLENGTH) goto _output_error; |
| 363 | DISPLAYLEVEL(4, "OK \n"); |
| Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 364 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 365 | _end: |
| 366 | free(CNBuffer); |
| 367 | free(compressedBuffer); |
| 368 | free(decodedBuffer); |
| 369 | return testResult; |
| 370 | |
| 371 | _output_error: |
| 372 | testResult = 1; |
| 373 | DISPLAY("Error detected in Unit tests ! \n"); |
| 374 | goto _end; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 379 | { |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 380 | const BYTE* b1 = (const BYTE*)buf1; |
| 381 | const BYTE* b2 = (const BYTE*)buf2; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 382 | size_t i; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 383 | for (i=0; i<max; i++) { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 384 | if (b1[i] != b2[i]) break; |
| 385 | } |
| 386 | return i; |
| 387 | } |
| 388 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 389 | #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ |
| 390 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 391 | |
| 392 | static const U32 maxSrcLog = 23; |
| 393 | static const U32 maxSampleLog = 22; |
| 394 | |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 395 | int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 maxDuration, double compressibility) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 396 | { |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 397 | BYTE* cNoiseBuffer[5]; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 398 | BYTE* srcBuffer; |
| 399 | BYTE* cBuffer; |
| 400 | BYTE* dstBuffer; |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 401 | BYTE* mirrorBuffer; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 402 | size_t srcBufferSize = (size_t)1<<maxSrcLog; |
| 403 | size_t dstBufferSize = (size_t)1<<maxSampleLog; |
| 404 | size_t cBufferSize = ZSTD_compressBound(dstBufferSize); |
| 405 | U32 result = 0; |
| 406 | U32 testNb = 0; |
| 407 | U32 coreSeed = seed, lseed = 0; |
| Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 408 | ZSTD_CCtx* refCtx; |
| Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 409 | ZSTD_CCtx* ctx; |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 410 | ZSTD_DCtx* dctx; |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 411 | U32 startTime = FUZ_GetMilliStart(); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 412 | |
| 413 | /* allocation */ |
| Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 414 | refCtx = ZSTD_createCCtx(); |
| Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 415 | ctx = ZSTD_createCCtx(); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 416 | dctx= ZSTD_createDCtx(); |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 417 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 418 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 419 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 420 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 421 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 422 | dstBuffer = (BYTE*)malloc (dstBufferSize); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 423 | mirrorBuffer = (BYTE*)malloc (dstBufferSize); |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 424 | cBuffer = (BYTE*)malloc (cBufferSize); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 425 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] |
| Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 426 | || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx, |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 427 | "Not enough memory, fuzzer tests cancelled"); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 428 | |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 429 | /* Create initial samples */ |
| 430 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 431 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 432 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 433 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 434 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 435 | srcBuffer = cNoiseBuffer[2]; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 436 | |
| 437 | /* catch up testNb */ |
| Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 438 | for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 439 | |
| Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 440 | /* main test loop */ |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 441 | for ( ; (testNb <= nbTests) || (FUZ_GetMilliSpan(startTime) < maxDuration); testNb++ ) { |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 442 | size_t sampleSize, sampleStart, maxTestSize, totalTestSize; |
| Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 443 | size_t cSize, dSize, errorCode, totalCSize, totalGenSize; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 444 | U32 sampleSizeLog, buffNb, cLevelMod, nbChunks, n; |
| Yann Collet | 5835e1b | 2016-01-05 01:44:36 +0100 | [diff] [blame] | 445 | XXH64_CREATESTATE_STATIC(xxh64); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 446 | U64 crcOrig, crcDest; |
| Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 447 | int cLevel; |
| Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 448 | BYTE* sampleBuffer; |
| Yann Collet | 4bfe415 | 2015-12-06 13:18:37 +0100 | [diff] [blame] | 449 | const BYTE* dict; |
| 450 | size_t dictSize; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 451 | |
| Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 452 | /* notification */ |
| 453 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
| Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 454 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
| 455 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 456 | FUZ_rand(&coreSeed); |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 457 | { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; } |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 458 | buffNb = FUZ_rand(&lseed) & 127; |
| 459 | if (buffNb & 7) buffNb=2; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 460 | else { |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 461 | buffNb >>= 3; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 462 | if (buffNb & 7) { |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 463 | const U32 tnb[2] = { 1, 3 }; |
| 464 | buffNb = tnb[buffNb >> 3]; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 465 | } else { |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 466 | const U32 tnb[2] = { 0, 4 }; |
| 467 | buffNb = tnb[buffNb >> 3]; |
| Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 468 | } } |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 469 | srcBuffer = cNoiseBuffer[buffNb]; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 470 | sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; |
| Yann Collet | f4ce891 | 2015-08-11 14:18:45 +0100 | [diff] [blame] | 471 | sampleSize = (size_t)1 << sampleSizeLog; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 472 | sampleSize += FUZ_rand(&lseed) & (sampleSize-1); |
| 473 | sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 474 | |
| Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 475 | /* create sample buffer (to catch read error with valgrind & sanitizers) */ |
| 476 | sampleBuffer = (BYTE*)malloc(sampleSize); |
| 477 | CHECK (sampleBuffer==NULL, "not enough memory for sample buffer"); |
| 478 | memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); |
| 479 | crcOrig = XXH64(sampleBuffer, sampleSize, 0); |
| 480 | |
| 481 | /* compression test */ |
| Yann Collet | e93add0 | 2016-02-15 17:44:14 +0100 | [diff] [blame] | 482 | cLevelMod = MIN( ZSTD_maxCLevel(), (U32)MAX(1, 55 - 3*(int)sampleSizeLog) ); /* high levels only for small samples, for manageable speed */ |
| Yann Collet | 4c7aae3 | 2015-11-08 14:24:59 +0100 | [diff] [blame] | 483 | cLevel = (FUZ_rand(&lseed) % cLevelMod) +1; |
| Yann Collet | dc5e3e9 | 2015-11-20 09:23:56 +0100 | [diff] [blame] | 484 | cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 485 | CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed"); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 486 | |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 487 | /* compression failure test : too small dest buffer */ |
| Yann Collet | 346bffb | 2016-03-15 15:24:52 +0100 | [diff] [blame] | 488 | if (cSize > 3) { |
| Yann Collet | f4ce891 | 2015-08-11 14:18:45 +0100 | [diff] [blame] | 489 | const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 490 | const size_t tooSmallSize = cSize - missing; |
| Yann Collet | 602834f | 2015-08-20 07:46:10 +0100 | [diff] [blame] | 491 | static const U32 endMark = 0x4DC2B1A9; |
| Yann Collet | 602834f | 2015-08-20 07:46:10 +0100 | [diff] [blame] | 492 | memcpy(dstBuffer+tooSmallSize, &endMark, 4); |
| Yann Collet | dc5e3e9 | 2015-11-20 09:23:56 +0100 | [diff] [blame] | 493 | errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 494 | CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 495 | { U32 endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, 4); |
| 496 | CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); } |
| Yann Collet | e9853b2 | 2015-08-07 19:07:32 +0100 | [diff] [blame] | 497 | } |
| 498 | |
| Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 499 | /* frame header decompression test */ |
| Yann Collet | 346bffb | 2016-03-15 15:24:52 +0100 | [diff] [blame] | 500 | { ZSTD_frameParams dParams; |
| 501 | size_t const check = ZSTD_getFrameParams(&dParams, cBuffer, cSize); |
| 502 | CHECK(ZSTD_isError(check), "Frame Parameters extraction failed"); |
| 503 | CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect"); |
| 504 | } |
| 505 | |
| Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 506 | /* successful decompression test */ |
| 507 | { size_t margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1; |
| 508 | dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize); |
| 509 | CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (U32)sampleSize, (U32)cSize); |
| 510 | crcDest = XXH64(dstBuffer, sampleSize, 0); |
| 511 | CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize); |
| 512 | } |
| Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 513 | |
| 514 | free(sampleBuffer); /* no longer useful after this point */ |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 515 | |
| 516 | /* truncated src decompression test */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 517 | { const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 518 | const size_t tooSmallSize = cSize - missing; |
| 519 | void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch overflows */ |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 520 | CHECK(cBufferTooSmall == NULL, "not enough memory !"); |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 521 | memcpy(cBufferTooSmall, cBuffer, tooSmallSize); |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 522 | errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize); |
| 523 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); |
| 524 | free(cBufferTooSmall); |
| 525 | } |
| 526 | |
| 527 | /* too small dst decompression test */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 528 | if (sampleSize > 3) { |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 529 | const size_t missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 530 | const size_t tooSmallSize = sampleSize - missing; |
| 531 | static const BYTE token = 0xA9; |
| 532 | dstBuffer[tooSmallSize] = token; |
| 533 | errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize); |
| 534 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); |
| 535 | CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow"); |
| 536 | } |
| Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 537 | |
| 538 | /* noisy src decompression test */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 539 | if (cSize > 6) { |
| 540 | /* insert noise into src */ |
| 541 | { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4)); |
| 542 | size_t pos = 4; /* preserve magic number (too easy to detect) */ |
| 543 | for (;;) { |
| 544 | /* keep some original src */ |
| 545 | { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits; |
| 546 | size_t const mask = (1<<nbBits) - 1; |
| 547 | size_t const skipLength = FUZ_rand(&lseed) & mask; |
| 548 | pos += skipLength; |
| 549 | } |
| 550 | if (pos <= cSize) break; |
| 551 | /* add noise */ |
| 552 | { U32 nbBits = FUZ_rand(&lseed) % maxNbBits; |
| 553 | size_t mask, noiseStart, noiseLength; |
| 554 | if (nbBits>0) nbBits--; |
| 555 | mask = (1<<nbBits) - 1; |
| 556 | noiseLength = (FUZ_rand(&lseed) & mask) + 1; |
| 557 | if ( pos+noiseLength > cSize ) noiseLength = cSize-pos; |
| 558 | noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength); |
| 559 | memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength); |
| 560 | pos += noiseLength; |
| 561 | } } } |
| Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 562 | |
| 563 | /* decompress noisy source */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 564 | { U32 const endMark = 0xA9B1C3D6; |
| Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 565 | memcpy(dstBuffer+sampleSize, &endMark, 4); |
| 566 | errorCode = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize); |
| Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 567 | /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */ |
| Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 568 | CHECK((!ZSTD_isError(errorCode)) && (errorCode>sampleSize), |
| 569 | "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)errorCode, (U32)sampleSize); |
| Yann Collet | f312041 | 2016-03-19 11:40:19 +0100 | [diff] [blame] | 570 | { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4); |
| 571 | CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow"); } |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 572 | } } /* noisy src decompression test */ |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 573 | |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 574 | /* Streaming compression of scattered segments test */ |
| Yann Collet | 5835e1b | 2016-01-05 01:44:36 +0100 | [diff] [blame] | 575 | XXH64_reset(xxh64, 0); |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 576 | nbChunks = (FUZ_rand(&lseed) & 127) + 2; |
| 577 | sampleSizeLog = FUZ_rand(&lseed) % maxSrcLog; |
| 578 | maxTestSize = (size_t)1 << sampleSizeLog; |
| 579 | maxTestSize += FUZ_rand(&lseed) & (maxTestSize-1); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 580 | if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1; |
| Yann Collet | 4bfe415 | 2015-12-06 13:18:37 +0100 | [diff] [blame] | 581 | |
| 582 | sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; |
| 583 | sampleSize = (size_t)1 << sampleSizeLog; |
| 584 | sampleSize += FUZ_rand(&lseed) & (sampleSize-1); |
| 585 | sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); |
| 586 | dict = srcBuffer + sampleStart; |
| 587 | dictSize = sampleSize; |
| 588 | |
| Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 589 | errorCode = ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, (FUZ_rand(&lseed) % (20 - (sampleSizeLog/3))) + 1); |
| 590 | CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 591 | errorCode = ZSTD_copyCCtx(ctx, refCtx); |
| Yann Collet | 464fa99 | 2016-02-03 01:09:46 +0100 | [diff] [blame] | 592 | CHECK (ZSTD_isError(errorCode), "ZSTD_copyCCtx error : %s", ZSTD_getErrorName(errorCode)); |
| Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 593 | totalTestSize = 0; cSize = 0; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 594 | for (n=0; n<nbChunks; n++) { |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 595 | sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; |
| 596 | sampleSize = (size_t)1 << sampleSizeLog; |
| 597 | sampleSize += FUZ_rand(&lseed) & (sampleSize-1); |
| 598 | sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); |
| 599 | |
| 600 | if (cBufferSize-cSize < ZSTD_compressBound(sampleSize)) |
| 601 | /* avoid invalid dstBufferTooSmall */ |
| 602 | break; |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 603 | if (totalTestSize+sampleSize > maxTestSize) break; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 604 | |
| 605 | errorCode = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+sampleStart, sampleSize); |
| 606 | CHECK (ZSTD_isError(errorCode), "multi-segments compression error : %s", ZSTD_getErrorName(errorCode)); |
| 607 | cSize += errorCode; |
| 608 | |
| Yann Collet | 5835e1b | 2016-01-05 01:44:36 +0100 | [diff] [blame] | 609 | XXH64_update(xxh64, srcBuffer+sampleStart, sampleSize); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 610 | memcpy(mirrorBuffer + totalTestSize, srcBuffer+sampleStart, sampleSize); |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 611 | totalTestSize += sampleSize; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 612 | } |
| 613 | errorCode = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize); |
| 614 | CHECK (ZSTD_isError(errorCode), "multi-segments epilogue error : %s", ZSTD_getErrorName(errorCode)); |
| 615 | cSize += errorCode; |
| Yann Collet | 5835e1b | 2016-01-05 01:44:36 +0100 | [diff] [blame] | 616 | crcOrig = XXH64_digest(xxh64); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 617 | |
| 618 | /* streaming decompression test */ |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 619 | errorCode = ZSTD_decompressBegin_usingDict(dctx, dict, dictSize); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 620 | CHECK (ZSTD_isError(errorCode), "cannot init DCtx : %s", ZSTD_getErrorName(errorCode)); |
| 621 | totalCSize = 0; |
| 622 | totalGenSize = 0; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 623 | while (totalCSize < cSize) { |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 624 | size_t inSize = ZSTD_nextSrcSizeToDecompress(dctx); |
| 625 | size_t genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize); |
| 626 | CHECK (ZSTD_isError(genSize), "streaming decompression error : %s", ZSTD_getErrorName(genSize)); |
| 627 | totalGenSize += genSize; |
| 628 | totalCSize += inSize; |
| 629 | } |
| 630 | CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded"); |
| 631 | CHECK (totalGenSize != totalTestSize, "decompressed data : wrong size") |
| 632 | CHECK (totalCSize != cSize, "compressed data should be fully read") |
| 633 | crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 634 | if (crcDest!=crcOrig) |
| 635 | errorCode = findDiff(mirrorBuffer, dstBuffer, totalTestSize); |
| 636 | CHECK (crcDest!=crcOrig, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)", |
| 637 | (U32)errorCode, (U32)totalTestSize, dstBuffer[errorCode], mirrorBuffer[errorCode]); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 638 | } |
| Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 639 | DISPLAY("\r%u fuzzer tests completed \n", testNb-1); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 640 | |
| 641 | _cleanup: |
| Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 642 | ZSTD_freeCCtx(refCtx); |
| Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 643 | ZSTD_freeCCtx(ctx); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 644 | ZSTD_freeDCtx(dctx); |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 645 | free(cNoiseBuffer[0]); |
| 646 | free(cNoiseBuffer[1]); |
| 647 | free(cNoiseBuffer[2]); |
| 648 | free(cNoiseBuffer[3]); |
| 649 | free(cNoiseBuffer[4]); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 650 | free(cBuffer); |
| 651 | free(dstBuffer); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 652 | free(mirrorBuffer); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 653 | return result; |
| 654 | |
| 655 | _output_error: |
| 656 | result = 1; |
| 657 | goto _cleanup; |
| 658 | } |
| 659 | |
| 660 | |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 661 | /*_******************************************************* |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 662 | * Command line |
| 663 | *********************************************************/ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 664 | int FUZ_usage(const char* programName) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 665 | { |
| 666 | DISPLAY( "Usage :\n"); |
| 667 | DISPLAY( " %s [args]\n", programName); |
| 668 | DISPLAY( "\n"); |
| 669 | DISPLAY( "Arguments :\n"); |
| 670 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 671 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 672 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 673 | DISPLAY( " -P# : Select compressibility in %% (default:%u%%)\n", FUZ_compressibility_default); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 674 | DISPLAY( " -v : verbose\n"); |
| Yann Collet | e9853b2 | 2015-08-07 19:07:32 +0100 | [diff] [blame] | 675 | DISPLAY( " -p : pause at the end\n"); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 676 | DISPLAY( " -h : display help and exit\n"); |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 681 | int main(int argc, const char** argv) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 682 | { |
| 683 | U32 seed=0; |
| 684 | int seedset=0; |
| 685 | int argNb; |
| 686 | int nbTests = nbTestsDefault; |
| 687 | int testNb = 0; |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 688 | U32 proba = FUZ_compressibility_default; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 689 | int result=0; |
| 690 | U32 mainPause = 0; |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 691 | U32 maxDuration = 0; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 692 | const char* programName; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 693 | |
| 694 | /* Check command line */ |
| 695 | programName = argv[0]; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 696 | for (argNb=1; argNb<argc; argNb++) { |
| 697 | const char* argument = argv[argNb]; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 698 | if(!argument) continue; /* Protection if argument empty */ |
| 699 | |
| 700 | /* Handle commands. Aggregated commands are allowed */ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 701 | if (argument[0]=='-') { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 702 | argument++; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 703 | while (*argument!=0) { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 704 | switch(*argument) |
| 705 | { |
| 706 | case 'h': |
| 707 | return FUZ_usage(programName); |
| 708 | case 'v': |
| 709 | argument++; |
| 710 | g_displayLevel=4; |
| 711 | break; |
| 712 | case 'q': |
| 713 | argument++; |
| 714 | g_displayLevel--; |
| 715 | break; |
| 716 | case 'p': /* pause at the end */ |
| 717 | argument++; |
| 718 | mainPause = 1; |
| 719 | break; |
| 720 | |
| 721 | case 'i': |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 722 | argument++; maxDuration=0; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 723 | nbTests=0; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 724 | while ((*argument>='0') && (*argument<='9')) { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 725 | nbTests *= 10; |
| 726 | nbTests += *argument - '0'; |
| 727 | argument++; |
| 728 | } |
| 729 | break; |
| 730 | |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 731 | case 'T': |
| 732 | argument++; |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 733 | nbTests=0; maxDuration=0; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 734 | while ((*argument>='0') && (*argument<='9')) { |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 735 | maxDuration *= 10; |
| 736 | maxDuration += *argument - '0'; |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 737 | argument++; |
| 738 | } |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 739 | if (*argument=='m') maxDuration *=60, argument++; |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 740 | if (*argument=='n') argument++; |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 741 | maxDuration *= 1000; |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 742 | break; |
| 743 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 744 | case 's': |
| 745 | argument++; |
| 746 | seed=0; |
| 747 | seedset=1; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 748 | while ((*argument>='0') && (*argument<='9')) { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 749 | seed *= 10; |
| 750 | seed += *argument - '0'; |
| 751 | argument++; |
| 752 | } |
| 753 | break; |
| 754 | |
| 755 | case 't': |
| 756 | argument++; |
| 757 | testNb=0; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 758 | while ((*argument>='0') && (*argument<='9')) { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 759 | testNb *= 10; |
| 760 | testNb += *argument - '0'; |
| 761 | argument++; |
| 762 | } |
| 763 | break; |
| 764 | |
| 765 | case 'P': /* compressibility % */ |
| 766 | argument++; |
| 767 | proba=0; |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 768 | while ((*argument>='0') && (*argument<='9')) { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 769 | proba *= 10; |
| 770 | proba += *argument - '0'; |
| 771 | argument++; |
| 772 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 773 | if (proba>100) proba=100; |
| 774 | break; |
| 775 | |
| 776 | default: |
| 777 | return FUZ_usage(programName); |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 778 | } } } } /* for (argNb=1; argNb<argc; argNb++) */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 779 | |
| 780 | /* Get Seed */ |
| 781 | DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION); |
| 782 | |
| 783 | if (!seedset) seed = FUZ_GetMilliStart() % 10000; |
| 784 | DISPLAY("Seed = %u\n", seed); |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 785 | if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %u%%\n", proba); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 786 | |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 787 | if (testNb==0) |
| 788 | result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 789 | if (!result) |
| Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 790 | result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100); |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 791 | if (mainPause) { |
| Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 792 | int unused; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 793 | DISPLAY("Press Enter \n"); |
| Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 794 | unused = getchar(); |
| 795 | (void)unused; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 796 | } |
| 797 | return result; |
| 798 | } |