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