Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [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/bzip.h" |
| 11 | #include "update_engine/gzip.h" |
| 12 | #include "update_engine/test_utils.h" |
| 13 | #include "update_engine/utils.h" |
| 14 | |
| 15 | using std::string; |
| 16 | using std::vector; |
| 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
| 20 | template <typename T> |
| 21 | class ZipTest : public ::testing::Test { |
| 22 | public: |
| 23 | bool ZipDecompress(const std::vector<char>& in, |
| 24 | std::vector<char>* out) const = 0; |
| 25 | bool ZipCompress(const std::vector<char>& in, |
| 26 | std::vector<char>* out) const = 0; |
| 27 | bool ZipCompressString(const std::string& str, |
| 28 | std::vector<char>* out) const = 0; |
| 29 | bool ZipDecompressString(const std::string& str, |
| 30 | std::vector<char>* out) const = 0; |
| 31 | }; |
| 32 | |
| 33 | class GzipTest {}; |
| 34 | |
| 35 | template <> |
| 36 | class ZipTest<GzipTest> : public ::testing::Test { |
| 37 | public: |
| 38 | bool ZipDecompress(const std::vector<char>& in, |
| 39 | std::vector<char>* out) const { |
| 40 | return GzipDecompress(in, out); |
| 41 | } |
| 42 | bool ZipCompress(const std::vector<char>& in, |
| 43 | std::vector<char>* out) const { |
| 44 | return GzipCompress(in, out); |
| 45 | } |
| 46 | bool ZipCompressString(const std::string& str, |
| 47 | std::vector<char>* out) const { |
| 48 | return GzipCompressString(str, out); |
| 49 | } |
| 50 | bool ZipDecompressString(const std::string& str, |
| 51 | std::vector<char>* out) const { |
| 52 | return GzipDecompressString(str, out); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | class BzipTest {}; |
| 57 | |
| 58 | template <> |
| 59 | class ZipTest<BzipTest> : public ::testing::Test { |
| 60 | public: |
| 61 | bool ZipDecompress(const std::vector<char>& in, |
| 62 | std::vector<char>* out) const { |
| 63 | return BzipDecompress(in, out); |
| 64 | } |
| 65 | bool ZipCompress(const std::vector<char>& in, |
| 66 | std::vector<char>* out) const { |
| 67 | return BzipCompress(in, out); |
| 68 | } |
| 69 | bool ZipCompressString(const std::string& str, |
| 70 | std::vector<char>* out) const { |
| 71 | return BzipCompressString(str, out); |
| 72 | } |
| 73 | bool ZipDecompressString(const std::string& str, |
| 74 | std::vector<char>* out) const { |
| 75 | return BzipDecompressString(str, out); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | typedef ::testing::Types<GzipTest, BzipTest> |
| 80 | ZipTestTypes; |
| 81 | TYPED_TEST_CASE(ZipTest, ZipTestTypes); |
| 82 | |
| 83 | |
| 84 | |
| 85 | TYPED_TEST(ZipTest, SimpleTest) { |
| 86 | string in("this should compress well xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 87 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 88 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 89 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 90 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 91 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); |
| 92 | vector<char> out; |
| 93 | EXPECT_TRUE(this->ZipCompressString(in, &out)); |
| 94 | EXPECT_LT(out.size(), in.size()); |
| 95 | EXPECT_GT(out.size(), 0); |
| 96 | vector<char> decompressed; |
| 97 | EXPECT_TRUE(this->ZipDecompress(out, &decompressed)); |
| 98 | EXPECT_EQ(in.size(), decompressed.size()); |
| 99 | EXPECT_TRUE(!memcmp(in.data(), &decompressed[0], in.size())); |
| 100 | } |
| 101 | |
| 102 | TYPED_TEST(ZipTest, PoorCompressionTest) { |
| 103 | string in(reinterpret_cast<const char*>(kRandomString), |
| 104 | sizeof(kRandomString)); |
| 105 | vector<char> out; |
| 106 | EXPECT_TRUE(this->ZipCompressString(in, &out)); |
| 107 | EXPECT_GT(out.size(), in.size()); |
| 108 | string out_string(&out[0], out.size()); |
| 109 | vector<char> decompressed; |
| 110 | EXPECT_TRUE(this->ZipDecompressString(out_string, &decompressed)); |
| 111 | EXPECT_EQ(in.size(), decompressed.size()); |
| 112 | EXPECT_TRUE(!memcmp(in.data(), &decompressed[0], in.size())); |
| 113 | } |
| 114 | |
| 115 | TYPED_TEST(ZipTest, MalformedZipTest) { |
| 116 | string in(reinterpret_cast<const char*>(kRandomString), |
| 117 | sizeof(kRandomString)); |
| 118 | vector<char> out; |
| 119 | EXPECT_FALSE(this->ZipDecompressString(in, &out)); |
| 120 | } |
| 121 | |
| 122 | TYPED_TEST(ZipTest, EmptyInputsTest) { |
| 123 | string in; |
| 124 | vector<char> out; |
| 125 | EXPECT_TRUE(this->ZipDecompressString(in, &out)); |
| 126 | EXPECT_EQ(0, out.size()); |
| 127 | |
| 128 | EXPECT_TRUE(this->ZipCompressString(in, &out)); |
| 129 | EXPECT_EQ(0, out.size()); |
| 130 | } |
| 131 | |
| 132 | } // namespace chromeos_update_engine |