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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 | // Copyright (C) 2020-2022 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 <http/basic_auth_config.h>
#include <testutils/gtest_utils.h>
#include <testutils/test_to_element.h>
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::http;
using namespace isc::test;
using namespace std;
namespace {
string data_dir(DATA_DIR);
// Test that basic auth client works as expected.
TEST(BasicHttpAuthClientTest, basic) {<--- syntax error
// Create a client.
ConstElementPtr ctx = Element::fromJSON("{ \"foo\": \"bar\" }");
BasicHttpAuthClient client("foo", "bar", ctx);
// Check it.
EXPECT_EQ("foo", client.getUser());
EXPECT_EQ("", client.getUserFile());
EXPECT_EQ("bar", client.getPassword());
EXPECT_EQ("", client.getPasswordFile());
EXPECT_FALSE(client.getPasswordFileOnly());
EXPECT_TRUE(ctx->equals(*client.getContext()));
// Check toElement.
ElementPtr expected = Element::createMap();
expected->set("user", Element::create(string("foo")));
expected->set("password", Element::create(string("bar")));
expected->set("user-context", ctx);
runToElementTest<BasicHttpAuthClient>(expected, client);
}
// Test that basic auth client with files works as expected.
TEST(BasicHttpAuthClientTest, basicFiles) {
// Create a client.
ConstElementPtr ctx = Element::fromJSON("{ \"foo\": \"bar\" }");
BasicHttpAuthClient client("", "foo", "", "bar", false, ctx);
// Check it.
EXPECT_EQ("", client.getUser());
EXPECT_EQ("foo", client.getUserFile());
EXPECT_EQ("", client.getPassword());
EXPECT_EQ("bar", client.getPasswordFile());
EXPECT_FALSE(client.getPasswordFileOnly());
EXPECT_TRUE(ctx->equals(*client.getContext()));
// Check toElement.
ElementPtr expected = Element::createMap();
expected->set("user-file", Element::create(string("foo")));
expected->set("password-file", Element::create(string("bar")));
expected->set("user-context", ctx);
runToElementTest<BasicHttpAuthClient>(expected, client);
}
// Test that basic auth client with one file works as expected.
TEST(BasicHttpAuthClientTest, basicOneFile) {
// Create a client.
ConstElementPtr ctx = Element::fromJSON("{ \"foo\": \"bar\" }");
BasicHttpAuthClient client("", "", "", "foobar", true, ctx);
// Check it.
EXPECT_EQ("", client.getUser());
EXPECT_EQ("", client.getUserFile());
EXPECT_EQ("", client.getPassword());
EXPECT_EQ("foobar", client.getPasswordFile());
EXPECT_TRUE(client.getPasswordFileOnly());
EXPECT_TRUE(ctx->equals(*client.getContext()));
// Check toElement.
ElementPtr expected = Element::createMap();
expected->set("password-file", Element::create(string("foobar")));
expected->set("user-context", ctx);
runToElementTest<BasicHttpAuthClient>(expected, client);
}
// Test that basic auth configuration works as expected.
TEST(BasicHttpAuthConfigTest, basic) {
// Create a configuration.
BasicHttpAuthConfig config;
// Initial configuration is empty.
EXPECT_TRUE(config.empty());
EXPECT_TRUE(config.getRealm().empty());
EXPECT_TRUE(config.getDirectory().empty());
EXPECT_TRUE(config.getClientList().empty());
EXPECT_TRUE(config.getCredentialMap().empty());
// Set the realm, directory and user context.
EXPECT_NO_THROW(config.setRealm("my-realm"));
EXPECT_EQ("my-realm", config.getRealm());
EXPECT_NO_THROW(config.setDirectory("/tmp"));
EXPECT_EQ("/tmp", config.getDirectory());
ConstElementPtr horse = Element::fromJSON("{ \"value\": \"a horse\" }");
EXPECT_NO_THROW(config.setContext(horse));
EXPECT_TRUE(horse->equals(*config.getContext()));
// Add rejects user id with embedded ':'.
EXPECT_THROW(config.add("foo:", "", "bar", ""), BadValue);
// Add a client.
EXPECT_TRUE(config.empty());
ConstElementPtr ctx = Element::fromJSON("{ \"foo\": \"bar\" }");
EXPECT_NO_THROW(config.add("foo", "", "bar", "", false, ctx));
EXPECT_FALSE(config.empty());
// Check the client.
ASSERT_EQ(1, config.getClientList().size());
const BasicHttpAuthClient& client = config.getClientList().front();
EXPECT_EQ("foo", client.getUser());
EXPECT_EQ("bar", client.getPassword());
EXPECT_TRUE(ctx->equals(*client.getContext()));
// Check the credential.
ASSERT_NE(0, config.getCredentialMap().count("Zm9vOmJhcg=="));
string user;
EXPECT_NO_THROW(user = config.getCredentialMap().at("Zm9vOmJhcg=="));
EXPECT_EQ("foo", user);
// Check toElement.
ElementPtr expected = Element::createMap();
ElementPtr clients = Element::createList();
ElementPtr elem = Element::createMap();
elem->set("user", Element::create(string("foo")));
elem->set("password", Element::create(string("bar")));
elem->set("user-context", ctx);
clients->add(elem);
expected->set("type", Element::create(string("basic")));
expected->set("realm", Element::create(string("my-realm")));
expected->set("directory", Element::create(string("/tmp")));
expected->set("user-context", horse);
expected->set("clients", clients);
runToElementTest<BasicHttpAuthConfig>(expected, config);
// Add a second client and test it.
EXPECT_NO_THROW(config.add("test", "", "123\xa3", ""));
ASSERT_EQ(2, config.getClientList().size());
EXPECT_EQ("foo", config.getClientList().front().getUser());
EXPECT_EQ("test", config.getClientList().back().getUser());
ASSERT_NE(0, config.getCredentialMap().count("dGVzdDoxMjPCow=="));
// Check clear.
config.clear();
EXPECT_TRUE(config.empty());
expected->set("clients", Element::createList());
runToElementTest<BasicHttpAuthConfig>(expected, config);
// Add clients again.
EXPECT_NO_THROW(config.add("test", "", "123\xa3", ""));
EXPECT_NO_THROW(config.add("foo", "", "bar", "", false, ctx));
// Check that toElement keeps add order.
ElementPtr elem0 = Element::createMap();
elem0->set("user", Element::create(string("test")));
elem0->set("password", Element::create(string("123\xa3")));
clients = Element::createList();
clients->add(elem0);
clients->add(elem);
expected->set("clients", clients);
runToElementTest<BasicHttpAuthConfig>(expected, config);
}
// Test that basic auth configuration with files works as expected.
TEST(BasicHttpAuthConfigTest, basicFiles) {
// Create a configuration.
BasicHttpAuthConfig config;
// Set the realm, directory and user context.
EXPECT_NO_THROW(config.setRealm("my-realm"));
EXPECT_EQ("my-realm", config.getRealm());
EXPECT_NO_THROW(config.setDirectory(data_dir));
EXPECT_EQ(data_dir, config.getDirectory());
ConstElementPtr horse = Element::fromJSON("{ \"value\": \"a horse\" }");
EXPECT_NO_THROW(config.setContext(horse));
EXPECT_TRUE(horse->equals(*config.getContext()));
// ':' in user id check is done during parsing
// Add a client.
EXPECT_TRUE(config.empty());
ConstElementPtr ctx = Element::fromJSON("{ \"foo\": \"bar\" }");
EXPECT_NO_THROW(config.add("foo", "", "", "hiddenp", false, ctx));
EXPECT_FALSE(config.empty());
// Check the client.
ASSERT_EQ(1, config.getClientList().size());
const BasicHttpAuthClient& client = config.getClientList().front();
EXPECT_EQ("foo", client.getUser());
EXPECT_EQ("", client.getUserFile());
EXPECT_EQ("", client.getPassword());
EXPECT_EQ("hiddenp", client.getPasswordFile());
EXPECT_FALSE(client.getPasswordFileOnly());
EXPECT_TRUE(ctx->equals(*client.getContext()));
// Check toElement.
ElementPtr expected = Element::createMap();
ElementPtr clients = Element::createList();
ElementPtr elem = Element::createMap();
elem->set("user", Element::create(string("foo")));
elem->set("password-file", Element::create(string("hiddenp")));
elem->set("user-context", ctx);
clients->add(elem);
expected->set("type", Element::create(string("basic")));
expected->set("realm", Element::create(string("my-realm")));
expected->set("directory", Element::create(data_dir));
expected->set("user-context", horse);
expected->set("clients", clients);
runToElementTest<BasicHttpAuthConfig>(expected, config);
// Add a second client and test it.
EXPECT_NO_THROW(config.add("", "hiddenu", "", "hiddenp"));
ASSERT_EQ(2, config.getClientList().size());
EXPECT_EQ("foo", config.getClientList().front().getUser());
EXPECT_EQ("hiddenu", config.getClientList().back().getUserFile());
// Check clear.
config.clear();
EXPECT_TRUE(config.empty());
expected->set("clients", Element::createList());
runToElementTest<BasicHttpAuthConfig>(expected, config);
// Add clients again.
EXPECT_NO_THROW(config.add("", "hiddenu", "", "hiddenp"));
EXPECT_NO_THROW(config.add("foo", "", "", "hiddenp", false, ctx));
// Check that toElement keeps add order.
ElementPtr elem0 = Element::createMap();
elem0->set("user-file", Element::create(string("hiddenu")));
elem0->set("password-file", Element::create(string("hiddenp")));
clients = Element::createList();
clients->add(elem0);
clients->add(elem);
expected->set("clients", clients);
runToElementTest<BasicHttpAuthConfig>(expected, config);
}
// Test that basic auth configuration parses.
TEST(BasicHttpAuthConfigTest, parse) {
BasicHttpAuthConfig config;
ElementPtr cfg;
// No config is accepted.
EXPECT_NO_THROW(config.parse(cfg));
EXPECT_TRUE(config.empty());
EXPECT_TRUE(config.getClientList().empty());
EXPECT_TRUE(config.getCredentialMap().empty());
ElementPtr expected = Element::createMap();
expected->set("type", Element::create(string("basic")));
expected->set("realm", Element::create(string("")));
expected->set("directory", Element::create(string("")));
expected->set("clients", Element::createList());
runToElementTest<BasicHttpAuthConfig>(expected, config);
// The config must be a map.
cfg = Element::createList();
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"authentication must be a map (:0:0)");
// The type must be present.
cfg = Element::createMap();
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"type is required in authentication (:0:0)");
// The type must be a string.
cfg->set("type", Element::create(true));
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"type must be a string (:0:0)");
// The type must be basic.
cfg->set("type", Element::create(string("foobar")));
string errmsg = "only basic HTTP authentication is supported: type is ";
errmsg += "'foobar' not 'basic' (:0:0)";
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError, errmsg);
cfg->set("type", Element::create(string("basic")));
EXPECT_NO_THROW(config.parse(cfg));
// The realm must be a string.
cfg->set("realm", Element::createList());
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"realm must be a string (:0:0)");
cfg->set("realm", Element::create(string("my-realm")));
EXPECT_NO_THROW(config.parse(cfg));
// The directory must be a string.
cfg->set("directory", Element::createMap());
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"directory must be a string (:0:0)");
cfg->set("directory", Element::create(data_dir));
EXPECT_NO_THROW(config.parse(cfg));
// The user context must be a map.
ElementPtr ctx = Element::createList();
cfg->set("user-context", ctx);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"user-context must be a map (:0:0)");
ctx = Element::fromJSON("{ \"value\": \"a horse\" }");
cfg->set("user-context", ctx);
EXPECT_NO_THROW(config.parse(cfg));
// Clients must be a list.
ElementPtr clients_cfg = Element::createMap();
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"clients must be a list (:0:0)");
// The client config must be a map.
clients_cfg = Element::createList();
ElementPtr client_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"clients items must be maps (:0:0)");
// The user parameter is mandatory in client config
// without a password file.
client_cfg = Element::createMap();
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"user is required in clients items (:0:0)");
// The user parameter must be a string.
ElementPtr user_cfg = Element::create(1);
client_cfg = Element::createMap();
client_cfg->set("user", user_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"user must be a string (:0:0)");
// The user parameter must not be empty.
user_cfg = Element::create(string(""));
client_cfg = Element::createMap();
client_cfg->set("user", user_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"user must not be empty (:0:0)");
// The user parameter must not contain ':'.
user_cfg = Element::create(string("foo:bar"));
client_cfg = Element::createMap();
client_cfg->set("user", user_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"user must not contain a ':': 'foo:bar' (:0:0)");
// The user-file parameter must be a string.
ElementPtr user_file_cfg = Element::create(1);
client_cfg = Element::createMap();
client_cfg->set("user-file", user_file_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"user-file must be a string (:0:0)");
// The user and user-file parameters are incompatible.
client_cfg = Element::createMap();
client_cfg->set("user", user_cfg);
client_cfg->set("user-file", user_file_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"user (:0:0) and user-file (:0:0) are "
"mutually exclusive");
// The user-file parameter must not be empty.
user_file_cfg = Element::create(string("empty"));
client_cfg = Element::createMap();
client_cfg->set("user-file", user_file_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"user must not be empty from user-file "
"'empty' (:0:0)");
// The user-file parameter must not contain ':'.
user_file_cfg = Element::create(string("hiddens"));
client_cfg = Element::createMap();
client_cfg->set("user-file", user_file_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"user must not contain a ':' from user-file "
"'hiddens' (:0:0)");
// Password is not required.
user_cfg = Element::create(string("foo"));
client_cfg = Element::createMap();
client_cfg->set("user", user_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_NO_THROW(config.parse(cfg));
ASSERT_EQ(1, config.getClientList().size());
EXPECT_EQ("", config.getClientList().front().getPassword());
config.clear();
// The password parameter must be a string.
ElementPtr password_cfg = Element::create(1);
client_cfg = Element::createMap();
client_cfg->set("password", password_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"password must be a string (:0:0)");
// Empty password is accepted.
password_cfg = Element::create(string(""));
client_cfg = Element::createMap();
client_cfg->set("user", user_cfg);
client_cfg->set("password", password_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_NO_THROW(config.parse(cfg));
ASSERT_EQ(1, config.getClientList().size());
EXPECT_EQ("", config.getClientList().front().getPassword());
config.clear();
// The password-file parameter must be a string.
ElementPtr password_file_cfg = Element::create(1);
client_cfg = Element::createMap();
// user is not required when password-file is here.
client_cfg->set("password-file", password_file_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"password-file must be a string (:0:0)");
// The password and password-file parameters are incompatible.
client_cfg = Element::createMap();
client_cfg->set("user", user_cfg);
client_cfg->set("password", password_cfg);
client_cfg->set("password-file", password_file_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"password (:0:0) and password-file (:0:0) are "
"mutually exclusive");
// Empty password-file is accepted.
password_file_cfg = Element::create(string("empty"));
client_cfg = Element::createMap();
client_cfg->set("user", user_cfg);
client_cfg->set("password-file", password_file_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_NO_THROW(config.parse(cfg));
ASSERT_EQ(1, config.getClientList().size());
EXPECT_EQ("", config.getClientList().front().getPassword());
config.clear();
// password-file is enough.
password_file_cfg = Element::create(string("hiddens"));
client_cfg = Element::createMap();
client_cfg->set("password-file", password_file_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_NO_THROW(config.parse(cfg));
ASSERT_EQ(1, config.getClientList().size());
EXPECT_EQ("test", config.getClientList().front().getPassword());
config.clear();
// password-file only requires a ':' in the content.
password_file_cfg = Element::create(string("hiddenp"));
client_cfg = Element::createMap();
client_cfg->set("password-file", password_file_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"can't find the user id part in password-file "
"'hiddenp' (:0:0)");
// User context must be a map.
password_cfg = Element::create(string("bar"));
ctx = Element::createList();
client_cfg = Element::createMap();
client_cfg->set("user", user_cfg);
client_cfg->set("password", password_cfg);
client_cfg->set("user-context", ctx);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_THROW_MSG(config.parse(cfg), DhcpConfigError,
"user-context must be a map (:0:0)");
// Check a working not empty config.
ctx = Element::fromJSON("{ \"foo\": \"bar\" }");
client_cfg = Element::createMap();
client_cfg->set("user", user_cfg);
client_cfg->set("password", password_cfg);
client_cfg->set("user-context", ctx);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_NO_THROW(config.parse(cfg));
runToElementTest<BasicHttpAuthConfig>(cfg, config);
// Check a working not empty config with files.
config.clear();
client_cfg = Element::createMap();
user_file_cfg = Element::create(string("hiddenu"));
client_cfg->set("user-file", user_file_cfg);
client_cfg->set("password-file", password_file_cfg);
clients_cfg = Element::createList();
clients_cfg->add(client_cfg);
cfg->set("clients", clients_cfg);
EXPECT_NO_THROW(config.parse(cfg));
runToElementTest<BasicHttpAuthConfig>(cfg, config);
}
} // end of anonymous namespace
|