diff --git a/docs/reference/command-line-interface/transfer.md b/docs/reference/command-line-interface/transfer.md index de23c99c37..432073f0c0 100644 --- a/docs/reference/command-line-interface/transfer.md +++ b/docs/reference/command-line-interface/transfer.md @@ -43,6 +43,20 @@ multipass transfer --recursive ample-pigeon:dir . ```{caution}Symbolic links are not followed during recursive transfer.``` +When copying from an instance to the host, source paths can contain `*`, `?`, and character-range +wildcards. Quote the source so that your host shell passes the pattern to Multipass unchanged: + +```{code-block} text +multipass transfer 'ample-pigeon:logs/*.txt' . +``` + +Wildcard patterns do not match hidden entries unless the pattern component starts with `.` (for +example, `logs/.*.txt`), and a pattern ending in `/` matches directories only. When copying to a +host directory, multiple matches require a directory destination; writing to `-` requires exactly +one match. Wildcards in local source paths are expanded by the host shell as usual. A path that +exists literally is transferred as-is, even if it contains wildcard characters; to match a literal +`[` in a pattern, put it in a character class, as in `logs/a_[[]*].txt`. + --- The full `multipass help transfer` output explains the available options: diff --git a/include/multipass/ssh/sftp_client.h b/include/multipass/ssh/sftp_client.h index dfc91964cc..d31bc8dde4 100644 --- a/include/multipass/ssh/sftp_client.h +++ b/include/multipass/ssh/sftp_client.h @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -54,6 +55,7 @@ class SFTPClient SFTPClient(SSHSessionUPtr ssh_session); virtual bool is_remote_dir(const fs::path& path); + virtual std::vector expand_remote_path(const fs::path& path); virtual bool push(const fs::path& source_path, const fs::path& target_path, Flags flags = {}); virtual bool pull(const fs::path& source_path, const fs::path& target_path, Flags flags = {}); virtual void from_cin(std::istream& cin, const fs::path& target_path, bool make_parent); diff --git a/src/client/cli/cmd/transfer.cpp b/src/client/cli/cmd/transfer.cpp index 91035560b3..c90de99387 100644 --- a/src/client/cli/cmd/transfer.cpp +++ b/src/client/cli/cmd/transfer.cpp @@ -58,15 +58,27 @@ mp::ReturnCodeVariant cmd::Transfer::run(mp::ArgParser* parser) if (const auto args = std::get_if(&arguments); args) { auto& [sources, target] = *args; + std::vector expanded_sources; + for (auto [source, source_end] = sources.equal_range(instance_name); + source != source_end; + ++source) + { + auto matches = sftp_client->expand_remote_path(source->second); + std::move(matches.begin(), + matches.end(), + std::back_inserter(expanded_sources)); + } + std::error_code err; - if (sources.size() > 1 && !MP_FILEOPS.is_directory(target, err) && !err) + if ((sources.size() > 1 || expanded_sources.size() > 1) && + !MP_FILEOPS.is_directory(target, err) && !err) throw std::runtime_error{ fmt::format("Target {:?} is not a directory", target)}; else if (err) throw std::runtime_error{ fmt::format("Cannot access {:?}: {}", target, err.message())}; - for (auto [s, s_end] = sources.equal_range(instance_name); s != s_end; ++s) - success &= sftp_client->pull(s->second, target, flags); + for (const auto& source : expanded_sources) + success &= sftp_client->pull(source, target, flags); } if (const auto args = std::get_if(&arguments); args) @@ -85,7 +97,14 @@ mp::ReturnCodeVariant cmd::Transfer::run(mp::ArgParser* parser) flags.testFlag(SFTPClient::Flag::MakeParent)); if (const auto args = std::get_if(&arguments); args) - sftp_client->to_cout(args->source, term->cout()); + { + const auto matches = sftp_client->expand_remote_path(args->source); + if (matches.size() != 1) + throw std::runtime_error{ + fmt::format("Cannot write multiple matches to stdout for source {:?}", + args->source)}; + sftp_client->to_cout(matches.front(), term->cout()); + } } catch (const std::exception& e) { diff --git a/src/ssh/sftp_client.cpp b/src/ssh/sftp_client.cpp index 33f5513422..596a0b8559 100644 --- a/src/ssh/sftp_client.cpp +++ b/src/ssh/sftp_client.cpp @@ -27,18 +27,91 @@ #include #include +#include #include #include #include #include +#include #include +#include + constexpr int file_mode = 0664; const std::string stream_file_name{"stream_output.dat"}; const char* log_category = "sftp"; +namespace +{ +bool has_wildcards(const std::string_view value) +{ + return value.find_first_of("*?[") != std::string_view::npos; +} + +std::vector split_remote_path(const std::string_view path) +{ + std::vector components; + for (std::size_t begin = 0; begin < path.size();) + { + begin = path.find_first_not_of('/', begin); + if (begin == std::string_view::npos) + break; + + const auto end = path.find('/', begin); + components.emplace_back(path.substr(begin, end - begin)); + begin = end; + } + return components; +} + +std::string append_remote_component(const std::string_view path, const std::string_view component) +{ + if (path.empty()) + return std::string{component}; + if (path == "/") + return fmt::format("/{}", component); + return fmt::format("{}/{}", path, component); +} + +bool wildcard_matches(const std::string_view pattern, + const std::string_view name, + const QRegularExpression& expression) +{ + if (name.starts_with('.') && !pattern.starts_with('.')) + return false; + + return expression.match(QString::fromStdString(std::string{name})).hasMatch(); +} +} // namespace + namespace multipass { +namespace +{ +enum class RemoteEntryType +{ + missing, + non_directory, + directory, +}; + +RemoteEntryType remote_entry_type(sftp_session sftp, const std::string& path) +{ + if (auto attr = mp_sftp_stat(sftp, path.c_str())) + return attr->type == SSH_FILEXFER_TYPE_DIRECTORY ? RemoteEntryType::directory + : RemoteEntryType::non_directory; + + // A missing entry simply stops matching; other failures, such as permission problems, + // must not silently truncate the transfer. + if (MP_LIBSSH.sftp_get_error(sftp) != SSH_FX_NO_SUCH_FILE) + throw SFTPError{"cannot access remote path '{}': {}", + path, + MP_LIBSSH.ssh_get_error(sftp->session)}; + + return RemoteEntryType::missing; +} +} // namespace + namespace mpl = logging; SFTPSessionUPtr make_sftp_session(ssh_session session) @@ -73,10 +146,112 @@ SFTPClient::SFTPClient(SSHSessionUPtr ssh_session) bool SFTPClient::is_remote_dir(const fs::path& path) { - auto attr = mp_sftp_stat(sftp.get(), path.string().c_str()); + const auto remote_path = path.generic_string(); + auto attr = mp_sftp_stat(sftp.get(), remote_path.c_str()); return attr && attr->type == SSH_FILEXFER_TYPE_DIRECTORY; } +std::vector SFTPClient::expand_remote_path(const fs::path& path) +{ + const auto remote_path = path.generic_string(); + + // A literal path takes precedence, so names containing wildcard characters (such as + // "a_[0].txt") can still be transferred without escaping the brackets. + if (!has_wildcards(remote_path) || mp_sftp_stat(sftp.get(), remote_path.c_str())) + return {path}; + + auto candidates = std::vector{remote_path.starts_with('/') ? "/" : ""}; + auto expanded_pattern = false; + + for (const auto& component : split_remote_path(remote_path)) + { + if (!has_wildcards(component)) + { + std::transform(candidates.begin(), + candidates.end(), + candidates.begin(), + [&component](const auto& candidate) { + return append_remote_component(candidate, component); + }); + + if (expanded_pattern) + { + std::erase_if(candidates, [this](const auto& candidate) { + return remote_entry_type(sftp.get(), candidate) == RemoteEntryType::missing; + }); + if (candidates.empty()) + throw SFTPError{"no matches found: {}", path}; + } + continue; + } + + std::vector matches; + const QRegularExpression expression{ + QRegularExpression::wildcardToRegularExpression(QString::fromStdString(component))}; + for (const auto& candidate : candidates) + { + const auto directory_path = candidate.empty() ? "." : candidate; + auto directory = mp_sftp_opendir(sftp.get(), directory_path.c_str()); + if (!directory) + { + // Once a wildcard has been expanded, a candidate may be a regular file that + // matched it; descending into it then legitimately fails. OpenSSH reports + // ENOTDIR as SSH_FX_NO_SUCH_FILE, so only that error is treated as a + // non-matching branch. Permission or connection failures must fail the + // transfer instead of silently dropping matches. + // TODO: distinguish a vanished entry from a non-directory; SFTP v3 reports + // both as SSH_FX_NO_SUCH_FILE. + if (expanded_pattern && MP_LIBSSH.sftp_get_error(sftp.get()) == SSH_FX_NO_SUCH_FILE) + continue; + + throw SFTPError{"cannot open remote directory '{}': {}", + directory_path, + MP_LIBSSH.ssh_get_error(sftp->session)}; + } + + while (auto entry = mp_sftp_readdir(sftp.get(), directory.get())) + { + const std::string_view name{entry->name}; + if (name != "." && name != ".." && wildcard_matches(component, name, expression)) + matches.emplace_back(append_remote_component(candidate, name)); + } + + if (!MP_LIBSSH.sftp_dir_eof(directory.get())) + throw SFTPError{"cannot read remote directory '{}': {}", + directory_path, + MP_LIBSSH.ssh_get_error(sftp->session)}; + } + + if (matches.empty()) + throw SFTPError{"no matches found: {}", path}; + + std::sort(matches.begin(), matches.end()); + matches.erase(std::unique(matches.begin(), matches.end()), matches.end()); + candidates = std::move(matches); + expanded_pattern = true; + } + + // Splitting the pattern drops a trailing slash, so restore its meaning here: a source + // ending in '/' only matches directories. + if (remote_path.ends_with('/')) + { + std::erase_if(candidates, [this](const auto& candidate) { + return remote_entry_type(sftp.get(), candidate) != RemoteEntryType::directory; + }); + + if (candidates.empty()) + throw SFTPError{"no matches found: {}", path}; + } + + std::vector paths; + paths.reserve(candidates.size()); + std::transform(candidates.begin(), + candidates.end(), + std::back_inserter(paths), + [](auto& candidate) { return fs::path{std::move(candidate)}; }); + return paths; +} + bool SFTPClient::push(const fs::path& source_path, const fs::path& target_path, const Flags flags) try { @@ -118,9 +293,8 @@ catch (const SFTPError& e) bool SFTPClient::pull(const fs::path& source_path, const fs::path& target_path, const Flags flags) try { - auto source = source_path.string(); - utils::trim_end(source, - [](char ch) { return ch == '/' || ch == fs::path::preferred_separator; }); + auto source = source_path.generic_string(); + utils::trim_end(source, [](char ch) { return ch == '/'; }); if (is_remote_dir(source_path)) { @@ -187,7 +361,8 @@ void SFTPClient::pull_file(const fs::path& source_path, const fs::path& target_p if (!pre_existed) { - auto source_perms = mp_sftp_stat(sftp.get(), source_path.string().c_str())->permissions; + const auto remote_source = source_path.generic_string(); + auto source_perms = mp_sftp_stat(sftp.get(), remote_source.c_str())->permissions; if (!MP_PLATFORM.set_permissions(target_path, static_cast(source_perms))) throw SFTPError{"cannot set permissions for local file {}", target_path}; } @@ -421,7 +596,8 @@ void SFTPClient::do_push_file(std::istream& source, const fs::path& target_path) void SFTPClient::do_pull_file(const fs::path& source_path, std::ostream& target) { - auto remote_file = mp_sftp_open(sftp.get(), source_path.string().c_str(), O_RDONLY, 0); + const auto remote_path = source_path.generic_string(); + auto remote_file = mp_sftp_open(sftp.get(), remote_path.c_str(), O_RDONLY, 0); if (!remote_file) throw SFTPError{"cannot open remote file {}: {}", source_path, diff --git a/src/ssh/sftp_dir_iterator.cpp b/src/ssh/sftp_dir_iterator.cpp index d29f78dedd..272f6a2175 100644 --- a/src/ssh/sftp_dir_iterator.cpp +++ b/src/ssh/sftp_dir_iterator.cpp @@ -37,7 +37,7 @@ void SFTPDirIterator::push_dir(const std::string& path) SFTPDirIterator::SFTPDirIterator(sftp_session sftp, const fs::path& path) : sftp(sftp) { - push_dir(path.string()); + push_dir(path.generic_string()); next(); } diff --git a/tests/cli/cli_transfer_test.py b/tests/cli/cli_transfer_test.py index d97d593aca..585ca04523 100644 --- a/tests/cli/cli_transfer_test.py +++ b/tests/cli/cli_transfer_test.py @@ -46,6 +46,26 @@ def test_transfer_single_file(self, instance): assert pull_file.exists() assert pull_file.read_text() == "hello from the other side" + def test_transfer_remote_wildcard(self, instance): + """Transfer files matching a wildcard from the guest to the host.""" + with TempDirectory() as tmp: + source = tmp / "wildcard-source" + source.mkdir() + (source / "first.txt").write_text("first") + (source / "second.txt").write_text("second") + (source / "ignored.log").write_text("ignored") + + assert multipass("transfer", "--recursive", str(source), f"{instance}:") + + target = tmp / "wildcard-target" + target.mkdir() + assert multipass( + "transfer", f"{instance}:wildcard-source/*.txt", str(target) + ) + assert (target / "first.txt").read_text() == "first" + assert (target / "second.txt").read_text() == "second" + assert not (target / "ignored.log").exists() + def test_transfer_single_file_create_parents(self, instance): """Transfer a single file from the host to guest where the target is a nested folder structure.""" diff --git a/tests/unit/c_mock_defines.cmake b/tests/unit/c_mock_defines.cmake index 946371aefa..61a87f476b 100644 --- a/tests/unit/c_mock_defines.cmake +++ b/tests/unit/c_mock_defines.cmake @@ -81,6 +81,7 @@ add_c_mocks( sftp_stat sftp_lstat sftp_opendir + sftp_closedir sftp_readdir sftp_readlink sftp_mkdir diff --git a/tests/unit/mock_sftp.cpp b/tests/unit/mock_sftp.cpp index ea3a8b6627..154752019e 100644 --- a/tests/unit/mock_sftp.cpp +++ b/tests/unit/mock_sftp.cpp @@ -29,6 +29,7 @@ IMPL_MOCK_DEFAULT(1, sftp_close); IMPL_MOCK_DEFAULT(2, sftp_stat); IMPL_MOCK_DEFAULT(2, sftp_lstat); IMPL_MOCK_DEFAULT(2, sftp_opendir); +IMPL_MOCK_DEFAULT(1, sftp_closedir); IMPL_MOCK_DEFAULT(2, sftp_readdir); IMPL_MOCK_DEFAULT(2, sftp_readlink); IMPL_MOCK_DEFAULT(3, sftp_mkdir); diff --git a/tests/unit/mock_sftp.h b/tests/unit/mock_sftp.h index a2346b12ad..fae1ee6702 100644 --- a/tests/unit/mock_sftp.h +++ b/tests/unit/mock_sftp.h @@ -32,6 +32,7 @@ DECL_MOCK(sftp_close); DECL_MOCK(sftp_stat); DECL_MOCK(sftp_lstat); DECL_MOCK(sftp_opendir); +DECL_MOCK(sftp_closedir); DECL_MOCK(sftp_readdir); DECL_MOCK(sftp_readlink); DECL_MOCK(sftp_mkdir); diff --git a/tests/unit/mock_sftp_client.h b/tests/unit/mock_sftp_client.h index ab1d337685..6769635d0e 100644 --- a/tests/unit/mock_sftp_client.h +++ b/tests/unit/mock_sftp_client.h @@ -25,6 +25,7 @@ namespace multipass::test struct MockSFTPClient : public SFTPClient { MOCK_METHOD(bool, is_remote_dir, (const fs::path& path), (override)); + MOCK_METHOD(std::vector, expand_remote_path, (const fs::path& path), (override)); MOCK_METHOD(bool, push, (const fs::path& source_path, const fs::path& target_path, Flags flags), diff --git a/tests/unit/test_cli_client.cpp b/tests/unit/test_cli_client.cpp index fe3c6d6fe4..4db7cb4647 100644 --- a/tests/unit/test_cli_client.cpp +++ b/tests/unit/test_cli_client.cpp @@ -63,6 +63,7 @@ namespace mp = multipass; namespace mcp = multipass::cli::platform; namespace mpt = multipass::test; namespace mpu = multipass::utils; +namespace fs = mp::fs; using namespace testing; namespace @@ -675,6 +676,8 @@ TEST_F(Client, transferCmdInstanceSourceLocalTarget) EXPECT_CALL(*mocked_sftp_utils, make_SFTPClient) .WillOnce(Return(std::move(mocked_sftp_client))); + EXPECT_CALL(*mocked_sftp_client_p, expand_remote_path(fs::path{"foo"})) + .WillOnce(Return(std::vector{"foo"})); EXPECT_CALL(*mocked_sftp_client_p, pull).WillOnce(Return(true)); EXPECT_CALL(mock_daemon, ssh_info) .WillOnce([](auto, grpc::ServerReaderWriter* server) { @@ -686,13 +689,72 @@ TEST_F(Client, transferCmdInstanceSourceLocalTarget) EXPECT_EQ(send_command({"transfer", "test-vm:foo", "bar"}), mp::ReturnCode::Ok); } +TEST_F(Client, transferCmdExpandsRemoteWildcard) +{ + auto [mocked_file_ops, mocked_file_ops_guard] = mpt::MockFileOps::inject(); + auto [mocked_sftp_utils, mocked_sftp_utils_guard] = mpt::MockSFTPUtils::inject(); + auto mocked_sftp_client = std::make_unique(); + auto mocked_sftp_client_p = mocked_sftp_client.get(); + + EXPECT_CALL(*mocked_sftp_utils, make_SFTPClient) + .WillOnce(Return(std::move(mocked_sftp_client))); + EXPECT_CALL(*mocked_sftp_client_p, expand_remote_path(fs::path{"dir/*.txt"})) + .WillOnce(Return(std::vector{"dir/first.txt", "dir/second.txt"})); + EXPECT_CALL(*mocked_file_ops, is_directory(fs::path{"target"}, _)).WillOnce(Return(true)); + EXPECT_CALL(*mocked_sftp_client_p, pull(fs::path{"dir/first.txt"}, fs::path{"target"}, _)) + .WillOnce(Return(true)); + EXPECT_CALL(*mocked_sftp_client_p, pull(fs::path{"dir/second.txt"}, fs::path{"target"}, _)) + .WillOnce(Return(true)); + EXPECT_CALL(mock_daemon, ssh_info) + .WillOnce([](auto, grpc::ServerReaderWriter* server) { + mp::SSHInfoReply reply; + reply.mutable_ssh_info()->insert({"test-vm", mp::SSHInfo{}}); + server->Write(reply); + return grpc::Status{}; + }); + + EXPECT_EQ(send_command({"transfer", "test-vm:dir/*.txt", "target"}), mp::ReturnCode::Ok); +} + +TEST_F(Client, transferCmdRemoteWildcardMatchesRequireDirectoryTarget) +{ + auto [mocked_file_ops, mocked_file_ops_guard] = mpt::MockFileOps::inject(); + auto [mocked_sftp_utils, mocked_sftp_utils_guard] = mpt::MockSFTPUtils::inject(); + auto mocked_sftp_client = std::make_unique(); + auto mocked_sftp_client_p = mocked_sftp_client.get(); + + EXPECT_CALL(*mocked_sftp_utils, make_SFTPClient) + .WillOnce(Return(std::move(mocked_sftp_client))); + EXPECT_CALL(*mocked_sftp_client_p, expand_remote_path(fs::path{"dir/*.txt"})) + .WillOnce(Return(std::vector{"dir/first.txt", "dir/second.txt"})); + EXPECT_CALL(*mocked_file_ops, is_directory(fs::path{"target"}, _)).WillOnce(Return(false)); + EXPECT_CALL(mock_daemon, ssh_info) + .WillOnce([](auto, grpc::ServerReaderWriter* server) { + mp::SSHInfoReply reply; + reply.mutable_ssh_info()->insert({"test-vm", mp::SSHInfo{}}); + server->Write(reply); + return grpc::Status{}; + }); + + std::stringstream err; + EXPECT_EQ(send_command({"transfer", "test-vm:dir/*.txt", "target"}, trash_stream, err), + mp::ReturnCode::CommandFail); + EXPECT_THAT(err.str(), HasSubstr("Target \"target\" is not a directory")); +} + TEST_F(Client, transferCmdInstanceSourcesLocalTargetNotDir) { auto [mocked_file_ops, mocked_file_ops_guard] = mpt::MockFileOps::inject(); auto [mocked_sftp_utils, mocked_sftp_utils_guard] = mpt::MockSFTPUtils::inject(); + auto mocked_sftp_client = std::make_unique(); + auto mocked_sftp_client_p = mocked_sftp_client.get(); EXPECT_CALL(*mocked_sftp_utils, make_SFTPClient) - .WillOnce(Return(std::make_unique())); + .WillOnce(Return(std::move(mocked_sftp_client))); + EXPECT_CALL(*mocked_sftp_client_p, expand_remote_path(fs::path{"foo"})) + .WillOnce(Return(std::vector{"foo"})); + EXPECT_CALL(*mocked_sftp_client_p, expand_remote_path(fs::path{"baz"})) + .WillOnce(Return(std::vector{"baz"})); EXPECT_CALL(*mocked_file_ops, is_directory(_, _)).WillOnce(Return(false)); EXPECT_CALL(mock_daemon, ssh_info) .WillOnce([](auto, grpc::ServerReaderWriter* server) { @@ -712,9 +774,15 @@ TEST_F(Client, transferCmdInstanceSourcesLocalTargetCannotAccess) { auto [mocked_file_ops, mocked_file_ops_guard] = mpt::MockFileOps::inject(); auto [mocked_sftp_utils, mocked_sftp_utils_guard] = mpt::MockSFTPUtils::inject(); + auto mocked_sftp_client = std::make_unique(); + auto mocked_sftp_client_p = mocked_sftp_client.get(); EXPECT_CALL(*mocked_sftp_utils, make_SFTPClient) - .WillOnce(Return(std::make_unique())); + .WillOnce(Return(std::move(mocked_sftp_client))); + EXPECT_CALL(*mocked_sftp_client_p, expand_remote_path(fs::path{"foo"})) + .WillOnce(Return(std::vector{"foo"})); + EXPECT_CALL(*mocked_sftp_client_p, expand_remote_path(fs::path{"baz"})) + .WillOnce(Return(std::vector{"baz"})); auto err = std::make_error_code(std::errc::permission_denied); EXPECT_CALL(*mocked_file_ops, is_directory(_, _)).WillOnce([&](auto, std::error_code& e) { e = err; @@ -840,6 +908,8 @@ TEST_F(Client, transferCmdStdoutGoodSourceOk) EXPECT_CALL(*mocked_sftp_utils, make_SFTPClient) .WillOnce(Return(std::move(mocked_sftp_client))); + EXPECT_CALL(*mocked_sftp_client_p, expand_remote_path(fs::path{"foo"})) + .WillOnce(Return(std::vector{"foo"})); EXPECT_CALL(*mocked_sftp_client_p, to_cout); EXPECT_CALL(mock_daemon, ssh_info) .WillOnce([](auto, grpc::ServerReaderWriter* server) { @@ -852,6 +922,50 @@ TEST_F(Client, transferCmdStdoutGoodSourceOk) EXPECT_EQ(send_command({"transfer", "test-vm1:foo", "-"}), mp::ReturnCode::Ok); } +TEST_F(Client, transferCmdStdoutExpandsSingleWildcardMatch) +{ + auto [mocked_sftp_utils, mocked_sftp_utils_guard] = mpt::MockSFTPUtils::inject(); + auto mocked_sftp_client = std::make_unique(); + auto mocked_sftp_client_p = mocked_sftp_client.get(); + + EXPECT_CALL(*mocked_sftp_utils, make_SFTPClient) + .WillOnce(Return(std::move(mocked_sftp_client))); + EXPECT_CALL(*mocked_sftp_client_p, expand_remote_path(fs::path{"foo/*.txt"})) + .WillOnce(Return(std::vector{"foo/only.txt"})); + EXPECT_CALL(*mocked_sftp_client_p, to_cout(fs::path{"foo/only.txt"}, _)); + EXPECT_CALL(mock_daemon, ssh_info) + .WillOnce([](auto, grpc::ServerReaderWriter* server) { + mp::SSHInfoReply reply; + reply.mutable_ssh_info()->insert({"test-vm", mp::SSHInfo{}}); + server->Write(reply); + return grpc::Status{}; + }); + + EXPECT_EQ(send_command({"transfer", "test-vm1:foo/*.txt", "-"}), mp::ReturnCode::Ok); +} + +TEST_F(Client, transferCmdStdoutRejectsMultipleWildcardMatches) +{ + auto [mocked_sftp_utils, mocked_sftp_utils_guard] = mpt::MockSFTPUtils::inject(); + auto mocked_sftp_client = std::make_unique(); + auto mocked_sftp_client_p = mocked_sftp_client.get(); + + EXPECT_CALL(*mocked_sftp_utils, make_SFTPClient) + .WillOnce(Return(std::move(mocked_sftp_client))); + EXPECT_CALL(*mocked_sftp_client_p, expand_remote_path(fs::path{"foo/*.txt"})) + .WillOnce(Return(std::vector{"foo/first.txt", "foo/second.txt"})); + EXPECT_CALL(*mocked_sftp_client_p, to_cout).Times(0); + EXPECT_CALL(mock_daemon, ssh_info) + .WillOnce([](auto, grpc::ServerReaderWriter* server) { + mp::SSHInfoReply reply; + reply.mutable_ssh_info()->insert({"test-vm", mp::SSHInfo{}}); + server->Write(reply); + return grpc::Status{}; + }); + + EXPECT_EQ(send_command({"transfer", "test-vm1:foo/*.txt", "-"}), mp::ReturnCode::CommandFail); +} + TEST_F(Client, transferCmdStdoutBadSourceFails) { EXPECT_THAT(send_command({"transfer", "foo", "-"}), Eq(mp::ReturnCode::CommandLineError)); diff --git a/tests/unit/test_sftp_client.cpp b/tests/unit/test_sftp_client.cpp index a8e555d5cd..cc9b03a288 100644 --- a/tests/unit/test_sftp_client.cpp +++ b/tests/unit/test_sftp_client.cpp @@ -56,7 +56,7 @@ sftp_attributes get_dummy_sftp_attr(uint8_t type = SSH_FILEXFER_TYPE_REGULAR, auto attr = static_cast( calloc(1, sizeof(struct sftp_attributes_struct))); attr->type = type; - attr->name = strdup(name.string().c_str()); + attr->name = strdup(name.generic_string().c_str()); attr->permissions = perms; return attr; } @@ -75,6 +75,13 @@ fs::path canon(const fs::path& path) return fs::weakly_canonical(path); } +sftp_dir get_dummy_sftp_dir(const fs::path& name) +{ + auto dir = static_cast(calloc(1, sizeof(struct sftp_dir_struct))); + dir->name = strdup(name.generic_string().c_str()); + return dir; +} + struct SFTPClient : public testing::Test { SFTPClient() @@ -85,10 +92,16 @@ struct SFTPClient : public testing::Test return sftp; }}, free_sftp{mock_sftp_free, [](sftp_session sftp) { std::free(sftp); }}, - close_sftp{mock_sftp_close, [](sftp_file file) { + close_sftp{mock_sftp_close, + [](sftp_file file) { std::free(file); return SSH_OK; - }} + }}, + close_sftp_dir{mock_sftp_closedir, [](sftp_dir dir) { + std::free(dir->name); + std::free(dir); + return SSH_OK; + }} { } @@ -107,6 +120,7 @@ struct SFTPClient : public testing::Test MockScope sftp_new; MockScope free_sftp; MockScope close_sftp; + MockScope close_sftp_dir; sftp_limits_struct limits{32768, 32768, 32768, 0}; @@ -165,6 +179,395 @@ TEST_F(SFTPClient, isDir) EXPECT_FALSE(sftp_client.is_remote_dir("not/a/directory")); } +TEST_F(SFTPClient, leavesRemotePathWithoutWildcardsUnchanged) +{ + REPLACE_SFTP_INIT(); + + auto sftp_client = make_sftp_client(); + + EXPECT_THAT(sftp_client.expand_remote_path("dir/file.txt"), + ElementsAre(fs::path{"dir/file.txt"})); +} + +TEST_F(SFTPClient, expandsRemoteWildcard) +{ + REPLACE_SFTP_INIT(); + + std::vector entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "third.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, ".hidden.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "second.log"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "first.txt"), + nullptr, + }; + REPLACE(sftp_stat, [](auto...) { return nullptr; }); + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + auto read_dir = [&, index = 0](auto...) mutable { return entries[index++]; }; + REPLACE(sftp_readdir, read_dir); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + + auto sftp_client = make_sftp_client(); + + EXPECT_THAT(sftp_client.expand_remote_path("dir/*.txt"), + ElementsAre(fs::path{"dir/first.txt"}, fs::path{"dir/third.txt"})); +} + +TEST_F(SFTPClient, expandsQuestionMarkWildcard) +{ + REPLACE_SFTP_INIT(); + + std::vector entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "fileA.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "file1.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "file10.txt"), + nullptr, + }; + REPLACE(sftp_stat, [](auto...) { return nullptr; }); + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + auto read_dir = [&, index = 0](auto...) mutable { return entries[index++]; }; + REPLACE(sftp_readdir, read_dir); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + + auto sftp_client = make_sftp_client(); + + EXPECT_THAT(sftp_client.expand_remote_path("dir/file?.txt"), + ElementsAre(fs::path{"dir/file1.txt"}, fs::path{"dir/fileA.txt"})); +} + +TEST_F(SFTPClient, expandsCharacterRangeWildcard) +{ + REPLACE_SFTP_INIT(); + + std::vector entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "c.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "a.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "d.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "b.txt"), + nullptr, + }; + REPLACE(sftp_stat, [](auto...) { return nullptr; }); + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + auto read_dir = [&, index = 0](auto...) mutable { return entries[index++]; }; + REPLACE(sftp_readdir, read_dir); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + + auto sftp_client = make_sftp_client(); + + EXPECT_THAT(sftp_client.expand_remote_path("dir/[a-c].txt"), + ElementsAre(fs::path{"dir/a.txt"}, fs::path{"dir/b.txt"}, fs::path{"dir/c.txt"})); +} + +TEST_F(SFTPClient, keepsExistingLiteralPathContainingWildcardCharacters) +{ + REPLACE_SFTP_INIT(); + + REPLACE(sftp_stat, [](auto, auto path) { + return std::string_view{path} == "dir/a_[0].txt" + ? get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, path) + : nullptr; + }); + REPLACE(sftp_opendir, [](auto...) { return nullptr; }); + + auto sftp_client = make_sftp_client(); + + EXPECT_THAT(sftp_client.expand_remote_path("dir/a_[0].txt"), + ElementsAre(fs::path{"dir/a_[0].txt"})); +} + +TEST_F(SFTPClient, expandsEscapedLiteralBracketWildcard) +{ + REPLACE_SFTP_INIT(); + + std::vector entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "a_[0].txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "a_[*].txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "a_0.txt"), + nullptr, + }; + REPLACE(sftp_stat, [](auto...) { return nullptr; }); + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + auto read_dir = [&, index = 0](auto...) mutable { return entries[index++]; }; + REPLACE(sftp_readdir, read_dir); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + + auto sftp_client = make_sftp_client(); + + EXPECT_THAT(sftp_client.expand_remote_path("dir/a_[[]*].txt"), + ElementsAre(fs::path{"dir/a_[*].txt"}, fs::path{"dir/a_[0].txt"})); +} + +TEST_F(SFTPClient, expandsExplicitDotPrefixedWildcard) +{ + REPLACE_SFTP_INIT(); + + std::vector entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "visible.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, ".hidden.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, ".hidden.log"), + nullptr, + }; + REPLACE(sftp_stat, [](auto...) { return nullptr; }); + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + auto read_dir = [&, index = 0](auto...) mutable { return entries[index++]; }; + REPLACE(sftp_readdir, read_dir); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + + auto sftp_client = make_sftp_client(); + + EXPECT_THAT(sftp_client.expand_remote_path("dir/.*.txt"), + ElementsAre(fs::path{"dir/.hidden.txt"})); +} + +TEST_F(SFTPClient, expandsWildcardsAcrossRemotePathComponents) +{ + REPLACE_SFTP_INIT(); + + auto root_index = 0; + auto first_release_index = 0; + auto second_release_index = 0; + std::vector root_entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "release-b"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "release-a"), + nullptr, + }; + std::vector first_release_entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "first.txt"), + nullptr, + }; + std::vector second_release_entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "second.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "ignored.log"), + nullptr, + }; + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + REPLACE(sftp_readdir, [&](auto, auto directory) -> sftp_attributes { + const std::string_view path{directory->name}; + if (path == ".") + return root_entries[root_index++]; + if (path == "release-a/logs") + return first_release_entries[first_release_index++]; + + return second_release_entries[second_release_index++]; + }); + REPLACE(sftp_stat, [](auto, auto path) { + return std::string_view{path} == "release-*/logs/*.txt" + ? nullptr + : get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, path); + }); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + + auto sftp_client = make_sftp_client(); + + EXPECT_THAT( + sftp_client.expand_remote_path("release-*/logs/*.txt"), + ElementsAre(fs::path{"release-a/logs/first.txt"}, fs::path{"release-b/logs/second.txt"})); +} + +TEST_F(SFTPClient, throwsWhenWildcardExpansionCannotOpenDirectory) +{ + REPLACE_SFTP_INIT(); + + auto root_index = 0; + std::vector root_entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "release-b"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "release-a"), + nullptr, + }; + REPLACE(sftp_opendir, [](auto, auto path) -> sftp_dir { + if (std::string_view{path} == "release-b/logs") + return nullptr; + + return get_dummy_sftp_dir(path); + }); + REPLACE(sftp_readdir, [&](auto, auto directory) -> sftp_attributes { + return std::string_view{directory->name} == "." ? root_entries[root_index++] : nullptr; + }); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + REPLACE(sftp_stat, [](auto, auto path) { + return std::string_view{path} == "release-*/logs/*.txt" + ? nullptr + : get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, path); + }); + REPLACE(sftp_get_error, [](auto...) { return SSH_FX_PERMISSION_DENIED; }); + auto err = "SFTP server: Permission denied"; + REPLACE(ssh_get_error, [&](auto...) { return err; }); + + auto sftp_client = make_sftp_client(); + + MP_EXPECT_THROW_THAT( + sftp_client.expand_remote_path("release-*/logs/*.txt"), + mp::SFTPError, + mpt::match_what( + StrEq(fmt::format("cannot open remote directory 'release-b/logs': {}", err)))); +} + +TEST_F(SFTPClient, skipsNonDirectoryCandidatesAfterWildcard) +{ + REPLACE_SFTP_INIT(); + + auto root_index = 0; + std::vector root_entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "release-b"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "release-a"), + nullptr, + }; + auto first_entry_returned = false; + REPLACE(sftp_opendir, [](auto, auto path) -> sftp_dir { + // "release-b/logs" is a regular file, so descending into it is not a match. + return std::string_view{path} == "release-b/logs" ? nullptr : get_dummy_sftp_dir(path); + }); + REPLACE(sftp_readdir, [&](auto, auto directory) -> sftp_attributes { + const std::string_view path{directory->name}; + if (path == ".") + return root_entries[root_index++]; + if (path == "release-a/logs" && !first_entry_returned) + { + first_entry_returned = true; + return get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "first.txt"); + } + + return nullptr; + }); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + REPLACE(sftp_stat, [](auto, auto path) { + return std::string_view{path} == "release-*/logs/*.txt" + ? nullptr + : get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, path); + }); + REPLACE(sftp_get_error, [](auto...) { return SSH_FX_NO_SUCH_FILE; }); + + auto sftp_client = make_sftp_client(); + + EXPECT_THAT(sftp_client.expand_remote_path("release-*/logs/*.txt"), + ElementsAre(fs::path{"release-a/logs/first.txt"})); +} + +TEST_F(SFTPClient, throwsWhenWildcardPathComponentCannotBeAccessed) +{ + REPLACE_SFTP_INIT(); + + auto root_index = 0; + std::vector root_entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "release-b"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "release-a"), + nullptr, + }; + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + REPLACE(sftp_readdir, [&](auto, auto directory) -> sftp_attributes { + return std::string_view{directory->name} == "." ? root_entries[root_index++] : nullptr; + }); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + REPLACE(sftp_stat, [](auto, auto path) -> sftp_attributes { + const std::string_view remote_path{path}; + if (remote_path == "release-*/logs/*.txt" || remote_path == "release-b/logs") + return nullptr; + + return get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, path); + }); + REPLACE(sftp_get_error, [](auto...) { return SSH_FX_PERMISSION_DENIED; }); + auto err = "SFTP server: Permission denied"; + REPLACE(ssh_get_error, [&](auto...) { return err; }); + + auto sftp_client = make_sftp_client(); + + MP_EXPECT_THROW_THAT( + sftp_client.expand_remote_path("release-*/logs/*.txt"), + mp::SFTPError, + mpt::match_what(StrEq(fmt::format("cannot access remote path 'release-b/logs': {}", err)))); +} + +TEST_F(SFTPClient, trailingSlashPatternOnlyMatchesDirectories) +{ + REPLACE_SFTP_INIT(); + + std::vector entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "ignored.txt"), + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "subdir"), + nullptr, + }; + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + auto read_dir = [&, index = 0](auto...) mutable { return entries[index++]; }; + REPLACE(sftp_readdir, read_dir); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + REPLACE(sftp_stat, [](auto, auto path) { + const std::string_view remote_path{path}; + return remote_path == "dir/*/" + ? nullptr + : get_dummy_sftp_attr(remote_path == "dir/subdir" ? SSH_FILEXFER_TYPE_DIRECTORY + : SSH_FILEXFER_TYPE_REGULAR, + path); + }); + + auto sftp_client = make_sftp_client(); + + EXPECT_THAT(sftp_client.expand_remote_path("dir/*/"), ElementsAre(fs::path{"dir/subdir"})); +} + +TEST_F(SFTPClient, throwsWhenTrailingSlashPatternMatchesNoDirectories) +{ + REPLACE_SFTP_INIT(); + + std::vector entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "ignored.txt"), + nullptr, + }; + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + auto read_dir = [&, index = 0](auto...) mutable { return entries[index++]; }; + REPLACE(sftp_readdir, read_dir); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + REPLACE(sftp_stat, [](auto, auto path) { + return std::string_view{path} == "dir/*/" + ? nullptr + : get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, path); + }); + + auto sftp_client = make_sftp_client(); + + MP_EXPECT_THROW_THAT(sftp_client.expand_remote_path("dir/*/"), + mp::SFTPError, + mpt::match_what(StrEq("no matches found: dir/*/"))); +} + +TEST_F(SFTPClient, throwsWhenTrailingSlashPatternCannotAccessCandidate) +{ + REPLACE_SFTP_INIT(); + + std::vector entries{ + get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "subdir"), + nullptr, + }; + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + auto read_dir = [&, index = 0](auto...) mutable { return entries[index++]; }; + REPLACE(sftp_readdir, read_dir); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + REPLACE(sftp_stat, [](auto...) { return nullptr; }); + REPLACE(sftp_get_error, [](auto...) { return SSH_FX_PERMISSION_DENIED; }); + auto err = "SFTP server: Permission denied"; + REPLACE(ssh_get_error, [&](auto...) { return err; }); + + auto sftp_client = make_sftp_client(); + + MP_EXPECT_THROW_THAT( + sftp_client.expand_remote_path("dir/*/"), + mp::SFTPError, + mpt::match_what(StrEq(fmt::format("cannot access remote path 'dir/subdir': {}", err)))); +} + +TEST_F(SFTPClient, throwsWhenRemoteWildcardHasNoMatches) +{ + REPLACE_SFTP_INIT(); + REPLACE(sftp_stat, [](auto...) { return nullptr; }); + REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); }); + REPLACE(sftp_readdir, [](auto...) { return nullptr; }); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + + auto sftp_client = make_sftp_client(); + + MP_EXPECT_THROW_THAT(sftp_client.expand_remote_path("dir/*.txt"), + mp::SFTPError, + mpt::match_what(StrEq("no matches found: dir/*.txt"))); +} + TEST_F(SFTPClient, pushFileSuccess) { std::string test_data = "test_data"; @@ -324,6 +727,8 @@ TEST_F(SFTPClient, pushFileCannotSetPerms) TEST_F(SFTPClient, pullFileSuccess) { std::string test_data = "test_data"; + std::string opened_remote_path; + std::vector remote_stat_paths; REPLACE_SFTP_INIT(); EXPECT_CALL(*mock_sftp_utils, get_local_file_target(source_path, target_path, _)) @@ -333,7 +738,10 @@ TEST_F(SFTPClient, pullFileSuccess) EXPECT_CALL(*mock_file_ops, open_write(target_path, _)) .WillOnce(Return(std::make_unique(test_file.rdbuf()))); EXPECT_CALL(*mock_file_ops, exists(target_path, _)).WillOnce(Return(false)); - REPLACE(sftp_open, [](auto sftp, auto...) { return get_dummy_sftp_file(sftp); }); + REPLACE(sftp_open, [&](auto sftp, auto path, auto...) { + opened_remote_path = path; + return get_dummy_sftp_file(sftp); + }); auto mocked_sftp_read = [&, read = false](auto, void* data, auto) mutable { strcpy((char*)data, test_data.c_str()); @@ -342,8 +750,10 @@ TEST_F(SFTPClient, pullFileSuccess) REPLACE(sftp_read, mocked_sftp_read); mode_t perms = 0777; - REPLACE(sftp_stat, - [&](auto...) { return get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "", perms); }); + REPLACE(sftp_stat, [&](auto, auto path) { + remote_stat_paths.emplace_back(path); + return get_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, "", perms); + }); std::filesystem::perms written_perms; EXPECT_CALL(mock_platform, set_permissions(target_path, static_cast(perms), _)) @@ -357,6 +767,9 @@ TEST_F(SFTPClient, pullFileSuccess) EXPECT_TRUE(sftp_client.pull(source_path, target_path)); EXPECT_EQ(test_data, test_file.str()); EXPECT_EQ(static_cast(perms), written_perms); + EXPECT_EQ(source_path.generic_string(), opened_remote_path); + EXPECT_THAT(remote_stat_paths, + ElementsAre(source_path.generic_string(), source_path.generic_string())); } TEST_F(SFTPClient, pullFileKeepsExistingTargetPermissions) @@ -934,7 +1347,7 @@ TEST_F(SFTPClient, pullDirRejectsEscapingEntry) EXPECT_CALL(*mock_sftp_utils, make_SFTPDirIterator(_, source_path)) .WillOnce(Return(std::move(iter))); EXPECT_CALL(*iter_p, hasNext).WillOnce(Return(true)).WillRepeatedly(Return(false)); - const auto entry_name = (source_path / "../../../evil").string(); + const auto entry_name = (source_path / "../../../evil").generic_string(); EXPECT_CALL(*iter_p, next) .WillOnce(Return(make_unique_dummy_sftp_attr(SSH_FILEXFER_TYPE_REGULAR, entry_name))); diff --git a/tests/unit/test_sftp_dir_iterator.cpp b/tests/unit/test_sftp_dir_iterator.cpp index 37f47436f4..44cd519e58 100644 --- a/tests/unit/test_sftp_dir_iterator.cpp +++ b/tests/unit/test_sftp_dir_iterator.cpp @@ -101,6 +101,23 @@ TEST(SFTPDirIterator, success) "dir/dir3/file6")); } +TEST(SFTPDirIterator, opensNestedRemotePathsWithForwardSlashes) +{ + const auto remote_path = mp::fs::path{"release-a/logs"}; + std::string opened_remote_path; + + REPLACE(sftp_opendir, [&](auto, auto path) { + opened_remote_path = path; + return get_dummy_dir("release-a/logs"); + }); + REPLACE(sftp_readdir, [](auto...) { return nullptr; }); + REPLACE(sftp_dir_eof, [](auto...) { return true; }); + + mp::SFTPDirIterator iter{nullptr, remote_path}; + + EXPECT_EQ(remote_path.generic_string(), opened_remote_path); +} + TEST(SFTPDirIterator, failOpendir) { REPLACE(sftp_opendir, [](auto...) { return nullptr; });