Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 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 | |
Tianjie Xu | 4d10c3e | 2017-10-26 14:02:06 -0700 | [diff] [blame] | 9 | #include "bsdiff/brotli_decompressor.h" |
| 10 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 11 | namespace { |
| 12 | |
Tianjie Xu | 4d10c3e | 2017-10-26 14:02:06 -0700 | [diff] [blame] | 13 | constexpr uint8_t kHelloWorld[] = { |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 14 | 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0a, |
| 15 | }; |
| 16 | } // namespace |
| 17 | |
| 18 | namespace bsdiff { |
| 19 | |
| 20 | TEST(BrotliCompressorTest, BrotliCompressorSmoke) { |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 21 | BrotliCompressor brotli_compressor(11); |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 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 | |
Tianjie Xu | 4d10c3e | 2017-10-26 14:02:06 -0700 | [diff] [blame] | 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); |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Tianjie Xu | d4875cd | 2017-11-17 16:06:30 -0800 | [diff] [blame] | 40 | TEST(BrotliCompressorTest, BrotliCompressorEmptyStream) { |
Alex Deymo | 6277fec | 2018-03-12 19:27:27 +0100 | [diff] [blame] | 41 | uint8_t unused_value = 0; |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 42 | BrotliCompressor brotli_compressor(11); |
Alex Deymo | 6277fec | 2018-03-12 19:27:27 +0100 | [diff] [blame] | 43 | EXPECT_TRUE(brotli_compressor.Write(&unused_value, 0)); |
Tianjie Xu | d4875cd | 2017-11-17 16:06:30 -0800 | [diff] [blame] | 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 | |
Alex Deymo | 9bb4ddb | 2018-02-14 16:30:54 +0100 | [diff] [blame] | 55 | } // namespace bsdiff |