mirror of
https://github.com/nestriness/cdc-file-transfer.git
synced 2026-01-30 14:25:36 +02:00
* Remove dependencies of cdc_sync from GGP Allows overriding the SSH and SCP commands via command line flags. Hence, strict host checking, SSH config etc. can be removed since it is passed in by command line flags for GGP. Also deploys cdc_rsync_server to ~/.cache/cdc_file_transfer/ and creates that dir if it does not exist. * Tweak RemoteUtil Replaces localhost: by //./ in the workaround for scp since localhost: had two disadvantages: 1) It required 2 gnubby touches for gLinux and 2) it didn't work for ggp. //./ works for both. Also tweaks quoting, which didn't quite work for ggp. * Don't check remote ports in cdc_rsync Turns off checking remote ports in PortManager. In the future, the server should return available ports after failing to connect to the provided port. Since now the first remote connection is running cdc_rsync_server, the timeout check has to be done when running that process. * Remove now-unused kInstancePickerNotAvailableInQuietMode enum * Add more details to the readme * [cdc_rsync] Accept [user@]host:destination Removes the --ip command line argument and assumes user/host are passed in along with the destination, so it works in the same way as other popular tools. * [ggp_rsync] Combine server deploy commands Combines two chmod and one mv command into one ssh command. This makes deploy a bit quicker, especially if each ssh command involves touching your gnubby. * Remove GGP specific stuff from VS build commands * [cdc_rsync] Get rid of cdc_rsync.dll Compile the CDC RSync client as a static library instead. This removes quite a bit of boiler plate and makes string handling easier since we can now pass std::strings instead of const chars. Also fixes an issue where we were sometimes trying to assign nullptr to std::strings, which is forbidden. * Allow specifying ssh/scp commands with env vars * Rename GgpRsync* to CdcRsync* * Merge ggp_rsync_cli into ggp_rsync * [cdc_rsync] Refactor cdc_rsync.cc/h Merges cdc_rsync.cc/h with main.cc and CdcRsyncClient since code is closer to where it's being used and should be more readable.
552 lines
20 KiB
C++
552 lines
20 KiB
C++
// Copyright 2022 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include "cdc_rsync/params.h"
|
|
|
|
#include "absl/strings/match.h"
|
|
#include "common/log.h"
|
|
#include "common/path.h"
|
|
#include "common/status_test_macros.h"
|
|
#include "common/test_main.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
namespace cdc_ft {
|
|
namespace params {
|
|
namespace {
|
|
|
|
using Options = CdcRsyncClient::Options;
|
|
|
|
constexpr char kSrc[] = "source";
|
|
constexpr char kUserHostDst[] = "user@host:destination";
|
|
constexpr char kUserHost[] = "user@host";
|
|
constexpr char kDst[] = "destination";
|
|
|
|
class TestLog : public Log {
|
|
public:
|
|
explicit TestLog() : Log(LogLevel::kInfo) {}
|
|
|
|
protected:
|
|
void WriteLogMessage(LogLevel level, const char* file, int line,
|
|
const char* func, const char* message) override {
|
|
errors_ += message;
|
|
}
|
|
|
|
private:
|
|
std::string errors_;
|
|
};
|
|
|
|
std::string NeedsValueError(const char* option_name) {
|
|
return absl::StrFormat("Option '%s' needs a value", option_name);
|
|
}
|
|
|
|
class ParamsTest : public ::testing::Test {
|
|
public:
|
|
void SetUp() override {
|
|
prev_stdout_ = std::cout.rdbuf(output_.rdbuf());
|
|
prev_stderr_ = std::cerr.rdbuf(errors_.rdbuf());
|
|
}
|
|
|
|
void TearDown() override {
|
|
std::cout.rdbuf(prev_stdout_);
|
|
std::cerr.rdbuf(prev_stderr_);
|
|
}
|
|
|
|
protected:
|
|
void ExpectNoError() const {
|
|
EXPECT_TRUE(errors_.str().empty())
|
|
<< "Expected empty stderr but got\n'" << errors_.str() << "'";
|
|
}
|
|
|
|
void ExpectOutput(const std::string& expected) const {
|
|
EXPECT_TRUE(absl::StrContains(output_.str(), expected))
|
|
<< "Expected stdout to contain '" << expected << "' but got\n'"
|
|
<< output_.str() << "'";
|
|
}
|
|
|
|
void ExpectError(const std::string& expected) const {
|
|
EXPECT_TRUE(absl::StrContains(errors_.str(), expected))
|
|
<< "Expected stderr to contain '" << expected << "' but got\n'"
|
|
<< errors_.str() << "'";
|
|
}
|
|
|
|
void ClearErrors() { errors_.str(std::string()); }
|
|
|
|
std::string base_dir_ = GetTestDataDir("params");
|
|
std::string sources_file_ = path::Join(base_dir_, "source_files.txt");
|
|
std::string empty_sources_file_ =
|
|
path::Join(base_dir_, "empty_source_files.txt");
|
|
|
|
Parameters parameters_;
|
|
std::stringstream output_;
|
|
std::stringstream errors_;
|
|
std::streambuf* prev_stdout_;
|
|
std::streambuf* prev_stderr_;
|
|
};
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsDefaults) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
EXPECT_EQ(RemoteUtil::kDefaultSshPort, parameters_.options.port);
|
|
EXPECT_FALSE(parameters_.options.delete_);
|
|
EXPECT_FALSE(parameters_.options.recursive);
|
|
EXPECT_EQ(0, parameters_.options.verbosity);
|
|
EXPECT_FALSE(parameters_.options.quiet);
|
|
EXPECT_FALSE(parameters_.options.whole_file);
|
|
EXPECT_FALSE(parameters_.options.compress);
|
|
EXPECT_FALSE(parameters_.options.checksum);
|
|
EXPECT_FALSE(parameters_.options.dry_run);
|
|
EXPECT_TRUE(parameters_.options.copy_dest.empty());
|
|
EXPECT_EQ(6, parameters_.options.compress_level);
|
|
EXPECT_EQ(10, parameters_.options.connection_timeout_sec);
|
|
EXPECT_EQ(1, parameters_.sources.size());
|
|
EXPECT_EQ(parameters_.sources[0], kSrc);
|
|
EXPECT_EQ(parameters_.user_host, kUserHost);
|
|
EXPECT_EQ(parameters_.destination, kDst);
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsWithOptionFromTwoArguments) {
|
|
const char* argv[] = {
|
|
"cdc_rsync.exe", "--compress-level", "2", kSrc, kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
EXPECT_EQ(parameters_.options.compress_level, 2);
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest,
|
|
ParseSucceedsWithOptionFromOneArgumentWithEqualityWithValue) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--compress-level=2", kSrc,
|
|
kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ASSERT_EQ(parameters_.sources.size(), 1);
|
|
EXPECT_EQ(parameters_.options.compress_level, 2);
|
|
EXPECT_EQ(parameters_.sources[0], kSrc);
|
|
EXPECT_EQ(parameters_.user_host, kUserHost);
|
|
EXPECT_EQ(parameters_.destination, kDst);
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseFailsOnCompressLevelEqualsNoValue) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--compress-level=", kSrc,
|
|
kUserHostDst, NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError(NeedsValueError("compress-level"));
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseFailsOnPortEqualsNoValue) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--port=", kSrc, kUserHostDst, NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError(NeedsValueError("port"));
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseFailsOnContimeoutEqualsNoValue) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--contimeout=", kSrc, kUserHostDst,
|
|
NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError(NeedsValueError("contimeout"));
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsWithSshScpCommands) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc,
|
|
kUserHostDst, "--ssh-command=sshcmd",
|
|
"--scp-command=scpcmd", NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
EXPECT_EQ(parameters_.options.scp_command, "scpcmd");
|
|
EXPECT_EQ(parameters_.options.ssh_command, "sshcmd");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsWithSshScpCommandsByEnvVars) {
|
|
EXPECT_OK(path::SetEnv("CDC_SSH_COMMAND", "sshcmd"));
|
|
EXPECT_OK(path::SetEnv("CDC_SCP_COMMAND", "scpcmd"));
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
EXPECT_EQ(parameters_.options.scp_command, "scpcmd");
|
|
EXPECT_EQ(parameters_.options.ssh_command, "sshcmd");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsWithNoSshCommand) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, kUserHostDst,
|
|
"--ssh-command=", NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError(NeedsValueError("ssh-command"));
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsWithNoScpCommand) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, kUserHostDst, "--scp-command",
|
|
NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError(NeedsValueError("scp-command"));
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseFailsOnNoUserHost) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, kDst, NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError("No remote host specified");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseDoesNotThinkCIsAHost) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, "C:\\foo", NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError("No remote host specified");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseWithoutParametersFailsOnMissingSourceAndDestination) {
|
|
const char* argv[] = {"cdc_rsync.exe", NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectOutput("Usage:");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseWithSingleParameterFailsOnMissingDestination) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError("Missing source or destination");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsWithMultipleLetterKeyConsumed) {
|
|
const char* argv[] = {"cdc_rsync.exe", "-rvqWRzcn", kSrc, kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
EXPECT_TRUE(parameters_.options.recursive);
|
|
EXPECT_EQ(parameters_.options.verbosity, 1);
|
|
EXPECT_TRUE(parameters_.options.quiet);
|
|
EXPECT_TRUE(parameters_.options.whole_file);
|
|
EXPECT_TRUE(parameters_.options.relative);
|
|
EXPECT_TRUE(parameters_.options.compress);
|
|
EXPECT_TRUE(parameters_.options.checksum);
|
|
EXPECT_TRUE(parameters_.options.dry_run);
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest,
|
|
ParseFailsOnMultipleLetterKeyConsumedOptionsWithUnsupportedOne) {
|
|
const char* argv[] = {"cdc_rsync.exe", "-rvqaWRzcn", kSrc, kUserHostDst,
|
|
NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError("Unknown option: 'a'");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsWithMultipleLongKeyConsumedOptions) {
|
|
const char* argv[] = {"cdc_rsync.exe",
|
|
"--recursive",
|
|
"--verbosity",
|
|
"--quiet",
|
|
"--whole-file",
|
|
"--compress",
|
|
"--relative",
|
|
"--delete",
|
|
"--checksum",
|
|
"--dry-run",
|
|
"--existing",
|
|
"--json",
|
|
kSrc,
|
|
kUserHostDst,
|
|
NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
EXPECT_TRUE(parameters_.options.recursive);
|
|
EXPECT_EQ(parameters_.options.verbosity, 1);
|
|
EXPECT_TRUE(parameters_.options.quiet);
|
|
EXPECT_TRUE(parameters_.options.whole_file);
|
|
EXPECT_TRUE(parameters_.options.relative);
|
|
EXPECT_TRUE(parameters_.options.compress);
|
|
EXPECT_TRUE(parameters_.options.delete_);
|
|
EXPECT_TRUE(parameters_.options.checksum);
|
|
EXPECT_TRUE(parameters_.options.dry_run);
|
|
EXPECT_TRUE(parameters_.options.existing);
|
|
EXPECT_TRUE(parameters_.options.json);
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseFailsOnUnknownKey) {
|
|
const char* argv[] = {"cdc_rsync.exe", "-unknownKey", kSrc, kUserHostDst,
|
|
NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError("Unknown option: 'u'");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsWithSupportedKeyValue) {
|
|
const char* argv[] = {
|
|
"cdc_rsync.exe", "--compress-level", "11", "--contimeout", "99", "--port",
|
|
"4086", "--copy-dest=dest", kSrc, kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
EXPECT_EQ(parameters_.options.compress_level, 11);
|
|
EXPECT_EQ(parameters_.options.connection_timeout_sec, 99);
|
|
EXPECT_EQ(parameters_.options.port, 4086);
|
|
EXPECT_EQ(parameters_.options.copy_dest, "dest");
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsWithSupportedKeyValueWithoutEqualityForChars) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--copy-dest", "dest", kSrc,
|
|
kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
EXPECT_EQ(parameters_.options.copy_dest, "dest");
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseFailsOnInvalidPort) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--port=0", kSrc, kUserHostDst, NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError("--port must specify a valid port");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseFailsOnDeleteNeedsRecursive) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--delete", kSrc, kUserHostDst, NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError("--delete does not work without --recursive (-r)");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseChecksCompressLevel) {
|
|
int minLevel = Options::kMinCompressLevel;
|
|
int maxLevel = Options::kMaxCompressLevel;
|
|
int levels[] = {minLevel - 1, minLevel, 0, maxLevel, maxLevel + 1};
|
|
bool valid[] = {false, true, false, true, false};
|
|
|
|
for (int n = 0; n < std::size(levels); ++n) {
|
|
std::string level = "--compress-level=" + std::to_string(levels[n]);
|
|
const char* argv[] = {"cdc_rsync.exe", level.c_str(), kSrc, kUserHostDst,
|
|
NULL};
|
|
EXPECT_EQ(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_),
|
|
valid[n]);
|
|
if (valid[n]) {
|
|
ExpectNoError();
|
|
} else {
|
|
ExpectError("--compress_level must be between");
|
|
}
|
|
ClearErrors();
|
|
}
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseFailsOnUnknownKeyValue) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--unknownKey=5", kSrc, kUserHostDst,
|
|
NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError("unknownKey");
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseFailsWithHelpOption) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
|
|
const char* argv2[] = {"cdc_rsync.exe", kSrc, kUserHostDst, "--help", NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv2)) - 1, argv2, ¶meters_));
|
|
ExpectNoError();
|
|
|
|
const char* argv3[] = {"cdc_rsync.exe", kSrc, kUserHostDst, "-h", NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv3)) - 1, argv3, ¶meters_));
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, ParseSucceedsWithIncludeExclude) {
|
|
const char* argv[] = {"cdc_rsync.exe",
|
|
"--include=*.txt",
|
|
"--exclude",
|
|
"*.dat",
|
|
"--include",
|
|
"*.exe",
|
|
kSrc,
|
|
kUserHostDst,
|
|
NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
const std::vector<PathFilter::Rule>& rules =
|
|
parameters_.options.filter.GetRules();
|
|
ASSERT_EQ(rules.size(), 3);
|
|
ASSERT_EQ(rules[0].type, PathFilter::Rule::Type::kInclude);
|
|
ASSERT_EQ(rules[0].pattern, "*.txt");
|
|
ASSERT_EQ(rules[1].type, PathFilter::Rule::Type::kExclude);
|
|
ASSERT_EQ(rules[1].pattern, "*.dat");
|
|
ASSERT_EQ(rules[2].type, PathFilter::Rule::Type::kInclude);
|
|
ASSERT_EQ(rules[2].pattern, "*.exe");
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, FilesFrom_NoFile) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, kUserHostDst, "--files-from",
|
|
NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError(NeedsValueError("files-from"));
|
|
}
|
|
|
|
TEST_F(ParamsTest, FilesFrom_ImpliesRelative) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--files-from",
|
|
sources_file_.c_str(), base_dir_.c_str(),
|
|
kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
EXPECT_TRUE(parameters_.options.relative);
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, FilesFrom_WithoutSourceArg) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--files-from", sources_file_.c_str(),
|
|
kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
EXPECT_TRUE(parameters_.options.sources_dir.empty());
|
|
EXPECT_EQ(parameters_.user_host, kUserHost);
|
|
EXPECT_EQ(parameters_.destination, kDst);
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, FilesFrom_WithSourceArg) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--files-from",
|
|
sources_file_.c_str(), base_dir_.c_str(),
|
|
kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
|
|
std::string expected_sources_dir = base_dir_;
|
|
path::EnsureEndsWithPathSeparator(&expected_sources_dir);
|
|
EXPECT_EQ(parameters_.options.sources_dir, expected_sources_dir);
|
|
EXPECT_EQ(parameters_.user_host, kUserHost);
|
|
EXPECT_EQ(parameters_.destination, kDst);
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, FilesFrom_ParsesFile) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--files-from", sources_file_.c_str(),
|
|
kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
|
|
std::vector<const char*> expected = {"file1", "file2", "file3"};
|
|
ASSERT_EQ(parameters_.sources.size(), expected.size());
|
|
for (size_t n = 0; n < expected.size(); ++n) {
|
|
EXPECT_EQ(parameters_.sources[n], expected[n]);
|
|
}
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, FilesFrom_EmptyFile_WithoutSourceArg) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--files-from",
|
|
empty_sources_file_.c_str(), kUserHostDst, NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError(empty_sources_file_);
|
|
ExpectError("--files-from option is empty");
|
|
}
|
|
|
|
TEST_F(ParamsTest, FilesFrom_EmptyFile_WithSourceArg) {
|
|
const char* argv[] = {
|
|
"cdc_rsync.exe", "--files-from", empty_sources_file_.c_str(),
|
|
base_dir_.c_str(), kUserHostDst, NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError(empty_sources_file_);
|
|
ExpectError("--files-from option is empty");
|
|
}
|
|
|
|
TEST_F(ParamsTest, FilesFrom_NoDestination) {
|
|
const char* argv[] = {"cdc_rsync.exe", "--files-from", sources_file_.c_str(),
|
|
NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError("Missing destination");
|
|
}
|
|
|
|
TEST_F(ParamsTest, IncludeFrom_NoFile) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, kUserHostDst, "--include-from",
|
|
NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError(NeedsValueError("include-from"));
|
|
}
|
|
|
|
TEST_F(ParamsTest, IncludeFrom_ParsesFile) {
|
|
std::string file = path::Join(base_dir_, "include_files.txt");
|
|
const char* argv[] = {"cdc_rsync.exe", "--include-from",
|
|
file.c_str(), kSrc,
|
|
kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
|
|
const std::vector<PathFilter::Rule>& rules =
|
|
parameters_.options.filter.GetRules();
|
|
ASSERT_EQ(rules.size(), 1);
|
|
ASSERT_EQ(rules[0].type, PathFilter::Rule::Type::kInclude);
|
|
ASSERT_EQ(rules[0].pattern, "file3");
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, ExcludeFrom_NoFile) {
|
|
const char* argv[] = {"cdc_rsync.exe", kSrc, kUserHostDst, "--exclude-from",
|
|
NULL};
|
|
EXPECT_FALSE(
|
|
Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
ExpectError(NeedsValueError("exclude-from"));
|
|
}
|
|
|
|
TEST_F(ParamsTest, ExcludeFrom_ParsesFile) {
|
|
std::string file = path::Join(base_dir_, "exclude_files.txt");
|
|
const char* argv[] = {"cdc_rsync.exe", "--exclude-from",
|
|
file.c_str(), kSrc,
|
|
kUserHostDst, NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
|
|
const std::vector<PathFilter::Rule>& rules =
|
|
parameters_.options.filter.GetRules();
|
|
ASSERT_EQ(rules.size(), 2);
|
|
EXPECT_EQ(rules[0].type, PathFilter::Rule::Type::kExclude);
|
|
EXPECT_EQ(rules[0].pattern, "file1");
|
|
EXPECT_EQ(rules[1].type, PathFilter::Rule::Type::kExclude);
|
|
EXPECT_EQ(rules[1].pattern, "file2");
|
|
ExpectNoError();
|
|
}
|
|
|
|
TEST_F(ParamsTest, IncludeExcludeMixed_ProperOrder) {
|
|
std::string exclude_file = path::Join(base_dir_, "exclude_files.txt");
|
|
std::string include_file = path::Join(base_dir_, "include_files.txt");
|
|
const char* argv[] = {"cdc_rsync.exe",
|
|
"--include-from",
|
|
include_file.c_str(),
|
|
"--exclude=excl1",
|
|
kSrc,
|
|
"--exclude-from",
|
|
exclude_file.c_str(),
|
|
kUserHostDst,
|
|
"--include",
|
|
"incl1",
|
|
NULL};
|
|
EXPECT_TRUE(Parse(static_cast<int>(std::size(argv)) - 1, argv, ¶meters_));
|
|
|
|
const std::vector<PathFilter::Rule>& rules =
|
|
parameters_.options.filter.GetRules();
|
|
ASSERT_EQ(rules.size(), 5);
|
|
EXPECT_EQ(rules[0].type, PathFilter::Rule::Type::kInclude);
|
|
EXPECT_EQ(rules[0].pattern, "file3");
|
|
EXPECT_EQ(rules[1].type, PathFilter::Rule::Type::kExclude);
|
|
EXPECT_EQ(rules[1].pattern, "excl1");
|
|
EXPECT_EQ(rules[2].type, PathFilter::Rule::Type::kExclude);
|
|
EXPECT_EQ(rules[2].pattern, "file1");
|
|
EXPECT_EQ(rules[3].type, PathFilter::Rule::Type::kExclude);
|
|
EXPECT_EQ(rules[3].pattern, "file2");
|
|
EXPECT_EQ(rules[4].type, PathFilter::Rule::Type::kInclude);
|
|
EXPECT_EQ(rules[4].pattern, "incl1");
|
|
ExpectNoError();
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace params
|
|
} // namespace cdc_ft
|