File: profile_id_service_factory_unittest.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 (237 lines) | stat: -rw-r--r-- 9,021 bytes parent folder | download | duplicates (5)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2022 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/enterprise/identifiers/profile_id_service_factory.h"

#include "base/base64url.h"
#include "base/hash/sha1.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "chrome/browser/enterprise/identifiers/profile_id_delegate_impl.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_manager_observer.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/enterprise/browser/identifiers/identifiers_prefs.h"
#include "components/enterprise/browser/identifiers/profile_id_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/components/system/fake_statistics_provider.h"
#endif

#if BUILDFLAG(IS_WIN)
#include "base/win/wmi.h"
#endif

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
    BUILDFLAG(IS_ANDROID)
#include "components/enterprise/browser/controller/fake_browser_dm_token_storage.h"
#else
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "components/policy/proto/device_management_backend.pb.h"
#endif

namespace enterprise {

namespace {

constexpr char kFakeDeviceID[] = "fake-id";

}  // namespace

class ProfileIdServiceFactoryTest : public testing::Test,
                                    public ProfileManagerObserver {
 public:
  ProfileIdServiceFactoryTest()
      : profile_manager_(TestingBrowserProcess::GetGlobal()) {
#if !BUILDFLAG(IS_CHROMEOS)
    policy::BrowserDMTokenStorage::SetForTesting(&storage_);
    storage_.SetClientId(kFakeDeviceID);
#else
    auto policy_data = std::make_unique<enterprise_management::PolicyData>();
    policy_data->set_machine_name(kFakeDeviceID);
    store_.set_policy_data_for_testing(std::move(policy_data));
    fake_statistics_provider_.SetMachineStatistic(ash::system::kSerialNumberKey,
                                                  kFakeDeviceID);
#endif  // !BUILDFLAG(IS_CHROMEOS)

    EXPECT_TRUE(profile_manager_.SetUp());
    profile_ = profile_manager_.CreateTestingProfile("test-user");

    service_ = ProfileIdServiceFactory::GetForProfile(profile_);
    EXPECT_TRUE(service_);

    profile_manager_observer_.Observe(profile_manager_.profile_manager());
  }

  Profile* CreateProfile(const std::string& profile_name) {
    return profile_manager_.CreateTestingProfile(profile_name);
  }

 protected:
  std::string GetTestProfileId(const Profile* profile) {
    std::string encoded_string;
    std::string device_id = kFakeDeviceID;
#if BUILDFLAG(IS_WIN)
    device_id += base::WideToUTF8(
        base::win::WmiComputerSystemInfo::Get().serial_number());
#endif  // (BUILDFLAG(IS_WIN)
    base::Base64UrlEncode(
        base::SHA1HashString(profile->GetPrefs()->GetString(kProfileGUIDPref) +
                             device_id),
        base::Base64UrlEncodePolicy::OMIT_PADDING, &encoded_string);
    return encoded_string;
  }

  void SetProfileIdService(Profile* profile) {
    service_ = ProfileIdServiceFactory::GetForProfile(profile);
  }

  void OnProfileCreationStarted(Profile* profile) override {
    if (!preset_guid_.empty()) {
      enterprise::PresetProfileManagementData::Get(profile)->SetGuid(
          preset_guid_);
    }
  }

// TODO(b/341267441): Enable this test for chrome os ash when
// `OnProfileCreationStarted` is fixed for `FakeProfileManager`.
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
  Profile* CreateNewProfileWithPresetGuid(std::string preset_guid) {
    Profile* new_profile = nullptr;
    // Making sure no two profiles have duplicate names/paths.
    std::string new_profile_name =
        "Profile " + base::Uuid::GenerateRandomV4().AsLowercaseString();
    preset_guid_ = preset_guid;

    base::RunLoop run_loop;

    ProfileManager::CreateMultiProfileAsync(
        base::UTF8ToUTF16(new_profile_name), /*icon_index=*/0,
        /*is_hidden=*/false,
        base::BindLambdaForTesting([&new_profile, &run_loop](Profile* profile) {
          ASSERT_TRUE(profile);
          new_profile = profile;
          run_loop.Quit();
        }));

    run_loop.Run();
    return new_profile;
  }
#endif  // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)

  content::BrowserTaskEnvironment task_environment_;
  TestingProfileManager profile_manager_;
  raw_ptr<TestingProfile> profile_;
  raw_ptr<ProfileIdService> service_;
  std::string preset_guid_;
  base::ScopedObservation<ProfileManager, ProfileManagerObserver>
      profile_manager_observer_{this};

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
    BUILDFLAG(IS_ANDROID)
  policy::FakeBrowserDMTokenStorage storage_;
#else
  policy::MockCloudPolicyStore store_;
#if BUILDFLAG(IS_CHROMEOS)
  ash::system::ScopedFakeStatisticsProvider fake_statistics_provider_;
#endif
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) ||
        // BUILDFLAG(IS_ANDROID)
};

// Tests multiple calls to get the profile identifier for the same profile has
// the same profile identifiers each time.
TEST_F(ProfileIdServiceFactoryTest, GetProfileId_MultipleCalls) {
  auto profile_id1 = service_->GetProfileId();
  EXPECT_EQ(GetTestProfileId(profile_), profile_id1.value());
  auto profile_id2 = service_->GetProfileId();
  EXPECT_EQ(profile_id2.value(), profile_id1.value());
  auto profile_id3 = service_->GetProfileId();
  EXPECT_EQ(profile_id3.value(), profile_id2.value());
}

// Tests that multiple profiles have different profile identifiers.
TEST_F(ProfileIdServiceFactoryTest, GetProfileId_MultipleProfiles) {
  // The original profile is the profile set in BaseTest.
  auto profile_id_1 = service_->GetProfileId();
  EXPECT_EQ(GetTestProfileId(profile_), profile_id_1.value());
  auto* profile_2 = CreateProfile("profile-2");
  SetProfileIdService(profile_2);
  auto profile_id_2 = service_->GetProfileId();
  EXPECT_EQ(GetTestProfileId(profile_2), profile_id_2.value());
  EXPECT_FALSE(profile_id_1.value() == profile_id_2.value());
}

// Tests that no profile identifier is created and no profile GUID is
// persisted(in the case that a profile guid did not previously exist) in guest
// profile sessions.
TEST_F(ProfileIdServiceFactoryTest, GetProfileId_Guest_Profile) {
  profile_->GetPrefs()->SetString(kProfileGUIDPref, "");
  profile_->SetGuestSession(true);
  SetProfileIdService(profile_);
  std::string profile_guid = profile_->GetPrefs()->GetString(kProfileGUIDPref);
  EXPECT_TRUE(profile_guid.empty());
}

// Tests that no service is created in OTR profiles since the factory has a
// profile preference for only creating the service for regular profiles.
TEST_F(ProfileIdServiceFactoryTest, GetProfileId_Incognito_Profile) {
  auto* otr_profile = profile_->GetOffTheRecordProfile(
      Profile::OTRProfileID::CreateUniqueForTesting(),
      /*create_if_needed=*/true);
  SetProfileIdService(otr_profile);
  EXPECT_FALSE(service_);
}

#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
TEST_F(ProfileIdServiceFactoryTest, GetProfileIdWithPresetGuid) {
  std::string random_guid = base::Uuid::GenerateRandomV4().AsLowercaseString();
  std::string device_id = kFakeDeviceID;
#if BUILDFLAG(IS_WIN)
  device_id +=
      base::WideToUTF8(base::win::WmiComputerSystemInfo::Get().serial_number());
#endif  // (BUILDFLAG(IS_WIN)
  std::string expected_profile_id =
      service_->GetProfileIdWithGuidAndDeviceId(random_guid, device_id).value();

  auto* new_profile = CreateNewProfileWithPresetGuid(random_guid);
  SetProfileIdService(new_profile);

  auto new_profile_id = service_->GetProfileId();
  EXPECT_EQ(new_profile_id, expected_profile_id);
}

TEST_F(ProfileIdServiceFactoryTest, PresetGuidIdUniqueness) {
  auto old_profile_id = service_->GetProfileId();
  std::string random_guid = base::Uuid::GenerateRandomV4().AsLowercaseString();

  auto* new_profile = CreateNewProfileWithPresetGuid(random_guid);
  SetProfileIdService(new_profile);

  EXPECT_NE(service_->GetProfileId(), old_profile_id);
}

TEST_F(ProfileIdServiceFactoryTest, PresetGuidDataIsOneOff) {
  std::string random_guid = base::Uuid::GenerateRandomV4().AsLowercaseString();

  auto* preset_guid_profile = CreateNewProfileWithPresetGuid(random_guid);
  SetProfileIdService(preset_guid_profile);
  auto preset_guid_profile_id = service_->GetProfileId();

  auto* no_preset_guid_profile = CreateNewProfileWithPresetGuid(std::string());
  SetProfileIdService(no_preset_guid_profile);

  EXPECT_NE(service_->GetProfileId(), preset_guid_profile_id);
}
#endif  // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)

}  // namespace enterprise