Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: content/browser/fileapi/local_file_stream_reader_unittest.cc

Issue 2775383003: Move some fileapi tests next to the files they cover. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_reader.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <memory>
11 #include <string>
12
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/files/scoped_temp_dir.h"
17 #include "base/location.h"
18 #include "base/macros.h"
19 #include "base/run_loop.h"
20 #include "base/single_thread_task_runner.h"
21 #include "base/threading/thread.h"
22 #include "net/base/io_buffer.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/test_completion_callback.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 using storage::LocalFileStreamReader;
28
29 namespace content {
30
31 namespace {
32
33 const char kTestData[] = "0123456789";
34 const int kTestDataSize = arraysize(kTestData) - 1;
35
36 void ReadFromReader(LocalFileStreamReader* reader,
37 std::string* data, size_t size,
38 int* result) {
39 ASSERT_TRUE(reader != NULL);
40 ASSERT_TRUE(result != NULL);
41 *result = net::OK;
42 net::TestCompletionCallback callback;
43 size_t total_bytes_read = 0;
44 while (total_bytes_read < size) {
45 scoped_refptr<net::IOBufferWithSize> buf(
46 new net::IOBufferWithSize(size - total_bytes_read));
47 int rv = reader->Read(buf.get(), buf->size(), callback.callback());
48 if (rv == net::ERR_IO_PENDING)
49 rv = callback.WaitForResult();
50 if (rv < 0)
51 *result = rv;
52 if (rv <= 0)
53 break;
54 total_bytes_read += rv;
55 data->append(buf->data(), rv);
56 }
57 }
58
59 void NeverCalled(int) { ADD_FAILURE(); }
60 void EmptyCallback() {}
61
62 void QuitLoop() {
63 base::MessageLoop::current()->QuitWhenIdle();
64 }
65
66 } // namespace
67
68 class LocalFileStreamReaderTest : public testing::Test {
69 public:
70 LocalFileStreamReaderTest()
71 : file_thread_("FileUtilProxyTestFileThread") {}
72
73 void SetUp() override {
74 ASSERT_TRUE(file_thread_.Start());
75 ASSERT_TRUE(dir_.CreateUniqueTempDir());
76
77 base::WriteFile(test_path(), kTestData, kTestDataSize);
78 base::File::Info info;
79 ASSERT_TRUE(base::GetFileInfo(test_path(), &info));
80 test_file_modification_time_ = info.last_modified;
81 }
82
83 void TearDown() override {
84 // Give another chance for deleted streams to perform Close.
85 base::RunLoop().RunUntilIdle();
86 file_thread_.Stop();
87 base::RunLoop().RunUntilIdle();
88 }
89
90 protected:
91 LocalFileStreamReader* CreateFileReader(
92 const base::FilePath& path,
93 int64_t initial_offset,
94 const base::Time& expected_modification_time) {
95 return new LocalFileStreamReader(
96 file_task_runner(),
97 path,
98 initial_offset,
99 expected_modification_time);
100 }
101
102 void TouchTestFile(base::TimeDelta delta) {
103 base::Time new_modified_time = test_file_modification_time() + delta;
104 ASSERT_TRUE(base::TouchFile(test_path(),
105 test_file_modification_time(),
106 new_modified_time));
107 }
108
109 base::SingleThreadTaskRunner* file_task_runner() const {
110 return file_thread_.task_runner().get();
111 }
112
113 base::FilePath test_dir() const { return dir_.GetPath(); }
114 base::FilePath test_path() const {
115 return dir_.GetPath().AppendASCII("test");
116 }
117 base::Time test_file_modification_time() const {
118 return test_file_modification_time_;
119 }
120
121 void EnsureFileTaskFinished() {
122 file_task_runner()->PostTaskAndReply(
123 FROM_HERE, base::Bind(&EmptyCallback), base::Bind(&QuitLoop));
124 base::RunLoop().Run();
125 }
126
127 private:
128 base::MessageLoopForIO message_loop_;
129 base::Thread file_thread_;
130 base::ScopedTempDir dir_;
131 base::Time test_file_modification_time_;
132 };
133
134 TEST_F(LocalFileStreamReaderTest, NonExistent) {
135 base::FilePath nonexistent_path = test_dir().AppendASCII("nonexistent");
136 std::unique_ptr<LocalFileStreamReader> reader(
137 CreateFileReader(nonexistent_path, 0, base::Time()));
138 int result = 0;
139 std::string data;
140 ReadFromReader(reader.get(), &data, 10, &result);
141 ASSERT_EQ(net::ERR_FILE_NOT_FOUND, result);
142 ASSERT_EQ(0U, data.size());
143 }
144
145 TEST_F(LocalFileStreamReaderTest, Empty) {
146 base::FilePath empty_path = test_dir().AppendASCII("empty");
147 base::File file(empty_path, base::File::FLAG_CREATE | base::File::FLAG_READ);
148 ASSERT_TRUE(file.IsValid());
149 file.Close();
150
151 std::unique_ptr<LocalFileStreamReader> reader(
152 CreateFileReader(empty_path, 0, base::Time()));
153 int result = 0;
154 std::string data;
155 ReadFromReader(reader.get(), &data, 10, &result);
156 ASSERT_EQ(net::OK, result);
157 ASSERT_EQ(0U, data.size());
158
159 net::TestInt64CompletionCallback callback;
160 int64_t length_result = reader->GetLength(callback.callback());
161 if (length_result == net::ERR_IO_PENDING)
162 length_result = callback.WaitForResult();
163 ASSERT_EQ(0, result);
164 }
165
166 TEST_F(LocalFileStreamReaderTest, GetLengthNormal) {
167 std::unique_ptr<LocalFileStreamReader> reader(
168 CreateFileReader(test_path(), 0, test_file_modification_time()));
169 net::TestInt64CompletionCallback callback;
170 int64_t result = reader->GetLength(callback.callback());
171 if (result == net::ERR_IO_PENDING)
172 result = callback.WaitForResult();
173 ASSERT_EQ(kTestDataSize, result);
174 }
175
176 TEST_F(LocalFileStreamReaderTest, GetLengthAfterModified) {
177 // Touch file so that the file's modification time becomes different
178 // from what we expect.
179 TouchTestFile(base::TimeDelta::FromSeconds(-1));
180
181 std::unique_ptr<LocalFileStreamReader> reader(
182 CreateFileReader(test_path(), 0, test_file_modification_time()));
183 net::TestInt64CompletionCallback callback;
184 int64_t result = reader->GetLength(callback.callback());
185 if (result == net::ERR_IO_PENDING)
186 result = callback.WaitForResult();
187 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result);
188
189 // With NULL expected modification time this should work.
190 reader.reset(CreateFileReader(test_path(), 0, base::Time()));
191 result = reader->GetLength(callback.callback());
192 if (result == net::ERR_IO_PENDING)
193 result = callback.WaitForResult();
194 ASSERT_EQ(kTestDataSize, result);
195 }
196
197 TEST_F(LocalFileStreamReaderTest, GetLengthWithOffset) {
198 std::unique_ptr<LocalFileStreamReader> reader(
199 CreateFileReader(test_path(), 3, base::Time()));
200 net::TestInt64CompletionCallback callback;
201 int64_t result = reader->GetLength(callback.callback());
202 if (result == net::ERR_IO_PENDING)
203 result = callback.WaitForResult();
204 // Initial offset does not affect the result of GetLength.
205 ASSERT_EQ(kTestDataSize, result);
206 }
207
208 TEST_F(LocalFileStreamReaderTest, ReadNormal) {
209 std::unique_ptr<LocalFileStreamReader> reader(
210 CreateFileReader(test_path(), 0, test_file_modification_time()));
211 int result = 0;
212 std::string data;
213 ReadFromReader(reader.get(), &data, kTestDataSize, &result);
214 ASSERT_EQ(net::OK, result);
215 ASSERT_EQ(kTestData, data);
216 }
217
218 TEST_F(LocalFileStreamReaderTest, ReadAfterModified) {
219 // Touch file so that the file's modification time becomes different
220 // from what we expect. Note that the resolution on some filesystems
221 // is 1s so we can't test with deltas less than that.
222 TouchTestFile(base::TimeDelta::FromSeconds(-1));
223 std::unique_ptr<LocalFileStreamReader> reader(
224 CreateFileReader(test_path(), 0, test_file_modification_time()));
225 int result = 0;
226 std::string data;
227 ReadFromReader(reader.get(), &data, kTestDataSize, &result);
228 EXPECT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result);
229 EXPECT_EQ(0U, data.size());
230
231 // Due to precision loss converting int64_t->double->int64_t (e.g. through
232 // Blink) the expected/actual time may vary by microseconds. With
233 // modification time delta < 10us this should work.
234 TouchTestFile(base::TimeDelta::FromMicroseconds(1));
235 data.clear();
236 reader.reset(CreateFileReader(test_path(), 0, test_file_modification_time()));
237 ReadFromReader(reader.get(), &data, kTestDataSize, &result);
238 EXPECT_EQ(net::OK, result);
239 EXPECT_EQ(kTestData, data);
240
241 // With matching modification times time this should work.
242 TouchTestFile(base::TimeDelta());
243 data.clear();
244 reader.reset(CreateFileReader(test_path(), 0, test_file_modification_time()));
245 ReadFromReader(reader.get(), &data, kTestDataSize, &result);
246 EXPECT_EQ(net::OK, result);
247 EXPECT_EQ(kTestData, data);
248
249 // And with NULL expected modification time this should work.
250 data.clear();
251 reader.reset(CreateFileReader(test_path(), 0, base::Time()));
252 ReadFromReader(reader.get(), &data, kTestDataSize, &result);
253 EXPECT_EQ(net::OK, result);
254 EXPECT_EQ(kTestData, data);
255 }
256
257 TEST_F(LocalFileStreamReaderTest, ReadWithOffset) {
258 std::unique_ptr<LocalFileStreamReader> reader(
259 CreateFileReader(test_path(), 3, base::Time()));
260 int result = 0;
261 std::string data;
262 ReadFromReader(reader.get(), &data, kTestDataSize, &result);
263 ASSERT_EQ(net::OK, result);
264 ASSERT_EQ(&kTestData[3], data);
265 }
266
267 TEST_F(LocalFileStreamReaderTest, DeleteWithUnfinishedRead) {
268 std::unique_ptr<LocalFileStreamReader> reader(
269 CreateFileReader(test_path(), 0, base::Time()));
270
271 net::TestCompletionCallback callback;
272 scoped_refptr<net::IOBufferWithSize> buf(
273 new net::IOBufferWithSize(kTestDataSize));
274 int rv = reader->Read(buf.get(), buf->size(), base::Bind(&NeverCalled));
275 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0);
276
277 // Delete immediately.
278 // Should not crash; nor should NeverCalled be callback.
279 reader.reset();
280 EnsureFileTaskFinished();
281 }
282
283 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/fileapi/isolated_context_unittest.cc ('k') | content/browser/fileapi/local_file_stream_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698