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