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
// Copyright (C) 2019-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>
#include <cb_cmds_test.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cc/command_interpreter.h>
#include <cc/data.h>
#include <database/backend_selector.h>
#include <dhcpsrv/config_backend_dhcp4_mgr.h>
#include <dhcp/option_string.h>
#include <dhcp/testutils/iface_mgr_test_config.h>
#include <set><--- 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 <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::asiolink;
using namespace isc::cb;
using namespace isc::cb::test;
using namespace isc::config;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::db;

namespace {

/// @brief Test fixture class for callouts pertaining to shared networks.
class SharedNetwork4CmdsTest : public ConfigCmdsDhcp4Test {
public:

    /// @brief Adds subnet to the configuration backend.
    ///
    /// @param subnet Pointer to the subnet to be stored.
    void setSubnet(const Subnet4Ptr& subnet) {
        ConfigBackendDHCPv4Mgr::instance().getPool()->
            createUpdateSubnet4(BackendSelector(), ServerSelector::ALL(), subnet);
    }

    /// @brief Adds unassigned shared network in the configuration backend.
    ///
    /// @param shared_network Pointer to the shared network to be stored.
    void setSharedNetwork(const SharedNetwork4Ptr& shared_network) {
        ConfigBackendDHCPv4Mgr::instance().getPool()->
            createUpdateSharedNetwork4(BackendSelector(),
                                       ServerSelector::UNASSIGNED(),
                                       shared_network);
    }

    /// @brief Returns remote-network4-set command as text.
    ///
    /// @param shared_network Pointer to the shared network to be used in the
    /// command.
    /// @param remote_map A map to be assigned to the "remote" argument
    /// of the command. If the string is empty the "remote" argument is
    /// not included.
    /// @param server_tags Server tags to be included in the command.
    ///
    /// @return Command in the textual format.
    std::string makeSharedNetwork4SetConfig(const SharedNetwork4Ptr& shared_network,
                                            const std::string& remote_map,
                                            const std::set<std::string>& server_tags) {
        ElementPtr sn_element = shared_network->toElement();
        sn_element->remove("subnet4");

        std::string config = "{"
            "\"command\": \"remote-network4-set\","
            "\"arguments\": {"
            "    \"shared-networks\": [";

        config += sn_element->str() + "]";

        // Only include "remote" parameter if non empty.
        if (!remote_map.empty()) {
            config += ", \"remote\": " + remote_map;
        }

        // Only include server tags if specified.
        if (!server_tags.empty()) {
            config += ", \"server-tags\": [";
            for (auto const& tag : server_tags) {
                config += "\"" + tag + "\",";
            }
            // Remove extraneous comma.
            config.pop_back();
            config += "]";
        }

        config += "} }";
        return (config);
    }

    /// @brief Positive test scenario for setting or updating shared network.
    ///
    /// @param shared_network Pointer to the shared network to be created or
    /// updated.
    /// @param remote_map A map to be assigned to the "remote" argument. The
    /// default value is the empty string in which case the "remote" argument
    /// is not included in the command.
    /// @param server_tags Server tags to be included in the command.
    void testSharedNetwork4Set(const SharedNetwork4Ptr& shared_network,
                               const std::string& remote_map = "",
                               const std::set<std::string>& server_tags = { ServerTag::ALL }) {
        // Generate the command as text.
        std::string command_text = makeSharedNetwork4SetConfig(shared_network,
                                                               remote_map,
                                                               server_tags);

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp4Impl::setSharedNetwork4,
                                             command_text));
        ASSERT_TRUE(response);

        // Make sure that the response indicates success.
        int rcode = 0;
        auto args = parseAnswer(rcode, response);
        EXPECT_EQ(CONTROL_RESULT_SUCCESS, rcode);

        auto msg = response->get(CONTROL_TEXT);
        ASSERT_TRUE(msg);
        EXPECT_EQ("IPv4 shared network successfully set.", msg->stringValue());

        ASSERT_TRUE(args);
        ASSERT_EQ(Element::map, args->getType());

        // Validate returned list of shared networks.
        auto shared_network_list = args->get("shared-networks");
        ASSERT_TRUE(shared_network_list);
        EXPECT_EQ(Element::list, shared_network_list->getType());
        ASSERT_EQ(1, shared_network_list->size());

        auto shared_network_0 = shared_network_list->get(0);
        ASSERT_TRUE(shared_network_0);
        ASSERT_EQ(Element::map, shared_network_0->getType());
        EXPECT_EQ(1, shared_network_0->size());

        auto shared_network_0_name = shared_network_0->get("name");
        ASSERT_TRUE(shared_network_0_name);
        EXPECT_EQ(Element::string, shared_network_0_name->getType());
        EXPECT_EQ(shared_network->getName(), shared_network_0_name->stringValue());

        // Make sure that the created/updated shared network is in the database
        // and has correct values and metadata.
        ASSERT_FALSE(server_tags.empty());
        auto fetched_shared_network = ConfigBackendDHCPv4Mgr::instance().getPool()->
            getSharedNetwork4(BackendSelector(),
                              ServerSelector::ONE(*server_tags.begin()),
                              shared_network->getName());
        ASSERT_TRUE(fetched_shared_network);

        EXPECT_EQ(shared_network->toElement()->str(),
                  fetched_shared_network->toElement()->str());

        // Make sure that the shared network options are not encapsulated.
        EXPECT_FALSE(fetched_shared_network->getCfgOption()->isEncapsulated());

        auto metadata = fetched_shared_network->getMetadata();
        ASSERT_TRUE(metadata);
        auto expected_metadata = createMetadata(server_tags);
        EXPECT_EQ(expected_metadata->str(), metadata->str());

        // Make sure that the created/updated subnets are in the database.
        auto fetched_subnets = ConfigBackendDHCPv4Mgr::instance().getPool()->
            getAllSubnets4(BackendSelector(),
                           ServerSelector::ONE(*server_tags.begin()));
        auto const& index = fetched_subnets.get<SubnetSubnetIdIndexTag>();
        for (auto const& subnet : *shared_network->getAllSubnets()) {
            EXPECT_EQ(1, index.count(subnet->getID()));
            // Make sure that the subnet options are not encapsulated.
            EXPECT_FALSE(subnet->getCfgOption()->isEncapsulated());
        }
    }

    /// @brief Negative test scenario for setting or updating a shared network.
    ///
    /// @param args Arguments for the command in the textual format.
    /// @param answer_text Expected errors message.
    void testSharedNetwork4SetFail(const std::string& args,
                                   const std::string& answer_text) {
        // Generate the command as text.
        std::string command_text = "{"
            "\"command\": \"remote-network4-set\","
            "\"arguments\": {" + args + "}"
        "}";

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp4Impl::setSharedNetwork4,
                                             command_text));
        ASSERT_TRUE(response);

        // Make sure that the response indicates error.
        int rcode = 0;
        auto answer = parseAnswer(rcode, response);
        EXPECT_EQ(CONTROL_RESULT_ERROR, rcode);

        auto msg = response->get(CONTROL_TEXT);
        ASSERT_TRUE(msg);

        /// @todo Skipping line number possibly appended to the error message by
        /// the shared network parser. We should modify the parser to not
        /// output the position when used for control commands parsing.
        EXPECT_EQ(0, msg->stringValue().find(answer_text))
            << "expected: " << answer_text
            << ", found: " << msg->stringValue();
    }

    /// @brief Returns remote-network4-del command as text.
    ///
    /// @param shared_network Pointer to the shared network to be used in the
    /// command.
    /// @param remote_map A map to be assigned to the "remote" argument
    /// of the command. If the string is empty the "remote" argument is
    /// not included.
    /// @param action A string to be assigned to the "subnets-action"
    /// argument of the command. If the string is empty the "subnets-action"
    /// argument is not included.
    ///
    /// @return Command in the textual format.
    std::string makeSharedNetwork4DelConfig(const SharedNetwork4Ptr& shared_network,
                                            const std::string& remote_map,
                                            const std::string& action) {
        std::string config = "{"
            "\"command\": \"remote-network4-del\","
            "\"arguments\": {"
            "    \"shared-networks\": [ { ";

        config += "\"name\": \"" + shared_network->getName() + "\" } ]";

        // Only include "remote" parameter if non empty.
        if (!remote_map.empty()) {
            config += ", \"remote\": " + remote_map;
        }

        // No server tags.

        // Only include "subnets-action" parameter if non empty.
        if (!action.empty()) {
            config += ", \"subnets-action\": \"" + action + "\"";
        }
        config += "} }";
        return (config);
    }

    /// @brief Positive test scenario for deleting shared network.
    ///
    /// @param shared_network Pointer to the shared network to be deleted.
    /// @param expected_count Expected number of deleted shared networks..
    /// @param remote_map A map to be assigned to the "remote" argument. The
    /// default value is the empty string in which case the "remote" argument
    /// is not included in the command.
    /// @param action A string to be assigned to the "subnets-action"
    /// argument of the command. If the string is empty the "subnets-action"
    /// argument is not included.
    void testSharedNetwork4Del(const SharedNetwork4Ptr& shared_network,
                               unsigned expected_count,
                               const std::string& remote_map = "",
                               const std::string& action = "") {

        // Generate the command as text.
        std::string command_text = makeSharedNetwork4DelConfig(shared_network,
                                                               remote_map,
                                                               action);

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp4Impl::delSharedNetwork4,
                                             command_text));
        ASSERT_TRUE(response);

        // Make sure that the response indicates success.
        int rcode = 0;
        auto args = parseAnswer(rcode, response);
        int expected_rcode = (expected_count == 0 ?
            CONTROL_RESULT_EMPTY : CONTROL_RESULT_SUCCESS);
        EXPECT_EQ(expected_rcode, rcode);

        std::ostringstream expected_msg;
        expected_msg << expected_count << " IPv4 shared network(s) deleted.";
        auto msg = response->get(CONTROL_TEXT);
        ASSERT_TRUE(msg);
        EXPECT_EQ(expected_msg.str(), msg->stringValue());

        ASSERT_TRUE(args);
        ASSERT_EQ(Element::map, args->getType());

        // Validate count.
        auto count = args->get("count");
        ASSERT_TRUE(count);
        ASSERT_EQ(Element::integer, count->getType());
        EXPECT_EQ(expected_count, count->intValue());

        // Make sure that the deleted shared network is not in the database.
        auto fetched_shared_network = ConfigBackendDHCPv4Mgr::instance().getPool()->
            getSharedNetwork4(BackendSelector(),
                              ServerSelector::ANY(),
                              shared_network->getName());
        ASSERT_FALSE(fetched_shared_network);
    }

    /// @brief Negative test scenario for deleting a shared network.
    ///
    /// @param args Arguments for the command in the textual format.
    /// @param answer_text Expected errors message.
    void testSharedNetwork4DelFail(const std::string& args,
                                   const std::string& answer_text) {
        // Generate the command as text.
        std::string command_text = "{"
            "\"command\": \"remote-network4-del\","
            "\"arguments\": {" + args + "}"
        "}";

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp4Impl::delSharedNetwork4,
                                             command_text));
        ASSERT_TRUE(response);

        // Make sure that the response indicates error.
        int rcode = 0;
        auto answer = parseAnswer(rcode, response);
        EXPECT_EQ(CONTROL_RESULT_ERROR, rcode);

        auto msg = response->get(CONTROL_TEXT);
        ASSERT_TRUE(msg);
        EXPECT_EQ(answer_text, answer->stringValue());
    }

    /// @brief Returns remote-network4-get command as text.
    ///
    /// @param shared_network Pointer to the shared network to be used in the
    /// command.
    /// @param subnets_include Value to be passed to be used for subnets-include.
    /// @param remote_map A map to be assigned to the "remote" argument
    /// of the command. If the string is empty the "remote" argument is
    /// not included.
    ///
    /// @return Command in the textual format.
    std::string makeSharedNetwork4GetConfig(const SharedNetwork4Ptr& shared_network,
                                            const std::string& subnets_include,
                                            const std::string& remote_map) {
        std::string config = "{"
            "\"command\": \"remote-network4-get\","
            "\"arguments\": {"
            "    \"shared-networks\": [ { ";

        config += "\"name\": \"" + shared_network->getName() + "\" } ]";

        // Only include "subnets-include" parameter if non empty.
        if (!subnets_include.empty()) {
            config += ", \"subnets-include\": \"" + subnets_include + "\"";
        }

        // Only include "remote" parameter if non empty.
        if (!remote_map.empty()) {
            config += ", \"remote\": " + remote_map;
        }

        // No server tags.

        config += "} }";
        return (config);
    }

    /// @brief Positive test scenario for getting a shared network.
    ///
    /// @param shared_network Pointer to the shared network to be got.
    /// @param expected Pointer to the expected shared network.
    /// @param subnets_include Value to be passed to be used for subnets-include.
    /// @param remote_map A map to be assigned to the "remote" argument. The
    /// default value is the empty string in which case the "remote" argument
    /// is not included in the command.
    void testSharedNetwork4Get(const SharedNetwork4Ptr& shared_network,
                               const SharedNetwork4Ptr& expected,
                               const std::string& subnets_include = "",
                               const std::string& remote_map = "") {
        // Generate the command as text.
        std::string command_text = makeSharedNetwork4GetConfig(shared_network,
                                                               subnets_include,
                                                               remote_map);

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response =
                        impl_.run(&TestConfigCmdsDhcp4Impl::getSharedNetwork4,
                                  command_text));
        ASSERT_TRUE(response);

        // Make sure that the response indicates success.
        int rcode = 0;
        auto args = parseAnswer(rcode, response);
        int expected_rcode = (!expected ?
            CONTROL_RESULT_EMPTY : CONTROL_RESULT_SUCCESS);
        EXPECT_EQ(expected_rcode, rcode);

        std::ostringstream expected_msg;
        expected_msg << "IPv4 shared network '" << shared_network->getName()
                     << "' " << (!expected ? "not " : "") << "found.";
        auto msg = response->get(CONTROL_TEXT);
        ASSERT_TRUE(msg);
        EXPECT_EQ(expected_msg.str(), msg->stringValue());

        ASSERT_TRUE(args);
        ASSERT_EQ(Element::map, args->getType());

        // Validate returned shared network.
        ConstElementPtr networks = args->get("shared-networks");
        ASSERT_TRUE(networks);
        ASSERT_EQ(Element::list, networks->getType());
        ConstElementPtr count = args->get("count");
        ASSERT_TRUE(count);
        ASSERT_EQ(Element::integer, count->getType());

        if (!expected) {
            EXPECT_EQ(0, networks->size());
            EXPECT_EQ(0, count->intValue());
        } else {
            ASSERT_EQ(1, networks->size());
            ConstElementPtr got = networks->get(0);

            // Make sure that expected shared network includes metadata.
            ElementPtr expected_element = expected->toElement();
            auto tags = expected->getServerTags();
            ASSERT_FALSE(tags.empty());
            expected_element->set("metadata",
                                  createMetadata(tags.begin()->get()));

            // There must be no subnet4 parameter if the subnets are not
            // to be included.
            if (subnets_include != "full") {
                expected_element->remove("subnet4");
            }

            EXPECT_TRUE(isEquivalent(got, expected_element))
                << "Actual: " << got->str()
                << ", \nExpected: " << expected_element->str();
            EXPECT_EQ(1, count->intValue());
        }
    }

    /// @brief Negative test scenario for getting a shared network.
    ///
    /// @param args Arguments for the command in the textual format.
    /// @param answer_text Expected errors message.
    void testSharedNetwork4GetFail(const std::string& args,
                                   const std::string& answer_text) {
        // Generate the command as text.
        std::string command_text = "{"
            "\"command\": \"remote-network4-get\","
            "\"arguments\": {" + args + "}"
        "}";

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response =
                        impl_.run(&TestConfigCmdsDhcp4Impl::getSharedNetwork4,
                                  command_text));
        ASSERT_TRUE(response);

        // Make sure that the response indicates error.
        int rcode = 0;
        auto answer = parseAnswer(rcode, response);
        EXPECT_EQ(CONTROL_RESULT_ERROR, rcode);

        auto msg = response->get(CONTROL_TEXT);
        ASSERT_TRUE(msg);
        EXPECT_EQ(answer_text, answer->stringValue());
    }

    /// @brief Returns remote-network4-list command as text.
    ///
    /// @param remote_map A map to be assigned to the "remote" argument
    /// of the command. If the string is empty the "remote" argument is
    /// not included.
    /// @param server_tags Server tags to be included in the command.
    ///
    /// @return Command in the textual format.
    std::string makeSharedNetwork4ListConfig(const std::string& remote_map,
                                             const std::set<std::string>& server_tags) {
        std::string config = "{"
            "\"command\": \"remote-network4-list\""
            ", \"arguments\": {";

        // Only include "remote" parameter if non empty.
        if (!remote_map.empty()) {
            config += "\"remote\": " + remote_map + ",";
        }

        // Always include server tags,
        config += "\"server-tags\": [";
        if (!server_tags.empty()) {
            for (auto const& tag : server_tags) {
                config += "\"" + tag + "\",";
            }
            // Remove extraneous comma.
            config.pop_back();
        } else {
            // Empty means unassigned.
            config += " null ";
        }

        config += "] } }";
        return (config);
    }

    /// @brief Positive test scenario for getting all shared networks.
    ///
    /// @param expected Expected result.
    /// @param remote_map A map to be assigned to the "remote" argument. The
    /// default value is the empty string in which case the "remote" argument
    /// is not included in the command.
    /// @param server_tags Server tags to be included in the command,
    /// empty means UNASSIGNED.
    void testSharedNetwork4List(ConstElementPtr expected,
                                const std::string& remote_map = "",
                                const std::set<std::string>& server_tags = { ServerTag::ALL }) {
        // Sanity.
        ASSERT_TRUE(expected) << " bad test";
        ASSERT_EQ(Element::list, expected->getType()) << " bad test";

        // Generate the command as text.
        std::string command_text = makeSharedNetwork4ListConfig(remote_map,
                                                                server_tags);

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response =
                        impl_.run(&TestConfigCmdsDhcp4Impl::listSharedNetworks4,
                                  command_text));
        ASSERT_TRUE(response);

        // Make sure that the response indicates success.
        int rcode = 0;
        auto args = parseAnswer(rcode, response);
        int expected_rcode = (expected->empty() ?
            CONTROL_RESULT_EMPTY : CONTROL_RESULT_SUCCESS);
        EXPECT_EQ(expected_rcode, rcode);

        std::ostringstream expected_msg;
        expected_msg << expected->size()
                     << " IPv4 shared network(s) found.";
        auto msg = response->get(CONTROL_TEXT);
        ASSERT_TRUE(msg);
        EXPECT_EQ(expected_msg.str(), msg->stringValue());

        ASSERT_TRUE(args);
        ASSERT_EQ(Element::map, args->getType());
        auto count = args->get("count");
        ASSERT_TRUE(count);
        ASSERT_EQ(Element::integer, count->getType());
        EXPECT_EQ(expected->size(), count->intValue());

        // Validate returned shared networks.
        // Note this includes the metadata.
        ConstElementPtr networks = args->get("shared-networks");
        ASSERT_TRUE(networks);
        EXPECT_TRUE(isEquivalent(expected, networks))
            << "Actual: " << networks->str()
            << "\nExpected: " << expected->str();
    }

    /// @brief Negative test scenario for getting all shared networks.
    ///
    /// @param args Arguments for the command in the textual format not
    /// including "arguments".
    /// @param answer_text Expected errors message.
    void testSharedNetwork4ListFail(const std::string& args,
                                  const std::string& answer_text) {
        // Generate the command as text.
        std::string command_text = "{"
            "\"command\": \"remote-network4-list\" " + args + "}";

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response =
                        impl_.run(&TestConfigCmdsDhcp4Impl::listSharedNetworks4,
                                  command_text));
        ASSERT_TRUE(response);

        // Make sure that the response indicates error.
        int rcode = 0;
        auto answer = parseAnswer(rcode, response);
        EXPECT_EQ(CONTROL_RESULT_ERROR, rcode);

        auto msg = response->get(CONTROL_TEXT);
        ASSERT_TRUE(msg);
        EXPECT_EQ(answer_text, answer->stringValue());
    }
};

// This test verifies that it is possible to add and update shared network.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4Set) {
    {
        SCOPED_TRACE("add new shared network");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("foo"));
        shared_network->setIface("eth1");
        testSharedNetwork4Set(shared_network, "", { "server1" });
    }

    {
        SCOPED_TRACE("add another shared network");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("bar"));
        shared_network->setIface("eth0");
        testSharedNetwork4Set(shared_network);
    }

    {
        SCOPED_TRACE("update existing shared network");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("foo"));
        shared_network->setIface("eth0");
        testSharedNetwork4Set(shared_network, "", { "server1" });
    }

    {
        SCOPED_TRACE("update another shared network with specifying remote");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("bar"));
        shared_network->setIface("eth1");
        testSharedNetwork4Set(shared_network, "{ \"type\": \"mysql\" }");
    }

    {
        SCOPED_TRACE("add a shared network for multiple servers");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("xyz"));
        shared_network->setIface("eth1");
        testSharedNetwork4Set(shared_network, "", { "server1", "server2" });
    }

    {
        SCOPED_TRACE("add a shared network with unknown interface");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("noif"));
        shared_network->setIface("foobar");
        testSharedNetwork4Set(shared_network, "", { "server1" });
    }
}

// This test verifies that it is possible to add a shared network with
// DHCP options.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4SetWithOptions) {
    {
        SCOPED_TRACE("add a shared network with standard option");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("foo"));
        OptionStringPtr option(new OptionString(Option::V4, DHO_BOOT_FILE_NAME,
                                                "my-boot-file"));
        OptionDescriptor option_desc(option, false, "my-boot-file");
        option_desc.space_name_ = DHCP4_OPTION_SPACE;
        shared_network->getCfgOption()->add(option_desc, DHCP4_OPTION_SPACE);
        testSharedNetwork4Set(shared_network, "", { "server1" });
    }

    {
        SCOPED_TRACE("add a shared network with a custom option definition");
        OptionDefinitionPtr option_def(new OptionDefinition("foo",
                                                            222,
                                                            DHCP4_OPTION_SPACE,
                                                            "string"));
        ASSERT_NO_THROW(<--- There is an unknown macro here somewhere. Configuration is required. If ASSERT_NO_THROW is a macro then please configure it.
            ConfigBackendDHCPv4Mgr::instance().getPool()->
                createUpdateOptionDef4(BackendSelector(),
                                       ServerSelector::ALL(),
                                       option_def);
        );

        SharedNetwork4Ptr shared_network(new SharedNetwork4("foo"));
        OptionStringPtr option(new OptionString(Option::V4, 222, "xyz"));
        OptionDescriptor option_desc(option, false, "xyz");
        option_desc.space_name_ = DHCP4_OPTION_SPACE;
        shared_network->getCfgOption()->add(option_desc, DHCP4_OPTION_SPACE);
        testSharedNetwork4Set(shared_network, "", { "server1" });
    }
}

// This test verifies that malformed requests to add and update shared networks
// are rejected and proper error messages are returned.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4SetFail) {
    {
        SCOPED_TRACE("empty arguments");
        testSharedNetwork4SetFail("", "invalid command 'remote-network4-set': 'arguments' is empty");
    }

    {
        SCOPED_TRACE("empty shared-networks list");
        testSharedNetwork4SetFail("\"server-tags\": [ \"all\" ], "
                                  "\"shared-networks\": [ ]", "'shared-networks' list "
                                  "must include exactly one element");
    }

    {
        SCOPED_TRACE("empty shared network map");
        testSharedNetwork4SetFail("\"server-tags\": [ \"all\" ], "
                                  "\"shared-networks\": [ { } ]",
                                  "missing parameter 'name'");
    }

    {
        SCOPED_TRACE("malformed shared network");
        testSharedNetwork4SetFail("\"server-tags\": [ \"all\" ], "
                                  "\"shared-networks\": [ { \"interface\": \"eth1\" } ]",
                                  "missing parameter 'name'");
    }

    {
        SCOPED_TRACE("spurious keyword");
        testSharedNetwork4SetFail("\"server-tags\": [ \"all\" ], "
                                  "\"shared-networks\": ["
                                  "{ \"name\": \"foo\", "
                                  "\"subnet6\": [ ] } ]",
                                  "spurious 'subnet6' parameter");
    }

    {
        SCOPED_TRACE("keyword with bad type");
        testSharedNetwork4SetFail("\"server-tags\": [ \"all\" ], "
                                  "\"shared-networks\": ["
                                  "{ \"name\": \"foo\", "
                                  "\"relay\": 0 } ]",
                                  "'relay' parameter is not a map");
    }

    {
        SCOPED_TRACE("multiple shared networks");
        testSharedNetwork4SetFail("\"server-tags\": [ \"all\" ], "
                                  "\"shared-networks\": ["
                                  "{ \"name\": \"foo\" },"
                                  "{ \"name\": \"bar\" }"
                                  "]",
                                  "'shared-networks' list must include exactly one element");
    }

    {
        SCOPED_TRACE("no server tags");
        testSharedNetwork4SetFail("\"shared-networks\": [ ]",
                                  "'server-tags' parameter is mandatory");
    }

    {
        SCOPED_TRACE("unassigned server tag");
        testSharedNetwork4SetFail("\"server-tags\": [ null ], "
                                  "\"shared-networks\": ["
                                  "    { \"name\": \"bar\" } ]",
                                  "'server-tags' list must contain one or"
                                  " more server tags for the "
                                  "'remote-network4-set' command");
    }

    {
        SCOPED_TRACE("empty server tags list");
        testSharedNetwork4SetFail("\"server-tags\": [ ]",
                                  "'server-tags' list must not be empty");
    }

    {
        SCOPED_TRACE("empty server tag");
        testSharedNetwork4SetFail("\"server-tags\": [ \" \" ]",
                                  "server-tag must not be empty");
    }

    {
        SCOPED_TRACE("subnet specified in shared network");
        testSharedNetwork4SetFail("\"server-tags\": [ \"all\" ], "
                                  "\"shared-networks\": ["
                                  "    { \"name\": \"bar\","
                                  "      \"subnet4\": [ { \"subnet\": \"192.0.2.0/24\", \"id\": 1 } ]"
                                  "    }"
                                  "]",
                                  "'subnet4' must not be specified for the shared network. "
                                  "Use 'remote-subnet4-set command to add a subnet to the "
                                  "shared network");
    }

    {
        SCOPED_TRACE("unsupported backend type");
        testSharedNetwork4SetFail("\"server-tags\": [ \"all\" ], "
                                  "\"shared-networks\": ["
                                  "    { \"name\": \"foo\" }"
                                  "],"
                                  "\"remote\": {"
                                  "    \"type\": \"postgresql\""
                                  "}",
                                  "no such database found for selector: type=postgresql");
    }

    {
        SCOPED_TRACE("lacking option definition");
        testSharedNetwork4SetFail("\"server-tags\": [ \"all\" ], "
                                  "\"shared-networks\": ["
                                  "    {"
                                  "        \"name\": \"foo\","
                                  "        \"option-data\": ["
                                  "            {"
                                  "                \"name\": \"boot-file-name\","
                                  "                \"data\": \"my-boot-file\","
                                  "                \"space\": \"myspace\""
                                  "            }"
                                  "        ]"
                                  "    }"
                                  "]",
                                  "definition for the option 'myspace.boot-file-name' does not exist");
    }
}

// This test verifies that sanity checks are performed.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4SetCheckFail) {
    {
        SCOPED_TRACE("bad relay");
        std::string config =
            "\"server-tags\": [ \"all\" ], "
            "\"shared-networks\": [ {"
            "    \"name\": \"foo\","
            "    \"relay\": -1"
            "} ]";
        testSharedNetwork4SetFail(config, "'relay' parameter is not a map");
    }

    {
        SCOPED_TRACE("empty name");
        std::string config =
            "\"server-tags\": [ \"all\" ], "
            "\"shared-networks\": [ {"
            "    \"name\": \"\""
            "} ]";
        testSharedNetwork4SetFail(config,
                                  "'name' parameter must not be empty");
    }
}

// This test verifies that it is possible to delete shared network.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4Del) {
    {
        SCOPED_TRACE("add and delete new shared network");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("foo"));
        shared_network->setIface("eth1");
        testSharedNetwork4Set(shared_network);
        testSharedNetwork4Del(shared_network, 1);
    }

    {
        SCOPED_TRACE("add and delete another shared network");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("bar"));
        shared_network->setIface("eth0");
        testSharedNetwork4Set(shared_network);
        testSharedNetwork4Del(shared_network, 1);
    }

    {
        SCOPED_TRACE("delete shared network");
        // Deleting not existing is not an error.
        SharedNetwork4Ptr shared_network(new SharedNetwork4("foo"));
        shared_network->setIface("eth1");
        testSharedNetwork4Del(shared_network, 0);
    }

    {
        SCOPED_TRACE("delete another shared network with specifying remote");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("foo"));
        shared_network->setIface("eth0");
        testSharedNetwork4Set(shared_network, "{ \"type\": \"mysql\" }");
        testSharedNetwork4Del(shared_network, 1, "{ \"type\": \"mysql\" }");
    }
}

// This test verifies the different subnets actions.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4DelSubnetsAction) {
    {
        SCOPED_TRACE("default is keep");
        isc::dhcp::test::IfaceMgrTestConfig test_config(true);
        SharedNetwork4Ptr shared_network(new SharedNetwork4("one"));
        shared_network->setIface("eth0");
        Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 24, 30, 40, 60, 1));
        subnet->setIface("eth0");
        subnet->setSharedNetworkName("one");
        setSubnet(subnet);

        testSharedNetwork4Set(shared_network);
        testSharedNetwork4Del(shared_network, 1);
        auto fetched_subnets = ConfigBackendDHCPv4Mgr::instance().getPool()->
            getAllSubnets4(BackendSelector(), ServerSelector::ALL());

        auto const& index = fetched_subnets.get<SubnetSubnetIdIndexTag>();
        ASSERT_EQ(1, index.count(subnet->getID()));
        Subnet4Ptr fetched_subnet = *index.find(subnet->getID());
        SharedNetwork4Ptr network;
        fetched_subnet->getSharedNetwork(network);
        EXPECT_FALSE(network);
        EXPECT_EQ("", fetched_subnet->getSharedNetworkName());
    }

    {
        SCOPED_TRACE("set subnets-action to keep");
        isc::dhcp::test::IfaceMgrTestConfig test_config(true);
        SharedNetwork4Ptr shared_network(new SharedNetwork4("two"));
        shared_network->setIface("eth0");
        Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.3.0"), 24, 30, 40, 60, 2));
        subnet->setIface("eth0");
        subnet->setSharedNetworkName("two");
        setSubnet(subnet);
        testSharedNetwork4Set(shared_network);
        testSharedNetwork4Del(shared_network, 1, "", "keep");
        auto fetched_subnets = ConfigBackendDHCPv4Mgr::instance().getPool()->
            getAllSubnets4(BackendSelector(), ServerSelector::ALL());
        auto const& index = fetched_subnets.get<SubnetSubnetIdIndexTag>();
        EXPECT_EQ(1, index.count(subnet->getID()));
    }

    {
        SCOPED_TRACE("set subnets-action to delete");
        isc::dhcp::test::IfaceMgrTestConfig test_config(true);
        SharedNetwork4Ptr shared_network(new SharedNetwork4("three"));
        shared_network->setIface("eth0");
        Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.4.0"), 24, 30, 40, 60, 3));
        subnet->setIface("eth0");
        subnet->setSharedNetworkName("three");
        setSubnet(subnet);
        testSharedNetwork4Set(shared_network);
        testSharedNetwork4Del(shared_network, 1, "", "delete");
        auto fetched_subnets = ConfigBackendDHCPv4Mgr::instance().getPool()->
            getAllSubnets4(BackendSelector(), ServerSelector::ALL());
        auto const& index = fetched_subnets.get<SubnetSubnetIdIndexTag>();
        EXPECT_EQ(0, index.count(subnet->getID()));
    }
}

// This test verifies that malformed requests to delete shared networks
// are rejected and proper error messages are returned.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4DelFail) {
    {
        SCOPED_TRACE("empty arguments");
        testSharedNetwork4DelFail("", "invalid command 'remote-network4-del': 'arguments' is empty");
    }

    {
        SCOPED_TRACE("empty shared-networks list");
        testSharedNetwork4DelFail("\"shared-networks\": [ ]",
                                  "'shared-networks' list "
                                  "must include exactly one element");
    }

    {
        SCOPED_TRACE("empty shared network map");
        testSharedNetwork4DelFail("\"shared-networks\": [ { } ]",
                                  "missing 'name' parameter");
    }

    {
        SCOPED_TRACE("malformed shared network");
        testSharedNetwork4DelFail("\"shared-networks\": [ { \"interface\": \"eth1\" } ]",
                                  "missing 'name' parameter");
    }

    {
        SCOPED_TRACE("multiple shared networks");
        testSharedNetwork4DelFail("\"shared-networks\": ["
                                  "{ \"name\": \"foo\" },"
                                  "{ \"name\": \"bar\" }"
                                  "]",
                                  "'shared-networks' list must include exactly one element");
    }

    {
        SCOPED_TRACE("server tags");
        testSharedNetwork4DelFail("\"server-tags\": [ ]",
                                  "'server-tags' parameter is forbidden");
    }

    {
        SCOPED_TRACE("unsupported backend type");
        testSharedNetwork4DelFail("\"shared-networks\": ["
                                  "    { \"name\": \"foo\" }"
                                  "],"
                                  "\"remote\": {"
                                  "    \"type\": \"postgresql\""
                                  "}",
                                  "no such database found for selector: type=postgresql");
    }

    {
        SCOPED_TRACE("unsupported subnets action");
        testSharedNetwork4DelFail("\"shared-networks\": ["
                                  "    { \"name\": \"foo\" }"
                                  "],"
                                  "\"subnets-action\": \"foo\"",
                                  "unknown 'subnets-action' parameter value:"
                                  " 'foo', possible vales are 'keep'"
                                  " (default) and 'delete'");
    }
}

// This test verifies that it is possible to get shared network.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4Get) {
    {
        SCOPED_TRACE("add and get new shared network");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("foo"));
        shared_network->setIface("eth1");
        shared_network->setServerTag("server1");
        testSharedNetwork4Set(shared_network, "", { "server1" });
        testSharedNetwork4Get(shared_network, shared_network);
    }

    {
        SCOPED_TRACE("add and get another shared network");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("bar"));
        shared_network->setIface("eth0");
        shared_network->setServerTag(ServerTag::ALL);
        testSharedNetwork4Set(shared_network);
        testSharedNetwork4Get(shared_network, shared_network);
    }

    {
        SCOPED_TRACE("get no shared network");
        // Getting not existing is not an error.
        SharedNetwork4Ptr shared_network(new SharedNetwork4("yzt"));
        shared_network->setIface("eth1");
        shared_network->setServerTag(ServerTag::ALL);
        testSharedNetwork4Get(shared_network, SharedNetwork4Ptr());
    }

    {
        SCOPED_TRACE("add and get a shared network with two subnets");
        isc::dhcp::test::IfaceMgrTestConfig test_config(true);
        SharedNetwork4Ptr shared_network(new SharedNetwork4("two"));
        shared_network->setIface("eth0");
        shared_network->setServerTag(ServerTag::ALL);
        Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 24, 30, 40, 60, 1));
        subnet->setIface("eth0");
        subnet->setSharedNetworkName("two");
        setSubnet(subnet);
        subnet.reset(new Subnet4(IOAddress("192.0.3.0"), 24, 30, 40, 60, 2));
        subnet->setIface("eth0");
        subnet->setSharedNetworkName("two");
        setSubnet(subnet);
        testSharedNetwork4Set(shared_network);
        testSharedNetwork4Get(shared_network, shared_network);
    }

    {
        SCOPED_TRACE("add and get another shared network with specifying remote");
        SharedNetwork4Ptr shared_network(new SharedNetwork4("abc"));
        shared_network->setIface("eth0");
        shared_network->setServerTag(ServerTag::ALL);
        testSharedNetwork4Set(shared_network, "{ \"type\": \"mysql\" }");
        testSharedNetwork4Get(shared_network, shared_network, "",
                              "{ \"type\": \"mysql\" }");
    }
}

// This test verifies that it is possible to control if the returned
// shared network contains subnets.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4DelSubnetsInclude) {
    isc::dhcp::test::IfaceMgrTestConfig test_config(true);
    SharedNetwork4Ptr shared_network(new SharedNetwork4("two"));
    shared_network->setIface("eth0");
    shared_network->setServerTag(ServerTag::ALL);
    Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 24, 30, 40, 60, 1));
    subnet1->setIface("eth0");
    subnet1->setSharedNetworkName("two");
    setSubnet(subnet1);
    Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.3.0"), 24, 30, 40, 60, 2));
    subnet2->setIface("eth0");
    subnet2->setSharedNetworkName("two");
    setSubnet(subnet2);
    testSharedNetwork4Set(shared_network);

    {
        SCOPED_TRACE("default subnets-include");
        testSharedNetwork4Get(shared_network, shared_network, "");
    }

    {
        SCOPED_TRACE("set subnets-include to no");
        testSharedNetwork4Get(shared_network, shared_network, "no");
    }

    {
        SCOPED_TRACE("set subnets-include to full");
        SharedNetwork4Ptr exp_shared_network(new SharedNetwork4("two"));
        exp_shared_network->setIface("eth0");
        exp_shared_network->setServerTag(ServerTag::ALL);
        Subnet4Ptr exp_subnet1(new Subnet4(IOAddress("192.0.2.0"), 24, 30, 40, 60, 1));
        exp_subnet1->setIface("eth0");
        exp_shared_network->add(exp_subnet1);
        Subnet4Ptr exp_subnet2(new Subnet4(IOAddress("192.0.3.0"), 24, 30, 40, 60, 2));
        exp_subnet2->setIface("eth0");
        exp_shared_network->add(exp_subnet2);
        testSharedNetwork4Get(shared_network, exp_shared_network, "full");
    }
}

// This test verifies that malformed requests to get shared networks
// are rejected and proper error messages are returned.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4GetFail) {
    {
        SCOPED_TRACE("empty arguments");
        testSharedNetwork4GetFail("", "invalid command 'remote-network4-get': 'arguments' is empty");
    }

    {
        SCOPED_TRACE("empty shared-networks list");
        testSharedNetwork4GetFail("\"shared-networks\": [ ]",
                                  "'shared-networks' list "
                                  "must include exactly one element");
    }

    {
        SCOPED_TRACE("empty shared network map");
        testSharedNetwork4GetFail("\"shared-networks\": [ { } ]",
                                  "missing 'name' parameter");
    }

    {
        SCOPED_TRACE("malformed shared network");
        testSharedNetwork4GetFail("\"shared-networks\": [ { \"interface\": \"eth1\" } ]",
                                  "missing 'name' parameter");
    }

    {
        SCOPED_TRACE("multiple shared networks");
        testSharedNetwork4GetFail("\"shared-networks\": ["
                                  "{ \"name\": \"foo\" },"
                                  "{ \"name\": \"bar\" }"
                                  "]",
                                  "'shared-networks' list must include exactly one element");
    }

    {
        SCOPED_TRACE("server tags");
        testSharedNetwork4GetFail("\"server-tags\": [ ]",
                                  "'server-tags' parameter is forbidden");
    }

    {
        SCOPED_TRACE("unsupported backend type");
        testSharedNetwork4GetFail("\"shared-networks\": ["
                                  "    { \"name\": \"foo\" }"
                                  "],"
                                  "\"remote\": {"
                                  "    \"type\": \"postgresql\""
                                  "}",
                                  "no such database found for selector: type=postgresql");
    }

    {
        SCOPED_TRACE("unsupported subnets-include value");
        testSharedNetwork4GetFail("\"shared-networks\": ["
                                  "    { \"name\": \"foo\" }"
                                  "],"
                                  "\"subnets-include\": \"foo\"",
                                  "unknown 'subnets-include' parameter value:"
                                  " 'foo', possible values are 'no' (default)"
                                  " and 'full'");
    }
}

// This test verifies that it is possible to get all shared networks.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4List) {
    // Empty.
    ElementPtr expected = Element::createList();
    ElementPtr expected_all = Element::createList();
    ElementPtr expected_unassigned = Element::createList();
    testSharedNetwork4List(expected);
    testSharedNetwork4List(expected_all);
    testSharedNetwork4List(expected_unassigned);

    // Add a shared network.
    SharedNetwork4Ptr shared_network(new SharedNetwork4("foo"));
    shared_network->setIface("eth1");
    shared_network->setServerTag("server1");
    testSharedNetwork4Set(shared_network, "", { "server1" });
    ElementPtr network_map = Element::createMap();
    network_map->set("name", Element::create("foo"));
    network_map->set("metadata", createMetadata("server1"));
    expected->add(network_map);
    testSharedNetwork4List(expected, "", { "server1" });

    // Add a second shared network.
    shared_network.reset(new SharedNetwork4("bar"));
    shared_network->setIface("eth0");
    shared_network->setServerTag(ServerTag::ALL);
    testSharedNetwork4Set(shared_network);
    network_map = Element::createMap();
    network_map->set("name", Element::create("bar"));
    network_map->set("metadata", createMetadata(ServerTag::ALL));
    expected->add(network_map);
    expected_all->add(network_map);
    testSharedNetwork4List(expected, "", { "server1" });
    testSharedNetwork4List(expected_all);

    // With another server only shared networks belonging to all are returned.
    testSharedNetwork4List(expected_all, "", { "server2" });

    // Try with a remote.
    shared_network.reset(new SharedNetwork4("abc"));
    shared_network->setIface("eth0");
    shared_network->setServerTag(ServerTag::ALL);
    testSharedNetwork4Set(shared_network, "{ \"type\": \"mysql\" }");
    network_map = Element::createMap();
    network_map->set("name", Element::create("abc"));
    network_map->set("metadata", createMetadata(ServerTag::ALL));
    expected->add(network_map);
    expected_all->add(network_map);
    testSharedNetwork4List(expected, "{ \"type\": \"mysql\" }", { "server1" });
    testSharedNetwork4List(expected_all, "{ \"type\": \"mysql\" }");

    // Add a third shared network in another server.
    shared_network.reset(new SharedNetwork4("xyz"));
    shared_network->setIface("eth0");
    shared_network->setServerTag("server2");
    testSharedNetwork4Set(shared_network, "", { "server2" });
    // Not in server1 so not yet expected.
    testSharedNetwork4List(expected, "", { "server1" });
    network_map = Element::createMap();
    network_map->set("name", Element::create("xyz"));
    network_map->set("metadata", createMetadata("server2"));
    expected->add(network_map);
    testSharedNetwork4List(expected, "", { "server1", "server2" });

    // Now try unassigned.
    testSharedNetwork4List(expected_unassigned, "", { });
    shared_network.reset(new SharedNetwork4("uvw"));
    shared_network->setIface("eth0");
    // Adds it directly to the backend (no command yet to do this).
    setSharedNetwork(shared_network);
    // Not visible by a server.
    testSharedNetwork4List(expected, "", { "server1", "server2" });
    network_map = Element::createMap();
    network_map->set("name", Element::create("uvw"));
    expected_unassigned->add(network_map);
    network_map->set("metadata", createMetadata(std::set<std::string>()));
    testSharedNetwork4List(expected_unassigned, "", { });
}

// This test verifies that malformed requests to get all shared networks
// are rejected and proper error messages are returned.
TEST_F(SharedNetwork4CmdsTest, sharedNetwork4ListFail) {
    {
        SCOPED_TRACE("empty arguments");
        testSharedNetwork4ListFail(", \"arguments\": { }",
                                   "invalid command 'remote-network4-list': 'arguments' is empty");
    }

    {
        SCOPED_TRACE("no server tags");
        testSharedNetwork4ListFail(", \"arguments\": { \"remote\": { } }",
                                   "'server-tags' parameter is mandatory");
    }

    {
        SCOPED_TRACE("empty server tags list");
        testSharedNetwork4ListFail(", \"arguments\": { \"server-tags\": [ ] }",
                                   "'server-tags' list must not be empty");
    }

    {
        SCOPED_TRACE("bad formed unassigned");
        testSharedNetwork4ListFail(", \"arguments\": { "
                                   "\"server-tags\": [ null, \"all\" ] }",
                                   "when the 'server-tags' list contains "
                                   "multiple elements all these elements must "
                                   "have the string type");
    }

    {
        SCOPED_TRACE("unsupported backend type");
        testSharedNetwork4ListFail(", \"arguments\": { "
                                   "\"server-tags\": [ \"all\" ], "
                                   "\"remote\": {"
                                   "    \"type\": \"postgresql\""
                                   "} }",
                                   "no such database found for selector: "
                                   "type=postgresql");
    }
}

}