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
|
// Copyright 2014 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 "chrome/browser/chromeos/file_system_provider/fileapi/file_stream_writer.h"
#include <string>
#include <vector>
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system.h"
#include "chrome/browser/chromeos/file_system_provider/service.h"
#include "chrome/browser/chromeos/file_system_provider/service_factory.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_file_system_context.h"
#include "extensions/browser/extension_registry.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "storage/browser/fileapi/async_file_util.h"
#include "storage/browser/fileapi/external_mount_points.h"
#include "storage/browser/fileapi/file_system_url.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace file_system_provider {
namespace {
const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
const char kFileSystemId[] = "testing-file-system";
const char kTextToWrite[] = "This is a test of FileStreamWriter.";
// Pushes a value to the passed log vector.
void LogValue(std::vector<int>* log, int value) {
log->push_back(value);
}
// Creates a cracked FileSystemURL for tests.
storage::FileSystemURL CreateFileSystemURL(const std::string& mount_point_name,
const base::FilePath& file_path) {
const std::string origin = std::string("chrome-extension://") + kExtensionId;
const storage::ExternalMountPoints* const mount_points =
storage::ExternalMountPoints::GetSystemInstance();
return mount_points->CreateCrackedFileSystemURL(
GURL(origin),
storage::kFileSystemTypeExternal,
base::FilePath::FromUTF8Unsafe(mount_point_name).Append(file_path));
}
// Creates a Service instance. Used to be able to destroy the service in
// TearDown().
KeyedService* CreateService(content::BrowserContext* context) {
return new Service(Profile::FromBrowserContext(context),
extensions::ExtensionRegistry::Get(context));
}
} // namespace
class FileSystemProviderFileStreamWriter : public testing::Test {
protected:
FileSystemProviderFileStreamWriter() {}
virtual ~FileSystemProviderFileStreamWriter() {}
virtual void SetUp() override {
ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
profile_manager_.reset(
new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
ASSERT_TRUE(profile_manager_->SetUp());
profile_ = profile_manager_->CreateTestingProfile("testing-profile");
ServiceFactory::GetInstance()->SetTestingFactory(profile_, &CreateService);
Service* service = Service::Get(profile_); // Owned by its factory.
service->SetFileSystemFactoryForTesting(
base::Bind(&FakeProvidedFileSystem::Create));
const base::File::Error result = service->MountFileSystem(
kExtensionId, MountOptions(kFileSystemId, "Testing File System"));
ASSERT_EQ(base::File::FILE_OK, result);
provided_file_system_ = static_cast<FakeProvidedFileSystem*>(
service->GetProvidedFileSystem(kExtensionId, kFileSystemId));
ASSERT_TRUE(provided_file_system_);
const ProvidedFileSystemInfo& file_system_info =
provided_file_system_->GetFileSystemInfo();
const std::string mount_point_name =
file_system_info.mount_path().BaseName().AsUTF8Unsafe();
file_url_ = CreateFileSystemURL(mount_point_name,
base::FilePath(kFakeFilePath + 1));
ASSERT_TRUE(file_url_.is_valid());
wrong_file_url_ = CreateFileSystemURL(
mount_point_name, base::FilePath(FILE_PATH_LITERAL("im-not-here.txt")));
ASSERT_TRUE(wrong_file_url_.is_valid());
}
virtual void TearDown() override {
// Setting the testing factory to NULL will destroy the created service
// associated with the testing profile.
ServiceFactory::GetInstance()->SetTestingFactory(profile_, NULL);
}
content::TestBrowserThreadBundle thread_bundle_;
base::ScopedTempDir data_dir_;
scoped_ptr<TestingProfileManager> profile_manager_;
TestingProfile* profile_; // Owned by TestingProfileManager.
FakeProvidedFileSystem* provided_file_system_; // Owned by Service.
storage::FileSystemURL file_url_;
storage::FileSystemURL wrong_file_url_;
};
TEST_F(FileSystemProviderFileStreamWriter, Write) {
std::vector<int> write_log;
const int64 initial_offset = 0;
FileStreamWriter writer(file_url_, initial_offset);
scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite));
{
const int result = writer.Write(io_buffer.get(),
sizeof(kTextToWrite) - 1,
base::Bind(&LogValue, &write_log));
EXPECT_EQ(net::ERR_IO_PENDING, result);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1u, write_log.size());
EXPECT_LT(0, write_log[0]);
EXPECT_EQ(sizeof(kTextToWrite) - 1, static_cast<size_t>(write_log[0]));
const FakeEntry* const entry =
provided_file_system_->GetEntry(base::FilePath(kFakeFilePath));
ASSERT_TRUE(entry);
EXPECT_EQ(kTextToWrite,
entry->contents.substr(0, sizeof(kTextToWrite) - 1));
}
// Write additional data to be sure, that the writer's offset is shifted
// properly.
{
const int result = writer.Write(io_buffer.get(),
sizeof(kTextToWrite) - 1,
base::Bind(&LogValue, &write_log));
EXPECT_EQ(net::ERR_IO_PENDING, result);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(2u, write_log.size());
EXPECT_LT(0, write_log[0]);
EXPECT_EQ(sizeof(kTextToWrite) - 1, static_cast<size_t>(write_log[0]));
const FakeEntry* const entry =
provided_file_system_->GetEntry(base::FilePath(kFakeFilePath));
ASSERT_TRUE(entry);
// The testing text is written twice.
const std::string expected_contents =
std::string(kTextToWrite) + kTextToWrite;
EXPECT_EQ(expected_contents,
entry->contents.substr(0, expected_contents.size()));
}
}
TEST_F(FileSystemProviderFileStreamWriter, Cancel) {
std::vector<int> write_log;
const int64 initial_offset = 0;
FileStreamWriter writer(file_url_, initial_offset);
scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite));
const int write_result = writer.Write(io_buffer.get(),
sizeof(kTextToWrite) - 1,
base::Bind(&LogValue, &write_log));
EXPECT_EQ(net::ERR_IO_PENDING, write_result);
std::vector<int> cancel_log;
const int cancel_result = writer.Cancel(base::Bind(&LogValue, &cancel_log));
EXPECT_EQ(net::ERR_IO_PENDING, cancel_result);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, write_log.size());
ASSERT_EQ(1u, cancel_log.size());
EXPECT_EQ(net::OK, cancel_log[0]);
}
TEST_F(FileSystemProviderFileStreamWriter, Write_WrongFile) {
std::vector<int> write_log;
const int64 initial_offset = 0;
FileStreamWriter writer(wrong_file_url_, initial_offset);
scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite));
const int result = writer.Write(io_buffer.get(),
sizeof(kTextToWrite) - 1,
base::Bind(&LogValue, &write_log));
EXPECT_EQ(net::ERR_IO_PENDING, result);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1u, write_log.size());
EXPECT_EQ(net::ERR_FILE_NOT_FOUND, write_log[0]);
}
TEST_F(FileSystemProviderFileStreamWriter, Write_Append) {
std::vector<int> write_log;
const FakeEntry* const entry =
provided_file_system_->GetEntry(base::FilePath(kFakeFilePath));
ASSERT_TRUE(entry);
const std::string original_contents = entry->contents;
const int64 initial_offset = entry->metadata->size;
ASSERT_LT(0, initial_offset);
FileStreamWriter writer(file_url_, initial_offset);
scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite));
const int result = writer.Write(io_buffer.get(),
sizeof(kTextToWrite) - 1,
base::Bind(&LogValue, &write_log));
EXPECT_EQ(net::ERR_IO_PENDING, result);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1u, write_log.size());
EXPECT_EQ(sizeof(kTextToWrite) - 1, static_cast<size_t>(write_log[0]));
const std::string expected_contents = original_contents + kTextToWrite;
EXPECT_EQ(expected_contents, entry->contents);
}
} // namespace file_system_provider
} // namespace chromeos
|