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
|
// 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 "services/network/shared_dictionary/shared_dictionary_disk_cache.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/numerics/safe_conversions.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/test/test_file_util.h"
#include "build/build_config.h"
#include "net/base/io_buffer.h"
#include "net/base/test_completion_callback.h"
#include "net/disk_cache/disk_cache_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace network {
namespace {
const std::string kTestKey = "test";
const std::string kTestData = "Hello world";
} // namespace
class SharedDictionaryDiskCacheTest : public testing::Test {
public:
SharedDictionaryDiskCacheTest() = default;
void SetUp() override {
ASSERT_TRUE(tmp_directory_.CreateUniqueTempDir());
directory_path_ =
tmp_directory_.GetPath().Append(FILE_PATH_LITERAL("dictionaries"));
}
void TearDown() override { FlushCacheTasks(); }
protected:
std::unique_ptr<SharedDictionaryDiskCache> CreateDiskCache() {
auto disk_cache = std::make_unique<SharedDictionaryDiskCache>();
disk_cache->Initialize(directory_path_,
#if BUILDFLAG(IS_ANDROID)
disk_cache::ApplicationStatusListenerGetter(),
#endif // BUILDFLAG(IS_ANDROID)
/*file_operations_factory=*/nullptr);
return disk_cache;
}
void FlushCacheTasks() {
disk_cache::FlushCacheThreadForTesting();
task_environment_.RunUntilIdle();
}
disk_cache::EntryResult CreateEntry(SharedDictionaryDiskCache* disk_cache,
const std::string& key) {
TestEntryResultCompletionCallback create_callback;
return create_callback.GetResult(disk_cache->OpenOrCreateEntry(
key, /*create=*/true, create_callback.callback()));
}
disk_cache::EntryResult OpenEntry(SharedDictionaryDiskCache* disk_cache,
const std::string& key) {
TestEntryResultCompletionCallback create_callback;
return create_callback.GetResult(disk_cache->OpenOrCreateEntry(
key, /*create=*/false, create_callback.callback()));
}
int DoomEntry(SharedDictionaryDiskCache* disk_cache, const std::string& key) {
net::TestCompletionCallback doom_entry_callback;
return doom_entry_callback.GetResult(
disk_cache->DoomEntry(key, doom_entry_callback.callback()));
}
int ClearAll(SharedDictionaryDiskCache* disk_cache) {
net::TestCompletionCallback clear_all_callback;
return clear_all_callback.GetResult(
disk_cache->ClearAll(clear_all_callback.callback()));
}
int WriteData(disk_cache::Entry* entry, const std::string& data) {
scoped_refptr<net::StringIOBuffer> write_buffer =
base::MakeRefCounted<net::StringIOBuffer>(data);
net::TestCompletionCallback write_callback;
return write_callback.GetResult(entry->WriteData(
/*index=*/1, /*offset=*/0, write_buffer.get(), write_buffer->size(),
write_callback.callback(), /*truncate=*/false));
}
void CheckEntryDataEquals(disk_cache::Entry* entry,
const std::string& expected_data) {
EXPECT_EQ(base::checked_cast<int32_t>(expected_data.size()),
entry->GetDataSize(/*index=*/1));
scoped_refptr<net::IOBufferWithSize> read_buffer =
base::MakeRefCounted<net::IOBufferWithSize>(expected_data.size());
net::TestCompletionCallback read_callback;
EXPECT_EQ(read_buffer->size(),
read_callback.GetResult(entry->ReadData(
/*index=*/1, /*offset=*/0, read_buffer.get(),
expected_data.size(), read_callback.callback())));
EXPECT_EQ(expected_data,
std::string(reinterpret_cast<const char*>(read_buffer->data()),
read_buffer->size()));
}
void PrepareDiskCacheWithTestData() {
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
// Create an entry.
disk_cache::EntryResult create_result =
CreateEntry(disk_cache.get(), kTestKey);
EXPECT_EQ(net::OK, create_result.net_error());
disk_cache::ScopedEntryPtr created_entry;
created_entry.reset(create_result.ReleaseEntry());
ASSERT_TRUE(created_entry);
// Write to the entry.
EXPECT_EQ(base::checked_cast<int>(kTestData.size()),
WriteData(created_entry.get(), kTestData));
}
void CorruptDiskCache() {
PrepareDiskCacheWithTestData();
// Corrupt the fake index file for the populated simple cache.
const base::FilePath index_file_path =
directory_path_.Append(FILE_PATH_LITERAL("index"));
ASSERT_TRUE(base::WriteFile(index_file_path, "corrupted"));
file_permissions_restorer_ = std::make_unique<base::FilePermissionRestorer>(
tmp_directory_.GetPath());
// Mark the parent directory unwritable. When the disk cache is corrupted,
// the SimpleCache backend will try to recreate the cache directory. By
// making the parent directory unwritable, we can simulate the failure of
// this recreation process.
ASSERT_TRUE(base::MakeFileUnwritable(tmp_directory_.GetPath()));
}
private:
base::test::TaskEnvironment task_environment_;
base::ScopedTempDir tmp_directory_;
base::FilePath directory_path_;
// `file_permissions_restorer_` must be below `tmp_directory_` to restore the
// file permission correctly.
std::unique_ptr<base::FilePermissionRestorer> file_permissions_restorer_;
};
TEST_F(SharedDictionaryDiskCacheTest, CreateEntry) {
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
FlushCacheTasks();
EXPECT_EQ(net::OK, CreateEntry(disk_cache.get(), kTestKey).net_error());
}
TEST_F(SharedDictionaryDiskCacheTest, CreateEntryWhileInitializing) {
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
EXPECT_EQ(net::OK, CreateEntry(disk_cache.get(), kTestKey).net_error());
}
TEST_F(SharedDictionaryDiskCacheTest, OpenNonExistentEntryFailure) {
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
FlushCacheTasks();
EXPECT_EQ(net::ERR_FAILED, OpenEntry(disk_cache.get(), kTestKey).net_error());
}
TEST_F(SharedDictionaryDiskCacheTest,
OpenNonExistentEntryFailureWhileInitializing) {
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
EXPECT_EQ(net::ERR_FAILED, OpenEntry(disk_cache.get(), kTestKey).net_error());
}
TEST_F(SharedDictionaryDiskCacheTest, OpenEntrySuccess) {
{
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
EXPECT_EQ(net::OK, CreateEntry(disk_cache.get(), kTestKey).net_error());
}
{
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
EXPECT_EQ(net::OK, OpenEntry(disk_cache.get(), kTestKey).net_error());
}
}
TEST_F(SharedDictionaryDiskCacheTest, DoomEntry) {
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
FlushCacheTasks();
EXPECT_EQ(net::OK, DoomEntry(disk_cache.get(), kTestKey));
}
TEST_F(SharedDictionaryDiskCacheTest, DoomEntryWhileInitializing) {
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
EXPECT_EQ(net::OK, DoomEntry(disk_cache.get(), kTestKey));
}
TEST_F(SharedDictionaryDiskCacheTest, ClearAll) {
PrepareDiskCacheWithTestData();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
FlushCacheTasks();
EXPECT_EQ(net::OK, ClearAll(disk_cache.get()));
EXPECT_EQ(net::ERR_FAILED, OpenEntry(disk_cache.get(), kTestKey).net_error());
}
TEST_F(SharedDictionaryDiskCacheTest, ClearAllWhileInitializing) {
PrepareDiskCacheWithTestData();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
EXPECT_EQ(net::OK, ClearAll(disk_cache.get()));
EXPECT_EQ(net::ERR_FAILED, OpenEntry(disk_cache.get(), kTestKey).net_error());
}
TEST_F(SharedDictionaryDiskCacheTest, CreateIterator) {
PrepareDiskCacheWithTestData();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
FlushCacheTasks();
std::unique_ptr<disk_cache::Backend::Iterator> iterator;
disk_cache->CreateIterator(base::BindLambdaForTesting(
[&](std::unique_ptr<disk_cache::Backend::Iterator> it) {
iterator = std::move(it);
}));
EXPECT_TRUE(iterator);
}
TEST_F(SharedDictionaryDiskCacheTest, CreateIteratorWhileInitializing) {
PrepareDiskCacheWithTestData();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
std::unique_ptr<disk_cache::Backend::Iterator> iterator;
disk_cache->CreateIterator(base::BindLambdaForTesting(
[&](std::unique_ptr<disk_cache::Backend::Iterator> it) {
iterator = std::move(it);
}));
EXPECT_FALSE(iterator);
FlushCacheTasks();
EXPECT_TRUE(iterator);
}
TEST_F(SharedDictionaryDiskCacheTest, CreateWriteOpenReadDeleteReopen) {
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
// Create an entry.
disk_cache::EntryResult create_result =
CreateEntry(disk_cache.get(), kTestKey);
EXPECT_EQ(net::OK, create_result.net_error());
disk_cache::ScopedEntryPtr created_entry;
created_entry.reset(create_result.ReleaseEntry());
ASSERT_TRUE(created_entry);
// Write to the entry.
EXPECT_EQ(base::checked_cast<int>(kTestData.size()),
WriteData(created_entry.get(), kTestData));
// Close the written entry.
created_entry.reset();
// Open the entry.
disk_cache::EntryResult open_result = OpenEntry(disk_cache.get(), kTestKey);
EXPECT_EQ(net::OK, open_result.net_error());
disk_cache::ScopedEntryPtr opened_entry;
opened_entry.reset(open_result.ReleaseEntry());
ASSERT_TRUE(opened_entry);
// Read the entry.
CheckEntryDataEquals(opened_entry.get(), kTestData);
// Close the opened entry.
opened_entry.reset();
// Doom the entry.
net::TestCompletionCallback doom_callback;
EXPECT_EQ(net::OK, doom_callback.GetResult(disk_cache->DoomEntry(
kTestKey, doom_callback.callback())));
// Reopen the entry.
disk_cache::EntryResult reopen_result = OpenEntry(disk_cache.get(), kTestKey);
EXPECT_EQ(net::ERR_FAILED, reopen_result.net_error());
}
#if !BUILDFLAG(IS_FUCHSIA)
// CorruptDiskCache() doesn't work on Fuchsia. So disabling the following tests
// on Fuchsia.
TEST_F(SharedDictionaryDiskCacheTest, CreateEntryCorruptedFailure) {
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
FlushCacheTasks();
EXPECT_EQ(net::ERR_FAILED,
CreateEntry(disk_cache.get(), kTestKey).net_error());
}
TEST_F(SharedDictionaryDiskCacheTest,
CreateEntryWhileInitializingCorruptedFailure) {
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
EXPECT_EQ(net::ERR_FAILED,
CreateEntry(disk_cache.get(), kTestKey).net_error());
}
TEST_F(SharedDictionaryDiskCacheTest, OpenEntryCorruptedFailure) {
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
FlushCacheTasks();
EXPECT_EQ(net::ERR_FAILED, OpenEntry(disk_cache.get(), kTestKey).net_error());
}
TEST_F(SharedDictionaryDiskCacheTest,
OpenEntryCorruptedFailureWhileInitializing) {
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
EXPECT_EQ(net::ERR_FAILED, OpenEntry(disk_cache.get(), kTestKey).net_error());
}
TEST_F(SharedDictionaryDiskCacheTest, DoomEntryCorruptedFailure) {
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
FlushCacheTasks();
EXPECT_EQ(net::ERR_FAILED, DoomEntry(disk_cache.get(), kTestKey));
}
TEST_F(SharedDictionaryDiskCacheTest,
DoomEntryCorruptedFailureWhileInitializing) {
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
EXPECT_EQ(net::ERR_FAILED, DoomEntry(disk_cache.get(), kTestKey));
}
TEST_F(SharedDictionaryDiskCacheTest, ClearAllCorruptedFailure) {
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
FlushCacheTasks();
EXPECT_EQ(net::ERR_FAILED, ClearAll(disk_cache.get()));
}
TEST_F(SharedDictionaryDiskCacheTest,
ClearAllCorruptedFailureWhileInitializing) {
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
EXPECT_EQ(net::ERR_FAILED, ClearAll(disk_cache.get()));
}
TEST_F(SharedDictionaryDiskCacheTest, CreateIteratorCorruptedFailure) {
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
FlushCacheTasks();
bool callback_called = false;
disk_cache->CreateIterator(base::BindLambdaForTesting(
[&](std::unique_ptr<disk_cache::Backend::Iterator> it) {
ASSERT_FALSE(it);
callback_called = true;
}));
EXPECT_TRUE(callback_called);
}
TEST_F(SharedDictionaryDiskCacheTest,
CreateIteratorCorruptedFailureWhileInitializing) {
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
bool callback_called = false;
disk_cache->CreateIterator(base::BindLambdaForTesting(
[&](std::unique_ptr<disk_cache::Backend::Iterator> it) {
ASSERT_FALSE(it);
callback_called = true;
}));
EXPECT_FALSE(callback_called);
FlushCacheTasks();
EXPECT_TRUE(callback_called);
}
TEST_F(SharedDictionaryDiskCacheTest, DeletedWhileRuningDidCreateBackend) {
// Corrupt the disk cache so that the callback is called synchronously.
CorruptDiskCache();
std::unique_ptr<SharedDictionaryDiskCache> disk_cache = CreateDiskCache();
// Test that UAF doesn't happen when `disk_cache` is synchronously deleted in
// the callback.
disk_cache->DoomEntry(
kTestKey, base::BindLambdaForTesting([&](int) { disk_cache.reset(); }));
disk_cache->DoomEntry(kTestKey,
base::BindOnce([](int) { ASSERT_TRUE(false); }));
FlushCacheTasks();
}
#endif // !BUILDFLAG(IS_FUCHSIA)
} // namespace network
|