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