blob: 0b6e5b8f23b23b6adff6b5302a2b6fb07bb224b1 [file] [log] [blame]
Darin Petkov7ed561b2011-10-04 02:59:03 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Andrew de los Reyesd2135f32010-03-11 16:00:28 -08002// 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 Petkov7ed561b2011-10-04 02:59:03 -07007
Andrew de los Reyesd2135f32010-03-11 16:00:28 -08008#include <string>
9#include <vector>
Darin Petkov7ed561b2011-10-04 02:59:03 -070010
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080011#include <gtest/gtest.h>
Darin Petkov7ed561b2011-10-04 02:59:03 -070012
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080013#include "update_engine/bzip.h"
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080014#include "update_engine/test_utils.h"
15#include "update_engine/utils.h"
16
Alex Deymo10875d92014-11-10 21:52:57 -080017using chromeos_update_engine::test_utils::kRandomString;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080018using std::string;
19using std::vector;
20
21namespace chromeos_update_engine {
22
23template <typename T>
24class ZipTest : public ::testing::Test {
25 public:
Alex Deymof329b932014-10-30 01:37:48 -070026 bool ZipDecompress(const vector<char>& in,
27 vector<char>* out) const = 0;
28 bool ZipCompress(const vector<char>& in,
29 vector<char>* out) const = 0;
30 bool ZipCompressString(const string& str,
31 vector<char>* out) const = 0;
32 bool ZipDecompressString(const string& str,
33 vector<char>* out) const = 0;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080034};
35
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080036class BzipTest {};
37
38template <>
39class ZipTest<BzipTest> : public ::testing::Test {
40 public:
Alex Deymof329b932014-10-30 01:37:48 -070041 bool ZipDecompress(const vector<char>& in,
42 vector<char>* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080043 return BzipDecompress(in, out);
44 }
Alex Deymof329b932014-10-30 01:37:48 -070045 bool ZipCompress(const vector<char>& in,
46 vector<char>* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080047 return BzipCompress(in, out);
48 }
Alex Deymof329b932014-10-30 01:37:48 -070049 bool ZipCompressString(const string& str,
50 vector<char>* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080051 return BzipCompressString(str, out);
52 }
Alex Deymof329b932014-10-30 01:37:48 -070053 bool ZipDecompressString(const string& str,
54 vector<char>* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080055 return BzipDecompressString(str, out);
56 }
57};
58
Darin Petkov7ed561b2011-10-04 02:59:03 -070059typedef ::testing::Types<BzipTest> ZipTestTypes;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080060TYPED_TEST_CASE(ZipTest, ZipTestTypes);
61
62
63
64TYPED_TEST(ZipTest, SimpleTest) {
65 string in("this should compress well xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
66 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
67 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
68 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
69 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
70 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
71 vector<char> out;
72 EXPECT_TRUE(this->ZipCompressString(in, &out));
73 EXPECT_LT(out.size(), in.size());
74 EXPECT_GT(out.size(), 0);
75 vector<char> decompressed;
76 EXPECT_TRUE(this->ZipDecompress(out, &decompressed));
77 EXPECT_EQ(in.size(), decompressed.size());
78 EXPECT_TRUE(!memcmp(in.data(), &decompressed[0], in.size()));
79}
80
81TYPED_TEST(ZipTest, PoorCompressionTest) {
82 string in(reinterpret_cast<const char*>(kRandomString),
83 sizeof(kRandomString));
84 vector<char> out;
85 EXPECT_TRUE(this->ZipCompressString(in, &out));
86 EXPECT_GT(out.size(), in.size());
87 string out_string(&out[0], out.size());
88 vector<char> decompressed;
89 EXPECT_TRUE(this->ZipDecompressString(out_string, &decompressed));
90 EXPECT_EQ(in.size(), decompressed.size());
91 EXPECT_TRUE(!memcmp(in.data(), &decompressed[0], in.size()));
92}
93
94TYPED_TEST(ZipTest, MalformedZipTest) {
95 string in(reinterpret_cast<const char*>(kRandomString),
96 sizeof(kRandomString));
97 vector<char> out;
98 EXPECT_FALSE(this->ZipDecompressString(in, &out));
99}
100
101TYPED_TEST(ZipTest, EmptyInputsTest) {
102 string in;
103 vector<char> out;
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