blob: 41572dd44264cda65589418197675fa736535d1c [file] [log] [blame]
Tianjie Xu1c26e2e2017-10-26 17:19:41 -07001// 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 Xu4d10c3e2017-10-26 14:02:06 -07009#include "bsdiff/brotli_decompressor.h"
10
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070011namespace {
12
Tianjie Xu4d10c3e2017-10-26 14:02:06 -070013constexpr uint8_t kHelloWorld[] = {
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070014 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0a,
15};
16} // namespace
17
18namespace bsdiff {
19
20TEST(BrotliCompressorTest, BrotliCompressorSmoke) {
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080021 BrotliCompressor brotli_compressor(11);
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070022 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 Xu4d10c3e2017-10-26 14:02:06 -070027 // 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 Xu1c26e2e2017-10-26 17:19:41 -070038}
39
Tianjie Xud4875cd2017-11-17 16:06:30 -080040TEST(BrotliCompressorTest, BrotliCompressorEmptyStream) {
Alex Deymo6277fec2018-03-12 19:27:27 +010041 uint8_t unused_value = 0;
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080042 BrotliCompressor brotli_compressor(11);
Alex Deymo6277fec2018-03-12 19:27:27 +010043 EXPECT_TRUE(brotli_compressor.Write(&unused_value, 0));
Tianjie Xud4875cd2017-11-17 16:06:30 -080044 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 Deymo9bb4ddb2018-02-14 16:30:54 +010055} // namespace bsdiff