Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 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> |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 7 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 10 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 12 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 13 | #include "update_engine/bzip.h" |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 14 | #include "update_engine/test_utils.h" |
| 15 | #include "update_engine/utils.h" |
| 16 | |
| 17 | using std::string; |
| 18 | using std::vector; |
| 19 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
| 22 | template <typename T> |
| 23 | class ZipTest : public ::testing::Test { |
| 24 | public: |
| 25 | bool ZipDecompress(const std::vector<char>& in, |
| 26 | std::vector<char>* out) const = 0; |
| 27 | bool ZipCompress(const std::vector<char>& in, |
| 28 | std::vector<char>* out) const = 0; |
| 29 | bool ZipCompressString(const std::string& str, |
| 30 | std::vector<char>* out) const = 0; |
| 31 | bool ZipDecompressString(const std::string& str, |
| 32 | std::vector<char>* out) const = 0; |
| 33 | }; |
| 34 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 35 | class BzipTest {}; |
| 36 | |
| 37 | template <> |
| 38 | class ZipTest<BzipTest> : public ::testing::Test { |
| 39 | public: |
| 40 | bool ZipDecompress(const std::vector<char>& in, |
| 41 | std::vector<char>* out) const { |
| 42 | return BzipDecompress(in, out); |
| 43 | } |
| 44 | bool ZipCompress(const std::vector<char>& in, |
| 45 | std::vector<char>* out) const { |
| 46 | return BzipCompress(in, out); |
| 47 | } |
| 48 | bool ZipCompressString(const std::string& str, |
| 49 | std::vector<char>* out) const { |
| 50 | return BzipCompressString(str, out); |
| 51 | } |
| 52 | bool ZipDecompressString(const std::string& str, |
| 53 | std::vector<char>* out) const { |
| 54 | return BzipDecompressString(str, out); |
| 55 | } |
| 56 | }; |
| 57 | |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 58 | typedef ::testing::Types<BzipTest> ZipTestTypes; |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 59 | TYPED_TEST_CASE(ZipTest, ZipTestTypes); |
| 60 | |
| 61 | |
| 62 | |
| 63 | TYPED_TEST(ZipTest, SimpleTest) { |
| 64 | string in("this should compress well xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 65 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 66 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 67 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 68 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 69 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); |
| 70 | vector<char> out; |
| 71 | EXPECT_TRUE(this->ZipCompressString(in, &out)); |
| 72 | EXPECT_LT(out.size(), in.size()); |
| 73 | EXPECT_GT(out.size(), 0); |
| 74 | vector<char> decompressed; |
| 75 | EXPECT_TRUE(this->ZipDecompress(out, &decompressed)); |
| 76 | EXPECT_EQ(in.size(), decompressed.size()); |
| 77 | EXPECT_TRUE(!memcmp(in.data(), &decompressed[0], in.size())); |
| 78 | } |
| 79 | |
| 80 | TYPED_TEST(ZipTest, PoorCompressionTest) { |
| 81 | string in(reinterpret_cast<const char*>(kRandomString), |
| 82 | sizeof(kRandomString)); |
| 83 | vector<char> out; |
| 84 | EXPECT_TRUE(this->ZipCompressString(in, &out)); |
| 85 | EXPECT_GT(out.size(), in.size()); |
| 86 | string out_string(&out[0], out.size()); |
| 87 | vector<char> decompressed; |
| 88 | EXPECT_TRUE(this->ZipDecompressString(out_string, &decompressed)); |
| 89 | EXPECT_EQ(in.size(), decompressed.size()); |
| 90 | EXPECT_TRUE(!memcmp(in.data(), &decompressed[0], in.size())); |
| 91 | } |
| 92 | |
| 93 | TYPED_TEST(ZipTest, MalformedZipTest) { |
| 94 | string in(reinterpret_cast<const char*>(kRandomString), |
| 95 | sizeof(kRandomString)); |
| 96 | vector<char> out; |
| 97 | EXPECT_FALSE(this->ZipDecompressString(in, &out)); |
| 98 | } |
| 99 | |
| 100 | TYPED_TEST(ZipTest, EmptyInputsTest) { |
| 101 | string in; |
| 102 | vector<char> out; |
| 103 | EXPECT_TRUE(this->ZipDecompressString(in, &out)); |
| 104 | EXPECT_EQ(0, out.size()); |
| 105 | |
| 106 | EXPECT_TRUE(this->ZipCompressString(in, &out)); |
| 107 | EXPECT_EQ(0, out.size()); |
| 108 | } |
| 109 | |
| 110 | } // namespace chromeos_update_engine |