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