blob: 19438aa4517c85eef435dce33995f1833e4c1a93 [file] [log] [blame]
Anand K Mistry110478e2019-10-29 15:24:11 +11001// 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 Fan713061e2021-03-08 15:45:12 +09009#include <base/check_op.h>
Anand K Mistry110478e2019-10-29 15:24:11 +110010#include <base/files/file_path.h>
Sergei Datsenko44612ac2020-11-30 09:35:56 +110011#include <base/strings/string_split.h>
Simon Glass2b1da092020-05-21 12:24:16 -060012#include <brillo/process/process_reaper.h>
Anand K Mistry110478e2019-10-29 15:24:11 +110013#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
21using testing::_;
22using testing::DoAll;
23using testing::HasSubstr;
24using testing::Return;
25using testing::SetArgPointee;
Sergei Datsenko44612ac2020-11-30 09:35:56 +110026using testing::UnorderedElementsAre;
Anand K Mistry110478e2019-10-29 15:24:11 +110027
28namespace cros_disks {
29
30namespace {
31
Anand K Mistry110478e2019-10-29 15:24:11 +110032const base::FilePath kMountDir("/mount_point");
33const Uri kSomeSource("smbfs", "foobarbaz");
34
Sergei Datsenko44612ac2020-11-30 09:35:56 +110035std::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 Mistry110478e2019-10-29 15:24:11 +110043// Mock Platform implementation for testing.
44class MockPlatform : public Platform {
45 public:
46 MockPlatform() = default;
47
Sergei Datsenko44612ac2020-11-30 09:35:56 +110048 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 Mistry110478e2019-10-29 15:24:11 +110060};
61
62} // namespace
63
64class SmbfsHelperTest : public ::testing::Test {
65 public:
66 SmbfsHelperTest() : helper_(&platform_, &process_reaper_) {
Anand K Mistry110478e2019-10-29 15:24:11 +110067 }
68
69 protected:
Sergei Datsenko44612ac2020-11-30 09:35:56 +110070 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 Mistry110478e2019-10-29 15:24:11 +110081 MockPlatform platform_;
82 brillo::ProcessReaper process_reaper_;
83 SmbfsHelper helper_;
84};
85
86TEST_F(SmbfsHelperTest, CreateMounter) {
Sergei Datsenko44612ac2020-11-30 09:35:56 +110087 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 Mistry110478e2019-10-29 15:24:11 +110091}
92
93TEST_F(SmbfsHelperTest, CanMount) {
Sergei Datsenko44612ac2020-11-30 09:35:56 +110094 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 Mistry110478e2019-10-29 15:24:11 +110099}
100
101} // namespace cros_disks