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 "storage/browser/fileapi/local_file_stream_writer.h"
9 #include "base/callback.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/threading/thread.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/test_completion_callback.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using storage::FileStreamWriter
;
22 using storage::LocalFileStreamWriter
;
26 class LocalFileStreamWriterTest
: public testing::Test
{
28 LocalFileStreamWriterTest()
29 : file_thread_("FileUtilProxyTestFileThread") {}
31 void SetUp() override
{
32 ASSERT_TRUE(file_thread_
.Start());
33 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
36 void TearDown() override
{
37 // Give another chance for deleted streams to perform Close.
38 base::RunLoop().RunUntilIdle();
40 base::RunLoop().RunUntilIdle();
44 base::FilePath
Path(const std::string
& name
) {
45 return temp_dir_
.path().AppendASCII(name
);
48 int WriteStringToWriter(LocalFileStreamWriter
* writer
,
49 const std::string
& data
) {
50 scoped_refptr
<net::StringIOBuffer
> buffer(new net::StringIOBuffer(data
));
51 scoped_refptr
<net::DrainableIOBuffer
> drainable(
52 new net::DrainableIOBuffer(buffer
.get(), buffer
->size()));
54 while (drainable
->BytesRemaining() > 0) {
55 net::TestCompletionCallback callback
;
56 int result
= writer
->Write(
57 drainable
.get(), drainable
->BytesRemaining(), callback
.callback());
58 if (result
== net::ERR_IO_PENDING
)
59 result
= callback
.WaitForResult();
62 drainable
->DidConsume(result
);
67 std::string
GetFileContent(const base::FilePath
& path
) {
69 base::ReadFileToString(path
, &content
);
73 base::FilePath
CreateFileWithContent(const std::string
& name
,
74 const std::string
& data
) {
75 base::FilePath path
= Path(name
);
76 base::WriteFile(path
, data
.c_str(), data
.size());
80 base::SingleThreadTaskRunner
* file_task_runner() const {
81 return file_thread_
.task_runner().get();
84 LocalFileStreamWriter
* CreateWriter(const base::FilePath
& path
,
86 return new LocalFileStreamWriter(file_task_runner(), path
, offset
,
87 FileStreamWriter::OPEN_EXISTING_FILE
);
91 base::MessageLoopForIO message_loop_
;
92 base::Thread file_thread_
;
93 base::ScopedTempDir temp_dir_
;
96 void NeverCalled(int unused
) {
100 TEST_F(LocalFileStreamWriterTest
, Write
) {
101 base::FilePath path
= CreateFileWithContent("file_a", std::string());
102 scoped_ptr
<LocalFileStreamWriter
> writer(CreateWriter(path
, 0));
103 EXPECT_EQ(net::OK
, WriteStringToWriter(writer
.get(), "foo"));
104 EXPECT_EQ(net::OK
, WriteStringToWriter(writer
.get(), "bar"));
106 base::RunLoop().RunUntilIdle();
107 EXPECT_TRUE(base::PathExists(path
));
108 EXPECT_EQ("foobar", GetFileContent(path
));
111 TEST_F(LocalFileStreamWriterTest
, WriteMiddle
) {
112 base::FilePath path
= CreateFileWithContent("file_a", "foobar");
113 scoped_ptr
<LocalFileStreamWriter
> writer(CreateWriter(path
, 2));
114 EXPECT_EQ(net::OK
, WriteStringToWriter(writer
.get(), "xxx"));
116 base::RunLoop().RunUntilIdle();
117 EXPECT_TRUE(base::PathExists(path
));
118 EXPECT_EQ("foxxxr", GetFileContent(path
));
121 TEST_F(LocalFileStreamWriterTest
, WriteEnd
) {
122 base::FilePath path
= CreateFileWithContent("file_a", "foobar");
123 scoped_ptr
<LocalFileStreamWriter
> writer(CreateWriter(path
, 6));
124 EXPECT_EQ(net::OK
, WriteStringToWriter(writer
.get(), "xxx"));
126 base::RunLoop().RunUntilIdle();
127 EXPECT_TRUE(base::PathExists(path
));
128 EXPECT_EQ("foobarxxx", GetFileContent(path
));
131 TEST_F(LocalFileStreamWriterTest
, WriteFailForNonexistingFile
) {
132 base::FilePath path
= Path("file_a");
133 ASSERT_FALSE(base::PathExists(path
));
134 scoped_ptr
<LocalFileStreamWriter
> writer(CreateWriter(path
, 0));
135 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, WriteStringToWriter(writer
.get(), "foo"));
137 base::RunLoop().RunUntilIdle();
138 EXPECT_FALSE(base::PathExists(path
));
141 TEST_F(LocalFileStreamWriterTest
, CancelBeforeOperation
) {
142 base::FilePath path
= Path("file_a");
143 scoped_ptr
<LocalFileStreamWriter
> writer(CreateWriter(path
, 0));
144 // Cancel immediately fails when there's no in-flight operation.
145 int cancel_result
= writer
->Cancel(base::Bind(&NeverCalled
));
146 EXPECT_EQ(net::ERR_UNEXPECTED
, cancel_result
);
149 TEST_F(LocalFileStreamWriterTest
, CancelAfterFinishedOperation
) {
150 base::FilePath path
= CreateFileWithContent("file_a", std::string());
151 scoped_ptr
<LocalFileStreamWriter
> writer(CreateWriter(path
, 0));
152 EXPECT_EQ(net::OK
, WriteStringToWriter(writer
.get(), "foo"));
154 // Cancel immediately fails when there's no in-flight operation.
155 int cancel_result
= writer
->Cancel(base::Bind(&NeverCalled
));
156 EXPECT_EQ(net::ERR_UNEXPECTED
, cancel_result
);
159 base::RunLoop().RunUntilIdle();
160 // Write operation is already completed.
161 EXPECT_TRUE(base::PathExists(path
));
162 EXPECT_EQ("foo", GetFileContent(path
));
165 TEST_F(LocalFileStreamWriterTest
, CancelWrite
) {
166 base::FilePath path
= CreateFileWithContent("file_a", "foobar");
167 scoped_ptr
<LocalFileStreamWriter
> writer(CreateWriter(path
, 0));
169 scoped_refptr
<net::StringIOBuffer
> buffer(new net::StringIOBuffer("xxx"));
171 writer
->Write(buffer
.get(), buffer
->size(), base::Bind(&NeverCalled
));
172 ASSERT_EQ(net::ERR_IO_PENDING
, result
);
174 net::TestCompletionCallback callback
;
175 writer
->Cancel(callback
.callback());
176 int cancel_result
= callback
.WaitForResult();
177 EXPECT_EQ(net::OK
, cancel_result
);
180 } // namespace content