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
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/stl_util.h"
#include "base/thread_task_runner_handle.h"
#include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h"
#include "chrome/browser/sync_file_system/local/local_file_change_tracker.h"
#include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
#include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
#include "chrome/browser/sync_file_system/syncable_file_system_util.h"
#include "content/public/test/async_file_test_helper.h"
#include "content/public/test/sandbox_file_system_test_helper.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "storage/browser/fileapi/file_system_context.h"
#include "storage/browser/fileapi/file_system_operation_context.h"
#include "storage/browser/fileapi/isolated_context.h"
#include "storage/browser/quota/quota_manager.h"
#include "storage/common/fileapi/file_system_types.h"
#include "storage/common/quota/quota_types.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
#include "third_party/leveldatabase/src/include/leveldb/env.h"
using content::SandboxFileSystemTestHelper;
using storage::FileSystemContext;
using storage::FileSystemOperationContext;
using storage::FileSystemURL;
using storage::FileSystemURLSet;
using storage::QuotaManager;
using storage::QuotaStatusCode;
namespace sync_file_system {
class SyncableFileSystemTest : public testing::Test {
public:
SyncableFileSystemTest()
: in_memory_env_(leveldb::NewMemEnv(leveldb::Env::Default())),
file_system_(GURL("http://example.com/"),
in_memory_env_.get(),
base::ThreadTaskRunnerHandle::Get().get(),
base::ThreadTaskRunnerHandle::Get().get()),
weak_factory_(this) {}
void SetUp() override {
ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
file_system_.SetUp(CannedSyncableFileSystem::QUOTA_ENABLED);
sync_context_ =
new LocalFileSyncContext(data_dir_.path(),
in_memory_env_.get(),
base::ThreadTaskRunnerHandle::Get().get(),
base::ThreadTaskRunnerHandle::Get().get());
ASSERT_EQ(
sync_file_system::SYNC_STATUS_OK,
file_system_.MaybeInitializeFileSystemContext(sync_context_.get()));
}
void TearDown() override {
if (sync_context_.get())
sync_context_->ShutdownOnUIThread();
sync_context_ = nullptr;
file_system_.TearDown();
// Make sure we don't leave the external filesystem.
// (CannedSyncableFileSystem::TearDown does not do this as there may be
// multiple syncable file systems registered for the name)
RevokeSyncableFileSystem();
}
protected:
void VerifyAndClearChange(const FileSystemURL& url,
const FileChange& expected_change) {
SCOPED_TRACE(testing::Message() << url.DebugString() <<
" expecting:" << expected_change.DebugString());
// Get the changes for URL and verify.
FileChangeList changes;
change_tracker()->GetChangesForURL(url, &changes);
ASSERT_EQ(1U, changes.size());
SCOPED_TRACE(testing::Message() << url.DebugString() <<
" actual:" << changes.DebugString());
EXPECT_EQ(expected_change, changes.front());
// Clear the URL from the change tracker.
change_tracker()->ClearChangesForURL(url);
}
FileSystemURL URL(const std::string& path) {
return file_system_.URL(path);
}
FileSystemContext* file_system_context() {
return file_system_.file_system_context();
}
LocalFileChangeTracker* change_tracker() {
return file_system_.backend()->change_tracker();
}
base::ScopedTempDir data_dir_;
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<leveldb::Env> in_memory_env_;
CannedSyncableFileSystem file_system_;
private:
scoped_refptr<LocalFileSyncContext> sync_context_;
base::WeakPtrFactory<SyncableFileSystemTest> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(SyncableFileSystemTest);
};
// Brief combined testing. Just see if all the sandbox feature works.
TEST_F(SyncableFileSystemTest, SyncableLocalSandboxCombined) {
// Opens a syncable file system.
EXPECT_EQ(base::File::FILE_OK,
file_system_.OpenFileSystem());
// Do some operations.
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateDirectory(URL("dir")));
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateFile(URL("dir/foo")));
const int64 kOriginalQuota = QuotaManager::kSyncableStorageDefaultHostQuota;
const int64 kQuota = 12345 * 1024;
QuotaManager::kSyncableStorageDefaultHostQuota = kQuota;
int64 usage, quota;
EXPECT_EQ(storage::kQuotaStatusOk,
file_system_.GetUsageAndQuota(&usage, "a));
// Returned quota must be what we overrode. Usage must be greater than 0
// as creating a file or directory consumes some space.
EXPECT_EQ(kQuota, quota);
EXPECT_GT(usage, 0);
// Truncate to extend an existing file and see if the usage reflects it.
const int64 kFileSizeToExtend = 333;
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateFile(URL("dir/foo")));
EXPECT_EQ(base::File::FILE_OK,
file_system_.TruncateFile(URL("dir/foo"), kFileSizeToExtend));
int64 new_usage;
EXPECT_EQ(storage::kQuotaStatusOk,
file_system_.GetUsageAndQuota(&new_usage, "a));
EXPECT_EQ(kFileSizeToExtend, new_usage - usage);
// Shrink the quota to the current usage, try to extend the file further
// and see if it fails.
QuotaManager::kSyncableStorageDefaultHostQuota = new_usage;
EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
file_system_.TruncateFile(URL("dir/foo"), kFileSizeToExtend + 1));
usage = new_usage;
EXPECT_EQ(storage::kQuotaStatusOk,
file_system_.GetUsageAndQuota(&new_usage, "a));
EXPECT_EQ(usage, new_usage);
// Deletes the file system.
EXPECT_EQ(base::File::FILE_OK,
file_system_.DeleteFileSystem());
// Now the usage must be zero.
EXPECT_EQ(storage::kQuotaStatusOk,
file_system_.GetUsageAndQuota(&usage, "a));
EXPECT_EQ(0, usage);
// Restore the system default quota.
QuotaManager::kSyncableStorageDefaultHostQuota = kOriginalQuota;
}
// Combined testing with LocalFileChangeTracker.
TEST_F(SyncableFileSystemTest, ChangeTrackerSimple) {
EXPECT_EQ(base::File::FILE_OK,
file_system_.OpenFileSystem());
const char kPath0[] = "dir a";
const char kPath1[] = "dir a/dir"; // child of kPath0
const char kPath2[] = "dir a/file"; // child of kPath0
const char kPath3[] = "dir b";
// Do some operations.
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateDirectory(URL(kPath0))); // Creates a dir.
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateDirectory(URL(kPath1))); // Creates another.
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateFile(URL(kPath2))); // Creates a file.
EXPECT_EQ(base::File::FILE_OK,
file_system_.TruncateFile(URL(kPath2), 1)); // Modifies the file.
EXPECT_EQ(base::File::FILE_OK,
file_system_.TruncateFile(URL(kPath2), 2)); // Modifies it again.
FileSystemURLSet urls;
file_system_.GetChangedURLsInTracker(&urls);
EXPECT_EQ(3U, urls.size());
EXPECT_TRUE(ContainsKey(urls, URL(kPath0)));
EXPECT_TRUE(ContainsKey(urls, URL(kPath1)));
EXPECT_TRUE(ContainsKey(urls, URL(kPath2)));
VerifyAndClearChange(URL(kPath0),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
sync_file_system::SYNC_FILE_TYPE_DIRECTORY));
VerifyAndClearChange(URL(kPath1),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
sync_file_system::SYNC_FILE_TYPE_DIRECTORY));
VerifyAndClearChange(URL(kPath2),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
sync_file_system::SYNC_FILE_TYPE_FILE));
// Creates and removes a same directory.
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateDirectory(URL(kPath3)));
EXPECT_EQ(base::File::FILE_OK,
file_system_.Remove(URL(kPath3), false /* recursive */));
// The changes will be offset.
urls.clear();
file_system_.GetChangedURLsInTracker(&urls);
EXPECT_TRUE(urls.empty());
// Recursively removes the kPath0 directory.
EXPECT_EQ(base::File::FILE_OK,
file_system_.Remove(URL(kPath0), true /* recursive */));
urls.clear();
file_system_.GetChangedURLsInTracker(&urls);
// kPath0 and its all chidren (kPath1 and kPath2) must have been deleted.
EXPECT_EQ(3U, urls.size());
EXPECT_TRUE(ContainsKey(urls, URL(kPath0)));
EXPECT_TRUE(ContainsKey(urls, URL(kPath1)));
EXPECT_TRUE(ContainsKey(urls, URL(kPath2)));
VerifyAndClearChange(URL(kPath0),
FileChange(FileChange::FILE_CHANGE_DELETE,
sync_file_system::SYNC_FILE_TYPE_DIRECTORY));
VerifyAndClearChange(URL(kPath1),
FileChange(FileChange::FILE_CHANGE_DELETE,
sync_file_system::SYNC_FILE_TYPE_DIRECTORY));
VerifyAndClearChange(URL(kPath2),
FileChange(FileChange::FILE_CHANGE_DELETE,
sync_file_system::SYNC_FILE_TYPE_FILE));
}
} // namespace sync_file_system
|