Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(514)

Side by Side Diff: content/browser/tracing/tracing_controller_browsertest.cc

Issue 1502583003: [Tracing] Add metadata filter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit fix Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file_util.h" 5 #include "base/files/file_util.h"
6 #include "base/memory/ref_counted_memory.h" 6 #include "base/memory/ref_counted_memory.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "base/strings/pattern.h"
8 #include "content/public/browser/browser_thread.h" 9 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/tracing_controller.h" 10 #include "content/public/browser/tracing_controller.h"
10 #include "content/public/test/browser_test_utils.h" 11 #include "content/public/test/browser_test_utils.h"
11 #include "content/public/test/content_browser_test.h" 12 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h" 13 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/shell/browser/shell.h" 14 #include "content/shell/browser/shell.h"
14 15
15 using base::trace_event::RECORD_CONTINUOUSLY; 16 using base::trace_event::RECORD_CONTINUOUSLY;
16 using base::trace_event::RECORD_UNTIL_FULL; 17 using base::trace_event::RECORD_UNTIL_FULL;
17 using base::trace_event::TraceConfig; 18 using base::trace_event::TraceConfig;
18 19
19 namespace content { 20 namespace content {
20 21
22 namespace {
23
24 bool IsTraceEventArgsWhitelisted(
25 const char* category_group_name,
26 const char* event_name,
27 base::trace_event::ArgumentNameFilterPredicate* arg_filter) {
28 if (base::MatchPattern(category_group_name, "benchmark") &&
29 base::MatchPattern(event_name, "whitelisted")) {
30 return true;
31 }
32 return false;
33 }
34
35 } // namespace
36
21 class TracingControllerTestEndpoint 37 class TracingControllerTestEndpoint
22 : public TracingController::TraceDataEndpoint { 38 : public TracingController::TraceDataEndpoint {
23 public: 39 public:
24 TracingControllerTestEndpoint( 40 TracingControllerTestEndpoint(
25 base::Callback<void(scoped_ptr<const base::DictionaryValue>, 41 base::Callback<void(scoped_ptr<const base::DictionaryValue>,
26 base::RefCountedString*)> done_callback) 42 base::RefCountedString*)> done_callback)
27 : done_callback_(done_callback) {} 43 : done_callback_(done_callback) {}
28 44
29 void ReceiveTraceChunk(const std::string& chunk) override { 45 void ReceiveTraceChunk(const std::string& chunk) override {
30 EXPECT_FALSE(chunk.empty()); 46 EXPECT_FALSE(chunk.empty());
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 enable_recording_done_callback_count_++; 100 enable_recording_done_callback_count_++;
85 quit_callback.Run(); 101 quit_callback.Run();
86 } 102 }
87 103
88 void StopTracingStringDoneCallbackTest( 104 void StopTracingStringDoneCallbackTest(
89 base::Closure quit_callback, 105 base::Closure quit_callback,
90 scoped_ptr<const base::DictionaryValue> metadata, 106 scoped_ptr<const base::DictionaryValue> metadata,
91 base::RefCountedString* data) { 107 base::RefCountedString* data) {
92 disable_recording_done_callback_count_++; 108 disable_recording_done_callback_count_++;
93 last_metadata_.reset(metadata.release()); 109 last_metadata_.reset(metadata.release());
110 last_data_ = data->data();
94 EXPECT_TRUE(data->size() > 0); 111 EXPECT_TRUE(data->size() > 0);
95 quit_callback.Run(); 112 quit_callback.Run();
96 } 113 }
97 114
98 void StopTracingFileDoneCallbackTest(base::Closure quit_callback, 115 void StopTracingFileDoneCallbackTest(base::Closure quit_callback,
99 const base::FilePath& file_path) { 116 const base::FilePath& file_path) {
100 disable_recording_done_callback_count_++; 117 disable_recording_done_callback_count_++;
101 EXPECT_TRUE(PathExists(file_path)); 118 EXPECT_TRUE(PathExists(file_path));
102 int64 file_size; 119 int64 file_size;
103 base::GetFileSize(file_path, &file_size); 120 base::GetFileSize(file_path, &file_size);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 } 173 }
157 174
158 base::FilePath last_actual_monitoring_file_path() const { 175 base::FilePath last_actual_monitoring_file_path() const {
159 return last_actual_monitoring_file_path_; 176 return last_actual_monitoring_file_path_;
160 } 177 }
161 178
162 const base::DictionaryValue* last_metadata() const { 179 const base::DictionaryValue* last_metadata() const {
163 return last_metadata_.get(); 180 return last_metadata_.get();
164 } 181 }
165 182
183 const std::string& last_data() const {
184 return last_data_;
185 }
186
166 void TestStartAndStopTracingString() { 187 void TestStartAndStopTracingString() {
167 Navigate(shell()); 188 Navigate(shell());
168 189
169 TracingController* controller = TracingController::GetInstance(); 190 TracingController* controller = TracingController::GetInstance();
170 191
171 { 192 {
172 base::RunLoop run_loop; 193 base::RunLoop run_loop;
173 TracingController::StartTracingDoneCallback callback = 194 TracingController::StartTracingDoneCallback callback =
174 base::Bind(&TracingControllerTest::StartTracingDoneCallbackTest, 195 base::Bind(&TracingControllerTest::StartTracingDoneCallbackTest,
175 base::Unretained(this), 196 base::Unretained(this),
(...skipping 13 matching lines...) Expand all
189 base::Unretained(this), 210 base::Unretained(this),
190 run_loop.QuitClosure()); 211 run_loop.QuitClosure());
191 bool result = controller->StopTracing( 212 bool result = controller->StopTracing(
192 TracingController::CreateStringSink(callback)); 213 TracingController::CreateStringSink(callback));
193 ASSERT_TRUE(result); 214 ASSERT_TRUE(result);
194 run_loop.Run(); 215 run_loop.Run();
195 EXPECT_EQ(disable_recording_done_callback_count(), 1); 216 EXPECT_EQ(disable_recording_done_callback_count(), 1);
196 } 217 }
197 } 218 }
198 219
220 void TestStartAndStopTracingStringWithFilter() {
221 Navigate(shell());
222
223 base::trace_event::TraceLog::GetInstance()->SetArgumentFilterPredicate(
224 base::Bind(&IsTraceEventArgsWhitelisted));
225 TracingController* controller = TracingController::GetInstance();
226
227 {
228 base::RunLoop run_loop;
229 TracingController::StartTracingDoneCallback callback =
230 base::Bind(&TracingControllerTest::StartTracingDoneCallbackTest,
231 base::Unretained(this),
232 run_loop.QuitClosure());
233 TraceConfig config = TraceConfig();
234 config.EnableArgumentFilter();
235 bool result = controller->StartTracing(config, callback);
236 ASSERT_TRUE(result);
237 run_loop.Run();
238 EXPECT_EQ(enable_recording_done_callback_count(), 1);
239 }
240
241 {
242 base::RunLoop run_loop;
243 base::Callback<void(scoped_ptr<const base::DictionaryValue>,
244 base::RefCountedString*)> callback = base::Bind(
245 &TracingControllerTest::StopTracingStringDoneCallbackTest,
246 base::Unretained(this),
247 run_loop.QuitClosure());
248
249 scoped_refptr<TracingController::TraceDataSink> trace_data_sink =
250 TracingController::CreateStringSink(callback);
251 base::DictionaryValue metadata;
252 metadata.SetString("not-whitelisted", "this_not_found");
253 trace_data_sink->AddMetadata(metadata);
254
255 bool result = controller->StopTracing(trace_data_sink);
256 ASSERT_TRUE(result);
257 run_loop.Run();
258 EXPECT_EQ(disable_recording_done_callback_count(), 1);
259 }
260 }
261
199 void TestStartAndStopTracingCompressed() { 262 void TestStartAndStopTracingCompressed() {
200 Navigate(shell()); 263 Navigate(shell());
201 264
202 TracingController* controller = TracingController::GetInstance(); 265 TracingController* controller = TracingController::GetInstance();
203 266
204 { 267 {
205 base::RunLoop run_loop; 268 base::RunLoop run_loop;
206 TracingController::StartTracingDoneCallback callback = 269 TracingController::StartTracingDoneCallback callback =
207 base::Bind(&TracingControllerTest::StartTracingDoneCallbackTest, 270 base::Bind(&TracingControllerTest::StartTracingDoneCallbackTest,
208 base::Unretained(this), run_loop.QuitClosure()); 271 base::Unretained(this), run_loop.QuitClosure());
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 private: 439 private:
377 int get_categories_done_callback_count_; 440 int get_categories_done_callback_count_;
378 int enable_recording_done_callback_count_; 441 int enable_recording_done_callback_count_;
379 int disable_recording_done_callback_count_; 442 int disable_recording_done_callback_count_;
380 int enable_monitoring_done_callback_count_; 443 int enable_monitoring_done_callback_count_;
381 int disable_monitoring_done_callback_count_; 444 int disable_monitoring_done_callback_count_;
382 int capture_monitoring_snapshot_done_callback_count_; 445 int capture_monitoring_snapshot_done_callback_count_;
383 base::FilePath last_actual_recording_file_path_; 446 base::FilePath last_actual_recording_file_path_;
384 base::FilePath last_actual_monitoring_file_path_; 447 base::FilePath last_actual_monitoring_file_path_;
385 scoped_ptr<const base::DictionaryValue> last_metadata_; 448 scoped_ptr<const base::DictionaryValue> last_metadata_;
449 std::string last_data_;
386 }; 450 };
387 451
388 IN_PROC_BROWSER_TEST_F(TracingControllerTest, GetCategories) { 452 IN_PROC_BROWSER_TEST_F(TracingControllerTest, GetCategories) {
389 Navigate(shell()); 453 Navigate(shell());
390 454
391 TracingController* controller = TracingController::GetInstance(); 455 TracingController* controller = TracingController::GetInstance();
392 456
393 base::RunLoop run_loop; 457 base::RunLoop run_loop;
394 TracingController::GetCategoriesDoneCallback callback = 458 TracingController::GetCategoriesDoneCallback callback =
395 base::Bind(&TracingControllerTest::GetCategoriesDoneCallbackTest, 459 base::Bind(&TracingControllerTest::GetCategoriesDoneCallbackTest,
(...skipping 20 matching lines...) Expand all
416 last_metadata()->GetString("user-agent", &user_agent); 480 last_metadata()->GetString("user-agent", &user_agent);
417 EXPECT_TRUE(user_agent.length() > 0); 481 EXPECT_TRUE(user_agent.length() > 0);
418 std::string os_name; 482 std::string os_name;
419 last_metadata()->GetString("os-name", &os_name); 483 last_metadata()->GetString("os-name", &os_name);
420 EXPECT_TRUE(os_name.length() > 0); 484 EXPECT_TRUE(os_name.length() > 0);
421 std::string cpu_brand; 485 std::string cpu_brand;
422 last_metadata()->GetString("cpu-brand", &cpu_brand); 486 last_metadata()->GetString("cpu-brand", &cpu_brand);
423 EXPECT_TRUE(cpu_brand.length() > 0); 487 EXPECT_TRUE(cpu_brand.length() > 0);
424 } 488 }
425 489
490 IN_PROC_BROWSER_TEST_F(TracingControllerTest, NotWhitelistedMetadataStripped) {
491 TestStartAndStopTracingStringWithFilter();
492 // Check that a number of important keys exist in the metadata dictionary.
493 EXPECT_TRUE(last_metadata() != NULL);
494 std::string network_type;
495 last_metadata()->GetString("network-type", &network_type);
496 EXPECT_TRUE(network_type.length() > 0);
497 EXPECT_TRUE(network_type != "__stripped__");
498 std::string user_agent;
499 last_metadata()->GetString("user-agent", &user_agent);
500 EXPECT_TRUE(user_agent.length() > 0);
501 EXPECT_TRUE(user_agent != "__stripped__");
502 std::string os_name;
503 last_metadata()->GetString("os-name", &os_name);
504 EXPECT_TRUE(os_name.length() > 0);
505 EXPECT_TRUE(os_name != "__stripped__");
506 std::string cpu_brand;
507 last_metadata()->GetString("cpu-brand", &cpu_brand);
508 EXPECT_TRUE(cpu_brand.length() > 0);
509 EXPECT_TRUE(cpu_brand != "__stripped__");
510
511 // Check that the not whitelisted metadata is stripped.
512 std::string not_whitelisted;
513 last_metadata()->GetString("not-whitelisted", &not_whitelisted);
514 EXPECT_TRUE(not_whitelisted.length() > 0);
515 EXPECT_TRUE(not_whitelisted == "__stripped__");
516
517 // Also check the string data.
518 EXPECT_TRUE(last_data().size() > 0);
519 EXPECT_TRUE(last_data().find("cpu-brand") != std::string::npos);
520 EXPECT_TRUE(last_data().find("network-type") != std::string::npos);
521 EXPECT_TRUE(last_data().find("os-name") != std::string::npos);
522 EXPECT_TRUE(last_data().find("user-agent") != std::string::npos);
523
524 EXPECT_TRUE(last_data().find("not-whitelisted") != std::string::npos);
525 EXPECT_TRUE(last_data().find("this_not_found") == std::string::npos);
526 }
527
426 IN_PROC_BROWSER_TEST_F(TracingControllerTest, 528 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
427 EnableAndStopTracingWithFilePath) { 529 EnableAndStopTracingWithFilePath) {
428 base::FilePath file_path; 530 base::FilePath file_path;
429 base::CreateTemporaryFile(&file_path); 531 base::CreateTemporaryFile(&file_path);
430 TestStartAndStopTracingFile(file_path); 532 TestStartAndStopTracingFile(file_path);
431 EXPECT_EQ(file_path.value(), last_actual_recording_file_path().value()); 533 EXPECT_EQ(file_path.value(), last_actual_recording_file_path().value());
432 } 534 }
433 535
434 IN_PROC_BROWSER_TEST_F(TracingControllerTest, 536 IN_PROC_BROWSER_TEST_F(TracingControllerTest,
435 EnableAndStopTracingWithCompression) { 537 EnableAndStopTracingWithCompression) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 trace_config, 593 trace_config,
492 TracingController::StartMonitoringDoneCallback())); 594 TracingController::StartMonitoringDoneCallback()));
493 controller->CaptureMonitoringSnapshot(NULL); 595 controller->CaptureMonitoringSnapshot(NULL);
494 base::RunLoop().RunUntilIdle(); 596 base::RunLoop().RunUntilIdle();
495 EXPECT_TRUE(controller->StopMonitoring( 597 EXPECT_TRUE(controller->StopMonitoring(
496 TracingController::StopMonitoringDoneCallback())); 598 TracingController::StopMonitoringDoneCallback()));
497 base::RunLoop().RunUntilIdle(); 599 base::RunLoop().RunUntilIdle();
498 } 600 }
499 601
500 } // namespace content 602 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698