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