• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/zlib/google/compression_utils.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <string>
11 
12 #include "base/logging.h"
13 #include "base/stl_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace compression {
17 
18 namespace {
19 
20 // The data to be compressed by gzip. This is the hex representation of "hello
21 // world".
22 const uint8_t kData[] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
23                          0x77, 0x6f, 0x72, 0x6c, 0x64};
24 
25 // This is the string representation of gzip compressed string above. It was
26 // obtained by running echo -n "hello world" | gzip -c | hexdump -e '8 1 ",
27 // 0x%x"' followed by 0'ing out the OS byte (10th byte) in the header. This is
28 // so that the test passes on all platforms (that run various OS'es).
29 const uint8_t kCompressedData[] = {
30     0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb,
31     0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0x01,
32     0x00, 0x85, 0x11, 0x4a, 0x0d, 0x0b, 0x00, 0x00, 0x00};
33 
34 }  // namespace
35 
TEST(CompressionUtilsTest,GzipCompression)36 TEST(CompressionUtilsTest, GzipCompression) {
37   std::string data(reinterpret_cast<const char*>(kData), base::size(kData));
38   std::string compressed_data;
39   EXPECT_TRUE(GzipCompress(data, &compressed_data));
40   std::string golden_compressed_data(
41       reinterpret_cast<const char*>(kCompressedData),
42       base::size(kCompressedData));
43   EXPECT_EQ(golden_compressed_data, compressed_data);
44 }
45 
TEST(CompressionUtilsTest,GzipUncompression)46 TEST(CompressionUtilsTest, GzipUncompression) {
47   std::string compressed_data(reinterpret_cast<const char*>(kCompressedData),
48                               base::size(kCompressedData));
49 
50   std::string uncompressed_data;
51   EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data));
52 
53   std::string golden_data(reinterpret_cast<const char*>(kData),
54                           base::size(kData));
55   EXPECT_EQ(golden_data, uncompressed_data);
56 }
57 
TEST(CompressionUtilsTest,GzipUncompressionFromStringPieceToString)58 TEST(CompressionUtilsTest, GzipUncompressionFromStringPieceToString) {
59   base::StringPiece compressed_data(
60       reinterpret_cast<const char*>(kCompressedData),
61       base::size(kCompressedData));
62 
63   std::string uncompressed_data;
64   EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data));
65 
66   std::string golden_data(reinterpret_cast<const char*>(kData),
67                           base::size(kData));
68   EXPECT_EQ(golden_data, uncompressed_data);
69 }
70 
71 // Checks that compressing/decompressing input > 256 bytes works as expected.
TEST(CompressionUtilsTest,LargeInput)72 TEST(CompressionUtilsTest, LargeInput) {
73   const size_t kSize = 32 * 1024;
74 
75   // Generate a data string of |kSize| for testing.
76   std::string data;
77   data.resize(kSize);
78   for (size_t i = 0; i < kSize; ++i)
79     data[i] = static_cast<char>(i & 0xFF);
80 
81   std::string compressed_data;
82   EXPECT_TRUE(GzipCompress(data, &compressed_data));
83 
84   std::string uncompressed_data;
85   EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data));
86 
87   EXPECT_EQ(data, uncompressed_data);
88 }
89 
TEST(CompressionUtilsTest,InPlace)90 TEST(CompressionUtilsTest, InPlace) {
91   const std::string original_data(reinterpret_cast<const char*>(kData),
92                                   base::size(kData));
93   const std::string golden_compressed_data(
94       reinterpret_cast<const char*>(kCompressedData),
95       base::size(kCompressedData));
96 
97   std::string data(original_data);
98   EXPECT_TRUE(GzipCompress(data, &data));
99   EXPECT_EQ(golden_compressed_data, data);
100   EXPECT_TRUE(GzipUncompress(data, &data));
101   EXPECT_EQ(original_data, data);
102 }
103 
104 }  // namespace compression
105