Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / device_policy_cros_browser_test.cc
blob8e780717fee5ab43bfd9a34fb1bb505fa80e35a2
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"
7 #include <vector>
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/path_service.h"
12 #include "base/stl_util.h"
13 #include "chrome/browser/chromeos/policy/device_policy_builder.h"
14 #include "chrome/browser/chromeos/policy/enterprise_install_attributes.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chromeos/chromeos_paths.h"
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/fake_session_manager_client.h"
19 #include "crypto/rsa_private_key.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using ::testing::_;
24 using ::testing::AnyNumber;
25 using ::testing::Return;
27 namespace policy {
29 DevicePolicyCrosTestHelper::DevicePolicyCrosTestHelper() {}
31 DevicePolicyCrosTestHelper::~DevicePolicyCrosTestHelper() {}
33 // static
34 void DevicePolicyCrosTestHelper::MarkAsEnterpriseOwnedBy(
35 const std::string& user_name) {
36 OverridePaths();
38 const std::string install_attrs_blob(
39 EnterpriseInstallAttributes::
40 GetEnterpriseOwnedInstallAttributesBlobForTesting(user_name));
42 base::FilePath install_attrs_file;
43 ASSERT_TRUE(
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::MarkAsEnterpriseOwned() {
52 MarkAsEnterpriseOwnedBy(device_policy_.policy_data().username());
55 void DevicePolicyCrosTestHelper::InstallOwnerKey() {
56 OverridePaths();
58 base::FilePath owner_key_file;
59 ASSERT_TRUE(PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_file));
60 std::vector<uint8> owner_key_bits;
61 ASSERT_TRUE(
62 device_policy()->GetSigningKey()->ExportPublicKey(&owner_key_bits));
63 ASSERT_EQ(base::WriteFile(
64 owner_key_file,
65 reinterpret_cast<const char*>(vector_as_array(&owner_key_bits)),
66 owner_key_bits.size()),
67 static_cast<int>(owner_key_bits.size()));
70 // static
71 void DevicePolicyCrosTestHelper::OverridePaths() {
72 // This is usually done by ChromeBrowserMainChromeOS, but some tests
73 // use the overridden paths before ChromeBrowserMain starts. Make sure that
74 // the paths are overridden before using them.
75 base::FilePath user_data_dir;
76 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
77 chromeos::RegisterStubPathOverrides(user_data_dir);
80 DevicePolicyCrosBrowserTest::DevicePolicyCrosBrowserTest()
81 : fake_session_manager_client_(new chromeos::FakeSessionManagerClient) {
84 DevicePolicyCrosBrowserTest::~DevicePolicyCrosBrowserTest() {
87 void DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture() {
88 dbus_setter_ = chromeos::DBusThreadManager::GetSetterForTesting();
89 dbus_setter_->SetSessionManagerClient(
90 scoped_ptr<chromeos::SessionManagerClient>(fake_session_manager_client_));
91 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
94 void DevicePolicyCrosBrowserTest::TearDownInProcessBrowserTestFixture() {
95 InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
98 void DevicePolicyCrosBrowserTest::MarkAsEnterpriseOwned() {
99 test_helper_.MarkAsEnterpriseOwned();
102 void DevicePolicyCrosBrowserTest::InstallOwnerKey() {
103 test_helper_.InstallOwnerKey();
106 void DevicePolicyCrosBrowserTest::RefreshDevicePolicy() {
107 // Reset the key to its original state.
108 device_policy()->SetDefaultSigningKey();
109 device_policy()->Build();
110 session_manager_client()->set_device_policy(device_policy()->GetBlob());
111 session_manager_client()->OnPropertyChangeComplete(true);
114 } // namespace policy