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/message_loop.h"
17 #include "base/path_service.h"
18 #include "base/process/kill.h"
19 #include "base/process/launch.h"
20 #include "base/process/process_handle.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_util.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/threading/thread.h"
25 #include "net/base/net_errors.h"
26 #include "net/base/net_export.h"
27 #include "net/base/test_completion_callback.h"
28 #include "net/disk_cache/backend_impl.h"
29 #include "net/disk_cache/disk_cache.h"
30 #include "net/disk_cache/disk_cache_test_util.h"
31 #include "net/disk_cache/rankings.h"
43 using disk_cache::RankCrashes
;
45 // Starts a new process, to generate the files.
46 int RunSlave(RankCrashes action
) {
48 PathService::Get(base::FILE_EXE
, &exe
);
50 CommandLine
cmdline(exe
);
51 cmdline
.AppendArg(base::IntToString(action
));
53 base::ProcessHandle handle
;
54 if (!base::LaunchProcess(cmdline
, base::LaunchOptions(), &handle
)) {
55 printf("Unable to run test %d\n", action
);
61 if (!base::WaitForExitCode(handle
, &exit_code
)) {
62 printf("Unable to get return code, test %d\n", action
);
65 if (ALL_GOOD
!= exit_code
)
66 printf("Test %d failed, code %d\n", action
, exit_code
);
71 // Main loop for the master process.
73 for (int i
= disk_cache::NO_CRASH
+ 1; i
< disk_cache::MAX_CRASH
; i
++) {
74 int ret
= RunSlave(static_cast<RankCrashes
>(i
));
82 // -----------------------------------------------------------------------
84 namespace disk_cache
{
85 NET_EXPORT_PRIVATE
extern RankCrashes g_rankings_crash
;
88 const char* kCrashEntryName
= "the first key";
90 // Creates the destinaton folder for this run, and returns it on full_path.
91 bool CreateTargetFolder(const base::FilePath
& path
, RankCrashes action
,
92 base::FilePath
* full_path
) {
93 const char* folders
[] = {
118 COMPILE_ASSERT(arraysize(folders
) == disk_cache::MAX_CRASH
, sync_folders
);
119 DCHECK(action
> disk_cache::NO_CRASH
&& action
< disk_cache::MAX_CRASH
);
121 *full_path
= path
.AppendASCII(folders
[action
]);
123 if (base::PathExists(*full_path
))
126 return base::CreateDirectory(*full_path
);
129 // Makes sure that any pending task is processed.
130 void FlushQueue(disk_cache::Backend
* cache
) {
131 net::TestCompletionCallback cb
;
133 reinterpret_cast<disk_cache::BackendImpl
*>(cache
)->FlushQueueForTest(
135 cb
.GetResult(rv
); // Ignore the result;
138 bool CreateCache(const base::FilePath
& path
,
139 base::Thread
* thread
,
140 disk_cache::Backend
** cache
,
141 net::TestCompletionCallback
* cb
) {
142 int size
= 1024 * 1024;
143 disk_cache::BackendImpl
* backend
= new disk_cache::BackendImpl(
144 path
, thread
->message_loop_proxy().get(), NULL
);
145 backend
->SetMaxSize(size
);
146 backend
->SetType(net::DISK_CACHE
);
147 backend
->SetFlags(disk_cache::kNoRandom
);
148 int rv
= backend
->Init(cb
->callback());
150 return (cb
->GetResult(rv
) == net::OK
&& !(*cache
)->GetEntryCount());
153 // Generates the files for an empty and one item cache.
154 int SimpleInsert(const base::FilePath
& path
, RankCrashes action
,
155 base::Thread
* cache_thread
) {
156 net::TestCompletionCallback cb
;
157 disk_cache::Backend
* cache
;
158 if (!CreateCache(path
, cache_thread
, &cache
, &cb
))
161 const char* test_name
= "some other key";
163 if (action
<= disk_cache::INSERT_EMPTY_3
) {
164 test_name
= kCrashEntryName
;
165 disk_cache::g_rankings_crash
= action
;
168 disk_cache::Entry
* entry
;
169 int rv
= cache
->CreateEntry(test_name
, &entry
, cb
.callback());
170 if (cb
.GetResult(rv
) != net::OK
)
176 DCHECK(action
<= disk_cache::INSERT_ONE_3
);
177 disk_cache::g_rankings_crash
= action
;
178 test_name
= kCrashEntryName
;
180 rv
= cache
->CreateEntry(test_name
, &entry
, cb
.callback());
181 if (cb
.GetResult(rv
) != net::OK
)
187 // Generates the files for a one item cache, and removing the head.
188 int SimpleRemove(const base::FilePath
& path
, RankCrashes action
,
189 base::Thread
* cache_thread
) {
190 DCHECK(action
>= disk_cache::REMOVE_ONE_1
);
191 DCHECK(action
<= disk_cache::REMOVE_TAIL_3
);
193 net::TestCompletionCallback cb
;
194 disk_cache::Backend
* cache
;
195 if (!CreateCache(path
, cache_thread
, &cache
, &cb
))
198 disk_cache::Entry
* entry
;
199 int rv
= cache
->CreateEntry(kCrashEntryName
, &entry
, cb
.callback());
200 if (cb
.GetResult(rv
) != net::OK
)
206 if (action
>= disk_cache::REMOVE_TAIL_1
) {
207 rv
= cache
->CreateEntry("some other key", &entry
, cb
.callback());
208 if (cb
.GetResult(rv
) != net::OK
)
215 rv
= cache
->OpenEntry(kCrashEntryName
, &entry
, cb
.callback());
216 if (cb
.GetResult(rv
) != net::OK
)
219 disk_cache::g_rankings_crash
= action
;
227 int HeadRemove(const base::FilePath
& path
, RankCrashes action
,
228 base::Thread
* cache_thread
) {
229 DCHECK(action
>= disk_cache::REMOVE_HEAD_1
);
230 DCHECK(action
<= disk_cache::REMOVE_HEAD_4
);
232 net::TestCompletionCallback cb
;
233 disk_cache::Backend
* cache
;
234 if (!CreateCache(path
, cache_thread
, &cache
, &cb
))
237 disk_cache::Entry
* entry
;
238 int rv
= cache
->CreateEntry("some other key", &entry
, cb
.callback());
239 if (cb
.GetResult(rv
) != net::OK
)
244 rv
= cache
->CreateEntry(kCrashEntryName
, &entry
, cb
.callback());
245 if (cb
.GetResult(rv
) != net::OK
)
251 rv
= cache
->OpenEntry(kCrashEntryName
, &entry
, cb
.callback());
252 if (cb
.GetResult(rv
) != net::OK
)
255 disk_cache::g_rankings_crash
= action
;
263 // Generates the files for insertion and removals on heavy loaded caches.
264 int LoadOperations(const base::FilePath
& path
, RankCrashes action
,
265 base::Thread
* cache_thread
) {
266 DCHECK(action
>= disk_cache::INSERT_LOAD_1
);
268 // Work with a tiny index table (16 entries).
269 disk_cache::BackendImpl
* cache
= new disk_cache::BackendImpl(
270 path
, 0xf, cache_thread
->message_loop_proxy().get(), NULL
);
271 if (!cache
->SetMaxSize(0x100000))
274 // No experiments and use a simple LRU.
275 cache
->SetFlags(disk_cache::kNoRandom
);
276 net::TestCompletionCallback cb
;
277 int rv
= cache
->Init(cb
.callback());
278 if (cb
.GetResult(rv
) != net::OK
|| cache
->GetEntryCount())
281 int seed
= static_cast<int>(Time::Now().ToInternalValue());
284 disk_cache::Entry
* entry
;
285 for (int i
= 0; i
< 100; i
++) {
286 std::string key
= GenerateKey(true);
287 rv
= cache
->CreateEntry(key
, &entry
, cb
.callback());
288 if (cb
.GetResult(rv
) != net::OK
)
292 if (50 == i
&& action
>= disk_cache::REMOVE_LOAD_1
) {
293 rv
= cache
->CreateEntry(kCrashEntryName
, &entry
, cb
.callback());
294 if (cb
.GetResult(rv
) != net::OK
)
301 if (action
<= disk_cache::INSERT_LOAD_2
) {
302 disk_cache::g_rankings_crash
= action
;
304 rv
= cache
->CreateEntry(kCrashEntryName
, &entry
, cb
.callback());
305 if (cb
.GetResult(rv
) != net::OK
)
309 rv
= cache
->OpenEntry(kCrashEntryName
, &entry
, cb
.callback());
310 if (cb
.GetResult(rv
) != net::OK
)
313 disk_cache::g_rankings_crash
= action
;
322 // Main function on the child process.
323 int SlaveCode(const base::FilePath
& path
, RankCrashes action
) {
324 base::MessageLoopForIO message_loop
;
326 base::FilePath full_path
;
327 if (!CreateTargetFolder(path
, action
, &full_path
)) {
328 printf("Destination folder found, please remove it.\n");
329 return CRASH_OVERWRITE
;
332 base::Thread
cache_thread("CacheThread");
333 if (!cache_thread
.StartWithOptions(
334 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0)))
337 if (action
<= disk_cache::INSERT_ONE_3
)
338 return SimpleInsert(full_path
, action
, &cache_thread
);
340 if (action
<= disk_cache::INSERT_LOAD_2
)
341 return LoadOperations(full_path
, action
, &cache_thread
);
343 if (action
<= disk_cache::REMOVE_ONE_4
)
344 return SimpleRemove(full_path
, action
, &cache_thread
);
346 if (action
<= disk_cache::REMOVE_HEAD_4
)
347 return HeadRemove(full_path
, action
, &cache_thread
);
349 if (action
<= disk_cache::REMOVE_TAIL_3
)
350 return SimpleRemove(full_path
, action
, &cache_thread
);
352 if (action
<= disk_cache::REMOVE_LOAD_3
)
353 return LoadOperations(full_path
, action
, &cache_thread
);
358 // -----------------------------------------------------------------------
360 int main(int argc
, const char* argv
[]) {
361 // Setup an AtExitManager so Singleton objects will be destructed.
362 base::AtExitManager at_exit_manager
;
368 RankCrashes action
= static_cast<RankCrashes
>(strtol(argv
[1], &end
, 0));
369 if (action
<= disk_cache::NO_CRASH
|| action
>= disk_cache::MAX_CRASH
) {
370 printf("Invalid action\n");
371 return INVALID_ARGUMENT
;
375 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
376 path
= path
.AppendASCII("net");
377 path
= path
.AppendASCII("data");
378 path
= path
.AppendASCII("cache_tests");
379 path
= path
.AppendASCII("new_crashes");
381 return SlaveCode(path
, action
);