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 | // 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_role.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::http;
using namespace isc::rbac;
using namespace std;
namespace {
/// @brief Test fixture for checking RBAC roles.
class RoleTest : public ::testing::Test {
public:
/// @brief Constructor.
RoleTest() {
}
/// @brief Destructor.
virtual ~RoleTest() = default;
/// @brief Create HTTP Request.
///
/// @return The HTTP request.
HttpRequestPtr createHttpRequest() {
HttpRequestPtr request(new HttpRequest());
return (request);
}
};
/// @brief This test verifies that role assignment by remote address works
/// as expected.
TEST_F(RoleTest, remote) {<--- syntax error
RolePtr role(new RemoteRole());
HttpRequestPtr request = createHttpRequest();
string ret;
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
request->setRemote("foo");
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_EQ("foo", ret);
}
/// @brief This test verifies that role assignment by subject name works
/// as expected.
TEST_F(RoleTest, subject) {
RolePtr role(new SubjectRole());
HttpRequestPtr request = createHttpRequest();
string ret;
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
request->setSubject("foo");
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_EQ("foo", ret);
EXPECT_FALSE(HttpRequest::recordSubject_);
role->setup(true);
EXPECT_TRUE(HttpRequest::recordSubject_);
role->setup(false);
EXPECT_FALSE(HttpRequest::recordSubject_);
}
/// @brief This test verifies that role assignment by issuer name works
/// as expected.
TEST_F(RoleTest, issuer) {
RolePtr role(new IssuerRole());
HttpRequestPtr request = createHttpRequest();
string ret;
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
request->setIssuer("foo");
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_EQ("foo", ret);
EXPECT_FALSE(HttpRequest::recordIssuer_);
role->setup(true);
EXPECT_TRUE(HttpRequest::recordIssuer_);
role->setup(false);
EXPECT_FALSE(HttpRequest::recordIssuer_);
}
/// @brief This test verifies that role assignment by basic auth works
/// as expected.
TEST_F(RoleTest, basicAuth) {
RolePtr role(new BasicAuthRole());
HttpRequestPtr request = createHttpRequest();
string ret;
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
request->setBasicAuth("foo");
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_EQ("foo", ret);
EXPECT_FALSE(HttpRequest::recordBasicAuth_);
role->setup(true);
EXPECT_TRUE(HttpRequest::recordBasicAuth_);
role->setup(false);
EXPECT_FALSE(HttpRequest::recordBasicAuth_);
}
/// @brief This test verifies that role assignment by custom value works
/// as expected.
TEST_F(RoleTest, custom) {
RolePtr role(new CustomRole());
HttpRequestPtr request = createHttpRequest();
string ret;
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
request->setCustom("foo");
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_EQ("foo", ret);
}
/// @brief This test verifies that static role assignment works as expected.
TEST_F(RoleTest, staticValue) {
RolePtr role(new StaticRole("foo"));
HttpRequestPtr request = createHttpRequest();
string ret;
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_EQ("foo", ret);
boost::shared_ptr<StaticRole> srole =
boost::dynamic_pointer_cast<StaticRole>(role);
ASSERT_TRUE(srole);
EXPECT_EQ("foo", srole->getRole());
}
/// @brief This test verifies that TLS requirement works as expected.
TEST_F(RoleTest, requireTls) {
HttpRequestPtr request = createHttpRequest();
EXPECT_FALSE(Role::requireTls(request));
request->setTls(true);
EXPECT_TRUE(Role::requireTls(request));
}
/// @brief This test verifies that null request works as expected.
TEST_F(RoleTest, nullRequest) {
HttpRequestPtr request;
RolePtr role(new RemoteRole());
EXPECT_EQ("remote", role->getName());
string ret;
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
role.reset(new SubjectRole());
EXPECT_EQ("subject", role->getName());
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
role.reset(new IssuerRole());
EXPECT_EQ("issuer", role->getName());
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
role.reset(new BasicAuthRole());
EXPECT_EQ("basic-auth", role->getName());
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
role.reset(new CustomRole());
EXPECT_EQ("custom", role->getName());
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
role.reset(new StaticRole("foo"));
EXPECT_EQ("static", role->getName());
ASSERT_NO_THROW(ret = role->assign(request));
EXPECT_TRUE(ret.empty());
EXPECT_FALSE(Role::requireTls(request));
}
/// @brief This test verifies that role assignment parsing works as expected.
TEST_F(RoleTest, parse) {
ConstElementPtr cfg;
string expected = "parse null role assignment config";
EXPECT_THROW_MSG(Role::parse(cfg), BadValue, expected);
cfg = Element::createMap();
expected = "role assignment config is not a string";
EXPECT_THROW_MSG(Role::parse(cfg), BadValue, expected);
cfg = Element::create(string("foobar"));
expected = "unknown role assignment keyword 'foobar'";
EXPECT_THROW_MSG(Role::parse(cfg), BadValue, expected);
cfg = Element::create(string("remote-address"));
RolePtr role;
EXPECT_NO_THROW(role = Role::parse(cfg));
ASSERT_TRUE(role);
EXPECT_EQ("remote", role->getName());
cfg = Element::create(string("cert-subject"));
role.reset();
EXPECT_NO_THROW(role = Role::parse(cfg));
ASSERT_TRUE(role);
EXPECT_EQ("subject", role->getName());
cfg = Element::create(string("cert-issuer"));
role.reset();
EXPECT_NO_THROW(role = Role::parse(cfg));
ASSERT_TRUE(role);
EXPECT_EQ("issuer", role->getName());
cfg = Element::create(string("basic-authentication"));
role.reset();
EXPECT_NO_THROW(role = Role::parse(cfg));
ASSERT_TRUE(role);
EXPECT_EQ("basic-auth", role->getName());
cfg = Element::create(string("custom-value"));
role.reset();
EXPECT_NO_THROW(role = Role::parse(cfg));
ASSERT_TRUE(role);
EXPECT_EQ("custom", role->getName());
}
} // end of anonymous namespace
|