1 /**
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2018-2018 Bareos GmbH & Co. KG
5 
6    This program is Free Software; you can redistribute it and/or
7    modify it under the terms of version three of the GNU Affero General Public
8    License as published by the Free Software Foundation and included
9    in the file LICENSE.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14    Affero General Public License for more details.
15 
16    You should have received a copy of the GNU Affero General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.
20 */
21 
22 #include "gtest/gtest.h"
23 #include "include/bareos.h"
24 #define BAREOS_TEST_LIB
25 #include "lib/bnet.h"
26 #include "lib/bstringlist.h"
27 
TEST(BStringList,ConstructorsTest)28 TEST(BStringList, ConstructorsTest)
29 {
30   BStringList list1;
31   EXPECT_TRUE(list1.empty());
32 
33   list1.emplace_back(std::string("Test123"));
34   EXPECT_EQ(0, list1.at(0).compare(std::string("Test123")));
35 
36   BStringList list2(list1);
37   EXPECT_EQ(1, list2.size());
38   EXPECT_EQ(0, list2.front().compare(std::string("Test123")));
39 }
40 
TEST(BStringList,AppendTest)41 TEST(BStringList, AppendTest)
42 {
43   BStringList list1;
44   std::vector<std::string> list{"T", "est", "123"};
45   list1.Append(list);
46   EXPECT_EQ(0, list1.front().compare(std::string("T")));
47   list1.erase(list1.begin());
48   EXPECT_EQ(0, list1.front().compare(std::string("est")));
49   list1.erase(list1.begin());
50   EXPECT_EQ(0, list1.front().compare(std::string("123")));
51 
52   BStringList list2;
53   list2.Append('T');
54   list2.Append('e');
55   EXPECT_EQ(0, list2.front().compare(std::string("T")));
56   list2.erase(list2.begin());
57   EXPECT_EQ(0, list2.front().compare(std::string("e")));
58 }
59 
TEST(BStringList,JoinTest)60 TEST(BStringList, JoinTest)
61 {
62   BStringList list1;
63   list1 << "Test";
64   list1 << 1 << 23;
65   EXPECT_EQ(3, list1.size());
66   EXPECT_STREQ(list1.Join().c_str(), "Test123");
67   EXPECT_STREQ(list1.Join(' ').c_str(), "Test 1 23");
68 
69   BStringList list2;
70   list2.Append("Test");
71   list2.Append("123");
72 
73   std::string s = list2.Join(AsciiControlCharacters::RecordSeparator());
74   EXPECT_EQ(8, s.size());
75 
76   std::string test {"Test"};
77   test += AsciiControlCharacters::RecordSeparator(); // 0x1e
78   test += "123";
79 
80   EXPECT_STREQ(s.c_str(), test.c_str());
81 }
82 
TEST(BStringList,SplitStringTest)83 TEST(BStringList, SplitStringTest)
84 {
85   std::string test {"Test@123@String"};
86   BStringList list1(test, '@');
87   EXPECT_EQ(3, list1.size());
88 
89   EXPECT_STREQ("Test", list1.front().c_str());
90   list1.erase(list1.begin());
91   EXPECT_STREQ("123", list1.front().c_str());
92   list1.erase(list1.begin());
93   EXPECT_STREQ("String", list1.front().c_str());
94 }
95 
TEST(BNet,ReadoutCommandIdFromStringTest)96 TEST(BNet, ReadoutCommandIdFromStringTest)
97 {
98   bool ok;
99   uint32_t id;
100 
101   std::string message1 = "1000";
102   message1 += 0x1e;
103   message1 += "OK: <director-name> Version: <version>";
104   BStringList list_of_arguments1(message1, 0x1e);
105   ok = ReadoutCommandIdFromMessage(list_of_arguments1, id);
106   EXPECT_EQ(id, kMessageIdOk);
107   EXPECT_EQ(ok, true);
108 
109   std::string message2 = "1001";
110   message2 += 0x1e;
111   message2 += "OK: <director-name> Version: <version>";
112   BStringList list_of_arguments2(message2, 0x1e);
113   ok = ReadoutCommandIdFromMessage(list_of_arguments2, id);
114   EXPECT_NE(id, kMessageIdOk);
115   EXPECT_EQ(ok, true);
116 }
117 
TEST(BNet,EvaluateResponseMessage_Wrong_Id)118 TEST(BNet, EvaluateResponseMessage_Wrong_Id)
119 {
120   bool ok;
121 
122   std::string message3 = "A1001";
123   message3 += 0x1e;
124   message3 += "OK: <director-name> Version: <version>";
125 
126   uint32_t id = kMessageIdUnknown;
127   BStringList args;
128   ok = EvaluateResponseMessageId(message3, id, args);
129 
130   EXPECT_EQ(id, kMessageIdUnknown);
131   EXPECT_EQ(ok, false);
132 
133   const char *m3 {"A1001 OK: <director-name> Version: <version>"};
134   EXPECT_STREQ(args.JoinReadable().c_str(), m3);
135 }
136 
TEST(BNet,EvaluateResponseMessage_Correct_Id)137 TEST(BNet, EvaluateResponseMessage_Correct_Id)
138 {
139   bool ok;
140   uint32_t id;
141 
142   std::string message4 = "1001";
143   message4 += 0x1e;
144   message4 += "OK: <director-name> Version: <version>";
145 
146   BStringList args;
147   ok = EvaluateResponseMessageId(message4, id, args);
148 
149   EXPECT_EQ(id, kMessageIdPamRequired);
150   EXPECT_EQ(ok, true);
151 
152   const char *m3 {"1001 OK: <director-name> Version: <version>"};
153   EXPECT_STREQ(args.JoinReadable().c_str(), m3);
154 }
155 
156 enum {
157   R_DIRECTOR = 1, R_CLIENT, R_JOB, R_STORAGE, R_CONSOLE
158 };
159 
160 #include "lib/qualified_resource_name_type_converter.h"
161 
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)162 static void do_get_name_from_hello_test(const char *client_string_fmt,
163                                         const char *client_name,
164                                         const std::string &r_type_test,
165                                         const BareosVersionNumber &version_test)
166 {
167   char bashed_client_name[20];
168   sprintf(bashed_client_name, "%s", client_name);
169   BashSpaces(bashed_client_name);
170 
171   char output_text[64];
172   sprintf(output_text, client_string_fmt, bashed_client_name);
173 
174   std::string name;
175   std::string r_type_str;
176   BareosVersionNumber version = BareosVersionNumber::kUndefined;
177 
178   bool ok = GetNameAndResourceTypeAndVersionFromHello(output_text, name, r_type_str, version);
179 
180   EXPECT_TRUE(ok);
181   EXPECT_STREQ(name.c_str(), client_name);
182   EXPECT_STREQ(r_type_str.c_str(), r_type_test.c_str());
183   EXPECT_EQ(version, version_test);
184 }
185 
TEST(Util,get_name_from_hello_test)186 TEST(Util, get_name_from_hello_test)
187 {
188   // clang-format off
189   do_get_name_from_hello_test("Hello Client %s calling",
190                               "Test Client",        "R_CLIENT",   BareosVersionNumber::kUndefined);
191   do_get_name_from_hello_test("Hello Storage calling Start Job %s",
192                               "Test Client",        "R_JOB",      BareosVersionNumber::kUndefined);
193   do_get_name_from_hello_test("Hello %s",
194                               "Console Name",       "R_CONSOLE",  BareosVersionNumber::kUndefined);
195   do_get_name_from_hello_test("Hello %s",
196                               "*UserAgent*",        "R_CONSOLE",  BareosVersionNumber::kUndefined);
197   do_get_name_from_hello_test("Hello %s",
198                               "*UserAgent*",        "R_CONSOLE",  BareosVersionNumber::kUndefined);
199   do_get_name_from_hello_test("Hello %s calling version 18.2.4rc2",
200                               "Console",            "R_CONSOLE",  BareosVersionNumber::kRelease_18_2);
201   do_get_name_from_hello_test("Hello Director %s calling\n",
202                               "bareos dir",         "R_DIRECTOR", BareosVersionNumber::kUndefined);
203   do_get_name_from_hello_test("Hello Start Storage Job %s",
204                               "Test Job",           "R_JOB",      BareosVersionNumber::kUndefined);
205   do_get_name_from_hello_test("Hello Client %s FdProtocolVersion=123 calling\n",
206                               "Test Client again",  "R_CLIENT",    BareosVersionNumber::kUndefined);
207   // clang-format on
208 }
209 
TEST(Util,version_number_test)210 TEST(Util, version_number_test)
211 {
212   EXPECT_EQ(BareosVersionNumber::kRelease_18_2, static_cast<BareosVersionNumber>(1802));
213   EXPECT_EQ(BareosVersionNumber::kUndefined, static_cast<BareosVersionNumber>(1));
214   EXPECT_NE(BareosVersionNumber::kRelease_18_2, static_cast<BareosVersionNumber>(1702));
215   EXPECT_GT(BareosVersionNumber::kRelease_18_2, BareosVersionNumber::kUndefined);
216 }
217 
TEST(Util,version_number_major_minor)218 TEST(Util, version_number_major_minor)
219 {
220   BareosVersionNumber version = BareosVersionNumber::kRelease_18_2;
221   BareosVersionToMajorMinor v(version);
222   EXPECT_EQ(v.major, 18);
223   EXPECT_EQ(v.minor,  2);
224 }
225 
226 #include "filed/evaluate_job_command.h"
227 
TEST(Filedaemon,evaluate_jobcommand_from_18_2_test)228 TEST(Filedaemon, evaluate_jobcommand_from_18_2_test)
229 {
230   /* command with ssl argument */
231   static const char jobcmd_kVersionFrom_18_2[]      = "JobId=111 Job=FirstJob SDid=222 SDtime=333 Authorization=SecretOne ssl=4\n";
232 
233   filedaemon::JobCommand eval(jobcmd_kVersionFrom_18_2);
234 
235   EXPECT_TRUE(eval.EvaluationSuccesful());
236   EXPECT_EQ(eval.protocol_version_, filedaemon::JobCommand::ProtocolVersion::kVersionFrom_18_2);
237   EXPECT_EQ(eval.job_id_, 111);
238   EXPECT_STREQ(eval.job_, "FirstJob");
239   EXPECT_EQ(eval.vol_session_id_, 222);
240   EXPECT_EQ(eval.vol_session_time_, 333);
241   EXPECT_STREQ(eval.sd_auth_key_, "SecretOne");
242   EXPECT_EQ(eval.tls_policy_, 4);
243 }
244 
TEST(Filedaemon,evaluate_jobcommand_before_18_2_test)245 TEST(Filedaemon, evaluate_jobcommand_before_18_2_test)
246 {
247   /* command without ssl argument */
248   static char jobcmdssl_KVersionBefore_18_2[] = "JobId=123 Job=SecondJob SDid=456 SDtime=789 Authorization=SecretTwo";
249 
250   filedaemon::JobCommand eval(jobcmdssl_KVersionBefore_18_2);
251 
252   EXPECT_TRUE(eval.EvaluationSuccesful());
253   EXPECT_EQ(eval.protocol_version_, filedaemon::JobCommand::ProtocolVersion::KVersionBefore_18_2);
254   EXPECT_EQ(eval.job_id_, 123);
255   EXPECT_STREQ(eval.job_, "SecondJob");
256   EXPECT_EQ(eval.vol_session_id_, 456);
257   EXPECT_EQ(eval.vol_session_time_, 789);
258   EXPECT_STREQ(eval.sd_auth_key_, "SecretTwo");
259 }
260 
TEST(Filedaemon,evaluate_jobcommand_wrong_format_test)261 TEST(Filedaemon, evaluate_jobcommand_wrong_format_test)
262 {
263   /* malformed command  */
264   static char jobcmdssl_KVersionBefore_18_2[] = "JobId=123 Job=Foo";
265 
266   filedaemon::JobCommand eval(jobcmdssl_KVersionBefore_18_2);
267 
268   EXPECT_FALSE(eval.EvaluationSuccesful());
269   EXPECT_EQ(eval.protocol_version_, filedaemon::JobCommand::ProtocolVersion::kVersionUndefinded);
270 }
271