1 // Copyright (c) 2014 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/trace_event/traced_value.h"
6 
7 #include <stddef.h>
8 
9 #include <utility>
10 
11 #include "base/strings/string_util.h"
12 #include "base/values.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace base {
16 namespace trace_event {
17 
TEST(TraceEventArgumentTest,FlatDictionary)18 TEST(TraceEventArgumentTest, FlatDictionary) {
19   std::unique_ptr<TracedValue> value(new TracedValue());
20   value->SetBoolean("bool", true);
21   value->SetDouble("double", 0.0);
22   value->SetInteger("int", 2014);
23   value->SetString("string", "string");
24   std::string json = "PREFIX";
25   value->AppendAsTraceFormat(&json);
26   EXPECT_EQ(
27       "PREFIX{\"bool\":true,\"double\":0.0,\"int\":2014,\"string\":\"string\"}",
28       json);
29 }
30 
TEST(TraceEventArgumentTest,NoDotPathExpansion)31 TEST(TraceEventArgumentTest, NoDotPathExpansion) {
32   std::unique_ptr<TracedValue> value(new TracedValue());
33   value->SetBoolean("bo.ol", true);
34   value->SetDouble("doub.le", 0.0);
35   value->SetInteger("in.t", 2014);
36   value->SetString("str.ing", "str.ing");
37   std::string json;
38   value->AppendAsTraceFormat(&json);
39   EXPECT_EQ(
40       "{\"bo.ol\":true,\"doub.le\":0.0,\"in.t\":2014,\"str.ing\":\"str.ing\"}",
41       json);
42 }
43 
TEST(TraceEventArgumentTest,Hierarchy)44 TEST(TraceEventArgumentTest, Hierarchy) {
45   std::unique_ptr<TracedValue> value(new TracedValue());
46   value->BeginArray("a1");
47   value->AppendInteger(1);
48   value->AppendBoolean(true);
49   value->BeginDictionary();
50   value->SetInteger("i2", 3);
51   value->EndDictionary();
52   value->EndArray();
53   value->SetBoolean("b0", true);
54   value->SetDouble("d0", 0.0);
55   value->BeginDictionary("dict1");
56   value->BeginDictionary("dict2");
57   value->SetBoolean("b2", false);
58   value->EndDictionary();
59   value->SetInteger("i1", 2014);
60   value->SetString("s1", "foo");
61   value->EndDictionary();
62   value->SetInteger("i0", 2014);
63   value->SetString("s0", "foo");
64   std::string json;
65   value->AppendAsTraceFormat(&json);
66   EXPECT_EQ(
67       "{\"a1\":[1,true,{\"i2\":3}],\"b0\":true,\"d0\":0.0,\"dict1\":{\"dict2\":"
68       "{\"b2\":false},\"i1\":2014,\"s1\":\"foo\"},\"i0\":2014,\"s0\":"
69       "\"foo\"}",
70       json);
71 }
72 
TEST(TraceEventArgumentTest,LongStrings)73 TEST(TraceEventArgumentTest, LongStrings) {
74   std::string kLongString = "supercalifragilisticexpialidocious";
75   std::string kLongString2 = "0123456789012345678901234567890123456789";
76   char kLongString3[4096];
77   for (size_t i = 0; i < sizeof(kLongString3); ++i)
78     kLongString3[i] = 'a' + (i % 25);
79   kLongString3[sizeof(kLongString3) - 1] = '\0';
80 
81   std::unique_ptr<TracedValue> value(new TracedValue());
82   value->SetString("a", "short");
83   value->SetString("b", kLongString);
84   value->BeginArray("c");
85   value->AppendString(kLongString2);
86   value->AppendString("");
87   value->BeginDictionary();
88   value->SetString("a", kLongString3);
89   value->EndDictionary();
90   value->EndArray();
91 
92   std::string json;
93   value->AppendAsTraceFormat(&json);
94   EXPECT_EQ("{\"a\":\"short\",\"b\":\"" + kLongString + "\",\"c\":[\"" +
95                 kLongString2 + "\",\"\",{\"a\":\"" + kLongString3 + "\"}]}",
96             json);
97 }
98 
TEST(TraceEventArgumentTest,PassTracedValue)99 TEST(TraceEventArgumentTest, PassTracedValue) {
100   auto dict_value = std::make_unique<TracedValue>();
101   dict_value->SetInteger("a", 1);
102 
103   auto nested_dict_value = std::make_unique<TracedValue>();
104   nested_dict_value->SetInteger("b", 2);
105   nested_dict_value->BeginArray("c");
106   nested_dict_value->AppendString("foo");
107   nested_dict_value->EndArray();
108 
109   dict_value->SetValue("e", nested_dict_value.get());
110 
111   // Check the merged result.
112   std::string json;
113   dict_value->AppendAsTraceFormat(&json);
114   EXPECT_EQ("{\"a\":1,\"e\":{\"b\":2,\"c\":[\"foo\"]}}", json);
115 
116   // Check that the passed nestd dict was left unouthced.
117   json = "";
118   nested_dict_value->AppendAsTraceFormat(&json);
119   EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"]}", json);
120 
121   // And that it is still usable.
122   nested_dict_value->SetInteger("f", 3);
123   nested_dict_value->BeginDictionary("g");
124   nested_dict_value->EndDictionary();
125   json = "";
126   nested_dict_value->AppendAsTraceFormat(&json);
127   EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"],\"f\":3,\"g\":{}}", json);
128 }
129 
TEST(TraceEventArgumentTest,NanAndInfinityJSON)130 TEST(TraceEventArgumentTest, NanAndInfinityJSON) {
131   TracedValueJSON value;
132   value.SetDouble("nan", std::nan(""));
133   value.SetDouble("infinity", INFINITY);
134   value.SetDouble("negInfinity", -INFINITY);
135   std::string json;
136   value.AppendAsTraceFormat(&json);
137   EXPECT_EQ(
138       "{\"nan\":\"NaN\",\"infinity\":\"Infinity\","
139       "\"negInfinity\":\"-Infinity\"}",
140       json);
141 
142   std::string formatted_json = value.ToFormattedJSON();
143   // Remove CR and LF to make the result platform-independent.
144   ReplaceChars(formatted_json, "\n\r", "", &formatted_json);
145   EXPECT_EQ(
146       "{"
147       "   \"infinity\": \"Infinity\","
148       "   \"nan\": \"NaN\","
149       "   \"negInfinity\": \"-Infinity\""
150       "}",
151       formatted_json);
152 }
153 
154 }  // namespace trace_event
155 }  // namespace base
156