2022-01-29 00:03:43 +01:00
|
|
|
|
|
|
|
// Copyright Catch2 Authors
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
// https://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
|
2020-01-20 23:24:04 +01:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2020-03-30 10:34:21 +02:00
|
|
|
#include <catch2/internal/catch_xmlwriter.hpp>
|
2017-11-13 16:38:52 +01:00
|
|
|
|
2020-08-18 13:47:58 +02:00
|
|
|
#include <catch2/internal/catch_stream.hpp>
|
2021-05-30 20:02:02 +02:00
|
|
|
#include <catch2/matchers/catch_matchers_string.hpp>
|
|
|
|
|
|
|
|
#include <sstream>
|
2017-11-13 16:38:52 +01:00
|
|
|
|
2020-08-18 13:47:58 +02:00
|
|
|
static std::string encode( std::string const& str, Catch::XmlEncode::ForWhat forWhat = Catch::XmlEncode::ForTextNodes ) {
|
|
|
|
Catch::ReusableStringStream oss;
|
2017-11-13 16:38:52 +01:00
|
|
|
oss << Catch::XmlEncode( str, forWhat );
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2018-03-25 20:44:30 +02:00
|
|
|
TEST_CASE( "XmlEncode", "[XML]" ) {
|
2017-11-13 16:38:52 +01:00
|
|
|
SECTION( "normal string" ) {
|
|
|
|
REQUIRE( encode( "normal string" ) == "normal string" );
|
|
|
|
}
|
|
|
|
SECTION( "empty string" ) {
|
|
|
|
REQUIRE( encode( "" ) == "" );
|
|
|
|
}
|
|
|
|
SECTION( "string with ampersand" ) {
|
|
|
|
REQUIRE( encode( "smith & jones" ) == "smith & jones" );
|
|
|
|
}
|
|
|
|
SECTION( "string with less-than" ) {
|
|
|
|
REQUIRE( encode( "smith < jones" ) == "smith < jones" );
|
|
|
|
}
|
|
|
|
SECTION( "string with greater-than" ) {
|
|
|
|
REQUIRE( encode( "smith > jones" ) == "smith > jones" );
|
|
|
|
REQUIRE( encode( "smith ]]> jones" ) == "smith ]]> jones" );
|
|
|
|
}
|
|
|
|
SECTION( "string with quotes" ) {
|
|
|
|
std::string stringWithQuotes = "don't \"quote\" me on that";
|
|
|
|
REQUIRE( encode( stringWithQuotes ) == stringWithQuotes );
|
|
|
|
REQUIRE( encode( stringWithQuotes, Catch::XmlEncode::ForAttributes ) == "don't "quote" me on that" );
|
|
|
|
}
|
|
|
|
SECTION( "string with control char (1)" ) {
|
|
|
|
REQUIRE( encode( "[\x01]" ) == "[\\x01]" );
|
|
|
|
}
|
|
|
|
SECTION( "string with control char (x7F)" ) {
|
|
|
|
REQUIRE( encode( "[\x7F]" ) == "[\\x7F]" );
|
|
|
|
}
|
2018-03-25 20:44:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Thanks to Peter Bindels (dascandy) for some of the tests
|
2019-11-04 10:42:34 +01:00
|
|
|
TEST_CASE("XmlEncode: UTF-8", "[XML][UTF-8][approvals]") {
|
2020-12-28 14:00:19 +01:00
|
|
|
#define ESC(lit) reinterpret_cast<const char*>(lit)
|
2018-03-25 20:44:30 +02:00
|
|
|
SECTION("Valid utf-8 strings") {
|
2019-11-04 10:42:34 +01:00
|
|
|
CHECK(encode(ESC(u8"Here be 👾")) == ESC(u8"Here be 👾"));
|
|
|
|
CHECK(encode(ESC(u8"šš")) == ESC(u8"šš"));
|
2018-03-25 20:44:30 +02:00
|
|
|
|
|
|
|
CHECK(encode("\xDF\xBF") == "\xDF\xBF"); // 0x7FF
|
|
|
|
CHECK(encode("\xE0\xA0\x80") == "\xE0\xA0\x80"); // 0x800
|
|
|
|
CHECK(encode("\xED\x9F\xBF") == "\xED\x9F\xBF"); // 0xD7FF
|
|
|
|
CHECK(encode("\xEE\x80\x80") == "\xEE\x80\x80"); // 0xE000
|
|
|
|
CHECK(encode("\xEF\xBF\xBF") == "\xEF\xBF\xBF"); // 0xFFFF
|
|
|
|
CHECK(encode("\xF0\x90\x80\x80") == "\xF0\x90\x80\x80"); // 0x10000
|
|
|
|
CHECK(encode("\xF4\x8F\xBF\xBF") == "\xF4\x8F\xBF\xBF"); // 0x10FFFF
|
|
|
|
}
|
|
|
|
SECTION("Invalid utf-8 strings") {
|
|
|
|
SECTION("Various broken strings") {
|
2019-11-04 10:42:34 +01:00
|
|
|
CHECK(encode(ESC("Here \xFF be \xF0\x9F\x91\xBE")) == ESC(u8"Here \\xFF be 👾"));
|
2018-03-25 20:44:30 +02:00
|
|
|
CHECK(encode("\xFF") == "\\xFF");
|
2019-11-04 10:42:34 +01:00
|
|
|
CHECK(encode("\xC5\xC5\xA0") == ESC(u8"\\xC5Š"));
|
|
|
|
CHECK(encode("\xF4\x90\x80\x80") == ESC(u8"\\xF4\\x90\\x80\\x80")); // 0x110000 -- out of unicode range
|
2018-03-25 20:44:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Overlong encodings") {
|
2019-11-04 10:42:34 +01:00
|
|
|
CHECK(encode("\xC0\x80") == "\\xC0\\x80"); // \0
|
|
|
|
CHECK(encode("\xF0\x80\x80\x80") == "\\xF0\\x80\\x80\\x80"); // Super-over-long \0
|
|
|
|
CHECK(encode("\xC1\xBF") == "\\xC1\\xBF"); // ASCII char as UTF-8 (0x7F)
|
|
|
|
CHECK(encode("\xE0\x9F\xBF") == "\\xE0\\x9F\\xBF"); // 0x7FF
|
|
|
|
CHECK(encode("\xF0\x8F\xBF\xBF") == "\\xF0\\x8F\\xBF\\xBF"); // 0xFFFF
|
2018-03-25 20:44:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we actually don't modify surrogate pairs, as we do not do strict checking
|
|
|
|
SECTION("Surrogate pairs") {
|
|
|
|
CHECK(encode("\xED\xA0\x80") == "\xED\xA0\x80"); // Invalid surrogate half 0xD800
|
|
|
|
CHECK(encode("\xED\xAF\xBF") == "\xED\xAF\xBF"); // Invalid surrogate half 0xDBFF
|
|
|
|
CHECK(encode("\xED\xB0\x80") == "\xED\xB0\x80"); // Invalid surrogate half 0xDC00
|
|
|
|
CHECK(encode("\xED\xBF\xBF") == "\xED\xBF\xBF"); // Invalid surrogate half 0xDFFF
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Invalid start byte") {
|
2019-11-04 10:42:34 +01:00
|
|
|
CHECK(encode("\x80") == "\\x80");
|
|
|
|
CHECK(encode("\x81") == "\\x81");
|
|
|
|
CHECK(encode("\xBC") == "\\xBC");
|
|
|
|
CHECK(encode("\xBF") == "\\xBF");
|
2018-03-25 20:44:30 +02:00
|
|
|
// Out of range
|
2019-11-04 10:42:34 +01:00
|
|
|
CHECK(encode("\xF5\x80\x80\x80") == "\\xF5\\x80\\x80\\x80");
|
|
|
|
CHECK(encode("\xF6\x80\x80\x80") == "\\xF6\\x80\\x80\\x80");
|
|
|
|
CHECK(encode("\xF7\x80\x80\x80") == "\\xF7\\x80\\x80\\x80");
|
2018-03-25 20:44:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Missing continuation byte(s)") {
|
|
|
|
// Missing first continuation byte
|
2019-11-04 10:42:34 +01:00
|
|
|
CHECK(encode("\xDE") == "\\xDE");
|
|
|
|
CHECK(encode("\xDF") == "\\xDF");
|
|
|
|
CHECK(encode("\xE0") == "\\xE0");
|
|
|
|
CHECK(encode("\xEF") == "\\xEF");
|
|
|
|
CHECK(encode("\xF0") == "\\xF0");
|
|
|
|
CHECK(encode("\xF4") == "\\xF4");
|
2018-03-25 20:44:30 +02:00
|
|
|
|
|
|
|
// Missing second continuation byte
|
2019-11-04 10:42:34 +01:00
|
|
|
CHECK(encode("\xE0\x80") == "\\xE0\\x80");
|
|
|
|
CHECK(encode("\xE0\xBF") == "\\xE0\\xBF");
|
|
|
|
CHECK(encode("\xE1\x80") == "\\xE1\\x80");
|
|
|
|
CHECK(encode("\xF0\x80") == "\\xF0\\x80");
|
|
|
|
CHECK(encode("\xF4\x80") == "\\xF4\\x80");
|
2018-03-25 20:44:30 +02:00
|
|
|
|
|
|
|
// Missing third continuation byte
|
2019-11-04 10:42:34 +01:00
|
|
|
CHECK(encode("\xF0\x80\x80") == "\\xF0\\x80\\x80");
|
|
|
|
CHECK(encode("\xF4\x80\x80") == "\\xF4\\x80\\x80");
|
2018-03-25 20:44:30 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 10:42:34 +01:00
|
|
|
#undef ESC
|
2018-03-25 20:44:30 +02:00
|
|
|
}
|
2021-05-30 20:02:02 +02:00
|
|
|
|
|
|
|
TEST_CASE("XmlWriter writes boolean attributes as true/false", "[XML][XmlWriter]") {
|
2021-09-23 23:28:59 +02:00
|
|
|
using Catch::Matchers::ContainsSubstring;
|
2021-05-30 20:02:02 +02:00
|
|
|
std::stringstream stream;
|
|
|
|
{
|
|
|
|
Catch::XmlWriter xml(stream);
|
|
|
|
|
|
|
|
xml.scopedElement("Element1")
|
|
|
|
.writeAttribute("attr1", true)
|
|
|
|
.writeAttribute("attr2", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
REQUIRE_THAT( stream.str(),
|
2021-09-23 23:28:59 +02:00
|
|
|
ContainsSubstring(R"(attr1="true")") &&
|
|
|
|
ContainsSubstring(R"(attr2="false")") );
|
2021-05-30 20:02:02 +02:00
|
|
|
}
|
2021-10-01 12:02:17 +02:00
|
|
|
|
|
|
|
TEST_CASE("XmlWriter does not escape comments", "[XML][XmlWriter][approvals]") {
|
|
|
|
using Catch::Matchers::ContainsSubstring;
|
|
|
|
std::stringstream stream;
|
|
|
|
{
|
|
|
|
Catch::XmlWriter xml(stream);
|
|
|
|
|
|
|
|
xml.writeComment(R"(unescaped special chars: < > ' " &)");
|
|
|
|
}
|
|
|
|
REQUIRE_THAT( stream.str(),
|
|
|
|
ContainsSubstring(R"(<!-- unescaped special chars: < > ' " & -->)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("XmlWriter errors out when writing text without enclosing element", "[XmlWriter][approvals]") {
|
|
|
|
std::stringstream stream;
|
|
|
|
Catch::XmlWriter xml(stream);
|
|
|
|
REQUIRE_THROWS(xml.writeText("some text"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("XmlWriter escapes text properly", "[XML][XmlWriter][approvals]") {
|
|
|
|
using Catch::Matchers::ContainsSubstring;
|
|
|
|
std::stringstream stream;
|
|
|
|
{
|
|
|
|
Catch::XmlWriter xml(stream);
|
|
|
|
xml.scopedElement("root")
|
|
|
|
.writeText(R"(Special chars need escaping: < > ' " &)");
|
|
|
|
}
|
|
|
|
|
|
|
|
REQUIRE_THAT( stream.str(),
|
|
|
|
ContainsSubstring(R"(Special chars need escaping: < > ' " &)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("XmlWriter escapes attributes properly", "[XML][XmlWriter][approvals]") {
|
|
|
|
using Catch::Matchers::ContainsSubstring;
|
|
|
|
std::stringstream stream;
|
|
|
|
{
|
|
|
|
Catch::XmlWriter xml(stream);
|
|
|
|
xml.scopedElement("root")
|
|
|
|
.writeAttribute("some-attribute", R"(Special chars need escaping: < > ' " &)");
|
|
|
|
}
|
|
|
|
|
|
|
|
REQUIRE_THAT(stream.str(),
|
|
|
|
ContainsSubstring(R"(some-attribute="Special chars need escaping: < > ' " &")"));
|
|
|
|
}
|