1 // Copyright 2014 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 "chrome/browser/chromeos/file_system_provider/fileapi/file_stream_writer.h"
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/run_loop.h"
16 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system.h"
17 #include "chrome/browser/chromeos/file_system_provider/service.h"
18 #include "chrome/browser/chromeos/file_system_provider/service_factory.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "chrome/test/base/testing_profile_manager.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "content/public/test/test_file_system_context.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "net/base/io_buffer.h"
26 #include "net/base/net_errors.h"
27 #include "storage/browser/fileapi/async_file_util.h"
28 #include "storage/browser/fileapi/external_mount_points.h"
29 #include "storage/browser/fileapi/file_system_url.h"
30 #include "testing/gtest/include/gtest/gtest.h"
33 namespace file_system_provider
{
36 const char kExtensionId
[] = "mbflcebpggnecokmikipoihdbecnjfoj";
37 const char kFileSystemId
[] = "testing-file-system";
38 const char kTextToWrite
[] = "This is a test of FileStreamWriter.";
40 // Pushes a value to the passed log vector.
41 void LogValue(std::vector
<int>* log
, int value
) {
42 log
->push_back(value
);
45 // Creates a cracked FileSystemURL for tests.
46 storage::FileSystemURL
CreateFileSystemURL(const std::string
& mount_point_name
,
47 const base::FilePath
& file_path
) {
48 const std::string origin
= std::string("chrome-extension://") + kExtensionId
;
49 const storage::ExternalMountPoints
* const mount_points
=
50 storage::ExternalMountPoints::GetSystemInstance();
51 return mount_points
->CreateCrackedFileSystemURL(
53 storage::kFileSystemTypeExternal
,
54 base::FilePath::FromUTF8Unsafe(mount_point_name
).Append(file_path
));
59 class FileSystemProviderFileStreamWriter
: public testing::Test
{
61 FileSystemProviderFileStreamWriter() {}
62 ~FileSystemProviderFileStreamWriter() override
{}
64 void SetUp() override
{
65 ASSERT_TRUE(data_dir_
.CreateUniqueTempDir());
66 profile_manager_
.reset(
67 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
68 ASSERT_TRUE(profile_manager_
->SetUp());
69 profile_
= profile_manager_
->CreateTestingProfile("testing-profile");
71 Service
* service
= Service::Get(profile_
); // Owned by its factory.
72 service
->SetFileSystemFactoryForTesting(
73 base::Bind(&FakeProvidedFileSystem::Create
));
75 const base::File::Error result
= service
->MountFileSystem(
76 kExtensionId
, MountOptions(kFileSystemId
, "Testing File System"));
77 ASSERT_EQ(base::File::FILE_OK
, result
);
78 provided_file_system_
= static_cast<FakeProvidedFileSystem
*>(
79 service
->GetProvidedFileSystem(kExtensionId
, kFileSystemId
));
80 ASSERT_TRUE(provided_file_system_
);
81 const ProvidedFileSystemInfo
& file_system_info
=
82 provided_file_system_
->GetFileSystemInfo();
83 const std::string mount_point_name
=
84 file_system_info
.mount_path().BaseName().AsUTF8Unsafe();
86 file_url_
= CreateFileSystemURL(mount_point_name
,
87 base::FilePath(kFakeFilePath
+ 1));
88 ASSERT_TRUE(file_url_
.is_valid());
89 wrong_file_url_
= CreateFileSystemURL(
90 mount_point_name
, base::FilePath(FILE_PATH_LITERAL("im-not-here.txt")));
91 ASSERT_TRUE(wrong_file_url_
.is_valid());
94 content::TestBrowserThreadBundle thread_bundle_
;
95 base::ScopedTempDir data_dir_
;
96 scoped_ptr
<TestingProfileManager
> profile_manager_
;
97 TestingProfile
* profile_
; // Owned by TestingProfileManager.
98 FakeProvidedFileSystem
* provided_file_system_
; // Owned by Service.
99 storage::FileSystemURL file_url_
;
100 storage::FileSystemURL wrong_file_url_
;
103 TEST_F(FileSystemProviderFileStreamWriter
, Write
) {
104 std::vector
<int> write_log
;
106 const int64 initial_offset
= 0;
107 FileStreamWriter
writer(file_url_
, initial_offset
);
108 scoped_refptr
<net::IOBuffer
> io_buffer(new net::StringIOBuffer(kTextToWrite
));
111 const int result
= writer
.Write(io_buffer
.get(),
112 sizeof(kTextToWrite
) - 1,
113 base::Bind(&LogValue
, &write_log
));
114 EXPECT_EQ(net::ERR_IO_PENDING
, result
);
115 base::RunLoop().RunUntilIdle();
117 ASSERT_EQ(1u, write_log
.size());
118 EXPECT_LT(0, write_log
[0]);
119 EXPECT_EQ(sizeof(kTextToWrite
) - 1, static_cast<size_t>(write_log
[0]));
121 const FakeEntry
* const entry
=
122 provided_file_system_
->GetEntry(base::FilePath(kFakeFilePath
));
125 EXPECT_EQ(kTextToWrite
,
126 entry
->contents
.substr(0, sizeof(kTextToWrite
) - 1));
129 // Write additional data to be sure, that the writer's offset is shifted
132 const int result
= writer
.Write(io_buffer
.get(),
133 sizeof(kTextToWrite
) - 1,
134 base::Bind(&LogValue
, &write_log
));
135 EXPECT_EQ(net::ERR_IO_PENDING
, result
);
136 base::RunLoop().RunUntilIdle();
138 ASSERT_EQ(2u, write_log
.size());
139 EXPECT_LT(0, write_log
[0]);
140 EXPECT_EQ(sizeof(kTextToWrite
) - 1, static_cast<size_t>(write_log
[0]));
142 const FakeEntry
* const entry
=
143 provided_file_system_
->GetEntry(base::FilePath(kFakeFilePath
));
146 // The testing text is written twice.
147 const std::string expected_contents
=
148 std::string(kTextToWrite
) + kTextToWrite
;
149 EXPECT_EQ(expected_contents
,
150 entry
->contents
.substr(0, expected_contents
.size()));
154 TEST_F(FileSystemProviderFileStreamWriter
, Cancel
) {
155 std::vector
<int> write_log
;
157 const int64 initial_offset
= 0;
158 FileStreamWriter
writer(file_url_
, initial_offset
);
159 scoped_refptr
<net::IOBuffer
> io_buffer(new net::StringIOBuffer(kTextToWrite
));
161 const int write_result
= writer
.Write(io_buffer
.get(),
162 sizeof(kTextToWrite
) - 1,
163 base::Bind(&LogValue
, &write_log
));
164 EXPECT_EQ(net::ERR_IO_PENDING
, write_result
);
166 std::vector
<int> cancel_log
;
167 const int cancel_result
= writer
.Cancel(base::Bind(&LogValue
, &cancel_log
));
168 EXPECT_EQ(net::ERR_IO_PENDING
, cancel_result
);
169 base::RunLoop().RunUntilIdle();
171 EXPECT_EQ(0u, write_log
.size());
172 ASSERT_EQ(1u, cancel_log
.size());
173 EXPECT_EQ(net::OK
, cancel_log
[0]);
176 TEST_F(FileSystemProviderFileStreamWriter
, Cancel_NotRunning
) {
177 std::vector
<int> write_log
;
179 const int64 initial_offset
= 0;
180 FileStreamWriter
writer(file_url_
, initial_offset
);
181 scoped_refptr
<net::IOBuffer
> io_buffer(new net::StringIOBuffer(kTextToWrite
));
183 std::vector
<int> cancel_log
;
184 const int cancel_result
= writer
.Cancel(base::Bind(&LogValue
, &cancel_log
));
185 EXPECT_EQ(net::ERR_UNEXPECTED
, cancel_result
);
186 base::RunLoop().RunUntilIdle();
188 EXPECT_EQ(0u, write_log
.size());
189 EXPECT_EQ(0u, cancel_log
.size()); // Result returned synchronously.
192 TEST_F(FileSystemProviderFileStreamWriter
, Write_WrongFile
) {
193 std::vector
<int> write_log
;
195 const int64 initial_offset
= 0;
196 FileStreamWriter
writer(wrong_file_url_
, initial_offset
);
197 scoped_refptr
<net::IOBuffer
> io_buffer(new net::StringIOBuffer(kTextToWrite
));
199 const int result
= writer
.Write(io_buffer
.get(),
200 sizeof(kTextToWrite
) - 1,
201 base::Bind(&LogValue
, &write_log
));
202 EXPECT_EQ(net::ERR_IO_PENDING
, result
);
203 base::RunLoop().RunUntilIdle();
205 ASSERT_EQ(1u, write_log
.size());
206 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, write_log
[0]);
209 TEST_F(FileSystemProviderFileStreamWriter
, Write_Append
) {
210 std::vector
<int> write_log
;
212 const FakeEntry
* const entry
=
213 provided_file_system_
->GetEntry(base::FilePath(kFakeFilePath
));
216 const std::string original_contents
= entry
->contents
;
217 const int64 initial_offset
= entry
->metadata
->size
;
218 ASSERT_LT(0, initial_offset
);
220 FileStreamWriter
writer(file_url_
, initial_offset
);
221 scoped_refptr
<net::IOBuffer
> io_buffer(new net::StringIOBuffer(kTextToWrite
));
223 const int result
= writer
.Write(io_buffer
.get(),
224 sizeof(kTextToWrite
) - 1,
225 base::Bind(&LogValue
, &write_log
));
226 EXPECT_EQ(net::ERR_IO_PENDING
, result
);
227 base::RunLoop().RunUntilIdle();
229 ASSERT_EQ(1u, write_log
.size());
230 EXPECT_EQ(sizeof(kTextToWrite
) - 1, static_cast<size_t>(write_log
[0]));
232 const std::string expected_contents
= original_contents
+ kTextToWrite
;
233 EXPECT_EQ(expected_contents
, entry
->contents
);
236 } // namespace file_system_provider
237 } // namespace chromeos