]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/escape.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
14 #include "common/escape.h"
15 #include "gtest/gtest.h"
18 static std::string
escape_xml_attrs(const char *str
)
20 int len
= escape_xml_attr_len(str
);
22 escape_xml_attr(str
, out
);
25 static std::string
escape_xml_stream(const char *str
)
28 ss
<< xml_stream_escaper(str
);
32 TEST(EscapeXml
, PassThrough
) {
33 ASSERT_EQ(escape_xml_attrs("simplicity itself"), "simplicity itself");
34 ASSERT_EQ(escape_xml_stream("simplicity itself"), "simplicity itself");
35 ASSERT_EQ(escape_xml_attrs(""), "");
36 ASSERT_EQ(escape_xml_stream(""), "");
37 ASSERT_EQ(escape_xml_attrs("simple examples please!"), "simple examples please!");
38 ASSERT_EQ(escape_xml_stream("simple examples please!"), "simple examples please!");
41 TEST(EscapeXml
, EntityRefs1
) {
42 ASSERT_EQ(escape_xml_attrs("The \"scare quotes\""), "The "scare quotes"");
43 ASSERT_EQ(escape_xml_stream("The \"scare quotes\""), "The "scare quotes"");
44 ASSERT_EQ(escape_xml_attrs("I <3 XML"), "I <3 XML");
45 ASSERT_EQ(escape_xml_stream("I <3 XML"), "I <3 XML");
46 ASSERT_EQ(escape_xml_attrs("Some 'single' \"quotes\" here"),
47 "Some 'single' "quotes" here");
48 ASSERT_EQ(escape_xml_stream("Some 'single' \"quotes\" here"),
49 "Some 'single' "quotes" here");
52 TEST(EscapeXml
, ControlChars
) {
53 ASSERT_EQ(escape_xml_attrs("\x01\x02\x03"), "");
54 ASSERT_EQ(escape_xml_stream("\x01\x02\x03"), "");
56 ASSERT_EQ(escape_xml_attrs("abc\x7f"), "abc");
57 ASSERT_EQ(escape_xml_stream("abc\x7f"), "abc");
60 TEST(EscapeXml
, Utf8
) {
61 const char *cc1
= "\xe6\xb1\x89\xe5\xad\x97\n";
62 ASSERT_EQ(escape_xml_attrs(cc1
), cc1
);
63 ASSERT_EQ(escape_xml_stream(cc1
), cc1
);
65 ASSERT_EQ(escape_xml_attrs("<\xe6\xb1\x89\xe5\xad\x97>\n"), "<\xe6\xb1\x89\xe5\xad\x97>\n");
66 ASSERT_EQ(escape_xml_stream("<\xe6\xb1\x89\xe5\xad\x97>\n"), "<\xe6\xb1\x89\xe5\xad\x97>\n");
69 static std::string
escape_json_attrs(const char *str
, size_t src_len
= 0)
72 src_len
= strlen(str
);
73 int len
= escape_json_attr_len(str
, src_len
);
75 escape_json_attr(str
, src_len
, out
);
78 static std::string
escape_json_stream(const char *str
, size_t src_len
= 0)
81 src_len
= strlen(str
);
83 ss
<< json_stream_escaper(std::string_view(str
, src_len
));
87 TEST(EscapeJson
, PassThrough
) {
88 ASSERT_EQ(escape_json_attrs("simplicity itself"), "simplicity itself");
89 ASSERT_EQ(escape_json_stream("simplicity itself"), "simplicity itself");
90 ASSERT_EQ(escape_json_attrs(""), "");
91 ASSERT_EQ(escape_json_stream(""), "");
92 ASSERT_EQ(escape_json_attrs("simple examples please!"), "simple examples please!");
93 ASSERT_EQ(escape_json_stream("simple examples please!"), "simple examples please!");
96 TEST(EscapeJson
, Escapes1
) {
97 ASSERT_EQ(escape_json_attrs("The \"scare quotes\""),
98 "The \\\"scare quotes\\\"");
99 ASSERT_EQ(escape_json_stream("The \"scare quotes\""),
100 "The \\\"scare quotes\\\"");
101 ASSERT_EQ(escape_json_attrs("I <3 JSON"), "I <3 JSON");
102 ASSERT_EQ(escape_json_stream("I <3 JSON"), "I <3 JSON");
103 ASSERT_EQ(escape_json_attrs("Some 'single' \"quotes\" here"),
104 "Some 'single' \\\"quotes\\\" here");
105 ASSERT_EQ(escape_json_stream("Some 'single' \"quotes\" here"),
106 "Some 'single' \\\"quotes\\\" here");
107 ASSERT_EQ(escape_json_attrs("tabs\tand\tnewlines\n, oh my"),
108 "tabs\\tand\\tnewlines\\n, oh my");
109 ASSERT_EQ(escape_json_stream("tabs\tand\tnewlines\n, oh my"),
110 "tabs\\tand\\tnewlines\\n, oh my");
113 TEST(EscapeJson
, ControlChars
) {
114 ASSERT_EQ(escape_json_attrs("\x01\x02\x03"), "\\u0001\\u0002\\u0003");
115 ASSERT_EQ(escape_json_stream("\x01\x02\x03"), "\\u0001\\u0002\\u0003");
116 ASSERT_EQ(escape_json_stream("\x00\x02\x03", 3), "\\u0000\\u0002\\u0003");
118 // json can't print binary data!
119 ASSERT_EQ(escape_json_stream("\x00\x7f\xff", 3), "\\u0000\\u007f\xff");
121 ASSERT_EQ(escape_json_attrs("abc\x7f"), "abc\\u007f");
122 ASSERT_EQ(escape_json_stream("abc\x7f"), "abc\\u007f");
125 TEST(EscapeJson
, Utf8
) {
126 EXPECT_EQ(escape_json_attrs("\xe6\xb1\x89\xe5\xad\x97\n"), "\xe6\xb1\x89\xe5\xad\x97\\n");
127 EXPECT_EQ(escape_json_stream("\xe6\xb1\x89\xe5\xad\x97\n"), "\xe6\xb1\x89\xe5\xad\x97\\n");