OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/policy/cloud_policy_controller.h" | |
6 | |
7 #include "base/message_loop.h" | |
8 #include "base/scoped_temp_dir.h" | |
9 #include "chrome/browser/browser_thread.h" | |
10 #include "chrome/browser/policy/cloud_policy_cache.h" | |
11 #include "chrome/browser/policy/mock_configuration_policy_store.h" | |
12 #include "chrome/browser/policy/mock_device_management_backend.h" | |
13 #include "policy/policy_constants.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 const char kTestToken[] = "cloud_policy_controller_test_auth_token"; | |
18 | |
19 namespace policy { | |
20 | |
21 using ::testing::_; | |
22 using ::testing::AtLeast; | |
23 using ::testing::InSequence; | |
24 using ::testing::Mock; | |
25 using ::testing::Return; | |
26 | |
27 class MockCloudPolicyIdentityStrategy : public CloudPolicyIdentityStrategy { | |
28 public: | |
29 MockCloudPolicyIdentityStrategy() {} | |
30 ~MockCloudPolicyIdentityStrategy() {} | |
31 | |
32 MOCK_METHOD0(GetDeviceToken, std::string()); | |
33 MOCK_METHOD0(GetDeviceID, std::string()); | |
34 MOCK_METHOD2(GetCredentials, bool(std::string*, std::string*)); | |
35 void AddObserver(CloudPolicyIdentityStrategy::Observer*) {} | |
36 void RemoveObserver(CloudPolicyIdentityStrategy::Observer*) {} | |
Mattias Nissler (ping if slow)
2011/02/21 14:39:13
Why do you have the observer declarations here?
Jakob Kummerow
2011/02/22 10:02:33
Done.
| |
37 virtual void OnDeviceTokenAvailable(const std::string&) {} | |
38 private: | |
Mattias Nissler (ping if slow)
2011/02/21 14:39:13
I think most of our code has a newline before visi
Jakob Kummerow
2011/02/22 10:02:33
Done.
| |
39 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyIdentityStrategy); | |
40 }; | |
41 | |
42 ACTION_P2(MockCloudPolicyIdentityStrategyGetCredentials, username, auth_token) { | |
43 *arg0 = username; | |
44 *arg1 = auth_token; | |
45 return true; | |
46 } | |
47 | |
48 class MockDeviceTokenFetcher : public DeviceTokenFetcher { | |
49 public: | |
50 explicit MockDeviceTokenFetcher(CloudPolicyCache* cache) | |
51 : DeviceTokenFetcher(NULL, cache) {} | |
52 ~MockDeviceTokenFetcher() {} | |
53 | |
54 MOCK_METHOD0(GetDeviceToken, std::string&()); | |
55 MOCK_METHOD2(FetchToken, void(const std::string&, const std::string&)); | |
56 void AddObserver(DeviceTokenFetcher::Observer*) {} | |
57 void RemoveObserver(DeviceTokenFetcher::Observer*) {} | |
Mattias Nissler (ping if slow)
2011/02/21 14:39:13
Why the observer declarations?
Jakob Kummerow
2011/02/22 10:02:33
Done.
| |
58 private: | |
Mattias Nissler (ping if slow)
2011/02/21 14:39:13
Same here.
Jakob Kummerow
2011/02/22 10:02:33
Done.
| |
59 DISALLOW_COPY_AND_ASSIGN(MockDeviceTokenFetcher); | |
60 }; | |
61 | |
62 class CloudPolicyControllerTest : public testing::Test { | |
63 public: | |
64 CloudPolicyControllerTest() | |
65 : ui_thread_(BrowserThread::UI, &loop_), | |
66 file_thread_(BrowserThread::FILE, &loop_) {} | |
67 | |
68 virtual ~CloudPolicyControllerTest() {} | |
69 | |
70 virtual void SetUp() { | |
71 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); | |
72 cache_.reset(new CloudPolicyCache( | |
73 temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest"))); | |
74 token_fetcher_.reset(new MockDeviceTokenFetcher(cache_.get())); | |
75 } | |
76 | |
77 virtual void TearDown() { | |
78 controller_.reset(); // Implicitly unregisters observers. | |
79 } | |
80 | |
81 // Takes ownership of |backend|. | |
82 void CreateNewController(DeviceManagementBackend* backend) { | |
83 controller_.reset(new CloudPolicyController( | |
84 cache_.get(), backend, token_fetcher_.get(), &identity_strategy_)); | |
85 } | |
86 | |
87 void CreateNewController(DeviceManagementBackend* backend, | |
88 int64 policy_refresh_rate_ms, | |
89 int policy_refresh_deviation_factor_percent, | |
90 int64 policy_refresh_deviation_max_ms, | |
91 int64 policy_refresh_error_delay_ms) { | |
92 controller_.reset(new CloudPolicyController( | |
93 cache_.get(), backend, token_fetcher_.get(), &identity_strategy_, | |
94 policy_refresh_rate_ms, | |
95 policy_refresh_deviation_factor_percent, | |
96 policy_refresh_deviation_max_ms, | |
97 policy_refresh_error_delay_ms)); | |
98 } | |
99 | |
100 void ExpectHasSpdyPolicy() { | |
101 MockConfigurationPolicyStore store; | |
102 EXPECT_CALL(store, Apply(_, _)).Times(AtLeast(1)); | |
103 cache_->GetManagedPolicyProvider()->Provide(&store); | |
104 FundamentalValue expected(true); | |
105 ASSERT_TRUE(store.Get(kPolicyDisableSpdy) != NULL); | |
106 EXPECT_TRUE(store.Get(kPolicyDisableSpdy)->Equals(&expected)); | |
107 } | |
108 | |
109 void SetupIdentityStrategy(const std::string& device_token, | |
110 const std::string& device_id, | |
111 const std::string& user_name, | |
112 const std::string& auth_token) { | |
113 EXPECT_CALL(identity_strategy_, GetDeviceToken()).WillRepeatedly( | |
114 Return(device_token)); | |
115 EXPECT_CALL(identity_strategy_, GetDeviceID()).WillRepeatedly( | |
116 Return(device_id)); | |
117 if (!user_name.empty()) { | |
118 EXPECT_CALL(identity_strategy_, GetCredentials(_, _)).WillRepeatedly( | |
119 MockCloudPolicyIdentityStrategyGetCredentials(user_name, auth_token)); | |
120 } | |
121 } | |
122 | |
123 protected: | |
124 scoped_ptr<CloudPolicyCache> cache_; | |
125 scoped_ptr<CloudPolicyController> controller_; | |
126 scoped_ptr<MockDeviceTokenFetcher> token_fetcher_; | |
127 MockCloudPolicyIdentityStrategy identity_strategy_; | |
128 ScopedTempDir temp_user_data_dir_; | |
129 MessageLoop loop_; | |
130 | |
131 private: | |
132 BrowserThread ui_thread_; | |
133 BrowserThread file_thread_; | |
134 | |
135 DISALLOW_COPY_AND_ASSIGN(CloudPolicyControllerTest); | |
136 }; | |
137 | |
138 // If a device token is present when the controller starts up, it should | |
139 // fetch and apply policy. | |
140 TEST_F(CloudPolicyControllerTest, StartupWithDeviceToken) { | |
141 SetupIdentityStrategy("fake_device_token", "device_id", "", ""); | |
142 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); | |
143 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( | |
144 MockDeviceManagementBackendSucceedSpdyCloudPolicy()); | |
145 CreateNewController(backend); | |
146 loop_.RunAllPending(); | |
147 ExpectHasSpdyPolicy(); | |
148 } | |
149 | |
150 // If no device token is present when the controller starts up, it should | |
151 // instruct the token_fetcher_ to fetch one. | |
152 TEST_F(CloudPolicyControllerTest, StartupWithoutDeviceToken) { | |
153 SetupIdentityStrategy("", "device_id", "a@b.com", "auth_token"); | |
154 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1); | |
155 CreateNewController(NULL); | |
156 loop_.RunAllPending(); | |
157 } | |
158 | |
159 // If the current user belongs to a known non-managed domain, no token fetch | |
160 // should be initiated. | |
161 TEST_F(CloudPolicyControllerTest, StartupUnmanagedUser) { | |
162 SetupIdentityStrategy("", "device_id", "DannoHelper@gmail.com", "auth_token"); | |
Mattias Nissler (ping if slow)
2011/02/21 14:39:13
Haha! I guess I hereby proved I read testing code
Jakob Kummerow
2011/02/22 10:02:33
I was wondering whether or not I'd get that throug
| |
163 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(0); | |
164 CreateNewController(NULL); | |
165 loop_.RunAllPending(); | |
166 } | |
167 | |
168 // After policy has been fetched successfully, a new fetch should be triggered | |
169 // after the refresh interval has timed out. | |
170 TEST_F(CloudPolicyControllerTest, RefreshAfterSuccessfulPolicy) { | |
171 SetupIdentityStrategy("device_token", "device_id", | |
172 "DannoHelperDelegate@b.com", "auth_token"); | |
173 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); | |
174 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( | |
175 MockDeviceManagementBackendSucceedSpdyCloudPolicy()).WillOnce( | |
176 MockDeviceManagementBackendFailPolicy( | |
177 DeviceManagementBackend::kErrorRequestFailed)); | |
178 CreateNewController(backend, 0, 0, 0, 1000 * 1000); | |
179 loop_.RunAllPending(); | |
180 ExpectHasSpdyPolicy(); | |
181 } | |
182 | |
183 // If poliy fetching failed, it should be retried. | |
184 TEST_F(CloudPolicyControllerTest, RefreshAfterError) { | |
185 SetupIdentityStrategy("device_token", "device_id", | |
186 "DannoHelperDelegateImpl@b.com", "auth_token"); | |
187 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); | |
188 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( | |
189 MockDeviceManagementBackendFailPolicy( | |
190 DeviceManagementBackend::kErrorRequestFailed)).WillOnce( | |
191 MockDeviceManagementBackendSucceedSpdyCloudPolicy()); | |
192 CreateNewController(backend, 1000 * 1000, 0, 0, 0); | |
193 loop_.RunAllPending(); | |
194 ExpectHasSpdyPolicy(); | |
195 } | |
196 | |
197 // If the backend reports that the device token was invalid, the controller | |
198 // should instruct the token fetcher to fetch a new token. | |
199 TEST_F(CloudPolicyControllerTest, InvalidToken) { | |
200 SetupIdentityStrategy("device_token", "device_id", "standup@ten.am", "auth"); | |
201 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); | |
202 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( | |
203 MockDeviceManagementBackendFailPolicy( | |
204 DeviceManagementBackend::kErrorServiceManagementTokenInvalid)); | |
205 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1); | |
206 CreateNewController(backend); | |
207 loop_.RunAllPending(); | |
208 } | |
209 | |
210 // If the backend reports that the device is unknown to the server, the | |
211 // controller should instruct the token fetcher to fetch a new token. | |
212 TEST_F(CloudPolicyControllerTest, DeviceNotFound) { | |
213 SetupIdentityStrategy("device_token", "device_id", "me@you.com", "auth"); | |
214 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); | |
215 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( | |
216 MockDeviceManagementBackendFailPolicy( | |
217 DeviceManagementBackend::kErrorServiceDeviceNotFound)); | |
218 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1); | |
219 CreateNewController(backend); | |
220 loop_.RunAllPending(); | |
221 } | |
222 | |
223 // If the backend reports that the device is no longer managed, the controller | |
224 // shoud instruct the token fetcher to fetch a new token (which will in turn | |
225 // set and persist the correct 'unmanaged' state). | |
226 TEST_F(CloudPolicyControllerTest, NoLongerManaged) { | |
227 SetupIdentityStrategy("device_token", "device_id", "who@what.com", "auth"); | |
228 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); | |
229 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( | |
230 MockDeviceManagementBackendFailPolicy( | |
231 DeviceManagementBackend::kErrorServiceManagementNotSupported)); | |
232 EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1); | |
233 CreateNewController(backend); | |
234 loop_.RunAllPending(); | |
235 } | |
236 | |
237 // If the server doesn't support the new protocol, the controller should fall | |
238 // back to the old protocol. | |
239 TEST_F(CloudPolicyControllerTest, FallbackToOldProtocol) { | |
240 SetupIdentityStrategy("device_token", "device_id", "a@b.com", "auth"); | |
241 MockDeviceManagementBackend* backend = new MockDeviceManagementBackend(); | |
242 // If the CloudPolicyRequest fails with kErrorRequestInvalid... | |
243 EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce( | |
244 MockDeviceManagementBackendFailPolicy( | |
245 DeviceManagementBackend::kErrorRequestInvalid)); | |
246 // ...the client should fall back to a classic PolicyRequest, | |
247 // and remember this fallback for any future request, | |
248 // both after successful fetches and after errors. | |
249 EXPECT_CALL(*backend, ProcessPolicyRequest(_, _, _, _)).WillOnce( | |
250 MockDeviceManagementBackendSucceedBooleanPolicy( | |
251 key::kDisableSpdy, true)).WillOnce( | |
252 MockDeviceManagementBackendFailPolicy( | |
253 DeviceManagementBackend::kErrorHttpStatus)).WillOnce( | |
254 Return()); | |
255 CreateNewController(backend, 0, 0, 0, 0); | |
256 loop_.RunAllPending(); | |
257 ExpectHasSpdyPolicy(); | |
258 } | |
259 | |
260 } // namespace policy | |
OLD | NEW |