Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / policy / cloud / cloud_policy_manager_browsertest.cc
blob9c63d1aa93ebd457cbb8bcbc06349cdc8dd52f18
1 // Copyright (c) 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.
5 #include "base/command_line.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/run_loop.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/policy/cloud/test_request_interceptor.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "components/policy/core/browser/browser_policy_connector.h"
14 #include "components/policy/core/common/cloud/cloud_policy_client.h"
15 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
16 #include "components/policy/core/common/policy_switches.h"
17 #include "components/policy/core/common/policy_test_utils.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "net/base/net_errors.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "policy/proto/device_management_backend.pb.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 #if defined(OS_CHROMEOS)
26 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
27 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
28 #else
29 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
30 #include "chrome/browser/signin/signin_manager_factory.h"
31 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
32 #include "components/signin/core/browser/signin_manager.h"
33 #endif
35 using content::BrowserThread;
36 using testing::AnyNumber;
37 using testing::InvokeWithoutArgs;
38 using testing::Mock;
39 using testing::_;
41 namespace em = enterprise_management;
43 namespace policy {
45 // Tests the cloud policy stack using a URLRequestJobFactory::ProtocolHandler
46 // to intercept requests and produce canned responses.
47 class CloudPolicyManagerTest : public InProcessBrowserTest {
48 protected:
49 CloudPolicyManagerTest() {}
50 ~CloudPolicyManagerTest() override {}
52 void SetUpInProcessBrowserTestFixture() override {
53 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
54 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl,
55 "http://localhost");
58 void SetUpOnMainThread() override {
59 ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service()))
60 << "Pre-existing policies in this machine will make this test fail.";
62 interceptor_.reset(new TestRequestInterceptor(
63 "localhost",
64 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
66 BrowserPolicyConnector* connector =
67 g_browser_process->browser_policy_connector();
68 connector->ScheduleServiceInitialization(0);
70 #if !defined(OS_CHROMEOS)
71 // Mock a signed-in user. This is used by the UserCloudPolicyStore to pass
72 // the username to the UserCloudPolicyValidator.
73 SigninManager* signin_manager =
74 SigninManagerFactory::GetForProfile(browser()->profile());
75 ASSERT_TRUE(signin_manager);
76 signin_manager->SetAuthenticatedAccountInfo("12345", "user@example.com");
78 ASSERT_TRUE(policy_manager());
79 policy_manager()->Connect(
80 g_browser_process->local_state(),
81 g_browser_process->system_request_context(),
82 UserCloudPolicyManager::CreateCloudPolicyClient(
83 connector->device_management_service(),
84 g_browser_process->system_request_context()).Pass());
85 #endif
88 void TearDownOnMainThread() override {
89 // Verify that all the expected requests were handled.
90 EXPECT_EQ(0u, interceptor_->GetPendingSize());
92 interceptor_.reset();
95 #if defined(OS_CHROMEOS)
96 UserCloudPolicyManagerChromeOS* policy_manager() {
97 return UserCloudPolicyManagerFactoryChromeOS::GetForProfile(
98 browser()->profile());
100 #else
101 UserCloudPolicyManager* policy_manager() {
102 return UserCloudPolicyManagerFactory::GetForBrowserContext(
103 browser()->profile());
105 #endif // defined(OS_CHROMEOS)
107 // Register the client of the policy_manager() using a bogus auth token, and
108 // returns once the registration gets a result back.
109 void Register() {
110 ASSERT_TRUE(policy_manager());
111 ASSERT_TRUE(policy_manager()->core()->client());
113 base::RunLoop run_loop;
114 MockCloudPolicyClientObserver observer;
115 EXPECT_CALL(observer, OnRegistrationStateChanged(_))
116 .Times(AnyNumber())
117 .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
118 EXPECT_CALL(observer, OnClientError(_))
119 .Times(AnyNumber())
120 .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
121 policy_manager()->core()->client()->AddObserver(&observer);
123 // Give a bogus OAuth token to the |policy_manager|. This should make its
124 // CloudPolicyClient fetch the DMToken.
125 em::DeviceRegisterRequest::Type registration_type =
126 #if defined(OS_CHROMEOS)
127 em::DeviceRegisterRequest::USER;
128 #else
129 em::DeviceRegisterRequest::BROWSER;
130 #endif
131 policy_manager()->core()->client()->Register(
132 registration_type, em::DeviceRegisterRequest::FLAVOR_USER_REGISTRATION,
133 "bogus", std::string(), std::string(), std::string());
134 run_loop.Run();
135 Mock::VerifyAndClearExpectations(&observer);
136 policy_manager()->core()->client()->RemoveObserver(&observer);
139 scoped_ptr<TestRequestInterceptor> interceptor_;
142 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, Register) {
143 // Accept one register request. The initial request should not include the
144 // reregister flag.
145 em::DeviceRegisterRequest::Type expected_type =
146 #if defined(OS_CHROMEOS)
147 em::DeviceRegisterRequest::USER;
148 #else
149 em::DeviceRegisterRequest::BROWSER;
150 #endif
151 const bool expect_reregister = false;
152 interceptor_->PushJobCallback(
153 TestRequestInterceptor::RegisterJob(expected_type, expect_reregister));
155 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
156 ASSERT_NO_FATAL_FAILURE(Register());
157 EXPECT_TRUE(policy_manager()->core()->client()->is_registered());
160 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterFails) {
161 // The interceptor makes all requests fail by default; this will trigger
162 // an OnClientError() call on the observer.
163 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
164 ASSERT_NO_FATAL_FAILURE(Register());
165 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
168 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterFailsWithRetries) {
169 // Fail 4 times with ERR_NETWORK_CHANGED; the first 3 will trigger a retry,
170 // the last one will forward the error to the client and unblock the
171 // register process.
172 for (int i = 0; i < 4; ++i) {
173 interceptor_->PushJobCallback(
174 TestRequestInterceptor::ErrorJob(net::ERR_NETWORK_CHANGED));
177 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
178 ASSERT_NO_FATAL_FAILURE(Register());
179 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
182 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterWithRetry) {
183 // Accept one register request after failing once. The retry request should
184 // set the reregister flag.
185 interceptor_->PushJobCallback(
186 TestRequestInterceptor::ErrorJob(net::ERR_NETWORK_CHANGED));
187 em::DeviceRegisterRequest::Type expected_type =
188 #if defined(OS_CHROMEOS)
189 em::DeviceRegisterRequest::USER;
190 #else
191 em::DeviceRegisterRequest::BROWSER;
192 #endif
193 const bool expect_reregister = true;
194 interceptor_->PushJobCallback(
195 TestRequestInterceptor::RegisterJob(expected_type, expect_reregister));
197 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
198 ASSERT_NO_FATAL_FAILURE(Register());
199 EXPECT_TRUE(policy_manager()->core()->client()->is_registered());
202 } // namespace policy