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