Add enterprise policy for SSL error overriding
[chromium-blink-merge.git] / chrome / browser / interstitials / security_interstitial_page_test_utils.cc
blob5c224c3e27c7138ba7e9c133c5c79f3bc0c924ed
1 // Copyright 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 "chrome/browser/interstitials/security_interstitial_page_test_utils.h"
7 #include <string>
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/interstitials/security_interstitial_page.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/pref_names.h"
16 #include "content/public/browser/interstitial_page.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h"
22 namespace chrome_browser_interstitials {
24 bool IsInterstitialDisplayingText(
25 const content::InterstitialPage* const interstitial,
26 const std::string& text) {
27 // It's valid for |text| to contain "\'", but simply look for "'" instead
28 // since this function is used for searching host names and a predefined
29 // string.
30 DCHECK(text.find("\'") == std::string::npos);
31 std::string command = base::StringPrintf(
32 "var hasText = document.body.textContent.indexOf('%s') >= 0;"
33 "window.domAutomationController.send(hasText ? %d : %d);",
34 text.c_str(), SecurityInterstitialPage::CMD_TEXT_FOUND,
35 SecurityInterstitialPage::CMD_TEXT_NOT_FOUND);
36 int result = 0;
37 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(interstitial->GetMainFrame(),
38 command, &result));
39 return result == SecurityInterstitialPage::CMD_TEXT_FOUND;
42 void SecurityInterstitialIDNTest::SetUpOnMainThread() {
43 // Clear AcceptLanguages to force punycode decoding.
44 browser()->profile()->GetPrefs()->SetString(prefs::kAcceptLanguages,
45 std::string());
48 testing::AssertionResult SecurityInterstitialIDNTest::VerifyIDNDecoded() const {
49 const char kHostname[] = "xn--d1abbgf6aiiy.xn--p1ai";
50 const char kHostnameJSUnicode[] =
51 "\\u043f\\u0440\\u0435\\u0437\\u0438\\u0434\\u0435\\u043d\\u0442."
52 "\\u0440\\u0444";
53 std::string request_url_spec = base::StringPrintf("https://%s/", kHostname);
54 GURL request_url(request_url_spec);
56 content::WebContents* contents =
57 browser()->tab_strip_model()->GetActiveWebContents();
58 DCHECK(contents);
59 SecurityInterstitialPage* blocking_page =
60 CreateInterstitial(contents, request_url);
61 blocking_page->Show();
63 WaitForInterstitialAttach(contents);
64 if (!WaitForRenderFrameReady(contents->GetInterstitialPage()->GetMainFrame()))
65 return testing::AssertionFailure() << "Render frame not ready";
67 if (IsInterstitialDisplayingText(contents->GetInterstitialPage(),
68 kHostnameJSUnicode)) {
69 return testing::AssertionSuccess();
71 return testing::AssertionFailure() << "Interstitial not displaying text";
73 } // namespace chrome_browser_interstitials