Skip to content

Commit

Permalink
Sync with various Google-internal changes.
Browse files Browse the repository at this point in the history
Should not mean much for the open-source version.
  • Loading branch information
sesse committed Jun 22, 2015
1 parent 22acaf4 commit 11ccdfb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 33 deletions.
7 changes: 6 additions & 1 deletion snappy-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ namespace File {
} // namespace File

namespace file {
int Defaults() { }
int Defaults() { return 0; }

class DummyStatus {
public:
Expand All @@ -158,6 +158,8 @@ namespace file {
}

fclose(fp);

return DummyStatus();
}

DummyStatus SetContents(const string& filename,
Expand All @@ -176,6 +178,8 @@ namespace file {
}

fclose(fp);

return DummyStatus();
}
} // namespace file

Expand Down Expand Up @@ -572,6 +576,7 @@ class LogMessageVoidify {
#define CHECK_NE(a, b) CRASH_UNLESS((a) != (b))
#define CHECK_LT(a, b) CRASH_UNLESS((a) < (b))
#define CHECK_GT(a, b) CRASH_UNLESS((a) > (b))
#define CHECK_OK(cond) (cond).CheckSuccess()

} // namespace

Expand Down
1 change: 0 additions & 1 deletion snappy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,6 @@ bool Uncompress(const char* compressed, size_t n, string* uncompressed) {
return RawUncompress(compressed, n, string_as_array(uncompressed));
}


// A Writer that drops everything on the floor and just does validation
class SnappyDecompressionValidator {
private:
Expand Down
52 changes: 21 additions & 31 deletions snappy_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ DEFINE_bool(fastlz, false,
"Run FastLZ compression (http://www.fastlz.org/");
DEFINE_bool(snappy, true, "Run snappy compression");


DEFINE_bool(write_compressed, false,
"Write compressed versions of each file to <file>.comp");
DEFINE_bool(write_uncompressed, false,
Expand Down Expand Up @@ -278,7 +277,6 @@ static bool Compress(const char* input, size_t input_size, CompressorType comp,
break;
}


default: {
return false; // the asked-for library wasn't compiled in
}
Expand Down Expand Up @@ -370,7 +368,6 @@ static bool Uncompress(const string& compressed, CompressorType comp,
break;
}


default: {
return false; // the asked-for library wasn't compiled in
}
Expand Down Expand Up @@ -474,7 +471,6 @@ static void Measure(const char* data,
urate.c_str());
}


static int VerifyString(const string& input) {
string compressed;
DataEndingAtUnreadablePage i(input);
Expand Down Expand Up @@ -589,12 +585,9 @@ static int Verify(const string& input) {
VerifyIOVec(input);
}


return result;
}

// This test checks to ensure that snappy doesn't coredump if it gets
// corrupted data.

static bool IsValidCompressedBuffer(const string& c) {
return snappy::IsValidCompressedBuffer(c.data(), c.size());
Expand All @@ -603,11 +596,13 @@ static bool Uncompress(const string& c, string* u) {
return snappy::Uncompress(c.data(), c.size(), u);
}

TYPED_TEST(CorruptedTest, VerifyCorrupted) {
// This test checks to ensure that snappy doesn't coredump if it gets
// corrupted data.
TEST(CorruptedTest, VerifyCorrupted) {
string source = "making sure we don't crash with corrupted input";
VLOG(1) << source;
string dest;
TypeParam uncmp;
string uncmp;
snappy::Compress(source.data(), source.size(), &dest);

// Mess around with the data. It's hard to simulate all possible
Expand All @@ -616,8 +611,8 @@ TYPED_TEST(CorruptedTest, VerifyCorrupted) {
dest[1]--;
dest[3]++;
// this really ought to fail.
CHECK(!IsValidCompressedBuffer(TypeParam(dest)));
CHECK(!Uncompress(TypeParam(dest), &uncmp));
CHECK(!IsValidCompressedBuffer(dest));
CHECK(!Uncompress(dest, &uncmp));

// This is testing for a security bug - a buffer that decompresses to 100k
// but we lie in the snappy header and only reserve 0 bytes of memory :)
Expand All @@ -627,8 +622,8 @@ TYPED_TEST(CorruptedTest, VerifyCorrupted) {
}
snappy::Compress(source.data(), source.size(), &dest);
dest[0] = dest[1] = dest[2] = dest[3] = 0;
CHECK(!IsValidCompressedBuffer(TypeParam(dest)));
CHECK(!Uncompress(TypeParam(dest), &uncmp));
CHECK(!IsValidCompressedBuffer(dest));
CHECK(!Uncompress(dest, &uncmp));

if (sizeof(void *) == 4) {
// Another security check; check a crazy big length can't DoS us with an
Expand All @@ -640,17 +635,17 @@ TYPED_TEST(CorruptedTest, VerifyCorrupted) {
dest[0] = dest[1] = dest[2] = dest[3] = 0xff;
// This decodes to a really large size, i.e., about 3 GB.
dest[4] = 'k';
CHECK(!IsValidCompressedBuffer(TypeParam(dest)));
CHECK(!Uncompress(TypeParam(dest), &uncmp));
CHECK(!IsValidCompressedBuffer(dest));
CHECK(!Uncompress(dest, &uncmp));
} else {
LOG(WARNING) << "Crazy decompression lengths not checked on 64-bit build";
}

// This decodes to about 2 MB; much smaller, but should still fail.
dest[0] = dest[1] = dest[2] = 0xff;
dest[3] = 0x00;
CHECK(!IsValidCompressedBuffer(TypeParam(dest)));
CHECK(!Uncompress(TypeParam(dest), &uncmp));
CHECK(!IsValidCompressedBuffer(dest));
CHECK(!Uncompress(dest, &uncmp));

// try reading stuff in from a bad file.
for (int i = 1; i <= 3; ++i) {
Expand All @@ -665,8 +660,8 @@ TYPED_TEST(CorruptedTest, VerifyCorrupted) {
snappy::ByteArraySource source(data.data(), data.size());
CHECK(!snappy::GetUncompressedLength(&source, &ulen2) ||
(ulen2 < (1<<20)));
CHECK(!IsValidCompressedBuffer(TypeParam(data)));
CHECK(!Uncompress(TypeParam(data), &uncmp));
CHECK(!IsValidCompressedBuffer(data));
CHECK(!Uncompress(data, &uncmp));
}
}

Expand Down Expand Up @@ -929,7 +924,6 @@ TEST(Snappy, IOVecCopyOverflow) {
}
}


static bool CheckUncompressedLength(const string& compressed,
size_t* ulength) {
const bool result1 = snappy::GetUncompressedLength(compressed.data(),
Expand Down Expand Up @@ -998,7 +992,6 @@ TEST(Snappy, ZeroOffsetCopyValidation) {
EXPECT_FALSE(snappy::IsValidCompressedBuffer(compressed, 4));
}


namespace {

int TestFindMatchLength(const char* s1, const char *s2, unsigned length) {
Expand Down Expand Up @@ -1125,21 +1118,20 @@ TEST(Snappy, FindMatchLengthRandom) {
}
}


static void CompressFile(const char* fname) {
string fullinput;
file::GetContents(fname, &fullinput, file::Defaults()).CheckSuccess();
CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));

string compressed;
Compress(fullinput.data(), fullinput.size(), SNAPPY, &compressed, false);

file::SetContents(string(fname).append(".comp"), compressed, file::Defaults())
.CheckSuccess();
CHECK_OK(file::SetContents(string(fname).append(".comp"), compressed,
file::Defaults()));
}

static void UncompressFile(const char* fname) {
string fullinput;
file::GetContents(fname, &fullinput, file::Defaults()).CheckSuccess();
CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));

size_t uncompLength;
CHECK(CheckUncompressedLength(fullinput, &uncompLength));
Expand All @@ -1148,13 +1140,13 @@ static void UncompressFile(const char* fname) {
uncompressed.resize(uncompLength);
CHECK(snappy::Uncompress(fullinput.data(), fullinput.size(), &uncompressed));

file::SetContents(string(fname).append(".uncomp"), uncompressed,
file::Defaults()).CheckSuccess();
CHECK_OK(file::SetContents(string(fname).append(".uncomp"), uncompressed,
file::Defaults()));
}

static void MeasureFile(const char* fname) {
string fullinput;
file::GetContents(fname, &fullinput, file::Defaults()).CheckSuccess();
CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));
printf("%-40s :\n", fname);

int start_len = (FLAGS_start_len < 0) ? fullinput.size() : FLAGS_start_len;
Expand Down Expand Up @@ -1329,15 +1321,13 @@ static void BM_ZFlat(int iters, int arg) {
}
BENCHMARK(BM_ZFlat)->DenseRange(0, ARRAYSIZE(files) - 1);


} // 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) {
Expand Down

0 comments on commit 11ccdfb

Please sign in to comment.