mount_point_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/components/disks/mount_point.h"
  5. #include "ash/components/disks/mock_disk_mount_manager.h"
  6. #include "base/run_loop.h"
  7. #include "base/test/bind.h"
  8. #include "base/test/gmock_callback_support.h"
  9. #include "base/test/task_environment.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. using ::base::test::RunOnceCallback;
  13. using ::testing::_;
  14. using ::testing::WithArg;
  15. using ::testing::WithoutArgs;
  16. namespace ash {
  17. namespace disks {
  18. namespace {
  19. constexpr char kSourcePath[] = "/source/path";
  20. constexpr char kMountPath[] = "/mount/path";
  21. class MountPointTest : public testing::Test {
  22. public:
  23. MountPointTest() = default;
  24. protected:
  25. base::test::TaskEnvironment task_environment_;
  26. MockDiskMountManager disk_mount_manager_;
  27. };
  28. TEST_F(MountPointTest, Mount) {
  29. EXPECT_CALL(disk_mount_manager_,
  30. MountPath(kSourcePath, "", "", _, MountType::kDevice,
  31. MountAccessMode::kReadWrite, _))
  32. .WillOnce(RunOnceCallback<6>(
  33. MountError::kNone, DiskMountManager::MountPoint{
  34. kSourcePath, kMountPath, MountType::kDevice}));
  35. EXPECT_CALL(disk_mount_manager_, UnmountPath(kMountPath, _)).Times(1);
  36. base::RunLoop run_loop;
  37. MountPoint::Mount(&disk_mount_manager_, kSourcePath, "", "", {},
  38. MountType::kDevice, MountAccessMode::kReadWrite,
  39. base::BindLambdaForTesting(
  40. [&run_loop](MountError mount_error,
  41. std::unique_ptr<MountPoint> mount) {
  42. EXPECT_EQ(MountError::kNone, mount_error);
  43. EXPECT_EQ(kMountPath, mount->mount_path().value());
  44. run_loop.Quit();
  45. }));
  46. run_loop.Run();
  47. }
  48. TEST_F(MountPointTest, MountFailure) {
  49. EXPECT_CALL(disk_mount_manager_,
  50. MountPath(kSourcePath, "", "", _, MountType::kDevice,
  51. MountAccessMode::kReadWrite, _))
  52. .WillOnce(RunOnceCallback<6>(
  53. MountError::kUnknown, DiskMountManager::MountPoint{
  54. kSourcePath, kMountPath, MountType::kDevice,
  55. MountCondition::kUnsupportedFilesystem}));
  56. EXPECT_CALL(disk_mount_manager_, UnmountPath(_, _)).Times(0);
  57. base::RunLoop run_loop;
  58. MountPoint::Mount(&disk_mount_manager_, kSourcePath, "", "", {},
  59. MountType::kDevice, MountAccessMode::kReadWrite,
  60. base::BindLambdaForTesting(
  61. [&run_loop](MountError mount_error,
  62. std::unique_ptr<MountPoint> mount) {
  63. EXPECT_EQ(MountError::kUnknown, mount_error);
  64. EXPECT_FALSE(mount);
  65. run_loop.Quit();
  66. }));
  67. run_loop.Run();
  68. }
  69. TEST_F(MountPointTest, Unmount) {
  70. EXPECT_CALL(disk_mount_manager_, UnmountPath(kMountPath, _))
  71. .WillOnce(base::test::RunOnceCallback<1>(MountError::kInternal));
  72. base::RunLoop run_loop;
  73. MountPoint mount_point(base::FilePath(kMountPath), &disk_mount_manager_);
  74. mount_point.Unmount(base::BindLambdaForTesting([&run_loop](MountError error) {
  75. EXPECT_EQ(MountError::kInternal, error);
  76. run_loop.Quit();
  77. }));
  78. run_loop.Run();
  79. }
  80. TEST_F(MountPointTest, UnmountOnDestruction) {
  81. EXPECT_CALL(disk_mount_manager_, UnmountPath(kMountPath, _)).Times(1);
  82. MountPoint mount_point(base::FilePath(kMountPath), &disk_mount_manager_);
  83. }
  84. TEST_F(MountPointTest, UnmountThenDestory) {
  85. base::RunLoop run_loop;
  86. EXPECT_CALL(disk_mount_manager_, UnmountPath(kMountPath, _))
  87. .WillOnce(WithArg<1>(
  88. [this, &run_loop](DiskMountManager::UnmountPathCallback callback) {
  89. task_environment_.GetMainThreadTaskRunner()->PostTask(
  90. FROM_HERE,
  91. base::BindOnce(std::move(callback), MountError::kInternal));
  92. task_environment_.GetMainThreadTaskRunner()->PostTask(
  93. FROM_HERE, run_loop.QuitClosure());
  94. }));
  95. std::unique_ptr<MountPoint> mount_point = std::make_unique<MountPoint>(
  96. base::FilePath(kMountPath), &disk_mount_manager_);
  97. mount_point->Unmount(base::BindLambdaForTesting([](MountError error) {
  98. // Expect that this callback is never run.
  99. FAIL();
  100. }));
  101. mount_point.reset();
  102. run_loop.Run();
  103. }
  104. } // namespace
  105. } // namespace disks
  106. } // namespace ash