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

Side by Side Diff: chrome/test/base/in_process_browser_test.cc

Issue 2013573007: Catch CSP violations in InProcessBrowserTest Base URL: https://chromium.googlesource.com/a/chromium/src.git@master
Patch Set: whitelist more Created 4 years, 6 months 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/test/base/in_process_browser_test.h" 5 #include "chrome/test/base/in_process_browser_test.h"
6 6
7 #include "ash/ash_switches.h" 7 #include "ash/ash_switches.h"
8 #include "base/auto_reset.h" 8 #include "base/auto_reset.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 #endif 83 #endif
84 84
85 #if defined(USE_ASH) 85 #if defined(USE_ASH)
86 #include "chrome/test/base/default_ash_event_generator_delegate.h" 86 #include "chrome/test/base/default_ash_event_generator_delegate.h"
87 #endif 87 #endif
88 88
89 namespace { 89 namespace {
90 90
91 // Passed as value of kTestType. 91 // Passed as value of kTestType.
92 const char kBrowserTestType[] = "browser"; 92 const char kBrowserTestType[] = "browser";
93 93
Tom Sepez 2016/05/31 16:21:38 nit: |static| redundant within namespace {}
wychen 2016/06/08 05:41:57 Done.
94 static bool seen_csp_error_messages_ = false;
95
96 bool CSPLogHandler(int severity,
Dan Beam 2016/06/07 01:18:30 this will conflict (potentially) with JS-based bro
wychen 2016/06/08 05:41:57 I've rebased this CL on top of https://codereview.
97 const char* file,
98 int line,
99 size_t message_start,
100 const std::string& str) {
101 if (file == NULL || std::string("CONSOLE") != file)
102 return false;
103
104 // Catch CSP violation.
105 bool contains_error =
106 str.find("Content Security Policy") != std::string::npos;
Paweł Hajdan Jr. 2016/05/30 08:43:47 It seems fragile to me to rely on string parsing a
wychen 2016/05/31 08:11:27 It's indeed not very robust in that case. Even tho
Mike West 2016/05/31 09:20:00 What would you like to see added? Sorry, I'm not e
Tom Sepez 2016/05/31 16:21:38 I'm OK so long as we don't silently pass failures
wychen 2016/05/31 18:17:45 This sounds like a good median between current fra
107 if (severity == logging::LOG_INFO && contains_error) {
108 seen_csp_error_messages_ = true;
109 }
110
111 return false;
112 }
113
94 } // namespace 114 } // namespace
95 115
96 // Library used for testing accessibility. 116 // Library used for testing accessibility.
97 const base::FilePath::CharType kAXSTesting[] = 117 const base::FilePath::CharType kAXSTesting[] =
98 FILE_PATH_LITERAL("third_party/accessibility-audit/axs_testing.js"); 118 FILE_PATH_LITERAL("third_party/accessibility-audit/axs_testing.js");
99 // JavaScript snippet to configure and run the accessibility audit. 119 // JavaScript snippet to configure and run the accessibility audit.
100 const char kAccessibilityTestString[] = 120 const char kAccessibilityTestString[] =
101 "var config = new axs.AuditConfiguration();" 121 "var config = new axs.AuditConfiguration();"
102 "/* Disable warning about rules that cannot be checked. */" 122 "/* Disable warning about rules that cannot be checked. */"
103 "config.showUnsupportedRulesWarning = false;" 123 "config.showUnsupportedRulesWarning = false;"
(...skipping 24 matching lines...) Expand all
128 " error = axs.Audit.createReport(result);" 148 " error = axs.Audit.createReport(result);"
129 " break;" 149 " break;"
130 " }" 150 " }"
131 "}" 151 "}"
132 "domAutomationController.send(error);"; 152 "domAutomationController.send(error);";
133 153
134 InProcessBrowserTest::InProcessBrowserTest() 154 InProcessBrowserTest::InProcessBrowserTest()
135 : browser_(NULL), 155 : browser_(NULL),
136 exit_when_last_browser_closes_(true), 156 exit_when_last_browser_closes_(true),
137 open_about_blank_on_browser_launch_(true), 157 open_about_blank_on_browser_launch_(true),
158 ignore_csp_messages_(false),
138 run_accessibility_checks_for_test_case_(false) 159 run_accessibility_checks_for_test_case_(false)
139 #if defined(OS_MACOSX) 160 #if defined(OS_MACOSX)
140 , autorelease_pool_(NULL) 161 , autorelease_pool_(NULL)
141 #endif // OS_MACOSX 162 #endif // OS_MACOSX
142 { 163 {
143 #if defined(OS_MACOSX) 164 #if defined(OS_MACOSX)
144 // TODO(phajdan.jr): Make browser_tests self-contained on Mac, remove this. 165 // TODO(phajdan.jr): Make browser_tests self-contained on Mac, remove this.
145 // Before we run the browser, we have to hack the path to the exe to match 166 // Before we run the browser, we have to hack the path to the exe to match
146 // what it would be if Chrome was running, because it is used to fork renderer 167 // what it would be if Chrome was running, because it is used to fork renderer
147 // processes, on Linux at least (failure to do so will cause a browser_test to 168 // processes, on Linux at least (failure to do so will cause a browser_test to
(...skipping 15 matching lines...) Expand all
163 CHECK(PathService::Override(chrome::DIR_TEST_DATA, 184 CHECK(PathService::Override(chrome::DIR_TEST_DATA,
164 src_dir.AppendASCII("chrome/test/data"))); 185 src_dir.AppendASCII("chrome/test/data")));
165 186
166 #if defined(OS_MACOSX) 187 #if defined(OS_MACOSX)
167 bundle_swizzler_.reset(new ScopedBundleSwizzlerMac); 188 bundle_swizzler_.reset(new ScopedBundleSwizzlerMac);
168 #endif 189 #endif
169 190
170 #if defined(USE_ASH) 191 #if defined(USE_ASH)
171 DefaultAshEventGeneratorDelegate::GetInstance(); 192 DefaultAshEventGeneratorDelegate::GetInstance();
172 #endif 193 #endif
194
195 logging::SetLogMessageHandler(&CSPLogHandler);
196 seen_csp_error_messages_ = false;
173 } 197 }
174 198
175 InProcessBrowserTest::~InProcessBrowserTest() { 199 InProcessBrowserTest::~InProcessBrowserTest() {
200 logging::SetLogMessageHandler(nullptr);
176 } 201 }
177 202
178 void InProcessBrowserTest::SetUp() { 203 void InProcessBrowserTest::SetUp() {
179 // Browser tests will create their own g_browser_process later. 204 // Browser tests will create their own g_browser_process later.
180 DCHECK(!g_browser_process); 205 DCHECK(!g_browser_process);
181 206
182 // Clear the FeatureList instance from base/test/test_suite.cc. Since this is 207 // Clear the FeatureList instance from base/test/test_suite.cc. Since this is
183 // a browser test, a FeatureList will be registered as part of normal browser 208 // a browser test, a FeatureList will be registered as part of normal browser
184 // start up in ChromeBrowserMainParts::SetupMetricsAndFieldTrials(). 209 // start up in ChromeBrowserMainParts::SetupMetricsAndFieldTrials().
185 base::FeatureList::ClearInstanceForTesting(); 210 base::FeatureList::ClearInstanceForTesting();
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 } 362 }
338 } 363 }
339 return test_launcher_utils::OverrideUserDataDir(user_data_dir); 364 return test_launcher_utils::OverrideUserDataDir(user_data_dir);
340 } 365 }
341 366
342 void InProcessBrowserTest::TearDown() { 367 void InProcessBrowserTest::TearDown() {
343 DCHECK(!g_browser_process); 368 DCHECK(!g_browser_process);
344 #if defined(OS_WIN) 369 #if defined(OS_WIN)
345 com_initializer_.reset(); 370 com_initializer_.reset();
346 #endif 371 #endif
372 if (!ignore_csp_messages_) {
373 EXPECT_FALSE(seen_csp_error_messages_);
374 }
347 BrowserTestBase::TearDown(); 375 BrowserTestBase::TearDown();
348 } 376 }
349 377
350 void InProcessBrowserTest::CloseBrowserSynchronously(Browser* browser) { 378 void InProcessBrowserTest::CloseBrowserSynchronously(Browser* browser) {
351 content::WindowedNotificationObserver observer( 379 content::WindowedNotificationObserver observer(
352 chrome::NOTIFICATION_BROWSER_CLOSED, 380 chrome::NOTIFICATION_BROWSER_CLOSED,
353 content::Source<Browser>(browser)); 381 content::Source<Browser>(browser));
354 CloseBrowserAsynchronously(browser); 382 CloseBrowserAsynchronously(browser);
355 observer.Wait(); 383 observer.Wait();
356 } 384 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 int index, 425 int index,
398 const GURL& url, 426 const GURL& url,
399 ui::PageTransition transition) { 427 ui::PageTransition transition) {
400 AddTabAtIndexToBrowser(browser(), index, url, transition, true); 428 AddTabAtIndexToBrowser(browser(), index, url, transition, true);
401 } 429 }
402 430
403 bool InProcessBrowserTest::SetUpUserDataDirectory() { 431 bool InProcessBrowserTest::SetUpUserDataDirectory() {
404 return true; 432 return true;
405 } 433 }
406 434
435 void InProcessBrowserTest::SetIgnoreCSPErrorMessages(bool ignore) {
436 ignore_csp_messages_ = ignore;
437 }
438
407 #if !defined(OS_MACOSX) 439 #if !defined(OS_MACOSX)
408 void InProcessBrowserTest::OpenDevToolsWindow( 440 void InProcessBrowserTest::OpenDevToolsWindow(
409 content::WebContents* web_contents) { 441 content::WebContents* web_contents) {
410 ASSERT_FALSE(content::DevToolsAgentHost::HasFor(web_contents)); 442 ASSERT_FALSE(content::DevToolsAgentHost::HasFor(web_contents));
411 DevToolsWindow::OpenDevToolsWindow(web_contents); 443 DevToolsWindow::OpenDevToolsWindow(web_contents);
412 ASSERT_TRUE(content::DevToolsAgentHost::HasFor(web_contents)); 444 ASSERT_TRUE(content::DevToolsAgentHost::HasFor(web_contents));
413 } 445 }
414 446
415 Browser* InProcessBrowserTest::OpenURLOffTheRecord(Profile* profile, 447 Browser* InProcessBrowserTest::OpenURLOffTheRecord(Profile* profile,
416 const GURL& url) { 448 const GURL& url) {
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 // On the Mac, this eventually reaches 624 // On the Mac, this eventually reaches
593 // -[BrowserWindowController windowWillClose:], which will post a deferred 625 // -[BrowserWindowController windowWillClose:], which will post a deferred
594 // -autorelease on itself to ultimately destroy the Browser object. The line 626 // -autorelease on itself to ultimately destroy the Browser object. The line
595 // below is necessary to pump these pending messages to ensure all Browsers 627 // below is necessary to pump these pending messages to ensure all Browsers
596 // get deleted. 628 // get deleted.
597 content::RunAllPendingInMessageLoop(); 629 content::RunAllPendingInMessageLoop();
598 delete autorelease_pool_; 630 delete autorelease_pool_;
599 autorelease_pool_ = NULL; 631 autorelease_pool_ = NULL;
600 #endif 632 #endif
601 } 633 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698