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

Side by Side Diff: chrome/browser/policy/cloud_policy_controller_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: rebase 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 "chrome/browser/policy/cloud_policy_controller.h" 5 #include "chrome/browser/policy/cloud_policy_controller.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
9 #include "chrome/browser/policy/device_management_service.h" 9 #include "chrome/browser/policy/cloud_policy_data_store.h"
10 #include "chrome/browser/policy/device_token_fetcher.h" 10 #include "chrome/browser/policy/device_token_fetcher.h"
11 #include "chrome/browser/policy/logging_work_scheduler.h" 11 #include "chrome/browser/policy/logging_work_scheduler.h"
12 #include "chrome/browser/policy/mock_configuration_policy_store.h"
13 #include "chrome/browser/policy/mock_device_management_backend.h" 12 #include "chrome/browser/policy/mock_device_management_backend.h"
14 #include "chrome/browser/policy/mock_device_management_service.h" 13 #include "chrome/browser/policy/mock_device_management_service.h"
15 #include "chrome/browser/policy/policy_notifier.h" 14 #include "chrome/browser/policy/policy_notifier.h"
16 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
17 #include "chrome/browser/policy/user_policy_cache.h" 15 #include "chrome/browser/policy/user_policy_cache.h"
18 #include "content/browser/browser_thread.h" 16 #include "content/browser/browser_thread.h"
19 #include "policy/policy_constants.h" 17 #include "policy/policy_constants.h"
20 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
22 20
23 const char kTestToken[] = "cloud_policy_controller_test_auth_token";
24
25 namespace policy { 21 namespace policy {
26 22
27 namespace em = enterprise_management;
28
29 using ::testing::_; 23 using ::testing::_;
30 using ::testing::AtLeast; 24 using ::testing::AtLeast;
31 using ::testing::InSequence; 25 using ::testing::InSequence;
32 using ::testing::Mock; 26 using ::testing::Mock;
33 using ::testing::Return; 27 using ::testing::Return;
34 28
35 class MockCloudPolicyIdentityStrategy : public CloudPolicyIdentityStrategy {
36 public:
37 MockCloudPolicyIdentityStrategy() {}
38 virtual ~MockCloudPolicyIdentityStrategy() {}
39
40 MOCK_METHOD0(GetDeviceToken, std::string());
41 MOCK_METHOD0(GetDeviceID, std::string());
42 MOCK_METHOD0(GetMachineID, std::string());
43 MOCK_METHOD0(GetMachineModel, std::string());
44 MOCK_METHOD0(GetPolicyType, std::string());
45 MOCK_METHOD0(GetPolicyRegisterType, em::DeviceRegisterRequest_Type());
46
47 MOCK_METHOD2(GetCredentials, bool(std::string*, std::string*));
48 virtual void OnDeviceTokenAvailable(const std::string&) {}
49
50 private:
51 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyIdentityStrategy);
52 };
53
54 ACTION_P2(MockCloudPolicyIdentityStrategyGetCredentials, username, auth_token) {
55 *arg0 = username;
56 *arg1 = auth_token;
57 return true;
58 }
59
60 class MockDeviceTokenFetcher : public DeviceTokenFetcher { 29 class MockDeviceTokenFetcher : public DeviceTokenFetcher {
61 public: 30 public:
62 explicit MockDeviceTokenFetcher(CloudPolicyCacheBase* cache) 31 explicit MockDeviceTokenFetcher(CloudPolicyCacheBase* cache)
63 : DeviceTokenFetcher(NULL, cache, NULL) {} 32 : DeviceTokenFetcher(NULL, cache, NULL, NULL) {}
64 virtual ~MockDeviceTokenFetcher() {} 33 virtual ~MockDeviceTokenFetcher() {}
65 34
66 MOCK_METHOD0(GetDeviceToken, const std::string&()); 35 MOCK_METHOD0(FetchToken, void());
67 MOCK_METHOD5(FetchToken,
68 void(const std::string&, const std::string&,
69 em::DeviceRegisterRequest_Type,
70 const std::string&, const std::string&));
71 MOCK_METHOD0(SetUnmanagedState, void()); 36 MOCK_METHOD0(SetUnmanagedState, void());
72 37
73 private: 38 private:
74 DISALLOW_COPY_AND_ASSIGN(MockDeviceTokenFetcher); 39 DISALLOW_COPY_AND_ASSIGN(MockDeviceTokenFetcher);
75 }; 40 };
76 41
77 class CloudPolicyControllerTest : public testing::Test { 42 class CloudPolicyControllerTest : public testing::Test {
78 public: 43 public:
79 CloudPolicyControllerTest() 44 CloudPolicyControllerTest()
80 : ui_thread_(BrowserThread::UI, &loop_), 45 : ui_thread_(BrowserThread::UI, &loop_),
81 file_thread_(BrowserThread::FILE, &loop_) {} 46 file_thread_(BrowserThread::FILE, &loop_) {}
82 47
83 virtual ~CloudPolicyControllerTest() {} 48 virtual ~CloudPolicyControllerTest() {}
84 49
85 virtual void SetUp() { 50 virtual void SetUp() {
86 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); 51 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir());
87 cache_.reset(new UserPolicyCache( 52 cache_.reset(new UserPolicyCache(
88 temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest"))); 53 temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest")));
89 token_fetcher_.reset(new MockDeviceTokenFetcher(cache_.get())); 54 token_fetcher_.reset(new MockDeviceTokenFetcher(cache_.get()));
90 service_.set_backend(&backend_); 55 service_.set_backend(&backend_);
56 data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies());
91 } 57 }
92 58
93 virtual void TearDown() { 59 virtual void TearDown() {
94 controller_.reset(); // Unregisters observers. 60 controller_.reset(); // Unregisters observers.
61 data_store_.reset();
95 } 62 }
96 63
97 void CreateNewController() { 64 void CreateNewController() {
98 controller_.reset(new CloudPolicyController( 65 controller_.reset(new CloudPolicyController(
99 &service_, cache_.get(), token_fetcher_.get(), &identity_strategy_, 66 &service_, cache_.get(), token_fetcher_.get(), data_store_.get(),
100 &notifier_, new DummyWorkScheduler)); 67 &notifier_, new DummyWorkScheduler));
101 } 68 }
102 69
103 void ExpectHasSpdyPolicy() { 70 void ExpectHasSpdyPolicy() {
104 FundamentalValue expected(true); 71 FundamentalValue expected(true);
105 const PolicyMap* policy_map = cache_->policy( 72 const PolicyMap* policy_map = cache_->policy(
106 CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY); 73 CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY);
107 ASSERT_TRUE(Value::Equals(&expected, policy_map->Get(kPolicyDisableSpdy))); 74 ASSERT_TRUE(Value::Equals(&expected, policy_map->Get(kPolicyDisableSpdy)));
108 } 75 }
109 76
110 void SetupIdentityStrategy(
111 const std::string& device_token,
112 const std::string& device_id,
113 const std::string& machine_id,
114 const std::string& machine_model,
115 const std::string& policy_type,
116 const em::DeviceRegisterRequest_Type& policy_register_type,
117 const std::string& user_name,
118 const std::string& auth_token) {
119 EXPECT_CALL(identity_strategy_, GetDeviceToken()).WillRepeatedly(
120 Return(device_token));
121 EXPECT_CALL(identity_strategy_, GetDeviceID()).WillRepeatedly(
122 Return(device_id));
123 EXPECT_CALL(identity_strategy_, GetMachineID()).WillRepeatedly(
124 Return(machine_id));
125 EXPECT_CALL(identity_strategy_, GetMachineModel()).WillRepeatedly(
126 Return(machine_model));
127 EXPECT_CALL(identity_strategy_, GetPolicyType()).WillRepeatedly(
128 Return(policy_type));
129 EXPECT_CALL(identity_strategy_, GetPolicyRegisterType()).WillRepeatedly(
130 Return(policy_register_type));
131 if (!user_name.empty()) {
132 EXPECT_CALL(identity_strategy_, GetCredentials(_, _)).WillRepeatedly(
133 MockCloudPolicyIdentityStrategyGetCredentials(user_name, auth_token));
134 }
135 }
136
137 void StopMessageLoop() { 77 void StopMessageLoop() {
138 loop_.QuitNow(); 78 loop_.QuitNow();
139 } 79 }
140 80
141 protected: 81 protected:
142 scoped_ptr<CloudPolicyCacheBase> cache_; 82 scoped_ptr<CloudPolicyCacheBase> cache_;
143 scoped_ptr<CloudPolicyController> controller_; 83 scoped_ptr<CloudPolicyController> controller_;
144 scoped_ptr<MockDeviceTokenFetcher> token_fetcher_; 84 scoped_ptr<MockDeviceTokenFetcher> token_fetcher_;
145 MockCloudPolicyIdentityStrategy identity_strategy_; 85 scoped_ptr<CloudPolicyDataStore> data_store_;
146 MockDeviceManagementBackend backend_; 86 MockDeviceManagementBackend backend_;
147 MockDeviceManagementService service_; 87 MockDeviceManagementService service_;
148 PolicyNotifier notifier_; 88 PolicyNotifier notifier_;
149 ScopedTempDir temp_user_data_dir_; 89 ScopedTempDir temp_user_data_dir_;
150 MessageLoop loop_; 90 MessageLoop loop_;
151 91
152 private: 92 private:
153 BrowserThread ui_thread_; 93 BrowserThread ui_thread_;
154 BrowserThread file_thread_; 94 BrowserThread file_thread_;
155 95
156 DISALLOW_COPY_AND_ASSIGN(CloudPolicyControllerTest); 96 DISALLOW_COPY_AND_ASSIGN(CloudPolicyControllerTest);
157 }; 97 };
158 98
159 // If a device token is present when the controller starts up, it should 99 // If a device token is present when the controller starts up, it should
160 // fetch and apply policy. 100 // fetch and apply policy.
161 TEST_F(CloudPolicyControllerTest, StartupWithDeviceToken) { 101 TEST_F(CloudPolicyControllerTest, StartupWithDeviceToken) {
162 SetupIdentityStrategy("fake_device_token", "device_id", "machine_id", 102 data_store_->SetupForTesting("fake_device_token", "device_id", "", "",
163 "machine_model", "google/chromeos/user", 103 true);
164 em::DeviceRegisterRequest::USER, "", "");
165 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(DoAll( 104 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(DoAll(
166 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), 105 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop),
167 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); 106 MockDeviceManagementBackendSucceedSpdyCloudPolicy()));
168 CreateNewController(); 107 CreateNewController();
169 loop_.RunAllPending(); 108 loop_.RunAllPending();
170 ExpectHasSpdyPolicy(); 109 ExpectHasSpdyPolicy();
171 } 110 }
172 111
173 // If no device token is present when the controller starts up, it should 112 // If no device token is present when the controller starts up, it should
174 // instruct the token_fetcher_ to fetch one. 113 // instruct the token_fetcher_ to fetch one.
175 TEST_F(CloudPolicyControllerTest, StartupWithoutDeviceToken) { 114 TEST_F(CloudPolicyControllerTest, StartupWithoutDeviceToken) {
176 SetupIdentityStrategy("", "device_id", "machine_id", "machine_model", 115 data_store_->SetupForTesting("", "device_id", "a@b.com", "auth_token",
177 "google/chromeos/user", em::DeviceRegisterRequest::USER, 116 true);
178 "a@b.com", "auth_token"); 117 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1);
179 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _, _, _, _)).Times(1);
180 CreateNewController(); 118 CreateNewController();
181 loop_.RunAllPending(); 119 loop_.RunAllPending();
182 } 120 }
183 121
184 // If the current user belongs to a known non-managed domain, no token fetch 122 // If the current user belongs to a known non-managed domain, no token fetch
185 // should be initiated. 123 // should be initiated.
186 TEST_F(CloudPolicyControllerTest, StartupUnmanagedUser) { 124 TEST_F(CloudPolicyControllerTest, StartupUnmanagedUser) {
187 SetupIdentityStrategy("", "device_id", "machine_id", "machine_mode", 125 data_store_->SetupForTesting("", "device_id", "DannoHelper@gmail.com",
188 "google/chromeos/user", em::DeviceRegisterRequest::USER, 126 "auth_token", true);
189 "DannoHelper@gmail.com", "auth_token"); 127 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(0);
190 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _, _, _, _)).Times(0);
191 CreateNewController(); 128 CreateNewController();
192 loop_.RunAllPending(); 129 loop_.RunAllPending();
193 } 130 }
194 131
195 // After policy has been fetched successfully, a new fetch should be triggered 132 // After policy has been fetched successfully, a new fetch should be triggered
196 // after the refresh interval has timed out. 133 // after the refresh interval has timed out.
197 TEST_F(CloudPolicyControllerTest, RefreshAfterSuccessfulPolicy) { 134 TEST_F(CloudPolicyControllerTest, RefreshAfterSuccessfulPolicy) {
198 SetupIdentityStrategy("device_token", "device_id", "machine_id", 135 data_store_->SetupForTesting("device_token", "device_id",
199 "machine_model", "google/chromeos/user", 136 "DannoHelperDelegate@b.com",
200 em::DeviceRegisterRequest::USER, 137 "auth_token", true);
201 "DannoHelperDelegate@b.com", "auth_token");
202 { 138 {
203 InSequence s; 139 InSequence s;
204 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( 140 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(
205 MockDeviceManagementBackendSucceedSpdyCloudPolicy()); 141 MockDeviceManagementBackendSucceedSpdyCloudPolicy());
206 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(DoAll( 142 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(DoAll(
207 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), 143 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop),
208 MockDeviceManagementBackendFailPolicy( 144 MockDeviceManagementBackendFailPolicy(
209 DeviceManagementBackend::kErrorRequestFailed))); 145 DeviceManagementBackend::kErrorRequestFailed)));
210 } 146 }
211 CreateNewController(); 147 CreateNewController();
212 loop_.RunAllPending(); 148 loop_.RunAllPending();
213 ExpectHasSpdyPolicy(); 149 ExpectHasSpdyPolicy();
214 } 150 }
215 151
216 // If policy fetching failed, it should be retried. 152 // If policy fetching failed, it should be retried.
217 TEST_F(CloudPolicyControllerTest, RefreshAfterError) { 153 TEST_F(CloudPolicyControllerTest, RefreshAfterError) {
218 SetupIdentityStrategy("device_token", "device_id", "machine_id", 154 data_store_->SetupForTesting("device_token", "device_id",
219 "machine_model", "google/chromeos/user", 155 "DannoHelperDelegateImpl@b.com",
220 em::DeviceRegisterRequest::USER, 156 "auth_token", true);
221 "DannoHelperDelegateImpl@b.com", "auth_token");
222 { 157 {
223 InSequence s; 158 InSequence s;
224 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( 159 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(
225 MockDeviceManagementBackendFailPolicy( 160 MockDeviceManagementBackendFailPolicy(
226 DeviceManagementBackend::kErrorRequestFailed)); 161 DeviceManagementBackend::kErrorRequestFailed));
227 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(DoAll( 162 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(DoAll(
228 InvokeWithoutArgs(this, 163 InvokeWithoutArgs(this,
229 &CloudPolicyControllerTest::StopMessageLoop), 164 &CloudPolicyControllerTest::StopMessageLoop),
230 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); 165 MockDeviceManagementBackendSucceedSpdyCloudPolicy()));
231 } 166 }
232 CreateNewController(); 167 CreateNewController();
233 loop_.RunAllPending(); 168 loop_.RunAllPending();
234 ExpectHasSpdyPolicy(); 169 ExpectHasSpdyPolicy();
235 } 170 }
236 171
237 // If the backend reports that the device token was invalid, the controller 172 // If the backend reports that the device token was invalid, the controller
238 // should instruct the token fetcher to fetch a new token. 173 // should instruct the token fetcher to fetch a new token.
239 TEST_F(CloudPolicyControllerTest, InvalidToken) { 174 TEST_F(CloudPolicyControllerTest, InvalidToken) {
240 SetupIdentityStrategy("device_token", "device_id", "machine_id", 175 data_store_->SetupForTesting("device_token", "device_id",
241 "machine_model", "google/chromeos/user", 176 "standup@ten.am", "auth", true);
242 em::DeviceRegisterRequest::USER,
243 "standup@ten.am", "auth");
244 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( 177 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(
245 MockDeviceManagementBackendFailPolicy( 178 MockDeviceManagementBackendFailPolicy(
246 DeviceManagementBackend::kErrorServiceManagementTokenInvalid)); 179 DeviceManagementBackend::kErrorServiceManagementTokenInvalid));
247 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _, _, _, _)).Times(1); 180 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1);
248 CreateNewController(); 181 CreateNewController();
249 loop_.RunAllPending(); 182 loop_.RunAllPending();
250 } 183 }
251 184
252 // If the backend reports that the device is unknown to the server, the 185 // If the backend reports that the device is unknown to the server, the
253 // controller should instruct the token fetcher to fetch a new token. 186 // controller should instruct the token fetcher to fetch a new token.
254 TEST_F(CloudPolicyControllerTest, DeviceNotFound) { 187 TEST_F(CloudPolicyControllerTest, DeviceNotFound) {
255 SetupIdentityStrategy("device_token", "device_id", "machine_id", 188 data_store_->SetupForTesting("device_token", "device_id",
256 "machine_model", "google/chromeos/user", 189 "me@you.com", "auth", true);
257 em::DeviceRegisterRequest::USER,
258 "me@you.com", "auth");
259 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( 190 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(
260 MockDeviceManagementBackendFailPolicy( 191 MockDeviceManagementBackendFailPolicy(
261 DeviceManagementBackend::kErrorServiceDeviceNotFound)); 192 DeviceManagementBackend::kErrorServiceDeviceNotFound));
262 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _, _, _, _)).Times(1); 193 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1);
263 CreateNewController(); 194 CreateNewController();
264 loop_.RunAllPending(); 195 loop_.RunAllPending();
265 } 196 }
266 197
267 // If the backend reports that the device is no longer managed, the controller 198 // If the backend reports that the device is no longer managed, the controller
268 // should instruct the token fetcher to fetch a new token (which will in turn 199 // should instruct the token fetcher to fetch a new token (which will in turn
269 // set and persist the correct 'unmanaged' state). 200 // set and persist the correct 'unmanaged' state).
270 TEST_F(CloudPolicyControllerTest, NoLongerManaged) { 201 TEST_F(CloudPolicyControllerTest, NoLongerManaged) {
271 SetupIdentityStrategy("device_token", "device_id", "machine_id", 202 data_store_->SetupForTesting("device_token", "device_id",
272 "machine_model", "google/chromeos/user", 203 "who@what.com", "auth", true);
273 em::DeviceRegisterRequest::USER,
274 "who@what.com", "auth");
275 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( 204 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(
276 MockDeviceManagementBackendFailPolicy( 205 MockDeviceManagementBackendFailPolicy(
277 DeviceManagementBackend::kErrorServiceManagementNotSupported)); 206 DeviceManagementBackend::kErrorServiceManagementNotSupported));
278 EXPECT_CALL(*token_fetcher_.get(), SetUnmanagedState()).Times(1); 207 EXPECT_CALL(*token_fetcher_.get(), SetUnmanagedState()).Times(1);
279 CreateNewController(); 208 CreateNewController();
280 loop_.RunAllPending(); 209 loop_.RunAllPending();
281 } 210 }
282 211
283 } // namespace policy 212 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud_policy_controller.cc ('k') | chrome/browser/policy/cloud_policy_data_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698