OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "storage/browser/fileapi/local_file_stream_reader.h" | 5 #include "storage/browser/fileapi/local_file_stream_reader.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 const base::FilePath& path, | 88 const base::FilePath& path, |
89 int64 initial_offset, | 89 int64 initial_offset, |
90 const base::Time& expected_modification_time) { | 90 const base::Time& expected_modification_time) { |
91 return new LocalFileStreamReader( | 91 return new LocalFileStreamReader( |
92 file_task_runner(), | 92 file_task_runner(), |
93 path, | 93 path, |
94 initial_offset, | 94 initial_offset, |
95 expected_modification_time); | 95 expected_modification_time); |
96 } | 96 } |
97 | 97 |
98 void TouchTestFile() { | 98 void TouchTestFile(base::TimeDelta delta) { |
99 base::Time new_modified_time = | 99 base::Time new_modified_time = test_file_modification_time() + delta; |
100 test_file_modification_time() - base::TimeDelta::FromSeconds(1); | |
101 ASSERT_TRUE(base::TouchFile(test_path(), | 100 ASSERT_TRUE(base::TouchFile(test_path(), |
102 test_file_modification_time(), | 101 test_file_modification_time(), |
103 new_modified_time)); | 102 new_modified_time)); |
104 } | 103 } |
105 | 104 |
106 base::SingleThreadTaskRunner* file_task_runner() const { | 105 base::SingleThreadTaskRunner* file_task_runner() const { |
107 return file_thread_.task_runner().get(); | 106 return file_thread_.task_runner().get(); |
108 } | 107 } |
109 | 108 |
110 base::FilePath test_dir() const { return dir_.path(); } | 109 base::FilePath test_dir() const { return dir_.path(); } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 net::TestInt64CompletionCallback callback; | 163 net::TestInt64CompletionCallback callback; |
165 int64 result = reader->GetLength(callback.callback()); | 164 int64 result = reader->GetLength(callback.callback()); |
166 if (result == net::ERR_IO_PENDING) | 165 if (result == net::ERR_IO_PENDING) |
167 result = callback.WaitForResult(); | 166 result = callback.WaitForResult(); |
168 ASSERT_EQ(kTestDataSize, result); | 167 ASSERT_EQ(kTestDataSize, result); |
169 } | 168 } |
170 | 169 |
171 TEST_F(LocalFileStreamReaderTest, GetLengthAfterModified) { | 170 TEST_F(LocalFileStreamReaderTest, GetLengthAfterModified) { |
172 // Touch file so that the file's modification time becomes different | 171 // Touch file so that the file's modification time becomes different |
173 // from what we expect. | 172 // from what we expect. |
174 TouchTestFile(); | 173 TouchTestFile(base::TimeDelta::FromSeconds(-1)); |
175 | 174 |
176 scoped_ptr<LocalFileStreamReader> reader( | 175 scoped_ptr<LocalFileStreamReader> reader( |
177 CreateFileReader(test_path(), 0, test_file_modification_time())); | 176 CreateFileReader(test_path(), 0, test_file_modification_time())); |
178 net::TestInt64CompletionCallback callback; | 177 net::TestInt64CompletionCallback callback; |
179 int64 result = reader->GetLength(callback.callback()); | 178 int64 result = reader->GetLength(callback.callback()); |
180 if (result == net::ERR_IO_PENDING) | 179 if (result == net::ERR_IO_PENDING) |
181 result = callback.WaitForResult(); | 180 result = callback.WaitForResult(); |
182 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); | 181 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); |
183 | 182 |
184 // With NULL expected modification time this should work. | 183 // With NULL expected modification time this should work. |
(...skipping 20 matching lines...) Expand all Loading... |
205 CreateFileReader(test_path(), 0, test_file_modification_time())); | 204 CreateFileReader(test_path(), 0, test_file_modification_time())); |
206 int result = 0; | 205 int result = 0; |
207 std::string data; | 206 std::string data; |
208 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 207 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
209 ASSERT_EQ(net::OK, result); | 208 ASSERT_EQ(net::OK, result); |
210 ASSERT_EQ(kTestData, data); | 209 ASSERT_EQ(kTestData, data); |
211 } | 210 } |
212 | 211 |
213 TEST_F(LocalFileStreamReaderTest, ReadAfterModified) { | 212 TEST_F(LocalFileStreamReaderTest, ReadAfterModified) { |
214 // Touch file so that the file's modification time becomes different | 213 // Touch file so that the file's modification time becomes different |
215 // from what we expect. | 214 // from what we expect. Note that the resolution on some filesystems |
216 TouchTestFile(); | 215 // is 1s so we can't test with deltas less than that. |
217 | 216 TouchTestFile(base::TimeDelta::FromSeconds(-1)); |
218 scoped_ptr<LocalFileStreamReader> reader( | 217 scoped_ptr<LocalFileStreamReader> reader( |
219 CreateFileReader(test_path(), 0, test_file_modification_time())); | 218 CreateFileReader(test_path(), 0, test_file_modification_time())); |
220 int result = 0; | 219 int result = 0; |
221 std::string data; | 220 std::string data; |
222 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 221 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
223 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); | 222 EXPECT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); |
224 ASSERT_EQ(0U, data.size()); | 223 EXPECT_EQ(0U, data.size()); |
225 | 224 |
226 // With NULL expected modification time this should work. | 225 // Due to precision loss converting int64->double->int64 (e.g. through |
| 226 // Blink) the expected/actual time may vary by microseconds. With |
| 227 // modification time delta < 10us this should work. |
| 228 TouchTestFile(base::TimeDelta::FromMicroseconds(1)); |
| 229 data.clear(); |
| 230 reader.reset(CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 231 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| 232 EXPECT_EQ(net::OK, result); |
| 233 EXPECT_EQ(kTestData, data); |
| 234 |
| 235 // With matching modification times time this should work. |
| 236 TouchTestFile(base::TimeDelta()); |
| 237 data.clear(); |
| 238 reader.reset(CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 239 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| 240 EXPECT_EQ(net::OK, result); |
| 241 EXPECT_EQ(kTestData, data); |
| 242 |
| 243 // And with NULL expected modification time this should work. |
227 data.clear(); | 244 data.clear(); |
228 reader.reset(CreateFileReader(test_path(), 0, base::Time())); | 245 reader.reset(CreateFileReader(test_path(), 0, base::Time())); |
229 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 246 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
230 ASSERT_EQ(net::OK, result); | 247 EXPECT_EQ(net::OK, result); |
231 ASSERT_EQ(kTestData, data); | 248 EXPECT_EQ(kTestData, data); |
232 } | 249 } |
233 | 250 |
234 TEST_F(LocalFileStreamReaderTest, ReadWithOffset) { | 251 TEST_F(LocalFileStreamReaderTest, ReadWithOffset) { |
235 scoped_ptr<LocalFileStreamReader> reader( | 252 scoped_ptr<LocalFileStreamReader> reader( |
236 CreateFileReader(test_path(), 3, base::Time())); | 253 CreateFileReader(test_path(), 3, base::Time())); |
237 int result = 0; | 254 int result = 0; |
238 std::string data; | 255 std::string data; |
239 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 256 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
240 ASSERT_EQ(net::OK, result); | 257 ASSERT_EQ(net::OK, result); |
241 ASSERT_EQ(&kTestData[3], data); | 258 ASSERT_EQ(&kTestData[3], data); |
242 } | 259 } |
243 | 260 |
244 TEST_F(LocalFileStreamReaderTest, DeleteWithUnfinishedRead) { | 261 TEST_F(LocalFileStreamReaderTest, DeleteWithUnfinishedRead) { |
245 scoped_ptr<LocalFileStreamReader> reader( | 262 scoped_ptr<LocalFileStreamReader> reader( |
246 CreateFileReader(test_path(), 0, base::Time())); | 263 CreateFileReader(test_path(), 0, base::Time())); |
247 | 264 |
248 net::TestCompletionCallback callback; | 265 net::TestCompletionCallback callback; |
249 scoped_refptr<net::IOBufferWithSize> buf( | 266 scoped_refptr<net::IOBufferWithSize> buf( |
250 new net::IOBufferWithSize(kTestDataSize)); | 267 new net::IOBufferWithSize(kTestDataSize)); |
251 int rv = reader->Read(buf.get(), buf->size(), base::Bind(&NeverCalled)); | 268 int rv = reader->Read(buf.get(), buf->size(), base::Bind(&NeverCalled)); |
252 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0); | 269 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0); |
253 | 270 |
254 // Delete immediately. | 271 // Delete immediately. |
255 // Should not crash; nor should NeverCalled be callback. | 272 // Should not crash; nor should NeverCalled be callback. |
256 reader.reset(); | 273 reader.reset(); |
257 EnsureFileTaskFinished(); | 274 EnsureFileTaskFinished(); |
258 } | 275 } |
259 | 276 |
260 } // namespace content | 277 } // namespace content |
OLD | NEW |