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

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

Issue 9064006: Convert DeviceTokenFetcher and CloudPolicyController response callbacks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address nits 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/device_token_fetcher.h" 5 #include "chrome/browser/policy/device_token_fetcher.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/cloud_policy_data_store.h" 9 #include "chrome/browser/policy/cloud_policy_data_store.h"
10 #include "chrome/browser/policy/logging_work_scheduler.h" 10 #include "chrome/browser/policy/logging_work_scheduler.h"
11 #include "chrome/browser/policy/mock_cloud_policy_data_store.h" 11 #include "chrome/browser/policy/mock_cloud_policy_data_store.h"
12 #include "chrome/browser/policy/mock_device_management_backend.h" 12 #include "chrome/browser/policy/mock_device_management_service.h"
13 #include "chrome/browser/policy/mock_device_management_service_old.h"
14 #include "chrome/browser/policy/policy_notifier.h" 13 #include "chrome/browser/policy/policy_notifier.h"
15 #include "chrome/browser/policy/user_policy_cache.h" 14 #include "chrome/browser/policy/user_policy_cache.h"
16 #include "content/test/test_browser_thread.h" 15 #include "content/test/test_browser_thread.h"
17 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
19 18
20 namespace em = enterprise_management; 19 namespace em = enterprise_management;
21 20
22 namespace policy { 21 namespace policy {
23 22
24 const char kTestToken[] = "device_token_fetcher_test_auth_token"; 23 const char kTestToken[] = "device_token_fetcher_test_auth_token";
25 24
26 using content::BrowserThread; 25 using content::BrowserThread;
27 using testing::AnyNumber; 26 using testing::AnyNumber;
28 using testing::Mock; 27 using testing::Mock;
29 using testing::_; 28 using testing::_;
30 29
31 class DeviceTokenFetcherTest : public testing::Test { 30 class DeviceTokenFetcherTest : public testing::Test {
32 protected: 31 protected:
33 DeviceTokenFetcherTest() 32 DeviceTokenFetcherTest()
34 : ui_thread_(BrowserThread::UI, &loop_), 33 : ui_thread_(BrowserThread::UI, &loop_),
35 file_thread_(BrowserThread::FILE, &loop_) { 34 file_thread_(BrowserThread::FILE, &loop_) {
36 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); 35 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir());
36 successful_registration_response_.mutable_register_response()->
37 set_device_management_token("fake_token");
37 } 38 }
38 39
39 virtual void SetUp() { 40 virtual void SetUp() {
40 cache_.reset(new UserPolicyCache( 41 cache_.reset(new UserPolicyCache(
41 temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest"), 42 temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest"),
42 false /* wait_for_policy_fetch */)); 43 false /* wait_for_policy_fetch */));
43 EXPECT_CALL(service_, CreateBackend()) 44 EXPECT_CALL(service_, StartJob(_)).Times(AnyNumber());
44 .Times(AnyNumber())
45 .WillRepeatedly(MockDeviceManagementServiceProxyBackend(&backend_));
46 data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies()); 45 data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies());
47 data_store_->AddObserver(&observer_); 46 data_store_->AddObserver(&observer_);
48 } 47 }
49 48
50 virtual void TearDown() { 49 virtual void TearDown() {
51 loop_.RunAllPending(); 50 loop_.RunAllPending();
52 data_store_->RemoveObserver(&observer_); 51 data_store_->RemoveObserver(&observer_);
53 } 52 }
54 53
55 void FetchToken(DeviceTokenFetcher* fetcher) { 54 void FetchToken(DeviceTokenFetcher* fetcher) {
56 data_store_->SetupForTesting("", "fake_device_id", "fake_user_name", 55 data_store_->SetupForTesting("", "fake_device_id", "fake_user_name",
57 "fake_auth_token", true); 56 "fake_auth_token", true);
58 fetcher->FetchToken(); 57 fetcher->FetchToken();
59 } 58 }
60 59
61 void CreateNewWaitingCache() { 60 void CreateNewWaitingCache() {
62 cache_.reset(new UserPolicyCache( 61 cache_.reset(new UserPolicyCache(
63 temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest"), 62 temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest"),
64 true /* wait_for_policy_fetch */)); 63 true /* wait_for_policy_fetch */));
65 // Make this cache's disk cache ready, but have it still waiting for a 64 // Make this cache's disk cache ready, but have it still waiting for a
66 // policy fetch. 65 // policy fetch.
67 cache_->Load(); 66 cache_->Load();
68 loop_.RunAllPending(); 67 loop_.RunAllPending();
69 ASSERT_TRUE(cache_->last_policy_refresh_time().is_null()); 68 ASSERT_TRUE(cache_->last_policy_refresh_time().is_null());
70 ASSERT_FALSE(cache_->IsReady()); 69 ASSERT_FALSE(cache_->IsReady());
71 } 70 }
72 71
73 MessageLoop loop_; 72 MessageLoop loop_;
74 MockDeviceManagementBackend backend_; 73 MockDeviceManagementService service_;
75 MockDeviceManagementServiceOld service_;
76 scoped_ptr<CloudPolicyCacheBase> cache_; 74 scoped_ptr<CloudPolicyCacheBase> cache_;
77 scoped_ptr<CloudPolicyDataStore> data_store_; 75 scoped_ptr<CloudPolicyDataStore> data_store_;
78 MockCloudPolicyDataStoreObserver observer_; 76 MockCloudPolicyDataStoreObserver observer_;
79 PolicyNotifier notifier_; 77 PolicyNotifier notifier_;
80 ScopedTempDir temp_user_data_dir_; 78 ScopedTempDir temp_user_data_dir_;
79 em::DeviceManagementResponse successful_registration_response_;
81 80
82 private: 81 private:
83 content::TestBrowserThread ui_thread_; 82 content::TestBrowserThread ui_thread_;
84 content::TestBrowserThread file_thread_; 83 content::TestBrowserThread file_thread_;
85 }; 84 };
86 85
87 TEST_F(DeviceTokenFetcherTest, FetchToken) { 86 TEST_F(DeviceTokenFetcherTest, FetchToken) {
88 testing::InSequence s; 87 testing::InSequence s;
89 EXPECT_CALL(backend_, ProcessRegisterRequest(_, _, _, _, _)).WillOnce( 88 EXPECT_CALL(service_,
90 MockDeviceManagementBackendSucceedRegister()); 89 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
90 .WillOnce(service_.SucceedJob(successful_registration_response_));
91 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 91 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
92 &notifier_); 92 &notifier_);
93 EXPECT_CALL(observer_, OnDeviceTokenChanged()); 93 EXPECT_CALL(observer_, OnDeviceTokenChanged());
94 EXPECT_EQ("", data_store_->device_token()); 94 EXPECT_EQ("", data_store_->device_token());
95 FetchToken(&fetcher); 95 FetchToken(&fetcher);
96 loop_.RunAllPending(); 96 loop_.RunAllPending();
97 Mock::VerifyAndClearExpectations(&observer_); 97 Mock::VerifyAndClearExpectations(&observer_);
98 std::string token = data_store_->device_token(); 98 std::string token = data_store_->device_token();
99 EXPECT_NE("", token); 99 EXPECT_NE("", token);
100 100
101 // Calling FetchToken() again should result in a new token being fetched. 101 // Calling FetchToken() again should result in a new token being fetched.
102 EXPECT_CALL(backend_, ProcessRegisterRequest(_, _, _, _, _)).WillOnce( 102 successful_registration_response_.mutable_register_response()->
103 MockDeviceManagementBackendSucceedRegister()); 103 set_device_management_token("new_fake_token");
104 EXPECT_CALL(service_,
105 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
106 .WillOnce(service_.SucceedJob(successful_registration_response_));
104 EXPECT_CALL(observer_, OnDeviceTokenChanged()); 107 EXPECT_CALL(observer_, OnDeviceTokenChanged());
105 FetchToken(&fetcher); 108 FetchToken(&fetcher);
106 loop_.RunAllPending(); 109 loop_.RunAllPending();
107 Mock::VerifyAndClearExpectations(&observer_); 110 Mock::VerifyAndClearExpectations(&observer_);
108 std::string token2 = data_store_->device_token(); 111 std::string token2 = data_store_->device_token();
109 EXPECT_NE("", token2); 112 EXPECT_NE("", token2);
110 EXPECT_NE(token, token2); 113 EXPECT_NE(token, token2);
111 } 114 }
112 115
113 TEST_F(DeviceTokenFetcherTest, RetryOnError) { 116 TEST_F(DeviceTokenFetcherTest, RetryOnError) {
114 testing::InSequence s; 117 testing::InSequence s;
115 EXPECT_CALL(backend_, ProcessRegisterRequest(_, _, _, _, _)).WillOnce( 118 EXPECT_CALL(service_,
116 MockDeviceManagementBackendFailRegister( 119 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
117 DeviceManagementBackend::kErrorRequestFailed)).WillOnce( 120 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED))
118 MockDeviceManagementBackendSucceedRegister()); 121 .WillOnce(service_.SucceedJob(successful_registration_response_));
119 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 122 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
120 &notifier_, new DummyWorkScheduler); 123 &notifier_, new DummyWorkScheduler);
121 EXPECT_CALL(observer_, OnDeviceTokenChanged()); 124 EXPECT_CALL(observer_, OnDeviceTokenChanged());
122 FetchToken(&fetcher); 125 FetchToken(&fetcher);
123 loop_.RunAllPending(); 126 loop_.RunAllPending();
124 Mock::VerifyAndClearExpectations(&observer_); 127 Mock::VerifyAndClearExpectations(&observer_);
125 EXPECT_NE("", data_store_->device_token()); 128 EXPECT_NE("", data_store_->device_token());
126 } 129 }
127 130
128 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { 131 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) {
129 EXPECT_CALL(backend_, ProcessRegisterRequest(_, _, _, _, _)).WillOnce( 132 EXPECT_CALL(service_,
130 MockDeviceManagementBackendFailRegister( 133 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
131 DeviceManagementBackend::kErrorServiceManagementNotSupported)); 134 .WillOnce(service_.FailJob(DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED));
132 EXPECT_FALSE(cache_->is_unmanaged()); 135 EXPECT_FALSE(cache_->is_unmanaged());
133 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 136 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
134 &notifier_); 137 &notifier_);
135 EXPECT_CALL(observer_, OnDeviceTokenChanged()).Times(0); 138 EXPECT_CALL(observer_, OnDeviceTokenChanged()).Times(0);
136 FetchToken(&fetcher); 139 FetchToken(&fetcher);
137 loop_.RunAllPending(); 140 loop_.RunAllPending();
138 Mock::VerifyAndClearExpectations(&observer_); 141 Mock::VerifyAndClearExpectations(&observer_);
139 EXPECT_EQ("", data_store_->device_token()); 142 EXPECT_EQ("", data_store_->device_token());
140 EXPECT_TRUE(cache_->is_unmanaged()); 143 EXPECT_TRUE(cache_->is_unmanaged());
141 } 144 }
142 145
143 TEST_F(DeviceTokenFetcherTest, DontSetFetchingDone) { 146 TEST_F(DeviceTokenFetcherTest, DontSetFetchingDone) {
144 CreateNewWaitingCache(); 147 CreateNewWaitingCache();
145 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 148 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
146 &notifier_); 149 &notifier_);
147 EXPECT_FALSE(cache_->IsReady()); 150 EXPECT_FALSE(cache_->IsReady());
148 } 151 }
149 152
150 TEST_F(DeviceTokenFetcherTest, DontSetFetchingDoneWithoutPolicyFetch) { 153 TEST_F(DeviceTokenFetcherTest, DontSetFetchingDoneWithoutPolicyFetch) {
151 CreateNewWaitingCache(); 154 CreateNewWaitingCache();
152 EXPECT_CALL(backend_, ProcessRegisterRequest(_, _, _, _, _)).WillOnce( 155 EXPECT_CALL(service_,
153 MockDeviceManagementBackendSucceedRegister()); 156 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
157 .WillOnce(service_.SucceedJob(successful_registration_response_));
154 EXPECT_CALL(observer_, OnDeviceTokenChanged()); 158 EXPECT_CALL(observer_, OnDeviceTokenChanged());
155 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 159 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
156 &notifier_); 160 &notifier_);
157 FetchToken(&fetcher); 161 FetchToken(&fetcher);
158 loop_.RunAllPending(); 162 loop_.RunAllPending();
159 // On successful token fetching the cache isn't set to ready, since the next 163 // On successful token fetching the cache isn't set to ready, since the next
160 // step is to fetch policy. Only failures to fetch the token should make 164 // step is to fetch policy. Only failures to fetch the token should make
161 // the cache ready. 165 // the cache ready.
162 EXPECT_FALSE(cache_->IsReady()); 166 EXPECT_FALSE(cache_->IsReady());
163 } 167 }
164 168
165 TEST_F(DeviceTokenFetcherTest, SetFetchingDoneWhenUnmanaged) { 169 TEST_F(DeviceTokenFetcherTest, SetFetchingDoneWhenUnmanaged) {
166 CreateNewWaitingCache(); 170 CreateNewWaitingCache();
167 cache_->SetUnmanaged(); 171 cache_->SetUnmanaged();
168 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 172 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
169 &notifier_); 173 &notifier_);
170 EXPECT_TRUE(cache_->IsReady()); 174 EXPECT_TRUE(cache_->IsReady());
171 } 175 }
172 176
173 TEST_F(DeviceTokenFetcherTest, SetFetchingDoneOnFailures) { 177 TEST_F(DeviceTokenFetcherTest, SetFetchingDoneOnFailures) {
174 CreateNewWaitingCache(); 178 CreateNewWaitingCache();
175 EXPECT_CALL(backend_, ProcessRegisterRequest(_, _, _, _, _)).WillOnce( 179 EXPECT_CALL(service_,
176 MockDeviceManagementBackendFailRegister( 180 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
177 DeviceManagementBackend::kErrorRequestFailed)); 181 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
178 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(), 182 DeviceTokenFetcher fetcher(&service_, cache_.get(), data_store_.get(),
179 &notifier_); 183 &notifier_);
180 FetchToken(&fetcher); 184 FetchToken(&fetcher);
181 loop_.RunAllPending(); 185 loop_.RunAllPending();
182 // This is the opposite case of DontSetFetchingDone1. 186 // This is the opposite case of DontSetFetchingDone1.
183 EXPECT_TRUE(cache_->IsReady()); 187 EXPECT_TRUE(cache_->IsReady());
184 } 188 }
185 189
186 } // namespace policy 190 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/device_token_fetcher.cc ('k') | chrome/browser/policy/mock_device_management_backend.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698