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
// 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 PingChannel class.

#include <config.h>

#include <ping_channel.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 <asiolink/interval_timer.h>
#include <asiolink/io_service_thread_pool.h>
#include <dhcp/iface_mgr.h>
#include <util/multi_threading_mgr.h>
#include <testutils/multi_threading_utils.h>
#include <testutils/gtest_utils.h>
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <boost/multi_index/indexed_by.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index/member.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index/mem_fun.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index/hashed_index.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index/ordered_index.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index_container.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/multi_index/composite_key.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <queue><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <list><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <thread><--- 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.

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

namespace ph = std::placeholders;

namespace {

/// @brief Tag for index by address.
struct AddressIdSequenceIndexTag { };

/// @brief A multi index container holding pointers ICMPMsgPtr
///
/// The message may be accessed using the following index(es):
/// - using an IPv4 address, id, and sequence number
typedef boost::multi_index_container<
    // It holds pointers to ICMPMsg objects.
    ICMPMsgPtr,
    boost::multi_index::indexed_by<
        // Specification of the first index starts here.
        // This index sorts PingContexts by IPv4 addresses represented as
        // IOAddress objects.
        // Specification of the first index starts here.
        boost::multi_index::ordered_unique<
            boost::multi_index::tag<AddressIdSequenceIndexTag>,
            boost::multi_index::composite_key<
                ICMPMsg,
                // The boolean value specifying if context is waiting for a reply
                boost::multi_index::const_mem_fun<ICMPMsg, const IOAddress&,
                                                  &ICMPMsg::getSource>,
                boost::multi_index::const_mem_fun<ICMPMsg, uint16_t,
                                                  &ICMPMsg::getId>,
                boost::multi_index::const_mem_fun<ICMPMsg, uint16_t,
                                                  &ICMPMsg::getSequence>
            >
        >
    >
> ReplyContainer;

/// @brief Single-threaded test fixture for exercising a PingChannel.
class PingChannelTest : public IOServiceTest {
public:
    /// @brief Constructor
    PingChannelTest() : mutex_(new mutex()), stopped_(false) {
        MultiThreadingMgr::instance().setMode(false);
    };

    /// @brief Destructor
    virtual ~PingChannelTest() {
        stopped_ = true;
        if (channel_) {
            channel_->close();
        }
        if (ios_pool_) {
            ios_pool_->getIOService()->stopAndPoll();
            ios_pool_->stop();
        }
        ios_pool_.reset();
        test_timer_.cancel();
        test_io_service_->stopAndPoll();
        MultiThreadingMgr::instance().setMode(false);
    }

    /// @brief Called prior to test destruction.
    /// Ensure we stop the pool in the even a test failed in an unexpected
    /// manner that left it running. Otherwise we can get false TSAN complaints.
    virtual void TearDown() {
        // Stop the thread pool (if one).
        if (ios_pool_) {
            ios_pool_->stop();
        }
    }

    /// @brief Initializes the IOServiceThreadPool
    ///
    /// @param num_threads number of threads in the pool
    /// @param defer_start enables deferred start of the pool's IOService
    void initThreadPool(size_t num_threads = 1, bool defer_start = false) {
        ios_pool_.reset(new IoServiceThreadPool(IOServicePtr(), num_threads, defer_start));
    };

    /// @brief Callback to invoke to fetch the next ping target.
    ///
    /// Fetches the next entry from the front of the send queue (if one).  Checks for
    /// test completion before returning.
    ///
    /// @param[out] next upon return it will contain the next target address.  Contents are
    /// only meaningful if the function returns true.
    ///
    /// @return True another target address exists, false otherwise.
    virtual bool nextToSend(IOAddress& next) {
        if (stopped_) {
            return (false);
        }
        MultiThreadingLock lock(*mutex_);
        bool use_next = true;
        if (send_queue_.empty()) {
            use_next = false;
        } else {
            next = send_queue_.front();
        }

        stopIfDone();
        return (use_next);
    }

    /// @brief Callback to invoke when an ECHO write has completed.
    ///
    /// Ensures the completed echo matches the front of the send queue and then
    /// pops it from the front of the queue. Checks for test completion before
    /// returning.
    ///
    /// @param echo ICMP echo message that as sent
    virtual void echoSent(ICMPMsgPtr& echo, bool send_failed) {
        if (stopped_) {
            return;
        }
        MultiThreadingLock lock(*mutex_);
        ASSERT_EQ(echo->getDestination(), send_queue_.front()) << "send queue mismatch";
        send_queue_.pop();
        if (!send_failed) {
            echos_sent_.push_back(echo);
        }
        stopIfDone();
    }

    /// @brief Callback to invoke when an ICMP reply has been received.
    ///
    /// Stores the reply if it is an ECHO REPLY message.  We check to the
    /// do avoid storing our outbound ECHO REQUESTs when testing with loop back
    /// address.  Checks for test completion before returning.
    ///
    /// @param reply ICMP message that was received
    virtual void replyReceived(ICMPMsgPtr& reply) {
        if (stopped_) {
            return;
        }
        MultiThreadingLock lock(*mutex_);
        if (reply->getType() == ICMPMsg::ECHO_REPLY) {
            // If loopback routing is enabled, Insert the original destination address
            // as the reply's source address.
            if (channel_->route_loopback_) {
                IOAddress address = channel_->loopback_map_.find(reply->getSequence());
                if (address  != IOAddress::IPV4_ZERO_ADDRESS()) {
                    reply->setSource(address);
                }
            }

            replies_received_.push_back(reply);
            storeReply(reply);
        }

        stopIfDone();
    }

    /// @brief Tests that a channel can send and receive, reliably
    /// in either single or multi-threaded mode.
    ///
    /// The test queues the given number of requests, beginning with
    /// address 127.0.0.1 and incrementing the address through the number
    /// of targets.  It then opens the channel and initiates reading and
    /// and writing, running until the test completes or times out.
    /// It expects to receive a reply for every request.
    ///
    /// @param num_threads number of threads in the thread pool. If 0,
    /// the channel will be single-threaded, sharing the test's IOService,
    /// otherwise the channel will be driven by an IOServiceThreadPool with
    /// the given number of threads.
    /// @param num_targets number of target IP addresses to ping. Must not
    /// be greater than 253.
    /// @param set_error_trigger optional function that sets the error trigger
    /// condition.
    void sendReceiveTest(size_t num_threads, size_t num_targets = 25,
                         const std::function<void()>& set_error_trigger = [](){});

    /// @brief Tests for graceful behavior when a channel encounters a read
    /// or write error, in either single or multi-threaded mode.
    ///
    /// The test runs in two passes. The first pass sends and receives until
    /// the error trigger occurs.  The error should induce a graceful cessation
    /// of operations. After verifying expected state of affairs, the second pass
    /// is begun by re-opening the channel and resuming operations until the test
    /// completes or times out.
    ///
    /// @param set_error_trigger function that sets the error trigger condition
    /// @param num_threads number of threads in the thread pool. If 0,
    /// the channel will be single-threaded, sharing the test's IOService,
    /// otherwise the channel will be driven by an IOServiceThreadPool with
    /// the given number of threads.
    /// @param num_targets number of target IP addresses to ping. Must not
    /// be greater than 253.
    void ioErrorTest(const std::function<void()>& set_error_trigger,
                     size_t num_threads, size_t num_targets = 10);

    /// @brief Adds a reply to reply store.
    ///
    /// Fails if a reply for the same address, id, and sequence number is already
    /// in the store.  Must be used in a thread-safe context.
    ///
    /// @param reply reply to store
    void storeReply(ICMPMsgPtr& reply) {
        auto retpair = replies_map_.insert(reply);
        ASSERT_TRUE(retpair.second)
                    << "failed to insert reply for: " << reply->getSource()
                    << ", id: " << reply->getId() << ", sequence: " << reply->getSequence();
    }

    /// @brief Fetches a reply from the store that matches a given ECHO
    ///
    /// Must be used in a thread-safe context.
    ///
    /// @param echo echo for whom a reply is sought
    ///
    /// @return The matching reply if found, otherwise an empty ICMPMsgPtr.
    ICMPMsgPtr findReply(const ICMPMsgPtr& echo) {
        auto const& index = replies_map_.get<AddressIdSequenceIndexTag>();
        auto key = boost::make_tuple(echo->getDestination(), echo->getId(), echo->getSequence());
        auto iter = index.find(key);
        return (iter == index.end() ? ICMPMsgPtr() : *iter);
    }

    /// @brief Channel instance.
    TestablePingChannelPtr channel_;

    /// @brief IoServiceThreadPool instance
    IoServiceThreadPoolPtr ios_pool_;

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

    /// @brief Queue of IOAddresses for which to send ECHO REQUESTs.
    std::queue<IOAddress> send_queue_;

    /// @brief List of ECHO REQUESTs that have been successfully sent in the order
    /// they were sent.
    std::list<ICMPMsgPtr> echos_sent_;

    /// @brief List of ECHO REPLYs that have been successfully  received in the
    /// order they were received.
    std::list<ICMPMsgPtr> replies_received_;

    /// @brief Map of ECHO REPLYs received, indexed by source IP, id, and sequence number.
    ReplyContainer replies_map_;

    /// @brief Flag which indicates that the manager has been stopped.
    bool stopped_;
};

void
PingChannelTest::sendReceiveTest(size_t num_threads, size_t num_targets /* = 25 */,
                                 const std::function<void()>& set_error_trigger) {
    stopped_ = false;

    // Clear state.
    send_queue_ = {};
    echos_sent_.clear();
    replies_received_.clear();
    replies_map_.clear();

    SKIP_IF(notRoot());

    ASSERT_TRUE(num_targets < 253);
    auto channel_ios = test_io_service_;
    if (num_threads) {
        // Enable MT mode.
        util::MultiThreadingMgr::instance().setMode(true);

        // Initialize the thread pool to num_threads, defer start.
        ASSERT_NO_THROW_LOG(initThreadPool(num_threads, true));
        ASSERT_TRUE(ios_pool_->isStopped());
        channel_ios = ios_pool_->getIOService();
    }

    // Create the channel instance with the appropriate io_service.
    ASSERT_NO_THROW_LOG(channel_.reset(new TestablePingChannel(
        channel_ios,
        std::bind(&PingChannelTest::nextToSend, this, ph::_1),
        std::bind(&PingChannelTest::echoSent, this, ph::_1, ph::_2),
        std::bind(&PingChannelTest::replyReceived, this, ph::_1)
    )));

    // Create the callback to check test completion criteria.
    // It returns true if we have sent out all the echos and received
    // all the replies.
    test_done_cb_ = [this]() {
        return (send_queue_.empty() && (echos_sent_.size() == replies_received_.size()));
    };

    //  Fill the send queue with num_target addresses to ping.
    IOAddress target("127.0.0.1");
    for (auto i = 0; i < num_targets; ++i) {
        send_queue_.push(target);
        target = IOAddress::increase(target);
    }

    (set_error_trigger)();

    // Open the channel.
    ASSERT_NO_THROW_LOG(channel_->open());
    ASSERT_TRUE(channel_->isOpen());

    if (num_threads) {
        ios_pool_->run();
    }

    // Initiate reading and writing.
    ASSERT_NO_THROW_LOG(channel_->startRead());
    ASSERT_NO_THROW_LOG(channel_->startSend());

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

    if (ios_pool_) {
        // Stop the thread pool.
        ASSERT_NO_THROW_LOG(ios_pool_->stop());
        ASSERT_TRUE(ios_pool_->isStopped());
    }

    // Send queue should be empty.
    EXPECT_TRUE(send_queue_.empty());

    // Should have as many replies as echos.
    EXPECT_EQ(echos_sent_.size(), replies_received_.size());

    // Should have a reply for every echo.
    for (auto const& echo : echos_sent_) {
        ICMPMsgPtr reply = findReply(echo);
        EXPECT_TRUE(reply) << "no reply found for:" << echo->getDestination()
                           << ", id:" << echo->getId() << ", sequence: " << echo->getSequence();
    }

    stopped_ = true;
    if (channel_) {
        channel_->close();
    }
    if (ios_pool_) {
        ios_pool_->getIOService()->stopAndPoll();
        ios_pool_->stop();
    }
    ios_pool_.reset();
    test_timer_.cancel();
    test_io_service_->stopAndPoll();
    MultiThreadingMgr::instance().setMode(false);
}

void
PingChannelTest::ioErrorTest(const std::function<void()>& set_error_trigger,
                             size_t num_threads, size_t num_targets) {
    ASSERT_TRUE(num_targets < 253);
    SKIP_IF(notRoot());

    ASSERT_TRUE(replies_received_.empty());

    /// If it's an MT test create the thread pool.
    auto channel_ios = test_io_service_;
    if (num_threads) {
        // Enable MT mode.
        util::MultiThreadingMgr::instance().setMode(true);

        // Initialize the thread pool to num_threads, defer start.
        ASSERT_NO_THROW_LOG(initThreadPool(num_threads, true));
        ASSERT_TRUE(ios_pool_->isStopped());
        channel_ios = ios_pool_->getIOService();
    }

    // Set local shutdown called flag to false.
    bool shutdown_cb_called = false;

    // Create the channel instance with the appropriate io_service.
    ASSERT_NO_THROW_LOG(channel_.reset(new TestablePingChannel(
        channel_ios,
        std::bind(&PingChannelTest::nextToSend, this, ph::_1),
        std::bind(&PingChannelTest::echoSent, this, ph::_1, ph::_2),
        std::bind(&PingChannelTest::replyReceived, this, ph::_1),
        ([this, &shutdown_cb_called]() {
                shutdown_cb_called = true;
                test_io_service_->stop();
            })
        )));

    // Set the test_done_cb_ to always return false (i.e. test is not
    // done).
    test_done_cb_ = []() {
        return (false);
    };

    //  Fill the send queue with target addresses to ping.
    IOAddress target("127.0.0.1");
    for (auto i = 0; i < (num_targets / 2); ++i) {
        send_queue_.push(target);
        target = IOAddress::increase(target);
    }

    // Set the error trigger.
    (set_error_trigger)();

    // FIRST PASS

    // Open the channel.
    ASSERT_NO_THROW_LOG(channel_->open());
    ASSERT_TRUE(channel_->isOpen());

    if (num_threads) {
        ios_pool_->run();
    }

    // Initiate reading and writing.
    ASSERT_NO_THROW_LOG(channel_->startRead());
    ASSERT_NO_THROW_LOG(channel_->startSend());

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

    // Shutdown callback should have been invoked, the channel should be closed,
    // but the pool should still be running.
    ASSERT_TRUE(shutdown_cb_called);
    ASSERT_FALSE(channel_->isOpen());

    if (ios_pool_) {
        ASSERT_TRUE(ios_pool_->isRunning());

        // Pause the thread pool.
        ASSERT_NO_THROW_LOG(ios_pool_->pause());
        ASSERT_TRUE(ios_pool_->isPaused());
    }

    // Save how many echos sent and replies received during the first pass.
    auto first_pass_echo_count = echos_sent_.size();
    auto first_pass_reply_count = replies_received_.size();

    // Should have sent some but not all.
    EXPECT_LE(first_pass_echo_count, num_targets);

    // SECOND PASS

    // Modify the test done callback to check test completion criteria.
    // It returns true if we have sent out all the echos and received
    // all the replies.
    test_done_cb_ = [this, &first_pass_reply_count]() {
        return (send_queue_.empty() && (replies_received_.size() > first_pass_reply_count));
    };

    //  Fill the send queue with target addresses to ping.
    for (auto i = 0; i < (num_targets / 2); ++i) {
        send_queue_.push(target);
        target = IOAddress::increase(target);
    }

    // Resume running the thread pool (if one).
    if (ios_pool_) {
        ASSERT_NO_THROW_LOG(ios_pool_->run());
        ASSERT_TRUE(ios_pool_->isRunning());
    }

    // Resume reopening the channel and restarting IO operations.
    ASSERT_NO_THROW_LOG(channel_->open());
    ASSERT_TRUE(channel_->isOpen());
    ASSERT_NO_THROW_LOG(channel_->startRead());
    ASSERT_NO_THROW_LOG(channel_->startSend());

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

    // Stop the thread pool (if one).
    if (ios_pool_) {
        ASSERT_NO_THROW_LOG(ios_pool_->stop());
        ASSERT_TRUE(ios_pool_->isStopped());
    }

    // Send queue should be empty.
    EXPECT_TRUE(send_queue_.empty());

    // Should have sent as many echos as we queued.
    EXPECT_EQ(echos_sent_.size(), num_targets);

    // Should have more replies than we had, but likely not all.
    EXPECT_GE(replies_received_.size(), first_pass_reply_count);
}

// Verifies PingChannel open and close operations.
TEST_F(PingChannelTest, openCloseST) {<--- syntax error
    SKIP_IF(notRoot());

    // Create the channel instance.
    ASSERT_NO_THROW_LOG(channel_.reset(new TestablePingChannel(
        test_io_service_,
        std::bind(&PingChannelTest::nextToSend, this, ph::_1),
        std::bind(&PingChannelTest::echoSent, this, ph::_1, ph::_2),
        std::bind(&PingChannelTest::replyReceived, this, ph::_1)
    )));

    ASSERT_TRUE(channel_);

    ASSERT_TRUE(channel_->getSingleThreaded());

    // Verify it is not open.
    ASSERT_FALSE(channel_->isOpen());

    EXPECT_FALSE(channel_->getWatchSocket());
    EXPECT_EQ(channel_->getRegisteredWriteFd(), -1);
    EXPECT_EQ(channel_->getRegisteredReadFd(), -1);

    // Verify that invoking close is harmless.
    ASSERT_NO_THROW_LOG(channel_->close());

    // Attempt to open the channel.
    ASSERT_NO_THROW_LOG(channel_->open());

    // PingChannel::open() is synchronous and while it has a callback
    // it should never be invoked.  Run the service to make sure.
    ASSERT_NO_THROW_LOG(runIOService(1000));

    // Verify the channel is open.
    ASSERT_TRUE(channel_->isOpen());

    // Verify the WatchSocket was created and that its fd and that of the
    // PingSocket are both registered with IfaceMgr.
    ASSERT_TRUE(channel_->getWatchSocket());
    int registered_write_fd = channel_->getRegisteredWriteFd();
    EXPECT_EQ(registered_write_fd, channel_->getWatchSocket()->getSelectFd());
    EXPECT_TRUE(IfaceMgr::instance().isExternalSocket(registered_write_fd));
    int registered_read_fd = channel_->getRegisteredReadFd();
    EXPECT_EQ(registered_read_fd, channel_->getPingSocket()->getNative());
    EXPECT_TRUE(IfaceMgr::instance().isExternalSocket(registered_read_fd));

    // A subsequent open should be harmless.
    ASSERT_NO_THROW_LOG(channel_->open());

    // Closing the socket should work.
    ASSERT_NO_THROW_LOG(channel_->close());

    // Verify watch socket is gone, registered fds are reset, and prior
    // registered fds are no longer registered.
    EXPECT_FALSE(channel_->getWatchSocket());
    EXPECT_EQ(channel_->getRegisteredWriteFd(), -1);
    EXPECT_FALSE(IfaceMgr::instance().isExternalSocket(registered_write_fd));
    EXPECT_EQ(channel_->getRegisteredReadFd(), -1);
    EXPECT_FALSE(IfaceMgr::instance().isExternalSocket(registered_read_fd));

    // Verify it is not open.
    ASSERT_FALSE(channel_->isOpen());
}

// Verifies PingChannel open and close operations.
TEST_F(PingChannelTest, openCloseMT) {
    SKIP_IF(notRoot());
    MultiThreadingTest mt;

    // Create the channel instance.
    ASSERT_NO_THROW_LOG(channel_.reset(new TestablePingChannel(
        test_io_service_,
        std::bind(&PingChannelTest::nextToSend, this, ph::_1),
        std::bind(&PingChannelTest::echoSent, this, ph::_1, ph::_2),
        std::bind(&PingChannelTest::replyReceived, this, ph::_1)
    )));

    ASSERT_TRUE(channel_);

    ASSERT_FALSE(channel_->getSingleThreaded());

    // Verify it is not open.
    ASSERT_FALSE(channel_->isOpen());

    // Verify that invoking close is harmless.
    ASSERT_NO_THROW_LOG(channel_->close());

    // Attempt to open the channel.
    ASSERT_NO_THROW_LOG(channel_->open());

    // PingChannel::open() is synchronous and while it has a callback
    // it should never be invoked.  Run the service to make sure.
    ASSERT_NO_THROW_LOG(runIOService(1000));

    // Verify the channel is open.
    ASSERT_TRUE(channel_->isOpen());

    // Verify that single-threaded members are not set.
    EXPECT_FALSE(channel_->getWatchSocket());
    EXPECT_EQ(channel_->getRegisteredWriteFd(), -1);
    EXPECT_EQ(channel_->getRegisteredReadFd(), -1);

    // A subsequent open should be harmless.
    ASSERT_NO_THROW_LOG(channel_->open());

    // Closing the socket should work.
    ASSERT_NO_THROW_LOG(channel_->close());

    // Verify it is not open.
    ASSERT_FALSE(channel_->isOpen());
}

// Verifies that a PingChannel can perpetuate sending requests and receiving
// replies when driven by a single-threaded IOService.
TEST_F(PingChannelTest, sendReceiveST) {
    sendReceiveTest(0);
}

// Verifies that a PingChannel can perpetuate sending requests and receiving
// replies when driven by a multi-threaded IOServiceThreadPool 3 threads
TEST_F(PingChannelTest, sendReceiveMT) {
    // Use a thread pool with 3 threads.
    sendReceiveTest(3);
}

// Verifies that an exception throw from asyncRead triggers graceful channel
// shutdown and that operations can be resumed with a single-threaded channel.
TEST_F(PingChannelTest, readExceptionErrorST) {
    ioErrorTest(
        [this]() {
            channel_->throw_on_read_number_ = 5;
        }, 0);
}

// Verifies that an exception throw from asyncRead triggers graceful channel
// shutdown and that operations can be resumed with a multi-threaded channel.
TEST_F(PingChannelTest, readExceptionErrorMT) {
    // Use a thread pool with 3 threads.
    ioErrorTest(
        [this]() {
            channel_->throw_on_read_number_ = 5;
        }, 3, 20);
}

// Verifies that a fatal error code passed into socketReadCallback triggers graceful channel
// shutdown and that operations can be resumed with a single-threaded channel.
TEST_F(PingChannelTest, readFatalErrorST) {
    ioErrorTest(
        [this]() {
            channel_->ec_on_read_number_ = 3;
            // See boost/asio/error.hpp for error codes
            channel_->read_error_ec_ = make_error_code(fault);
        }, 0);
}

// Verifies that a fatal error code passed into socketReadCallback triggers graceful channel
// shutdown and that operations can be resumed with a single-threaded channel.
TEST_F(PingChannelTest, readFatalErrorMT) {
    ioErrorTest(
        [this]() {
            channel_->ec_on_read_number_ = 3;
            // See boost/asio/error.hpp for error codes
            channel_->read_error_ec_ = make_error_code(fault);
        }, 4);
}

// Verifies that a non-fatal, EWOULDBLOCK error passed into socketReadCallback does
// not disrupt reading for a single-threaded channel.
TEST_F(PingChannelTest, readAgainErrorST) {
    sendReceiveTest(0, 10,
        [this]() {
            channel_->ec_on_read_number_ = 4;
            // See boost/asio/error.hpp for error codes
            channel_->read_error_ec_ = make_error_code(would_block);
        });
}

// Verifies that a non-fatal, EWOULDBLOCK error passed into socketReadCallback does
// not disrupt reading for a multi-threaded channel.
TEST_F(PingChannelTest, readAgainErrorMT) {
    sendReceiveTest(3, 10,
        [this]() {
            channel_->ec_on_read_number_ = 4;
            // See boost/asio/error.hpp for error codes
            channel_->read_error_ec_ = make_error_code(would_block);
        });
}

// Verifies that an exception throw from asyncRead triggers graceful channel
// shutdown and that operations can be resumed with a single-threaded channel.
TEST_F(PingChannelTest, writeExceptionErrorST) {
    ioErrorTest(
        [this]() {
            channel_->throw_on_write_number_ = 5;
        }, 0);
}

// Verifies that an exception throw from asyncRead triggers graceful channel
// shutdown and that operations can be resumed with a multi-threaded channel.
TEST_F(PingChannelTest, writeExceptionErrorMT) {
    // Use a thread pool with 3 threads.
    ioErrorTest(
        [this]() {
            channel_->throw_on_write_number_ = 5;
        }, 3);
}

// Verifies that a fatal error code passed into socketReadCallback triggers graceful channel
// shutdown and that operations can be resumed with a single-threaded channel.
TEST_F(PingChannelTest, writeFatalErrorST) {
    ioErrorTest(
        [this]() {
            channel_->ec_on_write_number_ = 3;
            // See boost/asio/error.hpp for error codes
            channel_->write_error_ec_ = make_error_code(fault);
        }, 0);
}

// Verifies that a fatal error code passed into socketReadCallback triggers graceful channel
// shutdown and that operations can be resumed with a single-threaded channel.
TEST_F(PingChannelTest, writeFatalErrorMT) {
    ioErrorTest(
        [this]() {
            channel_->ec_on_write_number_ = 3;
            // See boost/asio/error.hpp for error codes
            channel_->write_error_ec_ = make_error_code(fault);
        }, 4);
}

// Verifies that a non-fatal, EWOULDBLOCK error passed into socketWriteCallback does
// not disrupt writing for a single-threaded channel.
TEST_F(PingChannelTest, writeAgainErrorST) {
    sendReceiveTest(0, 10,
        [this]() {
            channel_->ec_on_write_number_ = 6;
            // See boost/asio/error.hpp for error codes
            channel_->write_error_ec_ = make_error_code(would_block);
        });
}

// Verifies that a non-fatal, EWOULDBLOCK error passed into socketWriteCallback
// does not disrupt writing for a multi-threaded channel.
TEST_F(PingChannelTest, writeAgainErrorMT) {
    sendReceiveTest(3, 10,
        [this]() {
            channel_->ec_on_write_number_ = 6;
            // See boost/asio/error.hpp for error codes
            channel_->write_error_ec_ = make_error_code(would_block);
        });
}

// Verify the recoverable write errors do not disrupt writing for a
// single-threaded channel.
TEST_F(PingChannelTest, writeSendFailedErrorST) {
    SKIP_IF(notRoot());

    std::list<boost::asio::error::basic_errors> errors = {
        boost::asio::error::network_unreachable,
        boost::asio::error::host_unreachable,
        boost::asio::error::network_down,
        boost::asio::error::no_buffer_space,
        boost::asio::error::access_denied
    };

    for (auto const& error : errors) {
        sendReceiveTest(0, 10,
            [this, error]() {
                channel_->ec_on_write_number_ = 6;
                // See boost/asio/error.hpp for error codes
                channel_->write_error_ec_ = make_error_code(error);
            });

        // Sanity check, we should have sent one less than we targeted.
        EXPECT_EQ(echos_sent_.size(), 9);
    }
}

// Verify the recoverable write errors do not disrupt writing for a
// multi-threaded channel.
TEST_F(PingChannelTest, writeSendFailedErrorMT) {
    SKIP_IF(notRoot());

    std::list<boost::asio::error::basic_errors> errors = {
        boost::asio::error::network_unreachable,
        boost::asio::error::host_unreachable,
        boost::asio::error::network_down,
        boost::asio::error::no_buffer_space,
        boost::asio::error::access_denied
    };

    for (auto const& error : errors) {
        sendReceiveTest(3, 10,
            [this, error]() {
                channel_->ec_on_write_number_ = 6;
                // See boost/asio/error.hpp for error codes
                channel_->write_error_ec_ = make_error_code(error);
            });

        // Sanity check, we should have sent one less than we targeted.
        EXPECT_EQ(echos_sent_.size(), 9);
    }
}

} // end of anonymous namespace