Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / base / trace_event / trace_config_unittest.cc
blobf01c449e7a411fa1d7fd1cb0d7a156a26e97c2f7
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"
8 namespace base {
9 namespace trace_event {
11 namespace {
13 const char kDefaultTraceConfigString[] =
14 "{"
15 "\"enable_argument_filter\":false,"
16 "\"enable_sampling\":false,"
17 "\"enable_systrace\":false,"
18 "\"excluded_categories\":[\"*Debug\",\"*Test\"],"
19 "\"record_mode\":\"record-until-full\""
20 "}";
22 } // namespace
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());
79 config = TraceConfig(
80 "",
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_);
86 EXPECT_STREQ(
87 "trace-to-console,enable-sampling,enable-systrace,enable-argument-filter",
88 config.ToTraceOptionsString().c_str());
90 config = TraceConfig(
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[] = {
169 "DELAY(",
170 "DELAY(;",
171 "DELAY(;)",
172 "DELAY(test.Delay)",
173 "DELAY(test.Delay;)"
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.
183 TraceConfig tc;
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\"]"
211 "}";
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());
236 // Clear
237 tc.Clear();
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\""
244 "}");
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.
250 TraceConfig tc("");
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\","
306 "\"invalid-delay\","
307 "\"test.Delay2;32\"]"
308 "}";
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) {
319 // Merge
320 TraceConfig tc;
321 TraceConfig tc2("included,-excluded,inc_pattern*,-exc_pattern*", "");
322 tc.Merge(tc2);
323 EXPECT_STREQ("{"
324 "\"enable_argument_filter\":false,"
325 "\"enable_sampling\":false,"
326 "\"enable_systrace\":false,"
327 "\"excluded_categories\":["
328 "\"*Debug\",\"*Test\",\"excluded\",\"exc_pattern*\""
329 "],"
330 "\"record_mode\":\"record-until-full\""
331 "}",
332 tc.ToString().c_str());
334 tc = TraceConfig("DELAY(test.Delay1;16)", "");
335 tc2 = TraceConfig("DELAY(test.Delay2;32)", "");
336 tc.Merge(tc2);
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
344 // to be included.
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(
368 " bad_category "));
369 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
370 " bad_category"));
371 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
372 "bad_category "));
373 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
374 " bad_category"));
375 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
376 "bad_category "));
377 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
378 " bad_category "));
379 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
380 ""));
381 EXPECT_FALSE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
382 "good_category"));
385 } // namespace trace_event
386 } // namespace base