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

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

Powered by Google App Engine
This is Rietveld 408576698