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
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This command-line program generates the set of files needed for the crash-
// cache unit tests (DiskCacheTest,CacheBackend_Recover*). This program only
// works properly on debug mode, because the crash functionality is not compiled
// on release builds of the cache.
#include <string>
#include "base/at_exit.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/message_loop/message_pump_type.h"
#include "base/path_service.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_executor.h"
#include "base/test/test_future.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "net/base/net_errors.h"
#include "net/base/net_export.h"
#include "net/base/test_completion_callback.h"
#include "net/disk_cache/backend_cleanup_tracker.h"
#include "net/disk_cache/blockfile/backend_impl.h"
#include "net/disk_cache/blockfile/rankings.h"
#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/disk_cache_test_util.h"
using base::Time;
enum Errors {
GENERIC = -1,
ALL_GOOD = 0,
INVALID_ARGUMENT = 1,
CRASH_OVERWRITE,
NOT_REACHED
};
using disk_cache::RankCrashes;
// Starts a new process, to generate the files.
int RunSlave(RankCrashes action) {
base::FilePath exe;
base::PathService::Get(base::FILE_EXE, &exe);
base::CommandLine cmdline(exe);
cmdline.AppendArg(base::NumberToString(action));
base::Process process = base::LaunchProcess(cmdline, base::LaunchOptions());
if (!process.IsValid()) {
printf("Unable to run test %d\n", action);
return GENERIC;
}
int exit_code;
if (!process.WaitForExit(&exit_code)) {
printf("Unable to get return code, test %d\n", action);
return GENERIC;
}
if (ALL_GOOD != exit_code)
printf("Test %d failed, code %d\n", action, exit_code);
return exit_code;
}
// Main loop for the master process.
int MasterCode() {
for (int i = disk_cache::NO_CRASH + 1; i < disk_cache::MAX_CRASH; i++) {
int ret = RunSlave(static_cast<RankCrashes>(i));
if (ALL_GOOD != ret)
return ret;
}
return ALL_GOOD;
}
// -----------------------------------------------------------------------
namespace disk_cache {
NET_EXPORT_PRIVATE extern RankCrashes g_rankings_crash;
}
const char kCrashEntryName[] = "the first key";
// Creates the destinaton folder for this run, and returns it on full_path.
bool CreateTargetFolder(const base::FilePath& path, RankCrashes action,
base::FilePath* full_path) {
auto folders = std::to_array({"",
"insert_empty1",
"insert_empty2",
"insert_empty3",
"insert_one1",
"insert_one2",
"insert_one3",
"insert_load1",
"insert_load2",
"remove_one1",
"remove_one2",
"remove_one3",
"remove_one4",
"remove_head1",
"remove_head2",
"remove_head3",
"remove_head4",
"remove_tail1",
"remove_tail2",
"remove_tail3",
"remove_load1",
"remove_load2",
"remove_load3"});
static_assert(std::size(folders) == disk_cache::MAX_CRASH, "sync folders");
DCHECK(action > disk_cache::NO_CRASH && action < disk_cache::MAX_CRASH);
*full_path = path.AppendASCII(folders[action]);
if (base::PathExists(*full_path))
return false;
return base::CreateDirectory(*full_path);
}
// Makes sure that any pending task is processed.
void FlushQueue(disk_cache::Backend* cache) {
net::TestCompletionCallback cb;
int rv =
reinterpret_cast<disk_cache::BackendImpl*>(cache)->FlushQueueForTest(
cb.callback());
cb.GetResult(rv); // Ignore the result;
}
int32_t GetCacheEntryCount(disk_cache::Backend* cache) {
base::test::TestFuture<int32_t> future;
base::expected<int32_t, net::Error> result =
cache->GetEntryCount(future.GetCallback());
if (result.has_value()) {
return result.value();
}
CHECK_EQ(result.error(), net::ERR_IO_PENDING);
return future.Get();
}
bool CreateCache(const base::FilePath& path,
base::Thread* thread,
disk_cache::Backend** cache,
net::TestCompletionCallback* cb) {
int size = 1024 * 1024;
disk_cache::BackendImpl* backend = new disk_cache::BackendImpl(
path, /* cleanup_tracker = */ nullptr, thread->task_runner().get(),
net::DISK_CACHE, /* net_log = */ nullptr);
backend->SetMaxSize(size);
backend->SetFlags(disk_cache::kNoRandom);
backend->Init(cb->callback());
*cache = backend;
return (cb->WaitForResult() == net::OK && !GetCacheEntryCount(*cache));
}
// Generates the files for an empty and one item cache.
int SimpleInsert(const base::FilePath& path, RankCrashes action,
base::Thread* cache_thread) {
net::TestCompletionCallback cb;
disk_cache::Backend* cache;
if (!CreateCache(path, cache_thread, &cache, &cb))
return GENERIC;
const char* test_name = "some other key";
if (action <= disk_cache::INSERT_EMPTY_3) {
test_name = kCrashEntryName;
disk_cache::g_rankings_crash = action;
}
TestEntryResultCompletionCallback cb_create;
disk_cache::EntryResult result = cb_create.GetResult(
cache->CreateEntry(test_name, net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
result.ReleaseEntry()->Close();
FlushQueue(cache);
DCHECK(action <= disk_cache::INSERT_ONE_3);
disk_cache::g_rankings_crash = action;
test_name = kCrashEntryName;
result = cb_create.GetResult(
cache->CreateEntry(test_name, net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
return NOT_REACHED;
}
// Generates the files for a one item cache, and removing the head.
int SimpleRemove(const base::FilePath& path, RankCrashes action,
base::Thread* cache_thread) {
DCHECK(action >= disk_cache::REMOVE_ONE_1);
DCHECK(action <= disk_cache::REMOVE_TAIL_3);
net::TestCompletionCallback cb;
disk_cache::Backend* cache;
if (!CreateCache(path, cache_thread, &cache, &cb))
return GENERIC;
TestEntryResultCompletionCallback cb_create;
disk_cache::EntryResult result = cb_create.GetResult(
cache->CreateEntry(kCrashEntryName, net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
result.ReleaseEntry()->Close();
FlushQueue(cache);
if (action >= disk_cache::REMOVE_TAIL_1) {
result = cb_create.GetResult(cache->CreateEntry(
"some other key", net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
result.ReleaseEntry()->Close();
FlushQueue(cache);
}
result = cb_create.GetResult(
cache->OpenEntry(kCrashEntryName, net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
disk_cache::g_rankings_crash = action;
disk_cache::Entry* entry = result.ReleaseEntry();
entry->Doom();
entry->Close();
FlushQueue(cache);
return NOT_REACHED;
}
int HeadRemove(const base::FilePath& path, RankCrashes action,
base::Thread* cache_thread) {
DCHECK(action >= disk_cache::REMOVE_HEAD_1);
DCHECK(action <= disk_cache::REMOVE_HEAD_4);
net::TestCompletionCallback cb;
disk_cache::Backend* cache;
if (!CreateCache(path, cache_thread, &cache, &cb))
return GENERIC;
TestEntryResultCompletionCallback cb_create;
disk_cache::EntryResult result = cb_create.GetResult(
cache->CreateEntry("some other key", net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
result.ReleaseEntry()->Close();
FlushQueue(cache);
result = cb_create.GetResult(
cache->CreateEntry(kCrashEntryName, net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
result.ReleaseEntry()->Close();
FlushQueue(cache);
result = cb_create.GetResult(
cache->OpenEntry(kCrashEntryName, net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
disk_cache::g_rankings_crash = action;
disk_cache::Entry* entry = result.ReleaseEntry();
entry->Doom();
entry->Close();
FlushQueue(cache);
return NOT_REACHED;
}
// Generates the files for insertion and removals on heavy loaded caches.
int LoadOperations(const base::FilePath& path, RankCrashes action,
base::Thread* cache_thread) {
DCHECK(action >= disk_cache::INSERT_LOAD_1);
// Work with a tiny index table (16 entries).
disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
path, 0xf, /*cleanup_tracker=*/nullptr, cache_thread->task_runner().get(),
net::DISK_CACHE, nullptr);
if (!cache->SetMaxSize(0x100000))
return GENERIC;
// No experiments and use a simple LRU.
cache->SetFlags(disk_cache::kNoRandom);
net::TestCompletionCallback cb;
cache->Init(cb.callback());
if (cb.WaitForResult() != net::OK || GetCacheEntryCount(cache)) {
return GENERIC;
}
int seed = static_cast<int>(Time::Now().ToInternalValue());
srand(seed);
TestEntryResultCompletionCallback cb_create;
for (int i = 0; i < 100; i++) {
std::string key = GenerateKey(true);
disk_cache::EntryResult result = cb_create.GetResult(
cache->CreateEntry(key, net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
result.ReleaseEntry()->Close();
FlushQueue(cache);
if (50 == i && action >= disk_cache::REMOVE_LOAD_1) {
result = cb_create.GetResult(cache->CreateEntry(
kCrashEntryName, net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
result.ReleaseEntry()->Close();
FlushQueue(cache);
}
}
if (action <= disk_cache::INSERT_LOAD_2) {
disk_cache::g_rankings_crash = action;
disk_cache::EntryResult result = cb_create.GetResult(cache->CreateEntry(
kCrashEntryName, net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
result.ReleaseEntry(); // leaks.
}
disk_cache::EntryResult result = cb_create.GetResult(
cache->OpenEntry(kCrashEntryName, net::HIGHEST, cb_create.callback()));
if (result.net_error() != net::OK)
return GENERIC;
disk_cache::g_rankings_crash = action;
disk_cache::Entry* entry = result.ReleaseEntry();
entry->Doom();
entry->Close();
FlushQueue(cache);
return NOT_REACHED;
}
// Main function on the child process.
int SlaveCode(const base::FilePath& path, RankCrashes action) {
base::SingleThreadTaskExecutor io_task_executor(base::MessagePumpType::IO);
base::FilePath full_path;
if (!CreateTargetFolder(path, action, &full_path)) {
printf("Destination folder found, please remove it.\n");
return CRASH_OVERWRITE;
}
base::Thread cache_thread("CacheThread");
if (!cache_thread.StartWithOptions(
base::Thread::Options(base::MessagePumpType::IO, 0)))
return GENERIC;
if (action <= disk_cache::INSERT_ONE_3)
return SimpleInsert(full_path, action, &cache_thread);
if (action <= disk_cache::INSERT_LOAD_2)
return LoadOperations(full_path, action, &cache_thread);
if (action <= disk_cache::REMOVE_ONE_4)
return SimpleRemove(full_path, action, &cache_thread);
if (action <= disk_cache::REMOVE_HEAD_4)
return HeadRemove(full_path, action, &cache_thread);
if (action <= disk_cache::REMOVE_TAIL_3)
return SimpleRemove(full_path, action, &cache_thread);
if (action <= disk_cache::REMOVE_LOAD_3)
return LoadOperations(full_path, action, &cache_thread);
return NOT_REACHED;
}
// -----------------------------------------------------------------------
int main(int argc, const char* argv[]) {
// Setup an AtExitManager so Singleton objects will be destructed.
base::AtExitManager at_exit_manager;
if (argc < 2)
return MasterCode();
int action_in = 0;
// SAFETY: We check that argc >= 2 above, so argv[1] is fine.
if (!base::StringToInt(UNSAFE_BUFFERS(argv[1]), &action_in) ||
action_in <= disk_cache::NO_CRASH || action_in >= disk_cache::MAX_CRASH) {
printf("Invalid action\n");
return INVALID_ARGUMENT;
}
RankCrashes action = static_cast<RankCrashes>(action_in);
base::FilePath path;
base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &path);
path = path.AppendASCII("net");
path = path.AppendASCII("data");
path = path.AppendASCII("cache_tests");
path = path.AppendASCII("new_crashes");
return SlaveCode(path, action);
}
|