-
Notifications
You must be signed in to change notification settings - Fork 256
/
globbing_test.cc
192 lines (164 loc) · 6.45 KB
/
globbing_test.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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2021-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, which is
listed 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 "dird/ua_tree.cc"
#include "dird/ua_output.h"
#include "dird/ua.h"
#include "dird/ua_restore.cc"
using namespace directordaemon;
namespace directordaemon {
bool DoReloadConfig() { return false; }
} // namespace directordaemon
int FakeCdCmd(UaContext* ua, TreeContext* tree, std::string path)
{
std::string command = "cd " + path;
PmStrcpy(ua->cmd, command.c_str());
ParseArgsOnly(ua->cmd, ua->args, &ua->argc, ua->argk, ua->argv, MAX_CMD_ARGS);
return cdcmd(ua, tree);
}
int FakeMarkCmd(UaContext* ua, TreeContext* tree, std::string path)
{
std::string command = "mark " + path;
PmStrcpy(ua->cmd, command.c_str());
ParseArgsOnly(ua->cmd, ua->args, &ua->argc, ua->argk, ua->argv, MAX_CMD_ARGS);
return MarkElements(ua, tree);
}
void PopulateTree(std::vector<std::string> files, TreeContext* tree)
{
int pnl, fnl;
PoolMem filename{PM_FNAME};
PoolMem path{PM_FNAME};
for (auto file : files) {
SplitPathAndFilename(file.c_str(), path.addr(), &pnl, filename.addr(),
&fnl);
char* row0 = path.c_str();
char* row1 = filename.c_str();
char row2[] = "1";
char row3[] = "2";
char row4[] = "P0A CF2xg IGk B Po Po A 3Y BAA I BhjA7I BU+HEc BhjA7I A A C";
char row5[] = "0";
char row6[] = "0";
char row7[] = "0";
char* row[] = {row0, row1, row2, row3, row4, row5, row6, row7};
InsertTreeHandler(tree, 0, row);
}
}
class Globbing : public testing::Test {
protected:
void SetUp() override
{
tree.root = new_tree(1);
tree.node = (TREE_NODE*)tree.root;
me = new DirectorResource;
me->optimize_for_size = true;
me->optimize_for_speed = false;
ua = new_ua_context(&jcr);
}
void TearDown() override
{
FreeUaContext(ua);
FreeTree(tree.root);
delete me;
}
JobControlRecord jcr{};
UaContext* ua{nullptr};
TreeContext tree;
};
TEST_F(Globbing, globbing_in_markcmd)
{
const std::vector<std::string> files
= {"/some/weirdfiles/normalefile",
"/some/weirdfiles/nottooweird",
"/some/weirdfiles/potato",
"/some/weirdfiles/potatomashed",
"/some/weirdfiles/subdirectory1/file1",
"/some/weirdfiles/subdirectory1/file2",
"/some/weirdfiles/subdirectory2/file3",
"/some/weirdfiles/subdirectory2/file4",
"/some/weirdfiles/subdirectory3/file5",
"/some/weirdfiles/subdirectory3/file6",
"/some/weirdfiles/lonesubdirectory/whatever",
"/some/wastefiles/normalefile",
"/some/wastefiles/nottooweird",
"/some/wastefiles/potato",
"/some/wastefiles/potatomashed",
"/some/wastefiles/subdirectory1/file1",
"/some/wastefiles/subdirectory1/file2",
"/some/wastefiles/subdirectory2/file3",
"/some/wastefiles/subdirectory2/file4",
"/some/wastefiles/subdirectory3/file5",
"/some/wastefiles/subdirectory3/file6",
"/some/wastefiles/lonesubdirectory/whatever",
"/testingwildcards/normalefile",
"/testingwildcards/nottooweird",
"/testingwildcards/potato",
"/testingwildcards/potatomashed",
"/testingwildcards/subdirectory1/file1",
"/testingwildcards/subdirectory1/file2",
"/testingwildcards/subdirectory2/file3",
"/testingwildcards/subdirectory2/file4",
"/testingwildcards/subdirectory3/file5",
"/testingwildcards/subdirectory3/file6",
"/testingwildcards/lonesubdirectory/whatever"};
PopulateTree(files, &tree);
// testing full paths
FakeCdCmd(ua, &tree, "/");
EXPECT_EQ(FakeMarkCmd(ua, &tree, "*"), files.size());
EXPECT_EQ(
FakeMarkCmd(ua, &tree, "/testingwildcards/lonesubdirectory/whatever"), 1);
EXPECT_EQ(
FakeMarkCmd(ua, &tree, "testingwildcards/lonesubdirectory/whatever"), 1);
// Using full path while being in a different folder than root
FakeCdCmd(ua, &tree, "/some/weirdfiles/");
EXPECT_EQ(
FakeMarkCmd(ua, &tree, "/testingwildcards/lonesubdirectory/whatever"), 1);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "/testingwildcards/sub*"), 6);
EXPECT_EQ(
FakeMarkCmd(ua, &tree, "testingwildcards/lonesubdirectory/whatever"), 0);
// Testing patterns in different folders
FakeCdCmd(ua, &tree, "/some/weirdfiles/");
EXPECT_EQ(FakeMarkCmd(ua, &tree, "nope"), 0);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "potato"), 1);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "potato*"), 2);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "lonesubdirectory/*"), 1);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "subdirectory2/*"), 2);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "sub*/*"), 6);
FakeCdCmd(ua, &tree, "/some/");
EXPECT_EQ(FakeMarkCmd(ua, &tree, "w*/sub*/*"), 12);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "wei*/sub*/*"), 6);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "wei*/subroza/*"), 0);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "w*efiles/sub*/*"), 6);
FakeCdCmd(ua, &tree, "/testingwildcards/");
EXPECT_EQ(FakeMarkCmd(ua, &tree, "p?tato"), 1);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "subdirectory?/file1"), 1);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "subdirectory?/file?"), 6);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "subdirectory?/file[!2,!3]"), 4);
EXPECT_EQ(FakeMarkCmd(ua, &tree, "su[a,b,c]directory?/file1"), 1);
/* The following two tests are commented out because they should be working
* correctly, but as the second test suggests, fnmatch (which is the main
* wildcard matching function used in bareos) has problems handling the curly
* braces wildcard.
*/
// EXPECT_EQ(FakeMarkCmd(&ua, tree, "{*tory1,*tory2}/file1"), 1);
// EXPECT_EQ(fnmatch("{*tory1,*tory2}", "subdirectory1", 0), 0);
}