-
Notifications
You must be signed in to change notification settings - Fork 256
/
lib_tests.cc
337 lines (276 loc) · 10.3 KB
/
lib_tests.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2022 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.
*/
#if defined(HAVE_MINGW)
# include "include/bareos.h"
# include "gtest/gtest.h"
#else
# include "gtest/gtest.h"
# include "include/bareos.h"
#endif
#include "include/version_numbers.h"
#define BAREOS_TEST_LIB
#include "lib/bnet.h"
#include "lib/bstringlist.h"
#include "lib/ascii_control_characters.h"
#include "lib/util.h"
TEST(BStringList, ConstructorsTest)
{
BStringList list1;
EXPECT_TRUE(list1.empty());
list1.emplace_back(std::string("Test123"));
EXPECT_EQ(0, list1.at(0).compare(std::string("Test123")));
BStringList list2(list1);
EXPECT_EQ(1, list2.size());
EXPECT_EQ(0, list2.front().compare(std::string("Test123")));
}
TEST(BStringList, AppendTest)
{
BStringList list1;
std::vector<std::string> list{"T", "est", "123"};
list1.Append(list);
EXPECT_EQ(0, list1.front().compare(std::string("T")));
list1.erase(list1.begin());
EXPECT_EQ(0, list1.front().compare(std::string("est")));
list1.erase(list1.begin());
EXPECT_EQ(0, list1.front().compare(std::string("123")));
BStringList list2;
list2.Append('T');
list2.Append('e');
EXPECT_EQ(0, list2.front().compare(std::string("T")));
list2.erase(list2.begin());
EXPECT_EQ(0, list2.front().compare(std::string("e")));
}
TEST(BStringList, JoinTest)
{
BStringList list1;
list1 << "Test";
list1 << 1 << 23;
EXPECT_EQ(3, list1.size());
EXPECT_STREQ(list1.Join().c_str(), "Test123");
EXPECT_STREQ(list1.Join(' ').c_str(), "Test 1 23");
BStringList list2;
list2.Append("Test");
list2.Append("123");
std::string s = list2.Join(AsciiControlCharacters::RecordSeparator());
EXPECT_EQ(8, s.size());
std::string test{"Test"};
test += AsciiControlCharacters::RecordSeparator(); // 0x1e
test += "123";
EXPECT_STREQ(s.c_str(), test.c_str());
}
TEST(BStringList, SplitStringTest)
{
std::string test{"Test@123@String"};
BStringList list1(test, '@');
EXPECT_EQ(3, list1.size());
EXPECT_STREQ("Test", list1.front().c_str());
list1.erase(list1.begin());
EXPECT_STREQ("123", list1.front().c_str());
list1.erase(list1.begin());
EXPECT_STREQ("String", list1.front().c_str());
}
TEST(BStringList, SplitStringTestStringSeparator)
{
std::string test{"Test::123::String::::"};
BStringList list1(test, "::");
EXPECT_EQ(5, list1.size());
EXPECT_STREQ("Test", list1.front().c_str());
list1.erase(list1.begin());
EXPECT_STREQ("123", list1.front().c_str());
list1.erase(list1.begin());
EXPECT_STREQ("String", list1.front().c_str());
}
TEST(BNet, ReadoutCommandIdFromStringTest)
{
bool ok;
uint32_t id;
std::string message1 = "1000";
message1 += 0x1e;
message1 += "OK: <director-name> Version: <version>";
BStringList list_of_arguments1(message1, 0x1e);
ok = ReadoutCommandIdFromMessage(list_of_arguments1, id);
EXPECT_EQ(id, kMessageIdOk);
EXPECT_EQ(ok, true);
std::string message2 = "1001";
message2 += 0x1e;
message2 += "OK: <director-name> Version: <version>";
BStringList list_of_arguments2(message2, 0x1e);
ok = ReadoutCommandIdFromMessage(list_of_arguments2, id);
EXPECT_NE(id, kMessageIdOk);
EXPECT_EQ(ok, true);
}
TEST(BNet, EvaluateResponseMessage_Wrong_Id)
{
bool ok;
std::string message3 = "A1001";
message3 += 0x1e;
message3 += "OK: <director-name> Version: <version>";
uint32_t id = kMessageIdUnknown;
BStringList args;
ok = EvaluateResponseMessageId(message3, id, args);
EXPECT_EQ(id, kMessageIdUnknown);
EXPECT_EQ(ok, false);
const char* m3{"A1001 OK: <director-name> Version: <version>"};
EXPECT_STREQ(args.JoinReadable().c_str(), m3);
}
TEST(BNet, EvaluateResponseMessage_Correct_Id)
{
bool ok;
uint32_t id;
std::string message4 = "1001";
message4 += 0x1e;
message4 += "OK: <director-name> Version: <version>";
BStringList args;
ok = EvaluateResponseMessageId(message4, id, args);
EXPECT_EQ(id, kMessageIdPamRequired);
EXPECT_EQ(ok, true);
const char* m3{"1001 OK: <director-name> Version: <version>"};
EXPECT_STREQ(args.JoinReadable().c_str(), m3);
}
enum
{
R_DIRECTOR = 1,
R_CLIENT,
R_JOB,
R_STORAGE,
R_CONSOLE
};
#include "lib/qualified_resource_name_type_converter.h"
static void do_get_name_from_hello_test(const char* client_string_fmt,
const char* client_name,
const std::string& r_type_test,
const BareosVersionNumber& version_test)
{
char bashed_client_name[20];
assert(strlen(client_name) <= 19);
strncpy(bashed_client_name, client_name, 20);
bashed_client_name[19] = '\0';
BashSpaces(bashed_client_name);
char output_text[64];
sprintf(output_text, client_string_fmt, bashed_client_name);
std::string name;
std::string r_type_str;
BareosVersionNumber version = BareosVersionNumber::kUndefined;
bool ok = GetNameAndResourceTypeAndVersionFromHello(output_text, name,
r_type_str, version);
EXPECT_TRUE(ok);
EXPECT_STREQ(name.c_str(), client_name);
EXPECT_STREQ(r_type_str.c_str(), r_type_test.c_str());
EXPECT_EQ(version, version_test);
}
TEST(Util, get_name_from_hello_test)
{
// clang-format off
do_get_name_from_hello_test("Hello Client %s calling",
"Test Client", "R_CLIENT", BareosVersionNumber::kUndefined);
do_get_name_from_hello_test("Hello Storage calling Start Job %s",
"Test Client", "R_JOB", BareosVersionNumber::kUndefined);
do_get_name_from_hello_test("Hello %s",
"Console Name", "R_CONSOLE", BareosVersionNumber::kUndefined);
do_get_name_from_hello_test("Hello %s",
"*UserAgent*", "R_CONSOLE", BareosVersionNumber::kUndefined);
do_get_name_from_hello_test("Hello %s",
"*UserAgent*", "R_CONSOLE", BareosVersionNumber::kUndefined);
do_get_name_from_hello_test("Hello %s calling version 18.2.4rc2",
"Console", "R_CONSOLE", BareosVersionNumber::kRelease_18_2);
do_get_name_from_hello_test("Hello Director %s calling\n",
"bareos dir", "R_DIRECTOR", BareosVersionNumber::kUndefined);
do_get_name_from_hello_test("Hello Start Storage Job %s",
"Test Job", "R_JOB", BareosVersionNumber::kUndefined);
do_get_name_from_hello_test("Hello Client %s FdProtocolVersion=123 calling\n",
"Test Client again", "R_CLIENT", BareosVersionNumber::kUndefined);
// clang-format on
}
TEST(Util, version_number_test)
{
EXPECT_EQ(BareosVersionNumber::kRelease_18_2,
static_cast<BareosVersionNumber>(1802));
EXPECT_EQ(BareosVersionNumber::kUndefined,
static_cast<BareosVersionNumber>(1));
EXPECT_NE(BareosVersionNumber::kRelease_18_2,
static_cast<BareosVersionNumber>(1702));
EXPECT_GT(BareosVersionNumber::kRelease_18_2,
BareosVersionNumber::kUndefined);
}
TEST(Util, version_number_major_minor)
{
BareosVersionNumber version = BareosVersionNumber::kRelease_18_2;
BareosVersionToMajorMinor v(version);
EXPECT_EQ(v.major, 18);
EXPECT_EQ(v.minor, 2);
}
#include "filed/evaluate_job_command.h"
TEST(Filedaemon, evaluate_jobcommand_from_18_2_test)
{
/* command with ssl argument */
static const char jobcmd_kVersionFrom_18_2[]
= "JobId=111 Job=FirstJob SDid=222 SDtime=333 Authorization=SecretOne "
"ssl=4\n";
filedaemon::JobCommand eval(jobcmd_kVersionFrom_18_2);
EXPECT_TRUE(eval.EvaluationSuccesful());
EXPECT_EQ(eval.protocol_version_,
filedaemon::JobCommand::ProtocolVersion::kVersionFrom_18_2);
EXPECT_EQ(eval.job_id_, 111);
EXPECT_STREQ(eval.job_, "FirstJob");
EXPECT_EQ(eval.vol_session_id_, 222);
EXPECT_EQ(eval.vol_session_time_, 333);
EXPECT_STREQ(eval.sd_auth_key_, "SecretOne");
EXPECT_EQ(eval.tls_policy_, 4);
}
TEST(Filedaemon, evaluate_jobcommand_before_18_2_test)
{
/* command without ssl argument */
static char jobcmdssl_KVersionBefore_18_2[]
= "JobId=123 Job=SecondJob SDid=456 SDtime=789 Authorization=SecretTwo";
filedaemon::JobCommand eval(jobcmdssl_KVersionBefore_18_2);
EXPECT_TRUE(eval.EvaluationSuccesful());
EXPECT_EQ(eval.protocol_version_,
filedaemon::JobCommand::ProtocolVersion::KVersionBefore_18_2);
EXPECT_EQ(eval.job_id_, 123);
EXPECT_STREQ(eval.job_, "SecondJob");
EXPECT_EQ(eval.vol_session_id_, 456);
EXPECT_EQ(eval.vol_session_time_, 789);
EXPECT_STREQ(eval.sd_auth_key_, "SecretTwo");
}
TEST(Filedaemon, evaluate_jobcommand_wrong_format_test)
{
/* malformed command */
static char jobcmdssl_KVersionBefore_18_2[] = "JobId=123 Job=Foo";
filedaemon::JobCommand eval(jobcmdssl_KVersionBefore_18_2);
EXPECT_FALSE(eval.EvaluationSuccesful());
EXPECT_EQ(eval.protocol_version_,
filedaemon::JobCommand::ProtocolVersion::kVersionUndefinded);
}
TEST(StringManipulation,
DelimitedSting_PrintsEmptySingleElementWhenNoElementIsGiven)
{
std::vector<char> jobtypes{};
std::string result = CreateDelimitedStringForSqlQueries(jobtypes, ',');
EXPECT_STREQ(result.c_str(), "''");
}
TEST(StringManipulation, DelimitedSting_PrintsCorrectSingleCharacter)
{
std::vector<char> jobtypes{65};
std::string result = CreateDelimitedStringForSqlQueries(jobtypes, ',');
EXPECT_STREQ(result.c_str(), "'A'");
}
TEST(StringManipulation, DelimitedString_PrintsCorrectMultipleCharacters)
{
std::vector<char> jobtypes{65, 66, 67, 68, 69, 70};
std::string result = CreateDelimitedStringForSqlQueries(jobtypes, ',');
EXPECT_STREQ(result.c_str(), "'A','B','C','D','E','F'");
}