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
|
// Copyright 2023 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/metrics/structured/key_data_provider_ash.h"
#include <memory>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/time/time.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/metrics/structured/lib/key_data_provider.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace metrics::structured {
namespace {
using testing::Eq;
using testing::IsNull;
using testing::NotNull;
constexpr char kProfileProjectName[] = "TestProjectOne";
constexpr char kDeviceProjectName[] = "TestProjectFour";
constexpr char kCrOSEventsProjectName[] = "CrOSEvents";
class KeyDataProviderAshTest : public testing::Test, KeyDataProvider::Observer {
protected:
KeyDataProviderAshTest()
: profile_manager_(TestingBrowserProcess::GetGlobal()) {}
KeyDataProviderAshTest(const KeyDataProviderAshTest&) = delete;
KeyDataProviderAshTest& operator=(const KeyDataProviderAshTest&) = delete;
void SetUp() override {
CHECK(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(profile_manager_.SetUp());
run_loop_ = std::make_unique<base::RunLoop>();
key_data_provider_ = std::make_unique<KeyDataProviderAsh>(
DeviceKeyFilePath(), /*write_delay=*/base::Milliseconds(0));
key_data_provider_->AddObserver(this);
Wait();
run_loop_->Run();
}
void TearDown() override { key_data_provider_->RemoveObserver(this); }
void OnKeyReady() override { run_loop_->Quit(); }
void Wait() { task_environment_.RunUntilIdle(); }
void SetUpProfileKeys() {
run_loop_ = std::make_unique<base::RunLoop>();
profile_manager_.CreateTestingProfile("p1");
Wait();
run_loop_->Run();
}
KeyData* GetCurrentKeyData(const std::string& project_name) {
return key_data_provider_->GetKeyData(project_name);
}
KeyData* GetDeviceKeyData() {
return key_data_provider_->GetKeyData(kDeviceProjectName);
}
KeyData* GetProfileKeyData() {
return key_data_provider_->GetKeyData(kProfileProjectName);
}
base::FilePath DeviceKeyFilePath() {
return temp_dir_.GetPath()
.Append("structured_metrics")
.Append("device_keys");
}
base::FilePath ProfileKeyFilePath() {
return GetProfilePath().Append("structured_metrics").Append("profile_keys");
}
base::FilePath GetProfilePath() { return profile_manager_.profiles_dir(); }
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfileManager profile_manager_;
base::ScopedTempDir temp_dir_;
// Used to wait for keys.
std::unique_ptr<base::RunLoop> run_loop_;
protected:
std::unique_ptr<KeyDataProviderAsh> key_data_provider_;
};
} // namespace
TEST_F(KeyDataProviderAshTest, UseDeviceKeyForDeviceProject) {
auto* key_data = GetCurrentKeyData(kDeviceProjectName);
EXPECT_NE(key_data, nullptr);
// Ensure that the pointers are not the same.
EXPECT_NE(key_data, GetProfileKeyData());
EXPECT_EQ(key_data, GetDeviceKeyData());
}
TEST_F(KeyDataProviderAshTest, UseProfileKeyForProfileProject) {
SetUpProfileKeys();
auto* key_data = GetCurrentKeyData(kProfileProjectName);
EXPECT_NE(key_data, nullptr);
// Ensure that the pointers are not the same.
EXPECT_EQ(key_data, GetProfileKeyData());
EXPECT_NE(key_data, GetDeviceKeyData());
}
TEST_F(KeyDataProviderAshTest, ReturnNullIfProfileProjectBeforeProfileKey) {
auto* key_data = GetCurrentKeyData(kProfileProjectName);
EXPECT_EQ(key_data, nullptr);
}
TEST_F(KeyDataProviderAshTest, ReturnProfileKeyForCrOSEvent) {
SetUpProfileKeys();
auto* key_data = GetCurrentKeyData(kCrOSEventsProjectName);
EXPECT_NE(key_data, nullptr);
EXPECT_EQ(key_data, GetProfileKeyData());
}
TEST_F(KeyDataProviderAshTest, ReturnsAppropriateSequenceIds) {
SetUpProfileKeys();
auto* key_data = GetCurrentKeyData(kCrOSEventsProjectName);
EXPECT_NE(key_data, nullptr);
EXPECT_EQ(key_data, GetProfileKeyData());
}
TEST_F(KeyDataProviderAshTest, SequenceEvents_ReturnsDifferentSequenceIds) {
SetUpProfileKeys();
std::optional<uint64_t> device_id =
key_data_provider_->GetSecondaryId(kCrOSEventsProjectName);
std::optional<uint64_t> profile_id =
key_data_provider_->GetId(kCrOSEventsProjectName);
EXPECT_TRUE(device_id.has_value());
EXPECT_TRUE(profile_id.has_value());
// Ids generated should be different.
EXPECT_NE(device_id.value(), profile_id.value());
}
TEST_F(KeyDataProviderAshTest, SequenceEvents_PrimaryIdEmptyOnNoProfileSetup) {
std::optional<uint64_t> device_id =
key_data_provider_->GetSecondaryId(kCrOSEventsProjectName);
std::optional<uint64_t> profile_id =
key_data_provider_->GetId(kCrOSEventsProjectName);
EXPECT_TRUE(device_id.has_value());
EXPECT_FALSE(profile_id.has_value());
}
} // namespace metrics::structured
|