]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/escape.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / 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
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2011 New Dream Network
7 *
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.
12 *
13 */
14 #include "common/escape.h"
15 #include "gtest/gtest.h"
16 #include <stdint.h>
17
18 static std::string escape_xml_attrs(const char *str)
19 {
20 int len = escape_xml_attr_len(str);
21 char out[len];
22 escape_xml_attr(str, out);
23 return out;
24 }
25 static std::string escape_xml_stream(const char *str)
26 {
27 std::stringstream ss;
28 ss << xml_stream_escaper(str);
29 return ss.str();
30 }
31
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!");
39 }
40
41 TEST(EscapeXml, EntityRefs1) {
42 ASSERT_EQ(escape_xml_attrs("The \"scare quotes\""), "The &quot;scare quotes&quot;");
43 ASSERT_EQ(escape_xml_stream("The \"scare quotes\""), "The &quot;scare quotes&quot;");
44 ASSERT_EQ(escape_xml_attrs("I <3 XML"), "I &lt;3 XML");
45 ASSERT_EQ(escape_xml_stream("I <3 XML"), "I &lt;3 XML");
46 ASSERT_EQ(escape_xml_attrs("Some 'single' \"quotes\" here"),
47 "Some &apos;single&apos; &quot;quotes&quot; here");
48 ASSERT_EQ(escape_xml_stream("Some 'single' \"quotes\" here"),
49 "Some &apos;single&apos; &quot;quotes&quot; here");
50 }
51
52 TEST(EscapeXml, ControlChars) {
53 ASSERT_EQ(escape_xml_attrs("\x01\x02\x03"), "&#x01;&#x02;&#x03;");
54 ASSERT_EQ(escape_xml_stream("\x01\x02\x03"), "&#x01;&#x02;&#x03;");
55
56 ASSERT_EQ(escape_xml_attrs("abc\x7f"), "abc&#x7f;");
57 ASSERT_EQ(escape_xml_stream("abc\x7f"), "abc&#x7f;");
58 }
59
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);
64
65 ASSERT_EQ(escape_xml_attrs("<\xe6\xb1\x89\xe5\xad\x97>\n"), "&lt;\xe6\xb1\x89\xe5\xad\x97&gt;\n");
66 ASSERT_EQ(escape_xml_stream("<\xe6\xb1\x89\xe5\xad\x97>\n"), "&lt;\xe6\xb1\x89\xe5\xad\x97&gt;\n");
67 }
68
69 static std::string escape_json_attrs(const char *str, size_t src_len = 0)
70 {
71 if (!src_len)
72 src_len = strlen(str);
73 int len = escape_json_attr_len(str, src_len);
74 char out[len];
75 escape_json_attr(str, src_len, out);
76 return out;
77 }
78 static std::string escape_json_stream(const char *str, size_t src_len = 0)
79 {
80 if (!src_len)
81 src_len = strlen(str);
82 std::stringstream ss;
83 ss << json_stream_escaper(std::string_view(str, src_len));
84 return ss.str();
85 }
86
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!");
94 }
95
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");
111 }
112
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");
117
118 // json can't print binary data!
119 ASSERT_EQ(escape_json_stream("\x00\x7f\xff", 3), "\\u0000\\u007f\xff");
120
121 ASSERT_EQ(escape_json_attrs("abc\x7f"), "abc\\u007f");
122 ASSERT_EQ(escape_json_stream("abc\x7f"), "abc\\u007f");
123 }
124
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");
128 }