device_policy_cros_browser_test.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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.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_(new chromeos::MockDBusThreadManager) {
30}
31
32DevicePolicyCrosBrowserTest::~DevicePolicyCrosBrowserTest() {
33}
34
35void DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture() {
36  EXPECT_CALL(*mock_dbus_thread_manager_, GetSessionManagerClient())
37      .WillRepeatedly(Return(&session_manager_client_));
38
39  SetMockDBusThreadManagerExpectations();
40  chromeos::DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_);
41  CrosInProcessBrowserTest::SetUpInProcessBrowserTestFixture();
42}
43
44void DevicePolicyCrosBrowserTest::SetMockDBusThreadManagerExpectations() {
45  // TODO(satorux): MockImageBurnerClient seems unnecessary. Remove it?
46  EXPECT_CALL(*mock_dbus_thread_manager_->mock_image_burner_client(),
47              ResetEventHandlers())
48      .Times(AnyNumber());
49  EXPECT_CALL(*mock_dbus_thread_manager_->mock_image_burner_client(),
50              SetEventHandlers(_, _))
51      .Times(AnyNumber());
52}
53
54void DevicePolicyCrosBrowserTest::InstallOwnerKey() {
55  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
56  base::FilePath owner_key_file = temp_dir_.path().AppendASCII("owner.key");
57  std::vector<uint8> owner_key_bits;
58  ASSERT_TRUE(device_policy()->signing_key()->ExportPublicKey(&owner_key_bits));
59  ASSERT_EQ(
60      file_util::WriteFile(
61          owner_key_file,
62          reinterpret_cast<const char*>(vector_as_array(&owner_key_bits)),
63          owner_key_bits.size()),
64      static_cast<int>(owner_key_bits.size()));
65  ASSERT_TRUE(PathService::Override(chromeos::FILE_OWNER_KEY, owner_key_file));
66}
67
68void DevicePolicyCrosBrowserTest::RefreshDevicePolicy() {
69  // Reset the key to its original state.
70  device_policy_.set_signing_key(PolicyBuilder::CreateTestSigningKey());
71  device_policy_.Build();
72  // The local instance of the private half of the owner key must be dropped
73  // as otherwise the NSS library will tell Chrome that the key is available -
74  // which is incorrect and leads to Chrome behaving as if a local owner were
75  // logged in.
76  device_policy_.set_signing_key(
77      make_scoped_ptr<crypto::RSAPrivateKey>(NULL));
78  device_policy_.set_new_signing_key(
79      make_scoped_ptr<crypto::RSAPrivateKey>(NULL));
80  session_manager_client_.set_device_policy(device_policy_.GetBlob());
81  session_manager_client_.OnPropertyChangeComplete(true);
82}
83
84void DevicePolicyCrosBrowserTest::TearDownInProcessBrowserTestFixture() {
85  CrosInProcessBrowserTest::TearDownInProcessBrowserTestFixture();
86  chromeos::DBusThreadManager::Shutdown();
87}
88
89}  // namespace policy
90