File: xml.cpp

package info (click to toggle)
libfilezilla 0.52.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,492 kB
  • sloc: cpp: 30,965; sh: 4,241; makefile: 375; xml: 37
file content (71 lines) | stat: -rw-r--r-- 1,735 bytes parent folder | download | duplicates (3)
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
#include "../lib/libfilezilla/xml.hpp"

#include "test_utils.hpp"

using namespace std::literals;

class xml_test final : public CppUnit::TestFixture
{
	CPPUNIT_TEST_SUITE(xml_test);
	CPPUNIT_TEST(test_simple);
	CPPUNIT_TEST_SUITE_END();

public:
	void setUp() {}
	void tearDown() {}

	void test_simple();
};

CPPUNIT_TEST_SUITE_REGISTRATION(xml_test);

namespace {
void do_test(std::string_view in)
{
	fz::xml::parser parser;
	fz::xml::namespace_parser ns_parser;
	CPPUNIT_ASSERT(parser.parse(in));
	CPPUNIT_ASSERT(ns_parser.parse(in));
	CPPUNIT_ASSERT(parser.finalize());
	CPPUNIT_ASSERT(ns_parser.finalize());

	fz::xml::parser parser2;
	fz::xml::namespace_parser ns_parser2;
	for (size_t i = 0; i < in.size(); ++i) {
		CPPUNIT_ASSERT(parser2.parse(in.substr(i, 1)));
		CPPUNIT_ASSERT(ns_parser2.parse(in.substr(i, 1)));
	}
	CPPUNIT_ASSERT(parser2.finalize());
	CPPUNIT_ASSERT(ns_parser2.finalize());
}
}
void xml_test::test_simple()
{
	// Test this simple XML in all supported encodings, with and without BOM
	auto utf8 = "<?xml version=\"1.0\" ?><!DOCTYPE nonsense PUBLIC \"makes\" 'nosense'><?PI blabla test?><foo attr = \"baz&lt;\"><![CDATA[]]><!--foo--><empty  foo   = 'bar\"'/><otherempty /><?INNERPI?>bar &#xe4; foo<foo ></foo></foo > <?PI2?>"sv;

	std::string utf16be, utf16le;
	for (auto const& c : utf8) {
		utf16be.append(1, 0);
		utf16be.append(1, c);

		utf16le.append(1, c);
		utf16le.append(1, 0);
	}

	std::string utf8bom = "\xef" "\xbb" "\xbf";
	utf8bom += utf8;

	std::string utf16bebom = "\xfe" "\xff";
	utf16bebom += utf16be;

	std::string utf16lebom = "\xff" "\xfe";
	utf16lebom += utf16le;

	do_test(utf8);
	do_test(utf16be);
	do_test(utf16le);
	do_test(utf8bom);
	do_test(utf16bebom);
	do_test(utf16lebom);
}