summaryrefslogtreecommitdiffstats
path: root/base/json/string_escape_unittest.cc
blob: c550ca383f97fb3afdf57edca448576c19fb87f8 (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
96
97
98
99
100
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/json/string_escape.h"
#include "base/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

namespace {

const struct json_narrow_test_data {
  const char* to_escape;
  const char* escaped;
} json_narrow_cases[] = {
  {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
  {"a\b\f\n\r\t\v\1\\.\"z",
      "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
  {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
  {"c<>d", "c\\u003C\\u003Ed"},
};

}  // namespace

TEST(StringEscapeTest, JsonDoubleQuoteNarrow) {
  for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) {
    std::string in = json_narrow_cases[i].to_escape;
    std::string out;
    JsonDoubleQuote(in, false, &out);
    EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out);
  }

  std::string in = json_narrow_cases[0].to_escape;
  std::string out;
  JsonDoubleQuote(in, false, &out);

  // test quoting
  std::string out_quoted;
  JsonDoubleQuote(in, true, &out_quoted);
  EXPECT_EQ(out.length() + 2, out_quoted.length());
  EXPECT_EQ(out_quoted.find(out), 1U);

  // now try with a NULL in the string
  std::string null_prepend = "test";
  null_prepend.push_back(0);
  in = null_prepend + in;
  std::string expected = "test\\u0000";
  expected += json_narrow_cases[0].escaped;
  out.clear();
  JsonDoubleQuote(in, false, &out);
  EXPECT_EQ(expected, out);
}

namespace {

const struct json_wide_test_data {
  const wchar_t* to_escape;
  const char* escaped;
} json_wide_cases[] = {
  {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"},
  {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
  {L"a\b\f\n\r\t\v\1\\.\"z",
      "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
  {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
  {L"c<>d", "c\\u003C\\u003Ed"},
};

}  // namespace

TEST(StringEscapeTest, JsonDoubleQuoteWide) {
  for (size_t i = 0; i < arraysize(json_wide_cases); ++i) {
    std::string out;
    string16 in = WideToUTF16(json_wide_cases[i].to_escape);
    JsonDoubleQuote(in, false, &out);
    EXPECT_EQ(std::string(json_wide_cases[i].escaped), out);
  }

  string16 in = WideToUTF16(json_wide_cases[0].to_escape);
  std::string out;
  JsonDoubleQuote(in, false, &out);

  // test quoting
  std::string out_quoted;
  JsonDoubleQuote(in, true, &out_quoted);
  EXPECT_EQ(out.length() + 2, out_quoted.length());
  EXPECT_EQ(out_quoted.find(out), 1U);

  // now try with a NULL in the string
  string16 null_prepend = WideToUTF16(L"test");
  null_prepend.push_back(0);
  in = null_prepend + in;
  std::string expected = "test\\u0000";
  expected += json_wide_cases[0].escaped;
  out.clear();
  JsonDoubleQuote(in, false, &out);
  EXPECT_EQ(expected, out);
}

}  // namespace base