File: test_PluginFactory.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (1429 lines) | stat: -rw-r--r-- 63,855 bytes parent folder | download | duplicates (2)
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
/** @file

  Unit tests for a class that deals with plugin Dynamic Shared Objects (DSO)

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  @section details Details

  Implements code necessary for Reverse Proxy which mostly consists of
  general purpose hostname substitution in URLs.

 */

#define CATCH_CONFIG_MAIN /* include main function */
#include <catch.hpp>      /* catch unit-test framework */
#include <fstream>        /* ofstream */
#include <utime.h>

#include "plugin_testing_common.h"
#include "../PluginFactory.h"
#include "../PluginDso.h"
#include "I_EventSystem.h"
#include "tscore/I_Layout.h"
#include "diags.i"

#define TEST_THREADS 2
struct EventProcessorListener : Catch::TestEventListenerBase {
  using TestEventListenerBase::TestEventListenerBase;

  void
  testRunStarting(Catch::TestRunInfo const &testRunInfo) override
  {
    Layout::create();
    init_diags("", nullptr);
    RecProcessInit(RECM_STAND_ALONE);

    ink_event_system_init(EVENT_SYSTEM_MODULE_PUBLIC_VERSION);
    eventProcessor.start(TEST_THREADS, 1048576);

    EThread *main_thread = new EThread;
    main_thread->set_specific();
  }
};

CATCH_REGISTER_LISTENER(EventProcessorListener);

thread_local PluginThreadContext *pluginThreadContext;

std::error_code ec;
static void *INSTANCE_HANDLER = (void *)789;

/* Mock of PluginFactory just to get consistent UUID to be able to test consistently */
static fs::path tempComponent = fs::path("c71e2bab-90dc-4770-9535-c9304c3de38e");
class PluginFactoryUnitTest : public PluginFactory
{
public:
  PluginFactoryUnitTest(const fs::path &tempComponent)
  {
    _tempComponent      = tempComponent;
    _preventiveCleaning = false;
  }

protected:
  const char *
  getUuid() override
  {
    return _tempComponent.c_str();
  }

  fs::path _tempComponent;
};

PluginDebugObject *
getDebugObject(const PluginDso &plugin)
{
  std::string error; /* ignore the error, return nullptr if symbol not defined */
  void *address = nullptr;
  plugin.getSymbol("getPluginDebugObjectTest", address, error);
  GetPluginDebugObjectFunction *getObject = reinterpret_cast<GetPluginDebugObjectFunction *>(address);
  if (getObject) {
    PluginDebugObject *object = reinterpret_cast<PluginDebugObject *>(getObject());
    return object;
  } else {
    return nullptr;
  }
}

/* The following are paths that are used commonly in the unit-tests */
static fs::path sandboxDir     = getTemporaryDir();
static fs::path runtimeRootDir = sandboxDir / "runtime";
static fs::path runtimeDir     = runtimeRootDir / tempComponent;
static fs::path searchDir      = sandboxDir / "search";
static fs::path pluginBuildDir = fs::current_path() / "unit-tests/.libs";

void
clean()
{
  fs::remove(sandboxDir, ec);
}

static int
getPluginVersion(const PluginDso &plugin)
{
  std::string error;
  void *s = nullptr;
  CHECK(plugin.getSymbol("pluginDsoVersionTest", s, error));
  int (*version)() = reinterpret_cast<int (*)()>(s);
  return version ? version() : -1;
}

// this is a simple class to simulate loading of Plugin Dso as done during loading of global plugins
class GlobalPluginInfo
{
public:
  GlobalPluginInfo() : _dlh(nullptr){};
  ~GlobalPluginInfo(){};

  bool
  loadDso(const fs::path &configPath)
  {
    CHECK(fs::exists(configPath));

    void *handle = dlopen(configPath.c_str(), RTLD_NOW | RTLD_LOCAL);
    if (!handle) {
      return false;
    }

    _dlh = handle;
    return true;
  }

  bool
  getSymbol(const char *symbol, void *&address, std::string &error) const
  {
    /* Clear the errors */
    dlerror();
    error.clear();

    address   = dlsym(_dlh, symbol);
    char *err = dlerror();

    if (nullptr == address && nullptr != err) {
      /* symbol really cannot be found */
      error.assign(err);
      return false;
    }

    return true;
  }

  void *
  dlOpenHandle()
  {
    return _dlh;
  }

  int
  getPluginVersion()
  {
    std::string error;
    void *s = nullptr;
    CHECK(getSymbol("pluginDsoVersionTest", s, error));
    auto version = reinterpret_cast<int (*)()>(s);
    return version ? version() : -1;
  }

private:
  void *_dlh;
};

// copies the .so file (pluginBuildPath) in the target directory (effectivePath)
// as the desired .so filename (configPath) with the desired timestamp (mtime)
static void
setupConfigPathTest(const fs::path &configPath, const fs::path &pluginBuildPath, const fs::path &uuid, fs::path &effectivePath,
                    fs::path &runtimePath, time_t mtime = 0, bool append = false)
{
  std::string error;
  if (!append) {
    clean();
  }

  effectivePath = configPath.is_absolute() ? configPath : searchDir / configPath;
  if (isPluginDynamicReloadEnabled()) {
    runtimePath = runtimeRootDir / uuid / effectivePath.relative_path();
  } else {
    runtimePath = effectivePath;
  }

  /* Create the directory structure and install plugins */
  fs::create_directories(effectivePath.parent_path(), ec);
  fs::copy(pluginBuildPath, effectivePath, ec);
  if (0 != mtime) {
    struct stat sb;
    struct utimbuf new_times;
    stat(effectivePath.c_str(), &sb);
    new_times.actime  = sb.st_atime; /* keep atime unchanged */
    new_times.modtime = mtime;       /* set mtime to current time */
    utime(effectivePath.c_str(), &new_times);
  }

  CHECK(fs::exists(effectivePath));
}

static PluginFactoryUnitTest *
getFactory(const fs::path &uuid)
{
  /* Instantiate and initialize a plugin factory. */
  PluginFactoryUnitTest *factory = new PluginFactoryUnitTest(uuid);
  factory->setRuntimeDir(runtimeRootDir);
  factory->addSearchDir(searchDir);
  return factory;
}

static void
teardownConfigPathTest(PluginFactoryUnitTest *factory)
{
  delete factory;
  clean();
}

static void
validateSuccessfulConfigPathTest(const RemapPluginInst *pluginInst, const std::string &error, const fs::path &effectivePath,
                                 const fs::path &runtimePath)
{
  CHECK(nullptr != pluginInst);
  CHECK("" == error);
  CHECK(effectivePath == pluginInst->_plugin.effectivePath());
  CHECK(runtimePath == pluginInst->_plugin.runtimePath());
}

SCENARIO("loading plugins", "[plugin][core]")
{
  REQUIRE_FALSE(sandboxDir.empty());

  fs::path effectivePath;
  fs::path runtimePath;
  std::string error;
  enablePluginDynamicReload();

  GIVEN("an existing plugin")
  {
    fs::path pluginName = fs::path("plugin_v1.so");
    fs::path buildPath  = pluginBuildDir / pluginName;

    WHEN("config using plugin file name only")
    {
      fs::path configPath = pluginName;
      CHECK(configPath.is_relative()); /* make sure this is relative path - this is what we are testing */

      setupConfigPathTest(configPath, buildPath, tempComponent, effectivePath, runtimePath);
      PluginFactoryUnitTest *factory = getFactory(tempComponent);
      RemapPluginInst *plugin        = factory->getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("expect it to successfully load")
      {
        validateSuccessfulConfigPathTest(plugin, error, effectivePath, runtimePath);
        CHECK(nullptr != PluginDso::loadedPlugins()->findByEffectivePath(effectivePath, isPluginDynamicReloadEnabled()));
      }

      teardownConfigPathTest(factory);
    }

    WHEN("config is using plugin relative filename")
    {
      fs::path configPath = fs::path("subdir") / pluginName;
      CHECK(configPath.is_relative()); /* make sure this is relative path - this is what we are testing */

      setupConfigPathTest(configPath, buildPath, tempComponent, effectivePath, runtimePath);
      PluginFactoryUnitTest *factory = getFactory(tempComponent);
      RemapPluginInst *plugin        = factory->getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("expect it to successfully load")
      {
        validateSuccessfulConfigPathTest(plugin, error, effectivePath, runtimePath);
        CHECK(nullptr != PluginDso::loadedPlugins()->findByEffectivePath(effectivePath, isPluginDynamicReloadEnabled()));
      }

      teardownConfigPathTest(factory);
    }

    WHEN("config is using plugin absolute path - dynamic reload is ENABLED")
    {
      fs::path configPath = searchDir / "subdir" / pluginName;
      CHECK(configPath.is_absolute()); /* make sure this is absolute path - this is what we are testing */
      setupConfigPathTest(configPath, buildPath, tempComponent, effectivePath, runtimePath);
      PluginFactoryUnitTest *factory = getFactory(tempComponent);
      RemapPluginInst *plugin        = factory->getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("expect it to successfully load")
      {
        validateSuccessfulConfigPathTest(plugin, error, effectivePath, runtimePath);
        CHECK(nullptr != PluginDso::loadedPlugins()->findByEffectivePath(effectivePath, isPluginDynamicReloadEnabled()));

        // check Dso at effective path still exists while copy at runtime path doesn't
        CHECK(fs::exists(plugin->_plugin.effectivePath()));
        CHECK(!fs::exists(plugin->_plugin.runtimePath()));
      }

      teardownConfigPathTest(factory);
    }

    WHEN("config is using plugin absolute path - dynamic reload is DISABLED")
    {
      disablePluginDynamicReload();
      fs::path configPath = searchDir / "subdir" / pluginName;
      CHECK(configPath.is_absolute()); /* make sure this is absolute path - this is what we are testing */
      setupConfigPathTest(configPath, buildPath, tempComponent, effectivePath, runtimePath);
      PluginFactoryUnitTest *factory = getFactory(tempComponent);
      RemapPluginInst *plugin        = factory->getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("expect it to successfully load")
      {
        validateSuccessfulConfigPathTest(plugin, error, effectivePath, runtimePath);
        CHECK(nullptr != PluginDso::loadedPlugins()->findByEffectivePath(effectivePath, isPluginDynamicReloadEnabled()));

        // check Dso still exists
        CHECK(plugin->_plugin.effectivePath() == plugin->_plugin.runtimePath());
        CHECK(fs::exists(plugin->_plugin.effectivePath()));
      }

      teardownConfigPathTest(factory);
      enablePluginDynamicReload();
    }

    WHEN("config is using plugin absolute path - dynamic reload is ENABLED but overwritten by optout")
    {
      enablePluginDynamicReload();
      fs::path configPath = searchDir / "subdir" / pluginName;
      CHECK(configPath.is_absolute()); /* make sure this is absolute path - this is what we are testing */

      // We have to force the path to be the effective == runtime
      disablePluginDynamicReload();
      setupConfigPathTest(configPath, buildPath, tempComponent, effectivePath, runtimePath);
      enablePluginDynamicReload();

      PluginFactoryUnitTest *factory = getFactory(tempComponent);
      // make the factory take this plugin as it opt out.
      PluginDso::loadedPlugins()->addPluginPathToDsoOptOutTable(effectivePath.string());

      RemapPluginInst *plugin = factory->getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("expect it to successfully load")
      {
        validateSuccessfulConfigPathTest(plugin, error, effectivePath, runtimePath);
        static const bool disableDynamicReloadByOptOut{false};
        CHECK(nullptr != PluginDso::loadedPlugins()->findByEffectivePath(effectivePath, disableDynamicReloadByOptOut));

        // check Dso still exists
        CHECK(plugin->_plugin.effectivePath() == plugin->_plugin.runtimePath());
        CHECK(fs::exists(plugin->_plugin.effectivePath()));
      }
      // we need to remove this from the list so next tests can work as expected.
      PluginDso::loadedPlugins()->removePluginPathFromDsoOptOutTable(effectivePath.string());
      teardownConfigPathTest(factory);
      enablePluginDynamicReload();
    }

    WHEN("config using nonexisting relative plugin file name")
    {
      fs::path relativeExistingPath = pluginName;
      CHECK(relativeExistingPath.is_relative());
      fs::path relativeNonexistingPath("subdir");
      relativeNonexistingPath /= fs::path("nonexisting_plugin.so");
      CHECK(relativeNonexistingPath.is_relative());

      setupConfigPathTest(relativeExistingPath, buildPath, tempComponent, effectivePath, runtimePath);
      PluginFactoryUnitTest *factory = getFactory(tempComponent);
      RemapPluginInst *plugin = factory->getRemapPlugin(relativeNonexistingPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("expect it to fail with appropriate error message")
      {
        std::string expectedError;
        expectedError.append("failed to find plugin '").append(relativeNonexistingPath.string()).append("'");
        CHECK(nullptr == plugin);
        CHECK(expectedError == error);
      }

      teardownConfigPathTest(factory);
    }

    WHEN("config using nonexisting absolute plugin file name")
    {
      fs::path relativeExistingPath = pluginName;
      CHECK(relativeExistingPath.is_relative());
      fs::path absoluteNonexistingPath = searchDir / "subdir" / "nonexisting_plugin.so";
      CHECK(absoluteNonexistingPath.is_absolute());

      setupConfigPathTest(relativeExistingPath, buildPath, tempComponent, effectivePath, runtimePath);
      PluginFactoryUnitTest *factory = getFactory(tempComponent);
      RemapPluginInst *plugin = factory->getRemapPlugin(absoluteNonexistingPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("expect it to fail with appropriate error message")
      {
        std::string expectedError;
        expectedError.append("failed to find plugin '").append(absoluteNonexistingPath.string()).append("'");
        CHECK(nullptr == plugin);
        CHECK(expectedError == error);
      }

      teardownConfigPathTest(factory);
    }

    WHEN("plugin initialization fails")
    {
      fs::path configPath = fs::path("plugin_init_fail.so");
      fs::path buildPath  = pluginBuildDir / configPath;
      setupConfigPathTest(configPath, buildPath, tempComponent, effectivePath, runtimePath);
      PluginFactoryUnitTest *factory = getFactory(tempComponent);
      RemapPluginInst *plugin        = factory->getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("expect it to unload the plugin dso")
      {
        CHECK(nullptr == plugin);
        CHECK(nullptr == PluginDso::loadedPlugins()->findByEffectivePath(effectivePath, isPluginDynamicReloadEnabled()));
      }

      teardownConfigPathTest(factory);
    }

    WHEN("instance initialization fails")
    {
      fs::path configPath = fs::path("plugin_instinit_fail.so");
      fs::path buildPath  = pluginBuildDir / configPath;
      setupConfigPathTest(configPath, buildPath, tempComponent, effectivePath, runtimePath);
      PluginFactoryUnitTest *factory = getFactory(tempComponent);
      RemapPluginInst *plugin        = factory->getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("expect it to unload the plugin dso")
      {
        CHECK(nullptr == plugin);
        CHECK(nullptr == PluginDso::loadedPlugins()->findByEffectivePath(effectivePath, isPluginDynamicReloadEnabled()));
      }

      teardownConfigPathTest(factory);
    }
  }
}

SCENARIO("multiple search dirs + multiple or no plugins installed", "[plugin][core]")
{
  REQUIRE_FALSE(sandboxDir.empty());
  enablePluginDynamicReload();

  GIVEN("multiple search dirs specified for the plugin search")
  {
    /* Create the directory structure and install plugins */
    fs::path configPath              = fs::path("plugin_v1.so");
    fs::path pluginName              = fs::path("plugin_v1.so");
    fs::path searchDir1              = sandboxDir / "search1";
    fs::path searchDir2              = sandboxDir / "search2";
    fs::path searchDir3              = sandboxDir / "search3";
    std::vector<fs::path> searchDirs = {searchDir1, searchDir2, searchDir3};
    fs::path effectivePath1          = searchDir1 / configPath;
    fs::path effectivePath2          = searchDir2 / configPath;
    fs::path effectivePath3          = searchDir3 / configPath;
    fs::path runtimePath1            = runtimeDir / effectivePath1.relative_path();
    fs::path runtimePath2            = runtimeDir / effectivePath2.relative_path();
    fs::path runtimePath3            = runtimeDir / effectivePath3.relative_path();
    fs::path pluginBuildPath         = fs::current_path() / fs::path("unit-tests/.libs") / pluginName;

    std::string error;

    for (auto searchDir : searchDirs) {
      CHECK(fs::create_directories(searchDir, ec));
      fs::copy(pluginBuildPath, searchDir, ec);
    }
    CHECK(fs::create_directories(runtimeDir, ec));

    /* Instantiate and initialize a plugin DSO instance. */
    PluginFactoryUnitTest factory(tempComponent);
    factory.setRuntimeDir(runtimeRootDir);
    for (auto searchDir : searchDirs) {
      factory.addSearchDir(searchDir);
    }

    CHECK(fs::exists(effectivePath1));
    CHECK(fs::exists(effectivePath2));
    CHECK(fs::exists(effectivePath3));

    WHEN("loading an existing plugin using its absolute path but the plugin is not located in any of the search dirs")
    {
      /* Prepare "unregistered" directory containing a valid plugin but not registered with the factory as a search directory */
      fs::path unregisteredDir = sandboxDir / searchDir / "unregistered";
      CHECK(fs::create_directories(unregisteredDir, ec));
      fs::copy(pluginBuildPath, unregisteredDir, ec);
      fs::path abEffectivePath = unregisteredDir / pluginName;
      fs::path absRuntimePath  = runtimeDir / abEffectivePath.relative_path();
      CHECK(abEffectivePath.is_absolute());
      CHECK(fs::exists(abEffectivePath));

      /* Now use an absolute path containing the unregistered search directory */
      RemapPluginInst *pluginInst = factory.getRemapPlugin(abEffectivePath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("Expect it to successfully load")
      {
        CHECK(nullptr != pluginInst);
        CHECK(error.empty());
        CHECK(abEffectivePath == pluginInst->_plugin.effectivePath());
        CHECK(absRuntimePath == pluginInst->_plugin.runtimePath());
      }
      CHECK(fs::remove(sandboxDir, ec));
    }

    WHEN("a valid plugin is found in the first search path")
    {
      RemapPluginInst *pluginInst = factory.getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("Expect it to successfully load the one found in the first search dir and copy it in the runtime dir")
      {
        CHECK(nullptr != pluginInst);
        CHECK(error.empty());
        CHECK(effectivePath1 == pluginInst->_plugin.effectivePath());
        CHECK(runtimePath1 == pluginInst->_plugin.runtimePath());
      }
      CHECK(fs::remove(sandboxDir, ec));
    }

    WHEN("the first search dir is missing the plugin but the second search has it")
    {
      CHECK(fs::remove(effectivePath1, ec));
      RemapPluginInst *pluginInst = factory.getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("Expect it to successfully load the one found in the second search dir")
      {
        CHECK(nullptr != pluginInst);
        CHECK(error.empty());
        CHECK(effectivePath2 == pluginInst->_plugin.effectivePath());
        CHECK(runtimePath2 == pluginInst->_plugin.runtimePath());
      }
      CHECK(fs::remove(sandboxDir, ec));
    }

    WHEN("the first and second search dir are missing the plugin but the third search has it")
    {
      CHECK(fs::remove(effectivePath1, ec));
      CHECK(fs::remove(effectivePath2, ec));
      RemapPluginInst *pluginInst = factory.getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());

      THEN("Expect it to successfully load the one found in the third search dir")
      {
        CHECK(nullptr != pluginInst);
        CHECK(error.empty());
        CHECK(effectivePath3 == pluginInst->_plugin.effectivePath());
        CHECK(runtimePath3 == pluginInst->_plugin.runtimePath());
      }
      CHECK(fs::remove(sandboxDir, ec));
    }

    WHEN("none of the search dirs contains a valid plugin")
    {
      CHECK(fs::remove(effectivePath1, ec));
      CHECK(fs::remove(effectivePath2, ec));
      CHECK(fs::remove(effectivePath3, ec));

      THEN("expect the plugin load to fail.")
      {
        RemapPluginInst *pluginInst = factory.getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled());
        CHECK(nullptr == pluginInst);
        CHECK(std::string("failed to find plugin '").append(configPath.string()).append("'") == error);
        CHECK_FALSE(fs::exists(runtimePath1));
        CHECK_FALSE(fs::exists(runtimePath2));
        CHECK_FALSE(fs::exists(runtimePath3));
      }
      CHECK(fs::remove(sandboxDir, ec));
    }
  }
}

void
checkTwoLoadedVersionsDifferent(const RemapPluginInst *plugin_v1, const RemapPluginInst *plugin_v2)
{
  void *tsRemapInitSym_v1_t2 = nullptr; /* callback address from DSO v1 at moment t2 */
  void *tsRemapInitSym_v2_t2 = nullptr; /* callback address from DSO v2 at moment t2 */
  std::string error;

  /* Make sure we ended up with different DSO objects and runtime paths are different - new plugin was indeed loaded */
  CHECK(&(plugin_v1->_plugin) != &(plugin_v2->_plugin));
  CHECK(plugin_v1->_plugin.runtimePath() != plugin_v2->_plugin.runtimePath());
  CHECK(plugin_v1->_plugin.dlOpenHandle() != plugin_v2->_plugin.dlOpenHandle());

  /* Make sure what we installed and loaded first was v1 and after the plugin reload we run v2 */
  CHECK(1 == getPluginVersion(plugin_v1->_plugin));
  CHECK(2 == getPluginVersion(plugin_v2->_plugin));

  /* Make sure the symbols we get from the 2 loaded plugins don't yield the same callback function pointer */
  plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);
  plugin_v2->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v2_t2, error);
  CHECK(nullptr != tsRemapInitSym_v1_t2);
  CHECK(nullptr != tsRemapInitSym_v2_t2);
  CHECK(tsRemapInitSym_v1_t2 != tsRemapInitSym_v2_t2);

  // 2 versions can be different only when dynamic reload enabled
  CHECK(isPluginDynamicReloadEnabled());

  // check Dso at effective path still exists while the copy at runtime path doesn't
  CHECK(plugin_v1->_plugin.effectivePath() != plugin_v1->_plugin.runtimePath());
  CHECK(fs::exists(plugin_v1->_plugin.effectivePath()));
  CHECK(!fs::exists(plugin_v1->_plugin.runtimePath()));
  CHECK(plugin_v2->_plugin.effectivePath() != plugin_v2->_plugin.runtimePath());
  CHECK(fs::exists(plugin_v2->_plugin.effectivePath()));
  CHECK(!fs::exists(plugin_v2->_plugin.runtimePath()));
}

void
checkTwoLoadedVersionsSame(RemapPluginInst *plugin_v1, RemapPluginInst *plugin_v2, bool pluginOptOut = false)
{
  void *tsRemapInitSym_v1_t2 = nullptr; /* callback address from DSO v1 at moment t2 */
  void *tsRemapInitSym_v2_t2 = nullptr; /* callback address from DSO v2 at moment t2 */
  std::string error;

  /* Make sure we ended up with the same DSO object and runtime paths should be same - no new plugin was loaded */
  CHECK(&(plugin_v1->_plugin) == &(plugin_v2->_plugin));
  CHECK(plugin_v1->_plugin.runtimePath() == plugin_v2->_plugin.runtimePath());
  CHECK(plugin_v1->_plugin.dlOpenHandle() == plugin_v2->_plugin.dlOpenHandle());

  /* Make sure v2 DSO was NOT really loaded - both instances should return same v1 version */
  CHECK(1 == getPluginVersion(plugin_v1->_plugin));
  CHECK(1 == getPluginVersion(plugin_v2->_plugin));

  /* Make sure the symbols we get from the 2 loaded plugins yield the same callback function pointer */
  plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);
  plugin_v2->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v2_t2, error);
  CHECK(nullptr != tsRemapInitSym_v1_t2);
  CHECK(nullptr != tsRemapInitSym_v2_t2);
  CHECK(tsRemapInitSym_v1_t2 == tsRemapInitSym_v2_t2);

  // 2 versions can be same even when dynamic reload is enabled
  // 2 versions must be same when dynamic reload is disabled or the plugin opt out
  // check presence/absence of Dso files
  if (pluginOptOut || !isPluginDynamicReloadEnabled()) {
    CHECK(plugin_v1->_plugin.effectivePath() == plugin_v1->_plugin.runtimePath());
    CHECK(fs::exists(plugin_v1->_plugin.effectivePath()));
  } else {
    CHECK(plugin_v1->_plugin.effectivePath() != plugin_v1->_plugin.runtimePath());
    CHECK(fs::exists(plugin_v1->_plugin.effectivePath()));
    CHECK(!fs::exists(plugin_v1->_plugin.runtimePath()));
  }
}

std::tuple<RemapPluginInst *, PluginFactoryUnitTest *>
testSetupLoadPlugin(const fs::path &configName, const fs::path &buildPath, const fs::path &uuid, const time_t mtime,
                    fs::path &effectivePath, fs::path &runtimePath)
{
  std::string error;
  setupConfigPathTest(configName, buildPath, uuid, effectivePath, runtimePath, mtime);
  auto factory    = getFactory(uuid);
  auto pluginInst = factory->getRemapPlugin(configName, 0, nullptr, error, isPluginDynamicReloadEnabled());

  return {pluginInst, factory};
}

std::tuple<RemapPluginInst *, PluginFactoryUnitTest *>
testSetupLoadPluginWithOptOut(const fs::path &configName, const fs::path &buildPath, const fs::path &uuid, const time_t mtime,
                              fs::path &effectivePath, fs::path &runtimePath)
{
  std::string error;

  // force paths to be as dynamic disabled which will be the case for a plugin that opt out.
  disablePluginDynamicReload();
  setupConfigPathTest(configName, buildPath, uuid, effectivePath, runtimePath, mtime);
  enablePluginDynamicReload();

  auto factory = getFactory(uuid);

  // Set factory's opt out plugin information.
  PluginDso::loadedPlugins()->addPluginPathToDsoOptOutTable(effectivePath.string());
  auto pluginInst = factory->getRemapPlugin(configName, 0, nullptr, error, isPluginDynamicReloadEnabled());
  // make sure we remove it so there is no evidence of this in the Plugin's list.
  PluginDso::loadedPlugins()->removePluginPathFromDsoOptOutTable(effectivePath.string());
  return {pluginInst, factory};
}

SCENARIO("loading multiple version of the same plugin at the same time", "[plugin][core]")
{
  REQUIRE_FALSE(sandboxDir.empty());
  enablePluginDynamicReload();

  static fs::path uuid_t1    = fs::path("c71e2bab-90dc-4770-9535-c9304c3de381"); /* UUID at moment t1 */
  static fs::path uuid_t2    = fs::path("c71e2bab-90dc-4770-9535-e7304c3ee732"); /* UUID at moment t2 */
  void *tsRemapInitSym_v1_t1 = nullptr;                                          /* callback address from DSO v1 at moment t1 */
  void *tsRemapInitSym_v1_t2 = nullptr;                                          /* callback address from DSO v1 at moment t2 */

  fs::path effectivePath_v1; /* expected effective path for DSO v1 */
  fs::path effectivePath_v2; /* expected effective path for DSO v2 */
  fs::path runtimePath_v1;   /* expected runtime path for DSO v1 */
  fs::path runtimePath_v2;   /* expected runtime path for DSO v2 */

  std::string error;
  std::string error1;
  std::string error2;

  fs::path configName   = fs::path("plugin.so");                     /* use same config name for all following tests */
  fs::path buildPath_v1 = pluginBuildDir / fs::path("plugin_v1.so"); /* DSO v1 */
  fs::path buildPath_v2 = pluginBuildDir / fs::path("plugin_v2.so"); /* DSO v1 */

  GIVEN("two different versions v1 and v2 of same plugin with different time stamps - dynamic plugin reload ENABLED")
  {
    WHEN("(1) loading v1, (2) overwriting with v2 and then (3) reloading by using the same plugin name, "
         "(*) v1 and v2 DSOs modification time are different (changed)")
    {
      enablePluginDynamicReload();

      /* Simulate installing plugin plugin_v1.so (ver 1) as plugin.so and loading it at some point of time t1 */
      auto [plugin_v1, factory1] =
        testSetupLoadPlugin(configName, buildPath_v1, uuid_t1, 1556825556, effectivePath_v1, runtimePath_v1);
      plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t1, error);

      /* Simulate installing plugin plugin_v2.so (v1) as plugin.so and loading it at some point of time t2 */
      /* Note that during the installation plugin_v2.so (v2) is "barberically" overriding the existing plugin.so which was v1 */
      auto [plugin_v2, factory2] =
        testSetupLoadPlugin(configName, buildPath_v2, uuid_t2, 1556825557, effectivePath_v2, runtimePath_v2);
      plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);

      /* Make sure plugin.so was overridden */
      CHECK(effectivePath_v1 == effectivePath_v2);
      /* Although effective path is the same runtime paths should be different */
      CHECK(runtimePath_v1 != runtimePath_v2);

      THEN("expect both to be successfully loaded and used simultaneously")
      {
        /* Both loadings should succeed */
        validateSuccessfulConfigPathTest(plugin_v1, error1, effectivePath_v1, runtimePath_v1);
        validateSuccessfulConfigPathTest(plugin_v2, error2, effectivePath_v2, runtimePath_v2);

        checkTwoLoadedVersionsDifferent(plugin_v1, plugin_v2);

        /* Make sure v1 callback functions addresses did not change for v1 after v2 was loaded */
        CHECK(tsRemapInitSym_v1_t1 == tsRemapInitSym_v1_t2);
      }

      teardownConfigPathTest(factory1);
      teardownConfigPathTest(factory2);
    }
  }

  GIVEN("two different versions v1 and v2 of same plugin with same time stamps - dynamic plugin reload ENABLED")
  {
    WHEN("(1) loading v1, (2) overwriting with v2 and then (3) reloading by using the same plugin name, "
         "(*) v1 and v2 DSOs modification time are same (did NOT change)")
    {
      enablePluginDynamicReload();

      /* Simulate installing plugin plugin_v1.so (ver 1) as plugin.so and loading it at some point of time t1 */
      auto [plugin_v1, factory1] =
        testSetupLoadPlugin(configName, buildPath_v1, uuid_t1, 1556825556, effectivePath_v1, runtimePath_v1);
      plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t1, error);

      /* Simulate installing plugin plugin_v2.so (v1) as plugin.so and loading it at some point of time t2 */
      /* Note that during the installation plugin_v2.so (v2) is "barberically" overriding the existing plugin.so
         which was v1, since the modification time is exactly the same the new v2 plugin would not be loaded and
         we should get the same PluginDso address and same effective and runtime paths */
      auto [plugin_v2, factory2] =
        testSetupLoadPlugin(configName, buildPath_v2, uuid_t2, 1556825556, effectivePath_v2, runtimePath_v2);
      plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);

      /* Make sure plugin.so was overridden */
      CHECK(effectivePath_v1 == effectivePath_v2);

      THEN("expect only v1 plugin to be loaded since the timestamp has not changed")
      {
        /* Both getRemapPlugin() calls should succeed but only v1 plugin DSO should be used */
        validateSuccessfulConfigPathTest(plugin_v1, error1, effectivePath_v1, runtimePath_v1);
        validateSuccessfulConfigPathTest(plugin_v2, error2, effectivePath_v2, runtimePath_v1);

        checkTwoLoadedVersionsSame(plugin_v1, plugin_v2);

        /* Make sure v1 callback functions addresses did not change for v1 after v2 was loaded */
        CHECK(tsRemapInitSym_v1_t1 == tsRemapInitSym_v1_t2);
      }

      teardownConfigPathTest(factory1);
      teardownConfigPathTest(factory2);
    }
  }

  GIVEN("two different versions v1 and v2 of same plugin with different time stamps - dynamic plugin reload DISABLED")
  {
    WHEN("(1) loading v1, (2) overwriting with v2 and then (3) reloading by using the same plugin name, "
         "(*) v1 and v2 DSOs modification time are different (changed)")
    {
      disablePluginDynamicReload();

      /* Simulate installing plugin plugin_v1.so (ver 1) as plugin.so and loading it at some point of time t1 */
      auto [plugin_v1, factory1] =
        testSetupLoadPlugin(configName, buildPath_v1, uuid_t1, 1556825556, effectivePath_v1, runtimePath_v1);
      plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t1, error);

      /* Simulate installing plugin plugin_v2.so (v1) as plugin.so and loading it at some point of time t2 */
      /* Note that during the installation plugin_v2.so (v2) is "barberically" overriding the existing plugin.so which was v1 */
      auto [plugin_v2, factory2] =
        testSetupLoadPlugin(configName, buildPath_v2, uuid_t2, 1556825557, effectivePath_v2, runtimePath_v2);
      plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);

      /* Make sure plugin.so was overridden */
      CHECK(effectivePath_v1 == effectivePath_v2);
      /* since dynamic reload is disabled, runtimepaths should be same */
      CHECK(runtimePath_v1 == runtimePath_v2);

      THEN("expect v1 plugin to remain loaded since dynamic reload is disabled, even though the timestamp has changed")
      {
        /* Both getRemapPlugin() calls should succeed but only v1 plugin DSO should be used */
        validateSuccessfulConfigPathTest(plugin_v1, error1, effectivePath_v1, runtimePath_v1);
        validateSuccessfulConfigPathTest(plugin_v2, error2, effectivePath_v2, runtimePath_v1);

        checkTwoLoadedVersionsSame(plugin_v1, plugin_v2);

        /* Make sure v1 callback functions addresses did not change for v1 after v2 was loaded */
        CHECK(tsRemapInitSym_v1_t1 == tsRemapInitSym_v1_t2);
      }

      teardownConfigPathTest(factory1);
      teardownConfigPathTest(factory2);
      enablePluginDynamicReload();
    }
  }

  GIVEN("two different versions v1 and v2 of same plugin with different time stamps - dynamic plugin reload ENABLED")
  {
    WHEN("(1) loading v1, (2) overwriting with v2 and then (3) reloading by using the same plugin name, "
         "(*) v1 and v2 DSOs modification time are different (changed)")
    {
      enablePluginDynamicReload();

      /* Simulate installing plugin plugin_v1.so (ver 1) as plugin.so and loading it at some point of time t1 */
      auto [plugin_v1, factory1] =
        testSetupLoadPluginWithOptOut(configName, buildPath_v1, uuid_t1, 1556825556, effectivePath_v1, runtimePath_v1);

      plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t1, error);

      /* Simulate installing plugin plugin_v2.so (v1) as plugin.so and loading it at some point of time t2 */
      /* Note that during the installation plugin_v2.so (v2) is "barberically" overriding the existing plugin.so which was v1 */
      auto [plugin_v2, factory2] =
        testSetupLoadPluginWithOptOut(configName, buildPath_v2, uuid_t2, 1556825557, effectivePath_v2, runtimePath_v2);
      plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);

      /* Make sure plugin.so was overridden */
      CHECK(effectivePath_v1 == effectivePath_v2);

      /* since dynamic reload is disabled, runtimepaths should be same */
      CHECK(runtimePath_v1 == runtimePath_v2);

      THEN("expect v1 plugin to remain loaded since dynamic reload is disabled, even though the timestamp has changed")
      {
        /* Both getRemapPlugin() calls should succeed but only v1 plugin DSO should be used */
        validateSuccessfulConfigPathTest(plugin_v1, error1, effectivePath_v1, runtimePath_v1);
        validateSuccessfulConfigPathTest(plugin_v2, error2, effectivePath_v2, runtimePath_v1);

        const bool pluginOptOut{true};
        checkTwoLoadedVersionsSame(plugin_v1, plugin_v2, pluginOptOut);

        /* Make sure v1 callback functions addresses did not change for v1 after v2 was loaded */
        CHECK(tsRemapInitSym_v1_t1 == tsRemapInitSym_v1_t2);
      }

      teardownConfigPathTest(factory1);
      teardownConfigPathTest(factory2);
      enablePluginDynamicReload();
    }
  }

  GIVEN("two different versions v1 and v2 of same plugin with same time stamp - dynamic plugin reload DISABLED")
  {
    WHEN("(1) loading v1, (2) overwriting with v2 and then (3) reloading by using the same plugin name, "
         "(*) v1 and v2 DSOs modification time are same (did NOT change)")
    {
      disablePluginDynamicReload();

      /* Simulate installing plugin plugin_v1.so (ver 1) as plugin.so and loading it at some point of time t1 */
      auto [plugin_v1, factory1] =
        testSetupLoadPlugin(configName, buildPath_v1, uuid_t1, 1556825556, effectivePath_v1, runtimePath_v1);
      plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t1, error);

      /* Simulate installing plugin plugin_v2.so (v1) as plugin.so and loading it at some point of time t2 */
      /* Note that during the installation plugin_v2.so (v2) is "barberically" overriding the existing plugin.so
         which was v1, since dynamic reload is disabled the new v2 plugin would not be loaded and
         we should get the same PluginDso address and same effective and runtime paths */
      auto [plugin_v2, factory2] =
        testSetupLoadPlugin(configName, buildPath_v2, uuid_t2, 1556825556, effectivePath_v2, runtimePath_v2);
      plugin_v1->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);

      /* Make sure plugin.so was overridden */
      CHECK(effectivePath_v1 == effectivePath_v2);
      /* since dynamic reload is disabled, runtimepaths should be same */
      CHECK(runtimePath_v1 == runtimePath_v2);

      THEN("expect v1 plugin to be loaded since dynamic reload is disabled")
      {
        /* Both getRemapPlugin() calls should succeed but only v1 plugin DSO should be used */
        validateSuccessfulConfigPathTest(plugin_v1, error1, effectivePath_v1, runtimePath_v1);
        validateSuccessfulConfigPathTest(plugin_v2, error2, effectivePath_v2, runtimePath_v1);

        checkTwoLoadedVersionsSame(plugin_v1, plugin_v2);

        /* Make sure v1 callback functions addresses did not change for v1 after v2 was loaded */
        CHECK(tsRemapInitSym_v1_t1 == tsRemapInitSym_v1_t2);
      }

      teardownConfigPathTest(factory1);
      teardownConfigPathTest(factory2);
      enablePluginDynamicReload();
    }
  }

  /* Since factories share the list of loaded plugins to avoid unnecessary loading of unchanged plugins
   * lets check if destroying a factory impacts plugins loaded from another factory */
  GIVEN("configurations with and without plugins")
  {
    WHEN("loading a configuration without plugins and then reloading configuration with a plugin")
    {
      /* Simulate configuration without plugins - an unused factory */
      PluginFactoryUnitTest *factory1 = getFactory(uuid_t1);

      /* Now provision and load a plugin using a second factory */
      auto [plugin_v2, factory2] =
        testSetupLoadPlugin(configName, buildPath_v2, uuid_t2, 1556825556, effectivePath_v2, runtimePath_v2);

      THEN("the plugin from the second factory to work")
      {
        validateSuccessfulConfigPathTest(plugin_v2, error2, effectivePath_v2, runtimePath_v2);

        /* Now delete the first factory and call a plugin from the second factory */
        delete factory1;
        CHECK(TSREMAP_NO_REMAP == plugin_v2->_plugin.doRemap(INSTANCE_HANDLER, nullptr, nullptr));
      }

      teardownConfigPathTest(factory2);
    }
  }
}

SCENARIO("loading multiple version of the same plugin in mixed mode - global as well as remap plugin", "[plugin][core]")
{
  REQUIRE_FALSE(sandboxDir.empty());
  enablePluginDynamicReload();

  static fs::path uuid_t1 = fs::path("c71e2bab-90dc-4770-9535-c9304c3de381"); /* UUID at moment t1 */
  static fs::path uuid_t2 = fs::path("c71e2bab-90dc-4770-9535-e7304c3ee732"); /* UUID at moment t2 */

  fs::path effectivePath_v1;            /* expected effective path for DSO v1 */
  fs::path effectivePath_v2;            /* expected effective path for DSO v2 */
  fs::path runtimePath_v1;              /* expected runtime path for DSO v1 */
  fs::path runtimePath_v2;              /* expected runtime path for DSO v2 */
  void *tsRemapInitSym_v1_t1 = nullptr; /* callback address from DSO v1 at moment t1 */
  void *tsRemapInitSym_v1_t2 = nullptr; /* callback address from DSO v1 at moment t2 */
  void *tsRemapInitSym_v2_t2 = nullptr; /* callback address from DSO v2 at moment t2 */

  std::string error;
  std::string error1;
  std::string error2;

  fs::path configName   = fs::path("plugin.so");                     /* use same config name for all following tests */
  fs::path buildPath_v1 = pluginBuildDir / fs::path("plugin_v1.so"); /* DSO v1 */
  fs::path buildPath_v2 = pluginBuildDir / fs::path("plugin_v2.so"); /* DSO v1 */

  GIVEN("two different versions v1 and v2 of same plugin in mixed mode - dynamic plugin reload DISABLED")
  {
    WHEN("(1) loading v1, (2) overwriting with v2 and then (3) reloading by using the same plugin name, "
         "(*) v1 and v2 DSOs modification time are different (changed)")
    {
      disablePluginDynamicReload();
      /* Simulate installing plugin plugin_v1.so (ver 1) as plugin.so and loading it at some point of time t1 */
      setupConfigPathTest(configName, buildPath_v1, uuid_t1, effectivePath_v1, runtimePath_v1, 1556825556);
      GlobalPluginInfo global_plugin_v1;
      CHECK(global_plugin_v1.loadDso(effectivePath_v1) == true);
      global_plugin_v1.getSymbol("TSRemapInit", tsRemapInitSym_v1_t1, error);

      /* Simulate installing plugin plugin_v2.so (v1) as plugin.so and loading it at some point of time t2 */
      /* Note that during the installation plugin_v2.so (v2) is "barberically" overriding the existing plugin.so which was v1 */
      setupConfigPathTest(configName, buildPath_v2, uuid_t2, effectivePath_v2, runtimePath_v2, 1556825557);
      PluginFactoryUnitTest *factory2 = getFactory(uuid_t2);
      RemapPluginInst *plugin_v2      = factory2->getRemapPlugin(configName, 0, nullptr, error2, isPluginDynamicReloadEnabled());

      /* Make sure plugin.so was overridden */
      CHECK(effectivePath_v1 == effectivePath_v2);

      /* since dynamic reload is disabled, runtimepaths should be same */
      CHECK(runtimePath_v1 == runtimePath_v2);

      THEN("expect v1 plugin to remain loaded since dynamic reload is disabled, even though the timestamp has changed")
      {
        validateSuccessfulConfigPathTest(plugin_v2, error2, effectivePath_v2, runtimePath_v2);

        /* Make sure we ended up with the same DSO object and runtime paths should be same - no new plugin was loaded */
        CHECK(global_plugin_v1.dlOpenHandle() == plugin_v2->_plugin.dlOpenHandle());

        /* Make sure v2 DSO was NOT really loaded - both instances should return same v1 version */
        CHECK(1 == global_plugin_v1.getPluginVersion());
        CHECK(1 == getPluginVersion(plugin_v2->_plugin));

        /* Make sure the symbols we get from the 2 loaded plugins yield the same callback function pointer */
        global_plugin_v1.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);
        plugin_v2->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v2_t2, error);
        CHECK(nullptr != tsRemapInitSym_v1_t2);
        CHECK(nullptr != tsRemapInitSym_v2_t2);
        CHECK(tsRemapInitSym_v1_t2 == tsRemapInitSym_v2_t2);

        /* check that the symbol we got originally is still valid */
        CHECK(tsRemapInitSym_v1_t1 == tsRemapInitSym_v1_t2);
      }

      teardownConfigPathTest(factory2);
      enablePluginDynamicReload();
    }
  }

  GIVEN("two different versions v1 and v2 of same plugin in mixed mode - dynamic plugin reload ENABLED but overwritten by opt out")
  {
    WHEN("(1) loading v1, (2) overwriting with v2 and then (3) reloading by using the same plugin name, "
         "(*) v1 and v2 DSOs modification time are different (changed)")
    {
      // just to get the proper path's
      disablePluginDynamicReload();
      /* Simulate installing plugin plugin_v1.so (ver 1) as plugin.so and loading it at some point of time t1 */
      setupConfigPathTest(configName, buildPath_v1, uuid_t1, effectivePath_v1, runtimePath_v1, 1556825556);

      GlobalPluginInfo global_plugin_v1;
      CHECK(global_plugin_v1.loadDso(effectivePath_v1) == true);
      global_plugin_v1.getSymbol("TSRemapInit", tsRemapInitSym_v1_t1, error);

      /* Simulate installing plugin plugin_v2.so (v1) as plugin.so and loading it at some point of time t2 */
      /* Note that during the installation plugin_v2.so (v2) is "barberically" overriding the existing plugin.so which was v1 */
      setupConfigPathTest(configName, buildPath_v2, uuid_t2, effectivePath_v2, runtimePath_v2, 1556825557);
      enablePluginDynamicReload();
      PluginFactoryUnitTest *factory = getFactory(uuid_t2);

      // Set factory's opt out plugin information.
      PluginDso::loadedPlugins()->addPluginPathToDsoOptOutTable(effectivePath_v1.string());

      RemapPluginInst *plugin_v2 = factory->getRemapPlugin(configName, 0, nullptr, error2, isPluginDynamicReloadEnabled());

      /* Make sure plugin.so was overridden */
      CHECK(effectivePath_v1 == effectivePath_v2);

      /* since dynamic reload is disabled, runtimepaths should be same */
      CHECK(runtimePath_v1 == runtimePath_v2);

      THEN("expect v1 plugin to remain loaded since dynamic reload is disabled, even though the timestamp has changed")
      {
        validateSuccessfulConfigPathTest(plugin_v2, error2, effectivePath_v2, runtimePath_v2);

        /* Make sure we ended up with the same DSO object and runtime paths should be same - no new plugin was loaded */
        CHECK(global_plugin_v1.dlOpenHandle() == plugin_v2->_plugin.dlOpenHandle());

        /* Make sure v2 DSO was NOT really loaded - both instances should return same v1 version */
        CHECK(1 == global_plugin_v1.getPluginVersion());
        CHECK(1 == getPluginVersion(plugin_v2->_plugin));

        /* Make sure the symbols we get from the 2 loaded plugins yield the same callback function pointer */
        global_plugin_v1.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);
        plugin_v2->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v2_t2, error);
        CHECK(nullptr != tsRemapInitSym_v1_t2);
        CHECK(nullptr != tsRemapInitSym_v2_t2);
        CHECK(tsRemapInitSym_v1_t2 == tsRemapInitSym_v2_t2);

        /* check that the symbol we got originally is still valid */
        CHECK(tsRemapInitSym_v1_t1 == tsRemapInitSym_v1_t2);
      }
      PluginDso::loadedPlugins()->removePluginPathFromDsoOptOutTable(effectivePath_v1.string());
      teardownConfigPathTest(factory);
      enablePluginDynamicReload();
    }
  }

  GIVEN("two different versions v1 and v2 of same plugin in mixed mode - dynamic plugin reload DISABLED")
  {
    WHEN("(1) loading v1, (2) overwriting with v2 and then (3) reloading by using the same plugin name, "
         "(*) v1 and v2 DSOs modification times are unchanged")
    {
      disablePluginDynamicReload();
      /* Simulate installing plugin plugin_v1.so (ver 1) as plugin.so and loading it at some point of time t1 */
      setupConfigPathTest(configName, buildPath_v1, uuid_t1, effectivePath_v1, runtimePath_v1, 1556825556);
      GlobalPluginInfo global_plugin_v1;
      CHECK(global_plugin_v1.loadDso(effectivePath_v1) == true);
      global_plugin_v1.getSymbol("TSRemapInit", tsRemapInitSym_v1_t1, error);

      /* Simulate installing plugin plugin_v2.so (v1) as plugin.so and loading it at some point of time t2 */
      /* Note that during the installation plugin_v2.so (v2) is "barberically" overriding the existing plugin.so which was v1 */
      setupConfigPathTest(configName, buildPath_v2, uuid_t2, effectivePath_v2, runtimePath_v2, 1556825556);
      PluginFactoryUnitTest *factory2 = getFactory(uuid_t2);
      RemapPluginInst *plugin_v2      = factory2->getRemapPlugin(configName, 0, nullptr, error2, isPluginDynamicReloadEnabled());

      /* Make sure plugin.so was overridden */
      CHECK(effectivePath_v1 == effectivePath_v2);

      /* since dynamic reload is disabled, runtimepaths should be same */
      CHECK(runtimePath_v1 == runtimePath_v2);

      THEN("expect v1 plugin to remain loaded since dynamic reload is disabled, even though the timestamp has changed")
      {
        validateSuccessfulConfigPathTest(plugin_v2, error2, effectivePath_v2, runtimePath_v2);

        /* Make sure we ended up with the same DSO object and runtime paths should be same - no new plugin was loaded */
        CHECK(global_plugin_v1.dlOpenHandle() == plugin_v2->_plugin.dlOpenHandle());

        /* Make sure v2 DSO was NOT really loaded - both instances should return same v1 version */
        CHECK(1 == global_plugin_v1.getPluginVersion());
        CHECK(1 == getPluginVersion(plugin_v2->_plugin));

        /* Make sure the symbols we get from the 2 loaded plugins yield the same callback function pointer */
        global_plugin_v1.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);
        plugin_v2->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v2_t2, error);
        CHECK(nullptr != tsRemapInitSym_v1_t2);
        CHECK(nullptr != tsRemapInitSym_v2_t2);
        CHECK(tsRemapInitSym_v1_t2 == tsRemapInitSym_v2_t2);

        /* check that the symbol we got originally is still valid */
        CHECK(tsRemapInitSym_v1_t1 == tsRemapInitSym_v1_t2);
      }

      teardownConfigPathTest(factory2);
      enablePluginDynamicReload();
    }
  }

  GIVEN("two different versions v1 and v2 of same plugin in mixed mode - dynamic plugin reload ENABLED (negative test)")
  {
    WHEN("(1) loading v1, (2) overwriting with v2 and then (3) reloading by using the same plugin name, "
         "(*) v1 and v2 DSOs modification time are different (changed)")
    {
      enablePluginDynamicReload();
      /* Simulate installing plugin plugin_v1.so (ver 1) as plugin.so and loading it at some point of time t1 */
      setupConfigPathTest(configName, buildPath_v1, uuid_t1, effectivePath_v1, runtimePath_v1, 1556825556);
      GlobalPluginInfo global_plugin_v1;
      CHECK(global_plugin_v1.loadDso(effectivePath_v1) == true);
      global_plugin_v1.getSymbol("TSRemapInit", tsRemapInitSym_v1_t1, error);

      /* Simulate installing plugin plugin_v2.so (v1) as plugin.so and loading it at some point of time t2 */
      /* Note that during the installation plugin_v2.so (v2) is "barberically" overriding the existing plugin.so which was v1 */
      auto [plugin_v2, factory2] =
        testSetupLoadPlugin(configName, buildPath_v2, uuid_t2, 1556825557, effectivePath_v2, runtimePath_v2);

      CHECK(effectivePath_v1 == effectivePath_v2);
      /* since dynamic reload is enabled runtime paths would be different */
      CHECK(runtimePath_v1 != runtimePath_v2);

      THEN("expect both to be successfully loaded and used simultaneously")
      {
        validateSuccessfulConfigPathTest(plugin_v2, error2, effectivePath_v2, runtimePath_v2);

        /* Make sure what we installed and loaded first was v1 and after the plugin reload we run v2 */
        CHECK(1 == global_plugin_v1.getPluginVersion());
        CHECK(2 == getPluginVersion(plugin_v2->_plugin));
        CHECK(global_plugin_v1.dlOpenHandle() != plugin_v2->_plugin.dlOpenHandle());

        /* Make sure the symbols we get from the 2 loaded plugins don't yield the same callback function pointer */
        global_plugin_v1.getSymbol("TSRemapInit", tsRemapInitSym_v1_t2, error);
        plugin_v2->_plugin.getSymbol("TSRemapInit", tsRemapInitSym_v2_t2, error);
        CHECK(nullptr != tsRemapInitSym_v1_t2);
        CHECK(nullptr != tsRemapInitSym_v2_t2);
        CHECK(tsRemapInitSym_v1_t2 != tsRemapInitSym_v2_t2);

        /* Make sure v1 callback functions addresses did not change for v1 after v2 was loaded */
        CHECK(tsRemapInitSym_v1_t1 == tsRemapInitSym_v1_t2);
      }

      teardownConfigPathTest(factory2);
      enablePluginDynamicReload();
    }
  }
}

SCENARIO("notifying plugins of config reload", "[plugin][core]")
{
  REQUIRE_FALSE(sandboxDir.empty());
  enablePluginDynamicReload();

  /* use 2 copies of the same plugin to test */
  fs::path configName1 = fs::path("plugin_testing_calls_1.so");
  fs::path configName2 = fs::path("plugin_testing_calls_2.so");
  fs::path buildPath   = pluginBuildDir / fs::path("plugin_testing_calls.so");

  static fs::path uuid_t1 = fs::path("c71e2bab-90dc-4770-9535-c9304c3de381"); /* UUID at moment t1 */
  static fs::path uuid_t2 = fs::path("c71e2bab-90dc-4770-9535-e7304c3ee732"); /* UUID at moment t2 */

  fs::path effectivePath1;
  fs::path effectivePath2;
  fs::path runtimePath1;
  fs::path runtimePath2;

  std::string error;

  GIVEN("simple configuration with 1 plugin and 1 factory")
  {
    WHEN("(1) signal pre-new-config-load, (2) signal post-new-config-load, (3) old-config deactivate")
    {
      /* Simulate configuration with 1 factory and 1 plugin */
      setupConfigPathTest(configName1, buildPath, uuid_t1, effectivePath1, runtimePath1, 1556825556);
      PluginFactoryUnitTest *factory1 = getFactory(uuid_t1);
      RemapPluginInst *plugin1        = factory1->getRemapPlugin(configName1, 0, nullptr, error, isPluginDynamicReloadEnabled());

      /* check if loaded successfully */
      validateSuccessfulConfigPathTest(plugin1, error, effectivePath1, runtimePath1);

      /* Prapare the debug object */
      PluginDebugObject *debugObject = getDebugObject(plugin1->_plugin);

      THEN("expect pre-, post- and done/delete_instance to be called accordingly ")
      {
        /* Signal before loading the config */
        debugObject->clear();

        factory1->indicatePreReload();
        CHECK(0 == debugObject->deleteInstanceCalled);
        CHECK(0 == debugObject->doneCalled);
        CHECK(1 == debugObject->preReloadConfigCalled);

        /* ... parse the new remap config ... */

        /* Assume (re)load was done OK and test */
        debugObject->clear();
        factory1->indicatePostReload(/* reload succeeded */ true);
        CHECK(0 == debugObject->deleteInstanceCalled);
        CHECK(0 == debugObject->doneCalled);
        CHECK(1 == debugObject->postReloadConfigCalled);
        CHECK(TSREMAP_CONFIG_RELOAD_SUCCESS_PLUGIN_USED == debugObject->postReloadConfigStatus);

        /* Assume (re)load failed and test */
        debugObject->clear();
        factory1->indicatePostReload(/* reload succeeded */ false);
        CHECK(0 == debugObject->deleteInstanceCalled);
        CHECK(0 == debugObject->doneCalled);
        CHECK(1 == debugObject->postReloadConfigCalled);
        CHECK(TSREMAP_CONFIG_RELOAD_FAILURE == debugObject->postReloadConfigStatus);

        /* ... swap the new and the old config ... */

        /* Signal de-activation of the old config */
        debugObject->clear();
        factory1->deactivate();
        CHECK(1 == debugObject->deleteInstanceCalled);
        CHECK(1 == debugObject->doneCalled);
        CHECK(0 == debugObject->preReloadConfigCalled);
      }

      teardownConfigPathTest(factory1);
    }
  }

  GIVEN("configuration with 2 plugins loaded by 1 factory")
  {
    WHEN("(1) signal pre-new-config-load, (2) signal post-new-config-load, (3) old-config deactivate")
    {
      /* Simulate configuration with 1 factories and 2 plugin */
      setupConfigPathTest(configName1, buildPath, uuid_t1, effectivePath1, runtimePath1, 1556825556);
      setupConfigPathTest(configName2, buildPath, uuid_t1, effectivePath2, runtimePath2, 1556825556, /* append */ true);
      PluginFactoryUnitTest *factory1 = getFactory(uuid_t1);
      RemapPluginInst *plugin1        = factory1->getRemapPlugin(configName1, 0, nullptr, error, isPluginDynamicReloadEnabled());
      RemapPluginInst *plugin2        = factory1->getRemapPlugin(configName2, 0, nullptr, error, isPluginDynamicReloadEnabled());

      /* check if loaded successfully */
      validateSuccessfulConfigPathTest(plugin1, error, effectivePath1, runtimePath1);
      validateSuccessfulConfigPathTest(plugin2, error, effectivePath2, runtimePath2);

      /* Prapare the debug objects */
      PluginDebugObject *debugObject1 = getDebugObject(plugin1->_plugin);
      PluginDebugObject *debugObject2 = getDebugObject(plugin2->_plugin);

      THEN("expect pre-, post- and done/delete_instance to be called accordingly")
      {
        /* Signal before loading the config */
        debugObject1->clear();
        debugObject2->clear();
        factory1->indicatePreReload();
        CHECK(0 == debugObject1->deleteInstanceCalled);
        CHECK(0 == debugObject1->doneCalled);
        CHECK(1 == debugObject1->preReloadConfigCalled);
        CHECK(0 == debugObject2->doneCalled);
        CHECK(0 == debugObject2->deleteInstanceCalled);
        CHECK(1 == debugObject2->preReloadConfigCalled);

        /* ... parse the new remap config ... */

        /* Assume (re)load was done OK */
        debugObject1->clear();
        debugObject2->clear();
        factory1->indicatePostReload(/* reload succeeded */ true);
        CHECK(0 == debugObject1->deleteInstanceCalled);
        CHECK(0 == debugObject1->doneCalled);
        CHECK(1 == debugObject1->postReloadConfigCalled);
        CHECK(TSREMAP_CONFIG_RELOAD_SUCCESS_PLUGIN_USED == debugObject1->postReloadConfigStatus);
        CHECK(0 == debugObject2->deleteInstanceCalled);
        CHECK(0 == debugObject2->doneCalled);
        CHECK(1 == debugObject2->postReloadConfigCalled);
        CHECK(TSREMAP_CONFIG_RELOAD_SUCCESS_PLUGIN_USED == debugObject2->postReloadConfigStatus);

        /* Assume (re)load failed */
        debugObject1->clear();
        debugObject2->clear();
        factory1->indicatePostReload(/* reload succeeded */ false);
        CHECK(0 == debugObject1->deleteInstanceCalled);
        CHECK(0 == debugObject1->doneCalled);
        CHECK(1 == debugObject1->postReloadConfigCalled);
        CHECK(TSREMAP_CONFIG_RELOAD_FAILURE == debugObject1->postReloadConfigStatus);
        CHECK(0 == debugObject2->deleteInstanceCalled);
        CHECK(0 == debugObject2->doneCalled);
        CHECK(1 == debugObject2->postReloadConfigCalled);
        CHECK(TSREMAP_CONFIG_RELOAD_FAILURE == debugObject2->postReloadConfigStatus);

        /* ... swap the new and the old config ... */

        /* Signal de-activation of the old config */
        debugObject1->clear();
        debugObject2->clear();
        factory1->deactivate();
        CHECK(1 == debugObject1->deleteInstanceCalled);
        CHECK(1 == debugObject1->doneCalled);
        CHECK(0 == debugObject1->preReloadConfigCalled);
        CHECK(1 == debugObject2->deleteInstanceCalled);
        CHECK(1 == debugObject2->doneCalled);
        CHECK(0 == debugObject2->preReloadConfigCalled);
      }

      teardownConfigPathTest(factory1);
    }
  }

  GIVEN("configuration with 1 plugin loaded by 2 separate factories")
  {
    WHEN("indicating de-activation of the factories")
    {
      /* Simulate configuration with 2 factories and 1 plugin */
      setupConfigPathTest(configName1, buildPath, uuid_t1, effectivePath1, runtimePath1, 1556825556);
      PluginFactoryUnitTest *factory1 = getFactory(uuid_t1);
      PluginFactoryUnitTest *factory2 = getFactory(uuid_t2);
      RemapPluginInst *plugin1        = factory1->getRemapPlugin(configName1, 0, nullptr, error, isPluginDynamicReloadEnabled());
      RemapPluginInst *plugin2        = factory2->getRemapPlugin(configName1, 0, nullptr, error, isPluginDynamicReloadEnabled());

      /* Prepare the debug objects */
      PluginDebugObject *debugObject1 = getDebugObject(plugin1->_plugin);
      PluginDebugObject *debugObject2 = getDebugObject(plugin2->_plugin);

      THEN("expect instance 'done' to be always called, but plugin 'done' called only after destroying both factories")
      {
        debugObject2->clear();
        factory2->deactivate();
        CHECK(0 == debugObject2->doneCalled);
        CHECK(1 == debugObject2->deleteInstanceCalled);
        CHECK(0 == debugObject2->preReloadConfigCalled);

        delete factory2;

        debugObject1->clear();
        factory1->deactivate();
        CHECK(1 == debugObject1->doneCalled);
        CHECK(1 == debugObject1->deleteInstanceCalled);
        CHECK(0 == debugObject1->preReloadConfigCalled);

        delete factory1;
      }

      clean();
    }
  }

  GIVEN("configuration with 2 plugin loaded by 2 factories")
  {
    WHEN("2 plugins are loaded by the 1st factory and only one of them loaded by the 2nd factory")
    {
      /* Simulate configuration with 2 factories and 2 different plugins */
      setupConfigPathTest(configName1, buildPath, uuid_t1, effectivePath1, runtimePath1, 1556825556, /* append */ false);
      setupConfigPathTest(configName2, buildPath, uuid_t1, effectivePath2, runtimePath2, 1556825556, /* append */ true);
      PluginFactoryUnitTest *factory1 = getFactory(uuid_t1);
      PluginFactoryUnitTest *factory2 = getFactory(uuid_t2);

      /* 2 plugins loaded by the 1st factory */
      RemapPluginInst *pluginInst1 = factory1->getRemapPlugin(configName1, 0, nullptr, error, isPluginDynamicReloadEnabled());
      RemapPluginInst *pluginInst2 = factory1->getRemapPlugin(configName2, 0, nullptr, error, isPluginDynamicReloadEnabled());

      /* only 1 plugin loaded by the 2nd factory */
      RemapPluginInst *pluginInst3 = factory2->getRemapPlugin(configName1, 0, nullptr, error, isPluginDynamicReloadEnabled());

      /* pluginInst1 and pluginInst3 should be using the same plugin DSO named configName1
       * pluginInst2 should be using plugin DSO named configName 2*/
      CHECK_FALSE(nullptr == pluginInst1);
      CHECK_FALSE(nullptr == pluginInst2);
      CHECK_FALSE(nullptr == pluginInst3);
      CHECK(&pluginInst1->_plugin == &pluginInst3->_plugin);

      /* Get test objects for the 2 plugins used by the 3 instances from the 2 factories */
      PluginDebugObject *debugObject1 = getDebugObject(pluginInst1->_plugin);
      PluginDebugObject *debugObject2 = getDebugObject(pluginInst2->_plugin);

      THEN(
        "expect the plugin that is not loaded by the second factory to receive 'plugin unused' notification from the 2nd factory")
      {
        /* Factory 1: reload was OK and both plugins were part of the configuration that used/instantiated that factory */
        debugObject1->clear();
        debugObject2->clear();

        factory1->indicatePostReload(/* reload succeeded */ true);
        CHECK(1 == debugObject1->postReloadConfigCalled);
        CHECK(TSREMAP_CONFIG_RELOAD_SUCCESS_PLUGIN_USED == debugObject1->postReloadConfigStatus);
        CHECK(1 == debugObject2->postReloadConfigCalled);
        CHECK(TSREMAP_CONFIG_RELOAD_SUCCESS_PLUGIN_USED == debugObject2->postReloadConfigStatus);

        /* Factory 2: (re)load was OK and only 1 plugin was part of the configuration that used/instantiated that factory */
        debugObject1->clear();
        debugObject2->clear();

        factory2->indicatePostReload(/* reload succeeded */ true);
        CHECK(1 == debugObject1->postReloadConfigCalled);
        CHECK(TSREMAP_CONFIG_RELOAD_SUCCESS_PLUGIN_USED == debugObject1->postReloadConfigStatus);
        CHECK(1 == debugObject2->postReloadConfigCalled);
        CHECK(TSREMAP_CONFIG_RELOAD_SUCCESS_PLUGIN_UNUSED == debugObject2->postReloadConfigStatus);

        delete factory1;
        delete factory2;
      }

      clean();
    }
  }
}