blob: bcaee82ee3b88aa1463bc367ee3b96d3b7565c98 [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
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080025#include "update_engine/bzip.h"
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080026#include "update_engine/test_utils.h"
27#include "update_engine/utils.h"
28
Alex Deymo10875d92014-11-10 21:52:57 -080029using chromeos_update_engine::test_utils::kRandomString;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080030using std::string;
31using std::vector;
32
33namespace chromeos_update_engine {
34
35template <typename T>
36class ZipTest : public ::testing::Test {
37 public:
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080038 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 Deymof329b932014-10-30 01:37:48 -070042 bool ZipCompressString(const string& str,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080043 chromeos::Blob* out) const = 0;
Alex Deymof329b932014-10-30 01:37:48 -070044 bool ZipDecompressString(const string& str,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080045 chromeos::Blob* out) const = 0;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080046};
47
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080048class BzipTest {};
49
50template <>
51class ZipTest<BzipTest> : public ::testing::Test {
52 public:
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080053 bool ZipDecompress(const chromeos::Blob& in,
54 chromeos::Blob* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080055 return BzipDecompress(in, out);
56 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080057 bool ZipCompress(const chromeos::Blob& in,
58 chromeos::Blob* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080059 return BzipCompress(in, out);
60 }
Alex Deymof329b932014-10-30 01:37:48 -070061 bool ZipCompressString(const string& str,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080062 chromeos::Blob* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080063 return BzipCompressString(str, out);
64 }
Alex Deymof329b932014-10-30 01:37:48 -070065 bool ZipDecompressString(const string& str,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080066 chromeos::Blob* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080067 return BzipDecompressString(str, out);
68 }
69};
70
Darin Petkov7ed561b2011-10-04 02:59:03 -070071typedef ::testing::Types<BzipTest> ZipTestTypes;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080072TYPED_TEST_CASE(ZipTest, ZipTestTypes);
73
74
75
76TYPED_TEST(ZipTest, SimpleTest) {
77 string in("this should compress well xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
78 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
79 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
80 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
81 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
82 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080083 chromeos::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080084 EXPECT_TRUE(this->ZipCompressString(in, &out));
85 EXPECT_LT(out.size(), in.size());
86 EXPECT_GT(out.size(), 0);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080087 chromeos::Blob decompressed;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080088 EXPECT_TRUE(this->ZipDecompress(out, &decompressed));
89 EXPECT_EQ(in.size(), decompressed.size());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080090 EXPECT_TRUE(!memcmp(in.data(), decompressed.data(), in.size()));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080091}
92
93TYPED_TEST(ZipTest, PoorCompressionTest) {
94 string in(reinterpret_cast<const char*>(kRandomString),
95 sizeof(kRandomString));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080096 chromeos::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080097 EXPECT_TRUE(this->ZipCompressString(in, &out));
98 EXPECT_GT(out.size(), in.size());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080099 string out_string(out.begin(), out.end());
100 chromeos::Blob decompressed;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800101 EXPECT_TRUE(this->ZipDecompressString(out_string, &decompressed));
102 EXPECT_EQ(in.size(), decompressed.size());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800103 EXPECT_TRUE(!memcmp(in.data(), decompressed.data(), in.size()));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800104}
105
106TYPED_TEST(ZipTest, MalformedZipTest) {
107 string in(reinterpret_cast<const char*>(kRandomString),
108 sizeof(kRandomString));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800109 chromeos::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800110 EXPECT_FALSE(this->ZipDecompressString(in, &out));
111}
112
113TYPED_TEST(ZipTest, EmptyInputsTest) {
114 string in;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800115 chromeos::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800116 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