From: Colin Patrick McCabe Date: Mon, 1 Aug 2011 18:14:55 +0000 (-0700) Subject: Formatter.cc: use common/escape.h X-Git-Tag: v0.33~71 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=1d3acd5d550191c2c4dab3d2384df1dfa7b072dd;p=ceph.git Formatter.cc: use common/escape.h * Rename rgw/rgw_escape.h to common/escape.h * Use escape.h in common/Formatter.cc Signed-off-by: Colin McCabe --- diff --git a/src/Makefile.am b/src/Makefile.am index e269d6dab867..06431e36b86b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -320,7 +320,6 @@ my_radosgw_src = \ rgw/rgw_formats.cc \ rgw/rgw_log.cc \ rgw/rgw_multi.cc \ - rgw/rgw_escape.c \ rgw/rgw_env.cc my_radosgw_ldadd = \ @@ -503,11 +502,11 @@ unittest_mime_LDADD = ${UNITTEST_LDADD} $(LIBGLOBAL_LDA) unittest_mime_CXXFLAGS = ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS} check_PROGRAMS += unittest_mime -unittest_rgw_escape_SOURCES = test/rgw_escape.cc rgw/rgw_escape.c -unittest_rgw_escape_LDFLAGS = -pthread ${AM_LDFLAGS} -unittest_rgw_escape_LDADD = ${UNITTEST_LDADD} $(LIBGLOBAL_LDA) -unittest_rgw_escape_CXXFLAGS = ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS} -check_PROGRAMS += unittest_rgw_escape +unittest_escape_SOURCES = test/escape.cc +unittest_escape_LDFLAGS = -pthread ${AM_LDFLAGS} +unittest_escape_LDADD = ${UNITTEST_LDADD} $(LIBGLOBAL_LDA) +unittest_escape_CXXFLAGS = ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS} +check_PROGRAMS += unittest_escape unittest_strtol_SOURCES = test/strtol.cc unittest_strtol_LDFLAGS = -pthread ${AM_LDFLAGS} @@ -705,6 +704,7 @@ libcommon_files = \ common/perf_counters.cc \ common/admin_socket.cc \ common/admin_socket_client.cc \ + common/escape.c \ common/Clock.cc \ common/Timer.cc \ common/Finisher.cc \ @@ -894,6 +894,7 @@ noinst_HEADERS = \ common/compiler_extensions.h\ common/debug.h\ common/dout.h\ + common/escape.h\ common/version.h\ common/hex.h\ common/entity_name.h\ @@ -1217,7 +1218,6 @@ noinst_HEADERS = \ rgw/rgw_rest_os.h\ rgw/rgw_rest_s3.h\ rgw/rgw_user.h\ - rgw/rgw_escape.h\ sample.ceph.conf\ tools/common.h\ tools/gui.h\ diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 44f659667d33..5aaa63d7cf95 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -1,14 +1,28 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ #define LARGE_SIZE 8192 -#include -#include -#include - -#include - #include "assert.h" #include "Formatter.h" +#include "common/escape.h" + +#include +#include +#include +#include +#include // ----------------------- namespace ceph { @@ -52,36 +66,11 @@ void JSONFormatter::print_comma(formatter_stack_entry_d& entry) void JSONFormatter::print_quoted_string(const char *s) { - m_ss << '\"'; - while (*s) { - if (*s == '\\') - m_ss << "\\"; - else if (*s == '\"') - m_ss << "\\\""; - else if (*s < 32) { - // do a few common control characters - switch (*s) { - case '\n': - m_ss << "\\n"; - break; - case '\r': - m_ss << "\\r"; - break; - case '\t': - m_ss << "\\t"; - break; - default: - { - // otherwise... - char s[10]; - sprintf(s, "\\u%04x", (int)*s); - } - } - } else - m_ss << *s; - s++; - } - m_ss << '\"'; + int len = escape_json_attr_len(s); + char *escaped = (char*)malloc(len); + escape_json_attr(s, escaped); + m_ss << '\"' << escaped << '\"'; + free(escaped); } void JSONFormatter::print_name(const char *name) diff --git a/src/common/Formatter.h b/src/common/Formatter.h index 598e86824e9e..fe746370d1b8 100644 --- a/src/common/Formatter.h +++ b/src/common/Formatter.h @@ -1,10 +1,13 @@ #ifndef CEPH_FORMATTER_H #define CEPH_FORMATTER_H +#include +#include +#include #include #include +#include #include -#include namespace ceph { diff --git a/src/common/escape.c b/src/common/escape.c new file mode 100644 index 000000000000..cb4076be151f --- /dev/null +++ b/src/common/escape.c @@ -0,0 +1,207 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "common/escape.h" + +#include +#include + +/* + * Some functions for escaping RGW responses + */ + +/* Static string length */ +#define SSTRL(x) ((sizeof(x)/sizeof(x[0])) - 1) + +#define LESS_THAN_XESCAPE "<" +#define AMPERSAND_XESCAPE "&" +#define GREATER_THAN_XESCAPE ">" +#define SGL_QUOTE_XESCAPE "'" +#define DBL_QUOTE_XESCAPE """ + +int escape_xml_attr_len(const char *buf) +{ + const char *b; + int ret = 0; + for (b = buf; *b; ++b) { + unsigned char c = *b; + switch (c) { + case '<': + ret += SSTRL(LESS_THAN_XESCAPE); + break; + case '&': + ret += SSTRL(AMPERSAND_XESCAPE); + break; + case '>': + ret += SSTRL(GREATER_THAN_XESCAPE); + break; + case '\'': + ret += SSTRL(SGL_QUOTE_XESCAPE); + break; + case '"': + ret += SSTRL(DBL_QUOTE_XESCAPE); + break; + default: + // Escape control characters. + if (((c < 0x20) && (c != 0x09) && (c != 0x0a)) || + (c == 0x7f)) { + ret += 6; + } + else { + ret++; + } + } + } + // leave room for null terminator + ret++; + return ret; +} + +void escape_xml_attr(const char *buf, char *out) +{ + char *o = out; + const char *b; + for (b = buf; *b; ++b) { + unsigned char c = *b; + switch (c) { + case '<': + memcpy(o, LESS_THAN_XESCAPE, SSTRL(LESS_THAN_XESCAPE)); + o += SSTRL(LESS_THAN_XESCAPE); + break; + case '&': + memcpy(o, AMPERSAND_XESCAPE, SSTRL(AMPERSAND_XESCAPE)); + o += SSTRL(AMPERSAND_XESCAPE); + break; + case '>': + memcpy(o, GREATER_THAN_XESCAPE, SSTRL(GREATER_THAN_XESCAPE)); + o += SSTRL(GREATER_THAN_XESCAPE); + break; + case '\'': + memcpy(o, SGL_QUOTE_XESCAPE, SSTRL(SGL_QUOTE_XESCAPE)); + o += SSTRL(SGL_QUOTE_XESCAPE); + break; + case '"': + memcpy(o, DBL_QUOTE_XESCAPE, SSTRL(DBL_QUOTE_XESCAPE)); + o += SSTRL(DBL_QUOTE_XESCAPE); + break; + default: + // Escape control characters. + if (((c < 0x20) && (c != 0x09) && (c != 0x0a)) || + (c == 0x7f)) { + sprintf(o, "&#x%02x;", c); + o += 6; + } + else { + *o++ = c; + } + break; + } + } + // null terminator + *o = '\0'; +} + +#define SGL_QUOTE_JESCAPE "\\'" +#define DBL_QUOTE_JESCAPE "\\\"" +#define BACKSLASH_JESCAPE "\\\\" +#define SLASH_JESCAPE "\\/" +#define TAB_JESCAPE "\\t" +#define NEWLINE_JESCAPE "\\n" + +int escape_json_attr_len(const char *buf) +{ + const char *b; + int ret = 0; + for (b = buf; *b; ++b) { + unsigned char c = *b; + switch (c) { + case '\'': + ret += SSTRL(SGL_QUOTE_JESCAPE); + break; + case '"': + ret += SSTRL(DBL_QUOTE_JESCAPE); + break; + case '\\': + ret += SSTRL(BACKSLASH_JESCAPE); + break; + case '/': + ret += SSTRL(SLASH_JESCAPE); + break; + case '\t': + ret += SSTRL(TAB_JESCAPE); + break; + case '\n': + ret += SSTRL(NEWLINE_JESCAPE); + break; + default: + // Escape control characters. + if ((c < 0x20) || (c == 0x7f)) { + ret += 5; + } + else { + ret++; + } + } + } + // leave room for null terminator + ret++; + return ret; +} + +void escape_json_attr(const char *buf, char *out) +{ + char *o = out; + const char *b; + for (b = buf; *b; ++b) { + unsigned char c = *b; + switch (c) { + case '\'': + memcpy(o, SGL_QUOTE_JESCAPE, SSTRL(SGL_QUOTE_JESCAPE)); + o += SSTRL(SGL_QUOTE_JESCAPE); + break; + case '"': + memcpy(o, DBL_QUOTE_JESCAPE, SSTRL(DBL_QUOTE_JESCAPE)); + o += SSTRL(DBL_QUOTE_JESCAPE); + break; + case '\\': + memcpy(o, BACKSLASH_JESCAPE, SSTRL(BACKSLASH_JESCAPE)); + o += SSTRL(BACKSLASH_JESCAPE); + break; + case '/': + memcpy(o, SLASH_JESCAPE, SSTRL(SLASH_JESCAPE)); + o += SSTRL(SLASH_JESCAPE); + break; + case '\t': + memcpy(o, TAB_JESCAPE, SSTRL(TAB_JESCAPE)); + o += SSTRL(TAB_JESCAPE); + break; + case '\n': + memcpy(o, NEWLINE_JESCAPE, SSTRL(NEWLINE_JESCAPE)); + o += SSTRL(NEWLINE_JESCAPE); + break; + default: + // Escape control characters. + if ((c < 0x20) || (c == 0x7f)) { + sprintf(o, "\\%04x", c); + o += 5; + } + else { + *o++ = c; + } + break; + } + } + // null terminator + *o = '\0'; +} diff --git a/src/common/escape.h b/src/common/escape.h new file mode 100644 index 000000000000..31aa5ac8bafe --- /dev/null +++ b/src/common/escape.h @@ -0,0 +1,52 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#ifndef CEPH_RGW_ESCAPE_H +#define CEPH_RGW_ESCAPE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Returns the length of a buffer that would be needed to escape 'buf' + * as an XML attrribute + */ +int escape_xml_attr_len(const char *buf); + +/* Escapes 'buf' as an XML attribute. Assumes that 'out' is at least long + * enough to fit the output. You can find out the required length by calling + * escape_xml_attr_len first. + */ +void escape_xml_attr(const char *buf, char *out); + +/* Returns the length of a buffer that would be needed to escape 'buf' + * as an JSON attrribute + */ +int escape_json_attr_len(const char *buf); + +/* Escapes 'buf' as an JSON attribute. Assumes that 'out' is at least long + * enough to fit the output. You can find out the required length by calling + * escape_json_attr_len first. + */ +void escape_json_attr(const char *buf, char *out); + +/* Note: we escape control characters. Although the XML spec doesn't actually + * require this, Amazon does it in their XML responses. + */ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/os/FileStore.cc b/src/os/FileStore.cc index d3ddfa26ba5f..005fa359b766 100644 --- a/src/os/FileStore.cc +++ b/src/os/FileStore.cc @@ -12,8 +12,6 @@ * */ - - #include "FileStore.h" #include "common/BackTrace.h" #include "include/types.h" diff --git a/src/rgw/rgw_escape.c b/src/rgw/rgw_escape.c deleted file mode 100644 index aa19720f43e7..000000000000 --- a/src/rgw/rgw_escape.c +++ /dev/null @@ -1,207 +0,0 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- -// vim: ts=8 sw=2 smarttab -/* - * Ceph - scalable distributed file system - * - * Copyright (C) 2011 New Dream Network - * - * This is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License version 2.1, as published by the Free Software - * Foundation. See file COPYING. - * - */ - -#include "rgw/rgw_escape.h" - -#include -#include - -/* - * Some functions for escaping RGW responses - */ - -/* Static string length */ -#define SSTRL(x) ((sizeof(x)/sizeof(x[0])) - 1) - -#define LESS_THAN_XESCAPE "<" -#define AMPERSAND_XESCAPE "&" -#define GREATER_THAN_XESCAPE ">" -#define SGL_QUOTE_XESCAPE "'" -#define DBL_QUOTE_XESCAPE """ - -int escape_xml_attr_len(const char *buf) -{ - const char *b; - int ret = 0; - for (b = buf; *b; ++b) { - unsigned char c = *b; - switch (c) { - case '<': - ret += SSTRL(LESS_THAN_XESCAPE); - break; - case '&': - ret += SSTRL(AMPERSAND_XESCAPE); - break; - case '>': - ret += SSTRL(GREATER_THAN_XESCAPE); - break; - case '\'': - ret += SSTRL(SGL_QUOTE_XESCAPE); - break; - case '"': - ret += SSTRL(DBL_QUOTE_XESCAPE); - break; - default: - // Escape control characters. - if (((c < 0x20) && (c != 0x09) && (c != 0x0a)) || - (c == 0x7f)) { - ret += 6; - } - else { - ret++; - } - } - } - // leave room for null terminator - ret++; - return ret; -} - -void escape_xml_attr(const char *buf, char *out) -{ - char *o = out; - const char *b; - for (b = buf; *b; ++b) { - unsigned char c = *b; - switch (c) { - case '<': - memcpy(o, LESS_THAN_XESCAPE, SSTRL(LESS_THAN_XESCAPE)); - o += SSTRL(LESS_THAN_XESCAPE); - break; - case '&': - memcpy(o, AMPERSAND_XESCAPE, SSTRL(AMPERSAND_XESCAPE)); - o += SSTRL(AMPERSAND_XESCAPE); - break; - case '>': - memcpy(o, GREATER_THAN_XESCAPE, SSTRL(GREATER_THAN_XESCAPE)); - o += SSTRL(GREATER_THAN_XESCAPE); - break; - case '\'': - memcpy(o, SGL_QUOTE_XESCAPE, SSTRL(SGL_QUOTE_XESCAPE)); - o += SSTRL(SGL_QUOTE_XESCAPE); - break; - case '"': - memcpy(o, DBL_QUOTE_XESCAPE, SSTRL(DBL_QUOTE_XESCAPE)); - o += SSTRL(DBL_QUOTE_XESCAPE); - break; - default: - // Escape control characters. - if (((c < 0x20) && (c != 0x09) && (c != 0x0a)) || - (c == 0x7f)) { - sprintf(o, "&#x%02x;", c); - o += 6; - } - else { - *o++ = c; - } - break; - } - } - // null terminator - *o = '\0'; -} - -#define SGL_QUOTE_JESCAPE "\\'" -#define DBL_QUOTE_JESCAPE "\\\"" -#define BACKSLASH_JESCAPE "\\\\" -#define SLASH_JESCAPE "\\/" -#define TAB_JESCAPE "\\t" -#define NEWLINE_JESCAPE "\\n" - -int escape_json_attr_len(const char *buf) -{ - const char *b; - int ret = 0; - for (b = buf; *b; ++b) { - unsigned char c = *b; - switch (c) { - case '\'': - ret += SSTRL(SGL_QUOTE_JESCAPE); - break; - case '"': - ret += SSTRL(DBL_QUOTE_JESCAPE); - break; - case '\\': - ret += SSTRL(BACKSLASH_JESCAPE); - break; - case '/': - ret += SSTRL(SLASH_JESCAPE); - break; - case '\t': - ret += SSTRL(TAB_JESCAPE); - break; - case '\n': - ret += SSTRL(NEWLINE_JESCAPE); - break; - default: - // Escape control characters. - if ((c < 0x20) || (c == 0x7f)) { - ret += 5; - } - else { - ret++; - } - } - } - // leave room for null terminator - ret++; - return ret; -} - -void escape_json_attr(const char *buf, char *out) -{ - char *o = out; - const char *b; - for (b = buf; *b; ++b) { - unsigned char c = *b; - switch (c) { - case '\'': - memcpy(o, SGL_QUOTE_JESCAPE, SSTRL(SGL_QUOTE_JESCAPE)); - o += SSTRL(SGL_QUOTE_JESCAPE); - break; - case '"': - memcpy(o, DBL_QUOTE_JESCAPE, SSTRL(DBL_QUOTE_JESCAPE)); - o += SSTRL(DBL_QUOTE_JESCAPE); - break; - case '\\': - memcpy(o, BACKSLASH_JESCAPE, SSTRL(BACKSLASH_JESCAPE)); - o += SSTRL(BACKSLASH_JESCAPE); - break; - case '/': - memcpy(o, SLASH_JESCAPE, SSTRL(SLASH_JESCAPE)); - o += SSTRL(SLASH_JESCAPE); - break; - case '\t': - memcpy(o, TAB_JESCAPE, SSTRL(TAB_JESCAPE)); - o += SSTRL(TAB_JESCAPE); - break; - case '\n': - memcpy(o, NEWLINE_JESCAPE, SSTRL(NEWLINE_JESCAPE)); - o += SSTRL(NEWLINE_JESCAPE); - break; - default: - // Escape control characters. - if ((c < 0x20) || (c == 0x7f)) { - sprintf(o, "\\%04x", c); - o += 5; - } - else { - *o++ = c; - } - break; - } - } - // null terminator - *o = '\0'; -} diff --git a/src/rgw/rgw_escape.h b/src/rgw/rgw_escape.h deleted file mode 100644 index 31aa5ac8bafe..000000000000 --- a/src/rgw/rgw_escape.h +++ /dev/null @@ -1,52 +0,0 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- -// vim: ts=8 sw=2 smarttab -/* - * Ceph - scalable distributed file system - * - * Copyright (C) 2011 New Dream Network - * - * This is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License version 2.1, as published by the Free Software - * Foundation. See file COPYING. - * - */ - -#ifndef CEPH_RGW_ESCAPE_H -#define CEPH_RGW_ESCAPE_H - -#ifdef __cplusplus -extern "C" { -#endif - -/* Returns the length of a buffer that would be needed to escape 'buf' - * as an XML attrribute - */ -int escape_xml_attr_len(const char *buf); - -/* Escapes 'buf' as an XML attribute. Assumes that 'out' is at least long - * enough to fit the output. You can find out the required length by calling - * escape_xml_attr_len first. - */ -void escape_xml_attr(const char *buf, char *out); - -/* Returns the length of a buffer that would be needed to escape 'buf' - * as an JSON attrribute - */ -int escape_json_attr_len(const char *buf); - -/* Escapes 'buf' as an JSON attribute. Assumes that 'out' is at least long - * enough to fit the output. You can find out the required length by calling - * escape_json_attr_len first. - */ -void escape_json_attr(const char *buf, char *out); - -/* Note: we escape control characters. Although the XML spec doesn't actually - * require this, Amazon does it in their XML responses. - */ - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/rgw/rgw_formats.cc b/src/rgw/rgw_formats.cc index 76befad77482..276425cd8e72 100644 --- a/src/rgw/rgw_formats.cc +++ b/src/rgw/rgw_formats.cc @@ -1,6 +1,20 @@ -#include "rgw_escape.h" -#include "rgw_common.h" -#include "rgw_formats.h" +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "common/escape.h" +#include "rgw/rgw_common.h" +#include "rgw/rgw_formats.h" /* Plain */ void RGWFormatter_Plain::formatter_init() diff --git a/src/test/escape.cc b/src/test/escape.cc new file mode 100644 index 000000000000..f494e9a1866c --- /dev/null +++ b/src/test/escape.cc @@ -0,0 +1,95 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ +#include "common/escape.h" +#include "gtest/gtest.h" +#include + +static std::string escape_xml_attrs(const char *str) +{ + int len = escape_xml_attr_len(str); + char out[len]; + escape_xml_attr(str, out); + return out; +} + +TEST(EscapeXml, PassThrough) { + ASSERT_EQ(escape_xml_attrs("simplicity itself"), "simplicity itself"); + ASSERT_EQ(escape_xml_attrs(""), ""); + ASSERT_EQ(escape_xml_attrs("simple examples please!"), "simple examples please!"); +} + +TEST(EscapeXml, EntityRefs1) { + ASSERT_EQ(escape_xml_attrs("The \"scare quotes\""), "The "scare quotes""); + ASSERT_EQ(escape_xml_attrs("I <3 XML"), "I <3 XML"); + ASSERT_EQ(escape_xml_attrs("Some 'single' \"quotes\" here"), + "Some 'single' "quotes" here"); +} + +TEST(EscapeXml, ControlChars) { + uint8_t cc1[] = { 0x01, 0x02, 0x03, 0x0 }; + ASSERT_EQ(escape_xml_attrs((char*)cc1), ""); + + uint8_t cc2[] = { 0x61, 0x62, 0x63, 0x7f, 0x0 }; + ASSERT_EQ(escape_xml_attrs((char*)cc2), "abc"); +} + +TEST(EscapeXml, Utf8) { + uint8_t cc1[] = { 0xe6, 0xb1, 0x89, 0xe5, 0xad, 0x97, 0x0a, 0x0 }; + ASSERT_EQ(escape_xml_attrs((const char*)cc1), (const char*)cc1); + + uint8_t cc2[] = { 0x3c, 0xe6, 0xb1, 0x89, 0xe5, 0xad, 0x97, 0x3e, 0x0a, 0x0 }; + uint8_t cc2_out[] = { 0x26, 0x6c, 0x74, 0x3b, 0xe6, 0xb1, 0x89, 0xe5, + 0xad, 0x97, 0x26, 0x67, 0x74, 0x3b, 0x0a, 0x0 }; + ASSERT_EQ(escape_xml_attrs((const char*)cc2), (const char*)cc2_out); +} + +static std::string escape_json_attrs(const char *str) +{ + int len = escape_json_attr_len(str); + char out[len]; + escape_json_attr(str, out); + return out; +} + +TEST(EscapeJson, PassThrough) { + ASSERT_EQ(escape_json_attrs("simplicity itself"), "simplicity itself"); + ASSERT_EQ(escape_json_attrs(""), ""); + ASSERT_EQ(escape_json_attrs("simple examples please!"), "simple examples please!"); +} + +TEST(EscapeJson, Escapes1) { + ASSERT_EQ(escape_json_attrs("The \"scare quotes\""), + "The \\\"scare quotes\\\""); + ASSERT_EQ(escape_json_attrs("I <3 JSON"), "I <3 JSON"); + ASSERT_EQ(escape_json_attrs( + "JSON calls a slash / backslash a solidus / reverse solidus"), + "JSON calls a slash \\/ backslash a solidus \\/ reverse solidus"); + ASSERT_EQ(escape_json_attrs("Some 'single' \"quotes\" here"), + "Some \\'single\\' \\\"quotes\\\" here"); + ASSERT_EQ(escape_json_attrs("tabs\tand\tnewlines\n, oh my"), + "tabs\\tand\\tnewlines\\n, oh my"); +} + +TEST(EscapeJson, ControlChars) { + uint8_t cc1[] = { 0x01, 0x02, 0x03, 0x0 }; + ASSERT_EQ(escape_json_attrs((char*)cc1), "\\0001\\0002\\0003"); + + uint8_t cc2[] = { 0x61, 0x62, 0x63, 0x7f, 0x0 }; + ASSERT_EQ(escape_json_attrs((char*)cc2), "abc\\007f"); +} + +TEST(EscapeJson, Utf8) { + uint8_t cc1[] = { 0xe6, 0xb1, 0x89, 0xe5, 0xad, 0x97, 0x0a, 0x0 }; + ASSERT_EQ(escape_xml_attrs((const char*)cc1), (const char*)cc1); +} diff --git a/src/test/rgw_escape.cc b/src/test/rgw_escape.cc deleted file mode 100644 index 16eb26188f11..000000000000 --- a/src/test/rgw_escape.cc +++ /dev/null @@ -1,95 +0,0 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- -// vim: ts=8 sw=2 smarttab -/* - * Ceph - scalable distributed file system - * - * Copyright (C) 2011 New Dream Network - * - * This is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License version 2.1, as published by the Free Software - * Foundation. See file COPYING. - * - */ -#include "rgw/rgw_escape.h" -#include "gtest/gtest.h" -#include - -static std::string escape_xml_attrs(const char *str) -{ - int len = escape_xml_attr_len(str); - char out[len]; - escape_xml_attr(str, out); - return out; -} - -TEST(EscapeXml, PassThrough) { - ASSERT_EQ(escape_xml_attrs("simplicity itself"), "simplicity itself"); - ASSERT_EQ(escape_xml_attrs(""), ""); - ASSERT_EQ(escape_xml_attrs("simple examples please!"), "simple examples please!"); -} - -TEST(EscapeXml, EntityRefs1) { - ASSERT_EQ(escape_xml_attrs("The \"scare quotes\""), "The "scare quotes""); - ASSERT_EQ(escape_xml_attrs("I <3 XML"), "I <3 XML"); - ASSERT_EQ(escape_xml_attrs("Some 'single' \"quotes\" here"), - "Some 'single' "quotes" here"); -} - -TEST(EscapeXml, ControlChars) { - uint8_t cc1[] = { 0x01, 0x02, 0x03, 0x0 }; - ASSERT_EQ(escape_xml_attrs((char*)cc1), ""); - - uint8_t cc2[] = { 0x61, 0x62, 0x63, 0x7f, 0x0 }; - ASSERT_EQ(escape_xml_attrs((char*)cc2), "abc"); -} - -TEST(EscapeXml, Utf8) { - uint8_t cc1[] = { 0xe6, 0xb1, 0x89, 0xe5, 0xad, 0x97, 0x0a, 0x0 }; - ASSERT_EQ(escape_xml_attrs((const char*)cc1), (const char*)cc1); - - uint8_t cc2[] = { 0x3c, 0xe6, 0xb1, 0x89, 0xe5, 0xad, 0x97, 0x3e, 0x0a, 0x0 }; - uint8_t cc2_out[] = { 0x26, 0x6c, 0x74, 0x3b, 0xe6, 0xb1, 0x89, 0xe5, - 0xad, 0x97, 0x26, 0x67, 0x74, 0x3b, 0x0a, 0x0 }; - ASSERT_EQ(escape_xml_attrs((const char*)cc2), (const char*)cc2_out); -} - -static std::string escape_json_attrs(const char *str) -{ - int len = escape_json_attr_len(str); - char out[len]; - escape_json_attr(str, out); - return out; -} - -TEST(EscapeJson, PassThrough) { - ASSERT_EQ(escape_json_attrs("simplicity itself"), "simplicity itself"); - ASSERT_EQ(escape_json_attrs(""), ""); - ASSERT_EQ(escape_json_attrs("simple examples please!"), "simple examples please!"); -} - -TEST(EscapeJson, Escapes1) { - ASSERT_EQ(escape_json_attrs("The \"scare quotes\""), - "The \\\"scare quotes\\\""); - ASSERT_EQ(escape_json_attrs("I <3 JSON"), "I <3 JSON"); - ASSERT_EQ(escape_json_attrs( - "JSON calls a slash / backslash a solidus / reverse solidus"), - "JSON calls a slash \\/ backslash a solidus \\/ reverse solidus"); - ASSERT_EQ(escape_json_attrs("Some 'single' \"quotes\" here"), - "Some \\'single\\' \\\"quotes\\\" here"); - ASSERT_EQ(escape_json_attrs("tabs\tand\tnewlines\n, oh my"), - "tabs\\tand\\tnewlines\\n, oh my"); -} - -TEST(EscapeJson, ControlChars) { - uint8_t cc1[] = { 0x01, 0x02, 0x03, 0x0 }; - ASSERT_EQ(escape_json_attrs((char*)cc1), "\\0001\\0002\\0003"); - - uint8_t cc2[] = { 0x61, 0x62, 0x63, 0x7f, 0x0 }; - ASSERT_EQ(escape_json_attrs((char*)cc2), "abc\\007f"); -} - -TEST(EscapeJson, Utf8) { - uint8_t cc1[] = { 0xe6, 0xb1, 0x89, 0xe5, 0xad, 0x97, 0x0a, 0x0 }; - ASSERT_EQ(escape_xml_attrs((const char*)cc1), (const char*)cc1); -}