Skip to content

Commit

Permalink
stored: added sd server to tests
Browse files Browse the repository at this point in the history
- some global symbols moved to separate file
- renamed symbols to meet our standard
- added object files from stored to bsock_test CMake
  • Loading branch information
franku committed Aug 16, 2018
1 parent d2ceac2 commit d2cb7ed
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 45 deletions.
36 changes: 34 additions & 2 deletions core/src/lib/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,43 @@ set_property(TEST test_lib

add_dependencies(check test_lib)

set (STORED_OBJS
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/append.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/askdir.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/authenticate.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/dir_cmd.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/fd_cmds.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/job.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/mac.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/ndmp_tape.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/read.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/sd_cmds.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/sd_stats.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/socket_server.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/status.cc.o
${CMAKE_BINARY_DIR}/src/stored/CMakeFiles/bareos-sd.dir/stored_globals.cc.o
)

SET_SOURCE_FILES_PROPERTIES (
${STORED_OBJS}
PROPERTIES
EXTERNAL_OBJECTS true
GENERATED true
)

add_executable(test_bsock
bsock_test.cc bsock_constructor_test.cc bsock_cert_verify_common_names_test.cc
)
bsock_test.cc
bsock_dir_sd_connection_test.cc
bsock_constructor_test.cc
bsock_cert_verify_common_names_test.cc
${STORED_OBJS}
)

target_link_libraries(test_bsock
bareos
bareossd
bareoscfg
bareosndmp
${JANSSON_LIBRARIES}
${GTEST_LIBRARIES}
${GTEST_MAIN_LIBRARIES}
Expand Down
35 changes: 35 additions & 0 deletions core/src/lib/unittests/bsock_dir_sd_connection_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2018 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "bsock_test.h"
#include "gtest/gtest.h"
#include "stored/socket_server.h"

static void start_sd_server()
{
int newsockfd = create_accepted_server_socket(BSOCK_TEST_PORT_NUMBER);
BareosSocket *bs = create_new_bareos_socket(newsockfd);
HandleConnectionRequest(bs);
}

TEST(bsock_dir_sd, dir_to_sd_connection_test)
{
// start_sd_server();
}
27 changes: 17 additions & 10 deletions core/src/lib/unittests/bsock_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "bsock_test.h"
#include "gtest/gtest.h"
#include <iostream>
#include <fstream>
Expand All @@ -43,7 +44,6 @@ class StorageResource;

#define MIN_MSG_LEN 15
#define MAX_MSG_LEN 175 + 25
#define PORT 54321
#define HOST "127.0.0.1"
#define CONSOLENAME "testconsole"
#define CONSOLEPASSWORD "secret"
Expand Down Expand Up @@ -98,6 +98,18 @@ int create_accepted_server_socket(int port)
return new_socket;
}

BareosSocket *create_new_bareos_socket(int fd)
{
BareosSocket *bs;
bs = New(BareosSocketTCP);
bs->sleep_time_after_authentication_error = 0;
bs->fd_ = fd;
bs->SetWho(bstrdup("client"));
memset(&bs->peer_addr, 0, sizeof(bs->peer_addr));
return bs;
}


#if 0
static bool check_cipher(const TlsResource &tls, const std::string &cipher)
{
Expand All @@ -111,7 +123,7 @@ static bool check_cipher(const TlsResource &tls, const std::string &cipher)
}
#endif

void clone_a_server_socket(BareosSocket* bs)
static void clone_a_server_socket(BareosSocket* bs)
{
BareosSocket *bs2 = bs->clone();
bs2->fsend("cloned-bareos-socket-0987654321");
Expand All @@ -129,12 +141,7 @@ void start_bareos_server(std::promise<bool> *promise, std::string console_name,
int newsockfd = create_accepted_server_socket(server_port);
bool success = false;

BareosSocket *bs;
bs = New(BareosSocketTCP);
bs->sleep_time_after_authentication_error = 0;
bs->fd_ = newsockfd;
bs->SetWho(bstrdup("client"));
memset(&bs->peer_addr, 0, sizeof(bs->peer_addr));
BareosSocket *bs = create_new_bareos_socket(newsockfd);

char *name = (char *)console_name.c_str();
s_password *password = new (s_password);
Expand Down Expand Up @@ -283,7 +290,7 @@ DirectorResource *CreateAndInitializeNewDirectorResource()
{
DirectorResource *dir = new (DirectorResource);
dir->address = (char *)HOST;
dir->DIRport = htons(PORT);
dir->DIRport = htons(BSOCK_TEST_PORT_NUMBER);
dir->tls_psk.enable = false;
dir->tls_cert.certfile = new (std::string)(CERTDIR "/bareos-dir.bareos.org-cert.pem");
dir->tls_cert.keyfile = new (std::string)(CERTDIR "/bareos-dir.bareos.org-key.pem");
Expand All @@ -300,7 +307,7 @@ std::string client_cons_password;
std::string server_cons_name;
std::string server_cons_password;

int port = PORT;
int port = BSOCK_TEST_PORT_NUMBER;

TEST(bsock, auth_works)
{
Expand Down
33 changes: 33 additions & 0 deletions core/src/lib/unittests/bsock_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2018 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/

#ifndef BAREOS_LIB_UNITTESTS_BSOCK_TEST_H_
#define BAREOS_LIB_UNITTESTS_BSOCK_TEST_H_

#define BSOCK_TEST_PORT_NUMBER 54321

class BareosSocket;

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


#endif /* BAREOS_LIB_UNITTESTS_BSOCK_TEST_H_ */
6 changes: 3 additions & 3 deletions core/src/stored/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ set (LIBBAREOSSD_SRCS acquire.cc ansi_label.cc askdir.cc autochanger.cc
${AVAILABLE_DEVICE_API_SRCS}
)

set(SDSRCS append.cc askdir.cc authenticate.cc dir_cmd.cc fd_cmds.cc
set(SDSRCS stored_globals.cc append.cc askdir.cc authenticate.cc dir_cmd.cc fd_cmds.cc
job.cc mac.cc ndmp_tape.cc read.cc sd_cmds.cc sd_stats.cc
socket_server.cc status.cc stored.cc
)
socket_server.cc status.cc stored.cc )

IF(HAVE_WIN32)
LIST(APPEND SDSRCS
../win32/generic/main.cc
Expand Down
2 changes: 1 addition & 1 deletion core/src/stored/dir_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "stored/parse_bsr.h"
#include "stored/sd_cmds.h"
#include "stored/sd_stats.h"
#include "stored/stored_globals.h"
#include "stored/wait.h"
#include "lib/bnet.h"
#include "lib/edit.h"
Expand All @@ -62,7 +63,6 @@

/* Imported variables */
extern struct s_last_job last_job;
extern bool init_done;

/* Commands received from director that need scanning */
static char setbandwidth[] =
Expand Down
7 changes: 4 additions & 3 deletions core/src/stored/job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "stored/ndmp_tape.h"
#include "stored/parse_bsr.h"
#include "stored/read_record.h"
#include "stored/stored_globals.h"
#include "lib/edit.h"
#include "include/jcr.h"

Expand All @@ -44,7 +45,7 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
extern uint32_t VolSessionTime;

/* Imported functions */
extern uint32_t newVolSessionId();
extern uint32_t NewVolSessionId();

/* Requests from the Director daemon */
static char jobcmd[] =
Expand Down Expand Up @@ -130,8 +131,8 @@ bool job_cmd(JobControlRecord *jcr)
* are given to us (same as restarted job).
*/
if (!jcr->rerunning) {
jcr->VolSessionId = newVolSessionId();
jcr->VolSessionTime = VolSessionTime;
jcr->VolSessionId = NewVolSessionId();
jcr->VolSessionTime = vol_session_time;
}
bstrncpy(jcr->Job, job, sizeof(jcr->Job));
UnbashSpaces(job_name);
Expand Down
2 changes: 1 addition & 1 deletion core/src/stored/socket_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static pthread_t tcp_server_tid;
* - If it was a connection from another SD, call handle_stored_connection()
* - Otherwise it was a connection from the DIR, call handle_director_connection()
*/
static void *HandleConnectionRequest(void *arg)
void *HandleConnectionRequest(void *arg)
{
BareosSocket *bs = (BareosSocket *)arg;
char name[MAX_NAME_LENGTH];
Expand Down
3 changes: 3 additions & 0 deletions core/src/stored/socket_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
#ifndef BAREOS_STORED_SOCKET_SERVER_H_
#define BAREOS_STORED_SOCKET_SERVER_H_

struct dlist;

void StartSocketServer(dlist *addrs);
void StopSocketServer();
void *HandleConnectionRequest(void *arg);

#endif // BAREOS_STORED_SOCKET_SERVER_H_
28 changes: 3 additions & 25 deletions core/src/stored/stored.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "stored/sd_backends.h"
#include "stored/sd_stats.h"
#include "stored/socket_server.h"
#include "stored/stored_globals.h"
#include "stored/wait.h"
#include "lib/daemon.h"
#include "lib/bsignal.h"
Expand All @@ -64,19 +65,8 @@ static void CleanUpOldFiles();

extern "C" void *device_initialization(void *arg);

/* Global variables exported */
char OK_msg[] = "3000 OK\n";
char TERM_msg[] = "3999 Terminate\n";

void *start_heap;

static uint32_t VolSessionId = 0;
uint32_t VolSessionTime;
bool init_done = false;

/* Global static variables */
static bool foreground = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

static void usage()
{
Expand Down Expand Up @@ -298,8 +288,8 @@ int main (int argc, char *argv[])
/* Ensure that Volume Session Time and Id are both
* set and are both non-zero.
*/
VolSessionTime = (uint32_t)daemon_start_time;
if (VolSessionTime == 0) { /* paranoid */
vol_session_time = (uint32_t)daemon_start_time;
if (vol_session_time == 0) { /* paranoid */
Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
}

Expand Down Expand Up @@ -340,18 +330,6 @@ int main (int argc, char *argv[])
return 0;
}

/* Return a new Session Id */
uint32_t newVolSessionId()
{
uint32_t Id;

P(mutex);
VolSessionId++;
Id = VolSessionId;
V(mutex);
return Id;
}

/* Check Configuration file for necessary info */
static int CheckResources()
{
Expand Down
42 changes: 42 additions & 0 deletions core/src/stored/stored_globals.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2016 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/

#include "stored_globals.h"

void *start_heap = nullptr;
bool init_done = false;
uint32_t vol_session_time;

static std::mutex mutex_;
static uint32_t vol_session_id_ = 0;

uint32_t NewVolSessionId()
{
uint32_t id;

mutex_.lock();
vol_session_id_++;
id = vol_session_id_;
mutex_.unlock();
return id;
}

0 comments on commit d2cb7ed

Please sign in to comment.