blob: c73d28d08349b7fb91d6f57a7354184720303d2c [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
17using std::string;
18using std::vector;
19
20namespace chromeos_update_engine {
21
22template <typename T>
23class ZipTest : public ::testing::Test {
24 public:
25 bool ZipDecompress(const std::vector<char>& in,
26 std::vector<char>* out) const = 0;
27 bool ZipCompress(const std::vector<char>& in,
28 std::vector<char>* out) const = 0;
29 bool ZipCompressString(const std::string& str,
30 std::vector<char>* out) const = 0;
31 bool ZipDecompressString(const std::string& str,
32 std::vector<char>* out) const = 0;
33};
34
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080035class BzipTest {};
36
37template <>
38class ZipTest<BzipTest> : public ::testing::Test {
39 public:
40 bool ZipDecompress(const std::vector<char>& in,
41 std::vector<char>* out) const {
42 return BzipDecompress(in, out);
43 }
44 bool ZipCompress(const std::vector<char>& in,
45 std::vector<char>* out) const {
46 return BzipCompress(in, out);
47 }
48 bool ZipCompressString(const std::string& str,
49 std::vector<char>* out) const {
50 return BzipCompressString(str, out);
51 }
52 bool ZipDecompressString(const std::string& str,
53 std::vector<char>* out) const {
54 return BzipDecompressString(str, out);
55 }
56};
57
Darin Petkov7ed561b2011-10-04 02:59:03 -070058typedef ::testing::Types<BzipTest> ZipTestTypes;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080059TYPED_TEST_CASE(ZipTest, ZipTestTypes);
60
61
62
63TYPED_TEST(ZipTest, SimpleTest) {
64 string in("this should compress well xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
65 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
66 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
67 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
68 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
69 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
70 vector<char> out;
71 EXPECT_TRUE(this->ZipCompressString(in, &out));
72 EXPECT_LT(out.size(), in.size());
73 EXPECT_GT(out.size(), 0);
74 vector<char> decompressed;
75 EXPECT_TRUE(this->ZipDecompress(out, &decompressed));
76 EXPECT_EQ(in.size(), decompressed.size());
77 EXPECT_TRUE(!memcmp(in.data(), &decompressed[0], in.size()));
78}
79
80TYPED_TEST(ZipTest, PoorCompressionTest) {
81 string in(reinterpret_cast<const char*>(kRandomString),
82 sizeof(kRandomString));
83 vector<char> out;
84 EXPECT_TRUE(this->ZipCompressString(in, &out));
85 EXPECT_GT(out.size(), in.size());
86 string out_string(&out[0], out.size());
87 vector<char> decompressed;
88 EXPECT_TRUE(this->ZipDecompressString(out_string, &decompressed));
89 EXPECT_EQ(in.size(), decompressed.size());
90 EXPECT_TRUE(!memcmp(in.data(), &decompressed[0], in.size()));
91}
92
93TYPED_TEST(ZipTest, MalformedZipTest) {
94 string in(reinterpret_cast<const char*>(kRandomString),
95 sizeof(kRandomString));
96 vector<char> out;
97 EXPECT_FALSE(this->ZipDecompressString(in, &out));
98}
99
100TYPED_TEST(ZipTest, EmptyInputsTest) {
101 string in;
102 vector<char> out;
103 EXPECT_TRUE(this->ZipDecompressString(in, &out));
104 EXPECT_EQ(0, out.size());
105
106 EXPECT_TRUE(this->ZipCompressString(in, &out));
107 EXPECT_EQ(0, out.size());
108}
109
110} // namespace chromeos_update_engine