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