adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame^] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string.h> |
| 6 | #include <unistd.h> |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | #include <gtest/gtest.h> |
| 10 | #include "update_engine/gzip.h" |
| 11 | #include "update_engine/test_utils.h" |
| 12 | #include "update_engine/utils.h" |
| 13 | |
| 14 | using std::string; |
| 15 | using std::vector; |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | class GzipTest : public ::testing::Test { }; |
| 20 | |
| 21 | TEST(GzipTest, SimpleTest) { |
| 22 | string in("this should compress well xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 23 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 24 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 25 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 26 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 27 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); |
| 28 | vector<char> out; |
| 29 | EXPECT_TRUE(GzipCompressString(in, &out)); |
| 30 | EXPECT_LT(out.size(), in.size()); |
| 31 | EXPECT_GT(out.size(), 0); |
| 32 | vector<char> decompressed; |
| 33 | EXPECT_TRUE(GzipDecompress(out, &decompressed)); |
| 34 | EXPECT_EQ(in.size(), decompressed.size()); |
| 35 | EXPECT_TRUE(!memcmp(in.data(), &decompressed[0], in.size())); |
| 36 | } |
| 37 | |
| 38 | TEST(GzipTest, PoorCompressionTest) { |
| 39 | string in(reinterpret_cast<const char*>(kRandomString), |
| 40 | sizeof(kRandomString)); |
| 41 | vector<char> out; |
| 42 | EXPECT_TRUE(GzipCompressString(in, &out)); |
| 43 | EXPECT_GT(out.size(), in.size()); |
| 44 | string out_string(&out[0], out.size()); |
| 45 | vector<char> decompressed; |
| 46 | EXPECT_TRUE(GzipDecompressString(out_string, &decompressed)); |
| 47 | EXPECT_EQ(in.size(), decompressed.size()); |
| 48 | EXPECT_TRUE(!memcmp(in.data(), &decompressed[0], in.size())); |
| 49 | } |
| 50 | |
| 51 | TEST(GzipTest, MalformedGzipTest) { |
| 52 | string in(reinterpret_cast<const char*>(kRandomString), |
| 53 | sizeof(kRandomString)); |
| 54 | vector<char> out; |
| 55 | EXPECT_FALSE(GzipDecompressString(in, &out)); |
| 56 | } |
| 57 | |
| 58 | TEST(GzipTest, EmptyInputsTest) { |
| 59 | string in; |
| 60 | vector<char> out; |
| 61 | EXPECT_TRUE(GzipDecompressString(in, &out)); |
| 62 | EXPECT_EQ(0, out.size()); |
| 63 | |
| 64 | EXPECT_TRUE(GzipCompressString(in, &out)); |
| 65 | EXPECT_EQ(0, out.size()); |
| 66 | } |
| 67 | |
| 68 | } // namespace chromeos_update_engine |