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

Side by Side Diff: chrome/browser/policy/cloud_policy_manager_browsertest.cc

Issue 12209070: Fix cloud policy duplicate registrations issue. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 10 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 | Annotate | Revision Log
OLDNEW
(Empty)
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_policy_client.h"
11 #include "chrome/browser/policy/mock_cloud_policy_client.h"
12 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
13 #include "chrome/browser/policy/test_request_interceptor.h"
14 #include "chrome/browser/policy/test_utils.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 "net/base/net_errors.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/policy/user_cloud_policy_manager_chromeos.h"
25 #else
26 #include "chrome/browser/policy/user_cloud_policy_manager.h"
27 #include "chrome/browser/policy/user_cloud_policy_manager_factory.h"
28 #include "chrome/browser/signin/signin_manager.h"
29 #include "chrome/browser/signin/signin_manager_factory.h"
30 #endif
31
32 using testing::AnyNumber;
33 using testing::InvokeWithoutArgs;
34 using testing::Mock;
35 using testing::_;
36
37 namespace em = enterprise_management;
38
39 namespace policy {
40
41 // Tests the cloud policy stack using a URLRequestJobFactory::ProtocolHandler
42 // to intercept requests and produce canned responses.
43 class CloudPolicyManagerTest : public InProcessBrowserTest {
44 protected:
45 CloudPolicyManagerTest() {}
46 virtual ~CloudPolicyManagerTest() {}
47
48 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
49 CommandLine* command_line = CommandLine::ForCurrentProcess();
50 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl,
51 "http://localhost");
52 }
53
54 virtual void SetUpOnMainThread() OVERRIDE {
55 ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service()))
56 << "Pre-existing policies in this machine will make this test fail.";
57
58 interceptor_.reset(new TestRequestInterceptor("localhost"));
59
60 BrowserPolicyConnector* connector =
61 g_browser_process->browser_policy_connector();
62 connector->ScheduleServiceInitialization(0);
63
64 #if !defined(OS_CHROMEOS)
65 // Mock a signed-in user. This is used by the UserCloudPolicyStore to pass
66 // the username to the UserCloudPolicyValidator.
67 SigninManager* signin_manager =
68 SigninManagerFactory::GetForProfile(browser()->profile());
69 ASSERT_TRUE(signin_manager);
70 signin_manager->SetAuthenticatedUsername("user@example.com");
71
72 UserCloudPolicyManager* policy_manager =
73 UserCloudPolicyManagerFactory::GetForProfile(browser()->profile());
74 ASSERT_TRUE(policy_manager);
75 policy_manager->Connect(g_browser_process->local_state(),
76 UserCloudPolicyManager::CreateCloudPolicyClient(
77 connector->device_management_service()).Pass());
78 #endif
79 }
80
81 virtual void CleanUpOnMainThread() OVERRIDE {
82 // Verify that all the expected requests were handled.
83 EXPECT_EQ(0u, interceptor_->GetPendingSize());
84
85 interceptor_.reset();
86 }
87
88 #if defined(OS_CHROMEOS)
89 UserCloudPolicyManagerChromeOS* policy_manager() {
90 return g_browser_process->browser_policy_connector()->
91 GetUserCloudPolicyManager();
92 }
93 #else
94 UserCloudPolicyManager* policy_manager() {
95 return UserCloudPolicyManagerFactory::GetForProfile(browser()->profile());
96 }
97 #endif // defined(OS_CHROMEOS)
98
99 // Register the client of the policy_manager() using a bogus auth token, and
100 // returns once the registration gets a result back.
101 void Register() {
102 ASSERT_TRUE(policy_manager());
103 ASSERT_TRUE(policy_manager()->core()->client());
104
105 base::RunLoop run_loop;
106 MockCloudPolicyClientObserver observer;
107 EXPECT_CALL(observer, OnRegistrationStateChanged(_))
108 .Times(AnyNumber())
109 .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
110 EXPECT_CALL(observer, OnClientError(_))
111 .Times(AnyNumber())
112 .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
113 policy_manager()->core()->client()->AddObserver(&observer);
114
115 // Give a bogus OAuth token to the |policy_manager|. This should make its
116 // CloudPolicyClient fetch the DMToken.
117 policy_manager()->RegisterClient("bogus");
118 run_loop.Run();
119 Mock::VerifyAndClearExpectations(&observer);
120 policy_manager()->core()->client()->RemoveObserver(&observer);
121 }
122
123 scoped_ptr<TestRequestInterceptor> interceptor_;
124 };
125
126 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, Register) {
127 // Accept one register request. The initial request should not include the
128 // reregister flag.
129 em::DeviceRegisterRequest::Type expected_type =
130 #if defined(OS_CHROMEOS)
131 em::DeviceRegisterRequest::USER;
132 #else
133 em::DeviceRegisterRequest::BROWSER;
134 #endif
135 const bool expect_reregister = false;
136 interceptor_->PushJobCallback(
137 TestRequestInterceptor::RegisterJob(expected_type, expect_reregister));
138
139 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
140 ASSERT_NO_FATAL_FAILURE(Register());
141 EXPECT_TRUE(policy_manager()->core()->client()->is_registered());
142 }
143
144 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterFails) {
145 // The interceptor makes all requests fail by default; this will trigger
146 // an OnClientError() call on the observer.
147 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
148 ASSERT_NO_FATAL_FAILURE(Register());
149 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
150 }
151
152 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterFailsWithRetries) {
153 // Fail 4 times with ERR_NETWORK_CHANGED; the first 3 will trigger a retry,
154 // the last one will forward the error to the client and unblock the
155 // register process.
156 for (int i = 0; i < 4; ++i) {
157 interceptor_->PushJobCallback(
158 TestRequestInterceptor::ErrorJob(net::ERR_NETWORK_CHANGED));
159 }
160
161 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
162 ASSERT_NO_FATAL_FAILURE(Register());
163 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
164 }
165
166 IN_PROC_BROWSER_TEST_F(CloudPolicyManagerTest, RegisterWithRetry) {
167 // Accept one register request after failing once. The retry request should
168 // set the reregister flag.
169 interceptor_->PushJobCallback(
170 TestRequestInterceptor::ErrorJob(net::ERR_NETWORK_CHANGED));
171 em::DeviceRegisterRequest::Type expected_type =
172 #if defined(OS_CHROMEOS)
173 em::DeviceRegisterRequest::USER;
174 #else
175 em::DeviceRegisterRequest::BROWSER;
176 #endif
177 const bool expect_reregister = true;
178 interceptor_->PushJobCallback(
179 TestRequestInterceptor::RegisterJob(expected_type, expect_reregister));
180
181 EXPECT_FALSE(policy_manager()->core()->client()->is_registered());
182 ASSERT_NO_FATAL_FAILURE(Register());
183 EXPECT_TRUE(policy_manager()->core()->client()->is_registered());
184 }
185
186 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud_policy_client_unittest.cc ('k') | chrome/browser/policy/device_management_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698