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