| 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 <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include <string> | 10 #include <string> |
| 8 | 11 |
| 9 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 16 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 17 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
| 18 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
| 19 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 20 #include "net/base/test_completion_callback.h" | 24 #include "net/base/test_completion_callback.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 26 |
| 23 using storage::LocalFileStreamReader; | 27 using storage::LocalFileStreamReader; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 void TearDown() override { | 83 void TearDown() override { |
| 80 // Give another chance for deleted streams to perform Close. | 84 // Give another chance for deleted streams to perform Close. |
| 81 base::RunLoop().RunUntilIdle(); | 85 base::RunLoop().RunUntilIdle(); |
| 82 file_thread_.Stop(); | 86 file_thread_.Stop(); |
| 83 base::RunLoop().RunUntilIdle(); | 87 base::RunLoop().RunUntilIdle(); |
| 84 } | 88 } |
| 85 | 89 |
| 86 protected: | 90 protected: |
| 87 LocalFileStreamReader* CreateFileReader( | 91 LocalFileStreamReader* CreateFileReader( |
| 88 const base::FilePath& path, | 92 const base::FilePath& path, |
| 89 int64 initial_offset, | 93 int64_t initial_offset, |
| 90 const base::Time& expected_modification_time) { | 94 const base::Time& expected_modification_time) { |
| 91 return new LocalFileStreamReader( | 95 return new LocalFileStreamReader( |
| 92 file_task_runner(), | 96 file_task_runner(), |
| 93 path, | 97 path, |
| 94 initial_offset, | 98 initial_offset, |
| 95 expected_modification_time); | 99 expected_modification_time); |
| 96 } | 100 } |
| 97 | 101 |
| 98 void TouchTestFile(base::TimeDelta delta) { | 102 void TouchTestFile(base::TimeDelta delta) { |
| 99 base::Time new_modified_time = test_file_modification_time() + delta; | 103 base::Time new_modified_time = test_file_modification_time() + delta; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 148 |
| 145 scoped_ptr<LocalFileStreamReader> reader( | 149 scoped_ptr<LocalFileStreamReader> reader( |
| 146 CreateFileReader(empty_path, 0, base::Time())); | 150 CreateFileReader(empty_path, 0, base::Time())); |
| 147 int result = 0; | 151 int result = 0; |
| 148 std::string data; | 152 std::string data; |
| 149 ReadFromReader(reader.get(), &data, 10, &result); | 153 ReadFromReader(reader.get(), &data, 10, &result); |
| 150 ASSERT_EQ(net::OK, result); | 154 ASSERT_EQ(net::OK, result); |
| 151 ASSERT_EQ(0U, data.size()); | 155 ASSERT_EQ(0U, data.size()); |
| 152 | 156 |
| 153 net::TestInt64CompletionCallback callback; | 157 net::TestInt64CompletionCallback callback; |
| 154 int64 length_result = reader->GetLength(callback.callback()); | 158 int64_t length_result = reader->GetLength(callback.callback()); |
| 155 if (length_result == net::ERR_IO_PENDING) | 159 if (length_result == net::ERR_IO_PENDING) |
| 156 length_result = callback.WaitForResult(); | 160 length_result = callback.WaitForResult(); |
| 157 ASSERT_EQ(0, result); | 161 ASSERT_EQ(0, result); |
| 158 } | 162 } |
| 159 | 163 |
| 160 TEST_F(LocalFileStreamReaderTest, GetLengthNormal) { | 164 TEST_F(LocalFileStreamReaderTest, GetLengthNormal) { |
| 161 scoped_ptr<LocalFileStreamReader> reader( | 165 scoped_ptr<LocalFileStreamReader> reader( |
| 162 CreateFileReader(test_path(), 0, test_file_modification_time())); | 166 CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 163 net::TestInt64CompletionCallback callback; | 167 net::TestInt64CompletionCallback callback; |
| 164 int64 result = reader->GetLength(callback.callback()); | 168 int64_t result = reader->GetLength(callback.callback()); |
| 165 if (result == net::ERR_IO_PENDING) | 169 if (result == net::ERR_IO_PENDING) |
| 166 result = callback.WaitForResult(); | 170 result = callback.WaitForResult(); |
| 167 ASSERT_EQ(kTestDataSize, result); | 171 ASSERT_EQ(kTestDataSize, result); |
| 168 } | 172 } |
| 169 | 173 |
| 170 TEST_F(LocalFileStreamReaderTest, GetLengthAfterModified) { | 174 TEST_F(LocalFileStreamReaderTest, GetLengthAfterModified) { |
| 171 // Touch file so that the file's modification time becomes different | 175 // Touch file so that the file's modification time becomes different |
| 172 // from what we expect. | 176 // from what we expect. |
| 173 TouchTestFile(base::TimeDelta::FromSeconds(-1)); | 177 TouchTestFile(base::TimeDelta::FromSeconds(-1)); |
| 174 | 178 |
| 175 scoped_ptr<LocalFileStreamReader> reader( | 179 scoped_ptr<LocalFileStreamReader> reader( |
| 176 CreateFileReader(test_path(), 0, test_file_modification_time())); | 180 CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 177 net::TestInt64CompletionCallback callback; | 181 net::TestInt64CompletionCallback callback; |
| 178 int64 result = reader->GetLength(callback.callback()); | 182 int64_t result = reader->GetLength(callback.callback()); |
| 179 if (result == net::ERR_IO_PENDING) | 183 if (result == net::ERR_IO_PENDING) |
| 180 result = callback.WaitForResult(); | 184 result = callback.WaitForResult(); |
| 181 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); | 185 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); |
| 182 | 186 |
| 183 // With NULL expected modification time this should work. | 187 // With NULL expected modification time this should work. |
| 184 reader.reset(CreateFileReader(test_path(), 0, base::Time())); | 188 reader.reset(CreateFileReader(test_path(), 0, base::Time())); |
| 185 result = reader->GetLength(callback.callback()); | 189 result = reader->GetLength(callback.callback()); |
| 186 if (result == net::ERR_IO_PENDING) | 190 if (result == net::ERR_IO_PENDING) |
| 187 result = callback.WaitForResult(); | 191 result = callback.WaitForResult(); |
| 188 ASSERT_EQ(kTestDataSize, result); | 192 ASSERT_EQ(kTestDataSize, result); |
| 189 } | 193 } |
| 190 | 194 |
| 191 TEST_F(LocalFileStreamReaderTest, GetLengthWithOffset) { | 195 TEST_F(LocalFileStreamReaderTest, GetLengthWithOffset) { |
| 192 scoped_ptr<LocalFileStreamReader> reader( | 196 scoped_ptr<LocalFileStreamReader> reader( |
| 193 CreateFileReader(test_path(), 3, base::Time())); | 197 CreateFileReader(test_path(), 3, base::Time())); |
| 194 net::TestInt64CompletionCallback callback; | 198 net::TestInt64CompletionCallback callback; |
| 195 int64 result = reader->GetLength(callback.callback()); | 199 int64_t result = reader->GetLength(callback.callback()); |
| 196 if (result == net::ERR_IO_PENDING) | 200 if (result == net::ERR_IO_PENDING) |
| 197 result = callback.WaitForResult(); | 201 result = callback.WaitForResult(); |
| 198 // Initial offset does not affect the result of GetLength. | 202 // Initial offset does not affect the result of GetLength. |
| 199 ASSERT_EQ(kTestDataSize, result); | 203 ASSERT_EQ(kTestDataSize, result); |
| 200 } | 204 } |
| 201 | 205 |
| 202 TEST_F(LocalFileStreamReaderTest, ReadNormal) { | 206 TEST_F(LocalFileStreamReaderTest, ReadNormal) { |
| 203 scoped_ptr<LocalFileStreamReader> reader( | 207 scoped_ptr<LocalFileStreamReader> reader( |
| 204 CreateFileReader(test_path(), 0, test_file_modification_time())); | 208 CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 205 int result = 0; | 209 int result = 0; |
| 206 std::string data; | 210 std::string data; |
| 207 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 211 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| 208 ASSERT_EQ(net::OK, result); | 212 ASSERT_EQ(net::OK, result); |
| 209 ASSERT_EQ(kTestData, data); | 213 ASSERT_EQ(kTestData, data); |
| 210 } | 214 } |
| 211 | 215 |
| 212 TEST_F(LocalFileStreamReaderTest, ReadAfterModified) { | 216 TEST_F(LocalFileStreamReaderTest, ReadAfterModified) { |
| 213 // Touch file so that the file's modification time becomes different | 217 // Touch file so that the file's modification time becomes different |
| 214 // from what we expect. Note that the resolution on some filesystems | 218 // from what we expect. Note that the resolution on some filesystems |
| 215 // is 1s so we can't test with deltas less than that. | 219 // is 1s so we can't test with deltas less than that. |
| 216 TouchTestFile(base::TimeDelta::FromSeconds(-1)); | 220 TouchTestFile(base::TimeDelta::FromSeconds(-1)); |
| 217 scoped_ptr<LocalFileStreamReader> reader( | 221 scoped_ptr<LocalFileStreamReader> reader( |
| 218 CreateFileReader(test_path(), 0, test_file_modification_time())); | 222 CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 219 int result = 0; | 223 int result = 0; |
| 220 std::string data; | 224 std::string data; |
| 221 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 225 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| 222 EXPECT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); | 226 EXPECT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); |
| 223 EXPECT_EQ(0U, data.size()); | 227 EXPECT_EQ(0U, data.size()); |
| 224 | 228 |
| 225 // Due to precision loss converting int64->double->int64 (e.g. through | 229 // Due to precision loss converting int64_t->double->int64_t (e.g. through |
| 226 // Blink) the expected/actual time may vary by microseconds. With | 230 // Blink) the expected/actual time may vary by microseconds. With |
| 227 // modification time delta < 10us this should work. | 231 // modification time delta < 10us this should work. |
| 228 TouchTestFile(base::TimeDelta::FromMicroseconds(1)); | 232 TouchTestFile(base::TimeDelta::FromMicroseconds(1)); |
| 229 data.clear(); | 233 data.clear(); |
| 230 reader.reset(CreateFileReader(test_path(), 0, test_file_modification_time())); | 234 reader.reset(CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 231 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 235 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| 232 EXPECT_EQ(net::OK, result); | 236 EXPECT_EQ(net::OK, result); |
| 233 EXPECT_EQ(kTestData, data); | 237 EXPECT_EQ(kTestData, data); |
| 234 | 238 |
| 235 // With matching modification times time this should work. | 239 // With matching modification times time this should work. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 int rv = reader->Read(buf.get(), buf->size(), base::Bind(&NeverCalled)); | 272 int rv = reader->Read(buf.get(), buf->size(), base::Bind(&NeverCalled)); |
| 269 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0); | 273 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0); |
| 270 | 274 |
| 271 // Delete immediately. | 275 // Delete immediately. |
| 272 // Should not crash; nor should NeverCalled be callback. | 276 // Should not crash; nor should NeverCalled be callback. |
| 273 reader.reset(); | 277 reader.reset(); |
| 274 EnsureFileTaskFinished(); | 278 EnsureFileTaskFinished(); |
| 275 } | 279 } |
| 276 | 280 |
| 277 } // namespace content | 281 } // namespace content |
| OLD | NEW |