add testcases and delete unnecessary test code for snappy

This commit is contained in:
hanxinke 2021-12-02 07:28:34 -05:00
parent d89ffb3518
commit 29a0534e43
2 changed files with 286 additions and 1 deletions

View File

@ -0,0 +1,278 @@
From 72c63614b10be25917c60741b0c58a0387978373 Mon Sep 17 00:00:00 2001
From: hanxinke <hanxinke@huawei.com>
Date: Thu, 2 Dec 2021 06:54:26 -0500
Subject: [PATCH] add testcases and delete unnecessary test code for snappy
Signed-off-by: hanxinke <hanxinke@huawei.com>
---
snappy-test.h | 14 +++++
snappy_unittest.cc | 131 ++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 132 insertions(+), 13 deletions(-)
diff --git a/snappy-test.h b/snappy-test.h
index c8b7d38..4c6ab02 100644
--- a/snappy-test.h
+++ b/snappy-test.h
@@ -170,6 +170,7 @@ namespace snappy {
using TypeParam = std::string;
void Test_CorruptedTest_VerifyCorrupted();
+void Test_CorryptedTest_VerifyUncompressFailed();
void Test_Snappy_SimpleTests();
void Test_Snappy_MaxBlowup();
void Test_Snappy_RandomData();
@@ -180,6 +181,11 @@ void Test_SnappyCorruption_OverflowingVarint();
void Test_Snappy_ReadPastEndOfBuffer();
void Test_Snappy_FindMatchLength();
void Test_Snappy_FindMatchLengthRandom();
+void Test_Snappy_SnappyCompress();
+void Test_Snappy_SnappyUncompress();
+void Test_Snappy_SnappyValidateCompress();
+void Test_Snappy_SnappyValidCompressed();
+void Test_Snappy_SnappySlowAppend();
std::string ReadTestDataFile(const std::string& base, size_t size_limit);
@@ -262,6 +268,7 @@ class Benchmark {
(new Benchmark(#benchmark_name, benchmark_name))
extern Benchmark* Benchmark_BM_UFlat;
+extern Benchmark* Benchmark_BM_UFlatSink;
extern Benchmark* Benchmark_BM_UIOVec;
extern Benchmark* Benchmark_BM_UValidate;
extern Benchmark* Benchmark_BM_ZFlat;
@@ -415,6 +422,7 @@ static inline void RunSpecifiedBenchmarks() {
fprintf(stderr, "---------------------------------------------------\n");
snappy::Benchmark_BM_UFlat->Run();
+ snappy::Benchmark_BM_UFlatSink->Run();
snappy::Benchmark_BM_UIOVec->Run();
snappy::Benchmark_BM_UValidate->Run();
snappy::Benchmark_BM_ZFlat->Run();
@@ -429,6 +437,7 @@ static inline void RunSpecifiedBenchmarks() {
static inline int RUN_ALL_TESTS() {
fprintf(stderr, "Running correctness tests.\n");
snappy::Test_CorruptedTest_VerifyCorrupted();
+ snappy::Test_CorryptedTest_VerifyUncompressFailed();
snappy::Test_Snappy_SimpleTests();
snappy::Test_Snappy_MaxBlowup();
snappy::Test_Snappy_RandomData();
@@ -439,6 +448,11 @@ static inline int RUN_ALL_TESTS() {
snappy::Test_Snappy_ReadPastEndOfBuffer();
snappy::Test_Snappy_FindMatchLength();
snappy::Test_Snappy_FindMatchLengthRandom();
+ snappy::Test_Snappy_SnappyCompress();
+ snappy::Test_Snappy_SnappyUncompress();
+ snappy::Test_Snappy_SnappyValidateCompress();
+ snappy::Test_Snappy_SnappyValidCompressed();
+ snappy::Test_Snappy_SnappySlowAppend();
fprintf(stderr, "All tests passed.\n");
return 0;
diff --git a/snappy_unittest.cc b/snappy_unittest.cc
index 37159c3..1872cac 100644
--- a/snappy_unittest.cc
+++ b/snappy_unittest.cc
@@ -39,6 +39,7 @@
#include "snappy-internal.h"
#include "snappy-test.h"
#include "snappy-sinksource.h"
+#include "snappy-c.h"
DEFINE_int32(start_len, -1,
"Starting prefix size for testing (-1: just full file contents)");
@@ -215,6 +216,25 @@ static bool Compress(const char* input, size_t input_size, CompressorType comp,
return true;
}
+snappy_status SnappyCompress(const char* input, size_t input_length,
+ std::string* compressed, size_t compressed_length) {
+ // Pre-grow the buffer to the max length of the compressed output
+ STLStringResizeUninitialized(compressed, MaxCompressedLength(input_length));
+
+ snappy_status status = snappy_compress(input, input_length, string_as_array(compressed),
+ &compressed_length);
+ compressed->resize(compressed_length);
+ return status;
+}
+
+snappy_status SnappyUncompress(const char* compressed, size_t compressed_length,
+ std::string* uncompressed, size_t uncompressed_length) {
+ STLStringResizeUninitialized(uncompressed, uncompressed_length);
+ snappy_status status = snappy_uncompress(compressed, compressed_length,
+ string_as_array(uncompressed), &uncompressed_length);
+ return status;
+}
+
static bool Uncompress(const std::string& compressed, CompressorType comp,
int size, std::string* output) {
switch (comp) {
@@ -392,6 +412,24 @@ static void VerifyStringSink(const std::string& input) {
CHECK_EQ(uncompressed, input);
}
+static void VerifySinkAsMuchAsPossible(const std::string& input) {
+ std::string compressed;
+ DataEndingAtUnreadablePage i(input);
+ const size_t written = snappy::Compress(i.data(), i.size(), &compressed);
+ CHECK_EQ(written, compressed.size());
+ CHECK_LE(compressed.size(),
+ snappy::MaxCompressedLength(input.size()));
+ CHECK(snappy::IsValidCompressedBuffer(compressed.data(), compressed.size()));
+
+ std::string uncompressed;
+ uncompressed.resize(input.size());
+ snappy::UncheckedByteArraySink sink(string_as_array(&uncompressed));
+ DataEndingAtUnreadablePage c(compressed);
+ snappy::ByteArraySource source(c.data(), c.size());
+ CHECK(snappy::UncompressAsMuchAsPossible(&source, &sink));
+ CHECK_EQ(uncompressed, input);
+}
+
static void VerifyIOVec(const std::string& input) {
std::string compressed;
DataEndingAtUnreadablePage i(input);
@@ -510,6 +548,10 @@ static int Verify(const std::string& input) {
// Verify using sink based routines
VerifyStringSink(input);
+ if (!input.empty()) {
+ VerifySinkAsMuchAsPossible(input);
+ }
+
VerifyNonBlockedCompression(input);
VerifyIOVec(input);
if (!input.empty()) {
@@ -582,7 +624,7 @@ TEST(CorruptedTest, VerifyCorrupted) {
// try reading stuff in from a bad file.
for (int i = 1; i <= 3; ++i) {
std::string data =
- ReadTestDataFile(StrFormat("baddata%d.snappy", i).c_str(), 0);
+ ReadTestDataFile(StrFormat("baddata%d.snappy", i).c_str());
std::string uncmp;
// check that we don't return a crazy length
size_t ulen;
@@ -597,6 +639,15 @@ TEST(CorruptedTest, VerifyCorrupted) {
}
}
+// This test checks to ensure that snappy compression fails.
+TEST(CorruptedTest, VerifyUncompressFailed) {
+ std::string source = "BPmaking sure we don't \r\025$crash";
+ std::string dest;
+ CHECK(!Uncompress(source, &dest));
+ source = "B/ma";
+ CHECK(!Uncompress(source, &dest));
+}
+
// Helper routines to construct arbitrary compressed strings.
// These mirror the compression code in snappy.cc, but are copied
// here so that we can bypass some limitations in the how snappy.cc
@@ -721,7 +772,6 @@ TEST(Snappy, RandomData) {
x.push_back(c);
}
}
-
Verify(x);
}
}
@@ -872,6 +922,59 @@ TEST(Snappy, IOVecCopyOverflow) {
}
}
+TEST(Snappy, SnappyCompress) {
+ std::string source = "making sure we don't crash with corrupted input";
+ std::string compressed;
+ CHECK_EQ(SnappyCompress(source.c_str(), source.size(),
+ &compressed, 100), SNAPPY_OK);
+ CHECK_EQ(SnappyCompress(source.c_str(),
+ source.size(), &compressed, 50), SNAPPY_BUFFER_TOO_SMALL);
+}
+
+TEST(Snappy, SnappyUncompress) {
+ std::string source = "making sure we don't crash with corrupted input";
+ std::string compressed;
+ CHECK_EQ(SnappyCompress(source.c_str(), source.size(),
+ &compressed, 100), SNAPPY_OK);
+ std::string uncompressed;
+ size_t uncompressed_length;
+ CHECK_EQ(SnappyUncompress(compressed.c_str(), compressed.size(),
+ &uncompressed, 47), SNAPPY_OK);
+ CHECK_EQ(snappy_uncompressed_length(compressed.c_str(),
+ compressed.size(), &uncompressed_length), SNAPPY_OK);
+ CHECK_EQ(SnappyUncompress(compressed.c_str(), compressed.size(),
+ &uncompressed, 46), SNAPPY_BUFFER_TOO_SMALL);
+ compressed[0]++;
+ CHECK_EQ(SnappyUncompress(compressed.c_str(), compressed.size(),
+ &uncompressed, 100), SNAPPY_INVALID_INPUT);
+}
+
+TEST(Snappy, SnappyValidateCompress) {
+ std::string source = "making sure we don't crash with corrupted input";
+ std::string compressed;
+ CHECK_EQ(SnappyCompress(source.c_str(), source.size(),
+ &compressed, 100), SNAPPY_OK);
+ CHECK_EQ(snappy_validate_compressed_buffer(compressed.c_str(),
+ compressed.size()), SNAPPY_OK);
+ CHECK_EQ(snappy_validate_compressed_buffer(compressed.c_str(), 50), SNAPPY_INVALID_INPUT);
+}
+
+TEST(Snappy, SnappyValidCompressed) {
+ std::string source = "making sure we don't crash with corrupted input";
+ ByteArraySource reader(source.c_str(), source.size());
+ IsValidCompressed(&reader);
+}
+
+TEST(Snappy, SnappySlowAppend) {
+ std::string compressed = "BPmaking sure we don't\r\025$crash with";
+ std::string uncompressed;
+ uncompressed.resize(compressed.size());
+ snappy::UncheckedByteArraySink sink(string_as_array(&uncompressed));
+ DataEndingAtUnreadablePage c(compressed);
+ snappy::ByteArraySource source(c.data(), c.size());
+ CHECK_EQ(snappy::UncompressAsMuchAsPossible(&source, &sink), 21);
+}
+
static bool CheckUncompressedLength(const std::string& compressed,
size_t* ulength) {
const bool result1 = snappy::GetUncompressedLength(compressed.data(),
@@ -1483,23 +1586,25 @@ static void BM_ZFlatIncreasingTableSize(int iters, int arg) {
}
BENCHMARK(BM_ZFlatIncreasingTableSize)->DenseRange(0, 0);
+
+static void Check_File(int iters) {
+ std::string filename;
+ filename = "testdata/" + std::string(files[iters].filename);
+ fprintf(stderr, "-----%s.\n", filename.c_str());
+ snappy::CompressFile(filename.c_str());
+ filename = filename.append(".comp");
+ snappy::UncompressFile(filename.c_str());
+ snappy::MeasureFile(filename.c_str());
+}
+
} // namespace snappy
int main(int argc, char** argv) {
InitGoogle(argv[0], &argc, &argv, true);
RunSpecifiedBenchmarks();
- if (argc >= 2) {
- for (int arg = 1; arg < argc; arg++) {
- if (FLAGS_write_compressed) {
- snappy::CompressFile(argv[arg]);
- } else if (FLAGS_write_uncompressed) {
- snappy::UncompressFile(argv[arg]);
- } else {
- snappy::MeasureFile(argv[arg]);
- }
- }
- return 0;
+ for (int iter = 0; iter < ARRAYSIZE(snappy::files); iter++) {
+ snappy::Check_File(iter);
}
return RUN_ALL_TESTS();
--
2.23.0

View File

@ -1,6 +1,6 @@
Name: snappy
Version: 1.1.8
Release: 1
Release: 2
Summary: A fast compressor/decompressor
License: BSD
URL: https://github.com/google/snappy
@ -8,6 +8,7 @@ Source0: https://github.com/google/snappy/archive/%{version}/%{name}-%{version}.
Source1: snappy.pc
Patch0: snappy-gtest.patch
Patch1: add-testcases-and-delete-unnecessary-test-code-for-snappy.patch
BuildRequires: gcc-c++ automake autoconf gtest-devel cmake
@ -68,6 +69,12 @@ make test
%doc NEWS README.md
%changelog
* Thu Dec 2 2021 hanxinke<hanxinke@huawei.com> - 1.1.8-2
- Type:enhancement
- ID:NA
- SUG:NA
- DESC: add testcases and delete unnecessary test code for snappy
* Fri Jul 17 2020 shixuantong <shixuantong> - 1.1.8-1
- Type:NA
- ID:NA