certificate_manager_browsertest.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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
3// found in the LICENSE file.
4
5#include "base/callback.h"
6#include "base/values.h"
7#include "chrome/browser/browser_process.h"
8#include "chrome/browser/policy/browser_policy_connector.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/tabs/tab_strip_model.h"
11#include "chrome/browser/ui/webui/options/options_ui_browsertest.h"
12#include "components/policy/core/common/external_data_fetcher.h"
13#include "components/policy/core/common/mock_configuration_policy_provider.h"
14#include "components/policy/core/common/policy_map.h"
15#include "components/policy/core/common/policy_types.h"
16#include "content/public/browser/web_contents.h"
17#include "content/public/test/browser_test_utils.h"
18#include "content/public/test/test_utils.h"
19#include "policy/policy_constants.h"
20#include "testing/gmock/include/gmock/gmock.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23#if defined(OS_CHROMEOS)
24#include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
25#include "chrome/browser/chromeos/policy/network_configuration_updater.h"
26#include "chromeos/network/onc/onc_test_utils.h"
27#include "crypto/nss_util.h"
28#endif
29
30using testing::Return;
31using testing::_;
32
33class CertificateManagerBrowserTest : public options::OptionsUIBrowserTest {
34 public:
35  CertificateManagerBrowserTest() {}
36  virtual ~CertificateManagerBrowserTest() {}
37
38 protected:
39  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
40#if defined(OS_CHROMEOS)
41    device_policy_test_helper_.MarkAsEnterpriseOwned();
42#endif
43    // Setup the policy provider for injecting certs through ONC policy.
44    EXPECT_CALL(provider_, IsInitializationComplete(_))
45        .WillRepeatedly(Return(true));
46    policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
47  }
48
49  void SetUpOnIOThread() {
50#if defined(OS_CHROMEOS)
51    test_nssdb_.reset(new crypto::ScopedTestNSSDB());
52#endif
53  }
54
55  void TearDownOnIOThread() {
56#if defined(OS_CHROMEOS)
57    test_nssdb_.reset();
58#endif
59  }
60
61  virtual void SetUpOnMainThread() OVERRIDE {
62    content::BrowserThread::PostTask(
63        content::BrowserThread::IO,
64        FROM_HERE,
65        base::Bind(&CertificateManagerBrowserTest::SetUpOnIOThread, this));
66    content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
67
68    content::RunAllPendingInMessageLoop();
69  }
70
71  virtual void CleanUpOnMainThread() OVERRIDE {
72    content::BrowserThread::PostTask(
73        content::BrowserThread::IO,
74        FROM_HERE,
75        base::Bind(&CertificateManagerBrowserTest::TearDownOnIOThread, this));
76    content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
77  }
78
79#if defined(OS_CHROMEOS)
80  void LoadONCPolicy(const std::string& filename) {
81    const std::string& user_policy_blob =
82        chromeos::onc::test_utils::ReadTestData(filename);
83    policy::PolicyMap policy;
84    policy.Set(policy::key::kOpenNetworkConfiguration,
85               policy::POLICY_LEVEL_MANDATORY,
86               policy::POLICY_SCOPE_USER,
87               Value::CreateStringValue(user_policy_blob),
88               NULL);
89    provider_.UpdateChromePolicy(policy);
90    content::RunAllPendingInMessageLoop();
91  }
92#endif
93
94  void ClickElement(const std::string& selector) {
95    EXPECT_TRUE(content::ExecuteScriptInFrame(
96        browser()->tab_strip_model()->GetActiveWebContents(),
97        "//div[@id='settings']/iframe",
98        "document.querySelector(\"" + selector + "\").click()"));
99  }
100
101  bool HasElement(const std::string& selector) {
102    bool result;
103    EXPECT_TRUE(content::ExecuteScriptInFrameAndExtractBool(
104        browser()->tab_strip_model()->GetActiveWebContents(),
105        "//div[@id='settings']/iframe",
106        "window.domAutomationController.send("
107        "    !!document.querySelector('" + selector + "'));",
108        &result));
109    return result;
110  }
111
112  policy::MockConfigurationPolicyProvider provider_;
113#if defined(OS_CHROMEOS)
114  policy::DevicePolicyCrosTestHelper device_policy_test_helper_;
115  scoped_ptr<crypto::ScopedTestNSSDB> test_nssdb_;
116#endif
117};
118
119#if defined(OS_CHROMEOS)
120// Ensure policy-installed certificates without web trust do not display
121// the managed setting indicator (only on Chrome OS).
122IN_PROC_BROWSER_TEST_F(CertificateManagerBrowserTest,
123                       PolicyCertificateWithoutWebTrustHasNoIndicator) {
124  LoadONCPolicy("certificate-authority.onc");
125  NavigateToSettings();
126  ClickElement("#certificatesManageButton");
127  ClickElement("#ca-certs-nav-tab");
128  EXPECT_FALSE(HasElement(".cert-policy"));
129}
130#endif
131
132#if defined(OS_CHROMEOS)
133// Ensure policy-installed certificates with web trust display the
134// managed setting indicator (only on Chrome OS).
135IN_PROC_BROWSER_TEST_F(CertificateManagerBrowserTest,
136                       PolicyCertificateWithWebTrustHasIndicator) {
137  LoadONCPolicy("certificate-web-authority.onc");
138  NavigateToSettings();
139  ClickElement("#certificatesManageButton");
140  ClickElement("#ca-certs-nav-tab");
141  EXPECT_TRUE(HasElement(".cert-policy"));
142}
143#endif
144