#ifndef NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
#define NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
#include <stddef.h>
#include <stdint.h>
#include <string>
#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "net/base/test_completion_callback.h"
#include "net/disk_cache/disk_cache.h"
namespace net {
class IOBufferWithSize;
}
bool CreateCacheTestFile(const base::FilePath& name);
bool DeleteCache(const base::FilePath& path);
void CacheTestFillBuffer(base::span<uint8_t> buffer, bool no_nulls);
scoped_refptr<net::IOBufferWithSize> CacheTestCreateAndFillBuffer(
size_t len,
bool no_nulls);
std::string GenerateKey(bool same_length);
bool CheckCacheIntegrity(const base::FilePath& path,
bool new_eviction,
int max_size,
uint32_t mask);
struct BackendResultIsPendingHelper {
bool operator()(const disk_cache::BackendResult& result) const {
return result.net_error == net::ERR_IO_PENDING;
}
};
using TestBackendResultCompletionCallbackBase =
net::internal::TestCompletionCallbackTemplate<disk_cache::BackendResult,
BackendResultIsPendingHelper>;
class TestBackendResultCompletionCallback
: public TestBackendResultCompletionCallbackBase {
public:
TestBackendResultCompletionCallback();
TestBackendResultCompletionCallback(
const TestBackendResultCompletionCallback&) = delete;
TestBackendResultCompletionCallback& operator=(
const TestBackendResultCompletionCallback&) = delete;
~TestBackendResultCompletionCallback() override;
disk_cache::BackendResultCallback callback();
};
struct EntryResultIsPendingHelper {
bool operator()(const disk_cache::EntryResult& result) const {
return result.net_error() == net::ERR_IO_PENDING;
}
};
using TestEntryResultCompletionCallbackBase =
net::internal::TestCompletionCallbackTemplate<disk_cache::EntryResult,
EntryResultIsPendingHelper>;
class TestEntryResultCompletionCallback
: public TestEntryResultCompletionCallbackBase {
public:
TestEntryResultCompletionCallback();
TestEntryResultCompletionCallback(const TestEntryResultCompletionCallback&) =
delete;
TestEntryResultCompletionCallback& operator=(
const TestEntryResultCompletionCallback&) = delete;
~TestEntryResultCompletionCallback() override;
disk_cache::Backend::EntryResultCallback callback();
};
struct RangeResultIsPendingHelper {
bool operator()(const disk_cache::RangeResult& result) const {
return result.net_error == net::ERR_IO_PENDING;
}
};
class TestRangeResultCompletionCallback
: public net::internal::TestCompletionCallbackTemplate<
disk_cache::RangeResult,
RangeResultIsPendingHelper> {
public:
TestRangeResultCompletionCallback();
~TestRangeResultCompletionCallback() override;
disk_cache::RangeResultCallback callback();
private:
void HelpSetResult(const disk_cache::RangeResult& result);
};
class MessageLoopHelper {
public:
MessageLoopHelper();
MessageLoopHelper(const MessageLoopHelper&) = delete;
MessageLoopHelper& operator=(const MessageLoopHelper&) = delete;
~MessageLoopHelper();
bool WaitUntilCacheIoFinished(int num_callbacks);
bool callback_reused_error() const { return callback_reused_error_; }
void set_callback_reused_error(bool error) {
callback_reused_error_ = error;
}
int callbacks_called() const { return callbacks_called_; }
void CallbackWasCalled();
private:
std::unique_ptr<base::RunLoop> run_loop_;
int expected_num_callbacks_ = 0;
bool completed_ = false;
bool callback_reused_error_ = false;
int callbacks_called_ = 0;
};
class CallbackTest {
public:
CallbackTest(MessageLoopHelper* helper, bool reuse);
CallbackTest(const CallbackTest&) = delete;
CallbackTest& operator=(const CallbackTest&) = delete;
~CallbackTest();
void Run(int result);
void RunWithEntry(disk_cache::EntryResult result);
int last_result() const { return last_result_; }
disk_cache::EntryResult ReleaseLastEntryResult() {
return std::move(last_entry_result_);
}
private:
raw_ptr<MessageLoopHelper> helper_;
int reuse_;
int last_result_;
disk_cache::EntryResult last_entry_result_;
};
#endif