smbfs_host_unittest.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/smbfs/smbfs_host.h"
  5. #include "ash/components/disks/mock_disk_mount_manager.h"
  6. #include "base/files/file_util.h"
  7. #include "base/run_loop.h"
  8. #include "base/test/bind.h"
  9. #include "base/test/gmock_callback_support.h"
  10. #include "base/test/task_environment.h"
  11. #include "mojo/public/cpp/system/platform_handle.h"
  12. #include "testing/gmock/include/gmock/gmock.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. using testing::_;
  15. namespace smbfs {
  16. namespace {
  17. constexpr base::FilePath::CharType kMountPath[] = FILE_PATH_LITERAL("/foo/bar");
  18. constexpr char kUsername[] = "my-username";
  19. constexpr char kWorkgroup[] = "my-workgroup";
  20. constexpr char kPassword[] = "super-secret-password-shhh-dont-tell-anyone";
  21. class MockDelegate : public SmbFsHost::Delegate {
  22. public:
  23. MOCK_METHOD(void, OnDisconnected, (), (override));
  24. MOCK_METHOD(void,
  25. RequestCredentials,
  26. (RequestCredentialsCallback),
  27. (override));
  28. };
  29. class SmbFsHostTest : public testing::Test {
  30. protected:
  31. void SetUp() override {
  32. smbfs_pending_receiver_ = smbfs_remote_.BindNewPipeAndPassReceiver();
  33. delegate_pending_receiver_ = delegate_remote_.BindNewPipeAndPassReceiver();
  34. }
  35. base::test::TaskEnvironment task_environment_;
  36. MockDelegate mock_delegate_;
  37. ash::disks::MockDiskMountManager mock_disk_mount_manager_;
  38. mojo::Remote<mojom::SmbFs> smbfs_remote_;
  39. mojo::PendingReceiver<mojom::SmbFs> smbfs_pending_receiver_;
  40. mojo::Remote<mojom::SmbFsDelegate> delegate_remote_;
  41. mojo::PendingReceiver<mojom::SmbFsDelegate> delegate_pending_receiver_;
  42. };
  43. TEST_F(SmbFsHostTest, DisconnectDelegate) {
  44. base::RunLoop run_loop;
  45. EXPECT_CALL(mock_delegate_, OnDisconnected())
  46. .WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
  47. EXPECT_CALL(mock_disk_mount_manager_, UnmountPath(kMountPath, _))
  48. .WillOnce(base::test::RunOnceCallback<1>(ash::MountError::kNone));
  49. std::unique_ptr<SmbFsHost> host = std::make_unique<SmbFsHost>(
  50. std::make_unique<ash::disks::MountPoint>(base::FilePath(kMountPath),
  51. &mock_disk_mount_manager_),
  52. &mock_delegate_, std::move(smbfs_remote_),
  53. std::move(delegate_pending_receiver_));
  54. delegate_remote_.reset();
  55. run_loop.Run();
  56. }
  57. TEST_F(SmbFsHostTest, DisconnectSmbFs) {
  58. base::RunLoop run_loop;
  59. EXPECT_CALL(mock_delegate_, OnDisconnected())
  60. .WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
  61. EXPECT_CALL(mock_disk_mount_manager_, UnmountPath(kMountPath, _))
  62. .WillOnce(base::test::RunOnceCallback<1>(ash::MountError::kNone));
  63. std::unique_ptr<SmbFsHost> host = std::make_unique<SmbFsHost>(
  64. std::make_unique<ash::disks::MountPoint>(base::FilePath(kMountPath),
  65. &mock_disk_mount_manager_),
  66. &mock_delegate_, std::move(smbfs_remote_),
  67. std::move(delegate_pending_receiver_));
  68. smbfs_pending_receiver_.reset();
  69. run_loop.Run();
  70. }
  71. TEST_F(SmbFsHostTest, UnmountOnDestruction) {
  72. EXPECT_CALL(mock_delegate_, OnDisconnected()).Times(0);
  73. EXPECT_CALL(mock_disk_mount_manager_, UnmountPath(kMountPath, _))
  74. .WillOnce(base::test::RunOnceCallback<1>(ash::MountError::kNone));
  75. base::RunLoop run_loop;
  76. std::unique_ptr<SmbFsHost> host = std::make_unique<SmbFsHost>(
  77. std::make_unique<ash::disks::MountPoint>(base::FilePath(kMountPath),
  78. &mock_disk_mount_manager_),
  79. &mock_delegate_, std::move(smbfs_remote_),
  80. std::move(delegate_pending_receiver_));
  81. run_loop.RunUntilIdle();
  82. host.reset();
  83. }
  84. TEST_F(SmbFsHostTest, RequestCredentials_ProvideCredentials) {
  85. EXPECT_CALL(mock_disk_mount_manager_, UnmountPath(kMountPath, _))
  86. .WillOnce(base::test::RunOnceCallback<1>(ash::MountError::kNone));
  87. std::unique_ptr<SmbFsHost> host = std::make_unique<SmbFsHost>(
  88. std::make_unique<ash::disks::MountPoint>(base::FilePath(kMountPath),
  89. &mock_disk_mount_manager_),
  90. &mock_delegate_, std::move(smbfs_remote_),
  91. std::move(delegate_pending_receiver_));
  92. EXPECT_CALL(mock_delegate_, RequestCredentials(_))
  93. .WillOnce(base::test::RunOnceCallback<0>(false /* cancel */, kUsername,
  94. kWorkgroup, kPassword));
  95. base::RunLoop run_loop;
  96. delegate_remote_->RequestCredentials(base::BindLambdaForTesting(
  97. [&run_loop](mojom::CredentialsPtr credentials) {
  98. ASSERT_TRUE(credentials);
  99. EXPECT_EQ(credentials->username, kUsername);
  100. EXPECT_EQ(credentials->workgroup, kWorkgroup);
  101. ASSERT_TRUE(credentials->password);
  102. EXPECT_EQ(credentials->password->length,
  103. static_cast<int32_t>(strlen(kPassword)));
  104. std::string password_buf(credentials->password->length, 'a');
  105. base::ScopedFD fd =
  106. mojo::UnwrapPlatformHandle(std::move(credentials->password->fd))
  107. .TakeFD();
  108. EXPECT_TRUE(base::ReadFromFD(fd.get(), &(password_buf.front()),
  109. credentials->password->length));
  110. EXPECT_EQ(password_buf, kPassword);
  111. run_loop.Quit();
  112. }));
  113. run_loop.Run();
  114. }
  115. TEST_F(SmbFsHostTest, RequestCredentials_Cancel) {
  116. EXPECT_CALL(mock_disk_mount_manager_, UnmountPath(kMountPath, _))
  117. .WillOnce(base::test::RunOnceCallback<1>(ash::MountError::kNone));
  118. std::unique_ptr<SmbFsHost> host = std::make_unique<SmbFsHost>(
  119. std::make_unique<ash::disks::MountPoint>(base::FilePath(kMountPath),
  120. &mock_disk_mount_manager_),
  121. &mock_delegate_, std::move(smbfs_remote_),
  122. std::move(delegate_pending_receiver_));
  123. EXPECT_CALL(mock_delegate_, RequestCredentials(_))
  124. .WillOnce(base::test::RunOnceCallback<0>(
  125. true /* cancel */, "" /* username */, "" /* workgroup */,
  126. "" /* password */));
  127. base::RunLoop run_loop;
  128. delegate_remote_->RequestCredentials(base::BindLambdaForTesting(
  129. [&run_loop](mojom::CredentialsPtr credentials) {
  130. EXPECT_FALSE(credentials);
  131. run_loop.Quit();
  132. }));
  133. run_loop.Run();
  134. }
  135. } // namespace
  136. } // namespace smbfs