| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/blob/local_file_reader.h" | 5 #include "webkit/blob/local_file_stream_reader.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/platform_file.h" | 13 #include "base/platform_file.h" |
| 14 #include "base/scoped_temp_dir.h" | 14 #include "base/scoped_temp_dir.h" |
| 15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace webkit_blob { | 21 namespace webkit_blob { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const char kTestData[] = "0123456789"; | 25 const char kTestData[] = "0123456789"; |
| 26 const int kTestDataSize = arraysize(kTestData) - 1; | 26 const int kTestDataSize = arraysize(kTestData) - 1; |
| 27 | 27 |
| 28 void ReadFromReader(LocalFileReader* reader, | 28 void ReadFromReader(LocalFileStreamReader* reader, |
| 29 std::string* data, size_t size, | 29 std::string* data, size_t size, |
| 30 int* result) { | 30 int* result) { |
| 31 ASSERT_TRUE(reader != NULL); | 31 ASSERT_TRUE(reader != NULL); |
| 32 ASSERT_TRUE(result != NULL); | 32 ASSERT_TRUE(result != NULL); |
| 33 *result = net::OK; | 33 *result = net::OK; |
| 34 net::TestCompletionCallback callback; | 34 net::TestCompletionCallback callback; |
| 35 size_t total_bytes_read = 0; | 35 size_t total_bytes_read = 0; |
| 36 while (total_bytes_read < size) { | 36 while (total_bytes_read < size) { |
| 37 scoped_refptr<net::IOBufferWithSize> buf( | 37 scoped_refptr<net::IOBufferWithSize> buf( |
| 38 new net::IOBufferWithSize(size - total_bytes_read)); | 38 new net::IOBufferWithSize(size - total_bytes_read)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 50 | 50 |
| 51 void NeverCalled(int) { ADD_FAILURE(); } | 51 void NeverCalled(int) { ADD_FAILURE(); } |
| 52 void EmptyCallback() {} | 52 void EmptyCallback() {} |
| 53 | 53 |
| 54 void QuitLoop() { | 54 void QuitLoop() { |
| 55 MessageLoop::current()->Quit(); | 55 MessageLoop::current()->Quit(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 } // namespace | 58 } // namespace |
| 59 | 59 |
| 60 class LocalFileReaderTest : public testing::Test { | 60 class LocalFileStreamReaderTest : public testing::Test { |
| 61 public: | 61 public: |
| 62 LocalFileReaderTest() | 62 LocalFileStreamReaderTest() |
| 63 : message_loop_(MessageLoop::TYPE_IO), | 63 : message_loop_(MessageLoop::TYPE_IO), |
| 64 file_thread_("FileUtilProxyTestFileThread") {} | 64 file_thread_("FileUtilProxyTestFileThread") {} |
| 65 | 65 |
| 66 virtual void SetUp() OVERRIDE { | 66 virtual void SetUp() OVERRIDE { |
| 67 ASSERT_TRUE(file_thread_.Start()); | 67 ASSERT_TRUE(file_thread_.Start()); |
| 68 ASSERT_TRUE(dir_.CreateUniqueTempDir()); | 68 ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
| 69 | 69 |
| 70 file_util::WriteFile(test_path(), kTestData, kTestDataSize); | 70 file_util::WriteFile(test_path(), kTestData, kTestDataSize); |
| 71 base::PlatformFileInfo info; | 71 base::PlatformFileInfo info; |
| 72 ASSERT_TRUE(file_util::GetFileInfo(test_path(), &info)); | 72 ASSERT_TRUE(file_util::GetFileInfo(test_path(), &info)); |
| 73 test_file_modification_time_ = info.last_modified; | 73 test_file_modification_time_ = info.last_modified; |
| 74 } | 74 } |
| 75 | 75 |
| 76 virtual void TearDown() OVERRIDE { | 76 virtual void TearDown() OVERRIDE { |
| 77 // Give another chance for deleted streams to perform Close. | 77 // Give another chance for deleted streams to perform Close. |
| 78 MessageLoop::current()->RunAllPending(); | 78 MessageLoop::current()->RunAllPending(); |
| 79 file_thread_.Stop(); | 79 file_thread_.Stop(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 protected: | 82 protected: |
| 83 LocalFileReader* CreateFileReader( | 83 LocalFileStreamReader* CreateFileReader( |
| 84 const FilePath& path, | 84 const FilePath& path, |
| 85 int64 initial_offset, | 85 int64 initial_offset, |
| 86 const base::Time& expected_modification_time) { | 86 const base::Time& expected_modification_time) { |
| 87 return new LocalFileReader( | 87 return new LocalFileStreamReader( |
| 88 file_task_runner(), | 88 file_task_runner(), |
| 89 path, | 89 path, |
| 90 initial_offset, | 90 initial_offset, |
| 91 expected_modification_time); | 91 expected_modification_time); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void TouchTestFile() { | 94 void TouchTestFile() { |
| 95 base::Time new_modified_time = | 95 base::Time new_modified_time = |
| 96 test_file_modification_time() - base::TimeDelta::FromSeconds(1); | 96 test_file_modification_time() - base::TimeDelta::FromSeconds(1); |
| 97 ASSERT_TRUE(file_util::TouchFile(test_path(), | 97 ASSERT_TRUE(file_util::TouchFile(test_path(), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 115 MessageLoop::current()->Run(); | 115 MessageLoop::current()->Run(); |
| 116 } | 116 } |
| 117 | 117 |
| 118 private: | 118 private: |
| 119 MessageLoop message_loop_; | 119 MessageLoop message_loop_; |
| 120 base::Thread file_thread_; | 120 base::Thread file_thread_; |
| 121 ScopedTempDir dir_; | 121 ScopedTempDir dir_; |
| 122 base::Time test_file_modification_time_; | 122 base::Time test_file_modification_time_; |
| 123 }; | 123 }; |
| 124 | 124 |
| 125 TEST_F(LocalFileReaderTest, NonExistent) { | 125 TEST_F(LocalFileStreamReaderTest, NonExistent) { |
| 126 FilePath nonexistent_path = test_dir().AppendASCII("nonexistent"); | 126 FilePath nonexistent_path = test_dir().AppendASCII("nonexistent"); |
| 127 scoped_ptr<LocalFileReader> reader( | 127 scoped_ptr<LocalFileStreamReader> reader( |
| 128 CreateFileReader(nonexistent_path, 0, base::Time())); | 128 CreateFileReader(nonexistent_path, 0, base::Time())); |
| 129 int result = 0; | 129 int result = 0; |
| 130 std::string data; | 130 std::string data; |
| 131 ReadFromReader(reader.get(), &data, 10, &result); | 131 ReadFromReader(reader.get(), &data, 10, &result); |
| 132 ASSERT_EQ(net::ERR_FILE_NOT_FOUND, result); | 132 ASSERT_EQ(net::ERR_FILE_NOT_FOUND, result); |
| 133 ASSERT_EQ(0U, data.size()); | 133 ASSERT_EQ(0U, data.size()); |
| 134 } | 134 } |
| 135 | 135 |
| 136 TEST_F(LocalFileReaderTest, Empty) { | 136 TEST_F(LocalFileStreamReaderTest, Empty) { |
| 137 FilePath empty_path = test_dir().AppendASCII("empty"); | 137 FilePath empty_path = test_dir().AppendASCII("empty"); |
| 138 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 138 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 139 base::PlatformFile file = base::CreatePlatformFile( | 139 base::PlatformFile file = base::CreatePlatformFile( |
| 140 empty_path, | 140 empty_path, |
| 141 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, | 141 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, |
| 142 NULL, &error); | 142 NULL, &error); |
| 143 ASSERT_EQ(base::PLATFORM_FILE_OK, error); | 143 ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
| 144 ASSERT_NE(base::kInvalidPlatformFileValue, file); | 144 ASSERT_NE(base::kInvalidPlatformFileValue, file); |
| 145 base::ClosePlatformFile(file); | 145 base::ClosePlatformFile(file); |
| 146 | 146 |
| 147 scoped_ptr<LocalFileReader> reader( | 147 scoped_ptr<LocalFileStreamReader> reader( |
| 148 CreateFileReader(empty_path, 0, base::Time())); | 148 CreateFileReader(empty_path, 0, base::Time())); |
| 149 int result = 0; | 149 int result = 0; |
| 150 std::string data; | 150 std::string data; |
| 151 ReadFromReader(reader.get(), &data, 10, &result); | 151 ReadFromReader(reader.get(), &data, 10, &result); |
| 152 ASSERT_EQ(net::OK, result); | 152 ASSERT_EQ(net::OK, result); |
| 153 ASSERT_EQ(0U, data.size()); | 153 ASSERT_EQ(0U, data.size()); |
| 154 | 154 |
| 155 net::TestInt64CompletionCallback callback; | 155 net::TestInt64CompletionCallback callback; |
| 156 result = reader->GetLength(callback.callback()); | 156 result = reader->GetLength(callback.callback()); |
| 157 if (result == net::ERR_IO_PENDING) | 157 if (result == net::ERR_IO_PENDING) |
| 158 result = callback.WaitForResult(); | 158 result = callback.WaitForResult(); |
| 159 ASSERT_EQ(0, result); | 159 ASSERT_EQ(0, result); |
| 160 } | 160 } |
| 161 | 161 |
| 162 TEST_F(LocalFileReaderTest, GetLengthNormal) { | 162 TEST_F(LocalFileStreamReaderTest, GetLengthNormal) { |
| 163 scoped_ptr<LocalFileReader> reader( | 163 scoped_ptr<LocalFileStreamReader> reader( |
| 164 CreateFileReader(test_path(), 0, test_file_modification_time())); | 164 CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 165 net::TestInt64CompletionCallback callback; | 165 net::TestInt64CompletionCallback callback; |
| 166 int result = reader->GetLength(callback.callback()); | 166 int result = reader->GetLength(callback.callback()); |
| 167 if (result == net::ERR_IO_PENDING) | 167 if (result == net::ERR_IO_PENDING) |
| 168 result = callback.WaitForResult(); | 168 result = callback.WaitForResult(); |
| 169 ASSERT_EQ(kTestDataSize, result); | 169 ASSERT_EQ(kTestDataSize, result); |
| 170 } | 170 } |
| 171 | 171 |
| 172 TEST_F(LocalFileReaderTest, GetLengthAfterModified) { | 172 TEST_F(LocalFileStreamReaderTest, GetLengthAfterModified) { |
| 173 // Touch file so that the file's modification time becomes different | 173 // Touch file so that the file's modification time becomes different |
| 174 // from what we expect. | 174 // from what we expect. |
| 175 TouchTestFile(); | 175 TouchTestFile(); |
| 176 | 176 |
| 177 scoped_ptr<LocalFileReader> reader( | 177 scoped_ptr<LocalFileStreamReader> reader( |
| 178 CreateFileReader(test_path(), 0, test_file_modification_time())); | 178 CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 179 net::TestInt64CompletionCallback callback; | 179 net::TestInt64CompletionCallback callback; |
| 180 int result = reader->GetLength(callback.callback()); | 180 int result = reader->GetLength(callback.callback()); |
| 181 if (result == net::ERR_IO_PENDING) | 181 if (result == net::ERR_IO_PENDING) |
| 182 result = callback.WaitForResult(); | 182 result = callback.WaitForResult(); |
| 183 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); | 183 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); |
| 184 | 184 |
| 185 // With NULL expected modification time this should work. | 185 // With NULL expected modification time this should work. |
| 186 reader.reset(CreateFileReader(test_path(), 0, base::Time())); | 186 reader.reset(CreateFileReader(test_path(), 0, base::Time())); |
| 187 result = reader->GetLength(callback.callback()); | 187 result = reader->GetLength(callback.callback()); |
| 188 if (result == net::ERR_IO_PENDING) | 188 if (result == net::ERR_IO_PENDING) |
| 189 result = callback.WaitForResult(); | 189 result = callback.WaitForResult(); |
| 190 ASSERT_EQ(kTestDataSize, result); | 190 ASSERT_EQ(kTestDataSize, result); |
| 191 } | 191 } |
| 192 | 192 |
| 193 TEST_F(LocalFileReaderTest, GetLengthWithOffset) { | 193 TEST_F(LocalFileStreamReaderTest, GetLengthWithOffset) { |
| 194 scoped_ptr<LocalFileReader> reader( | 194 scoped_ptr<LocalFileStreamReader> reader( |
| 195 CreateFileReader(test_path(), 3, base::Time())); | 195 CreateFileReader(test_path(), 3, base::Time())); |
| 196 net::TestInt64CompletionCallback callback; | 196 net::TestInt64CompletionCallback callback; |
| 197 int result = reader->GetLength(callback.callback()); | 197 int result = reader->GetLength(callback.callback()); |
| 198 if (result == net::ERR_IO_PENDING) | 198 if (result == net::ERR_IO_PENDING) |
| 199 result = callback.WaitForResult(); | 199 result = callback.WaitForResult(); |
| 200 // Initial offset does not affect the result of GetLength. | 200 // Initial offset does not affect the result of GetLength. |
| 201 ASSERT_EQ(kTestDataSize, result); | 201 ASSERT_EQ(kTestDataSize, result); |
| 202 } | 202 } |
| 203 | 203 |
| 204 TEST_F(LocalFileReaderTest, ReadNormal) { | 204 TEST_F(LocalFileStreamReaderTest, ReadNormal) { |
| 205 scoped_ptr<LocalFileReader> reader( | 205 scoped_ptr<LocalFileStreamReader> reader( |
| 206 CreateFileReader(test_path(), 0, test_file_modification_time())); | 206 CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 207 int result = 0; | 207 int result = 0; |
| 208 std::string data; | 208 std::string data; |
| 209 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 209 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| 210 ASSERT_EQ(net::OK, result); | 210 ASSERT_EQ(net::OK, result); |
| 211 ASSERT_EQ(kTestData, data); | 211 ASSERT_EQ(kTestData, data); |
| 212 } | 212 } |
| 213 | 213 |
| 214 TEST_F(LocalFileReaderTest, ReadAfterModified) { | 214 TEST_F(LocalFileStreamReaderTest, ReadAfterModified) { |
| 215 // Touch file so that the file's modification time becomes different | 215 // Touch file so that the file's modification time becomes different |
| 216 // from what we expect. | 216 // from what we expect. |
| 217 TouchTestFile(); | 217 TouchTestFile(); |
| 218 | 218 |
| 219 scoped_ptr<LocalFileReader> reader( | 219 scoped_ptr<LocalFileStreamReader> reader( |
| 220 CreateFileReader(test_path(), 0, test_file_modification_time())); | 220 CreateFileReader(test_path(), 0, test_file_modification_time())); |
| 221 int result = 0; | 221 int result = 0; |
| 222 std::string data; | 222 std::string data; |
| 223 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 223 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| 224 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); | 224 ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); |
| 225 ASSERT_EQ(0U, data.size()); | 225 ASSERT_EQ(0U, data.size()); |
| 226 | 226 |
| 227 // With NULL expected modification time this should work. | 227 // With NULL expected modification time this should work. |
| 228 data.clear(); | 228 data.clear(); |
| 229 reader.reset(CreateFileReader(test_path(), 0, base::Time())); | 229 reader.reset(CreateFileReader(test_path(), 0, base::Time())); |
| 230 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 230 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| 231 ASSERT_EQ(net::OK, result); | 231 ASSERT_EQ(net::OK, result); |
| 232 ASSERT_EQ(kTestData, data); | 232 ASSERT_EQ(kTestData, data); |
| 233 } | 233 } |
| 234 | 234 |
| 235 TEST_F(LocalFileReaderTest, ReadWithOffset) { | 235 TEST_F(LocalFileStreamReaderTest, ReadWithOffset) { |
| 236 scoped_ptr<LocalFileReader> reader( | 236 scoped_ptr<LocalFileStreamReader> reader( |
| 237 CreateFileReader(test_path(), 3, base::Time())); | 237 CreateFileReader(test_path(), 3, base::Time())); |
| 238 int result = 0; | 238 int result = 0; |
| 239 std::string data; | 239 std::string data; |
| 240 ReadFromReader(reader.get(), &data, kTestDataSize, &result); | 240 ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| 241 ASSERT_EQ(net::OK, result); | 241 ASSERT_EQ(net::OK, result); |
| 242 ASSERT_EQ(&kTestData[3], data); | 242 ASSERT_EQ(&kTestData[3], data); |
| 243 } | 243 } |
| 244 | 244 |
| 245 TEST_F(LocalFileReaderTest, DeleteWithUnfinishedRead) { | 245 TEST_F(LocalFileStreamReaderTest, DeleteWithUnfinishedRead) { |
| 246 scoped_ptr<LocalFileReader> reader( | 246 scoped_ptr<LocalFileStreamReader> reader( |
| 247 CreateFileReader(test_path(), 0, base::Time())); | 247 CreateFileReader(test_path(), 0, base::Time())); |
| 248 | 248 |
| 249 net::TestCompletionCallback callback; | 249 net::TestCompletionCallback callback; |
| 250 scoped_refptr<net::IOBufferWithSize> buf( | 250 scoped_refptr<net::IOBufferWithSize> buf( |
| 251 new net::IOBufferWithSize(kTestDataSize)); | 251 new net::IOBufferWithSize(kTestDataSize)); |
| 252 int rv = reader->Read(buf, buf->size(), base::Bind(&NeverCalled)); | 252 int rv = reader->Read(buf, buf->size(), base::Bind(&NeverCalled)); |
| 253 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0); | 253 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0); |
| 254 | 254 |
| 255 // Delete immediately. | 255 // Delete immediately. |
| 256 // Should not crash; nor should NeverCalled be callback. | 256 // Should not crash; nor should NeverCalled be callback. |
| 257 reader.reset(); | 257 reader.reset(); |
| 258 EnsureFileTaskFinished(); | 258 EnsureFileTaskFinished(); |
| 259 } | 259 } |
| 260 | 260 |
| 261 } // namespace webkit_blob | 261 } // namespace webkit_blob |
| OLD | NEW |