Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 9 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 10 | |
| 11 | /*-************************************ |
| 12 | * Compiler specific |
| 13 | **************************************/ |
| 14 | #ifdef _MSC_VER /* Visual Studio */ |
| 15 | # define _CRT_SECURE_NO_WARNINGS /* fgets */ |
| 16 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 17 | # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ |
| 18 | #endif |
| 19 | |
| 20 | |
| 21 | /*-************************************ |
| 22 | * Includes |
| 23 | **************************************/ |
| 24 | #include <stdlib.h> /* free */ |
| 25 | #include <stdio.h> /* fgets, sscanf */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 26 | #include <time.h> /* clock_t, clock() */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 27 | #include <string.h> /* strcmp */ |
| 28 | #include "mem.h" |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 29 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 30 | #include "zstd.h" /* ZSTD_compressBound */ |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame^] | 31 | #include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 32 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 33 | #define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 34 | #include "xxhash.h" /* XXH64_* */ |
| 35 | |
| 36 | |
| 37 | /*-************************************ |
| 38 | * Constants |
| 39 | **************************************/ |
| 40 | #define KB *(1U<<10) |
| 41 | #define MB *(1U<<20) |
| 42 | #define GB *(1U<<30) |
| 43 | |
| 44 | static const U32 nbTestsDefault = 10000; |
| 45 | #define COMPRESSIBLE_NOISE_LENGTH (10 MB) |
| 46 | #define FUZ_COMPRESSIBILITY_DEFAULT 50 |
| 47 | static const U32 prime1 = 2654435761U; |
| 48 | static const U32 prime2 = 2246822519U; |
| 49 | |
| 50 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 51 | /*-************************************ |
| 52 | * Display Macros |
| 53 | **************************************/ |
| 54 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 55 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 56 | static U32 g_displayLevel = 2; |
| 57 | |
| 58 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 59 | if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 60 | { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 61 | if (g_displayLevel>=4) fflush(stdout); } } |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 62 | static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 63 | static clock_t g_displayClock = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 64 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 65 | static clock_t g_clockTime = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 66 | |
| 67 | |
| 68 | /*-******************************************************* |
| 69 | * Fuzzer functions |
| 70 | *********************************************************/ |
| 71 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
| 72 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 73 | static clock_t FUZ_GetClockSpan(clock_t clockStart) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 74 | { |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 75 | return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /*! FUZ_rand() : |
| 79 | @return : a 27 bits random value, from a 32-bits `seed`. |
| 80 | `seed` is also modified */ |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 81 | #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 82 | unsigned int FUZ_rand(unsigned int* seedPtr) |
| 83 | { |
| 84 | U32 rand32 = *seedPtr; |
| 85 | rand32 *= prime1; |
| 86 | rand32 += prime2; |
| 87 | rand32 = FUZ_rotl32(rand32, 13); |
| 88 | *seedPtr = rand32; |
| 89 | return rand32 >> 5; |
| 90 | } |
| 91 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 92 | static void* allocFunction(void* opaque, size_t size) |
| 93 | { |
| 94 | void* address = malloc(size); |
| 95 | (void)opaque; |
| 96 | return address; |
| 97 | } |
| 98 | |
| 99 | static void freeFunction(void* opaque, void* address) |
| 100 | { |
| 101 | (void)opaque; |
| 102 | free(address); |
| 103 | } |
| 104 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 105 | |
| 106 | /*====================================================== |
| 107 | * Basic Unit tests |
| 108 | ======================================================*/ |
| 109 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 110 | static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem) |
| 111 | { |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 112 | size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 113 | void* CNBuffer = malloc(CNBufferSize); |
| 114 | size_t const skippableFrameSize = 11; |
| 115 | size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH); |
| 116 | void* compressedBuffer = malloc(compressedBufferSize); |
| 117 | size_t const decodedBufferSize = CNBufferSize; |
| 118 | void* decodedBuffer = malloc(decodedBufferSize); |
| 119 | size_t cSize; |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 120 | int testResult = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 121 | U32 testNb=0; |
| 122 | ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem); |
| 123 | ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 124 | ZSTD_inBuffer inBuff, inBuff2; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 125 | ZSTD_outBuffer outBuff; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 126 | |
| 127 | /* Create compressible test buffer */ |
| 128 | if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) { |
| 129 | DISPLAY("Not enough memory, aborting\n"); |
| 130 | goto _output_error; |
| 131 | } |
| 132 | RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed); |
| 133 | |
| 134 | /* generate skippable frame */ |
| 135 | MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START); |
| 136 | MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize); |
| 137 | cSize = skippableFrameSize + 8; |
| 138 | |
| 139 | /* Basic compression test */ |
| 140 | DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 141 | ZSTD_initCStream_usingDict(zc, CNBuffer, 128 KB, 1); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 142 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 143 | outBuff.size = compressedBufferSize; |
| 144 | outBuff.pos = 0; |
| 145 | inBuff.src = CNBuffer; |
| 146 | inBuff.size = CNBufferSize; |
| 147 | inBuff.pos = 0; |
| 148 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 149 | if (ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 150 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 151 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
Yann Collet | 9a021c1 | 2016-08-26 09:05:06 +0200 | [diff] [blame] | 152 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 153 | cSize += outBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 154 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 155 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 156 | DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++); |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 157 | { size_t const s = ZSTD_sizeof_CStream(zc); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 158 | if (ZSTD_isError(s)) goto _output_error; |
| 159 | DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s); |
| 160 | } |
| 161 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 162 | /* skippable frame test */ |
| 163 | DISPLAYLEVEL(4, "test%3i : decompress skippable frame : ", testNb++); |
| 164 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 165 | inBuff.src = compressedBuffer; |
| 166 | inBuff.size = cSize; |
| 167 | inBuff.pos = 0; |
| 168 | outBuff.dst = decodedBuffer; |
| 169 | outBuff.size = CNBufferSize; |
| 170 | outBuff.pos = 0; |
| 171 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 172 | if (r != 0) goto _output_error; } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 173 | if (outBuff.pos != 0) goto _output_error; /* skippable frame len is 0 */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 174 | DISPLAYLEVEL(4, "OK \n"); |
| 175 | |
| 176 | /* Basic decompression test */ |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 177 | inBuff2 = inBuff; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 178 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 179 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 180 | { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000000000); /* large limit */ |
| 181 | if (ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 182 | { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 183 | if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
| 184 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 185 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
| 186 | DISPLAYLEVEL(4, "OK \n"); |
| 187 | |
| 188 | /* Re-use without init */ |
| 189 | DISPLAYLEVEL(4, "test%3i : decompress again without init (re-use previous settings): ", testNb++); |
| 190 | outBuff.pos = 0; |
| 191 | { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2); |
| 192 | if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 193 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 194 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 195 | DISPLAYLEVEL(4, "OK \n"); |
| 196 | |
| 197 | /* check regenerated data is byte exact */ |
| 198 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 199 | { size_t i; |
| 200 | for (i=0; i<CNBufferSize; i++) { |
| 201 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error; |
| 202 | } } |
| 203 | DISPLAYLEVEL(4, "OK \n"); |
| 204 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 205 | DISPLAYLEVEL(4, "test%3i : check DStream size : ", testNb++); |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 206 | { size_t const s = ZSTD_sizeof_DStream(zd); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 207 | if (ZSTD_isError(s)) goto _output_error; |
| 208 | DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s); |
| 209 | } |
| 210 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 211 | /* Byte-by-byte decompression test */ |
| 212 | DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++); |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 213 | { /* skippable frame */ |
| 214 | size_t r = 1; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 215 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 216 | inBuff.src = compressedBuffer; |
| 217 | outBuff.dst = decodedBuffer; |
| 218 | inBuff.pos = 0; |
| 219 | outBuff.pos = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 220 | while (r) { /* skippable frame */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 221 | inBuff.size = inBuff.pos + 1; |
| 222 | outBuff.size = outBuff.pos + 1; |
| 223 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 224 | if (ZSTD_isError(r)) goto _output_error; |
| 225 | } |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 226 | /* normal frame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 227 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
| 228 | r=1; |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 229 | while (r) { |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 230 | inBuff.size = inBuff.pos + 1; |
| 231 | outBuff.size = outBuff.pos + 1; |
| 232 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 233 | if (ZSTD_isError(r)) goto _output_error; |
| 234 | } |
| 235 | } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 236 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 237 | if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 238 | DISPLAYLEVEL(4, "OK \n"); |
| 239 | |
| 240 | /* check regenerated data is byte exact */ |
| 241 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 242 | { size_t i; |
| 243 | for (i=0; i<CNBufferSize; i++) { |
| 244 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; |
| 245 | } } |
| 246 | DISPLAYLEVEL(4, "OK \n"); |
| 247 | |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame^] | 248 | /* _srcSize compression test */ |
| 249 | DISPLAYLEVEL(4, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 250 | ZSTD_initCStream_srcSize(zc, 1, CNBufferSize); |
| 251 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 252 | outBuff.size = compressedBufferSize; |
| 253 | outBuff.pos = 0; |
| 254 | inBuff.src = CNBuffer; |
| 255 | inBuff.size = CNBufferSize; |
| 256 | inBuff.pos = 0; |
| 257 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 258 | if (ZSTD_isError(r)) goto _output_error; } |
| 259 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 260 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 261 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 262 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 263 | |
| 264 | /* wrong _srcSize compression test */ |
| 265 | DISPLAYLEVEL(4, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1); |
| 266 | ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1); |
| 267 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 268 | outBuff.size = compressedBufferSize; |
| 269 | outBuff.pos = 0; |
| 270 | inBuff.src = CNBuffer; |
| 271 | inBuff.size = CNBufferSize; |
| 272 | inBuff.pos = 0; |
| 273 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 274 | if (ZSTD_isError(r)) goto _output_error; } |
| 275 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 276 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 277 | if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */ |
| 278 | DISPLAYLEVEL(4, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); } |
| 279 | |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 280 | /* Complex context re-use scenario */ |
| 281 | DISPLAYLEVEL(4, "test%3i : context re-use : ", testNb++); |
| 282 | ZSTD_freeCStream(zc); |
| 283 | zc = ZSTD_createCStream_advanced(customMem); |
| 284 | if (zc==NULL) goto _output_error; /* memory allocation issue */ |
| 285 | /* use 1 */ |
| 286 | { size_t const inSize = 513; |
| 287 | ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */ |
| 288 | inBuff.src = CNBuffer; |
| 289 | inBuff.size = inSize; |
| 290 | inBuff.pos = 0; |
| 291 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 292 | outBuff.size = ZSTD_compressBound(inSize); |
| 293 | outBuff.pos = 0; |
| 294 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 295 | if (ZSTD_isError(r)) goto _output_error; } |
| 296 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 297 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 298 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 299 | } |
| 300 | /* use 2 */ |
| 301 | { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */ |
| 302 | ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */ |
| 303 | inBuff.src = CNBuffer; |
| 304 | inBuff.size = inSize; |
| 305 | inBuff.pos = 0; |
| 306 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 307 | outBuff.size = ZSTD_compressBound(inSize); |
| 308 | outBuff.pos = 0; |
| 309 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 310 | if (ZSTD_isError(r)) goto _output_error; } |
| 311 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 312 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 313 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 314 | } |
| 315 | DISPLAYLEVEL(4, "OK \n"); |
| 316 | |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 317 | /* CDict scenario */ |
| 318 | DISPLAYLEVEL(4, "test%3i : digested dictionary : ", testNb++); |
| 319 | { ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, 128 KB, 1); |
| 320 | size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict); |
| 321 | if (ZSTD_isError(initError)) goto _output_error; |
| 322 | cSize = 0; |
| 323 | outBuff.dst = compressedBuffer; |
| 324 | outBuff.size = compressedBufferSize; |
| 325 | outBuff.pos = 0; |
| 326 | inBuff.src = CNBuffer; |
| 327 | inBuff.size = CNBufferSize; |
| 328 | inBuff.pos = 0; |
| 329 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 330 | if (ZSTD_isError(r)) goto _output_error; } |
| 331 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 332 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 333 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 334 | cSize = outBuff.pos; |
| 335 | ZSTD_freeCDict(cdict); |
| 336 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100); |
| 337 | } |
| 338 | |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 339 | DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++); |
| 340 | { size_t const s = ZSTD_sizeof_CStream(zc); |
| 341 | if (ZSTD_isError(s)) goto _output_error; |
| 342 | DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s); |
| 343 | } |
| 344 | |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 345 | /* DDict scenario */ |
| 346 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize); |
| 347 | { ZSTD_DDict* const ddict = ZSTD_createDDict(CNBuffer, 128 KB); |
| 348 | size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict); |
| 349 | if (ZSTD_isError(initError)) goto _output_error; |
| 350 | inBuff.src = compressedBuffer; |
| 351 | inBuff.size = cSize; |
| 352 | inBuff.pos = 0; |
| 353 | outBuff.dst = decodedBuffer; |
| 354 | outBuff.size = CNBufferSize; |
| 355 | outBuff.pos = 0; |
| 356 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 357 | if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
| 358 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 359 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
| 360 | ZSTD_freeDDict(ddict); |
| 361 | DISPLAYLEVEL(4, "OK \n"); |
| 362 | } |
| 363 | |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 364 | /* test ZSTD_setDStreamParameter() resilience */ |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 365 | DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++); |
| 366 | { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */ |
| 367 | if (!ZSTD_isError(r)) goto _output_error; } |
| 368 | DISPLAYLEVEL(4, "OK \n"); |
| 369 | |
| 370 | /* Memory restriction */ |
| 371 | DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++); |
| 372 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
| 373 | { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */ |
| 374 | if (ZSTD_isError(r)) goto _output_error; } |
| 375 | inBuff.src = compressedBuffer; |
| 376 | inBuff.size = cSize; |
| 377 | inBuff.pos = 0; |
| 378 | outBuff.dst = decodedBuffer; |
| 379 | outBuff.size = CNBufferSize; |
| 380 | outBuff.pos = 0; |
| 381 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 382 | if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */ |
| 383 | DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); } |
| 384 | |
| 385 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 386 | _end: |
| 387 | ZSTD_freeCStream(zc); |
| 388 | ZSTD_freeDStream(zd); |
| 389 | free(CNBuffer); |
| 390 | free(compressedBuffer); |
| 391 | free(decodedBuffer); |
| 392 | return testResult; |
| 393 | |
| 394 | _output_error: |
| 395 | testResult = 1; |
| 396 | DISPLAY("Error detected in Unit tests ! \n"); |
| 397 | goto _end; |
| 398 | } |
| 399 | |
| 400 | |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 401 | /* ====== Fuzzer tests ====== */ |
| 402 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 403 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 404 | { |
| 405 | const BYTE* b1 = (const BYTE*)buf1; |
| 406 | const BYTE* b2 = (const BYTE*)buf2; |
| 407 | size_t u; |
| 408 | for (u=0; u<max; u++) { |
| 409 | if (b1[u] != b2[u]) break; |
| 410 | } |
| 411 | return u; |
| 412 | } |
| 413 | |
| 414 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 415 | { |
| 416 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 417 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 418 | } |
| 419 | |
| 420 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 421 | { |
| 422 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 423 | return FUZ_rLogLength(seed, logLength); |
| 424 | } |
| 425 | |
| 426 | #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) |
| 427 | |
| 428 | #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ |
| 429 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } |
| 430 | |
| 431 | static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility) |
| 432 | { |
| 433 | static const U32 maxSrcLog = 24; |
| 434 | static const U32 maxSampleLog = 19; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 435 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 436 | BYTE* cNoiseBuffer[5]; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 437 | size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog); |
| 438 | BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize); |
| 439 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
| 440 | BYTE* const cBuffer = (BYTE*)malloc (cBufferSize); |
| 441 | size_t const dstBufferSize = srcBufferSize; |
| 442 | BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 443 | U32 result = 0; |
| 444 | U32 testNb = 0; |
| 445 | U32 coreSeed = seed; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 446 | ZSTD_CStream* zc = ZSTD_createCStream(); /* will be reset sometimes */ |
| 447 | ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */ |
| 448 | ZSTD_DStream* const zd_noise = ZSTD_createDStream(); |
| 449 | clock_t const startClock = clock(); |
| 450 | const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */ |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 451 | size_t dictSize = 0; |
| 452 | U32 oldTestLog = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 453 | |
| 454 | /* allocations */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 455 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 456 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 457 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 458 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 459 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 460 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 461 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise , |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 462 | "Not enough memory, fuzzer tests cancelled"); |
| 463 | |
| 464 | /* Create initial samples */ |
| 465 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 466 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 467 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 468 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 469 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 470 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 471 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 472 | |
| 473 | /* catch up testNb */ |
| 474 | for (testNb=1; testNb < startTest; testNb++) |
| 475 | FUZ_rand(&coreSeed); |
| 476 | |
| 477 | /* test loop */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 478 | for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 479 | U32 lseed; |
| 480 | const BYTE* srcBuffer; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 481 | size_t totalTestSize, totalGenSize, cSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 482 | XXH64_state_t xxhState; |
| 483 | U64 crcOrig; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 484 | U32 resetAllowed = 1; |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 485 | size_t maxTestSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 486 | |
| 487 | /* init */ |
Yann Collet | 4c0b44f | 2016-11-01 11:13:22 -0700 | [diff] [blame] | 488 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
| 489 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 490 | FUZ_rand(&coreSeed); |
| 491 | lseed = coreSeed ^ prime1; |
| 492 | |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 493 | /* states full reset (deliberately not synchronized) */ |
| 494 | /* some issues can only happen when reusing states */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 495 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); resetAllowed=0; } |
| 496 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZSTD_freeDStream(zd); zd = ZSTD_createDStream(); ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 497 | |
| 498 | /* srcBuffer selection [0-4] */ |
| 499 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 500 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 501 | else { |
| 502 | buffNb >>= 3; |
| 503 | if (buffNb & 7) { |
| 504 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 505 | buffNb = tnb[buffNb >> 3]; |
| 506 | } else { |
| 507 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 508 | buffNb = tnb[buffNb >> 3]; |
| 509 | } } |
| 510 | srcBuffer = cNoiseBuffer[buffNb]; |
| 511 | } |
| 512 | |
| 513 | /* compression init */ |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 514 | if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ |
| 515 | && oldTestLog /* at least one test happened */ && resetAllowed) { |
| 516 | maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); |
| 517 | if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1; |
| 518 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize; |
| 519 | size_t const resetError = ZSTD_resetCStream(zc, pledgedSrcSize); |
| 520 | CHECK(ZSTD_isError(resetError), "ZSTD_resetCStream error : %s", ZSTD_getErrorName(resetError)); |
| 521 | } |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 522 | } else { |
| 523 | U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 524 | U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1; |
| 525 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 526 | oldTestLog = testLog; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 527 | /* random dictionary selection */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 528 | dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 529 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 530 | dict = srcBuffer + dictStart; |
| 531 | } |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 532 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize; |
| 533 | ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 534 | params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; |
| 535 | params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 536 | { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize); |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 537 | CHECK (ZSTD_isError(initError),"ZSTD_initCStream_advanced error : %s", ZSTD_getErrorName(initError)); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 538 | } } } |
| 539 | |
| 540 | /* multi-segments compression test */ |
| 541 | XXH64_reset(&xxhState, 0); |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 542 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 543 | U32 n; |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 544 | for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) { |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame^] | 545 | /* compress random chunks into randomly sized dst buffers */ |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 546 | { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 547 | size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize); |
| 548 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 549 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 550 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 551 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 552 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 553 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 554 | { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 555 | CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 556 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 557 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 558 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 559 | totalTestSize += inBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /* random flush operation, to mess around */ |
| 563 | if ((FUZ_rand(&lseed) & 15) == 0) { |
| 564 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 565 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 566 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 567 | { size_t const flushError = ZSTD_flushStream(zc, &outBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 568 | CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError)); |
| 569 | } } } |
| 570 | |
| 571 | /* final frame epilogue */ |
| 572 | { size_t remainingToFlush = (size_t)(-1); |
| 573 | while (remainingToFlush) { |
| 574 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 575 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 576 | U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 577 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 578 | remainingToFlush = ZSTD_endStream(zc, &outBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 579 | CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush)); |
| 580 | CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush); |
| 581 | } } |
| 582 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 583 | cSize = outBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* multi - fragments decompression test */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 587 | if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) { |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 588 | CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed"); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 589 | } else { |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 590 | ZSTD_initDStream_usingDict(zd, dict, dictSize); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 591 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 592 | { size_t decompressionResult = 1; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 593 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 594 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 595 | for (totalGenSize = 0 ; decompressionResult ; ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 596 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 597 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 598 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 599 | inBuff.size = inBuff.pos + readCSrcSize; |
| 600 | outBuff.size = inBuff.pos + dstBuffSize; |
| 601 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 602 | CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 603 | } |
| 604 | CHECK (decompressionResult != 0, "frame not fully decoded"); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 605 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size") |
| 606 | CHECK (inBuff.pos != cSize, "compressed data should be fully read") |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 607 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 608 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 609 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 610 | } } |
| 611 | |
| 612 | /*===== noisy/erroneous src decompression test =====*/ |
| 613 | |
| 614 | /* add some noise */ |
| 615 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 616 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 617 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 618 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 619 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 620 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 621 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 622 | } } |
| 623 | |
| 624 | /* try decompression on noisy data */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 625 | ZSTD_initDStream(zd_noise); /* note : no dictionary */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 626 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 627 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 628 | while (outBuff.pos < dstBufferSize) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 629 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 630 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 631 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
| 632 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 633 | inBuff.size = inBuff.pos + randomCSrcSize; |
| 634 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 635 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
| 636 | } } } } |
| 637 | DISPLAY("\r%u fuzzer tests completed \n", testNb); |
| 638 | |
| 639 | _cleanup: |
| 640 | ZSTD_freeCStream(zc); |
| 641 | ZSTD_freeDStream(zd); |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 642 | ZSTD_freeDStream(zd_noise); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 643 | free(cNoiseBuffer[0]); |
| 644 | free(cNoiseBuffer[1]); |
| 645 | free(cNoiseBuffer[2]); |
| 646 | free(cNoiseBuffer[3]); |
| 647 | free(cNoiseBuffer[4]); |
| 648 | free(copyBuffer); |
| 649 | free(cBuffer); |
| 650 | free(dstBuffer); |
| 651 | return result; |
| 652 | |
| 653 | _output_error: |
| 654 | result = 1; |
| 655 | goto _cleanup; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | /*-******************************************************* |
| 660 | * Command line |
| 661 | *********************************************************/ |
| 662 | int FUZ_usage(const char* programName) |
| 663 | { |
| 664 | DISPLAY( "Usage :\n"); |
| 665 | DISPLAY( " %s [args]\n", programName); |
| 666 | DISPLAY( "\n"); |
| 667 | DISPLAY( "Arguments :\n"); |
| 668 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 669 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 670 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
| 671 | DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); |
| 672 | DISPLAY( " -v : verbose\n"); |
| 673 | DISPLAY( " -p : pause at the end\n"); |
| 674 | DISPLAY( " -h : display help and exit\n"); |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | |
| 679 | int main(int argc, const char** argv) |
| 680 | { |
| 681 | U32 seed=0; |
| 682 | int seedset=0; |
| 683 | int argNb; |
| 684 | int nbTests = nbTestsDefault; |
| 685 | int testNb = 0; |
| 686 | int proba = FUZ_COMPRESSIBILITY_DEFAULT; |
| 687 | int result=0; |
| 688 | U32 mainPause = 0; |
| 689 | const char* programName = argv[0]; |
| 690 | ZSTD_customMem customMem = { allocFunction, freeFunction, NULL }; |
| 691 | ZSTD_customMem customNULL = { NULL, NULL, NULL }; |
| 692 | |
| 693 | /* Check command line */ |
| 694 | for(argNb=1; argNb<argc; argNb++) { |
| 695 | const char* argument = argv[argNb]; |
| 696 | if(!argument) continue; /* Protection if argument empty */ |
| 697 | |
| 698 | /* Parsing commands. Aggregated commands are allowed */ |
| 699 | if (argument[0]=='-') { |
| 700 | argument++; |
| 701 | |
| 702 | while (*argument!=0) { |
| 703 | switch(*argument) |
| 704 | { |
| 705 | case 'h': |
| 706 | return FUZ_usage(programName); |
| 707 | case 'v': |
| 708 | argument++; |
| 709 | g_displayLevel=4; |
| 710 | break; |
| 711 | case 'q': |
| 712 | argument++; |
| 713 | g_displayLevel--; |
| 714 | break; |
| 715 | case 'p': /* pause at the end */ |
| 716 | argument++; |
| 717 | mainPause = 1; |
| 718 | break; |
| 719 | |
| 720 | case 'i': |
| 721 | argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 722 | nbTests=0; g_clockTime=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 723 | while ((*argument>='0') && (*argument<='9')) { |
| 724 | nbTests *= 10; |
| 725 | nbTests += *argument - '0'; |
| 726 | argument++; |
| 727 | } |
| 728 | break; |
| 729 | |
| 730 | case 'T': |
| 731 | argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 732 | nbTests=0; g_clockTime=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 733 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 734 | g_clockTime *= 10; |
| 735 | g_clockTime += *argument - '0'; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 736 | argument++; |
| 737 | } |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 738 | if (*argument=='m') g_clockTime *=60, argument++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 739 | if (*argument=='n') argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 740 | g_clockTime *= CLOCKS_PER_SEC; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 741 | break; |
| 742 | |
| 743 | case 's': |
| 744 | argument++; |
| 745 | seed=0; |
| 746 | seedset=1; |
| 747 | while ((*argument>='0') && (*argument<='9')) { |
| 748 | seed *= 10; |
| 749 | seed += *argument - '0'; |
| 750 | argument++; |
| 751 | } |
| 752 | break; |
| 753 | |
| 754 | case 't': |
| 755 | argument++; |
| 756 | testNb=0; |
| 757 | while ((*argument>='0') && (*argument<='9')) { |
| 758 | testNb *= 10; |
| 759 | testNb += *argument - '0'; |
| 760 | argument++; |
| 761 | } |
| 762 | break; |
| 763 | |
| 764 | case 'P': /* compressibility % */ |
| 765 | argument++; |
| 766 | proba=0; |
| 767 | while ((*argument>='0') && (*argument<='9')) { |
| 768 | proba *= 10; |
| 769 | proba += *argument - '0'; |
| 770 | argument++; |
| 771 | } |
| 772 | if (proba<0) proba=0; |
| 773 | if (proba>100) proba=100; |
| 774 | break; |
| 775 | |
| 776 | default: |
| 777 | return FUZ_usage(programName); |
| 778 | } |
| 779 | } } } /* for(argNb=1; argNb<argc; argNb++) */ |
| 780 | |
| 781 | /* Get Seed */ |
Yann Collet | bb85581 | 2016-08-25 19:11:11 +0200 | [diff] [blame] | 782 | DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 783 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 784 | if (!seedset) { |
| 785 | time_t const t = time(NULL); |
| 786 | U32 const h = XXH32(&t, sizeof(t), 1); |
| 787 | seed = h % 10000; |
| 788 | } |
| 789 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 790 | DISPLAY("Seed = %u\n", seed); |
| 791 | if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba); |
| 792 | |
| 793 | if (nbTests<=0) nbTests=1; |
| 794 | |
| 795 | if (testNb==0) { |
| 796 | result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */ |
| 797 | if (!result) { |
| 798 | DISPLAYLEVEL(4, "Unit tests using customMem :\n") |
| 799 | result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */ |
| 800 | } } |
| 801 | |
| 802 | if (!result) |
| 803 | result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); |
| 804 | |
| 805 | if (mainPause) { |
| 806 | int unused; |
| 807 | DISPLAY("Press Enter \n"); |
| 808 | unused = getchar(); |
| 809 | (void)unused; |
| 810 | } |
| 811 | return result; |
| 812 | } |