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

Side by Side Diff: mount_unittest.cc

Issue 6598074: DoAutomaticFreeDiskSpaceControl() introduced (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/cryptohome.git@master
Patch Set: Created 9 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 | Annotate | Revision Log
« no previous file with comments | « mount_task_unittest.cc ('k') | platform.h » ('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) 2009-2010 The Chromium OS Authors. All rights reserved. 1 // Copyright (c) 2009-2010 The Chromium OS 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 // Unit tests for Mount. 5 // Unit tests for Mount.
6 6
7 #include "mount.h" 7 #include "mount.h"
8 8
9 #include <openssl/sha.h> 9 #include <openssl/sha.h>
10 #include <pwd.h> 10 #include <pwd.h>
(...skipping 18 matching lines...) Expand all
29 #include "vault_keyset.pb.h" 29 #include "vault_keyset.pb.h"
30 30
31 namespace cryptohome { 31 namespace cryptohome {
32 using std::string; 32 using std::string;
33 using ::testing::Return; 33 using ::testing::Return;
34 using ::testing::_; 34 using ::testing::_;
35 using ::testing::NiceMock; 35 using ::testing::NiceMock;
36 36
37 const char kImageDir[] = "test_image_dir"; 37 const char kImageDir[] = "test_image_dir";
38 const char kSkelDir[] = "test_image_dir/skel"; 38 const char kSkelDir[] = "test_image_dir/skel";
39 const char kHomeDir[] = "alt_test_home_dir";
39 const char kAltImageDir[] = "alt_test_image_dir"; 40 const char kAltImageDir[] = "alt_test_image_dir";
40 const char kAltHomeDir[] = "alt_test_home_dir";
41 41
42 class MountTest : public ::testing::Test { 42 class MountTest : public ::testing::Test {
43 public: 43 public:
44 MountTest() { } 44 MountTest() { }
45 virtual ~MountTest() { } 45 virtual ~MountTest() { }
46 46
47 void SetUp() { 47 void SetUp() {
48 LoadSystemSalt(kImageDir); 48 LoadSystemSalt(kImageDir);
49 } 49 }
50 50
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 .WillRepeatedly(Return(true)); 537 .WillRepeatedly(Return(true));
538 538
539 mount.CleanUnmountedTrackedSubdirectories(); 539 mount.CleanUnmountedTrackedSubdirectories();
540 540
541 ASSERT_FALSE(file_util::PathExists(subdir_path)); 541 ASSERT_FALSE(file_util::PathExists(subdir_path));
542 } 542 }
543 543
544 TEST_F(MountTest, MigrationOfTrackedDirs) { 544 TEST_F(MountTest, MigrationOfTrackedDirs) {
545 // Checks that old cryptohomes (without pass-through tracked 545 // Checks that old cryptohomes (without pass-through tracked
546 // directories) migrate when Mount()ed. 546 // directories) migrate when Mount()ed.
547 LoadSystemSalt(kAltImageDir); 547 LoadSystemSalt(kImageDir);
548 Mount mount; 548 Mount mount;
549 NiceMock<MockTpm> tpm; 549 NiceMock<MockTpm> tpm;
550 mount.get_crypto()->set_tpm(&tpm); 550 mount.get_crypto()->set_tpm(&tpm);
551 mount.set_shadow_root(kAltImageDir); 551 mount.set_shadow_root(kImageDir);
552 mount.set_skel_source(kSkelDir);
553 mount.set_use_tpm(false); 552 mount.set_use_tpm(false);
554 553
555 NiceMock<MockPlatform> platform; 554 NiceMock<MockPlatform> platform;
556 mount.set_platform(&platform); 555 mount.set_platform(&platform);
557 556
558 EXPECT_TRUE(mount.Init()); 557 EXPECT_TRUE(mount.Init());
559 558
560 cryptohome::SecureBlob passkey; 559 cryptohome::SecureBlob passkey;
561 cryptohome::Crypto::PasswordToPasskey(kAlternateUsers[1].password, 560 cryptohome::Crypto::PasswordToPasskey(kDefaultUsers[8].password,
562 system_salt_, &passkey); 561 system_salt_, &passkey);
563 UsernamePasskey up(kAlternateUsers[1].username, passkey); 562 UsernamePasskey up(kDefaultUsers[8].username, passkey);
564 563
565 // As we don't have real mount in the test, immagine its output (home) 564 // As we don't have real mount in the test, immagine its output (home)
566 // directory. 565 // directory.
567 FilePath home_dir(kAltHomeDir); 566 FilePath home_dir(kHomeDir);
568 file_util::CreateDirectory(home_dir); 567 file_util::CreateDirectory(home_dir);
569 mount.set_home_dir(home_dir.value()); 568 mount.set_home_dir(home_dir.value());
570 569
571 // Pretend that mounted cryptohome already had non-pass-through 570 // Pretend that mounted cryptohome already had non-pass-through
572 // subdirs "Cache" and "Downloads". 571 // subdirs "Cache" and "Downloads".
573 FilePath cache_dir(home_dir.Append(kCacheDir)); 572 FilePath cache_dir(home_dir.Append(kCacheDir));
574 FilePath downloads_dir(home_dir.Append(kDownloadsDir)); 573 FilePath downloads_dir(home_dir.Append(kDownloadsDir));
575 file_util::CreateDirectory(cache_dir); 574 file_util::CreateDirectory(cache_dir);
576 file_util::CreateDirectory(downloads_dir); 575 file_util::CreateDirectory(downloads_dir);
577 576
(...skipping 10 matching lines...) Expand all
588 file_util::CreateDirectory(cache_subdir); 587 file_util::CreateDirectory(cache_subdir);
589 file_util::CreateDirectory(downloads_subdir); 588 file_util::CreateDirectory(downloads_subdir);
590 file_util::WriteFile(cache_subdir.Append("cached_file"), 589 file_util::WriteFile(cache_subdir.Append("cached_file"),
591 contents.c_str(), contents.length()); 590 contents.c_str(), contents.length());
592 file_util::WriteFile(downloads_subdir.Append("downloaded_file"), 591 file_util::WriteFile(downloads_subdir.Append("downloaded_file"),
593 contents.c_str(), contents.length()); 592 contents.c_str(), contents.length());
594 593
595 // Now Mount(). 594 // Now Mount().
596 EXPECT_CALL(platform, Mount(_, _, _, _)) 595 EXPECT_CALL(platform, Mount(_, _, _, _))
597 .WillRepeatedly(Return(true)); 596 .WillRepeatedly(Return(true));
598 EXPECT_CALL(platform, Unmount(_, _, _))
599 .WillRepeatedly(Return(true));
600 Mount::MountError error; 597 Mount::MountError error;
601 EXPECT_TRUE(mount.MountCryptohome(up, Mount::MountArgs(), &error)); 598 EXPECT_TRUE(mount.MountCryptohome(up, Mount::MountArgs(), &error));
602 599
603 // Check that vault path now have pass-through version of tracked dirs. 600 // Check that vault path now have pass-through version of tracked dirs.
604 FilePath image_dir(kAltImageDir); 601 FilePath image_dir(kImageDir);
605 FilePath user_path = image_dir.Append(up.GetObfuscatedUsername(system_salt_)); 602 FilePath user_path = image_dir.Append(up.GetObfuscatedUsername(system_salt_));
606 FilePath vault_path = user_path.Append("vault"); 603 FilePath vault_path = user_path.Append("vault");
607 ASSERT_TRUE(file_util::PathExists(vault_path.Append(kCacheDir))); 604 ASSERT_TRUE(file_util::PathExists(vault_path.Append(kCacheDir)));
608 ASSERT_TRUE(file_util::PathExists(vault_path.Append(kDownloadsDir))); 605 ASSERT_TRUE(file_util::PathExists(vault_path.Append(kDownloadsDir)));
609 606
610 // Check that vault path does not contain user data unencrypted. 607 // Check that vault path does not contain user data unencrypted.
611 // Note, that if we had real mount, we would see encrypted file names there; 608 // Note, that if we had real mount, we would see encrypted file names there;
612 // but with our mock mount, we must see empty directories. 609 // but with our mock mount, we must see empty directories.
613 EXPECT_TRUE(file_util::IsDirectoryEmpty(vault_path.Append(kCacheDir))); 610 EXPECT_TRUE(file_util::IsDirectoryEmpty(vault_path.Append(kCacheDir)));
614 EXPECT_TRUE(file_util::IsDirectoryEmpty(vault_path.Append(kDownloadsDir))); 611 EXPECT_TRUE(file_util::IsDirectoryEmpty(vault_path.Append(kDownloadsDir)));
(...skipping 12 matching lines...) Expand all
627 tested.clear(); 624 tested.clear();
628 EXPECT_TRUE(file_util::ReadFileToString( 625 EXPECT_TRUE(file_util::ReadFileToString(
629 downloads_subdir.Append("downloaded_file"), &tested)); 626 downloads_subdir.Append("downloaded_file"), &tested));
630 EXPECT_EQ(contents, tested); 627 EXPECT_EQ(contents, tested);
631 628
632 // Check that we did not leave any litter. 629 // Check that we did not leave any litter.
633 file_util::Delete(downloads_dir, true); 630 file_util::Delete(downloads_dir, true);
634 EXPECT_TRUE(file_util::IsDirectoryEmpty(home_dir)); 631 EXPECT_TRUE(file_util::IsDirectoryEmpty(home_dir));
635 } 632 }
636 633
634 TEST_F(MountTest, DoAutomaticFreeDiskSpaceControl) {
635 // Checks that DoAutomaticFreeDiskSpaceControl() does the clean-up
636 // if free disk space is low.
637 LoadSystemSalt(kAltImageDir);
638 Mount mount;
639 NiceMock<MockTpm> tpm;
640 mount.get_crypto()->set_tpm(&tpm);
641 mount.set_shadow_root(kAltImageDir);
642 mount.set_use_tpm(false);
643
644 NiceMock<MockPlatform> platform;
645 mount.set_platform(&platform);
646
647 EXPECT_TRUE(mount.Init());
648
649 // For every user, prepare cryptohome contents.
650 const string contents = "some crypted contets";
651 FilePath image_dir(kAltImageDir);
652 FilePath vault_path[kAlternateUserCount];
653 FilePath cache_dir[kAlternateUserCount];
654 FilePath cache_subdir[kAlternateUserCount];
655 for (size_t user = 0; user < kAlternateUserCount; user ++) {
656 cryptohome::SecureBlob passkey;
657 cryptohome::Crypto::PasswordToPasskey(kAlternateUsers[user].password,
658 system_salt_, &passkey);
659 UsernamePasskey up(kAlternateUsers[user].username, passkey);
660 vault_path[user] = image_dir
661 .Append(up.GetObfuscatedUsername(system_salt_))
662 .Append("vault");
663
664 // Let their Cache dirs be filled with some data.
665 cache_dir[user] = vault_path[user].Append(kCacheDir);
666 file_util::CreateDirectory(cache_dir[user]);
667 file_util::WriteFile(cache_dir[user].Append("cached_file"),
668 contents.c_str(), contents.length());
669 cache_subdir[user] = cache_dir[user].Append("cache_subdir");
670 file_util::CreateDirectory(cache_subdir[user]);
671 file_util::WriteFile(cache_subdir[user].Append("cached_file"),
672 contents.c_str(), contents.length());
673 }
674
675 // Firstly, pretend we have lots of free space.
676 EXPECT_CALL(platform, AmountOfFreeDiskSpace(_))
677 .WillRepeatedly(Return(kMinFreeSpace + 1));
678
679 // DoAutomaticFreeDiskSpaceControl() must do nothing.
680 mount.DoAutomaticFreeDiskSpaceControl();
681
682 // Check that Cache is not changed.
683 for (size_t user = 0; user < kAlternateUserCount; user ++) {
684 string tested;
685 EXPECT_TRUE(file_util::PathExists(cache_dir[user]));
686 EXPECT_TRUE(file_util::ReadFileToString(
687 cache_dir[user].Append("cached_file"), &tested));
688 EXPECT_EQ(contents, tested);
689 EXPECT_TRUE(file_util::PathExists(cache_subdir[user]));
690 tested.clear();
691 EXPECT_TRUE(file_util::ReadFileToString(
692 cache_subdir[user].Append("cached_file"), &tested));
693 EXPECT_EQ(contents, tested);
694 }
695
696 // Now pretend we have lack of free space.
697 EXPECT_CALL(platform, AmountOfFreeDiskSpace(_))
698 .WillRepeatedly(Return(kMinFreeSpace - 1));
699
700 // DoAutomaticFreeDiskSpaceControl() must do the clean-up..
701 mount.DoAutomaticFreeDiskSpaceControl();
702
703 // Cache must be empty (and may even be deleted).
704 for (size_t user = 0; user < kAlternateUserCount; user ++) {
705 EXPECT_TRUE(file_util::IsDirectoryEmpty(cache_dir[user]));
706
707 // Check that we did not leave any litter.
708 file_util::Delete(cache_dir[user], true);
709 EXPECT_TRUE(file_util::IsDirectoryEmpty(vault_path[user]));
710 }
711 }
712
637 } // namespace cryptohome 713 } // namespace cryptohome
OLDNEW
« no previous file with comments | « mount_task_unittest.cc ('k') | platform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698