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

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

Issue 6537020: Update policy backend and testserver for the newest policy protocol (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more last minute changes Created 9 years, 9 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/browser_thread.h" 9 #include "chrome/browser/browser_thread.h"
10 #include "chrome/browser/policy/cloud_policy_cache.h" 10 #include "chrome/browser/policy/cloud_policy_cache.h"
11 #include "chrome/browser/policy/device_token_fetcher.h"
12 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
11 #include "chrome/browser/policy/mock_configuration_policy_store.h" 13 #include "chrome/browser/policy/mock_configuration_policy_store.h"
12 #include "chrome/browser/policy/mock_device_management_backend.h" 14 #include "chrome/browser/policy/mock_device_management_backend.h"
13 #include "policy/policy_constants.h" 15 #include "policy/policy_constants.h"
14 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
16 18
17 const char kTestToken[] = "cloud_policy_controller_test_auth_token"; 19 const char kTestToken[] = "cloud_policy_controller_test_auth_token";
18 20
19 namespace policy { 21 namespace policy {
20 22
23 namespace em = enterprise_management;
24
21 using ::testing::_; 25 using ::testing::_;
22 using ::testing::AtLeast; 26 using ::testing::AtLeast;
23 using ::testing::InSequence; 27 using ::testing::InSequence;
24 using ::testing::Mock; 28 using ::testing::Mock;
25 using ::testing::Return; 29 using ::testing::Return;
26 30
27 class MockCloudPolicyIdentityStrategy : public CloudPolicyIdentityStrategy { 31 class MockCloudPolicyIdentityStrategy : public CloudPolicyIdentityStrategy {
28 public: 32 public:
29 MockCloudPolicyIdentityStrategy() {} 33 MockCloudPolicyIdentityStrategy() {}
30 virtual ~MockCloudPolicyIdentityStrategy() {} 34 virtual ~MockCloudPolicyIdentityStrategy() {}
31 35
32 MOCK_METHOD0(GetDeviceToken, std::string()); 36 MOCK_METHOD0(GetDeviceToken, std::string());
33 MOCK_METHOD0(GetDeviceID, std::string()); 37 MOCK_METHOD0(GetDeviceID, std::string());
38 MOCK_METHOD0(GetMachineID, std::string());
39 MOCK_METHOD0(GetPolicyType, std::string());
40 MOCK_METHOD0(GetPolicyRegisterType, em::DeviceRegisterRequest_Type());
41
34 MOCK_METHOD2(GetCredentials, bool(std::string*, std::string*)); 42 MOCK_METHOD2(GetCredentials, bool(std::string*, std::string*));
35 virtual void OnDeviceTokenAvailable(const std::string&) {} 43 virtual void OnDeviceTokenAvailable(const std::string&) {}
36 44
37 private: 45 private:
38 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyIdentityStrategy); 46 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyIdentityStrategy);
39 }; 47 };
40 48
41 ACTION_P2(MockCloudPolicyIdentityStrategyGetCredentials, username, auth_token) { 49 ACTION_P2(MockCloudPolicyIdentityStrategyGetCredentials, username, auth_token) {
42 *arg0 = username; 50 *arg0 = username;
43 *arg1 = auth_token; 51 *arg1 = auth_token;
44 return true; 52 return true;
45 } 53 }
46 54
47 class MockDeviceTokenFetcher : public DeviceTokenFetcher { 55 class MockDeviceTokenFetcher : public DeviceTokenFetcher {
48 public: 56 public:
49 explicit MockDeviceTokenFetcher(CloudPolicyCache* cache) 57 explicit MockDeviceTokenFetcher(CloudPolicyCache* cache)
50 : DeviceTokenFetcher(NULL, cache) {} 58 : DeviceTokenFetcher(NULL, cache) {}
51 virtual ~MockDeviceTokenFetcher() {} 59 virtual ~MockDeviceTokenFetcher() {}
52 60
53 MOCK_METHOD0(GetDeviceToken, std::string&()); 61 MOCK_METHOD0(GetDeviceToken, const std::string&());
54 MOCK_METHOD2(FetchToken, void(const std::string&, const std::string&)); 62 MOCK_METHOD4(FetchToken,
63 void(const std::string&, const std::string&,
64 em::DeviceRegisterRequest_Type, const std::string&));
55 65
56 private: 66 private:
57 DISALLOW_COPY_AND_ASSIGN(MockDeviceTokenFetcher); 67 DISALLOW_COPY_AND_ASSIGN(MockDeviceTokenFetcher);
58 }; 68 };
59 69
60 class CloudPolicyControllerTest : public testing::Test { 70 class CloudPolicyControllerTest : public testing::Test {
61 public: 71 public:
62 CloudPolicyControllerTest() 72 CloudPolicyControllerTest()
63 : ui_thread_(BrowserThread::UI, &loop_), 73 : ui_thread_(BrowserThread::UI, &loop_),
64 file_thread_(BrowserThread::FILE, &loop_) {} 74 file_thread_(BrowserThread::FILE, &loop_) {}
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 107
98 void ExpectHasSpdyPolicy() { 108 void ExpectHasSpdyPolicy() {
99 MockConfigurationPolicyStore store; 109 MockConfigurationPolicyStore store;
100 EXPECT_CALL(store, Apply(_, _)).Times(AtLeast(1)); 110 EXPECT_CALL(store, Apply(_, _)).Times(AtLeast(1));
101 cache_->GetManagedPolicyProvider()->Provide(&store); 111 cache_->GetManagedPolicyProvider()->Provide(&store);
102 FundamentalValue expected(true); 112 FundamentalValue expected(true);
103 ASSERT_TRUE(store.Get(kPolicyDisableSpdy) != NULL); 113 ASSERT_TRUE(store.Get(kPolicyDisableSpdy) != NULL);
104 EXPECT_TRUE(store.Get(kPolicyDisableSpdy)->Equals(&expected)); 114 EXPECT_TRUE(store.Get(kPolicyDisableSpdy)->Equals(&expected));
105 } 115 }
106 116
107 void SetupIdentityStrategy(const std::string& device_token, 117 void SetupIdentityStrategy(
108 const std::string& device_id, 118 const std::string& device_token,
109 const std::string& user_name, 119 const std::string& device_id,
110 const std::string& auth_token) { 120 const std::string& machine_id,
121 const std::string& policy_type,
122 const em::DeviceRegisterRequest_Type& policy_register_type,
123 const std::string& user_name,
124 const std::string& auth_token) {
111 EXPECT_CALL(identity_strategy_, GetDeviceToken()).WillRepeatedly( 125 EXPECT_CALL(identity_strategy_, GetDeviceToken()).WillRepeatedly(
112 Return(device_token)); 126 Return(device_token));
113 EXPECT_CALL(identity_strategy_, GetDeviceID()).WillRepeatedly( 127 EXPECT_CALL(identity_strategy_, GetDeviceID()).WillRepeatedly(
114 Return(device_id)); 128 Return(device_id));
129 EXPECT_CALL(identity_strategy_, GetMachineID()).WillRepeatedly(
130 Return(machine_id));
131 EXPECT_CALL(identity_strategy_, GetPolicyType()).WillRepeatedly(
132 Return(policy_type));
133 EXPECT_CALL(identity_strategy_, GetPolicyRegisterType()).WillRepeatedly(
134 Return(policy_register_type));
115 if (!user_name.empty()) { 135 if (!user_name.empty()) {
116 EXPECT_CALL(identity_strategy_, GetCredentials(_, _)).WillRepeatedly( 136 EXPECT_CALL(identity_strategy_, GetCredentials(_, _)).WillRepeatedly(
117 MockCloudPolicyIdentityStrategyGetCredentials(user_name, auth_token)); 137 MockCloudPolicyIdentityStrategyGetCredentials(user_name, auth_token));
118 } 138 }
119 } 139 }
120 140
121 protected: 141 protected:
122 scoped_ptr<CloudPolicyCache> cache_; 142 scoped_ptr<CloudPolicyCache> cache_;
123 scoped_ptr<CloudPolicyController> controller_; 143 scoped_ptr<CloudPolicyController> controller_;
124 scoped_ptr<MockDeviceTokenFetcher> token_fetcher_; 144 scoped_ptr<MockDeviceTokenFetcher> token_fetcher_;
125 MockCloudPolicyIdentityStrategy identity_strategy_; 145 MockCloudPolicyIdentityStrategy identity_strategy_;
126 ScopedTempDir temp_user_data_dir_; 146 ScopedTempDir temp_user_data_dir_;
127 MessageLoop loop_; 147 MessageLoop loop_;
128 148
129 private: 149 private:
130 BrowserThread ui_thread_; 150 BrowserThread ui_thread_;
131 BrowserThread file_thread_; 151 BrowserThread file_thread_;
132 152
133 DISALLOW_COPY_AND_ASSIGN(CloudPolicyControllerTest); 153 DISALLOW_COPY_AND_ASSIGN(CloudPolicyControllerTest);
134 }; 154 };
135 155
136 // If a device token is present when the controller starts up, it should 156 // If a device token is present when the controller starts up, it should
137 // fetch and apply policy. 157 // fetch and apply policy.
138 TEST_F(CloudPolicyControllerTest, StartupWithDeviceToken) { 158 TEST_F(CloudPolicyControllerTest, StartupWithDeviceToken) {
139 SetupIdentityStrategy("fake_device_token", "device_id", "", ""); 159 SetupIdentityStrategy("fake_device_token", "device_id", "machine_id",
160 "google/chromeos/user",
161 em::DeviceRegisterRequest::USER, "", "");
140 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); 162 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
141 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( 163 EXPECT_CALL(*backend, ProcessPolicyRequest(_, _, _, _)).WillOnce(
142 MockDeviceManagementBackendSucceedSpdyCloudPolicy()); 164 MockDeviceManagementBackendSucceedSpdyCloudPolicy());
143 CreateNewController(backend); 165 CreateNewController(backend);
144 loop_.RunAllPending(); 166 loop_.RunAllPending();
145 ExpectHasSpdyPolicy(); 167 ExpectHasSpdyPolicy();
146 } 168 }
147 169
148 // If no device token is present when the controller starts up, it should 170 // If no device token is present when the controller starts up, it should
149 // instruct the token_fetcher_ to fetch one. 171 // instruct the token_fetcher_ to fetch one.
150 TEST_F(CloudPolicyControllerTest, StartupWithoutDeviceToken) { 172 TEST_F(CloudPolicyControllerTest, StartupWithoutDeviceToken) {
151 SetupIdentityStrategy("", "device_id", "a@b.com", "auth_token"); 173 SetupIdentityStrategy("", "device_id", "machine_id", "google/chromeos/user",
152 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1); 174 em::DeviceRegisterRequest::USER,
175 "a@b.com", "auth_token");
176 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _, _, _)).Times(1);
153 CreateNewController(NULL); 177 CreateNewController(NULL);
154 loop_.RunAllPending(); 178 loop_.RunAllPending();
155 } 179 }
156 180
157 // If the current user belongs to a known non-managed domain, no token fetch 181 // If the current user belongs to a known non-managed domain, no token fetch
158 // should be initiated. 182 // should be initiated.
159 TEST_F(CloudPolicyControllerTest, StartupUnmanagedUser) { 183 TEST_F(CloudPolicyControllerTest, StartupUnmanagedUser) {
160 SetupIdentityStrategy("", "device_id", "DannoHelper@gmail.com", "auth_token"); 184 SetupIdentityStrategy("", "device_id", "machine_id", "google/chromeos/user",
161 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(0); 185 em::DeviceRegisterRequest::USER,
186 "DannoHelper@gmail.com", "auth_token");
187 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _, _, _)).Times(0);
162 CreateNewController(NULL); 188 CreateNewController(NULL);
163 loop_.RunAllPending(); 189 loop_.RunAllPending();
164 } 190 }
165 191
166 // After policy has been fetched successfully, a new fetch should be triggered 192 // After policy has been fetched successfully, a new fetch should be triggered
167 // after the refresh interval has timed out. 193 // after the refresh interval has timed out.
168 TEST_F(CloudPolicyControllerTest, RefreshAfterSuccessfulPolicy) { 194 TEST_F(CloudPolicyControllerTest, RefreshAfterSuccessfulPolicy) {
169 SetupIdentityStrategy("device_token", "device_id", 195 SetupIdentityStrategy("device_token", "device_id", "machine_id",
196 "google/chromeos/user", em::DeviceRegisterRequest::USER,
170 "DannoHelperDelegate@b.com", "auth_token"); 197 "DannoHelperDelegate@b.com", "auth_token");
171 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); 198 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
172 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( 199 EXPECT_CALL(*backend, ProcessPolicyRequest(_, _, _, _)).WillOnce(
173 MockDeviceManagementBackendSucceedSpdyCloudPolicy()).WillOnce( 200 MockDeviceManagementBackendSucceedSpdyCloudPolicy()).WillOnce(
174 MockDeviceManagementBackendFailPolicy( 201 MockDeviceManagementBackendFailPolicy(
175 DeviceManagementBackend::kErrorRequestFailed)); 202 DeviceManagementBackend::kErrorRequestFailed));
176 CreateNewController(backend, 0, 0, 0, 1000 * 1000); 203 CreateNewController(backend, 0, 0, 0, 1000 * 1000);
177 loop_.RunAllPending(); 204 loop_.RunAllPending();
178 ExpectHasSpdyPolicy(); 205 ExpectHasSpdyPolicy();
179 } 206 }
180 207
181 // If poliy fetching failed, it should be retried. 208 // If poliy fetching failed, it should be retried.
182 TEST_F(CloudPolicyControllerTest, RefreshAfterError) { 209 TEST_F(CloudPolicyControllerTest, RefreshAfterError) {
183 SetupIdentityStrategy("device_token", "device_id", 210 SetupIdentityStrategy("device_token", "device_id", "machine_id",
211 "google/chromeos/user", em::DeviceRegisterRequest::USER,
184 "DannoHelperDelegateImpl@b.com", "auth_token"); 212 "DannoHelperDelegateImpl@b.com", "auth_token");
185 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); 213 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
186 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( 214 EXPECT_CALL(*backend, ProcessPolicyRequest(_, _, _, _)).WillOnce(
187 MockDeviceManagementBackendFailPolicy( 215 MockDeviceManagementBackendFailPolicy(
188 DeviceManagementBackend::kErrorRequestFailed)).WillOnce( 216 DeviceManagementBackend::kErrorRequestFailed)).WillOnce(
189 MockDeviceManagementBackendSucceedSpdyCloudPolicy()); 217 MockDeviceManagementBackendSucceedSpdyCloudPolicy());
190 CreateNewController(backend, 1000 * 1000, 0, 0, 0); 218 CreateNewController(backend, 1000 * 1000, 0, 0, 0);
191 loop_.RunAllPending(); 219 loop_.RunAllPending();
192 ExpectHasSpdyPolicy(); 220 ExpectHasSpdyPolicy();
193 } 221 }
194 222
195 // If the backend reports that the device token was invalid, the controller 223 // If the backend reports that the device token was invalid, the controller
196 // should instruct the token fetcher to fetch a new token. 224 // should instruct the token fetcher to fetch a new token.
197 TEST_F(CloudPolicyControllerTest, InvalidToken) { 225 TEST_F(CloudPolicyControllerTest, InvalidToken) {
198 SetupIdentityStrategy("device_token", "device_id", "standup@ten.am", "auth"); 226 SetupIdentityStrategy("device_token", "device_id", "machine_id",
227 "google/chromeos/user", em::DeviceRegisterRequest::USER,
228 "standup@ten.am", "auth");
199 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); 229 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
200 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( 230 EXPECT_CALL(*backend, ProcessPolicyRequest(_, _, _, _)).WillOnce(
201 MockDeviceManagementBackendFailPolicy( 231 MockDeviceManagementBackendFailPolicy(
202 DeviceManagementBackend::kErrorServiceManagementTokenInvalid)); 232 DeviceManagementBackend::kErrorServiceManagementTokenInvalid));
203 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1); 233 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _, _, _)).Times(1);
204 CreateNewController(backend); 234 CreateNewController(backend);
205 loop_.RunAllPending(); 235 loop_.RunAllPending();
206 } 236 }
207 237
208 // If the backend reports that the device is unknown to the server, the 238 // If the backend reports that the device is unknown to the server, the
209 // controller should instruct the token fetcher to fetch a new token. 239 // controller should instruct the token fetcher to fetch a new token.
210 TEST_F(CloudPolicyControllerTest, DeviceNotFound) { 240 TEST_F(CloudPolicyControllerTest, DeviceNotFound) {
211 SetupIdentityStrategy("device_token", "device_id", "me@you.com", "auth"); 241 SetupIdentityStrategy("device_token", "device_id", "machine_id",
242 "google/chromeos/user", em::DeviceRegisterRequest::USER,
243 "me@you.com", "auth");
212 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); 244 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
213 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( 245 EXPECT_CALL(*backend, ProcessPolicyRequest(_, _, _, _)).WillOnce(
214 MockDeviceManagementBackendFailPolicy( 246 MockDeviceManagementBackendFailPolicy(
215 DeviceManagementBackend::kErrorServiceDeviceNotFound)); 247 DeviceManagementBackend::kErrorServiceDeviceNotFound));
216 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1); 248 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _, _, _)).Times(1);
217 CreateNewController(backend); 249 CreateNewController(backend);
218 loop_.RunAllPending(); 250 loop_.RunAllPending();
219 } 251 }
220 252
221 // If the backend reports that the device is no longer managed, the controller 253 // If the backend reports that the device is no longer managed, the controller
222 // shoud instruct the token fetcher to fetch a new token (which will in turn 254 // shoud instruct the token fetcher to fetch a new token (which will in turn
223 // set and persist the correct 'unmanaged' state). 255 // set and persist the correct 'unmanaged' state).
224 TEST_F(CloudPolicyControllerTest, NoLongerManaged) { 256 TEST_F(CloudPolicyControllerTest, NoLongerManaged) {
225 SetupIdentityStrategy("device_token", "device_id", "who@what.com", "auth"); 257 SetupIdentityStrategy("device_token", "device_id", "machine_id",
258 "google/chromeos/user", em::DeviceRegisterRequest::USER,
259 "who@what.com", "auth");
226 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); 260 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
227 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( 261 EXPECT_CALL(*backend, ProcessPolicyRequest(_, _, _, _)).WillOnce(
228 MockDeviceManagementBackendFailPolicy( 262 MockDeviceManagementBackendFailPolicy(
229 DeviceManagementBackend::kErrorServiceManagementNotSupported)); 263 DeviceManagementBackend::kErrorServiceManagementNotSupported));
230 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1); 264 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _, _, _)).Times(1);
231 CreateNewController(backend); 265 CreateNewController(backend);
232 loop_.RunAllPending(); 266 loop_.RunAllPending();
233 } 267 }
234 268
235 // If the server doesn't support the new protocol, the controller should fall
236 // back to the old protocol.
237 TEST_F(CloudPolicyControllerTest, FallbackToOldProtocol) {
238 SetupIdentityStrategy("device_token", "device_id", "a@b.com", "auth");
239 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
240 // If the CloudPolicyRequest fails with kErrorRequestInvalid...
241 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce(
242 MockDeviceManagementBackendFailPolicy(
243 DeviceManagementBackend::kErrorRequestInvalid));
244 // ...the client should fall back to a classic PolicyRequest,
245 // and remember this fallback for any future request,
246 // both after successful fetches and after errors.
247 EXPECT_CALL(*backend, ProcessPolicyRequest(_, _, _, _)).WillOnce(
248 MockDeviceManagementBackendSucceedBooleanPolicy(
249 key::kDisableSpdy, true)).WillOnce(
250 MockDeviceManagementBackendFailPolicy(
251 DeviceManagementBackend::kErrorHttpStatus)).WillOnce(
252 Return());
253 CreateNewController(backend, 0, 0, 0, 0);
254 loop_.RunAllPending();
255 ExpectHasSpdyPolicy();
256 }
257
258 } // namespace policy 269 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud_policy_controller.cc ('k') | chrome/browser/policy/cloud_policy_identity_strategy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698