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/string_util.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 };
     22 
     23 }
     24 
     25 TEST(StringEscapeTest, JsonDoubleQuoteNarrow) {
     26   for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) {
     27     std::string in = json_narrow_cases[i].to_escape;
     28     std::string out;
     29     JsonDoubleQuote(in, false, &out);
     30     EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out);
     31   }
     32 
     33   std::string in = json_narrow_cases[0].to_escape;
     34   std::string out;
     35   JsonDoubleQuote(in, false, &out);
     36 
     37   // test quoting
     38   std::string out_quoted;
     39   JsonDoubleQuote(in, true, &out_quoted);
     40   EXPECT_EQ(out.length() + 2, out_quoted.length());
     41   EXPECT_EQ(out_quoted.find(out), 1U);
     42 
     43   // now try with a NULL in the string
     44   std::string null_prepend = "test";
     45   null_prepend.push_back(0);
     46   in = null_prepend + in;
     47   std::string expected = "test\\u0000";
     48   expected += json_narrow_cases[0].escaped;
     49   out.clear();
     50   JsonDoubleQuote(in, false, &out);
     51   EXPECT_EQ(expected, out);
     52 }
     53 
     54 namespace {
     55 
     56 const struct json_wide_test_data {
     57   const wchar_t* to_escape;
     58   const char* escaped;
     59 } json_wide_cases[] = {
     60   {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"},
     61   {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
     62   {L"a\b\f\n\r\t\v\1\\.\"z",
     63       "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
     64   {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
     65 };
     66 
     67 }
     68 
     69 TEST(StringEscapeTest, JsonDoubleQuoteWide) {
     70 
     71   for (size_t i = 0; i < arraysize(json_wide_cases); ++i) {
     72     std::string out;
     73     string16 in = WideToUTF16(json_wide_cases[i].to_escape);
     74     JsonDoubleQuote(in, false, &out);
     75     EXPECT_EQ(std::string(json_wide_cases[i].escaped), out);
     76   }
     77 
     78   string16 in = WideToUTF16(json_wide_cases[0].to_escape);
     79   std::string out;
     80   JsonDoubleQuote(in, false, &out);
     81 
     82   // test quoting
     83   std::string out_quoted;
     84   JsonDoubleQuote(in, true, &out_quoted);
     85   EXPECT_EQ(out.length() + 2, out_quoted.length());
     86   EXPECT_EQ(out_quoted.find(out), 1U);
     87 
     88   // now try with a NULL in the string
     89   string16 null_prepend = WideToUTF16(L"test");
     90   null_prepend.push_back(0);
     91   in = null_prepend + in;
     92   std::string expected = "test\\u0000";
     93   expected += json_wide_cases[0].escaped;
     94   out.clear();
     95   JsonDoubleQuote(in, false, &out);
     96   EXPECT_EQ(expected, out);
     97 }
     98 
     99 }  // namespace base
    100