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 | // Copyright (C) 2021-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <config/http_command_config.h>
#include <http/basic_auth_config.h>
#include <testutils/gtest_utils.h>
#include <testutils/test_to_element.h>
using namespace isc;
using namespace isc::asiolink;
using namespace isc::config;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::http;
using namespace isc::test;
using namespace std;
namespace {
/// @brief Test fixture for HTTP control socket configuration.
class HttpCommandConfigTest : public ::testing::Test {
public:
/// @brief Constructor.
HttpCommandConfigTest() : http_config_() {
HttpCommandConfig::DEFAULT_SOCKET_ADDRESS = IOAddress("127.0.0.1");
HttpCommandConfig::DEFAULT_SOCKET_PORT = 8000;
HttpCommandConfig::DEFAULT_AUTHENTICATION_REALM = "";
}
/// @brief Destructor.
~HttpCommandConfigTest() {
HttpCommandConfig::DEFAULT_SOCKET_ADDRESS = IOAddress("127.0.0.1");
HttpCommandConfig::DEFAULT_SOCKET_PORT = 8000;
HttpCommandConfig::DEFAULT_AUTHENTICATION_REALM = "";
}
/// @brief HTTP control socket configuration.
HttpCommandConfigPtr http_config_;
};
// This test verifies the default HTTP control socket configuration.
TEST_F(HttpCommandConfigTest, default) {<--- syntax error
ElementPtr json = Element::createMap();
ASSERT_NO_THROW_LOG(http_config_.reset(new HttpCommandConfig(json)));
// Check default values.
EXPECT_EQ("http", http_config_->getSocketType());
EXPECT_EQ("127.0.0.1", http_config_->getSocketAddress().toText());
EXPECT_EQ(8000, http_config_->getSocketPort());
EXPECT_FALSE(http_config_->getAuthConfig());
EXPECT_EQ("", http_config_->getTrustAnchor());
EXPECT_EQ("", http_config_->getCertFile());
EXPECT_EQ("", http_config_->getKeyFile());
EXPECT_TRUE(http_config_->getCertRequired());
EXPECT_TRUE(http_config_->getEmulateAgentResponse());
// Check unparse.
string expected = R"(
{
"socket-type": "http",
"socket-address": "127.0.0.1",
"socket-port": 8000
}
)";
runToElementTest(expected, *http_config_);
// Change class defaults.
HttpCommandConfig::DEFAULT_SOCKET_ADDRESS = IOAddress("::1");
HttpCommandConfig::DEFAULT_SOCKET_PORT = 8080;
ASSERT_NO_THROW_LOG(http_config_.reset(new HttpCommandConfig(json)));
EXPECT_EQ("::1", http_config_->getSocketAddress().toText());
EXPECT_EQ(8080, http_config_->getSocketPort());
}
// This test verifies direct error cases.
TEST_F(HttpCommandConfigTest, errors) {
// Error scenarios.
struct scenario {
string title;
string input;
string msg;
} scenarios[] = {
{
"bad type",
"[ ]",
"expected map type (<string>:1:2)"
},
{
"bad socket-type type",
R"( { "socket-type": 1 } )",
"invalid type specified for parameter 'socket-type' "
"(<string>:1:19)"
},
{
"unsupported socket-type",
R"( { "socket-type": "unix" } )",
"unsupported 'socket-type' 'unix' not 'http' or 'https'"
},
{
"unsupported socket-name",
R"( { "socket-name": "::1" } )",
"parameter 'socket-name' is not supported by HTTP control sockets"
},
{
"bad socket-address type",
R"( { "socket-address": 8000 } )",
"invalid type specified for parameter 'socket-address' "
"(<string>:1:22)"
},
{
"bad address",
R"( { "socket-address": ":::" } )",
"failed to convert ':::' to address: "
"Failed to convert string to address ':::': Invalid argument "
"(<string>:1:22)"
},
{
"bad socket-port type",
R"( { "socket-port": "8000" } )",
"invalid type specified for parameter 'socket-port' "
"(<string>:1:19)"
},
{
"socket-port negative",
R"( { "socket-port": -1 } )",
"out of range value -1 specified for parameter 'socket-port' "
"(<string>:1:19)"
},
{
"bad authentication type",
R"( { "authentication": "none" } )",
"invalid type specified for parameter 'authentication' "
"(<string>:1:22)"
},
// Authentication parsing failures are checked at another place.
{
"bad trust-anchor type",
R"( { "trust-anchor": false } )",
"invalid type specified for parameter 'trust-anchor' "
"(<string>:1:20)"
},
{
"bad cert-file type",
R"( { "cert-file": false } )",
"invalid type specified for parameter 'cert-file' "
"(<string>:1:17)"
},
{
"bad key-file type",
R"( { "key-file": false } )",
"invalid type specified for parameter 'key-file' "
"(<string>:1:16)"
},
{
"bad cert-required type",
R"( { "cert-required": 0 } )",
"invalid type specified for parameter 'cert-required' "
"(<string>:1:21)"
},
{
"https requires TLS",
R"( { "socket-type": "https" } )",
"no TLS setup for a HTTPS control socket"
},
{
"missing trust-anchor",
R"( { "cert-file": "foo" } )",
"trust-anchor parameter is missing or empty:"
" all or none of TLS parameters must be set"
},
{
"empty trust-anchor",
R"( { "trust-anchor": "", "cert-file": "foo" } )",
"trust-anchor parameter is missing or empty:"
" all or none of TLS parameters must be set"
},
{
"missing cert-file",
R"( { "trust-anchor": "foo" } )",
"cert-file parameter is missing or empty:"
" all or none of TLS parameters must be set"
},
{
"empty cert-file",
R"( { "trust-anchor": "foo", "cert-file": "" } )",
"cert-file parameter is missing or empty:"
" all or none of TLS parameters must be set"
},
{
"missing key-file",
R"( { "trust-anchor": "foo", "cert-file": "bar" } )",
"key-file parameter is missing or empty:"
" all or none of TLS parameters must be set"
},
{
"empty key-file",
R"( {
"trust-anchor": "foo",
"cert-file": "bar",
"key-file": ""
} )",
"key-file parameter is missing or empty:"
" all or none of TLS parameters must be set"
}
};
for (auto const& s : scenarios) {
SCOPED_TRACE(s.title);
ElementPtr json;
ASSERT_NO_THROW(json = Element::fromJSON(s.input));
EXPECT_THROW_MSG(http_config_.reset(new HttpCommandConfig(json)),
DhcpConfigError, s.msg);
}
}
// This test verifies a HTTP control socket configuration with authentication
// can be parsed and unparsed.
TEST_F(HttpCommandConfigTest, authentication) {
// Configure with authentication.
string config = R"(
{
"authentication": {
"clients": [ {
"user": "admin",
"password": "1234"
} ]
}
})";
ElementPtr json;
ASSERT_NO_THROW(json = Element::fromJSON(config));
ASSERT_NO_THROW(http_config_.reset(new HttpCommandConfig(json)));
// Verify the authentication and its defaults.
auto auth_config = http_config_->getAuthConfig();
ASSERT_TRUE(auth_config);
EXPECT_EQ("", auth_config->getRealm());
BasicHttpAuthConfigPtr auth =
boost::dynamic_pointer_cast<BasicHttpAuthConfig>(auth_config);
ASSERT_TRUE(auth);
// Verify that default realm is applied.
HttpCommandConfig::DEFAULT_AUTHENTICATION_REALM = "FOO";
ASSERT_NO_THROW(json = Element::fromJSON(config));
ASSERT_NO_THROW(http_config_.reset(new HttpCommandConfig(json)));
auth_config = http_config_->getAuthConfig();
ASSERT_TRUE(auth_config);
EXPECT_EQ("FOO", auth_config->getRealm());
// Verify that default is applied only when not configured.
config = R"(
{
"authentication": {
"realm": "BAR",
"clients": [ {
"user": "admin",
"password": "1234"
} ]
}
})";
ASSERT_NO_THROW(json = Element::fromJSON(config));
ASSERT_NO_THROW(http_config_.reset(new HttpCommandConfig(json)));
auth_config = http_config_->getAuthConfig();
ASSERT_TRUE(auth_config);
EXPECT_EQ("BAR", auth_config->getRealm());
// Check unparse.
string expected = R"(
{
"socket-type": "http",
"socket-address": "127.0.0.1",
"socket-port": 8000,
"authentication": {
"type": "basic",
"realm": "BAR",
"directory": "",
"clients": [ {
"user": "admin",
"password": "1234"
} ]
}
})";
runToElementTest(expected, *http_config_);
}
// This test verifies a HTTP control socket configuration with TLS can
// be parsed and unparsed.
TEST_F(HttpCommandConfigTest, tls) {
// Configure with TLS.
string config = R"(
{
"trust-anchor": "my ca",
"cert-file": "my cert",
"key-file": "my key",
"cert-required": false
})";
ElementPtr json;
ASSERT_NO_THROW(json = Element::fromJSON(config));
ASSERT_NO_THROW(http_config_.reset(new HttpCommandConfig(json)));
EXPECT_EQ("http", http_config_->getSocketType());
EXPECT_EQ("127.0.0.1", http_config_->getSocketAddress().toText());
EXPECT_EQ(8000, http_config_->getSocketPort());
EXPECT_FALSE(http_config_->getAuthConfig());
EXPECT_EQ("my ca", http_config_->getTrustAnchor());
EXPECT_EQ("my cert", http_config_->getCertFile());
EXPECT_EQ("my key", http_config_->getKeyFile());
EXPECT_FALSE(http_config_->getCertRequired());
EXPECT_TRUE(http_config_->getEmulateAgentResponse());
// Check unparse.
string expected = R"(
{
"socket-type": "http",
"socket-address": "127.0.0.1",
"socket-port": 8000,
"trust-anchor": "my ca",
"cert-file": "my cert",
"key-file": "my key",
"cert-required": false
})";
runToElementTest(expected, *http_config_);
}
} // end of anonymous namespace
|