By moving the call to Load() up in SearchProvider::Start(), we are giving a chance...
[chromium-blink-merge.git] / net / tools / crash_cache / crash_cache.cc
blobd616446743ffc005852837b6d4d8ba4970e62de5
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
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.
10 #include <string>
12 #include "base/at_exit.h"
13 #include "base/command_line.h"
14 #include "base/file_util.h"
15 #include "base/logging.h"
16 #include "base/message_loop.h"
17 #include "base/path_service.h"
18 #include "base/process_util.h"
19 #include "base/string_number_conversions.h"
20 #include "base/string_util.h"
21 #include "base/threading/thread.h"
22 #include "base/utf_string_conversions.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/net_export.h"
25 #include "net/base/test_completion_callback.h"
26 #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"
31 using base::Time;
33 enum Errors {
34 GENERIC = -1,
35 ALL_GOOD = 0,
36 INVALID_ARGUMENT = 1,
37 CRASH_OVERWRITE,
38 NOT_REACHED
41 using disk_cache::RankCrashes;
43 // Starts a new process, to generate the files.
44 int RunSlave(RankCrashes action) {
45 FilePath exe;
46 PathService::Get(base::FILE_EXE, &exe);
48 CommandLine cmdline(exe);
49 cmdline.AppendArg(base::IntToString(action));
51 base::ProcessHandle handle;
52 if (!base::LaunchProcess(cmdline, base::LaunchOptions(), &handle)) {
53 printf("Unable to run test %d\n", action);
54 return GENERIC;
57 int exit_code;
59 if (!base::WaitForExitCode(handle, &exit_code)) {
60 printf("Unable to get return code, test %d\n", action);
61 return GENERIC;
63 if (ALL_GOOD != exit_code)
64 printf("Test %d failed, code %d\n", action, exit_code);
66 return exit_code;
69 // Main loop for the master process.
70 int 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;
77 return ALL_GOOD;
80 // -----------------------------------------------------------------------
82 namespace disk_cache {
83 NET_EXPORT_PRIVATE extern RankCrashes g_rankings_crash;
86 const char* kCrashEntryName = "the first key";
88 // Creates the destinaton folder for this run, and returns it on full_path.
89 bool CreateTargetFolder(const FilePath& path, RankCrashes action,
90 FilePath* full_path) {
91 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"
116 COMPILE_ASSERT(arraysize(folders) == disk_cache::MAX_CRASH, sync_folders);
117 DCHECK(action > disk_cache::NO_CRASH && action < disk_cache::MAX_CRASH);
119 *full_path = path.AppendASCII(folders[action]);
121 if (file_util::PathExists(*full_path))
122 return false;
124 return file_util::CreateDirectory(*full_path);
127 // Makes sure that any pending task is processed.
128 void FlushQueue(disk_cache::Backend* cache) {
129 net::TestCompletionCallback cb;
130 int rv =
131 reinterpret_cast<disk_cache::BackendImpl*>(cache)->FlushQueueForTest(
132 cb.callback());
133 cb.GetResult(rv); // Ignore the result;
136 bool CreateCache(const FilePath& path,
137 base::Thread* thread,
138 disk_cache::Backend** cache,
139 net::TestCompletionCallback* cb) {
140 int size = 1024 * 1024;
141 int rv = disk_cache::BackendImpl::CreateBackend(
142 path, false, size, net::DISK_CACHE, disk_cache::kNoRandom,
143 thread->message_loop_proxy(), NULL, cache, cb->callback());
145 return (cb->GetResult(rv) == net::OK && !(*cache)->GetEntryCount());
148 // Generates the files for an empty and one item cache.
149 int SimpleInsert(const FilePath& path, RankCrashes action,
150 base::Thread* cache_thread) {
151 net::TestCompletionCallback cb;
152 disk_cache::Backend* cache;
153 if (!CreateCache(path, cache_thread, &cache, &cb))
154 return GENERIC;
156 const char* test_name = "some other key";
158 if (action <= disk_cache::INSERT_EMPTY_3) {
159 test_name = kCrashEntryName;
160 disk_cache::g_rankings_crash = action;
163 disk_cache::Entry* entry;
164 int rv = cache->CreateEntry(test_name, &entry, cb.callback());
165 if (cb.GetResult(rv) != net::OK)
166 return GENERIC;
168 entry->Close();
169 FlushQueue(cache);
171 DCHECK(action <= disk_cache::INSERT_ONE_3);
172 disk_cache::g_rankings_crash = action;
173 test_name = kCrashEntryName;
175 rv = cache->CreateEntry(test_name, &entry, cb.callback());
176 if (cb.GetResult(rv) != net::OK)
177 return GENERIC;
179 return NOT_REACHED;
182 // Generates the files for a one item cache, and removing the head.
183 int SimpleRemove(const FilePath& path, RankCrashes action,
184 base::Thread* cache_thread) {
185 DCHECK(action >= disk_cache::REMOVE_ONE_1);
186 DCHECK(action <= disk_cache::REMOVE_TAIL_3);
188 net::TestCompletionCallback cb;
189 disk_cache::Backend* cache;
190 if (!CreateCache(path, cache_thread, &cache, &cb))
191 return GENERIC;
193 disk_cache::Entry* entry;
194 int rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
195 if (cb.GetResult(rv) != net::OK)
196 return GENERIC;
198 entry->Close();
199 FlushQueue(cache);
201 if (action >= disk_cache::REMOVE_TAIL_1) {
202 rv = cache->CreateEntry("some other key", &entry, cb.callback());
203 if (cb.GetResult(rv) != net::OK)
204 return GENERIC;
206 entry->Close();
207 FlushQueue(cache);
210 rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback());
211 if (cb.GetResult(rv) != net::OK)
212 return GENERIC;
214 disk_cache::g_rankings_crash = action;
215 entry->Doom();
216 entry->Close();
217 FlushQueue(cache);
219 return NOT_REACHED;
222 int HeadRemove(const FilePath& path, RankCrashes action,
223 base::Thread* cache_thread) {
224 DCHECK(action >= disk_cache::REMOVE_HEAD_1);
225 DCHECK(action <= disk_cache::REMOVE_HEAD_4);
227 net::TestCompletionCallback cb;
228 disk_cache::Backend* cache;
229 if (!CreateCache(path, cache_thread, &cache, &cb))
230 return GENERIC;
232 disk_cache::Entry* entry;
233 int rv = cache->CreateEntry("some other key", &entry, cb.callback());
234 if (cb.GetResult(rv) != net::OK)
235 return GENERIC;
237 entry->Close();
238 FlushQueue(cache);
239 rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
240 if (cb.GetResult(rv) != net::OK)
241 return GENERIC;
243 entry->Close();
244 FlushQueue(cache);
246 rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback());
247 if (cb.GetResult(rv) != net::OK)
248 return GENERIC;
250 disk_cache::g_rankings_crash = action;
251 entry->Doom();
252 entry->Close();
253 FlushQueue(cache);
255 return NOT_REACHED;
258 // Generates the files for insertion and removals on heavy loaded caches.
259 int LoadOperations(const FilePath& path, RankCrashes action,
260 base::Thread* cache_thread) {
261 DCHECK(action >= disk_cache::INSERT_LOAD_1);
263 // Work with a tiny index table (16 entries).
264 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
265 path, 0xf, cache_thread->message_loop_proxy(), NULL);
266 if (!cache || !cache->SetMaxSize(0x100000))
267 return GENERIC;
269 // No experiments and use a simple LRU.
270 cache->SetFlags(disk_cache::kNoRandom);
271 net::TestCompletionCallback cb;
272 int rv = cache->Init(cb.callback());
273 if (cb.GetResult(rv) != net::OK || cache->GetEntryCount())
274 return GENERIC;
276 int seed = static_cast<int>(Time::Now().ToInternalValue());
277 srand(seed);
279 disk_cache::Entry* entry;
280 for (int i = 0; i < 100; i++) {
281 std::string key = GenerateKey(true);
282 rv = cache->CreateEntry(key, &entry, cb.callback());
283 if (cb.GetResult(rv) != net::OK)
284 return GENERIC;
285 entry->Close();
286 FlushQueue(cache);
287 if (50 == i && action >= disk_cache::REMOVE_LOAD_1) {
288 rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
289 if (cb.GetResult(rv) != net::OK)
290 return GENERIC;
291 entry->Close();
292 FlushQueue(cache);
296 if (action <= disk_cache::INSERT_LOAD_2) {
297 disk_cache::g_rankings_crash = action;
299 rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback());
300 if (cb.GetResult(rv) != net::OK)
301 return GENERIC;
304 rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback());
305 if (cb.GetResult(rv) != net::OK)
306 return GENERIC;
308 disk_cache::g_rankings_crash = action;
310 entry->Doom();
311 entry->Close();
312 FlushQueue(cache);
314 return NOT_REACHED;
317 // Main function on the child process.
318 int SlaveCode(const FilePath& path, RankCrashes action) {
319 MessageLoopForIO message_loop;
321 FilePath full_path;
322 if (!CreateTargetFolder(path, action, &full_path)) {
323 printf("Destination folder found, please remove it.\n");
324 return CRASH_OVERWRITE;
327 base::Thread cache_thread("CacheThread");
328 if (!cache_thread.StartWithOptions(
329 base::Thread::Options(MessageLoop::TYPE_IO, 0)))
330 return GENERIC;
332 if (action <= disk_cache::INSERT_ONE_3)
333 return SimpleInsert(full_path, action, &cache_thread);
335 if (action <= disk_cache::INSERT_LOAD_2)
336 return LoadOperations(full_path, action, &cache_thread);
338 if (action <= disk_cache::REMOVE_ONE_4)
339 return SimpleRemove(full_path, action, &cache_thread);
341 if (action <= disk_cache::REMOVE_HEAD_4)
342 return HeadRemove(full_path, action, &cache_thread);
344 if (action <= disk_cache::REMOVE_TAIL_3)
345 return SimpleRemove(full_path, action, &cache_thread);
347 if (action <= disk_cache::REMOVE_LOAD_3)
348 return LoadOperations(full_path, action, &cache_thread);
350 return NOT_REACHED;
353 // -----------------------------------------------------------------------
355 int main(int argc, const char* argv[]) {
356 // Setup an AtExitManager so Singleton objects will be destructed.
357 base::AtExitManager at_exit_manager;
359 if (argc < 2)
360 return MasterCode();
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;
369 FilePath path;
370 PathService::Get(base::DIR_SOURCE_ROOT, &path);
371 path = path.AppendASCII("net");
372 path = path.AppendASCII("data");
373 path = path.AppendASCII("cache_tests");
374 path = path.AppendASCII("new_crashes");
376 return SlaveCode(path, action);