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.
5 #include "components/metrics/compression_utils.h"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 // The data to be compressed by gzip. This is the hex representation of "hello
20 {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f,
23 // This is the string representation of gzip compressed string above. It was
24 // obtained by running echo -n "hello world" | gzip -c | hexdump -e '8 1 ",
25 // 0x%x"' followed by 0'ing out the OS byte (10th byte) in the header. This is
26 // so that the test passes on all platforms (that run various OS'es).
27 const uint8 kCompressedData
[] =
28 {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29 0x00, 0xcb, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf,
30 0x2f, 0xca, 0x49, 0x01, 0x00, 0x85, 0x11, 0x4a, 0x0d,
31 0x0b, 0x00, 0x00, 0x00};
35 TEST(CompressionUtilsTest
, GzipCompression
) {
36 std::string
data(reinterpret_cast<const char*>(kData
), arraysize(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 arraysize(kCompressedData
));
42 EXPECT_EQ(golden_compressed_data
, compressed_data
);
45 TEST(CompressionUtilsTest
, GzipUncompression
) {
46 std::string
compressed_data(reinterpret_cast<const char*>(kCompressedData
),
47 arraysize(kCompressedData
));
49 std::string uncompressed_data
;
50 EXPECT_TRUE(GzipUncompress(compressed_data
, &uncompressed_data
));
52 std::string
golden_data(reinterpret_cast<const char*>(kData
),
54 EXPECT_EQ(golden_data
, uncompressed_data
);
57 // Checks that compressing/decompressing input > 256 bytes works as expected.
58 TEST(CompressionUtilsTest
, LargeInput
) {
59 const size_t kSize
= 32 * 1024;
61 // Generate a data string of |kSize| for testing.
64 for (size_t i
= 0; i
< kSize
; ++i
)
65 data
[i
] = static_cast<char>(i
& 0xFF);
67 std::string compressed_data
;
68 EXPECT_TRUE(GzipCompress(data
, &compressed_data
));
70 std::string uncompressed_data
;
71 EXPECT_TRUE(GzipUncompress(compressed_data
, &uncompressed_data
));
73 EXPECT_EQ(data
, uncompressed_data
);
76 TEST(CompressionUtilsTest
, InPlace
) {
77 const std::string
original_data(reinterpret_cast<const char*>(kData
),
79 const std::string
golden_compressed_data(
80 reinterpret_cast<const char*>(kCompressedData
),
81 arraysize(kCompressedData
));
83 std::string
data(original_data
);
84 EXPECT_TRUE(GzipCompress(data
, &data
));
85 EXPECT_EQ(golden_compressed_data
, data
);
86 EXPECT_TRUE(GzipUncompress(data
, &data
));
87 EXPECT_EQ(original_data
, data
);
90 } // namespace metrics