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

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

Issue 11946017: Remove old cloud policy code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address nits. Created 7 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/device_token_fetcher.h"
6
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop.h"
9 #include "chrome/browser/policy/cloud_policy_data_store.h"
10 #include "chrome/browser/policy/logging_work_scheduler.h"
11 #include "chrome/browser/policy/mock_cloud_policy_data_store.h"
12 #include "chrome/browser/policy/mock_device_management_service.h"
13 #include "chrome/browser/policy/policy_notifier.h"
14 #include "chrome/browser/policy/user_policy_cache.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace em = enterprise_management;
20
21 namespace policy {
22
23 const char kTestToken[] = "device_token_fetcher_test_auth_token";
24
25 using content::BrowserThread;
26 using testing::AnyNumber;
27 using testing::Mock;
28 using testing::_;
29
30 ACTION_P(VerifyRegisterRequest, known_machine_id) {
31 ASSERT_TRUE(arg6.has_register_request());
32 const em::DeviceRegisterRequest& request = arg6.register_request();
33 if (known_machine_id) {
34 EXPECT_TRUE(request.has_auto_enrolled());
35 EXPECT_TRUE(request.auto_enrolled());
36 } else {
37 EXPECT_FALSE(request.has_auto_enrolled());
38 }
39 }
40
41 class DeviceTokenFetcherTest : public testing::Test {
42 protected:
43 DeviceTokenFetcherTest()
44 : ui_thread_(BrowserThread::UI, &loop_),
45 file_thread_(BrowserThread::FILE, &loop_) {
46 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir());
47 successful_registration_response_.mutable_register_response()->
48 set_device_management_token("fake_token");
49 successful_registration_response_.mutable_register_response()->
50 set_enrollment_type(em::DeviceRegisterResponse::ENTERPRISE);
51 }
52
53 virtual void SetUp() {
54 cache_.reset(new UserPolicyCache(
55 temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest"),
56 false /* wait_for_policy_fetch */));
57 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)).Times(AnyNumber());
58 data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies());
59 data_store_->AddObserver(&observer_);
60 }
61
62 virtual void TearDown() {
63 loop_.RunUntilIdle();
64 data_store_->RemoveObserver(&observer_);
65 }
66
67 void FetchToken(DeviceTokenFetcher* fetcher, CloudPolicyDataStore* store) {
68 store->SetupForTesting("", "fake_device_id", "fake_user_name",
69 "fake_auth_token", true);
70 fetcher->FetchToken();
71 }
72
73 void CreateNewWaitingCache() {
74 cache_.reset(new UserPolicyCache(
75 temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest"),
76 true /* wait_for_policy_fetch */));
77 // Make this cache's disk cache ready, but have it still waiting for a
78 // policy fetch.
79 cache_->Load();
80 loop_.RunUntilIdle();
81 ASSERT_TRUE(cache_->last_policy_refresh_time().is_null());
82 ASSERT_FALSE(cache_->IsReady());
83 }
84
85 void SetUpSuccessfulRegistrationExpectation(bool known_machine_id) {
86 EXPECT_CALL(service_,
87 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
88 .WillOnce(service_.SucceedJob(successful_registration_response_));
89 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _))
90 .WillOnce(VerifyRegisterRequest(known_machine_id));
91 }
92
93 MessageLoop loop_;
94 MockDeviceManagementService service_;
95 scoped_ptr<CloudPolicyCacheBase> cache_;
96 scoped_ptr<CloudPolicyDataStore> data_store_;
97 MockCloudPolicyDataStoreObserver observer_;
98 PolicyNotifier notifier_;
99 base::ScopedTempDir temp_user_data_dir_;
100 em::DeviceManagementResponse successful_registration_response_;
101
102 private:
103 content::TestBrowserThread ui_thread_;
104 content::TestBrowserThread file_thread_;
105 };
106
107 TEST_F(DeviceTokenFetcherTest, FetchToken) {
108 testing::InSequence s;
109 SetUpSuccessfulRegistrationExpectation(false);
110 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
111 &notifier_);
112 EXPECT_CALL(observer_, OnDeviceTokenChanged());
113 EXPECT_EQ("", data_store_->device_token());
114 EXPECT_EQ(DEVICE_MODE_PENDING, data_store_->device_mode());
115 FetchToken(&fetcher, data_store_.get());
116 loop_.RunUntilIdle();
117 Mock::VerifyAndClearExpectations(&observer_);
118 std::string token = data_store_->device_token();
119 EXPECT_NE("", token);
120 // User policy registration should not set enrollment mode.
121 EXPECT_EQ(DEVICE_MODE_PENDING, data_store_->device_mode());
122
123 // Calling FetchToken() again should result in a new token being fetched.
124 successful_registration_response_.mutable_register_response()->
125 set_device_management_token("new_fake_token");
126 EXPECT_CALL(service_,
127 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
128 .WillOnce(service_.SucceedJob(successful_registration_response_));
129 EXPECT_CALL(observer_, OnDeviceTokenChanged());
130 FetchToken(&fetcher, data_store_.get());
131 loop_.RunUntilIdle();
132 Mock::VerifyAndClearExpectations(&observer_);
133 std::string token2 = data_store_->device_token();
134 EXPECT_NE("", token2);
135 EXPECT_NE(token, token2);
136 }
137
138 TEST_F(DeviceTokenFetcherTest, FetchDeviceToken) {
139 testing::InSequence s;
140 scoped_ptr<CloudPolicyDataStore> data_store(
141 CloudPolicyDataStore::CreateForDevicePolicies());
142 SetUpSuccessfulRegistrationExpectation(false);
143 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store.get(),
144 &notifier_);
145 EXPECT_EQ("", data_store->device_token());
146 EXPECT_EQ(DEVICE_MODE_PENDING, data_store->device_mode());
147 FetchToken(&fetcher, data_store.get());
148 loop_.RunUntilIdle();
149 EXPECT_NE("", data_store->device_token());
150 // For device registrations, the fetcher needs to determine device mode.
151 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, data_store->device_mode());
152 }
153
154 TEST_F(DeviceTokenFetcherTest, FetchDeviceTokenMissingMode) {
155 testing::InSequence s;
156 scoped_ptr<CloudPolicyDataStore> data_store(
157 CloudPolicyDataStore::CreateForDevicePolicies());
158 SetUpSuccessfulRegistrationExpectation(false);
159 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store.get(),
160 &notifier_);
161 EXPECT_EQ("", data_store->device_token());
162 EXPECT_EQ(DEVICE_MODE_PENDING, data_store->device_mode());
163 successful_registration_response_.mutable_register_response()->
164 clear_enrollment_type();
165 FetchToken(&fetcher, data_store.get());
166 loop_.RunUntilIdle();
167 Mock::VerifyAndClearExpectations(&observer_);
168 EXPECT_NE("", data_store->device_token());
169 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, data_store->device_mode());
170 }
171
172 TEST_F(DeviceTokenFetcherTest, RetryOnError) {
173 testing::InSequence s;
174 EXPECT_CALL(service_,
175 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
176 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED))
177 .WillOnce(service_.SucceedJob(successful_registration_response_));
178 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
179 &notifier_, new DummyWorkScheduler);
180 EXPECT_CALL(observer_, OnDeviceTokenChanged());
181 FetchToken(&fetcher, data_store_.get());
182 loop_.RunUntilIdle();
183 Mock::VerifyAndClearExpectations(&observer_);
184 EXPECT_NE("", data_store_->device_token());
185 }
186
187 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) {
188 EXPECT_CALL(service_,
189 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
190 .WillOnce(service_.FailJob(DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED));
191 EXPECT_FALSE(cache_->is_unmanaged());
192 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
193 &notifier_);
194 EXPECT_CALL(observer_, OnDeviceTokenChanged()).Times(0);
195 FetchToken(&fetcher, data_store_.get());
196 loop_.RunUntilIdle();
197 Mock::VerifyAndClearExpectations(&observer_);
198 EXPECT_EQ("", data_store_->device_token());
199 EXPECT_TRUE(cache_->is_unmanaged());
200 }
201
202 TEST_F(DeviceTokenFetcherTest, DontSetFetchingDone) {
203 CreateNewWaitingCache();
204 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
205 &notifier_);
206 EXPECT_FALSE(cache_->IsReady());
207 }
208
209 TEST_F(DeviceTokenFetcherTest, DontSetFetchingDoneWithoutPolicyFetch) {
210 CreateNewWaitingCache();
211 EXPECT_CALL(service_,
212 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
213 .WillOnce(service_.SucceedJob(successful_registration_response_));
214 EXPECT_CALL(observer_, OnDeviceTokenChanged());
215 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
216 &notifier_);
217 FetchToken(&fetcher, data_store_.get());
218 loop_.RunUntilIdle();
219 // On successful token fetching the cache isn't set to ready, since the next
220 // step is to fetch policy. Only failures to fetch the token should make
221 // the cache ready.
222 EXPECT_FALSE(cache_->IsReady());
223 }
224
225 TEST_F(DeviceTokenFetcherTest, SetFetchingDoneWhenUnmanaged) {
226 CreateNewWaitingCache();
227 cache_->SetUnmanaged();
228 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
229 &notifier_);
230 EXPECT_TRUE(cache_->IsReady());
231 }
232
233 TEST_F(DeviceTokenFetcherTest, SetFetchingDoneOnFailures) {
234 CreateNewWaitingCache();
235 EXPECT_CALL(service_,
236 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
237 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
238 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
239 &notifier_);
240 FetchToken(&fetcher, data_store_.get());
241 loop_.RunUntilIdle();
242 // This is the opposite case of DontSetFetchingDone1.
243 EXPECT_TRUE(cache_->IsReady());
244 }
245
246 TEST_F(DeviceTokenFetcherTest, SetKnownMachineId) {
247 SetUpSuccessfulRegistrationExpectation(true);
248
249 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
250 &notifier_);
251 EXPECT_CALL(observer_, OnDeviceTokenChanged());
252 EXPECT_EQ("", data_store_->device_token());
253
254 data_store_->set_known_machine_id(true);
255 FetchToken(&fetcher, data_store_.get());
256 loop_.RunUntilIdle();
257
258 Mock::VerifyAndClearExpectations(&observer_);
259 std::string token = data_store_->device_token();
260 EXPECT_NE("", token);
261 }
262
263 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/device_token_fetcher.cc ('k') | chrome/browser/policy/logging_work_scheduler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698