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