blob: 39f9d103912bfff7684694f591a211c0ad38e893 [file] [log] [blame]
[email protected]adda8692011-07-14 20:55:301// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5// This command-line program generates the set of files needed for the crash-
6// cache unit tests (DiskCacheTest,CacheBackend_Recover*). This program only
7// works properly on debug mode, because the crash functionality is not compiled
8// on release builds of the cache.
9
initial.commit586acc5fe2008-07-26 22:42:5210#include <string>
11
[email protected]9b7e4492008-08-14 21:11:4912#include "base/at_exit.h"
[email protected]c7856632009-01-13 17:38:4913#include "base/command_line.h"
initial.commit586acc5fe2008-07-26 22:42:5214#include "base/file_util.h"
15#include "base/logging.h"
16#include "base/message_loop.h"
17#include "base/path_service.h"
[email protected]c7856632009-01-13 17:38:4918#include "base/process_util.h"
[email protected]e83326f2010-07-31 17:29:2519#include "base/string_number_conversions.h"
initial.commit586acc5fe2008-07-26 22:42:5220#include "base/string_util.h"
[email protected]34b99632011-01-01 01:01:0621#include "base/threading/thread.h"
[email protected]e83326f2010-07-31 17:29:2522#include "base/utf_string_conversions.h"
[email protected]ec44a9a2010-06-15 19:31:5123#include "net/base/net_errors.h"
[email protected]172da1b2011-08-12 15:52:2624#include "net/base/net_export.h"
[email protected]ec44a9a2010-06-15 19:31:5125#include "net/base/test_completion_callback.h"
initial.commit586acc5fe2008-07-26 22:42:5226#include "net/disk_cache/backend_impl.h"
27#include "net/disk_cache/disk_cache.h"
28#include "net/disk_cache/disk_cache_test_util.h"
29#include "net/disk_cache/rankings.h"
30
[email protected]e1acf6f2008-10-27 20:43:3331using base::Time;
32
initial.commit586acc5fe2008-07-26 22:42:5233enum Errors {
34 GENERIC = -1,
35 ALL_GOOD = 0,
36 INVALID_ARGUMENT = 1,
37 CRASH_OVERWRITE,
38 NOT_REACHED
39};
40
41using disk_cache::RankCrashes;
42
43// Starts a new process, to generate the files.
44int RunSlave(RankCrashes action) {
[email protected]6cdfd7f2013-02-08 20:40:1545 base::FilePath exe;
initial.commit586acc5fe2008-07-26 22:42:5246 PathService::Get(base::FILE_EXE, &exe);
47
[email protected]51343d5a2009-10-26 22:39:3348 CommandLine cmdline(exe);
[email protected]0445eb42010-08-13 22:10:3049 cmdline.AppendArg(base::IntToString(action));
initial.commit586acc5fe2008-07-26 22:42:5250
[email protected]c7856632009-01-13 17:38:4951 base::ProcessHandle handle;
[email protected]e5992182011-07-15 16:47:0252 if (!base::LaunchProcess(cmdline, base::LaunchOptions(), &handle)) {
initial.commit586acc5fe2008-07-26 22:42:5253 printf("Unable to run test %d\n", action);
54 return GENERIC;
55 }
56
[email protected]c7856632009-01-13 17:38:4957 int exit_code;
initial.commit586acc5fe2008-07-26 22:42:5258
[email protected]c7856632009-01-13 17:38:4959 if (!base::WaitForExitCode(handle, &exit_code)) {
initial.commit586acc5fe2008-07-26 22:42:5260 printf("Unable to get return code, test %d\n", action);
61 return GENERIC;
62 }
[email protected]c7856632009-01-13 17:38:4963 if (ALL_GOOD != exit_code)
64 printf("Test %d failed, code %d\n", action, exit_code);
initial.commit586acc5fe2008-07-26 22:42:5265
[email protected]c7856632009-01-13 17:38:4966 return exit_code;
initial.commit586acc5fe2008-07-26 22:42:5267}
68
69// Main loop for the master process.
70int MasterCode() {
71 for (int i = disk_cache::NO_CRASH + 1; i < disk_cache::MAX_CRASH; i++) {
72 int ret = RunSlave(static_cast<RankCrashes>(i));
73 if (ALL_GOOD != ret)
74 return ret;
75 }
76
77 return ALL_GOOD;
78}
79
80// -----------------------------------------------------------------------
81
[email protected]77b42dd2011-05-07 01:04:0482namespace disk_cache {
[email protected]172da1b2011-08-12 15:52:2683NET_EXPORT_PRIVATE extern RankCrashes g_rankings_crash;
[email protected]77b42dd2011-05-07 01:04:0484}
85
initial.commit586acc5fe2008-07-26 22:42:5286const char* kCrashEntryName = "the first key";
87
88// Creates the destinaton folder for this run, and returns it on full_path.
[email protected]6cdfd7f2013-02-08 20:40:1589bool CreateTargetFolder(const base::FilePath& path, RankCrashes action,
90 base::FilePath* full_path) {
[email protected]5306d112009-10-15 17:38:0491 const char* folders[] = {
92 "",
93 "insert_empty1",
94 "insert_empty2",
95 "insert_empty3",
96 "insert_one1",
97 "insert_one2",
98 "insert_one3",
99 "insert_load1",
100 "insert_load2",
101 "remove_one1",
102 "remove_one2",
103 "remove_one3",
104 "remove_one4",
105 "remove_head1",
106 "remove_head2",
107 "remove_head3",
108 "remove_head4",
109 "remove_tail1",
110 "remove_tail2",
111 "remove_tail3",
112 "remove_load1",
113 "remove_load2",
114 "remove_load3"
initial.commit586acc5fe2008-07-26 22:42:52115 };
116 COMPILE_ASSERT(arraysize(folders) == disk_cache::MAX_CRASH, sync_folders);
117 DCHECK(action > disk_cache::NO_CRASH && action < disk_cache::MAX_CRASH);
118
[email protected]5306d112009-10-15 17:38:04119 *full_path = path.AppendASCII(folders[action]);
initial.commit586acc5fe2008-07-26 22:42:52120
[email protected]5306d112009-10-15 17:38:04121 if (file_util::PathExists(*full_path))
[email protected]c7856632009-01-13 17:38:49122 return false;
123
initial.commit586acc5fe2008-07-26 22:42:52124 return file_util::CreateDirectory(*full_path);
125}
126
[email protected]fb2622f2010-07-13 18:00:56127// Makes sure that any pending task is processed.
128void FlushQueue(disk_cache::Backend* cache) {
[email protected]2a65aceb82011-12-19 20:59:27129 net::TestCompletionCallback cb;
[email protected]fb2622f2010-07-13 18:00:56130 int rv =
[email protected]2a65aceb82011-12-19 20:59:27131 reinterpret_cast<disk_cache::BackendImpl*>(cache)->FlushQueueForTest(
132 cb.callback());
[email protected]fb2622f2010-07-13 18:00:56133 cb.GetResult(rv); // Ignore the result;
134}
135
[email protected]6cdfd7f2013-02-08 20:40:15136bool CreateCache(const base::FilePath& path,
[email protected]2a65aceb82011-12-19 20:59:27137 base::Thread* thread,
138 disk_cache::Backend** cache,
139 net::TestCompletionCallback* cb) {
[email protected]3a0ab94e2011-11-29 18:38:56140 int size = 1024 * 1024;
141 int rv = disk_cache::BackendImpl::CreateBackend(
142 path, false, size, net::DISK_CACHE, disk_cache::kNoRandom,
[email protected]2a65aceb82011-12-19 20:59:27143 thread->message_loop_proxy(), NULL, cache, cb->callback());
[email protected]3a0ab94e2011-11-29 18:38:56144
145 return (cb->GetResult(rv) == net::OK && !(*cache)->GetEntryCount());
146}
147
initial.commit586acc5fe2008-07-26 22:42:52148// Generates the files for an empty and one item cache.
[email protected]6cdfd7f2013-02-08 20:40:15149int SimpleInsert(const base::FilePath& path, RankCrashes action,
[email protected]ec44a9a2010-06-15 19:31:51150 base::Thread* cache_thread) {
[email protected]2a65aceb82011-12-19 20:59:27151 net::TestCompletionCallback cb;
[email protected]ec44a9a2010-06-15 19:31:51152 disk_cache::Backend* cache;
[email protected]3a0ab94e2011-11-29 18:38:56153 if (!CreateCache(path, cache_thread, &cache, &cb))
initial.commit586acc5fe2008-07-26 22:42:52154 return GENERIC;
155
156 const char* test_name = "some other key";
157
158 if (action <= disk_cache::INSERT_EMPTY_3) {
159 test_name = kCrashEntryName;
[email protected]77b42dd2011-05-07 01:04:04160 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52161 }
162
163 disk_cache::Entry* entry;
[email protected]2a65aceb82011-12-19 20:59:27164 int rv = cache->CreateEntry(test_name, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51165 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52166 return GENERIC;
167
168 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56169 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52170
171 DCHECK(action <= disk_cache::INSERT_ONE_3);
[email protected]77b42dd2011-05-07 01:04:04172 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52173 test_name = kCrashEntryName;
174
[email protected]2a65aceb82011-12-19 20:59:27175 rv = cache->CreateEntry(test_name, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51176 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52177 return GENERIC;
178
179 return NOT_REACHED;
180}
181
182// Generates the files for a one item cache, and removing the head.
[email protected]6cdfd7f2013-02-08 20:40:15183int SimpleRemove(const base::FilePath& path, RankCrashes action,
[email protected]ec44a9a2010-06-15 19:31:51184 base::Thread* cache_thread) {
initial.commit586acc5fe2008-07-26 22:42:52185 DCHECK(action >= disk_cache::REMOVE_ONE_1);
186 DCHECK(action <= disk_cache::REMOVE_TAIL_3);
187
[email protected]2a65aceb82011-12-19 20:59:27188 net::TestCompletionCallback cb;
[email protected]ec44a9a2010-06-15 19:31:51189 disk_cache::Backend* cache;
[email protected]3a0ab94e2011-11-29 18:38:56190 if (!CreateCache(path, cache_thread, &cache, &cb))
initial.commit586acc5fe2008-07-26 22:42:52191 return GENERIC;
192
193 disk_cache::Entry* entry;
[email protected]2a65aceb82011-12-19 20:59:27194 int rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51195 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52196 return GENERIC;
197
198 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56199 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52200
201 if (action >= disk_cache::REMOVE_TAIL_1) {
[email protected]2a65aceb82011-12-19 20:59:27202 rv = cache->CreateEntry("some other key", &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51203 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52204 return GENERIC;
205
206 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56207 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52208 }
209
[email protected]2a65aceb82011-12-19 20:59:27210 rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51211 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52212 return GENERIC;
213
[email protected]77b42dd2011-05-07 01:04:04214 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52215 entry->Doom();
216 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56217 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52218
219 return NOT_REACHED;
220}
221
[email protected]6cdfd7f2013-02-08 20:40:15222int HeadRemove(const base::FilePath& path, RankCrashes action,
[email protected]ec44a9a2010-06-15 19:31:51223 base::Thread* cache_thread) {
initial.commit586acc5fe2008-07-26 22:42:52224 DCHECK(action >= disk_cache::REMOVE_HEAD_1);
225 DCHECK(action <= disk_cache::REMOVE_HEAD_4);
226
[email protected]2a65aceb82011-12-19 20:59:27227 net::TestCompletionCallback cb;
[email protected]ec44a9a2010-06-15 19:31:51228 disk_cache::Backend* cache;
[email protected]3a0ab94e2011-11-29 18:38:56229 if (!CreateCache(path, cache_thread, &cache, &cb))
initial.commit586acc5fe2008-07-26 22:42:52230 return GENERIC;
231
232 disk_cache::Entry* entry;
[email protected]2a65aceb82011-12-19 20:59:27233 int rv = cache->CreateEntry("some other key", &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51234 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52235 return GENERIC;
236
237 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56238 FlushQueue(cache);
[email protected]2a65aceb82011-12-19 20:59:27239 rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51240 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52241 return GENERIC;
242
243 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56244 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52245
[email protected]2a65aceb82011-12-19 20:59:27246 rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51247 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52248 return GENERIC;
249
[email protected]77b42dd2011-05-07 01:04:04250 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52251 entry->Doom();
252 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56253 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52254
255 return NOT_REACHED;
256}
257
258// Generates the files for insertion and removals on heavy loaded caches.
[email protected]6cdfd7f2013-02-08 20:40:15259int LoadOperations(const base::FilePath& path, RankCrashes action,
[email protected]ec44a9a2010-06-15 19:31:51260 base::Thread* cache_thread) {
initial.commit586acc5fe2008-07-26 22:42:52261 DCHECK(action >= disk_cache::INSERT_LOAD_1);
262
[email protected]ec44a9a2010-06-15 19:31:51263 // Work with a tiny index table (16 entries).
264 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
[email protected]f6f1bebc2011-01-07 03:04:54265 path, 0xf, cache_thread->message_loop_proxy(), NULL);
[email protected]ec44a9a2010-06-15 19:31:51266 if (!cache || !cache->SetMaxSize(0x100000))
267 return GENERIC;
268
[email protected]3a0ab94e2011-11-29 18:38:56269 // No experiments and use a simple LRU.
270 cache->SetFlags(disk_cache::kNoRandom);
[email protected]42c459632011-12-17 02:20:23271 net::TestCompletionCallback cb;
272 int rv = cache->Init(cb.callback());
[email protected]fb2622f2010-07-13 18:00:56273 if (cb.GetResult(rv) != net::OK || cache->GetEntryCount())
initial.commit586acc5fe2008-07-26 22:42:52274 return GENERIC;
275
276 int seed = static_cast<int>(Time::Now().ToInternalValue());
277 srand(seed);
278
279 disk_cache::Entry* entry;
280 for (int i = 0; i < 100; i++) {
281 std::string key = GenerateKey(true);
[email protected]2a65aceb82011-12-19 20:59:27282 rv = cache->CreateEntry(key, &entry, cb.callback());
283 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52284 return GENERIC;
285 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56286 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52287 if (50 == i && action >= disk_cache::REMOVE_LOAD_1) {
[email protected]2a65aceb82011-12-19 20:59:27288 rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
289 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52290 return GENERIC;
291 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56292 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52293 }
294 }
295
296 if (action <= disk_cache::INSERT_LOAD_2) {
[email protected]77b42dd2011-05-07 01:04:04297 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52298
[email protected]2a65aceb82011-12-19 20:59:27299 rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
300 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52301 return GENERIC;
302 }
303
[email protected]2a65aceb82011-12-19 20:59:27304 rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback());
305 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52306 return GENERIC;
307
[email protected]77b42dd2011-05-07 01:04:04308 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52309
310 entry->Doom();
311 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56312 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52313
314 return NOT_REACHED;
315}
316
317// Main function on the child process.
[email protected]6cdfd7f2013-02-08 20:40:15318int SlaveCode(const base::FilePath& path, RankCrashes action) {
[email protected]ec44a9a2010-06-15 19:31:51319 MessageLoopForIO message_loop;
initial.commit586acc5fe2008-07-26 22:42:52320
[email protected]6cdfd7f2013-02-08 20:40:15321 base::FilePath full_path;
initial.commit586acc5fe2008-07-26 22:42:52322 if (!CreateTargetFolder(path, action, &full_path)) {
323 printf("Destination folder found, please remove it.\n");
324 return CRASH_OVERWRITE;
325 }
326
[email protected]ec44a9a2010-06-15 19:31:51327 base::Thread cache_thread("CacheThread");
328 if (!cache_thread.StartWithOptions(
329 base::Thread::Options(MessageLoop::TYPE_IO, 0)))
330 return GENERIC;
331
initial.commit586acc5fe2008-07-26 22:42:52332 if (action <= disk_cache::INSERT_ONE_3)
[email protected]ec44a9a2010-06-15 19:31:51333 return SimpleInsert(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52334
335 if (action <= disk_cache::INSERT_LOAD_2)
[email protected]ec44a9a2010-06-15 19:31:51336 return LoadOperations(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52337
338 if (action <= disk_cache::REMOVE_ONE_4)
[email protected]ec44a9a2010-06-15 19:31:51339 return SimpleRemove(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52340
341 if (action <= disk_cache::REMOVE_HEAD_4)
[email protected]ec44a9a2010-06-15 19:31:51342 return HeadRemove(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52343
344 if (action <= disk_cache::REMOVE_TAIL_3)
[email protected]ec44a9a2010-06-15 19:31:51345 return SimpleRemove(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52346
347 if (action <= disk_cache::REMOVE_LOAD_3)
[email protected]ec44a9a2010-06-15 19:31:51348 return LoadOperations(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52349
350 return NOT_REACHED;
351}
352
353// -----------------------------------------------------------------------
354
355int main(int argc, const char* argv[]) {
[email protected]9b7e4492008-08-14 21:11:49356 // Setup an AtExitManager so Singleton objects will be destructed.
[email protected]f0a51fb52009-03-05 12:46:38357 base::AtExitManager at_exit_manager;
[email protected]9b7e4492008-08-14 21:11:49358
initial.commit586acc5fe2008-07-26 22:42:52359 if (argc < 2)
360 return MasterCode();
361
362 char* end;
363 RankCrashes action = static_cast<RankCrashes>(strtol(argv[1], &end, 0));
364 if (action <= disk_cache::NO_CRASH || action >= disk_cache::MAX_CRASH) {
365 printf("Invalid action\n");
366 return INVALID_ARGUMENT;
367 }
368
[email protected]6cdfd7f2013-02-08 20:40:15369 base::FilePath path;
initial.commit586acc5fe2008-07-26 22:42:52370 PathService::Get(base::DIR_SOURCE_ROOT, &path);
[email protected]399b8702009-05-01 20:34:02371 path = path.AppendASCII("net");
372 path = path.AppendASCII("data");
373 path = path.AppendASCII("cache_tests");
374 path = path.AppendASCII("new_crashes");
initial.commit586acc5fe2008-07-26 22:42:52375
[email protected]5306d112009-10-15 17:38:04376 return SlaveCode(path, action);
initial.commit586acc5fe2008-07-26 22:42:52377}