Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: base/files/file_unittest.cc

Issue 1017243002: Add File::Duplicate to duplicate a file handle. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rvargas feedback Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/file_posix.cc ('k') | base/files/file_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/files/file.h" 5 #include "base/files/file.h"
6 #include "base/files/file_util.h" 6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/time/time.h" 9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 ASSERT_TRUE(file.IsValid()); 436 ASSERT_TRUE(file.IsValid());
437 437
438 const int64 kOffset = 10; 438 const int64 kOffset = 10;
439 EXPECT_EQ(kOffset, file.Seek(base::File::FROM_BEGIN, kOffset)); 439 EXPECT_EQ(kOffset, file.Seek(base::File::FROM_BEGIN, kOffset));
440 EXPECT_EQ(2 * kOffset, file.Seek(base::File::FROM_CURRENT, kOffset)); 440 EXPECT_EQ(2 * kOffset, file.Seek(base::File::FROM_CURRENT, kOffset));
441 EXPECT_EQ(kOffset, file.Seek(base::File::FROM_CURRENT, -kOffset)); 441 EXPECT_EQ(kOffset, file.Seek(base::File::FROM_CURRENT, -kOffset));
442 EXPECT_TRUE(file.SetLength(kOffset * 2)); 442 EXPECT_TRUE(file.SetLength(kOffset * 2));
443 EXPECT_EQ(kOffset, file.Seek(base::File::FROM_END, -kOffset)); 443 EXPECT_EQ(kOffset, file.Seek(base::File::FROM_END, -kOffset));
444 } 444 }
445 445
446 TEST(FileTest, Duplicate) {
447 base::ScopedTempDir temp_dir;
448 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
449 FilePath file_path = temp_dir.path().AppendASCII("file");
450 File file(file_path,(base::File::FLAG_CREATE |
451 base::File::FLAG_READ |
452 base::File::FLAG_WRITE));
453 ASSERT_TRUE(file.IsValid());
454
455 File file2(file.Duplicate());
456 ASSERT_TRUE(file2.IsValid());
457
458 // Write through one handle, close it, read through the other.
459 static const char kData[] = "now is a good time.";
460 static const int kDataLen = sizeof(kData) - 1;
461
462 ASSERT_EQ(0, file.Seek(base::File::FROM_CURRENT, 0));
463 ASSERT_EQ(0, file2.Seek(base::File::FROM_CURRENT, 0));
464 ASSERT_EQ(kDataLen, file.WriteAtCurrentPos(kData, kDataLen));
465 ASSERT_EQ(kDataLen, file.Seek(base::File::FROM_CURRENT, 0));
466 ASSERT_EQ(kDataLen, file2.Seek(base::File::FROM_CURRENT, 0));
467 file.Close();
468 char buf[kDataLen];
469 ASSERT_EQ(kDataLen, file2.Read(0, &buf[0], kDataLen));
470 ASSERT_EQ(std::string(kData, kDataLen), std::string(&buf[0], kDataLen));
471 }
472
473 TEST(FileTest, DuplicateDeleteOnClose) {
474 base::ScopedTempDir temp_dir;
475 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
476 FilePath file_path = temp_dir.path().AppendASCII("file");
477 File file(file_path,(base::File::FLAG_CREATE |
478 base::File::FLAG_READ |
479 base::File::FLAG_WRITE |
480 base::File::FLAG_DELETE_ON_CLOSE));
481 ASSERT_TRUE(file.IsValid());
482 ASSERT_TRUE(base::PathExists(file_path));
grt (UTC plus 2) 2015/03/19 04:07:09 This fails on ios_dbg_simulator_ninja. Is this exp
grt (UTC plus 2) 2015/03/19 15:26:27 Ah, I'd misunderstood how DELETE_ON_CLOSE worked o
483 File file2(file.Duplicate());
484 ASSERT_TRUE(file2.IsValid());
485 ASSERT_TRUE(base::PathExists(file_path));
486 file.Close();
487 file2.Close();
488 ASSERT_FALSE(base::PathExists(file_path));
489 }
490
446 #if defined(OS_WIN) 491 #if defined(OS_WIN)
447 TEST(FileTest, GetInfoForDirectory) { 492 TEST(FileTest, GetInfoForDirectory) {
448 base::ScopedTempDir temp_dir; 493 base::ScopedTempDir temp_dir;
449 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 494 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
450 FilePath empty_dir = temp_dir.path().Append(FILE_PATH_LITERAL("gpfi_test")); 495 FilePath empty_dir = temp_dir.path().Append(FILE_PATH_LITERAL("gpfi_test"));
451 ASSERT_TRUE(CreateDirectory(empty_dir)); 496 ASSERT_TRUE(CreateDirectory(empty_dir));
452 497
453 base::File dir( 498 base::File dir(
454 ::CreateFile(empty_dir.value().c_str(), 499 ::CreateFile(empty_dir.value().c_str(),
455 FILE_ALL_ACCESS, 500 FILE_ALL_ACCESS,
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 EXPECT_DEATH(file.Seek(File::FROM_BEGIN, 0), ""); 573 EXPECT_DEATH(file.Seek(File::FROM_BEGIN, 0), "");
529 EXPECT_DEATH(file.Read(0, NULL, 0), ""); 574 EXPECT_DEATH(file.Read(0, NULL, 0), "");
530 EXPECT_DEATH(file.ReadAtCurrentPos(NULL, 0), ""); 575 EXPECT_DEATH(file.ReadAtCurrentPos(NULL, 0), "");
531 EXPECT_DEATH(file.Write(0, NULL, 0), ""); 576 EXPECT_DEATH(file.Write(0, NULL, 0), "");
532 577
533 ignore_result(file.file_.file_.release()); 578 ignore_result(file.file_.file_.release());
534 file.file_.UpdateChecksum(); 579 file.file_.UpdateChecksum();
535 } 580 }
536 } 581 }
537 #endif // defined(OS_POSIX) 582 #endif // defined(OS_POSIX)
OLDNEW
« no previous file with comments | « base/files/file_posix.cc ('k') | base/files/file_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698