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

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

Issue 12209070: Fix cloud policy duplicate registrations issue. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Loads of tests 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/base_paths.h" 5 #include "base/base_paths.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
(...skipping 11 matching lines...) Expand all
22 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/browser.h" 23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/common/chrome_notification_types.h" 24 #include "chrome/common/chrome_notification_types.h"
25 #include "chrome/common/chrome_switches.h" 25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/test/base/in_process_browser_test.h" 26 #include "chrome/test/base/in_process_browser_test.h"
27 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/notification_service.h" 28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/notification_source.h" 29 #include "content/public/browser/notification_source.h"
30 #include "content/public/test/test_utils.h" 30 #include "content/public/test/test_utils.h"
31 #include "googleurl/src/gurl.h" 31 #include "googleurl/src/gurl.h"
32 #include "net/base/net_errors.h"
32 #include "net/test/test_server.h" 33 #include "net/test/test_server.h"
34 #include "net/url_request/url_request_error_job.h"
35 #include "net/url_request/url_request_filter.h"
36 #include "net/url_request/url_request_job_factory.h"
33 #include "policy/policy_constants.h" 37 #include "policy/policy_constants.h"
34 #include "testing/gmock/include/gmock/gmock.h" 38 #include "testing/gmock/include/gmock/gmock.h"
35 #include "testing/gtest/include/gtest/gtest.h" 39 #include "testing/gtest/include/gtest/gtest.h"
36 40
37 #if defined(OS_CHROMEOS) 41 #if defined(OS_CHROMEOS)
38 #include "chrome/browser/chromeos/login/user_manager.h" 42 #include "chrome/browser/chromeos/login/user_manager.h"
39 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" 43 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h"
40 #else 44 #else
41 #include "chrome/browser/policy/user_cloud_policy_manager.h" 45 #include "chrome/browser/policy/user_cloud_policy_manager.h"
42 #include "chrome/browser/policy/user_cloud_policy_manager_factory.h" 46 #include "chrome/browser/policy/user_cloud_policy_manager_factory.h"
43 #include "chrome/browser/signin/signin_manager.h" 47 #include "chrome/browser/signin/signin_manager.h"
44 #include "chrome/browser/signin/signin_manager_factory.h" 48 #include "chrome/browser/signin/signin_manager_factory.h"
45 #endif 49 #endif
46 50
51 using testing::AnyNumber;
47 using testing::InvokeWithoutArgs; 52 using testing::InvokeWithoutArgs;
48 using testing::Mock; 53 using testing::Mock;
49 using testing::_; 54 using testing::_;
50 55
51 namespace em = enterprise_management; 56 namespace em = enterprise_management;
52 57
53 namespace policy { 58 namespace policy {
54 59
55 namespace { 60 namespace {
56 61
62 // Bogus OAuth tokens to authenticate register requests at the testserver.
63 // The testserver accepts any token, but does additional checks for
64 // |kReRegisterAuthToken|: it verifies that the register request has the
65 // re-register flag set.
66 const char kAuthToken[] = "bogustoken";
67 const char kReRegisterAuthToken[] = "reregistertoken";
68
57 class MockCloudPolicyClientObserver : public CloudPolicyClient::Observer { 69 class MockCloudPolicyClientObserver : public CloudPolicyClient::Observer {
58 public: 70 public:
59 MockCloudPolicyClientObserver() {} 71 MockCloudPolicyClientObserver() {}
60 virtual ~MockCloudPolicyClientObserver() {} 72 virtual ~MockCloudPolicyClientObserver() {}
61 73
62 MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*)); 74 MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*));
63 MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*)); 75 MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*));
64 MOCK_METHOD1(OnClientError, void(CloudPolicyClient*)); 76 MOCK_METHOD1(OnClientError, void(CloudPolicyClient*));
65 }; 77 };
66 78
79 // Intercepts requests to the testserver and makes them fail. This is used to
80 // test the error recovery mechanisms.
81 class ErrorInjector : public net::URLRequestJobFactory::ProtocolHandler {
82 public:
83 // Fails only the first request sent if |once| is true, otherwise makes all
84 // requests fail.
85 explicit ErrorInjector(bool once) : once_(once) {}
86 ~ErrorInjector() {}
87
88 // Registers this object as a request interceptor. It becomes owned by
89 // URLRequestFilter.
90 void Register() {
91 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> self(this);
92 content::BrowserThread::PostTask(
93 content::BrowserThread::IO, FROM_HERE,
94 base::Bind(&net::URLRequestFilter::AddHostnameProtocolHandler,
95 base::Unretained(net::URLRequestFilter::GetInstance()),
96 "http",
97 net::TestServer::kLocalhost,
98 base::Passed(&self)));
99 }
100
101 // Cleans up this object's registration at the URLRequestFilter. This must be
102 // posted to IO after all expected requests are complete, if |once_| is false.
Mattias Nissler (ping if slow) 2013/02/11 18:23:50 That's a weird protocol. Let's have only one way o
Joao da Silva 2013/02/12 16:33:41 Obsolete.
103 static void Unregister() {
104 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(
105 "http",
106 net::TestServer::kLocalhost);
107 }
108
109 virtual net::URLRequestJob* MaybeCreateJob(
110 net::URLRequest* request,
111 net::NetworkDelegate* network_delegate) const OVERRIDE {
112 if (once_)
113 Unregister();
114 return new net::URLRequestErrorJob(
115 request, network_delegate, net::ERR_NETWORK_CHANGED);
116 }
117
118 private:
119 bool once_;
120 DISALLOW_COPY_AND_ASSIGN(ErrorInjector);
121 };
122
67 const char* GetTestUser() { 123 const char* GetTestUser() {
68 #if defined(OS_CHROMEOS) 124 #if defined(OS_CHROMEOS)
69 return chromeos::UserManager::kStubUser; 125 return chromeos::UserManager::kStubUser;
70 #else 126 #else
71 return "user@example.com"; 127 return "user@example.com";
72 #endif 128 #endif
73 } 129 }
74 130
75 std::string GetEmptyPolicy() { 131 std::string GetEmptyPolicy() {
76 const char kEmptyPolicy[] = 132 const char kEmptyPolicy[] =
(...skipping 26 matching lines...) Expand all
103 " \"managed_users\": [ \"*\" ]," 159 " \"managed_users\": [ \"*\" ],"
104 " \"policy_user\": \"%s\"" 160 " \"policy_user\": \"%s\""
105 "}"; 161 "}";
106 162
107 return base::StringPrintf(kTestPolicy, dm_protocol::kChromeUserPolicyType, 163 return base::StringPrintf(kTestPolicy, dm_protocol::kChromeUserPolicyType,
108 GetTestUser()); 164 GetTestUser());
109 } 165 }
110 166
111 } // namespace 167 } // namespace
112 168
113 // Tests the cloud policy stack(s). 169 // Tests the cloud policy stack.
114 class CloudPolicyTest : public InProcessBrowserTest { 170 class CloudPolicyTest : public InProcessBrowserTest {
115 protected: 171 protected:
116 CloudPolicyTest() {} 172 CloudPolicyTest() {}
117 virtual ~CloudPolicyTest() {} 173 virtual ~CloudPolicyTest() {}
118 174
119 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 175 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
120 // The TestServer wants the docroot as a path relative to the source dir. 176 // The TestServer wants the docroot as a path relative to the source dir.
121 base::FilePath source; 177 base::FilePath source;
122 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source)); 178 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source));
123 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(source)); 179 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(source));
(...skipping 19 matching lines...) Expand all
143 const PolicyMap& map = g_browser_process->policy_service()->GetPolicies( 199 const PolicyMap& map = g_browser_process->policy_service()->GetPolicies(
144 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); 200 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
145 if (!map.empty()) { 201 if (!map.empty()) {
146 base::DictionaryValue dict; 202 base::DictionaryValue dict;
147 for (PolicyMap::const_iterator it = map.begin(); it != map.end(); ++it) 203 for (PolicyMap::const_iterator it = map.begin(); it != map.end(); ++it)
148 dict.SetWithoutPathExpansion(it->first, it->second.value->DeepCopy()); 204 dict.SetWithoutPathExpansion(it->first, it->second.value->DeepCopy());
149 ADD_FAILURE() 205 ADD_FAILURE()
150 << "There are pre-existing policies in this machine that will " 206 << "There are pre-existing policies in this machine that will "
151 << "interfere with these tests. Policies found: " << dict; 207 << "interfere with these tests. Policies found: " << dict;
152 } 208 }
209 }
153 210
211 CloudPolicyManager* GetUserCloudPolicyManager() {
212 #if defined(OS_CHROMEOS)
213 return g_browser_process->browser_policy_connector()->
214 GetUserCloudPolicyManager();
215 #else
216 return UserCloudPolicyManagerFactory::GetForProfile(browser()->profile());
217 #endif // defined(OS_CHROMEOS)
218 }
219
220 void Register(const std::string& auth_token) {
154 BrowserPolicyConnector* connector = 221 BrowserPolicyConnector* connector =
155 g_browser_process->browser_policy_connector(); 222 g_browser_process->browser_policy_connector();
156 connector->ScheduleServiceInitialization(0); 223 connector->ScheduleServiceInitialization(0);
157 224
158 #if defined(OS_CHROMEOS) 225 #if defined(OS_CHROMEOS)
159 UserCloudPolicyManagerChromeOS* policy_manager = 226 UserCloudPolicyManagerChromeOS* policy_manager =
160 connector->GetUserCloudPolicyManager(); 227 connector->GetUserCloudPolicyManager();
161 ASSERT_TRUE(policy_manager); 228 ASSERT_TRUE(policy_manager);
162 #else 229 #else
163 // Mock a signed-in user. This is used by the UserCloudPolicyStore to pass 230 // Mock a signed-in user. This is used by the UserCloudPolicyStore to pass
164 // the username to the UserCloudPolicyValidator. 231 // the username to the UserCloudPolicyValidator.
165 SigninManager* signin_manager = 232 SigninManager* signin_manager =
166 SigninManagerFactory::GetForProfile(browser()->profile()); 233 SigninManagerFactory::GetForProfile(browser()->profile());
167 ASSERT_TRUE(signin_manager); 234 ASSERT_TRUE(signin_manager);
168 signin_manager->SetAuthenticatedUsername(GetTestUser()); 235 signin_manager->SetAuthenticatedUsername(GetTestUser());
169 236
170 UserCloudPolicyManager* policy_manager = 237 UserCloudPolicyManager* policy_manager =
171 UserCloudPolicyManagerFactory::GetForProfile(browser()->profile()); 238 UserCloudPolicyManagerFactory::GetForProfile(browser()->profile());
172 ASSERT_TRUE(policy_manager); 239 ASSERT_TRUE(policy_manager);
173 policy_manager->Connect(g_browser_process->local_state(), 240 policy_manager->Connect(g_browser_process->local_state(),
174 UserCloudPolicyManager::CreateCloudPolicyClient( 241 UserCloudPolicyManager::CreateCloudPolicyClient(
175 connector->device_management_service()).Pass()); 242 connector->device_management_service()).Pass());
176 #endif // defined(OS_CHROMEOS) 243 #endif // defined(OS_CHROMEOS)
177 244
178 ASSERT_TRUE(policy_manager->core()->client()); 245 ASSERT_TRUE(policy_manager->core()->client());
179 base::RunLoop run_loop; 246 base::RunLoop run_loop;
180 MockCloudPolicyClientObserver observer; 247 MockCloudPolicyClientObserver observer;
181 EXPECT_CALL(observer, OnRegistrationStateChanged(_)).WillOnce( 248 EXPECT_CALL(observer, OnRegistrationStateChanged(_))
182 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); 249 .Times(AnyNumber())
250 .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
251 EXPECT_CALL(observer, OnClientError(_))
252 .Times(AnyNumber())
253 .WillRepeatedly(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
183 policy_manager->core()->client()->AddObserver(&observer); 254 policy_manager->core()->client()->AddObserver(&observer);
184 255
185 // Give a bogus OAuth token to the |policy_manager|. This should make its 256 // Give a bogus OAuth token to the |policy_manager|. This should make its
186 // CloudPolicyClient fetch the DMToken. 257 // CloudPolicyClient fetch the DMToken.
187 policy_manager->RegisterClient("bogus"); 258 policy_manager->RegisterClient(auth_token);
188 run_loop.Run(); 259 run_loop.Run();
189 Mock::VerifyAndClearExpectations(&observer); 260 Mock::VerifyAndClearExpectations(&observer);
190 policy_manager->core()->client()->RemoveObserver(&observer); 261 policy_manager->core()->client()->RemoveObserver(&observer);
191 } 262 }
192 263
193 void SetServerPolicy(const std::string& policy) { 264 void SetServerPolicy(const std::string& policy) {
194 int result = file_util::WriteFile( 265 int result = file_util::WriteFile(
195 temp_dir_.path().AppendASCII("device_management"), 266 temp_dir_.path().AppendASCII("device_management"),
196 policy.data(), policy.size()); 267 policy.data(), policy.size());
197 ASSERT_EQ(static_cast<int>(policy.size()), result); 268 ASSERT_EQ(static_cast<int>(policy.size()), result);
198 } 269 }
199 270
200 base::ScopedTempDir temp_dir_; 271 base::ScopedTempDir temp_dir_;
201 scoped_ptr<net::TestServer> test_server_; 272 scoped_ptr<net::TestServer> test_server_;
202 }; 273 };
203 274
275 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, Register) {
276 EXPECT_FALSE(GetUserCloudPolicyManager()->core()->client()->is_registered());
277 ASSERT_NO_FATAL_FAILURE(Register(kAuthToken));
278 EXPECT_TRUE(GetUserCloudPolicyManager()->core()->client()->is_registered());
279 }
280
281 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, RetryRegister) {
282 // Verifies that if the first register request fails and is retried then the
283 // second request will set the re-register flag.
284
285 // Make the first request fail.
286 const bool fail_once = true;
287 ErrorInjector* injector = new ErrorInjector(fail_once);
288 injector->Register();
289
290 // Register with |kReRegisterAuthToken|. The testserver only accepts the
291 // registration with that token if the re-register flag is set.
Mattias Nissler (ping if slow) 2013/02/11 18:23:50 This is odd, because the error handling is now in
Joao da Silva 2013/02/12 16:33:41 Fair. I think there's value in having both kinds o
292 ASSERT_NO_FATAL_FAILURE(Register(kReRegisterAuthToken));
293
294 // Verify that the registration was accepted.
295 EXPECT_TRUE(GetUserCloudPolicyManager()->core()->client()->is_registered());
296 }
297
298 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, RetryRegisterAndFail) {
299 // Make all the registration retries fail.
300 const bool always_fail = false;
301 ErrorInjector* injector = new ErrorInjector(always_fail);
302 injector->Register();
303
304 // Registering should fail and give up after reaching the retry limit.
305 ASSERT_NO_FATAL_FAILURE(Register(kAuthToken));
306 EXPECT_FALSE(GetUserCloudPolicyManager()->core()->client()->is_registered());
307
308 // Cleanup.
309 content::BrowserThread::PostTask(
310 content::BrowserThread::IO, FROM_HERE,
311 base::Bind(&ErrorInjector::Unregister));
312 }
313
204 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) { 314 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) {
315 ASSERT_NO_FATAL_FAILURE(Register(kAuthToken));
316 ASSERT_TRUE(GetUserCloudPolicyManager()->core()->client()->is_registered());
317
205 PolicyService* policy_service = browser()->profile()->GetPolicyService(); 318 PolicyService* policy_service = browser()->profile()->GetPolicyService();
206 { 319 {
207 base::RunLoop run_loop; 320 base::RunLoop run_loop;
208 policy_service->RefreshPolicies(run_loop.QuitClosure()); 321 policy_service->RefreshPolicies(run_loop.QuitClosure());
209 run_loop.Run(); 322 run_loop.Run();
210 } 323 }
211 324
212 PolicyMap empty; 325 PolicyMap empty;
213 EXPECT_TRUE(empty.Equals(policy_service->GetPolicies( 326 EXPECT_TRUE(empty.Equals(policy_service->GetPolicies(
214 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); 327 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 393
281 // They should now serialize to the same bytes. 394 // They should now serialize to the same bytes.
282 std::string chrome_settings_serialized; 395 std::string chrome_settings_serialized;
283 std::string cloud_policy_serialized; 396 std::string cloud_policy_serialized;
284 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); 397 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized));
285 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); 398 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized));
286 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); 399 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized);
287 } 400 }
288 401
289 } // namespace policy 402 } // namespace policy
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/policy/cloud_policy_client.h » ('j') | net/tools/testserver/device_management.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698