[email protected] | adda869 | 2011-07-14 20:55:30 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 10 | #include <string> |
| 11 | |
[email protected] | 9b7e449 | 2008-08-14 21:11:49 | [diff] [blame] | 12 | #include "base/at_exit.h" |
[email protected] | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 13 | #include "base/command_line.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 14 | #include "base/file_util.h" |
| 15 | #include "base/logging.h" |
| 16 | #include "base/message_loop.h" |
| 17 | #include "base/path_service.h" |
[email protected] | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 18 | #include "base/process_util.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 19 | #include "base/strings/string_number_conversions.h" |
| 20 | #include "base/strings/string_util.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 22 | #include "base/threading/thread.h" |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 23 | #include "net/base/net_errors.h" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 24 | #include "net/base/net_export.h" |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 25 | #include "net/base/test_completion_callback.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 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" |
| 30 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 31 | using base::Time; |
| 32 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | enum Errors { |
| 34 | GENERIC = -1, |
| 35 | ALL_GOOD = 0, |
| 36 | INVALID_ARGUMENT = 1, |
| 37 | CRASH_OVERWRITE, |
| 38 | NOT_REACHED |
| 39 | }; |
| 40 | |
| 41 | using disk_cache::RankCrashes; |
| 42 | |
| 43 | // Starts a new process, to generate the files. |
| 44 | int RunSlave(RankCrashes action) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 45 | base::FilePath exe; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 46 | PathService::Get(base::FILE_EXE, &exe); |
| 47 | |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 48 | CommandLine cmdline(exe); |
[email protected] | 0445eb4 | 2010-08-13 22:10:30 | [diff] [blame] | 49 | cmdline.AppendArg(base::IntToString(action)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 50 | |
[email protected] | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 51 | base::ProcessHandle handle; |
[email protected] | e599218 | 2011-07-15 16:47:02 | [diff] [blame] | 52 | if (!base::LaunchProcess(cmdline, base::LaunchOptions(), &handle)) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 53 | printf("Unable to run test %d\n", action); |
| 54 | return GENERIC; |
| 55 | } |
| 56 | |
[email protected] | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 57 | int exit_code; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | |
[email protected] | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 59 | if (!base::WaitForExitCode(handle, &exit_code)) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 60 | printf("Unable to get return code, test %d\n", action); |
| 61 | return GENERIC; |
| 62 | } |
[email protected] | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 63 | if (ALL_GOOD != exit_code) |
| 64 | printf("Test %d failed, code %d\n", action, exit_code); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 65 | |
[email protected] | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 66 | return exit_code; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 67 | } |
| 68 | |
| 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; |
| 75 | } |
| 76 | |
| 77 | return ALL_GOOD; |
| 78 | } |
| 79 | |
| 80 | // ----------------------------------------------------------------------- |
| 81 | |
[email protected] | 77b42dd | 2011-05-07 01:04:04 | [diff] [blame] | 82 | namespace disk_cache { |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 83 | NET_EXPORT_PRIVATE extern RankCrashes g_rankings_crash; |
[email protected] | 77b42dd | 2011-05-07 01:04:04 | [diff] [blame] | 84 | } |
| 85 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 86 | const char* kCrashEntryName = "the first key"; |
| 87 | |
| 88 | // Creates the destinaton folder for this run, and returns it on full_path. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 89 | bool CreateTargetFolder(const base::FilePath& path, RankCrashes action, |
| 90 | base::FilePath* full_path) { |
[email protected] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 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" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 115 | }; |
| 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] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 119 | *full_path = path.AppendASCII(folders[action]); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 120 | |
[email protected] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 121 | if (file_util::PathExists(*full_path)) |
[email protected] | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 122 | return false; |
| 123 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 124 | return file_util::CreateDirectory(*full_path); |
| 125 | } |
| 126 | |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 127 | // Makes sure that any pending task is processed. |
| 128 | void FlushQueue(disk_cache::Backend* cache) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 129 | net::TestCompletionCallback cb; |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 130 | int rv = |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 131 | reinterpret_cast<disk_cache::BackendImpl*>(cache)->FlushQueueForTest( |
| 132 | cb.callback()); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 133 | cb.GetResult(rv); // Ignore the result; |
| 134 | } |
| 135 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 136 | bool CreateCache(const base::FilePath& path, |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 137 | base::Thread* thread, |
| 138 | disk_cache::Backend** cache, |
| 139 | net::TestCompletionCallback* cb) { |
[email protected] | 3a0ab94e | 2011-11-29 18:38:56 | [diff] [blame] | 140 | int size = 1024 * 1024; |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 141 | disk_cache::BackendImpl* backend = new disk_cache::BackendImpl( |
| 142 | path, thread->message_loop_proxy().get(), NULL); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 143 | 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] | 3a0ab94e | 2011-11-29 18:38:56 | [diff] [blame] | 148 | return (cb->GetResult(rv) == net::OK && !(*cache)->GetEntryCount()); |
| 149 | } |
| 150 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 151 | // Generates the files for an empty and one item cache. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 152 | int SimpleInsert(const base::FilePath& path, RankCrashes action, |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 153 | base::Thread* cache_thread) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 154 | net::TestCompletionCallback cb; |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 155 | disk_cache::Backend* cache; |
[email protected] | 3a0ab94e | 2011-11-29 18:38:56 | [diff] [blame] | 156 | if (!CreateCache(path, cache_thread, &cache, &cb)) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 157 | 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] | 77b42dd | 2011-05-07 01:04:04 | [diff] [blame] | 163 | disk_cache::g_rankings_crash = action; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | disk_cache::Entry* entry; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 167 | int rv = cache->CreateEntry(test_name, &entry, cb.callback()); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 168 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 169 | return GENERIC; |
| 170 | |
| 171 | entry->Close(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 172 | FlushQueue(cache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 173 | |
| 174 | DCHECK(action <= disk_cache::INSERT_ONE_3); |
[email protected] | 77b42dd | 2011-05-07 01:04:04 | [diff] [blame] | 175 | disk_cache::g_rankings_crash = action; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 176 | test_name = kCrashEntryName; |
| 177 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 178 | rv = cache->CreateEntry(test_name, &entry, cb.callback()); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 179 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 180 | return GENERIC; |
| 181 | |
| 182 | return NOT_REACHED; |
| 183 | } |
| 184 | |
| 185 | // Generates the files for a one item cache, and removing the head. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 186 | int SimpleRemove(const base::FilePath& path, RankCrashes action, |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 187 | base::Thread* cache_thread) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 188 | DCHECK(action >= disk_cache::REMOVE_ONE_1); |
| 189 | DCHECK(action <= disk_cache::REMOVE_TAIL_3); |
| 190 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 191 | net::TestCompletionCallback cb; |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 192 | disk_cache::Backend* cache; |
[email protected] | 3a0ab94e | 2011-11-29 18:38:56 | [diff] [blame] | 193 | if (!CreateCache(path, cache_thread, &cache, &cb)) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 194 | return GENERIC; |
| 195 | |
| 196 | disk_cache::Entry* entry; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 197 | int rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback()); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 198 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 199 | return GENERIC; |
| 200 | |
| 201 | entry->Close(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 202 | FlushQueue(cache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 203 | |
| 204 | if (action >= disk_cache::REMOVE_TAIL_1) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 205 | rv = cache->CreateEntry("some other key", &entry, cb.callback()); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 206 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 207 | return GENERIC; |
| 208 | |
| 209 | entry->Close(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 210 | FlushQueue(cache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 211 | } |
| 212 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 213 | rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback()); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 214 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 215 | return GENERIC; |
| 216 | |
[email protected] | 77b42dd | 2011-05-07 01:04:04 | [diff] [blame] | 217 | disk_cache::g_rankings_crash = action; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 218 | entry->Doom(); |
| 219 | entry->Close(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 220 | FlushQueue(cache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 221 | |
| 222 | return NOT_REACHED; |
| 223 | } |
| 224 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 225 | int HeadRemove(const base::FilePath& path, RankCrashes action, |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 226 | base::Thread* cache_thread) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 227 | DCHECK(action >= disk_cache::REMOVE_HEAD_1); |
| 228 | DCHECK(action <= disk_cache::REMOVE_HEAD_4); |
| 229 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 230 | net::TestCompletionCallback cb; |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 231 | disk_cache::Backend* cache; |
[email protected] | 3a0ab94e | 2011-11-29 18:38:56 | [diff] [blame] | 232 | if (!CreateCache(path, cache_thread, &cache, &cb)) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 233 | return GENERIC; |
| 234 | |
| 235 | disk_cache::Entry* entry; |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 236 | int rv = cache->CreateEntry("some other key", &entry, cb.callback()); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 237 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 238 | return GENERIC; |
| 239 | |
| 240 | entry->Close(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 241 | FlushQueue(cache); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 242 | rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback()); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 243 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 244 | return GENERIC; |
| 245 | |
| 246 | entry->Close(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 247 | FlushQueue(cache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 248 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 249 | rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback()); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 250 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 251 | return GENERIC; |
| 252 | |
[email protected] | 77b42dd | 2011-05-07 01:04:04 | [diff] [blame] | 253 | disk_cache::g_rankings_crash = action; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 254 | entry->Doom(); |
| 255 | entry->Close(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 256 | FlushQueue(cache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 257 | |
| 258 | return NOT_REACHED; |
| 259 | } |
| 260 | |
| 261 | // Generates the files for insertion and removals on heavy loaded caches. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 262 | int LoadOperations(const base::FilePath& path, RankCrashes action, |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 263 | base::Thread* cache_thread) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 264 | DCHECK(action >= disk_cache::INSERT_LOAD_1); |
| 265 | |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 266 | // Work with a tiny index table (16 entries). |
| 267 | disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 268 | path, 0xf, cache_thread->message_loop_proxy().get(), NULL); |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 269 | if (!cache || !cache->SetMaxSize(0x100000)) |
| 270 | return GENERIC; |
| 271 | |
[email protected] | 3a0ab94e | 2011-11-29 18:38:56 | [diff] [blame] | 272 | // No experiments and use a simple LRU. |
| 273 | cache->SetFlags(disk_cache::kNoRandom); |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 274 | net::TestCompletionCallback cb; |
| 275 | int rv = cache->Init(cb.callback()); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 276 | if (cb.GetResult(rv) != net::OK || cache->GetEntryCount()) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 277 | 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] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 285 | rv = cache->CreateEntry(key, &entry, cb.callback()); |
| 286 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 287 | return GENERIC; |
| 288 | entry->Close(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 289 | FlushQueue(cache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 290 | if (50 == i && action >= disk_cache::REMOVE_LOAD_1) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 291 | rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback()); |
| 292 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 293 | return GENERIC; |
| 294 | entry->Close(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 295 | FlushQueue(cache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | if (action <= disk_cache::INSERT_LOAD_2) { |
[email protected] | 77b42dd | 2011-05-07 01:04:04 | [diff] [blame] | 300 | disk_cache::g_rankings_crash = action; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 301 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 302 | rv = cache->CreateEntry(kCrashEntryName, &entry, cb.callback()); |
| 303 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 304 | return GENERIC; |
| 305 | } |
| 306 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 307 | rv = cache->OpenEntry(kCrashEntryName, &entry, cb.callback()); |
| 308 | if (cb.GetResult(rv) != net::OK) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 309 | return GENERIC; |
| 310 | |
[email protected] | 77b42dd | 2011-05-07 01:04:04 | [diff] [blame] | 311 | disk_cache::g_rankings_crash = action; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 312 | |
| 313 | entry->Doom(); |
| 314 | entry->Close(); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 315 | FlushQueue(cache); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 316 | |
| 317 | return NOT_REACHED; |
| 318 | } |
| 319 | |
| 320 | // Main function on the child process. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 321 | int SlaveCode(const base::FilePath& path, RankCrashes action) { |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 322 | base::MessageLoopForIO message_loop; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 323 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 324 | base::FilePath full_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 325 | if (!CreateTargetFolder(path, action, &full_path)) { |
| 326 | printf("Destination folder found, please remove it.\n"); |
| 327 | return CRASH_OVERWRITE; |
| 328 | } |
| 329 | |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 330 | base::Thread cache_thread("CacheThread"); |
| 331 | if (!cache_thread.StartWithOptions( |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 332 | base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 333 | return GENERIC; |
| 334 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 335 | if (action <= disk_cache::INSERT_ONE_3) |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 336 | return SimpleInsert(full_path, action, &cache_thread); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 337 | |
| 338 | if (action <= disk_cache::INSERT_LOAD_2) |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 339 | return LoadOperations(full_path, action, &cache_thread); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 340 | |
| 341 | if (action <= disk_cache::REMOVE_ONE_4) |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 342 | return SimpleRemove(full_path, action, &cache_thread); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 343 | |
| 344 | if (action <= disk_cache::REMOVE_HEAD_4) |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 345 | return HeadRemove(full_path, action, &cache_thread); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 346 | |
| 347 | if (action <= disk_cache::REMOVE_TAIL_3) |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 348 | return SimpleRemove(full_path, action, &cache_thread); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 349 | |
| 350 | if (action <= disk_cache::REMOVE_LOAD_3) |
[email protected] | ec44a9a | 2010-06-15 19:31:51 | [diff] [blame] | 351 | return LoadOperations(full_path, action, &cache_thread); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 352 | |
| 353 | return NOT_REACHED; |
| 354 | } |
| 355 | |
| 356 | // ----------------------------------------------------------------------- |
| 357 | |
| 358 | int main(int argc, const char* argv[]) { |
[email protected] | 9b7e449 | 2008-08-14 21:11:49 | [diff] [blame] | 359 | // Setup an AtExitManager so Singleton objects will be destructed. |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 360 | base::AtExitManager at_exit_manager; |
[email protected] | 9b7e449 | 2008-08-14 21:11:49 | [diff] [blame] | 361 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 362 | 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] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 372 | base::FilePath path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 373 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 374 | path = path.AppendASCII("net"); |
| 375 | path = path.AppendASCII("data"); |
| 376 | path = path.AppendASCII("cache_tests"); |
| 377 | path = path.AppendASCII("new_crashes"); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 378 | |
[email protected] | 5306d11 | 2009-10-15 17:38:04 | [diff] [blame] | 379 | return SlaveCode(path, action); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 380 | } |