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

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

Issue 9109009: Introduce CloudPolicyClient (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 7 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) 2012 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 "chrome/browser/policy/cloud_policy_client.h"
6
7 #include "base/compiler_specific.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/policy/mock_device_management_service.h"
10 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using testing::Return;
15 using testing::SaveArg;
16 using testing::StrictMock;
17 using testing::_;
18
19 namespace em = enterprise_management;
20
21 namespace policy {
22
23 namespace {
Joao da Silva 2012/05/22 21:23:36 Nit: newline
Mattias Nissler (ping if slow) 2012/05/24 10:12:01 Done.
24 const char kMachineID[] = "fake-machine-id";
25 const char kMachineModel[] = "fake-machine-model";
26 const char kOAuthToken[] = "fake-oauth-token";
27 const char kDMToken[] = "fake-dm-token";
28
29 class MockObserver : public CloudPolicyClient::Observer {
30 public:
31 virtual ~MockObserver() {}
32
33 MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*));
34 MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*));
35 MOCK_METHOD1(OnClientError, void(CloudPolicyClient*));
Joao da Silva 2012/05/22 21:23:36 Nit: similar mocks declare DISALLOW_COPY_AND_ASSIG
Mattias Nissler (ping if slow) 2012/05/24 10:12:01 Done.
36 };
37
38 class MockStatusProvider : public CloudPolicyClient::StatusProvider {
39 public:
40 virtual ~MockStatusProvider() {}
41
42 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest* status));
43 MOCK_METHOD1(GetSessionStatus, bool(em::SessionStatusReportRequest* status));
44 MOCK_METHOD0(OnSubmittedSuccessfully, void(void));
45 };
46
47 MATCHER_P(MatchProto, expected, "matches protobuf") {
48 return arg.SerializePartialAsString() == expected.SerializePartialAsString();
49 }
50
51 } // namespace
52
53 class CloudPolicyClientTest : public testing::Test {
54 protected:
55 CloudPolicyClientTest()
56 : client_id_("fake-client-id") {
57 em::DeviceRegisterRequest* register_request =
58 registration_request_.mutable_register_request();
59 register_request->set_type(em::DeviceRegisterRequest::USER);
60 register_request->set_machine_id(kMachineID);
61 register_request->set_machine_model(kMachineModel);
62 registration_response_.mutable_register_response()->
63 set_device_management_token(kDMToken);
64
65 em::PolicyFetchRequest* policy_fetch_request =
66 policy_request_.mutable_policy_request()->add_request();
67 policy_fetch_request->set_policy_type(dm_protocol::kChromeUserPolicyType);
68 policy_fetch_request->set_signature_type(em::PolicyFetchRequest::SHA1_RSA);
69 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
70 "fake-policy-data");
71
72 unregistration_request_.mutable_unregister_request();
73 unregistration_response_.mutable_unregister_response();
74 }
75
76 virtual void SetUp() OVERRIDE {
77 EXPECT_CALL(status_provider_, GetDeviceStatus(_))
78 .WillRepeatedly(Return(false));
79 EXPECT_CALL(status_provider_, GetSessionStatus(_))
80 .WillRepeatedly(Return(false));
81 CreateClient(USER_AFFILIATION_NONE, POLICY_SCOPE_USER);
82 }
83
84 virtual void TearDown() OVERRIDE {
85 client_->RemoveObserver(&observer_);
86 }
87
88 void Register() {
89 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
90 client_->SetupRegistration(kDMToken, client_id_);
91 }
92
93 void CreateClient(UserAffiliation user_affiliation, PolicyScope scope) {
94 if (client_.get())
95 client_->RemoveObserver(&observer_);
96
97 client_.reset(new CloudPolicyClient(kMachineID, kMachineModel,
98 user_affiliation, scope,
99 &status_provider_, &service_));
100 client_->AddObserver(&observer_);
101 }
102
103 void ExpectRegistration(const std::string& oauth_token) {
104 EXPECT_CALL(service_,
105 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
106 .WillOnce(service_.SucceedJob(registration_response_));
107 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister,
108 "", oauth_token, "", "", _,
109 MatchProto(registration_request_)))
110 .WillOnce(SaveArg<5>(&client_id_));
111 }
112
113 void ExpectPolicyFetch(const std::string& dm_token,
114 const std::string& user_affiliation) {
115 EXPECT_CALL(service_,
116 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
117 .WillOnce(service_.SucceedJob(policy_response_));
118 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestPolicy,
119 "", "", dm_token, user_affiliation,
120 client_id_,
121 MatchProto(policy_request_)));
122 }
123
124 void ExpectUnregistration(const std::string& dm_token) {
125 EXPECT_CALL(service_,
126 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION))
127 .WillOnce(service_.SucceedJob(unregistration_response_));
128 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestUnregister,
129 "", "", dm_token, "", client_id_,
130 MatchProto(unregistration_request_)));
131 }
132
133 void CheckPolicyResponse() {
134 ASSERT_TRUE(client_->policy());
135 EXPECT_THAT(*client_->policy(),
136 MatchProto(policy_response_.policy_response().response(0)));
137 }
138
139 // Request protobufs used as expectations for the client requests.
140 em::DeviceManagementRequest registration_request_;
141 em::DeviceManagementRequest policy_request_;
142 em::DeviceManagementRequest unregistration_request_;
143
144 // Protobufs used in successful responses.
145 em::DeviceManagementResponse registration_response_;
146 em::DeviceManagementResponse policy_response_;
147 em::DeviceManagementResponse unregistration_response_;
148
149 std::string client_id_;
150 MockDeviceManagementService service_;
151 StrictMock<MockStatusProvider> status_provider_;
152 StrictMock<MockObserver> observer_;
153 scoped_ptr<CloudPolicyClient> client_;
154 };
155
156 TEST_F(CloudPolicyClientTest, Init) {
157 EXPECT_CALL(service_, CreateJob(_)).Times(0);
158 EXPECT_FALSE(client_->is_registered());
159 EXPECT_FALSE(client_->policy());
160 }
161
162 TEST_F(CloudPolicyClientTest, SetupRegistrationAndPolicyFetch) {
163 EXPECT_CALL(service_, CreateJob(_)).Times(0);
164 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
165 client_->SetupRegistration(kDMToken, client_id_);
166 EXPECT_TRUE(client_->is_registered());
167 EXPECT_FALSE(client_->policy());
168
169 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
170 EXPECT_CALL(observer_, OnPolicyFetched(_));
171 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
172 client_->FetchPolicy();
173 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
174 CheckPolicyResponse();
175 }
176
177 TEST_F(CloudPolicyClientTest, RegistrationAndPolicyFetch) {
178 ExpectRegistration(kOAuthToken);
179 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
180 client_->Register(kOAuthToken);
181 EXPECT_TRUE(client_->is_registered());
182 EXPECT_FALSE(client_->policy());
183 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
184
185 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
186 EXPECT_CALL(observer_, OnPolicyFetched(_));
187 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
188 client_->FetchPolicy();
189 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
190 CheckPolicyResponse();
191 }
192
193 TEST_F(CloudPolicyClientTest, RegistrationNoToken) {
194 registration_response_.mutable_register_response()->
195 clear_device_management_token();
196 ExpectRegistration(kOAuthToken);
197 EXPECT_CALL(observer_, OnClientError(_));
198 client_->Register(kOAuthToken);
199 EXPECT_FALSE(client_->is_registered());
200 EXPECT_FALSE(client_->policy());
201 EXPECT_EQ(DM_STATUS_RESPONSE_DECODING_ERROR, client_->status());
202 }
203
204 TEST_F(CloudPolicyClientTest, RegistrationFailure) {
205 EXPECT_CALL(service_,
206 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
207 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
208 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
209 EXPECT_CALL(observer_, OnClientError(_));
210 client_->Register(kOAuthToken);
211 EXPECT_FALSE(client_->is_registered());
212 EXPECT_FALSE(client_->policy());
213 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status());
214 }
215
216 TEST_F(CloudPolicyClientTest, PolicyUpdate) {
217 Register();
218
219 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
220 EXPECT_CALL(observer_, OnPolicyFetched(_));
221 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
222 client_->FetchPolicy();
223 CheckPolicyResponse();
224
225 policy_response_.mutable_policy_response()->clear_response();
226 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
227 "updated-fake-policy-data");
228 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
229 EXPECT_CALL(observer_, OnPolicyFetched(_));
230 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
231 client_->FetchPolicy();
232 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
233 CheckPolicyResponse();
234 }
235
236 TEST_F(CloudPolicyClientTest, PolicyFetchWithMetaData) {
237 Register();
238
239 const base::Time timestamp(
240 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20));
241 client_->set_submit_machine_id(true);
242 client_->set_last_policy_timestamp(timestamp);
243 client_->set_public_key_version(CloudPolicyClient::PublicKeyVersion(42));
244 em::PolicyFetchRequest* policy_fetch_request =
245 policy_request_.mutable_policy_request()->mutable_request(0);
246 policy_fetch_request->set_machine_id(kMachineID);
247 policy_fetch_request->set_timestamp(
248 (timestamp - base::Time::UnixEpoch()).InMilliseconds());
249 policy_fetch_request->set_public_key_version(42);
250
251 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
252 EXPECT_CALL(observer_, OnPolicyFetched(_));
253 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
254 client_->FetchPolicy();
255 CheckPolicyResponse();
256 }
257
258 TEST_F(CloudPolicyClientTest, BadPolicyResponse) {
259 Register();
260
261 policy_response_.clear_policy_response();
262 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
263 EXPECT_CALL(observer_, OnClientError(_));
264 client_->FetchPolicy();
265 EXPECT_FALSE(client_->policy());
266 EXPECT_EQ(DM_STATUS_RESPONSE_DECODING_ERROR, client_->status());
267
268 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
269 "fake-policy-data");
270 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
271 "excess-fake-policy-data");
272 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
273 EXPECT_CALL(observer_, OnPolicyFetched(_));
274 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
275 client_->FetchPolicy();
276 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
277 CheckPolicyResponse();
278 }
279
280 TEST_F(CloudPolicyClientTest, PolicyRequestFailure) {
281 Register();
282
283 EXPECT_CALL(service_,
284 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
285 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
286 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
287 EXPECT_CALL(observer_, OnClientError(_));
288 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()).Times(0);
289 client_->FetchPolicy();
290 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status());
291 EXPECT_FALSE(client_->policy());
292 }
293
294 TEST_F(CloudPolicyClientTest, Unregister) {
295 Register();
296
297 ExpectUnregistration(kDMToken);
298 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
299 client_->Unregister();
300 EXPECT_FALSE(client_->is_registered());
301 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
302 }
303
304 TEST_F(CloudPolicyClientTest, UnregisterEmpty) {
305 Register();
306
307 unregistration_response_.clear_unregister_response();
308 EXPECT_CALL(service_,
309 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION))
310 .WillOnce(service_.SucceedJob(unregistration_response_));
311 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
312 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
313 client_->Unregister();
314 EXPECT_FALSE(client_->is_registered());
315 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
316 }
317
318 TEST_F(CloudPolicyClientTest, UnregisterFailure) {
319 Register();
320
321 EXPECT_CALL(service_,
322 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION))
323 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
324 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
325 EXPECT_CALL(observer_, OnClientError(_));
326 client_->Unregister();
327 EXPECT_TRUE(client_->is_registered());
328 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status());
329 }
330
331 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698