blob: 5613d0d3b47b23ac46ae46dd4b2b4d16c71e150e [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]4dc3ad4f2013-06-11 07:15:5019#include "base/strings/string_number_conversions.h"
20#include "base/strings/string_util.h"
[email protected]750b2f3c2013-06-07 18:41:0521#include "base/strings/utf_string_conversions.h"
[email protected]34b99632011-01-01 01:01:0622#include "base/threading/thread.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;
[email protected]cadac622013-06-11 16:46:36141 disk_cache::BackendImpl* backend = new disk_cache::BackendImpl(
142 path, thread->message_loop_proxy().get(), NULL);
[email protected]398ad132013-04-02 15:11:01143 backend->SetMaxSize(size);
144 backend->SetType(net::DISK_CACHE);
145 backend->SetFlags(disk_cache::kNoRandom);
146 int rv = backend->Init(cb->callback());
147 *cache = backend;
[email protected]3a0ab94e2011-11-29 18:38:56148 return (cb->GetResult(rv) == net::OK && !(*cache)->GetEntryCount());
149}
150
initial.commit586acc5fe2008-07-26 22:42:52151// Generates the files for an empty and one item cache.
[email protected]6cdfd7f2013-02-08 20:40:15152int SimpleInsert(const base::FilePath& path, RankCrashes action,
[email protected]ec44a9a2010-06-15 19:31:51153 base::Thread* cache_thread) {
[email protected]2a65aceb82011-12-19 20:59:27154 net::TestCompletionCallback cb;
[email protected]ec44a9a2010-06-15 19:31:51155 disk_cache::Backend* cache;
[email protected]3a0ab94e2011-11-29 18:38:56156 if (!CreateCache(path, cache_thread, &cache, &cb))
initial.commit586acc5fe2008-07-26 22:42:52157 return GENERIC;
158
159 const char* test_name = "some other key";
160
161 if (action <= disk_cache::INSERT_EMPTY_3) {
162 test_name = kCrashEntryName;
[email protected]77b42dd2011-05-07 01:04:04163 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52164 }
165
166 disk_cache::Entry* entry;
[email protected]2a65aceb82011-12-19 20:59:27167 int rv = cache->CreateEntry(test_name, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51168 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52169 return GENERIC;
170
171 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56172 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52173
174 DCHECK(action <= disk_cache::INSERT_ONE_3);
[email protected]77b42dd2011-05-07 01:04:04175 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52176 test_name = kCrashEntryName;
177
[email protected]2a65aceb82011-12-19 20:59:27178 rv = cache->CreateEntry(test_name, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51179 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52180 return GENERIC;
181
182 return NOT_REACHED;
183}
184
185// Generates the files for a one item cache, and removing the head.
[email protected]6cdfd7f2013-02-08 20:40:15186int SimpleRemove(const base::FilePath& path, RankCrashes action,
[email protected]ec44a9a2010-06-15 19:31:51187 base::Thread* cache_thread) {
initial.commit586acc5fe2008-07-26 22:42:52188 DCHECK(action >= disk_cache::REMOVE_ONE_1);
189 DCHECK(action <= disk_cache::REMOVE_TAIL_3);
190
[email protected]2a65aceb82011-12-19 20:59:27191 net::TestCompletionCallback cb;
[email protected]ec44a9a2010-06-15 19:31:51192 disk_cache::Backend* cache;
[email protected]3a0ab94e2011-11-29 18:38:56193 if (!CreateCache(path, cache_thread, &cache, &cb))
initial.commit586acc5fe2008-07-26 22:42:52194 return GENERIC;
195
196 disk_cache::Entry* entry;
[email protected]2a65aceb82011-12-19 20:59:27197 int rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51198 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52199 return GENERIC;
200
201 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56202 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52203
204 if (action >= disk_cache::REMOVE_TAIL_1) {
[email protected]2a65aceb82011-12-19 20:59:27205 rv = cache->CreateEntry("some other key", &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51206 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52207 return GENERIC;
208
209 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56210 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52211 }
212
[email protected]2a65aceb82011-12-19 20:59:27213 rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51214 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52215 return GENERIC;
216
[email protected]77b42dd2011-05-07 01:04:04217 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52218 entry->Doom();
219 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56220 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52221
222 return NOT_REACHED;
223}
224
[email protected]6cdfd7f2013-02-08 20:40:15225int HeadRemove(const base::FilePath& path, RankCrashes action,
[email protected]ec44a9a2010-06-15 19:31:51226 base::Thread* cache_thread) {
initial.commit586acc5fe2008-07-26 22:42:52227 DCHECK(action >= disk_cache::REMOVE_HEAD_1);
228 DCHECK(action <= disk_cache::REMOVE_HEAD_4);
229
[email protected]2a65aceb82011-12-19 20:59:27230 net::TestCompletionCallback cb;
[email protected]ec44a9a2010-06-15 19:31:51231 disk_cache::Backend* cache;
[email protected]3a0ab94e2011-11-29 18:38:56232 if (!CreateCache(path, cache_thread, &cache, &cb))
initial.commit586acc5fe2008-07-26 22:42:52233 return GENERIC;
234
235 disk_cache::Entry* entry;
[email protected]2a65aceb82011-12-19 20:59:27236 int rv = cache->CreateEntry("some other key", &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51237 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52238 return GENERIC;
239
240 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56241 FlushQueue(cache);
[email protected]2a65aceb82011-12-19 20:59:27242 rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51243 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52244 return GENERIC;
245
246 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56247 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52248
[email protected]2a65aceb82011-12-19 20:59:27249 rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback());
[email protected]ec44a9a2010-06-15 19:31:51250 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52251 return GENERIC;
252
[email protected]77b42dd2011-05-07 01:04:04253 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52254 entry->Doom();
255 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56256 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52257
258 return NOT_REACHED;
259}
260
261// Generates the files for insertion and removals on heavy loaded caches.
[email protected]6cdfd7f2013-02-08 20:40:15262int LoadOperations(const base::FilePath& path, RankCrashes action,
[email protected]ec44a9a2010-06-15 19:31:51263 base::Thread* cache_thread) {
initial.commit586acc5fe2008-07-26 22:42:52264 DCHECK(action >= disk_cache::INSERT_LOAD_1);
265
[email protected]ec44a9a2010-06-15 19:31:51266 // Work with a tiny index table (16 entries).
267 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
[email protected]cadac622013-06-11 16:46:36268 path, 0xf, cache_thread->message_loop_proxy().get(), NULL);
[email protected]ec44a9a2010-06-15 19:31:51269 if (!cache || !cache->SetMaxSize(0x100000))
270 return GENERIC;
271
[email protected]3a0ab94e2011-11-29 18:38:56272 // No experiments and use a simple LRU.
273 cache->SetFlags(disk_cache::kNoRandom);
[email protected]42c459632011-12-17 02:20:23274 net::TestCompletionCallback cb;
275 int rv = cache->Init(cb.callback());
[email protected]fb2622f2010-07-13 18:00:56276 if (cb.GetResult(rv) != net::OK || cache->GetEntryCount())
initial.commit586acc5fe2008-07-26 22:42:52277 return GENERIC;
278
279 int seed = static_cast<int>(Time::Now().ToInternalValue());
280 srand(seed);
281
282 disk_cache::Entry* entry;
283 for (int i = 0; i < 100; i++) {
284 std::string key = GenerateKey(true);
[email protected]2a65aceb82011-12-19 20:59:27285 rv = cache->CreateEntry(key, &entry, cb.callback());
286 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52287 return GENERIC;
288 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56289 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52290 if (50 == i && action >= disk_cache::REMOVE_LOAD_1) {
[email protected]2a65aceb82011-12-19 20:59:27291 rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
292 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52293 return GENERIC;
294 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56295 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52296 }
297 }
298
299 if (action <= disk_cache::INSERT_LOAD_2) {
[email protected]77b42dd2011-05-07 01:04:04300 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52301
[email protected]2a65aceb82011-12-19 20:59:27302 rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
303 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52304 return GENERIC;
305 }
306
[email protected]2a65aceb82011-12-19 20:59:27307 rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback());
308 if (cb.GetResult(rv) != net::OK)
initial.commit586acc5fe2008-07-26 22:42:52309 return GENERIC;
310
[email protected]77b42dd2011-05-07 01:04:04311 disk_cache::g_rankings_crash = action;
initial.commit586acc5fe2008-07-26 22:42:52312
313 entry->Doom();
314 entry->Close();
[email protected]fb2622f2010-07-13 18:00:56315 FlushQueue(cache);
initial.commit586acc5fe2008-07-26 22:42:52316
317 return NOT_REACHED;
318}
319
320// Main function on the child process.
[email protected]6cdfd7f2013-02-08 20:40:15321int SlaveCode(const base::FilePath& path, RankCrashes action) {
[email protected]2da659e2013-05-23 20:51:34322 base::MessageLoopForIO message_loop;
initial.commit586acc5fe2008-07-26 22:42:52323
[email protected]6cdfd7f2013-02-08 20:40:15324 base::FilePath full_path;
initial.commit586acc5fe2008-07-26 22:42:52325 if (!CreateTargetFolder(path, action, &full_path)) {
326 printf("Destination folder found, please remove it.\n");
327 return CRASH_OVERWRITE;
328 }
329
[email protected]ec44a9a2010-06-15 19:31:51330 base::Thread cache_thread("CacheThread");
331 if (!cache_thread.StartWithOptions(
[email protected]2da659e2013-05-23 20:51:34332 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)))
[email protected]ec44a9a2010-06-15 19:31:51333 return GENERIC;
334
initial.commit586acc5fe2008-07-26 22:42:52335 if (action <= disk_cache::INSERT_ONE_3)
[email protected]ec44a9a2010-06-15 19:31:51336 return SimpleInsert(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52337
338 if (action <= disk_cache::INSERT_LOAD_2)
[email protected]ec44a9a2010-06-15 19:31:51339 return LoadOperations(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52340
341 if (action <= disk_cache::REMOVE_ONE_4)
[email protected]ec44a9a2010-06-15 19:31:51342 return SimpleRemove(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52343
344 if (action <= disk_cache::REMOVE_HEAD_4)
[email protected]ec44a9a2010-06-15 19:31:51345 return HeadRemove(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52346
347 if (action <= disk_cache::REMOVE_TAIL_3)
[email protected]ec44a9a2010-06-15 19:31:51348 return SimpleRemove(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52349
350 if (action <= disk_cache::REMOVE_LOAD_3)
[email protected]ec44a9a2010-06-15 19:31:51351 return LoadOperations(full_path, action, &cache_thread);
initial.commit586acc5fe2008-07-26 22:42:52352
353 return NOT_REACHED;
354}
355
356// -----------------------------------------------------------------------
357
358int main(int argc, const char* argv[]) {
[email protected]9b7e4492008-08-14 21:11:49359 // Setup an AtExitManager so Singleton objects will be destructed.
[email protected]f0a51fb52009-03-05 12:46:38360 base::AtExitManager at_exit_manager;
[email protected]9b7e4492008-08-14 21:11:49361
initial.commit586acc5fe2008-07-26 22:42:52362 if (argc < 2)
363 return MasterCode();
364
365 char* end;
366 RankCrashes action = static_cast<RankCrashes>(strtol(argv[1], &end, 0));
367 if (action <= disk_cache::NO_CRASH || action >= disk_cache::MAX_CRASH) {
368 printf("Invalid action\n");
369 return INVALID_ARGUMENT;
370 }
371
[email protected]6cdfd7f2013-02-08 20:40:15372 base::FilePath path;
initial.commit586acc5fe2008-07-26 22:42:52373 PathService::Get(base::DIR_SOURCE_ROOT, &path);
[email protected]399b8702009-05-01 20:34:02374 path = path.AppendASCII("net");
375 path = path.AppendASCII("data");
376 path = path.AppendASCII("cache_tests");
377 path = path.AppendASCII("new_crashes");
initial.commit586acc5fe2008-07-26 22:42:52378
[email protected]5306d112009-10-15 17:38:04379 return SlaveCode(path, action);
initial.commit586acc5fe2008-07-26 22:42:52380}