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

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: posix fix 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 File file2(file.Duplicate());
483 ASSERT_TRUE(file2.IsValid());
484 file.Close();
485 file2.Close();
486 ASSERT_FALSE(base::PathExists(file_path));
487 }
488
446 #if defined(OS_WIN) 489 #if defined(OS_WIN)
447 TEST(FileTest, GetInfoForDirectory) { 490 TEST(FileTest, GetInfoForDirectory) {
448 base::ScopedTempDir temp_dir; 491 base::ScopedTempDir temp_dir;
449 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 492 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
450 FilePath empty_dir = temp_dir.path().Append(FILE_PATH_LITERAL("gpfi_test")); 493 FilePath empty_dir = temp_dir.path().Append(FILE_PATH_LITERAL("gpfi_test"));
451 ASSERT_TRUE(CreateDirectory(empty_dir)); 494 ASSERT_TRUE(CreateDirectory(empty_dir));
452 495
453 base::File dir( 496 base::File dir(
454 ::CreateFile(empty_dir.value().c_str(), 497 ::CreateFile(empty_dir.value().c_str(),
455 FILE_ALL_ACCESS, 498 FILE_ALL_ACCESS,
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 EXPECT_DEATH(file.Seek(File::FROM_BEGIN, 0), ""); 571 EXPECT_DEATH(file.Seek(File::FROM_BEGIN, 0), "");
529 EXPECT_DEATH(file.Read(0, NULL, 0), ""); 572 EXPECT_DEATH(file.Read(0, NULL, 0), "");
530 EXPECT_DEATH(file.ReadAtCurrentPos(NULL, 0), ""); 573 EXPECT_DEATH(file.ReadAtCurrentPos(NULL, 0), "");
531 EXPECT_DEATH(file.Write(0, NULL, 0), ""); 574 EXPECT_DEATH(file.Write(0, NULL, 0), "");
532 575
533 ignore_result(file.file_.file_.release()); 576 ignore_result(file.file_.file_.release());
534 file.file_.UpdateChecksum(); 577 file.file_.UpdateChecksum();
535 } 578 }
536 } 579 }
537 #endif // defined(OS_POSIX) 580 #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