Skip to content

Commit

Permalink
namespaces: improved namespace usage in tests
Browse files Browse the repository at this point in the history
- added console namespace
- improved calls from stored
- added wrapper for directordeamon::DoReloadConfig for bsock_tests
- added libraries to the buit  of bsock_tests
  • Loading branch information
franku committed Aug 21, 2018
1 parent 6f11581 commit 911af61
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 36 deletions.
5 changes: 4 additions & 1 deletion core/src/console/conio.cc
Expand Up @@ -52,7 +52,9 @@
/* We are in Bareos */
#include "include/bareos.h"

#endif
#endif /* TEST_PROGRAM */

namespace console {

#ifdef HAVE_CONIO

Expand Down Expand Up @@ -1190,3 +1192,4 @@ static void asdell()
}

#endif
} /* namespace console */
5 changes: 5 additions & 0 deletions core/src/console/conio.h
Expand Up @@ -21,6 +21,9 @@

#ifndef BAREOS_CONSOLE_CONIO_H_
#define BAREOS_CONSOLE_CONIO_H_

namespace console {

extern int InputLine(char *line, int len);
extern void ConInit(FILE *input);

Expand All @@ -35,4 +38,6 @@ extern void t_char(char c);
extern int usrbrk(void);
extern void clrbrk(void);
extern void trapctlc(void);

} /* namespace console */
#endif
6 changes: 5 additions & 1 deletion core/src/console/console.cc
Expand Up @@ -48,8 +48,12 @@
#define isatty(fd) (fd==0)
#endif

using namespace console;

/* Exported variables */
ConsoleResource *me = NULL; /* Our Global resource */
namespace console {
ConsoleResource *me = NULL; /* Our Global resource */
}
ConfigurationParser *my_config = nullptr; /* Our Global config */

//extern int rl_catch_signals;
Expand Down
8 changes: 8 additions & 0 deletions core/src/console/console_conf.cc
Expand Up @@ -48,6 +48,8 @@
#include "console_conf.h"
#include "lib/json.h"

namespace console {

/**
* Define the first and last resource ID record
* types. Note, these should be unique for each
Expand Down Expand Up @@ -111,6 +113,9 @@ static ResourceTable resources[] = {
{ "Director", dir_items, R_DIRECTOR, sizeof(DirectorResource), [] (void *res){ return new((DirectorResource *) res) DirectorResource(); } },
{ NULL, NULL, 0 }
};
} /* namespace console */

using namespace console;

/**
* Dump contents of resource
Expand Down Expand Up @@ -354,6 +359,8 @@ bool SaveResource(int type, ResourceItem *items, int pass)
return (error == 0);
}

namespace console {

ConfigurationParser *InitConsConfig(const char *configfile, int exit_code)
{
return new ConfigurationParser (
Expand Down Expand Up @@ -414,3 +421,4 @@ bool PrintConfigSchemaJson(PoolMem &buffer)
return false;
}
#endif
} /* namespace console */
8 changes: 7 additions & 1 deletion core/src/console/console_conf.h
Expand Up @@ -28,6 +28,11 @@
* Bareos User Agent specific configuration and defines
*/

class ConfigurationParser;
extern ConfigurationParser *my_config; /* Our Global config */

namespace console {

#define CONFIG_FILE "bconsole.conf" /* default configuration file */

/**
Expand Down Expand Up @@ -88,7 +93,8 @@ union UnionOfResources {
};

extern ConsoleResource *me; /* "Global" Client resource */
extern ConfigurationParser *my_config; /* Our Global config */

ConfigurationParser *InitConsConfig(const char *configfile, int exit_code);
bool PrintConfigSchemaJson(PoolMem &buffer);

} /* namespace console */
5 changes: 5 additions & 0 deletions core/src/dird/sd_cmds.h
Expand Up @@ -22,8 +22,13 @@
#ifndef BAREOS_DIRD_SD_CMDS_H_
#define BAREOS_DIRD_SD_CMDS_H_

class JobControlRecord;

namespace directordaemon {

class UaContext;
class StorageResource;

bool ConnectToStorageDaemon(JobControlRecord *jcr, int retry_interval,
int max_retry_time, bool verbose);
BareosSocket *open_sd_bsock(UaContext *ua);
Expand Down
23 changes: 17 additions & 6 deletions core/src/include/jcr.h
Expand Up @@ -38,21 +38,32 @@

#ifdef STORAGE_DAEMON
#include "stored/read_ctx.h"
#endif

#ifdef DIRECTOR_DAEMON
#include "cats/cats.h"
#endif

namespace directordaemon {
class JobResource;
class StorageResource;
class ClientResource;
class PoolResource;
class FilesetResource;
class CatalogResource;
}

namespace storagedaemon {
class VolumeList;
class DeviceControlRecord;
class DirectorResource;
class BootStrapRecord;
struct BootStrapRecord;
}
#endif

#ifdef FILE_DAEMON
namespace filedaemon {
class BareosAccurateFilelist;
struct save_pkt;
class BareosAccurateFilelist;
struct save_pkt;
}
#endif /* FILE_DAEMON */

/**
* Backup/Verify level code. These are stored in the DB
Expand Down
11 changes: 8 additions & 3 deletions core/src/lib/unittests/CMakeLists.txt
Expand Up @@ -63,12 +63,17 @@ add_executable(test_bsock
)

target_link_libraries(test_bsock
bareos
stored_objects
dird_objects
bareossd
bareos
bareoscats
bareoscfg
bareosndmp
stored_objects
bareossql
bareosfind
dird_objects
${LMDB_LIBS}
${NDMP_LIBS}
${JANSSON_LIBRARIES}
${GTEST_LIBRARIES}
${GTEST_MAIN_LIBRARIES}
Expand Down
35 changes: 31 additions & 4 deletions core/src/lib/unittests/bsock_dir_sd_connection_test.cc
Expand Up @@ -26,10 +26,37 @@
//{
// int newsockfd = create_accepted_server_socket(BSOCK_TEST_PORT_NUMBER);
// BareosSocket *bs = create_new_bareos_socket(newsockfd);
// HandleConnectionRequest(bs);
// storagedaemon::HandleConnectionRequest(bs);
//}
//
//#define DIRECTOR_DAEMON
//#include "include/bareos.h"
//#include "console/console_conf.h"
//#include "lib/tls_openssl.h"
//
//#include "include/jcr.h"
//#include "dird/sd_cmds.h"
//#include "dird/dird_globals.h"
//#include "dird/dird_conf.h"
//
//#include <thread>
//
//static void connect_dir_to_sd()
//{
// JobControlRecord jcr;
// directordaemon::ConnectToStorageDaemon(&jcr, 10, 1, true);
//}
//#undef DIRECTOR_DAEMON
//
//TEST(bsock_dir_sd, dir_to_sd_connection_test)
//{
// std::thread server_thread(start_sd_server);
// connect_dir_to_sd();
//}

TEST(bsock_dir_sd, dir_to_sd_connection_test)
{
// start_sd_server();
/* ********************************************************/
/* dummy wrapper for unused methods from other namespaces */
/* ********************************************************/
namespace directordaemon {
bool DoReloadConfig() { return false; }
}
54 changes: 35 additions & 19 deletions core/src/lib/unittests/bsock_test.cc
Expand Up @@ -32,6 +32,7 @@
#include "lib/tls_openssl.h"

#include "include/jcr.h"
#include "dird/dird_conf.h"


#define CLIENT_AS_A_THREAD 0
Expand Down Expand Up @@ -135,7 +136,7 @@ static void clone_a_server_socket(BareosSocket* bs)

void start_bareos_server(std::promise<bool> *promise, std::string console_name,
std::string console_password, std::string server_address, int server_port,
ConsoleResource *cons)
directordaemon::ConsoleResource *cons)

{
int newsockfd = create_accepted_server_socket(server_port);
Expand Down Expand Up @@ -206,10 +207,10 @@ void clone_a_client_socket(BareosSocket *UA_sock)

#if CLIENT_AS_A_THREAD
int connect_to_server(std::string console_name, std::string console_password,
std::string server_address, int server_port, DirectorResource *dir)
std::string server_address, int server_port, console::DirectorResource *dir)
#else
bool connect_to_server(std::string console_name, std::string console_password,
std::string server_address, int server_port, DirectorResource *dir)
std::string server_address, int server_port, console::DirectorResource *dir)
#endif
{
utime_t heart_beat = 0;
Expand Down Expand Up @@ -273,9 +274,9 @@ bool connect_to_server(std::string console_name, std::string console_password,
return success;
}

ConsoleResource *CreateAndInitializeNewConsoleResource()
directordaemon::ConsoleResource *CreateAndInitializeNewConsoleResource()
{
ConsoleResource *cons = new (ConsoleResource);
directordaemon::ConsoleResource *cons = new (directordaemon::ConsoleResource);
cons->tls_psk.enable = false; // enable_tls_psk;
cons->tls_cert.certfile = new (std::string)(CERTDIR "/console.bareos.org-cert.pem");
cons->tls_cert.keyfile = new (std::string)(CERTDIR "/console.bareos.org-key.pem");
Expand All @@ -286,9 +287,9 @@ ConsoleResource *CreateAndInitializeNewConsoleResource()
return cons;
}

DirectorResource *CreateAndInitializeNewDirectorResource()
console::DirectorResource *CreateAndInitializeNewDirectorResource()
{
DirectorResource *dir = new (DirectorResource);
console::DirectorResource *dir = new (console::DirectorResource);
dir->address = (char *)HOST;
dir->DIRport = htons(BSOCK_TEST_PORT_NUMBER);
dir->tls_psk.enable = false;
Expand All @@ -301,6 +302,21 @@ DirectorResource *CreateAndInitializeNewDirectorResource()
return dir;
}

directordaemon::StorageResource *CreateAndInitializeNewStorageResource()
{
directordaemon::StorageResource *store = new (directordaemon::StorageResource);
store->address = (char *)HOST;
store->SDport = htons(BSOCK_TEST_PORT_NUMBER);
store->tls_psk.enable = false;
store->tls_cert.certfile = new (std::string)(CERTDIR "/bareos-dir.bareos.org-cert.pem");
store->tls_cert.keyfile = new (std::string)(CERTDIR "/bareos-dir.bareos.org-key.pem");
store->tls_cert.CaCertfile = new (std::string)(CERTDIR "/bareos-ca.pem");
store->tls_cert.enable = false;
store->tls_cert.VerifyPeer = false;
store->tls_cert.require = false;
return store;
}

std::string client_cons_name;
std::string client_cons_password;

Expand All @@ -321,8 +337,8 @@ TEST(bsock, auth_works)
server_cons_name = client_cons_name;
server_cons_password = client_cons_password;

ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
DirectorResource *dir = CreateAndInitializeNewDirectorResource();
directordaemon::ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
console::DirectorResource *dir = CreateAndInitializeNewDirectorResource();

InitForTest();

Expand Down Expand Up @@ -353,8 +369,8 @@ TEST(bsock, auth_works_with_different_names)
server_cons_name = "differentclientname";
server_cons_password = client_cons_password;

ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
DirectorResource *dir = CreateAndInitializeNewDirectorResource();
directordaemon::ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
console::DirectorResource *dir = CreateAndInitializeNewDirectorResource();

InitForTest();

Expand Down Expand Up @@ -384,8 +400,8 @@ TEST(bsock, auth_fails_with_different_passwords)
server_cons_name = client_cons_name;
server_cons_password = "othersecretpassword";

ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
DirectorResource *dir = CreateAndInitializeNewDirectorResource();
directordaemon::ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
console::DirectorResource *dir = CreateAndInitializeNewDirectorResource();

InitForTest();

Expand Down Expand Up @@ -415,8 +431,8 @@ TEST(bsock, auth_works_with_tls_psk)
server_cons_name = client_cons_name;
server_cons_password = client_cons_password;

ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
DirectorResource *dir = CreateAndInitializeNewDirectorResource();
directordaemon::ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
console::DirectorResource *dir = CreateAndInitializeNewDirectorResource();

InitForTest();

Expand Down Expand Up @@ -455,8 +471,8 @@ TEST(bsock, auth_fails_with_different_names_with_tls_psk)
server_cons_name = "differentclientname";
server_cons_password = client_cons_password;

ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
DirectorResource *dir = CreateAndInitializeNewDirectorResource();
directordaemon::ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
console::DirectorResource *dir = CreateAndInitializeNewDirectorResource();

InitForTest();

Expand Down Expand Up @@ -495,8 +511,8 @@ TEST(bsock, auth_works_with_tls_cert)
server_cons_name = client_cons_name;
server_cons_password = client_cons_password;

ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
DirectorResource *dir = CreateAndInitializeNewDirectorResource();
directordaemon::ConsoleResource *cons = CreateAndInitializeNewConsoleResource();
console::DirectorResource *dir = CreateAndInitializeNewDirectorResource();

InitForTest();

Expand Down
6 changes: 6 additions & 0 deletions core/src/lib/unittests/bsock_test.h
Expand Up @@ -25,9 +25,15 @@
#define BSOCK_TEST_PORT_NUMBER 54321

class BareosSocket;
class StorageResource;

namespace directordaemon {
class StorageResource;
}

int create_accepted_server_socket(int port);
BareosSocket *create_new_bareos_socket(int fd);

directordaemon::StorageResource *CreateAndInitializeNewStorageResource();

#endif /* BAREOS_LIB_UNITTESTS_BSOCK_TEST_H_ */
2 changes: 1 addition & 1 deletion core/src/stored/ndmp_tape.cc
Expand Up @@ -1178,7 +1178,7 @@ extern "C" void *handle_ndmp_client_request(void *arg)

sess->param = (struct ndm_session_param *)malloc(sizeof(struct ndm_session_param));
memset(sess->param, 0, sizeof(struct ndm_session_param));
sess->param->log.deliver = NdmpLoghandler;
sess->param->log.deliver = storagedaemon::NdmpLoghandler;
nis = (NIS *)malloc(sizeof(NIS));
sess->param->log.ctx = nis;
sess->param->log_level = NativeToNdmpLoglevel(debug_level, nis);
Expand Down

0 comments on commit 911af61

Please sign in to comment.