- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / cloud / cloud_policy_manager_browsertest.cc
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.
4
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/browser_policy_connector.h"
10 #include "chrome/browser/policy/cloud/cloud_policy_client.h"
11 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h"
12 #include "chrome/browser/policy/cloud/test_request_interceptor.h"
13 #include "chrome/browser/policy/policy_test_utils.h"
14 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "net/base/net_errors.h"
21 #include "net/url_request/url_request_context_getter.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
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.h"
30 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
31 #include "chrome/browser/signin/signin_manager.h"
32 #include "chrome/browser/signin/signin_manager_factory.h"
33 #endif
34
35 using content::BrowserThread;
36 using testing::AnyNumber;
37 using testing::InvokeWithoutArgs;
38 using testing::Mock;
39 using testing::_;
40
41 namespace em = enterprise_management;
42
43 namespace policy {
44
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   virtual ~CloudPolicyManagerTest() {}
51
52   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
53     CommandLine* command_line = CommandLine::ForCurrentProcess();
54     command_line->AppendSwitchASCII(switches::kDeviceManagementUrl,
55                                     "http://localhost");
56   }
57
58   virtual void SetUpOnMainThread() OVERRIDE {
59     ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service()))
60         << "Pre-existing policies in this machine will make this test fail.";
61
62     interceptor_.reset(new TestRequestInterceptor(
63         "localhost",
64         BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
65
66     BrowserPolicyConnector* connector =
67         g_browser_process->browser_policy_connector();
68     connector->ScheduleServiceInitialization(0);
69
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->SetAuthenticatedUsername("user@example.com");
77
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()).Pass());
84 #endif
85   }
86
87   virtual void CleanUpOnMainThread() OVERRIDE {
88     // Verify that all the expected requests were handled.
89     EXPECT_EQ(0u, interceptor_->GetPendingSize());
90
91     interceptor_.reset();
92   }
93
94 #if defined(OS_CHROMEOS)
95   UserCloudPolicyManagerChromeOS* policy_manager() {
96     return UserCloudPolicyManagerFactoryChromeOS::GetForProfile(
97         browser()->profile());
98   }
99 #else
100   UserCloudPolicyManager* policy_manager() {
101     return UserCloudPolicyManagerFactory::GetForProfile(browser()->profile());
102   }
103 #endif  // defined(OS_CHROMEOS)
104
105   // Register the client of the policy_manager() using a bogus auth token, and
106   // returns once the registration gets a result back.
107   void Register() {
108     ASSERT_TRUE(policy_manager());
109     ASSERT_TRUE(policy_manager()->core()->client());
110
111     base::RunLoop run_loop;
112     MockCloudPolicyClientObserver observer;
113     EXPECT_CALL(observer, OnRegistrationStateChanged(_))
114         .Times(AnyNumber())
115         .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
116     EXPECT_CALL(observer, OnClientError(_))
117         .Times(AnyNumber())
118         .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
119     policy_manager()->core()->client()->AddObserver(&observer);
120
121     // Give a bogus OAuth token to the |policy_manager|. This should make its
122     // CloudPolicyClient fetch the DMToken.
123     em::DeviceRegisterRequest::Type registration_type =
124 #if defined(OS_CHROMEOS)
125         em::DeviceRegisterRequest::USER;
126 #else
127         em::DeviceRegisterRequest::BROWSER;
128 #endif
129     policy_manager()->core()->client()->Register(
130         registration_type, "bogus", std::string(), false, std::string());
131     run_loop.Run();
132     Mock::VerifyAndClearExpectations(&observer);
133     policy_manager()->core()->client()->RemoveObserver(&observer);
134   }
135
136   scoped_ptr<TestRequestInterceptor> interceptor_;
137 };
138
139 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, Register) {
140   // Accept one register request. The initial request should not include the
141   // reregister flag.
142   em::DeviceRegisterRequest::Type expected_type =
143 #if defined(OS_CHROMEOS)
144       em::DeviceRegisterRequest::USER;
145 #else
146       em::DeviceRegisterRequest::BROWSER;
147 #endif
148   const bool expect_reregister = false;
149   interceptor_->PushJobCallback(
150       TestRequestInterceptor::RegisterJob(expected_type, expect_reregister));
151
152   EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
153   ASSERT_NO_FATAL_FAILURE(Register());
154   EXPECT_TRUE(policy_manager()->core()->client()->is_registered());
155 }
156
157 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterFails) {
158   // The interceptor makes all requests fail by default; this will trigger
159   // an OnClientError() call on the observer.
160   EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
161   ASSERT_NO_FATAL_FAILURE(Register());
162   EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
163 }
164
165 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterFailsWithRetries) {
166   // Fail 4 times with ERR_NETWORK_CHANGED; the first 3 will trigger a retry,
167   // the last one will forward the error to the client and unblock the
168   // register process.
169   for (int i = 0; i < 4; ++i) {
170     interceptor_->PushJobCallback(
171         TestRequestInterceptor::ErrorJob(net::ERR_NETWORK_CHANGED));
172   }
173
174   EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
175   ASSERT_NO_FATAL_FAILURE(Register());
176   EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
177 }
178
179 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterWithRetry) {
180   // Accept one register request after failing once. The retry request should
181   // set the reregister flag.
182   interceptor_->PushJobCallback(
183       TestRequestInterceptor::ErrorJob(net::ERR_NETWORK_CHANGED));
184   em::DeviceRegisterRequest::Type expected_type =
185 #if defined(OS_CHROMEOS)
186       em::DeviceRegisterRequest::USER;
187 #else
188       em::DeviceRegisterRequest::BROWSER;
189 #endif
190   const bool expect_reregister = true;
191   interceptor_->PushJobCallback(
192       TestRequestInterceptor::RegisterJob(expected_type, expect_reregister));
193
194   EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
195   ASSERT_NO_FATAL_FAILURE(Register());
196   EXPECT_TRUE(policy_manager()->core()->client()->is_registered());
197 }
198
199 }  // namespace policy