summaryrefslogtreecommitdiffstats
path: root/base/string_escape_unittest.cc
blob: 28be9bd851001daeb4f19b7f1016436c032cac36 (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
// 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 "testing/gtest/include/gtest/gtest.h"
#include "base/string_escape.h"

TEST(StringEscapeTest, JavascriptDoubleQuote) {
  static const char* kToEscape          = "\b\001aZ\"\\wee";
  static const char* kEscaped           = "\\b\\x01aZ\\\"\\\\wee";
  static const char* kEscapedQuoted     = "\"\\b\\x01aZ\\\"\\\\wee\"";
  static const wchar_t* kUToEscape      = L"\b\x0001" L"a\x123fZ\"\\wee";
  static const char* kUEscaped          = "\\b\\x01a\\u123FZ\\\"\\\\wee";
  static const char* kUEscapedQuoted    = "\"\\b\\x01a\\u123FZ\\\"\\\\wee\"";

  std::string out;

  // Test wide unicode escaping
  out = "testy: ";
  string_escape::JavascriptDoubleQuote(std::wstring(kUToEscape), false, &out);
  ASSERT_EQ(std::string("testy: ") + kUEscaped, out);

  out = "testy: ";
  string_escape::JavascriptDoubleQuote(std::wstring(kUToEscape), true, &out);
  ASSERT_EQ(std::string("testy: ") + kUEscapedQuoted, out);

  // Test null and high bit / negative unicode values
  std::wstring wstr(L"TeSt");
  wstr.push_back(0);
  wstr.push_back(0xffb1);
  wstr.push_back(0x00ff);

  out = "testy: ";
  string_escape::JavascriptDoubleQuote(wstr, false, &out);
  ASSERT_EQ("testy: TeSt\\x00\\uFFB1\\xFF", out);

  // Test escaping of 7bit ascii
  out = "testy: ";
  string_escape::JavascriptDoubleQuote(std::string(kToEscape), false, &out);
  ASSERT_EQ(std::string("testy: ") + kEscaped, out);

  out = "testy: ";
  string_escape::JavascriptDoubleQuote(std::string(kToEscape), true, &out);
  ASSERT_EQ(std::string("testy: ") + kEscapedQuoted, out);

  // Test null, non-printable, and non-7bit
  std::string str("TeSt");
  str.push_back(0);
  str.push_back(15);
  str.push_back(127);
  str.push_back(-16);
  str.push_back(-128);
  str.push_back('!');

  out = "testy: ";
  string_escape::JavascriptDoubleQuote(str, false, &out);
  ASSERT_EQ("testy: TeSt\\x00\\x0F\\x7F\xf0\x80!", out);

  // Test escape sequences
  out = "testy: ";
  string_escape::JavascriptDoubleQuote("a\b\f\n\r\t\v\1\\.\"z", false, &out);
  ASSERT_EQ("testy: a\\b\\f\\n\\r\\t\\v\\x01\\\\.\\\"z", out);
}