Skip to content

Commit

Permalink
test/bbackupd: add a test for bbackupd-config and bbstored-config scr…
Browse files Browse the repository at this point in the history
…ipts

Test creating new clients and servers, signing their certificates and running a
test backup.
  • Loading branch information
qris committed Jun 20, 2019
1 parent b3e327f commit a9be6f8
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 18 deletions.
2 changes: 2 additions & 0 deletions lib/common/Test.cpp
Expand Up @@ -126,6 +126,7 @@ void cleanup_test_environment(bool delete_pid_files)
filename == "bbackupd-cache" ||
filename == "bbackupd-cache-2" ||
filename == "bbackupd-data" ||
filename == "ca" ||
StartsWith("file", filename) ||
StartsWith("notifyran", filename) ||
StartsWith("notifyscript.tag", filename) ||
Expand All @@ -136,6 +137,7 @@ void cleanup_test_environment(bool delete_pid_files)
filename == "test_map.db" ||
filename == "test2.downloaded" ||
EndsWith("testfile", filename) ||
filename == "tmp" ||
EndsWith(".qdbm", filename) ||
(delete_pid_files && EndsWith(".pid", filename)))
{
Expand Down
4 changes: 2 additions & 2 deletions lib/server/ServerControl.h
Expand Up @@ -11,8 +11,8 @@ bool KillServer(const std::string& pid_file, bool WaitForProcess = false);
bool KillServerInternal(int pid);
int StartDaemon(int current_pid, const std::string& cmd_line, const char* pid_file, int port = 0,
const std::string& socket_path = "");
bool StopDaemon(int current_pid, const std::string& pid_file,
const std::string& memleaks_file, bool wait_for_process);
bool StopDaemon(int current_pid, const std::string& pid_file, const std::string& memleaks_file,
bool wait_for_process);
int LaunchServer(const std::string& rCommandLine, const char *pidFile, int port = 0,
const std::string& socket_path = "");
int WaitForServerStartup(const char *pidFile, int pidIfKnown, int port = 0,
Expand Down
137 changes: 121 additions & 16 deletions test/bbackupd/testbbackupd.cpp
Expand Up @@ -12,39 +12,44 @@
// do not include MinGW's dirent.h on Win32,
// as we override some of it in lib/win32.

#include <limits.h>
#include <stdio.h>
#include <string.h>

#ifndef WIN32
#include <dirent.h>
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <string.h>

#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#ifdef WIN32
#include <process.h>
#endif

#ifdef HAVE_SYS_XATTR_H
#include <cerrno>
#include <sys/xattr.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif

#ifdef WIN32
#include <process.h>
#endif

#include <map>
#include <sys/stat.h>
#include <sys/types.h>

#ifdef HAVE_SYSCALL
#include <sys/syscall.h>
#endif

#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif

#ifdef HAVE_SYS_XATTR_H
#include <cerrno>
#include <sys/xattr.h>
#endif

#include <map>

#include <boost/scope_exit.hpp>

#include "BackupClientCryptoKeys.h"
Expand Down Expand Up @@ -4115,6 +4120,105 @@ bool test_parse_syncallowscript_output()
TEARDOWN_TEST_BBACKUPD();
}

bool test_bbackupd_config_script()
{
SETUP_TEST_BBACKUPD();

#ifdef WIN32
BOX_NOTICE("skipping test on this platform"); // TODO: write a PowerShell version
#else
char buf[PATH_MAX];
if (getcwd(buf, sizeof(buf)) == NULL)
{
BOX_LOG_SYS_ERROR("getcwd");
}
std::string current_dir = buf;

TEST_THAT(mkdir("testfiles/tmp", 0777) == 0);
TEST_THAT(mkdir("testfiles/TestDir1", 0777) == 0);

// Generate a new configuration for our test bbackupd, from scratch:
std::string cmd = "../../../bin/bbackupd/bbackupd-config " +
current_dir + "/testfiles/tmp " // config-dir
"lazy " // backup-mode
"12345 " // account-num
"localhost " + // server-hostname
current_dir + "/testfiles " + // working-dir
current_dir + "/testfiles/TestDir1"; // backup directories
TEST_RETURN(system(cmd.c_str()), 0)

// Open the generated config file and add a StorePort line:
{
FileStream conf_file("testfiles/tmp/bbackupd.conf", O_WRONLY | O_APPEND);
conf_file.Write("StorePort = 22011\n");
conf_file.Close();
}

// Generate a new configuration for our test bbstored, from scratch:
struct passwd *result = getpwuid(getuid());
TEST_THAT_OR(result != NULL, FAIL); // failed to get username for current user
std::string username = result->pw_name;

cmd = "../../../bin/bbstored/bbstored-config testfiles/tmp localhost " + username + " "
"testfiles/raidfile.conf";
TEST_RETURN(system(cmd.c_str()), 0)

cmd = "sed -i.orig -e 's/\\(ListenAddresses = inet:localhost\\)/\\1:22011/' "
"-e 's@PidFile = /var/run/bbstored.pid@PidFile = testfiles/bbstored.pid@' "
"testfiles/tmp/bbstored.conf";
TEST_RETURN(system(cmd.c_str()), 0)

// Create a server certificate authority, and sign the client and server certificates:
cmd = "../../../bin/bbstored/bbstored-certs testfiles/ca init";
TEST_RETURN(system(cmd.c_str()), 0)

cmd = "echo yes | ../../../bin/bbstored/bbstored-certs testfiles/ca sign "
"testfiles/tmp/bbackupd/12345-csr.pem";
TEST_RETURN(system(cmd.c_str()), 0)

cmd = "echo yes | ../../../bin/bbstored/bbstored-certs testfiles/ca sign-server "
"testfiles/tmp/bbstored/localhost-csr.pem";
TEST_RETURN(system(cmd.c_str()), 0)

// Copy the certificate files into the right places
cmd = "cp testfiles/ca/clients/12345-cert.pem testfiles/tmp/bbackupd";
TEST_RETURN(system(cmd.c_str()), 0)

cmd = "cp testfiles/ca/roots/serverCA.pem testfiles/tmp/bbackupd";
TEST_RETURN(system(cmd.c_str()), 0)

cmd = "cp testfiles/ca/servers/localhost-cert.pem testfiles/tmp/bbstored";
TEST_RETURN(system(cmd.c_str()), 0)

cmd = "cp testfiles/ca/roots/clientCA.pem testfiles/tmp/bbstored";
TEST_RETURN(system(cmd.c_str()), 0)

cmd = BBSTOREACCOUNTS " -c testfiles/tmp/bbstored.conf create 12345 0 1M 2M";
TEST_RETURN(system(cmd.c_str()), 0)

bbstored_pid = StartDaemon(bbstored_pid, BBSTORED " " + bbstored_args +
" testfiles/tmp/bbstored.conf", "testfiles/bbstored.pid", 22011);

BackupDaemon bbackupd;
TEST_THAT(
prepare_test_with_client_daemon(
bbackupd,
true, // do_unpack_files
false, // !do_start_bbstored
"testfiles/tmp/bbackupd.conf")
);

bbackupd.RunSyncNow();

TEST_THAT(compare_external(BackupQueries::ReturnCode::Compare_Same,
"", "-acQ", "testfiles/tmp/bbackupd.conf"));

TEST_THAT(StopServer());
#endif

TEARDOWN_TEST_BBACKUPD();
}

int test(int argc, const char *argv[])
{
// SSL library
Expand Down Expand Up @@ -4207,6 +4311,7 @@ int test(int argc, const char *argv[])
TEST_THAT(test_backup_many_files());
TEST_THAT(test_parse_incomplete_command());
TEST_THAT(test_parse_syncallowscript_output());
TEST_THAT(test_bbackupd_config_script());

#ifndef WIN32
if(::getuid() == 0)
Expand Down

0 comments on commit a9be6f8

Please sign in to comment.