1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "storage/browser/file_system/recursive_operation_delegate.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "storage/browser/file_system/file_system_file_util.h"
#include "storage/browser/file_system/file_system_operation.h"
#include "storage/browser/file_system/file_system_operation_runner.h"
#include "storage/browser/test/mock_quota_manager.h"
#include "storage/browser/test/mock_quota_manager_proxy.h"
#include "storage/browser/test/mock_special_storage_policy.h"
#include "storage/browser/test/sandbox_file_system_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace storage {
namespace {
class LoggingRecursiveOperation final : public RecursiveOperationDelegate {
public:
struct LogEntry {
enum Type { PROCESS_FILE, PROCESS_DIRECTORY, POST_PROCESS_DIRECTORY };
Type type;
FileSystemURL url;
};
LoggingRecursiveOperation(FileSystemContext* file_system_context,
const FileSystemURL& root,
StatusCallback callback)
: RecursiveOperationDelegate(file_system_context),
root_(root),
callback_(std::move(callback)) {}
LoggingRecursiveOperation(const LoggingRecursiveOperation&) = delete;
LoggingRecursiveOperation& operator=(const LoggingRecursiveOperation&) =
delete;
~LoggingRecursiveOperation() override = default;
const std::vector<LogEntry>& log_entries() const { return log_entries_; }
// RecursiveOperationDelegate overrides.
void Run() override { NOTREACHED(); }
void RunRecursively() override {
StartRecursiveOperation(root_, FileSystemOperation::ERROR_BEHAVIOR_ABORT,
std::move(callback_));
}
void RunRecursivelyWithIgnoringError() {
StartRecursiveOperation(root_, FileSystemOperation::ERROR_BEHAVIOR_SKIP,
std::move(callback_));
}
void ProcessFile(const FileSystemURL& url, StatusCallback callback) override {
RecordLogEntry(LogEntry::PROCESS_FILE, url);
if (error_url_.is_valid() && error_url_ == url) {
std::move(callback).Run(base::File::FILE_ERROR_FAILED);
return;
}
operation_runner()->GetMetadata(
url, {FileSystemOperation::GetMetadataField::kIsDirectory},
base::BindOnce(&LoggingRecursiveOperation::DidGetMetadata,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void ProcessDirectory(const FileSystemURL& url,
StatusCallback callback) override {
RecordLogEntry(LogEntry::PROCESS_DIRECTORY, url);
std::move(callback).Run(base::File::FILE_OK);
}
void PostProcessDirectory(const FileSystemURL& url,
StatusCallback callback) override {
RecordLogEntry(LogEntry::POST_PROCESS_DIRECTORY, url);
std::move(callback).Run(base::File::FILE_OK);
}
base::WeakPtr<RecursiveOperationDelegate> AsWeakPtr() override {
return weak_factory_.GetWeakPtr();
}
void SetEntryToFail(const FileSystemURL& url) { error_url_ = url; }
private:
void RecordLogEntry(LogEntry::Type type, const FileSystemURL& url) {
LogEntry entry;
entry.type = type;
entry.url = url;
log_entries_.push_back(entry);
}
void DidGetMetadata(StatusCallback callback,
base::File::Error result,
const base::File::Info& file_info) {
if (result != base::File::FILE_OK) {
std::move(callback).Run(result);
return;
}
std::move(callback).Run(file_info.is_directory
? base::File::FILE_ERROR_NOT_A_FILE
: base::File::FILE_OK);
}
FileSystemURL root_;
StatusCallback callback_;
std::vector<LogEntry> log_entries_;
FileSystemURL error_url_;
base::WeakPtrFactory<LoggingRecursiveOperation> weak_factory_{this};
};
void ReportStatus(base::File::Error* out_error, base::File::Error error) {
DCHECK(out_error);
*out_error = error;
}
// To test the Cancel() during operation, calls Cancel() of |operation|
// after |counter| times message posting.
void CallCancelLater(RecursiveOperationDelegate* operation, int counter) {
if (counter > 0) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&CallCancelLater, base::Unretained(operation),
counter - 1));
return;
}
operation->Cancel();
}
} // namespace
class RecursiveOperationDelegateTest : public testing::Test {
protected:
void SetUp() override {
EXPECT_TRUE(base_.CreateUniqueTempDir());
base::FilePath base_dir = base_.GetPath().AppendASCII("filesystem");
quota_manager_ = base::MakeRefCounted<storage::MockQuotaManager>(
/*is_incognito=*/false, base_dir,
base::SingleThreadTaskRunner::GetCurrentDefault(),
base::MakeRefCounted<storage::MockSpecialStoragePolicy>());
quota_manager_proxy_ = base::MakeRefCounted<storage::MockQuotaManagerProxy>(
quota_manager_.get(),
base::SingleThreadTaskRunner::GetCurrentDefault());
sandbox_file_system_.SetUp(base_dir, quota_manager_proxy_);
}
void TearDown() override { sandbox_file_system_.TearDown(); }
std::unique_ptr<FileSystemOperationContext> NewContext() {
auto context = sandbox_file_system_.NewOperationContext();
// Grant enough quota for all test cases.
context->set_allowed_bytes_growth(1000000);
return context;
}
FileSystemFileUtil* file_util() { return sandbox_file_system_.file_util(); }
FileSystemURL URLForPath(const std::string& path) const {
return sandbox_file_system_.CreateURLFromUTF8(path);
}
FileSystemURL CreateFile(const std::string& path) {
FileSystemURL url = URLForPath(path);
bool created = false;
EXPECT_EQ(base::File::FILE_OK,
file_util()->EnsureFileExists(NewContext().get(), url, &created));
EXPECT_TRUE(created);
return url;
}
FileSystemURL CreateDirectory(const std::string& path) {
FileSystemURL url = URLForPath(path);
EXPECT_EQ(base::File::FILE_OK,
file_util()->CreateDirectory(NewContext().get(), url,
false /* exclusive */, true));
return url;
}
private:
base::test::TaskEnvironment task_environment_;
// Common temp base for nondestructive uses.
base::ScopedTempDir base_;
SandboxFileSystemTestHelper sandbox_file_system_;
scoped_refptr<storage::MockQuotaManager> quota_manager_;
scoped_refptr<storage::MockQuotaManagerProxy> quota_manager_proxy_;
};
TEST_F(RecursiveOperationDelegateTest, RootIsFile) {
FileSystemURL src_file(CreateFile("src"));
base::File::Error error = base::File::FILE_ERROR_FAILED;
std::unique_ptr<FileSystemOperationContext> context = NewContext();
auto operation = std::make_unique<LoggingRecursiveOperation>(
context->file_system_context(), src_file,
base::BindOnce(&ReportStatus, &error));
operation->RunRecursively();
base::RunLoop().RunUntilIdle();
ASSERT_EQ(base::File::FILE_OK, error);
const std::vector<LoggingRecursiveOperation::LogEntry>& log_entries =
operation->log_entries();
ASSERT_EQ(1U, log_entries.size());
const LoggingRecursiveOperation::LogEntry& entry = log_entries[0];
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE, entry.type);
EXPECT_EQ(src_file, entry.url);
}
TEST_F(RecursiveOperationDelegateTest, RootIsDirectory) {
FileSystemURL src_root(CreateDirectory("src"));
FileSystemURL src_dir1(CreateDirectory("src/dir1"));
FileSystemURL src_file1(CreateFile("src/file1"));
FileSystemURL src_file2(CreateFile("src/dir1/file2"));
FileSystemURL src_file3(CreateFile("src/dir1/file3"));
base::File::Error error = base::File::FILE_ERROR_FAILED;
std::unique_ptr<FileSystemOperationContext> context = NewContext();
auto operation = std::make_unique<LoggingRecursiveOperation>(
context->file_system_context(), src_root,
base::BindOnce(&ReportStatus, &error));
operation->RunRecursively();
base::RunLoop().RunUntilIdle();
ASSERT_EQ(base::File::FILE_OK, error);
const std::vector<LoggingRecursiveOperation::LogEntry>& log_entries =
operation->log_entries();
ASSERT_EQ(8U, log_entries.size());
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE,
log_entries[0].type);
EXPECT_EQ(src_root, log_entries[0].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_DIRECTORY,
log_entries[1].type);
EXPECT_EQ(src_root, log_entries[1].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE,
log_entries[2].type);
EXPECT_EQ(src_file1, log_entries[2].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_DIRECTORY,
log_entries[3].type);
EXPECT_EQ(src_dir1, log_entries[3].url);
// The order of src/dir1/file2 and src/dir1/file3 depends on the file system
// implementation (can be swapped).
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE,
log_entries[4].type);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE,
log_entries[5].type);
EXPECT_TRUE(
(src_file2 == log_entries[4].url && src_file3 == log_entries[5].url) ||
(src_file3 == log_entries[4].url && src_file2 == log_entries[5].url));
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::POST_PROCESS_DIRECTORY,
log_entries[6].type);
EXPECT_EQ(src_dir1, log_entries[6].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::POST_PROCESS_DIRECTORY,
log_entries[7].type);
EXPECT_EQ(src_root, log_entries[7].url);
}
TEST_F(RecursiveOperationDelegateTest, Cancel) {
FileSystemURL src_root(CreateDirectory("src"));
FileSystemURL src_dir1(CreateDirectory("src/dir1"));
FileSystemURL src_file1(CreateFile("src/file1"));
FileSystemURL src_file2(CreateFile("src/dir1/file2"));
base::File::Error error = base::File::FILE_ERROR_FAILED;
std::unique_ptr<FileSystemOperationContext> context = NewContext();
auto operation = std::make_unique<LoggingRecursiveOperation>(
context->file_system_context(), src_root,
base::BindOnce(&ReportStatus, &error));
operation->RunRecursively();
// Invoke Cancel(), after 5 times message posting.
CallCancelLater(operation.get(), 5);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(base::File::FILE_ERROR_ABORT, error);
}
TEST_F(RecursiveOperationDelegateTest, AbortWithError) {
FileSystemURL src_root(CreateDirectory("src"));
FileSystemURL src_dir1(CreateDirectory("src/dir1"));
FileSystemURL src_file1(CreateFile("src/file1"));
FileSystemURL src_file2(CreateFile("src/dir1/file2"));
FileSystemURL src_file3(CreateFile("src/dir1/file3"));
base::File::Error error = base::File::FILE_ERROR_FAILED;
std::unique_ptr<FileSystemOperationContext> context = NewContext();
auto operation = std::make_unique<LoggingRecursiveOperation>(
context->file_system_context(), src_root,
base::BindOnce(&ReportStatus, &error));
operation->SetEntryToFail(src_file1);
operation->RunRecursively();
base::RunLoop().RunUntilIdle();
ASSERT_EQ(base::File::FILE_ERROR_FAILED, error);
// Confirm that operation has been aborted in the middle.
const std::vector<LoggingRecursiveOperation::LogEntry>& log_entries =
operation->log_entries();
ASSERT_EQ(3U, log_entries.size());
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE,
log_entries[0].type);
EXPECT_EQ(src_root, log_entries[0].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_DIRECTORY,
log_entries[1].type);
EXPECT_EQ(src_root, log_entries[1].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE,
log_entries[2].type);
EXPECT_EQ(src_file1, log_entries[2].url);
}
TEST_F(RecursiveOperationDelegateTest, ContinueWithError) {
FileSystemURL src_root(CreateDirectory("src"));
FileSystemURL src_dir1(CreateDirectory("src/dir1"));
FileSystemURL src_file1(CreateFile("src/file1"));
FileSystemURL src_file2(CreateFile("src/dir1/file2"));
FileSystemURL src_file3(CreateFile("src/dir1/file3"));
base::File::Error error = base::File::FILE_ERROR_FAILED;
std::unique_ptr<FileSystemOperationContext> context = NewContext();
auto operation = std::make_unique<LoggingRecursiveOperation>(
context->file_system_context(), src_root,
base::BindOnce(&ReportStatus, &error));
operation->SetEntryToFail(src_file1);
operation->RunRecursivelyWithIgnoringError();
base::RunLoop().RunUntilIdle();
// Error code should be base::File::FILE_ERROR_FAILED.
ASSERT_EQ(base::File::FILE_ERROR_FAILED, error);
// Confirm that operation continues after the error.
const std::vector<LoggingRecursiveOperation::LogEntry>& log_entries =
operation->log_entries();
ASSERT_EQ(8U, log_entries.size());
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE,
log_entries[0].type);
EXPECT_EQ(src_root, log_entries[0].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_DIRECTORY,
log_entries[1].type);
EXPECT_EQ(src_root, log_entries[1].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE,
log_entries[2].type);
EXPECT_EQ(src_file1, log_entries[2].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_DIRECTORY,
log_entries[3].type);
EXPECT_EQ(src_dir1, log_entries[3].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE,
log_entries[4].type);
EXPECT_EQ(src_file3, log_entries[4].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::PROCESS_FILE,
log_entries[5].type);
EXPECT_EQ(src_file2, log_entries[5].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::POST_PROCESS_DIRECTORY,
log_entries[6].type);
EXPECT_EQ(src_dir1, log_entries[6].url);
EXPECT_EQ(LoggingRecursiveOperation::LogEntry::POST_PROCESS_DIRECTORY,
log_entries[7].type);
EXPECT_EQ(src_root, log_entries[7].url);
}
} // namespace storage
|