Upstream version 7.36.149.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/file_util.h"
11 #include "base/files/file_path.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/browser/chromeos/policy/proto/install_attributes.pb.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chromeos/chromeos_paths.h"
19 #include "chromeos/dbus/fake_dbus_thread_manager.h"
20 #include "chromeos/dbus/fake_session_manager_client.h"
21 #include "crypto/rsa_private_key.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 using ::testing::_;
26 using ::testing::AnyNumber;
27 using ::testing::Return;
28
29 namespace policy {
30
31 DevicePolicyCrosTestHelper::DevicePolicyCrosTestHelper() {}
32
33 DevicePolicyCrosTestHelper::~DevicePolicyCrosTestHelper() {}
34
35 void DevicePolicyCrosTestHelper::MarkAsEnterpriseOwned() {
36   OverridePaths();
37
38   cryptohome::SerializedInstallAttributes install_attrs_proto;
39   cryptohome::SerializedInstallAttributes::Attribute* attribute = NULL;
40
41   attribute = install_attrs_proto.add_attributes();
42   attribute->set_name(EnterpriseInstallAttributes::kAttrEnterpriseOwned);
43   attribute->set_value("true");
44
45   attribute = install_attrs_proto.add_attributes();
46   attribute->set_name(EnterpriseInstallAttributes::kAttrEnterpriseUser);
47   attribute->set_value(device_policy_.policy_data().username());
48
49   base::FilePath install_attrs_file;
50   ASSERT_TRUE(
51       PathService::Get(chromeos::FILE_INSTALL_ATTRIBUTES, &install_attrs_file));
52   const std::string install_attrs_blob(
53       install_attrs_proto.SerializeAsString());
54   ASSERT_EQ(static_cast<int>(install_attrs_blob.size()),
55             base::WriteFile(install_attrs_file,
56                             install_attrs_blob.c_str(),
57                             install_attrs_blob.size()));
58 }
59
60 void DevicePolicyCrosTestHelper::InstallOwnerKey() {
61   OverridePaths();
62
63   base::FilePath owner_key_file;
64   ASSERT_TRUE(PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_file));
65   std::vector<uint8> owner_key_bits;
66   ASSERT_TRUE(
67       device_policy()->GetSigningKey()->ExportPublicKey(&owner_key_bits));
68   ASSERT_EQ(base::WriteFile(
69           owner_key_file,
70           reinterpret_cast<const char*>(vector_as_array(&owner_key_bits)),
71           owner_key_bits.size()),
72       static_cast<int>(owner_key_bits.size()));
73 }
74
75 void DevicePolicyCrosTestHelper::OverridePaths() {
76   // This is usually done by ChromeBrowserMainChromeOS, but some tests
77   // use the overridden paths before ChromeBrowserMain starts. Make sure that
78   // the paths are overridden before using them.
79   base::FilePath user_data_dir;
80   ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
81   chromeos::RegisterStubPathOverrides(user_data_dir);
82 }
83
84 DevicePolicyCrosBrowserTest::DevicePolicyCrosBrowserTest()
85     : fake_dbus_thread_manager_(new chromeos::FakeDBusThreadManager),
86       fake_session_manager_client_(new chromeos::FakeSessionManagerClient) {
87   fake_dbus_thread_manager_->SetFakeClients();
88   fake_dbus_thread_manager_->SetSessionManagerClient(
89       scoped_ptr<chromeos::SessionManagerClient>(fake_session_manager_client_));
90 }
91
92 DevicePolicyCrosBrowserTest::~DevicePolicyCrosBrowserTest() {
93 }
94
95 void DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture() {
96   chromeos::DBusThreadManager::SetInstanceForTesting(fake_dbus_thread_manager_);
97   InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
98 }
99
100 void DevicePolicyCrosBrowserTest::TearDownInProcessBrowserTestFixture() {
101   InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
102 }
103
104 void DevicePolicyCrosBrowserTest::MarkAsEnterpriseOwned() {
105   test_helper_.MarkAsEnterpriseOwned();
106 }
107
108 void DevicePolicyCrosBrowserTest::InstallOwnerKey() {
109   test_helper_.InstallOwnerKey();
110 }
111
112 void DevicePolicyCrosBrowserTest::RefreshDevicePolicy() {
113   // Reset the key to its original state.
114   device_policy()->SetDefaultSigningKey();
115   device_policy()->Build();
116   session_manager_client()->set_device_policy(device_policy()->GetBlob());
117   session_manager_client()->OnPropertyChangeComplete(true);
118 }
119
120 }  // namespace policy