File: test_helper.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (194 lines) | stat: -rw-r--r-- 7,523 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/user_manager/test_helper.h"

#include "base/check_deref.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "chromeos/ash/components/dbus/userdataauth/userdataauth_client.h"
#include "chromeos/ash/components/policy/device_local_account/device_local_account_type.h"
#include "components/account_id/account_id.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/user_manager/known_user.h"
#include "components/user_manager/user_manager.h"
#include "components/user_manager/user_manager_pref_names.h"
#include "components/user_manager/user_names.h"

namespace user_manager {
namespace {

void RegisterPersistedUserInternal(PrefService& local_state,
                                   const AccountId& account_id,
                                   UserType user_type) {
  {
    ScopedListPrefUpdate update(&local_state, prefs::kRegularUsersPref);
    update->Append(account_id.GetUserEmail());
  }
  {
    ScopedDictPrefUpdate update(&local_state, prefs::kUserType);
    update->Set(account_id.GetAccountIdKey(), static_cast<int>(user_type));
  }
  {
    KnownUser known_user(&local_state);
    known_user.UpdateId(account_id);
  }
}

}  // namespace

// static
void TestHelper::RegisterPersistedUser(PrefService& local_state,
                                       const AccountId& account_id) {
  RegisterPersistedUserInternal(local_state, account_id, UserType::kRegular);
}

// static
void TestHelper::RegisterPersistedChildUser(PrefService& local_state,
                                            const AccountId& account_id) {
  RegisterPersistedUserInternal(local_state, account_id, UserType::kChild);
}

// static
void TestHelper::RegisterKioskAppUser(PrefService& local_state,
                                      std::string_view user_id) {
  auto type = policy::GetDeviceLocalAccountType(user_id);
  CHECK_EQ(type, policy::DeviceLocalAccountType::kKioskApp)
      << user_id << " did not satisfy to be used for a kiosk user. "
      << "See policy::GetDeviceLocalAccountType for details";
  ScopedListPrefUpdate update(&local_state,
                              prefs::kDeviceLocalAccountsWithSavedData);
  update->Append(user_id);
}

// static
void TestHelper::RegisterWebKioskAppUser(PrefService& local_state,
                                         std::string_view user_id) {
  auto type = policy::GetDeviceLocalAccountType(user_id);
  CHECK_EQ(type, policy::DeviceLocalAccountType::kWebKioskApp)
      << user_id << " did not satisfy to be used for a web kiosk user. "
      << "See policy::GetDeviceLocalAccountType for details";
  ScopedListPrefUpdate update(&local_state,
                              prefs::kDeviceLocalAccountsWithSavedData);
  update->Append(user_id);
}

// static
void TestHelper::RegisterPublicAccountUser(PrefService& local_state,
                                           std::string_view user_id) {
  auto type = policy::GetDeviceLocalAccountType(user_id);
  CHECK_EQ(type, policy::DeviceLocalAccountType::kPublicSession)
      << user_id << " did not satisfy to be used for a public account user. "
      << "See policy::GetDeviceLocalAccountType for details";
  ScopedListPrefUpdate update(&local_state,
                              prefs::kDeviceLocalAccountsWithSavedData);
  update->Append(user_id);
}

// static
std::string TestHelper::GetFakeUsernameHash(const AccountId& account_id) {
  CHECK(account_id.is_valid());
  return ash::UserDataAuthClient::GetStubSanitizedUsername(
      cryptohome::CreateAccountIdentifierFromAccountId(account_id));
}

TestHelper::TestHelper(UserManager* user_manager)
    : user_manager_(CHECK_DEREF(user_manager)) {}

TestHelper::~TestHelper() = default;

User* TestHelper::AddRegularUser(const AccountId& account_id) {
  return AddUserInternal(account_id, UserType::kRegular);
}

User* TestHelper::AddChildUser(const AccountId& account_id) {
  return AddUserInternal(account_id, UserType::kChild);
}

User* TestHelper::AddGuestUser() {
  return AddUserInternal(GuestAccountId(), UserType::kGuest);
}

User* TestHelper::AddUserInternal(const AccountId& account_id,
                                  UserType user_type) {
  if (user_manager_->FindUser(account_id)) {
    LOG(ERROR) << "User for " << account_id << " already exists";
    return nullptr;
  }

  if (!user_manager_->EnsureUser(account_id, user_type,
                                 /*is_ephemeral=*/false)) {
    LOG(ERROR) << "Failed to create a user " << user_type << " for "
               << account_id;
    return nullptr;
  }
  return user_manager_->FindUserAndModify(account_id);
}

User* TestHelper::AddKioskChromeAppUser(std::string_view user_id) {
  // Quick check that the `user_id` satisfies kiosk-app type.
  auto type = policy::GetDeviceLocalAccountType(user_id);
  if (type != policy::DeviceLocalAccountType::kKioskApp) {
    LOG(ERROR) << "user_id (" << user_id << ") did not satisfy to be used for "
               << "a kiosk chrome app user. See "
                  "policy::GetDeviceLocalAccountType for details.";
    return nullptr;
  }

  return AddDeviceLocalAccountUserInternal(user_id, UserType::kKioskChromeApp);
}

User* TestHelper::AddKioskWebAppUser(std::string_view user_id) {
  // Quick check that the `user_id` satisfies web-kiosk-app type.
  auto type = policy::GetDeviceLocalAccountType(user_id);
  if (type != policy::DeviceLocalAccountType::kWebKioskApp) {
    LOG(ERROR) << "user_id (" << user_id << ") did not satisfy to be used for "
               << "a kiosk web app user. See policy::GetDeviceLocalAccountType "
                  "for details.";
    return nullptr;
  }

  return AddDeviceLocalAccountUserInternal(user_id, UserType::kKioskWebApp);
}

User* TestHelper::AddPublicAccountUser(std::string_view user_id) {
  // Quick check that the `user_id` satisfies kiosk-app type.
  auto type = policy::GetDeviceLocalAccountType(user_id);
  if (type != policy::DeviceLocalAccountType::kPublicSession) {
    LOG(ERROR)
        << "user_id (" << user_id << ") did not satisfy to be used for "
        << "a public account user. See policy::GetDeviceLocalAccountType "
        << "for details.";
    return nullptr;
  }

  return AddDeviceLocalAccountUserInternal(user_id, UserType::kPublicAccount);
}

User* TestHelper::AddDeviceLocalAccountUserInternal(std::string_view user_id,
                                                    UserType user_type) {
  // Build DeviceLocalAccountInfo for the existing users.
  std::vector<UserManager::DeviceLocalAccountInfo> device_local_accounts;
  for (const auto& user : user_manager_->GetPersistedUsers()) {
    if (user->GetAccountId().GetUserEmail() == user_id) {
      LOG(ERROR) << "duplicated account is found: " << user_id;
      return nullptr;
    }
    if (!user->IsDeviceLocalAccount()) {
      continue;
    }
    UserManager::DeviceLocalAccountInfo info(
        user->GetAccountId().GetUserEmail(), user->GetType());
    if (user->GetType() == UserType::kPublicAccount) {
      info.display_name = user->GetDisplayName();
    }
    device_local_accounts.push_back(info);
  }

  // Add the given `user_id`.
  device_local_accounts.emplace_back(std::string(user_id), user_type);
  user_manager_->UpdateDeviceLocalAccountUser(device_local_accounts);
  return user_manager_->FindUserAndModify(AccountId::FromUserEmail(user_id));
}

}  // namespace user_manager