1 // Copyright (c) 2012 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 "webkit/browser/blob/local_file_stream_reader.h"
9 #include "base/file_util.h"
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/run_loop.h"
15 #include "base/threading/thread.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/test_completion_callback.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace webkit_blob {
25 const char kTestData[] = "0123456789";
26 const int kTestDataSize = arraysize(kTestData) - 1;
28 void ReadFromReader(LocalFileStreamReader* reader,
29 std::string* data, size_t size,
31 ASSERT_TRUE(reader != NULL);
32 ASSERT_TRUE(result != NULL);
34 net::TestCompletionCallback callback;
35 size_t total_bytes_read = 0;
36 while (total_bytes_read < size) {
37 scoped_refptr<net::IOBufferWithSize> buf(
38 new net::IOBufferWithSize(size - total_bytes_read));
39 int rv = reader->Read(buf.get(), buf->size(), callback.callback());
40 if (rv == net::ERR_IO_PENDING)
41 rv = callback.WaitForResult();
46 total_bytes_read += rv;
47 data->append(buf->data(), rv);
51 void NeverCalled(int) { ADD_FAILURE(); }
52 void EmptyCallback() {}
55 base::MessageLoop::current()->Quit();
60 class LocalFileStreamReaderTest : public testing::Test {
62 LocalFileStreamReaderTest()
63 : file_thread_("FileUtilProxyTestFileThread") {}
65 virtual void SetUp() OVERRIDE {
66 ASSERT_TRUE(file_thread_.Start());
67 ASSERT_TRUE(dir_.CreateUniqueTempDir());
69 file_util::WriteFile(test_path(), kTestData, kTestDataSize);
70 base::File::Info info;
71 ASSERT_TRUE(base::GetFileInfo(test_path(), &info));
72 test_file_modification_time_ = info.last_modified;
75 virtual void TearDown() OVERRIDE {
76 // Give another chance for deleted streams to perform Close.
77 base::RunLoop().RunUntilIdle();
79 base::RunLoop().RunUntilIdle();
83 LocalFileStreamReader* CreateFileReader(
84 const base::FilePath& path,
86 const base::Time& expected_modification_time) {
87 return new LocalFileStreamReader(
91 expected_modification_time);
94 void TouchTestFile() {
95 base::Time new_modified_time =
96 test_file_modification_time() - base::TimeDelta::FromSeconds(1);
97 ASSERT_TRUE(base::TouchFile(test_path(),
98 test_file_modification_time(),
102 base::MessageLoopProxy* file_task_runner() const {
103 return file_thread_.message_loop_proxy().get();
106 base::FilePath test_dir() const { return dir_.path(); }
107 base::FilePath test_path() const { return dir_.path().AppendASCII("test"); }
108 base::Time test_file_modification_time() const {
109 return test_file_modification_time_;
112 void EnsureFileTaskFinished() {
113 file_task_runner()->PostTaskAndReply(
114 FROM_HERE, base::Bind(&EmptyCallback), base::Bind(&QuitLoop));
115 base::MessageLoop::current()->Run();
119 base::MessageLoopForIO message_loop_;
120 base::Thread file_thread_;
121 base::ScopedTempDir dir_;
122 base::Time test_file_modification_time_;
125 TEST_F(LocalFileStreamReaderTest, NonExistent) {
126 base::FilePath nonexistent_path = test_dir().AppendASCII("nonexistent");
127 scoped_ptr<LocalFileStreamReader> reader(
128 CreateFileReader(nonexistent_path, 0, base::Time()));
131 ReadFromReader(reader.get(), &data, 10, &result);
132 ASSERT_EQ(net::ERR_FILE_NOT_FOUND, result);
133 ASSERT_EQ(0U, data.size());
136 TEST_F(LocalFileStreamReaderTest, Empty) {
137 base::FilePath empty_path = test_dir().AppendASCII("empty");
138 base::File file(empty_path, base::File::FLAG_CREATE | base::File::FLAG_READ);
139 ASSERT_TRUE(file.IsValid());
142 scoped_ptr<LocalFileStreamReader> reader(
143 CreateFileReader(empty_path, 0, base::Time()));
146 ReadFromReader(reader.get(), &data, 10, &result);
147 ASSERT_EQ(net::OK, result);
148 ASSERT_EQ(0U, data.size());
150 net::TestInt64CompletionCallback callback;
151 int64 length_result = reader->GetLength(callback.callback());
152 if (length_result == net::ERR_IO_PENDING)
153 length_result = callback.WaitForResult();
154 ASSERT_EQ(0, result);
157 TEST_F(LocalFileStreamReaderTest, GetLengthNormal) {
158 scoped_ptr<LocalFileStreamReader> reader(
159 CreateFileReader(test_path(), 0, test_file_modification_time()));
160 net::TestInt64CompletionCallback callback;
161 int64 result = reader->GetLength(callback.callback());
162 if (result == net::ERR_IO_PENDING)
163 result = callback.WaitForResult();
164 ASSERT_EQ(kTestDataSize, result);
167 TEST_F(LocalFileStreamReaderTest, GetLengthAfterModified) {
168 // Touch file so that the file's modification time becomes different
169 // from what we expect.
172 scoped_ptr<LocalFileStreamReader> reader(
173 CreateFileReader(test_path(), 0, test_file_modification_time()));
174 net::TestInt64CompletionCallback callback;
175 int64 result = reader->GetLength(callback.callback());
176 if (result == net::ERR_IO_PENDING)
177 result = callback.WaitForResult();
178 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result);
180 // With NULL expected modification time this should work.
181 reader.reset(CreateFileReader(test_path(), 0, base::Time()));
182 result = reader->GetLength(callback.callback());
183 if (result == net::ERR_IO_PENDING)
184 result = callback.WaitForResult();
185 ASSERT_EQ(kTestDataSize, result);
188 TEST_F(LocalFileStreamReaderTest, GetLengthWithOffset) {
189 scoped_ptr<LocalFileStreamReader> reader(
190 CreateFileReader(test_path(), 3, base::Time()));
191 net::TestInt64CompletionCallback callback;
192 int64 result = reader->GetLength(callback.callback());
193 if (result == net::ERR_IO_PENDING)
194 result = callback.WaitForResult();
195 // Initial offset does not affect the result of GetLength.
196 ASSERT_EQ(kTestDataSize, result);
199 TEST_F(LocalFileStreamReaderTest, ReadNormal) {
200 scoped_ptr<LocalFileStreamReader> reader(
201 CreateFileReader(test_path(), 0, test_file_modification_time()));
204 ReadFromReader(reader.get(), &data, kTestDataSize, &result);
205 ASSERT_EQ(net::OK, result);
206 ASSERT_EQ(kTestData, data);
209 TEST_F(LocalFileStreamReaderTest, ReadAfterModified) {
210 // Touch file so that the file's modification time becomes different
211 // from what we expect.
214 scoped_ptr<LocalFileStreamReader> reader(
215 CreateFileReader(test_path(), 0, test_file_modification_time()));
218 ReadFromReader(reader.get(), &data, kTestDataSize, &result);
219 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result);
220 ASSERT_EQ(0U, data.size());
222 // With NULL expected modification time this should work.
224 reader.reset(CreateFileReader(test_path(), 0, base::Time()));
225 ReadFromReader(reader.get(), &data, kTestDataSize, &result);
226 ASSERT_EQ(net::OK, result);
227 ASSERT_EQ(kTestData, data);
230 TEST_F(LocalFileStreamReaderTest, ReadWithOffset) {
231 scoped_ptr<LocalFileStreamReader> reader(
232 CreateFileReader(test_path(), 3, base::Time()));
235 ReadFromReader(reader.get(), &data, kTestDataSize, &result);
236 ASSERT_EQ(net::OK, result);
237 ASSERT_EQ(&kTestData[3], data);
240 TEST_F(LocalFileStreamReaderTest, DeleteWithUnfinishedRead) {
241 scoped_ptr<LocalFileStreamReader> reader(
242 CreateFileReader(test_path(), 0, base::Time()));
244 net::TestCompletionCallback callback;
245 scoped_refptr<net::IOBufferWithSize> buf(
246 new net::IOBufferWithSize(kTestDataSize));
247 int rv = reader->Read(buf.get(), buf->size(), base::Bind(&NeverCalled));
248 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0);
250 // Delete immediately.
251 // Should not crash; nor should NeverCalled be callback.
253 EnsureFileTaskFinished();
256 } // namespace webkit_blob