blob: 0c95a8fa9f69a32f8c0b573da262e0ab9b5c0866 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Reyesd2135f32010-03-11 16:00:28 -080016
17#include <string.h>
18#include <unistd.h>
Darin Petkov7ed561b2011-10-04 02:59:03 -070019
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080020#include <string>
21#include <vector>
Darin Petkov7ed561b2011-10-04 02:59:03 -070022
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080023#include <gtest/gtest.h>
Darin Petkov7ed561b2011-10-04 02:59:03 -070024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/test_utils.h"
Alex Deymo0bc26112015-10-19 20:54:57 -070026#include "update_engine/payload_generator/bzip.h"
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080027
Alex Deymo10875d92014-11-10 21:52:57 -080028using chromeos_update_engine::test_utils::kRandomString;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080029using std::string;
30using std::vector;
31
32namespace chromeos_update_engine {
33
34template <typename T>
35class ZipTest : public ::testing::Test {
36 public:
Alex Deymo0bc26112015-10-19 20:54:57 -070037 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 Reyesd2135f32010-03-11 16:00:28 -080041};
42
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080043class BzipTest {};
44
45template <>
46class ZipTest<BzipTest> : public ::testing::Test {
47 public:
Alex Deymo0bc26112015-10-19 20:54:57 -070048 bool ZipDecompress(const brillo::Blob& in, brillo::Blob* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080049 return BzipDecompress(in, out);
50 }
Alex Deymo0bc26112015-10-19 20:54:57 -070051 bool ZipCompress(const brillo::Blob& in, brillo::Blob* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080052 return BzipCompress(in, out);
53 }
Alex Deymo0bc26112015-10-19 20:54:57 -070054 bool ZipCompressString(const string& str, brillo::Blob* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080055 return BzipCompressString(str, out);
56 }
Alex Deymo0bc26112015-10-19 20:54:57 -070057 bool ZipDecompressString(const string& str, brillo::Blob* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080058 return BzipDecompressString(str, out);
59 }
60};
61
Darin Petkov7ed561b2011-10-04 02:59:03 -070062typedef ::testing::Types<BzipTest> ZipTestTypes;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080063TYPED_TEST_CASE(ZipTest, ZipTestTypes);
64
65
66
67TYPED_TEST(ZipTest, SimpleTest) {
68 string in("this should compress well xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
69 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
70 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
71 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
72 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
73 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070074 brillo::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080075 EXPECT_TRUE(this->ZipCompressString(in, &out));
76 EXPECT_LT(out.size(), in.size());
Alex Deymo80f70ff2016-02-10 16:08:11 -080077 EXPECT_GT(out.size(), 0U);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070078 brillo::Blob decompressed;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080079 EXPECT_TRUE(this->ZipDecompress(out, &decompressed));
80 EXPECT_EQ(in.size(), decompressed.size());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080081 EXPECT_TRUE(!memcmp(in.data(), decompressed.data(), in.size()));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080082}
83
84TYPED_TEST(ZipTest, PoorCompressionTest) {
85 string in(reinterpret_cast<const char*>(kRandomString),
86 sizeof(kRandomString));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070087 brillo::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080088 EXPECT_TRUE(this->ZipCompressString(in, &out));
89 EXPECT_GT(out.size(), in.size());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080090 string out_string(out.begin(), out.end());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070091 brillo::Blob decompressed;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080092 EXPECT_TRUE(this->ZipDecompressString(out_string, &decompressed));
93 EXPECT_EQ(in.size(), decompressed.size());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080094 EXPECT_TRUE(!memcmp(in.data(), decompressed.data(), in.size()));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080095}
96
97TYPED_TEST(ZipTest, MalformedZipTest) {
98 string in(reinterpret_cast<const char*>(kRandomString),
99 sizeof(kRandomString));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700100 brillo::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800101 EXPECT_FALSE(this->ZipDecompressString(in, &out));
102}
103
104TYPED_TEST(ZipTest, EmptyInputsTest) {
105 string in;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700106 brillo::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800107 EXPECT_TRUE(this->ZipDecompressString(in, &out));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800108 EXPECT_EQ(0U, out.size());
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800109
110 EXPECT_TRUE(this->ZipCompressString(in, &out));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800111 EXPECT_EQ(0U, out.size());
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800112}
113
114} // namespace chromeos_update_engine