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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
// Copyright 2017 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/win/conflicts/module_database.h"
#include <memory>
#include <optional>
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "chrome/browser/win/conflicts/module_database_observer.h"
#include "chrome/browser/win/conflicts/module_info.h"
#include "chrome/services/util_win/util_win_impl.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr content::ProcessType kProcessType1 = content::PROCESS_TYPE_BROWSER;
constexpr content::ProcessType kProcessType2 = content::PROCESS_TYPE_RENDERER;
constexpr wchar_t kDll1[] = L"dummy.dll";
constexpr wchar_t kDll2[] = L"foo.dll";
constexpr size_t kSize1 = 100 * 4096;
constexpr size_t kSize2 = 20 * 4096;
constexpr uint32_t kTime1 = 0xDEADBEEF;
constexpr uint32_t kTime2 = 0xBAADF00D;
} // namespace
class ModuleDatabaseTest : public testing::Test {
public:
ModuleDatabaseTest(const ModuleDatabaseTest&) = delete;
ModuleDatabaseTest& operator=(const ModuleDatabaseTest&) = delete;
protected:
ModuleDatabaseTest()
: dll1_(kDll1),
dll2_(kDll2),
task_environment_(base::test::TaskEnvironment::MainThreadType::UI,
base::test::TaskEnvironment::TimeSource::MOCK_TIME),
scoped_testing_local_state_(TestingBrowserProcess::GetGlobal()),
module_database_(std::make_unique<ModuleDatabase>()) {
module_database_->module_inspector_.SetUtilWinFactoryCallbackForTesting(
base::BindRepeating(&ModuleDatabaseTest::CreateUtilWinService,
base::Unretained(this)));
module_database_->StartInspection();
}
~ModuleDatabaseTest() override {
module_database_ = nullptr;
// Clear the outstanding delayed tasks that were posted by the
// ModuleDatabase instance.
task_environment_.FastForwardUntilNoTasksRemain();
}
const ModuleDatabase::ModuleMap& modules() {
return module_database_->modules_;
}
ModuleDatabase* module_database() { return module_database_.get(); }
void RunSchedulerUntilIdle() { task_environment_.RunUntilIdle(); }
void FastForwardToIdleTimer() {
task_environment_.FastForwardBy(ModuleDatabase::kIdleTimeout);
task_environment_.RunUntilIdle();
}
const base::FilePath dll1_;
const base::FilePath dll2_;
private:
mojo::Remote<chrome::mojom::UtilWin> CreateUtilWinService() {
mojo::Remote<chrome::mojom::UtilWin> remote;
util_win_impl_.emplace(remote.BindNewPipeAndPassReceiver());
return remote;
}
// Must be before |module_database_|.
content::BrowserTaskEnvironment task_environment_;
ScopedTestingLocalState scoped_testing_local_state_;
std::optional<UtilWinImpl> util_win_impl_;
std::unique_ptr<ModuleDatabase> module_database_;
};
TEST_F(ModuleDatabaseTest, DatabaseIsConsistent) {
EXPECT_EQ(0u, modules().size());
// Load a module.
module_database()->OnModuleLoad(kProcessType1, dll1_, kSize1, kTime1);
EXPECT_EQ(1u, modules().size());
// Ensure that the process and module sets are up to date.
auto m1 = modules().begin();
EXPECT_EQ(dll1_, m1->first.module_path);
EXPECT_EQ(ProcessTypeToBit(content::PROCESS_TYPE_BROWSER),
m1->second.process_types);
// Provide a redundant load message for that module.
module_database()->OnModuleLoad(kProcessType1, dll1_, kSize1, kTime1);
EXPECT_EQ(1u, modules().size());
// Ensure that the process and module sets haven't changed.
EXPECT_EQ(dll1_, m1->first.module_path);
EXPECT_EQ(ProcessTypeToBit(content::PROCESS_TYPE_BROWSER),
m1->second.process_types);
// Load a second module into the process.
module_database()->OnModuleLoad(kProcessType1, dll2_, kSize2, kTime2);
EXPECT_EQ(2u, modules().size());
// Ensure that the process and module sets are up to date.
auto m2 = modules().rbegin();
EXPECT_EQ(dll2_, m2->first.module_path);
EXPECT_EQ(ProcessTypeToBit(content::PROCESS_TYPE_BROWSER),
m2->second.process_types);
// Load the dummy.dll in the second process as well.
module_database()->OnModuleLoad(kProcessType2, dll1_, kSize1, kTime1);
EXPECT_EQ(ProcessTypeToBit(content::PROCESS_TYPE_BROWSER) |
ProcessTypeToBit(content::PROCESS_TYPE_RENDERER),
m1->second.process_types);
}
// A dummy observer that only counts how many notifications it receives.
class DummyObserver : public ModuleDatabaseObserver {
public:
DummyObserver() = default;
DummyObserver(const DummyObserver&) = delete;
DummyObserver& operator=(const DummyObserver&) = delete;
~DummyObserver() override = default;
void OnNewModuleFound(const ModuleInfoKey& module_key,
const ModuleInfoData& module_data) override {
new_module_count_++;
}
void OnKnownModuleLoaded(const ModuleInfoKey& module_key,
const ModuleInfoData& module_data) override {
known_module_loaded_count_++;
}
void OnModuleDatabaseIdle() override {
on_module_database_idle_called_ = true;
}
int new_module_count() { return new_module_count_; }
int known_module_loaded_count() { return known_module_loaded_count_; }
bool on_module_database_idle_called() {
return on_module_database_idle_called_;
}
private:
int new_module_count_ = 0;
int known_module_loaded_count_ = 0;
bool on_module_database_idle_called_ = false;
};
TEST_F(ModuleDatabaseTest, Observers) {
// Assume there is no shell extensions or IMEs.
module_database()->OnShellExtensionEnumerationFinished();
module_database()->OnImeEnumerationFinished();
DummyObserver before_load_observer;
EXPECT_EQ(0, before_load_observer.new_module_count());
module_database()->AddObserver(&before_load_observer);
EXPECT_EQ(0, before_load_observer.new_module_count());
module_database()->OnModuleLoad(kProcessType1, dll1_, kSize1, kTime1);
RunSchedulerUntilIdle();
EXPECT_EQ(1, before_load_observer.new_module_count());
module_database()->RemoveObserver(&before_load_observer);
// New observers get notified for past loaded modules.
DummyObserver after_load_observer;
EXPECT_EQ(0, after_load_observer.new_module_count());
module_database()->AddObserver(&after_load_observer);
EXPECT_EQ(1, after_load_observer.new_module_count());
module_database()->RemoveObserver(&after_load_observer);
}
TEST_F(ModuleDatabaseTest, OnKnownModuleLoaded) {
DummyObserver dummy_observer;
module_database()->AddObserver(&dummy_observer);
EXPECT_EQ(0, dummy_observer.new_module_count());
EXPECT_EQ(0, dummy_observer.known_module_loaded_count());
// Assume there is one shell extension.
module_database()->OnShellExtensionEnumerated(dll1_, kSize1, kTime1);
module_database()->OnShellExtensionEnumerationFinished();
module_database()->OnImeEnumerationFinished();
RunSchedulerUntilIdle();
EXPECT_EQ(1, dummy_observer.new_module_count());
EXPECT_EQ(0, dummy_observer.known_module_loaded_count());
// Pretend the shell extension loads.
module_database()->OnModuleLoad(kProcessType1, dll1_, kSize1, kTime1);
RunSchedulerUntilIdle();
EXPECT_EQ(1, dummy_observer.new_module_count());
EXPECT_EQ(1, dummy_observer.known_module_loaded_count());
module_database()->RemoveObserver(&dummy_observer);
}
// Tests the idle cycle of the ModuleDatabase.
TEST_F(ModuleDatabaseTest, IsIdle) {
// Assume there is no shell extensions or IMEs.
module_database()->OnShellExtensionEnumerationFinished();
module_database()->OnImeEnumerationFinished();
// ModuleDatabase starts busy.
EXPECT_FALSE(module_database()->IsIdle());
// Can't fast forward to idle because a module load event is needed.
FastForwardToIdleTimer();
EXPECT_FALSE(module_database()->IsIdle());
// A load module event starts the timer.
module_database()->OnModuleLoad(kProcessType1, dll1_, kSize1, kTime1);
EXPECT_FALSE(module_database()->IsIdle());
FastForwardToIdleTimer();
EXPECT_TRUE(module_database()->IsIdle());
// A new shell extension resets the timer.
module_database()->OnShellExtensionEnumerated(dll1_, kSize1, kTime1);
EXPECT_FALSE(module_database()->IsIdle());
FastForwardToIdleTimer();
EXPECT_TRUE(module_database()->IsIdle());
// Adding an observer while idle immediately calls OnModuleDatabaseIdle().
DummyObserver is_idle_observer;
module_database()->AddObserver(&is_idle_observer);
EXPECT_TRUE(is_idle_observer.on_module_database_idle_called());
module_database()->RemoveObserver(&is_idle_observer);
// Make the ModuleDabatase busy.
module_database()->OnModuleLoad(kProcessType2, dll2_, kSize2, kTime2);
EXPECT_FALSE(module_database()->IsIdle());
// Adding an observer while busy doesn't.
DummyObserver is_busy_observer;
module_database()->AddObserver(&is_busy_observer);
EXPECT_FALSE(is_busy_observer.on_module_database_idle_called());
// Fast forward will call OnModuleDatabaseIdle().
FastForwardToIdleTimer();
EXPECT_TRUE(module_database()->IsIdle());
EXPECT_TRUE(is_busy_observer.on_module_database_idle_called());
module_database()->RemoveObserver(&is_busy_observer);
}
// The ModuleDatabase waits until shell extensions and IMEs are enumerated
// before notifying observers or going idle.
TEST_F(ModuleDatabaseTest, WaitUntilRegisteredModulesEnumerated) {
// This observer is added before the first loaded module.
DummyObserver before_load_observer;
module_database()->AddObserver(&before_load_observer);
EXPECT_EQ(0, before_load_observer.new_module_count());
module_database()->OnModuleLoad(kProcessType1, dll1_, kSize1, kTime1);
FastForwardToIdleTimer();
// Idle state is prevented.
EXPECT_FALSE(module_database()->IsIdle());
EXPECT_EQ(0, before_load_observer.new_module_count());
EXPECT_FALSE(before_load_observer.on_module_database_idle_called());
// This observer is added after the first loaded module.
DummyObserver after_load_observer;
module_database()->AddObserver(&after_load_observer);
EXPECT_EQ(0, after_load_observer.new_module_count());
EXPECT_FALSE(after_load_observer.on_module_database_idle_called());
// Simulate the enumerations ending.
module_database()->OnImeEnumerationFinished();
module_database()->OnShellExtensionEnumerationFinished();
EXPECT_EQ(1, before_load_observer.new_module_count());
EXPECT_TRUE(before_load_observer.on_module_database_idle_called());
EXPECT_EQ(1, after_load_observer.new_module_count());
EXPECT_TRUE(after_load_observer.on_module_database_idle_called());
module_database()->RemoveObserver(&after_load_observer);
module_database()->RemoveObserver(&before_load_observer);
}
|