Imported Upstream version 2.91.2
[platform/upstream/libxml++.git] / tests / saxparser_parse_double_free / main.cc
1 /* Copyright (C) 2015  The libxml++ development team
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #include <config.h>
19 #include <libxml++/libxml++.h>
20
21 #include <cstdlib>
22 #include <glibmm.h>
23 #include <stdexcept>
24
25 class OnCdataBlockTestParser : public xmlpp::SaxParser
26 {
27 protected:
28   void on_cdata_block(const Glib::ustring& text) override
29   {
30     g_assert_cmpstr(text.c_str(), ==, "some CDATA");
31     throw std::runtime_error("on_cdata_block runtime exception");
32   }
33 };
34
35 void test_on_cdata_block()
36 {
37   OnCdataBlockTestParser parser;
38   bool exceptionThrown = false;
39   try
40   {
41     parser.parse_memory("<root><![CDATA[some CDATA]]></root>");
42   }
43 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
44   catch(const std::runtime_error& e)
45 #else
46   catch(const xmlpp::exception& e)
47 #endif
48   {
49     exceptionThrown = true;
50     g_assert_cmpstr(e.what(), ==, "on_cdata_block runtime exception");
51   }
52   g_assert_true(exceptionThrown);
53 }
54
55
56 class OnCharactersTestParser : public xmlpp::SaxParser
57 {
58 protected:
59   void on_characters(const Glib::ustring& characters) override
60   {
61     g_assert_cmpstr(characters.c_str(), ==, "abc");
62     throw std::runtime_error("on_characters runtime exception");
63   }
64 };
65
66 void test_on_characters()
67 {
68   OnCharactersTestParser parser;
69   bool exceptionThrown = false;
70   try
71   {
72     parser.parse_memory("<root>abc</root>");
73   }
74 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
75   catch(const std::runtime_error& e)
76 #else
77   catch(const xmlpp::exception& e)
78 #endif
79   {
80     exceptionThrown = true;
81     g_assert_cmpstr(e.what(), ==, "on_characters runtime exception");
82   }
83   g_assert_true(exceptionThrown);
84 }
85
86
87 class OnCommentTestParser : public xmlpp::SaxParser
88 {
89 protected:
90   void on_comment(const Glib::ustring& text) override
91   {
92     g_assert_cmpstr(text.c_str(), ==, "a comment");
93     throw std::runtime_error("on_comment runtime exception");
94   }
95 };
96
97 void test_on_comment()
98 {
99   OnCommentTestParser parser;
100   bool exceptionThrown = false;
101   try
102   {
103     parser.parse_memory("<root><!--a comment--></root>");
104   }
105 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
106   catch(const std::runtime_error& e)
107 #else
108   catch(const xmlpp::exception& e)
109 #endif
110   {
111     exceptionThrown = true;
112     g_assert_cmpstr(e.what(), ==, "on_comment runtime exception");
113   }
114   g_assert_true(exceptionThrown);
115 }
116
117
118 class OnEndDocumentTestParser : public xmlpp::SaxParser
119 {
120 protected:
121   void on_end_document() override
122   {
123     throw std::runtime_error("on_end_document runtime exception");
124   }
125 };
126
127 void test_on_end_document()
128 {
129   OnEndDocumentTestParser parser;
130   bool exceptionThrown = false;
131   try
132   {
133     parser.parse_memory("<root></root>");
134   }
135 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
136   catch(const std::runtime_error& e)
137 #else
138   catch(const xmlpp::exception& e)
139 #endif
140   {
141     exceptionThrown = true;
142     g_assert_cmpstr(e.what(), ==, "on_end_document runtime exception");
143   }
144   g_assert_true(exceptionThrown);
145 }
146
147
148 class OnEndElementTestParser : public xmlpp::SaxParser
149 {
150 protected:
151   void on_end_element(const Glib::ustring& name) override
152   {
153     g_assert_cmpstr(name.c_str(), ==, "a:root");
154     throw std::runtime_error("on_end_element runtime exception");
155   }
156 };
157
158 void test_on_end_element()
159 {
160   OnEndElementTestParser parser;
161   bool exceptionThrown = false;
162   try
163   {
164     parser.parse_memory("<a:root xmlns:a=\"urn:test\"></a:root>");
165   }
166 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
167   catch(const std::runtime_error& e)
168 #else
169   catch(const xmlpp::exception& e)
170 #endif
171   {
172     exceptionThrown = true;
173     g_assert_cmpstr(e.what(), ==, "on_end_element runtime exception");
174   }
175   g_assert_true(exceptionThrown);
176 }
177
178
179 class OnEntityDeclarationTestParser : public xmlpp::SaxParser
180 {
181 protected:
182   void on_entity_declaration(const Glib::ustring& name, xmlpp::XmlEntityType /* type */,
183     const Glib::ustring& /* publicId */, const Glib::ustring& /* systemId */,
184     const Glib::ustring& content) override
185   {
186     g_assert_cmpstr(name.c_str(), ==, "number");
187     g_assert_cmpstr(content.c_str(), ==, "42");
188     throw std::runtime_error("on_entity_declaration runtime exception");
189   }
190 };
191
192 void test_on_entity_declaration()
193 {
194   OnEntityDeclarationTestParser parser;
195   bool exceptionThrown = false;
196   try
197   {
198     parser.parse_memory("<!DOCTYPE MyDocument [<!ENTITY number \"42\">]><root></root>");
199   }
200 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
201   catch(const std::runtime_error& e)
202 #else
203   catch(const xmlpp::exception& e)
204 #endif
205   {
206     exceptionThrown = true;
207     g_assert_cmpstr(e.what(), ==, "on_entity_declaration runtime exception");
208   }
209   g_assert_true(exceptionThrown);
210 }
211
212
213 class OnErrorTestParser : public xmlpp::SaxParser
214 {
215 protected:
216   void on_error(const Glib::ustring& /* text */) override
217   {
218     throw std::runtime_error("on_error runtime exception");
219   }
220 };
221
222 void test_on_error()
223 {
224   OnErrorTestParser parser;
225   bool exceptionThrown = false;
226   try
227   {
228     parser.parse_memory("<root>&unknown;</root>");
229   }
230 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
231   catch(const std::runtime_error& e)
232 #else
233   catch(const xmlpp::exception& e)
234 #endif
235   {
236     exceptionThrown = true;
237     g_assert_cmpstr(e.what(), ==, "on_error runtime exception");
238   }
239   g_assert_true(exceptionThrown);
240 }
241
242
243 class OnGetEntityTestParser : public xmlpp::SaxParser
244 {
245 public:
246   OnGetEntityTestParser()
247     : xmlpp::SaxParser(true)
248   {
249   }
250 protected:
251   _xmlEntity* on_get_entity(const Glib::ustring& name) override
252   {
253     g_assert_cmpstr(name.c_str(), ==, "number");
254     throw std::runtime_error("on_get_entity runtime exception");
255   }
256 };
257
258 void test_on_get_entity()
259 {
260   OnGetEntityTestParser parser;
261   bool exceptionThrown = false;
262   try
263   {
264     parser.parse_memory("<!DOCTYPE MyDocument [<!ENTITY number \"42\">]><root>&number;</root>");
265   }
266 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
267   catch(const std::runtime_error& e)
268 #else
269   catch(const xmlpp::exception& e)
270 #endif
271   {
272     exceptionThrown = true;
273     g_assert_cmpstr(e.what(), ==, "on_get_entity runtime exception");
274   }
275   g_assert_true(exceptionThrown);
276 }
277
278
279 class OnStartDocumentTestParser : public xmlpp::SaxParser
280 {
281 protected:
282   void on_start_document() override
283   {
284     throw std::runtime_error("on_start_document runtime exception");
285   }
286 };
287
288 void test_on_start_document()
289 {
290   OnStartDocumentTestParser parser;
291   bool exceptionThrown = false;
292   try
293   {
294     parser.parse_memory("<root></root>");
295   }
296 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
297   catch(const std::runtime_error& e)
298 #else
299   catch(const xmlpp::exception& e)
300 #endif
301   {
302     exceptionThrown = true;
303     g_assert_cmpstr(e.what(), ==, "on_start_document runtime exception");
304   }
305   g_assert_true(exceptionThrown);
306 }
307
308
309 class OnStartElementTestParser : public xmlpp::SaxParser
310 {
311 protected:
312   void on_start_element(const Glib::ustring& name, const xmlpp::SaxParser::AttributeList& attributes) override
313   {
314     g_assert_cmpstr(name.c_str(), ==, "b:root");
315     g_assert_cmpint(attributes.size(), ==, 2);
316     throw std::runtime_error("on_start_element runtime exception");
317   }
318 };
319
320 void test_on_start_element()
321 {
322   OnStartElementTestParser parser;
323   bool exceptionThrown = false;
324   try
325   {
326     parser.parse_memory("<b:root xmlns:b=\"urn:test\" someattr=\"test\"></b:root>");
327   }
328 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
329   catch(const std::runtime_error& e)
330 #else
331   catch(const xmlpp::exception& e)
332 #endif
333   {
334     exceptionThrown = true;
335     g_assert_cmpstr(e.what(), ==, "on_start_element runtime exception");
336   }
337   g_assert_true(exceptionThrown);
338 }
339
340
341 int main()
342 {
343   Glib::init();
344
345   test_on_cdata_block();
346   test_on_characters();
347   test_on_comment();
348   test_on_end_document();
349   test_on_end_element();
350   test_on_entity_declaration();
351   test_on_error();
352   // TODO test on_fatal_error()
353   // This is not currently possible because the fatalError handler is never called;
354   // error is called for all errors.
355   // http://xmlsoft.org/html/libxml-parser.html#fatalErrorSAXFunc
356   test_on_get_entity();
357   // TODO test on_internal_subset()
358   test_on_start_document();
359   test_on_start_element();
360   // TODO test on_warning(), on_validity_error(), on_validity_warning()
361
362   return EXIT_SUCCESS;
363 }