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/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"
41 using disk_cache::RankCrashes
;
43 // Starts a new process, to generate the files.
44 int RunSlave(RankCrashes action
) {
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
);
59 if (!base::WaitForExitCode(handle
, &exit_code
)) {
60 printf("Unable to get return code, test %d\n", action
);
63 if (ALL_GOOD
!= exit_code
)
64 printf("Test %d failed, code %d\n", action
, exit_code
);
69 // Main loop for the master process.
71 for (int i
= disk_cache::NO_CRASH
+ 1; i
< disk_cache::MAX_CRASH
; i
++) {
72 int ret
= RunSlave(static_cast<RankCrashes
>(i
));
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
[] = {
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
))
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
;
131 reinterpret_cast<disk_cache::BackendImpl
*>(cache
)->FlushQueueForTest(
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
))
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
)
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
)
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
))
193 disk_cache::Entry
* entry
;
194 int rv
= cache
->CreateEntry(kCrashEntryName
, &entry
, cb
.callback());
195 if (cb
.GetResult(rv
) != net::OK
)
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
)
210 rv
= cache
->OpenEntry(kCrashEntryName
, &entry
, cb
.callback());
211 if (cb
.GetResult(rv
) != net::OK
)
214 disk_cache::g_rankings_crash
= action
;
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
))
232 disk_cache::Entry
* entry
;
233 int rv
= cache
->CreateEntry("some other key", &entry
, cb
.callback());
234 if (cb
.GetResult(rv
) != net::OK
)
239 rv
= cache
->CreateEntry(kCrashEntryName
, &entry
, cb
.callback());
240 if (cb
.GetResult(rv
) != net::OK
)
246 rv
= cache
->OpenEntry(kCrashEntryName
, &entry
, cb
.callback());
247 if (cb
.GetResult(rv
) != net::OK
)
250 disk_cache::g_rankings_crash
= action
;
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))
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())
276 int seed
= static_cast<int>(Time::Now().ToInternalValue());
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
)
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
)
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
)
304 rv
= cache
->OpenEntry(kCrashEntryName
, &entry
, cb
.callback());
305 if (cb
.GetResult(rv
) != net::OK
)
308 disk_cache::g_rankings_crash
= action
;
317 // Main function on the child process.
318 int SlaveCode(const FilePath
& path
, RankCrashes action
) {
319 MessageLoopForIO message_loop
;
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)))
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
);
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
;
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
;
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
);