OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/metrics/net/compression_utils.h" | 5 #include "components/metrics/compression_utils.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base_paths.h" | |
10 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
11 #include "base/file_util.h" | 10 #include "base/rand_util.h" |
12 #include "base/path_service.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
14 | 12 |
15 namespace metrics { | 13 namespace metrics { |
16 | 14 |
17 namespace { | 15 namespace { |
18 | 16 |
19 // The data to be compressed by gzip. This is the hex representation of "hello | 17 // The data to be compressed by gzip. This is the hex representation of "hello |
20 // world". | 18 // world". |
21 const uint8 kData[] = | 19 const uint8 kData[] = |
22 {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, | 20 {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, |
23 0x72, 0x6c, 0x64}; | 21 0x72, 0x6c, 0x64}; |
24 | 22 |
25 // This is the string representation of gzip compressed string above. It was | 23 // 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 ", | 24 // 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 | 25 // 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). | 26 // so that the test passes on all platforms (that run various OS'es). |
29 const uint8 kCompressedData[] = | 27 const uint8 kCompressedData[] = |
30 {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 28 {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
31 0x00, 0xcb, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, | 29 0x00, 0xcb, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, |
32 0x2f, 0xca, 0x49, 0x01, 0x00, 0x85, 0x11, 0x4a, 0x0d, | 30 0x2f, 0xca, 0x49, 0x01, 0x00, 0x85, 0x11, 0x4a, 0x0d, |
33 0x0b, 0x00, 0x00, 0x00}; | 31 0x0b, 0x00, 0x00, 0x00}; |
34 | 32 |
35 // Re-enable C4309. | |
36 #if defined(OS_WIN) | |
37 #pragma warning( default: 4309 ) | |
38 #endif | |
Ilya Sherman
2014/06/09 23:17:00
What was this about, and why is it appropriate to
Alexei Svitkine (slow)
2014/06/10 17:01:15
It's this warning:
http://msdn.microsoft.com/en-u
| |
39 | |
40 } // namespace | 33 } // namespace |
41 | 34 |
42 TEST(CompressionUtilsTest, GzipCompression) { | 35 TEST(CompressionUtilsTest, GzipCompression) { |
43 std::string data(reinterpret_cast<const char*>(kData), arraysize(kData)); | 36 std::string data(reinterpret_cast<const char*>(kData), arraysize(kData)); |
44 std::string compressed_data; | 37 std::string compressed_data; |
45 EXPECT_TRUE(GzipCompress(data, &compressed_data)); | 38 EXPECT_TRUE(GzipCompress(data, &compressed_data)); |
46 std::string golden_compressed_data( | 39 std::string golden_compressed_data( |
47 reinterpret_cast<const char*>(kCompressedData), | 40 reinterpret_cast<const char*>(kCompressedData), |
48 arraysize(kCompressedData)); | 41 arraysize(kCompressedData)); |
49 EXPECT_EQ(golden_compressed_data, compressed_data); | 42 EXPECT_EQ(golden_compressed_data, compressed_data); |
50 } | 43 } |
51 | 44 |
45 TEST(CompressionUtilsTest, GzipUncompression) { | |
46 std::string compressed_data(reinterpret_cast<const char*>(kCompressedData), | |
47 arraysize(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 arraysize(kData)); | |
54 EXPECT_EQ(golden_data, uncompressed_data); | |
55 } | |
56 | |
57 // Checks that compressing/decompressing input > 256 bytes works as expected. | |
58 TEST(CompressionUtilsTest, LargeInput) { | |
59 const size_t kSize = 32 * 1024; | |
60 | |
61 std::string data = base::RandBytesAsString(kSize); | |
Ilya Sherman
2014/06/09 23:17:00
Please log the random input, so that failures are
Alexei Svitkine (slow)
2014/06/10 17:01:15
Changed this test to use deterministic input inste
| |
62 std::string compressed_data; | |
63 EXPECT_TRUE(GzipCompress(data, &compressed_data)); | |
64 | |
65 std::string uncompressed_data; | |
66 EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data)); | |
67 | |
68 EXPECT_EQ(data, uncompressed_data); | |
69 } | |
70 | |
52 } // namespace metrics | 71 } // namespace metrics |
OLD | NEW |