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

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

Issue 12183017: Verify the signature on user cloud policy downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased, addressed latest comments Created 7 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
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 "base/base_paths.h" 5 #include "base/base_paths.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
(...skipping 19 matching lines...) Expand all
30 #include "content/public/test/test_utils.h" 30 #include "content/public/test/test_utils.h"
31 #include "googleurl/src/gurl.h" 31 #include "googleurl/src/gurl.h"
32 #include "net/test/test_server.h" 32 #include "net/test/test_server.h"
33 #include "policy/policy_constants.h" 33 #include "policy/policy_constants.h"
34 #include "testing/gmock/include/gmock/gmock.h" 34 #include "testing/gmock/include/gmock/gmock.h"
35 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
36 36
37 #if defined(OS_CHROMEOS) 37 #if defined(OS_CHROMEOS)
38 #include "chrome/browser/chromeos/login/user_manager.h" 38 #include "chrome/browser/chromeos/login/user_manager.h"
39 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" 39 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h"
40 #include "chrome/common/chrome_paths.h"
41 #include "chromeos/dbus/mock_cryptohome_client.h"
42 #include "chromeos/dbus/mock_dbus_thread_manager.h"
43 #include "chromeos/dbus/mock_session_manager_client.h"
40 #else 44 #else
41 #include "chrome/browser/policy/user_cloud_policy_manager.h" 45 #include "chrome/browser/policy/user_cloud_policy_manager.h"
42 #include "chrome/browser/policy/user_cloud_policy_manager_factory.h" 46 #include "chrome/browser/policy/user_cloud_policy_manager_factory.h"
43 #include "chrome/browser/signin/signin_manager.h" 47 #include "chrome/browser/signin/signin_manager.h"
44 #include "chrome/browser/signin/signin_manager_factory.h" 48 #include "chrome/browser/signin/signin_manager_factory.h"
45 #endif 49 #endif
46 50
51 using testing::AnyNumber;
47 using testing::InvokeWithoutArgs; 52 using testing::InvokeWithoutArgs;
48 using testing::Mock; 53 using testing::Mock;
49 using testing::_; 54 using testing::_;
50 55
51 namespace em = enterprise_management; 56 namespace em = enterprise_management;
52 57
53 namespace policy { 58 namespace policy {
54 59
55 namespace { 60 namespace {
56 61
57 class MockCloudPolicyClientObserver : public CloudPolicyClient::Observer { 62 class MockCloudPolicyClientObserver : public CloudPolicyClient::Observer {
58 public: 63 public:
59 MockCloudPolicyClientObserver() {} 64 MockCloudPolicyClientObserver() {}
60 virtual ~MockCloudPolicyClientObserver() {} 65 virtual ~MockCloudPolicyClientObserver() {}
61 66
62 MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*)); 67 MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*));
63 MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*)); 68 MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*));
64 MOCK_METHOD1(OnClientError, void(CloudPolicyClient*)); 69 MOCK_METHOD1(OnClientError, void(CloudPolicyClient*));
65 }; 70 };
66 71
72 #if defined(OS_CHROMEOS)
73
74 const char kSanitizedUsername[] = "0123456789ABCDEF0123456789ABCDEF01234567";
75
76 ACTION(GetSanitizedUsername) {
77 MessageLoop::current()->PostTask(
78 FROM_HERE,
79 base::Bind(arg1, chromeos::DBUS_METHOD_CALL_SUCCESS, kSanitizedUsername));
80 }
81
82 ACTION_P(RetrieveUserPolicy, storage) {
83 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, *storage));
84 }
85
86 ACTION_P2(StoreUserPolicy, storage, user_policy_key_file) {
87 // The session_manager stores a copy of the policy key at
88 // /var/run/user_policy/$hash/policy.pub. Simulate that behavior here, so
89 // that the policy signature can be validated.
90 em::PolicyFetchResponse policy;
91 ASSERT_TRUE(policy.ParseFromString(arg0));
92 if (policy.has_new_public_key()) {
93 ASSERT_TRUE(file_util::CreateDirectory(user_policy_key_file.DirName()));
94 int result = file_util::WriteFile(
95 user_policy_key_file,
96 policy.new_public_key().data(),
97 policy.new_public_key().size());
98 ASSERT_EQ(static_cast<int>(policy.new_public_key().size()), result);
99 }
100
101 *storage = arg0;
102 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg1, true));
103 }
104
105 #endif
106
67 const char* GetTestUser() { 107 const char* GetTestUser() {
68 #if defined(OS_CHROMEOS) 108 #if defined(OS_CHROMEOS)
69 return chromeos::UserManager::kStubUser; 109 return chromeos::UserManager::kStubUser;
70 #else 110 #else
71 return "user@example.com"; 111 return "user@example.com";
72 #endif 112 #endif
73 } 113 }
74 114
75 std::string GetEmptyPolicy() { 115 std::string GetEmptyPolicy() {
76 const char kEmptyPolicy[] = 116 const char kEmptyPolicy[] =
77 "{" 117 "{"
78 " \"%s\": {" 118 " \"%s\": {"
79 " \"mandatory\": {}," 119 " \"mandatory\": {},"
80 " \"recommended\": {}" 120 " \"recommended\": {}"
81 " }," 121 " },"
82 " \"managed_users\": [ \"*\" ]," 122 " \"managed_users\": [ \"*\" ],"
83 " \"policy_user\": \"%s\"" 123 " \"policy_user\": \"%s\","
124 " \"current_key_index\": 0"
84 "}"; 125 "}";
85 126
86 return base::StringPrintf(kEmptyPolicy, dm_protocol::kChromeUserPolicyType, 127 return base::StringPrintf(
87 GetTestUser()); 128 kEmptyPolicy, dm_protocol::kChromeUserPolicyType, GetTestUser());
88 } 129 }
89 130
90 std::string GetTestPolicy() { 131 std::string GetTestPolicy(int key_version) {
91 const char kTestPolicy[] = 132 const char kTestPolicy[] =
92 "{" 133 "{"
93 " \"%s\": {" 134 " \"%s\": {"
94 " \"mandatory\": {" 135 " \"mandatory\": {"
95 " \"ShowHomeButton\": true," 136 " \"ShowHomeButton\": true,"
96 " \"MaxConnectionsPerProxy\": 42," 137 " \"MaxConnectionsPerProxy\": 42,"
97 " \"URLBlacklist\": [ \"dev.chromium.org\", \"youtube.com\" ]" 138 " \"URLBlacklist\": [ \"dev.chromium.org\", \"youtube.com\" ]"
98 " }," 139 " },"
99 " \"recommended\": {" 140 " \"recommended\": {"
100 " \"HomepageLocation\": \"google.com\"" 141 " \"HomepageLocation\": \"google.com\""
101 " }" 142 " }"
102 " }," 143 " },"
103 " \"managed_users\": [ \"*\" ]," 144 " \"managed_users\": [ \"*\" ],"
104 " \"policy_user\": \"%s\"" 145 " \"policy_user\": \"%s\","
146 " \"current_key_index\": %d"
105 "}"; 147 "}";
106 148
107 return base::StringPrintf(kTestPolicy, dm_protocol::kChromeUserPolicyType, 149 return base::StringPrintf(kTestPolicy,
108 GetTestUser()); 150 dm_protocol::kChromeUserPolicyType,
151 GetTestUser(),
152 key_version);
153 }
154
155 void GetExpectedTestPolicy(PolicyMap* expected) {
156 expected->Set(key::kShowHomeButton, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
157 base::Value::CreateBooleanValue(true));
158 expected->Set(key::kMaxConnectionsPerProxy, POLICY_LEVEL_MANDATORY,
159 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(42));
160 base::ListValue list;
161 list.AppendString("dev.chromium.org");
162 list.AppendString("youtube.com");
163 expected->Set(
164 key::kURLBlacklist, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
165 list.DeepCopy());
166 expected->Set(
167 key::kHomepageLocation, POLICY_LEVEL_RECOMMENDED,
168 POLICY_SCOPE_USER, base::Value::CreateStringValue("google.com"));
109 } 169 }
110 170
111 } // namespace 171 } // namespace
112 172
113 // Tests the cloud policy stack(s). 173 // Tests the cloud policy stack(s).
114 class CloudPolicyTest : public InProcessBrowserTest { 174 class CloudPolicyTest : public InProcessBrowserTest {
115 protected: 175 protected:
116 CloudPolicyTest() {} 176 CloudPolicyTest() {}
117 virtual ~CloudPolicyTest() {} 177 virtual ~CloudPolicyTest() {}
118 178
119 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 179 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
120 // The TestServer wants the docroot as a path relative to the source dir. 180 // The TestServer wants the docroot as a path relative to the source dir.
121 FilePath source; 181 FilePath source;
122 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source)); 182 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source));
123 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(source)); 183 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(source));
124 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetEmptyPolicy())); 184 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetEmptyPolicy()));
125 185
126 test_server_.reset( 186 test_server_.reset(
127 new net::TestServer( 187 new net::TestServer(
128 net::TestServer::TYPE_HTTP, 188 net::TestServer::TYPE_HTTP,
129 net::TestServer::kLocalhost, 189 net::TestServer::kLocalhost,
130 temp_dir_.path().BaseName())); 190 testserver_relative_docroot()));
131 ASSERT_TRUE(test_server_->Start()); 191 ASSERT_TRUE(test_server_->Start());
132 192
133 std::string url = test_server_->GetURL("device_management").spec(); 193 std::string url = test_server_->GetURL("device_management").spec();
134 194
135 CommandLine* command_line = CommandLine::ForCurrentProcess(); 195 CommandLine* command_line = CommandLine::ForCurrentProcess();
136 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url); 196 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url);
197
198 #if defined(OS_CHROMEOS)
199 PathService::Override(chrome::DIR_USER_POLICY_KEYS, user_policy_key_dir());
200
201 mock_dbus_thread_manager_ = new chromeos::MockDBusThreadManager();
202 chromeos::DBusThreadManager::InitializeForTesting(
203 mock_dbus_thread_manager_);
204 EXPECT_CALL(*mock_dbus_thread_manager_->mock_cryptohome_client(),
205 GetSanitizedUsername(_, _))
206 .WillRepeatedly(GetSanitizedUsername());
207 EXPECT_CALL(*mock_dbus_thread_manager_->mock_session_manager_client(),
208 StoreUserPolicy(_, _))
209 .WillRepeatedly(StoreUserPolicy(&session_manager_user_policy_,
210 user_policy_key_file()));
211 EXPECT_CALL(*mock_dbus_thread_manager_->mock_session_manager_client(),
212 RetrieveUserPolicy(_))
213 .WillRepeatedly(RetrieveUserPolicy(&session_manager_user_policy_));
214 #endif
137 } 215 }
138 216
139 virtual void SetUpOnMainThread() OVERRIDE { 217 virtual void SetUpOnMainThread() OVERRIDE {
140 // Checks that no policies have been loaded by the other providers before 218 // Checks that no policies have been loaded by the other providers before
141 // setting up the cloud connection. Other policies configured in the test 219 // setting up the cloud connection. Other policies configured in the test
142 // machine will interfere with these tests. 220 // machine will interfere with these tests.
143 const PolicyMap& map = g_browser_process->policy_service()->GetPolicies( 221 const PolicyMap& map = g_browser_process->policy_service()->GetPolicies(
144 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); 222 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
145 if (!map.empty()) { 223 if (!map.empty()) {
146 base::DictionaryValue dict; 224 base::DictionaryValue dict;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 policy_manager->core()->client()->AddObserver(&observer); 260 policy_manager->core()->client()->AddObserver(&observer);
183 261
184 // Give a bogus OAuth token to the |policy_manager|. This should make its 262 // Give a bogus OAuth token to the |policy_manager|. This should make its
185 // CloudPolicyClient fetch the DMToken. 263 // CloudPolicyClient fetch the DMToken.
186 policy_manager->RegisterClient("bogus"); 264 policy_manager->RegisterClient("bogus");
187 run_loop.Run(); 265 run_loop.Run();
188 Mock::VerifyAndClearExpectations(&observer); 266 Mock::VerifyAndClearExpectations(&observer);
189 policy_manager->core()->client()->RemoveObserver(&observer); 267 policy_manager->core()->client()->RemoveObserver(&observer);
190 } 268 }
191 269
270 FilePath testserver_relative_docroot() {
271 return temp_dir_.path().BaseName().AppendASCII("testserver");
272 }
273
274 FilePath testserver_device_management_file() {
275 return temp_dir_.path().AppendASCII("testserver")
276 .AppendASCII("device_management");
277 }
278
279 #if defined(OS_CHROMEOS)
280 FilePath user_policy_key_dir() {
281 return temp_dir_.path().AppendASCII("user_policy");
282 }
283
284 FilePath user_policy_key_file() {
285 return user_policy_key_dir().AppendASCII(kSanitizedUsername)
286 .AppendASCII("policy.pub");
287 }
288 #endif
289
192 void SetServerPolicy(const std::string& policy) { 290 void SetServerPolicy(const std::string& policy) {
291 ASSERT_TRUE(file_util::CreateDirectory(
292 testserver_device_management_file().DirName()));
193 int result = file_util::WriteFile( 293 int result = file_util::WriteFile(
194 temp_dir_.path().AppendASCII("device_management"), 294 testserver_device_management_file(), policy.data(), policy.size());
195 policy.data(), policy.size());
196 ASSERT_EQ(static_cast<int>(policy.size()), result); 295 ASSERT_EQ(static_cast<int>(policy.size()), result);
197 } 296 }
198 297
199 base::ScopedTempDir temp_dir_; 298 base::ScopedTempDir temp_dir_;
200 scoped_ptr<net::TestServer> test_server_; 299 scoped_ptr<net::TestServer> test_server_;
300
301 #if defined(OS_CHROMEOS)
302 std::string session_manager_user_policy_;
303 chromeos::MockDBusThreadManager* mock_dbus_thread_manager_;
304 #endif
201 }; 305 };
202 306
203 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) { 307 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) {
204 PolicyService* policy_service = browser()->profile()->GetPolicyService(); 308 PolicyService* policy_service = browser()->profile()->GetPolicyService();
205 { 309 {
206 base::RunLoop run_loop; 310 base::RunLoop run_loop;
311 // This does the initial fetch and stores the initial key.
207 policy_service->RefreshPolicies(run_loop.QuitClosure()); 312 policy_service->RefreshPolicies(run_loop.QuitClosure());
208 run_loop.Run(); 313 run_loop.Run();
209 } 314 }
210 315
211 PolicyMap empty; 316 PolicyMap empty;
212 EXPECT_TRUE(empty.Equals(policy_service->GetPolicies( 317 EXPECT_TRUE(empty.Equals(policy_service->GetPolicies(
213 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); 318 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
214 319
215 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetTestPolicy())); 320 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetTestPolicy(0)));
216 PolicyMap expected; 321 PolicyMap expected;
217 expected.Set(key::kShowHomeButton, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 322 GetExpectedTestPolicy(&expected);
218 base::Value::CreateBooleanValue(true));
219 expected.Set(key::kMaxConnectionsPerProxy, POLICY_LEVEL_MANDATORY,
220 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(42));
221 base::ListValue list;
222 list.AppendString("dev.chromium.org");
223 list.AppendString("youtube.com");
224 expected.Set(
225 key::kURLBlacklist, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
226 list.DeepCopy());
227 expected.Set(
228 key::kHomepageLocation, POLICY_LEVEL_RECOMMENDED,
229 POLICY_SCOPE_USER, base::Value::CreateStringValue("google.com"));
230 { 323 {
231 base::RunLoop run_loop; 324 base::RunLoop run_loop;
325 // This fetches the new policies, using the same key.
232 policy_service->RefreshPolicies(run_loop.QuitClosure()); 326 policy_service->RefreshPolicies(run_loop.QuitClosure());
233 run_loop.Run(); 327 run_loop.Run();
234 } 328 }
235 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies( 329 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies(
236 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); 330 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
237 } 331 }
238 332
333 #if defined(OS_CHROMEOS)
334 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicyWithRotatedKey) {
335 PolicyService* policy_service = browser()->profile()->GetPolicyService();
336 {
337 base::RunLoop run_loop;
338 // This does the initial fetch and stores the initial key.
339 policy_service->RefreshPolicies(run_loop.QuitClosure());
340 run_loop.Run();
341 }
342
343 // Read the initial key.
344 std::string initial_key;
345 ASSERT_TRUE(
346 file_util::ReadFileToString(user_policy_key_file(), &initial_key));
347
348 PolicyMap empty;
349 EXPECT_TRUE(empty.Equals(policy_service->GetPolicies(
350 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
351
352 // Set the new policies and a new key at the server.
353 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetTestPolicy(1)));
354 PolicyMap expected;
355 GetExpectedTestPolicy(&expected);
356 {
357 base::RunLoop run_loop;
358 // This fetches the new policies and does a key rotation.
359 policy_service->RefreshPolicies(run_loop.QuitClosure());
360 run_loop.Run();
361 }
362 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies(
363 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
364
365 // Verify that the key was rotated.
366 std::string rotated_key;
367 ASSERT_TRUE(
368 file_util::ReadFileToString(user_policy_key_file(), &rotated_key));
369 EXPECT_NE(rotated_key, initial_key);
370
371 // Another refresh using the same key won't rotate it again.
372 {
373 base::RunLoop run_loop;
374 policy_service->RefreshPolicies(run_loop.QuitClosure());
375 run_loop.Run();
376 }
377 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies(
378 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
379 std::string current_key;
380 ASSERT_TRUE(
381 file_util::ReadFileToString(user_policy_key_file(), &current_key));
382 EXPECT_EQ(rotated_key, current_key);
383 }
384 #endif
385
239 TEST(CloudPolicyProtoTest, VerifyProtobufEquivalence) { 386 TEST(CloudPolicyProtoTest, VerifyProtobufEquivalence) {
240 // There are 2 protobufs that can be used for user cloud policy: 387 // There are 2 protobufs that can be used for user cloud policy:
241 // cloud_policy.proto and chrome_settings.proto. chrome_settings.proto is the 388 // cloud_policy.proto and chrome_settings.proto. chrome_settings.proto is the
242 // version used by the server, but generates one proto message per policy; to 389 // version used by the server, but generates one proto message per policy; to
243 // save binary size on the client, the other version shares proto messages for 390 // save binary size on the client, the other version shares proto messages for
244 // policies of the same type. They generate the same bytes on the wire though, 391 // policies of the same type. They generate the same bytes on the wire though,
245 // so they are compatible. This test verifies that that stays true. 392 // so they are compatible. This test verifies that that stays true.
246 393
247 // Build a ChromeSettingsProto message with one policy of each supported type. 394 // Build a ChromeSettingsProto message with one policy of each supported type.
248 em::ChromeSettingsProto chrome_settings; 395 em::ChromeSettingsProto chrome_settings;
(...skipping 30 matching lines...) Expand all
279 426
280 // They should now serialize to the same bytes. 427 // They should now serialize to the same bytes.
281 std::string chrome_settings_serialized; 428 std::string chrome_settings_serialized;
282 std::string cloud_policy_serialized; 429 std::string cloud_policy_serialized;
283 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); 430 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized));
284 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); 431 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized));
285 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); 432 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized);
286 } 433 }
287 434
288 } // namespace policy 435 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698