1 // Copyright 2015 PDFium 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 <cstring>
6 #include <string>
7
8 #include "../../../../public/fpdfview.h"
9 #include "../../../../testing/fx_string_testhelpers.h"
10 #include "../../../include/fpdfapi/fpdf_parser.h"
11 #include "../../../include/fxcrt/fx_basic.h"
12 #include "../../../testing/embedder_test.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 class FPDFParserDecodeEmbeddertest : public EmbedderTest {
16 };
17
18 // NOTE: python's zlib.compress() and zlib.decompress() may be useful for
19 // external validation of the FlateEncode/FlateDecode test cases.
20
21 #define TEST_CASE(input_literal, expected_literal) \
22 { (const unsigned char*)input_literal, sizeof(input_literal) - 1, \
23 (const unsigned char*)expected_literal, sizeof(expected_literal) - 1 }
24
TEST_F(FPDFParserDecodeEmbeddertest,FlateEncode)25 TEST_F(FPDFParserDecodeEmbeddertest, FlateEncode) {
26 struct FlateEncodeCase {
27 const unsigned char* input;
28 unsigned int input_size;
29 const unsigned char* expected;
30 unsigned int expected_size;
31 } flate_encode_cases[] = {
32 TEST_CASE("", "\x78\x9c\x03\x00\x00\x00\x00\x01"),
33 TEST_CASE(" ", "\x78\x9c\x53\x00\x00\x00\x21\x00\x21"),
34 TEST_CASE("123", "\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97"),
35 TEST_CASE("\x00\xff", "\x78\x9c\x63\xf8\x0f\x00\x01\x01\x01\x00"),
36 TEST_CASE("1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n"
37 "W n\nq\n0.8009 0 0 0.8009 0 0 cm\n1 1 1 RG 1 1 1 rg\n/G0 gs\n"
38 "0 0 693 917 re\nf\nQ\nQ\n"
39 ,
40 "\x78\x9c\x33\x54\x30\x00\x42\x5d\x43\x05\x23\x4b\x05\x73\x33\x63"
41 "\x85\xe4\x5c\x2e\x90\x80\xa9\xa9\xa9\x82\xb9\xb1\xa9\x42\x51\x2a"
42 "\x57\xb8\x42\x1e\x57\x21\x92\xa0\x89\x9e\xb1\xa5\x09\x92\x84\x9e"
43 "\x85\x81\x81\x25\xd8\x14\x24\x26\xd0\x18\x43\x05\x10\x0c\x72\x57"
44 "\x80\x30\x8a\xd2\xb9\xf4\xdd\x0d\x14\xd2\x8b\xc1\x46\x99\x59\x1a"
45 "\x2b\x58\x1a\x9a\x83\x8c\x49\xe3\x0a\x04\x42\x00\x37\x4c\x1b\x42"
46 ),
47 };
48
49 for (size_t i = 0; i < FX_ArraySize(flate_encode_cases); ++i) {
50 FlateEncodeCase* ptr = &flate_encode_cases[i];
51 unsigned char* result;
52 unsigned int result_size;
53 FlateEncode(ptr->input, ptr->input_size, result, result_size);
54 ASSERT_TRUE(result);
55 EXPECT_EQ(std::string((const char*)ptr->expected, ptr->expected_size),
56 std::string((const char*)result, result_size))
57 << " for case " << i;
58 FX_Free(result);
59 }
60 }
61
TEST_F(FPDFParserDecodeEmbeddertest,FlateDecode)62 TEST_F(FPDFParserDecodeEmbeddertest, FlateDecode) {
63 struct FlateDecodeCase {
64 const unsigned char* input;
65 unsigned int input_size;
66 const unsigned char* expected;
67 unsigned int expected_size;
68 } flate_decode_cases[] = {
69 TEST_CASE("", ""),
70 TEST_CASE("preposterous nonsense", ""),
71 TEST_CASE("\x78\x9c\x03\x00\x00\x00\x00\x01", ""),
72 TEST_CASE("\x78\x9c\x53\x00\x00\x00\x21\x00\x21", " "),
73 TEST_CASE("\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97", "123"),
74 TEST_CASE("\x78\x9c\x63\xf8\x0f\x00\x01\x01\x01\x00", "\x00\xff"),
75 TEST_CASE("\x78\x9c\x33\x54\x30\x00\x42\x5d\x43\x05\x23\x4b\x05\x73\x33\x63"
76 "\x85\xe4\x5c\x2e\x90\x80\xa9\xa9\xa9\x82\xb9\xb1\xa9\x42\x51\x2a"
77 "\x57\xb8\x42\x1e\x57\x21\x92\xa0\x89\x9e\xb1\xa5\x09\x92\x84\x9e"
78 "\x85\x81\x81\x25\xd8\x14\x24\x26\xd0\x18\x43\x05\x10\x0c\x72\x57"
79 "\x80\x30\x8a\xd2\xb9\xf4\xdd\x0d\x14\xd2\x8b\xc1\x46\x99\x59\x1a"
80 "\x2b\x58\x1a\x9a\x83\x8c\x49\xe3\x0a\x04\x42\x00\x37\x4c\x1b\x42"
81 ,
82 "1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n"
83 "W n\nq\n0.8009 0 0 0.8009 0 0 cm\n1 1 1 RG 1 1 1 rg\n/G0 gs\n"
84 "0 0 693 917 re\nf\nQ\nQ\n"
85 ),
86 };
87
88 for (size_t i = 0; i < FX_ArraySize(flate_decode_cases); ++i) {
89 FlateDecodeCase* ptr = &flate_decode_cases[i];
90 unsigned char* result;
91 unsigned int result_size;
92 FlateDecode(ptr->input, ptr->input_size, result, result_size);
93 ASSERT_TRUE(result);
94 EXPECT_EQ(std::string((const char*)ptr->expected, ptr->expected_size),
95 std::string((const char*)result, result_size))
96 << " for case " << i;
97 FX_Free(result);
98 }
99 }
100
101
102 #undef TEST_CASE
103