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

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

Issue 6409040: New policy protobuf protocol. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Ready for review! Created 9 years, 10 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) 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_cache.h"
6
7 #include <limits>
8 #include <string>
9
10 #include "base/file_util.h"
11 #include "base/message_loop.h"
12 #include "base/scoped_temp_dir.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_thread.h"
15 #include "chrome/browser/policy/proto/cloud_policy.pb.h"
16 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
17 #include "chrome/browser/policy/proto/device_management_local.pb.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace policy {
21
22 Value* DecodeIntegerValue(google::protobuf::int64 value);
23 ListValue* DecodeStringList(const em::StringList& string_list);
24
25 // Tests the device management policy cache.
26 class CloudPolicyCacheTest : public testing::Test {
27 protected:
28 CloudPolicyCacheTest()
29 : loop_(MessageLoop::TYPE_UI),
30 ui_thread_(BrowserThread::UI, &loop_),
31 file_thread_(BrowserThread::FILE, &loop_) {}
32
33 void SetUp() {
34 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
35 }
36
37 void TearDown() {
38 loop_.RunAllPending();
39 }
40
41 // Creates a (signed) CloudPolicyResponse setting the given |homepage| and
42 // featuring the given |timestamp| (as issued by the server).
43 // Mildly hacky special feature: pass an empty string as |homepage| to get
44 // a completely empty policy.
45 em::CloudPolicyResponse* CreateHomepagePolicy(const std::string& homepage,
46 const base::Time& timestamp) {
gfeher 2011/02/02 08:42:45 Nit: indent.
Jakob Kummerow 2011/02/03 14:36:52 Done.
47 em::SignedCloudPolicyResponse signed_response;
48 if (homepage != "") {
49 em::CloudPolicySettings* settings = signed_response.mutable_settings();
50 em::HomepageProto* homepage_proto = settings->mutable_homepage();
51 homepage_proto->set_homepagelocation(homepage);
52 }
53 signed_response.set_timestamp(timestamp.ToTimeT());
54 std::string serialized_signed_response;
55 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response));
56
57 em::CloudPolicyResponse* response = new em::CloudPolicyResponse;
58 response->set_signed_response(serialized_signed_response);
59 // TODO(jkummerow): Set proper certificate_chain and signature (when
60 // implementing support for signature verification).
61 response->set_signature("TODO");
62 response->add_certificate_chain("TODO");
63 return response;
64 }
65
66 void WritePolicy(const em::CloudPolicyResponse& policy) {
67 std::string data;
68 em::CachedCloudPolicyResponse cached_policy;
69 cached_policy.mutable_policy_response()->CopyFrom(policy);
70 EXPECT_TRUE(cached_policy.SerializeToString(&data));
71 int size = static_cast<int>(data.size());
72 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size));
73 }
74
75 FilePath test_file() {
76 return temp_dir_.path().AppendASCII("CloudPolicyCacheTest");
77 }
78
79 bool Equals(const PolicyMapType* a, const PolicyMapType* b) const {
80 return CloudPolicyCache::Equals(a, b);
81 }
82
83 MessageLoop loop_;
84
85 private:
86 ScopedTempDir temp_dir_;
87 BrowserThread ui_thread_;
88 BrowserThread file_thread_;
89 };
90
91 TEST_F(CloudPolicyCacheTest, Equals) {
92 PolicyMapType a;
93 a.insert(std::make_pair(kPolicyHomepageLocation,
94 Value::CreateStringValue("aaa")));
95 PolicyMapType a2;
96 a2.insert(std::make_pair(kPolicyHomepageLocation,
97 Value::CreateStringValue("aaa")));
98 PolicyMapType b;
99 b.insert(std::make_pair(kPolicyHomepageLocation,
100 Value::CreateStringValue("bbb")));
101 PolicyMapType c;
102 c.insert(std::make_pair(kPolicyHomepageLocation,
103 Value::CreateStringValue("aaa")));
104 c.insert(std::make_pair(kPolicyHomepageIsNewTabPage,
105 Value::CreateBooleanValue(true)));
106 EXPECT_FALSE(Equals(&a, &b));
107 EXPECT_FALSE(Equals(&b, &a));
108 EXPECT_FALSE(Equals(&a, &c));
109 EXPECT_FALSE(Equals(&c, &a));
110 EXPECT_TRUE(Equals(&a, &a2));
111 EXPECT_TRUE(Equals(&a2, &a));
112 }
113
114 TEST_F(CloudPolicyCacheTest, DecodePolicy) {
115 em::CloudPolicySettings settings;
116 settings.mutable_homepage()->set_homepagelocation("chromium.org");
117 settings.mutable_javascriptenabled()->set_javascriptenabled(true);
118 settings.mutable_javascriptenabled()->mutable_policy_options()->set_mode(
119 em::PolicyOptions::MANDATORY);
120 settings.mutable_policyrefreshrate()->set_policyrefreshrate(5);
121 settings.mutable_policyrefreshrate()->mutable_policy_options()->set_mode(
122 em::PolicyOptions::RECOMMENDED);
123 scoped_ptr<PolicyMapType> mandatory_policy(new PolicyMapType);
124 scoped_ptr<PolicyMapType> recommended_policy(new PolicyMapType);
125 DecodePolicy(settings, mandatory_policy.get(), recommended_policy.get());
126 PolicyMapType mandatory;
127 mandatory.insert(std::make_pair(kPolicyHomepageLocation,
128 Value::CreateStringValue("chromium.org")));
129 mandatory.insert(std::make_pair(kPolicyJavascriptEnabled,
130 Value::CreateBooleanValue(true)));
131 PolicyMapType recommended;
132 recommended.insert(std::make_pair(kPolicyPolicyRefreshRate,
133 Value::CreateIntegerValue(5)));
134 EXPECT_TRUE(Equals(&mandatory, mandatory_policy.get()));
135 EXPECT_TRUE(Equals(&recommended, recommended_policy.get()));
136 }
137
138 TEST_F(CloudPolicyCacheTest, DecodeIntegerValue) {
139 const int min = std::numeric_limits<int>::min();
140 const int max = std::numeric_limits<int>::max();
141 scoped_ptr<Value> value(
142 DecodeIntegerValue(static_cast<google::protobuf::int64>(42)));
143 ASSERT_TRUE(value.get());
144 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(42)));
145 value.reset(
146 DecodeIntegerValue(static_cast<google::protobuf::int64>(min - 1LL)));
147 EXPECT_EQ(NULL, value.get());
148 value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(min)));
149 ASSERT_TRUE(value.get());
150 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(min)));
151 value.reset(
152 DecodeIntegerValue(static_cast<google::protobuf::int64>(max + 1LL)));
153 EXPECT_EQ(NULL, value.get());
154 value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(max)));
155 ASSERT_TRUE(value.get());
156 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(max)));
157 }
158
159 TEST_F(CloudPolicyCacheTest, DecodeStringList) {
160 em::StringList string_list;
161 string_list.add_entries("ponies");
162 string_list.add_entries("more ponies");
163 scoped_ptr<ListValue> decoded(DecodeStringList(string_list));
164 ListValue expected;
165 expected.Append(Value::CreateStringValue("ponies"));
166 expected.Append(Value::CreateStringValue("more ponies"));
167 EXPECT_TRUE(decoded->Equals(&expected));
168 }
169
170 TEST_F(CloudPolicyCacheTest, Empty) {
171 CloudPolicyCache cache(test_file());
172 PolicyMapType empty;
173 scoped_ptr<PolicyMapType> policy(cache.GetPolicy());
174 EXPECT_TRUE(Equals(&empty, policy.get()));
175 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
176 }
177
178 TEST_F(CloudPolicyCacheTest, LoadNoFile) {
179 CloudPolicyCache cache(test_file());
180 cache.LoadPolicyFromFile();
181 PolicyMapType empty;
182 scoped_ptr<PolicyMapType> policy(cache.GetPolicy());
183 EXPECT_TRUE(Equals(&empty, policy.get()));
184 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
185 }
186
187 TEST_F(CloudPolicyCacheTest, RejectFuture) {
188 scoped_ptr<em::CloudPolicyResponse> policy_response(
189 CreateHomepagePolicy("", base::Time::NowFromSystemTime() +
190 base::TimeDelta::FromMinutes(5)));
191 WritePolicy(*policy_response);
192 CloudPolicyCache cache(test_file());
193 cache.LoadPolicyFromFile();
194 PolicyMapType empty;
195 scoped_ptr<PolicyMapType> policy(cache.GetPolicy());
196 EXPECT_TRUE(Equals(&empty, policy.get()));
197 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
198 }
199
200 TEST_F(CloudPolicyCacheTest, LoadWithFile) {
201 scoped_ptr<em::CloudPolicyResponse> policy_response(
202 CreateHomepagePolicy("", base::Time::NowFromSystemTime()));
203 WritePolicy(*policy_response);
204 CloudPolicyCache cache(test_file());
205 cache.LoadPolicyFromFile();
206 PolicyMapType empty;
207 scoped_ptr<PolicyMapType> policy(cache.GetPolicy());
208 EXPECT_TRUE(Equals(&empty, policy.get()));
209 EXPECT_NE(base::Time(), cache.last_policy_refresh_time());
210 EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time());
211 }
212
213 TEST_F(CloudPolicyCacheTest, LoadWithData) {
214 scoped_ptr<em::CloudPolicyResponse> policy(
215 CreateHomepagePolicy("http://www.example.com",
216 base::Time::NowFromSystemTime()));
217 WritePolicy(*policy);
218 CloudPolicyCache cache(test_file());
219 cache.LoadPolicyFromFile();
220 PolicyMapType expected;
221 expected.insert(
222 std::make_pair(kPolicyHomepageLocation,
223 Value::CreateStringValue("http://www.example.com")));
224 scoped_ptr<PolicyMapType> policy_value(cache.GetPolicy());
225 EXPECT_TRUE(Equals(&expected, policy_value.get()));
226 }
227
228 TEST_F(CloudPolicyCacheTest, SetPolicy) {
229 CloudPolicyCache cache(test_file());
230 scoped_ptr<em::CloudPolicyResponse> policy(
231 CreateHomepagePolicy("http://www.example.com",
232 base::Time::NowFromSystemTime()));
233 EXPECT_TRUE(cache.SetPolicy(*policy));
234 scoped_ptr<em::CloudPolicyResponse> policy2(
235 CreateHomepagePolicy("http://www.example.com",
236 base::Time::NowFromSystemTime()));
237 EXPECT_FALSE(cache.SetPolicy(*policy2));
238 PolicyMapType expected;
239 expected.insert(
240 std::make_pair(kPolicyHomepageLocation,
241 Value::CreateStringValue("http://www.example.com")));
242 scoped_ptr<PolicyMapType> policy_value(cache.GetPolicy());
243 EXPECT_TRUE(Equals(&expected, policy_value.get()));
244 }
245
246 TEST_F(CloudPolicyCacheTest, ResetPolicy) {
247 CloudPolicyCache cache(test_file());
248
249 scoped_ptr<em::CloudPolicyResponse> policy(
250 CreateHomepagePolicy("http://www.example.com",
251 base::Time::NowFromSystemTime()));
252 EXPECT_TRUE(cache.SetPolicy(*policy));
253 PolicyMapType expected;
254 expected.insert(
255 std::make_pair(kPolicyHomepageLocation,
256 Value::CreateStringValue("http://www.example.com")));
257 scoped_ptr<PolicyMapType> policy_value(cache.GetPolicy());
258 EXPECT_TRUE(Equals(&expected, policy_value.get()));
259
260 scoped_ptr<em::CloudPolicyResponse> empty_policy(
261 CreateHomepagePolicy("", base::Time::NowFromSystemTime()));
262 EXPECT_TRUE(cache.SetPolicy(*empty_policy));
263 policy_value.reset(cache.GetPolicy());
264 PolicyMapType empty;
265 EXPECT_TRUE(Equals(&empty, policy_value.get()));
266 }
267
268 TEST_F(CloudPolicyCacheTest, PersistPolicy) {
269 {
270 CloudPolicyCache cache(test_file());
271 scoped_ptr<em::CloudPolicyResponse> policy(
272 CreateHomepagePolicy("http://www.example.com",
273 base::Time::NowFromSystemTime()));
274 cache.SetPolicy(*policy);
275 }
276
277 loop_.RunAllPending();
278
279 EXPECT_TRUE(file_util::PathExists(test_file()));
280 CloudPolicyCache cache(test_file());
281 cache.LoadPolicyFromFile();
282 PolicyMapType expected;
283 expected.insert(
284 std::make_pair(kPolicyHomepageLocation,
285 Value::CreateStringValue("http://www.example.com")));
286 scoped_ptr<PolicyMapType> policy_value(cache.GetPolicy());
287 EXPECT_TRUE(Equals(&expected, policy_value.get()));
288 }
289
290 TEST_F(CloudPolicyCacheTest, FreshPolicyOverride) {
291 scoped_ptr<em::CloudPolicyResponse> policy(
292 CreateHomepagePolicy("http://www.example.com",
293 base::Time::NowFromSystemTime()));
294 WritePolicy(*policy);
295
296 CloudPolicyCache cache(test_file());
297 scoped_ptr<em::CloudPolicyResponse> updated_policy(
298 CreateHomepagePolicy("http://www.chromium.org",
299 base::Time::NowFromSystemTime()));
300 EXPECT_TRUE(cache.SetPolicy(*updated_policy));
301
302 cache.LoadPolicyFromFile();
303 PolicyMapType expected;
304 expected.insert(
305 std::make_pair(kPolicyHomepageLocation,
306 Value::CreateStringValue("http://www.chromium.org")));
307 scoped_ptr<PolicyMapType> policy_value(cache.GetPolicy());
308 EXPECT_TRUE(Equals(&expected, policy_value.get()));
309 }
310
311 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698