Skip to content

Commit

Permalink
bnet: added ReadoutCommandIdFromString
Browse files Browse the repository at this point in the history
- added a function to read the command id from a message like this
  "1000 OK: <director-name> Version: <version>"
- added a unittest
  • Loading branch information
franku committed Nov 5, 2018
1 parent 02eacc8 commit 80fe781
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
41 changes: 41 additions & 0 deletions core/src/lib/bnet.cc
Expand Up @@ -589,3 +589,44 @@ const char *BnetSigToAscii(BareosSocket * bs)
return buf;
}
}

uint32_t ReadoutCommandIdFromString(std::string message)
{
size_t pos = message.find(' ');
if (pos == std::string::npos) {
return kProtokollError;
}
std::string id_string;
id_string = message.substr(0,pos);

uint32_t id;
try {
id = std::stoul(id_string);
} catch (const std::exception &e) {
id = kProtokollError;
}
return id;
}

uint32_t ReceiveAndEvaluateResponse(BareosSocket *bsock, std::string &message_output)
{
int recv_return_value = bsock->recv();
bsock->StopTimer();

if (recv_return_value <= 0) {
return kReceiveError;
}

Dmsg1(10, "<bsockd: %s", bsock->msg);

std::string message(bsock->msg);
uint32_t id = ReadoutCommandIdFromString(message);

// if (!bstrncmp(bsock->msg, OKhello, sizeof(OKhello) - 1)) {
// Bsnprintf(response, response_len, _("bsockector at \"%s:%d\" rejected Hello command\n"), bsock->host(),
// bsock->port());
// return false;
// } else {
// Bsnprintf(response, response_len, "%s", bsock->msg);
// }
}
13 changes: 13 additions & 0 deletions core/src/lib/bnet.h
Expand Up @@ -49,4 +49,17 @@ int NetConnect(int port);
BareosSocket *BnetBind(int port);
BareosSocket *BnetAccept(BareosSocket *bsock, char *who);

enum : uint32_t {
kUnknown = 0,
kProtokollError = 1,
kReceiveError = 2,
kOk = 1000,
kPamRequired = 1001,
kPamInteractive = 4001,
kPamUserCredentials = 4002
};

uint32_t ReadoutCommandIdFromString(std::string message);
uint32_t ReceiveAndEvaluateResponse(BareosSocket *bsock, std::string &message);

#endif // BAREOS_LIB_BNET_H_
1 change: 1 addition & 0 deletions core/src/tests/CMakeLists.txt
Expand Up @@ -26,6 +26,7 @@ set (TEST_SRC
dlist_test.cc
htable_test.cc
qualified_resource_name_type_converter_test.cc
lib_tests.cc
)


Expand Down
36 changes: 36 additions & 0 deletions core/src/tests/lib_tests.cc
@@ -0,0 +1,36 @@
/**
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 "gtest/gtest.h"
#include "include/bareos.h"
#include "lib/bnet.h"

TEST(ReadoutCommandIdFromStringTest, BNet)
{
int id;
std::string message1 {"1000 OK: <director-name> Version: <version>"};
id = ReadoutCommandIdFromString(message1);
EXPECT_EQ(id, 1000);

std::string message2 {"1001 OK: <director-name> Version: <version>"};
id = ReadoutCommandIdFromString(message2);
EXPECT_NE(id, 1000);
}

0 comments on commit 80fe781

Please sign in to comment.