[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 4 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 5 | #include "net/base/file_stream.h" |
| 6 | |
| 7 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 8 | #include "base/callback.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 10 | #include "base/message_loop.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 11 | #include "base/path_service.h" |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 12 | #include "base/platform_file.h" |
[email protected] | e3d66fde | 2012-02-17 22:10:34 | [diff] [blame] | 13 | #include "base/synchronization/waitable_event.h" |
| 14 | #include "base/test/test_timeouts.h" |
| 15 | #include "net/base/capturing_net_log.h" |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 16 | #include "net/base/io_buffer.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 17 | #include "net/base/net_errors.h" |
| 18 | #include "net/base/test_completion_callback.h" |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 20 | #include "testing/platform_test.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 21 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 22 | namespace net { |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 23 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | const char kTestData[] = "0123456789"; |
| 27 | const int kTestDataSize = arraysize(kTestData) - 1; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 28 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 29 | // Creates an IOBufferWithSize that contains the kTestDataSize. |
| 30 | IOBufferWithSize* CreateTestDataBuffer() { |
| 31 | IOBufferWithSize* buf = new IOBufferWithSize(kTestDataSize); |
| 32 | memcpy(buf->data(), kTestData, kTestDataSize); |
| 33 | return buf; |
| 34 | } |
| 35 | |
[email protected] | e3d66fde | 2012-02-17 22:10:34 | [diff] [blame] | 36 | // This NetLog is used for notifying when a file stream is closed |
| 37 | // (i.e. TYPE_FILE_STREAM_CLOSE event is recorded). |
| 38 | class NetLogForNotifyingFileClosure : public NetLog { |
| 39 | public: |
| 40 | NetLogForNotifyingFileClosure() |
| 41 | : id_(0), |
| 42 | on_closure_(false /* manual_reset */, false /* initially_signaled */) { |
| 43 | } |
| 44 | |
| 45 | // Wait until a file closure event is recorded. |
| 46 | bool WaitForClosure() { |
| 47 | const base::TimeDelta timeout( |
| 48 | base::TimeDelta::FromMilliseconds( |
| 49 | TestTimeouts::action_max_timeout_ms())); |
| 50 | return on_closure_.TimedWait(timeout); |
| 51 | } |
| 52 | |
| 53 | // NetLog overrides: |
| 54 | virtual void AddEntry(EventType type, |
| 55 | const base::TimeTicks& time, |
| 56 | const Source& source, |
| 57 | EventPhase phase, |
| 58 | EventParameters* extra_parameters) { |
| 59 | if (type == TYPE_FILE_STREAM_CLOSE) { |
| 60 | on_closure_.Signal(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | virtual uint32 NextID() { return id_++; } |
| 65 | virtual LogLevel GetLogLevel() const { return LOG_ALL; } |
| 66 | virtual void AddThreadSafeObserver(ThreadSafeObserver* observer) {} |
| 67 | virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) {}; |
| 68 | |
| 69 | |
| 70 | private: |
| 71 | uint32 id_; |
| 72 | base::WaitableEvent on_closure_; |
| 73 | }; |
| 74 | |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 75 | } // namespace |
| 76 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 77 | class FileStreamTest : public PlatformTest { |
| 78 | public: |
| 79 | virtual void SetUp() { |
| 80 | PlatformTest::SetUp(); |
| 81 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 82 | file_util::CreateTemporaryFile(&temp_file_path_); |
[email protected] | 7ff3f63 | 2009-10-13 18:43:35 | [diff] [blame] | 83 | file_util::WriteFile(temp_file_path_, kTestData, kTestDataSize); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 84 | } |
| 85 | virtual void TearDown() { |
| 86 | file_util::Delete(temp_file_path_, false); |
| 87 | |
| 88 | PlatformTest::TearDown(); |
| 89 | } |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 90 | |
[email protected] | 07167e8f | 2009-01-26 21:38:11 | [diff] [blame] | 91 | const FilePath temp_file_path() const { return temp_file_path_; } |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 92 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 93 | private: |
[email protected] | 07167e8f | 2009-01-26 21:38:11 | [diff] [blame] | 94 | FilePath temp_file_path_; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 95 | }; |
| 96 | |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 97 | namespace { |
| 98 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 99 | TEST_F(FileStreamTest, BasicOpenClose) { |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 100 | base::PlatformFile file = base::kInvalidPlatformFileValue; |
| 101 | { |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 102 | FileStream stream(NULL); |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 103 | int rv = stream.OpenSync(temp_file_path(), |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 104 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); |
| 105 | EXPECT_EQ(OK, rv); |
| 106 | EXPECT_TRUE(stream.IsOpen()); |
[email protected] | 84e0309f | 2012-02-24 01:56:59 | [diff] [blame] | 107 | file = stream.GetPlatformFileForTesting(); |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 108 | } |
| 109 | EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 110 | base::PlatformFileInfo info; |
| 111 | // The file should be closed. |
| 112 | EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); |
| 113 | } |
| 114 | |
| 115 | TEST_F(FileStreamTest, FileHandleLeftOpen) { |
| 116 | bool created = false; |
| 117 | ASSERT_EQ(kTestDataSize, |
| 118 | file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
| 119 | int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; |
| 120 | base::PlatformFile file = base::CreatePlatformFile( |
| 121 | temp_file_path(), flags, &created, NULL); |
| 122 | |
| 123 | { |
| 124 | // Seek to the beginning of the file and read. |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 125 | FileStream read_stream(file, flags, NULL); |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 126 | EXPECT_TRUE(read_stream.IsOpen()); |
| 127 | } |
| 128 | |
| 129 | EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 130 | base::PlatformFileInfo info; |
| 131 | // The file should still be open. |
| 132 | EXPECT_TRUE(base::GetPlatformFileInfo(file, &info)); |
| 133 | // Clean up. |
| 134 | EXPECT_TRUE(base::ClosePlatformFile(file)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 137 | // Test the use of FileStream with a file handle provided at construction. |
| 138 | TEST_F(FileStreamTest, UseFileHandle) { |
| 139 | bool created = false; |
| 140 | |
| 141 | // 1. Test reading with a file handle. |
| 142 | ASSERT_EQ(kTestDataSize, |
| 143 | file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
| 144 | int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; |
| 145 | base::PlatformFile file = base::CreatePlatformFile( |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 146 | temp_file_path(), flags, &created, NULL); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 147 | |
| 148 | // Seek to the beginning of the file and read. |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 149 | FileStream read_stream(file, flags, NULL); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 150 | ASSERT_EQ(0, read_stream.Seek(FROM_BEGIN, 0)); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 151 | ASSERT_EQ(kTestDataSize, read_stream.Available()); |
| 152 | // Read into buffer and compare. |
| 153 | char buffer[kTestDataSize]; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 154 | ASSERT_EQ(kTestDataSize, |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 155 | read_stream.ReadSync(buffer, kTestDataSize)); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 156 | ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 157 | read_stream.CloseSync(); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 158 | |
| 159 | // 2. Test writing with a file handle. |
| 160 | file_util::Delete(temp_file_path(), false); |
| 161 | flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE; |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 162 | file = base::CreatePlatformFile(temp_file_path(), flags, &created, NULL); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 163 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 164 | FileStream write_stream(file, flags, NULL); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 165 | ASSERT_EQ(0, write_stream.Seek(FROM_BEGIN, 0)); |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 166 | ASSERT_EQ(kTestDataSize, |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 167 | write_stream.WriteSync(kTestData, kTestDataSize)); |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 168 | write_stream.CloseSync(); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 169 | |
| 170 | // Read into buffer and compare to make sure the handle worked fine. |
| 171 | ASSERT_EQ(kTestDataSize, |
| 172 | file_util::ReadFile(temp_file_path(), buffer, kTestDataSize)); |
| 173 | ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); |
| 174 | } |
| 175 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 176 | TEST_F(FileStreamTest, UseClosedStream) { |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 177 | FileStream stream(NULL); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 178 | |
| 179 | EXPECT_FALSE(stream.IsOpen()); |
| 180 | |
| 181 | // Try seeking... |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 182 | int64 new_offset = stream.Seek(FROM_BEGIN, 5); |
| 183 | EXPECT_EQ(ERR_UNEXPECTED, new_offset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 184 | |
| 185 | // Try available... |
| 186 | int64 avail = stream.Available(); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 187 | EXPECT_EQ(ERR_UNEXPECTED, avail); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 188 | |
| 189 | // Try reading... |
| 190 | char buf[10]; |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 191 | int rv = stream.ReadSync(buf, arraysize(buf)); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 192 | EXPECT_EQ(ERR_UNEXPECTED, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | TEST_F(FileStreamTest, BasicRead) { |
| 196 | int64 file_size; |
| 197 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 198 | EXPECT_TRUE(ok); |
| 199 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 200 | FileStream stream(NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 201 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 202 | base::PLATFORM_FILE_READ; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 203 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 204 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 205 | |
| 206 | int64 total_bytes_avail = stream.Available(); |
| 207 | EXPECT_EQ(file_size, total_bytes_avail); |
| 208 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 209 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 210 | |
| 211 | std::string data_read; |
| 212 | for (;;) { |
| 213 | char buf[4]; |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 214 | rv = stream.ReadSync(buf, arraysize(buf)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 215 | EXPECT_LE(0, rv); |
| 216 | if (rv <= 0) |
| 217 | break; |
| 218 | total_bytes_read += rv; |
| 219 | data_read.append(buf, rv); |
| 220 | } |
| 221 | EXPECT_EQ(file_size, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 222 | EXPECT_EQ(kTestData, data_read); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | TEST_F(FileStreamTest, AsyncRead) { |
| 226 | int64 file_size; |
| 227 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 228 | EXPECT_TRUE(ok); |
| 229 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 230 | FileStream stream(NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 231 | int flags = base::PLATFORM_FILE_OPEN | |
| 232 | base::PLATFORM_FILE_READ | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 233 | base::PLATFORM_FILE_ASYNC; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 234 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 235 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 236 | |
| 237 | int64 total_bytes_avail = stream.Available(); |
| 238 | EXPECT_EQ(file_size, total_bytes_avail); |
| 239 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 240 | TestCompletionCallback callback; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 241 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 242 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 243 | |
| 244 | std::string data_read; |
| 245 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 246 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 247 | rv = stream.Read(buf, buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 248 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 249 | rv = callback.WaitForResult(); |
| 250 | EXPECT_LE(0, rv); |
| 251 | if (rv <= 0) |
| 252 | break; |
| 253 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 254 | data_read.append(buf->data(), rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 255 | } |
| 256 | EXPECT_EQ(file_size, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 257 | EXPECT_EQ(kTestData, data_read); |
| 258 | } |
| 259 | |
| 260 | TEST_F(FileStreamTest, AsyncRead_EarlyClose) { |
| 261 | int64 file_size; |
| 262 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 263 | EXPECT_TRUE(ok); |
| 264 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 265 | FileStream stream(NULL); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 266 | int flags = base::PLATFORM_FILE_OPEN | |
| 267 | base::PLATFORM_FILE_READ | |
| 268 | base::PLATFORM_FILE_ASYNC; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 269 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 270 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 271 | |
| 272 | int64 total_bytes_avail = stream.Available(); |
| 273 | EXPECT_EQ(file_size, total_bytes_avail); |
| 274 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 275 | TestCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 276 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 277 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 278 | rv = stream.Read(buf, buf->size(), callback.callback()); |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 279 | stream.CloseSync(); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 280 | if (rv < 0) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 281 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 282 | // The callback should not be called if the request is cancelled. |
| 283 | MessageLoop::current()->RunAllPending(); |
| 284 | EXPECT_FALSE(callback.have_result()); |
| 285 | } else { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 286 | EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 287 | } |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 288 | } |
| 289 | |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 290 | // Similar to AsyncRead_EarlyClose but deletes a stream instead, to ensure |
| 291 | // that deleting a stream is safe while an async read is in flight. |
| 292 | TEST_F(FileStreamTest, AsyncRead_EarlyDelete) { |
| 293 | int64 file_size; |
| 294 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 295 | EXPECT_TRUE(ok); |
| 296 | |
| 297 | scoped_ptr<FileStream> stream(new FileStream(NULL)); |
| 298 | int flags = base::PLATFORM_FILE_OPEN | |
| 299 | base::PLATFORM_FILE_READ | |
| 300 | base::PLATFORM_FILE_ASYNC; |
| 301 | TestCompletionCallback callback; |
| 302 | int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
| 303 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 304 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 305 | |
| 306 | int64 total_bytes_avail = stream->Available(); |
| 307 | EXPECT_EQ(file_size, total_bytes_avail); |
| 308 | |
| 309 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 310 | rv = stream->Read(buf, buf->size(), callback.callback()); |
| 311 | stream.reset(); // Delete instead of closing it. |
| 312 | if (rv < 0) { |
| 313 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 314 | // The callback should not be called if the request is cancelled. |
| 315 | MessageLoop::current()->RunAllPending(); |
| 316 | EXPECT_FALSE(callback.have_result()); |
| 317 | } else { |
| 318 | EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); |
| 319 | } |
| 320 | } |
| 321 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 322 | TEST_F(FileStreamTest, BasicRead_FromOffset) { |
| 323 | int64 file_size; |
| 324 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 325 | EXPECT_TRUE(ok); |
| 326 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 327 | FileStream stream(NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 328 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 329 | base::PLATFORM_FILE_READ; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 330 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 331 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 332 | |
| 333 | const int64 kOffset = 3; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 334 | int64 new_offset = stream.Seek(FROM_BEGIN, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 335 | EXPECT_EQ(kOffset, new_offset); |
| 336 | |
| 337 | int64 total_bytes_avail = stream.Available(); |
| 338 | EXPECT_EQ(file_size - kOffset, total_bytes_avail); |
| 339 | |
| 340 | int64 total_bytes_read = 0; |
| 341 | |
| 342 | std::string data_read; |
| 343 | for (;;) { |
| 344 | char buf[4]; |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 345 | rv = stream.ReadSync(buf, arraysize(buf)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 346 | EXPECT_LE(0, rv); |
| 347 | if (rv <= 0) |
| 348 | break; |
| 349 | total_bytes_read += rv; |
| 350 | data_read.append(buf, rv); |
| 351 | } |
| 352 | EXPECT_EQ(file_size - kOffset, total_bytes_read); |
| 353 | EXPECT_TRUE(data_read == kTestData + kOffset); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 354 | EXPECT_EQ(kTestData + kOffset, data_read); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | TEST_F(FileStreamTest, AsyncRead_FromOffset) { |
| 358 | int64 file_size; |
| 359 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 360 | EXPECT_TRUE(ok); |
| 361 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 362 | FileStream stream(NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 363 | int flags = base::PLATFORM_FILE_OPEN | |
| 364 | base::PLATFORM_FILE_READ | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 365 | base::PLATFORM_FILE_ASYNC; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 366 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 367 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 368 | |
| 369 | const int64 kOffset = 3; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 370 | int64 new_offset = stream.Seek(FROM_BEGIN, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 371 | EXPECT_EQ(kOffset, new_offset); |
| 372 | |
| 373 | int64 total_bytes_avail = stream.Available(); |
| 374 | EXPECT_EQ(file_size - kOffset, total_bytes_avail); |
| 375 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 376 | TestCompletionCallback callback; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 377 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 378 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 379 | |
| 380 | std::string data_read; |
| 381 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 382 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 383 | rv = stream.Read(buf, buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 384 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 385 | rv = callback.WaitForResult(); |
| 386 | EXPECT_LE(0, rv); |
| 387 | if (rv <= 0) |
| 388 | break; |
| 389 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 390 | data_read.append(buf->data(), rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 391 | } |
| 392 | EXPECT_EQ(file_size - kOffset, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 393 | EXPECT_EQ(kTestData + kOffset, data_read); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | TEST_F(FileStreamTest, SeekAround) { |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 397 | FileStream stream(NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 398 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 399 | base::PLATFORM_FILE_READ; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 400 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 401 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 402 | |
| 403 | const int64 kOffset = 3; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 404 | int64 new_offset = stream.Seek(FROM_BEGIN, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 405 | EXPECT_EQ(kOffset, new_offset); |
| 406 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 407 | new_offset = stream.Seek(FROM_CURRENT, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 408 | EXPECT_EQ(2 * kOffset, new_offset); |
| 409 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 410 | new_offset = stream.Seek(FROM_CURRENT, -kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 411 | EXPECT_EQ(kOffset, new_offset); |
| 412 | |
| 413 | const int kTestDataLen = arraysize(kTestData) - 1; |
| 414 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 415 | new_offset = stream.Seek(FROM_END, -kTestDataLen); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 416 | EXPECT_EQ(0, new_offset); |
| 417 | } |
| 418 | |
| 419 | TEST_F(FileStreamTest, BasicWrite) { |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 420 | FileStream stream(NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 421 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 422 | base::PLATFORM_FILE_WRITE; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 423 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 424 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 425 | |
| 426 | int64 file_size; |
| 427 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 428 | EXPECT_TRUE(ok); |
| 429 | EXPECT_EQ(0, file_size); |
| 430 | |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 431 | rv = stream.WriteSync(kTestData, kTestDataSize); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 432 | EXPECT_EQ(kTestDataSize, rv); |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 433 | stream.CloseSync(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 434 | |
| 435 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 436 | EXPECT_TRUE(ok); |
| 437 | EXPECT_EQ(kTestDataSize, file_size); |
| 438 | } |
| 439 | |
| 440 | TEST_F(FileStreamTest, AsyncWrite) { |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 441 | FileStream stream(NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 442 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
| 443 | base::PLATFORM_FILE_WRITE | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 444 | base::PLATFORM_FILE_ASYNC; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 445 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 446 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 447 | |
| 448 | int64 file_size; |
| 449 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 450 | EXPECT_TRUE(ok); |
| 451 | EXPECT_EQ(0, file_size); |
| 452 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 453 | TestCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 454 | int total_bytes_written = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 455 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 456 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 457 | scoped_refptr<DrainableIOBuffer> drainable = |
| 458 | new DrainableIOBuffer(buf, buf->size()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 459 | while (total_bytes_written != kTestDataSize) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 460 | rv = stream.Write(drainable, drainable->BytesRemaining(), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 461 | callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 462 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 463 | rv = callback.WaitForResult(); |
| 464 | EXPECT_LT(0, rv); |
| 465 | if (rv <= 0) |
| 466 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 467 | drainable->DidConsume(rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 468 | total_bytes_written += rv; |
| 469 | } |
| 470 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 471 | EXPECT_TRUE(ok); |
| 472 | EXPECT_EQ(file_size, total_bytes_written); |
| 473 | } |
| 474 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 475 | TEST_F(FileStreamTest, AsyncWrite_EarlyClose) { |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 476 | FileStream stream(NULL); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 477 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
| 478 | base::PLATFORM_FILE_WRITE | |
| 479 | base::PLATFORM_FILE_ASYNC; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 480 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 481 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 482 | |
| 483 | int64 file_size; |
| 484 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 485 | EXPECT_TRUE(ok); |
| 486 | EXPECT_EQ(0, file_size); |
| 487 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 488 | TestCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 489 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 490 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 491 | rv = stream.Write(buf, buf->size(), callback.callback()); |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 492 | stream.CloseSync(); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 493 | if (rv < 0) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 494 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 495 | // The callback should not be called if the request is cancelled. |
| 496 | MessageLoop::current()->RunAllPending(); |
| 497 | EXPECT_FALSE(callback.have_result()); |
| 498 | } else { |
| 499 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 500 | EXPECT_TRUE(ok); |
| 501 | EXPECT_EQ(file_size, rv); |
| 502 | } |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 503 | } |
| 504 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 505 | TEST_F(FileStreamTest, BasicWrite_FromOffset) { |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 506 | FileStream stream(NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 507 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 508 | base::PLATFORM_FILE_WRITE; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 509 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 510 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 511 | |
| 512 | int64 file_size; |
| 513 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 514 | EXPECT_TRUE(ok); |
| 515 | EXPECT_EQ(kTestDataSize, file_size); |
| 516 | |
| 517 | const int64 kOffset = 0; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 518 | int64 new_offset = stream.Seek(FROM_END, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 519 | EXPECT_EQ(kTestDataSize, new_offset); |
| 520 | |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 521 | rv = stream.WriteSync(kTestData, kTestDataSize); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 522 | EXPECT_EQ(kTestDataSize, rv); |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 523 | stream.CloseSync(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 524 | |
| 525 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 526 | EXPECT_TRUE(ok); |
| 527 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 528 | } |
| 529 | |
| 530 | TEST_F(FileStreamTest, AsyncWrite_FromOffset) { |
| 531 | int64 file_size; |
| 532 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 533 | EXPECT_TRUE(ok); |
| 534 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 535 | FileStream stream(NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 536 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 537 | base::PLATFORM_FILE_WRITE | |
| 538 | base::PLATFORM_FILE_ASYNC; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 539 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 540 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 541 | |
| 542 | const int64 kOffset = 0; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 543 | int64 new_offset = stream.Seek(FROM_END, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 544 | EXPECT_EQ(kTestDataSize, new_offset); |
| 545 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 546 | TestCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 547 | int total_bytes_written = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 548 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 549 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 550 | scoped_refptr<DrainableIOBuffer> drainable = |
| 551 | new DrainableIOBuffer(buf, buf->size()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 552 | while (total_bytes_written != kTestDataSize) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 553 | rv = stream.Write(drainable, drainable->BytesRemaining(), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 554 | callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 555 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 556 | rv = callback.WaitForResult(); |
| 557 | EXPECT_LT(0, rv); |
| 558 | if (rv <= 0) |
| 559 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 560 | drainable->DidConsume(rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 561 | total_bytes_written += rv; |
| 562 | } |
| 563 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 564 | EXPECT_TRUE(ok); |
| 565 | EXPECT_EQ(file_size, kTestDataSize * 2); |
| 566 | } |
| 567 | |
| 568 | TEST_F(FileStreamTest, BasicReadWrite) { |
| 569 | int64 file_size; |
| 570 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 571 | EXPECT_TRUE(ok); |
| 572 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 573 | FileStream stream(NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 574 | int flags = base::PLATFORM_FILE_OPEN | |
| 575 | base::PLATFORM_FILE_READ | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 576 | base::PLATFORM_FILE_WRITE; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 577 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 578 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 579 | |
| 580 | int64 total_bytes_avail = stream.Available(); |
| 581 | EXPECT_EQ(file_size, total_bytes_avail); |
| 582 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 583 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 584 | |
| 585 | std::string data_read; |
| 586 | for (;;) { |
| 587 | char buf[4]; |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 588 | rv = stream.ReadSync(buf, arraysize(buf)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 589 | EXPECT_LE(0, rv); |
| 590 | if (rv <= 0) |
| 591 | break; |
| 592 | total_bytes_read += rv; |
| 593 | data_read.append(buf, rv); |
| 594 | } |
| 595 | EXPECT_EQ(file_size, total_bytes_read); |
| 596 | EXPECT_TRUE(data_read == kTestData); |
| 597 | |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 598 | rv = stream.WriteSync(kTestData, kTestDataSize); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 599 | EXPECT_EQ(kTestDataSize, rv); |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 600 | stream.CloseSync(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 601 | |
| 602 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 603 | EXPECT_TRUE(ok); |
| 604 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 605 | } |
| 606 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 607 | TEST_F(FileStreamTest, BasicWriteRead) { |
| 608 | int64 file_size; |
| 609 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 610 | EXPECT_TRUE(ok); |
| 611 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 612 | FileStream stream(NULL); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 613 | int flags = base::PLATFORM_FILE_OPEN | |
| 614 | base::PLATFORM_FILE_READ | |
| 615 | base::PLATFORM_FILE_WRITE; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 616 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 617 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 618 | |
| 619 | int64 total_bytes_avail = stream.Available(); |
| 620 | EXPECT_EQ(file_size, total_bytes_avail); |
| 621 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 622 | int64 offset = stream.Seek(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 623 | EXPECT_EQ(offset, file_size); |
| 624 | |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 625 | rv = stream.WriteSync(kTestData, kTestDataSize); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 626 | EXPECT_EQ(kTestDataSize, rv); |
| 627 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 628 | offset = stream.Seek(FROM_BEGIN, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 629 | EXPECT_EQ(0, offset); |
| 630 | |
| 631 | int64 total_bytes_read = 0; |
| 632 | |
| 633 | std::string data_read; |
| 634 | for (;;) { |
| 635 | char buf[4]; |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 636 | rv = stream.ReadSync(buf, arraysize(buf)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 637 | EXPECT_LE(0, rv); |
| 638 | if (rv <= 0) |
| 639 | break; |
| 640 | total_bytes_read += rv; |
| 641 | data_read.append(buf, rv); |
| 642 | } |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 643 | stream.CloseSync(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 644 | |
| 645 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 646 | EXPECT_TRUE(ok); |
| 647 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 648 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 649 | |
| 650 | const std::string kExpectedFileData = |
| 651 | std::string(kTestData) + std::string(kTestData); |
| 652 | EXPECT_EQ(kExpectedFileData, data_read); |
| 653 | } |
| 654 | |
| 655 | TEST_F(FileStreamTest, BasicAsyncReadWrite) { |
| 656 | int64 file_size; |
| 657 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 658 | EXPECT_TRUE(ok); |
| 659 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 660 | FileStream stream(NULL); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 661 | int flags = base::PLATFORM_FILE_OPEN | |
| 662 | base::PLATFORM_FILE_READ | |
| 663 | base::PLATFORM_FILE_WRITE | |
| 664 | base::PLATFORM_FILE_ASYNC; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 665 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 666 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 667 | |
| 668 | int64 total_bytes_avail = stream.Available(); |
| 669 | EXPECT_EQ(file_size, total_bytes_avail); |
| 670 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 671 | TestCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 672 | int64 total_bytes_read = 0; |
| 673 | |
| 674 | std::string data_read; |
| 675 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 676 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 677 | rv = stream.Read(buf, buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 678 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 679 | rv = callback.WaitForResult(); |
| 680 | EXPECT_LE(0, rv); |
| 681 | if (rv <= 0) |
| 682 | break; |
| 683 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 684 | data_read.append(buf->data(), rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 685 | } |
| 686 | EXPECT_EQ(file_size, total_bytes_read); |
| 687 | EXPECT_TRUE(data_read == kTestData); |
| 688 | |
| 689 | int total_bytes_written = 0; |
| 690 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 691 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 692 | scoped_refptr<DrainableIOBuffer> drainable = |
| 693 | new DrainableIOBuffer(buf, buf->size()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 694 | while (total_bytes_written != kTestDataSize) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 695 | rv = stream.Write(drainable, drainable->BytesRemaining(), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 696 | callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 697 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 698 | rv = callback.WaitForResult(); |
| 699 | EXPECT_LT(0, rv); |
| 700 | if (rv <= 0) |
| 701 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 702 | drainable->DidConsume(rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 703 | total_bytes_written += rv; |
| 704 | } |
| 705 | |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 706 | stream.CloseSync(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 707 | |
| 708 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 709 | EXPECT_TRUE(ok); |
| 710 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 711 | } |
| 712 | |
| 713 | TEST_F(FileStreamTest, BasicAsyncWriteRead) { |
| 714 | int64 file_size; |
| 715 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 716 | EXPECT_TRUE(ok); |
| 717 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 718 | FileStream stream(NULL); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 719 | int flags = base::PLATFORM_FILE_OPEN | |
| 720 | base::PLATFORM_FILE_READ | |
| 721 | base::PLATFORM_FILE_WRITE | |
| 722 | base::PLATFORM_FILE_ASYNC; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 723 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 724 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 725 | |
| 726 | int64 total_bytes_avail = stream.Available(); |
| 727 | EXPECT_EQ(file_size, total_bytes_avail); |
| 728 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 729 | int64 offset = stream.Seek(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 730 | EXPECT_EQ(offset, file_size); |
| 731 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 732 | TestCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 733 | int total_bytes_written = 0; |
| 734 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 735 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 736 | scoped_refptr<DrainableIOBuffer> drainable = |
| 737 | new DrainableIOBuffer(buf, buf->size()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 738 | while (total_bytes_written != kTestDataSize) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 739 | rv = stream.Write(drainable, drainable->BytesRemaining(), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 740 | callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 741 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 742 | rv = callback.WaitForResult(); |
| 743 | EXPECT_LT(0, rv); |
| 744 | if (rv <= 0) |
| 745 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 746 | drainable->DidConsume(rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 747 | total_bytes_written += rv; |
| 748 | } |
| 749 | |
| 750 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 751 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 752 | offset = stream.Seek(FROM_BEGIN, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 753 | EXPECT_EQ(0, offset); |
| 754 | |
| 755 | int total_bytes_read = 0; |
| 756 | |
| 757 | std::string data_read; |
| 758 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 759 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 760 | rv = stream.Read(buf, buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 761 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 762 | rv = callback.WaitForResult(); |
| 763 | EXPECT_LE(0, rv); |
| 764 | if (rv <= 0) |
| 765 | break; |
| 766 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 767 | data_read.append(buf->data(), rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 768 | } |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 769 | stream.CloseSync(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 770 | |
| 771 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 772 | EXPECT_TRUE(ok); |
| 773 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 774 | |
| 775 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 776 | const std::string kExpectedFileData = |
| 777 | std::string(kTestData) + std::string(kTestData); |
| 778 | EXPECT_EQ(kExpectedFileData, data_read); |
| 779 | } |
| 780 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 781 | class TestWriteReadCompletionCallback { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 782 | public: |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 783 | TestWriteReadCompletionCallback( |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 784 | FileStream* stream, |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 785 | int* total_bytes_written, |
| 786 | int* total_bytes_read, |
| 787 | std::string* data_read) |
| 788 | : result_(0), |
| 789 | have_result_(false), |
| 790 | waiting_for_result_(false), |
| 791 | stream_(stream), |
| 792 | total_bytes_written_(total_bytes_written), |
| 793 | total_bytes_read_(total_bytes_read), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 794 | data_read_(data_read), |
| 795 | callback_(base::Bind(&TestWriteReadCompletionCallback::OnComplete, |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 796 | base::Unretained(this))), |
| 797 | test_data_(CreateTestDataBuffer()), |
| 798 | drainable_(new DrainableIOBuffer(test_data_, kTestDataSize)) { |
| 799 | } |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 800 | |
| 801 | int WaitForResult() { |
| 802 | DCHECK(!waiting_for_result_); |
| 803 | while (!have_result_) { |
| 804 | waiting_for_result_ = true; |
| 805 | MessageLoop::current()->Run(); |
| 806 | waiting_for_result_ = false; |
| 807 | } |
| 808 | have_result_ = false; // auto-reset for next callback |
| 809 | return result_; |
| 810 | } |
| 811 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 812 | const CompletionCallback& callback() const { return callback_; } |
| 813 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 814 | private: |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 815 | void OnComplete(int result) { |
| 816 | DCHECK_LT(0, result); |
| 817 | *total_bytes_written_ += result; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 818 | |
| 819 | int rv; |
| 820 | |
| 821 | if (*total_bytes_written_ != kTestDataSize) { |
| 822 | // Recurse to finish writing all data. |
| 823 | int total_bytes_written = 0, total_bytes_read = 0; |
| 824 | std::string data_read; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 825 | TestWriteReadCompletionCallback callback( |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 826 | stream_, &total_bytes_written, &total_bytes_read, &data_read); |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 827 | rv = stream_->Write(drainable_, drainable_->BytesRemaining(), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 828 | callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 829 | DCHECK_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 830 | rv = callback.WaitForResult(); |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 831 | drainable_->DidConsume(total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 832 | *total_bytes_written_ += total_bytes_written; |
| 833 | *total_bytes_read_ += total_bytes_read; |
| 834 | *data_read_ += data_read; |
| 835 | } else { // We're done writing all data. Start reading the data. |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 836 | stream_->Seek(FROM_BEGIN, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 837 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 838 | TestCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 839 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 840 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 841 | rv = stream_->Read(buf, buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 842 | if (rv == ERR_IO_PENDING) { |
[email protected] | b5717a4 | 2012-02-14 19:33:52 | [diff] [blame] | 843 | MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 844 | rv = callback.WaitForResult(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 845 | } |
| 846 | EXPECT_LE(0, rv); |
| 847 | if (rv <= 0) |
| 848 | break; |
| 849 | *total_bytes_read_ += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 850 | data_read_->append(buf->data(), rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
| 854 | result_ = *total_bytes_written_; |
| 855 | have_result_ = true; |
| 856 | if (waiting_for_result_) |
| 857 | MessageLoop::current()->Quit(); |
| 858 | } |
| 859 | |
| 860 | int result_; |
| 861 | bool have_result_; |
| 862 | bool waiting_for_result_; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 863 | FileStream* stream_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 864 | int* total_bytes_written_; |
| 865 | int* total_bytes_read_; |
| 866 | std::string* data_read_; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 867 | const CompletionCallback callback_; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 868 | scoped_refptr<IOBufferWithSize> test_data_; |
| 869 | scoped_refptr<DrainableIOBuffer> drainable_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 870 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 871 | DISALLOW_COPY_AND_ASSIGN(TestWriteReadCompletionCallback); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 872 | }; |
| 873 | |
| 874 | TEST_F(FileStreamTest, AsyncWriteRead) { |
| 875 | int64 file_size; |
| 876 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 877 | EXPECT_TRUE(ok); |
| 878 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 879 | FileStream stream(NULL); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 880 | int flags = base::PLATFORM_FILE_OPEN | |
| 881 | base::PLATFORM_FILE_READ | |
| 882 | base::PLATFORM_FILE_WRITE | |
| 883 | base::PLATFORM_FILE_ASYNC; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 884 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 885 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 886 | |
| 887 | int64 total_bytes_avail = stream.Available(); |
| 888 | EXPECT_EQ(file_size, total_bytes_avail); |
| 889 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 890 | int64 offset = stream.Seek(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 891 | EXPECT_EQ(offset, file_size); |
| 892 | |
| 893 | int total_bytes_written = 0; |
| 894 | int total_bytes_read = 0; |
| 895 | std::string data_read; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 896 | TestWriteReadCompletionCallback callback(&stream, &total_bytes_written, |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 897 | &total_bytes_read, &data_read); |
| 898 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 899 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 900 | rv = stream.Write(buf, buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 901 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 902 | rv = callback.WaitForResult(); |
| 903 | EXPECT_LT(0, rv); |
| 904 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 905 | |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 906 | stream.CloseSync(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 907 | |
| 908 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 909 | EXPECT_TRUE(ok); |
| 910 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 911 | |
| 912 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 913 | const std::string kExpectedFileData = |
| 914 | std::string(kTestData) + std::string(kTestData); |
| 915 | EXPECT_EQ(kExpectedFileData, data_read); |
| 916 | } |
| 917 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 918 | class TestWriteCloseCompletionCallback { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 919 | public: |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 920 | TestWriteCloseCompletionCallback(FileStream* stream, int* total_bytes_written) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 921 | : result_(0), |
| 922 | have_result_(false), |
| 923 | waiting_for_result_(false), |
| 924 | stream_(stream), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 925 | total_bytes_written_(total_bytes_written), |
| 926 | callback_(base::Bind(&TestWriteCloseCompletionCallback::OnComplete, |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 927 | base::Unretained(this))), |
| 928 | test_data_(CreateTestDataBuffer()), |
| 929 | drainable_(new DrainableIOBuffer(test_data_, kTestDataSize)) { |
| 930 | } |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 931 | |
| 932 | int WaitForResult() { |
| 933 | DCHECK(!waiting_for_result_); |
| 934 | while (!have_result_) { |
| 935 | waiting_for_result_ = true; |
| 936 | MessageLoop::current()->Run(); |
| 937 | waiting_for_result_ = false; |
| 938 | } |
| 939 | have_result_ = false; // auto-reset for next callback |
| 940 | return result_; |
| 941 | } |
| 942 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 943 | const CompletionCallback& callback() const { return callback_; } |
| 944 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 945 | private: |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 946 | void OnComplete(int result) { |
| 947 | DCHECK_LT(0, result); |
| 948 | *total_bytes_written_ += result; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 949 | |
| 950 | int rv; |
| 951 | |
| 952 | if (*total_bytes_written_ != kTestDataSize) { |
| 953 | // Recurse to finish writing all data. |
| 954 | int total_bytes_written = 0; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 955 | TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written); |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 956 | rv = stream_->Write(drainable_, drainable_->BytesRemaining(), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 957 | callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 958 | DCHECK_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 959 | rv = callback.WaitForResult(); |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 960 | drainable_->DidConsume(total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 961 | *total_bytes_written_ += total_bytes_written; |
| 962 | } else { // We're done writing all data. Close the file. |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 963 | stream_->CloseSync(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | result_ = *total_bytes_written_; |
| 967 | have_result_ = true; |
| 968 | if (waiting_for_result_) |
| 969 | MessageLoop::current()->Quit(); |
| 970 | } |
| 971 | |
| 972 | int result_; |
| 973 | bool have_result_; |
| 974 | bool waiting_for_result_; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 975 | FileStream* stream_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 976 | int* total_bytes_written_; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 977 | const CompletionCallback callback_; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 978 | scoped_refptr<IOBufferWithSize> test_data_; |
| 979 | scoped_refptr<DrainableIOBuffer> drainable_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 980 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 981 | DISALLOW_COPY_AND_ASSIGN(TestWriteCloseCompletionCallback); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 982 | }; |
| 983 | |
| 984 | TEST_F(FileStreamTest, AsyncWriteClose) { |
| 985 | int64 file_size; |
| 986 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 987 | EXPECT_TRUE(ok); |
| 988 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 989 | FileStream stream(NULL); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 990 | int flags = base::PLATFORM_FILE_OPEN | |
| 991 | base::PLATFORM_FILE_READ | |
| 992 | base::PLATFORM_FILE_WRITE | |
| 993 | base::PLATFORM_FILE_ASYNC; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 994 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 995 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 996 | |
| 997 | int64 total_bytes_avail = stream.Available(); |
| 998 | EXPECT_EQ(file_size, total_bytes_avail); |
| 999 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 1000 | int64 offset = stream.Seek(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1001 | EXPECT_EQ(offset, file_size); |
| 1002 | |
| 1003 | int total_bytes_written = 0; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 1004 | TestWriteCloseCompletionCallback callback(&stream, &total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1005 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 1006 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 1007 | rv = stream.Write(buf, buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 1008 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1009 | total_bytes_written = callback.WaitForResult(); |
| 1010 | EXPECT_LT(0, total_bytes_written); |
| 1011 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 1012 | |
| 1013 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 1014 | EXPECT_TRUE(ok); |
| 1015 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 1016 | } |
| 1017 | |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1018 | // Tests truncating a file. |
| 1019 | TEST_F(FileStreamTest, Truncate) { |
| 1020 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE; |
| 1021 | |
[email protected] | 1034299 | 2012-02-02 18:49:43 | [diff] [blame] | 1022 | FileStream write_stream(NULL); |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 1023 | ASSERT_EQ(OK, write_stream.OpenSync(temp_file_path(), flags)); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1024 | |
| 1025 | // Write some data to the file. |
| 1026 | const char test_data[] = "0123456789"; |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 1027 | write_stream.WriteSync(test_data, arraysize(test_data)); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1028 | |
| 1029 | // Truncate the file. |
| 1030 | ASSERT_EQ(4, write_stream.Truncate(4)); |
| 1031 | |
| 1032 | // Write again. |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 1033 | write_stream.WriteSync(test_data, 4); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1034 | |
| 1035 | // Close the stream. |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 1036 | write_stream.CloseSync(); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1037 | |
| 1038 | // Read in the contents and make sure we get back what we expected. |
| 1039 | std::string read_contents; |
[email protected] | 3fc364f9 | 2009-08-06 22:35:13 | [diff] [blame] | 1040 | EXPECT_TRUE(file_util::ReadFileToString(temp_file_path(), &read_contents)); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1041 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1042 | EXPECT_EQ("01230123", read_contents); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1043 | } |
| 1044 | |
[email protected] | e3d66fde | 2012-02-17 22:10:34 | [diff] [blame] | 1045 | TEST_F(FileStreamTest, AsyncBasicOpenClose) { |
| 1046 | FileStream stream(NULL); |
| 1047 | int flags = base::PLATFORM_FILE_OPEN | |
| 1048 | base::PLATFORM_FILE_READ | |
| 1049 | base::PLATFORM_FILE_ASYNC; |
| 1050 | TestCompletionCallback callback; |
| 1051 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 1052 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1053 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 1054 | EXPECT_TRUE(stream.IsOpen()); |
| 1055 | |
| 1056 | stream.Close(callback.callback()); |
| 1057 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 1058 | EXPECT_FALSE(stream.IsOpen()); |
| 1059 | } |
| 1060 | |
| 1061 | TEST_F(FileStreamTest, SyncCloseTwice) { |
| 1062 | FileStream stream(NULL); |
| 1063 | int flags = base::PLATFORM_FILE_OPEN | |
| 1064 | base::PLATFORM_FILE_READ; |
| 1065 | int rv = stream.OpenSync(temp_file_path(), flags); |
| 1066 | EXPECT_EQ(OK, rv); |
| 1067 | EXPECT_TRUE(stream.IsOpen()); |
| 1068 | |
| 1069 | // Closing twice should be safe. |
| 1070 | stream.CloseSync(); |
| 1071 | EXPECT_FALSE(stream.IsOpen()); |
| 1072 | |
| 1073 | stream.CloseSync(); |
| 1074 | EXPECT_FALSE(stream.IsOpen()); |
| 1075 | } |
| 1076 | |
| 1077 | TEST_F(FileStreamTest, AsyncCloseTwice) { |
| 1078 | FileStream stream(NULL); |
| 1079 | int flags = base::PLATFORM_FILE_OPEN | |
| 1080 | base::PLATFORM_FILE_READ | |
| 1081 | base::PLATFORM_FILE_ASYNC; |
| 1082 | TestCompletionCallback callback; |
| 1083 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 1084 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1085 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 1086 | EXPECT_TRUE(stream.IsOpen()); |
| 1087 | |
| 1088 | // Closing twice should be safe. |
| 1089 | stream.Close(callback.callback()); |
| 1090 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 1091 | EXPECT_FALSE(stream.IsOpen()); |
| 1092 | |
| 1093 | stream.Close(callback.callback()); |
| 1094 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 1095 | EXPECT_FALSE(stream.IsOpen()); |
| 1096 | } |
| 1097 | |
[email protected] | 285673c | 2012-02-24 20:48:24 | [diff] [blame^] | 1098 | // TODO(satorux): This should be gone once all once all async clients are |
| 1099 | // migrated to use Close(). crbug.com/114783 |
| 1100 | TEST_F(FileStreamTest, AsyncWriteAndCloseSync) { |
| 1101 | FileStream stream(NULL); |
| 1102 | int flags = base::PLATFORM_FILE_OPEN | |
| 1103 | base::PLATFORM_FILE_WRITE | |
| 1104 | base::PLATFORM_FILE_ASYNC; |
| 1105 | TestCompletionCallback callback; |
| 1106 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 1107 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1108 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 1109 | EXPECT_TRUE(stream.IsOpen()); |
| 1110 | |
| 1111 | // Write some data asynchronously. |
| 1112 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 1113 | stream.Write(buf, buf->size(), callback.callback()); |
| 1114 | |
| 1115 | // Close the stream without waiting for the completion. |
| 1116 | stream.CloseSync(); |
| 1117 | } |
| 1118 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1119 | } // namespace |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 1120 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 1121 | } // namespace net |