1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2019-2020 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 #if defined(HAVE_MINGW)
22 #  include "include/bareos.h"
23 #  include "gtest/gtest.h"
24 #else
25 #  include "gtest/gtest.h"
26 #  include "include/bareos.h"
27 #endif
28 
29 #include "lib/parse_conf.h"
30 #include "dird/dird_globals.h"
31 #include "dird/dird_conf.h"
32 
33 namespace directordaemon {
34 
TEST(ConfigParser_Dir,bareos_configparser_tests)35 TEST(ConfigParser_Dir, bareos_configparser_tests)
36 {
37   OSDependentInit();
38   InitMsg(NULL, NULL); /* initialize message handler */
39 
40   std::string path_to_config_file = std::string(
41       RELATIVE_PROJECT_SOURCE_DIR "/configs/bareos-configparser-tests");
42   my_config = InitDirConfig(path_to_config_file.c_str(), M_ERROR_TERM);
43   my_config->ParseConfig();
44 
45   my_config->DumpResources(PrintMessage, NULL);
46 
47   delete my_config;
48 
49   TermMsg();         /* Terminate message handler */
50   CloseMemoryPool(); /* release free memory in pool */
51 }
52 
TEST(ConfigParser_Dir,runscript_test)53 TEST(ConfigParser_Dir, runscript_test)
54 {
55   OSDependentInit();
56   InitMsg(NULL, NULL); /* initialize message handler */
57 
58   std::string path_to_config_file = std::string(
59       RELATIVE_PROJECT_SOURCE_DIR "/configs/runscript-tests/bareos-dir.conf");
60   my_config = InitDirConfig(path_to_config_file.c_str(), M_ERROR_TERM);
61   my_config->ParseConfig();
62 
63   my_config->DumpResources(PrintMessage, NULL);
64 
65   delete my_config;
66 
67   TermMsg();         /* Terminate message handler */
68   CloseMemoryPool(); /* release free memory in pool */
69 }
70 
test_config_directive_type(std::function<void (DirectorResource *)> test_func)71 void test_config_directive_type(
72     std::function<void(DirectorResource*)> test_func)
73 {
74   std::string test_name = std::string(
75       ::testing::UnitTest::GetInstance()->current_test_info()->name());
76 
77   OSDependentInit();
78   InitMsg(NULL, NULL); /* initialize message handler */
79 
80   std::string path_to_config_file
81       = std::string(RELATIVE_PROJECT_SOURCE_DIR
82                     "/configs/bareos-configparser-tests/bareos-dir-")
83         + test_name + std::string(".conf");
84   my_config = InitDirConfig(path_to_config_file.c_str(), M_ERROR_TERM);
85   my_config->ParseConfig();
86 
87   my_config->DumpResources(PrintMessage, NULL);
88 
89   const char* dir_resource_name = "bareos-dir";
90 
91   DirectorResource* me
92       = (DirectorResource*)my_config->GetNextRes(R_DIRECTOR, NULL);
93   EXPECT_STREQ(dir_resource_name, me->resource_name_);
94 
95   test_func(me);
96 
97   delete my_config;
98 
99   TermMsg();         /* Terminate message handler */
100   CloseMemoryPool(); /* release free memory in pool */
101 }
102 
103 
test_CFG_TYPE_AUDIT(DirectorResource * me)104 void test_CFG_TYPE_AUDIT(DirectorResource* me)
105 {
106   char* val = nullptr;
107   foreach_alist (val, me->audit_events) {
108     printf("AuditEvents = %s\n", val);
109   }
110   EXPECT_EQ(me->audit_events->size(), 8);
111 }
112 
TEST(ConfigParser_Dir,CFG_TYPE_AUDIT)113 TEST(ConfigParser_Dir, CFG_TYPE_AUDIT)
114 {
115   test_config_directive_type(test_CFG_TYPE_AUDIT);
116 }
117 
118 
test_CFG_TYPE_PLUGIN_NAMES(DirectorResource * me)119 void test_CFG_TYPE_PLUGIN_NAMES(DirectorResource* me)
120 {
121   char* val = nullptr;
122   foreach_alist (val, me->plugin_names) {
123     printf("PluginNames = %s\n", val);
124   }
125   EXPECT_EQ(me->plugin_names->size(), 16);
126 }
127 
TEST(ConfigParser_Dir,CFG_TYPE_PLUGIN_NAMES)128 TEST(ConfigParser_Dir, CFG_TYPE_PLUGIN_NAMES)
129 {
130   test_config_directive_type(test_CFG_TYPE_PLUGIN_NAMES);
131 }
132 
133 
test_CFG_TYPE_STR_VECTOR(DirectorResource * me)134 void test_CFG_TYPE_STR_VECTOR(DirectorResource* me)
135 {
136   EXPECT_EQ(me->tls_cert_.allowed_certificate_common_names_.size(), 8);
137 }
138 
TEST(ConfigParser_Dir,CFG_TYPE_STR_VECTOR)139 TEST(ConfigParser_Dir, CFG_TYPE_STR_VECTOR)
140 {
141   test_config_directive_type(test_CFG_TYPE_STR_VECTOR);
142 }
143 
test_CFG_TYPE_STR_VECTOR_OF_DIRS(DirectorResource * me)144 void test_CFG_TYPE_STR_VECTOR_OF_DIRS(DirectorResource* me)
145 {
146   EXPECT_EQ(me->backend_directories.size(), 9);
147   /*
148    *  WIN32:
149    *  cmake uses some value for PATH_BAREOS_BACKENDDIR,
150    *  which ends up in the configuration files
151    *  but this is later overwritten in the Director Daemon with ".".
152    *  Therefore we skip this test.
153    */
154 #if !defined(HAVE_WIN32)
155   EXPECT_EQ(me->backend_directories.at(0), PATH_BAREOS_BACKENDDIR);
156 #endif
157 }
158 
TEST(ConfigParser_Dir,CFG_TYPE_STR_VECTOR_OF_DIRS)159 TEST(ConfigParser_Dir, CFG_TYPE_STR_VECTOR_OF_DIRS)
160 {
161   test_config_directive_type(test_CFG_TYPE_STR_VECTOR_OF_DIRS);
162 }
163 
test_CFG_TYPE_ALIST_STR(DirectorResource * me)164 void test_CFG_TYPE_ALIST_STR(DirectorResource* me)
165 {
166   JobResource* job1 = (JobResource*)my_config->GetResWithName(R_JOB, "job1");
167   EXPECT_STREQ("job1", job1->resource_name_);
168   EXPECT_EQ(job1->run_cmds->size(), 8);
169 
170   JobResource* jobdefs2
171       = (JobResource*)my_config->GetResWithName(R_JOBDEFS, "jobdefs2");
172   EXPECT_STREQ("jobdefs2", jobdefs2->resource_name_);
173   EXPECT_EQ(jobdefs2->run_cmds->size(), 8);
174 
175   JobResource* job2 = (JobResource*)my_config->GetResWithName(R_JOB, "job2");
176   EXPECT_STREQ("job2", job2->resource_name_);
177   EXPECT_EQ(job2->run_cmds->size(), 8);
178 }
179 
TEST(ConfigParser_Dir,CFG_TYPE_ALIST_STR)180 TEST(ConfigParser_Dir, CFG_TYPE_ALIST_STR)
181 {
182   test_config_directive_type(test_CFG_TYPE_ALIST_STR);
183 }
184 
185 
test_CFG_TYPE_ALIST_RES(DirectorResource * me)186 void test_CFG_TYPE_ALIST_RES(DirectorResource* me)
187 {
188   JobResource* job1
189       = (JobResource*)my_config->GetResWithName(R_JOB, "resultjob");
190   EXPECT_STREQ("resultjob", job1->resource_name_);
191   EXPECT_EQ(job1->base->size(), 8);
192 }
193 
TEST(ConfigParser_Dir,CFG_TYPE_ALIST_RES)194 TEST(ConfigParser_Dir, CFG_TYPE_ALIST_RES)
195 {
196   test_config_directive_type(test_CFG_TYPE_ALIST_RES);
197 }
198 
199 
test_CFG_TYPE_STR(DirectorResource * me)200 void test_CFG_TYPE_STR(DirectorResource* me)
201 {
202   /*
203    * Only the first entry from the last "Description" config directive is taken.
204    * This can be considered as a bug.
205    */
206   EXPECT_STREQ(me->description_, "item31");
207 }
208 
TEST(ConfigParser_Dir,CFG_TYPE_STR)209 TEST(ConfigParser_Dir, CFG_TYPE_STR)
210 {
211   test_config_directive_type(test_CFG_TYPE_STR);
212 }
213 
214 
test_CFG_TYPE_FNAME(DirectorResource * me)215 void test_CFG_TYPE_FNAME(DirectorResource* me)
216 {
217   FilesetResource* fileset1
218       = (FilesetResource*)my_config->GetResWithName(R_FILESET, "fileset1");
219   EXPECT_STREQ("fileset1", fileset1->resource_name_);
220   EXPECT_EQ(fileset1->exclude_items.size(), 0);
221   EXPECT_EQ(fileset1->include_items.size(), 1);
222 
223   alist* files = std::addressof(fileset1->include_items.at(0)->name_list);
224   char* val = nullptr;
225   foreach_alist (val, files) {
226     printf("Files = %s\n", val);
227   }
228 }
229 
TEST(ConfigParser_Dir,CFG_TYPE_FNAME)230 TEST(ConfigParser_Dir, CFG_TYPE_FNAME)
231 {
232   test_config_directive_type(test_CFG_TYPE_FNAME);
233 }
234 
235 
236 }  // namespace directordaemon
237