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 "storage/browser/fileapi/local_file_stream_writer.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <memory> | |
10 #include <string> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/files/file_util.h" | |
14 #include "base/files/scoped_temp_dir.h" | |
15 #include "base/logging.h" | |
16 #include "base/run_loop.h" | |
17 #include "base/single_thread_task_runner.h" | |
18 #include "base/threading/thread.h" | |
19 #include "net/base/io_buffer.h" | |
20 #include "net/base/test_completion_callback.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 using storage::FileStreamWriter; | |
24 using storage::LocalFileStreamWriter; | |
25 | |
26 namespace content { | |
27 | |
28 class LocalFileStreamWriterTest : public testing::Test { | |
29 public: | |
30 LocalFileStreamWriterTest() | |
31 : file_thread_("FileUtilProxyTestFileThread") {} | |
32 | |
33 void SetUp() override { | |
34 ASSERT_TRUE(file_thread_.Start()); | |
35 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
36 } | |
37 | |
38 void TearDown() override { | |
39 // Give another chance for deleted streams to perform Close. | |
40 base::RunLoop().RunUntilIdle(); | |
41 file_thread_.Stop(); | |
42 base::RunLoop().RunUntilIdle(); | |
43 } | |
44 | |
45 protected: | |
46 base::FilePath Path(const std::string& name) { | |
47 return temp_dir_.GetPath().AppendASCII(name); | |
48 } | |
49 | |
50 int WriteStringToWriter(LocalFileStreamWriter* writer, | |
51 const std::string& data) { | |
52 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data)); | |
53 scoped_refptr<net::DrainableIOBuffer> drainable( | |
54 new net::DrainableIOBuffer(buffer.get(), buffer->size())); | |
55 | |
56 while (drainable->BytesRemaining() > 0) { | |
57 net::TestCompletionCallback callback; | |
58 int result = writer->Write( | |
59 drainable.get(), drainable->BytesRemaining(), callback.callback()); | |
60 if (result == net::ERR_IO_PENDING) | |
61 result = callback.WaitForResult(); | |
62 if (result <= 0) | |
63 return result; | |
64 drainable->DidConsume(result); | |
65 } | |
66 return net::OK; | |
67 } | |
68 | |
69 std::string GetFileContent(const base::FilePath& path) { | |
70 std::string content; | |
71 base::ReadFileToString(path, &content); | |
72 return content; | |
73 } | |
74 | |
75 base::FilePath CreateFileWithContent(const std::string& name, | |
76 const std::string& data) { | |
77 base::FilePath path = Path(name); | |
78 base::WriteFile(path, data.c_str(), data.size()); | |
79 return path; | |
80 } | |
81 | |
82 base::SingleThreadTaskRunner* file_task_runner() const { | |
83 return file_thread_.task_runner().get(); | |
84 } | |
85 | |
86 LocalFileStreamWriter* CreateWriter(const base::FilePath& path, | |
87 int64_t offset) { | |
88 return new LocalFileStreamWriter(file_task_runner(), path, offset, | |
89 FileStreamWriter::OPEN_EXISTING_FILE); | |
90 } | |
91 | |
92 private: | |
93 base::MessageLoopForIO message_loop_; | |
94 base::Thread file_thread_; | |
95 base::ScopedTempDir temp_dir_; | |
96 }; | |
97 | |
98 void NeverCalled(int unused) { | |
99 ADD_FAILURE(); | |
100 } | |
101 | |
102 TEST_F(LocalFileStreamWriterTest, Write) { | |
103 base::FilePath path = CreateFileWithContent("file_a", std::string()); | |
104 std::unique_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); | |
105 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); | |
106 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar")); | |
107 writer.reset(); | |
108 base::RunLoop().RunUntilIdle(); | |
109 EXPECT_TRUE(base::PathExists(path)); | |
110 EXPECT_EQ("foobar", GetFileContent(path)); | |
111 } | |
112 | |
113 TEST_F(LocalFileStreamWriterTest, WriteMiddle) { | |
114 base::FilePath path = CreateFileWithContent("file_a", "foobar"); | |
115 std::unique_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 2)); | |
116 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); | |
117 writer.reset(); | |
118 base::RunLoop().RunUntilIdle(); | |
119 EXPECT_TRUE(base::PathExists(path)); | |
120 EXPECT_EQ("foxxxr", GetFileContent(path)); | |
121 } | |
122 | |
123 TEST_F(LocalFileStreamWriterTest, WriteEnd) { | |
124 base::FilePath path = CreateFileWithContent("file_a", "foobar"); | |
125 std::unique_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 6)); | |
126 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); | |
127 writer.reset(); | |
128 base::RunLoop().RunUntilIdle(); | |
129 EXPECT_TRUE(base::PathExists(path)); | |
130 EXPECT_EQ("foobarxxx", GetFileContent(path)); | |
131 } | |
132 | |
133 TEST_F(LocalFileStreamWriterTest, WriteFailForNonexistingFile) { | |
134 base::FilePath path = Path("file_a"); | |
135 ASSERT_FALSE(base::PathExists(path)); | |
136 std::unique_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); | |
137 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, WriteStringToWriter(writer.get(), "foo")); | |
138 writer.reset(); | |
139 base::RunLoop().RunUntilIdle(); | |
140 EXPECT_FALSE(base::PathExists(path)); | |
141 } | |
142 | |
143 TEST_F(LocalFileStreamWriterTest, CancelBeforeOperation) { | |
144 base::FilePath path = Path("file_a"); | |
145 std::unique_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); | |
146 // Cancel immediately fails when there's no in-flight operation. | |
147 int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); | |
148 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); | |
149 } | |
150 | |
151 TEST_F(LocalFileStreamWriterTest, CancelAfterFinishedOperation) { | |
152 base::FilePath path = CreateFileWithContent("file_a", std::string()); | |
153 std::unique_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); | |
154 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); | |
155 | |
156 // Cancel immediately fails when there's no in-flight operation. | |
157 int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); | |
158 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); | |
159 | |
160 writer.reset(); | |
161 base::RunLoop().RunUntilIdle(); | |
162 // Write operation is already completed. | |
163 EXPECT_TRUE(base::PathExists(path)); | |
164 EXPECT_EQ("foo", GetFileContent(path)); | |
165 } | |
166 | |
167 TEST_F(LocalFileStreamWriterTest, CancelWrite) { | |
168 base::FilePath path = CreateFileWithContent("file_a", "foobar"); | |
169 std::unique_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); | |
170 | |
171 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx")); | |
172 int result = | |
173 writer->Write(buffer.get(), buffer->size(), base::Bind(&NeverCalled)); | |
174 ASSERT_EQ(net::ERR_IO_PENDING, result); | |
175 | |
176 net::TestCompletionCallback callback; | |
177 writer->Cancel(callback.callback()); | |
178 int cancel_result = callback.WaitForResult(); | |
179 EXPECT_EQ(net::OK, cancel_result); | |
180 } | |
181 | |
182 } // namespace content | |
OLD | NEW |