OLD | NEW |
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 const char* kMetadataWhitelist[] = { |
| 25 "cpu-brand", |
| 26 "network-type", |
| 27 "os-name", |
| 28 "user-agent" |
| 29 }; |
| 30 |
| 31 bool IsMetadataWhitelisted(const std::string& metadata_name) { |
| 32 for (auto key : kMetadataWhitelist) { |
| 33 if (base::MatchPattern(metadata_name, key)) { |
| 34 return true; |
| 35 } |
| 36 } |
| 37 return false; |
| 38 } |
| 39 |
| 40 bool IsTraceEventArgsWhitelisted( |
| 41 const char* category_group_name, |
| 42 const char* event_name, |
| 43 base::trace_event::ArgumentNameFilterPredicate* arg_filter) { |
| 44 if (base::MatchPattern(category_group_name, "benchmark") && |
| 45 base::MatchPattern(event_name, "whitelisted")) { |
| 46 return true; |
| 47 } |
| 48 return false; |
| 49 } |
| 50 |
| 51 } // namespace |
| 52 |
21 class TracingControllerTestEndpoint | 53 class TracingControllerTestEndpoint |
22 : public TracingController::TraceDataEndpoint { | 54 : public TracingController::TraceDataEndpoint { |
23 public: | 55 public: |
24 TracingControllerTestEndpoint( | 56 TracingControllerTestEndpoint( |
25 base::Callback<void(scoped_ptr<const base::DictionaryValue>, | 57 base::Callback<void(scoped_ptr<const base::DictionaryValue>, |
26 base::RefCountedString*)> done_callback) | 58 base::RefCountedString*)> done_callback) |
27 : done_callback_(done_callback) {} | 59 : done_callback_(done_callback) {} |
28 | 60 |
29 void ReceiveTraceChunk(const std::string& chunk) override { | 61 void ReceiveTraceChunk(const std::string& chunk) override { |
30 EXPECT_FALSE(chunk.empty()); | 62 EXPECT_FALSE(chunk.empty()); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 enable_recording_done_callback_count_++; | 116 enable_recording_done_callback_count_++; |
85 quit_callback.Run(); | 117 quit_callback.Run(); |
86 } | 118 } |
87 | 119 |
88 void StopTracingStringDoneCallbackTest( | 120 void StopTracingStringDoneCallbackTest( |
89 base::Closure quit_callback, | 121 base::Closure quit_callback, |
90 scoped_ptr<const base::DictionaryValue> metadata, | 122 scoped_ptr<const base::DictionaryValue> metadata, |
91 base::RefCountedString* data) { | 123 base::RefCountedString* data) { |
92 disable_recording_done_callback_count_++; | 124 disable_recording_done_callback_count_++; |
93 last_metadata_.reset(metadata.release()); | 125 last_metadata_.reset(metadata.release()); |
| 126 last_data_ = data->data(); |
94 EXPECT_TRUE(data->size() > 0); | 127 EXPECT_TRUE(data->size() > 0); |
95 quit_callback.Run(); | 128 quit_callback.Run(); |
96 } | 129 } |
97 | 130 |
98 void StopTracingFileDoneCallbackTest(base::Closure quit_callback, | 131 void StopTracingFileDoneCallbackTest(base::Closure quit_callback, |
99 const base::FilePath& file_path) { | 132 const base::FilePath& file_path) { |
100 disable_recording_done_callback_count_++; | 133 disable_recording_done_callback_count_++; |
101 EXPECT_TRUE(PathExists(file_path)); | 134 EXPECT_TRUE(PathExists(file_path)); |
102 int64 file_size; | 135 int64 file_size; |
103 base::GetFileSize(file_path, &file_size); | 136 base::GetFileSize(file_path, &file_size); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 } | 189 } |
157 | 190 |
158 base::FilePath last_actual_monitoring_file_path() const { | 191 base::FilePath last_actual_monitoring_file_path() const { |
159 return last_actual_monitoring_file_path_; | 192 return last_actual_monitoring_file_path_; |
160 } | 193 } |
161 | 194 |
162 const base::DictionaryValue* last_metadata() const { | 195 const base::DictionaryValue* last_metadata() const { |
163 return last_metadata_.get(); | 196 return last_metadata_.get(); |
164 } | 197 } |
165 | 198 |
| 199 const std::string& last_data() const { |
| 200 return last_data_; |
| 201 } |
| 202 |
166 void TestStartAndStopTracingString() { | 203 void TestStartAndStopTracingString() { |
167 Navigate(shell()); | 204 Navigate(shell()); |
168 | 205 |
169 TracingController* controller = TracingController::GetInstance(); | 206 TracingController* controller = TracingController::GetInstance(); |
170 | 207 |
171 { | 208 { |
172 base::RunLoop run_loop; | 209 base::RunLoop run_loop; |
173 TracingController::StartTracingDoneCallback callback = | 210 TracingController::StartTracingDoneCallback callback = |
174 base::Bind(&TracingControllerTest::StartTracingDoneCallbackTest, | 211 base::Bind(&TracingControllerTest::StartTracingDoneCallbackTest, |
175 base::Unretained(this), | 212 base::Unretained(this), |
(...skipping 13 matching lines...) Expand all Loading... |
189 base::Unretained(this), | 226 base::Unretained(this), |
190 run_loop.QuitClosure()); | 227 run_loop.QuitClosure()); |
191 bool result = controller->StopTracing( | 228 bool result = controller->StopTracing( |
192 TracingController::CreateStringSink(callback)); | 229 TracingController::CreateStringSink(callback)); |
193 ASSERT_TRUE(result); | 230 ASSERT_TRUE(result); |
194 run_loop.Run(); | 231 run_loop.Run(); |
195 EXPECT_EQ(disable_recording_done_callback_count(), 1); | 232 EXPECT_EQ(disable_recording_done_callback_count(), 1); |
196 } | 233 } |
197 } | 234 } |
198 | 235 |
| 236 void TestStartAndStopTracingStringWithFilter() { |
| 237 Navigate(shell()); |
| 238 |
| 239 base::trace_event::TraceLog::GetInstance()->SetArgumentFilterPredicate( |
| 240 base::Bind(&IsTraceEventArgsWhitelisted)); |
| 241 TracingController* controller = TracingController::GetInstance(); |
| 242 |
| 243 { |
| 244 base::RunLoop run_loop; |
| 245 TracingController::StartTracingDoneCallback callback = |
| 246 base::Bind(&TracingControllerTest::StartTracingDoneCallbackTest, |
| 247 base::Unretained(this), |
| 248 run_loop.QuitClosure()); |
| 249 |
| 250 TraceConfig config = TraceConfig(); |
| 251 config.EnableArgumentFilter(); |
| 252 |
| 253 bool result = controller->StartTracing(config, callback); |
| 254 ASSERT_TRUE(result); |
| 255 run_loop.Run(); |
| 256 EXPECT_EQ(enable_recording_done_callback_count(), 1); |
| 257 } |
| 258 |
| 259 { |
| 260 base::RunLoop run_loop; |
| 261 base::Callback<void(scoped_ptr<const base::DictionaryValue>, |
| 262 base::RefCountedString*)> callback = base::Bind( |
| 263 &TracingControllerTest::StopTracingStringDoneCallbackTest, |
| 264 base::Unretained(this), |
| 265 run_loop.QuitClosure()); |
| 266 |
| 267 scoped_refptr<TracingController::TraceDataSink> trace_data_sink = |
| 268 TracingController::CreateStringSink(callback); |
| 269 |
| 270 trace_data_sink->SetMetadataFilterPredicate( |
| 271 base::Bind(&IsMetadataWhitelisted)); |
| 272 base::DictionaryValue metadata; |
| 273 metadata.SetString("not-whitelisted", "this_not_found"); |
| 274 trace_data_sink->AddMetadata(metadata); |
| 275 |
| 276 bool result = controller->StopTracing(trace_data_sink); |
| 277 ASSERT_TRUE(result); |
| 278 run_loop.Run(); |
| 279 EXPECT_EQ(disable_recording_done_callback_count(), 1); |
| 280 } |
| 281 } |
| 282 |
199 void TestStartAndStopTracingCompressed() { | 283 void TestStartAndStopTracingCompressed() { |
200 Navigate(shell()); | 284 Navigate(shell()); |
201 | 285 |
202 TracingController* controller = TracingController::GetInstance(); | 286 TracingController* controller = TracingController::GetInstance(); |
203 | 287 |
204 { | 288 { |
205 base::RunLoop run_loop; | 289 base::RunLoop run_loop; |
206 TracingController::StartTracingDoneCallback callback = | 290 TracingController::StartTracingDoneCallback callback = |
207 base::Bind(&TracingControllerTest::StartTracingDoneCallbackTest, | 291 base::Bind(&TracingControllerTest::StartTracingDoneCallbackTest, |
208 base::Unretained(this), run_loop.QuitClosure()); | 292 base::Unretained(this), run_loop.QuitClosure()); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 private: | 460 private: |
377 int get_categories_done_callback_count_; | 461 int get_categories_done_callback_count_; |
378 int enable_recording_done_callback_count_; | 462 int enable_recording_done_callback_count_; |
379 int disable_recording_done_callback_count_; | 463 int disable_recording_done_callback_count_; |
380 int enable_monitoring_done_callback_count_; | 464 int enable_monitoring_done_callback_count_; |
381 int disable_monitoring_done_callback_count_; | 465 int disable_monitoring_done_callback_count_; |
382 int capture_monitoring_snapshot_done_callback_count_; | 466 int capture_monitoring_snapshot_done_callback_count_; |
383 base::FilePath last_actual_recording_file_path_; | 467 base::FilePath last_actual_recording_file_path_; |
384 base::FilePath last_actual_monitoring_file_path_; | 468 base::FilePath last_actual_monitoring_file_path_; |
385 scoped_ptr<const base::DictionaryValue> last_metadata_; | 469 scoped_ptr<const base::DictionaryValue> last_metadata_; |
| 470 std::string last_data_; |
386 }; | 471 }; |
387 | 472 |
388 IN_PROC_BROWSER_TEST_F(TracingControllerTest, GetCategories) { | 473 IN_PROC_BROWSER_TEST_F(TracingControllerTest, GetCategories) { |
389 Navigate(shell()); | 474 Navigate(shell()); |
390 | 475 |
391 TracingController* controller = TracingController::GetInstance(); | 476 TracingController* controller = TracingController::GetInstance(); |
392 | 477 |
393 base::RunLoop run_loop; | 478 base::RunLoop run_loop; |
394 TracingController::GetCategoriesDoneCallback callback = | 479 TracingController::GetCategoriesDoneCallback callback = |
395 base::Bind(&TracingControllerTest::GetCategoriesDoneCallbackTest, | 480 base::Bind(&TracingControllerTest::GetCategoriesDoneCallbackTest, |
(...skipping 20 matching lines...) Expand all Loading... |
416 last_metadata()->GetString("user-agent", &user_agent); | 501 last_metadata()->GetString("user-agent", &user_agent); |
417 EXPECT_TRUE(user_agent.length() > 0); | 502 EXPECT_TRUE(user_agent.length() > 0); |
418 std::string os_name; | 503 std::string os_name; |
419 last_metadata()->GetString("os-name", &os_name); | 504 last_metadata()->GetString("os-name", &os_name); |
420 EXPECT_TRUE(os_name.length() > 0); | 505 EXPECT_TRUE(os_name.length() > 0); |
421 std::string cpu_brand; | 506 std::string cpu_brand; |
422 last_metadata()->GetString("cpu-brand", &cpu_brand); | 507 last_metadata()->GetString("cpu-brand", &cpu_brand); |
423 EXPECT_TRUE(cpu_brand.length() > 0); | 508 EXPECT_TRUE(cpu_brand.length() > 0); |
424 } | 509 } |
425 | 510 |
| 511 IN_PROC_BROWSER_TEST_F(TracingControllerTest, NotWhitelistedMetadataStripped) { |
| 512 TestStartAndStopTracingStringWithFilter(); |
| 513 // Check that a number of important keys exist in the metadata dictionary. |
| 514 EXPECT_TRUE(last_metadata() != NULL); |
| 515 std::string cpu_brand; |
| 516 last_metadata()->GetString("cpu-brand", &cpu_brand); |
| 517 EXPECT_TRUE(cpu_brand.length() > 0); |
| 518 EXPECT_TRUE(cpu_brand != "__stripped__"); |
| 519 std::string network_type; |
| 520 last_metadata()->GetString("network-type", &network_type); |
| 521 EXPECT_TRUE(network_type.length() > 0); |
| 522 EXPECT_TRUE(network_type != "__stripped__"); |
| 523 std::string os_name; |
| 524 last_metadata()->GetString("os-name", &os_name); |
| 525 EXPECT_TRUE(os_name.length() > 0); |
| 526 EXPECT_TRUE(os_name != "__stripped__"); |
| 527 std::string user_agent; |
| 528 last_metadata()->GetString("user-agent", &user_agent); |
| 529 EXPECT_TRUE(user_agent.length() > 0); |
| 530 EXPECT_TRUE(user_agent != "__stripped__"); |
| 531 |
| 532 // Check that the not whitelisted metadata is stripped. |
| 533 std::string not_whitelisted; |
| 534 last_metadata()->GetString("not-whitelisted", ¬_whitelisted); |
| 535 EXPECT_TRUE(not_whitelisted.length() > 0); |
| 536 EXPECT_TRUE(not_whitelisted == "__stripped__"); |
| 537 |
| 538 // Also check the string data. |
| 539 EXPECT_TRUE(last_data().size() > 0); |
| 540 EXPECT_TRUE(last_data().find("cpu-brand") != std::string::npos); |
| 541 EXPECT_TRUE(last_data().find("network-type") != std::string::npos); |
| 542 EXPECT_TRUE(last_data().find("os-name") != std::string::npos); |
| 543 EXPECT_TRUE(last_data().find("user-agent") != std::string::npos); |
| 544 |
| 545 EXPECT_TRUE(last_data().find("not-whitelisted") != std::string::npos); |
| 546 EXPECT_TRUE(last_data().find("this_not_found") == std::string::npos); |
| 547 } |
| 548 |
426 IN_PROC_BROWSER_TEST_F(TracingControllerTest, | 549 IN_PROC_BROWSER_TEST_F(TracingControllerTest, |
427 EnableAndStopTracingWithFilePath) { | 550 EnableAndStopTracingWithFilePath) { |
428 base::FilePath file_path; | 551 base::FilePath file_path; |
429 base::CreateTemporaryFile(&file_path); | 552 base::CreateTemporaryFile(&file_path); |
430 TestStartAndStopTracingFile(file_path); | 553 TestStartAndStopTracingFile(file_path); |
431 EXPECT_EQ(file_path.value(), last_actual_recording_file_path().value()); | 554 EXPECT_EQ(file_path.value(), last_actual_recording_file_path().value()); |
432 } | 555 } |
433 | 556 |
434 IN_PROC_BROWSER_TEST_F(TracingControllerTest, | 557 IN_PROC_BROWSER_TEST_F(TracingControllerTest, |
435 EnableAndStopTracingWithCompression) { | 558 EnableAndStopTracingWithCompression) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 trace_config, | 614 trace_config, |
492 TracingController::StartMonitoringDoneCallback())); | 615 TracingController::StartMonitoringDoneCallback())); |
493 controller->CaptureMonitoringSnapshot(NULL); | 616 controller->CaptureMonitoringSnapshot(NULL); |
494 base::RunLoop().RunUntilIdle(); | 617 base::RunLoop().RunUntilIdle(); |
495 EXPECT_TRUE(controller->StopMonitoring( | 618 EXPECT_TRUE(controller->StopMonitoring( |
496 TracingController::StopMonitoringDoneCallback())); | 619 TracingController::StopMonitoringDoneCallback())); |
497 base::RunLoop().RunUntilIdle(); | 620 base::RunLoop().RunUntilIdle(); |
498 } | 621 } |
499 | 622 |
500 } // namespace content | 623 } // namespace content |
OLD | NEW |