Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / policy / device_policy_cros_browser_test.cc
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.
4
5 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
6
7 #include <string>
8 #include <vector>
9
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"
23
24 using ::testing::_;
25 using ::testing::AnyNumber;
26 using ::testing::Return;
27
28 namespace policy {
29
30 DevicePolicyCrosTestHelper::DevicePolicyCrosTestHelper() {}
31
32 DevicePolicyCrosTestHelper::~DevicePolicyCrosTestHelper() {}
33
34 void DevicePolicyCrosTestHelper::MarkAsEnterpriseOwned() {
35   OverridePaths();
36
37   const std::string install_attrs_blob(
38       EnterpriseInstallAttributes::
39           GetEnterpriseOwnedInstallAttributesBlobForTesting(
40               device_policy_.policy_data().username()));
41
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()));
49 }
50
51 void DevicePolicyCrosTestHelper::InstallOwnerKey() {
52   OverridePaths();
53
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;
57   ASSERT_TRUE(
58       device_policy()->GetSigningKey()->ExportPublicKey(&owner_key_bits));
59   ASSERT_EQ(base::WriteFile(
60           owner_key_file,
61           reinterpret_cast<const char*>(vector_as_array(&owner_key_bits)),
62           owner_key_bits.size()),
63       static_cast<int>(owner_key_bits.size()));
64 }
65
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);
73 }
74
75 DevicePolicyCrosBrowserTest::DevicePolicyCrosBrowserTest()
76     : fake_session_manager_client_(new chromeos::FakeSessionManagerClient) {
77 }
78
79 DevicePolicyCrosBrowserTest::~DevicePolicyCrosBrowserTest() {
80 }
81
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();
87 }
88
89 void DevicePolicyCrosBrowserTest::TearDownInProcessBrowserTestFixture() {
90   InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
91 }
92
93 void DevicePolicyCrosBrowserTest::MarkAsEnterpriseOwned() {
94   test_helper_.MarkAsEnterpriseOwned();
95 }
96
97 void DevicePolicyCrosBrowserTest::InstallOwnerKey() {
98   test_helper_.InstallOwnerKey();
99 }
100
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);
107 }
108
109 }  // namespace policy