blob: 4dcee597fbf55920b5b4426e12f6a53a795ed3fa [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 Vakulenkof68bbbc2015-02-09 12:53:18 -080026 bool ZipDecompress(const chromeos::Blob& in,
27 chromeos::Blob* out) const = 0;
28 bool ZipCompress(const chromeos::Blob& in,
29 chromeos::Blob* out) const = 0;
Alex Deymof329b932014-10-30 01:37:48 -070030 bool ZipCompressString(const string& str,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080031 chromeos::Blob* out) const = 0;
Alex Deymof329b932014-10-30 01:37:48 -070032 bool ZipDecompressString(const string& str,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080033 chromeos::Blob* 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 Vakulenkof68bbbc2015-02-09 12:53:18 -080041 bool ZipDecompress(const chromeos::Blob& in,
42 chromeos::Blob* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080043 return BzipDecompress(in, out);
44 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080045 bool ZipCompress(const chromeos::Blob& in,
46 chromeos::Blob* 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,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080050 chromeos::Blob* 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,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080054 chromeos::Blob* 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");
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080071 chromeos::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080072 EXPECT_TRUE(this->ZipCompressString(in, &out));
73 EXPECT_LT(out.size(), in.size());
74 EXPECT_GT(out.size(), 0);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080075 chromeos::Blob decompressed;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080076 EXPECT_TRUE(this->ZipDecompress(out, &decompressed));
77 EXPECT_EQ(in.size(), decompressed.size());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080078 EXPECT_TRUE(!memcmp(in.data(), decompressed.data(), in.size()));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080079}
80
81TYPED_TEST(ZipTest, PoorCompressionTest) {
82 string in(reinterpret_cast<const char*>(kRandomString),
83 sizeof(kRandomString));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080084 chromeos::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080085 EXPECT_TRUE(this->ZipCompressString(in, &out));
86 EXPECT_GT(out.size(), in.size());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080087 string out_string(out.begin(), out.end());
88 chromeos::Blob decompressed;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080089 EXPECT_TRUE(this->ZipDecompressString(out_string, &decompressed));
90 EXPECT_EQ(in.size(), decompressed.size());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080091 EXPECT_TRUE(!memcmp(in.data(), decompressed.data(), in.size()));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080092}
93
94TYPED_TEST(ZipTest, MalformedZipTest) {
95 string in(reinterpret_cast<const char*>(kRandomString),
96 sizeof(kRandomString));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080097 chromeos::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080098 EXPECT_FALSE(this->ZipDecompressString(in, &out));
99}
100
101TYPED_TEST(ZipTest, EmptyInputsTest) {
102 string in;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800103 chromeos::Blob out;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800104 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