Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS 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 | |
| 5 | #include "cros-disks/smbfs_helper.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 9 | #include <base/check_op.h> |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 10 | #include <base/files/file_path.h> |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 11 | #include <base/strings/string_split.h> |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame] | 12 | #include <brillo/process/process_reaper.h> |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 13 | #include <gmock/gmock.h> |
| 14 | #include <gtest/gtest.h> |
| 15 | |
| 16 | #include "cros-disks/fuse_mounter.h" |
| 17 | #include "cros-disks/mount_options.h" |
| 18 | #include "cros-disks/platform.h" |
| 19 | #include "cros-disks/uri.h" |
| 20 | |
| 21 | using testing::_; |
| 22 | using testing::DoAll; |
| 23 | using testing::HasSubstr; |
| 24 | using testing::Return; |
| 25 | using testing::SetArgPointee; |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 26 | using testing::UnorderedElementsAre; |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 27 | |
| 28 | namespace cros_disks { |
| 29 | |
| 30 | namespace { |
| 31 | |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 32 | const base::FilePath kMountDir("/mount_point"); |
| 33 | const Uri kSomeSource("smbfs", "foobarbaz"); |
| 34 | |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 35 | std::vector<std::string> ParseOptions(const SandboxedProcess& sandbox) { |
| 36 | CHECK_EQ(2, sandbox.arguments().size()); |
| 37 | CHECK_EQ("-o", sandbox.arguments()[0]); |
| 38 | return base::SplitString(sandbox.arguments()[1], ",", |
| 39 | base::WhitespaceHandling::KEEP_WHITESPACE, |
| 40 | base::SplitResult::SPLIT_WANT_ALL); |
| 41 | } |
| 42 | |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 43 | // Mock Platform implementation for testing. |
| 44 | class MockPlatform : public Platform { |
| 45 | public: |
| 46 | MockPlatform() = default; |
| 47 | |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 48 | bool GetUserAndGroupId(const std::string& name, |
| 49 | uid_t* uid, |
| 50 | gid_t* gid) const override { |
| 51 | if (name == "fuse-smbfs") { |
| 52 | if (uid) |
| 53 | *uid = 123; |
| 54 | if (gid) |
| 55 | *gid = 456; |
| 56 | return true; |
| 57 | } |
| 58 | return false; |
| 59 | } |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } // namespace |
| 63 | |
| 64 | class SmbfsHelperTest : public ::testing::Test { |
| 65 | public: |
| 66 | SmbfsHelperTest() : helper_(&platform_, &process_reaper_) { |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | protected: |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 70 | MountErrorType ConfigureSandbox(const std::string& source, |
| 71 | std::vector<std::string>* args) { |
| 72 | FakeSandboxedProcess sandbox; |
| 73 | MountErrorType error = |
| 74 | helper_.ConfigureSandbox(source, kMountDir, {}, &sandbox); |
| 75 | if (error == MOUNT_ERROR_NONE) { |
| 76 | *args = ParseOptions(sandbox); |
| 77 | } |
| 78 | return error; |
| 79 | } |
| 80 | |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 81 | MockPlatform platform_; |
| 82 | brillo::ProcessReaper process_reaper_; |
| 83 | SmbfsHelper helper_; |
| 84 | }; |
| 85 | |
| 86 | TEST_F(SmbfsHelperTest, CreateMounter) { |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 87 | std::vector<std::string> args; |
| 88 | EXPECT_EQ(MOUNT_ERROR_NONE, ConfigureSandbox(kSomeSource.value(), &args)); |
| 89 | EXPECT_THAT( |
| 90 | args, UnorderedElementsAre("uid=1000", "gid=1001", "mojo_id=foobarbaz")); |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST_F(SmbfsHelperTest, CanMount) { |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 94 | base::FilePath name; |
| 95 | EXPECT_TRUE(helper_.CanMount("smbfs://foo", {}, &name)); |
| 96 | EXPECT_FALSE(helper_.CanMount("smbfss://foo", {}, &name)); |
| 97 | EXPECT_FALSE(helper_.CanMount("smb://foo", {}, &name)); |
| 98 | EXPECT_TRUE(helper_.CanMount("smbfs://", {}, &name)); |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } // namespace cros_disks |