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/operations/open_file.h"
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
16 #include "chrome/common/extensions/api/file_system_provider.h"
17 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.h"
18 #include "chrome/common/extensions/api/file_system_provider_internal.h"
19 #include "extensions/browser/event_router.h"
20 #include "storage/browser/fileapi/async_file_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
24 namespace file_system_provider
{
25 namespace operations
{
28 const char kExtensionId
[] = "mbflcebpggnecokmikipoihdbecnjfoj";
29 const char kFileSystemId
[] = "testing-file-system";
30 const int kRequestId
= 2;
31 const base::FilePath::CharType kFilePath
[] =
32 FILE_PATH_LITERAL("/directory/blueberries.txt");
34 // Callback invocation logger. Acts as a fileapi end-point.
35 class CallbackLogger
{
39 Event(int file_handle
, base::File::Error result
)
40 : file_handle_(file_handle
), result_(result
) {}
43 int file_handle() { return file_handle_
; }
44 base::File::Error
result() { return result_
; }
48 base::File::Error result_
;
50 DISALLOW_COPY_AND_ASSIGN(Event
);
54 virtual ~CallbackLogger() {}
56 void OnOpenFile(int file_handle
, base::File::Error result
) {
57 events_
.push_back(new Event(file_handle
, result
));
60 ScopedVector
<Event
>& events() { return events_
; }
63 ScopedVector
<Event
> events_
;
65 DISALLOW_COPY_AND_ASSIGN(CallbackLogger
);
70 class FileSystemProviderOperationsOpenFileTest
: public testing::Test
{
72 FileSystemProviderOperationsOpenFileTest() {}
73 ~FileSystemProviderOperationsOpenFileTest() override
{}
75 void SetUp() override
{
76 file_system_info_
= ProvidedFileSystemInfo(
77 kExtensionId
, MountOptions(kFileSystemId
, "" /* display_name */),
78 base::FilePath(), false /* configurable */, true /* watchable */,
79 extensions::SOURCE_FILE
);
82 ProvidedFileSystemInfo file_system_info_
;
85 TEST_F(FileSystemProviderOperationsOpenFileTest
, Execute
) {
86 using extensions::api::file_system_provider::OpenFileRequestedOptions
;
88 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
89 CallbackLogger callback_logger
;
91 OpenFile
open_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
93 base::Bind(&CallbackLogger::OnOpenFile
,
94 base::Unretained(&callback_logger
)));
95 open_file
.SetDispatchEventImplForTesting(
96 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
97 base::Unretained(&dispatcher
)));
99 EXPECT_TRUE(open_file
.Execute(kRequestId
));
101 ASSERT_EQ(1u, dispatcher
.events().size());
102 extensions::Event
* event
= dispatcher
.events()[0];
104 extensions::api::file_system_provider::OnOpenFileRequested::kEventName
,
106 base::ListValue
* event_args
= event
->event_args
.get();
107 ASSERT_EQ(1u, event_args
->GetSize());
109 const base::DictionaryValue
* options_as_value
= NULL
;
110 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
112 OpenFileRequestedOptions options
;
113 ASSERT_TRUE(OpenFileRequestedOptions::Populate(*options_as_value
, &options
));
114 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
115 EXPECT_EQ(kRequestId
, options
.request_id
);
116 EXPECT_EQ(kFilePath
, options
.file_path
);
117 EXPECT_EQ(extensions::api::file_system_provider::OPEN_FILE_MODE_READ
,
121 TEST_F(FileSystemProviderOperationsOpenFileTest
, Execute_NoListener
) {
122 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
123 CallbackLogger callback_logger
;
125 OpenFile
open_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
127 base::Bind(&CallbackLogger::OnOpenFile
,
128 base::Unretained(&callback_logger
)));
129 open_file
.SetDispatchEventImplForTesting(
130 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
131 base::Unretained(&dispatcher
)));
133 EXPECT_FALSE(open_file
.Execute(kRequestId
));
136 TEST_F(FileSystemProviderOperationsOpenFileTest
, Execute_ReadOnly
) {
137 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
138 CallbackLogger callback_logger
;
140 const ProvidedFileSystemInfo
read_only_file_system_info(
141 kExtensionId
, MountOptions(kFileSystemId
, "" /* display_name */),
142 base::FilePath() /* mount_path */, false /* configurable */,
143 true /* watchable */, extensions::SOURCE_FILE
);
145 // Opening for read on a read-only file system is allowed.
147 OpenFile
open_file(NULL
, read_only_file_system_info
,
148 base::FilePath(kFilePath
), OPEN_FILE_MODE_READ
,
149 base::Bind(&CallbackLogger::OnOpenFile
,
150 base::Unretained(&callback_logger
)));
151 open_file
.SetDispatchEventImplForTesting(
152 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
153 base::Unretained(&dispatcher
)));
155 EXPECT_TRUE(open_file
.Execute(kRequestId
));
158 // Opening for write on a read-only file system is forbidden and must fail.
160 OpenFile
open_file(NULL
, read_only_file_system_info
,
161 base::FilePath(kFilePath
), OPEN_FILE_MODE_WRITE
,
162 base::Bind(&CallbackLogger::OnOpenFile
,
163 base::Unretained(&callback_logger
)));
164 open_file
.SetDispatchEventImplForTesting(
165 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
166 base::Unretained(&dispatcher
)));
168 EXPECT_FALSE(open_file
.Execute(kRequestId
));
172 TEST_F(FileSystemProviderOperationsOpenFileTest
, OnSuccess
) {
173 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
174 CallbackLogger callback_logger
;
176 OpenFile
open_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
178 base::Bind(&CallbackLogger::OnOpenFile
,
179 base::Unretained(&callback_logger
)));
180 open_file
.SetDispatchEventImplForTesting(
181 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
182 base::Unretained(&dispatcher
)));
184 EXPECT_TRUE(open_file
.Execute(kRequestId
));
186 open_file
.OnSuccess(kRequestId
,
187 scoped_ptr
<RequestValue
>(new RequestValue()),
188 false /* has_more */);
189 ASSERT_EQ(1u, callback_logger
.events().size());
190 CallbackLogger::Event
* event
= callback_logger
.events()[0];
191 EXPECT_EQ(base::File::FILE_OK
, event
->result());
192 EXPECT_LT(0, event
->file_handle());
195 TEST_F(FileSystemProviderOperationsOpenFileTest
, OnError
) {
196 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
197 CallbackLogger callback_logger
;
199 OpenFile
open_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
201 base::Bind(&CallbackLogger::OnOpenFile
,
202 base::Unretained(&callback_logger
)));
203 open_file
.SetDispatchEventImplForTesting(
204 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
205 base::Unretained(&dispatcher
)));
207 EXPECT_TRUE(open_file
.Execute(kRequestId
));
209 open_file
.OnError(kRequestId
,
210 scoped_ptr
<RequestValue
>(new RequestValue()),
211 base::File::FILE_ERROR_TOO_MANY_OPENED
);
212 ASSERT_EQ(1u, callback_logger
.events().size());
213 CallbackLogger::Event
* event
= callback_logger
.events()[0];
214 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, event
->result());
215 ASSERT_EQ(0, event
->file_handle());
218 } // namespace operations
219 } // namespace file_system_provider
220 } // namespace chromeos