Home | History | Annotate | Download | only in json
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/json/string_escape.h"
      6 #include "base/utf_string_conversions.h"
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace base {
     10 
     11 namespace {
     12 
     13 const struct json_narrow_test_data {
     14   const char* to_escape;
     15   const char* escaped;
     16 } json_narrow_cases[] = {
     17   {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
     18   {"a\b\f\n\r\t\v\1\\.\"z",
     19       "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
     20   {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
     21   {"c<>d", "c\\u003C\\u003Ed"},
     22 };
     23 
     24 }  // namespace
     25 
     26 TEST(StringEscapeTest, JsonDoubleQuoteNarrow) {
     27   for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) {
     28     std::string in = json_narrow_cases[i].to_escape;
     29     std::string out;
     30     JsonDoubleQuote(in, false, &out);
     31     EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out);
     32   }
     33 
     34   std::string in = json_narrow_cases[0].to_escape;
     35   std::string out;
     36   JsonDoubleQuote(in, false, &out);
     37 
     38   // test quoting
     39   std::string out_quoted;
     40   JsonDoubleQuote(in, true, &out_quoted);
     41   EXPECT_EQ(out.length() + 2, out_quoted.length());
     42   EXPECT_EQ(out_quoted.find(out), 1U);
     43 
     44   // now try with a NULL in the string
     45   std::string null_prepend = "test";
     46   null_prepend.push_back(0);
     47   in = null_prepend + in;
     48   std::string expected = "test\\u0000";
     49   expected += json_narrow_cases[0].escaped;
     50   out.clear();
     51   JsonDoubleQuote(in, false, &out);
     52   EXPECT_EQ(expected, out);
     53 }
     54 
     55 namespace {
     56 
     57 const struct json_wide_test_data {
     58   const wchar_t* to_escape;
     59   const char* escaped;
     60 } json_wide_cases[] = {
     61   {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"},
     62   {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
     63   {L"a\b\f\n\r\t\v\1\\.\"z",
     64       "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
     65   {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
     66   {L"c<>d", "c\\u003C\\u003Ed"},
     67 };
     68 
     69 }  // namespace
     70 
     71 TEST(StringEscapeTest, JsonDoubleQuoteWide) {
     72   for (size_t i = 0; i < arraysize(json_wide_cases); ++i) {
     73     std::string out;
     74     string16 in = WideToUTF16(json_wide_cases[i].to_escape);
     75     JsonDoubleQuote(in, false, &out);
     76     EXPECT_EQ(std::string(json_wide_cases[i].escaped), out);
     77   }
     78 
     79   string16 in = WideToUTF16(json_wide_cases[0].to_escape);
     80   std::string out;
     81   JsonDoubleQuote(in, false, &out);
     82 
     83   // test quoting
     84   std::string out_quoted;
     85   JsonDoubleQuote(in, true, &out_quoted);
     86   EXPECT_EQ(out.length() + 2, out_quoted.length());
     87   EXPECT_EQ(out_quoted.find(out), 1U);
     88 
     89   // now try with a NULL in the string
     90   string16 null_prepend = WideToUTF16(L"test");
     91   null_prepend.push_back(0);
     92   in = null_prepend + in;
     93   std::string expected = "test\\u0000";
     94   expected += json_wide_cases[0].escaped;
     95   out.clear();
     96   JsonDoubleQuote(in, false, &out);
     97   EXPECT_EQ(expected, out);
     98 }
     99 
    100 }  // namespace base
    101