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 #include "net/disk_cache/disk_cache_test_util.h"
7 #include "base/files/file.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "net/base/net_errors.h"
12 #include "net/disk_cache/blockfile/backend_impl.h"
13 #include "net/disk_cache/blockfile/file.h"
14 #include "net/disk_cache/cache_util.h"
17 using base::TimeDelta
;
19 std::string
GenerateKey(bool same_length
) {
21 CacheTestFillBuffer(key
, sizeof(key
), same_length
);
24 return std::string(key
);
27 void CacheTestFillBuffer(char* buffer
, size_t len
, bool no_nulls
) {
28 static bool called
= false;
31 int seed
= static_cast<int>(Time::Now().ToInternalValue());
35 for (size_t i
= 0; i
< len
; i
++) {
36 buffer
[i
] = static_cast<char>(rand());
37 if (!buffer
[i
] && no_nulls
)
40 if (len
&& !buffer
[0])
44 bool CreateCacheTestFile(const base::FilePath
& name
) {
45 int flags
= base::File::FLAG_CREATE_ALWAYS
| base::File::FLAG_READ
|
46 base::File::FLAG_WRITE
;
48 base::File
file(name
, flags
);
52 file
.SetLength(4 * 1024 * 1024);
56 bool DeleteCache(const base::FilePath
& path
) {
57 disk_cache::DeleteCache(path
, false);
61 bool CheckCacheIntegrity(const base::FilePath
& path
, bool new_eviction
,
63 scoped_ptr
<disk_cache::BackendImpl
> cache(new disk_cache::BackendImpl(
64 path
, mask
, base::ThreadTaskRunnerHandle::Get(), NULL
));
68 cache
->SetNewEviction();
69 cache
->SetFlags(disk_cache::kNoRandom
);
70 if (cache
->SyncInit() != net::OK
)
72 return cache
->SelfCheck() >= 0;
75 // -----------------------------------------------------------------------
77 MessageLoopHelper::MessageLoopHelper()
82 callback_reused_error_(false),
83 callbacks_called_(0) {
86 MessageLoopHelper::~MessageLoopHelper() {
89 bool MessageLoopHelper::WaitUntilCacheIoFinished(int num_callbacks
) {
90 if (num_callbacks
== callbacks_called_
)
93 ExpectCallbacks(num_callbacks
);
94 // Create a recurrent timer of 50 mS.
95 if (!timer_
.IsRunning())
96 timer_
.Start(FROM_HERE
, TimeDelta::FromMilliseconds(50), this,
97 &MessageLoopHelper::TimerExpired
);
98 base::MessageLoop::current()->Run();
102 // Quits the message loop when all callbacks are called or we've been waiting
103 // too long for them (2 secs without a callback).
104 void MessageLoopHelper::TimerExpired() {
105 CHECK_LE(callbacks_called_
, num_callbacks_
);
106 if (callbacks_called_
== num_callbacks_
) {
108 base::MessageLoop::current()->Quit();
110 // Not finished yet. See if we have to abort.
111 if (last_
== callbacks_called_
)
114 last_
= callbacks_called_
;
115 if (40 == num_iterations_
)
116 base::MessageLoop::current()->Quit();
120 // -----------------------------------------------------------------------
122 CallbackTest::CallbackTest(MessageLoopHelper
* helper
,
125 reuse_(reuse
? 0 : 1) {
128 CallbackTest::~CallbackTest() {
131 // On the actual callback, increase the number of tests received and check for
132 // errors (an unexpected test received)
133 void CallbackTest::Run(int result
) {
134 last_result_
= result
;
137 DCHECK_EQ(1, reuse_
);
139 helper_
->set_callback_reused_error(true);
143 helper_
->CallbackWasCalled();