| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/file_stream.h" | 5 #include "net/base/file_stream.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 EXPECT_EQ(OK, rv); | 106 EXPECT_EQ(OK, rv); |
| 107 EXPECT_TRUE(stream.IsOpen()); | 107 EXPECT_TRUE(stream.IsOpen()); |
| 108 file = stream.GetPlatformFileForTesting(); | 108 file = stream.GetPlatformFileForTesting(); |
| 109 } | 109 } |
| 110 EXPECT_NE(base::kInvalidPlatformFileValue, file); | 110 EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 111 base::PlatformFileInfo info; | 111 base::PlatformFileInfo info; |
| 112 // The file should be closed. | 112 // The file should be closed. |
| 113 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); | 113 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST_F(FileStreamTest, FileHandleLeftOpen) { | 116 TEST_F(FileStreamTest, FileHandleNotLeftOpen) { |
| 117 bool created = false; | 117 bool created = false; |
| 118 ASSERT_EQ(kTestDataSize, | 118 ASSERT_EQ(kTestDataSize, |
| 119 file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); | 119 file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
| 120 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; | 120 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; |
| 121 base::PlatformFile file = base::CreatePlatformFile( | 121 base::PlatformFile file = base::CreatePlatformFile( |
| 122 temp_file_path(), flags, &created, NULL); | 122 temp_file_path(), flags, &created, NULL); |
| 123 | 123 |
| 124 { | 124 { |
| 125 // Seek to the beginning of the file and read. | 125 // Seek to the beginning of the file and read. |
| 126 FileStream read_stream(file, flags, NULL); | 126 FileStream read_stream(file, flags, NULL); |
| 127 EXPECT_TRUE(read_stream.IsOpen()); | 127 EXPECT_TRUE(read_stream.IsOpen()); |
| 128 } | 128 } |
| 129 | 129 |
| 130 EXPECT_NE(base::kInvalidPlatformFileValue, file); | 130 EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 131 base::PlatformFileInfo info; | 131 base::PlatformFileInfo info; |
| 132 // The file should still be open. | 132 // The file should be closed. |
| 133 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info)); | 133 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); |
| 134 // Clean up. | 134 EXPECT_FALSE(base::ClosePlatformFile(file)); |
| 135 EXPECT_TRUE(base::ClosePlatformFile(file)); | |
| 136 } | 135 } |
| 137 | 136 |
| 138 // Test the use of FileStream with a file handle provided at construction. | 137 // Test the use of FileStream with a file handle provided at construction. |
| 139 TEST_F(FileStreamTest, UseFileHandle) { | 138 TEST_F(FileStreamTest, UseFileHandle) { |
| 140 bool created = false; | 139 bool created = false; |
| 141 | 140 |
| 142 // 1. Test reading with a file handle. | 141 // 1. Test reading with a file handle. |
| 143 ASSERT_EQ(kTestDataSize, | 142 ASSERT_EQ(kTestDataSize, |
| 144 file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); | 143 file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); |
| 145 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; | 144 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 if (rv < 0) { | 312 if (rv < 0) { |
| 314 EXPECT_EQ(ERR_IO_PENDING, rv); | 313 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 315 // The callback should not be called if the request is cancelled. | 314 // The callback should not be called if the request is cancelled. |
| 316 MessageLoop::current()->RunAllPending(); | 315 MessageLoop::current()->RunAllPending(); |
| 317 EXPECT_FALSE(callback.have_result()); | 316 EXPECT_FALSE(callback.have_result()); |
| 318 } else { | 317 } else { |
| 319 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); | 318 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); |
| 320 } | 319 } |
| 321 } | 320 } |
| 322 | 321 |
| 323 // Similar to AsyncRead_EarlyDelete but using a given file handler rather than | |
| 324 // calling FileStream::Open, to ensure that deleting a stream with in-flight | |
| 325 // operation without auto-closing feature is also ok. | |
| 326 TEST_F(FileStreamTest, AsyncRead_EarlyDelete_NoAutoClose) { | |
| 327 int64 file_size; | |
| 328 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); | |
| 329 EXPECT_TRUE(ok); | |
| 330 | |
| 331 bool created = false; | |
| 332 int flags = base::PLATFORM_FILE_OPEN | | |
| 333 base::PLATFORM_FILE_READ | | |
| 334 base::PLATFORM_FILE_ASYNC; | |
| 335 base::PlatformFileError error_code = base::PLATFORM_FILE_ERROR_FAILED; | |
| 336 base::PlatformFile file = base::CreatePlatformFile( | |
| 337 temp_file_path(), flags, &created, &error_code); | |
| 338 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | |
| 339 | |
| 340 scoped_ptr<FileStream> stream(new FileStream(file, flags, NULL)); | |
| 341 int64 total_bytes_avail = stream->Available(); | |
| 342 EXPECT_EQ(file_size, total_bytes_avail); | |
| 343 | |
| 344 TestCompletionCallback callback; | |
| 345 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | |
| 346 int rv = stream->Read(buf, buf->size(), callback.callback()); | |
| 347 stream.reset(); // Delete instead of closing it. | |
| 348 if (rv < 0) { | |
| 349 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 350 // The callback should not be called if the request is cancelled. | |
| 351 MessageLoop::current()->RunAllPending(); | |
| 352 EXPECT_FALSE(callback.have_result()); | |
| 353 } else { | |
| 354 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); | |
| 355 } | |
| 356 | |
| 357 base::PlatformFileInfo info; | |
| 358 // The file should still be open. | |
| 359 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info)); | |
| 360 // Clean up. | |
| 361 EXPECT_TRUE(base::ClosePlatformFile(file)); | |
| 362 } | |
| 363 | |
| 364 TEST_F(FileStreamTest, BasicRead_FromOffset) { | 322 TEST_F(FileStreamTest, BasicRead_FromOffset) { |
| 365 int64 file_size; | 323 int64 file_size; |
| 366 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); | 324 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 367 EXPECT_TRUE(ok); | 325 EXPECT_TRUE(ok); |
| 368 | 326 |
| 369 FileStream stream(NULL); | 327 FileStream stream(NULL); |
| 370 int flags = base::PLATFORM_FILE_OPEN | | 328 int flags = base::PLATFORM_FILE_OPEN | |
| 371 base::PLATFORM_FILE_READ; | 329 base::PLATFORM_FILE_READ; |
| 372 int rv = stream.OpenSync(temp_file_path(), flags); | 330 int rv = stream.OpenSync(temp_file_path(), flags); |
| 373 EXPECT_EQ(OK, rv); | 331 EXPECT_EQ(OK, rv); |
| (...skipping 836 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1210 int flags = base::PLATFORM_FILE_OPEN | | 1168 int flags = base::PLATFORM_FILE_OPEN | |
| 1211 base::PLATFORM_FILE_WRITE | | 1169 base::PLATFORM_FILE_WRITE | |
| 1212 base::PLATFORM_FILE_ASYNC; | 1170 base::PLATFORM_FILE_ASYNC; |
| 1213 TestCompletionCallback open_callback; | 1171 TestCompletionCallback open_callback; |
| 1214 int rv = stream.Open(temp_file_path(), flags, open_callback.callback()); | 1172 int rv = stream.Open(temp_file_path(), flags, open_callback.callback()); |
| 1215 EXPECT_EQ(ERR_IO_PENDING, rv); | 1173 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1216 | 1174 |
| 1217 // Close the stream without waiting for the completion. Should be safe. | 1175 // Close the stream without waiting for the completion. Should be safe. |
| 1218 stream.CloseSync(); | 1176 stream.CloseSync(); |
| 1219 // open_callback won't be called. | 1177 // open_callback won't be called. |
| 1178 MessageLoop::current()->RunAllPending(); |
| 1220 EXPECT_FALSE(open_callback.have_result()); | 1179 EXPECT_FALSE(open_callback.have_result()); |
| 1221 } | 1180 } |
| 1222 | 1181 |
| 1223 TEST_F(FileStreamTest, AsyncOpenAndDelete) { | 1182 TEST_F(FileStreamTest, AsyncOpenAndDelete) { |
| 1224 scoped_ptr<FileStream> stream(new FileStream(NULL)); | 1183 scoped_ptr<FileStream> stream(new FileStream(NULL)); |
| 1225 int flags = base::PLATFORM_FILE_OPEN | | 1184 int flags = base::PLATFORM_FILE_OPEN | |
| 1226 base::PLATFORM_FILE_WRITE | | 1185 base::PLATFORM_FILE_WRITE | |
| 1227 base::PLATFORM_FILE_ASYNC; | 1186 base::PLATFORM_FILE_ASYNC; |
| 1228 TestCompletionCallback open_callback; | 1187 TestCompletionCallback open_callback; |
| 1229 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); | 1188 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
| 1230 EXPECT_EQ(ERR_IO_PENDING, rv); | 1189 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1231 | 1190 |
| 1232 // Delete the stream without waiting for the open operation to be | 1191 // Delete the stream without waiting for the open operation to be |
| 1233 // complete. Should be safe. | 1192 // complete. Should be safe. |
| 1234 stream.reset(); | 1193 stream.reset(); |
| 1235 // open_callback won't be called. | 1194 // open_callback won't be called. |
| 1195 MessageLoop::current()->RunAllPending(); |
| 1236 EXPECT_FALSE(open_callback.have_result()); | 1196 EXPECT_FALSE(open_callback.have_result()); |
| 1237 } | 1197 } |
| 1238 | 1198 |
| 1239 TEST_F(FileStreamTest, AsyncCloseAndDelete) { | 1199 TEST_F(FileStreamTest, AsyncCloseAndDelete) { |
| 1240 scoped_ptr<FileStream> stream(new FileStream(NULL)); | 1200 scoped_ptr<FileStream> stream(new FileStream(NULL)); |
| 1241 int flags = base::PLATFORM_FILE_OPEN | | 1201 int flags = base::PLATFORM_FILE_OPEN | |
| 1242 base::PLATFORM_FILE_WRITE | | 1202 base::PLATFORM_FILE_WRITE | |
| 1243 base::PLATFORM_FILE_ASYNC; | 1203 base::PLATFORM_FILE_ASYNC; |
| 1244 TestCompletionCallback open_callback; | 1204 TestCompletionCallback open_callback; |
| 1245 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); | 1205 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
| 1246 EXPECT_EQ(ERR_IO_PENDING, rv); | 1206 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1247 EXPECT_EQ(OK, open_callback.WaitForResult()); | 1207 EXPECT_EQ(OK, open_callback.WaitForResult()); |
| 1248 EXPECT_TRUE(stream->IsOpen()); | 1208 EXPECT_TRUE(stream->IsOpen()); |
| 1249 | 1209 |
| 1250 TestCompletionCallback close_callback; | 1210 TestCompletionCallback close_callback; |
| 1251 stream->Close(close_callback.callback()); | 1211 stream->Close(close_callback.callback()); |
| 1252 | 1212 |
| 1253 // Delete the stream without waiting for the close operation to be | 1213 // Delete the stream without waiting for the close operation to be |
| 1254 // complete. Should be safe. | 1214 // complete. Should be safe. |
| 1255 stream.reset(); | 1215 stream.reset(); |
| 1256 // close_callback won't be called. | 1216 // close_callback won't be called. |
| 1217 MessageLoop::current()->RunAllPending(); |
| 1257 EXPECT_FALSE(close_callback.have_result()); | 1218 EXPECT_FALSE(close_callback.have_result()); |
| 1258 } | 1219 } |
| 1259 | 1220 |
| 1260 } // namespace | 1221 } // namespace |
| 1261 | 1222 |
| 1262 } // namespace net | 1223 } // namespace net |
| OLD | NEW |