• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium OS 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 "bsdiff/brotli_compressor.h"
6 
7 #include <gtest/gtest.h>
8 
9 #include "bsdiff/brotli_decompressor.h"
10 
11 namespace {
12 
13 constexpr uint8_t kHelloWorld[] = {
14     0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0a,
15 };
16 }  // namespace
17 
18 namespace bsdiff {
19 
TEST(BrotliCompressorTest,BrotliCompressorSmoke)20 TEST(BrotliCompressorTest, BrotliCompressorSmoke) {
21   BrotliCompressor brotli_compressor(11);
22   EXPECT_TRUE(brotli_compressor.Write(kHelloWorld, sizeof(kHelloWorld)));
23   EXPECT_TRUE(brotli_compressor.Finish());
24   std::vector<uint8_t> compressed_data = brotli_compressor.GetCompressedData();
25   EXPECT_GT(compressed_data.size(), static_cast<size_t>(0));
26 
27   // Run decompressor and check we can get the exact same data as kHelloWorld.
28   std::vector<uint8_t> decompressed_data(sizeof(kHelloWorld));
29   BrotliDecompressor brotli_decompressor;
30   EXPECT_TRUE(brotli_decompressor.SetInputData(compressed_data.data(),
31                                                compressed_data.size()));
32   EXPECT_TRUE(
33       brotli_decompressor.Read(decompressed_data.data(), sizeof(kHelloWorld)));
34   EXPECT_TRUE(brotli_decompressor.Close());
35   EXPECT_EQ(
36       std::vector<uint8_t>(kHelloWorld, kHelloWorld + sizeof(kHelloWorld)),
37       decompressed_data);
38 }
39 
TEST(BrotliCompressorTest,BrotliCompressorEmptyStream)40 TEST(BrotliCompressorTest, BrotliCompressorEmptyStream) {
41   uint8_t unused_value = 0;
42   BrotliCompressor brotli_compressor(11);
43   EXPECT_TRUE(brotli_compressor.Write(&unused_value, 0));
44   EXPECT_TRUE(brotli_compressor.Finish());
45 
46   std::vector<uint8_t> compressed_data = brotli_compressor.GetCompressedData();
47 
48   // Check that we can close the decompressor without errors.
49   BrotliDecompressor brotli_decompressor;
50   EXPECT_TRUE(brotli_decompressor.SetInputData(compressed_data.data(),
51                                                compressed_data.size()));
52   EXPECT_TRUE(brotli_decompressor.Close());
53 }
54 
55 }  // namespace bsdiff
56