1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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("<>&\"'"), "<>&"'");
}
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"), " ");
EXPECT_EQ(xml_attribute_escaped("\r"), " ");
EXPECT_EQ(xml_attribute_escaped(stringref("\x00", 1)), "�"); // Can't just invoke strlen with null byte :)
EXPECT_EQ(xml_attribute_escaped("\x1f"), "");
}
TEST(StringEscapeTest, xml_content_special_chars_are_escaped) {
EXPECT_EQ(xml_content_escaped("<>&"), "<>&");
}
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"), " ");
EXPECT_EQ(xml_content_escaped(stringref("\x00", 1)), "�");
EXPECT_EQ(xml_content_escaped("\x1f"), "");
}
GTEST_MAIN_RUN_ALL_TESTS()
|