File: device_settings_test_helper.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (121 lines) | stat: -rw-r--r-- 4,696 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/settings/device_settings_test_helper.h"

#include <memory>

#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "chrome/browser/ash/ownership/owner_settings_service_ash.h"
#include "chrome/browser/ash/ownership/owner_settings_service_ash_factory.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/ash/settings/device_settings_service.h"
#include "chrome/browser/net/fake_nss_service.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/components/browser_context_helper/annotated_account_id.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "chromeos/ash/components/dbus/concierge/concierge_client.h"
#include "chromeos/ash/components/dbus/userdataauth/cryptohome_misc_client.h"
#include "chromeos/ash/components/dbus/userdataauth/userdataauth_client.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "chromeos/dbus/tpm_manager/tpm_manager_client.h"
#include "components/ownership/mock_owner_key_util.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_utils.h"

namespace ash {

DeviceSettingsTestBase::DeviceSettingsTestBase(bool profile_creation_enabled)
    : profile_creation_enabled_(profile_creation_enabled),
      task_environment_(content::BrowserTaskEnvironment::IO_MAINLOOP) {}

DeviceSettingsTestBase::DeviceSettingsTestBase(
    base::test::TaskEnvironment::TimeSource time)
    : task_environment_(content::BrowserTaskEnvironment::IO_MAINLOOP, time) {}

DeviceSettingsTestBase::~DeviceSettingsTestBase() {
  CHECK(teardown_called_);
}

void DeviceSettingsTestBase::SetUp() {
  // Initialize ProfileHelper including BrowserContextHelper.
  ProfileHelper::Get();

  device_policy_ = std::make_unique<policy::DevicePolicyBuilder>();
  owner_key_util_ = new ownership::MockOwnerKeyUtil();
  device_settings_service_ = std::make_unique<DeviceSettingsService>();
  ConciergeClient::InitializeFake(/*fake_cicerone_client=*/nullptr);
  UserDataAuthClient::InitializeFake();
  CryptohomeMiscClient::InitializeFake();
  chromeos::PowerManagerClient::InitializeFake();
  chromeos::TpmManagerClient::InitializeFake();
  OwnerSettingsServiceAshFactory::SetDeviceSettingsServiceForTesting(
      device_settings_service_.get());
  OwnerSettingsServiceAshFactory::GetInstance()->SetOwnerKeyUtilForTesting(
      owner_key_util_);
  base::RunLoop().RunUntilIdle();

  device_policy_->payload().mutable_metrics_enabled()->set_metrics_enabled(
      false);
  ReloadDevicePolicy();
  owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_->GetSigningKey());
  device_settings_service_->StartProcessing(
      TestingBrowserProcess::GetGlobal()->local_state(),
      &session_manager_client_, owner_key_util_);

  if (profile_creation_enabled_) {
    profile_ = std::make_unique<TestingProfile>();
    FakeNssService::InitializeForBrowserContext(profile_.get(),
                                                /*enable_system_slot=*/false);
  }
}

void DeviceSettingsTestBase::TearDown() {
  teardown_called_ = true;
  OwnerSettingsServiceAshFactory::SetDeviceSettingsServiceForTesting(nullptr);
  FlushDeviceSettings();
  device_settings_service_->StopProcessing();
  device_settings_service_.reset();
  chromeos::TpmManagerClient::Shutdown();
  chromeos::PowerManagerClient::Shutdown();
  CryptohomeMiscClient::Shutdown();
  UserDataAuthClient::Shutdown();
  device_policy_.reset();
  base::RunLoop().RunUntilIdle();
  profile_.reset();
  ConciergeClient::Shutdown();
}

void DeviceSettingsTestBase::ReloadDevicePolicy() {
  device_policy_->Build();
  session_manager_client_.set_device_policy(device_policy_->GetBlob());
}

void DeviceSettingsTestBase::FlushDeviceSettings() {
  content::RunAllTasksUntilIdle();
}

void DeviceSettingsTestBase::ReloadDeviceSettings() {
  device_settings_service_->OwnerKeySet(true);
  FlushDeviceSettings();
}

void DeviceSettingsTestBase::InitOwner(const AccountId& account_id,
                                       bool tpm_is_ready) {
  ash::AnnotatedAccountId::Set(profile_.get(), account_id);
  OwnerSettingsServiceAsh* service =
      OwnerSettingsServiceAshFactory::GetForBrowserContext(profile_.get());
  CHECK(service);
  if (tpm_is_ready)
    service->OnTPMTokenReady();
}

void DeviceSettingsTestBase::SetSessionStopping() {
  session_manager_client_.NotifySessionStopping();
}

}  // namespace ash