1 // Copyright 2017 The 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 "core/fxcrt/xml/cfx_saxreader.h"
6 #include "core/fxcrt/cfx_memorystream.h"
7 #include "testing/gmock/include/gmock/gmock.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "testing/test_support.h"
10
11 using testing::_;
12 using testing::Eq;
13 using testing::Return;
14
15 namespace {
16
17 class MockHandler : public CFX_SAXReader::HandlerIface {
18 public:
19 MOCK_METHOD3(OnTagEnter,
20 CFX_SAXContext*(const ByteStringView& bsTagName,
21 CFX_SAXItem::Type eType,
22 uint32_t dwStartPos));
23 MOCK_METHOD3(OnTagAttribute,
24 void(CFX_SAXContext* pTag,
25 const ByteStringView& bsAttri,
26 const ByteStringView& bsValue));
27 MOCK_METHOD1(OnTagBreak, void(CFX_SAXContext* pTag));
28 MOCK_METHOD4(OnTagData,
29 void(CFX_SAXContext* pTag,
30 CFX_SAXItem::Type eType,
31 const ByteStringView& bsData,
32 uint32_t dwStartPos));
33 MOCK_METHOD2(OnTagClose, void(CFX_SAXContext* pTag, uint32_t dwEndPos));
34 MOCK_METHOD3(OnTagEnd,
35 void(CFX_SAXContext* pTag,
36 const ByteStringView& bsTagName,
37 uint32_t dwEndPos));
38 MOCK_METHOD4(OnTargetData,
39 void(CFX_SAXContext* pTag,
40 CFX_SAXItem::Type eType,
41 const ByteStringView& bsData,
42 uint32_t dwStartPos));
43 };
44
45 } // namespace
46
47 class CFX_SAXReaderTest : public testing::Test {
48 public:
SetHandler(CFX_SAXReader::HandlerIface * handler)49 void SetHandler(CFX_SAXReader::HandlerIface* handler) {
50 reader_.SetHandler(handler);
51 }
52
StartParse(char * str)53 bool StartParse(char* str) {
54 return reader_.StartParse(
55 pdfium::MakeRetain<CFX_MemoryStream>(
56 reinterpret_cast<uint8_t*>(str), strlen(str), false),
57 0, static_cast<uint32_t>(-1),
58 CFX_SaxParseMode_NotSkipSpace) >= 0;
59 }
60
ContinueParse()61 int32_t ContinueParse() {
62 int32_t ret;
63 do {
64 ret = reader_.ContinueParse();
65 } while (ret >= 0 && ret < 100);
66 return ret;
67 }
68
69 private:
70 CFX_SAXReader reader_;
71 };
72
TEST_F(CFX_SAXReaderTest,Null)73 TEST_F(CFX_SAXReaderTest, Null) {
74 char data[] = "";
75 ASSERT_FALSE(StartParse(data));
76 }
77
TEST_F(CFX_SAXReaderTest,SimpleText)78 TEST_F(CFX_SAXReaderTest, SimpleText) {
79 MockHandler mock;
80 SetHandler(&mock);
81
82 char data[] = "clams";
83 ASSERT_TRUE(StartParse(data));
84 EXPECT_EQ(100, ContinueParse());
85 }
86
TEST_F(CFX_SAXReaderTest,SimpleTag)87 TEST_F(CFX_SAXReaderTest, SimpleTag) {
88 MockHandler mock;
89 EXPECT_CALL(mock, OnTagEnter(Eq("clams"), _, _));
90 EXPECT_CALL(mock, OnTagBreak(_));
91 SetHandler(&mock);
92
93 char data[] = "<clams>";
94 ASSERT_TRUE(StartParse(data));
95 EXPECT_EQ(100, ContinueParse());
96 }
97
TEST_F(CFX_SAXReaderTest,AttributeTag)98 TEST_F(CFX_SAXReaderTest, AttributeTag) {
99 MockHandler mock;
100 EXPECT_CALL(mock, OnTagEnter(Eq("clams"), _, _));
101 EXPECT_CALL(mock, OnTagAttribute(_, Eq("size"), Eq("small")));
102 EXPECT_CALL(mock, OnTagAttribute(_, Eq("color"), Eq("red")));
103 EXPECT_CALL(mock, OnTagBreak(_));
104 SetHandler(&mock);
105
106 char data[] = "<clams size='small' color='red'>";
107 ASSERT_TRUE(StartParse(data));
108 EXPECT_EQ(100, ContinueParse());
109 }
110
TEST_F(CFX_SAXReaderTest,AttributeEntityTag)111 TEST_F(CFX_SAXReaderTest, AttributeEntityTag) {
112 MockHandler mock;
113 EXPECT_CALL(mock, OnTagEnter(Eq("clams"), _, _));
114 EXPECT_CALL(mock, OnTagAttribute(_, Eq("predicate"), Eq("1 < 2")));
115 EXPECT_CALL(mock, OnTagBreak(_));
116 SetHandler(&mock);
117
118 char data[] = "<clams predicate='1 < 2'>";
119 ASSERT_TRUE(StartParse(data));
120 EXPECT_EQ(100, ContinueParse());
121 }
122
TEST_F(CFX_SAXReaderTest,TextWithinTag)123 TEST_F(CFX_SAXReaderTest, TextWithinTag) {
124 MockHandler mock;
125 EXPECT_CALL(mock, OnTagEnter(Eq("b"), _, _));
126 EXPECT_CALL(mock, OnTagBreak(_));
127 EXPECT_CALL(mock, OnTagData(_, _, Eq("biff"), _));
128 EXPECT_CALL(mock, OnTagEnd(_, Eq("b"), _));
129 SetHandler(&mock);
130
131 char data[] = "<b>biff</b>";
132 ASSERT_TRUE(StartParse(data));
133 EXPECT_EQ(100, ContinueParse());
134 }
135
TEST_F(CFX_SAXReaderTest,bug_711459)136 TEST_F(CFX_SAXReaderTest, bug_711459) {
137 char data[] =
138 "&a<tag "
139 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
140 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
141 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
142 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
143 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
144 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
145 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
146 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
147 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
148 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
149 ">x;";
150 ASSERT_TRUE(StartParse(data));
151 EXPECT_EQ(100, ContinueParse());
152 }
153