OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <string> |
| 6 #include <vector> |
| 7 |
| 8 #include "base/files/file.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/platform_file.h" |
| 14 #include "base/run_loop.h" |
| 15 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system
.h" |
| 16 #include "chrome/browser/chromeos/file_system_provider/fileapi/file_stream_reade
r.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 "testing/gtest/include/gtest/gtest.h" |
| 28 #include "webkit/browser/fileapi/async_file_util.h" |
| 29 #include "webkit/browser/fileapi/external_mount_points.h" |
| 30 #include "webkit/browser/fileapi/file_system_url.h" |
| 31 |
| 32 namespace chromeos { |
| 33 namespace file_system_provider { |
| 34 namespace { |
| 35 |
| 36 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 37 |
| 38 // Logs callbacks invocations on the file stream reader. |
| 39 class EventLogger { |
| 40 public: |
| 41 EventLogger() : weak_ptr_factory_(this) {} |
| 42 virtual ~EventLogger() {} |
| 43 |
| 44 void OnRead(int result) { results_.push_back(result); } |
| 45 void OnGetLength(int64 result) { results_.push_back(result); } |
| 46 |
| 47 base::WeakPtr<EventLogger> GetWeakPtr() { |
| 48 return weak_ptr_factory_.GetWeakPtr(); |
| 49 } |
| 50 |
| 51 const std::vector<int64>& results() const { return results_; } |
| 52 |
| 53 private: |
| 54 std::vector<int64> results_; |
| 55 base::WeakPtrFactory<EventLogger> weak_ptr_factory_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(EventLogger); |
| 58 }; |
| 59 |
| 60 // Creates a cracked FileSystemURL for tests. |
| 61 fileapi::FileSystemURL CreateFileSystemURL(const std::string& mount_point_name, |
| 62 const base::FilePath& file_path) { |
| 63 const std::string origin = std::string("chrome-extension://") + kExtensionId; |
| 64 const fileapi::ExternalMountPoints* const mount_points = |
| 65 fileapi::ExternalMountPoints::GetSystemInstance(); |
| 66 return mount_points->CreateCrackedFileSystemURL( |
| 67 GURL(origin), |
| 68 fileapi::kFileSystemTypeExternal, |
| 69 base::FilePath::FromUTF8Unsafe(mount_point_name).Append(file_path)); |
| 70 } |
| 71 |
| 72 // Creates a Service instance. Used to be able to destroy the service in |
| 73 // TearDown(). |
| 74 KeyedService* CreateService(content::BrowserContext* context) { |
| 75 return new Service(Profile::FromBrowserContext(context), |
| 76 extensions::ExtensionRegistry::Get(context)); |
| 77 } |
| 78 |
| 79 } // namespace |
| 80 |
| 81 class FileSystemProviderFileStreamReader : public testing::Test { |
| 82 protected: |
| 83 FileSystemProviderFileStreamReader() {} |
| 84 virtual ~FileSystemProviderFileStreamReader() {} |
| 85 |
| 86 virtual void SetUp() OVERRIDE { |
| 87 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 88 profile_manager_.reset( |
| 89 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 90 ASSERT_TRUE(profile_manager_->SetUp()); |
| 91 profile_ = profile_manager_->CreateTestingProfile("testing-profile"); |
| 92 |
| 93 ServiceFactory::GetInstance()->SetTestingFactory(profile_, &CreateService); |
| 94 Service* service = Service::Get(profile_); // Owned by its factory. |
| 95 service->SetFileSystemFactoryForTests( |
| 96 base::Bind(&FakeProvidedFileSystem::Create)); |
| 97 |
| 98 const int file_system_id = |
| 99 service->MountFileSystem(kExtensionId, "testing-file-system"); |
| 100 ASSERT_LT(0, file_system_id); |
| 101 const ProvidedFileSystemInfo& file_system_info = |
| 102 service->GetProvidedFileSystem(kExtensionId, file_system_id) |
| 103 ->GetFileSystemInfo(); |
| 104 const std::string mount_point_name = |
| 105 file_system_info.mount_path().BaseName().AsUTF8Unsafe(); |
| 106 |
| 107 file_url_ = CreateFileSystemURL( |
| 108 mount_point_name, base::FilePath::FromUTF8Unsafe(kFakeFilePath + 1)); |
| 109 ASSERT_TRUE(file_url_.is_valid()); |
| 110 wrong_file_url_ = CreateFileSystemURL( |
| 111 mount_point_name, base::FilePath::FromUTF8Unsafe("im-not-here.txt")); |
| 112 ASSERT_TRUE(wrong_file_url_.is_valid()); |
| 113 } |
| 114 |
| 115 virtual void TearDown() OVERRIDE { |
| 116 // Setting the testing factory to NULL will destroy the created service |
| 117 // associated with the testing profile. |
| 118 ServiceFactory::GetInstance()->SetTestingFactory(profile_, NULL); |
| 119 } |
| 120 |
| 121 content::TestBrowserThreadBundle thread_bundle_; |
| 122 base::ScopedTempDir data_dir_; |
| 123 scoped_ptr<TestingProfileManager> profile_manager_; |
| 124 TestingProfile* profile_; // Owned by TestingProfileManager. |
| 125 fileapi::FileSystemURL file_url_; |
| 126 fileapi::FileSystemURL wrong_file_url_; |
| 127 }; |
| 128 |
| 129 TEST_F(FileSystemProviderFileStreamReader, Read_AllAtOnce) { |
| 130 EventLogger logger; |
| 131 |
| 132 const int64 initial_offset = 0; |
| 133 FileStreamReader reader(NULL, |
| 134 file_url_, |
| 135 initial_offset, |
| 136 base::Time::Now()); // Not used yet. |
| 137 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(kFakeFileSize)); |
| 138 |
| 139 const int result = |
| 140 reader.Read(io_buffer.get(), |
| 141 kFakeFileSize, |
| 142 base::Bind(&EventLogger::OnRead, logger.GetWeakPtr())); |
| 143 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 144 base::RunLoop().RunUntilIdle(); |
| 145 |
| 146 ASSERT_EQ(1u, logger.results().size()); |
| 147 EXPECT_LT(0, logger.results()[0]); |
| 148 EXPECT_EQ(kFakeFileSize, static_cast<size_t>(logger.results()[0])); |
| 149 |
| 150 std::string buffer_as_string(io_buffer->data(), kFakeFileSize); |
| 151 EXPECT_EQ(kFakeFileText, buffer_as_string); |
| 152 } |
| 153 |
| 154 TEST_F(FileSystemProviderFileStreamReader, Read_WrongFile) { |
| 155 EventLogger logger; |
| 156 |
| 157 const int64 initial_offset = 0; |
| 158 FileStreamReader reader(NULL, |
| 159 wrong_file_url_, |
| 160 initial_offset, |
| 161 base::Time::Now()); // Not used yet. |
| 162 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(kFakeFileSize)); |
| 163 |
| 164 const int result = |
| 165 reader.Read(io_buffer.get(), |
| 166 kFakeFileSize, |
| 167 base::Bind(&EventLogger::OnRead, logger.GetWeakPtr())); |
| 168 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 169 base::RunLoop().RunUntilIdle(); |
| 170 |
| 171 ASSERT_EQ(1u, logger.results().size()); |
| 172 EXPECT_EQ(net::ERR_FAILED, logger.results()[0]); |
| 173 } |
| 174 |
| 175 TEST_F(FileSystemProviderFileStreamReader, Read_InChunks) { |
| 176 EventLogger logger; |
| 177 |
| 178 const int64 initial_offset = 0; |
| 179 FileStreamReader reader(NULL, |
| 180 file_url_, |
| 181 initial_offset, |
| 182 base::Time::Now()); // Not used yet. |
| 183 |
| 184 for (size_t offset = 0; offset < kFakeFileSize; ++offset) { |
| 185 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(1)); |
| 186 const int result = |
| 187 reader.Read(io_buffer.get(), |
| 188 1, |
| 189 base::Bind(&EventLogger::OnRead, logger.GetWeakPtr())); |
| 190 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 191 base::RunLoop().RunUntilIdle(); |
| 192 ASSERT_EQ(offset + 1u, logger.results().size()); |
| 193 EXPECT_EQ(1, logger.results()[offset]); |
| 194 EXPECT_EQ(kFakeFileText[offset], io_buffer->data()[0]); |
| 195 } |
| 196 } |
| 197 |
| 198 TEST_F(FileSystemProviderFileStreamReader, Read_Slice) { |
| 199 EventLogger logger; |
| 200 |
| 201 // Trim first 3 and last 3 characters. |
| 202 const int64 initial_offset = 3; |
| 203 const int length = static_cast<int>(kFakeFileSize) - initial_offset - 3; |
| 204 ASSERT_GT(kFakeFileSize, static_cast<size_t>(initial_offset)); |
| 205 ASSERT_LT(0, length); |
| 206 |
| 207 FileStreamReader reader(NULL, |
| 208 file_url_, |
| 209 initial_offset, |
| 210 base::Time::Now()); // Not used yet. |
| 211 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(length)); |
| 212 |
| 213 const int result = |
| 214 reader.Read(io_buffer.get(), |
| 215 length, |
| 216 base::Bind(&EventLogger::OnRead, logger.GetWeakPtr())); |
| 217 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 218 base::RunLoop().RunUntilIdle(); |
| 219 |
| 220 ASSERT_EQ(1u, logger.results().size()); |
| 221 EXPECT_EQ(length, logger.results()[0]); |
| 222 |
| 223 std::string buffer_as_string(io_buffer->data(), length); |
| 224 std::string expected_buffer(kFakeFileText + initial_offset, length); |
| 225 EXPECT_EQ(expected_buffer, buffer_as_string); |
| 226 } |
| 227 |
| 228 TEST_F(FileSystemProviderFileStreamReader, Read_Beyond) { |
| 229 EventLogger logger; |
| 230 |
| 231 // Request reading 1KB more than available. |
| 232 const int64 initial_offset = 0; |
| 233 const int length = static_cast<int>(kFakeFileSize) + 1024; |
| 234 |
| 235 FileStreamReader reader(NULL, |
| 236 file_url_, |
| 237 initial_offset, |
| 238 base::Time::Now()); // Not used yet. |
| 239 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(length)); |
| 240 |
| 241 const int result = |
| 242 reader.Read(io_buffer.get(), |
| 243 length, |
| 244 base::Bind(&EventLogger::OnRead, logger.GetWeakPtr())); |
| 245 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 246 base::RunLoop().RunUntilIdle(); |
| 247 |
| 248 ASSERT_EQ(1u, logger.results().size()); |
| 249 EXPECT_LT(0, logger.results()[0]); |
| 250 EXPECT_EQ(kFakeFileSize, static_cast<size_t>(logger.results()[0])); |
| 251 |
| 252 std::string buffer_as_string(io_buffer->data(), kFakeFileSize); |
| 253 EXPECT_EQ(kFakeFileText, buffer_as_string); |
| 254 } |
| 255 |
| 256 TEST_F(FileSystemProviderFileStreamReader, GetLength) { |
| 257 EventLogger logger; |
| 258 |
| 259 const int64 initial_offset = 0; |
| 260 FileStreamReader reader(NULL, |
| 261 file_url_, |
| 262 initial_offset, |
| 263 base::Time::Now()); // Not used yet. |
| 264 |
| 265 const int result = reader.GetLength( |
| 266 base::Bind(&EventLogger::OnGetLength, logger.GetWeakPtr())); |
| 267 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 268 base::RunLoop().RunUntilIdle(); |
| 269 |
| 270 ASSERT_EQ(1u, logger.results().size()); |
| 271 EXPECT_LT(0, logger.results()[0]); |
| 272 EXPECT_EQ(kFakeFileSize, static_cast<size_t>(logger.results()[0])); |
| 273 } |
| 274 |
| 275 TEST_F(FileSystemProviderFileStreamReader, GetLength_WrongFile) { |
| 276 EventLogger logger; |
| 277 |
| 278 const int64 initial_offset = 0; |
| 279 FileStreamReader reader(NULL, |
| 280 wrong_file_url_, |
| 281 initial_offset, |
| 282 base::Time::Now()); // Not used yet. |
| 283 |
| 284 const int result = reader.GetLength( |
| 285 base::Bind(&EventLogger::OnGetLength, logger.GetWeakPtr())); |
| 286 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 287 base::RunLoop().RunUntilIdle(); |
| 288 |
| 289 ASSERT_EQ(1u, logger.results().size()); |
| 290 EXPECT_EQ(net::ERR_FAILED, logger.results()[0]); |
| 291 } |
| 292 |
| 293 } // namespace file_system_provider |
| 294 } // namespace chromeos |
OLD | NEW |