2022-03-31 19:07:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Michiel Visser <opensource@webmichiel.nl>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibTest/TestCase.h>
|
|
|
|
|
|
|
|
#include <LibCompress/Brotli.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2022-03-31 19:07:45 +00:00
|
|
|
|
|
|
|
static void run_test(StringView const file_name)
|
|
|
|
{
|
|
|
|
// This makes sure that the tests will run both on target and in Lagom.
|
2022-10-09 21:23:23 +00:00
|
|
|
#ifdef AK_OS_SERENITY
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString path = DeprecatedString::formatted("/usr/Tests/LibCompress/brotli-test-files/{}", file_name);
|
2022-03-31 19:07:45 +00:00
|
|
|
#else
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString path = DeprecatedString::formatted("brotli-test-files/{}", file_name);
|
2022-03-31 19:07:45 +00:00
|
|
|
#endif
|
|
|
|
|
2023-02-09 02:02:46 +00:00
|
|
|
auto cmp_file = MUST(Core::File::open(path, Core::File::OpenMode::Read));
|
2022-12-11 16:49:00 +00:00
|
|
|
auto cmp_data = MUST(cmp_file->read_until_eof());
|
2022-03-31 19:07:45 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString path_compressed = DeprecatedString::formatted("{}.br", path);
|
2022-03-31 19:07:45 +00:00
|
|
|
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file = MUST(Core::File::open(path_compressed, Core::File::OpenMode::Read));
|
2023-06-08 17:48:05 +00:00
|
|
|
auto brotli_stream = Compress::BrotliDecompressionStream { MaybeOwned<Stream> { *file } };
|
2022-12-11 16:49:00 +00:00
|
|
|
auto data = MUST(brotli_stream.read_until_eof());
|
2022-03-31 19:07:45 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(data, cmp_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(brotli_decompress_uncompressed)
|
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
run_test("wellhello.txt"sv);
|
2022-03-31 19:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(brotli_decompress_simple)
|
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
run_test("hello.txt"sv);
|
2022-03-31 19:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(brotli_decompress_simple2)
|
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
run_test("wellhello2.txt"sv);
|
2022-03-31 19:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(brotli_decompress_lorem)
|
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
run_test("lorem.txt"sv);
|
2022-03-31 19:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(brotli_decompress_lorem2)
|
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
run_test("lorem2.txt"sv);
|
2022-03-31 19:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(brotli_decompress_transform)
|
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
run_test("transform.txt"sv);
|
2022-03-31 19:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(brotli_decompress_serenityos_html)
|
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
run_test("serenityos.html"sv);
|
2022-03-31 19:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(brotli_decompress_happy3rd_html)
|
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
run_test("happy3rd.html"sv);
|
2022-03-31 19:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(brotli_decompress_katica_regular_10_font)
|
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
run_test("KaticaRegular10.font"sv);
|
2022-03-31 19:07:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 17:21:29 +00:00
|
|
|
TEST_CASE(brotli_single_z)
|
|
|
|
{
|
|
|
|
run_test("single-z.txt"sv);
|
|
|
|
}
|
|
|
|
|
2022-09-18 17:38:07 +00:00
|
|
|
TEST_CASE(brotli_single_x)
|
|
|
|
{
|
|
|
|
run_test("single-x.txt"sv);
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:07:45 +00:00
|
|
|
TEST_CASE(brotli_decompress_zero_one_bin)
|
|
|
|
{
|
|
|
|
// This makes sure that the tests will run both on target and in Lagom.
|
2022-10-09 21:23:23 +00:00
|
|
|
#ifdef AK_OS_SERENITY
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString path = "/usr/Tests/LibCompress/brotli-test-files/zero-one.bin";
|
2022-03-31 19:07:45 +00:00
|
|
|
#else
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString path = "brotli-test-files/zero-one.bin";
|
2022-03-31 19:07:45 +00:00
|
|
|
#endif
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString path_compressed = DeprecatedString::formatted("{}.br", path);
|
2022-03-31 19:07:45 +00:00
|
|
|
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file = MUST(Core::File::open(path_compressed, Core::File::OpenMode::Read));
|
2023-06-08 17:48:05 +00:00
|
|
|
auto brotli_stream = Compress::BrotliDecompressionStream { MaybeOwned<Stream> { *file } };
|
2022-03-31 19:07:45 +00:00
|
|
|
|
|
|
|
u8 buffer_raw[4096];
|
|
|
|
Bytes buffer { buffer_raw, 4096 };
|
|
|
|
|
|
|
|
size_t bytes_read = 0;
|
|
|
|
while (true) {
|
2023-02-24 21:38:01 +00:00
|
|
|
size_t nread = MUST(brotli_stream.read_some(buffer)).size();
|
2022-03-31 19:07:45 +00:00
|
|
|
if (nread == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < nread; i++) {
|
|
|
|
if (bytes_read < 16 * MiB)
|
|
|
|
EXPECT(buffer[i] == 0);
|
|
|
|
else
|
|
|
|
EXPECT(buffer[i] == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes_read += nread;
|
|
|
|
}
|
|
|
|
EXPECT(bytes_read == 32 * MiB);
|
|
|
|
EXPECT(brotli_stream.is_eof());
|
|
|
|
}
|