Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 5 | * This source code is licensed under both the BSD-style license (found in the |
| 6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 | * in the COPYING file in the root directory of this source tree). |
Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 10 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 11 | |
| 12 | /*-************************************ |
| 13 | * Compiler specific |
| 14 | **************************************/ |
| 15 | #ifdef _MSC_VER /* Visual Studio */ |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 16 | # define _CRT_SECURE_NO_WARNINGS /* fgets */ |
| 17 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 18 | # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 19 | #endif |
| 20 | |
| 21 | |
| 22 | /*-************************************ |
| 23 | * Includes |
| 24 | **************************************/ |
| 25 | #include <stdlib.h> /* free */ |
| 26 | #include <stdio.h> /* fgets, sscanf */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 27 | #include <time.h> /* clock_t, clock() */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 28 | #include <string.h> /* strcmp */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 29 | #include <assert.h> /* assert */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 30 | #include "mem.h" |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 31 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem, ZSTD_getDictID_fromFrame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 32 | #include "zstd.h" /* ZSTD_compressBound */ |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 33 | #include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 34 | #include "zstdmt_compress.h" |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 35 | #include "zdict.h" /* ZDICT_trainFromBuffer */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 36 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 37 | #define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 38 | #include "xxhash.h" /* XXH64_* */ |
| 39 | |
| 40 | |
| 41 | /*-************************************ |
| 42 | * Constants |
| 43 | **************************************/ |
| 44 | #define KB *(1U<<10) |
| 45 | #define MB *(1U<<20) |
| 46 | #define GB *(1U<<30) |
| 47 | |
| 48 | static const U32 nbTestsDefault = 10000; |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 49 | static const U32 g_cLevelMax_smallTests = 10; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 50 | #define COMPRESSIBLE_NOISE_LENGTH (10 MB) |
| 51 | #define FUZ_COMPRESSIBILITY_DEFAULT 50 |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 52 | static const U32 prime32 = 2654435761U; |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 53 | static const U32 windowLogMax = 27; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 54 | |
| 55 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 56 | /*-************************************ |
| 57 | * Display Macros |
| 58 | **************************************/ |
| 59 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 60 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { \ |
| 61 | DISPLAY(__VA_ARGS__); \ |
| 62 | if (g_displayLevel>=4) fflush(stderr); } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 63 | static U32 g_displayLevel = 2; |
| 64 | |
| 65 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 66 | if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 67 | { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \ |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 68 | if (g_displayLevel>=4) fflush(stderr); } } |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 69 | static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 70 | static clock_t g_displayClock = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 71 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 72 | static clock_t g_clockTime = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 73 | |
| 74 | |
| 75 | /*-******************************************************* |
| 76 | * Fuzzer functions |
| 77 | *********************************************************/ |
| 78 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
| 79 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 80 | static clock_t FUZ_GetClockSpan(clock_t clockStart) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 81 | { |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 82 | return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /*! FUZ_rand() : |
| 86 | @return : a 27 bits random value, from a 32-bits `seed`. |
| 87 | `seed` is also modified */ |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 88 | #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 89 | unsigned int FUZ_rand(unsigned int* seedPtr) |
| 90 | { |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 91 | static const U32 prime2 = 2246822519U; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 92 | U32 rand32 = *seedPtr; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 93 | rand32 *= prime32; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 94 | rand32 += prime2; |
| 95 | rand32 = FUZ_rotl32(rand32, 13); |
| 96 | *seedPtr = rand32; |
| 97 | return rand32 >> 5; |
| 98 | } |
| 99 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 100 | |
| 101 | /*====================================================== |
| 102 | * Basic Unit tests |
| 103 | ======================================================*/ |
| 104 | |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 105 | typedef struct { |
| 106 | void* start; |
| 107 | size_t size; |
| 108 | size_t filled; |
| 109 | } buffer_t; |
| 110 | |
| 111 | static const buffer_t g_nullBuffer = { NULL, 0 , 0 }; |
| 112 | |
| 113 | static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize) |
| 114 | { |
| 115 | buffer_t dict = { NULL, 0, 0 }; |
| 116 | size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize; |
| 117 | size_t* const blockSizes = (size_t*) malloc(nbBlocks * sizeof(size_t)); |
| 118 | if (!blockSizes) return dict; |
| 119 | dict.start = malloc(requestedDictSize); |
| 120 | if (!dict.start) { free(blockSizes); return dict; } |
| 121 | { size_t nb; |
| 122 | for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize; |
| 123 | blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1)); |
| 124 | } |
| 125 | { size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks); |
| 126 | free(blockSizes); |
Yann Collet | 2c5514c | 2017-04-18 22:52:41 -0700 | [diff] [blame] | 127 | if (ZDICT_isError(dictSize)) { free(dict.start); return g_nullBuffer; } |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 128 | dict.size = requestedDictSize; |
| 129 | dict.filled = dictSize; |
| 130 | return dict; /* how to return dictSize ? */ |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static void FUZ_freeDictionary(buffer_t dict) |
| 135 | { |
| 136 | free(dict.start); |
| 137 | } |
| 138 | |
| 139 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 140 | static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem) |
| 141 | { |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 142 | size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 143 | void* CNBuffer = malloc(CNBufferSize); |
| 144 | size_t const skippableFrameSize = 11; |
| 145 | size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH); |
| 146 | void* compressedBuffer = malloc(compressedBufferSize); |
| 147 | size_t const decodedBufferSize = CNBufferSize; |
| 148 | void* decodedBuffer = malloc(decodedBufferSize); |
| 149 | size_t cSize; |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 150 | int testResult = 0; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 151 | U32 testNb = 1; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 152 | ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem); |
| 153 | ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 154 | ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2); |
| 155 | |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 156 | ZSTD_inBuffer inBuff, inBuff2; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 157 | ZSTD_outBuffer outBuff; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 158 | buffer_t dictionary = g_nullBuffer; |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 159 | size_t const dictSize = 128 KB; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 160 | unsigned dictID = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 161 | |
| 162 | /* Create compressible test buffer */ |
| 163 | if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) { |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 164 | DISPLAY("Not enough memory, aborting \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 165 | goto _output_error; |
| 166 | } |
| 167 | RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed); |
| 168 | |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 169 | /* Create dictionary */ |
| 170 | MEM_STATIC_ASSERT(COMPRESSIBLE_NOISE_LENGTH >= 4 MB); |
| 171 | dictionary = FUZ_createDictionary(CNBuffer, 4 MB, 4 KB, 40 KB); |
| 172 | if (!dictionary.start) { |
| 173 | DISPLAY("Error creating dictionary, aborting \n"); |
| 174 | goto _output_error; |
| 175 | } |
| 176 | dictID = ZDICT_getDictID(dictionary.start, dictionary.filled); |
| 177 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 178 | /* generate skippable frame */ |
| 179 | MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START); |
| 180 | MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize); |
| 181 | cSize = skippableFrameSize + 8; |
| 182 | |
| 183 | /* Basic compression test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 184 | DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 185 | { size_t const r = ZSTD_initCStream_usingDict(zc, CNBuffer, dictSize, 1); |
| 186 | if (ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 187 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 188 | outBuff.size = compressedBufferSize; |
| 189 | outBuff.pos = 0; |
| 190 | inBuff.src = CNBuffer; |
| 191 | inBuff.size = CNBufferSize; |
| 192 | inBuff.pos = 0; |
| 193 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 194 | if (ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 195 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 196 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
Yann Collet | 9a021c1 | 2016-08-26 09:05:06 +0200 | [diff] [blame] | 197 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 198 | cSize += outBuff.pos; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 199 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 200 | |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 201 | /* context size functions */ |
| 202 | DISPLAYLEVEL(3, "test%3i : estimate CStream size : ", testNb++); |
| 203 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize); |
Stella Lau | b5b9275 | 2017-08-29 10:49:29 -0700 | [diff] [blame] | 204 | size_t const s = ZSTD_estimateCStreamSize_advanced_usingCParams(cParams) |
Yann Collet | 25989e3 | 2017-05-25 15:07:37 -0700 | [diff] [blame] | 205 | /* uses ZSTD_initCStream_usingDict() */ |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 206 | + ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy); |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 207 | if (ZSTD_isError(s)) goto _output_error; |
| 208 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); |
| 209 | } |
| 210 | |
| 211 | DISPLAYLEVEL(3, "test%3i : check actual CStream size : ", testNb++); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 212 | { size_t const s = ZSTD_sizeof_CStream(zc); |
| 213 | if (ZSTD_isError(s)) goto _output_error; |
| 214 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 217 | /* Attempt bad compression parameters */ |
| 218 | DISPLAYLEVEL(3, "test%3i : use bad compression parameters : ", testNb++); |
| 219 | { size_t r; |
| 220 | ZSTD_parameters params = ZSTD_getParams(1, 0, 0); |
| 221 | params.cParams.searchLength = 2; |
| 222 | r = ZSTD_initCStream_advanced(zc, NULL, 0, params, 0); |
| 223 | if (!ZSTD_isError(r)) goto _output_error; |
| 224 | DISPLAYLEVEL(3, "init error : %s \n", ZSTD_getErrorName(r)); |
| 225 | } |
| 226 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 227 | /* skippable frame test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 228 | DISPLAYLEVEL(3, "test%3i : decompress skippable frame : ", testNb++); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 229 | if (ZSTD_isError( ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize) )) |
| 230 | goto _output_error; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 231 | inBuff.src = compressedBuffer; |
| 232 | inBuff.size = cSize; |
| 233 | inBuff.pos = 0; |
| 234 | outBuff.dst = decodedBuffer; |
| 235 | outBuff.size = CNBufferSize; |
| 236 | outBuff.pos = 0; |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 237 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 238 | DISPLAYLEVEL(5, " ( ZSTD_decompressStream => %u ) ", (U32)r); |
| 239 | if (r != 0) goto _output_error; |
| 240 | } |
Yann Collet | a33ae64 | 2017-02-28 01:15:28 -0800 | [diff] [blame] | 241 | if (outBuff.pos != 0) goto _output_error; /* skippable frame output len is 0 */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 242 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 243 | |
| 244 | /* Basic decompression test */ |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 245 | inBuff2 = inBuff; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 246 | DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 247 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 248 | { size_t const r = ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000000000); /* large limit */ |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 249 | if (ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 250 | { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 251 | if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
| 252 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 253 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 254 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 255 | |
| 256 | /* Re-use without init */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 257 | DISPLAYLEVEL(3, "test%3i : decompress again without init (re-use previous settings): ", testNb++); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 258 | outBuff.pos = 0; |
| 259 | { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2); |
| 260 | 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] | 261 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 262 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 263 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 264 | |
| 265 | /* check regenerated data is byte exact */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 266 | DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 267 | { size_t i; |
| 268 | for (i=0; i<CNBufferSize; i++) { |
| 269 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error; |
| 270 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 271 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 272 | |
Yann Collet | f16f449 | 2017-05-09 16:18:17 -0700 | [diff] [blame] | 273 | /* context size functions */ |
| 274 | DISPLAYLEVEL(3, "test%3i : estimate DStream size : ", testNb++); |
| 275 | { ZSTD_frameHeader fhi; |
| 276 | const void* cStart = (char*)compressedBuffer + (skippableFrameSize + 8); |
| 277 | size_t const gfhError = ZSTD_getFrameHeader(&fhi, cStart, cSize); |
| 278 | if (gfhError!=0) goto _output_error; |
Yann Collet | dde10b2 | 2017-06-26 17:44:26 -0700 | [diff] [blame] | 279 | DISPLAYLEVEL(5, " (windowSize : %u) ", (U32)fhi.windowSize); |
| 280 | { size_t const s = ZSTD_estimateDStreamSize(fhi.windowSize) |
Yann Collet | 25989e3 | 2017-05-25 15:07:37 -0700 | [diff] [blame] | 281 | /* uses ZSTD_initDStream_usingDict() */ |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 282 | + ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy); |
Yann Collet | f16f449 | 2017-05-09 16:18:17 -0700 | [diff] [blame] | 283 | if (ZSTD_isError(s)) goto _output_error; |
| 284 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); |
| 285 | } } |
| 286 | |
| 287 | DISPLAYLEVEL(3, "test%3i : check actual DStream size : ", testNb++); |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 288 | { size_t const s = ZSTD_sizeof_DStream(zd); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 289 | if (ZSTD_isError(s)) goto _output_error; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 290 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 293 | /* Byte-by-byte decompression test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 294 | DISPLAYLEVEL(3, "test%3i : decompress byte-by-byte : ", testNb++); |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 295 | { /* skippable frame */ |
| 296 | size_t r = 1; |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 297 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 298 | inBuff.src = compressedBuffer; |
| 299 | outBuff.dst = decodedBuffer; |
| 300 | inBuff.pos = 0; |
| 301 | outBuff.pos = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 302 | while (r) { /* skippable frame */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 303 | inBuff.size = inBuff.pos + 1; |
| 304 | outBuff.size = outBuff.pos + 1; |
| 305 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 306 | if (ZSTD_isError(r)) goto _output_error; |
| 307 | } |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 308 | /* normal frame */ |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 309 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 310 | r=1; |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 311 | while (r) { |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 312 | inBuff.size = inBuff.pos + 1; |
| 313 | outBuff.size = outBuff.pos + 1; |
| 314 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 315 | if (ZSTD_isError(r)) goto _output_error; |
| 316 | } |
| 317 | } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 318 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 319 | if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 320 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 321 | |
| 322 | /* check regenerated data is byte exact */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 323 | DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 324 | { size_t i; |
| 325 | for (i=0; i<CNBufferSize; i++) { |
| 326 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; |
| 327 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 328 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 329 | |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 330 | /* _srcSize compression test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 331 | DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 332 | ZSTD_initCStream_srcSize(zc, 1, CNBufferSize); |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 333 | outBuff.dst = (char*)(compressedBuffer); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 334 | outBuff.size = compressedBufferSize; |
| 335 | outBuff.pos = 0; |
| 336 | inBuff.src = CNBuffer; |
| 337 | inBuff.size = CNBufferSize; |
| 338 | inBuff.pos = 0; |
| 339 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 340 | if (ZSTD_isError(r)) goto _output_error; } |
| 341 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 342 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 343 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 344 | { unsigned long long origSize = ZSTD_findDecompressedSize(outBuff.dst, outBuff.pos); |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 345 | if ((size_t)origSize != CNBufferSize) goto _output_error; } /* exact original size must be present */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 346 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 347 | |
| 348 | /* wrong _srcSize compression test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 349 | DISPLAYLEVEL(3, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 350 | ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1); |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 351 | outBuff.dst = (char*)(compressedBuffer); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 352 | outBuff.size = compressedBufferSize; |
| 353 | outBuff.pos = 0; |
| 354 | inBuff.src = CNBuffer; |
| 355 | inBuff.size = CNBufferSize; |
| 356 | inBuff.pos = 0; |
| 357 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 358 | if (ZSTD_isError(r)) goto _output_error; } |
| 359 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 360 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 361 | if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 362 | DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); } |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 363 | |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 364 | /* Complex context re-use scenario */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 365 | DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 366 | ZSTD_freeCStream(zc); |
| 367 | zc = ZSTD_createCStream_advanced(customMem); |
| 368 | if (zc==NULL) goto _output_error; /* memory allocation issue */ |
| 369 | /* use 1 */ |
| 370 | { size_t const inSize = 513; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 371 | DISPLAYLEVEL(5, "use1 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 372 | ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */ |
| 373 | inBuff.src = CNBuffer; |
| 374 | inBuff.size = inSize; |
| 375 | inBuff.pos = 0; |
| 376 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 377 | outBuff.size = ZSTD_compressBound(inSize); |
| 378 | outBuff.pos = 0; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 379 | DISPLAYLEVEL(5, "compress1 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 380 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 381 | if (ZSTD_isError(r)) goto _output_error; } |
| 382 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 383 | DISPLAYLEVEL(5, "end1 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 384 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 385 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 386 | } |
| 387 | /* use 2 */ |
| 388 | { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */ |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 389 | DISPLAYLEVEL(5, "use2 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 390 | ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */ |
| 391 | inBuff.src = CNBuffer; |
| 392 | inBuff.size = inSize; |
| 393 | inBuff.pos = 0; |
| 394 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 395 | outBuff.size = ZSTD_compressBound(inSize); |
| 396 | outBuff.pos = 0; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 397 | DISPLAYLEVEL(5, "compress2 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 398 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 399 | if (ZSTD_isError(r)) goto _output_error; } |
| 400 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 401 | DISPLAYLEVEL(5, "end2 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 402 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 403 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 404 | } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 405 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 406 | |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 407 | /* CDict scenario */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 408 | DISPLAYLEVEL(3, "test%3i : digested dictionary : ", testNb++); |
Yann Collet | 2e42742 | 2017-06-27 17:09:12 -0700 | [diff] [blame] | 409 | { ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, 1 /*byRef*/ ); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 410 | size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict); |
| 411 | if (ZSTD_isError(initError)) goto _output_error; |
| 412 | cSize = 0; |
| 413 | outBuff.dst = compressedBuffer; |
| 414 | outBuff.size = compressedBufferSize; |
| 415 | outBuff.pos = 0; |
| 416 | inBuff.src = CNBuffer; |
| 417 | inBuff.size = CNBufferSize; |
| 418 | inBuff.pos = 0; |
| 419 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 420 | if (ZSTD_isError(r)) goto _output_error; } |
| 421 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 422 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 423 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 424 | cSize = outBuff.pos; |
| 425 | ZSTD_freeCDict(cdict); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 426 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 429 | DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 430 | { size_t const s = ZSTD_sizeof_CStream(zc); |
| 431 | if (ZSTD_isError(s)) goto _output_error; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 432 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 435 | DISPLAYLEVEL(4, "test%3i : check Dictionary ID : ", testNb++); |
| 436 | { unsigned const dID = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 437 | if (dID != dictID) goto _output_error; |
| 438 | DISPLAYLEVEL(4, "OK (%u) \n", dID); |
| 439 | } |
| 440 | |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 441 | /* DDict scenario */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 442 | DISPLAYLEVEL(3, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize); |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 443 | { ZSTD_DDict* const ddict = ZSTD_createDDict(dictionary.start, dictionary.filled); |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 444 | size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict); |
| 445 | if (ZSTD_isError(initError)) goto _output_error; |
| 446 | inBuff.src = compressedBuffer; |
| 447 | inBuff.size = cSize; |
| 448 | inBuff.pos = 0; |
| 449 | outBuff.dst = decodedBuffer; |
| 450 | outBuff.size = CNBufferSize; |
| 451 | outBuff.pos = 0; |
| 452 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 453 | if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
| 454 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 455 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
| 456 | ZSTD_freeDDict(ddict); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 457 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 460 | /* test ZSTD_setDStreamParameter() resilience */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 461 | DISPLAYLEVEL(3, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++); |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 462 | { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */ |
| 463 | if (!ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 464 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 465 | |
| 466 | /* Memory restriction */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 467 | DISPLAYLEVEL(3, "test%3i : maxWindowSize < frame requirement : ", testNb++); |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 468 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 469 | { size_t const r = ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000); /* too small limit */ |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 470 | if (ZSTD_isError(r)) goto _output_error; } |
| 471 | inBuff.src = compressedBuffer; |
| 472 | inBuff.size = cSize; |
| 473 | inBuff.pos = 0; |
| 474 | outBuff.dst = decodedBuffer; |
| 475 | outBuff.size = CNBufferSize; |
| 476 | outBuff.pos = 0; |
| 477 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 478 | if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 479 | DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); } |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 480 | |
Yann Collet | 7d283cd | 2017-04-27 14:48:34 -0700 | [diff] [blame] | 481 | DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_usingCDict_advanced with masked dictID : ", testNb++); |
| 482 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictionary.filled); |
| 483 | ZSTD_frameParameters const fParams = { 1 /* contentSize */, 1 /* checksum */, 1 /* noDictID */}; |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 484 | ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dm_auto, cParams, customMem); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 485 | size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, fParams, CNBufferSize); |
Yann Collet | 7d283cd | 2017-04-27 14:48:34 -0700 | [diff] [blame] | 486 | if (ZSTD_isError(initError)) goto _output_error; |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 487 | cSize = 0; |
| 488 | outBuff.dst = compressedBuffer; |
| 489 | outBuff.size = compressedBufferSize; |
| 490 | outBuff.pos = 0; |
| 491 | inBuff.src = CNBuffer; |
| 492 | inBuff.size = CNBufferSize; |
| 493 | inBuff.pos = 0; |
| 494 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 495 | if (ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 496 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 497 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 498 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 499 | cSize = outBuff.pos; |
| 500 | ZSTD_freeCDict(cdict); |
| 501 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100); |
Yann Collet | 4ee6b15 | 2017-04-11 11:59:44 -0700 | [diff] [blame] | 502 | } |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 503 | |
Yann Collet | a92cbb7 | 2017-04-27 15:08:56 -0700 | [diff] [blame] | 504 | DISPLAYLEVEL(3, "test%3i : try retrieving dictID from frame : ", testNb++); |
| 505 | { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 506 | if (did != 0) goto _output_error; |
| 507 | } |
| 508 | DISPLAYLEVEL(3, "OK (not detected) \n"); |
| 509 | |
Yann Collet | 4ee6b15 | 2017-04-11 11:59:44 -0700 | [diff] [blame] | 510 | DISPLAYLEVEL(3, "test%3i : decompress without dictionary : ", testNb++); |
| 511 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize); |
| 512 | if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */ |
| 513 | DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 516 | DISPLAYLEVEL(3, "test%3i : compress with ZSTD_CCtx_refPrefix : ", testNb++); |
| 517 | { size_t const refErr = ZSTD_CCtx_refPrefix(zc, dictionary.start, dictionary.filled); |
| 518 | if (ZSTD_isError(refErr)) goto _output_error; } |
| 519 | outBuff.dst = compressedBuffer; |
| 520 | outBuff.size = compressedBufferSize; |
| 521 | outBuff.pos = 0; |
| 522 | inBuff.src = CNBuffer; |
| 523 | inBuff.size = CNBufferSize; |
| 524 | inBuff.pos = 0; |
| 525 | { size_t const r = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end); |
| 526 | if (ZSTD_isError(r)) goto _output_error; } |
| 527 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 528 | cSize = outBuff.pos; |
| 529 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100); |
| 530 | |
| 531 | DISPLAYLEVEL(3, "test%3i : decompress with dictionary : ", testNb++); |
| 532 | { size_t const r = ZSTD_decompress_usingDict(zd, |
| 533 | decodedBuffer, CNBufferSize, |
| 534 | compressedBuffer, cSize, |
| 535 | dictionary.start, dictionary.filled); |
| 536 | if (ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */ |
| 537 | DISPLAYLEVEL(3, "OK \n"); |
| 538 | } |
| 539 | |
| 540 | DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should fail): ", testNb++); |
| 541 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize); |
| 542 | if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */ |
| 543 | DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); |
| 544 | } |
| 545 | |
| 546 | DISPLAYLEVEL(3, "test%3i : compress again with ZSTD_compress_generic : ", testNb++); |
| 547 | outBuff.dst = compressedBuffer; |
| 548 | outBuff.size = compressedBufferSize; |
| 549 | outBuff.pos = 0; |
| 550 | inBuff.src = CNBuffer; |
| 551 | inBuff.size = CNBufferSize; |
| 552 | inBuff.pos = 0; |
| 553 | { size_t const r = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end); |
| 554 | if (ZSTD_isError(r)) goto _output_error; } |
| 555 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 556 | cSize = outBuff.pos; |
| 557 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100); |
| 558 | |
| 559 | DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should work): ", testNb++); |
| 560 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize); |
| 561 | if (ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */ |
| 562 | DISPLAYLEVEL(3, "OK \n"); |
| 563 | } |
| 564 | |
Yann Collet | 0bb381d | 2017-04-18 15:08:52 -0700 | [diff] [blame] | 565 | /* Empty srcSize */ |
| 566 | DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced with pledgedSrcSize=0 and dict : ", testNb++); |
| 567 | { ZSTD_parameters params = ZSTD_getParams(5, 0, 0); |
| 568 | params.fParams.contentSizeFlag = 1; |
| 569 | ZSTD_initCStream_advanced(zc, dictionary.start, dictionary.filled, params, 0); |
| 570 | } /* cstream advanced shall write content size = 0 */ |
| 571 | inBuff.src = CNBuffer; |
| 572 | inBuff.size = 0; |
| 573 | inBuff.pos = 0; |
| 574 | outBuff.dst = compressedBuffer; |
| 575 | outBuff.size = compressedBufferSize; |
| 576 | outBuff.pos = 0; |
| 577 | if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error; |
| 578 | if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error; |
| 579 | cSize = outBuff.pos; |
| 580 | if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error; |
| 581 | DISPLAYLEVEL(3, "OK \n"); |
| 582 | |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 583 | DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++); |
| 584 | { ZSTD_parameters params = ZSTD_getParams(5, 0, 0); |
| 585 | params.fParams.contentSizeFlag = 1; |
Yann Collet | 4ee6b15 | 2017-04-11 11:59:44 -0700 | [diff] [blame] | 586 | ZSTD_initCStream_advanced(zc, NULL, 0, params, 0); |
| 587 | } /* cstream advanced shall write content size = 0 */ |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 588 | inBuff.src = CNBuffer; |
| 589 | inBuff.size = 0; |
| 590 | inBuff.pos = 0; |
| 591 | outBuff.dst = compressedBuffer; |
| 592 | outBuff.size = compressedBufferSize; |
| 593 | outBuff.pos = 0; |
| 594 | if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error; |
| 595 | if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error; |
| 596 | cSize = outBuff.pos; |
| 597 | if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error; |
| 598 | |
| 599 | ZSTD_resetCStream(zc, 0); /* resetCStream should treat 0 as unknown */ |
| 600 | inBuff.src = CNBuffer; |
| 601 | inBuff.size = 0; |
| 602 | inBuff.pos = 0; |
| 603 | outBuff.dst = compressedBuffer; |
| 604 | outBuff.size = compressedBufferSize; |
| 605 | outBuff.pos = 0; |
| 606 | if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error; |
| 607 | if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error; |
| 608 | cSize = outBuff.pos; |
| 609 | if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error; |
| 610 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 611 | |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 612 | /* Basic multithreading compression test */ |
| 613 | DISPLAYLEVEL(3, "test%3i : compress %u bytes with multiple threads : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 614 | { ZSTD_parameters const params = ZSTD_getParams(1, 0, 0); |
| 615 | size_t const r = ZSTDMT_initCStream_advanced(mtctx, CNBuffer, dictSize, params, CNBufferSize); |
| 616 | if (ZSTD_isError(r)) goto _output_error; } |
| 617 | outBuff.dst = (char*)(compressedBuffer); |
| 618 | outBuff.size = compressedBufferSize; |
| 619 | outBuff.pos = 0; |
| 620 | inBuff.src = CNBuffer; |
| 621 | inBuff.size = CNBufferSize; |
| 622 | inBuff.pos = 0; |
| 623 | { size_t const r = ZSTDMT_compressStream_generic(mtctx, &outBuff, &inBuff, ZSTD_e_end); |
| 624 | if (ZSTD_isError(r)) goto _output_error; } |
| 625 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 626 | { size_t const r = ZSTDMT_endStream(mtctx, &outBuff); |
| 627 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 628 | DISPLAYLEVEL(3, "OK \n"); |
| 629 | |
| 630 | |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 631 | /* Overlen overwriting window data bug */ |
| 632 | DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++); |
Sean Purcell | 0ed3901 | 2017-02-16 13:29:47 -0800 | [diff] [blame] | 633 | { /* This test has a window size of 1024 bytes and consists of 3 blocks: |
| 634 | 1. 'a' repeated 517 times |
| 635 | 2. 'b' repeated 516 times |
| 636 | 3. a compressed block with no literals and 3 sequence commands: |
| 637 | litlength = 0, offset = 24, match length = 24 |
| 638 | litlength = 0, offset = 24, match length = 3 (this one creates an overlength write of length 2*WILDCOPY_OVERLENGTH - 3) |
| 639 | litlength = 0, offset = 1021, match length = 3 (this one will try to read from overwritten data if the buffer is too small) */ |
| 640 | |
| 641 | const char* testCase = |
| 642 | "\x28\xB5\x2F\xFD\x04\x00\x4C\x00\x00\x10\x61\x61\x01\x00\x00\x2A" |
| 643 | "\x80\x05\x44\x00\x00\x08\x62\x01\x00\x00\x2A\x20\x04\x5D\x00\x00" |
| 644 | "\x00\x03\x40\x00\x00\x64\x60\x27\xB0\xE0\x0C\x67\x62\xCE\xE0"; |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 645 | ZSTD_DStream* zds = ZSTD_createDStream(); |
| 646 | |
| 647 | ZSTD_initDStream(zds); |
| 648 | inBuff.src = testCase; |
Sean Purcell | 0ed3901 | 2017-02-16 13:29:47 -0800 | [diff] [blame] | 649 | inBuff.size = 47; |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 650 | inBuff.pos = 0; |
| 651 | outBuff.dst = decodedBuffer; |
| 652 | outBuff.size = CNBufferSize; |
| 653 | outBuff.pos = 0; |
| 654 | |
| 655 | while (inBuff.pos < inBuff.size) { |
| 656 | size_t const r = ZSTD_decompressStream(zds, &outBuff, &inBuff); |
| 657 | /* Bug will cause checksum to fail */ |
| 658 | if (ZSTD_isError(r)) goto _output_error; |
| 659 | } |
Przemyslaw Skibinski | 684858e | 2017-02-21 18:17:24 +0100 | [diff] [blame] | 660 | |
| 661 | ZSTD_freeDStream(zds); |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 662 | } |
| 663 | DISPLAYLEVEL(3, "OK \n"); |
| 664 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 665 | _end: |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 666 | FUZ_freeDictionary(dictionary); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 667 | ZSTD_freeCStream(zc); |
| 668 | ZSTD_freeDStream(zd); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 669 | ZSTDMT_freeCCtx(mtctx); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 670 | free(CNBuffer); |
| 671 | free(compressedBuffer); |
| 672 | free(decodedBuffer); |
| 673 | return testResult; |
| 674 | |
| 675 | _output_error: |
| 676 | testResult = 1; |
| 677 | DISPLAY("Error detected in Unit tests ! \n"); |
| 678 | goto _end; |
| 679 | } |
| 680 | |
| 681 | |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 682 | /* ====== Fuzzer tests ====== */ |
| 683 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 684 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 685 | { |
| 686 | const BYTE* b1 = (const BYTE*)buf1; |
| 687 | const BYTE* b2 = (const BYTE*)buf2; |
| 688 | size_t u; |
| 689 | for (u=0; u<max; u++) { |
| 690 | if (b1[u] != b2[u]) break; |
| 691 | } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 692 | DISPLAY("Error at position %u / %u \n", (U32)u, (U32)max); |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 693 | DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n", |
| 694 | b1[u-3], b1[u-2], b1[u-1], b1[u-0], b1[u+1], b1[u+2], b1[u+3], b1[u+4], b1[u+5]); |
| 695 | DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n", |
| 696 | b2[u-3], b2[u-2], b2[u-1], b2[u-0], b2[u+1], b2[u+2], b2[u+3], b2[u+4], b2[u+5]); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 697 | return u; |
| 698 | } |
| 699 | |
| 700 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 701 | { |
| 702 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 703 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 704 | } |
| 705 | |
| 706 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 707 | { |
| 708 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 709 | return FUZ_rLogLength(seed, logLength); |
| 710 | } |
| 711 | |
| 712 | #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) |
| 713 | |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 714 | /* Return value in range minVal <= v <= maxVal */ |
| 715 | static U32 FUZ_randomClampedLength(U32* seed, U32 minVal, U32 maxVal) |
| 716 | { |
| 717 | U32 const mod = maxVal < minVal ? 1 : (maxVal + 1) - minVal; |
| 718 | return (U32)((FUZ_rand(seed) % mod) + minVal); |
| 719 | } |
| 720 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 721 | #define CHECK(cond, ...) { \ |
| 722 | if (cond) { \ |
| 723 | DISPLAY("Error => "); \ |
| 724 | DISPLAY(__VA_ARGS__); \ |
| 725 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); \ |
| 726 | goto _output_error; \ |
| 727 | } } |
| 728 | |
| 729 | #define CHECK_Z(f) { \ |
| 730 | size_t const err = f; \ |
| 731 | if (ZSTD_isError(err)) { \ |
| 732 | DISPLAY("Error => %s : %s ", \ |
| 733 | #f, ZSTD_getErrorName(err)); \ |
| 734 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); \ |
| 735 | goto _output_error; \ |
| 736 | } } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 737 | |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 738 | static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 739 | { |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 740 | U32 const maxSrcLog = bigTests ? 24 : 22; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 741 | static const U32 maxSampleLog = 19; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 742 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 743 | BYTE* cNoiseBuffer[5]; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 744 | size_t const copyBufferSize = srcBufferSize + (1<<maxSampleLog); |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 745 | BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize); |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 746 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 747 | BYTE* const cBuffer = (BYTE*)malloc (cBufferSize); |
| 748 | size_t const dstBufferSize = srcBufferSize; |
| 749 | BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 750 | U32 result = 0; |
| 751 | U32 testNb = 0; |
| 752 | U32 coreSeed = seed; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 753 | ZSTD_CStream* zc = ZSTD_createCStream(); /* will be re-created sometimes */ |
| 754 | ZSTD_DStream* zd = ZSTD_createDStream(); /* will be re-created sometimes */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 755 | ZSTD_DStream* const zd_noise = ZSTD_createDStream(); |
| 756 | clock_t const startClock = clock(); |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 757 | const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */ |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 758 | size_t dictSize = 0; |
| 759 | U32 oldTestLog = 0; |
Yann Collet | 49f8459 | 2017-06-21 18:43:39 -0700 | [diff] [blame] | 760 | U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel() : g_cLevelMax_smallTests; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 761 | |
| 762 | /* allocations */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 763 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 764 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 765 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 766 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 767 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 768 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 769 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise , |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 770 | "Not enough memory, fuzzer tests cancelled"); |
| 771 | |
| 772 | /* Create initial samples */ |
| 773 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 774 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 775 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 776 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 777 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 778 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 779 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 780 | |
| 781 | /* catch up testNb */ |
| 782 | for (testNb=1; testNb < startTest; testNb++) |
| 783 | FUZ_rand(&coreSeed); |
| 784 | |
| 785 | /* test loop */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 786 | for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 787 | U32 lseed; |
| 788 | const BYTE* srcBuffer; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 789 | size_t totalTestSize, totalGenSize, cSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 790 | XXH64_state_t xxhState; |
| 791 | U64 crcOrig; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 792 | U32 resetAllowed = 1; |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 793 | size_t maxTestSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 794 | |
| 795 | /* init */ |
Yann Collet | 4c0b44f | 2016-11-01 11:13:22 -0700 | [diff] [blame] | 796 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
| 797 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 798 | FUZ_rand(&coreSeed); |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 799 | lseed = coreSeed ^ prime32; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 800 | |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 801 | /* states full reset (deliberately not synchronized) */ |
| 802 | /* some issues can only happen when reusing states */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 803 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { |
| 804 | ZSTD_freeCStream(zc); |
| 805 | zc = ZSTD_createCStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 806 | CHECK(zc==NULL, "ZSTD_createCStream : allocation error"); |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 807 | resetAllowed=0; |
| 808 | } |
| 809 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { |
| 810 | ZSTD_freeDStream(zd); |
| 811 | zd = ZSTD_createDStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 812 | CHECK(zd==NULL, "ZSTD_createDStream : allocation error"); |
| 813 | CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 814 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 815 | |
| 816 | /* srcBuffer selection [0-4] */ |
| 817 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 818 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 819 | else { |
| 820 | buffNb >>= 3; |
| 821 | if (buffNb & 7) { |
| 822 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 823 | buffNb = tnb[buffNb >> 3]; |
| 824 | } else { |
| 825 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 826 | buffNb = tnb[buffNb >> 3]; |
| 827 | } } |
| 828 | srcBuffer = cNoiseBuffer[buffNb]; |
| 829 | } |
| 830 | |
| 831 | /* compression init */ |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 832 | if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ |
| 833 | && oldTestLog /* at least one test happened */ && resetAllowed) { |
| 834 | maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 835 | maxTestSize = MIN(maxTestSize, srcBufferSize-16); |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 836 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 837 | CHECK_Z( ZSTD_resetCStream(zc, pledgedSrcSize) ); |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 838 | } |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 839 | } else { |
| 840 | U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 841 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 842 | U32 const cLevelCandidate = ( FUZ_rand(&lseed) % |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 843 | (ZSTD_maxCLevel() - |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 844 | (MAX(testLog, dictLog) / 3))) |
Yann Collet | ce80098 | 2017-04-05 16:34:09 -0700 | [diff] [blame] | 845 | + 1; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 846 | U32 const cLevel = MIN(cLevelCandidate, cLevelMax); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 847 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 848 | oldTestLog = testLog; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 849 | /* random dictionary selection */ |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 850 | dictSize = ((FUZ_rand(&lseed)&7)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 851 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 852 | dict = srcBuffer + dictStart; |
| 853 | } |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 854 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize; |
| 855 | ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 856 | params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; |
| 857 | params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 858 | CHECK_Z ( ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) ); |
| 859 | } } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 860 | |
| 861 | /* multi-segments compression test */ |
| 862 | XXH64_reset(&xxhState, 0); |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 863 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 864 | U32 n; |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 865 | for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) { |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 866 | /* compress random chunks into randomly sized dst buffers */ |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 867 | { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 868 | size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize); |
| 869 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 870 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 871 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 872 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 873 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 874 | |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 875 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 876 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 877 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 878 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 879 | totalTestSize += inBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /* random flush operation, to mess around */ |
| 883 | if ((FUZ_rand(&lseed) & 15) == 0) { |
| 884 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 885 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 886 | outBuff.size = outBuff.pos + adjustedDstSize; |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 887 | CHECK_Z( ZSTD_flushStream(zc, &outBuff) ); |
| 888 | } } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 889 | |
| 890 | /* final frame epilogue */ |
| 891 | { size_t remainingToFlush = (size_t)(-1); |
| 892 | while (remainingToFlush) { |
| 893 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 894 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 895 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 896 | remainingToFlush = ZSTD_endStream(zc, &outBuff); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 897 | CHECK (ZSTD_isError(remainingToFlush), "end error : %s", ZSTD_getErrorName(remainingToFlush)); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 898 | } } |
| 899 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 900 | cSize = outBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /* multi - fragments decompression test */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 904 | if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) { |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 905 | CHECK_Z ( ZSTD_resetDStream(zd) ); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 906 | } else { |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 907 | CHECK_Z ( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 908 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 909 | { size_t decompressionResult = 1; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 910 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 911 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 912 | for (totalGenSize = 0 ; decompressionResult ; ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 913 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 914 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 915 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 916 | inBuff.size = inBuff.pos + readCSrcSize; |
| 917 | outBuff.size = inBuff.pos + dstBuffSize; |
| 918 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | b3f33cc | 2017-09-09 14:37:28 -0700 | [diff] [blame] | 919 | if (ZSTD_getErrorCode(decompressionResult) == ZSTD_error_checksum_wrong) { |
| 920 | DISPLAY("checksum error : \n"); |
| 921 | findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 922 | } |
| 923 | CHECK( ZSTD_isError(decompressionResult), "decompression error : %s", |
| 924 | ZSTD_getErrorName(decompressionResult) ); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 925 | } |
| 926 | CHECK (decompressionResult != 0, "frame not fully decoded"); |
Yann Collet | b3f33cc | 2017-09-09 14:37:28 -0700 | [diff] [blame] | 927 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", |
| 928 | (U32)outBuff.pos, (U32)totalTestSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 929 | CHECK (inBuff.pos != cSize, "compressed data should be fully read") |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 930 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 931 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 932 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 933 | } } |
| 934 | |
| 935 | /*===== noisy/erroneous src decompression test =====*/ |
| 936 | |
| 937 | /* add some noise */ |
| 938 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 939 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 940 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 941 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 942 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 943 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 944 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 945 | } } |
| 946 | |
| 947 | /* try decompression on noisy data */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 948 | CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 949 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 950 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 951 | while (outBuff.pos < dstBufferSize) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 952 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 953 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 954 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
Yann Collet | 64bf8ff | 2017-01-27 17:25:07 -0800 | [diff] [blame] | 955 | size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 956 | outBuff.size = outBuff.pos + adjustedDstSize; |
Yann Collet | 64bf8ff | 2017-01-27 17:25:07 -0800 | [diff] [blame] | 957 | inBuff.size = inBuff.pos + adjustedCSrcSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 958 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 959 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
Yann Collet | 64bf8ff | 2017-01-27 17:25:07 -0800 | [diff] [blame] | 960 | /* No forward progress possible */ |
| 961 | if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 962 | } } } } |
| 963 | DISPLAY("\r%u fuzzer tests completed \n", testNb); |
| 964 | |
| 965 | _cleanup: |
| 966 | ZSTD_freeCStream(zc); |
| 967 | ZSTD_freeDStream(zd); |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 968 | ZSTD_freeDStream(zd_noise); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 969 | free(cNoiseBuffer[0]); |
| 970 | free(cNoiseBuffer[1]); |
| 971 | free(cNoiseBuffer[2]); |
| 972 | free(cNoiseBuffer[3]); |
| 973 | free(cNoiseBuffer[4]); |
| 974 | free(copyBuffer); |
| 975 | free(cBuffer); |
| 976 | free(dstBuffer); |
| 977 | return result; |
| 978 | |
| 979 | _output_error: |
| 980 | result = 1; |
| 981 | goto _cleanup; |
| 982 | } |
| 983 | |
| 984 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 985 | /* Multi-threading version of fuzzer Tests */ |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 986 | static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests) |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 987 | { |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 988 | const U32 maxSrcLog = bigTests ? 24 : 22; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 989 | static const U32 maxSampleLog = 19; |
| 990 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
| 991 | BYTE* cNoiseBuffer[5]; |
| 992 | size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog); |
| 993 | BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize); |
| 994 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
| 995 | BYTE* const cBuffer = (BYTE*)malloc (cBufferSize); |
| 996 | size_t const dstBufferSize = srcBufferSize; |
| 997 | BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize); |
| 998 | U32 result = 0; |
| 999 | U32 testNb = 0; |
| 1000 | U32 coreSeed = seed; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1001 | U32 nbThreads = 2; |
| 1002 | ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(nbThreads); /* will be reset sometimes */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1003 | ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */ |
| 1004 | ZSTD_DStream* const zd_noise = ZSTD_createDStream(); |
| 1005 | clock_t const startClock = clock(); |
| 1006 | const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */ |
| 1007 | size_t dictSize = 0; |
| 1008 | U32 oldTestLog = 0; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1009 | int const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests; |
| 1010 | U32 const nbThreadsMax = bigTests ? 4 : 2; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1011 | |
| 1012 | /* allocations */ |
| 1013 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 1014 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 1015 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 1016 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 1017 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| 1018 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
| 1019 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise , |
| 1020 | "Not enough memory, fuzzer tests cancelled"); |
| 1021 | |
| 1022 | /* Create initial samples */ |
| 1023 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 1024 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 1025 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 1026 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 1027 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 1028 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
| 1029 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
| 1030 | |
| 1031 | /* catch up testNb */ |
| 1032 | for (testNb=1; testNb < startTest; testNb++) |
| 1033 | FUZ_rand(&coreSeed); |
| 1034 | |
| 1035 | /* test loop */ |
| 1036 | for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) { |
| 1037 | U32 lseed; |
| 1038 | const BYTE* srcBuffer; |
| 1039 | size_t totalTestSize, totalGenSize, cSize; |
| 1040 | XXH64_state_t xxhState; |
| 1041 | U64 crcOrig; |
| 1042 | U32 resetAllowed = 1; |
| 1043 | size_t maxTestSize; |
| 1044 | |
| 1045 | /* init */ |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1046 | if (testNb < nbTests) { |
| 1047 | DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); |
| 1048 | } else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1049 | FUZ_rand(&coreSeed); |
Yann Collet | d7e3cb5 | 2017-01-20 16:44:50 -0800 | [diff] [blame] | 1050 | lseed = coreSeed ^ prime32; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1051 | |
| 1052 | /* states full reset (deliberately not synchronized) */ |
| 1053 | /* some issues can only happen when reusing states */ |
| 1054 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1055 | U32 const nbThreadsCandidate = (FUZ_rand(&lseed) % 6) + 1; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1056 | nbThreads = MIN(nbThreadsCandidate, nbThreadsMax); |
Yann Collet | 30c7698 | 2017-03-31 18:27:03 -0700 | [diff] [blame] | 1057 | DISPLAYLEVEL(5, "Creating new context with %u threads \n", nbThreads); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1058 | ZSTDMT_freeCCtx(zc); |
| 1059 | zc = ZSTDMT_createCCtx(nbThreads); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1060 | CHECK(zc==NULL, "ZSTDMT_createCCtx allocation error") |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1061 | resetAllowed=0; |
| 1062 | } |
| 1063 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { |
| 1064 | ZSTD_freeDStream(zd); |
| 1065 | zd = ZSTD_createDStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1066 | CHECK(zd==NULL, "ZSTDMT_createCCtx allocation error") |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1067 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
| 1068 | } |
| 1069 | |
| 1070 | /* srcBuffer selection [0-4] */ |
| 1071 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 1072 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 1073 | else { |
| 1074 | buffNb >>= 3; |
| 1075 | if (buffNb & 7) { |
| 1076 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 1077 | buffNb = tnb[buffNb >> 3]; |
| 1078 | } else { |
| 1079 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 1080 | buffNb = tnb[buffNb >> 3]; |
| 1081 | } } |
| 1082 | srcBuffer = cNoiseBuffer[buffNb]; |
| 1083 | } |
| 1084 | |
| 1085 | /* compression init */ |
| 1086 | if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ |
| 1087 | && oldTestLog /* at least one test happened */ && resetAllowed) { |
| 1088 | maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); |
| 1089 | if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1; |
| 1090 | { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1; |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1091 | CHECK_Z( ZSTDMT_initCStream(zc, compressionLevel) ); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1092 | } |
| 1093 | } else { |
| 1094 | U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 1095 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1096 | int const cLevelCandidate = ( FUZ_rand(&lseed) |
| 1097 | % (ZSTD_maxCLevel() - (MAX(testLog, dictLog) / 2)) ) |
| 1098 | + 1; |
| 1099 | int const cLevelThreadAdjusted = cLevelCandidate - (nbThreads * 2) + 2; /* reduce cLevel when multiple threads to reduce memory consumption */ |
| 1100 | int const cLevelMin = MAX(cLevelThreadAdjusted, 1); /* no negative cLevel yet */ |
| 1101 | int const cLevel = MIN(cLevelMin, cLevelMax); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1102 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
| 1103 | oldTestLog = testLog; |
| 1104 | /* random dictionary selection */ |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 1105 | dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1106 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 1107 | dict = srcBuffer + dictStart; |
| 1108 | } |
| 1109 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize; |
| 1110 | ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize); |
Yann Collet | ce80098 | 2017-04-05 16:34:09 -0700 | [diff] [blame] | 1111 | DISPLAYLEVEL(5, "Init with windowLog = %u and pledgedSrcSize = %u \n", |
| 1112 | params.cParams.windowLog, (U32)pledgedSrcSize); |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1113 | params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; |
| 1114 | params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | ce80098 | 2017-04-05 16:34:09 -0700 | [diff] [blame] | 1115 | params.fParams.contentSizeFlag = pledgedSrcSize>0; |
Yann Collet | 2c5514c | 2017-04-18 22:52:41 -0700 | [diff] [blame] | 1116 | DISPLAYLEVEL(5, "checksumFlag : %u \n", params.fParams.checksumFlag); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1117 | CHECK_Z( ZSTDMT_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) ); |
| 1118 | CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_overlapSectionLog, FUZ_rand(&lseed) % 12) ); |
| 1119 | CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_sectionSize, FUZ_rand(&lseed) % (2*maxTestSize+1)) ); |
Yann Collet | cd23dd2 | 2017-01-30 12:46:35 -0800 | [diff] [blame] | 1120 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1121 | |
| 1122 | /* multi-segments compression test */ |
| 1123 | XXH64_reset(&xxhState, 0); |
| 1124 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
| 1125 | U32 n; |
| 1126 | for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) { |
| 1127 | /* compress random chunks into randomly sized dst buffers */ |
| 1128 | { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1129 | size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize); |
| 1130 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); |
| 1131 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1132 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1133 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; |
| 1134 | outBuff.size = outBuff.pos + dstBuffSize; |
| 1135 | |
| 1136 | DISPLAYLEVEL(5, "Sending %u bytes to compress \n", (U32)srcSize); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1137 | CHECK_Z( ZSTDMT_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1138 | DISPLAYLEVEL(5, "%u bytes read by ZSTDMT_compressStream \n", (U32)inBuff.pos); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1139 | |
| 1140 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 1141 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 1142 | totalTestSize += inBuff.pos; |
| 1143 | } |
| 1144 | |
| 1145 | /* random flush operation, to mess around */ |
| 1146 | if ((FUZ_rand(&lseed) & 15) == 0) { |
| 1147 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1148 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1149 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 1150 | DISPLAYLEVEL(5, "Flushing into dst buffer of size %u \n", (U32)adjustedDstSize); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1151 | CHECK_Z( ZSTDMT_flushStream(zc, &outBuff) ); |
| 1152 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1153 | |
| 1154 | /* final frame epilogue */ |
| 1155 | { size_t remainingToFlush = (size_t)(-1); |
| 1156 | while (remainingToFlush) { |
| 1157 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1158 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1159 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 1160 | DISPLAYLEVEL(5, "Ending into dst buffer of size %u \n", (U32)adjustedDstSize); |
| 1161 | remainingToFlush = ZSTDMT_endStream(zc, &outBuff); |
Yann Collet | 30c7698 | 2017-03-31 18:27:03 -0700 | [diff] [blame] | 1162 | CHECK (ZSTD_isError(remainingToFlush), "ZSTDMT_endStream error : %s", ZSTD_getErrorName(remainingToFlush)); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1163 | DISPLAYLEVEL(5, "endStream : remainingToFlush : %u \n", (U32)remainingToFlush); |
| 1164 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1165 | crcOrig = XXH64_digest(&xxhState); |
| 1166 | cSize = outBuff.pos; |
Yann Collet | 2c5514c | 2017-04-18 22:52:41 -0700 | [diff] [blame] | 1167 | DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | /* multi - fragments decompression test */ |
| 1171 | if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) { |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1172 | CHECK_Z( ZSTD_resetDStream(zd) ); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1173 | } else { |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1174 | CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1175 | } |
| 1176 | { size_t decompressionResult = 1; |
| 1177 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1178 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1179 | for (totalGenSize = 0 ; decompressionResult ; ) { |
| 1180 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1181 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1182 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
| 1183 | inBuff.size = inBuff.pos + readCSrcSize; |
| 1184 | outBuff.size = inBuff.pos + dstBuffSize; |
Yann Collet | ce80098 | 2017-04-05 16:34:09 -0700 | [diff] [blame] | 1185 | DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes \n", (U32)readCSrcSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1186 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 1187 | CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); |
Yann Collet | 2c5514c | 2017-04-18 22:52:41 -0700 | [diff] [blame] | 1188 | DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1189 | } |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 1190 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize); |
| 1191 | CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1192 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 1193 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 1194 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 1195 | } } |
| 1196 | |
| 1197 | /*===== noisy/erroneous src decompression test =====*/ |
| 1198 | |
| 1199 | /* add some noise */ |
| 1200 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 1201 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 1202 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1203 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 1204 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 1205 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 1206 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 1207 | } } |
| 1208 | |
| 1209 | /* try decompression on noisy data */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1210 | CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1211 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1212 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1213 | while (outBuff.pos < dstBufferSize) { |
| 1214 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1215 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1216 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
Nick Terrell | d98bf49 | 2017-01-27 15:42:36 -0800 | [diff] [blame] | 1217 | size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1218 | outBuff.size = outBuff.pos + adjustedDstSize; |
Nick Terrell | d98bf49 | 2017-01-27 15:42:36 -0800 | [diff] [blame] | 1219 | inBuff.size = inBuff.pos + adjustedCSrcSize; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1220 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 1221 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
Nick Terrell | d98bf49 | 2017-01-27 15:42:36 -0800 | [diff] [blame] | 1222 | /* No forward progress possible */ |
| 1223 | if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1224 | } } } } |
| 1225 | DISPLAY("\r%u fuzzer tests completed \n", testNb); |
| 1226 | |
| 1227 | _cleanup: |
| 1228 | ZSTDMT_freeCCtx(zc); |
| 1229 | ZSTD_freeDStream(zd); |
| 1230 | ZSTD_freeDStream(zd_noise); |
| 1231 | free(cNoiseBuffer[0]); |
| 1232 | free(cNoiseBuffer[1]); |
| 1233 | free(cNoiseBuffer[2]); |
| 1234 | free(cNoiseBuffer[3]); |
| 1235 | free(cNoiseBuffer[4]); |
| 1236 | free(copyBuffer); |
| 1237 | free(cBuffer); |
| 1238 | free(dstBuffer); |
| 1239 | return result; |
| 1240 | |
| 1241 | _output_error: |
| 1242 | result = 1; |
| 1243 | goto _cleanup; |
| 1244 | } |
| 1245 | |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1246 | /** If useOpaqueAPI, sets param in cctxParams. |
| 1247 | * Otherwise, sets the param in zc. */ |
| 1248 | static size_t setCCtxParameter(ZSTD_CCtx* zc, ZSTD_CCtx_params* cctxParams, |
| 1249 | ZSTD_cParameter param, unsigned value, |
| 1250 | U32 useOpaqueAPI) |
| 1251 | { |
| 1252 | if (useOpaqueAPI) { |
| 1253 | return ZSTD_CCtxParam_setParameter(cctxParams, param, value); |
| 1254 | } else { |
| 1255 | return ZSTD_CCtx_setParameter(zc, param, value); |
| 1256 | } |
| 1257 | } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1258 | |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 1259 | /* Tests for ZSTD_compress_generic() API */ |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1260 | static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests, U32 const useOpaqueAPI) |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1261 | { |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 1262 | U32 const maxSrcLog = bigTests ? 24 : 22; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1263 | static const U32 maxSampleLog = 19; |
| 1264 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
| 1265 | BYTE* cNoiseBuffer[5]; |
| 1266 | size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog); |
| 1267 | BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize); |
| 1268 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
| 1269 | BYTE* const cBuffer = (BYTE*)malloc (cBufferSize); |
| 1270 | size_t const dstBufferSize = srcBufferSize; |
| 1271 | BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize); |
| 1272 | U32 result = 0; |
| 1273 | U32 testNb = 0; |
| 1274 | U32 coreSeed = seed; |
| 1275 | ZSTD_CCtx* zc = ZSTD_createCCtx(); /* will be reset sometimes */ |
| 1276 | ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */ |
| 1277 | ZSTD_DStream* const zd_noise = ZSTD_createDStream(); |
| 1278 | clock_t const startClock = clock(); |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 1279 | const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1280 | size_t dictSize = 0; |
| 1281 | U32 oldTestLog = 0; |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1282 | U32 windowLogMalus = 0; /* can survive between 2 loops */ |
| 1283 | U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests; |
| 1284 | U32 const nbThreadsMax = bigTests ? 4 : 2; |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1285 | ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams(); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1286 | |
| 1287 | /* allocations */ |
| 1288 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 1289 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 1290 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 1291 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 1292 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| 1293 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
| 1294 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise , |
| 1295 | "Not enough memory, fuzzer tests cancelled"); |
| 1296 | |
| 1297 | /* Create initial samples */ |
| 1298 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 1299 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 1300 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 1301 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 1302 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 1303 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1304 | CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1305 | |
| 1306 | /* catch up testNb */ |
| 1307 | for (testNb=1; testNb < startTest; testNb++) |
| 1308 | FUZ_rand(&coreSeed); |
| 1309 | |
| 1310 | /* test loop */ |
| 1311 | for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) { |
| 1312 | U32 lseed; |
| 1313 | const BYTE* srcBuffer; |
| 1314 | size_t totalTestSize, totalGenSize, cSize; |
| 1315 | XXH64_state_t xxhState; |
| 1316 | U64 crcOrig; |
| 1317 | U32 resetAllowed = 1; |
| 1318 | size_t maxTestSize; |
| 1319 | |
| 1320 | /* init */ |
| 1321 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
| 1322 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
| 1323 | FUZ_rand(&coreSeed); |
| 1324 | lseed = coreSeed ^ prime32; |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame^] | 1325 | DISPLAYLEVEL(5, " *** Test %u *** \n", testNb); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1326 | |
| 1327 | /* states full reset (deliberately not synchronized) */ |
| 1328 | /* some issues can only happen when reusing states */ |
| 1329 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { |
| 1330 | DISPLAYLEVEL(5, "Creating new context \n"); |
| 1331 | ZSTD_freeCCtx(zc); |
| 1332 | zc = ZSTD_createCCtx(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1333 | CHECK(zc==NULL, "ZSTD_createCCtx allocation error"); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1334 | resetAllowed=0; |
| 1335 | } |
| 1336 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { |
| 1337 | ZSTD_freeDStream(zd); |
| 1338 | zd = ZSTD_createDStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1339 | CHECK(zd==NULL, "ZSTD_createDStream allocation error"); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1340 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
| 1341 | } |
| 1342 | |
| 1343 | /* srcBuffer selection [0-4] */ |
| 1344 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 1345 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 1346 | else { |
| 1347 | buffNb >>= 3; |
| 1348 | if (buffNb & 7) { |
| 1349 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 1350 | buffNb = tnb[buffNb >> 3]; |
| 1351 | } else { |
| 1352 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 1353 | buffNb = tnb[buffNb >> 3]; |
| 1354 | } } |
| 1355 | srcBuffer = cNoiseBuffer[buffNb]; |
| 1356 | } |
| 1357 | |
| 1358 | /* compression init */ |
Yann Collet | b737293 | 2017-06-27 15:49:12 -0700 | [diff] [blame] | 1359 | CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* cancel previous dict /*/ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1360 | if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ |
| 1361 | && oldTestLog /* at least one test happened */ && resetAllowed) { |
| 1362 | maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); |
| 1363 | if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1364 | { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1; |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1365 | CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_compressionLevel, compressionLevel, useOpaqueAPI) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1366 | } |
| 1367 | } else { |
| 1368 | U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
| 1369 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1370 | U32 const cLevelCandidate = (FUZ_rand(&lseed) % |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1371 | (ZSTD_maxCLevel() - |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1372 | (MAX(testLog, dictLog) / 3))) + |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1373 | 1; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1374 | U32 const cLevel = MIN(cLevelCandidate, cLevelMax); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame^] | 1375 | DISPLAYLEVEL(5, "t%u: cLevel : %u \n", testNb, cLevel); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1376 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame^] | 1377 | DISPLAYLEVEL(5, "t%u: maxTestSize : %u \n", testNb, (U32)maxTestSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1378 | oldTestLog = testLog; |
| 1379 | /* random dictionary selection */ |
| 1380 | dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; |
| 1381 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 1382 | dict = srcBuffer + dictStart; |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 1383 | if (!dictSize) dict=NULL; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1384 | } |
| 1385 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize; |
| 1386 | ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize); |
| 1387 | |
| 1388 | /* mess with compression parameters */ |
| 1389 | cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1; |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 1390 | cParams.windowLog = MIN(windowLogMax, cParams.windowLog); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1391 | cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1; |
| 1392 | cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1; |
| 1393 | cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1; |
| 1394 | cParams.searchLength += (FUZ_rand(&lseed) & 3) - 1; |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1395 | cParams.targetLength = (U32)((cParams.targetLength + 1 ) * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128))); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1396 | cParams = ZSTD_adjustCParams(cParams, 0, 0); |
| 1397 | |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1398 | if (FUZ_rand(&lseed) & 1) { |
| 1399 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_windowLog, cParams.windowLog, useOpaqueAPI) ); |
| 1400 | assert(cParams.windowLog >= ZSTD_WINDOWLOG_MIN); /* guaranteed by ZSTD_adjustCParams() */ |
| 1401 | windowLogMalus = (cParams.windowLog - ZSTD_WINDOWLOG_MIN) / 5; |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame^] | 1402 | DISPLAYLEVEL(5, "t%u: windowLog : %u \n", testNb, cParams.windowLog); |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1403 | } |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame^] | 1404 | if (FUZ_rand(&lseed) & 1) { |
| 1405 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_hashLog, cParams.hashLog, useOpaqueAPI) ); |
| 1406 | DISPLAYLEVEL(5, "t%u: hashLog : %u \n", testNb, cParams.hashLog); |
| 1407 | } |
| 1408 | if (FUZ_rand(&lseed) & 1) { |
| 1409 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_chainLog, cParams.chainLog, useOpaqueAPI) ); |
| 1410 | DISPLAYLEVEL(5, "t%u: chainLog : %u \n", testNb, cParams.chainLog); |
| 1411 | } |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1412 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_searchLog, cParams.searchLog, useOpaqueAPI) ); |
| 1413 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_minMatch, cParams.searchLength, useOpaqueAPI) ); |
| 1414 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_targetLength, cParams.targetLength, useOpaqueAPI) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1415 | |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 1416 | /* mess with long distance matching parameters */ |
Stella Lau | 67d4a61 | 2017-09-02 21:10:36 -0700 | [diff] [blame] | 1417 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_enableLongDistanceMatching, FUZ_rand(&lseed) & 63, useOpaqueAPI) ); |
Stella Lau | 3d8e313 | 2017-09-11 17:21:28 -0700 | [diff] [blame] | 1418 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashLog, FUZ_randomClampedLength(&lseed, ZSTD_HASHLOG_MIN, 23), useOpaqueAPI) ); |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 1419 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmMinMatch, FUZ_randomClampedLength(&lseed, ZSTD_LDM_MINMATCH_MIN, ZSTD_LDM_MINMATCH_MAX), useOpaqueAPI) ); |
| 1420 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmBucketSizeLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_LDM_BUCKETSIZELOG_MAX), useOpaqueAPI) ); |
| 1421 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashEveryLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN), useOpaqueAPI) ); |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 1422 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1423 | /* mess with frame parameters */ |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1424 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_checksumFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); |
| 1425 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); |
| 1426 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); |
Yann Collet | f5deae8 | 2017-06-18 23:41:38 -0700 | [diff] [blame] | 1427 | if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) ); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame^] | 1428 | DISPLAYLEVEL(5, "t%u: pledgedSrcSize : %u \n", testNb, (U32)pledgedSrcSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1429 | |
| 1430 | /* multi-threading parameters */ |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1431 | { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1; |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1432 | U32 const nbThreadsAdjusted = (windowLogMalus < nbThreadsCandidate) ? nbThreadsCandidate - windowLogMalus : 1; |
| 1433 | U32 const nbThreads = MIN(nbThreadsAdjusted, nbThreadsMax); |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1434 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_nbThreads, nbThreads, useOpaqueAPI) ); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame^] | 1435 | DISPLAYLEVEL(5, "t%u: nbThreads : %u \n", testNb, nbThreads); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1436 | if (nbThreads > 1) { |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1437 | U32 const jobLog = FUZ_rand(&lseed) % (testLog+1); |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1438 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10, useOpaqueAPI) ); |
| 1439 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog), useOpaqueAPI) ); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1443 | if (FUZ_rand(&lseed) & 1) CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1444 | |
| 1445 | /* Apply parameters */ |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1446 | if (useOpaqueAPI) { |
Stella Lau | 82d636b | 2017-08-29 18:03:06 -0700 | [diff] [blame] | 1447 | CHECK_Z (ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams) ); |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1448 | } |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1449 | |
| 1450 | if (FUZ_rand(&lseed) & 1) { |
Stella Lau | eb7bbab | 2017-08-25 10:48:07 -0700 | [diff] [blame] | 1451 | if (FUZ_rand(&lseed) & 1) { |
| 1452 | CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) ); |
| 1453 | } else { |
| 1454 | CHECK_Z( ZSTD_CCtx_loadDictionary_byReference(zc, dict, dictSize) ); |
| 1455 | } |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1456 | if (dict && dictSize) { |
| 1457 | /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */ |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1458 | if (useOpaqueAPI) { |
Stella Lau | 82d636b | 2017-08-29 18:03:06 -0700 | [diff] [blame] | 1459 | size_t const setError = ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams); |
| 1460 | CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParametersUsingCCtxParams should have failed"); |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1461 | } else { |
| 1462 | size_t const setError = ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog-1); |
| 1463 | CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParameter should have failed"); |
| 1464 | } |
| 1465 | } |
| 1466 | } else { |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1467 | CHECK_Z( ZSTD_CCtx_refPrefix(zc, dict, dictSize) ); |
| 1468 | } |
| 1469 | } } |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1470 | |
| 1471 | /* multi-segments compression test */ |
| 1472 | XXH64_reset(&xxhState, 0); |
| 1473 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
Yann Collet | 052a95f | 2017-07-11 17:18:26 -0700 | [diff] [blame] | 1474 | for (cSize=0, totalTestSize=0 ; (totalTestSize < maxTestSize) ; ) { |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1475 | /* compress random chunks into randomly sized dst buffers */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1476 | size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1477 | size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize); |
| 1478 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); |
Yann Collet | 052a95f | 2017-07-11 17:18:26 -0700 | [diff] [blame] | 1479 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1); |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1480 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1481 | ZSTD_EndDirective const flush = (FUZ_rand(&lseed) & 15) ? ZSTD_e_continue : ZSTD_e_flush; |
| 1482 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; |
| 1483 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1484 | |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1485 | CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, flush) ); |
| 1486 | DISPLAYLEVEL(5, "compress consumed %u bytes (total : %u) \n", |
| 1487 | (U32)inBuff.pos, (U32)(totalTestSize + inBuff.pos)); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1488 | |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1489 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 1490 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 1491 | totalTestSize += inBuff.pos; |
| 1492 | } |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1493 | |
| 1494 | /* final frame epilogue */ |
| 1495 | { size_t remainingToFlush = (size_t)(-1); |
| 1496 | while (remainingToFlush) { |
| 1497 | ZSTD_inBuffer inBuff = { NULL, 0, 0 }; |
Yann Collet | 052a95f | 2017-07-11 17:18:26 -0700 | [diff] [blame] | 1498 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1499 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1500 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 1501 | DISPLAYLEVEL(5, "End-flush into dst buffer of size %u \n", (U32)adjustedDstSize); |
| 1502 | remainingToFlush = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end); |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 1503 | CHECK(ZSTD_isError(remainingToFlush), |
| 1504 | "ZSTD_compress_generic w/ ZSTD_e_end error : %s", |
| 1505 | ZSTD_getErrorName(remainingToFlush) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1506 | } } |
| 1507 | crcOrig = XXH64_digest(&xxhState); |
| 1508 | cSize = outBuff.pos; |
| 1509 | DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize); |
| 1510 | } |
| 1511 | |
| 1512 | /* multi - fragments decompression test */ |
| 1513 | if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) { |
Yann Collet | 33a6639 | 2017-06-28 11:09:43 -0700 | [diff] [blame] | 1514 | DISPLAYLEVEL(5, "resetting DCtx (dict:%08X) \n", (U32)(size_t)dict); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1515 | CHECK_Z( ZSTD_resetDStream(zd) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1516 | } else { |
Yann Collet | 33a6639 | 2017-06-28 11:09:43 -0700 | [diff] [blame] | 1517 | DISPLAYLEVEL(5, "using dict of size %u \n", (U32)dictSize); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1518 | CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1519 | } |
| 1520 | { size_t decompressionResult = 1; |
| 1521 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1522 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1523 | for (totalGenSize = 0 ; decompressionResult ; ) { |
| 1524 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1525 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1526 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
| 1527 | inBuff.size = inBuff.pos + readCSrcSize; |
| 1528 | outBuff.size = inBuff.pos + dstBuffSize; |
Yann Collet | 33a6639 | 2017-06-28 11:09:43 -0700 | [diff] [blame] | 1529 | DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes (pos:%u/%u)\n", |
| 1530 | (U32)readCSrcSize, (U32)inBuff.pos, (U32)cSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1531 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 1532 | CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); |
| 1533 | DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize); |
| 1534 | } |
| 1535 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize); |
| 1536 | CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize); |
| 1537 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 1538 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 1539 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 1540 | } } |
| 1541 | |
| 1542 | /*===== noisy/erroneous src decompression test =====*/ |
| 1543 | |
| 1544 | /* add some noise */ |
| 1545 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 1546 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 1547 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1548 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 1549 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 1550 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 1551 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 1552 | } } |
| 1553 | |
| 1554 | /* try decompression on noisy data */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1555 | CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1556 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1557 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1558 | while (outBuff.pos < dstBufferSize) { |
| 1559 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1560 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1561 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
| 1562 | size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize); |
| 1563 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 1564 | inBuff.size = inBuff.pos + adjustedCSrcSize; |
| 1565 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 1566 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1567 | /* Good so far, but no more progress possible */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1568 | if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break; |
| 1569 | } } } } |
Yann Collet | 8dee0ec | 2017-06-18 23:25:15 -0700 | [diff] [blame] | 1570 | DISPLAY("\r%u fuzzer tests completed \n", testNb-1); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1571 | |
| 1572 | _cleanup: |
| 1573 | ZSTD_freeCCtx(zc); |
| 1574 | ZSTD_freeDStream(zd); |
| 1575 | ZSTD_freeDStream(zd_noise); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1576 | ZSTD_freeCCtxParams(cctxParams); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1577 | free(cNoiseBuffer[0]); |
| 1578 | free(cNoiseBuffer[1]); |
| 1579 | free(cNoiseBuffer[2]); |
| 1580 | free(cNoiseBuffer[3]); |
| 1581 | free(cNoiseBuffer[4]); |
| 1582 | free(copyBuffer); |
| 1583 | free(cBuffer); |
| 1584 | free(dstBuffer); |
| 1585 | return result; |
| 1586 | |
| 1587 | _output_error: |
| 1588 | result = 1; |
| 1589 | goto _cleanup; |
| 1590 | } |
| 1591 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1592 | /*-******************************************************* |
| 1593 | * Command line |
| 1594 | *********************************************************/ |
| 1595 | int FUZ_usage(const char* programName) |
| 1596 | { |
| 1597 | DISPLAY( "Usage :\n"); |
| 1598 | DISPLAY( " %s [args]\n", programName); |
| 1599 | DISPLAY( "\n"); |
| 1600 | DISPLAY( "Arguments :\n"); |
| 1601 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 1602 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 1603 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
| 1604 | DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); |
| 1605 | DISPLAY( " -v : verbose\n"); |
| 1606 | DISPLAY( " -p : pause at the end\n"); |
| 1607 | DISPLAY( " -h : display help and exit\n"); |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1611 | typedef enum { simple_api, mt_api, advanced_api } e_api; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1612 | |
| 1613 | int main(int argc, const char** argv) |
| 1614 | { |
| 1615 | U32 seed=0; |
| 1616 | int seedset=0; |
| 1617 | int argNb; |
| 1618 | int nbTests = nbTestsDefault; |
| 1619 | int testNb = 0; |
| 1620 | int proba = FUZ_COMPRESSIBILITY_DEFAULT; |
| 1621 | int result=0; |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1622 | int mainPause = 0; |
Yann Collet | afb0aca | 2017-06-29 18:19:09 -0700 | [diff] [blame] | 1623 | int bigTests = (sizeof(size_t) == 8); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1624 | e_api selected_api = simple_api; |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1625 | const char* const programName = argv[0]; |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 1626 | ZSTD_customMem const customNULL = ZSTD_defaultCMem; |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1627 | U32 useOpaqueAPI = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1628 | |
| 1629 | /* Check command line */ |
| 1630 | for(argNb=1; argNb<argc; argNb++) { |
| 1631 | const char* argument = argv[argNb]; |
| 1632 | if(!argument) continue; /* Protection if argument empty */ |
| 1633 | |
| 1634 | /* Parsing commands. Aggregated commands are allowed */ |
| 1635 | if (argument[0]=='-') { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1636 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1637 | if (!strcmp(argument, "--mt")) { selected_api=mt_api; continue; } |
| 1638 | if (!strcmp(argument, "--newapi")) { selected_api=advanced_api; continue; } |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1639 | if (!strcmp(argument, "--opaqueapi")) { selected_api=advanced_api; useOpaqueAPI = 1; continue; } |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1640 | if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; } |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1641 | |
| 1642 | argument++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1643 | while (*argument!=0) { |
| 1644 | switch(*argument) |
| 1645 | { |
| 1646 | case 'h': |
| 1647 | return FUZ_usage(programName); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1648 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1649 | case 'v': |
| 1650 | argument++; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1651 | g_displayLevel++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1652 | break; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1653 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1654 | case 'q': |
| 1655 | argument++; |
| 1656 | g_displayLevel--; |
| 1657 | break; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1658 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1659 | case 'p': /* pause at the end */ |
| 1660 | argument++; |
| 1661 | mainPause = 1; |
| 1662 | break; |
| 1663 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1664 | case 'i': /* limit tests by nb of iterations (default) */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1665 | argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 1666 | nbTests=0; g_clockTime=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1667 | while ((*argument>='0') && (*argument<='9')) { |
| 1668 | nbTests *= 10; |
| 1669 | nbTests += *argument - '0'; |
| 1670 | argument++; |
| 1671 | } |
| 1672 | break; |
| 1673 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1674 | case 'T': /* limit tests by time */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1675 | argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 1676 | nbTests=0; g_clockTime=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1677 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 1678 | g_clockTime *= 10; |
| 1679 | g_clockTime += *argument - '0'; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1680 | argument++; |
| 1681 | } |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 1682 | if (*argument=='m') g_clockTime *=60, argument++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1683 | if (*argument=='n') argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 1684 | g_clockTime *= CLOCKS_PER_SEC; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1685 | break; |
| 1686 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1687 | case 's': /* manually select seed */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1688 | argument++; |
| 1689 | seed=0; |
| 1690 | seedset=1; |
| 1691 | while ((*argument>='0') && (*argument<='9')) { |
| 1692 | seed *= 10; |
| 1693 | seed += *argument - '0'; |
| 1694 | argument++; |
| 1695 | } |
| 1696 | break; |
| 1697 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1698 | case 't': /* select starting test number */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1699 | argument++; |
| 1700 | testNb=0; |
| 1701 | while ((*argument>='0') && (*argument<='9')) { |
| 1702 | testNb *= 10; |
| 1703 | testNb += *argument - '0'; |
| 1704 | argument++; |
| 1705 | } |
| 1706 | break; |
| 1707 | |
| 1708 | case 'P': /* compressibility % */ |
| 1709 | argument++; |
| 1710 | proba=0; |
| 1711 | while ((*argument>='0') && (*argument<='9')) { |
| 1712 | proba *= 10; |
| 1713 | proba += *argument - '0'; |
| 1714 | argument++; |
| 1715 | } |
| 1716 | if (proba<0) proba=0; |
| 1717 | if (proba>100) proba=100; |
| 1718 | break; |
| 1719 | |
| 1720 | default: |
| 1721 | return FUZ_usage(programName); |
| 1722 | } |
| 1723 | } } } /* for(argNb=1; argNb<argc; argNb++) */ |
| 1724 | |
| 1725 | /* Get Seed */ |
Yann Collet | bb85581 | 2016-08-25 19:11:11 +0200 | [diff] [blame] | 1726 | 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] | 1727 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 1728 | if (!seedset) { |
| 1729 | time_t const t = time(NULL); |
| 1730 | U32 const h = XXH32(&t, sizeof(t), 1); |
| 1731 | seed = h % 10000; |
| 1732 | } |
| 1733 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1734 | DISPLAY("Seed = %u\n", seed); |
| 1735 | if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba); |
| 1736 | |
| 1737 | if (nbTests<=0) nbTests=1; |
| 1738 | |
| 1739 | if (testNb==0) { |
| 1740 | result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */ |
Yann Collet | 4616fad | 2017-07-10 17:16:41 -0700 | [diff] [blame] | 1741 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1742 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1743 | if (!result) { |
| 1744 | switch(selected_api) |
| 1745 | { |
| 1746 | case simple_api : |
| 1747 | result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100, bigTests); |
| 1748 | break; |
| 1749 | case mt_api : |
| 1750 | result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests); |
| 1751 | break; |
| 1752 | case advanced_api : |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1753 | result = fuzzerTests_newAPI(seed, nbTests, testNb, ((double)proba) / 100, bigTests, useOpaqueAPI); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1754 | break; |
| 1755 | default : |
| 1756 | assert(0); /* impossible */ |
| 1757 | } |
| 1758 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1759 | |
| 1760 | if (mainPause) { |
| 1761 | int unused; |
| 1762 | DISPLAY("Press Enter \n"); |
| 1763 | unused = getchar(); |
| 1764 | (void)unused; |
| 1765 | } |
| 1766 | return result; |
| 1767 | } |