1 // Copyright (c) 2015 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.
5 #include "base/trace_event/trace_config.h"
6 #include "testing/gtest/include/gtest/gtest.h"
9 namespace trace_event
{
13 const char kDefaultTraceConfigString
[] =
15 "\"enable_argument_filter\":false,"
16 "\"enable_sampling\":false,"
17 "\"enable_systrace\":false,"
18 "\"excluded_categories\":[\"*Debug\",\"*Test\"],"
19 "\"record_mode\":\"record-until-full\""
24 TEST(TraceConfigTest
, TraceConfigFromValidLegacyStrings
) {
25 // From trace options strings
26 TraceConfig
config("", "record-until-full");
27 EXPECT_EQ(RECORD_UNTIL_FULL
, config
.record_mode_
);
28 EXPECT_FALSE(config
.enable_sampling_
);
29 EXPECT_FALSE(config
.enable_systrace_
);
30 EXPECT_FALSE(config
.enable_argument_filter_
);
31 EXPECT_STREQ("record-until-full", config
.ToTraceOptionsString().c_str());
33 config
= TraceConfig("", "record-continuously");
34 EXPECT_EQ(RECORD_CONTINUOUSLY
, config
.record_mode_
);
35 EXPECT_FALSE(config
.enable_sampling_
);
36 EXPECT_FALSE(config
.enable_systrace_
);
37 EXPECT_FALSE(config
.enable_argument_filter_
);
38 EXPECT_STREQ("record-continuously", config
.ToTraceOptionsString().c_str());
40 config
= TraceConfig("", "trace-to-console");
41 EXPECT_EQ(ECHO_TO_CONSOLE
, config
.record_mode_
);
42 EXPECT_FALSE(config
.enable_sampling_
);
43 EXPECT_FALSE(config
.enable_systrace_
);
44 EXPECT_FALSE(config
.enable_argument_filter_
);
45 EXPECT_STREQ("trace-to-console", config
.ToTraceOptionsString().c_str());
47 config
= TraceConfig("", "record-as-much-as-possible");
48 EXPECT_EQ(RECORD_AS_MUCH_AS_POSSIBLE
, config
.record_mode_
);
49 EXPECT_FALSE(config
.enable_sampling_
);
50 EXPECT_FALSE(config
.enable_systrace_
);
51 EXPECT_FALSE(config
.enable_argument_filter_
);
52 EXPECT_STREQ("record-as-much-as-possible",
53 config
.ToTraceOptionsString().c_str());
55 config
= TraceConfig("", "record-until-full, enable-sampling");
56 EXPECT_EQ(RECORD_UNTIL_FULL
, config
.record_mode_
);
57 EXPECT_TRUE(config
.enable_sampling_
);
58 EXPECT_FALSE(config
.enable_systrace_
);
59 EXPECT_FALSE(config
.enable_argument_filter_
);
60 EXPECT_STREQ("record-until-full,enable-sampling",
61 config
.ToTraceOptionsString().c_str());
63 config
= TraceConfig("", "enable-systrace, record-continuously");
64 EXPECT_EQ(RECORD_CONTINUOUSLY
, config
.record_mode_
);
65 EXPECT_FALSE(config
.enable_sampling_
);
66 EXPECT_TRUE(config
.enable_systrace_
);
67 EXPECT_FALSE(config
.enable_argument_filter_
);
68 EXPECT_STREQ("record-continuously,enable-systrace",
69 config
.ToTraceOptionsString().c_str());
71 config
= TraceConfig("", "enable-argument-filter,record-as-much-as-possible");
72 EXPECT_EQ(RECORD_AS_MUCH_AS_POSSIBLE
, config
.record_mode_
);
73 EXPECT_FALSE(config
.enable_sampling_
);
74 EXPECT_FALSE(config
.enable_systrace_
);
75 EXPECT_TRUE(config
.enable_argument_filter_
);
76 EXPECT_STREQ("record-as-much-as-possible,enable-argument-filter",
77 config
.ToTraceOptionsString().c_str());
81 "enable-systrace,trace-to-console,enable-sampling,enable-argument-filter");
82 EXPECT_EQ(ECHO_TO_CONSOLE
, config
.record_mode_
);
83 EXPECT_TRUE(config
.enable_sampling_
);
84 EXPECT_TRUE(config
.enable_systrace_
);
85 EXPECT_TRUE(config
.enable_argument_filter_
);
87 "trace-to-console,enable-sampling,enable-systrace,enable-argument-filter",
88 config
.ToTraceOptionsString().c_str());
91 "", "record-continuously, record-until-full, trace-to-console");
92 EXPECT_EQ(ECHO_TO_CONSOLE
, config
.record_mode_
);
93 EXPECT_FALSE(config
.enable_systrace_
);
94 EXPECT_FALSE(config
.enable_sampling_
);
95 EXPECT_FALSE(config
.enable_argument_filter_
);
96 EXPECT_STREQ("trace-to-console", config
.ToTraceOptionsString().c_str());
98 // From category filter strings
99 config
= TraceConfig("-*Debug,-*Test", "");
100 EXPECT_STREQ("-*Debug,-*Test", config
.ToCategoryFilterString().c_str());
102 config
= TraceConfig("included,-excluded,inc_pattern*,-exc_pattern*", "");
103 EXPECT_STREQ("included,inc_pattern*,-excluded,-exc_pattern*",
104 config
.ToCategoryFilterString().c_str());
106 config
= TraceConfig("only_inc_cat", "");
107 EXPECT_STREQ("only_inc_cat", config
.ToCategoryFilterString().c_str());
109 config
= TraceConfig("-only_exc_cat", "");
110 EXPECT_STREQ("-only_exc_cat", config
.ToCategoryFilterString().c_str());
112 config
= TraceConfig("disabled-by-default-cc,-excluded", "");
113 EXPECT_STREQ("disabled-by-default-cc,-excluded",
114 config
.ToCategoryFilterString().c_str());
116 config
= TraceConfig("disabled-by-default-cc,included", "");
117 EXPECT_STREQ("included,disabled-by-default-cc",
118 config
.ToCategoryFilterString().c_str());
120 config
= TraceConfig("DELAY(test.Delay1;16),included", "");
121 EXPECT_STREQ("included,DELAY(test.Delay1;16)",
122 config
.ToCategoryFilterString().c_str());
124 // From both trace options and category filter strings
125 config
= TraceConfig("", "");
126 EXPECT_EQ(RECORD_UNTIL_FULL
, config
.record_mode_
);
127 EXPECT_FALSE(config
.enable_systrace_
);
128 EXPECT_FALSE(config
.enable_sampling_
);
129 EXPECT_FALSE(config
.enable_argument_filter_
);
130 EXPECT_STREQ("", config
.ToCategoryFilterString().c_str());
131 EXPECT_STREQ("record-until-full", config
.ToTraceOptionsString().c_str());
133 config
= TraceConfig("included,-excluded,inc_pattern*,-exc_pattern*",
134 "enable-systrace, trace-to-console, enable-sampling");
135 EXPECT_STREQ("included,inc_pattern*,-excluded,-exc_pattern*",
136 config
.ToCategoryFilterString().c_str());
137 EXPECT_STREQ("trace-to-console,enable-sampling,enable-systrace",
138 config
.ToTraceOptionsString().c_str());
140 // From both trace options and category filter strings with spaces.
141 config
= TraceConfig(" included , -excluded, inc_pattern*, ,-exc_pattern* ",
142 "enable-systrace, ,trace-to-console, enable-sampling ");
143 EXPECT_STREQ("included,inc_pattern*,-excluded,-exc_pattern*",
144 config
.ToCategoryFilterString().c_str());
145 EXPECT_STREQ("trace-to-console,enable-sampling,enable-systrace",
146 config
.ToTraceOptionsString().c_str());
149 TEST(TraceConfigTest
, TraceConfigFromInvalidLegacyStrings
) {
150 TraceConfig
config("", "foo-bar-baz");
151 EXPECT_EQ(RECORD_UNTIL_FULL
, config
.record_mode_
);
152 EXPECT_FALSE(config
.enable_systrace_
);
153 EXPECT_FALSE(config
.enable_sampling_
);
154 EXPECT_FALSE(config
.enable_argument_filter_
);
155 EXPECT_STREQ("", config
.ToCategoryFilterString().c_str());
156 EXPECT_STREQ("record-until-full", config
.ToTraceOptionsString().c_str());
158 config
= TraceConfig("arbitrary-category", "foo-bar-baz, enable-systrace");
159 EXPECT_EQ(RECORD_UNTIL_FULL
, config
.record_mode_
);
160 EXPECT_TRUE(config
.enable_systrace_
);
161 EXPECT_FALSE(config
.enable_sampling_
);
162 EXPECT_FALSE(config
.enable_argument_filter_
);
163 EXPECT_STREQ("arbitrary-category", config
.ToCategoryFilterString().c_str());
164 EXPECT_STREQ("record-until-full,enable-systrace",
165 config
.ToTraceOptionsString().c_str());
167 const char* const configs
[] = {
175 for (size_t i
= 0; i
< arraysize(configs
); i
++) {
176 TraceConfig
tc(configs
[i
], "");
177 EXPECT_EQ(0u, tc
.GetSyntheticDelayValues().size());
181 TEST(TraceConfigTest
, ConstructDefaultTraceConfig
) {
182 // Make sure that upon an empty string, we fall back to the default config.
184 EXPECT_STREQ(kDefaultTraceConfigString
, tc
.ToString().c_str());
185 EXPECT_TRUE(tc
.record_mode_
== RECORD_UNTIL_FULL
);
186 EXPECT_FALSE(tc
.enable_sampling_
);
187 EXPECT_FALSE(tc
.enable_systrace_
);
188 EXPECT_FALSE(tc
.enable_argument_filter_
);
189 EXPECT_STREQ("-*Debug,-*Test", tc
.ToCategoryFilterString().c_str());
190 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("CategoryDebug"));
191 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("CategoryTest"));
192 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("CategoryDebug,CategoryTest"));
193 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("Category1,CategoryDebug"));
194 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("CategoryDebug,Category1"));
195 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("CategoryTest,Category2"));
196 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("not-excluded-category"));
197 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("disabled-by-default-cc"));
200 TEST(TraceConfigTest
, TraceConfigFromValidString
) {
201 // Using some non-empty config string.
202 const char config_string
[] =
204 "\"enable_argument_filter\":true,"
205 "\"enable_sampling\":true,"
206 "\"enable_systrace\":true,"
207 "\"excluded_categories\":[\"excluded\",\"exc_pattern*\"],"
208 "\"included_categories\":[\"included\",\"inc_pattern*\"],"
209 "\"record_mode\":\"record-continuously\","
210 "\"synthetic_delays\":[\"test.Delay1;16\",\"test.Delay2;32\"]"
212 TraceConfig
tc(config_string
);
213 EXPECT_STREQ(config_string
, tc
.ToString().c_str());
214 EXPECT_TRUE(tc
.record_mode_
== RECORD_CONTINUOUSLY
);
215 EXPECT_TRUE(tc
.enable_sampling_
);
216 EXPECT_TRUE(tc
.enable_systrace_
);
217 EXPECT_TRUE(tc
.enable_argument_filter_
);
218 EXPECT_STREQ("included,inc_pattern*,-excluded,-exc_pattern*,"
219 "DELAY(test.Delay1;16),DELAY(test.Delay2;32)",
220 tc
.ToCategoryFilterString().c_str());
221 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("included"));
222 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("included,excluded"));
223 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("inc_pattern_category"));
224 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("exc_pattern_category"));
225 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("excluded"));
226 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("not-excluded-nor-included"));
227 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("Category1,CategoryDebug"));
228 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("CategoryDebug,Category1"));
229 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("CategoryTest,Category2"));
230 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("included,DELAY(test.Delay1;16)"));
231 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("DELAY(test.Delay1;16)"));
232 EXPECT_EQ(2u, tc
.GetSyntheticDelayValues().size());
233 EXPECT_STREQ("test.Delay1;16", tc
.GetSyntheticDelayValues()[0].c_str());
234 EXPECT_STREQ("test.Delay2;32", tc
.GetSyntheticDelayValues()[1].c_str());
238 EXPECT_STREQ(tc
.ToString().c_str(),
240 "\"enable_argument_filter\":false,"
241 "\"enable_sampling\":false,"
242 "\"enable_systrace\":false,"
243 "\"record_mode\":\"record-until-full\""
247 TEST(TraceConfigTest
, TraceConfigFromInvalidString
) {
248 // The config string needs to be a dictionary correctly formatted as a JSON
249 // string. Otherwise, it will fall back to the default initialization.
251 EXPECT_STREQ(kDefaultTraceConfigString
, tc
.ToString().c_str());
252 EXPECT_TRUE(tc
.record_mode_
== RECORD_UNTIL_FULL
);
253 EXPECT_FALSE(tc
.enable_sampling_
);
254 EXPECT_FALSE(tc
.enable_systrace_
);
255 EXPECT_FALSE(tc
.enable_argument_filter_
);
256 EXPECT_STREQ("-*Debug,-*Test", tc
.ToCategoryFilterString().c_str());
258 tc
= TraceConfig("This is an invalid config string.");
259 EXPECT_STREQ(kDefaultTraceConfigString
, tc
.ToString().c_str());
260 EXPECT_TRUE(tc
.record_mode_
== RECORD_UNTIL_FULL
);
261 EXPECT_FALSE(tc
.enable_sampling_
);
262 EXPECT_FALSE(tc
.enable_systrace_
);
263 EXPECT_FALSE(tc
.enable_argument_filter_
);
264 EXPECT_STREQ("-*Debug,-*Test", tc
.ToCategoryFilterString().c_str());
266 tc
= TraceConfig("[\"This\", \"is\", \"not\", \"a\", \"dictionary\"]");
267 EXPECT_STREQ(kDefaultTraceConfigString
, tc
.ToString().c_str());
268 EXPECT_TRUE(tc
.record_mode_
== RECORD_UNTIL_FULL
);
269 EXPECT_FALSE(tc
.enable_sampling_
);
270 EXPECT_FALSE(tc
.enable_systrace_
);
271 EXPECT_FALSE(tc
.enable_argument_filter_
);
272 EXPECT_STREQ("-*Debug,-*Test", tc
.ToCategoryFilterString().c_str());
274 tc
= TraceConfig("{\"record_mode\": invalid-value-needs-double-quote}");
275 EXPECT_STREQ(kDefaultTraceConfigString
, tc
.ToString().c_str());
276 EXPECT_TRUE(tc
.record_mode_
== RECORD_UNTIL_FULL
);
277 EXPECT_FALSE(tc
.enable_sampling_
);
278 EXPECT_FALSE(tc
.enable_systrace_
);
279 EXPECT_FALSE(tc
.enable_argument_filter_
);
280 EXPECT_STREQ("-*Debug,-*Test", tc
.ToCategoryFilterString().c_str());
282 // If the config string a dictionary formatted as a JSON string, it will
283 // initialize TraceConfig with best effort.
284 tc
= TraceConfig("{}");
285 EXPECT_TRUE(tc
.record_mode_
== RECORD_UNTIL_FULL
);
286 EXPECT_FALSE(tc
.enable_sampling_
);
287 EXPECT_FALSE(tc
.enable_systrace_
);
288 EXPECT_FALSE(tc
.enable_argument_filter_
);
289 EXPECT_STREQ("", tc
.ToCategoryFilterString().c_str());
291 tc
= TraceConfig("{\"arbitrary-key\":\"arbitrary-value\"}");
292 EXPECT_TRUE(tc
.record_mode_
== RECORD_UNTIL_FULL
);
293 EXPECT_FALSE(tc
.enable_sampling_
);
294 EXPECT_FALSE(tc
.enable_systrace_
);
295 EXPECT_FALSE(tc
.enable_argument_filter_
);
296 EXPECT_STREQ("", tc
.ToCategoryFilterString().c_str());
298 const char invalid_config_string
[] =
300 "\"enable_sampling\":\"true\","
301 "\"enable_systrace\":1,"
302 "\"excluded_categories\":[\"excluded\"],"
303 "\"included_categories\":\"not a list\","
304 "\"record_mode\":\"arbitrary-mode\","
305 "\"synthetic_delays\":[\"test.Delay1;16\","
307 "\"test.Delay2;32\"]"
309 tc
= TraceConfig(invalid_config_string
);
310 EXPECT_TRUE(tc
.record_mode_
== RECORD_UNTIL_FULL
);
311 EXPECT_FALSE(tc
.enable_sampling_
);
312 EXPECT_FALSE(tc
.enable_systrace_
);
313 EXPECT_FALSE(tc
.enable_argument_filter_
);
314 EXPECT_STREQ("-excluded,DELAY(test.Delay1;16),DELAY(test.Delay2;32)",
315 tc
.ToCategoryFilterString().c_str());
318 TEST(TraceConfigTest
, MergingTraceConfigs
) {
321 TraceConfig
tc2("included,-excluded,inc_pattern*,-exc_pattern*", "");
324 "\"enable_argument_filter\":false,"
325 "\"enable_sampling\":false,"
326 "\"enable_systrace\":false,"
327 "\"excluded_categories\":["
328 "\"*Debug\",\"*Test\",\"excluded\",\"exc_pattern*\""
330 "\"record_mode\":\"record-until-full\""
332 tc
.ToString().c_str());
334 tc
= TraceConfig("DELAY(test.Delay1;16)", "");
335 tc2
= TraceConfig("DELAY(test.Delay2;32)", "");
337 EXPECT_EQ(2u, tc
.GetSyntheticDelayValues().size());
338 EXPECT_STREQ("test.Delay1;16", tc
.GetSyntheticDelayValues()[0].c_str());
339 EXPECT_STREQ("test.Delay2;32", tc
.GetSyntheticDelayValues()[1].c_str());
342 TEST(TraceConfigTest
, IsCategoryGroupEnabled
) {
343 // Enabling a disabled- category does not require all categories to be traced
345 TraceConfig
tc("disabled-by-default-cc,-excluded", "");
346 EXPECT_STREQ("disabled-by-default-cc,-excluded",
347 tc
.ToCategoryFilterString().c_str());
348 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("disabled-by-default-cc"));
349 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("some_other_group"));
350 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("excluded"));
352 // Enabled a disabled- category and also including makes all categories to
353 // be traced require including.
354 tc
= TraceConfig("disabled-by-default-cc,included", "");
355 EXPECT_STREQ("included,disabled-by-default-cc",
356 tc
.ToCategoryFilterString().c_str());
357 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("disabled-by-default-cc"));
358 EXPECT_TRUE(tc
.IsCategoryGroupEnabled("included"));
359 EXPECT_FALSE(tc
.IsCategoryGroupEnabled("other_included"));
362 TEST(TraceConfigTest
, IsEmptyOrContainsLeadingOrTrailingWhitespace
) {
363 // Test that IsEmptyOrContainsLeadingOrTrailingWhitespace actually catches
364 // categories that are explicitly forbidden.
365 // This method is called in a DCHECK to assert that we don't have these types
366 // of strings as categories.
367 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
369 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
371 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
373 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
375 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
377 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
379 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
381 EXPECT_FALSE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
385 } // namespace trace_event