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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628 | // Copyright (C) 2017-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 <agent/ca_controller.h>
#include <agent/ca_process.h>
#include <agent/ca_command_mgr.h>
#include <agent/ca_response_creator.h>
#include <cc/command_interpreter.h>
#include <cryptolink/crypto_rng.h>
#include <hooks/hooks_manager.h>
#include <http/basic_auth_config.h>
#include <http/post_request.h>
#include <http/post_request_json.h>
#include <http/response_json.h>
#include <process/testutils/d_test_stubs.h>
#include <agent/tests/test_basic_auth_libraries.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/pointer_cast.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <functional><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc;
using namespace isc::agent;
using namespace isc::config;
using namespace isc::data;
using namespace isc::hooks;
using namespace isc::http;
using namespace isc::process;
namespace ph = std::placeholders;
namespace {
/// @brief Test fixture class for @ref CtrlAgentResponseCreator.
class CtrlAgentResponseCreatorTest : public DControllerTest {
public:
/// @brief Constructor.
///
/// Creates instance of the response creator and uses this instance to
/// create "empty" request. It also removes registered commands from the
/// command manager.
CtrlAgentResponseCreatorTest()
: DControllerTest(CtrlAgentController::instance),
response_creator_(),
request_(response_creator_.createNewHttpRequest()) {
// Deregisters commands.
CtrlAgentCommandMgr::instance().deregisterAll();
CtrlAgentCommandMgr::instance().
registerCommand("foo", std::bind(&CtrlAgentResponseCreatorTest::
fooCommandHandler,
this, ph::_1, ph::_2));
// Make sure that the request has been initialized properly.
if (!request_) {
ADD_FAILURE() << "CtrlAgentResponseCreator::createNewHttpRequest"
" returns NULL!";
}
HttpRequest::recordBasicAuth_ = true;
// Initialize process and cfgmgr.
try {
initProcess();
static_cast<void>(getCtrlAgentCfgContext());
} catch (const std::exception& ex) {
ADD_FAILURE() << "Initialization failed: " << ex.what();
}
}
/// @brief Destructor.
///
/// Removes registered commands from the command manager.
virtual ~CtrlAgentResponseCreatorTest() {
HttpRequest::recordBasicAuth_ = false;
CtrlAgentCommandMgr::instance().deregisterAll();
HooksManager::prepareUnloadLibraries();
static_cast<void>(HooksManager::unloadLibraries());
}
/// @brief Fills request context with required data to create new request.
///
/// @param request Request which context should be configured.
void setBasicContext(const HttpRequestPtr& request) {
request->context()->method_ = "POST";
request->context()->http_version_major_ = 1;
request->context()->http_version_minor_ = 1;
request->context()->uri_ = "/foo";
// Content-Type
HttpHeaderContext content_type;
content_type.name_ = "Content-Type";
content_type.value_ = "application/json";
request->context()->headers_.push_back(content_type);
// Content-Length
HttpHeaderContext content_length;
content_length.name_ = "Content-Length";
content_length.value_ = "0";
request->context()->headers_.push_back(content_length);
}
/// @brief Test creation of stock response.
///
/// @param status_code Status code to be included in the response.
/// @param must_contain Text that must be present in the textual
/// representation of the generated response.
void testStockResponse(const HttpStatusCode& status_code,
const std::string& must_contain) {
HttpResponsePtr response;
ASSERT_NO_THROW(
response = response_creator_.createStockHttpResponse(request_,
status_code)
);
ASSERT_TRUE(response);
HttpResponseJsonPtr response_json = boost::dynamic_pointer_cast<
HttpResponseJson>(response);
ASSERT_TRUE(response_json);
// Make sure the response contains the string specified as argument.
EXPECT_TRUE(response_json->toString().find(must_contain) != std::string::npos);
}
/// @brief Handler for the 'foo' test command.
///
/// @param command_name Command name, i.e. 'foo'.
/// @param command_arguments Command arguments (empty).
///
/// @return Returns response with a single string "bar".
ConstElementPtr fooCommandHandler(const std::string& /*command_name*/,
const ConstElementPtr& /*command_arguments*/) {
ElementPtr arguments = Element::createList();
arguments->add(Element::create("bar"));
return (createAnswer(CONTROL_RESULT_SUCCESS, arguments));
}
/// @brief Returns a pointer to the configuration context.
CtrlAgentCfgContextPtr getCtrlAgentCfgContext() {
CtrlAgentProcessPtr process =
boost::dynamic_pointer_cast<CtrlAgentProcess>(getProcess());
if (!process) {
isc_throw(Unexpected, "no process");
}
CtrlAgentCfgMgrPtr cfgmgr = process->getCtrlAgentCfgMgr();
if (!cfgmgr) {
isc_throw(Unexpected, "no cfgmgr");
}
CtrlAgentCfgContextPtr ctx = cfgmgr->getCtrlAgentCfgContext();
if (!ctx) {
isc_throw(Unexpected, "no context");
}
return (ctx);
}
/// @brief Convert header vector to header map.
static std::map<std::string, std::string>
headers2map(std::vector<HttpHeaderContext> headers) {
std::map<std::string, std::string> result;
for (const auto& header : headers) {
result[header.name_] = header.value_;
}
return (result);
}
/// @brief Instance of the response creator.
CtrlAgentResponseCreator response_creator_;
/// @brief Instance of the "empty" request.
///
/// The context belonging to this request may be modified by the unit
/// tests to verify various scenarios of response creation.
HttpRequestPtr request_;
};
// This test verifies that the created "empty" request has valid type.
TEST_F(CtrlAgentResponseCreatorTest, createNewHttpRequest) {<--- syntax error
// The request must be of PostHttpRequestJson type.
PostHttpRequestJsonPtr request_json = boost::dynamic_pointer_cast<
PostHttpRequestJson>(request_);
ASSERT_TRUE(request_json);
}
// Test that HTTP version of stock response is set to 1.0 if the request
// context doesn't specify any version.
TEST_F(CtrlAgentResponseCreatorTest, createStockHttpResponseNoVersion) {
testStockResponse(HttpStatusCode::BAD_REQUEST, "HTTP/1.0 400 Bad Request");
}
// Test that HTTP version of stock response is set to 1.0 if the request
// version is higher than 1.1.
TEST_F(CtrlAgentResponseCreatorTest, createStockHttpResponseHighVersion) {
request_->context()->http_version_major_ = 2;
testStockResponse(HttpStatusCode::REQUEST_TIMEOUT,
"HTTP/1.0 408 Request Timeout");
}
// Test that the server responds with version 1.1 if request version is 1.1.
TEST_F(CtrlAgentResponseCreatorTest, createStockHttpResponseCorrectVersion) {
request_->context()->http_version_major_ = 1;
request_->context()->http_version_minor_ = 1;
testStockResponse(HttpStatusCode::NO_CONTENT, "HTTP/1.1 204 No Content");
}
// Test that the server responds with extra headers for an error response.
TEST_F(CtrlAgentResponseCreatorTest, createStockHttpResponseHeaders) {
CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgContext();
ASSERT_TRUE(ctx);
// Add a STS header.
CfgHttpHeader hsts("Strict-Transport-Security", "max-age=31536000");
CfgHttpHeaders headers;
headers.push_back(hsts);
// Add a random header.
CfgHttpHeader foobar("Foo", "bar");
headers.push_back(foobar);
ctx->setHttpHeaders(headers);
// Set request.
request_->context()->http_version_major_ = 1;
request_->context()->http_version_minor_ = 1;
const HttpStatusCode& status_code = HttpStatusCode::NO_CONTENT;
HttpResponsePtr response;
response = response_creator_.createStockHttpResponse(request_, status_code);
ASSERT_TRUE(response);
// Check that the two extra headers are in the response.
auto got = headers2map(response->context()->headers_);
EXPECT_EQ("max-age=31536000", got["Strict-Transport-Security"]);
EXPECT_EQ("bar", got["Foo"]);
}
// Test successful server response when the client specifies valid command.
TEST_F(CtrlAgentResponseCreatorTest, createDynamicHttpResponse) {
setBasicContext(request_);
// Body: "foo" command has been registered in the test fixture constructor.
request_->context()->body_ = "{ \"command\": \"foo\" }";
// All requests must be finalized before they can be processed.
ASSERT_NO_THROW(request_->finalize());
// Create response from the request.
HttpResponsePtr response;
ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_));
ASSERT_TRUE(response);
// Response must be convertible to HttpResponseJsonPtr.
HttpResponseJsonPtr response_json = boost::dynamic_pointer_cast<
HttpResponseJson>(response);
ASSERT_TRUE(response_json);
// Response must be successful.
EXPECT_TRUE(response_json->toString().find("HTTP/1.1 200 OK") !=
std::string::npos);
// Response must contain JSON body with "result" of 0.
EXPECT_TRUE(response_json->toString().find("\"result\": 0") !=
std::string::npos);
}
// Test that the server responds with extra headers for a command response.
TEST_F(CtrlAgentResponseCreatorTest, createDynamicHttpResponseHeaders) {
CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgContext();
ASSERT_TRUE(ctx);
// Add a STS header.
CfgHttpHeader hsts("Strict-Transport-Security", "max-age=31536000");
CfgHttpHeaders headers;
headers.push_back(hsts);
// Add a random header.
CfgHttpHeader foobar("Foo", "bar");
headers.push_back(foobar);
ctx->setHttpHeaders(headers);
// From previous test.
setBasicContext(request_);
// Body: "foo" command has been registered in the test fixture constructor.
request_->context()->body_ = "{ \"command\": \"foo\" }";
// All requests must be finalized before they can be processed.
ASSERT_NO_THROW(request_->finalize());
// Create response from the request.
HttpResponsePtr response;
ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_));
ASSERT_TRUE(response);
// Check that the two extra headers are in the response.
auto got = headers2map(response->context()->headers_);
EXPECT_EQ("max-age=31536000", got["Strict-Transport-Security"]);
EXPECT_EQ("bar", got["Foo"]);
}
// This test verifies that Internal Server Error is returned when invalid C++
// request type is used. This is considered an error in the server logic.
TEST_F(CtrlAgentResponseCreatorTest, createDynamicHttpResponseInvalidType) {
PostHttpRequestPtr request(new PostHttpRequest());
setBasicContext(request);
// Body: "list-commands" is natively supported by the command manager.
request->context()->body_ = "{ \"command\": \"list-commands\" }";
// All requests must be finalized before they can be processed.
ASSERT_NO_THROW(request->finalize());
HttpResponsePtr response;
ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request));
ASSERT_TRUE(response);
// Response must be convertible to HttpResponseJsonPtr.
HttpResponseJsonPtr response_json = boost::dynamic_pointer_cast<
HttpResponseJson>(response);
ASSERT_TRUE(response_json);
// Response must contain Internal Server Error status code.
EXPECT_TRUE(response_json->toString().find("HTTP/1.1 500 Internal Server Error") !=
std::string::npos);
}
// This test verifies that Unauthorized is returned when authentication is
// required but not provided by request.
TEST_F(CtrlAgentResponseCreatorTest, noAuth) {
setBasicContext(request_);
// Body: "list-commands" is natively supported by the command manager.
request_->context()->body_ = "{ \"command\": \"list-commands\" }";
// All requests must be finalized before they can be processed.
ASSERT_NO_THROW(request_->finalize());
// Require authentication.
CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgContext();
ASSERT_TRUE(ctx);
BasicHttpAuthConfigPtr auth(new BasicHttpAuthConfig());
ASSERT_NO_THROW(ctx->setAuthConfig(auth));
auth->setRealm("ISC.ORG");
auth->add("foo", "", "bar", "");
HttpResponsePtr response;
ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_));
EXPECT_TRUE(request_->getBasicAuth().empty());
ASSERT_TRUE(response);
// Response must be convertible to HttpResponseJsonPtr.
HttpResponseJsonPtr response_json = boost::dynamic_pointer_cast<
HttpResponseJson>(response);
ASSERT_TRUE(response_json);
// Response must contain Unauthorized status code.
std::string expected = "HTTP/1.1 401 Unauthorized";
EXPECT_TRUE(response_json->toString().find(expected) != std::string::npos);
// Reponse should contain WWW-Authenticate header with configured realm.
expected = "WWW-Authenticate: Basic realm=\"ISC.ORG\"";
EXPECT_TRUE(response_json->toString().find(expected) != std::string::npos);
}
// Test that the server responds with extra headers for no auth response.
TEST_F(CtrlAgentResponseCreatorTest, noAuthHeader) {
setBasicContext(request_);
// Body: "list-commands" is natively supported by the command manager.
request_->context()->body_ = "{ \"command\": \"list-commands\" }";
// All requests must be finalized before they can be processed.
ASSERT_NO_THROW(request_->finalize());
// Require authentication.
CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgContext();
ASSERT_TRUE(ctx);
BasicHttpAuthConfigPtr auth(new BasicHttpAuthConfig());
ASSERT_NO_THROW(ctx->setAuthConfig(auth));
auth->setRealm("ISC.ORG");
auth->add("foo", "", "bar", "");
// Add a STS header.
CfgHttpHeader hsts("Strict-Transport-Security", "max-age=31536000");
CfgHttpHeaders headers;
headers.push_back(hsts);
// Add a random header.
CfgHttpHeader foobar("Foo", "bar");
headers.push_back(foobar);
ctx->setHttpHeaders(headers);
HttpResponsePtr response;
ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_));
EXPECT_TRUE(request_->getBasicAuth().empty());
ASSERT_TRUE(response);
// Check that the two extra headers are in the response.
auto got = headers2map(response->context()->headers_);
EXPECT_EQ("max-age=31536000", got["Strict-Transport-Security"]);
EXPECT_EQ("bar", got["Foo"]);
}
// Test successful server response when the client is authenticated.
TEST_F(CtrlAgentResponseCreatorTest, basicAuth) {
setBasicContext(request_);
// Body: "list-commands" is natively supported by the command manager.
request_->context()->body_ = "{ \"command\": \"list-commands\" }";
// Add basic HTTP authentication header.
const BasicHttpAuth& basic_auth = BasicHttpAuth("foo", "bar");
const BasicAuthHttpHeaderContext& basic_auth_header =
BasicAuthHttpHeaderContext(basic_auth);
request_->context()->headers_.push_back(basic_auth_header);
// All requests must be finalized before they can be processed.
ASSERT_NO_THROW(request_->finalize());
// Require authentication.
CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgContext();
ASSERT_TRUE(ctx);
BasicHttpAuthConfigPtr auth(new BasicHttpAuthConfig());
ASSERT_NO_THROW(ctx->setAuthConfig(auth));
// In fact the realm is used only on errors... set it anyway.
auth->setRealm("ISC.ORG");
auth->add("foo", "", "bar", "");
HttpResponsePtr response;
ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_));
EXPECT_EQ("foo", request_->getBasicAuth());
ASSERT_TRUE(response);
// Response must be convertible to HttpResponseJsonPtr.
HttpResponseJsonPtr response_json = boost::dynamic_pointer_cast<
HttpResponseJson>(response);
ASSERT_TRUE(response_json);
// Response must be successful.
EXPECT_TRUE(response_json->toString().find("HTTP/1.1 200 OK") !=
std::string::npos);
// Response must contain JSON body with "result" of 0.
EXPECT_TRUE(response_json->toString().find("\"result\": 0") !=
std::string::npos);
}
// This test verifies that Unauthorized is returned when authentication is
// required but not provided by request using the hook.
TEST_F(CtrlAgentResponseCreatorTest, hookNoAuth) {
setBasicContext(request_);
// Body: "list-commands" is natively supported by the command manager.
// We add a random value in the extra entry: see next unit test
// for an explanation about how it is used.
auto r32 = isc::cryptolink::random(4);
ASSERT_EQ(4, r32.size());
int extra = r32[0];
extra = (extra << 8) | r32[1];
extra = (extra << 8) | r32[2];
extra = (extra << 8) | r32[3];
request_->context()->body_ = "{ \"command\": \"list-commands\", ";
request_->context()->body_ += "\"extra\": " + std::to_string(extra) + " }";
// All requests must be finalized before they can be processed.
ASSERT_NO_THROW(request_->finalize());
// Setup hook.
CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgContext();
ASSERT_TRUE(ctx);
HooksConfig& hooks_cfg = ctx->getHooksConfig();
std::string auth_cfg = "{ \"config\": {\n"
"\"type\": \"basic\",\n"
"\"realm\": \"ISC.ORG\",\n"
"\"clients\": [{\n"
" \"user\": \"foo\",\n"
" \"password\": \"bar\"\n"
" }]}}";
ConstElementPtr auth_json;
ASSERT_NO_THROW(auth_json = Element::fromJSON(auth_cfg));
hooks_cfg.add(std::string(BASIC_AUTH_LIBRARY), auth_json);
ASSERT_NO_THROW(hooks_cfg.loadLibraries(false));
HttpResponsePtr response;
ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_));
EXPECT_TRUE(request_->getBasicAuth().empty());
ASSERT_TRUE(response);
// Request should have no extra.
EXPECT_EQ("{ \"command\": \"list-commands\" }",
request_->context()->body_);
// Response must be convertible to HttpResponseJsonPtr.
HttpResponseJsonPtr response_json = boost::dynamic_pointer_cast<
HttpResponseJson>(response);
ASSERT_TRUE(response_json);
// Response must contain Unauthorized status code.
std::string expected = "HTTP/1.1 401 Unauthorized";
EXPECT_TRUE(response_json->toString().find(expected) != std::string::npos);
// Reponse should contain WWW-Authenticate header with configured realm.
expected = "WWW-Authenticate: Basic realm=\"ISC.ORG\"";
EXPECT_TRUE(response_json->toString().find(expected) != std::string::npos);
}
// This test verifies that the server responds with extra headers for no
// auth response provided by the hool.
TEST_F(CtrlAgentResponseCreatorTest, hookNoAuthHeaders) {
setBasicContext(request_);
// Body: "list-commands" is natively supported by the command manager.
// We add a random value in the extra entry: see next unit test
// for an explanation about how it is used.
auto r32 = isc::cryptolink::random(4);
ASSERT_EQ(4, r32.size());
int extra = r32[0];
extra = (extra << 8) | r32[1];
extra = (extra << 8) | r32[2];
extra = (extra << 8) | r32[3];
request_->context()->body_ = "{ \"command\": \"list-commands\", ";
request_->context()->body_ += "\"extra\": " + std::to_string(extra) + " }";
// All requests must be finalized before they can be processed.
ASSERT_NO_THROW(request_->finalize());
// Setup hook.
CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgContext();
ASSERT_TRUE(ctx);
HooksConfig& hooks_cfg = ctx->getHooksConfig();
std::string auth_cfg = "{ \"config\": {\n"
"\"type\": \"basic\",\n"
"\"realm\": \"ISC.ORG\",\n"
"\"clients\": [{\n"
" \"user\": \"foo\",\n"
" \"password\": \"bar\"\n"
" }]}}";
ConstElementPtr auth_json;
ASSERT_NO_THROW(auth_json = Element::fromJSON(auth_cfg));
hooks_cfg.add(std::string(BASIC_AUTH_LIBRARY), auth_json);
ASSERT_NO_THROW(hooks_cfg.loadLibraries(false));
// Add a STS header.
CfgHttpHeader hsts("Strict-Transport-Security", "max-age=31536000");
CfgHttpHeaders headers;
headers.push_back(hsts);
// Add a random header.
CfgHttpHeader foobar("Foo", "bar");
headers.push_back(foobar);
ctx->setHttpHeaders(headers);
HttpResponsePtr response;
ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_));
EXPECT_TRUE(request_->getBasicAuth().empty());
ASSERT_TRUE(response);
// Check that the two extra headers are in the response.
auto got = headers2map(response->context()->headers_);
EXPECT_EQ("max-age=31536000", got["Strict-Transport-Security"]);
EXPECT_EQ("bar", got["Foo"]);
}
// Test successful server response when the client is authenticated.
TEST_F(CtrlAgentResponseCreatorTest, hookBasicAuth) {
setBasicContext(request_);
// Body: "list-commands" is natively supported by the command manager.
// We add a random value in the extra entry:
// - this proves that the http_auth callout can get the request argument
// - this proves that the http_auth callout can modify the request
// argument before the request is executed (the extra entry
// if still present would make the command to be rejected as malformed)
// - this proves that a value can be communicate between the http_auth
// and http_response callout points
// - this proves that the http_response callout can get the
// response argument
// - this proves that the http_response callout can modify the
// response argument
auto r32 = isc::cryptolink::random(4);
ASSERT_EQ(4, r32.size());
int extra = r32[0];
extra = (extra << 8) | r32[1];
extra = (extra << 8) | r32[2];
extra = (extra << 8) | r32[3];
if (extra == 0) {
extra = 1;
}
std::string extra_str = std::to_string(extra);
request_->context()->body_ = "{ \"command\": \"list-commands\", ";
request_->context()->body_ += "\"extra\": " + extra_str + " }";
// Add basic HTTP authentication header.
const BasicHttpAuth& basic_auth = BasicHttpAuth("foo", "bar");
const BasicAuthHttpHeaderContext& basic_auth_header =
BasicAuthHttpHeaderContext(basic_auth);
request_->context()->headers_.push_back(basic_auth_header);
// All requests must be finalized before they can be processed.
ASSERT_NO_THROW(request_->finalize());
// Setup hook.
CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgContext();
ASSERT_TRUE(ctx);
HooksConfig& hooks_cfg = ctx->getHooksConfig();
std::string auth_cfg = "{ \"config\": {\n"
"\"type\": \"basic\",\n"
"\"realm\": \"ISC.ORG\",\n"
"\"clients\": [{\n"
" \"user\": \"foo\",\n"
" \"password\": \"bar\"\n"
" }]}}";
ConstElementPtr auth_json;
ASSERT_NO_THROW(auth_json = Element::fromJSON(auth_cfg));
hooks_cfg.add(std::string(BASIC_AUTH_LIBRARY), auth_json);
ASSERT_NO_THROW(hooks_cfg.loadLibraries(false));
HttpResponsePtr response;
ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_));
EXPECT_EQ("foo", request_->getBasicAuth());
ASSERT_TRUE(response);
// Request should have no extra.
EXPECT_EQ("{ \"command\": \"list-commands\" }",
request_->context()->body_);
// Response must be convertible to HttpResponseJsonPtr.
HttpResponseJsonPtr response_json = boost::dynamic_pointer_cast<
HttpResponseJson>(response);
ASSERT_TRUE(response_json);
// Response must be successful.
EXPECT_TRUE(response_json->toString().find("HTTP/1.1 200 OK") !=
std::string::npos);
// Response must contain JSON body with "result" of 0.
EXPECT_TRUE(response_json->toString().find("\"result\": 0") !=
std::string::npos);
// Response must contain JSON body with "comment": "got".
std::string expected = "\"got\": " + extra_str + ", ";
EXPECT_TRUE(response_json->toString().find(expected) !=
std::string::npos);
}
}
|