aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/util/string_escape/string_escape_test.cpp
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-09-05 19:50:43 +0200
committerGitHub <noreply@github.com>2022-09-05 19:50:43 +0200
commit3c9c1d909476896b16ba750090f042e7824b50b2 (patch)
treebdddf72f0cce5b1b309bb61fc60a7f0bf77d8bb1 /vespalib/src/tests/util/string_escape/string_escape_test.cpp
parentb81ed9944fd93513b18c48f8fc84d9aeec8615a5 (diff)
parent506f285043535af5d81fd098dfd28166930704e5 (diff)
Merge pull request #23934 from vespa-engine/vekterli/factor-out-xml-string-escapingv8.48.22
Factor out XML string escaping and use for internal legacy status pages [run-systemtest]
Diffstat (limited to 'vespalib/src/tests/util/string_escape/string_escape_test.cpp')
-rw-r--r--vespalib/src/tests/util/string_escape/string_escape_test.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/vespalib/src/tests/util/string_escape/string_escape_test.cpp b/vespalib/src/tests/util/string_escape/string_escape_test.cpp
new file mode 100644
index 00000000000..1ee2c08fbc3
--- /dev/null
+++ b/vespalib/src/tests/util/string_escape/string_escape_test.cpp
@@ -0,0 +1,44 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/vespalib/util/string_escape.h>
+#include <vespa/vespalib/gtest/gtest.h>
+
+using namespace vespalib;
+using namespace ::testing;
+
+TEST(StringEscapeTest, xml_attribute_special_chars_are_escaped) {
+ // We always escape both " and ' since we don't know the quoting context of the enclosing attribute.
+ EXPECT_EQ(xml_attribute_escaped("<>&\"'"), "&lt;&gt;&amp;&quot;&#39;");
+}
+
+TEST(StringEscapeTest, xml_attribute_regular_chars_are_not_escaped) {
+ // Far from exhaustive, but should catch obvious mess-ups.
+ EXPECT_EQ(xml_attribute_escaped("09azAZ.,()[]$!"), "09azAZ.,()[]$!");
+}
+
+TEST(StringEscapeTest, control_characters_are_escaped_in_attributes) {
+ EXPECT_EQ(xml_attribute_escaped("\n"), "&#10;");
+ EXPECT_EQ(xml_attribute_escaped("\r"), "&#13;");
+ EXPECT_EQ(xml_attribute_escaped(stringref("\x00", 1)), "&#0;"); // Can't just invoke strlen with null byte :)
+ EXPECT_EQ(xml_attribute_escaped("\x1f"), "&#31;");
+}
+
+TEST(StringEscapeTest, xml_content_special_chars_are_escaped) {
+ EXPECT_EQ(xml_content_escaped("<>&"), "&lt;&gt;&amp;");
+}
+
+TEST(StringEscapeTest, xml_content_regular_chars_are_not_escaped) {
+ EXPECT_EQ(xml_content_escaped("09azAZ.,()[]$!"), "09azAZ.,()[]$!");
+ // Newlines are not escaped in content
+ EXPECT_EQ(xml_content_escaped("\n"), "\n");
+ // Quotes are not escaped in content
+ EXPECT_EQ(xml_content_escaped("\"'"), "\"'");
+}
+
+TEST(StringEscapeTest, control_characters_are_escaped_in_content) {
+ EXPECT_EQ(xml_content_escaped("\r"), "&#13;");
+ EXPECT_EQ(xml_content_escaped(stringref("\x00", 1)), "&#0;");
+ EXPECT_EQ(xml_content_escaped("\x1f"), "&#31;");
+}
+
+GTEST_MAIN_RUN_ALL_TESTS()