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

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 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 base::FilePath source; 181 base::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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 policy_manager->core()->client()->AddObserver(&observer); 261 policy_manager->core()->client()->AddObserver(&observer);
184 262
185 // Give a bogus OAuth token to the |policy_manager|. This should make its 263 // Give a bogus OAuth token to the |policy_manager|. This should make its
186 // CloudPolicyClient fetch the DMToken. 264 // CloudPolicyClient fetch the DMToken.
187 policy_manager->RegisterClient("bogus"); 265 policy_manager->RegisterClient("bogus");
188 run_loop.Run(); 266 run_loop.Run();
189 Mock::VerifyAndClearExpectations(&observer); 267 Mock::VerifyAndClearExpectations(&observer);
190 policy_manager->core()->client()->RemoveObserver(&observer); 268 policy_manager->core()->client()->RemoveObserver(&observer);
191 } 269 }
192 270
271 FilePath testserver_relative_docroot() {
272 return temp_dir_.path().BaseName().AppendASCII("testserver");
273 }
274
275 FilePath testserver_device_management_file() {
276 return temp_dir_.path().AppendASCII("testserver")
277 .AppendASCII("device_management");
278 }
279
280 #if defined(OS_CHROMEOS)
281 FilePath user_policy_key_dir() {
282 return temp_dir_.path().AppendASCII("user_policy");
283 }
284
285 FilePath user_policy_key_file() {
286 return user_policy_key_dir().AppendASCII(kSanitizedUsername)
287 .AppendASCII("policy.pub");
288 }
289 #endif
290
193 void SetServerPolicy(const std::string& policy) { 291 void SetServerPolicy(const std::string& policy) {
292 ASSERT_TRUE(file_util::CreateDirectory(
293 testserver_device_management_file().DirName()));
194 int result = file_util::WriteFile( 294 int result = file_util::WriteFile(
195 temp_dir_.path().AppendASCII("device_management"), 295 testserver_device_management_file(), policy.data(), policy.size());
196 policy.data(), policy.size());
197 ASSERT_EQ(static_cast<int>(policy.size()), result); 296 ASSERT_EQ(static_cast<int>(policy.size()), result);
198 } 297 }
199 298
200 base::ScopedTempDir temp_dir_; 299 base::ScopedTempDir temp_dir_;
201 scoped_ptr<net::TestServer> test_server_; 300 scoped_ptr<net::TestServer> test_server_;
301
302 #if defined(OS_CHROMEOS)
303 std::string session_manager_user_policy_;
304 chromeos::MockDBusThreadManager* mock_dbus_thread_manager_;
305 #endif
202 }; 306 };
203 307
204 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) { 308 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) {
205 PolicyService* policy_service = browser()->profile()->GetPolicyService(); 309 PolicyService* policy_service = browser()->profile()->GetPolicyService();
206 { 310 {
207 base::RunLoop run_loop; 311 base::RunLoop run_loop;
312 // This does the initial fetch and stores the initial key.
208 policy_service->RefreshPolicies(run_loop.QuitClosure()); 313 policy_service->RefreshPolicies(run_loop.QuitClosure());
209 run_loop.Run(); 314 run_loop.Run();
210 } 315 }
211 316
212 PolicyMap empty; 317 PolicyMap empty;
213 EXPECT_TRUE(empty.Equals(policy_service->GetPolicies( 318 EXPECT_TRUE(empty.Equals(policy_service->GetPolicies(
214 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); 319 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
215 320
216 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetTestPolicy())); 321 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetTestPolicy(0)));
217 PolicyMap expected; 322 PolicyMap expected;
218 expected.Set(key::kShowHomeButton, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 323 GetExpectedTestPolicy(&expected);
219 base::Value::CreateBooleanValue(true));
220 expected.Set(key::kMaxConnectionsPerProxy, POLICY_LEVEL_MANDATORY,
221 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(42));
222 base::ListValue list;
223 list.AppendString("dev.chromium.org");
224 list.AppendString("youtube.com");
225 expected.Set(
226 key::kURLBlacklist, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
227 list.DeepCopy());
228 expected.Set(
229 key::kHomepageLocation, POLICY_LEVEL_RECOMMENDED,
230 POLICY_SCOPE_USER, base::Value::CreateStringValue("google.com"));
231 { 324 {
232 base::RunLoop run_loop; 325 base::RunLoop run_loop;
326 // This fetches the new policies, using the same key.
233 policy_service->RefreshPolicies(run_loop.QuitClosure()); 327 policy_service->RefreshPolicies(run_loop.QuitClosure());
234 run_loop.Run(); 328 run_loop.Run();
235 } 329 }
236 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies( 330 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies(
237 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); 331 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
238 } 332 }
239 333
334 #if defined(OS_CHROMEOS)
335 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicyWithRotatedKey) {
336 PolicyService* policy_service = browser()->profile()->GetPolicyService();
337 {
338 base::RunLoop run_loop;
339 // This does the initial fetch and stores the initial key.
340 policy_service->RefreshPolicies(run_loop.QuitClosure());
341 run_loop.Run();
342 }
343
344 // Read the initial key.
345 std::string initial_key;
346 ASSERT_TRUE(
347 file_util::ReadFileToString(user_policy_key_file(), &initial_key));
348
349 PolicyMap empty;
350 EXPECT_TRUE(empty.Equals(policy_service->GetPolicies(
351 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
352
353 // Set the new policies and a new key at the server.
354 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetTestPolicy(1)));
355 PolicyMap expected;
356 GetExpectedTestPolicy(&expected);
357 {
358 base::RunLoop run_loop;
359 // This fetches the new policies and does a key rotation.
360 policy_service->RefreshPolicies(run_loop.QuitClosure());
361 run_loop.Run();
362 }
363 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies(
364 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
365
366 // Verify that the key was rotated.
367 std::string rotated_key;
368 ASSERT_TRUE(
369 file_util::ReadFileToString(user_policy_key_file(), &rotated_key));
370 EXPECT_NE(rotated_key, initial_key);
371
372 // Another refresh using the same key won't rotate it again.
373 {
374 base::RunLoop run_loop;
375 policy_service->RefreshPolicies(run_loop.QuitClosure());
376 run_loop.Run();
377 }
378 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies(
379 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))));
380 std::string current_key;
381 ASSERT_TRUE(
382 file_util::ReadFileToString(user_policy_key_file(), &current_key));
383 EXPECT_EQ(rotated_key, current_key);
384 }
385 #endif
386
240 TEST(CloudPolicyProtoTest, VerifyProtobufEquivalence) { 387 TEST(CloudPolicyProtoTest, VerifyProtobufEquivalence) {
241 // There are 2 protobufs that can be used for user cloud policy: 388 // There are 2 protobufs that can be used for user cloud policy:
242 // cloud_policy.proto and chrome_settings.proto. chrome_settings.proto is the 389 // cloud_policy.proto and chrome_settings.proto. chrome_settings.proto is the
243 // version used by the server, but generates one proto message per policy; to 390 // version used by the server, but generates one proto message per policy; to
244 // save binary size on the client, the other version shares proto messages for 391 // save binary size on the client, the other version shares proto messages for
245 // policies of the same type. They generate the same bytes on the wire though, 392 // policies of the same type. They generate the same bytes on the wire though,
246 // so they are compatible. This test verifies that that stays true. 393 // so they are compatible. This test verifies that that stays true.
247 394
248 // Build a ChromeSettingsProto message with one policy of each supported type. 395 // Build a ChromeSettingsProto message with one policy of each supported type.
249 em::ChromeSettingsProto chrome_settings; 396 em::ChromeSettingsProto chrome_settings;
(...skipping 30 matching lines...) Expand all
280 427
281 // They should now serialize to the same bytes. 428 // They should now serialize to the same bytes.
282 std::string chrome_settings_serialized; 429 std::string chrome_settings_serialized;
283 std::string cloud_policy_serialized; 430 std::string cloud_policy_serialized;
284 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); 431 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized));
285 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); 432 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized));
286 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); 433 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized);
287 } 434 }
288 435
289 } // namespace policy 436 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/browser_policy_connector.cc ('k') | chrome/browser/policy/user_cloud_policy_store_chromeos.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698