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
// Copyright (C) 2021-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 <dhcp/option_int.h>
#include <dhcp/option_string.h>
#include <database/backend_selector.h>
#include <dhcpsrv/config_backend_dhcp6_mgr.h>
#include <boost/make_shared.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

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

namespace {

/// @brief Test fixture class for commands pertaining to client classes.
class ClientClass6CmdsTest : public ConfigCmdsDhcp6Test {
public:

    /// @brief Adds unassigned client class to the configuration backend.
    ///
    /// @param client_class pointer to the client class to be stored.
    void setClientClass(const ClientClassDefPtr& client_class) {
        ConfigBackendDHCPv6Mgr::instance().getPool()->
            createUpdateClientClass6(BackendSelector(),
                                     ServerSelector::UNASSIGNED(),
                                     client_class,
                                     "");
    }

    /// @brief Returns remote-client-class6-set command as text.
    ///
    /// @param client_class pointer to a client class to be used in the
    /// command.
    /// @param follow_class_name name value of the follow-class-name parameter.
    /// If the string is equal to "null", the null JSON value is assigned.
    /// @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 makeClientClass6SetConfig(const ClientClassDefPtr& client_class,
                                          const std::string& follow_class_name,
                                          const std::string& remote_map,
                                          const std::set<std::string>& server_tags) {
        auto class_element = client_class->toElement();
        std::string config = "{"
            "\"command\": \"remote-class6-set\","
            "\"arguments\": {"
            "    \"client-classes\": [";

        // Include follow-class-name if specified.
        if (!follow_class_name.empty()) {
            if (follow_class_name == "null") {
                class_element->set("follow-class-name", NullElement::create());
            } else {
                class_element->set("follow-class-name", Element::create(follow_class_name));
            }
        }

        config += class_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 Returns remote-class6-del command as text.
    ///
    /// @param client_class a pointer to a client class 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.
    ///
    /// @return Command in the textual format.
    std::string makeClientClass6DelConfig(const ClientClassDefPtr& client_class,
                                          const std::string& remote_map) {
        std::string config = "{"
            "\"command\": \"remote-class6-del\","
            "\"arguments\": {"
            "    \"client-classes\": [ { ";

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

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

    /// @brief Returns remote-class6-get command as text.
    ///
    /// @param client_class pointer to the client class 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.
    ///
    /// @return Command in the textual format.
    std::string makeClientClass6GetConfig(const ClientClassDefPtr& client_class,
                                          const std::string& remote_map) {
        std::string config = "{"
            "\"command\": \"remote-class6-get\","
            "\"arguments\": {"
            "    \"client-classes\": [ { ";

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

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

        // No server tags.

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

    /// @brief Returns remote-class6-get-all 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 makeClientClass6GetAllConfig(const std::string& remote_map,
                                             const std::set<std::string>& server_tags) {
        std::string config = "{"
            "\"command\": \"remote-class6-get-all\""
            ", \"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 setting or updating client class
    ///
    /// @param client_class pointer to a client class to be created or updated.
    /// @param follow_class_name name value of the follow-class-name parameter.
    /// If the string is equal to "null", the null JSON value is assigned.
    /// @param expected_position position at which the client class should be
    /// after the update.
    /// @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 testClientClass6Set(const ClientClassDefPtr& client_class,
                             const std::string& follow_class_name = "",
                             const int expected_position = -1,
                             const std::string& remote_map = "",
                             const std::set<std::string>& server_tags = { ServerTag::ALL }) {
        auto command_text = makeClientClass6SetConfig(client_class, follow_class_name,
                                                      remote_map, server_tags);

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

        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("DHCPv6 client class successfully set.", msg->stringValue());

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

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

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

        auto client_class_0_name = client_class_0->get("name");
        ASSERT_TRUE(client_class_0_name);
        EXPECT_EQ(Element::string, client_class_0_name->getType());
        EXPECT_EQ(client_class->getName(), client_class_0_name->stringValue());

        auto fetched_client_class = ConfigBackendDHCPv6Mgr::instance().getPool()->
            getClientClass6(BackendSelector(),
                            ServerSelector::ONE(*server_tags.begin()),
                            client_class->getName());
        ASSERT_TRUE(fetched_client_class);
        auto metadata = fetched_client_class->getMetadata();
        ASSERT_TRUE(metadata);
        auto expected_metadata = createMetadata(server_tags);
        EXPECT_EQ(expected_metadata->str(), metadata->str());

        if (client_class->getCfgOption() && !client_class->getCfgOption()->empty()) {
            EXPECT_TRUE(isEquivalent(fetched_client_class->getCfgOption()->toElement(),
                                     client_class->getCfgOption()->toElement()));
        }

        if (expected_position >= 0) {
            auto all_classes = ConfigBackendDHCPv6Mgr::instance().getPool()->
                getAllClientClasses6(BackendSelector(),
                                     ServerSelector::ONE(*server_tags.begin()));
            auto class_list = all_classes.getClasses();
            EXPECT_EQ((*(class_list->begin() + expected_position))->getName(),
                      client_class->getName());
        }
    }

    /// @brief Negative test scenario for setting or updating a client class.
    ///
    /// @param args arguments for the command in the textual format.
    /// @param answer_text expected error message.
    void testClientClass6SetFail(const std::string& args,
                                 const std::string& answer_text) {
        // Generate the command as text.
        std::string command_text = "{"
            "\"command\": \"remote-class6-set\","
            "\"arguments\": {" + args + "}"
        "}";

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::setClientClass6,
                                             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(0, msg->stringValue().find(answer_text))
            << "expected: " << answer_text
            << ", found: " << msg->stringValue();
    }

    /// @brief Positive test scenario for deleting a client class.
    ///
    /// @param client_class pointer to a client class to be deleted.
    /// @param expected_count Expected number of deleted client classes..
    /// @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 testClientClass6Del(const ClientClassDefPtr& client_class,
                             unsigned expected_count,
                             const std::string& remote_map = "") {

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

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::delClientClass6,
                                             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 << " DHCPv6 client class(es) 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 client class is not in the database.
        auto fetched_client_class = ConfigBackendDHCPv6Mgr::instance().getPool()->
            getClientClass6(BackendSelector(), ServerSelector::ANY(), client_class->getName());
        ASSERT_FALSE(fetched_client_class);
    }

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

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::delClientClass6,
                                             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 Positive test scenario for getting a client class.
    ///
    /// @param client_class a pointer to a client class to be returned.
    /// @param expected_client_class pointer to the expected client class.
    /// @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 testClientClass6Get(const ClientClassDefPtr& client_class,
                             const ClientClassDefPtr& expected_client_class,
                             const std::string& remote_map = "") {
        // Generate the command as text.
        std::string command_text = makeClientClass6GetConfig(client_class,
                                                             remote_map);

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

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

        std::ostringstream expected_msg;
        expected_msg << "DHCPv6 client class '" << client_class->getName()
                     << "' " << (!expected_client_class ? "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 client class.
        ConstElementPtr client_classes = args->get("client-classes");
        ASSERT_TRUE(client_classes);
        ASSERT_EQ(Element::list, client_classes->getType());
        ConstElementPtr count = args->get("count");
        ASSERT_TRUE(count);
        ASSERT_EQ(Element::integer, count->getType());

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

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

            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 client class.
    ///
    /// @param args arguments for the command in the textual format.
    /// @param answer_text expected errors message.
    void testClientClass6GetFail(const std::string& args,
                                 const std::string& answer_text) {
        // Generate the command as text.
        std::string command_text = "{"
            "\"command\": \"remote-class6-get\","
            "\"arguments\": {" + args + "}"
        "}";

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response =
                        impl_.run(&TestConfigCmdsDhcp6Impl::getClientClass6,
                                  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 Positive test scenario for getting all client classes.
    ///
    /// @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 testClientClass6GetAll(ConstElementPtr expected,
                                const std::string& remote_map = "",
                                const std::set<std::string>& server_tags = { ServerTag::ALL }) {
        // Sanity check.
        ASSERT_TRUE(expected) << " bad test";
        ASSERT_EQ(Element::list, expected->getType()) << " bad test";

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

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response =
                        impl_.run(&TestConfigCmdsDhcp6Impl::getAllClientClasses6,
                                  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()
                     << " DHCPv6 client class(es) 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 client classes and the metadata.
        ConstElementPtr networks = args->get("client-classes");
        ASSERT_TRUE(networks);
        EXPECT_TRUE(isEquivalent(expected, networks))
            << "Actual: " << networks->str()
            << "\nExpected: " << expected->str();
    }

    /// @brief Negative test scenario for getting all client classes.
    ///
    /// @param args arguments for the command in the textual format.
    /// @param answer_text expected errors message.
    void testClientClass6GetAllFail(const std::string& args,
                                    const std::string& answer_text) {
        // Generate the command as text.
        std::string command_text = "{"
            "\"command\": \"remote-class6-get-all\" " + args + "}";

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response =
                        impl_.run(&TestConfigCmdsDhcp6Impl::getAllClientClasses6,
                                  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 client class.
TEST_F(ClientClass6CmdsTest, clientClass6Set) {
    ExpressionPtr match_expr = boost::make_shared<Expression>();
    CfgOptionPtr cfg_option = boost::make_shared<CfgOption>();

    {
        SCOPED_TRACE("add new client class");
        auto client_class = boost::make_shared<ClientClassDef>("foo", match_expr, cfg_option);
        testClientClass6Set(client_class);
    }

    {
        SCOPED_TRACE("add another client class");
        auto client_class = boost::make_shared<ClientClassDef>("bar", match_expr, cfg_option);
        client_class->setTest("member('foo')");
        testClientClass6Set(client_class, "foo", 1);
    }

    {
        SCOPED_TRACE("update existing client class");
        auto client_class = boost::make_shared<ClientClassDef>("foo", match_expr, cfg_option);
        client_class->setTest("member('KNOWN')");
        testClientClass6Set(client_class, "null", 0);
    }

    {
        SCOPED_TRACE("update another client class with specifying remote");
        auto client_class = boost::make_shared<ClientClassDef>("bar", match_expr, cfg_option);
        testClientClass6Set(client_class, "", 1, "{ \"type\": \"mysql\" }");
    }

    {
        SCOPED_TRACE("add a client class for multiple servers");
        auto client_class = boost::make_shared<ClientClassDef>("xyz", match_expr, cfg_option);
        testClientClass6Set(client_class, "foo", 1, "", { "server1", "server2" });
    }
}

// This test verifies that the options can be specified with the classes.
TEST_F(ClientClass6CmdsTest, clientClass6SetWithOptions) {
    ExpressionPtr match_expr = boost::make_shared<Expression>();
    CfgOptionPtr cfg_option = boost::make_shared<CfgOption>();

    {
        SCOPED_TRACE("add a client class with a standard option");

        // Add a class with a standard option at class level to test
        // that options can be added together with a class to the
        // config backend.
        auto client_class = boost::make_shared<ClientClassDef>("my-class", match_expr, cfg_option);
        OptionStringPtr option(new OptionString(Option::V6, D6O_NIS_SERVERS, "2001:db8:1::1"));
        OptionDescriptor option_desc(option, false, false, "2001:db8:1::1");
        option_desc.space_name_ = DHCP6_OPTION_SPACE;
        client_class->getCfgOption()->add(option_desc, DHCP6_OPTION_SPACE);

        testClientClass6Set(client_class);
    }

    {
        SCOPED_TRACE("add a client class with an option definition specified at global scope");
        auto client_class = boost::make_shared<ClientClassDef>("my-class", match_expr, cfg_option);

        // Define option definition for an option 1001 at the global level and add to the
        // config backend.
        OptionDefinitionPtr option_def(new OptionDefinition("foo", 1001, DHCP6_OPTION_SPACE, "uint32"));
        ASSERT_NO_THROW(<--- There is an unknown macro here somewhere. Configuration is required. If ASSERT_NO_THROW is a macro then please configure it.
            ConfigBackendDHCPv6Mgr::instance().getPool()->
                createUpdateOptionDef6(BackendSelector(),
                                       ServerSelector::ALL(),
                                       option_def);
        );

        // Define an option using this definition.
        OptionUint32Ptr option(new OptionUint32(Option::V6, 1001, 123));
        OptionDescriptor option_desc(option, false, false, "123");
        option_desc.space_name_ = DHCP6_OPTION_SPACE;
        client_class->getCfgOption()->add(option_desc, DHCP6_OPTION_SPACE);

        testClientClass6Set(client_class);
    }
}

// This test verifies that malformed requests to add and update client classes
// are rejected and proper error messages are returned.
TEST_F(ClientClass6CmdsTest, clientClass6SetFail) {
    {
        SCOPED_TRACE("empty arguments");
        testClientClass6SetFail("", "invalid command 'remote-class6-set': 'arguments' is empty");
    }

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

    {
        SCOPED_TRACE("empty client class map");
        testClientClass6SetFail("\"server-tags\": [ \"all\" ], "
                                "\"client-classes\": [ { } ]",
                                "missing parameter 'name'");
    }

    {
        SCOPED_TRACE("malformed client class");
        testClientClass6SetFail("\"server-tags\": [ \"all\" ], "
                                "\"client-classes\": [ { \"test\": \"member('KNOWN')\" } ]",
                                "missing parameter 'name'");
    }

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

    {
        SCOPED_TRACE("no server tags");
        testClientClass6SetFail("\"client-classes\": [ ]",
                                "'server-tags' parameter is mandatory");
    }

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

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

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

    {
        SCOPED_TRACE("follow class name is not a string");
        testClientClass6SetFail("\"server-tags\": [ \"all\" ], "
                                "\"client-classes\": [ { "
                                "    \"name\": \"foo\","
                                "    \"test\": \"member('KNOWN')\","
                                "    \"follow-class-name\": 1"
                                "} ]",
                                "invalid type specified for parameter 'follow-class-name'");
    }

    {
        SCOPED_TRACE("lacking option definition");
        testClientClass6SetFail("\"server-tags\": [ \"all\" ], "
                                "\"client-classes\": [ { "
                                "    \"name\": \"foo\","
                                "    \"test\": \"member('KNOWN')\","
                                "    \"option-data\": ["
                                "        {"
                                "            \"name\": \"bootfile-url\","
                                "            \"data\": \"my-boot-file\","
                                "            \"space\": \"myspace\""
                                "        }"
                                "    ]"
                                "} ]",
                                "definition for the option 'myspace.bootfile-url' does not exist");
    }
}

// This test verifies that it is possible to delete a client class.
TEST_F(ClientClass6CmdsTest, clientClass6Del) {
    ExpressionPtr match_expr = boost::make_shared<Expression>();
    CfgOptionPtr cfg_option = boost::make_shared<CfgOption>();

    {
        SCOPED_TRACE("add and delete new client class");
        auto client_class = boost::make_shared<ClientClassDef>("foo", match_expr, cfg_option);
        testClientClass6Set(client_class);
        testClientClass6Del(client_class, 1);
    }

    {
        SCOPED_TRACE("add and delete another client class");
        auto client_class = boost::make_shared<ClientClassDef>("bar", match_expr, cfg_option);
        testClientClass6Set(client_class);
        testClientClass6Del(client_class, 1);
    }

    {
        SCOPED_TRACE("delete client class");
        // Deleting not existing is not an error.
        auto client_class = boost::make_shared<ClientClassDef>("foo", match_expr, cfg_option);
        testClientClass6Del(client_class, 0);
    }

    {
        SCOPED_TRACE("delete another client class with specifying remote");
        auto client_class = boost::make_shared<ClientClassDef>("foo", match_expr, cfg_option);
        testClientClass6Set(client_class, "", -1, "{ \"type\": \"mysql\" }");
        testClientClass6Del(client_class, 1, "{ \"type\": \"mysql\" }");
    }
}

// This test verifies that malformed requests to delete client classes
// are rejected and proper error messages are returned.
TEST_F(ClientClass6CmdsTest, clientClass6DelFail) {
    {
        SCOPED_TRACE("empty arguments");
        testClientClass6DelFail("", "invalid command 'remote-class6-del': 'arguments' is empty");
    }

    {
        SCOPED_TRACE("empty client-classes list");
        testClientClass6DelFail("\"client-classes\": [ ]",
                                "'client-classes' list "
                                "must include exactly one element");
    }

    {
        SCOPED_TRACE("empty client class map");
        testClientClass6DelFail("\"client-classes\": [ { } ]",
                                "missing 'name' parameter");
    }

    {
        SCOPED_TRACE("malformed client class");
        testClientClass6DelFail("\"client-classes\": [ { \"test\": \"member('KNOWN')\" } ]",
                                "missing 'name' parameter");
    }

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

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

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

// This test verifies that it is possible to get client class
TEST_F(ClientClass6CmdsTest, clientClass6Get) {
    ExpressionPtr match_expr = boost::make_shared<Expression>();
    CfgOptionPtr cfg_option = boost::make_shared<CfgOption>();
    CfgOptionDefPtr cfg_option_def = boost::make_shared<CfgOptionDef>();

    {
        SCOPED_TRACE("add and get new client class");
        auto client_class = boost::make_shared<ClientClassDef>("foo", match_expr, cfg_option);
        client_class->setCfgOptionDef(cfg_option_def);
        client_class->setServerTag("server1");
        testClientClass6Set(client_class, "", -1, "", { "server1" });
        testClientClass6Get(client_class, client_class);
    }

    {
        SCOPED_TRACE("add and get another client class");
        auto client_class = boost::make_shared<ClientClassDef>("bar", match_expr, cfg_option);
        client_class->setCfgOptionDef(cfg_option_def);
        client_class->setServerTag(ServerTag::ALL);
        testClientClass6Set(client_class);
        testClientClass6Get(client_class, client_class);
    }

    {
        SCOPED_TRACE("get no client class");
        // Getting not existing is not an error.
        auto client_class = boost::make_shared<ClientClassDef>("baz", match_expr, cfg_option);
        client_class->setCfgOptionDef(cfg_option_def);
        client_class->setServerTag(ServerTag::ALL);
        testClientClass6Get(client_class, ClientClassDefPtr());
    }

    {
        SCOPED_TRACE("add and get client class with specifying remote");
        auto client_class = boost::make_shared<ClientClassDef>("abc", match_expr, cfg_option);
        client_class->setCfgOptionDef(cfg_option_def);
        client_class->setServerTag(ServerTag::ALL);
        testClientClass6Set(client_class, "", -1, "{ \"type\": \"mysql\" }");
        testClientClass6Get(client_class, client_class, "{ \"type\": \"mysql\" }");
    }
}

// This test verifies that malformed requests to get a client class are
// rejected and proper error messages are returned.
TEST_F(ClientClass6CmdsTest, clientClass6GetFail) {
    {
        SCOPED_TRACE("empty arguments");
        testClientClass6GetFail("", "invalid command 'remote-class6-get': 'arguments' is empty");
    }

    {
        SCOPED_TRACE("empty client-classes list");
        testClientClass6GetFail("\"client-classes\": [ ]",
                                "'client-classes' list "
                                "must include exactly one element");
    }

    {
        SCOPED_TRACE("empty client class map");
        testClientClass6GetFail("\"client-classes\": [ { } ]",
                                "missing 'name' parameter");
    }

    {
        SCOPED_TRACE("malformed client class");
        testClientClass6GetFail("\"client-classes\": [ { \"test\": \"member('KNOWN')\" } ]",
                                "missing 'name' parameter");
    }

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

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

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

// This test verifies that it is possible to get all client classes.
TEST_F(ClientClass6CmdsTest, clientClass6GetAll) {
    ExpressionPtr match_expr = boost::make_shared<Expression>();
    CfgOptionPtr cfg_option = boost::make_shared<CfgOption>();
    CfgOptionDefPtr cfg_option_def = boost::make_shared<CfgOptionDef>();

    ElementPtr expected = Element::createList();
    ElementPtr expected_all = Element::createList();
    ElementPtr expected_unassigned = Element::createList();

    {
        SCOPED_TRACE("no client classes in the database");
        testClientClass6GetAll(expected);
        testClientClass6GetAll(expected_all);
        testClientClass6GetAll(expected_unassigned);
    }

    {
        SCOPED_TRACE("add first class for server1");
        auto client_class = boost::make_shared<ClientClassDef>("foo", match_expr, cfg_option);
        client_class->setCfgOptionDef(cfg_option_def);
        client_class->setServerTag("server1");
        testClientClass6Set(client_class, "", -1, "", { "server1" });
        auto client_class_map = client_class->toElement();
        client_class_map->set("metadata", createMetadata("server1"));
        expected->add(client_class_map);
        testClientClass6GetAll(expected, "", { "server1" });
    }

    {
        SCOPED_TRACE("add second class for all servers");
        auto client_class = boost::make_shared<ClientClassDef>("bar", match_expr, cfg_option);
        client_class->setCfgOptionDef(cfg_option_def);
        client_class->setServerTag(ServerTag::ALL);
        testClientClass6Set(client_class);
        auto client_class_map = client_class->toElement();
        client_class_map->set("metadata", createMetadata(ServerTag::ALL));
        expected->add(client_class_map);
        expected_all->add(client_class_map);
        testClientClass6GetAll(expected, "", { "server1" });
        testClientClass6GetAll(expected_all);

        // With another server only client class belonging to all is returned.
        testClientClass6GetAll(expected_all, "", { "server2" });
    }

    {
        SCOPED_TRACE("add third class and use remote to fetch it");
        auto client_class = boost::make_shared<ClientClassDef>("abc", match_expr, cfg_option);
        client_class->setCfgOptionDef(cfg_option_def);
        client_class->setServerTag(ServerTag::ALL);
        testClientClass6Set(client_class, "", -1, "{ \"type\": \"mysql\" }");
        auto client_class_map = client_class->toElement();
        client_class_map->set("metadata", createMetadata(ServerTag::ALL));
        expected->add(client_class_map);
        expected_all->add(client_class_map);
        testClientClass6GetAll(expected, "{ \"type\": \"mysql\" }", { "server1" });
        testClientClass6GetAll(expected_all, "{ \"type\": \"mysql\" }");
    }

    {
        SCOPED_TRACE("get classes for multiple server tags");
        auto client_class = boost::make_shared<ClientClassDef>("xyz", match_expr, cfg_option);
        client_class->setCfgOptionDef(cfg_option_def);
        client_class->setServerTag("server2");
        testClientClass6Set(client_class, "", -1, "", { "server2" });
        testClientClass6GetAll(expected, "", { "server1" });
        auto client_class_map = client_class->toElement();
        client_class_map->set("metadata", createMetadata("server2"));
        expected->add(client_class_map);
        testClientClass6GetAll(expected, "", { "server1", "server2" });
    }

    {
        SCOPED_TRACE("get unassigned client classes");
        testClientClass6GetAll(expected_unassigned, "", { });
        auto client_class = boost::make_shared<ClientClassDef>("uvw", match_expr, cfg_option);
        client_class->setCfgOptionDef(cfg_option_def);
        // Add it directly to the backend (no command yet to do this).
        setClientClass(client_class);
        // Not visible by a server.
        testClientClass6GetAll(expected, "", { "server1", "server2" });
        auto client_class_map = client_class->toElement();
        client_class_map->set("metadata", createMetadata(std::set<std::string>()));
        expected_unassigned->add(client_class_map);
        testClientClass6GetAll(expected_unassigned, "", { });
    }
}

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

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

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

    {
        SCOPED_TRACE("bad formed unassigned");
        testClientClass6GetAllFail(", \"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");
        testClientClass6GetAllFail(", \"arguments\": { "
                                   "\"server-tags\": [ \"all\" ], "
                                   "\"remote\": {"
                                   "    \"type\": \"postgresql\""
                                   "} }",
                                   "no such database found for selector: "
                                   "type=postgresql");
    }
}

}