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
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
// Copyright (C) 2023-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.

/// @file This file contains tests which exercise the PingCheckMgr class.
#include <config.h>

#include <ping_check_mgr.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ping_test_utils.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cc/data.h>
#include <dhcp/pkt4.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/lease.h>
#include <hooks/hooks_manager.h>
#include <util/chrono_time_utils.h>
#include <testutils/gtest_utils.h>
#include <testutils/multi_threading_utils.h>

#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <mutex><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <chrono><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::util;
using namespace isc::asiolink;
using namespace isc::ping_check;
using namespace isc::hooks;
using namespace isc::test;
using namespace std::chrono;
using namespace boost::asio::error;

namespace ph = std::placeholders;

namespace {

// Sanity check the basics for production class, PingCheckMgr, single-threaded mode.
TEST(PingCheckMgr, basicsST) {
    SKIP_IF(IOServiceTest::notRoot());
    MultiThreadingMgr::instance().setMode(false);

    // Create a multi-threaded manager.
    IOServicePtr main_ios(new IOService());
    PingCheckMgrPtr mgr;
    ASSERT_NO_THROW_LOG(mgr.reset(new PingCheckMgr(0)));
    ASSERT_TRUE(mgr);
    mgr->setIOService(main_ios);

    // Sanity check the global configuration. More robust tests are done
    // elsewhere.
    auto& config = mgr->getGlobalConfig();
    EXPECT_TRUE(config->getEnablePingCheck());
    EXPECT_EQ(1, config->getMinPingRequests());
    EXPECT_EQ(100, config->getReplyTimeout());
    EXPECT_EQ(60, config->getPingClttSecs());
    EXPECT_EQ(0, config->getPingChannelThreads());

    // Verify we report as stopped.
    EXPECT_FALSE(mgr->isRunning());
    EXPECT_TRUE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Starting it should be OK.
    ASSERT_NO_THROW_LOG(mgr->start());

    // Verify we report as running.
    EXPECT_TRUE(mgr->isRunning());
    EXPECT_FALSE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Pausing it should be harmless.
    ASSERT_NO_THROW_LOG(mgr->pause());

    // Verify we report as running.
    EXPECT_TRUE(mgr->isRunning());
    EXPECT_FALSE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Resuming it should be harmless.
    ASSERT_NO_THROW_LOG(mgr->resume());

    // Verify we report as running.
    EXPECT_TRUE(mgr->isRunning());
    EXPECT_FALSE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Stopping it should be fine
    ASSERT_NO_THROW_LOG(mgr->stop());

    // Verify we report as stopped.
    EXPECT_FALSE(mgr->isRunning());
    EXPECT_TRUE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Re-starting it should be OK.
    ASSERT_NO_THROW_LOG(mgr->start());

    // Verify we report as running.
    EXPECT_TRUE(mgr->isRunning());
    EXPECT_FALSE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Calling destructor when its running should be OK.
    ASSERT_NO_THROW_LOG(mgr.reset());

    main_ios->stopAndPoll();
}

// Sanity check the basics for production class, PingCheckMgr. Bulk of testing
// is done with test derivation, TestPingCheckMgr.
TEST(PingCheckMgr, basicsMT) {<--- syntax error
    SKIP_IF(IOServiceTest::notRoot());
    MultiThreadingTest mt;

    // Create a multi-threaded manager.
    IOServicePtr main_ios(new IOService());
    PingCheckMgrPtr mgr;
    ASSERT_NO_THROW_LOG(mgr.reset(new PingCheckMgr(3)));
    ASSERT_TRUE(mgr);
    mgr->setIOService(main_ios);

    // Sanity check the global configuration. More robust tests are done
    // elsewhere.
    auto& config = mgr->getGlobalConfig();
    EXPECT_TRUE(config->getEnablePingCheck());
    EXPECT_EQ(1, config->getMinPingRequests());
    EXPECT_EQ(100, config->getReplyTimeout());
    EXPECT_EQ(60, config->getPingClttSecs());
    EXPECT_EQ(3, config->getPingChannelThreads());

    // It should not be running yet.
    EXPECT_FALSE(mgr->isRunning());
    EXPECT_TRUE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Starting it should be OK.
    ASSERT_NO_THROW_LOG(mgr->start());

    // Verify it's running.
    EXPECT_TRUE(mgr->isRunning());
    EXPECT_FALSE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Pausing it should be fine.
    ASSERT_NO_THROW_LOG(mgr->pause());

    // Verify it's paused.
    EXPECT_FALSE(mgr->isRunning());
    EXPECT_FALSE(mgr->isStopped());
    EXPECT_TRUE(mgr->isPaused());

    // Resuming it should be fine.
    ASSERT_NO_THROW_LOG(mgr->resume());

    // Verify it's running.
    EXPECT_TRUE(mgr->isRunning());
    EXPECT_FALSE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Stopping it should be fine
    ASSERT_NO_THROW_LOG(mgr->stop());

    // It should not be running.
    EXPECT_FALSE(mgr->isRunning());
    EXPECT_TRUE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Re-starting it should be OK.
    ASSERT_NO_THROW_LOG(mgr->start());

    // Verify it's running.
    EXPECT_TRUE(mgr->isRunning());
    EXPECT_FALSE(mgr->isStopped());
    EXPECT_FALSE(mgr->isPaused());

    // Calling destructor when its running should be OK.
    ASSERT_NO_THROW_LOG(mgr.reset());
}

// Verify basic behavior of PingCheckMgr::configure().
TEST(PingCheckMgr, configure) {
    // Create a manager.
    IOServicePtr main_ios(new IOService());
    PingCheckMgrPtr mgr;
    ASSERT_NO_THROW_LOG(mgr.reset(new PingCheckMgr()));
    ASSERT_TRUE(mgr);

    // Verify initial global configuration.
    auto& default_config = mgr->getGlobalConfig();
    EXPECT_TRUE(default_config->getEnablePingCheck());
    EXPECT_EQ(1, default_config->getMinPingRequests());
    EXPECT_EQ(100, default_config->getReplyTimeout());
    EXPECT_EQ(60, default_config->getPingClttSecs());
    EXPECT_EQ(0, default_config->getPingChannelThreads());

    //Create a valid configuration.
    std::string valid_json_cfg =
        R"({
            "enable-ping-check" : false,
            "min-ping-requests" : 2,
            "reply-timeout" : 250,
            "ping-cltt-secs" : 90,
            "ping-channel-threads" : 3
        })";

    auto parameters = Element::fromJSON(valid_json_cfg);

    // Parse it.
    ASSERT_NO_THROW_LOG(mgr->configure(parameters));

    // Verify updated global configuration.
    auto& config = mgr->getGlobalConfig();
    ASSERT_TRUE(config);
    EXPECT_FALSE(config->getEnablePingCheck());
    EXPECT_EQ(2, config->getMinPingRequests());
    EXPECT_EQ(250, config->getReplyTimeout());
    EXPECT_EQ(90, config->getPingClttSecs());
    EXPECT_EQ(3, config->getPingChannelThreads());

    // Create an invalid configuration.
    std::string invalid_json_cfg =
        R"({
            "enable-ping-check" : true,
            "min-ping-requests" : 4,
            "reply-timeout" : 500,
            "ping-cltt-secs" : 45,
            "ping-channel-threads" : 6,
            "bogus" : 0
        })";

    parameters = Element::fromJSON(invalid_json_cfg);

    // Parsing it should throw.
    ASSERT_THROW_MSG(mgr->configure(parameters), DhcpConfigError, "spurious 'bogus' parameter");

    // Verify configuration values were left unchanged.
    auto& final_config = mgr->getGlobalConfig();
    ASSERT_TRUE(final_config);
    EXPECT_EQ(final_config->getEnablePingCheck(), config->getEnablePingCheck());
    EXPECT_EQ(final_config->getMinPingRequests(), config->getMinPingRequests());
    EXPECT_EQ(final_config->getReplyTimeout(), config->getReplyTimeout());
    EXPECT_EQ(final_config->getPingClttSecs(), config->getPingClttSecs());
    EXPECT_EQ(final_config->getPingChannelThreads(), config->getPingChannelThreads());
}

/// @brief Defines a callback to invoke at the bottom of sendCompleted()
typedef std::function<void(const ICMPMsgPtr& echo, bool send_failed)> SendCompletedCallback;

/// @brief Defines a callback to invoke at the bottom of replyReceived()
typedef std::function<void(const ICMPMsgPtr& reply)> ReplyReceivedCallback;

/// @brief Testable derivation of PingCheckMgr
///
/// Uses a TestablePingChannel to facilitate more robust testing.
class TestablePingCheckMgr : public PingCheckMgr {
public:
    /// @brief Constructor.
    ///
    /// @param num_threads number of threads to use in the thread pool (0 means follow
    /// core thread pool size)
    /// @param min_echos minimum number of ECHO REQUESTs sent without replies
    /// received required to declare an address free to offer. Defaults to 1,
    /// must be greater than zero.
    /// @param reply_timeout maximum number of milliseconds to wait for an
    /// ECHO REPLY after an ECHO REQUEST has been sent.  Defaults to 100,
    TestablePingCheckMgr(uint32_t num_threads, uint32_t min_echos = 1,
                         uint32_t reply_timeout = 100)
        : PingCheckMgr(num_threads, min_echos, reply_timeout),
          post_send_completed_cb_(SendCompletedCallback()),
          post_reply_received_cb_(ReplyReceivedCallback()) {
    }

    /// @brief Destructor.
    virtual ~TestablePingCheckMgr() {
        post_send_completed_cb_ = SendCompletedCallback();
        post_reply_received_cb_ = ReplyReceivedCallback();
        if (getIOService()) {
            getIOService()->stopAndPoll();
        }
    }

    /// @brief Fetch the current channel instance.
    ///
    /// @return pointer to the TestablePingChannel instance (or an empty pointer).
    TestablePingChannelPtr getChannel() {
        return (boost::dynamic_pointer_cast<TestablePingChannel>(channel_));
    }

    /// @brief Fetches the manager's context store.
    ///
    /// @return Pointer to the PingContextStore.
    PingContextStorePtr getStore() {
        return (store_);
    }

    /// @brief Fetches the expiration timer's current interval (in milliseconds).
    ///
    /// @return current interval as long or 0L if the timer is not currently
    /// running or does not exist.
    long getExpirationTimerInterval() {
        if (expiration_timer_) {
            return (expiration_timer_->getInterval());
        }

        return (0);
    }

protected:
    /// @brief Creates a TestablePingChannel instance.
    ///
    /// This override the base case creator.
    ///
    /// @param io_service IOService that will drive the channel.
    /// @return pointer to the newly created channel.
    virtual PingChannelPtr createChannel(asiolink::IOServicePtr io_service) {
        return (TestablePingChannelPtr(
            new TestablePingChannel(io_service,
                                    std::bind(&PingCheckMgr::nextToSend, this, ph::_1),
                                    std::bind(&TestablePingCheckMgr::sendCompleted,
                                              this, ph::_1, ph::_2),
                                    std::bind(&TestablePingCheckMgr::replyReceived, this, ph::_1),
                                    std::bind(&PingCheckMgr::channelShutdown, this))));
    }

public:
    /// @brief Fetches the current size of the parking lot.
    ///
    /// @return size_t containing the number of entries parked.
    size_t parkingLotSize() const {
        auto const& parking_lot = ServerHooks::getServerHooks().getParkingLotPtr("lease4_offer");
        return (parking_lot->size());
    }

    /// @brief Callback passed to PingChannel to invoke when an ECHO REQUEST
    /// send has completed.
    ///
    /// -# Invokes the base class implementation
    /// -# Invokes an optional callback
    ///
    /// @param echo ICMP echo message that is sent.
    /// @param send_failed True if the send completed with a non-fatal error,
    /// false otherwise.
    virtual void sendCompleted(const ICMPMsgPtr& echo, bool send_failed) {
        // Call the production callback.
        PingCheckMgr::sendCompleted(echo, send_failed);

        // Invoke the post check, if one.
        if (post_send_completed_cb_) {
            (post_send_completed_cb_)(echo, send_failed);
        }
    }

    /// @brief Callback invoked by the channel to process received ICMP messages.
    ///
    /// -# Invokes the base class implementation
    /// -# Pauses the test IOService thread and returns if the parking lot is empty
    /// -# Invokes an option callback passing in the reply received
    ///
    /// @param reply pointer to the ICMP message received.
    virtual void replyReceived(const ICMPMsgPtr& reply) {
        if (reply->getType() == ICMPMsg::ECHO_REQUEST) {
            return;
        }

        // If we're routing loopback messages, look up the original address based
        // on the sequence number and use it as the reply's source address.
        if (getChannel()->route_loopback_) {
            IOAddress address = getChannel()->loopback_map_.find(reply->getSequence());
            if (address  != IOAddress::IPV4_ZERO_ADDRESS()) {
                reply->setSource(address);
            }
        }

        // Call the production callback.
        PingCheckMgr::replyReceived(reply);

        // Invoke the post check, if one.
        if (post_reply_received_cb_) {
            (post_reply_received_cb_)(reply);
        }
    }

    /// @brief Fetches the thread pool (if it exists).
    ///
    /// @return pointer to theIoServiceThreadPool.  Will be empty
    /// in ST mode or if the manager has not been started.
    asiolink::IoServiceThreadPoolPtr getThreadPool() {
        return (thread_pool_);
    }

    /// @brief Sets the network_state object.
    ///
    /// @param network_state pointer to a NetworkState instance.
    void setNetworkState(NetworkStatePtr network_state) {
        network_state_ = network_state;
    }

    /// @brief Callback to invoke at the bottom of sendCompleted().
    SendCompletedCallback post_send_completed_cb_;

    /// @brief Callback to invoke at the bottom of replyReceived().
    ReplyReceivedCallback post_reply_received_cb_;
};

/// @brief Defines a shared pointer to a PingCheckMgr.
typedef boost::shared_ptr<TestablePingCheckMgr> TestablePingCheckMgrPtr;

/// @brief Holds a lease and its associated query.
struct LeaseQueryPair {
public:
    /// @brief Constructor.
    ///
    /// @param lease pointer to the lease.
    /// @param query pointer to the query.
    LeaseQueryPair(Lease4Ptr lease, Pkt4Ptr query) : lease_(lease), query_(query) {
    };

    /// @brief Pointer to the lease.
    Lease4Ptr lease_;

    /// @brief Pointer to the query.
    Pkt4Ptr query_;
};

/// @brief Container of leases and their associated queries.
typedef std::vector<LeaseQueryPair> LeaseQueryPairs;

/// @brief Test fixture for exercising PingCheckMgr.
///
/// Uses a TestablePingCheckMgr instance for all tests and
/// provides numerous helper functions.
class PingCheckMgrTest : public IOServiceTest {
public:
    /// @brief Constructor.
    PingCheckMgrTest() : mgr_(), lease_query_pairs_(), mutex_(new mutex()),
        test_start_time_(PingContext::now()), unparked_(0) {
        MultiThreadingMgr::instance().setMode(false);
    };

    /// @brief Destructor.
    virtual ~PingCheckMgrTest() {
        test_timer_.cancel();
        test_io_service_->stopAndPoll();
        MultiThreadingMgr::instance().setMode(false);
    }

    /// @brief Pretest setup.
    ///
    /// Registers the hook point and creates its parking lot.
    virtual void SetUp() {
        HooksManager::registerHook("lease4_offer");
        parking_lot_ = boost::make_shared<ParkingLotHandle>(
                        ServerHooks::getServerHooks().getParkingLotPtr("lease4_offer"));
    }

    /// @brief Ensure we stop cleanly.
    virtual void TearDown() {
        if (mgr_) {
            mgr_->stop();
        }

        HooksManager::clearParkingLots();
    }

    /// @brief Creates the test's manager instance.
    ///
    /// @param num_threads number of threads in the thread pool.
    /// @param min_echos minimum number of echos per ping check.
    /// @param reply_timeout reply timeout per ping.
    /// @param start_and_pause when false, the manager is only created,
    /// when true it is created, started and then paused.  This allows
    /// manipulation of context store contents while the threads are doing
    /// no work.
    void createMgr(uint32_t num_threads,
                   uint32_t min_echos = 1,
                   uint32_t reply_timeout = 100,
                   bool start_and_pause = false) {
        ASSERT_NO_THROW_LOG(
            mgr_.reset(new TestablePingCheckMgr(num_threads, min_echos, reply_timeout)));
        ASSERT_TRUE(mgr_);
        mgr_->setIOService(test_io_service_);

        if (start_and_pause) {
            ASSERT_NO_THROW_LOG(mgr_->start());

            if (!MultiThreadingMgr::instance().getMode()) {
                ASSERT_FALSE(mgr_->getThreadPool());
            } else {
                ASSERT_TRUE(mgr_->getThreadPool());
                ASSERT_NO_THROW_LOG(mgr_->pause());
                ASSERT_TRUE(mgr_->isPaused());
            }
        }
    }

    /// @brief Add a new lease and query pair to the test's list of lease query pairs.
    ///
    /// Creates a bare-bones DHCPv4 lease and DHCPDISCOVER, wraps them in a
    /// LeaseQueryPair and adds the pair to the end of the test's internal
    /// list of pairs, lease_query_pairs_.
    ///
    /// @param target IOAddress of the lease.
    /// @param transid transaction id of the query.
    ///
    /// @return A copy of the newly created pair.
    LeaseQueryPair makeLeaseQueryPair(IOAddress target, uint16_t transid) {
        // Make a lease and query pair
        Lease4Ptr lease(new Lease4());
        lease->addr_ = IOAddress(target);
        Pkt4Ptr query(new Pkt4(DHCPDISCOVER, transid));
        LeaseQueryPair lqp(lease, query);
        lease_query_pairs_.push_back(lqp);
        return (lqp);
    }

    /// @brief Start ping checks for a given number of targets.
    ///
    /// The function first creates and parks the given number of targets, and
    /// then starts a ping check for each of them. Parking them all first
    /// establishes the number of ping checks expected to be conducted during
    /// the test prior to actually starting any of them. This avoids the
    /// parking lot from becoming empty part way through the test.
    ///
    /// It unpark callback lambda increments the unparked_ counter and then
    /// pushes the unparked lease/query pair to either the list of frees
    /// or list of declines.
    ///
    /// @param num_targets number of target ip addresses to ping check.
    /// @param start_address starting target address. Defaults to 127.0.0.1.
    ///
    /// @return last target address started.
    IOAddress startTargets(size_t num_targets, IOAddress start_address = IOAddress("127.0.0.1")) {
        IOAddress target = start_address;
        for (auto i = 0; i < num_targets; ++i) {
            auto lqp = makeLeaseQueryPair(IOAddress(target), i+1);
            HooksManager::park("lease4_offer", lqp.query_,
                [this, lqp]() {
                    MultiThreadingLock lock(*mutex_);
                    ++unparked_;
                    auto handle = lqp.query_->getCalloutHandle();
                    bool offer_address_in_use;
                    handle->getArgument("offer_address_in_use", offer_address_in_use);
                    offer_address_in_use ?  declines_.push_back(lqp) : frees_.push_back(lqp);
               });

            try {
                mgr_->startPing(lqp.lease_, lqp.query_, parking_lot_);
            } catch (const std::exception& ex) {
                ADD_FAILURE() << "startPing threw: " << ex.what();
            }

            target = IOAddress::increase(target);
        }

        return(target);
    }

    /// @brief Fetches the context, by lease address, from the store for a
    /// given lease query pair.
    ///
    /// @param lqp LeaseQueryPair for which the context is desired.
    /// @return pointer to the found context or an empty pointer.
    PingContextPtr getContext(const LeaseQueryPair& lqp) {
        return (getContext(lqp.lease_->addr_));
    }

    /// @brief Fetches the context, by lease address, from the store for address.
    ///
    /// @param address lease ip address for which the context is desired.
    /// @return pointer to the found context or an empty pointer.
    PingContextPtr getContext(const IOAddress& address) {
        return (mgr_->getStore()->getContextByAddress(address));
    }

    /// @brief Updates a context in the store.
    ///
    /// @param context context to update.
    void updateContext(PingContextPtr& context) {
        ASSERT_NO_THROW_LOG(mgr_->getStore()->updateContext(context));
    }

    /// @brief Tests equality of two timestamps within a given tolerance.
    ///
    /// The two time stamps are considered equal if the absolute value of their
    /// difference is between 0 and the specified tolerance (inclusive).
    ///
    /// @param lhs first TimeStamp to compare.
    /// @param rhs second TimeStamp to compare.
    /// @param tolerance margin of difference allowed for equality in milliseconds.
    /// Defaults to 10.
    ///
    /// @return True if the time stamps are "equal", false otherwise.
    bool fuzzyEqual(const TimeStamp& lhs, const TimeStamp& rhs, long tolerance = 10) {
        auto diff = abs(duration_cast<milliseconds>(lhs - rhs).count());
        return (diff >= 0 && diff <= tolerance);
    }

    /// @brief Tests equality of two longs within a given tolerance.
    ///
    /// The two values are considered equal if the absolute value of their
    /// difference is between 0 and the specified tolerance (inclusive).
    ///
    /// @param lhs first value to compare.
    /// @param rhs second value to compare.
    /// @param tolerance margin of difference allowed for equality in milliseconds.
    /// Defaults to 10.
    ///
    /// @return True if the time values are "equal", false otherwise.
    bool fuzzyEqual(const long& lhs, const long& rhs, long tolerance = 10) {
        auto diff = abs(lhs - rhs);
        return (diff >= 0 && diff <= tolerance);
    }

    /// @brief Creates an ECHO REQUEST message from a given address.
    ///
    /// @param target ip address to use as the echo's destination address.
    /// @return Pointer to the new message.
    ICMPMsgPtr makeEchoRequest(const IOAddress& target) {
        ICMPMsgPtr msg(new ICMPMsg());
        msg->setType(ICMPMsg::ECHO_REQUEST);
        msg->setDestination(IOAddress(target));
        msg->setSource(IOAddress("127.0.0.1"));
        return (msg);
    }

    /// @brief Creates an ECHO_REPLY message from a given address.
    ///
    /// @param from ip address to use as the reply's source address.
    /// @return Pointer to the new message.
    ICMPMsgPtr makeEchoReply(const IOAddress& from) {
        ICMPMsgPtr msg(new ICMPMsg());
        msg->setType(ICMPMsg::ECHO_REPLY);
        msg->setSource(IOAddress(from));
        msg->setDestination(IOAddress("127.0.0.1"));
        return (msg);
    }

    /// @brief Creates an TARGET_UNREACHABLE message from a given address.
    ///
    /// @param target ip address to use as the reply's source address.
    /// @return Pointer to the new message.
    ICMPMsgPtr makeUnreachable(const IOAddress& target) {
        // Make the TARGET_UNREACHABLE message first.
        ICMPMsgPtr msg(new ICMPMsg());
        msg->setType(ICMPMsg::TARGET_UNREACHABLE);
        msg->setSource(IOAddress("127.0.0.1"));
        msg->setDestination(IOAddress("127.0.0.1"));

        // Now embed the ping target's "original" echo into the unreachable
        // message's payload.  This includes the IP header followed by the
        // ECHO REQUEST.  First make the IP header and add it to the payload.
        // We only set values we care about.
        struct ip ip_header;
        memset((void *)(&ip_header), 0x00, sizeof(struct ip));
        ip_header.ip_v = 4;
        ip_header.ip_hl = 5;     /* shift left twice = 20 */
        ip_header.ip_len = 48;   /* ip_header + echo length */
        ip_header.ip_dst.s_addr = htonl(target.toUint32());
        ip_header.ip_src.s_addr = htonl(msg->getSource().toUint32());
        msg->setPayload((const uint8_t*)(&ip_header), sizeof(struct ip));

        // Now make the ECHO_REQUEST, pack it and add that to the payload.
        ICMPMsgPtr echo = makeEchoRequest(target);
        ICMPPtr packed_echo = echo->pack();
        msg->setPayload((const uint8_t*)(packed_echo.get()), sizeof(struct icmp));

        return (msg);
    }

    /// @brief Compares a LeaseQueryPair collection to the internal collection
    /// of pairs created (see makeLeaseQueryPairs()).
    ///
    /// @param test_collection Collection of pairs to compare against those in
    /// the creation collection.
    void compareLeaseQueryPairs(LeaseQueryPairs& test_collection) {
        // We should have as many in the test_collection as we have creation
        // collection.
        ASSERT_EQ(test_collection.size(), lease_query_pairs_.size());

        // Order is not guaranteed so we sort both lists then compare.
        std::sort(test_collection.begin(), test_collection.end(),
            [](LeaseQueryPair const& a, LeaseQueryPair const& b)
                { return (a.lease_->addr_ < b.lease_->addr_); });

        std::sort(lease_query_pairs_.begin(), lease_query_pairs_.end(),
            [](LeaseQueryPair const& a, LeaseQueryPair const& b)
                { return (a.lease_->addr_ < b.lease_->addr_); });

        auto dpi = test_collection.begin();
        for (auto const& lqpi : lease_query_pairs_) {
            ASSERT_EQ((*dpi).lease_->addr_, lqpi.lease_->addr_);
            ++dpi;
        }
    }

    /// @brief Exercises the operational basics: create, start, and stop
    /// for TestablePingCheckMgr.
    ///
    /// @param num_threads number of threads in the thread pool.
    void testOperationalBasics(size_t num_threads) {
        SKIP_IF(notRoot());

        // Create manager with the given number of threads.
        ASSERT_NO_THROW_LOG(createMgr(num_threads));
        ASSERT_TRUE(mgr_);

        // Should not be running.
        EXPECT_FALSE(mgr_->isRunning());
        EXPECT_TRUE(mgr_->isStopped());
        EXPECT_FALSE(mgr_->isPaused());

        // Channel should not yet exist.
        ASSERT_FALSE(mgr_->getChannel());

        // Start the manager.
        ASSERT_NO_THROW_LOG(mgr_->start());

        // Thread pool should exist in MT mode only.
        if (MultiThreadingMgr::instance().getMode()) {
            ASSERT_TRUE(mgr_->getThreadPool());
        } else {
            ASSERT_FALSE(mgr_->getThreadPool());
        }

        // Should be running.
        EXPECT_TRUE(mgr_->isRunning());
        EXPECT_FALSE(mgr_->isStopped());
        EXPECT_FALSE(mgr_->isPaused());

        // Channel should exist and be open.
        auto channel = mgr_->getChannel();
        ASSERT_TRUE(channel);
        ASSERT_TRUE(channel->isOpen());

        // Context store should exist and be empty.
        auto store = mgr_->getStore();
        ASSERT_TRUE(store);
        auto pings = store->getAll();
        ASSERT_EQ(0, pings->size());

        // Destruction should be graceful.
        ASSERT_NO_THROW_LOG(mgr_.reset());
    }

    /// @brief Verifies that startPing() creates a new context in the store and
    /// it can be fetched with the nextToSend() callback.
    void testStartPing() {
        SKIP_IF(notRoot());

        // Create manager with thread-pool size of 3, min_echos 2, reply_timeout 250 ms.
        // ST mode should ingore requested thread number.
        ASSERT_NO_THROW_LOG(createMgr(3, 2, 250));
        ASSERT_TRUE(mgr_);

        // Make a lease and query pair
        auto lqp1 = makeLeaseQueryPair(IOAddress("127.0.0.101"), 101);

        // Channel isn't open, startPing should throw.
        ASSERT_THROW_MSG(mgr_->startPing(lqp1.lease_, lqp1.query_, parking_lot_), InvalidOperation,
                         "PingCheckMgr::startPing() - channel isn't open");

        // Start the manager.  This will open the channel.
        ASSERT_NO_THROW_LOG(mgr_->start());
        ASSERT_TRUE(mgr_->isRunning());

        if (mgr_->getThreadPool()) {
            // Pause the manager so startPing() will succeed but no events will occur.
            // This should let us add contexts that sit in WAITING_TO_SEND state.
            ASSERT_NO_THROW_LOG(mgr_->pause());
            ASSERT_TRUE(mgr_->isPaused());
        }

        // Call startPing() again. It should work.
        ASSERT_NO_THROW_LOG(mgr_->startPing(lqp1.lease_, lqp1.query_, parking_lot_));

        // Calling  startPing() on the same lease should fail, duplicates not allowed.
        ASSERT_THROW_MSG(mgr_->startPing(lqp1.lease_, lqp1.query_, parking_lot_), DuplicateContext,
                         "PingContextStore::addContex: context already exists for: 127.0.0.101");

        // Our context should be present.
        auto const& store = mgr_->getStore();
        auto pings = store->getAll();
        ASSERT_EQ(1, pings->size());
        PingContextPtr context1;
        ASSERT_NO_THROW_LOG(context1 = store->getContextByAddress(lqp1.lease_->addr_));
        ASSERT_TRUE(context1);

        // Verify the context's state.
        EXPECT_EQ(2, context1->getMinEchos());
        EXPECT_EQ(250, context1->getReplyTimeout());
        EXPECT_EQ(0, context1->getEchosSent());
        EXPECT_EQ(PingContext::EMPTY_TIME(), context1->getLastEchoSentTime());
        EXPECT_LE(test_start_time_, context1->getSendWaitStart());
        EXPECT_EQ(PingContext::EMPTY_TIME(), context1->getNextExpiry());
        EXPECT_LE(test_start_time_, context1->getCreatedTime());
        EXPECT_EQ(lqp1.lease_, context1->getLease());
        EXPECT_EQ(lqp1.query_, context1->getQuery());
        EXPECT_EQ(PingContext::WAITING_TO_SEND, context1->getState());

        // Sleep a bit to make sure there's a difference in context times.
        usleep(5);

        // Make a second lease and query pair
        auto lqp2 = makeLeaseQueryPair(IOAddress("127.0.0.102"), 102);

        // Start a ping for lease2.
        ASSERT_NO_THROW_LOG(mgr_->startPing(lqp2.lease_, lqp2.query_, parking_lot_));

        // Both contexts should be present.
        pings = store->getAll();
        ASSERT_EQ(2, pings->size());

        // Fetch the second context by address.
        PingContextPtr context2;
        ASSERT_NO_THROW_LOG(context2 = store->getContextByAddress(lqp2.lease_->addr_));
        ASSERT_TRUE(context2);

        // Verify the second context's state.
        EXPECT_EQ(2, context2->getMinEchos());
        EXPECT_EQ(250, context2->getReplyTimeout());
        EXPECT_EQ(0, context2->getEchosSent());
        EXPECT_EQ(PingContext::EMPTY_TIME(), context2->getLastEchoSentTime());
        // Its send_wait_start_time_ should be more recent than context1.
        EXPECT_LE(context1->getSendWaitStart(), context2->getSendWaitStart());
        EXPECT_EQ(PingContext::EMPTY_TIME(), context2->getNextExpiry());
        // Its created_time_ should be more recent than context1.
        EXPECT_LE(context1->getCreatedTime(), context2->getCreatedTime());
        EXPECT_EQ(lqp2.lease_, context2->getLease());
        EXPECT_EQ(lqp2.query_, context2->getQuery());
        EXPECT_EQ(PingContext::WAITING_TO_SEND, context2->getState());
    }

    /// @brief Exercises PingCheckMgr::nextToSend().
    void testNextToSend() {
        SKIP_IF(notRoot());

        // Create a paused manager. 3 threads, 2 echos, 250 ms timeout.
        // ST mode should ingore requested thread number.
        createMgr(3, 2, 250, true);

        // Calling nextToSend() should return false.
        IOAddress next("0.0.0.0");
        ASSERT_FALSE(mgr_->nextToSend(next));

        // Now let's start 3 contexts.
        size_t num_targets = 3;
        IOAddress target("127.0.0.1");
        for (auto i = 0; i < num_targets; ++i) {
            auto lqp = makeLeaseQueryPair(IOAddress(target), i+1);

            // Call startPing().
            ASSERT_NO_THROW_LOG(mgr_->startPing(lqp.lease_, lqp.query_, parking_lot_));
            target = IOAddress::increase(target);

            PingContextPtr context = getContext(lqp);
            ASSERT_TRUE(context);

            // Verify the context's initial state is correct.
            EXPECT_EQ(0, context->getEchosSent());
            EXPECT_EQ(PingContext::EMPTY_TIME(), context->getLastEchoSentTime());
            EXPECT_LE(test_start_time_, context->getSendWaitStart());
            EXPECT_EQ(PingContext::EMPTY_TIME(), context->getNextExpiry());
            EXPECT_LE(test_start_time_, context->getCreatedTime());
            EXPECT_EQ(PingContext::WAITING_TO_SEND, context->getState());

            // Sleep a few before we add the next one to ensure ordering by
            // time is consistent.
            usleep(5);
        }

        // Consecutive calls to nextToSend() should return target addresses
        // in the order they were created.
        for (auto const& lqp : lease_query_pairs_) {
            // Next to send should return the next address to send.
            ASSERT_TRUE(mgr_->nextToSend(next));

            // It should match the lease as created.
            ASSERT_EQ(next, lqp.lease_->addr_);

            // Fetch the corresponding context.
            PingContextPtr context = getContext(next);
            ASSERT_TRUE(context);

            // Verify the state has properly moved to SENDING.
            EXPECT_EQ(0, context->getEchosSent());
            EXPECT_EQ(PingContext::EMPTY_TIME(), context->getLastEchoSentTime());
            EXPECT_EQ(PingContext::EMPTY_TIME(), context->getNextExpiry());
            EXPECT_EQ(PingContext::SENDING, context->getState());
        }

        // A final call to nextToSend should return false.
        ASSERT_FALSE(mgr_->nextToSend(next));
    }

    /// @brief Exercises PingCheckMgr::setNextExpiration.
    void testSetNextExpiration() {
        SKIP_IF(notRoot());

        // Create a paused manager. 3 threads, 2 echos, 500 ms timeout.
        // ST mode should ingore requested thread number.
        createMgr(3, 2, 500, true);

        // Should not have an expiration time, timer should not be running.
        ASSERT_EQ(PingContext::EMPTY_TIME(), mgr_->getNextExpiry());
        ASSERT_EQ(mgr_->getExpirationTimerInterval(), 0);

        // Now let's start 3 contexts.
        size_t num_targets = 3;
        IOAddress target("127.0.0.1");
        for (auto i = 0; i < num_targets; ++i) {
            auto lqp = makeLeaseQueryPair(IOAddress(target), i+1);

            // Call startPing().
            ASSERT_NO_THROW_LOG(mgr_->startPing(lqp.lease_, lqp.query_, parking_lot_));
            target = IOAddress::increase(target);
        }

        // Still should not have an expiration time nor running timer.
        ASSERT_EQ(PingContext::EMPTY_TIME(), mgr_->getNextExpiry());
        EXPECT_EQ(mgr_->getExpirationTimerInterval(), 0);

        // Simulate a completed send for the second context.
        PingContextPtr context2;
        context2 = getContext(lease_query_pairs_[1]);
        ASSERT_TRUE(context2);
        context2->beginWaitingForReply(test_start_time_ - milliseconds(50));
        updateContext(context2);

        // Call setNextExpiration().
        ASSERT_NO_THROW_LOG(mgr_->setNextExpiration());

        // Refresh the context.
        context2 = getContext(lease_query_pairs_[1]);

        // Verify the mgr has the same next expiration as the context and
        // that the expiration timer is running. Allow for some fudge in
        // the checks.
        auto original_mgr_expiry = mgr_->getNextExpiry();
        EXPECT_TRUE(fuzzyEqual(original_mgr_expiry, context2->getNextExpiry()));

        auto original_interval = mgr_->getExpirationTimerInterval();
        EXPECT_TRUE(fuzzyEqual(original_interval, 450));

        // Simulate a completed send for the third context.
        PingContextPtr context3;
        context3 = getContext(lease_query_pairs_[2]);
        ASSERT_TRUE(context3);
        context3->beginWaitingForReply();
        updateContext(context3);

        // Call setNextExpiration().
        ASSERT_NO_THROW_LOG(mgr_->setNextExpiration());

        // Refresh the context.
        context3 = getContext(lease_query_pairs_[2]);

        // Context3 should have a later expiration than context2.
        EXPECT_LT(context2->getNextExpiry(), context3->getNextExpiry());

        // Expiration and timer should still match the original values based on
        // the second context.
        EXPECT_TRUE(fuzzyEqual(mgr_->getNextExpiry(), original_mgr_expiry));
        EXPECT_EQ(mgr_->getExpirationTimerInterval(), original_interval);

        // Simulate a completed send for the first context but use a smaller
        // timeout and back date it.
        PingContextPtr context1;
        context1 = getContext(lease_query_pairs_[0]);
        ASSERT_TRUE(context1);
        context1->setReplyTimeout(50);
        context1->beginWaitingForReply(test_start_time_ - milliseconds(1));
        updateContext(context1);

        // Call setNextExpiration().
        ASSERT_NO_THROW_LOG(mgr_->setNextExpiration());

        // Refresh the context.
        context1 = getContext(lease_query_pairs_[0]);

       // Context1 should have a earlier expiration than context2.
        EXPECT_LT(context1->getNextExpiry(), context2->getNextExpiry());
        // Timer interval should be based on context1.
        EXPECT_TRUE(fuzzyEqual(mgr_->getExpirationTimerInterval(), 50, 20))
                    << " interval: " << mgr_->getExpirationTimerInterval();

        // Move all contexts to TARGET_FREE.  This should leave none
        // still waiting.
        context1->setState(PingContext::TARGET_FREE);
        updateContext(context1);
        context2->setState(PingContext::TARGET_FREE);
        updateContext(context2);
        context3->setState(PingContext::TARGET_FREE);
        updateContext(context3);

        // Call setNextExpiration().
        ASSERT_NO_THROW_LOG(mgr_->setNextExpiration());

        // Should not have an expiration time, timer should not be running.
        ASSERT_EQ(PingContext::EMPTY_TIME(), mgr_->getNextExpiry());
        ASSERT_EQ(mgr_->getExpirationTimerInterval(), 0);
    }

    /// @brief Exercises PingCheckMgr::sendCompleted.
    void testSendCompleted() {
        SKIP_IF(notRoot());

        // Create a paused manager. 3 threads, 2 echos, 500 ms timeout.
        // ST mode should ingore requested thread number.
        createMgr(3, 2, 500, true);

        // Start a ping for an address so we have a context.
        IOAddress target("127.0.0.2");
        auto lqp = makeLeaseQueryPair(IOAddress(target), 102);

        // Call startPing().
        ASSERT_NO_THROW_LOG(mgr_->startPing(lqp.lease_, lqp.query_, parking_lot_));

        // Simulate a completed send for the context.
        PingContextPtr context;
        context = getContext(lqp);
        ASSERT_TRUE(context);

        // Make an ECHO REQUEST packet based on context.
        ICMPMsgPtr echo_request = makeEchoRequest(context->getLease()->addr_);

        // Invoke sendCompleted() with fabricated request. Should succeed.
        ASSERT_NO_THROW_LOG(mgr_->sendCompleted(echo_request, false));

        // Refresh the context.
        context = getContext(context->getLease()->addr_);

        EXPECT_EQ(PingContext::WAITING_FOR_REPLY, context->getState());
        EXPECT_EQ(1, context->getEchosSent());
        EXPECT_GE(context->getLastEchoSentTime(), test_start_time_);

        // Verify the mgr has the same next expiration as the context and
        // that the expiration timer is running. Allow for some fudge in
        // the checks.
        EXPECT_GT(context->getNextExpiry(), test_start_time_);
        EXPECT_TRUE(fuzzyEqual(mgr_->getNextExpiry(), context->getNextExpiry()));
        EXPECT_TRUE(fuzzyEqual(mgr_->getExpirationTimerInterval(), 500));

        // Make an ECHO REQUEST packet for an address that has no context.
        echo_request = makeEchoRequest(IOAddress("192.168.0.1"));

        // Invoking sendCompleted() with request for a non-existent address be harmless.
        ASSERT_NO_THROW_LOG(mgr_->sendCompleted(echo_request, false));

        // Invoking sendCompleted() with an invalid message type should be harmless.
        echo_request->setType(ICMPMsg::ECHO_REPLY);
        ASSERT_NO_THROW_LOG(mgr_->sendCompleted(ICMPMsgPtr(), false));

        // Invoking sendCompleted() with an empty message should be harmless.
        echo_request.reset();
        ASSERT_NO_THROW_LOG(mgr_->sendCompleted(ICMPMsgPtr(), false));

        // Verify expiration values should not have not been altered.
        EXPECT_TRUE(fuzzyEqual(mgr_->getNextExpiry(), context->getNextExpiry()));
        EXPECT_TRUE(fuzzyEqual(mgr_->getExpirationTimerInterval(), 500));
    }

    /// @brief Exercises PingCheckMgr::replyReceived() for ECHO REPLYs. Note this
    /// also exercises handleEchoReply().
    void testReplyReceivedForEchoReply() {
        SKIP_IF(notRoot());

        // Create a paused manager. 3 threads, 2 echos, 500 ms timeout.
        // ST mode should ingore requested thread number.
        createMgr(3, 2, 500, true);

        // Install a post reply received callback to stop the test if we're done.
        mgr_->post_reply_received_cb_ =
            [this](const ICMPMsgPtr& /* reply */) {
                MultiThreadingLock lock(*mutex_);
                if (mgr_->parkingLotSize() == 0) {
                    stopTestService();
                    return;
                }
            };

        // Turn off loopback routing.
        mgr_->getChannel()->route_loopback_ = false;

        // Start a ping for an address so we have a context.
        startTargets(1);
        auto lqp = lease_query_pairs_[0];

        // Simulate a completed send for the context.
        PingContextPtr context;
        context = getContext(lqp);
        ASSERT_TRUE(context);

        // Make an ECHO REQUEST packet based on context and invoke sendCompleted().
        ICMPMsgPtr echo_request = makeEchoRequest(context->getLease()->addr_);
        ASSERT_NO_THROW_LOG(mgr_->sendCompleted(echo_request, false));

        // Should still have one parked query.
        EXPECT_EQ(1, mgr_->parkingLotSize());

        // Verify the expiration timer is running.
        EXPECT_TRUE(fuzzyEqual(mgr_->getExpirationTimerInterval(), 500));

        // Make an ECHO REPLY packet based on context and invoke replyReceived().
        ICMPMsgPtr echo_reply = makeEchoReply(context->getLease()->addr_);
        ASSERT_NO_THROW_LOG(mgr_->replyReceived(echo_reply));

        // Verify the expiration timer is no longer running.
        EXPECT_EQ(mgr_->getExpirationTimerInterval(), 0);

        // The context should no longer be in the store.
        EXPECT_FALSE(getContext(lqp));

        // We should have dropped the query from the lot rather than unparking it.
        EXPECT_EQ(mgr_->parkingLotSize(), 0);
        EXPECT_EQ(unparked_, 1);

        // We should have one decline that matches our lease query pair.
        compareLeaseQueryPairs(declines_);

        // Make an ECHO REPLY packet for an address that has no context.
        echo_reply = makeEchoReply(IOAddress("192.168.0.1"));

        // Invoke replyReceived() for a reply with no matching context,
        // it should not throw.
        ASSERT_NO_THROW_LOG(mgr_->PingCheckMgr::replyReceived(echo_reply));

        // Invoke replyReceived() an empty message, it should not throw.
        // (Bypass test implementation for this check).
        echo_reply.reset();
        ASSERT_NO_THROW_LOG(mgr_->PingCheckMgr::replyReceived(echo_reply));
    }

    /// @brief Exercises PingCheckMgr::replyReceived() for UNREACHABLEs. Note this
    /// also exercises handleTargetUnreachable().
    void testReplyReceivedForTargetUnreachable() {
        SKIP_IF(notRoot());

        // Create a paused manager. 3 threads, 2 echos, 500 ms timeout.
        // ST mode should ingore requested thread number.
        createMgr(3, 2, 500, true);

        // Install a post reply received callback to stop the test if we're done.
        mgr_->post_reply_received_cb_ =
            [this](const ICMPMsgPtr& /* reply */) {
                MultiThreadingLock lock(*mutex_);
                if (mgr_->parkingLotSize() == 0) {
                    stopTestService();
                    return;
                }
            };

        // Turn off loopback routing.
        mgr_->getChannel()->route_loopback_ = false;

        // Start a ping for an address so we have a context.
        startTargets(1);
        auto lqp = lease_query_pairs_[0];

        // Simulate a completed send for the context.
        PingContextPtr context;
        context = getContext(lqp);
        ASSERT_TRUE(context);

        // Make an ECHO REQUEST packet based on context and invoke sendCompleted().
        ICMPMsgPtr echo_request = makeEchoRequest(context->getLease()->addr_);
        ASSERT_NO_THROW_LOG(mgr_->sendCompleted(echo_request, false));

        // Should still have one parked query.
        EXPECT_EQ(1, mgr_->parkingLotSize());

        // Verify the expiration timer is running.
        EXPECT_TRUE(fuzzyEqual(mgr_->getExpirationTimerInterval(), 500));

        // Make an ECHO REPLY packet based on context and invoke replyReceived().
        ICMPMsgPtr unreachable = makeUnreachable(context->getLease()->addr_);
        ASSERT_NO_THROW_LOG(mgr_->replyReceived(unreachable));

        // Verify the expiration timer is no longer running.
        EXPECT_EQ(mgr_->getExpirationTimerInterval(), 0);

        // The context should no longer be in the store.
        EXPECT_FALSE(getContext(lqp));

        // We should have unparked the query from the lot.
        EXPECT_EQ(mgr_->parkingLotSize(), 0);
        EXPECT_EQ(unparked_, 1);

        // We should have one free that matches our lease query pair.
        compareLeaseQueryPairs(frees_);

        // Invoke replyReceived() for an unreachable with no matching context,
        // it should not throw.
        unreachable = makeUnreachable(IOAddress("192.168.0.1"));
        ASSERT_NO_THROW_LOG(mgr_->replyReceived(unreachable));
    }

    /// @brief Verifies expiration processing by invoking expirationTimedout().
    /// This also exercises processExpiredSince(), doNextEcho(), finishFree(),
    /// and setNextExpiration().
    void testExpirationProcessing() {
        SKIP_IF(notRoot());

        // Create a paused manager. 3 threads, 1 echos, 250 ms timeout.
        // ST mode should ingore requested thread number.
        createMgr(3, 1, 250, true);

        // Start four ping checks, then stage them so:
        //
        // First context is WAITING_TO_SEND, no expiry.
        // Second context is WAITING_FOR_REPLY, has expired and has
        // exhausted min_echos_.
        // Third context is  WAITING_FOR_REPLY, has expired but has
        // not exhausted min_echos_.
        // Fourth context is WAITING_FOR_REPLY but has not yet expired.
        //
        size_t num_targets = 4;

        // Start the desired number of targets with an unpark callback
        // that increments the unparked count.
        startTargets(num_targets);

        // Now establish the desired state for each context.
        // First context is in WAITING_TO_SEND, no expiry.
        PingContextPtr context1 = getContext(lease_query_pairs_[0]);
        ASSERT_TRUE(context1);
        EXPECT_EQ(context1->getState(), PingContext::WAITING_TO_SEND);

        // Second context setup: expired and has exhausted min_echos_
        PingContextPtr context2 = getContext(lease_query_pairs_[1]);
        ASSERT_TRUE(context2);
        context2->beginWaitingForReply(test_start_time_ - milliseconds(500));
        updateContext(context2);

        // Third context setup: expired but has not exhausted min_echos_
        PingContextPtr context3 = getContext(lease_query_pairs_[2]);
        ASSERT_TRUE(context3);
        context3->setMinEchos(2);
        context3->beginWaitingForReply(test_start_time_ - milliseconds(500));
        updateContext(context3);

        // Fourth context setup: has not yet expired
        PingContextPtr context4 = getContext(lease_query_pairs_[3]);
        ASSERT_TRUE(context4);
        context4->beginWaitingForReply(test_start_time_);
        updateContext(context4);

        // Now invoke expirationTimedout().
        ASSERT_NO_THROW_LOG(mgr_->expirationTimedOut());

        // Verify the contexts are in the expected states.
        // Context1 should still be WAITING_TO_SEND.
        context1 = getContext(lease_query_pairs_[0]);
        ASSERT_TRUE(context1);
        EXPECT_EQ(context1->getState(), PingContext::WAITING_TO_SEND);

        // Context2 should be gone by unparking and its address freed.
        IOAddress address = lease_query_pairs_[1].lease_->addr_;
        context2 = getContext(address);
        ASSERT_FALSE(context2);
        EXPECT_EQ(unparked_, 1);
        ASSERT_EQ(frees_.size(), 1);
        EXPECT_EQ(frees_[0].lease_->addr_, address);

        // Context3 should be in WAITING_TO_SEND.
        context3 = getContext(lease_query_pairs_[2]);
        ASSERT_TRUE(context3);
        EXPECT_EQ(context3->getState(), PingContext::WAITING_TO_SEND);

        // Context4 should still be WAITING_FOR_REPLY.
        context4 = getContext(lease_query_pairs_[3]);
        ASSERT_TRUE(context4);
        EXPECT_EQ(context4->getState(), PingContext::WAITING_FOR_REPLY);

        // Manager's next_expiry_ should be based on context4?
        EXPECT_TRUE(fuzzyEqual(mgr_->getNextExpiry(), context4->getNextExpiry()));
    }

    /// @brief Generates a number of ping checks to local loop back addresses.
    ///
    /// Pings should all result in ECHO_REPLYs that get "declined".  Declined
    /// addresses are added to a list.  Test completion is gated by the parking
    /// lot becoming empty or test times out.
    void testMultiplePingsWithReply() {
        SKIP_IF(notRoot());

        // Create manager with thread-pool size of 3, min_echos 1,
        // reply_timeout 1000 milliseconds.  Larger time out for this test
        // avoids sporadic expirations which leads to unaccounted for UNPARKs.
        // ST mode should ingore requested thread number.
        ASSERT_NO_THROW_LOG(createMgr(3, 1, 1000));
        ASSERT_TRUE(mgr_);

        // Install a post reply received callback to stop the test if we're done.
        int num_targets = 25;
        mgr_->post_reply_received_cb_ =
            [this, num_targets](const ICMPMsgPtr& /* reply */) {
                MultiThreadingLock lock(*mutex_);
                if (unparked_ == num_targets) {
                    stopTestService();
                    return;
                }
            };

        // Start the manager.  This will open the channel.
        ASSERT_NO_THROW_LOG(mgr_->start());
        ASSERT_TRUE(mgr_->isRunning());

        // Start the ping checks.
        startTargets(num_targets);

        // Run the main thread's IOService until we complete or timeout.
        ASSERT_NO_THROW_LOG(runIOService());

        // Stop the thread pool.
        ASSERT_NO_THROW_LOG(mgr_->stop());
        ASSERT_TRUE(mgr_->isStopped());

        // Calling nextToSend() should return false.
        IOAddress next("0.0.0.0");
        ASSERT_FALSE(mgr_->nextToSend(next));

        // We should have as many declines as we have pairs created.
        compareLeaseQueryPairs(declines_);
    }

    /// @brief Generates a large number of ping checks to local loop back addresses.
    ///
    /// A pause is induced approximately halfway through the number of replies
    /// at which point the manager is paused and then resumed.  This is intended
    /// to demonstrate the ability to pause and resume the manager gracefully.
    /// The pings should all result in ECHO_REPLYs that get "declined".  Declined
    /// addresses are added to a list.  Test completion is gated by the parking
    /// lot becoming empty or test times out.
    void testMultiplePingsWithReplyAndPause() {
        SKIP_IF(notRoot());

        // Create manager with thread-pool size of 3, min_echos 1,
        // reply_timeout 1000 milliseconds.  Larger time out for this test
        // avoids sporadic expirations which leads to unaccounted for UNPARKs.
        // ST mode should ingore requested thread number.
        ASSERT_NO_THROW_LOG(createMgr(3, 1, 1000));
        ASSERT_TRUE(mgr_);

        // Generate ping checks to the desired number of targets.
        // Set up the pause callback to pause at half the number of
        // expected replies.
        size_t num_targets = 24;
        size_t reply_cnt = 0;
        size_t pause_at = num_targets / 2;
        bool test_paused = false;

        // Install post reply callback to stop the test thread when we reach
        // the pause count.
        mgr_->post_reply_received_cb_ =
            [this, &reply_cnt, &test_paused, &pause_at](const ICMPMsgPtr& reply) {
                MultiThreadingLock lock(*mutex_);
                if (reply->getType() == ICMPMsg::ECHO_REPLY) {
                    ++reply_cnt;
                    if (pause_at && (reply_cnt >= pause_at)) {
                        test_paused = true;
                        stopTestService();
                        pause_at = 0;
                    }
                }
            };

        // Start the manager.  This will open the channel.
        ASSERT_NO_THROW_LOG(mgr_->start());
        ASSERT_TRUE(mgr_->isRunning());
        ASSERT_NO_THROW_LOG(mgr_->pause());

        // Start 1/2 desired number of ping checks.
        startTargets(num_targets / 2);

        // Run the main thread's IOService until we pause or timeout.
        ASSERT_NO_THROW_LOG(mgr_->resume());
        ASSERT_TRUE(mgr_->isRunning());
        ASSERT_NO_THROW_LOG(runIOService());

        // Manager should still be running.  Pause it.
        ASSERT_TRUE(mgr_->isRunning());
        if (mgr_->getThreadPool()) {
            ASSERT_NO_THROW_LOG(mgr_->pause());
            ASSERT_TRUE(mgr_->isPaused());
        }

        // Verify that the pause callback is why we stopped, that we
        // received at least as many as we should have before pause
        // and that we have more work to do.  The test is a range as
        // pausing does not happen exactly at the same point from test
        // run to test run.
        ASSERT_TRUE(test_paused);
        ASSERT_TRUE((reply_cnt >= pause_at) && (reply_cnt < num_targets))
                << "reply_cnt " << reply_cnt
                << ", pause_at  " << pause_at
                << ", num_targets " << num_targets;

        mgr_->post_reply_received_cb_ =
            [this, num_targets](const ICMPMsgPtr& /* reply */) {
                MultiThreadingLock lock(*mutex_);
                if (unparked_ == num_targets) {
                    stopTestService();
                    return;
                }
            };

        // Start second batch of targets.
        startTargets(num_targets / 2, IOAddress("127.0.0.15"));

        ASSERT_NO_THROW_LOG(mgr_->resume());
        ASSERT_TRUE(mgr_->isRunning());

        // Restart the main thread's IOService until we complete or timeout.
        ASSERT_NO_THROW_LOG(runIOService());

        ASSERT_NO_THROW_LOG(mgr_->stop());
        ASSERT_TRUE(mgr_->isStopped());

        // Calling nextToSend() should return false.
        IOAddress next("0.0.0.0");
        ASSERT_FALSE(mgr_->nextToSend(next));

        // We should have as many declines as we have pairs created.
        compareLeaseQueryPairs(declines_);
    }

    /// @brief Verifies that a recoverable error completion in sendCompleted() results
    /// in the target address being free to use.  In other words, it should have
    /// the same outcome as the receiving a TARGET_UNREACHABLE reply from the OS.
    void testSendCompletedSendFailed() {
        SKIP_IF(notRoot());

        // Create manager with thread-pool size of 3, min_echos 1,
        // reply_timeout 250 milliseconds.
        // ST mode should ingore requested thread number.
        ASSERT_NO_THROW_LOG(createMgr(3, 1, 250));
        ASSERT_TRUE(mgr_);

        // Install a post send completed callback to stop the test if we're done.
        mgr_->post_send_completed_cb_ =
            [this](const ICMPMsgPtr& /* echo */, bool send_failed) {
                MultiThreadingLock lock(*mutex_);
                if (send_failed) {
                    stopTestService();
                }
            };

        // Start the manager.
        ASSERT_NO_THROW_LOG(mgr_->start());

        // Set the test channel to complete the first send with a network_unreachable
        // error. This saves us from trying to determine an address in the test
        // environment that would cause it.
        mgr_->getChannel()->ec_on_write_number_ = 1;
        mgr_->getChannel()->write_error_ec_ = make_error_code(network_unreachable);

        // Start a ping for one target.
        startTargets(1);
        auto lqp = lease_query_pairs_[0];

        // Run the main thread's IOService until we complete or timeout.
        ASSERT_NO_THROW_LOG(runIOService());

        // Verify the expiration timer is no longer running.
        EXPECT_EQ(mgr_->getExpirationTimerInterval(), 0);

        // The context should no longer be in the store.
        EXPECT_FALSE(getContext(lqp));

        // We should have unparked the query from the lot.
        EXPECT_EQ(mgr_->parkingLotSize(), 0);
        EXPECT_EQ(unparked_, 1);

        // We should have one free that matches our lease query pair.
        compareLeaseQueryPairs(frees_);
    }

    /// @brief Exercises shouldPing().
    void testShouldPingTest() {
        SKIP_IF(notRoot());

        // Create manager with thread-pool size of 3, min_echos 1,
        // reply_timeout 250 milliseconds.
        // ST mode should ingore requested thread number.
        ASSERT_NO_THROW_LOG(createMgr(3, 1, 250));
        ASSERT_TRUE(mgr_);

        // Make a default config.
        PingCheckConfigPtr config(new PingCheckConfig());

        // Make a lease query pair.
        auto lqp1 = makeLeaseQueryPair(IOAddress("127.0.0.2"), 111);
        const uint8_t id1[] = { 0x31, 0x32, 0x33, 0x34 };
        ClientIdPtr cid1(new ClientId(id1, sizeof(id1)));
        lqp1.lease_->client_id_ = cid1;

        Lease4Ptr empty_lease;
        CalloutHandle::CalloutNextStep status;

        // Ping checking enabled, no old lease, channel doesn't exist, should return CONTINUE.
        ASSERT_TRUE(config->getEnablePingCheck());
        ASSERT_NO_THROW_LOG(status = mgr_->shouldPing(lqp1.lease_, lqp1.query_, empty_lease, config));
        EXPECT_EQ(status, CalloutHandle::NEXT_STEP_CONTINUE);

        // Start the manager, then pause it.  This lets us start pings without
        // them changing state.
        ASSERT_NO_THROW_LOG(mgr_->start());
        ASSERT_NO_THROW_LOG(mgr_->pause());

        // Ping checking disabled, no old lease, should return CONTINUE.
        config->setEnablePingCheck(false);
        ASSERT_NO_THROW_LOG(status = mgr_->shouldPing(lqp1.lease_, lqp1.query_, empty_lease, config));
        EXPECT_EQ(status, CalloutHandle::NEXT_STEP_CONTINUE);

        // Ping checking enabled, no old lease, should return PARK.
        config->setEnablePingCheck(true);
        ASSERT_NO_THROW_LOG(status = mgr_->shouldPing(lqp1.lease_, lqp1.query_, empty_lease, config));
        EXPECT_EQ(status, CalloutHandle::NEXT_STEP_PARK);

        // Make an old lease based on the first lease.
        time_t now = time(0);
        Lease4Ptr old_lease(new Lease4(*(lqp1.lease_)));

        // Prior lease belonging to the same client with cltt greater than ping-cltt-secs
        // should return PARK.
        old_lease->cltt_ = now - config->getPingClttSecs() * 2;
        ASSERT_NO_THROW_LOG(status = mgr_->shouldPing(lqp1.lease_, lqp1.query_, old_lease, config));
        EXPECT_EQ(status, CalloutHandle::NEXT_STEP_PARK);

        // Prior lease belonging to the same client but with cltt less than ping-cltt-secs
        // should return CONTINUE.
        old_lease->cltt_ = now - config->getPingClttSecs() / 2;
        ASSERT_NO_THROW_LOG(status = mgr_->shouldPing(lqp1.lease_, lqp1.query_, old_lease, config));
        EXPECT_EQ(status, CalloutHandle::NEXT_STEP_CONTINUE);

        // Prior lease belonging to a different client, should return PARK.
        const uint8_t id2[] = { 0x35, 0x36, 0x37, 0x34 };
        old_lease->client_id_.reset(new ClientId(id2, sizeof(id2)));
        ASSERT_NO_THROW_LOG(status = mgr_->shouldPing(lqp1.lease_, lqp1.query_, old_lease, config));
        EXPECT_EQ(status, CalloutHandle::NEXT_STEP_PARK);

        // Now let's start a ping for the lease-query pair.
        ASSERT_NO_THROW_LOG(mgr_->startPing(lqp1.lease_, lqp1.query_, parking_lot_));

        // Make a second lease query pair.  Same address, different client.
        auto lqp2 = makeLeaseQueryPair(IOAddress("127.0.0.2"), 333);
        lqp2.lease_->client_id_ = old_lease->client_id_;

        // Trying to start a ping for an address already being checked should return DROP.
        ASSERT_NO_THROW_LOG(status = mgr_->shouldPing(lqp2.lease_, lqp2.query_, empty_lease, config));
        EXPECT_EQ(status, CalloutHandle::NEXT_STEP_DROP);

        // Stop the mgr.
        ASSERT_NO_THROW(mgr_->stop());

        // Ping checking enabled, no old lease, channel isn't open, should return CONTINUE.
        ASSERT_TRUE(config->getEnablePingCheck());
        ASSERT_NO_THROW_LOG(status = mgr_->shouldPing(lqp1.lease_, lqp1.query_, empty_lease, config));
        EXPECT_EQ(status, CalloutHandle::NEXT_STEP_CONTINUE);
    }

    /// @brief Exercise's getScopedConfig().
    void testGetScopedConfig() {
        CfgMgr::instance().setFamily(AF_INET);

        // Start with empty cache, any subnet that hasn't been seen should get parsed
        // and, if valid, added to the cache.
        CfgMgr& cfg_mgr = CfgMgr::instance();
        CfgSubnets4Ptr subnets = cfg_mgr.getStagingCfg()->getCfgSubnets4();

        // Subnet 1 has no ping-check config. Should return global config.
        ElementPtr user_context = Element::createMap();
        Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.1.0"), 24, 30, 40, 60, 1));
        subnet->setContext(user_context);
        subnets->add(subnet);

        // Subnet 2 has invalid ping-check content. Should return global config.
        std::string invalid_json_cfg =
            R"({
                "ping-check": {
                    "enable-ping-check" : true,
                    "bogus-key-word" : true
                }
            })";

        user_context = Element::fromJSON(invalid_json_cfg);
        subnet.reset(new Subnet4(IOAddress("192.0.2.0"), 24, 30, 40, 60, 2));
        subnet->setContext(user_context);
        subnets->add(subnet);

        // Subnet 3 has valid ping check.  Should return subnet config
        std::string valid_json_cfg =
            R"({
                "ping-check": {
                    "enable-ping-check" : true,
                    "min-ping-requests" : 13
                }
            })";

        user_context = Element::fromJSON(valid_json_cfg);
        subnet.reset(new Subnet4(IOAddress("192.0.3.0"), 24, 30, 40, 60, 3));
        subnet->setContext(user_context);
        subnets->add(subnet);

        // Commit the subnet configuration.
        cfg_mgr.commit();

        // Create manager with thread-pool size of 3, min_echos 2, reply_timeout 250 ms.
        ASSERT_NO_THROW_LOG(createMgr(3, 2, 250));
        ASSERT_TRUE(mgr_);

        Lease4Ptr lease(new Lease4());
        PingCheckConfigPtr config;

        // Should get the global configuration for subnet 1.
        lease->addr_ = IOAddress("192.0.1.1");
        lease->subnet_id_ = 1;
        ASSERT_NO_THROW_LOG(config = mgr_->getScopedConfig(lease));
        ASSERT_TRUE(config);
        ASSERT_EQ(config, mgr_->getGlobalConfig());

        // Should get the global configuration for subnet 2.
        lease->addr_ = IOAddress("192.0.2.1");
        lease->subnet_id_ = 2;
        ASSERT_NO_THROW_LOG(config = mgr_->getScopedConfig(lease));
        ASSERT_TRUE(config);
        ASSERT_EQ(config, mgr_->getGlobalConfig());

        // Should get subnet configuration for subnet 3.
        lease->addr_ = IOAddress("192.0.3.1");
        lease->subnet_id_ = 3;
        ASSERT_NO_THROW_LOG(config = mgr_->getScopedConfig(lease));
        ASSERT_TRUE(config);
        ASSERT_NE(config, mgr_->getGlobalConfig());
        EXPECT_EQ(config->getMinPingRequests(), 13);
    }

    /// @brief Exercises checkSuspended().
    ///
    /// This is intended to verify that ping checking is suspended and resumed based
    /// on the DHCP service state, not to verify every place that checkSuspended()
    /// is called.
    void testCheckSuspended() {
        SKIP_IF(notRoot());

        // Create manager with thread-pool size of 3, min_echos 1,
        // reply_timeout 250 milliseconds.
        ASSERT_NO_THROW_LOG(createMgr(3, 1, 250));
        ASSERT_TRUE(mgr_);

        // Make a default config.
        PingCheckConfigPtr config(new PingCheckConfig());

        // Give the manager a NetworkState instance.
        NetworkStatePtr network_state(new NetworkState());
        mgr_->setNetworkState(network_state);

        // Verify that ping checking is not suspended.
        ASSERT_FALSE(mgr_->checkSuspended());

        // Start the manager, then pause it.  This lets us start pings without
        // them changing state.
        ASSERT_NO_THROW_LOG(mgr_->start());
        ASSERT_NO_THROW_LOG(mgr_->pause());

        // Verfify the ping store is empty.
        auto store = mgr_->getStore();
        ASSERT_TRUE(store);
        auto pings = store->getAll();
        ASSERT_EQ(0, pings->size());

        // Make a lease query pair.
        auto lqp1 = makeLeaseQueryPair(IOAddress("127.0.0.2"), 111);
        const uint8_t id1[] = { 0x31, 0x32, 0x33, 0x34 };
        ClientIdPtr cid1(new ClientId(id1, sizeof(id1)));
        lqp1.lease_->client_id_ = cid1;

        // Now let's try to start a ping for the lease-query pair.  It should work.
        ASSERT_NO_THROW_LOG(mgr_->startPing(lqp1.lease_, lqp1.query_, parking_lot_));

        // Verify we have an entry in the store.
        pings = store->getAll();
        ASSERT_EQ(1, pings->size());

        // Disable the DHCP service.
        network_state->disableService(NetworkState::USER_COMMAND);

        // Make a second lease query pair.  Different address, different client.
        auto lqp2 = makeLeaseQueryPair(IOAddress("127.0.0.3"), 333);
        const uint8_t id2[] = { 0x31, 0x32, 0x33, 0x35 };
        ClientIdPtr cid2(new ClientId(id1, sizeof(id2)));
        lqp2.lease_->client_id_ = cid2;

        // Try to start a ping. We should not be able to do it.
        ASSERT_THROW_MSG(mgr_->startPing(lqp2.lease_, lqp2.query_, parking_lot_),
                         InvalidOperation,
                         "PingCheckMgr::startPing() - DHCP service is suspended!");

        // Store should be empty, having been flushed by suspension detection.
        pings = store->getAll();
        ASSERT_EQ(0, pings->size());

        // Ping checking should report as suspended.
        ASSERT_TRUE(mgr_->checkSuspended());

        // Re-enable the DHCP service.
        network_state->enableService(NetworkState::USER_COMMAND);

        // Suspension checking should lift the suspension and we should once again
        // be able to start a new ping check.
        ASSERT_NO_THROW_LOG(mgr_->startPing(lqp2.lease_, lqp2.query_, parking_lot_));

        // Store should have one check in it.
        pings = store->getAll();
        ASSERT_EQ(1, pings->size());

        // Ping checking should report as not suspended.
        ASSERT_FALSE(mgr_->checkSuspended());
    }

    /// @brief Manager instance.
    TestablePingCheckMgrPtr mgr_;

    /// @brief List of lease/query pairs used during the test, in the order
    /// they were created.
    LeaseQueryPairs lease_query_pairs_;

    /// @brief The mutex used to protect internal state.
    const boost::scoped_ptr<std::mutex> mutex_;

    /// @brief Marks the start time of a test.
    TimeStamp test_start_time_;

    /// @brief Parking lot where the associated query is parked.
    /// If empty parking is not being employed.
    ParkingLotHandlePtr parking_lot_;

    /// @brief Number of queries unparked during a test.
    size_t unparked_;

    /// @brief List of leases that were found to be in-use during a test.
    LeaseQueryPairs declines_;

    /// @brief List of leases that were found to be free to use during a test.
    LeaseQueryPairs frees_;
};

TEST_F(PingCheckMgrTest, operationalBasicsST) {
    testOperationalBasics(0);
}

TEST_F(PingCheckMgrTest, operationalBasicsMT) {
    MultiThreadingTest mt;
    testOperationalBasics(3);
}

TEST_F(PingCheckMgrTest, startPingST) {
    testStartPing();
}

TEST_F(PingCheckMgrTest, startPingMT) {
    MultiThreadingTest mt;
    testStartPing();
}

TEST_F(PingCheckMgrTest, nextToSendST) {
    testNextToSend();
}

TEST_F(PingCheckMgrTest, nextToSendMT) {
    MultiThreadingTest mt;
    testNextToSend();
}

TEST_F(PingCheckMgrTest, setNextExpirationST) {
    testSetNextExpiration();
}

TEST_F(PingCheckMgrTest, setNextExpirationMT) {
    MultiThreadingTest mt;
    testSetNextExpiration();
}

TEST_F(PingCheckMgrTest, sendCompletedST) {
    testSendCompleted();
}

TEST_F(PingCheckMgrTest, sendCompletedMT) {
    MultiThreadingTest mt;
    testSendCompleted();
}

TEST_F(PingCheckMgrTest, replyReceivedForEchoReplyST) {
    testReplyReceivedForEchoReply();
}

TEST_F(PingCheckMgrTest, replyReceivedForEchoReplyMT) {
    MultiThreadingTest mt;
    testReplyReceivedForEchoReply();
}

TEST_F(PingCheckMgrTest, replyReceivedForTargetUnreachableST) {
    testReplyReceivedForTargetUnreachable();
}

TEST_F(PingCheckMgrTest, replyReceivedForTargetUnreachableMT) {
    MultiThreadingTest mt;
    testReplyReceivedForTargetUnreachable();
}

TEST_F(PingCheckMgrTest, expirationProcessingST) {
    testExpirationProcessing();
}

TEST_F(PingCheckMgrTest, expirationProcessingMT) {
    MultiThreadingTest mt;
    testExpirationProcessing();
}

TEST_F(PingCheckMgrTest, multiplePingsWithReplyST) {
    testMultiplePingsWithReply();
}

TEST_F(PingCheckMgrTest, multiplePingsWithReplyMT) {
    MultiThreadingTest mt;
    testMultiplePingsWithReply();
}

TEST_F(PingCheckMgrTest, multiplePingsWithReplyAndPauseST) {
    testMultiplePingsWithReplyAndPause();
}

TEST_F(PingCheckMgrTest, multiplePingsWithReplyAndPauseMT) {
    MultiThreadingTest mt;
    testMultiplePingsWithReplyAndPause();
}

TEST_F(PingCheckMgrTest, sendCompletedSendFailedST) {
    testSendCompletedSendFailed();
}

TEST_F(PingCheckMgrTest, sendCompletedSendFailedMT) {
    MultiThreadingTest mt;
    testSendCompletedSendFailed();
}

TEST_F(PingCheckMgrTest, shouldPingST) {
    testShouldPingTest();
}

TEST_F(PingCheckMgrTest, shouldPingMT) {
    MultiThreadingTest mt;
    testShouldPingTest();
}

TEST_F(PingCheckMgrTest, getScopedConfigST) {
    testGetScopedConfig();
}

TEST_F(PingCheckMgrTest, getScopedConfigMT) {
    MultiThreadingTest mt;
    testGetScopedConfig();
}

TEST_F(PingCheckMgrTest, checkSuspendedST) {
    testCheckSuspended();
}

TEST_F(PingCheckMgrTest, checkSuspendedMT) {
    MultiThreadingTest mt;
    testCheckSuspended();
}

} // end of anonymous namespace