Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (C) 2011 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 16 | |
| 17 | #include <string.h> |
| 18 | #include <unistd.h> |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 19 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 22 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 24 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 25 | #include "update_engine/bzip.h" |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 26 | #include "update_engine/test_utils.h" |
| 27 | #include "update_engine/utils.h" |
| 28 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 29 | using chromeos_update_engine::test_utils::kRandomString; |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 30 | using std::string; |
| 31 | using std::vector; |
| 32 | |
| 33 | namespace chromeos_update_engine { |
| 34 | |
| 35 | template <typename T> |
| 36 | class ZipTest : public ::testing::Test { |
| 37 | public: |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 38 | bool ZipDecompress(const chromeos::Blob& in, |
| 39 | chromeos::Blob* out) const = 0; |
| 40 | bool ZipCompress(const chromeos::Blob& in, |
| 41 | chromeos::Blob* out) const = 0; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 42 | bool ZipCompressString(const string& str, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 43 | chromeos::Blob* out) const = 0; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 44 | bool ZipDecompressString(const string& str, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 45 | chromeos::Blob* out) const = 0; |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 48 | class BzipTest {}; |
| 49 | |
| 50 | template <> |
| 51 | class ZipTest<BzipTest> : public ::testing::Test { |
| 52 | public: |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 53 | bool ZipDecompress(const chromeos::Blob& in, |
| 54 | chromeos::Blob* out) const { |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 55 | return BzipDecompress(in, out); |
| 56 | } |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 57 | bool ZipCompress(const chromeos::Blob& in, |
| 58 | chromeos::Blob* out) const { |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 59 | return BzipCompress(in, out); |
| 60 | } |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 61 | bool ZipCompressString(const string& str, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 62 | chromeos::Blob* out) const { |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 63 | return BzipCompressString(str, out); |
| 64 | } |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 65 | bool ZipDecompressString(const string& str, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 66 | chromeos::Blob* out) const { |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 67 | return BzipDecompressString(str, out); |
| 68 | } |
| 69 | }; |
| 70 | |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 71 | typedef ::testing::Types<BzipTest> ZipTestTypes; |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 72 | TYPED_TEST_CASE(ZipTest, ZipTestTypes); |
| 73 | |
| 74 | |
| 75 | |
| 76 | TYPED_TEST(ZipTest, SimpleTest) { |
| 77 | string in("this should compress well xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 78 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 79 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 80 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 81 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 82 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 83 | chromeos::Blob out; |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 84 | EXPECT_TRUE(this->ZipCompressString(in, &out)); |
| 85 | EXPECT_LT(out.size(), in.size()); |
| 86 | EXPECT_GT(out.size(), 0); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 87 | chromeos::Blob decompressed; |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 88 | EXPECT_TRUE(this->ZipDecompress(out, &decompressed)); |
| 89 | EXPECT_EQ(in.size(), decompressed.size()); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 90 | EXPECT_TRUE(!memcmp(in.data(), decompressed.data(), in.size())); |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TYPED_TEST(ZipTest, PoorCompressionTest) { |
| 94 | string in(reinterpret_cast<const char*>(kRandomString), |
| 95 | sizeof(kRandomString)); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 96 | chromeos::Blob out; |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 97 | EXPECT_TRUE(this->ZipCompressString(in, &out)); |
| 98 | EXPECT_GT(out.size(), in.size()); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 99 | string out_string(out.begin(), out.end()); |
| 100 | chromeos::Blob decompressed; |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 101 | EXPECT_TRUE(this->ZipDecompressString(out_string, &decompressed)); |
| 102 | EXPECT_EQ(in.size(), decompressed.size()); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 103 | EXPECT_TRUE(!memcmp(in.data(), decompressed.data(), in.size())); |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TYPED_TEST(ZipTest, MalformedZipTest) { |
| 107 | string in(reinterpret_cast<const char*>(kRandomString), |
| 108 | sizeof(kRandomString)); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 109 | chromeos::Blob out; |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 110 | EXPECT_FALSE(this->ZipDecompressString(in, &out)); |
| 111 | } |
| 112 | |
| 113 | TYPED_TEST(ZipTest, EmptyInputsTest) { |
| 114 | string in; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 115 | chromeos::Blob out; |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 116 | EXPECT_TRUE(this->ZipDecompressString(in, &out)); |
| 117 | EXPECT_EQ(0, out.size()); |
| 118 | |
| 119 | EXPECT_TRUE(this->ZipCompressString(in, &out)); |
| 120 | EXPECT_EQ(0, out.size()); |
| 121 | } |
| 122 | |
| 123 | } // namespace chromeos_update_engine |