[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] | 5ee2098 | 2013-07-17 21:51:18 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 11 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 12 | #include "base/path_service.h" |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 13 | #include "base/platform_file.h" |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 14 | #include "base/run_loop.h" |
[email protected] | e3d66fde | 2012-02-17 22:10:34 | [diff] [blame] | 15 | #include "base/synchronization/waitable_event.h" |
| 16 | #include "base/test/test_timeouts.h" |
| 17 | #include "net/base/capturing_net_log.h" |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 18 | #include "net/base/io_buffer.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 19 | #include "net/base/net_errors.h" |
| 20 | #include "net/base/test_completion_callback.h" |
| 21 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 22 | #include "testing/platform_test.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 23 | |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 24 | #if defined(OS_ANDROID) |
| 25 | #include "base/test/test_file_util.h" |
| 26 | #endif |
| 27 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 28 | namespace net { |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 29 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | const char kTestData[] = "0123456789"; |
| 33 | const int kTestDataSize = arraysize(kTestData) - 1; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 34 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 35 | // Creates an IOBufferWithSize that contains the kTestDataSize. |
| 36 | IOBufferWithSize* CreateTestDataBuffer() { |
| 37 | IOBufferWithSize* buf = new IOBufferWithSize(kTestDataSize); |
| 38 | memcpy(buf->data(), kTestData, kTestDataSize); |
| 39 | return buf; |
| 40 | } |
| 41 | |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 42 | } // namespace |
| 43 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 44 | class FileStreamTest : public PlatformTest { |
| 45 | public: |
| 46 | virtual void SetUp() { |
| 47 | PlatformTest::SetUp(); |
| 48 | |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame^] | 49 | base::CreateTemporaryFile(&temp_file_path_); |
[email protected] | 7ff3f63 | 2009-10-13 18:43:35 | [diff] [blame] | 50 | file_util::WriteFile(temp_file_path_, kTestData, kTestDataSize); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 51 | } |
| 52 | virtual void TearDown() { |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 53 | EXPECT_TRUE(base::DeleteFile(temp_file_path_, false)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 54 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 55 | // FileStreamContexts must be asynchronously closed on the file task runner |
| 56 | // before they can be deleted. Pump the RunLoop to avoid leaks. |
| 57 | base::RunLoop().RunUntilIdle(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 58 | PlatformTest::TearDown(); |
| 59 | } |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 60 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 61 | const base::FilePath temp_file_path() const { return temp_file_path_; } |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 62 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 63 | private: |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 64 | base::FilePath temp_file_path_; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 65 | }; |
| 66 | |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 67 | namespace { |
| 68 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 69 | TEST_F(FileStreamTest, BasicOpenClose) { |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 70 | base::PlatformFile file = base::kInvalidPlatformFileValue; |
| 71 | { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 72 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 73 | int rv = stream.OpenSync(temp_file_path(), |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 74 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); |
| 75 | EXPECT_EQ(OK, rv); |
| 76 | EXPECT_TRUE(stream.IsOpen()); |
[email protected] | 84e0309f | 2012-02-24 01:56:59 | [diff] [blame] | 77 | file = stream.GetPlatformFileForTesting(); |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 78 | } |
| 79 | EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 80 | base::PlatformFileInfo info; |
| 81 | // The file should be closed. |
| 82 | EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); |
| 83 | } |
| 84 | |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 85 | TEST_F(FileStreamTest, BasicOpenExplicitClose) { |
| 86 | base::PlatformFile file = base::kInvalidPlatformFileValue; |
| 87 | FileStream stream(NULL); |
| 88 | int rv = stream.OpenSync(temp_file_path(), |
| 89 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); |
| 90 | EXPECT_EQ(OK, rv); |
| 91 | EXPECT_TRUE(stream.IsOpen()); |
| 92 | file = stream.GetPlatformFileForTesting(); |
| 93 | EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 94 | EXPECT_EQ(OK, stream.CloseSync()); |
| 95 | EXPECT_FALSE(stream.IsOpen()); |
| 96 | base::PlatformFileInfo info; |
| 97 | // The file should be closed. |
| 98 | EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); |
| 99 | } |
| 100 | |
| 101 | TEST_F(FileStreamTest, AsyncOpenExplicitClose) { |
| 102 | base::PlatformFile file = base::kInvalidPlatformFileValue; |
| 103 | TestCompletionCallback callback; |
| 104 | FileStream stream(NULL); |
| 105 | int flags = base::PLATFORM_FILE_OPEN | |
| 106 | base::PLATFORM_FILE_READ | |
| 107 | base::PLATFORM_FILE_ASYNC; |
| 108 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 109 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 110 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 111 | EXPECT_TRUE(stream.IsOpen()); |
| 112 | file = stream.GetPlatformFileForTesting(); |
| 113 | EXPECT_EQ(ERR_IO_PENDING, stream.Close(callback.callback())); |
| 114 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 115 | EXPECT_FALSE(stream.IsOpen()); |
| 116 | base::PlatformFileInfo info; |
| 117 | // The file should be closed. |
| 118 | EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); |
| 119 | } |
| 120 | |
| 121 | TEST_F(FileStreamTest, AsyncOpenExplicitCloseOrphaned) { |
| 122 | base::PlatformFile file = base::kInvalidPlatformFileValue; |
| 123 | TestCompletionCallback callback; |
| 124 | base::PlatformFileInfo info; |
| 125 | scoped_ptr<FileStream> stream(new FileStream( |
| 126 | NULL, base::MessageLoopProxy::current())); |
| 127 | int flags = base::PLATFORM_FILE_OPEN | |
| 128 | base::PLATFORM_FILE_READ | |
| 129 | base::PLATFORM_FILE_ASYNC; |
| 130 | int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
| 131 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 132 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 133 | EXPECT_TRUE(stream->IsOpen()); |
| 134 | file = stream->GetPlatformFileForTesting(); |
| 135 | EXPECT_EQ(ERR_IO_PENDING, stream->Close(callback.callback())); |
| 136 | stream.reset(); |
| 137 | // File isn't actually closed yet. |
| 138 | EXPECT_TRUE(base::GetPlatformFileInfo(file, &info)); |
| 139 | base::RunLoop runloop; |
| 140 | runloop.RunUntilIdle(); |
| 141 | // The file should now be closed, though the callback has not been called. |
| 142 | EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); |
| 143 | } |
| 144 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 145 | TEST_F(FileStreamTest, FileHandleNotLeftOpen) { |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 146 | bool created = false; |
| 147 | ASSERT_EQ(kTestDataSize, |
| 148 | file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
| 149 | int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; |
| 150 | base::PlatformFile file = base::CreatePlatformFile( |
| 151 | temp_file_path(), flags, &created, NULL); |
| 152 | |
| 153 | { |
| 154 | // Seek to the beginning of the file and read. |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 155 | FileStream read_stream(file, flags, NULL, |
| 156 | base::MessageLoopProxy::current()); |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 157 | EXPECT_TRUE(read_stream.IsOpen()); |
| 158 | } |
| 159 | |
| 160 | EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 161 | base::PlatformFileInfo info; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 162 | // The file should be closed. |
| 163 | EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 166 | // Test the use of FileStream with a file handle provided at construction. |
| 167 | TEST_F(FileStreamTest, UseFileHandle) { |
| 168 | bool created = false; |
| 169 | |
| 170 | // 1. Test reading with a file handle. |
| 171 | ASSERT_EQ(kTestDataSize, |
| 172 | file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
| 173 | int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; |
| 174 | base::PlatformFile file = base::CreatePlatformFile( |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 175 | temp_file_path(), flags, &created, NULL); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 176 | |
| 177 | // Seek to the beginning of the file and read. |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 178 | scoped_ptr<FileStream> read_stream( |
| 179 | new FileStream(file, flags, NULL, base::MessageLoopProxy::current())); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 180 | ASSERT_EQ(0, read_stream->SeekSync(FROM_BEGIN, 0)); |
| 181 | ASSERT_EQ(kTestDataSize, read_stream->Available()); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 182 | // Read into buffer and compare. |
| 183 | char buffer[kTestDataSize]; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 184 | ASSERT_EQ(kTestDataSize, |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 185 | read_stream->ReadSync(buffer, kTestDataSize)); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 186 | ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 187 | read_stream.reset(); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 188 | |
| 189 | // 2. Test writing with a file handle. |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 190 | base::DeleteFile(temp_file_path(), false); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 191 | flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE; |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 192 | file = base::CreatePlatformFile(temp_file_path(), flags, &created, NULL); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 193 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 194 | scoped_ptr<FileStream> write_stream( |
| 195 | new FileStream(file, flags, NULL, base::MessageLoopProxy::current())); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 196 | ASSERT_EQ(0, write_stream->SeekSync(FROM_BEGIN, 0)); |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 197 | ASSERT_EQ(kTestDataSize, |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 198 | write_stream->WriteSync(kTestData, kTestDataSize)); |
| 199 | write_stream.reset(); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 200 | |
| 201 | // Read into buffer and compare to make sure the handle worked fine. |
| 202 | ASSERT_EQ(kTestDataSize, |
| 203 | file_util::ReadFile(temp_file_path(), buffer, kTestDataSize)); |
| 204 | ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); |
| 205 | } |
| 206 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 207 | TEST_F(FileStreamTest, UseClosedStream) { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 208 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 209 | |
| 210 | EXPECT_FALSE(stream.IsOpen()); |
| 211 | |
| 212 | // Try seeking... |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 213 | int64 new_offset = stream.SeekSync(FROM_BEGIN, 5); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 214 | EXPECT_EQ(ERR_UNEXPECTED, new_offset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 215 | |
| 216 | // Try available... |
| 217 | int64 avail = stream.Available(); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 218 | EXPECT_EQ(ERR_UNEXPECTED, avail); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 219 | |
| 220 | // Try reading... |
| 221 | char buf[10]; |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 222 | int rv = stream.ReadSync(buf, arraysize(buf)); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 223 | EXPECT_EQ(ERR_UNEXPECTED, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | TEST_F(FileStreamTest, BasicRead) { |
| 227 | int64 file_size; |
| 228 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 229 | EXPECT_TRUE(ok); |
| 230 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 231 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 232 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 233 | base::PLATFORM_FILE_READ; |
[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] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 240 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 241 | |
| 242 | std::string data_read; |
| 243 | for (;;) { |
| 244 | char buf[4]; |
[email protected] | 6b230f4 | 2012-02-15 04:08:41 | [diff] [blame] | 245 | rv = stream.ReadSync(buf, arraysize(buf)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 246 | EXPECT_LE(0, rv); |
| 247 | if (rv <= 0) |
| 248 | break; |
| 249 | total_bytes_read += rv; |
| 250 | data_read.append(buf, rv); |
| 251 | } |
| 252 | EXPECT_EQ(file_size, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 253 | EXPECT_EQ(kTestData, data_read); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | TEST_F(FileStreamTest, AsyncRead) { |
| 257 | int64 file_size; |
| 258 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 259 | EXPECT_TRUE(ok); |
| 260 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 261 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 262 | int flags = base::PLATFORM_FILE_OPEN | |
| 263 | base::PLATFORM_FILE_READ | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 264 | base::PLATFORM_FILE_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 265 | TestCompletionCallback callback; |
| 266 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 267 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 268 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 269 | |
| 270 | int64 total_bytes_avail = stream.Available(); |
| 271 | EXPECT_EQ(file_size, total_bytes_avail); |
| 272 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 273 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 274 | |
| 275 | std::string data_read; |
| 276 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 277 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 278 | rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 279 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 280 | rv = callback.WaitForResult(); |
| 281 | EXPECT_LE(0, rv); |
| 282 | if (rv <= 0) |
| 283 | break; |
| 284 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 285 | data_read.append(buf->data(), rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 286 | } |
| 287 | EXPECT_EQ(file_size, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 288 | EXPECT_EQ(kTestData, data_read); |
| 289 | } |
| 290 | |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 291 | TEST_F(FileStreamTest, AsyncRead_EarlyDelete) { |
| 292 | int64 file_size; |
| 293 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 294 | EXPECT_TRUE(ok); |
| 295 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 296 | scoped_ptr<FileStream> stream( |
| 297 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 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); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 310 | rv = stream->Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 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. |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 315 | base::RunLoop().RunUntilIdle(); |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 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] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 327 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
[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] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 334 | int64 new_offset = stream.SeekSync(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] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 362 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
[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] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 366 | TestCompletionCallback callback; |
| 367 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 368 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 369 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 370 | |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 371 | TestInt64CompletionCallback callback64; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 372 | const int64 kOffset = 3; |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 373 | rv = stream.Seek(FROM_BEGIN, kOffset, callback64.callback()); |
| 374 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 375 | int64 new_offset = callback64.WaitForResult(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 376 | EXPECT_EQ(kOffset, new_offset); |
| 377 | |
| 378 | int64 total_bytes_avail = stream.Available(); |
| 379 | EXPECT_EQ(file_size - kOffset, total_bytes_avail); |
| 380 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 381 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 382 | |
| 383 | std::string data_read; |
| 384 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 385 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 386 | rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 387 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 388 | rv = callback.WaitForResult(); |
| 389 | EXPECT_LE(0, rv); |
| 390 | if (rv <= 0) |
| 391 | break; |
| 392 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 393 | data_read.append(buf->data(), rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 394 | } |
| 395 | EXPECT_EQ(file_size - kOffset, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 396 | EXPECT_EQ(kTestData + kOffset, data_read); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | TEST_F(FileStreamTest, SeekAround) { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 400 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 401 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 402 | base::PLATFORM_FILE_READ; |
[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 403 | int rv = stream.OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 404 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 405 | |
| 406 | const int64 kOffset = 3; |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 407 | int64 new_offset = stream.SeekSync(FROM_BEGIN, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 408 | EXPECT_EQ(kOffset, new_offset); |
| 409 | |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 410 | new_offset = stream.SeekSync(FROM_CURRENT, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 411 | EXPECT_EQ(2 * kOffset, new_offset); |
| 412 | |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 413 | new_offset = stream.SeekSync(FROM_CURRENT, -kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 414 | EXPECT_EQ(kOffset, new_offset); |
| 415 | |
| 416 | const int kTestDataLen = arraysize(kTestData) - 1; |
| 417 | |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 418 | new_offset = stream.SeekSync(FROM_END, -kTestDataLen); |
| 419 | EXPECT_EQ(0, new_offset); |
| 420 | } |
| 421 | |
| 422 | TEST_F(FileStreamTest, AsyncSeekAround) { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 423 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 424 | int flags = base::PLATFORM_FILE_OPEN | |
| 425 | base::PLATFORM_FILE_ASYNC | |
| 426 | base::PLATFORM_FILE_READ; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 427 | TestCompletionCallback callback; |
| 428 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 429 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 430 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 431 | |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 432 | TestInt64CompletionCallback callback64; |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 433 | |
| 434 | const int64 kOffset = 3; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 435 | rv = stream.Seek(FROM_BEGIN, kOffset, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 436 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 437 | int64 new_offset = callback64.WaitForResult(); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 438 | EXPECT_EQ(kOffset, new_offset); |
| 439 | |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 440 | rv = stream.Seek(FROM_CURRENT, kOffset, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 441 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 442 | new_offset = callback64.WaitForResult(); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 443 | EXPECT_EQ(2 * kOffset, new_offset); |
| 444 | |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 445 | rv = stream.Seek(FROM_CURRENT, -kOffset, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 446 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 447 | new_offset = callback64.WaitForResult(); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 448 | EXPECT_EQ(kOffset, new_offset); |
| 449 | |
| 450 | const int kTestDataLen = arraysize(kTestData) - 1; |
| 451 | |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 452 | rv = stream.Seek(FROM_END, -kTestDataLen, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 453 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 454 | new_offset = callback64.WaitForResult(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 455 | EXPECT_EQ(0, new_offset); |
| 456 | } |
| 457 | |
| 458 | TEST_F(FileStreamTest, BasicWrite) { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 459 | scoped_ptr<FileStream> stream( |
| 460 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 461 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 462 | base::PLATFORM_FILE_WRITE; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 463 | int rv = stream->OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 464 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 465 | |
| 466 | int64 file_size; |
| 467 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 468 | EXPECT_TRUE(ok); |
| 469 | EXPECT_EQ(0, file_size); |
| 470 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 471 | rv = stream->WriteSync(kTestData, kTestDataSize); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 472 | EXPECT_EQ(kTestDataSize, rv); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 473 | stream.reset(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 474 | |
| 475 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 476 | EXPECT_TRUE(ok); |
| 477 | EXPECT_EQ(kTestDataSize, file_size); |
| 478 | } |
| 479 | |
| 480 | TEST_F(FileStreamTest, AsyncWrite) { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 481 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 482 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
| 483 | base::PLATFORM_FILE_WRITE | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 484 | base::PLATFORM_FILE_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 485 | TestCompletionCallback callback; |
| 486 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 487 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 488 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 489 | |
| 490 | int64 file_size; |
| 491 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 492 | EXPECT_TRUE(ok); |
| 493 | EXPECT_EQ(0, file_size); |
| 494 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 495 | int total_bytes_written = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 496 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 497 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 498 | scoped_refptr<DrainableIOBuffer> drainable = |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 499 | new DrainableIOBuffer(buf.get(), buf->size()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 500 | while (total_bytes_written != kTestDataSize) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 501 | rv = stream.Write( |
| 502 | drainable.get(), drainable->BytesRemaining(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 503 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 504 | rv = callback.WaitForResult(); |
| 505 | EXPECT_LT(0, rv); |
| 506 | if (rv <= 0) |
| 507 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 508 | drainable->DidConsume(rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 509 | total_bytes_written += rv; |
| 510 | } |
| 511 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 512 | EXPECT_TRUE(ok); |
| 513 | EXPECT_EQ(file_size, total_bytes_written); |
| 514 | } |
| 515 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 516 | TEST_F(FileStreamTest, AsyncWrite_EarlyDelete) { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 517 | scoped_ptr<FileStream> stream( |
| 518 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 519 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
| 520 | base::PLATFORM_FILE_WRITE | |
| 521 | base::PLATFORM_FILE_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 522 | TestCompletionCallback callback; |
| 523 | int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
| 524 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 525 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 526 | |
| 527 | int64 file_size; |
| 528 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 529 | EXPECT_TRUE(ok); |
| 530 | EXPECT_EQ(0, file_size); |
| 531 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 532 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 533 | rv = stream->Write(buf.get(), buf->size(), callback.callback()); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 534 | stream.reset(); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 535 | if (rv < 0) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 536 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 537 | // The callback should not be called if the request is cancelled. |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 538 | base::RunLoop().RunUntilIdle(); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 539 | EXPECT_FALSE(callback.have_result()); |
| 540 | } else { |
| 541 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 542 | EXPECT_TRUE(ok); |
| 543 | EXPECT_EQ(file_size, rv); |
| 544 | } |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 545 | } |
| 546 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 547 | TEST_F(FileStreamTest, BasicWrite_FromOffset) { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 548 | scoped_ptr<FileStream> stream( |
| 549 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 550 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 551 | base::PLATFORM_FILE_WRITE; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 552 | int rv = stream->OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 553 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 554 | |
| 555 | int64 file_size; |
| 556 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 557 | EXPECT_TRUE(ok); |
| 558 | EXPECT_EQ(kTestDataSize, file_size); |
| 559 | |
| 560 | const int64 kOffset = 0; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 561 | int64 new_offset = stream->SeekSync(FROM_END, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 562 | EXPECT_EQ(kTestDataSize, new_offset); |
| 563 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 564 | rv = stream->WriteSync(kTestData, kTestDataSize); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 565 | EXPECT_EQ(kTestDataSize, rv); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 566 | stream.reset(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 567 | |
| 568 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 569 | EXPECT_TRUE(ok); |
| 570 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 571 | } |
| 572 | |
| 573 | TEST_F(FileStreamTest, AsyncWrite_FromOffset) { |
| 574 | int64 file_size; |
| 575 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 576 | EXPECT_TRUE(ok); |
| 577 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 578 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 579 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 580 | base::PLATFORM_FILE_WRITE | |
| 581 | base::PLATFORM_FILE_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 582 | TestCompletionCallback callback; |
| 583 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 584 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 585 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 586 | |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 587 | TestInt64CompletionCallback callback64; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 588 | const int64 kOffset = 0; |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 589 | rv = stream.Seek(FROM_END, kOffset, callback64.callback()); |
| 590 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 591 | int64 new_offset = callback64.WaitForResult(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 592 | EXPECT_EQ(kTestDataSize, new_offset); |
| 593 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 594 | int total_bytes_written = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 595 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 596 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 597 | scoped_refptr<DrainableIOBuffer> drainable = |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 598 | new DrainableIOBuffer(buf.get(), buf->size()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 599 | while (total_bytes_written != kTestDataSize) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 600 | rv = stream.Write( |
| 601 | drainable.get(), drainable->BytesRemaining(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 602 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 603 | rv = callback.WaitForResult(); |
| 604 | EXPECT_LT(0, rv); |
| 605 | if (rv <= 0) |
| 606 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 607 | drainable->DidConsume(rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 608 | total_bytes_written += rv; |
| 609 | } |
| 610 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 611 | EXPECT_TRUE(ok); |
| 612 | EXPECT_EQ(file_size, kTestDataSize * 2); |
| 613 | } |
| 614 | |
| 615 | TEST_F(FileStreamTest, BasicReadWrite) { |
| 616 | int64 file_size; |
| 617 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 618 | EXPECT_TRUE(ok); |
| 619 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 620 | scoped_ptr<FileStream> stream( |
| 621 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 622 | int flags = base::PLATFORM_FILE_OPEN | |
| 623 | base::PLATFORM_FILE_READ | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 624 | base::PLATFORM_FILE_WRITE; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 625 | int rv = stream->OpenSync(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 626 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 627 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 628 | int64 total_bytes_avail = stream->Available(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 629 | EXPECT_EQ(file_size, total_bytes_avail); |
| 630 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 631 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 632 | |
| 633 | std::string data_read; |
| 634 | for (;;) { |
| 635 | char buf[4]; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 636 | rv = stream->ReadSync(buf, arraysize(buf)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [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 | } |
| 643 | EXPECT_EQ(file_size, total_bytes_read); |
| 644 | EXPECT_TRUE(data_read == kTestData); |
| 645 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 646 | rv = stream->WriteSync(kTestData, kTestDataSize); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 647 | EXPECT_EQ(kTestDataSize, rv); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 648 | stream.reset(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 649 | |
| 650 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 651 | EXPECT_TRUE(ok); |
| 652 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 653 | } |
| 654 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 655 | TEST_F(FileStreamTest, BasicWriteRead) { |
| 656 | int64 file_size; |
| 657 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 658 | EXPECT_TRUE(ok); |
| 659 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 660 | scoped_ptr<FileStream> stream( |
| 661 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 662 | int flags = base::PLATFORM_FILE_OPEN | |
| 663 | base::PLATFORM_FILE_READ | |
| 664 | base::PLATFORM_FILE_WRITE; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [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 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 668 | int64 total_bytes_avail = stream->Available(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 669 | EXPECT_EQ(file_size, total_bytes_avail); |
| 670 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 671 | int64 offset = stream->SeekSync(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 672 | EXPECT_EQ(offset, file_size); |
| 673 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 674 | rv = stream->WriteSync(kTestData, kTestDataSize); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 675 | EXPECT_EQ(kTestDataSize, rv); |
| 676 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 677 | offset = stream->SeekSync(FROM_BEGIN, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 678 | EXPECT_EQ(0, offset); |
| 679 | |
| 680 | int64 total_bytes_read = 0; |
| 681 | |
| 682 | std::string data_read; |
| 683 | for (;;) { |
| 684 | char buf[4]; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 685 | rv = stream->ReadSync(buf, arraysize(buf)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 686 | EXPECT_LE(0, rv); |
| 687 | if (rv <= 0) |
| 688 | break; |
| 689 | total_bytes_read += rv; |
| 690 | data_read.append(buf, rv); |
| 691 | } |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 692 | stream.reset(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 693 | |
| 694 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 695 | EXPECT_TRUE(ok); |
| 696 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 697 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 698 | |
| 699 | const std::string kExpectedFileData = |
| 700 | std::string(kTestData) + std::string(kTestData); |
| 701 | EXPECT_EQ(kExpectedFileData, data_read); |
| 702 | } |
| 703 | |
| 704 | TEST_F(FileStreamTest, BasicAsyncReadWrite) { |
| 705 | int64 file_size; |
| 706 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 707 | EXPECT_TRUE(ok); |
| 708 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 709 | scoped_ptr<FileStream> stream( |
| 710 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 711 | int flags = base::PLATFORM_FILE_OPEN | |
| 712 | base::PLATFORM_FILE_READ | |
| 713 | base::PLATFORM_FILE_WRITE | |
| 714 | base::PLATFORM_FILE_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 715 | TestCompletionCallback callback; |
| 716 | int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
| 717 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 718 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 719 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 720 | int64 total_bytes_avail = stream->Available(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 721 | EXPECT_EQ(file_size, total_bytes_avail); |
| 722 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 723 | int64 total_bytes_read = 0; |
| 724 | |
| 725 | std::string data_read; |
| 726 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 727 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 728 | rv = stream->Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 729 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 730 | rv = callback.WaitForResult(); |
| 731 | EXPECT_LE(0, rv); |
| 732 | if (rv <= 0) |
| 733 | break; |
| 734 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 735 | data_read.append(buf->data(), rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 736 | } |
| 737 | EXPECT_EQ(file_size, total_bytes_read); |
| 738 | EXPECT_TRUE(data_read == kTestData); |
| 739 | |
| 740 | int total_bytes_written = 0; |
| 741 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 742 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 743 | scoped_refptr<DrainableIOBuffer> drainable = |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 744 | new DrainableIOBuffer(buf.get(), buf->size()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 745 | while (total_bytes_written != kTestDataSize) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 746 | rv = stream->Write( |
| 747 | drainable.get(), drainable->BytesRemaining(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 748 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 749 | rv = callback.WaitForResult(); |
| 750 | EXPECT_LT(0, rv); |
| 751 | if (rv <= 0) |
| 752 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 753 | drainable->DidConsume(rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 754 | total_bytes_written += rv; |
| 755 | } |
| 756 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 757 | stream.reset(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 758 | |
| 759 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 760 | EXPECT_TRUE(ok); |
| 761 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 762 | } |
| 763 | |
| 764 | TEST_F(FileStreamTest, BasicAsyncWriteRead) { |
| 765 | int64 file_size; |
| 766 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 767 | EXPECT_TRUE(ok); |
| 768 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 769 | scoped_ptr<FileStream> stream( |
| 770 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 771 | int flags = base::PLATFORM_FILE_OPEN | |
| 772 | base::PLATFORM_FILE_READ | |
| 773 | base::PLATFORM_FILE_WRITE | |
| 774 | base::PLATFORM_FILE_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 775 | TestCompletionCallback callback; |
| 776 | int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
| 777 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 778 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 779 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 780 | int64 total_bytes_avail = stream->Available(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 781 | EXPECT_EQ(file_size, total_bytes_avail); |
| 782 | |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 783 | TestInt64CompletionCallback callback64; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 784 | rv = stream->Seek(FROM_END, 0, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 785 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 786 | int64 offset = callback64.WaitForResult(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 787 | EXPECT_EQ(offset, file_size); |
| 788 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 789 | int total_bytes_written = 0; |
| 790 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 791 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 792 | scoped_refptr<DrainableIOBuffer> drainable = |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 793 | new DrainableIOBuffer(buf.get(), buf->size()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 794 | while (total_bytes_written != kTestDataSize) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 795 | rv = stream->Write( |
| 796 | drainable.get(), drainable->BytesRemaining(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 797 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 798 | rv = callback.WaitForResult(); |
| 799 | EXPECT_LT(0, rv); |
| 800 | if (rv <= 0) |
| 801 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 802 | drainable->DidConsume(rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 803 | total_bytes_written += rv; |
| 804 | } |
| 805 | |
| 806 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 807 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 808 | rv = stream->Seek(FROM_BEGIN, 0, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 809 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 810 | offset = callback64.WaitForResult(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 811 | EXPECT_EQ(0, offset); |
| 812 | |
| 813 | int total_bytes_read = 0; |
| 814 | |
| 815 | std::string data_read; |
| 816 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 817 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 818 | rv = stream->Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 819 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 820 | rv = callback.WaitForResult(); |
| 821 | EXPECT_LE(0, rv); |
| 822 | if (rv <= 0) |
| 823 | break; |
| 824 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 825 | data_read.append(buf->data(), rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 826 | } |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 827 | stream.reset(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 828 | |
| 829 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 830 | EXPECT_TRUE(ok); |
| 831 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 832 | |
| 833 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 834 | const std::string kExpectedFileData = |
| 835 | std::string(kTestData) + std::string(kTestData); |
| 836 | EXPECT_EQ(kExpectedFileData, data_read); |
| 837 | } |
| 838 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 839 | class TestWriteReadCompletionCallback { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 840 | public: |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 841 | TestWriteReadCompletionCallback(FileStream* stream, |
| 842 | int* total_bytes_written, |
| 843 | int* total_bytes_read, |
| 844 | std::string* data_read) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 845 | : result_(0), |
| 846 | have_result_(false), |
| 847 | waiting_for_result_(false), |
| 848 | stream_(stream), |
| 849 | total_bytes_written_(total_bytes_written), |
| 850 | total_bytes_read_(total_bytes_read), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 851 | data_read_(data_read), |
| 852 | callback_(base::Bind(&TestWriteReadCompletionCallback::OnComplete, |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 853 | base::Unretained(this))), |
| 854 | test_data_(CreateTestDataBuffer()), |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 855 | drainable_(new DrainableIOBuffer(test_data_.get(), kTestDataSize)) {} |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 856 | |
| 857 | int WaitForResult() { |
| 858 | DCHECK(!waiting_for_result_); |
| 859 | while (!have_result_) { |
| 860 | waiting_for_result_ = true; |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 861 | base::RunLoop().Run(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 862 | waiting_for_result_ = false; |
| 863 | } |
| 864 | have_result_ = false; // auto-reset for next callback |
| 865 | return result_; |
| 866 | } |
| 867 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 868 | const CompletionCallback& callback() const { return callback_; } |
| 869 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 870 | private: |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 871 | void OnComplete(int result) { |
| 872 | DCHECK_LT(0, result); |
| 873 | *total_bytes_written_ += result; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 874 | |
| 875 | int rv; |
| 876 | |
| 877 | if (*total_bytes_written_ != kTestDataSize) { |
| 878 | // Recurse to finish writing all data. |
| 879 | int total_bytes_written = 0, total_bytes_read = 0; |
| 880 | std::string data_read; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 881 | TestWriteReadCompletionCallback callback( |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 882 | stream_, &total_bytes_written, &total_bytes_read, &data_read); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 883 | rv = stream_->Write( |
| 884 | drainable_.get(), drainable_->BytesRemaining(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 885 | DCHECK_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 886 | rv = callback.WaitForResult(); |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 887 | drainable_->DidConsume(total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 888 | *total_bytes_written_ += total_bytes_written; |
| 889 | *total_bytes_read_ += total_bytes_read; |
| 890 | *data_read_ += data_read; |
| 891 | } else { // We're done writing all data. Start reading the data. |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 892 | stream_->SeekSync(FROM_BEGIN, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 893 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 894 | TestCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 895 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 896 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 897 | rv = stream_->Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 898 | if (rv == ERR_IO_PENDING) { |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 899 | base::MessageLoop::ScopedNestableTaskAllower allow( |
| 900 | base::MessageLoop::current()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 901 | rv = callback.WaitForResult(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 902 | } |
| 903 | EXPECT_LE(0, rv); |
| 904 | if (rv <= 0) |
| 905 | break; |
| 906 | *total_bytes_read_ += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 907 | data_read_->append(buf->data(), rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
| 911 | result_ = *total_bytes_written_; |
| 912 | have_result_ = true; |
| 913 | if (waiting_for_result_) |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 914 | base::MessageLoop::current()->Quit(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | int result_; |
| 918 | bool have_result_; |
| 919 | bool waiting_for_result_; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 920 | FileStream* stream_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 921 | int* total_bytes_written_; |
| 922 | int* total_bytes_read_; |
| 923 | std::string* data_read_; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 924 | const CompletionCallback callback_; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 925 | scoped_refptr<IOBufferWithSize> test_data_; |
| 926 | scoped_refptr<DrainableIOBuffer> drainable_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 927 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 928 | DISALLOW_COPY_AND_ASSIGN(TestWriteReadCompletionCallback); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 929 | }; |
| 930 | |
| 931 | TEST_F(FileStreamTest, AsyncWriteRead) { |
| 932 | int64 file_size; |
| 933 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 934 | EXPECT_TRUE(ok); |
| 935 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 936 | scoped_ptr<FileStream> stream( |
| 937 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 938 | int flags = base::PLATFORM_FILE_OPEN | |
| 939 | base::PLATFORM_FILE_READ | |
| 940 | base::PLATFORM_FILE_WRITE | |
| 941 | base::PLATFORM_FILE_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 942 | TestCompletionCallback open_callback; |
| 943 | int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
| 944 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 945 | EXPECT_EQ(OK, open_callback.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 946 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 947 | int64 total_bytes_avail = stream->Available(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 948 | EXPECT_EQ(file_size, total_bytes_avail); |
| 949 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 950 | int64 offset = stream->SeekSync(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 951 | EXPECT_EQ(offset, file_size); |
| 952 | |
| 953 | int total_bytes_written = 0; |
| 954 | int total_bytes_read = 0; |
| 955 | std::string data_read; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 956 | TestWriteReadCompletionCallback callback(stream.get(), &total_bytes_written, |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 957 | &total_bytes_read, &data_read); |
| 958 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 959 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 960 | rv = stream->Write(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 961 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 962 | rv = callback.WaitForResult(); |
| 963 | EXPECT_LT(0, rv); |
| 964 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 965 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 966 | stream.reset(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 967 | |
| 968 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 969 | EXPECT_TRUE(ok); |
| 970 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 971 | |
| 972 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 973 | const std::string kExpectedFileData = |
| 974 | std::string(kTestData) + std::string(kTestData); |
| 975 | EXPECT_EQ(kExpectedFileData, data_read); |
| 976 | } |
| 977 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 978 | class TestWriteCloseCompletionCallback { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 979 | public: |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 980 | TestWriteCloseCompletionCallback(FileStream* stream, int* total_bytes_written) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 981 | : result_(0), |
| 982 | have_result_(false), |
| 983 | waiting_for_result_(false), |
| 984 | stream_(stream), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 985 | total_bytes_written_(total_bytes_written), |
| 986 | callback_(base::Bind(&TestWriteCloseCompletionCallback::OnComplete, |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 987 | base::Unretained(this))), |
| 988 | test_data_(CreateTestDataBuffer()), |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 989 | drainable_(new DrainableIOBuffer(test_data_.get(), kTestDataSize)) {} |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 990 | |
| 991 | int WaitForResult() { |
| 992 | DCHECK(!waiting_for_result_); |
| 993 | while (!have_result_) { |
| 994 | waiting_for_result_ = true; |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 995 | base::RunLoop().Run(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 996 | waiting_for_result_ = false; |
| 997 | } |
| 998 | have_result_ = false; // auto-reset for next callback |
| 999 | return result_; |
| 1000 | } |
| 1001 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 1002 | const CompletionCallback& callback() const { return callback_; } |
| 1003 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1004 | private: |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 1005 | void OnComplete(int result) { |
| 1006 | DCHECK_LT(0, result); |
| 1007 | *total_bytes_written_ += result; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1008 | |
| 1009 | int rv; |
| 1010 | |
| 1011 | if (*total_bytes_written_ != kTestDataSize) { |
| 1012 | // Recurse to finish writing all data. |
| 1013 | int total_bytes_written = 0; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 1014 | TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1015 | rv = stream_->Write( |
| 1016 | drainable_.get(), drainable_->BytesRemaining(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 1017 | DCHECK_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1018 | rv = callback.WaitForResult(); |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 1019 | drainable_->DidConsume(total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1020 | *total_bytes_written_ += total_bytes_written; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | result_ = *total_bytes_written_; |
| 1024 | have_result_ = true; |
| 1025 | if (waiting_for_result_) |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1026 | base::MessageLoop::current()->Quit(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | int result_; |
| 1030 | bool have_result_; |
| 1031 | bool waiting_for_result_; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 1032 | FileStream* stream_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1033 | int* total_bytes_written_; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 1034 | const CompletionCallback callback_; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 1035 | scoped_refptr<IOBufferWithSize> test_data_; |
| 1036 | scoped_refptr<DrainableIOBuffer> drainable_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1037 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 1038 | DISALLOW_COPY_AND_ASSIGN(TestWriteCloseCompletionCallback); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1039 | }; |
| 1040 | |
| 1041 | TEST_F(FileStreamTest, AsyncWriteClose) { |
| 1042 | int64 file_size; |
| 1043 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 1044 | EXPECT_TRUE(ok); |
| 1045 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 1046 | scoped_ptr<FileStream> stream( |
| 1047 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1048 | int flags = base::PLATFORM_FILE_OPEN | |
| 1049 | base::PLATFORM_FILE_READ | |
| 1050 | base::PLATFORM_FILE_WRITE | |
| 1051 | base::PLATFORM_FILE_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 1052 | TestCompletionCallback open_callback; |
| 1053 | int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
| 1054 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1055 | EXPECT_EQ(OK, open_callback.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1056 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 1057 | int64 total_bytes_avail = stream->Available(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1058 | EXPECT_EQ(file_size, total_bytes_avail); |
| 1059 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 1060 | int64 offset = stream->SeekSync(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1061 | EXPECT_EQ(offset, file_size); |
| 1062 | |
| 1063 | int total_bytes_written = 0; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 1064 | TestWriteCloseCompletionCallback callback(stream.get(), &total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1065 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 1066 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1067 | rv = stream->Write(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 1068 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1069 | total_bytes_written = callback.WaitForResult(); |
| 1070 | EXPECT_LT(0, total_bytes_written); |
| 1071 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 1072 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 1073 | stream.reset(); |
| 1074 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1075 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 1076 | EXPECT_TRUE(ok); |
| 1077 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 1078 | } |
| 1079 | |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1080 | // Tests truncating a file. |
| 1081 | TEST_F(FileStreamTest, Truncate) { |
| 1082 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE; |
| 1083 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 1084 | scoped_ptr<FileStream> write_stream( |
| 1085 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 1086 | ASSERT_EQ(OK, write_stream->OpenSync(temp_file_path(), flags)); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1087 | |
| 1088 | // Write some data to the file. |
| 1089 | const char test_data[] = "0123456789"; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 1090 | write_stream->WriteSync(test_data, arraysize(test_data)); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1091 | |
| 1092 | // Truncate the file. |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 1093 | ASSERT_EQ(4, write_stream->Truncate(4)); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1094 | |
| 1095 | // Write again. |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 1096 | write_stream->WriteSync(test_data, 4); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1097 | |
| 1098 | // Close the stream. |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 1099 | write_stream.reset(); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1100 | |
| 1101 | // Read in the contents and make sure we get back what we expected. |
| 1102 | std::string read_contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 1103 | EXPECT_TRUE(base::ReadFileToString(temp_file_path(), &read_contents)); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1104 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1105 | EXPECT_EQ("01230123", read_contents); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 1106 | } |
| 1107 | |
[email protected] | dbb747c | 2012-02-29 19:29:47 | [diff] [blame] | 1108 | TEST_F(FileStreamTest, AsyncOpenAndDelete) { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 1109 | scoped_ptr<FileStream> stream( |
| 1110 | new FileStream(NULL, base::MessageLoopProxy::current())); |
[email protected] | dbb747c | 2012-02-29 19:29:47 | [diff] [blame] | 1111 | int flags = base::PLATFORM_FILE_OPEN | |
| 1112 | base::PLATFORM_FILE_WRITE | |
| 1113 | base::PLATFORM_FILE_ASYNC; |
| 1114 | TestCompletionCallback open_callback; |
| 1115 | int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
| 1116 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1117 | |
| 1118 | // Delete the stream without waiting for the open operation to be |
| 1119 | // complete. Should be safe. |
| 1120 | stream.reset(); |
| 1121 | // open_callback won't be called. |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 1122 | base::RunLoop().RunUntilIdle(); |
[email protected] | dbb747c | 2012-02-29 19:29:47 | [diff] [blame] | 1123 | EXPECT_FALSE(open_callback.have_result()); |
| 1124 | } |
| 1125 | |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 1126 | // Verify that async Write() errors are mapped correctly. |
| 1127 | TEST_F(FileStreamTest, AsyncWriteError) { |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 1128 | // Try opening file as read-only and then writing to it using FileStream. |
| 1129 | base::PlatformFile file = base::CreatePlatformFile( |
| 1130 | temp_file_path(), |
| 1131 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | |
| 1132 | base::PLATFORM_FILE_ASYNC, |
| 1133 | NULL, |
| 1134 | NULL); |
| 1135 | ASSERT_NE(base::kInvalidPlatformFileValue, file); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 1136 | |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 1137 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE | |
| 1138 | base::PLATFORM_FILE_ASYNC; |
| 1139 | scoped_ptr<FileStream> stream( |
| 1140 | new FileStream(file, flags, NULL, base::MessageLoopProxy::current())); |
| 1141 | |
| 1142 | scoped_refptr<IOBuffer> buf = new IOBuffer(1); |
[email protected] | 1a00d08 | 2013-09-14 19:57:16 | [diff] [blame] | 1143 | buf->data()[0] = 0; |
| 1144 | |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 1145 | TestCompletionCallback callback; |
| 1146 | int rv = stream->Write(buf.get(), 1, callback.callback()); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 1147 | if (rv == ERR_IO_PENDING) |
| 1148 | rv = callback.WaitForResult(); |
| 1149 | EXPECT_LT(rv, 0); |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 1150 | |
| 1151 | base::ClosePlatformFile(file); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | // Verify that async Read() errors are mapped correctly. |
| 1155 | TEST_F(FileStreamTest, AsyncReadError) { |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 1156 | // Try opening file for write and then reading from it using FileStream. |
| 1157 | base::PlatformFile file = base::CreatePlatformFile( |
| 1158 | temp_file_path(), |
| 1159 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | |
| 1160 | base::PLATFORM_FILE_ASYNC, |
| 1161 | NULL, |
| 1162 | NULL); |
| 1163 | ASSERT_NE(base::kInvalidPlatformFileValue, file); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 1164 | |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 1165 | int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | |
| 1166 | base::PLATFORM_FILE_ASYNC; |
| 1167 | scoped_ptr<FileStream> stream( |
| 1168 | new FileStream(file, flags, NULL, base::MessageLoopProxy::current())); |
| 1169 | |
| 1170 | scoped_refptr<IOBuffer> buf = new IOBuffer(1); |
| 1171 | TestCompletionCallback callback; |
| 1172 | int rv = stream->Read(buf.get(), 1, callback.callback()); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 1173 | if (rv == ERR_IO_PENDING) |
| 1174 | rv = callback.WaitForResult(); |
| 1175 | EXPECT_LT(rv, 0); |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 1176 | |
| 1177 | base::ClosePlatformFile(file); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 1178 | } |
| 1179 | |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 1180 | #if defined(OS_ANDROID) |
| 1181 | TEST_F(FileStreamTest, ContentUriAsyncRead) { |
| 1182 | base::FilePath test_dir; |
| 1183 | PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); |
| 1184 | test_dir = test_dir.AppendASCII("net"); |
| 1185 | test_dir = test_dir.AppendASCII("data"); |
| 1186 | test_dir = test_dir.AppendASCII("file_stream_unittest"); |
| 1187 | ASSERT_TRUE(base::PathExists(test_dir)); |
| 1188 | base::FilePath image_file = test_dir.Append(FILE_PATH_LITERAL("red.png")); |
| 1189 | |
| 1190 | // Insert the image into MediaStore. MediaStore will do some conversions, and |
| 1191 | // return the content URI. |
| 1192 | base::FilePath path = file_util::InsertImageIntoMediaStore(image_file); |
| 1193 | EXPECT_TRUE(path.IsContentUri()); |
| 1194 | EXPECT_TRUE(base::PathExists(path)); |
| 1195 | int64 file_size; |
| 1196 | EXPECT_TRUE(file_util::GetFileSize(path, &file_size)); |
| 1197 | EXPECT_LT(0, file_size); |
| 1198 | |
| 1199 | FileStream stream(NULL, base::MessageLoopProxy::current()); |
| 1200 | int flags = base::PLATFORM_FILE_OPEN | |
| 1201 | base::PLATFORM_FILE_READ | |
| 1202 | base::PLATFORM_FILE_ASYNC; |
| 1203 | TestCompletionCallback callback; |
| 1204 | int rv = stream.Open(path, flags, callback.callback()); |
| 1205 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1206 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 1207 | |
| 1208 | int64 total_bytes_avail = stream.Available(); |
| 1209 | EXPECT_EQ(file_size, total_bytes_avail); |
| 1210 | |
| 1211 | int total_bytes_read = 0; |
| 1212 | |
| 1213 | std::string data_read; |
| 1214 | for (;;) { |
| 1215 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 1216 | rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
| 1217 | if (rv == ERR_IO_PENDING) |
| 1218 | rv = callback.WaitForResult(); |
| 1219 | EXPECT_LE(0, rv); |
| 1220 | if (rv <= 0) |
| 1221 | break; |
| 1222 | total_bytes_read += rv; |
| 1223 | data_read.append(buf->data(), rv); |
| 1224 | } |
| 1225 | EXPECT_EQ(file_size, total_bytes_read); |
| 1226 | } |
| 1227 | #endif |
| 1228 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 1229 | } // namespace |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 1230 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 1231 | } // namespace net |