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