Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 9 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 10 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 11 | /*-************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 12 | * Compiler specific |
| 13 | **************************************/ |
| 14 | #ifdef _MSC_VER /* Visual Studio */ |
| 15 | # define _CRT_SECURE_NO_WARNINGS /* fgets */ |
| 16 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
cyan | b880631 | 2016-05-30 18:20:46 +0200 | [diff] [blame] | 17 | # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 18 | #endif |
| 19 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 20 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 21 | /*-************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 22 | * Includes |
| 23 | **************************************/ |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 24 | #include <stdlib.h> /* free */ |
| 25 | #include <stdio.h> /* fgets, sscanf */ |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 26 | #include <string.h> /* strcmp */ |
| 27 | #include <time.h> /* clock_t */ |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 28 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressContinue, ZSTD_compressBlock */ |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 29 | #include "zstd.h" /* ZSTD_VERSION_STRING */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 30 | #include "zstd_errors.h" /* ZSTD_getErrorCode */ |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 31 | #define ZDICT_STATIC_LINKING_ONLY |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 32 | #include "zdict.h" /* ZDICT_trainFromBuffer */ |
| 33 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | 353c5d2 | 2015-10-21 14:39:26 +0100 | [diff] [blame] | 34 | #include "mem.h" |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 35 | #define XXH_STATIC_LINKING_ONLY |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 36 | #include "xxhash.h" /* XXH64 */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 37 | |
| 38 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 39 | /*-************************************ |
| 40 | * Constants |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 41 | **************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 42 | #define KB *(1U<<10) |
| 43 | #define MB *(1U<<20) |
| 44 | #define GB *(1U<<30) |
| 45 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 46 | static const U32 FUZ_compressibility_default = 50; |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 47 | static const U32 nbTestsDefault = 30000; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 48 | |
| 49 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 50 | /*-************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 51 | * Display Macros |
| 52 | **************************************/ |
| 53 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 54 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 55 | static U32 g_displayLevel = 2; |
| 56 | |
| 57 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 58 | if ((FUZ_clockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 59 | { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 60 | if (g_displayLevel>=4) fflush(stdout); } } |
Yann Collet | 64deef3 | 2016-09-14 00:16:07 +0200 | [diff] [blame] | 61 | static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6; |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 62 | static clock_t g_displayClock = 0; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 63 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 64 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 65 | /*-******************************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 66 | * Fuzzer functions |
| 67 | *********************************************************/ |
Yann Collet | e93add0 | 2016-02-15 17:44:14 +0100 | [diff] [blame] | 68 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 69 | |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 70 | static clock_t FUZ_clockSpan(clock_t cStart) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 71 | { |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 72 | return clock() - cStart; /* works even when overflow; max span ~ 30mn */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 76 | #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 77 | static unsigned FUZ_rand(unsigned* src) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 78 | { |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 79 | static const U32 prime1 = 2654435761U; |
| 80 | static const U32 prime2 = 2246822519U; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 81 | U32 rand32 = *src; |
| 82 | rand32 *= prime1; |
| 83 | rand32 += prime2; |
| 84 | rand32 = FUZ_rotl32(rand32, 13); |
| 85 | *src = rand32; |
| 86 | return rand32 >> 5; |
| 87 | } |
| 88 | |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 89 | static unsigned FUZ_highbit32(U32 v32) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 90 | { |
| 91 | unsigned nbBits = 0; |
| 92 | if (v32==0) return 0; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 93 | while (v32) v32 >>= 1, nbBits++; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 94 | return nbBits; |
| 95 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 96 | |
| 97 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 98 | /*============================================= |
| 99 | * Basic Unit tests |
| 100 | =============================================*/ |
| 101 | |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 102 | #define CHECK_V(var, fn) size_t const var = fn; if (ZSTD_isError(var)) goto _output_error |
| 103 | #define CHECK(fn) { CHECK_V(err, fn); } |
| 104 | #define CHECKPLUS(var, fn, more) { CHECK_V(var, fn); more; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 105 | static int basicUnitTests(U32 seed, double compressibility) |
| 106 | { |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 107 | size_t const CNBuffSize = 5 MB; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 108 | void* const CNBuffer = malloc(CNBuffSize); |
| 109 | void* const compressedBuffer = malloc(ZSTD_compressBound(CNBuffSize)); |
| 110 | void* const decodedBuffer = malloc(CNBuffSize); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 111 | int testResult = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 112 | U32 testNb=0; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 113 | size_t cSize; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 114 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 115 | /* Create compressible noise */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 116 | if (!CNBuffer || !compressedBuffer || !decodedBuffer) { |
Yann Collet | 94f998b | 2015-07-04 23:10:40 -0800 | [diff] [blame] | 117 | DISPLAY("Not enough memory, aborting\n"); |
| 118 | testResult = 1; |
| 119 | goto _end; |
| 120 | } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 121 | RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 122 | |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 123 | /* Basic tests */ |
Yann Collet | 9a69ec4 | 2016-08-01 16:25:58 +0200 | [diff] [blame] | 124 | DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName : ", testNb++); |
| 125 | { const char* errorString = ZSTD_getErrorName(0); |
| 126 | DISPLAYLEVEL(4, "OK : %s \n", errorString); |
| 127 | } |
| 128 | |
| 129 | DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName with wrong value : ", testNb++); |
| 130 | { const char* errorString = ZSTD_getErrorName(499); |
| 131 | DISPLAYLEVEL(4, "OK : %s \n", errorString); |
| 132 | } |
| 133 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 134 | DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, (U32)CNBuffSize); |
| 135 | CHECKPLUS(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 136 | CNBuffer, CNBuffSize, 1), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 137 | cSize=r ); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 138 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 139 | |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 140 | DISPLAYLEVEL(4, "test%3i : decompressed size test : ", testNb++); |
Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 141 | { unsigned long long const rSize = ZSTD_findDecompressedSize(compressedBuffer, cSize); |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 142 | if (rSize != CNBuffSize) goto _output_error; |
| 143 | } |
| 144 | DISPLAYLEVEL(4, "OK \n"); |
| 145 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 146 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize); |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 147 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize); |
| 148 | if (r != CNBuffSize) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 149 | DISPLAYLEVEL(4, "OK \n"); |
| 150 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 151 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 152 | { size_t u; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 153 | for (u=0; u<CNBuffSize; u++) { |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 154 | if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;; |
| 155 | } } |
| 156 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 157 | |
| 158 | DISPLAYLEVEL(4, "test%3i : decompress with 1 missing byte : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 159 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize-1); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 160 | if (!ZSTD_isError(r)) goto _output_error; |
| 161 | if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_srcSize_wrong) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 162 | DISPLAYLEVEL(4, "OK \n"); |
| 163 | |
| 164 | DISPLAYLEVEL(4, "test%3i : decompress with 1 too much byte : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 165 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize+1); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 166 | if (!ZSTD_isError(r)) goto _output_error; |
| 167 | if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 168 | DISPLAYLEVEL(4, "OK \n"); |
| 169 | |
Sean Purcell | ba2ad9f | 2017-02-07 16:16:55 -0800 | [diff] [blame] | 170 | /* Simple API multiframe test */ |
| 171 | DISPLAYLEVEL(4, "test%3i : compress multiple frames : ", testNb++); |
| 172 | { size_t off = 0; |
| 173 | int i; |
| 174 | int const segs = 4; |
| 175 | /* only use the first half so we don't push against size limit of compressedBuffer */ |
| 176 | size_t const segSize = (CNBuffSize / 2) / segs; |
| 177 | for (i = 0; i < segs; i++) { |
| 178 | CHECK_V(r, |
| 179 | ZSTD_compress( |
| 180 | (BYTE *)compressedBuffer + off, CNBuffSize - off, |
| 181 | (BYTE *)CNBuffer + segSize * i, |
| 182 | segSize, 5)); |
| 183 | off += r; |
| 184 | if (i == segs/2) { |
| 185 | /* insert skippable frame */ |
| 186 | const U32 skipLen = 128 KB; |
| 187 | MEM_writeLE32((BYTE*)compressedBuffer + off, ZSTD_MAGIC_SKIPPABLE_START); |
| 188 | MEM_writeLE32((BYTE*)compressedBuffer + off + 4, skipLen); |
| 189 | off += skipLen + ZSTD_skippableHeaderSize; |
| 190 | } |
| 191 | } |
| 192 | cSize = off; |
| 193 | } |
| 194 | DISPLAYLEVEL(4, "OK \n"); |
| 195 | |
| 196 | DISPLAYLEVEL(4, "test%3i : get decompressed size of multiple frames : ", testNb++); |
| 197 | { unsigned long long const r = ZSTD_findDecompressedSize(compressedBuffer, cSize); |
| 198 | if (r != CNBuffSize / 2) goto _output_error; } |
| 199 | DISPLAYLEVEL(4, "OK \n"); |
| 200 | |
| 201 | DISPLAYLEVEL(4, "test%3i : decompress multiple frames : ", testNb++); |
| 202 | { CHECK_V(r, ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize)); |
| 203 | if (r != CNBuffSize / 2) goto _output_error; } |
| 204 | DISPLAYLEVEL(4, "OK \n"); |
| 205 | |
| 206 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 207 | if (memcmp(decodedBuffer, CNBuffer, CNBuffSize / 2) != 0) goto _output_error; |
| 208 | DISPLAYLEVEL(4, "OK \n"); |
| 209 | |
Yann Collet | 389648c | 2016-04-12 19:13:08 +0200 | [diff] [blame] | 210 | /* Dictionary and CCtx Duplication tests */ |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 211 | { ZSTD_CCtx* const ctxOrig = ZSTD_createCCtx(); |
| 212 | ZSTD_CCtx* const ctxDuplicated = ZSTD_createCCtx(); |
| 213 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| 214 | static const size_t dictSize = 551; |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 215 | |
Yann Collet | 887e7da | 2016-04-11 20:12:27 +0200 | [diff] [blame] | 216 | DISPLAYLEVEL(4, "test%3i : copy context too soon : ", testNb++); |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 217 | { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 218 | if (!ZSTD_isError(copyResult)) goto _output_error; } /* error must be detected */ |
Yann Collet | 887e7da | 2016-04-11 20:12:27 +0200 | [diff] [blame] | 219 | DISPLAYLEVEL(4, "OK \n"); |
| 220 | |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 221 | DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 222 | CHECK( ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2) ); |
Sean Purcell | 84b37cc | 2017-02-09 12:27:32 -0800 | [diff] [blame] | 223 | CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0) ); /* Begin_usingDict implies unknown srcSize, so match that */ |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 224 | DISPLAYLEVEL(4, "OK \n"); |
| 225 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 226 | DISPLAYLEVEL(4, "test%3i : compress with flat dictionary : ", testNb++); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 227 | cSize = 0; |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 228 | CHECKPLUS(r, ZSTD_compressEnd(ctxOrig, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 229 | (const char*)CNBuffer + dictSize, CNBuffSize - dictSize), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 230 | cSize += r); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 231 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 232 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 233 | DISPLAYLEVEL(4, "test%3i : frame built with flat dictionary should be decompressible : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 234 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 235 | decodedBuffer, CNBuffSize, |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 236 | compressedBuffer, cSize, |
| 237 | CNBuffer, dictSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 238 | if (r != CNBuffSize - dictSize) goto _output_error); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 239 | DISPLAYLEVEL(4, "OK \n"); |
| 240 | |
| 241 | DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++); |
Yann Collet | 389648c | 2016-04-12 19:13:08 +0200 | [diff] [blame] | 242 | { size_t const cSizeOrig = cSize; |
| 243 | cSize = 0; |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 244 | CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 245 | (const char*)CNBuffer + dictSize, CNBuffSize - dictSize), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 246 | cSize += r); |
Yann Collet | 227cc39 | 2016-07-15 11:27:09 +0200 | [diff] [blame] | 247 | if (cSize != cSizeOrig) goto _output_error; /* should be identical ==> same size */ |
Yann Collet | 389648c | 2016-04-12 19:13:08 +0200 | [diff] [blame] | 248 | } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 249 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 250 | |
| 251 | DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 252 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 253 | decodedBuffer, CNBuffSize, |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 254 | compressedBuffer, cSize, |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 255 | CNBuffer, dictSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 256 | if (r != CNBuffSize - dictSize) goto _output_error); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 257 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 258 | |
Yann Collet | 8dff956 | 2017-02-25 10:11:15 -0800 | [diff] [blame] | 259 | DISPLAYLEVEL(4, "test%3i : decompress with DDict : ", testNb++); |
Yann Collet | bd7fa21 | 2017-02-26 14:43:07 -0800 | [diff] [blame^] | 260 | { ZSTD_DDict* const ddict = ZSTD_createDDict_byReference(CNBuffer, dictSize); |
Yann Collet | 8dff956 | 2017-02-25 10:11:15 -0800 | [diff] [blame] | 261 | size_t const r = ZSTD_decompress_usingDDict(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, ddict); |
| 262 | if (r != CNBuffSize - dictSize) goto _output_error; |
| 263 | DISPLAYLEVEL(4, "OK (size of DDict : %u) \n", (U32)ZSTD_sizeof_DDict(ddict)); |
| 264 | ZSTD_freeDDict(ddict); |
| 265 | } |
| 266 | |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 267 | DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 268 | { size_t const testSize = CNBuffSize / 3; |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 269 | { ZSTD_parameters p = ZSTD_getParams(2, testSize, dictSize); |
| 270 | p.fParams.contentSizeFlag = 1; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 271 | CHECK( ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1) ); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 272 | } |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 273 | CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, testSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 274 | |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 275 | CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize), |
| 276 | (const char*)CNBuffer + dictSize, testSize), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 277 | cSize = r); |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 278 | { ZSTD_frameParams fp; |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 279 | if (ZSTD_getFrameParams(&fp, compressedBuffer, cSize)) goto _output_error; |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 280 | if ((fp.frameContentSize != testSize) && (fp.frameContentSize != 0)) goto _output_error; |
| 281 | } } |
| 282 | DISPLAYLEVEL(4, "OK \n"); |
| 283 | |
| 284 | ZSTD_freeCCtx(ctxOrig); |
| 285 | ZSTD_freeCCtx(ctxDuplicated); |
| 286 | ZSTD_freeDCtx(dctx); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 289 | /* Dictionary and dictBuilder tests */ |
| 290 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
| 291 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| 292 | size_t dictSize = 16 KB; |
| 293 | void* dictBuffer = malloc(dictSize); |
| 294 | size_t const totalSampleSize = 1 MB; |
| 295 | size_t const sampleUnitSize = 8 KB; |
Yann Collet | b81cbba | 2016-05-30 22:29:45 +0200 | [diff] [blame] | 296 | U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 297 | size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t)); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 298 | U32 dictID; |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 299 | |
| 300 | if (dictBuffer==NULL || samplesSizes==NULL) { |
| 301 | free(dictBuffer); |
| 302 | free(samplesSizes); |
| 303 | goto _output_error; |
| 304 | } |
| 305 | |
| 306 | DISPLAYLEVEL(4, "test%3i : dictBuilder : ", testNb++); |
| 307 | { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; } |
| 308 | dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize, |
| 309 | CNBuffer, samplesSizes, nbSamples); |
| 310 | if (ZDICT_isError(dictSize)) goto _output_error; |
| 311 | DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize); |
| 312 | |
Yann Collet | da3fbcb | 2016-08-19 14:23:58 +0200 | [diff] [blame] | 313 | DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 314 | dictID = ZDICT_getDictID(dictBuffer, dictSize); |
| 315 | if (dictID==0) goto _output_error; |
| 316 | DISPLAYLEVEL(4, "OK : %u \n", dictID); |
Yann Collet | da3fbcb | 2016-08-19 14:23:58 +0200 | [diff] [blame] | 317 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 318 | DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++); |
| 319 | cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 320 | CNBuffer, CNBuffSize, |
| 321 | dictBuffer, dictSize, 4); |
| 322 | if (ZSTD_isError(cSize)) goto _output_error; |
| 323 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 324 | |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 325 | DISPLAYLEVEL(4, "test%3i : retrieve dictID from dictionary : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 326 | { U32 const did = ZSTD_getDictID_fromDict(dictBuffer, dictSize); |
| 327 | if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 328 | } |
| 329 | DISPLAYLEVEL(4, "OK \n"); |
| 330 | |
| 331 | DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 332 | { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 333 | if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 334 | } |
| 335 | DISPLAYLEVEL(4, "OK \n"); |
| 336 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 337 | DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++); |
| 338 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 339 | decodedBuffer, CNBuffSize, |
| 340 | compressedBuffer, cSize, |
| 341 | dictBuffer, dictSize), |
| 342 | if (r != CNBuffSize) goto _output_error); |
| 343 | DISPLAYLEVEL(4, "OK \n"); |
| 344 | |
| 345 | DISPLAYLEVEL(4, "test%3i : compress without dictID : ", testNb++); |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 346 | { ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize); |
| 347 | p.fParams.noDictIDFlag = 1; |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 348 | cSize = ZSTD_compress_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 349 | CNBuffer, CNBuffSize, |
| 350 | dictBuffer, dictSize, p); |
| 351 | if (ZSTD_isError(cSize)) goto _output_error; |
| 352 | } |
| 353 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 354 | |
| 355 | DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++); |
| 356 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 357 | decodedBuffer, CNBuffSize, |
| 358 | compressedBuffer, cSize, |
| 359 | dictBuffer, dictSize), |
| 360 | if (r != CNBuffSize) goto _output_error); |
| 361 | DISPLAYLEVEL(4, "OK \n"); |
| 362 | |
Sean Purcell | 834ab50 | 2017-01-11 17:31:06 -0800 | [diff] [blame] | 363 | DISPLAYLEVEL(4, "test%3i : dictionary containing only header should return error : ", testNb++); |
| 364 | { |
| 365 | const size_t ret = ZSTD_decompress_usingDict( |
| 366 | dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize, |
| 367 | "\x37\xa4\x30\xec\x11\x22\x33\x44", 8); |
| 368 | if (ZSTD_getErrorCode(ret) != ZSTD_error_dictionary_corrupted) goto _output_error; |
| 369 | } |
Sean Purcell | c44c4d5 | 2017-01-12 09:38:29 -0800 | [diff] [blame] | 370 | DISPLAYLEVEL(4, "OK \n"); |
Sean Purcell | 834ab50 | 2017-01-11 17:31:06 -0800 | [diff] [blame] | 371 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 372 | ZSTD_freeCCtx(cctx); |
| 373 | ZSTD_freeDCtx(dctx); |
| 374 | free(dictBuffer); |
| 375 | free(samplesSizes); |
| 376 | } |
| 377 | |
Nick Terrell | a8b4fe0 | 2017-01-02 18:45:19 -0800 | [diff] [blame] | 378 | /* COVER dictionary builder tests */ |
| 379 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
| 380 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| 381 | size_t dictSize = 16 KB; |
| 382 | size_t optDictSize = dictSize; |
| 383 | void* dictBuffer = malloc(dictSize); |
| 384 | size_t const totalSampleSize = 1 MB; |
| 385 | size_t const sampleUnitSize = 8 KB; |
| 386 | U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize); |
| 387 | size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t)); |
| 388 | COVER_params_t params; |
| 389 | U32 dictID; |
| 390 | |
| 391 | if (dictBuffer==NULL || samplesSizes==NULL) { |
| 392 | free(dictBuffer); |
| 393 | free(samplesSizes); |
| 394 | goto _output_error; |
| 395 | } |
| 396 | |
| 397 | DISPLAYLEVEL(4, "test%3i : COVER_trainFromBuffer : ", testNb++); |
| 398 | { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; } |
| 399 | memset(¶ms, 0, sizeof(params)); |
| 400 | params.d = 1 + (FUZ_rand(&seed) % 16); |
| 401 | params.k = params.d + (FUZ_rand(&seed) % 256); |
| 402 | dictSize = COVER_trainFromBuffer(dictBuffer, dictSize, |
| 403 | CNBuffer, samplesSizes, nbSamples, |
| 404 | params); |
| 405 | if (ZDICT_isError(dictSize)) goto _output_error; |
| 406 | DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize); |
| 407 | |
| 408 | DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); |
| 409 | dictID = ZDICT_getDictID(dictBuffer, dictSize); |
| 410 | if (dictID==0) goto _output_error; |
| 411 | DISPLAYLEVEL(4, "OK : %u \n", dictID); |
| 412 | |
| 413 | DISPLAYLEVEL(4, "test%3i : COVER_optimizeTrainFromBuffer : ", testNb++); |
| 414 | memset(¶ms, 0, sizeof(params)); |
| 415 | params.steps = 4; |
| 416 | optDictSize = COVER_optimizeTrainFromBuffer(dictBuffer, optDictSize, |
| 417 | CNBuffer, samplesSizes, nbSamples, |
| 418 | ¶ms); |
| 419 | if (ZDICT_isError(optDictSize)) goto _output_error; |
| 420 | DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)optDictSize); |
| 421 | |
| 422 | DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); |
| 423 | dictID = ZDICT_getDictID(dictBuffer, optDictSize); |
| 424 | if (dictID==0) goto _output_error; |
| 425 | DISPLAYLEVEL(4, "OK : %u \n", dictID); |
| 426 | |
| 427 | ZSTD_freeCCtx(cctx); |
| 428 | ZSTD_freeDCtx(dctx); |
| 429 | free(dictBuffer); |
| 430 | free(samplesSizes); |
| 431 | } |
| 432 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 433 | /* Decompression defense tests */ |
| 434 | DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 435 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 3); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 436 | if (!ZSTD_isError(r)) goto _output_error; |
| 437 | if (r != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 438 | DISPLAYLEVEL(4, "OK \n"); |
| 439 | |
| 440 | DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++); |
| 441 | ((char*)(CNBuffer))[0] = 1; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 442 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 4); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 443 | if (!ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 444 | DISPLAYLEVEL(4, "OK \n"); |
| 445 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 446 | /* block API tests */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 447 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 448 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
Yann Collet | d3d2db5 | 2016-07-15 12:20:26 +0200 | [diff] [blame] | 449 | static const size_t dictSize = 65 KB; |
| 450 | static const size_t blockSize = 100 KB; /* won't cause pb with small dict size */ |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 451 | size_t cSize2; |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 452 | |
| 453 | /* basic block compression */ |
| 454 | DISPLAYLEVEL(4, "test%3i : Block compression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 455 | CHECK( ZSTD_compressBegin(cctx, 5) ); |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 456 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize); |
| 457 | if (ZSTD_isError(cSize)) goto _output_error; |
| 458 | DISPLAYLEVEL(4, "OK \n"); |
| 459 | |
| 460 | DISPLAYLEVEL(4, "test%3i : Block decompression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 461 | CHECK( ZSTD_decompressBegin(dctx) ); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 462 | { CHECK_V(r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) ); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 463 | if (r != blockSize) goto _output_error; } |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 464 | DISPLAYLEVEL(4, "OK \n"); |
| 465 | |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 466 | /* dictionary block compression */ |
| 467 | DISPLAYLEVEL(4, "test%3i : Dictionary Block compression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 468 | CHECK( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) ); |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 469 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize); |
| 470 | if (ZSTD_isError(cSize)) goto _output_error; |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 471 | cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize+blockSize, blockSize); |
| 472 | if (ZSTD_isError(cSize2)) goto _output_error; |
Yann Collet | aa2628d | 2016-07-07 15:28:41 +0200 | [diff] [blame] | 473 | memcpy((char*)compressedBuffer+cSize, (char*)CNBuffer+dictSize+blockSize, blockSize); /* fake non-compressed block */ |
| 474 | cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize+blockSize, ZSTD_compressBound(blockSize), |
| 475 | (char*)CNBuffer+dictSize+2*blockSize, blockSize); |
| 476 | if (ZSTD_isError(cSize2)) goto _output_error; |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 477 | DISPLAYLEVEL(4, "OK \n"); |
| 478 | |
| 479 | DISPLAYLEVEL(4, "test%3i : Dictionary Block decompression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 480 | CHECK( ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize) ); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 481 | { CHECK_V( r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) ); |
| 482 | if (r != blockSize) goto _output_error; } |
Yann Collet | aa2628d | 2016-07-07 15:28:41 +0200 | [diff] [blame] | 483 | ZSTD_insertBlock(dctx, (char*)decodedBuffer+blockSize, blockSize); /* insert non-compressed block into dctx history */ |
| 484 | { CHECK_V( r, ZSTD_decompressBlock(dctx, (char*)decodedBuffer+2*blockSize, CNBuffSize, (char*)compressedBuffer+cSize+blockSize, cSize2) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 485 | if (r != blockSize) goto _output_error; } |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 486 | DISPLAYLEVEL(4, "OK \n"); |
| 487 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 488 | ZSTD_freeCCtx(cctx); |
| 489 | ZSTD_freeDCtx(dctx); |
| 490 | } |
| 491 | |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 492 | /* long rle test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 493 | { size_t sampleSize = 0; |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 494 | DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 495 | RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., seed+1); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 496 | memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1); |
| 497 | sampleSize += 256 KB - 1; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 498 | RDG_genBuffer((char*)CNBuffer+sampleSize, 96 KB, compressibility, 0., seed+2); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 499 | sampleSize += 96 KB; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 500 | cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 501 | if (ZSTD_isError(cSize)) goto _output_error; |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 502 | { CHECK_V(regenSize, ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize)); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 503 | if (regenSize!=sampleSize) goto _output_error; } |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 504 | DISPLAYLEVEL(4, "OK \n"); |
| 505 | } |
| 506 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 507 | /* All zeroes test (test bug #137) */ |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 508 | #define ZEROESLENGTH 100 |
| 509 | DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH); |
| 510 | memset(CNBuffer, 0, ZEROESLENGTH); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 511 | { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 512 | cSize = r; } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 513 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100); |
| 514 | |
| 515 | DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 516 | { CHECK_V(r, ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 517 | if (r != ZEROESLENGTH) goto _output_error; } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 518 | DISPLAYLEVEL(4, "OK \n"); |
| 519 | |
| 520 | /* nbSeq limit test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 521 | #define _3BYTESTESTLENGTH 131000 |
| 522 | #define NB3BYTESSEQLOG 9 |
| 523 | #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG) |
| 524 | #define NB3BYTESSEQMASK (NB3BYTESSEQ-1) |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 525 | /* creates a buffer full of 3-bytes sequences */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 526 | { BYTE _3BytesSeqs[NB3BYTESSEQ][3]; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 527 | U32 rSeed = 1; |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 528 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 529 | /* create batch of 3-bytes sequences */ |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 530 | { int i; |
| 531 | for (i=0; i < NB3BYTESSEQ; i++) { |
| 532 | _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 533 | _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 534 | _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 535 | } } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 536 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 537 | /* randomly fills CNBuffer with prepared 3-bytes sequences */ |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 538 | { int i; |
| 539 | for (i=0; i < _3BYTESTESTLENGTH; i += 3) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */ |
| 540 | U32 const id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK; |
| 541 | ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0]; |
| 542 | ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1]; |
| 543 | ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2]; |
| 544 | } } } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 545 | DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 546 | { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 547 | CNBuffer, _3BYTESTESTLENGTH, 19) ); |
| 548 | cSize = r; } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 549 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100); |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 550 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 551 | DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 552 | { CHECK_V(r, ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 553 | if (r != _3BYTESTESTLENGTH) goto _output_error; } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 554 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 555 | |
Sean Purcell | 9050e19 | 2017-02-22 12:12:32 -0800 | [diff] [blame] | 556 | /* findFrameCompressedSize on skippable frames */ |
| 557 | DISPLAYLEVEL(4, "test%3i : frame compressed size of skippable frame : ", testNb++); |
| 558 | { const char* frame = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde"; |
| 559 | size_t const frameSrcSize = 13; |
| 560 | if (ZSTD_findFrameCompressedSize(frame, frameSrcSize) != frameSrcSize) goto _output_error; } |
| 561 | DISPLAYLEVEL(4, "OK \n"); |
| 562 | |
Sean Purcell | e0b3265 | 2017-02-08 15:31:47 -0800 | [diff] [blame] | 563 | /* error string tests */ |
| 564 | DISPLAYLEVEL(4, "test%3i : testing ZSTD error code strings : ", testNb++); |
| 565 | if (strcmp("No error detected", ZSTD_getErrorName((ZSTD_ErrorCode)(0-ZSTD_error_no_error))) != 0) goto _output_error; |
| 566 | if (strcmp("No error detected", ZSTD_getErrorString(ZSTD_error_no_error)) != 0) goto _output_error; |
| 567 | if (strcmp("Unspecified error code", ZSTD_getErrorString((ZSTD_ErrorCode)(0-ZSTD_error_GENERIC))) != 0) goto _output_error; |
| 568 | if (strcmp("Error (generic)", ZSTD_getErrorName((size_t)0-ZSTD_error_GENERIC)) != 0) goto _output_error; |
| 569 | if (strcmp("Error (generic)", ZSTD_getErrorString(ZSTD_error_GENERIC)) != 0) goto _output_error; |
| 570 | if (strcmp("No error detected", ZSTD_getErrorName(ZSTD_error_GENERIC)) != 0) goto _output_error; |
| 571 | DISPLAYLEVEL(4, "OK \n"); |
| 572 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 573 | _end: |
| 574 | free(CNBuffer); |
| 575 | free(compressedBuffer); |
| 576 | free(decodedBuffer); |
| 577 | return testResult; |
| 578 | |
| 579 | _output_error: |
| 580 | testResult = 1; |
| 581 | DISPLAY("Error detected in Unit tests ! \n"); |
| 582 | goto _end; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 587 | { |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 588 | const BYTE* b1 = (const BYTE*)buf1; |
| 589 | const BYTE* b2 = (const BYTE*)buf2; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 590 | size_t u; |
| 591 | for (u=0; u<max; u++) { |
| 592 | if (b1[u] != b2[u]) break; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 593 | } |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 594 | return u; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 597 | |
| 598 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 599 | { |
| 600 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 601 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 602 | } |
| 603 | |
| 604 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 605 | { |
| 606 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 607 | return FUZ_rLogLength(seed, logLength); |
| 608 | } |
| 609 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 610 | #undef CHECK |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 611 | #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ |
| 612 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 613 | |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 614 | static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxDurationS, double compressibility) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 615 | { |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 616 | static const U32 maxSrcLog = 23; |
| 617 | static const U32 maxSampleLog = 22; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 618 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
| 619 | size_t const dstBufferSize = (size_t)1<<maxSampleLog; |
| 620 | size_t const cBufferSize = ZSTD_compressBound(dstBufferSize); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 621 | BYTE* cNoiseBuffer[5]; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 622 | BYTE* srcBuffer; /* jumping pointer */ |
| 623 | BYTE* const cBuffer = (BYTE*) malloc (cBufferSize); |
| 624 | BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize); |
| 625 | BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 626 | ZSTD_CCtx* const refCtx = ZSTD_createCCtx(); |
| 627 | ZSTD_CCtx* const ctx = ZSTD_createCCtx(); |
| 628 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 629 | U32 result = 0; |
| 630 | U32 testNb = 0; |
| 631 | U32 coreSeed = seed, lseed = 0; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 632 | clock_t const startClock = clock(); |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 633 | clock_t const maxClockSpan = maxDurationS * CLOCKS_PER_SEC; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 634 | |
| 635 | /* allocation */ |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 636 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 637 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 638 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 639 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 640 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 641 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 642 | || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx, |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 643 | "Not enough memory, fuzzer tests cancelled"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 644 | |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 645 | /* Create initial samples */ |
| 646 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 647 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 648 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 649 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 650 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 651 | srcBuffer = cNoiseBuffer[2]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 652 | |
| 653 | /* catch up testNb */ |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 654 | for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 655 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 656 | /* main test loop */ |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 657 | for ( ; (testNb <= nbTests) || (FUZ_clockSpan(startClock) < maxClockSpan); testNb++ ) { |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 658 | size_t sampleSize, maxTestSize, totalTestSize; |
| 659 | size_t cSize, totalCSize, totalGenSize; |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 660 | XXH64_state_t xxhState; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 661 | U64 crcOrig; |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 662 | BYTE* sampleBuffer; |
Yann Collet | 4bfe415 | 2015-12-06 13:18:37 +0100 | [diff] [blame] | 663 | const BYTE* dict; |
| 664 | size_t dictSize; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 665 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 666 | /* notification */ |
| 667 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
Yann Collet | 4c0b44f | 2016-11-01 11:13:22 -0700 | [diff] [blame] | 668 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 669 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 670 | FUZ_rand(&coreSeed); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 671 | { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; } |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 672 | |
| 673 | /* srcBuffer selection [0-4] */ |
| 674 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 675 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 676 | else { |
| 677 | buffNb >>= 3; |
| 678 | if (buffNb & 7) { |
| 679 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 680 | buffNb = tnb[buffNb >> 3]; |
| 681 | } else { |
| 682 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 683 | buffNb = tnb[buffNb >> 3]; |
| 684 | } } |
| 685 | srcBuffer = cNoiseBuffer[buffNb]; |
| 686 | } |
| 687 | |
| 688 | /* select src segment */ |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 689 | sampleSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 690 | |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 691 | /* create sample buffer (to catch read error with valgrind & sanitizers) */ |
| 692 | sampleBuffer = (BYTE*)malloc(sampleSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 693 | CHECK(sampleBuffer==NULL, "not enough memory for sample buffer"); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 694 | { size_t const sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); |
| 695 | memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); } |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 696 | crcOrig = XXH64(sampleBuffer, sampleSize, 0); |
| 697 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 698 | /* compression tests */ |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 699 | { unsigned const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (FUZ_highbit32((U32)sampleSize)/3))) + 1; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 700 | cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 701 | CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed : %s", ZSTD_getErrorName(cSize)); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 702 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 703 | /* compression failure test : too small dest buffer */ |
| 704 | if (cSize > 3) { |
| 705 | const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 706 | const size_t tooSmallSize = cSize - missing; |
| 707 | const U32 endMark = 0x4DC2B1A9; |
| 708 | memcpy(dstBuffer+tooSmallSize, &endMark, 4); |
| 709 | { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel); |
| 710 | CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); } |
| 711 | { U32 endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, 4); |
| 712 | CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 713 | } } |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 714 | |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 715 | /* Decompressed size test */ |
Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 716 | { unsigned long long const rSize = ZSTD_findDecompressedSize(cBuffer, cSize); |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 717 | CHECK(rSize != sampleSize, "decompressed size incorrect"); |
| 718 | } |
| 719 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 720 | /* frame header decompression test */ |
Yann Collet | 346bffb | 2016-03-15 15:24:52 +0100 | [diff] [blame] | 721 | { ZSTD_frameParams dParams; |
| 722 | size_t const check = ZSTD_getFrameParams(&dParams, cBuffer, cSize); |
| 723 | CHECK(ZSTD_isError(check), "Frame Parameters extraction failed"); |
| 724 | CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect"); |
| 725 | } |
| 726 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 727 | /* successful decompression test */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 728 | { size_t const margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 729 | size_t const dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize); |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 730 | CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (U32)sampleSize, (U32)cSize); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 731 | { U64 const crcDest = XXH64(dstBuffer, sampleSize, 0); |
| 732 | CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize); |
| 733 | } } |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 734 | |
| 735 | free(sampleBuffer); /* no longer useful after this point */ |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 736 | |
| 737 | /* truncated src decompression test */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 738 | { size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 739 | size_t const tooSmallSize = cSize - missing; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 740 | void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch read overflows */ |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 741 | CHECK(cBufferTooSmall == NULL, "not enough memory !"); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 742 | memcpy(cBufferTooSmall, cBuffer, tooSmallSize); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 743 | { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize); |
| 744 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); } |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 745 | free(cBufferTooSmall); |
| 746 | } |
| 747 | |
| 748 | /* too small dst decompression test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 749 | if (sampleSize > 3) { |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 750 | size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 751 | size_t const tooSmallSize = sampleSize - missing; |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 752 | static const BYTE token = 0xA9; |
| 753 | dstBuffer[tooSmallSize] = token; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 754 | { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize); |
| 755 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); } |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 756 | CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow"); |
| 757 | } |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 758 | |
| 759 | /* noisy src decompression test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 760 | if (cSize > 6) { |
| 761 | /* insert noise into src */ |
| 762 | { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4)); |
| 763 | size_t pos = 4; /* preserve magic number (too easy to detect) */ |
| 764 | for (;;) { |
| 765 | /* keep some original src */ |
| 766 | { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits; |
| 767 | size_t const mask = (1<<nbBits) - 1; |
| 768 | size_t const skipLength = FUZ_rand(&lseed) & mask; |
| 769 | pos += skipLength; |
| 770 | } |
| 771 | if (pos <= cSize) break; |
| 772 | /* add noise */ |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 773 | { U32 const nbBitsCodes = FUZ_rand(&lseed) % maxNbBits; |
| 774 | U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0; |
| 775 | size_t const mask = (1<<nbBits) - 1; |
| 776 | size_t const rNoiseLength = (FUZ_rand(&lseed) & mask) + 1; |
| 777 | size_t const noiseLength = MIN(rNoiseLength, cSize-pos); |
| 778 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 779 | memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength); |
| 780 | pos += noiseLength; |
| 781 | } } } |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 782 | |
| 783 | /* decompress noisy source */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 784 | { U32 const endMark = 0xA9B1C3D6; |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 785 | memcpy(dstBuffer+sampleSize, &endMark, 4); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 786 | { size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize); |
| 787 | /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */ |
| 788 | CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize), |
| 789 | "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)decompressResult, (U32)sampleSize); |
| 790 | } |
| 791 | { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4); |
| 792 | CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow"); |
| 793 | } } } /* noisy src decompression test */ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 794 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 795 | /*===== Streaming compression test, scattered segments and dictionary =====*/ |
| 796 | |
| 797 | { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
| 798 | int const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1; |
| 799 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
| 800 | if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1; |
| 801 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 802 | dictSize = FUZ_randomLength(&lseed, maxSampleLog); /* needed also for decompression */ |
| 803 | dict = srcBuffer + (FUZ_rand(&lseed) % (srcBufferSize - dictSize)); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 804 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 805 | if (FUZ_rand(&lseed) & 0xF) { |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 806 | size_t const errorCode = ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel); |
| 807 | CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); |
| 808 | } else { |
cyan | b880631 | 2016-05-30 18:20:46 +0200 | [diff] [blame] | 809 | ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, 0, dictSize); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 810 | ZSTD_frameParameters const fpar = { FUZ_rand(&lseed)&1 /* contentSizeFlag */, |
| 811 | !(FUZ_rand(&lseed)&3) /* contentChecksumFlag*/, |
| 812 | 0 /*NodictID*/ }; /* note : since dictionary is fake, dictIDflag has no impact */ |
cyan | b880631 | 2016-05-30 18:20:46 +0200 | [diff] [blame] | 813 | ZSTD_parameters p; |
| 814 | size_t errorCode; |
| 815 | p.cParams = cPar; p.fParams = fpar; |
| 816 | errorCode = ZSTD_compressBegin_advanced(refCtx, dict, dictSize, p, 0); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 817 | CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_advanced error : %s", ZSTD_getErrorName(errorCode)); |
| 818 | } |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 819 | { size_t const errorCode = ZSTD_copyCCtx(ctx, refCtx, 0); |
| 820 | CHECK (ZSTD_isError(errorCode), "ZSTD_copyCCtx error : %s", ZSTD_getErrorName(errorCode)); |
| 821 | } } |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 822 | XXH64_reset(&xxhState, 0); |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 823 | ZSTD_setCCtxParameter(ctx, ZSTD_p_forceWindow, FUZ_rand(&lseed) & 1); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 824 | { U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2; |
| 825 | U32 n; |
| 826 | for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) { |
| 827 | size_t const segmentSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 828 | size_t const segmentStart = FUZ_rand(&lseed) % (srcBufferSize - segmentSize); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 829 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 830 | if (cBufferSize-cSize < ZSTD_compressBound(segmentSize)) break; /* avoid invalid dstBufferTooSmall */ |
| 831 | if (totalTestSize+segmentSize > maxTestSize) break; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 832 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 833 | { size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+segmentStart, segmentSize); |
| 834 | CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult)); |
| 835 | cSize += compressResult; |
| 836 | } |
| 837 | XXH64_update(&xxhState, srcBuffer+segmentStart, segmentSize); |
| 838 | memcpy(mirrorBuffer + totalTestSize, srcBuffer+segmentStart, segmentSize); |
| 839 | totalTestSize += segmentSize; |
| 840 | } } |
| 841 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 842 | { size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize, NULL, 0); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 843 | CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult)); |
| 844 | cSize += flushResult; |
| 845 | } |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 846 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 847 | |
| 848 | /* streaming decompression test */ |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 849 | if (dictSize<8) dictSize=0, dict=NULL; /* disable dictionary */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 850 | { size_t const errorCode = ZSTD_decompressBegin_usingDict(dctx, dict, dictSize); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 851 | CHECK (ZSTD_isError(errorCode), "ZSTD_decompressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); } |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 852 | totalCSize = 0; |
| 853 | totalGenSize = 0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 854 | while (totalCSize < cSize) { |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 855 | size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx); |
| 856 | size_t const genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize); |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 857 | CHECK (ZSTD_isError(genSize), "ZSTD_decompressContinue error : %s", ZSTD_getErrorName(genSize)); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 858 | totalGenSize += genSize; |
| 859 | totalCSize += inSize; |
| 860 | } |
| 861 | CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded"); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 862 | CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size") |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 863 | CHECK (totalCSize != cSize, "compressed data should be fully read") |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 864 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 865 | if (crcDest!=crcOrig) { |
| 866 | size_t const errorPos = findDiff(mirrorBuffer, dstBuffer, totalTestSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 867 | CHECK (1, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)", |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 868 | (U32)errorPos, (U32)totalTestSize, dstBuffer[errorPos], mirrorBuffer[errorPos]); |
| 869 | } } |
| 870 | } /* for ( ; (testNb <= nbTests) */ |
Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 871 | DISPLAY("\r%u fuzzer tests completed \n", testNb-1); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 872 | |
| 873 | _cleanup: |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 874 | ZSTD_freeCCtx(refCtx); |
Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 875 | ZSTD_freeCCtx(ctx); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 876 | ZSTD_freeDCtx(dctx); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 877 | free(cNoiseBuffer[0]); |
| 878 | free(cNoiseBuffer[1]); |
| 879 | free(cNoiseBuffer[2]); |
| 880 | free(cNoiseBuffer[3]); |
| 881 | free(cNoiseBuffer[4]); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 882 | free(cBuffer); |
| 883 | free(dstBuffer); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 884 | free(mirrorBuffer); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 885 | return result; |
| 886 | |
| 887 | _output_error: |
| 888 | result = 1; |
| 889 | goto _cleanup; |
| 890 | } |
| 891 | |
| 892 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 893 | /*_******************************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 894 | * Command line |
| 895 | *********************************************************/ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 896 | int FUZ_usage(const char* programName) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 897 | { |
| 898 | DISPLAY( "Usage :\n"); |
| 899 | DISPLAY( " %s [args]\n", programName); |
| 900 | DISPLAY( "\n"); |
| 901 | DISPLAY( "Arguments :\n"); |
| 902 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 903 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 904 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 905 | DISPLAY( " -P# : Select compressibility in %% (default:%u%%)\n", FUZ_compressibility_default); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 906 | DISPLAY( " -v : verbose\n"); |
Yann Collet | e9853b2 | 2015-08-07 19:07:32 +0100 | [diff] [blame] | 907 | DISPLAY( " -p : pause at the end\n"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 908 | DISPLAY( " -h : display help and exit\n"); |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 913 | int main(int argc, const char** argv) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 914 | { |
| 915 | U32 seed=0; |
| 916 | int seedset=0; |
| 917 | int argNb; |
| 918 | int nbTests = nbTestsDefault; |
| 919 | int testNb = 0; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 920 | U32 proba = FUZ_compressibility_default; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 921 | int result=0; |
| 922 | U32 mainPause = 0; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 923 | U32 maxDuration = 0; |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 924 | const char* programName = argv[0]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 925 | |
| 926 | /* Check command line */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 927 | for (argNb=1; argNb<argc; argNb++) { |
| 928 | const char* argument = argv[argNb]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 929 | if(!argument) continue; /* Protection if argument empty */ |
| 930 | |
| 931 | /* Handle commands. Aggregated commands are allowed */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 932 | if (argument[0]=='-') { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 933 | argument++; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 934 | while (*argument!=0) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 935 | switch(*argument) |
| 936 | { |
| 937 | case 'h': |
| 938 | return FUZ_usage(programName); |
| 939 | case 'v': |
| 940 | argument++; |
| 941 | g_displayLevel=4; |
| 942 | break; |
| 943 | case 'q': |
| 944 | argument++; |
| 945 | g_displayLevel--; |
| 946 | break; |
| 947 | case 'p': /* pause at the end */ |
| 948 | argument++; |
| 949 | mainPause = 1; |
| 950 | break; |
| 951 | |
| 952 | case 'i': |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 953 | argument++; maxDuration=0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 954 | nbTests=0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 955 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 956 | nbTests *= 10; |
| 957 | nbTests += *argument - '0'; |
| 958 | argument++; |
| 959 | } |
| 960 | break; |
| 961 | |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 962 | case 'T': |
| 963 | argument++; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 964 | nbTests=0; maxDuration=0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 965 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 966 | maxDuration *= 10; |
| 967 | maxDuration += *argument - '0'; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 968 | argument++; |
| 969 | } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 970 | if (*argument=='m') maxDuration *=60, argument++; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 971 | if (*argument=='n') argument++; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 972 | break; |
| 973 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 974 | case 's': |
| 975 | argument++; |
| 976 | seed=0; |
| 977 | seedset=1; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 978 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 979 | seed *= 10; |
| 980 | seed += *argument - '0'; |
| 981 | argument++; |
| 982 | } |
| 983 | break; |
| 984 | |
| 985 | case 't': |
| 986 | argument++; |
| 987 | testNb=0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 988 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 989 | testNb *= 10; |
| 990 | testNb += *argument - '0'; |
| 991 | argument++; |
| 992 | } |
| 993 | break; |
| 994 | |
| 995 | case 'P': /* compressibility % */ |
| 996 | argument++; |
| 997 | proba=0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 998 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 999 | proba *= 10; |
| 1000 | proba += *argument - '0'; |
| 1001 | argument++; |
| 1002 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1003 | if (proba>100) proba=100; |
| 1004 | break; |
| 1005 | |
| 1006 | default: |
| 1007 | return FUZ_usage(programName); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1008 | } } } } /* for (argNb=1; argNb<argc; argNb++) */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1009 | |
| 1010 | /* Get Seed */ |
Yann Collet | 45f84ab | 2016-05-20 12:34:40 +0200 | [diff] [blame] | 1011 | DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1012 | |
Yann Collet | 3f01c88 | 2016-06-16 13:38:10 +0200 | [diff] [blame] | 1013 | if (!seedset) { |
| 1014 | time_t const t = time(NULL); |
| 1015 | U32 const h = XXH32(&t, sizeof(t), 1); |
| 1016 | seed = h % 10000; |
| 1017 | } |
| 1018 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1019 | DISPLAY("Seed = %u\n", seed); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 1020 | if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %u%%\n", proba); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1021 | |
Yann Collet | 803c05e | 2016-06-16 11:32:57 +0200 | [diff] [blame] | 1022 | if (nbTests < testNb) nbTests = testNb; |
| 1023 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1024 | if (testNb==0) |
| 1025 | result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1026 | if (!result) |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 1027 | result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 1028 | if (mainPause) { |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 1029 | int unused; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1030 | DISPLAY("Press Enter \n"); |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 1031 | unused = getchar(); |
| 1032 | (void)unused; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1033 | } |
| 1034 | return result; |
| 1035 | } |