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.
12 #include "base/at_exit.h"
13 #include "base/command_line.h"
14 #include "base/files/file_util.h"
15 #include "base/logging.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/path_service.h"
18 #include "base/process/kill.h"
19 #include "base/process/launch.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/threading/thread.h"
24 #include "net/base/net_errors.h"
25 #include "net/base/net_export.h"
26 #include "net/base/test_completion_callback.h"
27 #include "net/disk_cache/blockfile/backend_impl.h"
28 #include "net/disk_cache/blockfile/rankings.h"
29 #include "net/disk_cache/disk_cache.h"
30 #include "net/disk_cache/disk_cache_test_util.h"
42 using disk_cache::RankCrashes
;
44 // Starts a new process, to generate the files.
45 int RunSlave(RankCrashes action
) {
47 PathService::Get(base::FILE_EXE
, &exe
);
49 base::CommandLine
cmdline(exe
);
50 cmdline
.AppendArg(base::IntToString(action
));
52 base::Process process
= base::LaunchProcess(cmdline
, base::LaunchOptions());
53 if (!process
.IsValid()) {
54 printf("Unable to run test %d\n", action
);
60 if (!process
.WaitForExit(&exit_code
)) {
61 printf("Unable to get return code, test %d\n", action
);
64 if (ALL_GOOD
!= exit_code
)
65 printf("Test %d failed, code %d\n", action
, exit_code
);
70 // Main loop for the master process.
72 for (int i
= disk_cache::NO_CRASH
+ 1; i
< disk_cache::MAX_CRASH
; i
++) {
73 int ret
= RunSlave(static_cast<RankCrashes
>(i
));
81 // -----------------------------------------------------------------------
83 namespace disk_cache
{
84 NET_EXPORT_PRIVATE
extern RankCrashes g_rankings_crash
;
87 const char kCrashEntryName
[] = "the first key";
89 // Creates the destinaton folder for this run, and returns it on full_path.
90 bool CreateTargetFolder(const base::FilePath
& path
, RankCrashes action
,
91 base::FilePath
* full_path
) {
92 const char* const folders
[] = {
117 static_assert(arraysize(folders
) == disk_cache::MAX_CRASH
, "sync folders");
118 DCHECK(action
> disk_cache::NO_CRASH
&& action
< disk_cache::MAX_CRASH
);
120 *full_path
= path
.AppendASCII(folders
[action
]);
122 if (base::PathExists(*full_path
))
125 return base::CreateDirectory(*full_path
);
128 // Makes sure that any pending task is processed.
129 void FlushQueue(disk_cache::Backend
* cache
) {
130 net::TestCompletionCallback cb
;
132 reinterpret_cast<disk_cache::BackendImpl
*>(cache
)->FlushQueueForTest(
134 cb
.GetResult(rv
); // Ignore the result;
137 bool CreateCache(const base::FilePath
& path
,
138 base::Thread
* thread
,
139 disk_cache::Backend
** cache
,
140 net::TestCompletionCallback
* cb
) {
141 int size
= 1024 * 1024;
142 disk_cache::BackendImpl
* backend
= new disk_cache::BackendImpl(
143 path
, thread
->message_loop_proxy().get(), NULL
);
144 backend
->SetMaxSize(size
);
145 backend
->SetType(net::DISK_CACHE
);
146 backend
->SetFlags(disk_cache::kNoRandom
);
147 int rv
= backend
->Init(cb
->callback());
149 return (cb
->GetResult(rv
) == net::OK
&& !(*cache
)->GetEntryCount());
152 // Generates the files for an empty and one item cache.
153 int SimpleInsert(const base::FilePath
& path
, RankCrashes action
,
154 base::Thread
* cache_thread
) {
155 net::TestCompletionCallback cb
;
156 disk_cache::Backend
* cache
;
157 if (!CreateCache(path
, cache_thread
, &cache
, &cb
))
160 const char* test_name
= "some other key";
162 if (action
<= disk_cache::INSERT_EMPTY_3
) {
163 test_name
= kCrashEntryName
;
164 disk_cache::g_rankings_crash
= action
;
167 disk_cache::Entry
* entry
;
168 int rv
= cache
->CreateEntry(test_name
, &entry
, cb
.callback());
169 if (cb
.GetResult(rv
) != net::OK
)
175 DCHECK(action
<= disk_cache::INSERT_ONE_3
);
176 disk_cache::g_rankings_crash
= action
;
177 test_name
= kCrashEntryName
;
179 rv
= cache
->CreateEntry(test_name
, &entry
, cb
.callback());
180 if (cb
.GetResult(rv
) != net::OK
)
186 // Generates the files for a one item cache, and removing the head.
187 int SimpleRemove(const base::FilePath
& path
, RankCrashes action
,
188 base::Thread
* cache_thread
) {
189 DCHECK(action
>= disk_cache::REMOVE_ONE_1
);
190 DCHECK(action
<= disk_cache::REMOVE_TAIL_3
);
192 net::TestCompletionCallback cb
;
193 disk_cache::Backend
* cache
;
194 if (!CreateCache(path
, cache_thread
, &cache
, &cb
))
197 disk_cache::Entry
* entry
;
198 int rv
= cache
->CreateEntry(kCrashEntryName
, &entry
, cb
.callback());
199 if (cb
.GetResult(rv
) != net::OK
)
205 if (action
>= disk_cache::REMOVE_TAIL_1
) {
206 rv
= cache
->CreateEntry("some other key", &entry
, cb
.callback());
207 if (cb
.GetResult(rv
) != net::OK
)
214 rv
= cache
->OpenEntry(kCrashEntryName
, &entry
, cb
.callback());
215 if (cb
.GetResult(rv
) != net::OK
)
218 disk_cache::g_rankings_crash
= action
;
226 int HeadRemove(const base::FilePath
& path
, RankCrashes action
,
227 base::Thread
* cache_thread
) {
228 DCHECK(action
>= disk_cache::REMOVE_HEAD_1
);
229 DCHECK(action
<= disk_cache::REMOVE_HEAD_4
);
231 net::TestCompletionCallback cb
;
232 disk_cache::Backend
* cache
;
233 if (!CreateCache(path
, cache_thread
, &cache
, &cb
))
236 disk_cache::Entry
* entry
;
237 int rv
= cache
->CreateEntry("some other key", &entry
, cb
.callback());
238 if (cb
.GetResult(rv
) != net::OK
)
243 rv
= cache
->CreateEntry(kCrashEntryName
, &entry
, cb
.callback());
244 if (cb
.GetResult(rv
) != net::OK
)
250 rv
= cache
->OpenEntry(kCrashEntryName
, &entry
, cb
.callback());
251 if (cb
.GetResult(rv
) != net::OK
)
254 disk_cache::g_rankings_crash
= action
;
262 // Generates the files for insertion and removals on heavy loaded caches.
263 int LoadOperations(const base::FilePath
& path
, RankCrashes action
,
264 base::Thread
* cache_thread
) {
265 DCHECK(action
>= disk_cache::INSERT_LOAD_1
);
267 // Work with a tiny index table (16 entries).
268 disk_cache::BackendImpl
* cache
= new disk_cache::BackendImpl(
269 path
, 0xf, cache_thread
->message_loop_proxy().get(), NULL
);
270 if (!cache
->SetMaxSize(0x100000))
273 // No experiments and use a simple LRU.
274 cache
->SetFlags(disk_cache::kNoRandom
);
275 net::TestCompletionCallback cb
;
276 int rv
= cache
->Init(cb
.callback());
277 if (cb
.GetResult(rv
) != net::OK
|| cache
->GetEntryCount())
280 int seed
= static_cast<int>(Time::Now().ToInternalValue());
283 disk_cache::Entry
* entry
;
284 for (int i
= 0; i
< 100; i
++) {
285 std::string key
= GenerateKey(true);
286 rv
= cache
->CreateEntry(key
, &entry
, cb
.callback());
287 if (cb
.GetResult(rv
) != net::OK
)
291 if (50 == i
&& action
>= disk_cache::REMOVE_LOAD_1
) {
292 rv
= cache
->CreateEntry(kCrashEntryName
, &entry
, cb
.callback());
293 if (cb
.GetResult(rv
) != net::OK
)
300 if (action
<= disk_cache::INSERT_LOAD_2
) {
301 disk_cache::g_rankings_crash
= action
;
303 rv
= cache
->CreateEntry(kCrashEntryName
, &entry
, cb
.callback());
304 if (cb
.GetResult(rv
) != net::OK
)
308 rv
= cache
->OpenEntry(kCrashEntryName
, &entry
, cb
.callback());
309 if (cb
.GetResult(rv
) != net::OK
)
312 disk_cache::g_rankings_crash
= action
;
321 // Main function on the child process.
322 int SlaveCode(const base::FilePath
& path
, RankCrashes action
) {
323 base::MessageLoopForIO message_loop
;
325 base::FilePath full_path
;
326 if (!CreateTargetFolder(path
, action
, &full_path
)) {
327 printf("Destination folder found, please remove it.\n");
328 return CRASH_OVERWRITE
;
331 base::Thread
cache_thread("CacheThread");
332 if (!cache_thread
.StartWithOptions(
333 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0)))
336 if (action
<= disk_cache::INSERT_ONE_3
)
337 return SimpleInsert(full_path
, action
, &cache_thread
);
339 if (action
<= disk_cache::INSERT_LOAD_2
)
340 return LoadOperations(full_path
, action
, &cache_thread
);
342 if (action
<= disk_cache::REMOVE_ONE_4
)
343 return SimpleRemove(full_path
, action
, &cache_thread
);
345 if (action
<= disk_cache::REMOVE_HEAD_4
)
346 return HeadRemove(full_path
, action
, &cache_thread
);
348 if (action
<= disk_cache::REMOVE_TAIL_3
)
349 return SimpleRemove(full_path
, action
, &cache_thread
);
351 if (action
<= disk_cache::REMOVE_LOAD_3
)
352 return LoadOperations(full_path
, action
, &cache_thread
);
357 // -----------------------------------------------------------------------
359 int main(int argc
, const char* argv
[]) {
360 // Setup an AtExitManager so Singleton objects will be destructed.
361 base::AtExitManager at_exit_manager
;
367 RankCrashes action
= static_cast<RankCrashes
>(strtol(argv
[1], &end
, 0));
368 if (action
<= disk_cache::NO_CRASH
|| action
>= disk_cache::MAX_CRASH
) {
369 printf("Invalid action\n");
370 return INVALID_ARGUMENT
;
374 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
375 path
= path
.AppendASCII("net");
376 path
= path
.AppendASCII("data");
377 path
= path
.AppendASCII("cache_tests");
378 path
= path
.AppendASCII("new_crashes");
380 return SlaveCode(path
, action
);