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 "../../../../public/fpdfview.h" | 8 #include "../../../../public/fpdfview.h" |
9 #include "../../../../testing/fx_string_testhelpers.h" | 9 #include "../../../../testing/fx_string_testhelpers.h" |
10 #include "../../../include/fpdfapi/fpdf_parser.h" | 10 #include "../../../include/fpdfapi/fpdf_parser.h" |
11 #include "../../../include/fxcrt/fx_basic.h" | 11 #include "../../../include/fxcrt/fx_basic.h" |
12 #include "../../../testing/embedder_test.h" | 12 #include "../../../testing/embedder_test.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 class FPDFParserDecodeEmbeddertest : public EmbedderTest { | 15 class FPDFParserDecodeEmbeddertest : public EmbedderTest {}; |
16 }; | |
17 | 16 |
18 // 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 |
19 // external validation of the FlateEncode/FlateDecode test cases. | 18 // external validation of the FlateEncode/FlateDecode test cases. |
20 | 19 |
21 #define TEST_CASE(input_literal, expected_literal) \ | 20 #define TEST_CASE(input_literal, expected_literal) \ |
22 { (const unsigned char*)input_literal, sizeof(input_literal) - 1, \ | 21 { \ |
23 (const unsigned char*)expected_literal, sizeof(expected_literal) - 1 } | 22 (const unsigned char*) input_literal, sizeof(input_literal) - 1, \ |
| 23 (const unsigned char*)expected_literal, sizeof(expected_literal) - 1 \ |
| 24 } |
24 | 25 |
25 TEST_F(FPDFParserDecodeEmbeddertest, FlateEncode) { | 26 TEST_F(FPDFParserDecodeEmbeddertest, FlateEncode) { |
26 struct FlateEncodeCase { | 27 struct FlateEncodeCase { |
27 const unsigned char* input; | 28 const unsigned char* input; |
28 unsigned int input_size; | 29 unsigned int input_size; |
29 const unsigned char* expected; | 30 const unsigned char* expected; |
30 unsigned int expected_size; | 31 unsigned int expected_size; |
31 } flate_encode_cases[] = { | 32 } flate_encode_cases[] = { |
32 TEST_CASE("", "\x78\x9c\x03\x00\x00\x00\x00\x01"), | 33 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(" ", "\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("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("\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 TEST_CASE( |
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 "1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n" |
38 "0 0 693 917 re\nf\nQ\nQ\n" | 39 "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 , | 40 "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" | 41 "\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 "\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 "\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 "\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 "\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 "\x2b\x58\x1a\x9a\x83\x8c\x49\xe3\x0a\x04\x42\x00\x37\x4c\x1b\x42"), |
46 ), | |
47 }; | 47 }; |
48 | 48 |
49 for (size_t i = 0; i < FX_ArraySize(flate_encode_cases); ++i) { | 49 for (size_t i = 0; i < FX_ArraySize(flate_encode_cases); ++i) { |
50 FlateEncodeCase* ptr = &flate_encode_cases[i]; | 50 FlateEncodeCase* ptr = &flate_encode_cases[i]; |
51 unsigned char* result; | 51 unsigned char* result; |
52 unsigned int result_size; | 52 unsigned int result_size; |
53 FlateEncode(ptr->input, ptr->input_size, result, result_size); | 53 FlateEncode(ptr->input, ptr->input_size, result, result_size); |
54 ASSERT_TRUE(result); | 54 ASSERT_TRUE(result); |
55 EXPECT_EQ(std::string((const char*)ptr->expected, ptr->expected_size), | 55 EXPECT_EQ(std::string((const char*)ptr->expected, ptr->expected_size), |
56 std::string((const char*)result, result_size)) | 56 std::string((const char*)result, result_size)) |
57 << " for case " << i; | 57 << " for case " << i; |
58 FX_Free(result); | 58 FX_Free(result); |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 TEST_F(FPDFParserDecodeEmbeddertest, FlateDecode) { | 62 TEST_F(FPDFParserDecodeEmbeddertest, FlateDecode) { |
63 struct FlateDecodeCase { | 63 struct FlateDecodeCase { |
64 const unsigned char* input; | 64 const unsigned char* input; |
65 unsigned int input_size; | 65 unsigned int input_size; |
66 const unsigned char* expected; | 66 const unsigned char* expected; |
67 unsigned int expected_size; | 67 unsigned int expected_size; |
68 } flate_decode_cases[] = { | 68 } flate_decode_cases[] = { |
69 TEST_CASE("", ""), | 69 TEST_CASE("", ""), TEST_CASE("preposterous nonsense", ""), |
70 TEST_CASE("preposterous nonsense", ""), | 70 TEST_CASE("\x78\x9c\x03\x00\x00\x00\x00\x01", ""), |
71 TEST_CASE("\x78\x9c\x03\x00\x00\x00\x00\x01", ""), | 71 TEST_CASE("\x78\x9c\x53\x00\x00\x00\x21\x00\x21", " "), |
72 TEST_CASE("\x78\x9c\x53\x00\x00\x00\x21\x00\x21", " "), | 72 TEST_CASE("\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97", "123"), |
73 TEST_CASE("\x78\x9c\x33\x34\x32\x06\x00\01\x2d\x00\x97", "123"), | 73 TEST_CASE("\x78\x9c\x63\xf8\x0f\x00\x01\x01\x01\x00", "\x00\xff"), |
74 TEST_CASE("\x78\x9c\x63\xf8\x0f\x00\x01\x01\x01\x00", "\x00\xff"), | 74 TEST_CASE( |
75 TEST_CASE("\x78\x9c\x33\x54\x30\x00\x42\x5d\x43\x05\x23\x4b\x05\x73\x33\x63" | 75 "\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" | 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" | 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" | 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" | 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" | 80 "\x2b\x58\x1a\x9a\x83\x8c\x49\xe3\x0a\x04\x42\x00\x37\x4c\x1b\x42", |
81 , | 81 "1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n" |
82 "1 0 0 -1 29 763 cm\n0 0 555 735 re\nW n\nq\n0 0 555 734.394 re\n" | 82 "W n\nq\n0.8009 0 0 0.8009 0 0 cm\n1 1 1 RG 1 1 1 rg\n/G0 gs\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" | 83 "0 0 693 917 re\nf\nQ\nQ\n"), |
84 "0 0 693 917 re\nf\nQ\nQ\n" | |
85 ), | |
86 }; | 84 }; |
87 | 85 |
88 for (size_t i = 0; i < FX_ArraySize(flate_decode_cases); ++i) { | 86 for (size_t i = 0; i < FX_ArraySize(flate_decode_cases); ++i) { |
89 FlateDecodeCase* ptr = &flate_decode_cases[i]; | 87 FlateDecodeCase* ptr = &flate_decode_cases[i]; |
90 unsigned char* result; | 88 unsigned char* result; |
91 unsigned int result_size; | 89 unsigned int result_size; |
92 FlateDecode(ptr->input, ptr->input_size, result, result_size); | 90 FlateDecode(ptr->input, ptr->input_size, result, result_size); |
93 ASSERT_TRUE(result); | 91 ASSERT_TRUE(result); |
94 EXPECT_EQ(std::string((const char*)ptr->expected, ptr->expected_size), | 92 EXPECT_EQ(std::string((const char*)ptr->expected, ptr->expected_size), |
95 std::string((const char*)result, result_size)) | 93 std::string((const char*)result, result_size)) |
96 << " for case " << i; | 94 << " for case " << i; |
97 FX_Free(result); | 95 FX_Free(result); |
98 } | 96 } |
99 } | 97 } |
100 | 98 |
101 | |
102 #undef TEST_CASE | 99 #undef TEST_CASE |
OLD | NEW |