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

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

Issue 7298012: Consolidate data storage and notifications in the cloud policy subsystem (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 5 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <vector> 5 #include <vector>
6 6
7 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h" 8 #include "base/message_loop.h"
8 #include "base/scoped_temp_dir.h" 9 #include "base/scoped_temp_dir.h"
9 #include "base/values.h" 10 #include "base/values.h"
10 #include "chrome/browser/browser_process.h" 11 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/policy/cloud_policy_identity_strategy.h" 12 #include "chrome/browser/policy/cloud_policy_data.h"
12 #include "chrome/browser/policy/logging_work_scheduler.h" 13 #include "chrome/browser/policy/logging_work_scheduler.h"
13 #include "chrome/browser/policy/mock_configuration_policy_store.h" 14 #include "chrome/browser/policy/mock_configuration_policy_store.h"
Joao da Silva 2011/07/06 16:45:14 Nit: mock_configuration_policy_store.h not used
gfeher 2011/07/07 13:51:00 Done.
14 #include "chrome/browser/policy/proto/cloud_policy.pb.h" 15 #include "chrome/browser/policy/proto/cloud_policy.pb.h"
15 #include "chrome/browser/policy/proto/device_management_backend.pb.h" 16 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
16 #include "chrome/browser/policy/testing_cloud_policy_subsystem.h" 17 #include "chrome/browser/policy/testing_cloud_policy_subsystem.h"
17 #include "chrome/browser/policy/testing_policy_url_fetcher_factory.h" 18 #include "chrome/browser/policy/testing_policy_url_fetcher_factory.h"
18 #include "chrome/browser/policy/user_policy_cache.h" 19 #include "chrome/browser/policy/user_policy_cache.h"
19 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
20 #include "chrome/test/testing_browser_process.h" 21 #include "chrome/test/testing_browser_process.h"
21 #include "chrome/test/testing_pref_service.h" 22 #include "chrome/test/testing_pref_service.h"
22 #include "content/browser/browser_thread.h" 23 #include "content/browser/browser_thread.h"
23 #include "content/common/url_fetcher.h" 24 #include "content/common/url_fetcher.h"
24 #include "policy/policy_constants.h" 25 #include "policy/policy_constants.h"
25 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gmock/include/gmock/gmock.h"
Joao da Silva 2011/07/06 16:45:14 Nit: gmock.h not used
gfeher 2011/07/07 13:51:00 Done.
26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
27 28
28 namespace policy { 29 namespace policy {
29 30
30 using ::testing::_; 31 using ::testing::_;
31 using ::testing::AtLeast; 32 using ::testing::AtLeast;
Joao da Silva 2011/07/06 16:45:14 Nit: AtLeast not used
gfeher 2011/07/07 13:51:00 Done.
32 using ::testing::AtMost; 33 using ::testing::AtMost;
33 using ::testing::DoAll; 34 using ::testing::DoAll;
Joao da Silva 2011/07/06 16:45:14 Nit: DoAll not used
gfeher 2011/07/07 13:51:00 Done.
34 using ::testing::InSequence; 35 using ::testing::InSequence;
35 using ::testing::Invoke; 36 using ::testing::Invoke;
36 using ::testing::Return; 37 using ::testing::Return;
Joao da Silva 2011/07/06 16:45:14 Nit: Invoke and Return not used
gfeher 2011/07/07 13:51:00 Done.
37 38
38 namespace em = enterprise_management; 39 namespace em = enterprise_management;
39 40
40 class CloudPolicySubsystemTest; 41 class CloudPolicySubsystemTest;
Joao da Silva 2011/07/06 16:45:14 Nit: not used
gfeher 2011/07/07 13:51:00 Done.
41 42
42 namespace { 43 namespace {
43 44
44 const char kGaiaAuthHeader[] = "GoogleLogin auth=secret123"; 45 const char kGaiaAuthHeader[] = "GoogleLogin auth=secret123";
45 const char kDMAuthHeader[] = "GoogleDMToken token=token123456"; 46 const char kDMAuthHeader[] = "GoogleDMToken token=token123456";
46 const char kDMToken[] = "token123456"; 47 const char kDMToken[] = "token123456";
47 48
48 const char kDeviceManagementUrl[] = 49 const char kDeviceManagementUrl[] =
49 "http://localhost:12345/device_management_test"; 50 "http://localhost:12345/device_management_test";
50 51
51 // Constant responses of the identity strategy. 52 // Fake data to be included in requests.
52 const char kMachineId[] = "dummy-cros-machine-123";
53 const char kMachineModel[] = "Pony";
54 const char kDeviceId[] = "abc-xyz-123";
55 const char kUsername[] = "john@smith.com"; 53 const char kUsername[] = "john@smith.com";
56 const char kAuthToken[] = "secret123"; 54 const char kAuthToken[] = "secret123";
57 const char kPolicyType[] = "google/chrome/test"; 55 const char kPolicyType[] = "google/chrome/test";
58 56
59 } // namespace 57 } // namespace
60 58
61 // A stripped-down identity strategy for the tests.
62 class TestingIdentityStrategy : public CloudPolicyIdentityStrategy {
63 public:
64 TestingIdentityStrategy() {
65 }
66
67 virtual std::string GetDeviceToken() OVERRIDE {
68 return device_token_;
69 }
70
71 virtual std::string GetDeviceID() OVERRIDE {
72 return kDeviceId;
73 }
74
75 virtual std::string GetMachineID() OVERRIDE {
76 return kMachineId;
77 }
78
79 virtual std::string GetMachineModel() OVERRIDE {
80 return kMachineModel;
81 }
82
83 virtual em::DeviceRegisterRequest_Type GetPolicyRegisterType() OVERRIDE {
84 return em::DeviceRegisterRequest::USER;
85 }
86
87 virtual std::string GetPolicyType() OVERRIDE {
88 return kPolicyType;
89 }
90
91 virtual bool GetCredentials(std::string* username,
92 std::string* auth_token) OVERRIDE {
93 *username = kUsername;
94 *auth_token = kAuthToken;;
95 return !username->empty() && !auth_token->empty();
96 }
97
98 virtual void OnDeviceTokenAvailable(const std::string& token) OVERRIDE {
99 device_token_ = token;
100 NotifyDeviceTokenChanged();
101 }
102
103 private:
104 std::string device_token_;
105
106 DISALLOW_COPY_AND_ASSIGN(TestingIdentityStrategy);
107 };
108
109 // An action that returns an URLRequestJob with an HTTP error code. 59 // An action that returns an URLRequestJob with an HTTP error code.
110 ACTION_P(CreateFailedResponse, http_error_code) { 60 ACTION_P(CreateFailedResponse, http_error_code) {
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
112 em::DeviceManagementResponse response_data; 62 em::DeviceManagementResponse response_data;
113 63
114 arg2->response_data = response_data.SerializeAsString(); 64 arg2->response_data = response_data.SerializeAsString();
115 arg2->response_code = http_error_code; 65 arg2->response_code = http_error_code;
116 } 66 }
117 67
118 // An action that returns an URLRequestJob with a successful device 68 // An action that returns an URLRequestJob with a successful device
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 } 116 }
167 117
168 virtual void SetUp() { 118 virtual void SetUp() {
169 prefs_.reset(new TestingPrefService); 119 prefs_.reset(new TestingPrefService);
170 CloudPolicySubsystem::RegisterPrefs(prefs_.get()); 120 CloudPolicySubsystem::RegisterPrefs(prefs_.get());
171 ((TestingBrowserProcess*) g_browser_process)->SetLocalState(prefs_.get()); 121 ((TestingBrowserProcess*) g_browser_process)->SetLocalState(prefs_.get());
172 122
173 logger_.reset(new EventLogger); 123 logger_.reset(new EventLogger);
174 factory_.reset(new TestingPolicyURLFetcherFactory(logger_.get())); 124 factory_.reset(new TestingPolicyURLFetcherFactory(logger_.get()));
175 URLFetcher::set_factory(factory_.get()); 125 URLFetcher::set_factory(factory_.get());
176 identity_strategy_.reset(new TestingIdentityStrategy);
177 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); 126 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir());
127 cloud_policy_data_.reset(CloudPolicyData::CreateForUserPolicies());
178 cache_ = new UserPolicyCache( 128 cache_ = new UserPolicyCache(
179 temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest")); 129 temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest"));
180 cloud_policy_subsystem_.reset(new TestingCloudPolicySubsystem( 130 cloud_policy_subsystem_.reset(new TestingCloudPolicySubsystem(
181 identity_strategy_.get(), cache_, kDeviceManagementUrl, 131 cloud_policy_data_.get(), cache_,
182 logger_.get())); 132 kDeviceManagementUrl, logger_.get()));
183 cloud_policy_subsystem_->CompleteInitialization( 133 cloud_policy_subsystem_->CompleteInitialization(
184 prefs::kDevicePolicyRefreshRate, 0); 134 prefs::kDevicePolicyRefreshRate, 0);
185 } 135 }
186 136
187 virtual void TearDown() { 137 virtual void TearDown() {
188 ((TestingBrowserProcess*) g_browser_process)->SetLocalState(NULL); 138 ((TestingBrowserProcess*) g_browser_process)->SetLocalState(NULL);
189 cloud_policy_subsystem_->Shutdown(); 139 cloud_policy_subsystem_->Shutdown();
190 cloud_policy_subsystem_.reset(); 140 cloud_policy_subsystem_.reset();
141 cloud_policy_data_.reset();
191 factory_.reset(); 142 factory_.reset();
192 logger_.reset(); 143 logger_.reset();
193 prefs_.reset(); 144 prefs_.reset();
194 } 145 }
195 146
196 void ExecuteTest() { 147 void ExecuteTest() {
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
198 // The first unexpected request of the policy subsystem will stop the 149 // The first unexpected request of the policy subsystem will stop the
199 // message loop. 150 // message loop.
200 // This code relies on the fact that an InSequence object is active. 151 // This code relies on the fact that an InSequence object is active.
201 EXPECT_CALL(*(factory_.get()), Intercept(_, _, _)) 152 EXPECT_CALL(*(factory_.get()), Intercept(_, _, _))
202 .Times(AtMost(1)) 153 .Times(AtMost(1))
203 .WillOnce(InvokeWithoutArgs( 154 .WillOnce(InvokeWithoutArgs(
204 this, 155 this,
205 &CloudPolicySubsystemTestBase::StopMessageLoop)); 156 &CloudPolicySubsystemTestBase::StopMessageLoop));
157 cloud_policy_data_->set_user_name(kUsername);
158 cloud_policy_data_->SetGaiaToken(kAuthToken);
159 cloud_policy_data_->SetDeviceToken("", true);
206 loop_.RunAllPending(); 160 loop_.RunAllPending();
207 } 161 }
208 162
209 void VerifyTest(const std::string& homepage_location) { 163 void VerifyTest(const std::string& homepage_location) {
210 // Test conditions. 164 // Test conditions.
211 EXPECT_EQ(CloudPolicySubsystem::SUCCESS, cloud_policy_subsystem_->state()); 165 EXPECT_EQ(CloudPolicySubsystem::SUCCESS, cloud_policy_subsystem_->state());
212 StringValue homepage_value(homepage_location); 166 StringValue homepage_value(homepage_location);
213 VerifyPolicy(kPolicyHomepageLocation, &homepage_value); 167 VerifyPolicy(kPolicyHomepageLocation, &homepage_value);
214 VerifyServerLoad(); 168 VerifyServerLoad();
215 } 169 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 << "No enough requests were fired during the test run."; 237 << "No enough requests were fired during the test run.";
284 } 238 }
285 239
286 ScopedTempDir temp_user_data_dir_; 240 ScopedTempDir temp_user_data_dir_;
287 241
288 MessageLoop loop_; 242 MessageLoop loop_;
289 BrowserThread ui_thread_; 243 BrowserThread ui_thread_;
290 BrowserThread io_thread_; 244 BrowserThread io_thread_;
291 245
292 scoped_ptr<EventLogger> logger_; 246 scoped_ptr<EventLogger> logger_;
293 scoped_ptr<TestingIdentityStrategy> identity_strategy_; 247 scoped_ptr<CloudPolicyData> cloud_policy_data_;
294 scoped_ptr<CloudPolicySubsystem> cloud_policy_subsystem_; 248 scoped_ptr<CloudPolicySubsystem> cloud_policy_subsystem_;
295 scoped_ptr<PrefService> prefs_; 249 scoped_ptr<PrefService> prefs_;
296 CloudPolicyCacheBase* cache_; 250 CloudPolicyCacheBase* cache_;
297 251
298 scoped_ptr<TestingPolicyURLFetcherFactory> factory_; 252 scoped_ptr<TestingPolicyURLFetcherFactory> factory_;
299 253
300 DISALLOW_COPY_AND_ASSIGN(CloudPolicySubsystemTestBase); 254 DISALLOW_COPY_AND_ASSIGN(CloudPolicySubsystemTestBase);
301 }; 255 };
302 256
303 // A parameterized test case that simulates 100 failed registration attempts, 257 // A parameterized test case that simulates 100 failed registration attempts,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 VerifyTest("http://www.chromium.org"); 292 VerifyTest("http://www.chromium.org");
339 } 293 }
340 294
341 // A random sample of error code pairs. Note that the following policy error 295 // A random sample of error code pairs. Note that the following policy error
342 // codes (401, 403, 410) make the policy subsystem to try and reregister, and 296 // codes (401, 403, 410) make the policy subsystem to try and reregister, and
343 // that is not expected in these tests. 297 // that is not expected in these tests.
344 INSTANTIATE_TEST_CASE_P( 298 INSTANTIATE_TEST_CASE_P(
345 CloudPolicySubsystemCombinedTestInstance, 299 CloudPolicySubsystemCombinedTestInstance,
346 CloudPolicySubsystemCombinedTest, 300 CloudPolicySubsystemCombinedTest,
347 testing::Values( 301 testing::Values(
348 CombinedTestDesc(403, 400), 302 CombinedTestDesc(403, 400),
pastarmovj 2011/07/06 12:11:57 I think you can use the constants from the policy
gfeher 2011/07/06 15:14:20 Those are actually private variables there, so thi
pastarmovj 2011/07/06 15:36:01 My only reasoning is that these values are known t
gfeher 2011/07/07 13:51:00 Discussed off-line, I'll create another CL for thi
349 CombinedTestDesc(403, 404), 303 CombinedTestDesc(403, 404),
350 CombinedTestDesc(403, 412), 304 CombinedTestDesc(403, 412),
351 CombinedTestDesc(403, 500), 305 CombinedTestDesc(403, 500),
352 CombinedTestDesc(403, 503), 306 CombinedTestDesc(403, 503),
353 CombinedTestDesc(403, 902), 307 CombinedTestDesc(403, 902),
354 CombinedTestDesc(902, 400), 308 CombinedTestDesc(902, 400),
355 CombinedTestDesc(503, 404), 309 CombinedTestDesc(503, 404),
356 CombinedTestDesc(500, 412), 310 CombinedTestDesc(500, 412),
357 CombinedTestDesc(412, 500), 311 CombinedTestDesc(412, 500),
358 CombinedTestDesc(404, 503), 312 CombinedTestDesc(404, 503),
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 ExecuteTest(); 390 ExecuteTest();
437 VerifyTest("http://www.youtube.com"); 391 VerifyTest("http://www.youtube.com");
438 } 392 }
439 393
440 INSTANTIATE_TEST_CASE_P( 394 INSTANTIATE_TEST_CASE_P(
441 CloudPolicySubsystemPolicyReregisterTestInstance, 395 CloudPolicySubsystemPolicyReregisterTestInstance,
442 CloudPolicySubsystemPolicyReregisterTest, 396 CloudPolicySubsystemPolicyReregisterTest,
443 testing::Values(401, 403, 410)); 397 testing::Values(401, 403, 410));
444 398
445 } // policy 399 } // policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698