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