OLD | NEW |
| (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_service.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback.h" | |
9 #include "chrome/browser/policy/cloud_policy_constants.h" | |
10 #include "chrome/browser/policy/mock_cloud_policy_client.h" | |
11 #include "chrome/browser/policy/mock_cloud_policy_store.h" | |
12 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
13 #include "testing/gmock/include/gmock/gmock.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace em = enterprise_management; | |
17 | |
18 using testing::_; | |
19 | |
20 namespace policy { | |
21 | |
22 class MockCloudPolicyServiceObserver : public CloudPolicyService::Observer { | |
23 public: | |
24 MockCloudPolicyServiceObserver() {} | |
25 virtual ~MockCloudPolicyServiceObserver() {} | |
26 | |
27 MOCK_METHOD1(OnInitializationCompleted, void(CloudPolicyService* service)); | |
28 private: | |
29 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyServiceObserver); | |
30 }; | |
31 | |
32 class CloudPolicyServiceTest : public testing::Test { | |
33 public: | |
34 CloudPolicyServiceTest() | |
35 : policy_ns_key_(dm_protocol::kChromeUserPolicyType, std::string()), | |
36 service_(policy_ns_key_, &client_, &store_) {} | |
37 | |
38 MOCK_METHOD1(OnPolicyRefresh, void(bool)); | |
39 | |
40 protected: | |
41 PolicyNamespaceKey policy_ns_key_; | |
42 MockCloudPolicyClient client_; | |
43 MockCloudPolicyStore store_; | |
44 CloudPolicyService service_; | |
45 }; | |
46 | |
47 MATCHER_P(ProtoMatches, proto, "") { | |
48 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); | |
49 } | |
50 | |
51 TEST_F(CloudPolicyServiceTest, ManagedByEmptyPolicy) { | |
52 EXPECT_EQ("", service_.ManagedBy()); | |
53 } | |
54 | |
55 TEST_F(CloudPolicyServiceTest, ManagedByValidPolicy) { | |
56 store_.policy_.reset(new em::PolicyData()); | |
57 store_.policy_->set_username("user@example.com"); | |
58 EXPECT_EQ("example.com", service_.ManagedBy()); | |
59 } | |
60 | |
61 TEST_F(CloudPolicyServiceTest, PolicyUpdateSuccess) { | |
62 em::PolicyFetchResponse policy; | |
63 policy.set_policy_data("fake policy"); | |
64 client_.SetPolicy(policy_ns_key_, policy); | |
65 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); | |
66 client_.NotifyPolicyFetched(); | |
67 | |
68 // After |store_| initializes, credentials and other meta data should be | |
69 // transferred to |client_|. | |
70 store_.policy_.reset(new em::PolicyData()); | |
71 store_.policy_->set_request_token("fake token"); | |
72 store_.policy_->set_device_id("fake client id"); | |
73 store_.policy_->set_timestamp(32); | |
74 store_.policy_->set_valid_serial_number_missing(true); | |
75 store_.policy_->set_public_key_version(17); | |
76 EXPECT_CALL(client_, | |
77 SetupRegistration(store_.policy_->request_token(), | |
78 store_.policy_->device_id())).Times(1); | |
79 store_.NotifyStoreLoaded(); | |
80 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMilliseconds(32), | |
81 client_.last_policy_timestamp_); | |
82 EXPECT_TRUE(client_.submit_machine_id_); | |
83 EXPECT_TRUE(client_.public_key_version_valid_); | |
84 EXPECT_EQ(17, client_.public_key_version_); | |
85 } | |
86 | |
87 TEST_F(CloudPolicyServiceTest, PolicyUpdateClientFailure) { | |
88 client_.SetStatus(DM_STATUS_REQUEST_FAILED); | |
89 EXPECT_CALL(store_, Store(_)).Times(0); | |
90 client_.NotifyPolicyFetched(); | |
91 } | |
92 | |
93 TEST_F(CloudPolicyServiceTest, RefreshPolicySuccess) { | |
94 testing::InSequence seq; | |
95 | |
96 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); | |
97 client_.SetDMToken("fake token"); | |
98 | |
99 // Trigger a fetch on the client. | |
100 EXPECT_CALL(client_, FetchPolicy()).Times(1); | |
101 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | |
102 base::Unretained(this))); | |
103 | |
104 // Client responds, push policy to store. | |
105 em::PolicyFetchResponse policy; | |
106 policy.set_policy_data("fake policy"); | |
107 client_.SetPolicy(policy_ns_key_, policy); | |
108 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); | |
109 client_.NotifyPolicyFetched(); | |
110 | |
111 // Store reloads policy, callback gets triggered. | |
112 store_.policy_.reset(new em::PolicyData()); | |
113 store_.policy_->set_request_token("token"); | |
114 store_.policy_->set_device_id("device-id"); | |
115 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(1); | |
116 store_.NotifyStoreLoaded(); | |
117 } | |
118 | |
119 TEST_F(CloudPolicyServiceTest, RefreshPolicyNotRegistered) { | |
120 // Clear the token so the client is not registered. | |
121 client_.SetDMToken(""); | |
122 | |
123 EXPECT_CALL(client_, FetchPolicy()).Times(0); | |
124 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); | |
125 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | |
126 base::Unretained(this))); | |
127 } | |
128 | |
129 TEST_F(CloudPolicyServiceTest, RefreshPolicyClientError) { | |
130 testing::InSequence seq; | |
131 | |
132 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); | |
133 client_.SetDMToken("fake token"); | |
134 | |
135 // Trigger a fetch on the client. | |
136 EXPECT_CALL(client_, FetchPolicy()).Times(1); | |
137 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | |
138 base::Unretained(this))); | |
139 | |
140 // Client responds with an error, which should trigger the callback. | |
141 client_.SetStatus(DM_STATUS_REQUEST_FAILED); | |
142 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); | |
143 client_.NotifyClientError(); | |
144 } | |
145 | |
146 TEST_F(CloudPolicyServiceTest, RefreshPolicyStoreError) { | |
147 testing::InSequence seq; | |
148 | |
149 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); | |
150 client_.SetDMToken("fake token"); | |
151 | |
152 // Trigger a fetch on the client. | |
153 EXPECT_CALL(client_, FetchPolicy()).Times(1); | |
154 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | |
155 base::Unretained(this))); | |
156 | |
157 // Client responds, push policy to store. | |
158 em::PolicyFetchResponse policy; | |
159 policy.set_policy_data("fake policy"); | |
160 client_.SetPolicy(policy_ns_key_, policy); | |
161 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); | |
162 client_.NotifyPolicyFetched(); | |
163 | |
164 // Store fails, which should trigger the callback. | |
165 EXPECT_CALL(*this, OnPolicyRefresh(false)).Times(1); | |
166 store_.NotifyStoreError(); | |
167 } | |
168 | |
169 TEST_F(CloudPolicyServiceTest, RefreshPolicyConcurrent) { | |
170 testing::InSequence seq; | |
171 | |
172 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); | |
173 client_.SetDMToken("fake token"); | |
174 | |
175 // Trigger a fetch on the client. | |
176 EXPECT_CALL(client_, FetchPolicy()).Times(1); | |
177 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | |
178 base::Unretained(this))); | |
179 | |
180 // Triggering another policy refresh should generate a new fetch request. | |
181 EXPECT_CALL(client_, FetchPolicy()).Times(1); | |
182 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | |
183 base::Unretained(this))); | |
184 | |
185 // Client responds, push policy to store. | |
186 em::PolicyFetchResponse policy; | |
187 policy.set_policy_data("fake policy"); | |
188 client_.SetPolicy(policy_ns_key_, policy); | |
189 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); | |
190 client_.NotifyPolicyFetched(); | |
191 | |
192 // Trigger another policy fetch. | |
193 EXPECT_CALL(client_, FetchPolicy()).Times(1); | |
194 service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh, | |
195 base::Unretained(this))); | |
196 | |
197 // The store finishing the first load should not generate callbacks. | |
198 EXPECT_CALL(*this, OnPolicyRefresh(_)).Times(0); | |
199 store_.NotifyStoreLoaded(); | |
200 | |
201 // Second policy fetch finishes. | |
202 EXPECT_CALL(store_, Store(ProtoMatches(policy))).Times(1); | |
203 client_.NotifyPolicyFetched(); | |
204 | |
205 // Corresponding store operation finishes, all _three_ callbacks fire. | |
206 EXPECT_CALL(*this, OnPolicyRefresh(true)).Times(3); | |
207 store_.NotifyStoreLoaded(); | |
208 } | |
209 | |
210 TEST_F(CloudPolicyServiceTest, StoreAlreadyInitialized) { | |
211 // Service should start off initialized if the store has already loaded | |
212 // policy. | |
213 store_.NotifyStoreLoaded(); | |
214 CloudPolicyService service(policy_ns_key_, &client_, &store_); | |
215 EXPECT_TRUE(service.IsInitializationComplete()); | |
216 } | |
217 | |
218 TEST_F(CloudPolicyServiceTest, StoreLoadAfterCreation) { | |
219 // Service should start off un-initialized if the store has not yet loaded | |
220 // policy. | |
221 EXPECT_FALSE(service_.IsInitializationComplete()); | |
222 MockCloudPolicyServiceObserver observer; | |
223 service_.AddObserver(&observer); | |
224 // Service should be marked as initialized and observer should be called back. | |
225 EXPECT_CALL(observer, OnInitializationCompleted(&service_)).Times(1); | |
226 store_.NotifyStoreLoaded(); | |
227 EXPECT_TRUE(service_.IsInitializationComplete()); | |
228 testing::Mock::VerifyAndClearExpectations(&observer); | |
229 | |
230 // Now, the next time the store is loaded, the observer should not be called | |
231 // again. | |
232 EXPECT_CALL(observer, OnInitializationCompleted(&service_)).Times(0); | |
233 store_.NotifyStoreLoaded(); | |
234 service_.RemoveObserver(&observer); | |
235 } | |
236 | |
237 } // namespace policy | |
OLD | NEW |