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