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 "chromeos/chromeos_paths.h" 18 #include "chromeos/dbus/fake_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 CHECK(temp_dir_.CreateUniqueTempDir()); 32 } 33 34 DevicePolicyCrosTestHelper::~DevicePolicyCrosTestHelper() {} 35 36 void DevicePolicyCrosTestHelper::MarkAsEnterpriseOwned() { 37 cryptohome::SerializedInstallAttributes install_attrs_proto; 38 cryptohome::SerializedInstallAttributes::Attribute* attribute = NULL; 39 40 attribute = install_attrs_proto.add_attributes(); 41 attribute->set_name(EnterpriseInstallAttributes::kAttrEnterpriseOwned); 42 attribute->set_value("true"); 43 44 attribute = install_attrs_proto.add_attributes(); 45 attribute->set_name(EnterpriseInstallAttributes::kAttrEnterpriseUser); 46 attribute->set_value(device_policy_.policy_data().username()); 47 48 base::FilePath install_attrs_file = 49 temp_dir_.path().AppendASCII("install_attributes.pb"); 50 const std::string install_attrs_blob( 51 install_attrs_proto.SerializeAsString()); 52 ASSERT_EQ(static_cast<int>(install_attrs_blob.size()), 53 file_util::WriteFile(install_attrs_file, 54 install_attrs_blob.c_str(), 55 install_attrs_blob.size())); 56 ASSERT_TRUE(PathService::Override(chromeos::FILE_INSTALL_ATTRIBUTES, 57 install_attrs_file)); 58 } 59 60 void DevicePolicyCrosTestHelper::InstallOwnerKey() { 61 base::FilePath owner_key_file = temp_dir_.path().AppendASCII("owner.key"); 62 std::vector<uint8> owner_key_bits; 63 ASSERT_TRUE( 64 device_policy()->GetSigningKey()->ExportPublicKey(&owner_key_bits)); 65 ASSERT_EQ( 66 file_util::WriteFile( 67 owner_key_file, 68 reinterpret_cast<const char*>(vector_as_array(&owner_key_bits)), 69 owner_key_bits.size()), 70 static_cast<int>(owner_key_bits.size())); 71 ASSERT_TRUE(PathService::Override(chromeos::FILE_OWNER_KEY, owner_key_file)); 72 } 73 74 DevicePolicyCrosBrowserTest::DevicePolicyCrosBrowserTest() 75 : fake_dbus_thread_manager_(new chromeos::FakeDBusThreadManager), 76 fake_session_manager_client_(new chromeos::FakeSessionManagerClient) { 77 fake_dbus_thread_manager_->SetFakeClients(); 78 fake_dbus_thread_manager_->SetSessionManagerClient( 79 scoped_ptr<chromeos::SessionManagerClient>(fake_session_manager_client_)); 80 } 81 82 DevicePolicyCrosBrowserTest::~DevicePolicyCrosBrowserTest() { 83 } 84 85 void DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture() { 86 chromeos::DBusThreadManager::SetInstanceForTesting(fake_dbus_thread_manager_); 87 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); 88 } 89 90 void DevicePolicyCrosBrowserTest::TearDownInProcessBrowserTestFixture() { 91 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); 92 } 93 94 void DevicePolicyCrosBrowserTest::MarkAsEnterpriseOwned() { 95 test_helper_.MarkAsEnterpriseOwned(); 96 } 97 98 void DevicePolicyCrosBrowserTest::InstallOwnerKey() { 99 test_helper_.InstallOwnerKey(); 100 } 101 102 void DevicePolicyCrosBrowserTest::RefreshDevicePolicy() { 103 // Reset the key to its original state. 104 device_policy()->SetDefaultSigningKey(); 105 device_policy()->Build(); 106 session_manager_client()->set_device_policy(device_policy()->GetBlob()); 107 session_manager_client()->OnPropertyChangeComplete(true); 108 } 109 110 } // namespace policy 111