summaryrefslogtreecommitdiff
path: root/src/test/escape.cc
blob: cf09a5a25644773a12d2522101fc9626070761f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 <stdint.h>

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 &quot;scare quotes&quot;");
  ASSERT_EQ(escape_xml_attrs("I <3 XML"), "I &lt;3 XML");
  ASSERT_EQ(escape_xml_attrs("Some 'single' \"quotes\" here"),
	    "Some &apos;single&apos; &quot;quotes&quot; here");
}

TEST(EscapeXml, ControlChars) {
  uint8_t cc1[] = { 0x01, 0x02, 0x03, 0x0 };
  ASSERT_EQ(escape_xml_attrs((char*)cc1), "&#x01;&#x02;&#x03;");

  uint8_t cc2[] = { 0x61, 0x62, 0x63, 0x7f, 0x0 };
  ASSERT_EQ(escape_xml_attrs((char*)cc2), "abc&#x7f;");
}

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), "\\u0001\\u0002\\u0003");

  uint8_t cc2[] = { 0x61, 0x62, 0x63, 0x7f, 0x0 };
  ASSERT_EQ(escape_json_attrs((char*)cc2), "abc\\u007f");
}

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);
}