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