Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
flatemodule_unittest.cpp
Go to the documentation of this file.
1// Copyright 2023 The PDFium Authors
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 "core/fxcodec/flate/flatemodule.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8#include "testing/test_support.h"
9
10// NOTE: python's zlib.compress() and zlib.decompress() may be useful for
11// external validation of the FlateDncode/FlateEecode test cases.
12TEST(FlateModule, Decode) {
13 static const pdfium::DecodeTestData flate_decode_cases[] = {
14 STR_IN_OUT_CASE("", "", 0),
15 STR_IN_OUT_CASE("preposterous nonsense", "", 2),
16 STR_IN_OUT_CASE("\x78\x9c\x03\x00\x00\x00\x00\x01", "", 8),
17 STR_IN_OUT_CASE("\x78\x9c\x53\x00\x00\x00\x21\x00\x21", " ", 9),
18 STR_IN_OUT_CASE("\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97", "123", 11),
19 STR_IN_OUT_CASE("\x78\x9c\x63\xf8\x0f\x00\x01\x01\x01\x00", "\x00\xff",
20 10),
22 "\x78\x9c\x33\x54\x30\x00\x42\x5d\x43\x05\x23\x4b\x05\x73\x33\x63"
23 "\x85\xe4\x5c\x2e\x90\x80\xa9\xa9\xa9\x82\xb9\xb1\xa9\x42\x51\x2a"
24 "\x57\xb8\x42\x1e\x57\x21\x92\xa0\x89\x9e\xb1\xa5\x09\x92\x84\x9e"
25 "\x85\x81\x81\x25\xd8\x14\x24\x26\xd0\x18\x43\x05\x10\x0c\x72\x57"
26 "\x80\x30\x8a\xd2\xb9\xf4\xdd\x0d\x14\xd2\x8b\xc1\x46\x99\x59\x1a"
27 "\x2b\x58\x1a\x9a\x83\x8c\x49\xe3\x0a\x04\x42\x00\x37\x4c\x1b\x42",
28 "1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n"
29 "W n\nq\n0.8009 0 0 0.8009 0 0 cm\n1 1 1 RG 1 1 1 rg\n/G0 gs\n"
30 "0 0 693 917 re\nf\nQ\nQ\n",
31 96),
32 };
33
34 for (size_t i = 0; i < std::size(flate_decode_cases); ++i) {
35 const pdfium::DecodeTestData& data = flate_decode_cases[i];
36 std::unique_ptr<uint8_t, FxFreeDeleter> buf;
37 uint32_t buf_size;
38 EXPECT_EQ(data.processed_size, FlateModule::FlateOrLZWDecode(
39 false, {data.input, data.input_size},
40 false, 0, 0, 0, 0, 0, &buf, &buf_size))
41 << " for case " << i;
42 ASSERT_TRUE(buf);
43 EXPECT_EQ(data.expected_size, buf_size) << " for case " << i;
44 if (data.expected_size != buf_size) {
45 continue;
46 }
47 EXPECT_EQ(0, memcmp(data.expected, buf.get(), data.expected_size))
48 << " for case " << i;
49 }
50}
51
52TEST(FlateModule, Encode) {
53 static const pdfium::StrFuncTestData flate_encode_cases[] = {
54 STR_IN_OUT_CASE("", "\x78\x9c\x03\x00\x00\x00\x00\x01"),
55 STR_IN_OUT_CASE(" ", "\x78\x9c\x53\x00\x00\x00\x21\x00\x21"),
56 STR_IN_OUT_CASE("123", "\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97"),
57 STR_IN_OUT_CASE("\x00\xff", "\x78\x9c\x63\xf8\x0f\x00\x01\x01\x01\x00"),
59 "1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n"
60 "W n\nq\n0.8009 0 0 0.8009 0 0 cm\n1 1 1 RG 1 1 1 rg\n/G0 gs\n"
61 "0 0 693 917 re\nf\nQ\nQ\n",
62 "\x78\x9c\x4d\x8c\x3b\x0e\x80\x20\x10\x05\xfb\x3d\xc5\xbb\x80\xb8"
63 "\xc8\xcf\x3d\x01\x35\x36\x1e\x80\x28\x15\x26\xe2\xfd\x13\x83\x36"
64 "\xe4\x35\x93\x79\xc9\x68\x30\x18\x93\xc6\x22\x08\xde\x20\x57\xea"
65 "\xc2\x39\x87\x60\x1c\xda\x41\x3b\x2e\xba\x07\x69\x95\x11\x3b\x1c"
66 "\x6a\x65\x96\xaf\x32\x60\xae\xa4\xd1\xb7\x45\xfc\xd0\x0a\xcd\x91"
67 "\x51\x9e\x2f\xe5\xc5\x40\x74\xe8\x99\x93\x12\x25\x7a\x01\x37\x4c"
68 "\x1b\x42"),
69 };
70
71 for (size_t i = 0; i < std::size(flate_encode_cases); ++i) {
72 const pdfium::StrFuncTestData& data = flate_encode_cases[i];
73 DataVector<uint8_t> result =
74 FlateModule::Encode({data.input, data.input_size});
75 EXPECT_EQ(data.expected_size, result.size()) << " for case " << i;
76 if (data.expected_size != result.size()) {
77 continue;
78 }
79 EXPECT_EQ(0, memcmp(data.expected, result.data(), data.expected_size))
80 << " for case " << i;
81 }
82}
static uint32_t FlateOrLZWDecode(bool bLZW, pdfium::span< const uint8_t > src_span, bool bEarlyChange, int predictor, int Colors, int BitsPerComponent, int Columns, uint32_t estimated_size, std::unique_ptr< uint8_t, FxFreeDeleter > *dest_buf, uint32_t *dest_size)
TEST(FlateModule, Decode)
const unsigned char * expected
const unsigned char * input
const unsigned char * expected
#define STR_IN_OUT_CASE(input_literal, expected_literal,...)