OLD | NEW |
| (Empty) |
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 "components/compression/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/macros.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 | |
36 TEST(CompressionUtilsTest, GzipCompression) { | |
37 std::string data(reinterpret_cast<const char*>(kData), arraysize(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 arraysize(kCompressedData)); | |
43 EXPECT_EQ(golden_compressed_data, compressed_data); | |
44 } | |
45 | |
46 TEST(CompressionUtilsTest, GzipUncompression) { | |
47 std::string compressed_data(reinterpret_cast<const char*>(kCompressedData), | |
48 arraysize(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 arraysize(kData)); | |
55 EXPECT_EQ(golden_data, uncompressed_data); | |
56 } | |
57 | |
58 // Checks that compressing/decompressing input > 256 bytes works as expected. | |
59 TEST(CompressionUtilsTest, LargeInput) { | |
60 const size_t kSize = 32 * 1024; | |
61 | |
62 // Generate a data string of |kSize| for testing. | |
63 std::string data; | |
64 data.resize(kSize); | |
65 for (size_t i = 0; i < kSize; ++i) | |
66 data[i] = static_cast<char>(i & 0xFF); | |
67 | |
68 std::string compressed_data; | |
69 EXPECT_TRUE(GzipCompress(data, &compressed_data)); | |
70 | |
71 std::string uncompressed_data; | |
72 EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data)); | |
73 | |
74 EXPECT_EQ(data, uncompressed_data); | |
75 } | |
76 | |
77 TEST(CompressionUtilsTest, InPlace) { | |
78 const std::string original_data(reinterpret_cast<const char*>(kData), | |
79 arraysize(kData)); | |
80 const std::string golden_compressed_data( | |
81 reinterpret_cast<const char*>(kCompressedData), | |
82 arraysize(kCompressedData)); | |
83 | |
84 std::string data(original_data); | |
85 EXPECT_TRUE(GzipCompress(data, &data)); | |
86 EXPECT_EQ(golden_compressed_data, data); | |
87 EXPECT_TRUE(GzipUncompress(data, &data)); | |
88 EXPECT_EQ(original_data, data); | |
89 } | |
90 | |
91 } // namespace compression | |
OLD | NEW |