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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
// Copyright 2014 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/extensions/updater/local_extension_cache.h"
#include <stddef.h>
#include <stdint.h>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/values.h"
#include "base/version.h"
#include "chrome/common/extensions/extension_constants.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "crypto/hash.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kTestExtensionId1[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const char kTestExtensionId2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
const char kTestExtensionId3[] = "cccccccccccccccccccccccccccccccc";
} // namespace
namespace extensions {
class LocalExtensionCacheTest : public testing::Test {
public:
LocalExtensionCacheTest() = default;
LocalExtensionCacheTest(const LocalExtensionCacheTest&) = delete;
LocalExtensionCacheTest& operator=(const LocalExtensionCacheTest&) = delete;
~LocalExtensionCacheTest() override = default;
base::FilePath CreateCacheDir() {
EXPECT_TRUE(cache_dir_.CreateUniqueTempDir());
return cache_dir_.GetPath();
}
base::FilePath CreateTempDir() {
EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
return temp_dir_.GetPath();
}
void CreateExtensionFile(const base::FilePath& dir,
const std::string& id,
const std::string& version,
size_t size,
const base::Time& timestamp,
base::FilePath* filename) {
const base::FilePath file = GetExtensionFileName(dir, id, version, "");
if (filename) {
*filename = file;
}
CreateFile(file, size, timestamp);
}
void CreateFile(const base::FilePath& file,
size_t size,
const base::Time& timestamp) {
std::string data(size, 0);
EXPECT_TRUE(base::WriteFile(file, data));
EXPECT_TRUE(base::TouchFile(file, timestamp, timestamp));
}
std::string CreateSignedExtensionFile(const base::FilePath& dir,
const std::string& id,
const std::string& version,
size_t size,
const base::Time& timestamp,
base::FilePath* filename) {
std::string data(size, 0);
const std::string hex_hash = base::ToLowerASCII(
base::HexEncode(crypto::hash::Sha256(base::as_byte_span(data))));
const base::FilePath file =
GetExtensionFileName(dir, id, version, hex_hash);
if (filename) {
*filename = file;
}
EXPECT_TRUE(base::WriteFile(file, data));
EXPECT_TRUE(base::TouchFile(file, timestamp, timestamp));
return hex_hash;
}
base::FilePath GetExtensionFileName(const base::FilePath& dir,
const std::string& id,
const std::string& version,
const std::string& hash) {
return dir.Append(
extensions::LocalExtensionCache::ExtensionFileName(id, version, hash));
}
base::FilePath GetInvalidCacheFilePath() {
return cache_dir_.GetPath().AppendASCII(
LocalExtensionCache::kInvalidCacheIdsFileName);
}
private:
content::BrowserTaskEnvironment task_environment_;
base::ScopedTempDir cache_dir_;
base::ScopedTempDir temp_dir_;
};
static void SimpleCallback(bool* ptr) {
*ptr = true;
}
TEST_F(LocalExtensionCacheTest, Basic) {
base::FilePath cache_dir(CreateCacheDir());
LocalExtensionCache cache(
cache_dir, 1000, base::Days(30),
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}));
cache.SetCacheStatusPollingDelayForTests(base::TimeDelta());
bool initialized = false;
cache.Init(true, base::BindOnce(&SimpleCallback, &initialized));
base::FilePath file10, file01, file20, file30;
CreateExtensionFile(cache_dir, kTestExtensionId1, "1.0", 100,
base::Time::Now() - base::Days(1), &file10);
CreateExtensionFile(cache_dir, kTestExtensionId1, "0.1", 100,
base::Time::Now() - base::Days(10), &file01);
CreateExtensionFile(cache_dir, kTestExtensionId2, "2.0", 100,
base::Time::Now() - base::Days(40), &file20);
CreateExtensionFile(cache_dir, kTestExtensionId3, "3.0", 900,
base::Time::Now() - base::Days(41), &file30);
content::RunAllTasksUntilIdle();
ASSERT_TRUE(initialized);
// Older version should be removed on cache initialization.
EXPECT_FALSE(base::PathExists(file01));
// All extensions should be there because cleanup happens on shutdown to
// support use case when device was not used to more than 30 days and cache
// shouldn't be cleaned before someone will have a chance to use it.
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, "", nullptr, nullptr));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId2, "", nullptr, nullptr));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId3, "", nullptr, nullptr));
bool did_shutdown = false;
cache.Shutdown(base::BindOnce(&SimpleCallback, &did_shutdown));
content::RunAllTasksUntilIdle();
ASSERT_TRUE(did_shutdown);
EXPECT_TRUE(base::PathExists(file10));
EXPECT_FALSE(base::PathExists(file20));
EXPECT_FALSE(base::PathExists(file30));
}
TEST_F(LocalExtensionCacheTest, KeepHashed) {
base::FilePath cache_dir(CreateCacheDir());
LocalExtensionCache cache(
cache_dir, 1000, base::Days(30),
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}));
cache.SetCacheStatusPollingDelayForTests(base::TimeDelta());
bool initialized = false;
cache.Init(true, base::BindOnce(&SimpleCallback, &initialized));
// Add three identical extensions with different hash sums.
const base::Time time = base::Time::Now() - base::Days(1);
base::FilePath file, file1, file2;
CreateExtensionFile(cache_dir, kTestExtensionId1, "1.0", 100, time, &file);
const std::string hash1 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "1.0", 100, time, &file1);
const std::string hash2 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "1.0", 123, time, &file2);
content::RunAllTasksUntilIdle();
ASSERT_TRUE(initialized);
// Unhashed version should be removed on cache initialization.
EXPECT_FALSE(base::PathExists(file));
// Both hashed versions should stay
EXPECT_TRUE(base::PathExists(file1));
EXPECT_TRUE(base::PathExists(file2));
// We should be able to lookup all three extension queries
std::string version;
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, "", nullptr, &version));
EXPECT_EQ(version, "1.0");
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash1, nullptr, nullptr));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash2, nullptr, nullptr));
}
TEST_F(LocalExtensionCacheTest, KeepLatest) {
base::FilePath cache_dir(CreateCacheDir());
LocalExtensionCache cache(
cache_dir, 1000, base::Days(30),
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}));
cache.SetCacheStatusPollingDelayForTests(base::TimeDelta());
bool initialized = false;
cache.Init(true, base::BindOnce(&SimpleCallback, &initialized));
// All extension files are hashed, but have different versions.
const base::Time time = base::Time::Now() - base::Days(1);
base::FilePath file1, file21, file22;
const std::string hash1 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "1.0", 100, time, &file1);
const std::string hash21 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "2.0", 101, time, &file21);
const std::string hash22 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "2.0", 123, time, &file22);
content::RunAllTasksUntilIdle();
ASSERT_TRUE(initialized);
// Older version should be removed.
EXPECT_FALSE(base::PathExists(file1));
// Both newer hashed versions should stay.
EXPECT_TRUE(base::PathExists(file21));
EXPECT_TRUE(base::PathExists(file22));
// We should be able to lookup only the latest version queries.
EXPECT_FALSE(cache.GetExtension(kTestExtensionId1, hash1, nullptr, nullptr));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash21, nullptr, nullptr));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash22, nullptr, nullptr));
}
TEST_F(LocalExtensionCacheTest, Complex) {
base::FilePath cache_dir(CreateCacheDir());
LocalExtensionCache cache(
cache_dir, 1000, base::Days(30),
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}));
cache.SetCacheStatusPollingDelayForTests(base::TimeDelta());
bool initialized = false;
cache.Init(true, base::BindOnce(&SimpleCallback, &initialized));
// Like in KeepHashed test, but with two different versions.
const base::Time time = base::Time::Now() - base::Days(1);
base::FilePath file1, file11, file12, file2, file21, file22;
CreateExtensionFile(cache_dir, kTestExtensionId1, "1.0", 100, time, &file1);
const std::string hash11 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "1.0", 101, time, &file11);
const std::string hash12 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "1.0", 102, time, &file12);
CreateExtensionFile(cache_dir, kTestExtensionId1, "2.0", 103, time, &file2);
const std::string hash21 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "2.0", 104, time, &file21);
const std::string hash22 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "2.0", 105, time, &file22);
content::RunAllTasksUntilIdle();
ASSERT_TRUE(initialized);
// Older and unhashed versions should be removed.
EXPECT_FALSE(base::PathExists(file1));
EXPECT_FALSE(base::PathExists(file11));
EXPECT_FALSE(base::PathExists(file12));
EXPECT_FALSE(base::PathExists(file2));
// Newest hashed versions should stay.
EXPECT_TRUE(base::PathExists(file21));
EXPECT_TRUE(base::PathExists(file22));
// We should be able to lookup only the latest version queries, both with and
// without hash.
std::string version;
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, "", nullptr, &version));
EXPECT_EQ(version, "2.0");
EXPECT_FALSE(cache.GetExtension(kTestExtensionId1, hash11, nullptr, nullptr));
EXPECT_FALSE(cache.GetExtension(kTestExtensionId1, hash12, nullptr, nullptr));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash21, nullptr, nullptr));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash22, nullptr, nullptr));
}
static void PutExtensionAndWait(LocalExtensionCache* cache,
const std::string& id,
const std::string& expected_hash,
const base::FilePath& path,
const std::string& version) {
base::RunLoop run_loop;
cache->PutExtension(
id, expected_hash, path, base::Version(version),
base::BindRepeating([](base::RunLoop* run_loop, const base::FilePath&,
bool) { run_loop->Quit(); },
&run_loop));
run_loop.Run();
}
TEST_F(LocalExtensionCacheTest, PutExtensionCases) {
base::FilePath cache_dir(CreateCacheDir());
LocalExtensionCache cache(
cache_dir, 1000, base::Days(30),
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}));
cache.SetCacheStatusPollingDelayForTests(base::TimeDelta());
bool initialized = false;
cache.Init(true, base::BindOnce(&SimpleCallback, &initialized));
// Initialize cache with several different files
const base::Time time = base::Time::Now() - base::Days(1);
base::FilePath file11, file12, file2, file3;
const std::string hash11 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "1.0", 101, time, &file11);
const std::string hash12 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "1.0", 102, time, &file12);
CreateSignedExtensionFile(cache_dir, kTestExtensionId2, "0.2", 200, time,
&file2);
CreateExtensionFile(cache_dir, kTestExtensionId3, "0.3", 300, time, &file3);
content::RunAllTasksUntilIdle();
ASSERT_TRUE(initialized);
// Create and initialize installation source directory.
base::ScopedTempDir temp_dir;
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
const base::FilePath temp_path = temp_dir.GetPath();
std::string version;
// Right now we have two files for the first extension.
EXPECT_TRUE(base::PathExists(file11));
EXPECT_TRUE(base::PathExists(file12));
EXPECT_TRUE(base::PathExists(file2));
EXPECT_TRUE(base::PathExists(file3));
// 1. Cache contains an older version.
base::FilePath temp1;
CreateExtensionFile(temp_path, kTestExtensionId1, "3.0", 110, time, &temp1);
PutExtensionAndWait(&cache, kTestExtensionId1, "", temp1, "3.0");
// New file added.
const base::FilePath unhashed =
GetExtensionFileName(cache_dir, kTestExtensionId1, "3.0", "");
EXPECT_TRUE(base::PathExists(unhashed));
// Old files removed from cache (kept in the directory though).
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash11, nullptr, &version));
EXPECT_EQ(version, "3.0");
EXPECT_TRUE(base::DeleteFile(temp1));
// 2. Cache contains a newer version.
base::FilePath temp2;
CreateExtensionFile(temp_path, kTestExtensionId1, "2.0", 120, time, &temp2);
PutExtensionAndWait(&cache, kTestExtensionId1, "", temp2, "2.0");
// New file skipped.
EXPECT_FALSE(base::PathExists(
GetExtensionFileName(cache_dir, kTestExtensionId1, "2.0", "")));
// Old file kept.
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, "", nullptr, &version));
EXPECT_EQ(version, "3.0");
EXPECT_TRUE(base::DeleteFile(temp2));
// 3. Cache contains the same version without hash, our file is unhashed.
base::FilePath temp3;
CreateExtensionFile(temp_path, kTestExtensionId1, "3.0", 130, time, &temp3);
PutExtensionAndWait(&cache, kTestExtensionId1, "", temp3, "3.0");
// New file skipped, old file kept
EXPECT_EQ(base::File(unhashed, base::File::FLAG_READ | base::File::FLAG_OPEN)
.GetLength(),
110);
EXPECT_TRUE(base::DeleteFile(temp3));
// 4. Cache contains the same version without hash, our file is hashed.
base::FilePath temp4;
const std::string hash3 = CreateSignedExtensionFile(
temp_path, kTestExtensionId1, "3.0", 140, time, &temp4);
PutExtensionAndWait(&cache, kTestExtensionId1, hash3, temp4, "3.0");
// New file added.
const base::FilePath hashed =
GetExtensionFileName(cache_dir, kTestExtensionId1, "3.0", hash3);
EXPECT_TRUE(base::PathExists(hashed));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash3, nullptr, nullptr));
// Old file removed (queries return hashed version)
base::FilePath unhashed_path;
EXPECT_TRUE(
cache.GetExtension(kTestExtensionId1, "", &unhashed_path, nullptr));
EXPECT_EQ(unhashed_path, hashed);
EXPECT_TRUE(base::DeleteFile(temp4));
EXPECT_TRUE(base::DeleteFile(unhashed));
// 5. Cache contains the same version with hash, our file is unhashed.
base::FilePath temp5;
CreateExtensionFile(temp_path, kTestExtensionId1, "3.0", 150, time, &temp5);
PutExtensionAndWait(&cache, kTestExtensionId1, "", temp5, "3.0");
// New file skipped.
EXPECT_FALSE(base::PathExists(unhashed));
// Old file kept.
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash3, nullptr, nullptr));
EXPECT_TRUE(base::DeleteFile(temp5));
// 6. Cache contains the same version with hash, our file has the "same" hash.
base::FilePath temp6;
CreateExtensionFile(temp_path, kTestExtensionId1, "3.0", 160, time, &temp6);
PutExtensionAndWait(&cache, kTestExtensionId1, hash3, temp6, "3.0");
// New file skipped, old file kept
EXPECT_EQ(base::File(hashed, base::File::FLAG_READ | base::File::FLAG_OPEN)
.GetLength(),
140);
EXPECT_TRUE(base::DeleteFile(temp6));
// 7. Cache contains the same version with hash, our file is different.
base::FilePath temp7;
const std::string hash4 = CreateSignedExtensionFile(
temp_path, kTestExtensionId1, "3.0", 170, time, &temp7);
PutExtensionAndWait(&cache, kTestExtensionId1, hash4, temp7, "3.0");
// New file added.
const base::FilePath hashed2 =
GetExtensionFileName(cache_dir, kTestExtensionId1, "3.0", hash4);
EXPECT_TRUE(base::PathExists(hashed2));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash4, nullptr, nullptr));
// Old file kept.
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash3, nullptr, nullptr));
EXPECT_TRUE(base::DeleteFile(temp7));
}
// This test checks that scheduling extension cache removal with
// `RemoveOnNextInit` works correctly: extension cache is deleted for the
// specified extension right on the next initialization, another extension is
// not affected.
TEST_F(LocalExtensionCacheTest, InvalidExtensionRemoval) {
base::FilePath cache_dir(CreateCacheDir());
LocalExtensionCache cache(
cache_dir, /*max_cache_size=*/1000, /*max_cache_age=*/base::Days(30),
/*backend_task_runner=*/
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}));
cache.SetCacheStatusPollingDelayForTests(base::TimeDelta());
bool initialized = false;
cache.Init(true, base::BindOnce(&SimpleCallback, &initialized));
const base::Time time = base::Time::Now() - base::Days(1);
base::FilePath file_1, file_2_1, file_2_2;
const std::string hash_1 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId1, "1.0", 100, time, &file_1);
const std::string hash_2_1 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId2, "2.0", 101, time, &file_2_1);
const std::string hash_2_2 = CreateSignedExtensionFile(
cache_dir, kTestExtensionId2, "2.0", 123, time, &file_2_2);
content::RunAllTasksUntilIdle();
ASSERT_TRUE(initialized);
EXPECT_TRUE(base::PathExists(file_1));
EXPECT_TRUE(base::PathExists(file_2_1));
EXPECT_TRUE(base::PathExists(file_2_2));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash_1, nullptr, nullptr));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId2, "", nullptr, nullptr));
// Invalid cache file should be removed on initializion.
EXPECT_FALSE(base::PathExists(GetInvalidCacheFilePath()));
cache.RemoveOnNextInit(kTestExtensionId2);
content::RunAllTasksUntilIdle();
// Extension files should still exist, nothing should be deleted before the
// next initialization.
EXPECT_TRUE(base::PathExists(file_1));
EXPECT_TRUE(base::PathExists(file_2_1));
EXPECT_TRUE(base::PathExists(file_2_2));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId1, hash_1, nullptr, nullptr));
EXPECT_TRUE(cache.GetExtension(kTestExtensionId2, "", nullptr, nullptr));
bool did_shutdown = false;
cache.Shutdown(base::BindOnce(&SimpleCallback, &did_shutdown));
content::RunAllTasksUntilIdle();
ASSERT_TRUE(did_shutdown);
EXPECT_TRUE(base::PathExists(file_1));
EXPECT_TRUE(base::PathExists(file_2_1));
EXPECT_TRUE(base::PathExists(file_2_2));
// Create cache again for the same directory.
LocalExtensionCache new_cache(
cache_dir, 1000, base::Days(30),
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}));
initialized = false;
new_cache.Init(true, base::BindOnce(&SimpleCallback, &initialized));
content::RunAllTasksUntilIdle();
ASSERT_TRUE(initialized);
// Check that second extension's cache was cleaned up after initialization.
EXPECT_TRUE(base::PathExists(file_1));
EXPECT_FALSE(base::PathExists(file_2_1));
EXPECT_FALSE(base::PathExists(file_2_2));
EXPECT_TRUE(
new_cache.GetExtension(kTestExtensionId1, hash_1, nullptr, nullptr));
EXPECT_FALSE(new_cache.GetExtension(kTestExtensionId2, "", nullptr, nullptr));
// Invalid cache file should be removed on initializion.
EXPECT_FALSE(base::PathExists(GetInvalidCacheFilePath()));
did_shutdown = false;
new_cache.Shutdown(base::BindOnce(&SimpleCallback, &did_shutdown));
content::RunAllTasksUntilIdle();
ASSERT_TRUE(did_shutdown);
}
} // namespace extensions
|