| 1 |
|
| 2 |
// Copyright Catch2 Authors |
| 3 |
// Distributed under the Boost Software License, Version 1.0. |
| 4 |
// (See accompanying file LICENSE.txt or copy at |
| 5 |
// https://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
// SPDX-License-Identifier: BSL-1.0 |
| 8 |
|
| 9 |
#include <catch2/catch_test_macros.hpp> |
| 10 |
#include <catch2/benchmark/catch_benchmark.hpp> |
| 11 |
#include <catch2/generators/catch_generators.hpp> |
| 12 |
#include <catch2/internal/catch_reusable_string_stream.hpp> |
| 13 |
#include <catch2/internal/catch_xmlwriter.hpp> |
| 14 |
#include <catch2/matchers/catch_matchers_string.hpp> |
| 15 |
|
| 16 |
#include <sstream> |
| 17 |
|
| 18 |
namespace { |
| 19 |
|
| 20 |
static std::string encode( std::string const& str, Catch::XmlEncode::ForWhat forWhat = Catch::XmlEncode::ForTextNodes ) { |
| 21 |
Catch::ReusableStringStream oss; |
| 22 |
oss << Catch::XmlEncode( str, forWhat ); |
| 23 |
return oss.str(); |
| 24 |
} |
| 25 |
|
| 26 |
TEST_CASE( "XmlEncode", "[XML]" ) { |
| 27 |
SECTION( "normal string" ) { |
| 28 |
REQUIRE( encode( "normal string" ) == "normal string" ); |
| 29 |
} |
| 30 |
SECTION( "empty string" ) { |
| 31 |
REQUIRE( encode( "" ) == "" ); |
| 32 |
} |
| 33 |
SECTION( "string with ampersand" ) { |
| 34 |
REQUIRE( encode( "smith & jones" ) == "smith & jones" ); |
| 35 |
} |
| 36 |
SECTION( "string with less-than" ) { |
| 37 |
REQUIRE( encode( "smith < jones" ) == "smith < jones" ); |
| 38 |
} |
| 39 |
SECTION( "string with greater-than" ) { |
| 40 |
REQUIRE( encode( "smith > jones" ) == "smith > jones" ); |
| 41 |
REQUIRE( encode( "smith ]]> jones" ) == "smith ]]> jones" ); |
| 42 |
} |
| 43 |
SECTION( "string with quotes" ) { |
| 44 |
std::string stringWithQuotes = "don't \"quote\" me on that"; |
| 45 |
REQUIRE( encode( stringWithQuotes ) == stringWithQuotes ); |
| 46 |
REQUIRE( encode( stringWithQuotes, Catch::XmlEncode::ForAttributes ) == "don't "quote" me on that" ); |
| 47 |
} |
| 48 |
SECTION( "string with control char (1)" ) { |
| 49 |
REQUIRE( encode( "[\x01]" ) == "[\\x01]" ); |
| 50 |
} |
| 51 |
SECTION( "string with control char (x7F)" ) { |
| 52 |
REQUIRE( encode( "[\x7F]" ) == "[\\x7F]" ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Thanks to Peter Bindels (dascandy) for some of the tests |
| 57 |
TEST_CASE("XmlEncode: UTF-8", "[XML][UTF-8][approvals]") { |
| 58 |
SECTION("Valid utf-8 strings") { |
| 59 |
CHECK(encode("Here be 👾") == "Here be 👾"); |
| 60 |
CHECK(encode("šš") == "šš"); |
| 61 |
|
| 62 |
CHECK(encode("\xDF\xBF") == "\xDF\xBF"); // 0x7FF |
| 63 |
CHECK(encode("\xE0\xA0\x80") == "\xE0\xA0\x80"); // 0x800 |
| 64 |
CHECK(encode("\xED\x9F\xBF") == "\xED\x9F\xBF"); // 0xD7FF |
| 65 |
CHECK(encode("\xEE\x80\x80") == "\xEE\x80\x80"); // 0xE000 |
| 66 |
CHECK(encode("\xEF\xBF\xBF") == "\xEF\xBF\xBF"); // 0xFFFF |
| 67 |
CHECK(encode("\xF0\x90\x80\x80") == "\xF0\x90\x80\x80"); // 0x10000 |
| 68 |
CHECK(encode("\xF4\x8F\xBF\xBF") == "\xF4\x8F\xBF\xBF"); // 0x10FFFF |
| 69 |
} |
| 70 |
SECTION("Invalid utf-8 strings") { |
| 71 |
SECTION("Various broken strings") { |
| 72 |
CHECK(encode("Here \xFF be \xF0\x9F\x91\xBE") == "Here \\xFF be 👾"); |
| 73 |
CHECK(encode("\xFF") == "\\xFF"); |
| 74 |
CHECK(encode("\xC5\xC5\xA0") == "\\xC5Å "); |
| 75 |
CHECK(encode("\xF4\x90\x80\x80") == "\\xF4\\x90\\x80\\x80"); // 0x110000 -- out of unicode range |
| 76 |
} |
| 77 |
|
| 78 |
SECTION("Overlong encodings") { |
| 79 |
CHECK(encode("\xC0\x80") == "\\xC0\\x80"); // \0 |
| 80 |
CHECK(encode("\xF0\x80\x80\x80") == "\\xF0\\x80\\x80\\x80"); // Super-over-long \0 |
| 81 |
CHECK(encode("\xC1\xBF") == "\\xC1\\xBF"); // ASCII char as UTF-8 (0x7F) |
| 82 |
CHECK(encode("\xE0\x9F\xBF") == "\\xE0\\x9F\\xBF"); // 0x7FF |
| 83 |
CHECK(encode("\xF0\x8F\xBF\xBF") == "\\xF0\\x8F\\xBF\\xBF"); // 0xFFFF |
| 84 |
} |
| 85 |
|
| 86 |
// Note that we actually don't modify surrogate pairs, as we do not do strict checking |
| 87 |
SECTION("Surrogate pairs") { |
| 88 |
CHECK(encode("\xED\xA0\x80") == "\xED\xA0\x80"); // Invalid surrogate half 0xD800 |
| 89 |
CHECK(encode("\xED\xAF\xBF") == "\xED\xAF\xBF"); // Invalid surrogate half 0xDBFF |
| 90 |
CHECK(encode("\xED\xB0\x80") == "\xED\xB0\x80"); // Invalid surrogate half 0xDC00 |
| 91 |
CHECK(encode("\xED\xBF\xBF") == "\xED\xBF\xBF"); // Invalid surrogate half 0xDFFF |
| 92 |
} |
| 93 |
|
| 94 |
SECTION("Invalid start byte") { |
| 95 |
CHECK(encode("\x80") == "\\x80"); |
| 96 |
CHECK(encode("\x81") == "\\x81"); |
| 97 |
CHECK(encode("\xBC") == "\\xBC"); |
| 98 |
CHECK(encode("\xBF") == "\\xBF"); |
| 99 |
// Out of range |
| 100 |
CHECK(encode("\xF5\x80\x80\x80") == "\\xF5\\x80\\x80\\x80"); |
| 101 |
CHECK(encode("\xF6\x80\x80\x80") == "\\xF6\\x80\\x80\\x80"); |
| 102 |
CHECK(encode("\xF7\x80\x80\x80") == "\\xF7\\x80\\x80\\x80"); |
| 103 |
} |
| 104 |
|
| 105 |
SECTION("Missing continuation byte(s)") { |
| 106 |
// Missing first continuation byte |
| 107 |
CHECK(encode("\xDE") == "\\xDE"); |
| 108 |
CHECK(encode("\xDF") == "\\xDF"); |
| 109 |
CHECK(encode("\xE0") == "\\xE0"); |
| 110 |
CHECK(encode("\xEF") == "\\xEF"); |
| 111 |
CHECK(encode("\xF0") == "\\xF0"); |
| 112 |
CHECK(encode("\xF4") == "\\xF4"); |
| 113 |
|
| 114 |
// Missing second continuation byte |
| 115 |
CHECK(encode("\xE0\x80") == "\\xE0\\x80"); |
| 116 |
CHECK(encode("\xE0\xBF") == "\\xE0\\xBF"); |
| 117 |
CHECK(encode("\xE1\x80") == "\\xE1\\x80"); |
| 118 |
CHECK(encode("\xF0\x80") == "\\xF0\\x80"); |
| 119 |
CHECK(encode("\xF4\x80") == "\\xF4\\x80"); |
| 120 |
|
| 121 |
// Missing third continuation byte |
| 122 |
CHECK(encode("\xF0\x80\x80") == "\\xF0\\x80\\x80"); |
| 123 |
CHECK(encode("\xF4\x80\x80") == "\\xF4\\x80\\x80"); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
TEST_CASE("XmlWriter writes boolean attributes as true/false", "[XML][XmlWriter]") { |
| 129 |
using Catch::Matchers::ContainsSubstring; |
| 130 |
std::stringstream stream; |
| 131 |
{ |
| 132 |
Catch::XmlWriter xml(stream); |
| 133 |
|
| 134 |
xml.scopedElement("Element1") |
| 135 |
.writeAttribute("attr1", true) |
| 136 |
.writeAttribute("attr2", false); |
| 137 |
} |
| 138 |
|
| 139 |
REQUIRE_THAT( stream.str(), |
| 140 |
ContainsSubstring(R"(attr1="true")") && |
| 141 |
ContainsSubstring(R"(attr2="false")") ); |
| 142 |
} |
| 143 |
|
| 144 |
TEST_CASE("XmlWriter does not escape comments", "[XML][XmlWriter][approvals]") { |
| 145 |
using Catch::Matchers::ContainsSubstring; |
| 146 |
std::stringstream stream; |
| 147 |
{ |
| 148 |
Catch::XmlWriter xml(stream); |
| 149 |
|
| 150 |
xml.writeComment(R"(unescaped special chars: < > ' " &)"); |
| 151 |
} |
| 152 |
REQUIRE_THAT( stream.str(), |
| 153 |
ContainsSubstring(R"(<!-- unescaped special chars: < > ' " & -->)")); |
| 154 |
} |
| 155 |
|
| 156 |
TEST_CASE("XmlWriter errors out when writing text without enclosing element", "[XmlWriter][approvals]") { |
| 157 |
std::stringstream stream; |
| 158 |
Catch::XmlWriter xml(stream); |
| 159 |
REQUIRE_THROWS(xml.writeText("some text")); |
| 160 |
} |
| 161 |
|
| 162 |
TEST_CASE("XmlWriter escapes text properly", "[XML][XmlWriter][approvals]") { |
| 163 |
using Catch::Matchers::ContainsSubstring; |
| 164 |
std::stringstream stream; |
| 165 |
{ |
| 166 |
Catch::XmlWriter xml(stream); |
| 167 |
xml.scopedElement("root") |
| 168 |
.writeText(R"(Special chars need escaping: < > ' " &)"); |
| 169 |
} |
| 170 |
|
| 171 |
REQUIRE_THAT( stream.str(), |
| 172 |
ContainsSubstring(R"(Special chars need escaping: < > ' " &)")); |
| 173 |
} |
| 174 |
|
| 175 |
TEST_CASE("XmlWriter escapes attributes properly", "[XML][XmlWriter][approvals]") { |
| 176 |
using Catch::Matchers::ContainsSubstring; |
| 177 |
std::stringstream stream; |
| 178 |
{ |
| 179 |
Catch::XmlWriter xml(stream); |
| 180 |
xml.scopedElement("root") |
| 181 |
.writeAttribute("some-attribute", R"(Special chars need escaping: < > ' " &)"); |
| 182 |
} |
| 183 |
|
| 184 |
REQUIRE_THAT(stream.str(), |
| 185 |
ContainsSubstring(R"(some-attribute="Special chars need escaping: < > ' " &")")); |
| 186 |
} |
| 187 |
|
| 188 |
TEST_CASE( "XmlWriter benchmarks", "[XML][XmlWriter][!benchmark]" ) { |
| 189 |
const auto input_length = GENERATE( as<size_t>{}, 10, 100, 10'000 ); |
| 190 |
std::string test_input( input_length, 'a' ); |
| 191 |
BENCHMARK_ADVANCED( "write string, no-escaping, len=" + |
| 192 |
std::to_string( input_length ) ) { |
| 193 |
return encode( test_input ); |
| 194 |
}; |
| 195 |
|
| 196 |
std::string escape_input( input_length, '\b' ); |
| 197 |
BENCHMARK_ADVANCED( "write string, all-escaped, len=" + |
| 198 |
std::to_string( input_length ) ) { |
| 199 |
return encode( escape_input ); |
| 200 |
}; |
| 201 |
} |
| 202 |
|
| 203 |
} // namespace |