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 | // 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 <asiolink/asio_wrapper.h>
#include <ha_test.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <asiolink/interval_timer.h>
#include <cc/command_interpreter.h>
#include <cc/data.h>
#include <dhcp/dhcp4.h>
#include <dhcp/dhcp6.h>
#include <dhcp/duid.h>
#include <dhcp/hwaddr.h>
#include <dhcp/option.h>
#include <dhcp/option_int.h>
#include <dhcpsrv/cfgmgr.h>
#include <hooks/hooks.h>
#include <hooks/hooks_manager.h>
#include <util/range_utilities.h>
#include <functional><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::hooks;
using namespace std;
namespace {
/// @brief Structure that holds registered hook indexes.
struct TestHooks {
int hooks_index_dhcp4_srv_configured_;
/// Constructor that registers hook points for the tests.
TestHooks() {
hooks_index_dhcp4_srv_configured_ = HooksManager::registerHook("dhcp4_srv_configured");
}
};
TestHooks Hooks;
}
namespace isc {
namespace ha {
namespace test {
HATest::HATest()
: io_service_(new IOService()),
network_state_(new NetworkState()) {
}
HATest::~HATest() {
if (timer_) {
timer_->cancel();
}
io_service_->stopAndPoll();
}
void
HATest::startHAService() {
if (HooksManager::calloutsPresent(Hooks.hooks_index_dhcp4_srv_configured_)) {
CalloutHandlePtr callout_handle = HooksManager::createCalloutHandle();
callout_handle->setArgument("network_state", network_state_);
HooksManager::callCallouts(Hooks.hooks_index_dhcp4_srv_configured_,
*callout_handle);
}
}
void
HATest::runIOService(long ms) {
io_service_->stop();
io_service_->restart();
timer_.reset(new IntervalTimer(io_service_));
timer_->setup(std::bind(&IOService::stop, io_service_), ms,
IntervalTimer::ONE_SHOT);
io_service_->run();
timer_->cancel();
}
void
HATest::runIOService(long ms, std::function<bool()> stop_condition) {
io_service_->stop();
io_service_->restart();
timer_.reset(new IntervalTimer(io_service_));
bool timeout = false;
timer_->setup(std::bind(&HATest::stopIOServiceHandler, this, std::ref(timeout)),
ms, IntervalTimer::ONE_SHOT);
while (!stop_condition() && !timeout) {
io_service_->runOne();
}
timer_->cancel();
io_service_->stopAndPoll();
}
boost::shared_ptr<std::thread>
HATest::runIOServiceInThread() {
io_service_->stop();
io_service_->restart();
bool running = false;
std::mutex mutex;
std::condition_variable condvar;
io_service_->post(std::bind(&HATest::signalServiceRunning, this, std::ref(running),
std::ref(mutex), std::ref(condvar)));
boost::shared_ptr<std::thread>
th(new std::thread(std::bind(&IOService::run, io_service_.get())));
std::unique_lock<std::mutex> lock(mutex);<--- The lock is ineffective because the mutex is locked at the same scope as the mutex itself.
while (!running) {
condvar.wait(lock);
}
return (th);
}
void
HATest::testSynchronousCommands(std::function<void()> commands) {
// Run IO service in thread.
auto thread = runIOServiceInThread();
// Run desired commands.
commands();
// Stop the IO service. This should cause the thread to terminate.
io_service_->stop();
thread->join();
}
void
HATest::signalServiceRunning(bool& running, std::mutex& mutex,
std::condition_variable& condvar) {
std::lock_guard<std::mutex> lock(mutex);
running = true;
condvar.notify_one();
}
void
HATest::checkThatTimeIsParsable(ElementPtr const& node, bool const null_expected) {
ConstElementPtr system_time(node->get("system-time"));
EXPECT_TRUE(system_time);
// Is freed automatically by std::locale. See [localization.locales.locale#6] and
// [localization.locales.locale.facet#2] in the C++ standard.
boost::posix_time::time_input_facet* facet(
new boost::posix_time::time_input_facet("%Y-%m-%d %H:%M:%S"));
stringstream ss;
ss.imbue(std::locale(std::locale(), facet));
if (null_expected) {
EXPECT_EQ(system_time->getType(), Element::null);
} else {
EXPECT_EQ(system_time->getType(), Element::string);
ss << system_time->stringValue();
boost::posix_time::ptime t;
ss >> t;
// Reset stringstream.
ss = stringstream();
ss << t;
EXPECT_NE(ss.str(), "not-a-date-time");
}
node->remove("system-time");
}
void
HATest::stopIOServiceHandler(bool& stop_flag) {
stop_flag = true;
}
ConstElementPtr
HATest::createValidJsonConfiguration(const HAConfig::HAMode& ha_mode) const {
std::ostringstream config_text;
config_text <<
"["
" {"
" \"this-server-name\": \"server1\","
" \"mode\": " << (ha_mode == HAConfig::LOAD_BALANCING ?
"\"load-balancing\"" : "\"hot-standby\"") << ","
" \"sync-page-limit\": 3,"
" \"heartbeat-delay\": 1000,"
" \"max-response-delay\": 1000,"
" \"max-ack-delay\": 10000,"
" \"max-unacked-clients\": 10,"
" \"max-rejected-clients\": 10,"
" \"multi-threading\": {"
" \"enable-multi-threading\": false"
" },"
" \"wait-backup-ack\": false,"
" \"peers\": ["
" {"
" \"name\": \"server1\","
" \"url\": \"http://127.0.0.1:18123/\","
" \"role\": \"primary\","
" \"auto-failover\": true"
" },"
" {"
" \"name\": \"server2\","
" \"url\": \"http://127.0.0.1:18124/\","
" \"role\": " << (ha_mode == HAConfig::LOAD_BALANCING ?
"\"secondary\"" : "\"standby\"") << ","
" \"auto-failover\": true"
" },"
" {"
" \"name\": \"server3\","
" \"url\": \"http://127.0.0.1:18125/\","
" \"role\": \"backup\","
" \"auto-failover\": false"
" }"
" ]"
" }"
"]";
return (Element::fromJSON(config_text.str()));
}
ConstElementPtr
HATest::createValidPassiveBackupJsonConfiguration() const {
std::ostringstream config_text;
config_text <<
"["
" {"
" \"this-server-name\": \"server1\","
" \"mode\": \"passive-backup\","
" \"sync-page-limit\": 3,"
" \"multi-threading\": {"
" \"enable-multi-threading\": false"
" },"
" \"wait-backup-ack\": false,"
" \"peers\": ["
" {"
" \"name\": \"server1\","
" \"url\": \"http://127.0.0.1:18123/\","
" \"role\": \"primary\""
" },"
" {"
" \"name\": \"server2\","
" \"url\": \"http://127.0.0.1:18124/\","
" \"role\": \"backup\""
" },"
" {"
" \"name\": \"server3\","
" \"url\": \"http://127.0.0.1:18125/\","
" \"role\": \"backup\""
" }"
" ]"
" }"
"]";
return (Element::fromJSON(config_text.str()));
}
ConstElementPtr
HATest::createValidHubJsonConfiguration() const {
std::ostringstream config_text;
config_text <<
"["
" {"
" \"this-server-name\": \"server2\","
" \"mode\": \"hot-standby\","
" \"sync-page-limit\": 3,"
" \"multi-threading\": {"
" \"enable-multi-threading\": false"
" },"
" \"wait-backup-ack\": false,"
" \"peers\": ["
" {"
" \"name\": \"server1\","
" \"url\": \"http://127.0.0.1:18123/\","
" \"role\": \"primary\""
" },"
" {"
" \"name\": \"server2\","
" \"url\": \"http://127.0.0.1:18124/\","
" \"role\": \"standby\""
" },"
" {"
" \"name\": \"server5\","
" \"url\": \"http://127.0.0.1:18125/\","
" \"role\": \"backup\""
" }"
" ]"
" },"
" {"
" \"this-server-name\": \"server4\","
" \"mode\": \"hot-standby\","
" \"multi-threading\": {"
" \"enable-multi-threading\": false"
" },"
" \"wait-backup-ack\": false,"
" \"peers\": ["
" {"
" \"name\": \"server3\","
" \"url\": \"http://127.0.0.1:18123/\","
" \"role\": \"primary\""
" },"
" {"
" \"name\": \"server4\","
" \"url\": \"http://127.0.0.1:18124/\","
" \"role\": \"standby\""
" },"
" {"
" \"name\": \"server6\","
" \"url\": \"http://127.0.0.1:18125/\","
" \"role\": \"backup\""
" }"
" ]"
" }"
"]";
return (Element::fromJSON(config_text.str()));
}
HAConfigPtr
HATest::createValidConfiguration(const HAConfig::HAMode& ha_mode) const {
auto config_storage = HAConfigParser::parse(createValidJsonConfiguration(ha_mode));
return (config_storage->get());
}
HAConfigPtr
HATest::createValidPassiveBackupConfiguration() const {
auto config_storage = HAConfigParser::parse(createValidPassiveBackupJsonConfiguration());
return (config_storage->get());
}
HAConfigPtr
HATest::createValidHubConfiguration() const {
auto config_storage = HAConfigParser::parse(createValidHubJsonConfiguration());
return (config_storage->get());
}
void
HATest::checkAnswer(const isc::data::ConstElementPtr& answer,
const int exp_status,
const std::string& exp_txt) {
int rcode = 0;
isc::data::ConstElementPtr comment;
comment = isc::config::parseAnswer(rcode, answer);
EXPECT_EQ(exp_status, rcode)
<< "Expected status code " << exp_status
<< " but received " << rcode << ", comment: "
<< (comment ? comment->str() : "(none)");
// Ok, parseAnswer interface is weird. If there are no arguments,
// it returns content of text. But if there is an argument,
// it returns the argument and it's not possible to retrieve
// "text" (i.e. comment).
if (comment->getType() != Element::string) {
comment = answer->get("text");
}
if (!exp_txt.empty()) {
EXPECT_EQ(exp_txt, comment->stringValue());
}
}
std::vector<uint8_t>
HATest::randomKey(const size_t key_size) const {
std::vector<uint8_t> key(key_size);
util::fillRandom(key.begin(), key.end());
return (key);
}
Pkt4Ptr
HATest::createMessage4(const uint8_t msg_type, const uint8_t hw_address_seed,
const uint8_t client_id_seed, const uint16_t secs) const {
Pkt4Ptr message(new Pkt4(msg_type, 0x1234));
HWAddrPtr hw_address(new HWAddr(std::vector<uint8_t>(6, hw_address_seed),
HTYPE_ETHER));
message->setHWAddr(hw_address);
message->setSecs(secs);
if (client_id_seed > 0) {
OptionPtr opt_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER,
std::vector<uint8_t>(6, client_id_seed)));
message->addOption(opt_client_id);
}
return (message);
}
Pkt4Ptr
HATest::createQuery4(const std::string& hw_address_text) const {
Pkt4Ptr query4(new Pkt4(DHCPDISCOVER, 0x1234));
HWAddr hwaddr = HWAddr::fromText(hw_address_text);
query4->setHWAddr(HWAddrPtr(new HWAddr(hwaddr.hwaddr_, HTYPE_ETHER)));
return (query4);
}
Pkt4Ptr
HATest::createQuery4(const std::vector<uint8_t>& hw_address,
const std::vector<uint8_t>& client_id) const {
Pkt4Ptr query4(new Pkt4(DHCPDISCOVER, 0x1234));
query4->setHWAddr(HWAddrPtr(new HWAddr(hw_address, HTYPE_ETHER)));
if (!client_id.empty()) {
OptionPtr opt_client_id(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER,
client_id));
query4->addOption(opt_client_id);
}
return (query4);
}
Pkt6Ptr
HATest::createQuery6(const std::vector<uint8_t>& duid) const {
Pkt6Ptr query6(new Pkt6(DHCPV6_SOLICIT, 0x1234));
OptionPtr opt_duid(new Option(Option::V6, D6O_CLIENTID, duid));
query6->addOption(opt_duid);
return (query6);
}
Pkt6Ptr
HATest::createMessage6(const uint8_t msg_type, const uint8_t duid_seed,
const uint16_t elapsed_time) const {
Pkt6Ptr message(new Pkt6(msg_type, 0x1234));
OptionPtr opt_duid(new Option(Option::V6, D6O_CLIENTID, OptionBuffer(8, duid_seed)));
message->addOption(opt_duid);
OptionUint16Ptr opt_elapsed_time(new OptionUint16(Option::V6, D6O_ELAPSED_TIME,
elapsed_time));
message->addOption(opt_elapsed_time);
return (message);
}
Pkt6Ptr
HATest::createQuery6(const std::string& duid_text) const {
Pkt6Ptr query6(new Pkt6(DHCPV6_SOLICIT, 0x1234));
DUID duid = DUID::fromText(duid_text);
OptionPtr client_id(new Option(Option::V6, D6O_CLIENTID, duid.getDuid()));
query6->addOption(client_id);
return (query6);
}
void
HATest::setDHCPMultiThreadingConfig(bool enabled, uint32_t thread_pool_size,
uint32_t packet_queue_size) {
ElementPtr mt_config = Element::createMap();
mt_config->set("enable-multi-threading", Element::create(enabled));
mt_config->set("thread-pool-size",
Element::create(static_cast<int>(thread_pool_size)));
mt_config->set("queue-size",
Element::create(static_cast<int>(packet_queue_size)));
CfgMgr::instance().getStagingCfg()->setDHCPMultiThreading(mt_config);
}
std::string
HATest::makeHAMtJson(bool enable_multi_threading, bool http_dedicated_listener,
uint32_t http_listener_threads, uint32_t http_client_threads) {
std::stringstream ss;
ss << "\"multi-threading\": {"
<< " \"enable-multi-threading\": "
<< (enable_multi_threading ? "true" : "false") << ","
<< " \"http-dedicated-listener\": "
<< (http_dedicated_listener ? "true" : "false") << ","
<< " \"http-listener-threads\": " << http_listener_threads << ","
<< " \"http-client-threads\": " << http_client_threads << "}";
return (ss.str());
}
} // namespace test
} // namespace ha
} // namespace isc
|