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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353 | // Copyright (C) 2022-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.
#include <config.h>
#include <rbac.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/gtest_utils.h>
using namespace isc;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::http;
using namespace isc::rbac;
using namespace std;
namespace {
/// @brief Test fixture for checking RBAC role configs.
class RoleConfigTest : public ::testing::Test {
public:
/// @brief Constructor.
RoleConfigTest() {
roleConfigTable.clear();
aclTable.clear();
Acl::initTable();
apiTable.clear();
}
/// @brief Destructor.
virtual ~RoleConfigTest() {
roleConfigTable.clear();
aclTable.clear();
apiTable.clear();
}
};
/// @brief This test verifies that role config object works as expected.
TEST_F(RoleConfigTest, basic) {<--- syntax error
RoleConfigPtr rc;
AclPtr accept(new AllAcl());
AclPtr reject(new NoneAcl());
ResponseFilterList rfl;
EXPECT_NO_THROW(rc.reset(new RoleConfig("foo", accept, reject,
true, false, rfl)));
ASSERT_TRUE(rc);
EXPECT_EQ("foo", rc->name_);
ASSERT_TRUE(rc->accept_);
EXPECT_EQ("all", rc->accept_->getClassName());
ASSERT_TRUE(rc->reject_);
EXPECT_EQ("none", rc->reject_->getClassName());
EXPECT_TRUE(rc->others_);
EXPECT_FALSE(rc->preference_);
EXPECT_TRUE(rc->response_filters_.empty());
EXPECT_NO_THROW(rc.reset(new RoleConfig("bar", AclPtr(), AclPtr(),
false, true, rfl)));
ASSERT_TRUE(rc);
EXPECT_EQ("bar", rc->name_);
EXPECT_FALSE(rc->accept_);
EXPECT_FALSE(rc->reject_);
EXPECT_FALSE(rc->others_);
EXPECT_TRUE(rc->preference_);
EXPECT_TRUE(rc->response_filters_.empty());
}
/// @brief This test verifies that others member works as expected.
TEST_F(RoleConfigTest, others) {
RoleConfigPtr rc(new RoleConfig("foo", AclPtr(), AclPtr(), false,
true, ResponseFilterList()));
ASSERT_TRUE(rc);
EXPECT_FALSE(rc->match("bar"));
EXPECT_FALSE(rc->match("any command"));
rc->others_ = true;
EXPECT_TRUE(rc->match("bar"));
EXPECT_TRUE(rc->match("any command"));
}
/// @brief This test verifies that preference member works as expected.
TEST_F(RoleConfigTest, preference) {
RoleConfigPtr rc(new RoleConfig("foo", AclPtr(new AllAcl()),
AclPtr(new AllAcl()), false, true,
ResponseFilterList()));
ASSERT_TRUE(rc);
EXPECT_TRUE(rc->match("bar"));
EXPECT_TRUE(rc->match("any command"));
rc->preference_ = false;
EXPECT_FALSE(rc->match("bar"));
EXPECT_FALSE(rc->match("any command"));
}
/// @brief This test verifies that the role config table works as expected.
TEST_F(RoleConfigTest, table) {
ASSERT_TRUE(roleConfigTable.empty());
RoleConfigPtr rc(new RoleConfig("all", AclPtr(new AllAcl()),
AclPtr(new NoneAcl()), true, true,
ResponseFilterList()));
ASSERT_TRUE(rc);
EXPECT_FALSE(RoleConfig::getConfig("all"));
roleConfigTable["all"] = rc;
RoleConfigPtr got = RoleConfig::getConfig("all");
ASSERT_TRUE(got);
EXPECT_EQ(got->name_, rc->name_);
}
/// @brief This test verifies that the role config parsing works as expected.
TEST_F(RoleConfigTest, parse) {
ElementPtr cfg;
string expected = "parse null role config";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), BadValue, expected);
cfg = Element::createList();
expected = "role config is not a map";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), BadValue, expected);
// Keywords.
cfg = Element::createMap();
cfg->set("name", Element::create(1));
expected = "'name' parameter is not a string";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), DhcpConfigError, expected);
cfg->set("name", Element::create(string("foo")));
cfg->set("accept-commands", Element::create(true));
expected = "access control list is not a string or a map";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), BadValue, expected);
cfg->set("accept-commands", Element::create(string("ALL")));
cfg->set("reject-commands", Element::create(3.14));
EXPECT_THROW_MSG(RoleConfig::parse(cfg), BadValue, expected);
cfg->set("reject-commands", Element::create(string("NONE")));
cfg->set("other-commands", Element::create(false));
expected = "'other-commands' parameter is not a string";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), DhcpConfigError, expected);
cfg->remove("other-commands");
cfg->set("list-match-first", Element::create(true));
expected = "'list-match-first' parameter is not a string";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), DhcpConfigError, expected);
cfg->remove("list-match-first");
cfg->set("response-filters", Element::create(0.));
expected = "'response-filters' parameter is not a list";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), DhcpConfigError, expected);
cfg->remove("response-filters");
cfg->set("comment", Element::create(string("a comment")));
cfg->set("bad", Element::create(string("unknown parameter")));
expected = "spurious 'bad' parameter";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), DhcpConfigError, expected);
// Name.
cfg->remove("bad");
RoleConfigPtr rc;
EXPECT_NO_THROW(rc = RoleConfig::parse(cfg));
ASSERT_TRUE(rc);
EXPECT_EQ("foo", rc->name_);
expected = "spurious 'name' parameter for bar role";
EXPECT_THROW_MSG(RoleConfig::parse(cfg, "bar"), BadValue, expected);
cfg = Element::createMap();
rc.reset();
EXPECT_NO_THROW(rc = RoleConfig::parse(cfg, "bar"));
ASSERT_TRUE(rc);
EXPECT_EQ("bar", rc->name_);
expected = "'name' parameter is required in { }";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), BadValue, expected);
// ACLs.
cfg->set("name", Element::create(string("foo")));
cfg->set("accept-commands", Element::create(string("ALL")));
cfg->set("reject-commands", Element::create(string("NONE")));
rc.reset();
EXPECT_NO_THROW(rc = RoleConfig::parse(cfg));
ASSERT_TRUE(rc);
EXPECT_EQ("foo", rc->name_);
ASSERT_TRUE(rc->accept_);
EXPECT_EQ("all", rc->accept_->getClassName());
ASSERT_TRUE(rc->reject_);
EXPECT_EQ("none", rc->reject_->getClassName());
EXPECT_FALSE(rc->others_);
EXPECT_TRUE(rc->preference_);
// Other commands.
cfg->set("other-commands", Element::create(string("foobar")));
expected = "other-commands 'foobar' is not 'accept' or 'reject'";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), BadValue, expected);
cfg->set("other-commands", Element::create(string("accept")));
EXPECT_NO_THROW(rc = RoleConfig::parse(cfg));
ASSERT_TRUE(rc);
EXPECT_TRUE(rc->others_);
cfg->set("other-commands", Element::create(string("reject")));
EXPECT_NO_THROW(rc = RoleConfig::parse(cfg));
ASSERT_TRUE(rc);
EXPECT_FALSE(rc->others_);
// Preference.
cfg->set("list-match-first", Element::create(string("foobar")));
expected = "list-match-first 'foobar' is not 'accept' or 'reject'";
EXPECT_THROW_MSG(RoleConfig::parse(cfg), BadValue, expected);
cfg->set("list-match-first", Element::create(string("accept")));
EXPECT_NO_THROW(rc = RoleConfig::parse(cfg));
ASSERT_TRUE(rc);
EXPECT_TRUE(rc->preference_);
cfg->set("list-match-first", Element::create(string("reject")));
EXPECT_NO_THROW(rc = RoleConfig::parse(cfg));
ASSERT_TRUE(rc);
EXPECT_FALSE(rc->preference_);
}
/// @brief This test verifies that createReject works as expected.
TEST_F(RoleConfigTest, createReject) {
HttpRequestPtr request;
HttpStatusCode status_code = HttpStatusCode::FORBIDDEN;
string expected = "null request";
EXPECT_THROW_MSG(RoleConfig::createReject(request, status_code),
Unexpected, expected);
request.reset(new HttpRequest());
HttpResponseJsonPtr response;
EXPECT_NO_THROW(response = RoleConfig::createReject(request, status_code));
ASSERT_TRUE(response);
EXPECT_EQ("HTTP/1.0 403 Forbidden", response->toBriefString());
ConstElementPtr json = response->getBodyAsJson();
ASSERT_TRUE(json);
EXPECT_EQ("{ \"result\": 403, \"text\": \"Forbidden\" }", json->str());
}
/// @brief This test verifies that user1 extensive example parses.
TEST_F(RoleConfigTest, parseUser1) {
string config = R"(
{
"name": "user1",
"accept-commands":
{
"and": [
"READ",
{ "not":
{ "commands": [ "config-get" ] }
}
]
},
"reject-commands": "ALL",
"list-match-first": "accept"
})";
ConstElementPtr cfg;
ASSERT_NO_THROW(cfg = Element::fromJSON(config));
ASSERT_TRUE(cfg);
Acl::initTable();
Api::fillApiTable(string(API_DIR));
RoleConfigPtr role;
EXPECT_NO_THROW(role = RoleConfig::parse(cfg));
ASSERT_TRUE(role);
EXPECT_TRUE(role->match("status-get"));
EXPECT_FALSE(role->match("config-get"));
EXPECT_FALSE(role->match("config-set"));
}
/// @brief This test verifies that user2 extensive example parses.
TEST_F(RoleConfigTest, parseUser2) {
string config = R"(
{
"name": "user2",
"accept-commands":
{
"and": [
"READ",
{ "not":
{ "commands": [ "config-get" ] }
}
]
},
"other-commands": "reject"
})";
ConstElementPtr cfg;
ASSERT_NO_THROW(cfg = Element::fromJSON(config));
ASSERT_TRUE(cfg);
Acl::initTable();
Api::fillApiTable(string(API_DIR));
RoleConfigPtr role;
EXPECT_NO_THROW(role = RoleConfig::parse(cfg));
ASSERT_TRUE(role);
EXPECT_TRUE(role->match("status-get"));
EXPECT_FALSE(role->match("config-get"));
EXPECT_FALSE(role->match("config-set"));
}
/// @brief This test verifies that user3 extensive example parses.
TEST_F(RoleConfigTest, parseUser3) {
string config = R"(
{
"name": "user3",
"reject-commands":
{
"or": [
"WRITE",
{ "commands": [ "config-get" ] }
]
},
"other-commands": "accept"
})";
ConstElementPtr cfg;
ASSERT_NO_THROW(cfg = Element::fromJSON(config));
ASSERT_TRUE(cfg);
Acl::initTable();
Api::fillApiTable(string(API_DIR));
RoleConfigPtr role;
EXPECT_NO_THROW(role = RoleConfig::parse(cfg));
ASSERT_TRUE(role);
EXPECT_TRUE(role->match("status-get"));
EXPECT_FALSE(role->match("config-get"));
EXPECT_FALSE(role->match("config-set"));
}
/// @brief This test verifies that user4 extensive example parses.
TEST_F(RoleConfigTest, parseUser4) {
string config = R"(
{
"name": "user4",
"accept-commands": "READ",
"reject-commands": { "commands": [ "config-get" ] },
"list-match-first": "reject"
})";
ConstElementPtr cfg;
ASSERT_NO_THROW(cfg = Element::fromJSON(config));
ASSERT_TRUE(cfg);
Acl::initTable();
Api::fillApiTable(string(API_DIR));
RoleConfigPtr role;
EXPECT_NO_THROW(role = RoleConfig::parse(cfg));
ASSERT_TRUE(role);
EXPECT_TRUE(role->match("status-get"));
EXPECT_FALSE(role->match("config-get"));
EXPECT_FALSE(role->match("config-set"));
}
} // end of anonymous namespace
|