Home | History | Annotate | Download | only in metrics
      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/metrics/compression_utils.h"
      6 
      7 #include <string>
      8 
      9 #include "base/basictypes.h"
     10 #include "base/logging.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 namespace metrics {
     14 
     15 namespace {
     16 
     17 // The data to be compressed by gzip. This is the hex representation of "hello
     18 // world".
     19 const uint8 kData[] =
     20     {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f,
     21      0x72, 0x6c, 0x64};
     22 
     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};
     32 
     33 }  // namespace
     34 
     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);
     43 }
     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   // Generate a data string of |kSize| for testing.
     62   std::string data;
     63   data.resize(kSize);
     64   for (size_t i = 0; i < kSize; ++i)
     65     data[i] = static_cast<char>(i & 0xFF);
     66 
     67   std::string compressed_data;
     68   EXPECT_TRUE(GzipCompress(data, &compressed_data));
     69 
     70   std::string uncompressed_data;
     71   EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data));
     72 
     73   EXPECT_EQ(data, uncompressed_data);
     74 }
     75 
     76 }  // namespace metrics
     77