1 // Copyright (c) 2013 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.
5 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/path_service.h"
13 #include "base/stl_util.h"
14 #include "chrome/browser/chromeos/policy/device_policy_builder.h"
15 #include "chrome/browser/chromeos/policy/enterprise_install_attributes.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chromeos/chromeos_paths.h"
18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "chromeos/dbus/fake_session_manager_client.h"
20 #include "crypto/rsa_private_key.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
25 using ::testing::AnyNumber
;
26 using ::testing::Return
;
30 DevicePolicyCrosTestHelper::DevicePolicyCrosTestHelper() {}
32 DevicePolicyCrosTestHelper::~DevicePolicyCrosTestHelper() {}
34 void DevicePolicyCrosTestHelper::MarkAsEnterpriseOwned() {
37 const std::string
install_attrs_blob(
38 EnterpriseInstallAttributes::
39 GetEnterpriseOwnedInstallAttributesBlobForTesting(
40 device_policy_
.policy_data().username()));
42 base::FilePath install_attrs_file
;
44 PathService::Get(chromeos::FILE_INSTALL_ATTRIBUTES
, &install_attrs_file
));
45 ASSERT_EQ(static_cast<int>(install_attrs_blob
.size()),
46 base::WriteFile(install_attrs_file
,
47 install_attrs_blob
.c_str(),
48 install_attrs_blob
.size()));
51 void DevicePolicyCrosTestHelper::InstallOwnerKey() {
54 base::FilePath owner_key_file
;
55 ASSERT_TRUE(PathService::Get(chromeos::FILE_OWNER_KEY
, &owner_key_file
));
56 std::vector
<uint8
> owner_key_bits
;
58 device_policy()->GetSigningKey()->ExportPublicKey(&owner_key_bits
));
59 ASSERT_EQ(base::WriteFile(
61 reinterpret_cast<const char*>(vector_as_array(&owner_key_bits
)),
62 owner_key_bits
.size()),
63 static_cast<int>(owner_key_bits
.size()));
66 void DevicePolicyCrosTestHelper::OverridePaths() {
67 // This is usually done by ChromeBrowserMainChromeOS, but some tests
68 // use the overridden paths before ChromeBrowserMain starts. Make sure that
69 // the paths are overridden before using them.
70 base::FilePath user_data_dir
;
71 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
));
72 chromeos::RegisterStubPathOverrides(user_data_dir
);
75 DevicePolicyCrosBrowserTest::DevicePolicyCrosBrowserTest()
76 : fake_session_manager_client_(new chromeos::FakeSessionManagerClient
) {
79 DevicePolicyCrosBrowserTest::~DevicePolicyCrosBrowserTest() {
82 void DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture() {
83 dbus_setter_
= chromeos::DBusThreadManager::GetSetterForTesting();
84 dbus_setter_
->SetSessionManagerClient(
85 scoped_ptr
<chromeos::SessionManagerClient
>(fake_session_manager_client_
));
86 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
89 void DevicePolicyCrosBrowserTest::TearDownInProcessBrowserTestFixture() {
90 InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
93 void DevicePolicyCrosBrowserTest::MarkAsEnterpriseOwned() {
94 test_helper_
.MarkAsEnterpriseOwned();
97 void DevicePolicyCrosBrowserTest::InstallOwnerKey() {
98 test_helper_
.InstallOwnerKey();
101 void DevicePolicyCrosBrowserTest::RefreshDevicePolicy() {
102 // Reset the key to its original state.
103 device_policy()->SetDefaultSigningKey();
104 device_policy()->Build();
105 session_manager_client()->set_device_policy(device_policy()->GetBlob());
106 session_manager_client()->OnPropertyChangeComplete(true);
109 } // namespace policy