1 // Copyright 2016 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 <limits>
6 #include <string>
7
8 #include "core/fpdfapi/parser/cpdf_object.h"
9 #include "core/fpdfapi/parser/cpdf_parser.h"
10 #include "core/fpdfapi/parser/cpdf_syntax_parser.h"
11 #include "core/fxcrt/cfx_memorystream.h"
12 #include "core/fxcrt/fx_extension.h"
13 #include "core/fxcrt/fx_stream.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/utils/path_service.h"
16
TEST(cpdf_syntax_parser,ReadHexString)17 TEST(cpdf_syntax_parser, ReadHexString) {
18 {
19 // Empty string.
20 uint8_t data[] = "";
21 CPDF_SyntaxParser parser;
22 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 0, false), 0);
23 EXPECT_EQ("", parser.ReadHexString());
24 EXPECT_EQ(0, parser.GetPos());
25 }
26
27 {
28 // Blank string.
29 uint8_t data[] = " ";
30 CPDF_SyntaxParser parser;
31 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 2, false), 0);
32 EXPECT_EQ("", parser.ReadHexString());
33 EXPECT_EQ(2, parser.GetPos());
34 }
35
36 {
37 // Skips unknown characters.
38 uint8_t data[] = "z12b";
39 CPDF_SyntaxParser parser;
40 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 4, false), 0);
41 EXPECT_EQ("\x12\xb0", parser.ReadHexString());
42 EXPECT_EQ(4, parser.GetPos());
43 }
44
45 {
46 // Skips unknown characters.
47 uint8_t data[] = "*<&*#$^&@1";
48 CPDF_SyntaxParser parser;
49 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 10, false), 0);
50 EXPECT_EQ("\x10", parser.ReadHexString());
51 EXPECT_EQ(10, parser.GetPos());
52 }
53
54 {
55 // Skips unknown characters.
56 uint8_t data[] = "\x80zab";
57 CPDF_SyntaxParser parser;
58 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 4, false), 0);
59 EXPECT_EQ("\xab", parser.ReadHexString());
60 EXPECT_EQ(4, parser.GetPos());
61 }
62
63 {
64 // Skips unknown characters.
65 uint8_t data[] = "\xffzab";
66 CPDF_SyntaxParser parser;
67 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 4, false), 0);
68 EXPECT_EQ("\xab", parser.ReadHexString());
69 EXPECT_EQ(4, parser.GetPos());
70 }
71
72 {
73 // Regular conversion.
74 uint8_t data[] = "1A2b>abcd";
75 CPDF_SyntaxParser parser;
76 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 9, false), 0);
77 EXPECT_EQ("\x1a\x2b", parser.ReadHexString());
78 EXPECT_EQ(5, parser.GetPos());
79 }
80
81 {
82 // Position out of bounds.
83 uint8_t data[] = "12ab>";
84 CPDF_SyntaxParser parser;
85 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 5, false), 0);
86 parser.SetPos(5);
87 EXPECT_EQ("", parser.ReadHexString());
88
89 parser.SetPos(6);
90 EXPECT_EQ("", parser.ReadHexString());
91
92 parser.SetPos(-1);
93 EXPECT_EQ("", parser.ReadHexString());
94
95 parser.SetPos(std::numeric_limits<FX_FILESIZE>::max());
96 EXPECT_EQ("", parser.ReadHexString());
97
98 // Check string still parses when set to 0.
99 parser.SetPos(0);
100 EXPECT_EQ("\x12\xab", parser.ReadHexString());
101 }
102
103 {
104 // Missing ending >.
105 uint8_t data[] = "1A2b";
106 CPDF_SyntaxParser parser;
107 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 4, false), 0);
108 EXPECT_EQ("\x1a\x2b", parser.ReadHexString());
109 EXPECT_EQ(4, parser.GetPos());
110 }
111
112 {
113 // Missing ending >.
114 uint8_t data[] = "12abz";
115 CPDF_SyntaxParser parser;
116 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 5, false), 0);
117 EXPECT_EQ("\x12\xab", parser.ReadHexString());
118 EXPECT_EQ(5, parser.GetPos());
119 }
120
121 {
122 // Uneven number of bytes.
123 uint8_t data[] = "1A2>asdf";
124 CPDF_SyntaxParser parser;
125 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 8, false), 0);
126 EXPECT_EQ("\x1a\x20", parser.ReadHexString());
127 EXPECT_EQ(4, parser.GetPos());
128 }
129
130 {
131 // Uneven number of bytes.
132 uint8_t data[] = "1A2zasdf";
133 CPDF_SyntaxParser parser;
134 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 8, false), 0);
135 EXPECT_EQ("\x1a\x2a\xdf", parser.ReadHexString());
136 EXPECT_EQ(8, parser.GetPos());
137 }
138
139 {
140 // Just ending character.
141 uint8_t data[] = ">";
142 CPDF_SyntaxParser parser;
143 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 1, false), 0);
144 EXPECT_EQ("", parser.ReadHexString());
145 EXPECT_EQ(1, parser.GetPos());
146 }
147 }
148
TEST(cpdf_syntax_parser,GetInvalidReference)149 TEST(cpdf_syntax_parser, GetInvalidReference) {
150 CPDF_SyntaxParser parser;
151 // Data with a reference with number CPDF_Object::kInvalidObjNum
152 uint8_t data[] = "4294967295 0 R";
153 parser.InitParser(pdfium::MakeRetain<CFX_MemoryStream>(data, 14, false), 0);
154 std::unique_ptr<CPDF_Object> ref = parser.GetObjectBody(nullptr);
155 EXPECT_FALSE(ref);
156 }
157
TEST(cpdf_syntax_parser,PeekNextWord)158 TEST(cpdf_syntax_parser, PeekNextWord) {
159 CPDF_SyntaxParser parser;
160 uint8_t data[] = " WORD ";
161 parser.InitParser(
162 pdfium::MakeRetain<CFX_MemoryStream>(data, sizeof(data), false), 0);
163 EXPECT_EQ("WORD", parser.PeekNextWord(nullptr));
164 EXPECT_EQ("WORD", parser.GetNextWord(nullptr));
165 }
166