summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Collet <cyan@fb.com>2018-09-05 01:23:21 (GMT)
committerYann Collet <cyan@fb.com>2018-09-05 17:25:38 (GMT)
commit52cce3056227188cedcd36f35a03924425dc14c9 (patch)
treeeeba0f1c6cf5f6e80d354696ab3217e1252a68d1
parent2e4847c2d55fb4c4f5dc833e61b5f374da8407e6 (diff)
downloadlz4-52cce3056227188cedcd36f35a03924425dc14c9.zip
lz4-52cce3056227188cedcd36f35a03924425dc14c9.tar.gz
lz4-52cce3056227188cedcd36f35a03924425dc14c9.tar.bz2
made roundTripTest fully general
no longer "locked" on level 9
-rw-r--r--tests/roundTripTest.c101
1 files changed, 73 insertions, 28 deletions
diff --git a/tests/roundTripTest.c b/tests/roundTripTest.c
index 2caa2bc..2d34451 100644
--- a/tests/roundTripTest.c
+++ b/tests/roundTripTest.c
@@ -10,7 +10,7 @@
/*
* This program takes a file in input,
- * performs an LZ4 round-trip test (compression - decompress)
+ * performs an LZ4 round-trip test (compress + decompress)
* compares the result with original
* and generates an abort() on corruption detection,
* in order for afl to register the event as a crash.
@@ -18,6 +18,15 @@
/*===========================================
+* Tuning Constant
+*==========================================*/
+#ifndef MIN_CLEVEL
+# define MIN_CLEVEL (int)(-5)
+#endif
+
+
+
+/*===========================================
* Dependencies
*==========================================*/
#include <stddef.h> /* size_t */
@@ -62,27 +71,42 @@ static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize
return pos;
}
+
+/* select a compression level
+ * based on first bytes present in a reference buffer */
+static int select_clevel(const void* refBuff, size_t refBuffSize)
+{
+ const int minCLevel = MIN_CLEVEL;
+ const int maxClevel = LZ4HC_CLEVEL_MAX;
+ const int cLevelSpan = maxClevel - minCLevel;
+ size_t const hashLength = MIN(16, refBuffSize);
+ unsigned const h32 = XXH32(refBuff, hashLength, 0);
+ int const randL = h32 % (cLevelSpan+1);
+
+ return minCLevel + randL;
+}
+
+
+typedef int (*compressFn)(const char* src, char* dst, int srcSize, int dstSize, int cLevel);
+
+
/** roundTripTest() :
* Compresses `srcBuff` into `compressedBuff`,
* then decompresses `compressedBuff` into `resultBuff`.
- * Compression level used is derived from content's head bytes.
- * This function abort() if it detects any round-trip error,
- * so if it returns, round trip is considered successfully validated.
+ * If clevel==0, compression level is derived from srcBuff's content head bytes.
+ * This function abort() if it detects any round-trip error.
+ * Therefore, if it returns, round trip is considered successfully validated.
* Note : `compressedBuffCapacity` should be `>= LZ4_compressBound(srcSize)`
* for compression to be guaranteed to work */
static void roundTripTest(void* resultBuff, size_t resultBuffCapacity,
void* compressedBuff, size_t compressedBuffCapacity,
- const void* srcBuff, size_t srcSize)
+ const void* srcBuff, size_t srcSize,
+ int clevel)
{
- const int minCLevel = 1;
- const int maxClevel = 12;
- const int cLevelSpan = maxClevel - minCLevel;
- size_t const hashLength = MIN(16, srcSize);
- unsigned const h32 = XXH32(srcBuff, hashLength, 0);
- int const randL = h32 % (cLevelSpan+1);
- int const cLevel = minCLevel + randL;
- int const realCLevel = (cLevel * 0) + 9; /* <=== Currently : only test level 9 */
- int const cSize = LZ4_compress_HC((const char*)srcBuff, (char*)compressedBuff, (int)srcSize, (int)compressedBuffCapacity, realCLevel);
+ int const proposed_clevel = clevel ? clevel : select_clevel(srcBuff, srcSize);
+ int const selected_clevel = proposed_clevel < 0 ? -proposed_clevel : proposed_clevel; /* if level < 0, it becomes an accelearion value */
+ compressFn compress = selected_clevel >= LZ4HC_CLEVEL_MIN ? LZ4_compress_HC : LZ4_compress_fast;
+ int const cSize = compress((const char*)srcBuff, (char*)compressedBuff, (int)srcSize, (int)compressedBuffCapacity, selected_clevel);
CONTROL_MSG(cSize == 0, "Compression error !");
{ int const dSize = LZ4_decompress_safe((const char*)compressedBuff, (char*)resultBuff, cSize, (int)resultBuffCapacity);
@@ -100,7 +124,7 @@ static void roundTripTest(void* resultBuff, size_t resultBuffCapacity,
}
-static void roundTripCheck(const void* srcBuff, size_t srcSize)
+static void roundTripCheck(const void* srcBuff, size_t srcSize, int clevel)
{
size_t const cBuffSize = LZ4_compressBound((int)srcSize);
void* const cBuff = malloc(cBuffSize);
@@ -113,7 +137,8 @@ static void roundTripCheck(const void* srcBuff, size_t srcSize)
roundTripTest(rBuff, cBuffSize,
cBuff, cBuffSize,
- srcBuff, srcSize);
+ srcBuff, srcSize,
+ clevel);
free(rBuff);
free(cBuff);
@@ -158,46 +183,66 @@ static void loadFile(void* buffer, const char* fileName, size_t fileSize)
{
FILE* const f = fopen(fileName, "rb");
if (isDirectory(fileName)) {
- fprintf(stderr, "Ignoring %s directory \n", fileName);
+ MSG("Ignoring %s directory \n", fileName);
exit(2);
}
if (f==NULL) {
- fprintf(stderr, "Impossible to open %s \n", fileName);
+ MSG("Impossible to open %s \n", fileName);
exit(3);
}
{ size_t const readSize = fread(buffer, 1, fileSize, f);
if (readSize != fileSize) {
- fprintf(stderr, "Error reading %s \n", fileName);
+ MSG("Error reading %s \n", fileName);
exit(5);
} }
fclose(f);
}
-static void fileCheck(const char* fileName)
+static void fileCheck(const char* fileName, int clevel)
{
size_t const fileSize = getFileSize(fileName);
void* const buffer = malloc(fileSize + !fileSize /* avoid 0 */);
if (!buffer) {
- fprintf(stderr, "not enough memory \n");
+ MSG("not enough memory \n");
exit(4);
}
loadFile(buffer, fileName, fileSize);
- roundTripCheck(buffer, fileSize);
+ roundTripCheck(buffer, fileSize, clevel);
free (buffer);
}
+int bad_usage(const char* exeName)
+{
+ MSG(" \n");
+ MSG("bad usage: \n");
+ MSG(" \n");
+ MSG("%s [Options] fileName \n", exeName);
+ MSG(" \n");
+ MSG("Options: \n");
+ MSG("-# : use #=[0-9] compression level (default:0 == random) \n");
+ return 1;
+}
+
+
int main(int argCount, const char** argv)
{
- int const argNb = 1;
+ const char* const exeName = argv[0];
+ int argNb = 1;
+ int clevel = 0;
+
+ assert(argCount >= 1);
+ if (argCount < 2) return bad_usage(exeName);
- if (argCount < 2) {
- fprintf(stderr, "Error : no argument : need input file \n");
- exit(9);
+ if (argv[1][0] == '-') {
+ clevel = argv[1][1] - '0';
+ argNb = 2;
}
- fileCheck(argv[argNb]);
- fprintf(stderr, "no pb detected \n");
+ if (argNb >= argCount) return bad_usage(exeName);
+
+ fileCheck(argv[argNb], clevel);
+ MSG("no pb detected \n");
return 0;
}