]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/escape.cc
import ceph quincy 17.2.6
[ceph.git] / ceph / src / test / escape.cc
CommitLineData
7c673cae
FG
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
18static 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}
11fdf7f2
TL
25static std::string escape_xml_stream(const char *str)
26{
27 std::stringstream ss;
28 ss << xml_stream_escaper(str);
29 return ss.str();
30}
7c673cae
FG
31
32TEST(EscapeXml, PassThrough) {
33 ASSERT_EQ(escape_xml_attrs("simplicity itself"), "simplicity itself");
11fdf7f2 34 ASSERT_EQ(escape_xml_stream("simplicity itself"), "simplicity itself");
7c673cae 35 ASSERT_EQ(escape_xml_attrs(""), "");
11fdf7f2 36 ASSERT_EQ(escape_xml_stream(""), "");
7c673cae 37 ASSERT_EQ(escape_xml_attrs("simple examples please!"), "simple examples please!");
11fdf7f2 38 ASSERT_EQ(escape_xml_stream("simple examples please!"), "simple examples please!");
7c673cae
FG
39}
40
41TEST(EscapeXml, EntityRefs1) {
42 ASSERT_EQ(escape_xml_attrs("The \"scare quotes\""), "The &quot;scare quotes&quot;");
11fdf7f2 43 ASSERT_EQ(escape_xml_stream("The \"scare quotes\""), "The &quot;scare quotes&quot;");
7c673cae 44 ASSERT_EQ(escape_xml_attrs("I <3 XML"), "I &lt;3 XML");
11fdf7f2 45 ASSERT_EQ(escape_xml_stream("I <3 XML"), "I &lt;3 XML");
7c673cae
FG
46 ASSERT_EQ(escape_xml_attrs("Some 'single' \"quotes\" here"),
47 "Some &apos;single&apos; &quot;quotes&quot; here");
11fdf7f2
TL
48 ASSERT_EQ(escape_xml_stream("Some 'single' \"quotes\" here"),
49 "Some &apos;single&apos; &quot;quotes&quot; here");
7c673cae
FG
50}
51
52TEST(EscapeXml, ControlChars) {
11fdf7f2
TL
53 ASSERT_EQ(escape_xml_attrs("\x01\x02\x03"), "&#x01;&#x02;&#x03;");
54 ASSERT_EQ(escape_xml_stream("\x01\x02\x03"), "&#x01;&#x02;&#x03;");
7c673cae 55
11fdf7f2
TL
56 ASSERT_EQ(escape_xml_attrs("abc\x7f"), "abc&#x7f;");
57 ASSERT_EQ(escape_xml_stream("abc\x7f"), "abc&#x7f;");
7c673cae
FG
58}
59
60TEST(EscapeXml, Utf8) {
11fdf7f2
TL
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);
7c673cae 64
11fdf7f2
TL
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");
7c673cae
FG
67}
68
11fdf7f2 69static std::string escape_json_attrs(const char *str, size_t src_len = 0)
7c673cae 70{
11fdf7f2
TL
71 if (!src_len)
72 src_len = strlen(str);
7c673cae
FG
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}
11fdf7f2
TL
78static 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}
7c673cae
FG
86
87TEST(EscapeJson, PassThrough) {
88 ASSERT_EQ(escape_json_attrs("simplicity itself"), "simplicity itself");
11fdf7f2 89 ASSERT_EQ(escape_json_stream("simplicity itself"), "simplicity itself");
7c673cae 90 ASSERT_EQ(escape_json_attrs(""), "");
11fdf7f2 91 ASSERT_EQ(escape_json_stream(""), "");
7c673cae 92 ASSERT_EQ(escape_json_attrs("simple examples please!"), "simple examples please!");
11fdf7f2 93 ASSERT_EQ(escape_json_stream("simple examples please!"), "simple examples please!");
7c673cae
FG
94}
95
96TEST(EscapeJson, Escapes1) {
97 ASSERT_EQ(escape_json_attrs("The \"scare quotes\""),
98 "The \\\"scare quotes\\\"");
11fdf7f2
TL
99 ASSERT_EQ(escape_json_stream("The \"scare quotes\""),
100 "The \\\"scare quotes\\\"");
7c673cae 101 ASSERT_EQ(escape_json_attrs("I <3 JSON"), "I <3 JSON");
11fdf7f2 102 ASSERT_EQ(escape_json_stream("I <3 JSON"), "I <3 JSON");
7c673cae
FG
103 ASSERT_EQ(escape_json_attrs("Some 'single' \"quotes\" here"),
104 "Some 'single' \\\"quotes\\\" here");
11fdf7f2
TL
105 ASSERT_EQ(escape_json_stream("Some 'single' \"quotes\" here"),
106 "Some 'single' \\\"quotes\\\" here");
7c673cae
FG
107 ASSERT_EQ(escape_json_attrs("tabs\tand\tnewlines\n, oh my"),
108 "tabs\\tand\\tnewlines\\n, oh my");
11fdf7f2
TL
109 ASSERT_EQ(escape_json_stream("tabs\tand\tnewlines\n, oh my"),
110 "tabs\\tand\\tnewlines\\n, oh my");
7c673cae
FG
111}
112
113TEST(EscapeJson, ControlChars) {
11fdf7f2
TL
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");
7c673cae 120
11fdf7f2
TL
121 ASSERT_EQ(escape_json_attrs("abc\x7f"), "abc\\u007f");
122 ASSERT_EQ(escape_json_stream("abc\x7f"), "abc\\u007f");
7c673cae
FG
123}
124
125TEST(EscapeJson, Utf8) {
11fdf7f2
TL
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");
7c673cae 128}