[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 5 | #include "base/callback.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 6 | #include "base/file_util.h" |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 7 | #include "base/message_loop.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 8 | #include "base/path_service.h" |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 9 | #include "base/platform_file.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 10 | #include "net/base/file_stream.h" |
| 11 | #include "net/base/net_errors.h" |
| 12 | #include "net/base/test_completion_callback.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 14 | #include "testing/platform_test.h" |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 15 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 16 | namespace net { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | const char kTestData[] = "0123456789"; |
| 20 | const int kTestDataSize = arraysize(kTestData) - 1; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 21 | |
| 22 | class FileStreamTest : public PlatformTest { |
| 23 | public: |
| 24 | virtual void SetUp() { |
| 25 | PlatformTest::SetUp(); |
| 26 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 27 | file_util::CreateTemporaryFile(&temp_file_path_); |
[email protected] | 7ff3f63 | 2009-10-13 18:43:35 | [diff] [blame] | 28 | file_util::WriteFile(temp_file_path_, kTestData, kTestDataSize); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 29 | } |
| 30 | virtual void TearDown() { |
| 31 | file_util::Delete(temp_file_path_, false); |
| 32 | |
| 33 | PlatformTest::TearDown(); |
| 34 | } |
[email protected] | 07167e8f | 2009-01-26 21:38:11 | [diff] [blame] | 35 | const FilePath temp_file_path() const { return temp_file_path_; } |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 36 | private: |
[email protected] | 07167e8f | 2009-01-26 21:38:11 | [diff] [blame] | 37 | FilePath temp_file_path_; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | TEST_F(FileStreamTest, BasicOpenClose) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 41 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 42 | int rv = stream.Open(temp_file_path(), |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 43 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 44 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 47 | // Test the use of FileStream with a file handle provided at construction. |
| 48 | TEST_F(FileStreamTest, UseFileHandle) { |
| 49 | bool created = false; |
| 50 | |
| 51 | // 1. Test reading with a file handle. |
| 52 | ASSERT_EQ(kTestDataSize, |
| 53 | file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
| 54 | int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; |
| 55 | base::PlatformFile file = base::CreatePlatformFile( |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 56 | temp_file_path(), flags, &created, NULL); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 57 | |
| 58 | // Seek to the beginning of the file and read. |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 59 | FileStream read_stream(file, flags); |
| 60 | ASSERT_EQ(0, read_stream.Seek(FROM_BEGIN, 0)); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 61 | ASSERT_EQ(kTestDataSize, read_stream.Available()); |
| 62 | // Read into buffer and compare. |
| 63 | char buffer[kTestDataSize]; |
| 64 | ASSERT_EQ(kTestDataSize, read_stream.Read(buffer, kTestDataSize, NULL)); |
| 65 | ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); |
| 66 | read_stream.Close(); |
| 67 | |
| 68 | // 2. Test writing with a file handle. |
| 69 | file_util::Delete(temp_file_path(), false); |
| 70 | flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE; |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 71 | file = base::CreatePlatformFile(temp_file_path(), flags, &created, NULL); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 72 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 73 | FileStream write_stream(file, flags); |
| 74 | ASSERT_EQ(0, write_stream.Seek(FROM_BEGIN, 0)); |
[email protected] | 92aad522 | 2009-02-09 22:26:41 | [diff] [blame] | 75 | ASSERT_EQ(kTestDataSize, write_stream.Write(kTestData, kTestDataSize, NULL)); |
| 76 | write_stream.Close(); |
| 77 | |
| 78 | // Read into buffer and compare to make sure the handle worked fine. |
| 79 | ASSERT_EQ(kTestDataSize, |
| 80 | file_util::ReadFile(temp_file_path(), buffer, kTestDataSize)); |
| 81 | ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); |
| 82 | } |
| 83 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 84 | TEST_F(FileStreamTest, UseClosedStream) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 85 | FileStream stream; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 86 | |
| 87 | EXPECT_FALSE(stream.IsOpen()); |
| 88 | |
| 89 | // Try seeking... |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 90 | int64 new_offset = stream.Seek(FROM_BEGIN, 5); |
| 91 | EXPECT_EQ(ERR_UNEXPECTED, new_offset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 92 | |
| 93 | // Try available... |
| 94 | int64 avail = stream.Available(); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 95 | EXPECT_EQ(ERR_UNEXPECTED, avail); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 96 | |
| 97 | // Try reading... |
| 98 | char buf[10]; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 99 | int rv = stream.Read(buf, arraysize(buf), NULL); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 100 | EXPECT_EQ(ERR_UNEXPECTED, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | TEST_F(FileStreamTest, BasicRead) { |
| 104 | int64 file_size; |
| 105 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 106 | EXPECT_TRUE(ok); |
| 107 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 108 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 109 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 110 | base::PLATFORM_FILE_READ; |
| 111 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 112 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 113 | |
| 114 | int64 total_bytes_avail = stream.Available(); |
| 115 | EXPECT_EQ(file_size, total_bytes_avail); |
| 116 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 117 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 118 | |
| 119 | std::string data_read; |
| 120 | for (;;) { |
| 121 | char buf[4]; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 122 | rv = stream.Read(buf, arraysize(buf), NULL); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 123 | EXPECT_LE(0, rv); |
| 124 | if (rv <= 0) |
| 125 | break; |
| 126 | total_bytes_read += rv; |
| 127 | data_read.append(buf, rv); |
| 128 | } |
| 129 | EXPECT_EQ(file_size, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 130 | EXPECT_EQ(kTestData, data_read); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST_F(FileStreamTest, AsyncRead) { |
| 134 | int64 file_size; |
| 135 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 136 | EXPECT_TRUE(ok); |
| 137 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 138 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 139 | int flags = base::PLATFORM_FILE_OPEN | |
| 140 | base::PLATFORM_FILE_READ | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 141 | base::PLATFORM_FILE_ASYNC; |
| 142 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 143 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 144 | |
| 145 | int64 total_bytes_avail = stream.Available(); |
| 146 | EXPECT_EQ(file_size, total_bytes_avail); |
| 147 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 148 | TestOldCompletionCallback callback; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 149 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 150 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 151 | |
| 152 | std::string data_read; |
| 153 | for (;;) { |
| 154 | char buf[4]; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 155 | rv = stream.Read(buf, arraysize(buf), &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 156 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 157 | rv = callback.WaitForResult(); |
| 158 | EXPECT_LE(0, rv); |
| 159 | if (rv <= 0) |
| 160 | break; |
| 161 | total_bytes_read += rv; |
| 162 | data_read.append(buf, rv); |
| 163 | } |
| 164 | EXPECT_EQ(file_size, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 165 | EXPECT_EQ(kTestData, data_read); |
| 166 | } |
| 167 | |
| 168 | TEST_F(FileStreamTest, AsyncRead_EarlyClose) { |
| 169 | int64 file_size; |
| 170 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 171 | EXPECT_TRUE(ok); |
| 172 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 173 | FileStream stream; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 174 | int flags = base::PLATFORM_FILE_OPEN | |
| 175 | base::PLATFORM_FILE_READ | |
| 176 | base::PLATFORM_FILE_ASYNC; |
| 177 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 178 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 179 | |
| 180 | int64 total_bytes_avail = stream.Available(); |
| 181 | EXPECT_EQ(file_size, total_bytes_avail); |
| 182 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 183 | TestOldCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 184 | |
| 185 | char buf[4]; |
| 186 | rv = stream.Read(buf, arraysize(buf), &callback); |
| 187 | stream.Close(); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 188 | if (rv < 0) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 189 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 190 | // The callback should not be called if the request is cancelled. |
| 191 | MessageLoop::current()->RunAllPending(); |
| 192 | EXPECT_FALSE(callback.have_result()); |
| 193 | } else { |
| 194 | EXPECT_EQ(std::string(kTestData, rv), std::string(buf, rv)); |
| 195 | } |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | TEST_F(FileStreamTest, BasicRead_FromOffset) { |
| 199 | int64 file_size; |
| 200 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 201 | EXPECT_TRUE(ok); |
| 202 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 203 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 204 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 205 | base::PLATFORM_FILE_READ; |
| 206 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 207 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 208 | |
| 209 | const int64 kOffset = 3; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 210 | int64 new_offset = stream.Seek(FROM_BEGIN, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 211 | EXPECT_EQ(kOffset, new_offset); |
| 212 | |
| 213 | int64 total_bytes_avail = stream.Available(); |
| 214 | EXPECT_EQ(file_size - kOffset, total_bytes_avail); |
| 215 | |
| 216 | int64 total_bytes_read = 0; |
| 217 | |
| 218 | std::string data_read; |
| 219 | for (;;) { |
| 220 | char buf[4]; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 221 | rv = stream.Read(buf, arraysize(buf), NULL); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 222 | EXPECT_LE(0, rv); |
| 223 | if (rv <= 0) |
| 224 | break; |
| 225 | total_bytes_read += rv; |
| 226 | data_read.append(buf, rv); |
| 227 | } |
| 228 | EXPECT_EQ(file_size - kOffset, total_bytes_read); |
| 229 | EXPECT_TRUE(data_read == kTestData + kOffset); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 230 | EXPECT_EQ(kTestData + kOffset, data_read); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | TEST_F(FileStreamTest, AsyncRead_FromOffset) { |
| 234 | int64 file_size; |
| 235 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 236 | EXPECT_TRUE(ok); |
| 237 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 238 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 239 | int flags = base::PLATFORM_FILE_OPEN | |
| 240 | base::PLATFORM_FILE_READ | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 241 | base::PLATFORM_FILE_ASYNC; |
| 242 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 243 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 244 | |
| 245 | const int64 kOffset = 3; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 246 | int64 new_offset = stream.Seek(FROM_BEGIN, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 247 | EXPECT_EQ(kOffset, new_offset); |
| 248 | |
| 249 | int64 total_bytes_avail = stream.Available(); |
| 250 | EXPECT_EQ(file_size - kOffset, total_bytes_avail); |
| 251 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 252 | TestOldCompletionCallback callback; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 253 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 254 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 255 | |
| 256 | std::string data_read; |
| 257 | for (;;) { |
| 258 | char buf[4]; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 259 | rv = stream.Read(buf, arraysize(buf), &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 260 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 261 | rv = callback.WaitForResult(); |
| 262 | EXPECT_LE(0, rv); |
| 263 | if (rv <= 0) |
| 264 | break; |
| 265 | total_bytes_read += rv; |
| 266 | data_read.append(buf, rv); |
| 267 | } |
| 268 | EXPECT_EQ(file_size - kOffset, total_bytes_read); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 269 | EXPECT_EQ(kTestData + kOffset, data_read); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | TEST_F(FileStreamTest, SeekAround) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 273 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 274 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 275 | base::PLATFORM_FILE_READ; |
| 276 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 277 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 278 | |
| 279 | const int64 kOffset = 3; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 280 | int64 new_offset = stream.Seek(FROM_BEGIN, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 281 | EXPECT_EQ(kOffset, new_offset); |
| 282 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 283 | new_offset = stream.Seek(FROM_CURRENT, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 284 | EXPECT_EQ(2 * kOffset, new_offset); |
| 285 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 286 | new_offset = stream.Seek(FROM_CURRENT, -kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 287 | EXPECT_EQ(kOffset, new_offset); |
| 288 | |
| 289 | const int kTestDataLen = arraysize(kTestData) - 1; |
| 290 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 291 | new_offset = stream.Seek(FROM_END, -kTestDataLen); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 292 | EXPECT_EQ(0, new_offset); |
| 293 | } |
| 294 | |
| 295 | TEST_F(FileStreamTest, BasicWrite) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 296 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 297 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 298 | base::PLATFORM_FILE_WRITE; |
| 299 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 300 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 301 | |
| 302 | int64 file_size; |
| 303 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 304 | EXPECT_TRUE(ok); |
| 305 | EXPECT_EQ(0, file_size); |
| 306 | |
| 307 | rv = stream.Write(kTestData, kTestDataSize, NULL); |
| 308 | EXPECT_EQ(kTestDataSize, rv); |
| 309 | stream.Close(); |
| 310 | |
| 311 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 312 | EXPECT_TRUE(ok); |
| 313 | EXPECT_EQ(kTestDataSize, file_size); |
| 314 | } |
| 315 | |
| 316 | TEST_F(FileStreamTest, AsyncWrite) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 317 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 318 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
| 319 | base::PLATFORM_FILE_WRITE | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 320 | base::PLATFORM_FILE_ASYNC; |
| 321 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 322 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 323 | |
| 324 | int64 file_size; |
| 325 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 326 | EXPECT_TRUE(ok); |
| 327 | EXPECT_EQ(0, file_size); |
| 328 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 329 | TestOldCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 330 | int total_bytes_written = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 331 | |
| 332 | while (total_bytes_written != kTestDataSize) { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 333 | rv = stream.Write(kTestData + total_bytes_written, |
| 334 | kTestDataSize - total_bytes_written, |
| 335 | &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 336 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 337 | rv = callback.WaitForResult(); |
| 338 | EXPECT_LT(0, rv); |
| 339 | if (rv <= 0) |
| 340 | break; |
| 341 | total_bytes_written += rv; |
| 342 | } |
| 343 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 344 | EXPECT_TRUE(ok); |
| 345 | EXPECT_EQ(file_size, total_bytes_written); |
| 346 | } |
| 347 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 348 | TEST_F(FileStreamTest, AsyncWrite_EarlyClose) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 349 | FileStream stream; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 350 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
| 351 | base::PLATFORM_FILE_WRITE | |
| 352 | base::PLATFORM_FILE_ASYNC; |
| 353 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 354 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 355 | |
| 356 | int64 file_size; |
| 357 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 358 | EXPECT_TRUE(ok); |
| 359 | EXPECT_EQ(0, file_size); |
| 360 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 361 | TestOldCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 362 | int total_bytes_written = 0; |
| 363 | |
| 364 | rv = stream.Write(kTestData + total_bytes_written, |
| 365 | kTestDataSize - total_bytes_written, |
| 366 | &callback); |
| 367 | stream.Close(); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 368 | if (rv < 0) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 369 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3828a75 | 2009-06-03 23:05:59 | [diff] [blame] | 370 | // The callback should not be called if the request is cancelled. |
| 371 | MessageLoop::current()->RunAllPending(); |
| 372 | EXPECT_FALSE(callback.have_result()); |
| 373 | } else { |
| 374 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 375 | EXPECT_TRUE(ok); |
| 376 | EXPECT_EQ(file_size, rv); |
| 377 | } |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 380 | TEST_F(FileStreamTest, BasicWrite_FromOffset) { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 381 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 382 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 383 | base::PLATFORM_FILE_WRITE; |
| 384 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 385 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 386 | |
| 387 | int64 file_size; |
| 388 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 389 | EXPECT_TRUE(ok); |
| 390 | EXPECT_EQ(kTestDataSize, file_size); |
| 391 | |
| 392 | const int64 kOffset = 0; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 393 | int64 new_offset = stream.Seek(FROM_END, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 394 | EXPECT_EQ(kTestDataSize, new_offset); |
| 395 | |
| 396 | rv = stream.Write(kTestData, kTestDataSize, NULL); |
| 397 | EXPECT_EQ(kTestDataSize, rv); |
| 398 | stream.Close(); |
| 399 | |
| 400 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 401 | EXPECT_TRUE(ok); |
| 402 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 403 | } |
| 404 | |
| 405 | TEST_F(FileStreamTest, AsyncWrite_FromOffset) { |
| 406 | int64 file_size; |
| 407 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 408 | EXPECT_TRUE(ok); |
| 409 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 410 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 411 | int flags = base::PLATFORM_FILE_OPEN | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 412 | base::PLATFORM_FILE_WRITE | |
| 413 | base::PLATFORM_FILE_ASYNC; |
| 414 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 415 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 416 | |
| 417 | const int64 kOffset = 0; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 418 | int64 new_offset = stream.Seek(FROM_END, kOffset); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 419 | EXPECT_EQ(kTestDataSize, new_offset); |
| 420 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 421 | TestOldCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 422 | int total_bytes_written = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 423 | |
| 424 | while (total_bytes_written != kTestDataSize) { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 425 | rv = stream.Write(kTestData + total_bytes_written, |
| 426 | kTestDataSize - total_bytes_written, |
| 427 | &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 428 | if (rv == ERR_IO_PENDING) |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 429 | rv = callback.WaitForResult(); |
| 430 | EXPECT_LT(0, rv); |
| 431 | if (rv <= 0) |
| 432 | break; |
| 433 | total_bytes_written += rv; |
| 434 | } |
| 435 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 436 | EXPECT_TRUE(ok); |
| 437 | EXPECT_EQ(file_size, kTestDataSize * 2); |
| 438 | } |
| 439 | |
| 440 | TEST_F(FileStreamTest, BasicReadWrite) { |
| 441 | int64 file_size; |
| 442 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 443 | EXPECT_TRUE(ok); |
| 444 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 445 | FileStream stream; |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 446 | int flags = base::PLATFORM_FILE_OPEN | |
| 447 | base::PLATFORM_FILE_READ | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 448 | base::PLATFORM_FILE_WRITE; |
| 449 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 450 | EXPECT_EQ(OK, rv); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 451 | |
| 452 | int64 total_bytes_avail = stream.Available(); |
| 453 | EXPECT_EQ(file_size, total_bytes_avail); |
| 454 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 455 | int total_bytes_read = 0; |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 456 | |
| 457 | std::string data_read; |
| 458 | for (;;) { |
| 459 | char buf[4]; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 460 | rv = stream.Read(buf, arraysize(buf), NULL); |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 461 | EXPECT_LE(0, rv); |
| 462 | if (rv <= 0) |
| 463 | break; |
| 464 | total_bytes_read += rv; |
| 465 | data_read.append(buf, rv); |
| 466 | } |
| 467 | EXPECT_EQ(file_size, total_bytes_read); |
| 468 | EXPECT_TRUE(data_read == kTestData); |
| 469 | |
| 470 | rv = stream.Write(kTestData, kTestDataSize, NULL); |
| 471 | EXPECT_EQ(kTestDataSize, rv); |
| 472 | stream.Close(); |
| 473 | |
| 474 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 475 | EXPECT_TRUE(ok); |
| 476 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 477 | } |
| 478 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 479 | TEST_F(FileStreamTest, BasicWriteRead) { |
| 480 | int64 file_size; |
| 481 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 482 | EXPECT_TRUE(ok); |
| 483 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 484 | FileStream stream; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 485 | int flags = base::PLATFORM_FILE_OPEN | |
| 486 | base::PLATFORM_FILE_READ | |
| 487 | base::PLATFORM_FILE_WRITE; |
| 488 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 489 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 490 | |
| 491 | int64 total_bytes_avail = stream.Available(); |
| 492 | EXPECT_EQ(file_size, total_bytes_avail); |
| 493 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 494 | int64 offset = stream.Seek(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 495 | EXPECT_EQ(offset, file_size); |
| 496 | |
| 497 | rv = stream.Write(kTestData, kTestDataSize, NULL); |
| 498 | EXPECT_EQ(kTestDataSize, rv); |
| 499 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 500 | offset = stream.Seek(FROM_BEGIN, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 501 | EXPECT_EQ(0, offset); |
| 502 | |
| 503 | int64 total_bytes_read = 0; |
| 504 | |
| 505 | std::string data_read; |
| 506 | for (;;) { |
| 507 | char buf[4]; |
| 508 | rv = stream.Read(buf, arraysize(buf), NULL); |
| 509 | EXPECT_LE(0, rv); |
| 510 | if (rv <= 0) |
| 511 | break; |
| 512 | total_bytes_read += rv; |
| 513 | data_read.append(buf, rv); |
| 514 | } |
| 515 | stream.Close(); |
| 516 | |
| 517 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 518 | EXPECT_TRUE(ok); |
| 519 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 520 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 521 | |
| 522 | const std::string kExpectedFileData = |
| 523 | std::string(kTestData) + std::string(kTestData); |
| 524 | EXPECT_EQ(kExpectedFileData, data_read); |
| 525 | } |
| 526 | |
| 527 | TEST_F(FileStreamTest, BasicAsyncReadWrite) { |
| 528 | int64 file_size; |
| 529 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 530 | EXPECT_TRUE(ok); |
| 531 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 532 | FileStream stream; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 533 | int flags = base::PLATFORM_FILE_OPEN | |
| 534 | base::PLATFORM_FILE_READ | |
| 535 | base::PLATFORM_FILE_WRITE | |
| 536 | base::PLATFORM_FILE_ASYNC; |
| 537 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 538 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 539 | |
| 540 | int64 total_bytes_avail = stream.Available(); |
| 541 | EXPECT_EQ(file_size, total_bytes_avail); |
| 542 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 543 | TestOldCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 544 | int64 total_bytes_read = 0; |
| 545 | |
| 546 | std::string data_read; |
| 547 | for (;;) { |
| 548 | char buf[4]; |
| 549 | rv = stream.Read(buf, arraysize(buf), &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 550 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 551 | rv = callback.WaitForResult(); |
| 552 | EXPECT_LE(0, rv); |
| 553 | if (rv <= 0) |
| 554 | break; |
| 555 | total_bytes_read += rv; |
| 556 | data_read.append(buf, rv); |
| 557 | } |
| 558 | EXPECT_EQ(file_size, total_bytes_read); |
| 559 | EXPECT_TRUE(data_read == kTestData); |
| 560 | |
| 561 | int total_bytes_written = 0; |
| 562 | |
| 563 | while (total_bytes_written != kTestDataSize) { |
| 564 | rv = stream.Write(kTestData + total_bytes_written, |
| 565 | kTestDataSize - total_bytes_written, |
| 566 | &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 567 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 568 | rv = callback.WaitForResult(); |
| 569 | EXPECT_LT(0, rv); |
| 570 | if (rv <= 0) |
| 571 | break; |
| 572 | total_bytes_written += rv; |
| 573 | } |
| 574 | |
| 575 | stream.Close(); |
| 576 | |
| 577 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 578 | EXPECT_TRUE(ok); |
| 579 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 580 | } |
| 581 | |
| 582 | TEST_F(FileStreamTest, BasicAsyncWriteRead) { |
| 583 | int64 file_size; |
| 584 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 585 | EXPECT_TRUE(ok); |
| 586 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 587 | FileStream stream; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 588 | int flags = base::PLATFORM_FILE_OPEN | |
| 589 | base::PLATFORM_FILE_READ | |
| 590 | base::PLATFORM_FILE_WRITE | |
| 591 | base::PLATFORM_FILE_ASYNC; |
| 592 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 593 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 594 | |
| 595 | int64 total_bytes_avail = stream.Available(); |
| 596 | EXPECT_EQ(file_size, total_bytes_avail); |
| 597 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 598 | int64 offset = stream.Seek(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 599 | EXPECT_EQ(offset, file_size); |
| 600 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 601 | TestOldCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 602 | int total_bytes_written = 0; |
| 603 | |
| 604 | while (total_bytes_written != kTestDataSize) { |
| 605 | rv = stream.Write(kTestData + total_bytes_written, |
| 606 | kTestDataSize - total_bytes_written, |
| 607 | &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 608 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 609 | rv = callback.WaitForResult(); |
| 610 | EXPECT_LT(0, rv); |
| 611 | if (rv <= 0) |
| 612 | break; |
| 613 | total_bytes_written += rv; |
| 614 | } |
| 615 | |
| 616 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 617 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 618 | offset = stream.Seek(FROM_BEGIN, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 619 | EXPECT_EQ(0, offset); |
| 620 | |
| 621 | int total_bytes_read = 0; |
| 622 | |
| 623 | std::string data_read; |
| 624 | for (;;) { |
| 625 | char buf[4]; |
| 626 | rv = stream.Read(buf, arraysize(buf), &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 627 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 628 | rv = callback.WaitForResult(); |
| 629 | EXPECT_LE(0, rv); |
| 630 | if (rv <= 0) |
| 631 | break; |
| 632 | total_bytes_read += rv; |
| 633 | data_read.append(buf, rv); |
| 634 | } |
| 635 | stream.Close(); |
| 636 | |
| 637 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 638 | EXPECT_TRUE(ok); |
| 639 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 640 | |
| 641 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 642 | const std::string kExpectedFileData = |
| 643 | std::string(kTestData) + std::string(kTestData); |
| 644 | EXPECT_EQ(kExpectedFileData, data_read); |
| 645 | } |
| 646 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 647 | class TestWriteReadOldCompletionCallback : public Callback1<int>::Type { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 648 | public: |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 649 | TestWriteReadOldCompletionCallback( |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 650 | FileStream* stream, |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 651 | int* total_bytes_written, |
| 652 | int* total_bytes_read, |
| 653 | std::string* data_read) |
| 654 | : result_(0), |
| 655 | have_result_(false), |
| 656 | waiting_for_result_(false), |
| 657 | stream_(stream), |
| 658 | total_bytes_written_(total_bytes_written), |
| 659 | total_bytes_read_(total_bytes_read), |
| 660 | data_read_(data_read) {} |
| 661 | |
| 662 | int WaitForResult() { |
| 663 | DCHECK(!waiting_for_result_); |
| 664 | while (!have_result_) { |
| 665 | waiting_for_result_ = true; |
| 666 | MessageLoop::current()->Run(); |
| 667 | waiting_for_result_ = false; |
| 668 | } |
| 669 | have_result_ = false; // auto-reset for next callback |
| 670 | return result_; |
| 671 | } |
| 672 | |
| 673 | private: |
| 674 | virtual void RunWithParams(const Tuple1<int>& params) { |
| 675 | DCHECK_LT(0, params.a); |
| 676 | *total_bytes_written_ += params.a; |
| 677 | |
| 678 | int rv; |
| 679 | |
| 680 | if (*total_bytes_written_ != kTestDataSize) { |
| 681 | // Recurse to finish writing all data. |
| 682 | int total_bytes_written = 0, total_bytes_read = 0; |
| 683 | std::string data_read; |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 684 | TestWriteReadOldCompletionCallback callback( |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 685 | stream_, &total_bytes_written, &total_bytes_read, &data_read); |
| 686 | rv = stream_->Write(kTestData + *total_bytes_written_, |
| 687 | kTestDataSize - *total_bytes_written_, |
| 688 | &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 689 | DCHECK_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 690 | rv = callback.WaitForResult(); |
| 691 | *total_bytes_written_ += total_bytes_written; |
| 692 | *total_bytes_read_ += total_bytes_read; |
| 693 | *data_read_ += data_read; |
| 694 | } else { // We're done writing all data. Start reading the data. |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 695 | stream_->Seek(FROM_BEGIN, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 696 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 697 | TestOldCompletionCallback callback; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 698 | for (;;) { |
| 699 | char buf[4]; |
| 700 | rv = stream_->Read(buf, arraysize(buf), &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 701 | if (rv == ERR_IO_PENDING) { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 702 | bool old_state = MessageLoop::current()->NestableTasksAllowed(); |
| 703 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 704 | rv = callback.WaitForResult(); |
| 705 | MessageLoop::current()->SetNestableTasksAllowed(old_state); |
| 706 | } |
| 707 | EXPECT_LE(0, rv); |
| 708 | if (rv <= 0) |
| 709 | break; |
| 710 | *total_bytes_read_ += rv; |
| 711 | data_read_->append(buf, rv); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | result_ = *total_bytes_written_; |
| 716 | have_result_ = true; |
| 717 | if (waiting_for_result_) |
| 718 | MessageLoop::current()->Quit(); |
| 719 | } |
| 720 | |
| 721 | int result_; |
| 722 | bool have_result_; |
| 723 | bool waiting_for_result_; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 724 | FileStream* stream_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 725 | int* total_bytes_written_; |
| 726 | int* total_bytes_read_; |
| 727 | std::string* data_read_; |
| 728 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 729 | DISALLOW_COPY_AND_ASSIGN(TestWriteReadOldCompletionCallback); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 730 | }; |
| 731 | |
| 732 | TEST_F(FileStreamTest, AsyncWriteRead) { |
| 733 | int64 file_size; |
| 734 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 735 | EXPECT_TRUE(ok); |
| 736 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 737 | FileStream stream; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 738 | int flags = base::PLATFORM_FILE_OPEN | |
| 739 | base::PLATFORM_FILE_READ | |
| 740 | base::PLATFORM_FILE_WRITE | |
| 741 | base::PLATFORM_FILE_ASYNC; |
| 742 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 743 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 744 | |
| 745 | int64 total_bytes_avail = stream.Available(); |
| 746 | EXPECT_EQ(file_size, total_bytes_avail); |
| 747 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 748 | int64 offset = stream.Seek(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 749 | EXPECT_EQ(offset, file_size); |
| 750 | |
| 751 | int total_bytes_written = 0; |
| 752 | int total_bytes_read = 0; |
| 753 | std::string data_read; |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 754 | TestWriteReadOldCompletionCallback callback(&stream, &total_bytes_written, |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 755 | &total_bytes_read, &data_read); |
| 756 | |
| 757 | rv = stream.Write(kTestData + total_bytes_written, |
| 758 | kTestDataSize - static_cast<int>(total_bytes_written), |
| 759 | &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 760 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 761 | rv = callback.WaitForResult(); |
| 762 | EXPECT_LT(0, rv); |
| 763 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 764 | |
| 765 | stream.Close(); |
| 766 | |
| 767 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 768 | EXPECT_TRUE(ok); |
| 769 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 770 | |
| 771 | EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
| 772 | const std::string kExpectedFileData = |
| 773 | std::string(kTestData) + std::string(kTestData); |
| 774 | EXPECT_EQ(kExpectedFileData, data_read); |
| 775 | } |
| 776 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 777 | class TestWriteCloseOldCompletionCallback : public Callback1<int>::Type { |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 778 | public: |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 779 | TestWriteCloseOldCompletionCallback(FileStream* stream, int* total_bytes_written) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 780 | : result_(0), |
| 781 | have_result_(false), |
| 782 | waiting_for_result_(false), |
| 783 | stream_(stream), |
| 784 | total_bytes_written_(total_bytes_written) {} |
| 785 | |
| 786 | int WaitForResult() { |
| 787 | DCHECK(!waiting_for_result_); |
| 788 | while (!have_result_) { |
| 789 | waiting_for_result_ = true; |
| 790 | MessageLoop::current()->Run(); |
| 791 | waiting_for_result_ = false; |
| 792 | } |
| 793 | have_result_ = false; // auto-reset for next callback |
| 794 | return result_; |
| 795 | } |
| 796 | |
| 797 | private: |
| 798 | virtual void RunWithParams(const Tuple1<int>& params) { |
| 799 | DCHECK_LT(0, params.a); |
| 800 | *total_bytes_written_ += params.a; |
| 801 | |
| 802 | int rv; |
| 803 | |
| 804 | if (*total_bytes_written_ != kTestDataSize) { |
| 805 | // Recurse to finish writing all data. |
| 806 | int total_bytes_written = 0; |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 807 | TestWriteCloseOldCompletionCallback callback(stream_, &total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 808 | rv = stream_->Write(kTestData + *total_bytes_written_, |
| 809 | kTestDataSize - *total_bytes_written_, |
| 810 | &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 811 | DCHECK_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 812 | rv = callback.WaitForResult(); |
| 813 | *total_bytes_written_ += total_bytes_written; |
| 814 | } else { // We're done writing all data. Close the file. |
| 815 | stream_->Close(); |
| 816 | } |
| 817 | |
| 818 | result_ = *total_bytes_written_; |
| 819 | have_result_ = true; |
| 820 | if (waiting_for_result_) |
| 821 | MessageLoop::current()->Quit(); |
| 822 | } |
| 823 | |
| 824 | int result_; |
| 825 | bool have_result_; |
| 826 | bool waiting_for_result_; |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 827 | FileStream* stream_; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 828 | int* total_bytes_written_; |
| 829 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 830 | DISALLOW_COPY_AND_ASSIGN(TestWriteCloseOldCompletionCallback); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 831 | }; |
| 832 | |
| 833 | TEST_F(FileStreamTest, AsyncWriteClose) { |
| 834 | int64 file_size; |
| 835 | bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 836 | EXPECT_TRUE(ok); |
| 837 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 838 | FileStream stream; |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 839 | int flags = base::PLATFORM_FILE_OPEN | |
| 840 | base::PLATFORM_FILE_READ | |
| 841 | base::PLATFORM_FILE_WRITE | |
| 842 | base::PLATFORM_FILE_ASYNC; |
| 843 | int rv = stream.Open(temp_file_path(), flags); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 844 | EXPECT_EQ(OK, rv); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 845 | |
| 846 | int64 total_bytes_avail = stream.Available(); |
| 847 | EXPECT_EQ(file_size, total_bytes_avail); |
| 848 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 849 | int64 offset = stream.Seek(FROM_END, 0); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 850 | EXPECT_EQ(offset, file_size); |
| 851 | |
| 852 | int total_bytes_written = 0; |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 853 | TestWriteCloseOldCompletionCallback callback(&stream, &total_bytes_written); |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 854 | |
| 855 | rv = stream.Write(kTestData, kTestDataSize, &callback); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 856 | if (rv == ERR_IO_PENDING) |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 857 | total_bytes_written = callback.WaitForResult(); |
| 858 | EXPECT_LT(0, total_bytes_written); |
| 859 | EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 860 | |
| 861 | ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 862 | EXPECT_TRUE(ok); |
| 863 | EXPECT_EQ(kTestDataSize * 2, file_size); |
| 864 | } |
| 865 | |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 866 | // Tests truncating a file. |
| 867 | TEST_F(FileStreamTest, Truncate) { |
| 868 | int flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE; |
| 869 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 870 | FileStream write_stream; |
| 871 | ASSERT_EQ(OK, write_stream.Open(temp_file_path(), flags)); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 872 | |
| 873 | // Write some data to the file. |
| 874 | const char test_data[] = "0123456789"; |
| 875 | write_stream.Write(test_data, arraysize(test_data), NULL); |
| 876 | |
| 877 | // Truncate the file. |
| 878 | ASSERT_EQ(4, write_stream.Truncate(4)); |
| 879 | |
| 880 | // Write again. |
| 881 | write_stream.Write(test_data, 4, NULL); |
| 882 | |
| 883 | // Close the stream. |
| 884 | write_stream.Close(); |
| 885 | |
| 886 | // Read in the contents and make sure we get back what we expected. |
| 887 | std::string read_contents; |
[email protected] | 3fc364f9 | 2009-08-06 22:35:13 | [diff] [blame] | 888 | EXPECT_TRUE(file_util::ReadFileToString(temp_file_path(), &read_contents)); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 889 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 890 | EXPECT_EQ("01230123", read_contents); |
[email protected] | 0643a49 | 2009-03-09 15:49:20 | [diff] [blame] | 891 | } |
| 892 | |
[email protected] | 4c2048a | 2009-03-24 21:02:01 | [diff] [blame] | 893 | } // namespace |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 894 | } // namespace net |