Xml.tests.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "catch.hpp"
  2. #include "internal/catch_xmlwriter.h"
  3. #include <sstream>
  4. inline std::string encode( std::string const& str, Catch::XmlEncode::ForWhat forWhat = Catch::XmlEncode::ForTextNodes ) {
  5. std::ostringstream oss;
  6. oss << Catch::XmlEncode( str, forWhat );
  7. return oss.str();
  8. }
  9. TEST_CASE( "XmlEncode", "[XML]" ) {
  10. SECTION( "normal string" ) {
  11. REQUIRE( encode( "normal string" ) == "normal string" );
  12. }
  13. SECTION( "empty string" ) {
  14. REQUIRE( encode( "" ) == "" );
  15. }
  16. SECTION( "string with ampersand" ) {
  17. REQUIRE( encode( "smith & jones" ) == "smith &amp; jones" );
  18. }
  19. SECTION( "string with less-than" ) {
  20. REQUIRE( encode( "smith < jones" ) == "smith &lt; jones" );
  21. }
  22. SECTION( "string with greater-than" ) {
  23. REQUIRE( encode( "smith > jones" ) == "smith > jones" );
  24. REQUIRE( encode( "smith ]]> jones" ) == "smith ]]&gt; jones" );
  25. }
  26. SECTION( "string with quotes" ) {
  27. std::string stringWithQuotes = "don't \"quote\" me on that";
  28. REQUIRE( encode( stringWithQuotes ) == stringWithQuotes );
  29. REQUIRE( encode( stringWithQuotes, Catch::XmlEncode::ForAttributes ) == "don't &quot;quote&quot; me on that" );
  30. }
  31. SECTION( "string with control char (1)" ) {
  32. REQUIRE( encode( "[\x01]" ) == "[\\x01]" );
  33. }
  34. SECTION( "string with control char (x7F)" ) {
  35. REQUIRE( encode( "[\x7F]" ) == "[\\x7F]" );
  36. }
  37. }
  38. // Thanks to Peter Bindels (dascandy) for some of the tests
  39. TEST_CASE("XmlEncode: UTF-8", "[XML][UTF-8][approvals]") {
  40. #define ESC(lit) (char*)(lit)
  41. SECTION("Valid utf-8 strings") {
  42. CHECK(encode(ESC(u8"Here be 👾")) == ESC(u8"Here be 👾"));
  43. CHECK(encode(ESC(u8"šš")) == ESC(u8"šš"));
  44. CHECK(encode("\xDF\xBF") == "\xDF\xBF"); // 0x7FF
  45. CHECK(encode("\xE0\xA0\x80") == "\xE0\xA0\x80"); // 0x800
  46. CHECK(encode("\xED\x9F\xBF") == "\xED\x9F\xBF"); // 0xD7FF
  47. CHECK(encode("\xEE\x80\x80") == "\xEE\x80\x80"); // 0xE000
  48. CHECK(encode("\xEF\xBF\xBF") == "\xEF\xBF\xBF"); // 0xFFFF
  49. CHECK(encode("\xF0\x90\x80\x80") == "\xF0\x90\x80\x80"); // 0x10000
  50. CHECK(encode("\xF4\x8F\xBF\xBF") == "\xF4\x8F\xBF\xBF"); // 0x10FFFF
  51. }
  52. SECTION("Invalid utf-8 strings") {
  53. SECTION("Various broken strings") {
  54. CHECK(encode(ESC("Here \xFF be \xF0\x9F\x91\xBE")) == ESC(u8"Here \\xFF be 👾"));
  55. CHECK(encode("\xFF") == "\\xFF");
  56. CHECK(encode("\xC5\xC5\xA0") == ESC(u8"\\xC5Š"));
  57. CHECK(encode("\xF4\x90\x80\x80") == ESC(u8"\\xF4\\x90\\x80\\x80")); // 0x110000 -- out of unicode range
  58. }
  59. SECTION("Overlong encodings") {
  60. CHECK(encode("\xC0\x80") == "\\xC0\\x80"); // \0
  61. CHECK(encode("\xF0\x80\x80\x80") == "\\xF0\\x80\\x80\\x80"); // Super-over-long \0
  62. CHECK(encode("\xC1\xBF") == "\\xC1\\xBF"); // ASCII char as UTF-8 (0x7F)
  63. CHECK(encode("\xE0\x9F\xBF") == "\\xE0\\x9F\\xBF"); // 0x7FF
  64. CHECK(encode("\xF0\x8F\xBF\xBF") == "\\xF0\\x8F\\xBF\\xBF"); // 0xFFFF
  65. }
  66. // Note that we actually don't modify surrogate pairs, as we do not do strict checking
  67. SECTION("Surrogate pairs") {
  68. CHECK(encode("\xED\xA0\x80") == "\xED\xA0\x80"); // Invalid surrogate half 0xD800
  69. CHECK(encode("\xED\xAF\xBF") == "\xED\xAF\xBF"); // Invalid surrogate half 0xDBFF
  70. CHECK(encode("\xED\xB0\x80") == "\xED\xB0\x80"); // Invalid surrogate half 0xDC00
  71. CHECK(encode("\xED\xBF\xBF") == "\xED\xBF\xBF"); // Invalid surrogate half 0xDFFF
  72. }
  73. SECTION("Invalid start byte") {
  74. CHECK(encode("\x80") == "\\x80");
  75. CHECK(encode("\x81") == "\\x81");
  76. CHECK(encode("\xBC") == "\\xBC");
  77. CHECK(encode("\xBF") == "\\xBF");
  78. // Out of range
  79. CHECK(encode("\xF5\x80\x80\x80") == "\\xF5\\x80\\x80\\x80");
  80. CHECK(encode("\xF6\x80\x80\x80") == "\\xF6\\x80\\x80\\x80");
  81. CHECK(encode("\xF7\x80\x80\x80") == "\\xF7\\x80\\x80\\x80");
  82. }
  83. SECTION("Missing continuation byte(s)") {
  84. // Missing first continuation byte
  85. CHECK(encode("\xDE") == "\\xDE");
  86. CHECK(encode("\xDF") == "\\xDF");
  87. CHECK(encode("\xE0") == "\\xE0");
  88. CHECK(encode("\xEF") == "\\xEF");
  89. CHECK(encode("\xF0") == "\\xF0");
  90. CHECK(encode("\xF4") == "\\xF4");
  91. // Missing second continuation byte
  92. CHECK(encode("\xE0\x80") == "\\xE0\\x80");
  93. CHECK(encode("\xE0\xBF") == "\\xE0\\xBF");
  94. CHECK(encode("\xE1\x80") == "\\xE1\\x80");
  95. CHECK(encode("\xF0\x80") == "\\xF0\\x80");
  96. CHECK(encode("\xF4\x80") == "\\xF4\\x80");
  97. // Missing third continuation byte
  98. CHECK(encode("\xF0\x80\x80") == "\\xF0\\x80\\x80");
  99. CHECK(encode("\xF4\x80\x80") == "\\xF4\\x80\\x80");
  100. }
  101. }
  102. #undef ESC
  103. }