summaryrefslogtreecommitdiff
path: root/core/fxcrt/xml/cfx_saxreader_unittest.cpp
blob: 13d06325a79928950595a5565797da311609e6be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2017 The PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/fxcrt/xml/cfx_saxreader.h"
#include "core/fxcrt/cfx_memorystream.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/test_support.h"

using testing::_;
using testing::Eq;
using testing::Return;

namespace {

class MockHandler : public CFX_SAXReader::HandlerIface {
 public:
  MOCK_METHOD3(OnTagEnter,
               CFX_SAXContext*(const ByteStringView& bsTagName,
                               CFX_SAXItem::Type eType,
                               uint32_t dwStartPos));
  MOCK_METHOD3(OnTagAttribute,
               void(CFX_SAXContext* pTag,
                    const ByteStringView& bsAttri,
                    const ByteStringView& bsValue));
  MOCK_METHOD1(OnTagBreak, void(CFX_SAXContext* pTag));
  MOCK_METHOD4(OnTagData,
               void(CFX_SAXContext* pTag,
                    CFX_SAXItem::Type eType,
                    const ByteStringView& bsData,
                    uint32_t dwStartPos));
  MOCK_METHOD2(OnTagClose, void(CFX_SAXContext* pTag, uint32_t dwEndPos));
  MOCK_METHOD3(OnTagEnd,
               void(CFX_SAXContext* pTag,
                    const ByteStringView& bsTagName,
                    uint32_t dwEndPos));
  MOCK_METHOD4(OnTargetData,
               void(CFX_SAXContext* pTag,
                    CFX_SAXItem::Type eType,
                    const ByteStringView& bsData,
                    uint32_t dwStartPos));
};

}  // namespace

class CFX_SAXReaderTest : public testing::Test {
 public:
  void SetHandler(CFX_SAXReader::HandlerIface* handler) {
    reader_.SetHandler(handler);
  }

  bool StartParse(char* str) {
    return reader_.StartParse(
               pdfium::MakeRetain<CFX_MemoryStream>(
                   reinterpret_cast<uint8_t*>(str), strlen(str), false),
               0, static_cast<uint32_t>(-1),
               CFX_SaxParseMode_NotSkipSpace) >= 0;
  }

  int32_t ContinueParse() {
    int32_t ret;
    do {
      ret = reader_.ContinueParse();
    } while (ret >= 0 && ret < 100);
    return ret;
  }

 private:
  CFX_SAXReader reader_;
};

TEST_F(CFX_SAXReaderTest, Null) {
  char data[] = "";
  ASSERT_FALSE(StartParse(data));
}

TEST_F(CFX_SAXReaderTest, SimpleText) {
  MockHandler mock;
  SetHandler(&mock);

  char data[] = "clams";
  ASSERT_TRUE(StartParse(data));
  EXPECT_EQ(100, ContinueParse());
}

TEST_F(CFX_SAXReaderTest, SimpleTag) {
  MockHandler mock;
  EXPECT_CALL(mock, OnTagEnter(Eq("clams"), _, _));
  EXPECT_CALL(mock, OnTagBreak(_));
  SetHandler(&mock);

  char data[] = "<clams>";
  ASSERT_TRUE(StartParse(data));
  EXPECT_EQ(100, ContinueParse());
}

TEST_F(CFX_SAXReaderTest, AttributeTag) {
  MockHandler mock;
  EXPECT_CALL(mock, OnTagEnter(Eq("clams"), _, _));
  EXPECT_CALL(mock, OnTagAttribute(_, Eq("size"), Eq("small")));
  EXPECT_CALL(mock, OnTagAttribute(_, Eq("color"), Eq("red")));
  EXPECT_CALL(mock, OnTagBreak(_));
  SetHandler(&mock);

  char data[] = "<clams size='small' color='red'>";
  ASSERT_TRUE(StartParse(data));
  EXPECT_EQ(100, ContinueParse());
}

TEST_F(CFX_SAXReaderTest, AttributeEntityTag) {
  MockHandler mock;
  EXPECT_CALL(mock, OnTagEnter(Eq("clams"), _, _));
  EXPECT_CALL(mock, OnTagAttribute(_, Eq("predicate"), Eq("1 < 2")));
  EXPECT_CALL(mock, OnTagBreak(_));
  SetHandler(&mock);

  char data[] = "<clams predicate='1 &lt; 2'>";
  ASSERT_TRUE(StartParse(data));
  EXPECT_EQ(100, ContinueParse());
}

TEST_F(CFX_SAXReaderTest, TextWithinTag) {
  MockHandler mock;
  EXPECT_CALL(mock, OnTagEnter(Eq("b"), _, _));
  EXPECT_CALL(mock, OnTagBreak(_));
  EXPECT_CALL(mock, OnTagData(_, _, Eq("biff"), _));
  EXPECT_CALL(mock, OnTagEnd(_, Eq("b"), _));
  SetHandler(&mock);

  char data[] = "<b>biff</b>";
  ASSERT_TRUE(StartParse(data));
  EXPECT_EQ(100, ContinueParse());
}

TEST_F(CFX_SAXReaderTest, bug_711459) {
  char data[] =
      "&a<tag "
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      ">x;";
  ASSERT_TRUE(StartParse(data));
  EXPECT_EQ(100, ContinueParse());
}