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