1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2007-2012 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2013-2020 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation, which is
11    listed in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 /*
24  * extracted the TEST_PROGRAM functionality from filed/fd_plugins.cc
25  * and adapted for gtest
26  *
27  * Andreas Rogge, Feb 2019
28  */
29 
30 /**
31  * @file
32  * Bareos pluginloader
33  */
34 #if defined(HAVE_MINGW)
35 #  include "include/bareos.h"
36 #  include "gtest/gtest.h"
37 #else
38 #  include "gtest/gtest.h"
39 #  include "include/bareos.h"
40 #endif
41 
42 #include "filed/filed.h"
43 #include "filed/filed_globals.h"
44 #include "filed/accurate.h"
45 #include "filed/heartbeat.h"
46 #include "filed/fileset.h"
47 #include "filed/heartbeat.h"
48 #include "findlib/attribs.h"
49 #include "findlib/find.h"
50 #include "findlib/find_one.h"
51 #include "findlib/hardlink.h"
52 
53 /**
54  * Function pointers to be set here (findlib)
55  */
56 extern int (*plugin_bopen)(BareosWinFilePacket* bfd,
57                            const char* fname,
58                            int flags,
59                            mode_t mode);
60 extern int (*plugin_bclose)(BareosWinFilePacket* bfd);
61 extern ssize_t (*plugin_bread)(BareosWinFilePacket* bfd,
62                                void* buf,
63                                size_t count);
64 extern ssize_t (*plugin_bwrite)(BareosWinFilePacket* bfd,
65                                 void* buf,
66                                 size_t count);
67 extern boffset_t (*plugin_blseek)(BareosWinFilePacket* bfd,
68                                   boffset_t offset,
69                                   int whence);
70 
71 extern char* exepath;
72 extern char* version;
73 
74 namespace filedaemon {
75 
76 int SaveFile(JobControlRecord*, FindFilesPacket*, bool);
77 bool SetCmdPlugin(BareosWinFilePacket*, JobControlRecord*);
78 
79 /* Exported variables */
80 ClientResource* me; /* my resource */
81 
SaveFile(JobControlRecord *,FindFilesPacket *,bool)82 int SaveFile(JobControlRecord*, FindFilesPacket*, bool) { return 0; }
83 
AccurateMarkFileAsSeen(JobControlRecord *,char *)84 bool AccurateMarkFileAsSeen(JobControlRecord*, char*) { return true; }
85 
AccurateMarkAllFilesAsSeen(JobControlRecord *)86 bool AccurateMarkAllFilesAsSeen(JobControlRecord*) { return true; }
87 
accurate_unMarkFileAsSeen(JobControlRecord *,char *)88 bool accurate_unMarkFileAsSeen(JobControlRecord*, char*) { return true; }
89 
accurate_unMarkAllFilesAsSeen(JobControlRecord *)90 bool accurate_unMarkAllFilesAsSeen(JobControlRecord*) { return true; }
91 
SetCmdPlugin(BareosWinFilePacket *,JobControlRecord *)92 bool SetCmdPlugin(BareosWinFilePacket*, JobControlRecord*) { return true; }
93 
TEST(fd,fd_plugins)94 TEST(fd, fd_plugins)
95 {
96   char plugin_dir[PATH_MAX];
97   JobControlRecord mjcr1, mjcr2;
98   JobControlRecord* jcr1 = &mjcr1;
99   JobControlRecord* jcr2 = &mjcr2;
100 
101   InitMsg(NULL, NULL);
102 
103   OSDependentInit();
104 
105   (void)!getcwd(plugin_dir, sizeof(plugin_dir) - 1);
106 
107   LoadFdPlugins(plugin_dir, NULL);
108 
109   jcr1->JobId = 111;
110   NewPlugins(jcr1);
111 
112   jcr2->JobId = 222;
113   NewPlugins(jcr2);
114 
115   EXPECT_EQ(GeneratePluginEvent(jcr1, bEventJobStart, (void*)"Start Job 1"),
116             bRC_OK);
117   EXPECT_EQ(GeneratePluginEvent(jcr1, bEventJobEnd), bRC_OK);
118   EXPECT_EQ(GeneratePluginEvent(jcr2, bEventJobStart, (void*)"Start Job 2"),
119             bRC_OK);
120   FreePlugins(jcr1);
121   EXPECT_EQ(GeneratePluginEvent(jcr2, bEventJobEnd), bRC_OK);
122   FreePlugins(jcr2);
123 
124   UnloadFdPlugins();
125 
126   TermMsg();
127   CloseMemoryPool();
128 }
129 } /* namespace filedaemon */
130