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
// Copyright (C) 2013-2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <hooks/callout_handle.h>
#include <hooks/callout_manager.h>
#include <hooks/library_manager.h>
#include <hooks/server_hooks.h>

#include <hooks/tests/common_test_class.h>
#include <hooks/tests/marker_file.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <hooks/tests/test_libraries.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <log/message_dictionary.h>
#include <log/message_initializer.h>

#include <util/multi_threading_mgr.h>

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

#include <algorithm><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

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


using namespace isc;
using namespace isc::hooks;
using namespace isc::log;
using namespace isc::util;
using namespace std;

namespace {

/// @brief Library manager test class

class LibraryManagerTest : public ::testing::Test,
                           public HooksCommonTestClass {
public:
    /// @brief Constructor
    ///
    /// Initializes the CalloutManager object used in the tests.  It sets it
    /// up with the hooks initialized in the HooksCommonTestClass object and
    /// with four libraries.
    LibraryManagerTest() {
        callout_manager_.reset(new CalloutManager(4));

        // Ensure the marker file is not present at the start of a test.
        static_cast<void>(remove(MARKER_FILE));

        // Disable multi-threading.
        MultiThreadingMgr::instance().setMode(false);
    }

    /// @brief Destructor
    ///
    /// Ensures a marker file is removed after each test.
    ~LibraryManagerTest() {
        static_cast<void>(remove(MARKER_FILE));

        // Disable multi-threading.
        MultiThreadingMgr::instance().setMode(false);
    }

    /// @brief Marker file present
    ///
    /// Convenience function to check whether a marker file is present.  It
    /// does this by opening the file.
    ///
    /// @return true if the marker file is present.
    bool markerFilePresent() const {

        // Try to open it.
        std::fstream marker;
        marker.open(MARKER_FILE, std::fstream::in);

        // Check if it is open and close it if so.
        bool exists = marker.is_open();
        if (exists) {
            marker.close();
        }

        return (exists);
    }

    /// @brief Call callouts test
    ///
    /// A wrapper around the method of the same name in the HooksCommonTestClass
    /// object, this passes this class's CalloutManager to that method.
    ///
    /// @param r0...r3, d1..d3 Values and intermediate values expected.  They
    ///        are ordered so that the variables appear in the argument list in
    ///        the order they are used.  See HooksCommonTestClass::execute for
    ///        a full description. (rN is used to indicate an expected result,
    ///        dN is data to be passed to the calculation.)
    void executeCallCallouts(int r0, int d1, int r1, int d2, int r2, int d3,
                             int r3) {
        HooksCommonTestClass::executeCallCallouts(callout_manager_, r0, d1,
                                                  r1, d2, r2, d3, r3);
    }

    /// @brief Call command handlers test.
    ///
    /// A wrapper around the method of the same name in the HooksCommonTestClass
    /// object, this passes this class's CalloutManager to that method.
    ///
    /// @param r1..r2, d1..d2 Values and intermediate values expected.
    void executeCallCommandHandlers(int d1, int r1, int d2, int r2) {
        HooksCommonTestClass::executeCallCommandHandlers(callout_manager_,
                                                         d1, r1, d2, r2);
    }

    /// Callout manager used for the test.
    boost::shared_ptr<CalloutManager> callout_manager_;
};

/// @brief Library manager class
///
/// This is an instance of the LibraryManager class but with the protected
/// methods made public for test purposes.

class PublicLibraryManager : public isc::hooks::LibraryManager {
public:
    /// @brief Constructor
    ///
    /// Stores the library name.  The actual loading is done in loadLibrary().
    ///
    /// @param name Name of the library to load.  This should be an absolute
    ///        path name.
    /// @param index Index of this library.  For all these tests, it will be
    ///        zero, as we are only using one library.
    /// @param manager CalloutManager object
    PublicLibraryManager(const std::string& name, int index,
                         const boost::shared_ptr<CalloutManager>& manager)
        : LibraryManager(name, index, manager)
    {}

    /// Public methods that call protected methods on the superclass.
    using LibraryManager::unloadLibrary;
    using LibraryManager::openLibrary;
    using LibraryManager::closeLibrary;
    using LibraryManager::checkVersion;
    using LibraryManager::checkMultiThreadingCompatible;
    using LibraryManager::registerStandardCallouts;
    using LibraryManager::runLoad;
    using LibraryManager::prepareUnloadLibrary;
};


// Check that LibraryManager constructor requires a not null manager

TEST_F(LibraryManagerTest, NullManager) {<--- syntax error
    boost::shared_ptr<CalloutManager> null_manager;
    EXPECT_THROW(PublicLibraryManager(std::string("foo"), 0, null_manager),
                 NoCalloutManager);
}

// Check that openLibrary() reports an error when it can't find the specified
// library.

TEST_F(LibraryManagerTest, NoLibrary) {
    PublicLibraryManager lib_manager(std::string(NOT_PRESENT_LIBRARY),
                                     0, callout_manager_);
    EXPECT_FALSE(lib_manager.openLibrary());
}

// Check that the openLibrary() and closeLibrary() methods work.

TEST_F(LibraryManagerTest, OpenClose) {
    PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
                                     0, callout_manager_);

    // Open and close the library
    EXPECT_TRUE(lib_manager.openLibrary());
    EXPECT_TRUE(lib_manager.closeLibrary());

    // Check that a second close on an already closed library does not report
    // an error.
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Check that the code handles the case of a library with no version function.

TEST_F(LibraryManagerTest, NoVersion) {
    PublicLibraryManager lib_manager(std::string(NO_VERSION_LIBRARY),
                                     0, callout_manager_);
    // Open should succeed.
    EXPECT_TRUE(lib_manager.openLibrary());

    // Version check should fail.
    EXPECT_FALSE(lib_manager.checkVersion());

    // Tidy up.
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Check that the code handles the case of a library with a version function
// that returns an incorrect version number.

TEST_F(LibraryManagerTest, WrongVersion) {
    PublicLibraryManager lib_manager(std::string(INCORRECT_VERSION_LIBRARY),
                                     0, callout_manager_);
    // Open should succeed.
    EXPECT_TRUE(lib_manager.openLibrary());

    // Version check should fail.
    EXPECT_FALSE(lib_manager.checkVersion());

    // Tidy up.
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Check that the code handles the case of a library where the version function
// throws an exception.

TEST_F(LibraryManagerTest, VersionException) {
    PublicLibraryManager lib_manager(std::string(FRAMEWORK_EXCEPTION_LIBRARY),
                                     0, callout_manager_);
    // Open should succeed.
    EXPECT_TRUE(lib_manager.openLibrary());

    // Version check should fail.
    EXPECT_FALSE(lib_manager.checkVersion());

    // Tidy up.
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Tests that checkVersion() function succeeds in the case of a library with a
// version function that returns the correct version number.

TEST_F(LibraryManagerTest, CorrectVersionReturned) {
    PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
                                     0, callout_manager_);
    // Open should succeed.
    EXPECT_TRUE(lib_manager.openLibrary());

    // Version check should succeed.
    EXPECT_TRUE(lib_manager.checkVersion());

    // Tidy up.
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Checks that the code handles the case of a library with no
// multi_threading_compatible function.

TEST_F(LibraryManagerTest, NoMultiThreadingCompatible) {
    PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
                                     0, callout_manager_);

    // Open should succeed.
    EXPECT_TRUE(lib_manager.openLibrary());

    // Not multi-threading compatible: does not matter without MT.
    EXPECT_TRUE(lib_manager.checkMultiThreadingCompatible(false));

    // Not multi-threading compatible: does matter with MT.
    EXPECT_FALSE(lib_manager.checkMultiThreadingCompatible(true));

    // Tidy up.
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Checks that the code handles the case of a library with a
// multi_threading_compatible function returning 0 (not compatible).

TEST_F(LibraryManagerTest, multiThreadingNotCompatible) {
    PublicLibraryManager lib_manager(std::string(LOAD_ERROR_CALLOUT_LIBRARY),
                                     0, callout_manager_);

    // Open should succeed.
    EXPECT_TRUE(lib_manager.openLibrary());

    // Not multi-threading compatible: does not matter without MT.
    EXPECT_TRUE(lib_manager.checkMultiThreadingCompatible(false));

    // Not multi-threading compatible: does matter with MT.
    EXPECT_FALSE(lib_manager.checkMultiThreadingCompatible(true));

    // Tidy up.
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Checks that the code handles the case of a library with a
// multi_threading_compatible function returning 1 (compatible)

TEST_F(LibraryManagerTest, multiThreadingCompatible) {
    PublicLibraryManager lib_manager(std::string(FULL_CALLOUT_LIBRARY),
                                     0, callout_manager_);

    // Open should succeed.
    EXPECT_TRUE(lib_manager.openLibrary());

    // Multi-threading compatible: does not matter without MT.
    EXPECT_TRUE(lib_manager.checkMultiThreadingCompatible(false));

    // Multi-threading compatible: does matter with MT.
    EXPECT_TRUE(lib_manager.checkMultiThreadingCompatible(true));

    // Tidy up.
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Checks that the code handles the case of a library with a
// multi_threading_compatible function returning 1 (compatible)

TEST_F(LibraryManagerTest, multiThreadingCompatibleException) {
    PublicLibraryManager lib_manager(std::string(FRAMEWORK_EXCEPTION_LIBRARY),
                                     0, callout_manager_);

    // Open should succeed.
    EXPECT_TRUE(lib_manager.openLibrary());

    // Throw exception: does not matter without MT.
    EXPECT_TRUE(lib_manager.checkMultiThreadingCompatible(false));

    // Throw exception: does matter with MT.
    EXPECT_FALSE(lib_manager.checkMultiThreadingCompatible(true));

    // Tidy up.
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Checks the registration of standard callouts.

TEST_F(LibraryManagerTest, RegisterStandardCallouts) {

    // Load the only library, specifying the index of 0 as it's the only
    // library.  This should load all callouts.
    PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
                                     0, callout_manager_);
    EXPECT_TRUE(lib_manager.openLibrary());

    // Check the version of the library.
    EXPECT_TRUE(lib_manager.checkVersion());

    // Load the standard callouts
    EXPECT_NO_THROW(lib_manager.registerStandardCallouts());

    // Now execute the callouts in the order expected.  The library performs
    // the calculation:
    //
    // r3 = (10 + d1) * d2 - d3
    executeCallCallouts(10, 5, 15, 7, 105, 17, 88);

    // Tidy up
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Test that the "load" function is called correctly.

TEST_F(LibraryManagerTest, CheckLoadCalled) {

    // Load the only library, specifying the index of 0 as it's the only
    // library.  This should load all callouts.
    PublicLibraryManager lib_manager(std::string(LOAD_CALLOUT_LIBRARY),
                                     0, callout_manager_);
    EXPECT_TRUE(lib_manager.openLibrary());

    // Check the version of the library.
    EXPECT_TRUE(lib_manager.checkVersion());

    // Load the standard callouts
    EXPECT_NO_THROW(lib_manager.registerStandardCallouts());

    // Check that only context_create and hookpt_one have callouts registered.
    EXPECT_TRUE(callout_manager_->calloutsPresent(
                ServerHooks::CONTEXT_CREATE));
    EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_));
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_));
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_));
    EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-one"));
    EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-two"));
    EXPECT_FALSE(callout_manager_->calloutsPresent(
                 ServerHooks::CONTEXT_DESTROY));

    // Call the runLoad() method to run the load() function.
    EXPECT_TRUE(lib_manager.runLoad());
    EXPECT_TRUE(callout_manager_->calloutsPresent(
                ServerHooks::CONTEXT_CREATE));
    EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_));
    EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_two_index_));
    EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_three_index_));
    EXPECT_TRUE(callout_manager_->commandHandlersPresent("command-one"));
    EXPECT_TRUE(callout_manager_->commandHandlersPresent("command-two"));
    EXPECT_FALSE(callout_manager_->calloutsPresent(
                 ServerHooks::CONTEXT_DESTROY));

    // Now execute the callouts in the order expected.  The library performs
    // the calculation:
    //
    // r3 = (5 * d1 + d2) * d3
    executeCallCallouts(5, 5, 25, 7, 32, 10, 320);

    // Execute command handlers for 'command-one' and 'command-two'.
    //
    // r2 = d1 * d2 * 10;
    executeCallCommandHandlers(5, 5, 7, 350);

    // Tidy up
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Check handling of a "load" function that throws an exception

TEST_F(LibraryManagerTest, CheckLoadException) {

    // Load the only library, specifying the index of 0 as it's the only
    // library.  This should load all callouts.
    PublicLibraryManager lib_manager(std::string(FRAMEWORK_EXCEPTION_LIBRARY),
                                     0, callout_manager_);
    EXPECT_TRUE(lib_manager.openLibrary());

    // Running the load function should fail.
    EXPECT_FALSE(lib_manager.runLoad());

    // Tidy up
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Check handling of a "load" function that returns an error.

TEST_F(LibraryManagerTest, CheckLoadError) {

    // Load the only library, specifying the index of 0 as it's the only
    // library.  This should load all callouts.
    PublicLibraryManager lib_manager(std::string(LOAD_ERROR_CALLOUT_LIBRARY),
                                     0, callout_manager_);
    EXPECT_TRUE(lib_manager.openLibrary());

    // Check that we catch a load error
    EXPECT_FALSE(lib_manager.runLoad());

    // Tidy up
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// No unload function

TEST_F(LibraryManagerTest, CheckNoUnload) {

    // Load the only library, specifying the index of 0 as it's the only
    // library.  This should load all callouts.
    PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
                                     0, callout_manager_);
    EXPECT_TRUE(lib_manager.openLibrary());

    // Check that no unload function returns true.
    EXPECT_TRUE(lib_manager.prepareUnloadLibrary());

    // Tidy up
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Unload function returns an error

TEST_F(LibraryManagerTest, CheckUnloadError) {

    // Load the only library, specifying the index of 0 as it's the only
    // library.  This should load all callouts.
    PublicLibraryManager lib_manager(std::string(LOAD_ERROR_CALLOUT_LIBRARY),
                                     0, callout_manager_);
    EXPECT_TRUE(lib_manager.openLibrary());

    // Check that unload function returning an error returns false.
    EXPECT_FALSE(lib_manager.prepareUnloadLibrary());

    // Tidy up
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Unload function throws an exception.

TEST_F(LibraryManagerTest, CheckUnloadException) {

    // Load the only library, specifying the index of 0 as it's the only
    // library.  This should load all callouts.
    PublicLibraryManager lib_manager(std::string(FRAMEWORK_EXCEPTION_LIBRARY),
                                     0, callout_manager_);
    EXPECT_TRUE(lib_manager.openLibrary());

    // Check that we detect that the unload function throws an exception.
    EXPECT_FALSE(lib_manager.prepareUnloadLibrary());

    // Tidy up
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Check that the case of the library's unload() function returning a
// success is handled correctly.

TEST_F(LibraryManagerTest, CheckUnload) {

    // Load the only library, specifying the index of 0 as it's the only
    // library.  This should load all callouts.
    PublicLibraryManager lib_manager(std::string(UNLOAD_CALLOUT_LIBRARY),
                                     0, callout_manager_);
    EXPECT_TRUE(lib_manager.openLibrary());


    // Check that the marker file is not present (at least that the file
    // open fails).
    EXPECT_FALSE(markerFilePresent());

    // Check that unload function runs and returns a success
    EXPECT_TRUE(lib_manager.prepareUnloadLibrary());

    // Check that the marker file was created.
    EXPECT_TRUE(markerFilePresent());

    // Tidy up
    EXPECT_TRUE(lib_manager.closeLibrary());
}

// Test the operation of unloadLibrary().  We load a library with a set
// of callouts then unload it.  We need to check that the callouts have been
// removed.  We'll also check that the library's unload() function was called
// as well.

TEST_F(LibraryManagerTest, LibUnload) {

    // Load the only library, specifying the index of 0 as it's the only
    // library.  This should load all callouts.
    PublicLibraryManager lib_manager(std::string(LOAD_CALLOUT_LIBRARY),
                                     0, callout_manager_);
    EXPECT_TRUE(lib_manager.openLibrary());

    // Check the version of the library.
    EXPECT_TRUE(lib_manager.checkVersion());

    // No callouts should be registered at the moment.
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_one_index_));
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_));
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_));
    EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-one"));
    EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-two"));

    // Load the single standard callout and check it is registered correctly.
    EXPECT_NO_THROW(lib_manager.registerStandardCallouts());
    EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_));
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_));
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_));
    EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-one"));
    EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-two"));

    // Call the load function to load the other callouts.
    EXPECT_TRUE(lib_manager.runLoad());
    EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_one_index_));
    EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_two_index_));
    EXPECT_TRUE(callout_manager_->calloutsPresent(hookpt_three_index_));
    EXPECT_TRUE(callout_manager_->commandHandlersPresent("command-one"));
    EXPECT_TRUE(callout_manager_->commandHandlersPresent("command-two"));

    // Unload the library and check that the callouts have been removed from
    // the CalloutManager.
    lib_manager.unloadLibrary();
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_one_index_));
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_));
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_));
    EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-one"));
    EXPECT_FALSE(callout_manager_->commandHandlersPresent("command-two"));
}

// Now come the loadLibrary() tests that make use of all the methods tested
// above.  These tests are really to make sure that the methods have been
// tied together correctly.

// First test the basic error cases - no library, no version function, version
// function returning an error.

TEST_F(LibraryManagerTest, LoadLibraryNoLibrary) {
    LibraryManager lib_manager(std::string(NOT_PRESENT_LIBRARY), 0,
                                           callout_manager_);
    EXPECT_FALSE(lib_manager.loadLibrary());
}

// Check that the code handles the case of a library with no version function.

TEST_F(LibraryManagerTest, LoadLibraryNoVersion) {
    LibraryManager lib_manager(std::string(NO_VERSION_LIBRARY), 0,
                                           callout_manager_);
    EXPECT_FALSE(lib_manager.loadLibrary());
}

// Check that the code handles the case of a library with a version function
// that returns an incorrect version number.

TEST_F(LibraryManagerTest, LoadLibraryWrongVersion) {
    LibraryManager lib_manager(std::string(INCORRECT_VERSION_LIBRARY), 0,
                                           callout_manager_);
    EXPECT_FALSE(lib_manager.loadLibrary());
}

// Check that the full loadLibrary call works.

TEST_F(LibraryManagerTest, LoadLibrary) {
    PublicLibraryManager lib_manager(std::string(FULL_CALLOUT_LIBRARY), 0,
                                     callout_manager_);
    EXPECT_TRUE(lib_manager.loadLibrary());

    // Now execute the callouts in the order expected.  The library performs
    // the calculation:
    //
    // r3 = (7 * d1 - d2) * d3
    executeCallCallouts(7, 5, 35, 9, 26, 3, 78);

    EXPECT_TRUE(lib_manager.unloadLibrary());

    // Check that the callouts have been removed from the callout manager.
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_one_index_));
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_two_index_));
    EXPECT_FALSE(callout_manager_->calloutsPresent(hookpt_three_index_));
}

// Now test for multiple libraries.  We'll load the full callout library
// first, then load some of the libraries with missing framework functions.
// This will check that when searching for framework functions, only the
// specified library is checked, not other loaded libraries. We will
// load a second library with suitable callouts and check that the callouts
// are added correctly. Finally, we'll unload one of the libraries and
// check that only the callouts belonging to that library were removed.

TEST_F(LibraryManagerTest, LoadMultipleLibraries) {
    // Load a library with all framework functions.
    PublicLibraryManager lib_manager_1(std::string(FULL_CALLOUT_LIBRARY),
                                       0, callout_manager_);
    EXPECT_TRUE(lib_manager_1.loadLibrary());

    // Attempt to load a library with no version() function.  We should detect
    // this and not end up calling the function from the already loaded
    // library.
    PublicLibraryManager lib_manager_2(std::string(NO_VERSION_LIBRARY),
                                       1, callout_manager_);
    EXPECT_FALSE(lib_manager_2.loadLibrary());

    // Attempt to load the library with an incorrect version.  This should
    // be detected.
    PublicLibraryManager lib_manager_3(std::string(INCORRECT_VERSION_LIBRARY),
                                       1, callout_manager_);
    EXPECT_FALSE(lib_manager_3.loadLibrary());

    // Load the basic callout library.  This only has standard callouts so,
    // if the first library's load() function gets called, some callouts
    // will be registered twice and lead to incorrect results.
    PublicLibraryManager lib_manager_4(std::string(BASIC_CALLOUT_LIBRARY),
                                       1, callout_manager_);
    EXPECT_TRUE(lib_manager_4.loadLibrary());

    // Execute the callouts.  The first library implements the calculation.
    //
    // r3 = (7 * d1 - d2) * d3
    //
    // The last-loaded library implements the calculation
    //
    // r3 = (10 + d1) * d2 - d3
    //
    // Putting the processing for each library together in the appropriate
    // order, we get:
    //
    // r3 = ((10 * d1 + d1) - d2) * d2 * d3 - d3
    executeCallCallouts(10, 3, 33, 2, 62, 3, 183);

    // All done, so unload the first library.
    EXPECT_TRUE(lib_manager_1.unloadLibrary());

    // Now execute the callouts again and check that the results are as
    // expected for the new calculation.
    executeCallCallouts(10, 5, 15, 7, 105, 17, 88);

    // ... and tidy up.
    EXPECT_TRUE(lib_manager_4.unloadLibrary());
}

// Check that libraries can be validated.

TEST_F(LibraryManagerTest, validateLibraries) {
    EXPECT_TRUE(LibraryManager::validateLibrary(BASIC_CALLOUT_LIBRARY));
    EXPECT_TRUE(LibraryManager::validateLibrary(FULL_CALLOUT_LIBRARY));
    EXPECT_FALSE(LibraryManager::validateLibrary(FRAMEWORK_EXCEPTION_LIBRARY));
    EXPECT_FALSE(LibraryManager::validateLibrary(INCORRECT_VERSION_LIBRARY));
    EXPECT_TRUE(LibraryManager::validateLibrary(LOAD_CALLOUT_LIBRARY));
    EXPECT_TRUE(LibraryManager::validateLibrary(LOAD_ERROR_CALLOUT_LIBRARY));
    EXPECT_FALSE(LibraryManager::validateLibrary(NOT_PRESENT_LIBRARY));
    EXPECT_FALSE(LibraryManager::validateLibrary(NO_VERSION_LIBRARY));
    EXPECT_TRUE(LibraryManager::validateLibrary(UNLOAD_CALLOUT_LIBRARY));
    EXPECT_TRUE(LibraryManager::validateLibrary(CALLOUT_PARAMS_LIBRARY));

    EXPECT_FALSE(LibraryManager::validateLibrary(BASIC_CALLOUT_LIBRARY, true));
    EXPECT_TRUE(LibraryManager::validateLibrary(FULL_CALLOUT_LIBRARY, true));
    EXPECT_FALSE(LibraryManager::validateLibrary(FRAMEWORK_EXCEPTION_LIBRARY, true));
    EXPECT_FALSE(LibraryManager::validateLibrary(INCORRECT_VERSION_LIBRARY, true));
    EXPECT_FALSE(LibraryManager::validateLibrary(LOAD_CALLOUT_LIBRARY, true));
    EXPECT_FALSE(LibraryManager::validateLibrary(LOAD_ERROR_CALLOUT_LIBRARY, true));
    EXPECT_FALSE(LibraryManager::validateLibrary(NOT_PRESENT_LIBRARY, true));
    EXPECT_FALSE(LibraryManager::validateLibrary(NO_VERSION_LIBRARY, true));
    EXPECT_FALSE(LibraryManager::validateLibrary(UNLOAD_CALLOUT_LIBRARY, true));
    EXPECT_FALSE(LibraryManager::validateLibrary(CALLOUT_PARAMS_LIBRARY, true));
}

// Check that log messages are properly registered and unregistered.

TEST_F(LibraryManagerTest, libraryLoggerSetup) {
    // Load a library with all framework functions.
    PublicLibraryManager lib_manager(std::string(BASIC_CALLOUT_LIBRARY),
                                     0, callout_manager_);
    EXPECT_TRUE(lib_manager.loadLibrary());

    // After loading the library, the global logging dictionary should
    // contain log messages registered for this library.
    const MessageDictionaryPtr& dict = MessageDictionary::globalDictionary();
    EXPECT_EQ("basic callout load %1", dict->getText("BCL_LOAD_START"));
    EXPECT_EQ("basic callout load end", dict->getText("BCL_LOAD_END"));
    // Some of the messages defined by the hook library are duplicates. But,
    // the loadLibrary function should have logged the duplicates and clear
    // the duplicates list. By checking that the list of duplicates is empty
    // we test that the LibraryManager handles the duplicates (logs and
    // clears them).
    EXPECT_TRUE(MessageInitializer::getDuplicates().empty());

    // After unloading the library, the messages should be unregistered.
    EXPECT_TRUE(lib_manager.unloadLibrary());
    // The musl libc does not implement dlclose
#ifndef LIBC_MUSL
    EXPECT_TRUE(dict->getText("BCL_LOAD_START").empty());
    EXPECT_TRUE(dict->getText("BCL_LOAD_END").empty());
#endif
}

} // Anonymous namespace