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 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 if (rv < 0) { | 315 if (rv < 0) { |
316 EXPECT_EQ(ERR_IO_PENDING, rv); | 316 EXPECT_EQ(ERR_IO_PENDING, rv); |
317 // The callback should not be called if the request is cancelled. | 317 // The callback should not be called if the request is cancelled. |
318 MessageLoop::current()->RunAllPending(); | 318 MessageLoop::current()->RunAllPending(); |
319 EXPECT_FALSE(callback.have_result()); | 319 EXPECT_FALSE(callback.have_result()); |
320 } else { | 320 } else { |
321 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); | 321 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); |
322 } | 322 } |
323 } | 323 } |
324 | 324 |
325 // Similar to AsyncRead_EarlyDelete but using a given file handler rather than | 325 // Similar to AsyncRead_EarlyDelete but calls CloseAndCancelAsync() instead, |
326 // calling FileStream::Open, to ensure that deleting a stream with in-flight | 326 // to ensure that it works as expected while an async read is in flight. |
327 // operation without auto-closing feature is also ok. | 327 TEST_F(FileStreamTest, AsyncRead_CloseAndCancelAsync) { |
328 TEST_F(FileStreamTest, AsyncRead_EarlyDelete_NoAutoClose) { | |
329 int64 file_size; | 328 int64 file_size; |
330 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); | 329 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
331 EXPECT_TRUE(ok); | 330 EXPECT_TRUE(ok); |
332 | 331 |
333 bool created = false; | 332 scoped_ptr<FileStream> stream(new FileStream(NULL)); |
334 int flags = base::PLATFORM_FILE_OPEN | | 333 int flags = base::PLATFORM_FILE_OPEN | |
335 base::PLATFORM_FILE_READ | | 334 base::PLATFORM_FILE_READ | |
336 base::PLATFORM_FILE_ASYNC; | 335 base::PLATFORM_FILE_ASYNC; |
337 base::PlatformFileError error_code = base::PLATFORM_FILE_ERROR_FAILED; | 336 TestCompletionCallback callback; |
338 base::PlatformFile file = base::CreatePlatformFile( | 337 int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
339 temp_file_path(), flags, &created, &error_code); | 338 EXPECT_EQ(ERR_IO_PENDING, rv); |
340 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | 339 EXPECT_EQ(OK, callback.WaitForResult()); |
341 | 340 |
342 scoped_ptr<FileStream> stream(new FileStream(file, flags, NULL)); | |
343 int64 total_bytes_avail = stream->Available(); | 341 int64 total_bytes_avail = stream->Available(); |
344 EXPECT_EQ(file_size, total_bytes_avail); | 342 EXPECT_EQ(file_size, total_bytes_avail); |
345 | 343 |
346 TestCompletionCallback callback; | |
347 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 344 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
348 int rv = stream->Read(buf, buf->size(), callback.callback()); | 345 rv = stream->Read(buf, buf->size(), callback.callback()); |
349 stream.reset(); // Delete instead of closing it. | 346 stream->CloseAndCancelAsync(); |
350 if (rv < 0) { | 347 if (rv < 0) { |
351 EXPECT_EQ(ERR_IO_PENDING, rv); | 348 EXPECT_EQ(ERR_IO_PENDING, rv); |
352 // The callback should not be called if the request is cancelled. | 349 // The callback should not be called if the request is canceled. |
353 MessageLoop::current()->RunAllPending(); | 350 MessageLoop::current()->RunAllPending(); |
354 EXPECT_FALSE(callback.have_result()); | 351 EXPECT_FALSE(callback.have_result()); |
355 } else { | 352 } else { |
356 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); | 353 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); |
357 } | 354 } |
358 | |
359 base::PlatformFileInfo info; | |
360 // The file should still be open. | |
361 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info)); | |
362 // Clean up. | |
363 EXPECT_TRUE(base::ClosePlatformFile(file)); | |
364 } | 355 } |
365 | 356 |
366 TEST_F(FileStreamTest, BasicRead_FromOffset) { | 357 TEST_F(FileStreamTest, BasicRead_FromOffset) { |
367 int64 file_size; | 358 int64 file_size; |
368 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); | 359 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
369 EXPECT_TRUE(ok); | 360 EXPECT_TRUE(ok); |
370 | 361 |
371 FileStream stream(NULL); | 362 FileStream stream(NULL); |
372 int flags = base::PLATFORM_FILE_OPEN | | 363 int flags = base::PLATFORM_FILE_OPEN | |
373 base::PLATFORM_FILE_READ; | 364 base::PLATFORM_FILE_READ; |
(...skipping 838 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1212 int flags = base::PLATFORM_FILE_OPEN | | 1203 int flags = base::PLATFORM_FILE_OPEN | |
1213 base::PLATFORM_FILE_WRITE | | 1204 base::PLATFORM_FILE_WRITE | |
1214 base::PLATFORM_FILE_ASYNC; | 1205 base::PLATFORM_FILE_ASYNC; |
1215 TestCompletionCallback open_callback; | 1206 TestCompletionCallback open_callback; |
1216 int rv = stream.Open(temp_file_path(), flags, open_callback.callback()); | 1207 int rv = stream.Open(temp_file_path(), flags, open_callback.callback()); |
1217 EXPECT_EQ(ERR_IO_PENDING, rv); | 1208 EXPECT_EQ(ERR_IO_PENDING, rv); |
1218 | 1209 |
1219 // Close the stream without waiting for the completion. Should be safe. | 1210 // Close the stream without waiting for the completion. Should be safe. |
1220 stream.CloseSync(); | 1211 stream.CloseSync(); |
1221 // open_callback won't be called. | 1212 // open_callback won't be called. |
| 1213 MessageLoop::current()->RunAllPending(); |
1222 EXPECT_FALSE(open_callback.have_result()); | 1214 EXPECT_FALSE(open_callback.have_result()); |
1223 } | 1215 } |
1224 | 1216 |
1225 TEST_F(FileStreamTest, AsyncOpenAndDelete) { | 1217 TEST_F(FileStreamTest, AsyncOpenAndDelete) { |
1226 scoped_ptr<FileStream> stream(new FileStream(NULL)); | 1218 scoped_ptr<FileStream> stream(new FileStream(NULL)); |
1227 int flags = base::PLATFORM_FILE_OPEN | | 1219 int flags = base::PLATFORM_FILE_OPEN | |
1228 base::PLATFORM_FILE_WRITE | | 1220 base::PLATFORM_FILE_WRITE | |
1229 base::PLATFORM_FILE_ASYNC; | 1221 base::PLATFORM_FILE_ASYNC; |
1230 TestCompletionCallback open_callback; | 1222 TestCompletionCallback open_callback; |
1231 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); | 1223 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
1232 EXPECT_EQ(ERR_IO_PENDING, rv); | 1224 EXPECT_EQ(ERR_IO_PENDING, rv); |
1233 | 1225 |
1234 // Delete the stream without waiting for the open operation to be | 1226 // Delete the stream without waiting for the open operation to be |
1235 // complete. Should be safe. | 1227 // complete. Should be safe. |
1236 stream.reset(); | 1228 stream.reset(); |
1237 // open_callback won't be called. | 1229 // open_callback won't be called. |
| 1230 MessageLoop::current()->RunAllPending(); |
| 1231 EXPECT_FALSE(open_callback.have_result()); |
| 1232 } |
| 1233 |
| 1234 TEST_F(FileStreamTest, AsyncOpenAndCancelAsync) { |
| 1235 scoped_ptr<FileStream> stream(new FileStream(NULL)); |
| 1236 int flags = base::PLATFORM_FILE_OPEN | |
| 1237 base::PLATFORM_FILE_WRITE | |
| 1238 base::PLATFORM_FILE_ASYNC; |
| 1239 TestCompletionCallback open_callback; |
| 1240 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
| 1241 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1242 |
| 1243 // Close and cancel without waiting for the open operation to be |
| 1244 // complete. Should be safe. |
| 1245 stream->CloseAndCancelAsync(); |
| 1246 // open_callback won't be called. |
| 1247 MessageLoop::current()->RunAllPending(); |
1238 EXPECT_FALSE(open_callback.have_result()); | 1248 EXPECT_FALSE(open_callback.have_result()); |
1239 } | 1249 } |
1240 | 1250 |
1241 TEST_F(FileStreamTest, AsyncCloseAndDelete) { | 1251 TEST_F(FileStreamTest, AsyncCloseAndDelete) { |
1242 scoped_ptr<FileStream> stream(new FileStream(NULL)); | 1252 scoped_ptr<FileStream> stream(new FileStream(NULL)); |
1243 int flags = base::PLATFORM_FILE_OPEN | | 1253 int flags = base::PLATFORM_FILE_OPEN | |
1244 base::PLATFORM_FILE_WRITE | | 1254 base::PLATFORM_FILE_WRITE | |
1245 base::PLATFORM_FILE_ASYNC; | 1255 base::PLATFORM_FILE_ASYNC; |
1246 TestCompletionCallback open_callback; | 1256 TestCompletionCallback open_callback; |
1247 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); | 1257 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
1248 EXPECT_EQ(ERR_IO_PENDING, rv); | 1258 EXPECT_EQ(ERR_IO_PENDING, rv); |
1249 EXPECT_EQ(OK, open_callback.WaitForResult()); | 1259 EXPECT_EQ(OK, open_callback.WaitForResult()); |
1250 EXPECT_TRUE(stream->IsOpen()); | 1260 EXPECT_TRUE(stream->IsOpen()); |
1251 | 1261 |
1252 TestCompletionCallback close_callback; | 1262 TestCompletionCallback close_callback; |
1253 stream->Close(close_callback.callback()); | 1263 stream->Close(close_callback.callback()); |
1254 | 1264 |
1255 // Delete the stream without waiting for the close operation to be | 1265 // Delete the stream without waiting for the close operation to be |
1256 // complete. Should be safe. | 1266 // complete. Should be safe. |
1257 stream.reset(); | 1267 stream.reset(); |
1258 // close_callback won't be called. | 1268 // close_callback won't be called. |
| 1269 MessageLoop::current()->RunAllPending(); |
1259 EXPECT_FALSE(close_callback.have_result()); | 1270 EXPECT_FALSE(close_callback.have_result()); |
1260 } | 1271 } |
1261 | 1272 |
1262 } // namespace | 1273 } // namespace |
1263 | 1274 |
1264 } // namespace net | 1275 } // namespace net |
OLD | NEW |