v8/test/cctest/test-traced-value.cc
Alexei Filippov 4157c3b3e4 [tracing] Fix Unicode values in TracedValue
Do not encode single byte characters with \uXXXX.

BUG=chromium:895974

Change-Id: If8e0bdb19d326228a7d3d0c9559d4457c8bfd270
Reviewed-on: https://chromium-review.googlesource.com/c/1292679
Reviewed-by: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Reviewed-by: Ulan Degenbaev <ulan@chromium.org>
Commit-Queue: Alexei Filippov <alph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56835}
2018-10-20 19:44:45 +00:00

140 lines
4.3 KiB
C++

// Copyright 2016 the V8 project 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 "src/tracing/traced-value.h"
#include "test/cctest/cctest.h"
using v8::tracing::TracedValue;
TEST(FlatDictionary) {
auto value = TracedValue::Create();
value->SetInteger("int", 2014);
value->SetDouble("double", 0.0);
value->SetBoolean("bool", true);
value->SetString("string", "string");
std::string json = "PREFIX";
value->AppendAsTraceFormat(&json);
CHECK_EQ(
"PREFIX{\"int\":2014,\"double\":0,\"bool\":true,\"string\":"
"\"string\"}",
json);
}
TEST(NoDotPathExpansion) {
auto value = TracedValue::Create();
value->SetInteger("in.t", 2014);
value->SetDouble("doub.le", -20.25);
value->SetBoolean("bo.ol", true);
value->SetString("str.ing", "str.ing");
std::string json;
value->AppendAsTraceFormat(&json);
CHECK_EQ(
"{\"in.t\":2014,\"doub.le\":-20.25,\"bo.ol\":true,\"str.ing\":\"str."
"ing\"}",
json);
}
TEST(Hierarchy) {
auto value = TracedValue::Create();
value->SetInteger("i0", 2014);
value->BeginDictionary("dict1");
value->SetInteger("i1", 2014);
value->BeginDictionary("dict2");
value->SetBoolean("b2", false);
value->EndDictionary();
value->SetString("s1", "foo");
value->EndDictionary();
value->SetDouble("d0", 0.0);
value->SetDouble("d1", 10.5);
value->SetBoolean("b0", true);
value->BeginArray("a1");
value->AppendInteger(1);
value->AppendBoolean(true);
value->BeginDictionary();
value->SetInteger("i2", 3);
value->EndDictionary();
value->EndArray();
value->SetString("s0", "foo");
value->BeginArray("arr1");
value->BeginDictionary();
value->EndDictionary();
value->BeginArray();
value->EndArray();
value->BeginDictionary();
value->EndDictionary();
value->EndArray();
std::string json;
value->AppendAsTraceFormat(&json);
CHECK_EQ(
"{\"i0\":2014,\"dict1\":{\"i1\":2014,\"dict2\":{\"b2\":false},"
"\"s1\":\"foo\"},\"d0\":0,\"d1\":10.5,\"b0\":true,\"a1\":[1,true,{\"i2\":"
"3}],\"s0\":\"foo\",\"arr1\":[{},[],{}]}",
json);
}
TEST(LongStrings) {
std::string long_string = "supercalifragilisticexpialidocious";
std::string long_string2 = "0123456789012345678901234567890123456789";
char long_string3[4096];
for (size_t i = 0; i < sizeof(long_string3); ++i)
long_string3[i] = static_cast<char>('a' + (i % 26));
long_string3[sizeof(long_string3) - 1] = '\0';
auto value = TracedValue::Create();
value->SetString("a", "short");
value->SetString("b", long_string);
value->BeginArray("c");
value->AppendString(long_string2);
value->AppendString("");
value->BeginDictionary();
value->SetString("a", long_string3);
value->EndDictionary();
value->EndArray();
std::string json;
value->AppendAsTraceFormat(&json);
CHECK_EQ("{\"a\":\"short\",\"b\":\"" + long_string + "\",\"c\":[\"" +
long_string2 + "\",\"\",{\"a\":\"" + long_string3 + "\"}]}",
json);
}
TEST(Escaping) {
const char* string1 = "abc\"\'\\\\x\"y\'z\n\x09\x17";
std::string chars127;
for (int i = 1; i <= 127; ++i) {
chars127 += static_cast<char>(i);
}
auto value = TracedValue::Create();
value->SetString("a", string1);
value->SetString("b", chars127);
std::string json;
value->AppendAsTraceFormat(&json);
// Cannot use the expected value literal directly in CHECK_EQ
// as it fails to process the # character on Windows.
const char* expected =
R"({"a":"abc\"'\\\\x\"y'z\n\t\x17","b":"\x01\x02\x03\x04\x05\x06\x07\x08)"
R"(\t\n\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A)"
R"(\x1B\x1C\x1D\x1E\x1F !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO)"
R"(PQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F"})";
CHECK_EQ(expected, json);
}
TEST(Utf8) {
const char* string1 = "Люблю тебя, Петра творенье";
const char* string2 = "\u2600\u26FF";
auto value = TracedValue::Create();
value->SetString("a", string1);
value->SetString("b", string2);
std::string json;
value->AppendAsTraceFormat(&json);
const char* expected =
"{\"a\":\"\u041b\u044e\u0431\u043b\u044e \u0442\u0435\u0431\u044f, \u041f"
"\u0435\u0442\u0440\u0430 \u0442\u0432\u043e\u0440\u0435\u043d\u044c"
"\u0435\",\"b\":\"\u2600\u2600\u26ff\"}";
CHECK_EQ(expected, json);
}