• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "xfa/fde/xml/fde_xml_imp.h"
6 
7 #include <memory>
8 
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "xfa/fgas/crt/fgas_stream.h"
11 
TEST(CFDE_XMLSyntaxParser,CData)12 TEST(CFDE_XMLSyntaxParser, CData) {
13   const FX_WCHAR* input =
14       L"<script contentType=\"application/x-javascript\">\n"
15       L"  <![CDATA[\n"
16       L"    if (a[1] < 3)\n"
17       L"      app.alert(\"Tclams\");\n"
18       L"  ]]>\n"
19       L"</script>";
20 
21   const FX_WCHAR* cdata =
22       L"\n"
23       L"    if (a[1] < 3)\n"
24       L"      app.alert(\"Tclams\");\n"
25       L"  ";
26 
27   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
28   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
29   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
30       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
31   CFDE_XMLSyntaxParser parser;
32   parser.Init(stream, 256);
33   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
34   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
35 
36   CFX_WideString data;
37   parser.GetTagName(data);
38   EXPECT_EQ(L"script", data);
39 
40   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
41   parser.GetAttributeName(data);
42   EXPECT_EQ(L"contentType", data);
43   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
44   parser.GetAttributeValue(data);
45   EXPECT_EQ(L"application/x-javascript", data);
46 
47   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
48   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
49   parser.GetTextData(data);
50   EXPECT_EQ(L"\n  ", data);
51 
52   EXPECT_EQ(FDE_XmlSyntaxResult::CData, parser.DoSyntaxParse());
53   parser.GetTextData(data);
54   EXPECT_EQ(cdata, data);
55 
56   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
57   parser.GetTextData(data);
58   EXPECT_EQ(L"\n", data);
59 
60   EXPECT_EQ(FDE_XmlSyntaxResult::ElementClose, parser.DoSyntaxParse());
61   parser.GetTagName(data);
62   EXPECT_EQ(L"script", data);
63 
64   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
65 }
66 
TEST(CFDE_XMLSyntaxParser,CDataWithInnerScript)67 TEST(CFDE_XMLSyntaxParser, CDataWithInnerScript) {
68   const FX_WCHAR* input =
69       L"<script contentType=\"application/x-javascript\">\n"
70       L"  <![CDATA[\n"
71       L"    if (a[1] < 3)\n"
72       L"      app.alert(\"Tclams\");\n"
73       L"    </script>\n"
74       L"  ]]>\n"
75       L"</script>";
76 
77   const FX_WCHAR* cdata =
78       L"\n"
79       L"    if (a[1] < 3)\n"
80       L"      app.alert(\"Tclams\");\n"
81       L"    </script>\n"
82       L"  ";
83 
84   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
85   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
86   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
87       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
88   CFDE_XMLSyntaxParser parser;
89   parser.Init(stream, 256);
90   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
91   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
92 
93   CFX_WideString data;
94   parser.GetTagName(data);
95   EXPECT_EQ(L"script", data);
96 
97   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
98   parser.GetAttributeName(data);
99   EXPECT_EQ(L"contentType", data);
100   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
101   parser.GetAttributeValue(data);
102   EXPECT_EQ(L"application/x-javascript", data);
103 
104   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
105   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
106   parser.GetTextData(data);
107   EXPECT_EQ(L"\n  ", data);
108 
109   EXPECT_EQ(FDE_XmlSyntaxResult::CData, parser.DoSyntaxParse());
110   parser.GetTextData(data);
111   EXPECT_EQ(cdata, data);
112 
113   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
114   parser.GetTextData(data);
115   EXPECT_EQ(L"\n", data);
116 
117   EXPECT_EQ(FDE_XmlSyntaxResult::ElementClose, parser.DoSyntaxParse());
118   parser.GetTagName(data);
119   EXPECT_EQ(L"script", data);
120 
121   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
122 }
123 
TEST(CFDE_XMLSyntaxParser,ArrowBangArrow)124 TEST(CFDE_XMLSyntaxParser, ArrowBangArrow) {
125   const FX_WCHAR* input =
126       L"<script contentType=\"application/x-javascript\">\n"
127       L"  <!>\n"
128       L"</script>";
129 
130   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
131   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
132   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
133       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
134   CFDE_XMLSyntaxParser parser;
135   parser.Init(stream, 256);
136   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
137   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
138 
139   CFX_WideString data;
140   parser.GetTagName(data);
141   EXPECT_EQ(L"script", data);
142 
143   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
144   parser.GetAttributeName(data);
145   EXPECT_EQ(L"contentType", data);
146   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
147   parser.GetAttributeValue(data);
148   EXPECT_EQ(L"application/x-javascript", data);
149 
150   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
151   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
152   parser.GetTextData(data);
153   EXPECT_EQ(L"\n  ", data);
154 
155   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
156   parser.GetTextData(data);
157   EXPECT_EQ(L"\n", data);
158 
159   EXPECT_EQ(FDE_XmlSyntaxResult::ElementClose, parser.DoSyntaxParse());
160   parser.GetTagName(data);
161   EXPECT_EQ(L"script", data);
162 
163   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
164 }
165 
TEST(CFDE_XMLSyntaxParser,ArrowBangBracketArrow)166 TEST(CFDE_XMLSyntaxParser, ArrowBangBracketArrow) {
167   const FX_WCHAR* input =
168       L"<script contentType=\"application/x-javascript\">\n"
169       L"  <![>\n"
170       L"</script>";
171 
172   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
173   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
174   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
175       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
176   CFDE_XMLSyntaxParser parser;
177   parser.Init(stream, 256);
178   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
179   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
180 
181   CFX_WideString data;
182   parser.GetTagName(data);
183   EXPECT_EQ(L"script", data);
184 
185   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
186   parser.GetAttributeName(data);
187   EXPECT_EQ(L"contentType", data);
188   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
189   parser.GetAttributeValue(data);
190   EXPECT_EQ(L"application/x-javascript", data);
191 
192   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
193   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
194   parser.GetTextData(data);
195   EXPECT_EQ(L"\n  ", data);
196 
197   // Parser walks to end of input.
198 
199   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
200 }
201 
TEST(CFDE_XMLSyntaxParser,IncompleteCData)202 TEST(CFDE_XMLSyntaxParser, IncompleteCData) {
203   const FX_WCHAR* input =
204       L"<script contentType=\"application/x-javascript\">\n"
205       L"  <![CDATA>\n"
206       L"</script>";
207 
208   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
209   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
210   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
211       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
212   CFDE_XMLSyntaxParser parser;
213   parser.Init(stream, 256);
214   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
215   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
216 
217   CFX_WideString data;
218   parser.GetTagName(data);
219   EXPECT_EQ(L"script", data);
220 
221   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
222   parser.GetAttributeName(data);
223   EXPECT_EQ(L"contentType", data);
224   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
225   parser.GetAttributeValue(data);
226   EXPECT_EQ(L"application/x-javascript", data);
227 
228   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
229   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
230   parser.GetTextData(data);
231   EXPECT_EQ(L"\n  ", data);
232 
233   // Parser walks to end of input.
234 
235   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
236 }
237 
TEST(CFDE_XMLSyntaxParser,UnClosedCData)238 TEST(CFDE_XMLSyntaxParser, UnClosedCData) {
239   const FX_WCHAR* input =
240       L"<script contentType=\"application/x-javascript\">\n"
241       L"  <![CDATA[\n"
242       L"</script>";
243 
244   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
245   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
246   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
247       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
248   CFDE_XMLSyntaxParser parser;
249   parser.Init(stream, 256);
250   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
251   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
252 
253   CFX_WideString data;
254   parser.GetTagName(data);
255   EXPECT_EQ(L"script", data);
256 
257   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
258   parser.GetAttributeName(data);
259   EXPECT_EQ(L"contentType", data);
260   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
261   parser.GetAttributeValue(data);
262   EXPECT_EQ(L"application/x-javascript", data);
263 
264   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
265   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
266   parser.GetTextData(data);
267   EXPECT_EQ(L"\n  ", data);
268 
269   // Parser walks to end of input.
270 
271   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
272 }
273 
TEST(CFDE_XMLSyntaxParser,EmptyCData)274 TEST(CFDE_XMLSyntaxParser, EmptyCData) {
275   const FX_WCHAR* input =
276       L"<script contentType=\"application/x-javascript\">\n"
277       L"  <![CDATA[]]>\n"
278       L"</script>";
279 
280   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
281   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
282   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
283       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
284   CFDE_XMLSyntaxParser parser;
285   parser.Init(stream, 256);
286   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
287   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
288 
289   CFX_WideString data;
290   parser.GetTagName(data);
291   EXPECT_EQ(L"script", data);
292 
293   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
294   parser.GetAttributeName(data);
295   EXPECT_EQ(L"contentType", data);
296   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
297   parser.GetAttributeValue(data);
298   EXPECT_EQ(L"application/x-javascript", data);
299 
300   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
301   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
302   parser.GetTextData(data);
303   EXPECT_EQ(L"\n  ", data);
304 
305   EXPECT_EQ(FDE_XmlSyntaxResult::CData, parser.DoSyntaxParse());
306   parser.GetTextData(data);
307   EXPECT_EQ(L"", data);
308 
309   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
310   parser.GetTextData(data);
311   EXPECT_EQ(L"\n", data);
312 
313   EXPECT_EQ(FDE_XmlSyntaxResult::ElementClose, parser.DoSyntaxParse());
314   parser.GetTagName(data);
315   EXPECT_EQ(L"script", data);
316 
317   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
318 }
319 
TEST(CFDE_XMLSyntaxParser,Comment)320 TEST(CFDE_XMLSyntaxParser, Comment) {
321   const FX_WCHAR* input =
322       L"<script contentType=\"application/x-javascript\">\n"
323       L"  <!-- A Comment -->\n"
324       L"</script>";
325 
326   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
327   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
328   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
329       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
330   CFDE_XMLSyntaxParser parser;
331   parser.Init(stream, 256);
332   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
333   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
334 
335   CFX_WideString data;
336   parser.GetTagName(data);
337   EXPECT_EQ(L"script", data);
338 
339   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
340   parser.GetAttributeName(data);
341   EXPECT_EQ(L"contentType", data);
342   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
343   parser.GetAttributeValue(data);
344   EXPECT_EQ(L"application/x-javascript", data);
345 
346   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
347   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
348   parser.GetTextData(data);
349   EXPECT_EQ(L"\n  ", data);
350 
351   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
352   parser.GetTextData(data);
353   EXPECT_EQ(L"\n", data);
354 
355   EXPECT_EQ(FDE_XmlSyntaxResult::ElementClose, parser.DoSyntaxParse());
356   parser.GetTagName(data);
357   EXPECT_EQ(L"script", data);
358 
359   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
360 }
361 
TEST(CFDE_XMLSyntaxParser,IncorrectCommentStart)362 TEST(CFDE_XMLSyntaxParser, IncorrectCommentStart) {
363   const FX_WCHAR* input =
364       L"<script contentType=\"application/x-javascript\">\n"
365       L"  <!- A Comment -->\n"
366       L"</script>";
367 
368   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
369   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
370   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
371       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
372   CFDE_XMLSyntaxParser parser;
373   parser.Init(stream, 256);
374   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
375   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
376 
377   CFX_WideString data;
378   parser.GetTagName(data);
379   EXPECT_EQ(L"script", data);
380 
381   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
382   parser.GetAttributeName(data);
383   EXPECT_EQ(L"contentType", data);
384   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
385   parser.GetAttributeValue(data);
386   EXPECT_EQ(L"application/x-javascript", data);
387 
388   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
389   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
390   parser.GetTextData(data);
391   EXPECT_EQ(L"\n  ", data);
392 
393   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
394   parser.GetTextData(data);
395   EXPECT_EQ(L"\n", data);
396 
397   EXPECT_EQ(FDE_XmlSyntaxResult::ElementClose, parser.DoSyntaxParse());
398   parser.GetTagName(data);
399   EXPECT_EQ(L"script", data);
400 
401   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
402 }
403 
TEST(CFDE_XMLSyntaxParser,CommentEmpty)404 TEST(CFDE_XMLSyntaxParser, CommentEmpty) {
405   const FX_WCHAR* input =
406       L"<script contentType=\"application/x-javascript\">\n"
407       L"  <!---->\n"
408       L"</script>";
409 
410   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
411   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
412   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
413       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
414   CFDE_XMLSyntaxParser parser;
415   parser.Init(stream, 256);
416   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
417   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
418 
419   CFX_WideString data;
420   parser.GetTagName(data);
421   EXPECT_EQ(L"script", data);
422 
423   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
424   parser.GetAttributeName(data);
425   EXPECT_EQ(L"contentType", data);
426   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
427   parser.GetAttributeValue(data);
428   EXPECT_EQ(L"application/x-javascript", data);
429 
430   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
431   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
432   parser.GetTextData(data);
433   EXPECT_EQ(L"\n  ", data);
434 
435   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
436   parser.GetTextData(data);
437   EXPECT_EQ(L"\n", data);
438 
439   EXPECT_EQ(FDE_XmlSyntaxResult::ElementClose, parser.DoSyntaxParse());
440   parser.GetTagName(data);
441   EXPECT_EQ(L"script", data);
442 
443   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
444 }
445 
TEST(CFDE_XMLSyntaxParser,CommentThreeDash)446 TEST(CFDE_XMLSyntaxParser, CommentThreeDash) {
447   const FX_WCHAR* input =
448       L"<script contentType=\"application/x-javascript\">\n"
449       L"  <!--->\n"
450       L"</script>";
451 
452   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
453   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
454   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
455       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
456   CFDE_XMLSyntaxParser parser;
457   parser.Init(stream, 256);
458   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
459   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
460 
461   CFX_WideString data;
462   parser.GetTagName(data);
463   EXPECT_EQ(L"script", data);
464 
465   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
466   parser.GetAttributeName(data);
467   EXPECT_EQ(L"contentType", data);
468   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
469   parser.GetAttributeValue(data);
470   EXPECT_EQ(L"application/x-javascript", data);
471 
472   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
473   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
474   parser.GetTextData(data);
475   EXPECT_EQ(L"\n  ", data);
476 
477   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
478 }
479 
TEST(CFDE_XMLSyntaxParser,CommentTwoDash)480 TEST(CFDE_XMLSyntaxParser, CommentTwoDash) {
481   const FX_WCHAR* input =
482       L"<script contentType=\"application/x-javascript\">\n"
483       L"  <!-->\n"
484       L"</script>";
485 
486   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
487   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
488   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
489       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
490   CFDE_XMLSyntaxParser parser;
491   parser.Init(stream, 256);
492   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
493   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
494 
495   CFX_WideString data;
496   parser.GetTagName(data);
497   EXPECT_EQ(L"script", data);
498 
499   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
500   parser.GetAttributeName(data);
501   EXPECT_EQ(L"contentType", data);
502   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
503   parser.GetAttributeValue(data);
504   EXPECT_EQ(L"application/x-javascript", data);
505 
506   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
507   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
508   parser.GetTextData(data);
509   EXPECT_EQ(L"\n  ", data);
510 
511   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
512 }
513 
TEST(CFDE_XMLSyntaxParser,Entities)514 TEST(CFDE_XMLSyntaxParser, Entities) {
515   const FX_WCHAR* input =
516       L"<script contentType=\"application/x-javascript\">"
517       L"&#66;"
518       L"&#x54;"
519       L"&#x00000000000000000048;"
520       L"&#x0000000000000000AB48;"
521       L"&#x0000000000000000000;"
522       L"</script>";
523 
524   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
525   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
526   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
527       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
528   CFDE_XMLSyntaxParser parser;
529   parser.Init(stream, 256);
530   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
531   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
532 
533   CFX_WideString data;
534   parser.GetTagName(data);
535   EXPECT_EQ(L"script", data);
536 
537   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
538   parser.GetAttributeName(data);
539   EXPECT_EQ(L"contentType", data);
540   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
541   parser.GetAttributeValue(data);
542   EXPECT_EQ(L"application/x-javascript", data);
543 
544   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
545   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
546   parser.GetTextData(data);
547   EXPECT_EQ(L"BTH\xab48", data);
548 
549   EXPECT_EQ(FDE_XmlSyntaxResult::ElementClose, parser.DoSyntaxParse());
550   parser.GetTagName(data);
551   EXPECT_EQ(L"script", data);
552 
553   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
554 }
555 
TEST(CFDE_XMLSyntaxParser,EntityOverflowHex)556 TEST(CFDE_XMLSyntaxParser, EntityOverflowHex) {
557   const FX_WCHAR* input =
558       L"<script contentType=\"application/x-javascript\">"
559       L"&#xaDBDFFFFF;"
560       L"&#xafffffffffffffffffffffffffffffffff;"
561       L"</script>";
562 
563   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
564   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
565   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
566       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
567   CFDE_XMLSyntaxParser parser;
568   parser.Init(stream, 256);
569   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
570   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
571 
572   CFX_WideString data;
573   parser.GetTagName(data);
574   EXPECT_EQ(L"script", data);
575 
576   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
577   parser.GetAttributeName(data);
578   EXPECT_EQ(L"contentType", data);
579   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
580   parser.GetAttributeValue(data);
581   EXPECT_EQ(L"application/x-javascript", data);
582 
583   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
584   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
585   parser.GetTextData(data);
586   EXPECT_EQ(L"  ", data);
587 
588   EXPECT_EQ(FDE_XmlSyntaxResult::ElementClose, parser.DoSyntaxParse());
589   parser.GetTagName(data);
590   EXPECT_EQ(L"script", data);
591 
592   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
593 }
594 
TEST(CFDE_XMLSyntaxParser,EntityOverflowDecimal)595 TEST(CFDE_XMLSyntaxParser, EntityOverflowDecimal) {
596   const FX_WCHAR* input =
597       L"<script contentType=\"application/x-javascript\">"
598       L"&#2914910205;"
599       L"&#29149102052342342134521341234512351234213452315;"
600       L"</script>";
601 
602   // We * sizeof(FX_WCHAR) because we pass in the uint8_t, not the FX_WCHAR.
603   size_t len = FXSYS_wcslen(input) * sizeof(FX_WCHAR);
604   CFX_RetainPtr<IFGAS_Stream> stream = IFGAS_Stream::CreateStream(
605       reinterpret_cast<uint8_t*>(const_cast<FX_WCHAR*>(input)), len, 0);
606   CFDE_XMLSyntaxParser parser;
607   parser.Init(stream, 256);
608   EXPECT_EQ(FDE_XmlSyntaxResult::ElementOpen, parser.DoSyntaxParse());
609   EXPECT_EQ(FDE_XmlSyntaxResult::TagName, parser.DoSyntaxParse());
610 
611   CFX_WideString data;
612   parser.GetTagName(data);
613   EXPECT_EQ(L"script", data);
614 
615   EXPECT_EQ(FDE_XmlSyntaxResult::AttriName, parser.DoSyntaxParse());
616   parser.GetAttributeName(data);
617   EXPECT_EQ(L"contentType", data);
618   EXPECT_EQ(FDE_XmlSyntaxResult::AttriValue, parser.DoSyntaxParse());
619   parser.GetAttributeValue(data);
620   EXPECT_EQ(L"application/x-javascript", data);
621 
622   EXPECT_EQ(FDE_XmlSyntaxResult::ElementBreak, parser.DoSyntaxParse());
623   EXPECT_EQ(FDE_XmlSyntaxResult::Text, parser.DoSyntaxParse());
624   parser.GetTextData(data);
625   EXPECT_EQ(L"  ", data);
626 
627   EXPECT_EQ(FDE_XmlSyntaxResult::ElementClose, parser.DoSyntaxParse());
628   parser.GetTagName(data);
629   EXPECT_EQ(L"script", data);
630 
631   EXPECT_EQ(FDE_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse());
632 }
633