[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] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 9 | #include "base/files/file.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame^] | 10 | #include "base/files/file_util.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] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
[email protected] | e3d66fde | 2012-02-17 22:10:34 | [diff] [blame] | 16 | #include "base/synchronization/waitable_event.h" |
| 17 | #include "base/test/test_timeouts.h" |
[email protected] | e3ec0625d | 2014-05-02 23:58:54 | [diff] [blame] | 18 | #include "base/threading/sequenced_worker_pool.h" |
| 19 | #include "base/threading/thread_restrictions.h" |
[email protected] | e3d66fde | 2012-02-17 22:10:34 | [diff] [blame] | 20 | #include "net/base/capturing_net_log.h" |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 21 | #include "net/base/io_buffer.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 22 | #include "net/base/net_errors.h" |
| 23 | #include "net/base/test_completion_callback.h" |
| 24 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 25 | #include "testing/platform_test.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 26 | |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 27 | #if defined(OS_ANDROID) |
| 28 | #include "base/test/test_file_util.h" |
| 29 | #endif |
| 30 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 31 | namespace net { |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 32 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | const char kTestData[] = "0123456789"; |
| 36 | const int kTestDataSize = arraysize(kTestData) - 1; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 37 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 38 | // Creates an IOBufferWithSize that contains the kTestDataSize. |
| 39 | IOBufferWithSize* CreateTestDataBuffer() { |
| 40 | IOBufferWithSize* buf = new IOBufferWithSize(kTestDataSize); |
| 41 | memcpy(buf->data(), kTestData, kTestDataSize); |
| 42 | return buf; |
| 43 | } |
| 44 | |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 45 | } // namespace |
| 46 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 47 | class FileStreamTest : public PlatformTest { |
| 48 | public: |
| 49 | virtual void SetUp() { |
| 50 | PlatformTest::SetUp(); |
| 51 | |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 52 | base::CreateTemporaryFile(&temp_file_path_); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 53 | base::WriteFile(temp_file_path_, kTestData, kTestDataSize); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 54 | } |
| 55 | virtual void TearDown() { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 56 | // FileStreamContexts must be asynchronously closed on the file task runner |
| 57 | // before they can be deleted. Pump the RunLoop to avoid leaks. |
| 58 | base::RunLoop().RunUntilIdle(); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 59 | EXPECT_TRUE(base::DeleteFile(temp_file_path_, false)); |
| 60 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 61 | PlatformTest::TearDown(); |
| 62 | } |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 63 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 64 | const base::FilePath temp_file_path() const { return temp_file_path_; } |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 65 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 66 | private: |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 67 | base::FilePath temp_file_path_; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 68 | }; |
| 69 | |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 70 | namespace { |
| 71 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 72 | TEST_F(FileStreamTest, OpenExplicitClose) { |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 73 | TestCompletionCallback callback; |
[email protected] | 671e95fd | 2014-04-30 11:21:36 | [diff] [blame] | 74 | FileStream stream(base::MessageLoopProxy::current()); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 75 | int flags = base::File::FLAG_OPEN | |
| 76 | base::File::FLAG_READ | |
| 77 | base::File::FLAG_ASYNC; |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 78 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 79 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 80 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 81 | EXPECT_TRUE(stream.IsOpen()); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 82 | EXPECT_TRUE(stream.GetFileForTesting().IsValid()); |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 83 | EXPECT_EQ(ERR_IO_PENDING, stream.Close(callback.callback())); |
| 84 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 85 | EXPECT_FALSE(stream.IsOpen()); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 86 | EXPECT_FALSE(stream.GetFileForTesting().IsValid()); |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 89 | TEST_F(FileStreamTest, OpenExplicitCloseOrphaned) { |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 90 | TestCompletionCallback callback; |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 91 | scoped_ptr<FileStream> stream(new FileStream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 92 | base::MessageLoopProxy::current())); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 93 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 94 | base::File::FLAG_ASYNC; |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 95 | int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
| 96 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 97 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 98 | EXPECT_TRUE(stream->IsOpen()); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 99 | EXPECT_TRUE(stream->GetFileForTesting().IsValid()); |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 100 | EXPECT_EQ(ERR_IO_PENDING, stream->Close(callback.callback())); |
| 101 | stream.reset(); |
| 102 | // File isn't actually closed yet. |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 103 | base::RunLoop runloop; |
| 104 | runloop.RunUntilIdle(); |
| 105 | // The file should now be closed, though the callback has not been called. |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 108 | // Test the use of FileStream with a file handle provided at construction. |
| 109 | TEST_F(FileStreamTest, UseFileHandle) { |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 110 | int rv = 0; |
| 111 | TestCompletionCallback callback; |
| 112 | TestInt64CompletionCallback callback64; |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 113 | // 1. Test reading with a file handle. |
| 114 | ASSERT_EQ(kTestDataSize, |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 115 | base::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 116 | int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
| 117 | base::File::FLAG_ASYNC; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 118 | base::File file(temp_file_path(), flags); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 119 | |
| 120 | // Seek to the beginning of the file and read. |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 121 | scoped_ptr<FileStream> read_stream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 122 | new FileStream(file.Pass(), base::MessageLoopProxy::current())); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 123 | ASSERT_EQ(ERR_IO_PENDING, |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 124 | read_stream->Seek(base::File::FROM_BEGIN, 0, |
| 125 | callback64.callback())); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 126 | ASSERT_EQ(0, callback64.WaitForResult()); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 127 | // Read into buffer and compare. |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 128 | scoped_refptr<IOBufferWithSize> read_buffer = |
| 129 | new IOBufferWithSize(kTestDataSize); |
| 130 | rv = read_stream->Read(read_buffer.get(), kTestDataSize, callback.callback()); |
| 131 | ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); |
| 132 | ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 133 | read_stream.reset(); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 134 | |
| 135 | // 2. Test writing with a file handle. |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 136 | base::DeleteFile(temp_file_path(), false); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 137 | flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE | |
| 138 | base::File::FLAG_ASYNC; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 139 | file.Initialize(temp_file_path(), flags); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 140 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 141 | scoped_ptr<FileStream> write_stream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 142 | new FileStream(file.Pass(), base::MessageLoopProxy::current())); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 143 | ASSERT_EQ(ERR_IO_PENDING, |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 144 | write_stream->Seek(base::File::FROM_BEGIN, 0, |
| 145 | callback64.callback())); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 146 | ASSERT_EQ(0, callback64.WaitForResult()); |
| 147 | scoped_refptr<IOBufferWithSize> write_buffer = CreateTestDataBuffer(); |
| 148 | rv = write_stream->Write(write_buffer.get(), kTestDataSize, |
| 149 | callback.callback()); |
| 150 | ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 151 | write_stream.reset(); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 152 | |
| 153 | // Read into buffer and compare to make sure the handle worked fine. |
| 154 | ASSERT_EQ(kTestDataSize, |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 155 | base::ReadFile(temp_file_path(), read_buffer->data(), |
| 156 | kTestDataSize)); |
| 157 | ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 160 | TEST_F(FileStreamTest, UseClosedStream) { |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 161 | int rv = 0; |
| 162 | TestCompletionCallback callback; |
| 163 | TestInt64CompletionCallback callback64; |
| 164 | |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 165 | FileStream stream(base::MessageLoopProxy::current()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 166 | |
| 167 | EXPECT_FALSE(stream.IsOpen()); |
| 168 | |
| 169 | // Try seeking... |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 170 | rv = stream.Seek(base::File::FROM_BEGIN, 5, callback64.callback()); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 171 | EXPECT_EQ(ERR_UNEXPECTED, callback64.GetResult(rv)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 172 | |
| 173 | // Try reading... |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 174 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(10); |
dcheng | c80fed2a | 2014-08-27 21:47:36 | [diff] [blame] | 175 | rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 176 | EXPECT_EQ(ERR_UNEXPECTED, callback.GetResult(rv)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 179 | TEST_F(FileStreamTest, Read) { |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 180 | int64 file_size; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 181 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 182 | |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 183 | FileStream stream(base::MessageLoopProxy::current()); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 184 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 185 | base::File::FLAG_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 186 | TestCompletionCallback callback; |
| 187 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 188 | EXPECT_EQ(OK, callback.GetResult(rv)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 189 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 190 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 191 | |
| 192 | std::string data_read; |
| 193 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 194 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 195 | rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 196 | rv = callback.GetResult(rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 197 | EXPECT_LE(0, rv); |
| 198 | if (rv <= 0) |
| 199 | break; |
| 200 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 201 | data_read.append(buf->data(), rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 202 | } |
| 203 | EXPECT_EQ(file_size, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 204 | EXPECT_EQ(kTestData, data_read); |
| 205 | } |
| 206 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 207 | TEST_F(FileStreamTest, Read_EarlyDelete) { |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 208 | int64 file_size; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 209 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 210 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 211 | scoped_ptr<FileStream> stream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 212 | new FileStream(base::MessageLoopProxy::current())); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 213 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 214 | base::File::FLAG_ASYNC; |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 215 | TestCompletionCallback callback; |
| 216 | int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
| 217 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 218 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 219 | |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 220 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 221 | rv = stream->Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 222 | stream.reset(); // Delete instead of closing it. |
| 223 | if (rv < 0) { |
| 224 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 225 | // The callback should not be called if the request is cancelled. |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 226 | base::RunLoop().RunUntilIdle(); |
[email protected] | 06b802b | 2012-02-22 03:34:54 | [diff] [blame] | 227 | EXPECT_FALSE(callback.have_result()); |
| 228 | } else { |
| 229 | EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); |
| 230 | } |
| 231 | } |
| 232 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 233 | TEST_F(FileStreamTest, Read_FromOffset) { |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 234 | int64 file_size; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 235 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 236 | |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 237 | FileStream stream(base::MessageLoopProxy::current()); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 238 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 239 | base::File::FLAG_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 240 | TestCompletionCallback callback; |
| 241 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 242 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 243 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 244 | |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 245 | TestInt64CompletionCallback callback64; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 246 | const int64 kOffset = 3; |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 247 | rv = stream.Seek(base::File::FROM_BEGIN, kOffset, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 248 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 249 | int64 new_offset = callback64.WaitForResult(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 250 | EXPECT_EQ(kOffset, new_offset); |
| 251 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 252 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 253 | |
| 254 | std::string data_read; |
| 255 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 256 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 257 | rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 258 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 259 | rv = callback.WaitForResult(); |
| 260 | EXPECT_LE(0, rv); |
| 261 | if (rv <= 0) |
| 262 | break; |
| 263 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 264 | data_read.append(buf->data(), rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 265 | } |
| 266 | EXPECT_EQ(file_size - kOffset, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 267 | EXPECT_EQ(kTestData + kOffset, data_read); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 268 | } |
| 269 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 270 | TEST_F(FileStreamTest, SeekAround) { |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 271 | FileStream stream(base::MessageLoopProxy::current()); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 272 | int flags = base::File::FLAG_OPEN | base::File::FLAG_ASYNC | |
| 273 | base::File::FLAG_READ; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 274 | TestCompletionCallback callback; |
| 275 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 276 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 277 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 278 | |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 279 | TestInt64CompletionCallback callback64; |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 280 | |
| 281 | const int64 kOffset = 3; |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 282 | rv = stream.Seek(base::File::FROM_BEGIN, kOffset, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 283 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 284 | int64 new_offset = callback64.WaitForResult(); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 285 | EXPECT_EQ(kOffset, new_offset); |
| 286 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 287 | rv = stream.Seek(base::File::FROM_CURRENT, kOffset, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 288 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 289 | new_offset = callback64.WaitForResult(); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 290 | EXPECT_EQ(2 * kOffset, new_offset); |
| 291 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 292 | rv = stream.Seek(base::File::FROM_CURRENT, -kOffset, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 293 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 294 | new_offset = callback64.WaitForResult(); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 295 | EXPECT_EQ(kOffset, new_offset); |
| 296 | |
| 297 | const int kTestDataLen = arraysize(kTestData) - 1; |
| 298 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 299 | rv = stream.Seek(base::File::FROM_END, -kTestDataLen, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 300 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 301 | new_offset = callback64.WaitForResult(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 302 | EXPECT_EQ(0, new_offset); |
| 303 | } |
| 304 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 305 | TEST_F(FileStreamTest, Write) { |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 306 | FileStream stream(base::MessageLoopProxy::current()); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 307 | int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | |
| 308 | base::File::FLAG_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 309 | TestCompletionCallback callback; |
| 310 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 311 | EXPECT_EQ(OK, callback.GetResult(rv)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 312 | |
| 313 | int64 file_size; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 314 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 315 | EXPECT_EQ(0, file_size); |
| 316 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 317 | scoped_refptr<IOBuffer> buf = CreateTestDataBuffer(); |
| 318 | rv = stream.Write(buf.get(), kTestDataSize, callback.callback()); |
| 319 | rv = callback.GetResult(rv); |
| 320 | EXPECT_EQ(kTestDataSize, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 321 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 322 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 323 | EXPECT_EQ(kTestDataSize, file_size); |
| 324 | |
| 325 | std::string data_read; |
| 326 | EXPECT_TRUE(base::ReadFileToString(temp_file_path(), &data_read)); |
| 327 | EXPECT_EQ(kTestData, data_read); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 330 | TEST_F(FileStreamTest, Write_EarlyDelete) { |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 331 | scoped_ptr<FileStream> stream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 332 | new FileStream(base::MessageLoopProxy::current())); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 333 | int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | |
| 334 | base::File::FLAG_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 335 | TestCompletionCallback callback; |
| 336 | int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
| 337 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 338 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 339 | |
| 340 | int64 file_size; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 341 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 342 | EXPECT_EQ(0, file_size); |
| 343 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 344 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 345 | rv = stream->Write(buf.get(), buf->size(), callback.callback()); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 346 | stream.reset(); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 347 | if (rv < 0) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 348 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 349 | // The callback should not be called if the request is cancelled. |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 350 | base::RunLoop().RunUntilIdle(); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 351 | EXPECT_FALSE(callback.have_result()); |
| 352 | } else { |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 353 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 354 | EXPECT_EQ(file_size, rv); |
| 355 | } |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 358 | TEST_F(FileStreamTest, Write_FromOffset) { |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 359 | int64 file_size; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 360 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 361 | |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 362 | FileStream stream(base::MessageLoopProxy::current()); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 363 | int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | |
| 364 | base::File::FLAG_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 365 | TestCompletionCallback callback; |
| 366 | int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
| 367 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 368 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 369 | |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 370 | TestInt64CompletionCallback callback64; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 371 | const int64 kOffset = 0; |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 372 | rv = stream.Seek(base::File::FROM_END, kOffset, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 373 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 374 | int64 new_offset = callback64.WaitForResult(); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 375 | EXPECT_EQ(kTestDataSize, new_offset); |
| 376 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 377 | int total_bytes_written = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 378 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 379 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 380 | scoped_refptr<DrainableIOBuffer> drainable = |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 381 | new DrainableIOBuffer(buf.get(), buf->size()); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 382 | while (total_bytes_written != kTestDataSize) { |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 383 | rv = stream.Write(drainable.get(), drainable->BytesRemaining(), |
| 384 | callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 385 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 386 | rv = callback.WaitForResult(); |
| 387 | EXPECT_LT(0, rv); |
| 388 | if (rv <= 0) |
| 389 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 390 | drainable->DidConsume(rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 391 | total_bytes_written += rv; |
| 392 | } |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 393 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 394 | EXPECT_EQ(file_size, kTestDataSize * 2); |
| 395 | } |
| 396 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 397 | TEST_F(FileStreamTest, BasicReadWrite) { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 398 | int64 file_size; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 399 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 400 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 401 | scoped_ptr<FileStream> stream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 402 | new FileStream(base::MessageLoopProxy::current())); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 403 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 404 | base::File::FLAG_WRITE | base::File::FLAG_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 405 | TestCompletionCallback callback; |
| 406 | int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
| 407 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 408 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 409 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 410 | int64 total_bytes_read = 0; |
| 411 | |
| 412 | std::string data_read; |
| 413 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 414 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 415 | rv = stream->Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 416 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 417 | rv = callback.WaitForResult(); |
| 418 | EXPECT_LE(0, rv); |
| 419 | if (rv <= 0) |
| 420 | break; |
| 421 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 422 | data_read.append(buf->data(), rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 423 | } |
| 424 | EXPECT_EQ(file_size, total_bytes_read); |
| 425 | EXPECT_TRUE(data_read == kTestData); |
| 426 | |
| 427 | int total_bytes_written = 0; |
| 428 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 429 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 430 | scoped_refptr<DrainableIOBuffer> drainable = |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 431 | new DrainableIOBuffer(buf.get(), buf->size()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 432 | while (total_bytes_written != kTestDataSize) { |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 433 | rv = stream->Write(drainable.get(), drainable->BytesRemaining(), |
| 434 | callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 435 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 436 | rv = callback.WaitForResult(); |
| 437 | EXPECT_LT(0, rv); |
| 438 | if (rv <= 0) |
| 439 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 440 | drainable->DidConsume(rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 441 | total_bytes_written += rv; |
| 442 | } |
| 443 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 444 | stream.reset(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 445 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 446 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 447 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 448 | } |
| 449 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 450 | TEST_F(FileStreamTest, BasicWriteRead) { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 451 | int64 file_size; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 452 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 453 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 454 | scoped_ptr<FileStream> stream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 455 | new FileStream(base::MessageLoopProxy::current())); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 456 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 457 | base::File::FLAG_WRITE | base::File::FLAG_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 | |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 463 | TestInt64CompletionCallback callback64; |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 464 | rv = stream->Seek(base::File::FROM_END, 0, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 465 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 466 | int64 offset = callback64.WaitForResult(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 467 | EXPECT_EQ(offset, file_size); |
| 468 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 469 | int total_bytes_written = 0; |
| 470 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 471 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
| 472 | scoped_refptr<DrainableIOBuffer> drainable = |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 473 | new DrainableIOBuffer(buf.get(), buf->size()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 474 | while (total_bytes_written != kTestDataSize) { |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 475 | rv = stream->Write(drainable.get(), drainable->BytesRemaining(), |
| 476 | callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 477 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 478 | rv = callback.WaitForResult(); |
| 479 | EXPECT_LT(0, rv); |
| 480 | if (rv <= 0) |
| 481 | break; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 482 | drainable->DidConsume(rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 483 | total_bytes_written += rv; |
| 484 | } |
| 485 | |
| 486 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 487 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 488 | rv = stream->Seek(base::File::FROM_BEGIN, 0, callback64.callback()); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 489 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 490 | offset = callback64.WaitForResult(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 491 | EXPECT_EQ(0, offset); |
| 492 | |
| 493 | int total_bytes_read = 0; |
| 494 | |
| 495 | std::string data_read; |
| 496 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 497 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 498 | rv = stream->Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 499 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 500 | rv = callback.WaitForResult(); |
| 501 | EXPECT_LE(0, rv); |
| 502 | if (rv <= 0) |
| 503 | break; |
| 504 | total_bytes_read += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 505 | data_read.append(buf->data(), rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 506 | } |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 507 | stream.reset(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 508 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 509 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 510 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 511 | |
| 512 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 513 | const std::string kExpectedFileData = |
| 514 | std::string(kTestData) + std::string(kTestData); |
| 515 | EXPECT_EQ(kExpectedFileData, data_read); |
| 516 | } |
| 517 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 518 | class TestWriteReadCompletionCallback { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 519 | public: |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 520 | TestWriteReadCompletionCallback(FileStream* stream, |
| 521 | int* total_bytes_written, |
| 522 | int* total_bytes_read, |
| 523 | std::string* data_read) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 524 | : result_(0), |
| 525 | have_result_(false), |
| 526 | waiting_for_result_(false), |
| 527 | stream_(stream), |
| 528 | total_bytes_written_(total_bytes_written), |
| 529 | total_bytes_read_(total_bytes_read), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 530 | data_read_(data_read), |
| 531 | callback_(base::Bind(&TestWriteReadCompletionCallback::OnComplete, |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 532 | base::Unretained(this))), |
| 533 | test_data_(CreateTestDataBuffer()), |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 534 | drainable_(new DrainableIOBuffer(test_data_.get(), kTestDataSize)) {} |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 535 | |
| 536 | int WaitForResult() { |
| 537 | DCHECK(!waiting_for_result_); |
| 538 | while (!have_result_) { |
| 539 | waiting_for_result_ = true; |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 540 | base::RunLoop().Run(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 541 | waiting_for_result_ = false; |
| 542 | } |
| 543 | have_result_ = false; // auto-reset for next callback |
| 544 | return result_; |
| 545 | } |
| 546 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 547 | const CompletionCallback& callback() const { return callback_; } |
| 548 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 549 | private: |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 550 | void OnComplete(int result) { |
| 551 | DCHECK_LT(0, result); |
| 552 | *total_bytes_written_ += result; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 553 | |
| 554 | int rv; |
| 555 | |
| 556 | if (*total_bytes_written_ != kTestDataSize) { |
| 557 | // Recurse to finish writing all data. |
| 558 | int total_bytes_written = 0, total_bytes_read = 0; |
| 559 | std::string data_read; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 560 | TestWriteReadCompletionCallback callback( |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 561 | stream_, &total_bytes_written, &total_bytes_read, &data_read); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 562 | rv = stream_->Write( |
| 563 | drainable_.get(), drainable_->BytesRemaining(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 564 | DCHECK_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 565 | rv = callback.WaitForResult(); |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 566 | drainable_->DidConsume(total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 567 | *total_bytes_written_ += total_bytes_written; |
| 568 | *total_bytes_read_ += total_bytes_read; |
| 569 | *data_read_ += data_read; |
| 570 | } else { // We're done writing all data. Start reading the data. |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 571 | TestInt64CompletionCallback callback64; |
| 572 | EXPECT_EQ(ERR_IO_PENDING, |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 573 | stream_->Seek(base::File::FROM_BEGIN, 0, |
| 574 | callback64.callback())); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 575 | { |
| 576 | base::MessageLoop::ScopedNestableTaskAllower allow( |
| 577 | base::MessageLoop::current()); |
| 578 | EXPECT_LE(0, callback64.WaitForResult()); |
| 579 | } |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 580 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 581 | TestCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 582 | for (;;) { |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 583 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 584 | rv = stream_->Read(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 585 | if (rv == ERR_IO_PENDING) { |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 586 | base::MessageLoop::ScopedNestableTaskAllower allow( |
| 587 | base::MessageLoop::current()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 588 | rv = callback.WaitForResult(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 589 | } |
| 590 | EXPECT_LE(0, rv); |
| 591 | if (rv <= 0) |
| 592 | break; |
| 593 | *total_bytes_read_ += rv; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 594 | data_read_->append(buf->data(), rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | result_ = *total_bytes_written_; |
| 599 | have_result_ = true; |
| 600 | if (waiting_for_result_) |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 601 | base::MessageLoop::current()->Quit(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | int result_; |
| 605 | bool have_result_; |
| 606 | bool waiting_for_result_; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 607 | FileStream* stream_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 608 | int* total_bytes_written_; |
| 609 | int* total_bytes_read_; |
| 610 | std::string* data_read_; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 611 | const CompletionCallback callback_; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 612 | scoped_refptr<IOBufferWithSize> test_data_; |
| 613 | scoped_refptr<DrainableIOBuffer> drainable_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 614 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 615 | DISALLOW_COPY_AND_ASSIGN(TestWriteReadCompletionCallback); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 616 | }; |
| 617 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 618 | TEST_F(FileStreamTest, WriteRead) { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 619 | int64 file_size; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 620 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 621 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 622 | scoped_ptr<FileStream> stream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 623 | new FileStream(base::MessageLoopProxy::current())); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 624 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 625 | base::File::FLAG_WRITE | base::File::FLAG_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 626 | TestCompletionCallback open_callback; |
| 627 | int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
| 628 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 629 | EXPECT_EQ(OK, open_callback.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 630 | |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 631 | TestInt64CompletionCallback callback64; |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 632 | EXPECT_EQ(ERR_IO_PENDING, |
| 633 | stream->Seek(base::File::FROM_END, 0, callback64.callback())); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 634 | EXPECT_EQ(file_size, callback64.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 635 | |
| 636 | int total_bytes_written = 0; |
| 637 | int total_bytes_read = 0; |
| 638 | std::string data_read; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 639 | TestWriteReadCompletionCallback callback(stream.get(), &total_bytes_written, |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 640 | &total_bytes_read, &data_read); |
| 641 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 642 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 643 | rv = stream->Write(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 644 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 645 | rv = callback.WaitForResult(); |
| 646 | EXPECT_LT(0, rv); |
| 647 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 648 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 649 | stream.reset(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 650 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 651 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 652 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 653 | |
| 654 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 655 | const std::string kExpectedFileData = |
| 656 | std::string(kTestData) + std::string(kTestData); |
| 657 | EXPECT_EQ(kExpectedFileData, data_read); |
| 658 | } |
| 659 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 660 | class TestWriteCloseCompletionCallback { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 661 | public: |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 662 | TestWriteCloseCompletionCallback(FileStream* stream, int* total_bytes_written) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 663 | : result_(0), |
| 664 | have_result_(false), |
| 665 | waiting_for_result_(false), |
| 666 | stream_(stream), |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 667 | total_bytes_written_(total_bytes_written), |
| 668 | callback_(base::Bind(&TestWriteCloseCompletionCallback::OnComplete, |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 669 | base::Unretained(this))), |
| 670 | test_data_(CreateTestDataBuffer()), |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 671 | drainable_(new DrainableIOBuffer(test_data_.get(), kTestDataSize)) {} |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 672 | |
| 673 | int WaitForResult() { |
| 674 | DCHECK(!waiting_for_result_); |
| 675 | while (!have_result_) { |
| 676 | waiting_for_result_ = true; |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 677 | base::RunLoop().Run(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 678 | waiting_for_result_ = false; |
| 679 | } |
| 680 | have_result_ = false; // auto-reset for next callback |
| 681 | return result_; |
| 682 | } |
| 683 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 684 | const CompletionCallback& callback() const { return callback_; } |
| 685 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 686 | private: |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 687 | void OnComplete(int result) { |
| 688 | DCHECK_LT(0, result); |
| 689 | *total_bytes_written_ += result; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 690 | |
| 691 | int rv; |
| 692 | |
| 693 | if (*total_bytes_written_ != kTestDataSize) { |
| 694 | // Recurse to finish writing all data. |
| 695 | int total_bytes_written = 0; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 696 | TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 697 | rv = stream_->Write( |
| 698 | drainable_.get(), drainable_->BytesRemaining(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 699 | DCHECK_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 700 | rv = callback.WaitForResult(); |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 701 | drainable_->DidConsume(total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 702 | *total_bytes_written_ += total_bytes_written; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | result_ = *total_bytes_written_; |
| 706 | have_result_ = true; |
| 707 | if (waiting_for_result_) |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 708 | base::MessageLoop::current()->Quit(); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | int result_; |
| 712 | bool have_result_; |
| 713 | bool waiting_for_result_; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 714 | FileStream* stream_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 715 | int* total_bytes_written_; |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 716 | const CompletionCallback callback_; |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 717 | scoped_refptr<IOBufferWithSize> test_data_; |
| 718 | scoped_refptr<DrainableIOBuffer> drainable_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 719 | |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 720 | DISALLOW_COPY_AND_ASSIGN(TestWriteCloseCompletionCallback); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 721 | }; |
| 722 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 723 | TEST_F(FileStreamTest, WriteClose) { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 724 | int64 file_size; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 725 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 726 | |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 727 | scoped_ptr<FileStream> stream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 728 | new FileStream(base::MessageLoopProxy::current())); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 729 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 730 | base::File::FLAG_WRITE | base::File::FLAG_ASYNC; |
[email protected] | 66af1d6 | 2013-03-06 23:13:20 | [diff] [blame] | 731 | TestCompletionCallback open_callback; |
| 732 | int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
| 733 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 734 | EXPECT_EQ(OK, open_callback.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 735 | |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 736 | TestInt64CompletionCallback callback64; |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 737 | EXPECT_EQ(ERR_IO_PENDING, |
| 738 | stream->Seek(base::File::FROM_END, 0, callback64.callback())); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 739 | EXPECT_EQ(file_size, callback64.WaitForResult()); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 740 | |
| 741 | int total_bytes_written = 0; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 742 | TestWriteCloseCompletionCallback callback(stream.get(), &total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 743 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 744 | scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 745 | rv = stream->Write(buf.get(), buf->size(), callback.callback()); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 746 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 747 | total_bytes_written = callback.WaitForResult(); |
| 748 | EXPECT_LT(0, total_bytes_written); |
| 749 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 750 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 751 | stream.reset(); |
| 752 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 753 | EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 754 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 755 | } |
| 756 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 757 | TEST_F(FileStreamTest, OpenAndDelete) { |
[email protected] | e3ec0625d | 2014-05-02 23:58:54 | [diff] [blame] | 758 | scoped_refptr<base::SequencedWorkerPool> pool( |
| 759 | new base::SequencedWorkerPool(1, "StreamTest")); |
| 760 | |
| 761 | bool prev = base::ThreadRestrictions::SetIOAllowed(false); |
| 762 | scoped_ptr<FileStream> stream(new FileStream(pool.get())); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 763 | int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | |
| 764 | base::File::FLAG_ASYNC; |
[email protected] | dbb747c | 2012-02-29 19:29:47 | [diff] [blame] | 765 | TestCompletionCallback open_callback; |
| 766 | int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
| 767 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 768 | |
| 769 | // Delete the stream without waiting for the open operation to be |
| 770 | // complete. Should be safe. |
| 771 | stream.reset(); |
[email protected] | e3ec0625d | 2014-05-02 23:58:54 | [diff] [blame] | 772 | |
| 773 | // Force an operation through the pool. |
| 774 | scoped_ptr<FileStream> stream2(new FileStream(pool.get())); |
| 775 | TestCompletionCallback open_callback2; |
| 776 | rv = stream2->Open(temp_file_path(), flags, open_callback2.callback()); |
| 777 | EXPECT_EQ(OK, open_callback2.GetResult(rv)); |
| 778 | stream2.reset(); |
| 779 | |
| 780 | pool->Shutdown(); |
| 781 | |
[email protected] | dbb747c | 2012-02-29 19:29:47 | [diff] [blame] | 782 | // open_callback won't be called. |
[email protected] | dd2c438 | 2013-09-07 16:01:47 | [diff] [blame] | 783 | base::RunLoop().RunUntilIdle(); |
[email protected] | dbb747c | 2012-02-29 19:29:47 | [diff] [blame] | 784 | EXPECT_FALSE(open_callback.have_result()); |
[email protected] | e3ec0625d | 2014-05-02 23:58:54 | [diff] [blame] | 785 | base::ThreadRestrictions::SetIOAllowed(prev); |
[email protected] | dbb747c | 2012-02-29 19:29:47 | [diff] [blame] | 786 | } |
| 787 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 788 | // Verify that Write() errors are mapped correctly. |
| 789 | TEST_F(FileStreamTest, WriteError) { |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 790 | // Try opening file as read-only and then writing to it using FileStream. |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 791 | uint32 flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 792 | base::File::FLAG_ASYNC; |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 793 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 794 | base::File file(temp_file_path(), flags); |
| 795 | ASSERT_TRUE(file.IsValid()); |
| 796 | |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 797 | scoped_ptr<FileStream> stream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 798 | new FileStream(file.Pass(), base::MessageLoopProxy::current())); |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 799 | |
| 800 | scoped_refptr<IOBuffer> buf = new IOBuffer(1); |
[email protected] | 1a00d08 | 2013-09-14 19:57:16 | [diff] [blame] | 801 | buf->data()[0] = 0; |
| 802 | |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 803 | TestCompletionCallback callback; |
| 804 | int rv = stream->Write(buf.get(), 1, callback.callback()); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 805 | if (rv == ERR_IO_PENDING) |
| 806 | rv = callback.WaitForResult(); |
| 807 | EXPECT_LT(rv, 0); |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 808 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 809 | stream.reset(); |
| 810 | base::RunLoop().RunUntilIdle(); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 811 | } |
| 812 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 813 | // Verify that Read() errors are mapped correctly. |
| 814 | TEST_F(FileStreamTest, ReadError) { |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 815 | // Try opening file for write and then reading from it using FileStream. |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 816 | uint32 flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | |
| 817 | base::File::FLAG_ASYNC; |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 818 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 819 | base::File file(temp_file_path(), flags); |
| 820 | ASSERT_TRUE(file.IsValid()); |
| 821 | |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 822 | scoped_ptr<FileStream> stream( |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 823 | new FileStream(file.Pass(), base::MessageLoopProxy::current())); |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 824 | |
| 825 | scoped_refptr<IOBuffer> buf = new IOBuffer(1); |
| 826 | TestCompletionCallback callback; |
| 827 | int rv = stream->Read(buf.get(), 1, callback.callback()); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 828 | if (rv == ERR_IO_PENDING) |
| 829 | rv = callback.WaitForResult(); |
| 830 | EXPECT_LT(rv, 0); |
[email protected] | 7f00ad6 | 2013-09-14 00:56:21 | [diff] [blame] | 831 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 832 | stream.reset(); |
| 833 | base::RunLoop().RunUntilIdle(); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 834 | } |
| 835 | |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 836 | #if defined(OS_ANDROID) |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 837 | TEST_F(FileStreamTest, ContentUriRead) { |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 838 | base::FilePath test_dir; |
| 839 | PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); |
| 840 | test_dir = test_dir.AppendASCII("net"); |
| 841 | test_dir = test_dir.AppendASCII("data"); |
| 842 | test_dir = test_dir.AppendASCII("file_stream_unittest"); |
| 843 | ASSERT_TRUE(base::PathExists(test_dir)); |
| 844 | base::FilePath image_file = test_dir.Append(FILE_PATH_LITERAL("red.png")); |
| 845 | |
| 846 | // Insert the image into MediaStore. MediaStore will do some conversions, and |
| 847 | // return the content URI. |
[email protected] | 92be8eb | 2014-08-07 22:57:11 | [diff] [blame] | 848 | base::FilePath path = base::InsertImageIntoMediaStore(image_file); |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 849 | EXPECT_TRUE(path.IsContentUri()); |
| 850 | EXPECT_TRUE(base::PathExists(path)); |
| 851 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 852 | EXPECT_TRUE(base::GetFileSize(path, &file_size)); |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 853 | EXPECT_LT(0, file_size); |
| 854 | |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 855 | FileStream stream(base::MessageLoopProxy::current()); |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 856 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 857 | base::File::FLAG_ASYNC; |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 858 | TestCompletionCallback callback; |
| 859 | int rv = stream.Open(path, flags, callback.callback()); |
| 860 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 861 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 862 | |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 863 | int total_bytes_read = 0; |
| 864 | |
| 865 | std::string data_read; |
| 866 | for (;;) { |
| 867 | scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 868 | rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
| 869 | if (rv == ERR_IO_PENDING) |
| 870 | rv = callback.WaitForResult(); |
| 871 | EXPECT_LE(0, rv); |
| 872 | if (rv <= 0) |
| 873 | break; |
| 874 | total_bytes_read += rv; |
| 875 | data_read.append(buf->data(), rv); |
| 876 | } |
| 877 | EXPECT_EQ(file_size, total_bytes_read); |
| 878 | } |
| 879 | #endif |
| 880 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 881 | } // namespace |
[email protected] | 96d7382 | 2011-10-10 02:11:24 | [diff] [blame] | 882 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 883 | } // namespace net |