mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 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 "chrome/browser/chromeos/file_system_provider/operations/write_file.h" |
| 6 | |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 7 | #include <memory> |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 8 | #include <string> |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame] | 9 | #include <vector> |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 10 | |
| 11 | #include "base/files/file.h" |
| 12 | #include "base/files/file_path.h" |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame] | 13 | #include "base/macros.h" |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 15 | #include "base/values.h" |
| 16 | #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" |
| 17 | #include "chrome/common/extensions/api/file_system_provider.h" |
mtomasz | b9c370a | 2015-05-15 11:54:18 | [diff] [blame] | 18 | #include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.h" |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 19 | #include "chrome/common/extensions/api/file_system_provider_internal.h" |
| 20 | #include "extensions/browser/event_router.h" |
| 21 | #include "net/base/io_buffer.h" |
pilgrim | e92c5fcd | 2014-09-10 23:31:23 | [diff] [blame] | 22 | #include "storage/browser/fileapi/async_file_util.h" |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 23 | #include "testing/gtest/include/gtest/gtest.h" |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 24 | |
| 25 | namespace chromeos { |
| 26 | namespace file_system_provider { |
| 27 | namespace operations { |
| 28 | namespace { |
| 29 | |
| 30 | const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 31 | const char kFileSystemId[] = "testing-file-system"; |
| 32 | const int kRequestId = 2; |
| 33 | const int kFileHandle = 3; |
mtomasz | 3485d1a0 | 2014-09-03 07:34:20 | [diff] [blame] | 34 | const char kWriteData[] = "Welcome to my world!"; |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 35 | const int kOffset = 10; |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 36 | |
| 37 | } // namespace |
| 38 | |
| 39 | class FileSystemProviderOperationsWriteFileTest : public testing::Test { |
| 40 | protected: |
| 41 | FileSystemProviderOperationsWriteFileTest() {} |
dcheng | b35a132 | 2015-01-16 02:21:32 | [diff] [blame] | 42 | ~FileSystemProviderOperationsWriteFileTest() override {} |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 43 | |
dcheng | b35a132 | 2015-01-16 02:21:32 | [diff] [blame] | 44 | void SetUp() override { |
mtomasz | 00336db6f | 2014-10-14 05:57:37 | [diff] [blame] | 45 | MountOptions mount_options(kFileSystemId, "" /* display_name */); |
| 46 | mount_options.writable = true; |
mtomasz | b9c370a | 2015-05-15 11:54:18 | [diff] [blame] | 47 | file_system_info_ = ProvidedFileSystemInfo( |
| 48 | kExtensionId, mount_options, base::FilePath(), false /* configurable */, |
mtomasz | b577564f | 2015-07-07 06:07:40 | [diff] [blame] | 49 | true /* watchable */, extensions::SOURCE_FILE); |
kylechar | 96f3eba | 2017-09-25 20:23:56 | [diff] [blame^] | 50 | io_buffer_ = base::MakeRefCounted<net::StringIOBuffer>(kWriteData); |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | ProvidedFileSystemInfo file_system_info_; |
mtomasz | 3485d1a0 | 2014-09-03 07:34:20 | [diff] [blame] | 54 | scoped_refptr<net::StringIOBuffer> io_buffer_; |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | TEST_F(FileSystemProviderOperationsWriteFileTest, Execute) { |
mtomasz | 091a0ad7 | 2014-09-12 00:35:38 | [diff] [blame] | 58 | using extensions::api::file_system_provider::WriteFileRequestedOptions; |
| 59 | |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 60 | util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 61 | util::StatusCallbackLog callback_log; |
| 62 | |
| 63 | WriteFile write_file(NULL, |
| 64 | file_system_info_, |
| 65 | kFileHandle, |
| 66 | io_buffer_.get(), |
| 67 | kOffset, |
mtomasz | 3485d1a0 | 2014-09-03 07:34:20 | [diff] [blame] | 68 | io_buffer_->size(), |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 69 | base::Bind(&util::LogStatusCallback, &callback_log)); |
| 70 | write_file.SetDispatchEventImplForTesting( |
| 71 | base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 72 | base::Unretained(&dispatcher))); |
| 73 | |
| 74 | EXPECT_TRUE(write_file.Execute(kRequestId)); |
| 75 | |
| 76 | ASSERT_EQ(1u, dispatcher.events().size()); |
avi | 3dbcc20 | 2017-03-13 02:48:36 | [diff] [blame] | 77 | extensions::Event* event = dispatcher.events()[0].get(); |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 78 | EXPECT_EQ( |
| 79 | extensions::api::file_system_provider::OnWriteFileRequested::kEventName, |
| 80 | event->event_name); |
| 81 | base::ListValue* event_args = event->event_args.get(); |
| 82 | ASSERT_EQ(1u, event_args->GetSize()); |
| 83 | |
mtomasz | 091a0ad7 | 2014-09-12 00:35:38 | [diff] [blame] | 84 | const base::DictionaryValue* options_as_value = NULL; |
| 85 | ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value)); |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 86 | |
mtomasz | 091a0ad7 | 2014-09-12 00:35:38 | [diff] [blame] | 87 | WriteFileRequestedOptions options; |
| 88 | ASSERT_TRUE(WriteFileRequestedOptions::Populate(*options_as_value, &options)); |
| 89 | EXPECT_EQ(kFileSystemId, options.file_system_id); |
| 90 | EXPECT_EQ(kRequestId, options.request_id); |
| 91 | EXPECT_EQ(kFileHandle, options.open_request_id); |
| 92 | EXPECT_EQ(kOffset, static_cast<double>(options.offset)); |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame] | 93 | std::string write_data(kWriteData); |
| 94 | EXPECT_EQ(std::vector<char>(write_data.begin(), write_data.end()), |
| 95 | options.data); |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | TEST_F(FileSystemProviderOperationsWriteFileTest, Execute_NoListener) { |
| 99 | util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); |
| 100 | util::StatusCallbackLog callback_log; |
| 101 | |
| 102 | WriteFile write_file(NULL, |
| 103 | file_system_info_, |
| 104 | kFileHandle, |
| 105 | io_buffer_.get(), |
| 106 | kOffset, |
mtomasz | 3485d1a0 | 2014-09-03 07:34:20 | [diff] [blame] | 107 | io_buffer_->size(), |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 108 | base::Bind(&util::LogStatusCallback, &callback_log)); |
| 109 | write_file.SetDispatchEventImplForTesting( |
| 110 | base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 111 | base::Unretained(&dispatcher))); |
| 112 | |
| 113 | EXPECT_FALSE(write_file.Execute(kRequestId)); |
| 114 | } |
| 115 | |
| 116 | TEST_F(FileSystemProviderOperationsWriteFileTest, Execute_ReadOnly) { |
| 117 | util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 118 | util::StatusCallbackLog callback_log; |
| 119 | |
| 120 | const ProvidedFileSystemInfo read_only_file_system_info( |
mtomasz | b9c370a | 2015-05-15 11:54:18 | [diff] [blame] | 121 | kExtensionId, MountOptions(kFileSystemId, "" /* display_name */), |
| 122 | base::FilePath() /* mount_path */, false /* configurable */, |
mtomasz | b577564f | 2015-07-07 06:07:40 | [diff] [blame] | 123 | true /* watchable */, extensions::SOURCE_FILE); |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 124 | |
| 125 | WriteFile write_file(NULL, |
| 126 | read_only_file_system_info, |
| 127 | kFileHandle, |
| 128 | io_buffer_.get(), |
| 129 | kOffset, |
mtomasz | 3485d1a0 | 2014-09-03 07:34:20 | [diff] [blame] | 130 | io_buffer_->size(), |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 131 | base::Bind(&util::LogStatusCallback, &callback_log)); |
| 132 | write_file.SetDispatchEventImplForTesting( |
| 133 | base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 134 | base::Unretained(&dispatcher))); |
| 135 | |
| 136 | EXPECT_FALSE(write_file.Execute(kRequestId)); |
| 137 | } |
| 138 | |
| 139 | TEST_F(FileSystemProviderOperationsWriteFileTest, OnSuccess) { |
| 140 | util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 141 | util::StatusCallbackLog callback_log; |
| 142 | |
| 143 | WriteFile write_file(NULL, |
| 144 | file_system_info_, |
| 145 | kFileHandle, |
| 146 | io_buffer_.get(), |
| 147 | kOffset, |
mtomasz | 3485d1a0 | 2014-09-03 07:34:20 | [diff] [blame] | 148 | io_buffer_->size(), |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 149 | base::Bind(&util::LogStatusCallback, &callback_log)); |
| 150 | write_file.SetDispatchEventImplForTesting( |
| 151 | base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 152 | base::Unretained(&dispatcher))); |
| 153 | |
| 154 | EXPECT_TRUE(write_file.Execute(kRequestId)); |
| 155 | |
| 156 | write_file.OnSuccess(kRequestId, |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 157 | std::unique_ptr<RequestValue>(new RequestValue()), |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 158 | false /* has_more */); |
| 159 | ASSERT_EQ(1u, callback_log.size()); |
| 160 | EXPECT_EQ(base::File::FILE_OK, callback_log[0]); |
| 161 | } |
| 162 | |
| 163 | TEST_F(FileSystemProviderOperationsWriteFileTest, OnError) { |
| 164 | util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 165 | util::StatusCallbackLog callback_log; |
| 166 | |
| 167 | WriteFile write_file(NULL, |
| 168 | file_system_info_, |
| 169 | kFileHandle, |
| 170 | io_buffer_.get(), |
| 171 | kOffset, |
mtomasz | 3485d1a0 | 2014-09-03 07:34:20 | [diff] [blame] | 172 | io_buffer_->size(), |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 173 | base::Bind(&util::LogStatusCallback, &callback_log)); |
| 174 | write_file.SetDispatchEventImplForTesting( |
| 175 | base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 176 | base::Unretained(&dispatcher))); |
| 177 | |
| 178 | EXPECT_TRUE(write_file.Execute(kRequestId)); |
| 179 | |
| 180 | write_file.OnError(kRequestId, |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 181 | std::unique_ptr<RequestValue>(new RequestValue()), |
mtomasz@chromium.org | fa90215f | 2014-07-29 07:52:54 | [diff] [blame] | 182 | base::File::FILE_ERROR_TOO_MANY_OPENED); |
| 183 | |
| 184 | ASSERT_EQ(1u, callback_log.size()); |
| 185 | EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); |
| 186 | } |
| 187 | |
| 188 | } // namespace operations |
| 189 | } // namespace file_system_provider |
| 190 | } // namespace chromeos |