device_policy_cros_browser_test.cc revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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 <vector> 8 9#include "base/file_util.h" 10#include "base/files/file_path.h" 11#include "base/files/scoped_temp_dir.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 "chromeos/chromeos_paths.h" 16#include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h" 17#include "chromeos/dbus/mock_image_burner_client.h" 18#include "crypto/rsa_private_key.h" 19#include "testing/gmock/include/gmock/gmock.h" 20#include "testing/gtest/include/gtest/gtest.h" 21 22using ::testing::_; 23using ::testing::AnyNumber; 24using ::testing::Return; 25 26namespace policy { 27 28DevicePolicyCrosBrowserTest::DevicePolicyCrosBrowserTest() 29 : mock_dbus_thread_manager_( 30 new chromeos::MockDBusThreadManagerWithoutGMock) { 31} 32 33DevicePolicyCrosBrowserTest::~DevicePolicyCrosBrowserTest() { 34} 35 36void DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture() { 37 chromeos::DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); 38 CrosInProcessBrowserTest::SetUpInProcessBrowserTestFixture(); 39} 40 41void DevicePolicyCrosBrowserTest::InstallOwnerKey() { 42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 43 base::FilePath owner_key_file = temp_dir_.path().AppendASCII("owner.key"); 44 std::vector<uint8> owner_key_bits; 45 ASSERT_TRUE(device_policy()->signing_key()->ExportPublicKey(&owner_key_bits)); 46 ASSERT_EQ( 47 file_util::WriteFile( 48 owner_key_file, 49 reinterpret_cast<const char*>(vector_as_array(&owner_key_bits)), 50 owner_key_bits.size()), 51 static_cast<int>(owner_key_bits.size())); 52 ASSERT_TRUE(PathService::Override(chromeos::FILE_OWNER_KEY, owner_key_file)); 53} 54 55void DevicePolicyCrosBrowserTest::RefreshDevicePolicy() { 56 // Reset the key to its original state. 57 device_policy_.set_signing_key(PolicyBuilder::CreateTestSigningKey()); 58 device_policy_.Build(); 59 // The local instance of the private half of the owner key must be dropped 60 // as otherwise the NSS library will tell Chrome that the key is available - 61 // which is incorrect and leads to Chrome behaving as if a local owner were 62 // logged in. 63 device_policy_.set_signing_key( 64 make_scoped_ptr<crypto::RSAPrivateKey>(NULL)); 65 device_policy_.set_new_signing_key( 66 make_scoped_ptr<crypto::RSAPrivateKey>(NULL)); 67 session_manager_client()->set_device_policy(device_policy_.GetBlob()); 68 session_manager_client()->OnPropertyChangeComplete(true); 69} 70 71void DevicePolicyCrosBrowserTest::TearDownInProcessBrowserTestFixture() { 72 CrosInProcessBrowserTest::TearDownInProcessBrowserTestFixture(); 73 chromeos::DBusThreadManager::Shutdown(); 74} 75 76} // namespace policy 77