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/logging.h"
8 #include "base/file_util.h"
9 #include "base/message_loop_proxy.h"
10 #include "base/path_service.h"
11 #include "net/base/net_errors.h"
12 #include "net/disk_cache/backend_impl.h"
13 #include "net/disk_cache/cache_util.h"
14 #include "net/disk_cache/file.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::PLATFORM_FILE_CREATE_ALWAYS
|
46 base::PLATFORM_FILE_READ
|
47 base::PLATFORM_FILE_WRITE
;
49 scoped_refptr
<disk_cache::File
> file(new disk_cache::File(
50 base::CreatePlatformFile(name
, flags
, NULL
, NULL
)));
54 file
->SetLength(4 * 1024 * 1024);
58 bool DeleteCache(const base::FilePath
& path
) {
59 disk_cache::DeleteCache(path
, false);
63 bool CheckCacheIntegrity(const base::FilePath
& path
, bool new_eviction
,
65 scoped_ptr
<disk_cache::BackendImpl
> cache(new disk_cache::BackendImpl(
66 path
, mask
, base::MessageLoopProxy::current(), NULL
));
70 cache
->SetNewEviction();
71 cache
->SetFlags(disk_cache::kNoRandom
);
72 if (cache
->SyncInit() != net::OK
)
74 return cache
->SelfCheck() >= 0;
77 // -----------------------------------------------------------------------
79 MessageLoopHelper::MessageLoopHelper()
84 callback_reused_error_(false),
85 callbacks_called_(0) {
88 MessageLoopHelper::~MessageLoopHelper() {
91 bool MessageLoopHelper::WaitUntilCacheIoFinished(int num_callbacks
) {
92 if (num_callbacks
== callbacks_called_
)
95 ExpectCallbacks(num_callbacks
);
96 // Create a recurrent timer of 50 mS.
97 if (!timer_
.IsRunning())
98 timer_
.Start(FROM_HERE
, TimeDelta::FromMilliseconds(50), this,
99 &MessageLoopHelper::TimerExpired
);
100 MessageLoop::current()->Run();
104 // Quits the message loop when all callbacks are called or we've been waiting
105 // too long for them (2 secs without a callback).
106 void MessageLoopHelper::TimerExpired() {
107 CHECK_LE(callbacks_called_
, num_callbacks_
);
108 if (callbacks_called_
== num_callbacks_
) {
110 MessageLoop::current()->Quit();
112 // Not finished yet. See if we have to abort.
113 if (last_
== callbacks_called_
)
116 last_
= callbacks_called_
;
117 if (40 == num_iterations_
)
118 MessageLoop::current()->Quit();
122 // -----------------------------------------------------------------------
124 CallbackTest::CallbackTest(MessageLoopHelper
* helper
,
127 reuse_(reuse
? 0 : 1) {
130 CallbackTest::~CallbackTest() {
133 // On the actual callback, increase the number of tests received and check for
134 // errors (an unexpected test received)
135 void CallbackTest::Run(int params
) {
137 DCHECK_EQ(1, reuse_
);
139 helper_
->set_callback_reused_error(true);
143 helper_
->CallbackWasCalled();