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