]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
26 | TEST(EscapeXml, PassThrough) { | |
27 | ASSERT_EQ(escape_xml_attrs("simplicity itself"), "simplicity itself"); | |
28 | ASSERT_EQ(escape_xml_attrs(""), ""); | |
29 | ASSERT_EQ(escape_xml_attrs("simple examples please!"), "simple examples please!"); | |
30 | } | |
31 | ||
32 | TEST(EscapeXml, EntityRefs1) { | |
33 | ASSERT_EQ(escape_xml_attrs("The \"scare quotes\""), "The "scare quotes""); | |
34 | ASSERT_EQ(escape_xml_attrs("I <3 XML"), "I <3 XML"); | |
35 | ASSERT_EQ(escape_xml_attrs("Some 'single' \"quotes\" here"), | |
36 | "Some 'single' "quotes" here"); | |
37 | } | |
38 | ||
39 | TEST(EscapeXml, ControlChars) { | |
40 | uint8_t cc1[] = { 0x01, 0x02, 0x03, 0x0 }; | |
41 | ASSERT_EQ(escape_xml_attrs((char*)cc1), ""); | |
42 | ||
43 | uint8_t cc2[] = { 0x61, 0x62, 0x63, 0x7f, 0x0 }; | |
44 | ASSERT_EQ(escape_xml_attrs((char*)cc2), "abc"); | |
45 | } | |
46 | ||
47 | TEST(EscapeXml, Utf8) { | |
48 | uint8_t cc1[] = { 0xe6, 0xb1, 0x89, 0xe5, 0xad, 0x97, 0x0a, 0x0 }; | |
49 | ASSERT_EQ(escape_xml_attrs((const char*)cc1), (const char*)cc1); | |
50 | ||
51 | uint8_t cc2[] = { 0x3c, 0xe6, 0xb1, 0x89, 0xe5, 0xad, 0x97, 0x3e, 0x0a, 0x0 }; | |
52 | uint8_t cc2_out[] = { 0x26, 0x6c, 0x74, 0x3b, 0xe6, 0xb1, 0x89, 0xe5, | |
53 | 0xad, 0x97, 0x26, 0x67, 0x74, 0x3b, 0x0a, 0x0 }; | |
54 | ASSERT_EQ(escape_xml_attrs((const char*)cc2), (const char*)cc2_out); | |
55 | } | |
56 | ||
57 | static std::string escape_json_attrs(const char *str) | |
58 | { | |
59 | int src_len = strlen(str); | |
60 | int len = escape_json_attr_len(str, src_len); | |
61 | char out[len]; | |
62 | escape_json_attr(str, src_len, out); | |
63 | return out; | |
64 | } | |
65 | ||
66 | TEST(EscapeJson, PassThrough) { | |
67 | ASSERT_EQ(escape_json_attrs("simplicity itself"), "simplicity itself"); | |
68 | ASSERT_EQ(escape_json_attrs(""), ""); | |
69 | ASSERT_EQ(escape_json_attrs("simple examples please!"), "simple examples please!"); | |
70 | } | |
71 | ||
72 | TEST(EscapeJson, Escapes1) { | |
73 | ASSERT_EQ(escape_json_attrs("The \"scare quotes\""), | |
74 | "The \\\"scare quotes\\\""); | |
75 | ASSERT_EQ(escape_json_attrs("I <3 JSON"), "I <3 JSON"); | |
76 | ASSERT_EQ(escape_json_attrs("Some 'single' \"quotes\" here"), | |
77 | "Some 'single' \\\"quotes\\\" here"); | |
78 | ASSERT_EQ(escape_json_attrs("tabs\tand\tnewlines\n, oh my"), | |
79 | "tabs\\tand\\tnewlines\\n, oh my"); | |
80 | } | |
81 | ||
82 | TEST(EscapeJson, ControlChars) { | |
83 | uint8_t cc1[] = { 0x01, 0x02, 0x03, 0x0 }; | |
84 | ASSERT_EQ(escape_json_attrs((char*)cc1), "\\u0001\\u0002\\u0003"); | |
85 | ||
86 | uint8_t cc2[] = { 0x61, 0x62, 0x63, 0x7f, 0x0 }; | |
87 | ASSERT_EQ(escape_json_attrs((char*)cc2), "abc\\u007f"); | |
88 | } | |
89 | ||
90 | TEST(EscapeJson, Utf8) { | |
91 | uint8_t cc1[] = { 0xe6, 0xb1, 0x89, 0xe5, 0xad, 0x97, 0x0a, 0x0 }; | |
92 | ASSERT_EQ(escape_xml_attrs((const char*)cc1), (const char*)cc1); | |
93 | } |