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
// Copyright (C) 2019-2023 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/dhcp6.h>
#include <dhcp/option_int.h>
#include <dhcp/option_string.h>
#include <database/backend_selector.h>
#include <dhcpsrv/cfg_option.h>
#include <dhcpsrv/config_backend_dhcp6_mgr.h>
#include <dhcpsrv/parsers/option_data_parser.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 DHCP options.
class OptionPool6CmdsTest : public ConfigCmdsDhcp6Test {
public:

    /// @brief Inserts subnet and pools used in tests into the database.
    void initTestSubnet6() {
      Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 64, 30, 40, 60, 70, 1));
      PoolPtr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8::"), 112));
      PoolPtr pool2(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8::1:0"),
                                IOAddress("2001:db8::2:0")));
        subnet->addPool(pool1);
        subnet->addPool(pool2);
        ConfigBackendDHCPv6Mgr::instance().getPool()->
            createUpdateSubnet6(BackendSelector(), ServerSelector::ALL(), subnet);
    }

    /// @brief Returns remote-option6-pool-set as text.
    ///
    /// @param option Pointer to the option descriptor to be used in the command.
    /// @param pool Which pool (1 or 2).
    /// @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 makePoolOption6SetConfig(const OptionDescriptorPtr& option,
                                         int pool,
                                         const std::string& remote_map) {
        std::string config = "{"
            "\"command\": \"remote-option6-pool-set\","
            "\"arguments\": {"
            "    \"pools\": [ { \"pool\": ";
        switch (pool) {
        case 1:
            config += "\"2001:db8::/30\"";
            break;
        default:
            config += "\"2001:db8::1:0 - 2001:db8::2:0\"";
            break;
        }
        config += "} ],    \"options\": ";

        CfgOption cfg_option;
        cfg_option.add(*option, option->space_name_);

        config += cfg_option.toElement()->str();

        // 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 setting or updating a DHCP option
    /// embedded in a pool.
    ///
    /// @param option Pointer to the option descriptor to be used to create or
    /// update option instance.
    /// @param pool Which pool (1 or 2).
    /// @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 testPoolOption6Set(const OptionDescriptorPtr& option,
                            int pool,
                            const std::string& remote_map = "") {
        // Generate the command as text.
        std::string command_text =
            makePoolOption6SetConfig(option, pool, remote_map);

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::setPoolOption6,
                                             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("DHCPv6 option successfully set.", msg->stringValue());

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

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

        auto option_0 = option_list->get(0);
        ASSERT_TRUE(option_0);
        ASSERT_EQ(Element::map, option_0->getType());
        EXPECT_EQ(2, option_0->size());

        auto option_0_code = option_0->get("code");
        ASSERT_TRUE(option_0_code);
        ASSERT_EQ(Element::integer, option_0_code->getType());
        EXPECT_EQ(static_cast<int64_t>(option->option_->getType()),
                  option_0_code->intValue());

        auto option_0_space = option_0->get("space");
        ASSERT_TRUE(option_0_space);
        ASSERT_EQ(Element::string, option_0_space->getType());
        EXPECT_EQ(option->space_name_, option_0_space->stringValue());

        // Make sure that the created/updated pool option is in the database
        // and has correct values.
        auto fetched_subnet = ConfigBackendDHCPv6Mgr::instance().getPool()->
            getSubnet6(BackendSelector(), ServerSelector::ANY(), 1);
        ASSERT_TRUE(fetched_subnet);
        auto addr = IOAddress(pool == 1 ? "2001:db8::1" : "2001:db8::1:2");
        auto fetched_pool = fetched_subnet->getPool(Lease::TYPE_NA, addr, false);
        ASSERT_TRUE(fetched_pool);
        auto fetched_options = fetched_pool->getCfgOption();
        ASSERT_TRUE(fetched_options);

        // Make sure that the pool options are not encapsulated.
        EXPECT_FALSE(fetched_options->isEncapsulated());

        auto fetched_option = fetched_options->get(option->space_name_,
                                                   option->option_->getType());
        ASSERT_TRUE(fetched_option.option_);
        EXPECT_TRUE(fetched_option.equals(*option));
    }

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

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::setPoolOption6,
                                             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);

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

    /// @brief Returns remote-option6-pool-del as text.
    ///
    /// @param option Pointer to the option descriptor to be used in the command.
    /// @param pool Which pool (1 or 2).
    /// @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 makePoolOption6DelConfig(const OptionDescriptorPtr& option,
                                         int pool,
                                         const std::string& remote_map) {
        std::string config = "{"
            "\"command\": \"remote-option6-pool-del\","
            "\"arguments\": {"
            "    \"pools\": [ { \"pool\": ";
        switch (pool) {
        case 1:
            config += "\"2001:db8::/30\"";
            break;
        default:
            config += "\"2001:db8::1:0 - 2001:db8::2:0\"";
            break;
        }
        config += "} ],    \"options\": [ { ";

        CfgOption cfg_option;
        cfg_option.add(*option, option->space_name_);

        std::ostringstream code;
        code << option->option_->getType();
        config += "\"code\": " + code.str();
        config += ", \"space\": \"" + option->space_name_ + "\" } ]";

        // 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 deleting a DHCP option
    /// embedded in a pool.
    ///
    /// @param option Pointer to the option descriptor to be used to create or
    /// update option instance.
    /// @param expected_count Expected number of deleted DHCP options.
    /// @param pool Which pool (1 or 2).
    /// @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 testPoolOption6Del(const OptionDescriptorPtr& option,
                            unsigned expected_count, int pool,
                            const std::string& remote_map = "") {

        // Generate the command as text.
        std::string command_text =
            makePoolOption6DelConfig(option, pool, remote_map);

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::delPoolOption6,
                                             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 option(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 pool option is not in the database.
        auto fetched_subnet = ConfigBackendDHCPv6Mgr::instance().getPool()->
            getSubnet6(BackendSelector(), ServerSelector::ANY(), 1);
        ASSERT_TRUE(fetched_subnet);
        auto addr = IOAddress(pool == 1 ? "2001:db8::1" : "2001:db8::1:2");
        auto fetched_pool = fetched_subnet->getPool(Lease::TYPE_NA, addr, false);
        ASSERT_TRUE(fetched_pool);
        auto fetched_options = fetched_pool->getCfgOption();
        ASSERT_TRUE(fetched_options);
        auto fetched_option = fetched_options->get(option->space_name_,
                                                   option->option_->getType());
        ASSERT_FALSE(fetched_option.option_);
    }

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

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::delPoolOption6,
                                             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);
        EXPECT_EQ(answer_text, answer->stringValue());
    }
};

// This test verifies that it is possible to add and update pool DHCP option.
TEST_F(OptionPool6CmdsTest, poolOption6Set) {
    initTestSubnet6();

    {
        SCOPED_TRACE("add new pool option");
        OptionStringPtr option(new OptionString(Option::V6, D6O_BOOTFILE_URL,
                                                "my-boot-file"));
        OptionDescriptorPtr option_desc(new OptionDescriptor(option, false,
                                                             false));
        option_desc->space_name_ = DHCP6_OPTION_SPACE;
        testPoolOption6Set(option_desc, 1);
    }

    {
        SCOPED_TRACE("add another pool option with formatted value");
        OptionStringPtr option(new OptionString(Option::V6, D6O_V6_CAPTIVE_PORTAL,
                                                "myportal"));
        OptionDescriptorPtr option_desc(new OptionDescriptor(option, false,
                                                             false,
                                                             "myportal"));
        option_desc->space_name_ = DHCP6_OPTION_SPACE;
        testPoolOption6Set(option_desc, 2);
    }

    {
        SCOPED_TRACE("update existing pool option");
        OptionStringPtr option(new OptionString(Option::V6, D6O_BOOTFILE_URL,
                                                "another-boot-file"));
        OptionDescriptorPtr option_desc(new OptionDescriptor(option, false,
                                                             false));
        option_desc->space_name_ = DHCP6_OPTION_SPACE;
        testPoolOption6Set(option_desc, 1);
    }

    {
        SCOPED_TRACE("update another option with specifying remote");
        OptionStringPtr option(new OptionString(Option::V6, D6O_V6_CAPTIVE_PORTAL,
                                                "anotherdomain"));
        OptionDescriptorPtr option_desc(new OptionDescriptor(option, true,
                                                             false,
                                                             "anotherdomain"));
        option_desc->space_name_ = DHCP6_OPTION_SPACE;
        testPoolOption6Set(option_desc, 2, "{ \"type\": \"mysql\" }");
    }

    {
        SCOPED_TRACE("add an option for which a definition is required");
        // Add a definition for option 1001 to the config backend.
        auto option_def = boost::make_shared<OptionDefinition>("foo", 1001, DHCP6_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.
            ConfigBackendDHCPv6Mgr::instance().getPool()->
                createUpdateOptionDef6(BackendSelector(),
                                       ServerSelector::ALL(),
                                       option_def);
        );

        // Set the option 1001.
        OptionStringPtr option(new OptionString(Option::V6, 1001, "baz"));
        OptionDescriptorPtr option_desc(new OptionDescriptor(option, false,
                                                             false, "baz"));
        option_desc->space_name_ = DHCP6_OPTION_SPACE;

        testPoolOption6Set(option_desc, 1);
    }
}

// This test verifies that malformed requests to add and update pool
// DHCP options are rejected and proper error messages are returned.
TEST_F(OptionPool6CmdsTest, poolOption6SetFail) {
    {
        SCOPED_TRACE("empty arguments");
        testPoolOption6SetFail("", "invalid command 'remote-option6-pool-set': 'arguments' is empty");
    }

    {
        SCOPED_TRACE("empty pools list");
        testPoolOption6SetFail("\"pools\": [ ]",
                               "'pools' list "
                               "must include exactly one element");
    }

    {
        SCOPED_TRACE("empty pool map");
        testPoolOption6SetFail("\"pools\": [ { } ]",
                               "missing 'pool' parameter");
    }

    {
        SCOPED_TRACE("malformed pool");
        testPoolOption6SetFail("\"pools\": [ { \"pool\": true } ]",
                               "'pool' parameter is not a string");
    }

    {
        SCOPED_TRACE("invalid pool prefix");
        testPoolOption6SetFail("\"pools\": [ { \"pool\": \"2001:db8::/129\" } ]",
                               "Failed to create pool defined by: "
                               "2001:db8::/129 (<string>:1:74)");
    }

    {
        SCOPED_TRACE("invalid pool range");
        testPoolOption6SetFail("\"pools\": [ { \"pool\": "
                               "\"2001:db8::2:2 - 2001:db8::1:1\" } ]",
                               "Failed to create pool defined by: "
                               "2001:db8::2:2-2001:db8::1:1 (<string>:1:74)");
    }

    {
        SCOPED_TRACE("invalid pool prefix");
        testPoolOption6SetFail("\"pools\": [ { \"pool\": "
                               "\"2001:db8::/25\" } ]",
                               "Failed to create pool defined by: "
                               "2001:db8::/25 (<string>:1:74)");
    }

    {
        SCOPED_TRACE("spurious keyword");
        testPoolOption6SetFail("\"pools\": [ { "
                               "\"id\": 1, \"pool\": \"2001:db8::/64\" } ]",
                               "spurious 'id' parameter");
    }

    {
        SCOPED_TRACE("multiple pools");
        testPoolOption6SetFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/64\" },"
                               "{ \"pool\": \"2001:db8:1::/80\" }"
                               "]",
                               "'pools' list must include exactly one element");
    }

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

    {
        SCOPED_TRACE("no options list");
        testPoolOption6SetFail("\"pools\": [ { \"pool\": \"2001:db8::/64\" } ]",
                               "'options' parameter must be specified "
                               "and must be a list");
    }

    {
        SCOPED_TRACE("no pools list");
        testPoolOption6SetFail("\"options\": ["
                               "{ \"name\": \"bootfile-url\", "
                               "\"data\": \"my-boot-file\" } ]",
                               "'pools' parameter must be specified "
                               "and must be a list");
    }

    {
        SCOPED_TRACE("empty options list");
        testPoolOption6SetFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ ]",
                               "'options' list must include exactly one element");
    }

    {
        SCOPED_TRACE("empty options map");
        testPoolOption6SetFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ { } ]",
                               "option data configuration requires one of "
                               "'code' or 'name' parameters to be specified");
    }

    {
        SCOPED_TRACE("spurious keyword");
        testPoolOption6SetFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ "
                               "{ \"name\": \"boot-file-name\", \"data\": \"my-boot-file\", "
                               "\"type\": \"string\" } ]",
                               "spurious 'type' parameter");
    }

    {
        SCOPED_TRACE("keyword with bad type");
        testPoolOption6SetFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ "
                               "{ \"name\": \"boot-file-name\", \"data\": \"my-boot-file\", "
                               "\"space\": 4 } ]",
                               "'space' parameter is not a string");
    }

    {
        SCOPED_TRACE("malformed option (integer instead of string)");
        testPoolOption6SetFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ { \"name\": \"new-posix-timezone\" } ]",
                               "option data does not match option definition");
    }

    {
        SCOPED_TRACE("multiple options");
        testPoolOption6SetFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": ["
                               "{ \"name\": \"bootfile-url\", \"data\": \"my-boot-file\" },"
                               "{ \"name\": \"nis-domain-name\", \"data\": \"mydomain\" }"
                               "]",
                               "'options' list must include exactly one element");
    }

    {
        SCOPED_TRACE("unsupported backend type");
        testPoolOption6SetFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": ["
                               "    { \"name\": \"bootfile-url\", \"data\": \"my-boot-file\" }"
                               "],"
                               "\"remote\": {"
                               "    \"type\": \"postgresql\""
                               "}",
                               "no such database found for selector: type=postgresql");
    }
}

// This test verifies that it is possible to delete pool DHCP option.
TEST_F(OptionPool6CmdsTest, poolOption6Del) {
    initTestSubnet6();

    {
        SCOPED_TRACE("add and delete new pool option");
        OptionStringPtr option(new OptionString(Option::V6, D6O_BOOTFILE_URL,
                                                "my-boot-file"));
        OptionDescriptorPtr option_desc(new OptionDescriptor(option, false,
                                                             false));
        option_desc->space_name_ = DHCP6_OPTION_SPACE;
        testPoolOption6Set(option_desc, 1);
        testPoolOption6Del(option_desc, 1, 1);
    }

    {
        SCOPED_TRACE("add and delete another pool option with formatted value");
        OptionStringPtr option(new OptionString(Option::V6, D6O_V6_CAPTIVE_PORTAL,
                                                "myportal"));
        OptionDescriptorPtr option_desc(new OptionDescriptor(option, false,
                                                             false,
                                                             "myportal"));
        option_desc->space_name_ = DHCP6_OPTION_SPACE;
        testPoolOption6Set(option_desc, 2);
        testPoolOption6Del(option_desc, 1, 2);
    }

    {
        SCOPED_TRACE("delete pool option");
        // Deleting not existing is not an error.
        OptionStringPtr option(new OptionString(Option::V6, D6O_BOOTFILE_URL,
                                                "another-boot-file"));
        OptionDescriptorPtr option_desc(new OptionDescriptor(option, false,
                                                             false));
        option_desc->space_name_ = DHCP6_OPTION_SPACE;
        testPoolOption6Del(option_desc, 0, 1);
        testPoolOption6Del(option_desc, 0, 2);
    }

    {
        SCOPED_TRACE("delete another option with specifying remote");
        OptionStringPtr option(new OptionString(Option::V6, D6O_V6_CAPTIVE_PORTAL,
                                                "anotherdomain"));
        OptionDescriptorPtr option_desc(new OptionDescriptor(option, true,
                                                             false,
                                                             "anotherdomain"));
        option_desc->space_name_ = DHCP6_OPTION_SPACE;
        testPoolOption6Set(option_desc, 1, "{ \"type\": \"mysql\" }");
        testPoolOption6Del(option_desc, 1, 1, "{ \"type\": \"mysql\" }");
    }

    {
        SCOPED_TRACE("delete option 0 in not dhcp6 space");
        OptionPtr option(new Option(Option::V6, 0));
        OptionDescriptorPtr option_desc(new OptionDescriptor(option, false,
                                                             false));
        option_desc->space_name_ = "foo";
        testPoolOption6Set(option_desc, 2);
        testPoolOption6Del(option_desc, 1, 2);
    }
}

// This test verifies that malformed requests to delete pool DHCP
// options are rejected and proper error messages are returned.
TEST_F(OptionPool6CmdsTest, poolOption6DelFail) {
    {
        SCOPED_TRACE("empty arguments");
        testPoolOption6DelFail("", "invalid command 'remote-option6-pool-del': 'arguments' is empty");
    }

    {
        SCOPED_TRACE("empty pools list");
        testPoolOption6DelFail("\"pools\": [ ]",
                               "'pools' list "
                               "must include exactly one element");
    }

    {
        SCOPED_TRACE("empty pool map");
        testPoolOption6DelFail("\"pools\": [ { } ]",
                               "missing 'pool' parameter");
    }

    {
        SCOPED_TRACE("malformed pool");
        testPoolOption6DelFail("\"pools\": [ { \"pool\": true } ]",
                               "'pool' parameter is not a string");
    }

    {
        SCOPED_TRACE("invalid pool prefix");
        testPoolOption6DelFail("\"pools\": [ { \"pool\": \"2001:db8::/129\" } ]",
                               "Failed to create pool defined by: "
                               "2001:db8::/129 (<string>:1:74)");
    }

    {
        SCOPED_TRACE("invalid pool range");
        testPoolOption6DelFail("\"pools\": [ { \"pool\": "
                               "\"2001:db8::2:2 - 2001:db8::1:1\" } ]",
                               "Failed to create pool defined by: "
                               "2001:db8::2:2-2001:db8::1:1 (<string>:1:74)");
    }

    {
        SCOPED_TRACE("invalid pool prefix");
        testPoolOption6DelFail("\"pools\": [ { \"pool\": "
                               "\"2001:db8::/25\" } ]",
                               "Failed to create pool defined by: "
                               "2001:db8::/25 (<string>:1:74)");
    }

    {
        SCOPED_TRACE("spurious keyword");
        testPoolOption6DelFail("\"pools\": [ { "
                               "\"id\": 1, \"pool\": \"2001:db8::/64\" } ]",
                               "spurious 'id' parameter");
    }

    {
        SCOPED_TRACE("multiple pools");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" },"
                               "{ \"pool\": \"2001:db8:1::/80\" }"
                               "]",
                               "'pools' list must include exactly one element");
    }

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

    {
        SCOPED_TRACE("no options list");
        testPoolOption6DelFail("\"pools\": [ { \"pool\": \"2001:db8::/64\" } ]",
                               "'options' parameter must be specified "
                               "and must be a list");
    }

    {
        SCOPED_TRACE("no pools list");
        testPoolOption6DelFail("\"options\": ["
                               "{ \"name\": \"bootfile-url\", "
                               "\"data\": \"my-boot-file\" } ]",
                               "'pools' parameter must be specified "
                               "and must be a list");
    }

    {
        SCOPED_TRACE("empty options list");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ ]", "'options' list "
                               "must include exactly one element");
    }

    {
        SCOPED_TRACE("empty options map");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ { } ]",
                               "missing 'code' parameter");
    }

    {
        SCOPED_TRACE("empty space");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ { \"code\": 1, "
                               "\"space\": \"\" } ]",
                               "'space' parameter must be a "
                               "valid space name");
    }

    {
        SCOPED_TRACE("invalid space");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ { \"code\": 1, "
                               "\"space\": \" \" } ]",
                               "'space' parameter must be a "
                               "valid space name");
    }

    {
        SCOPED_TRACE("malformed option (integer instead of string)");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ { \"code\": 1, "
                               "\"space\": 40 } ]",
                               "'space' parameter is not a string");
    }

    {
        SCOPED_TRACE("malformed option (string instead of integer)");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ { \"code\": \"nis-domain-name\" } ]",
                               "'code' parameter is not an integer");
    }

    {
        SCOPED_TRACE("out of range code");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ { \"code\": 100000} ]",
                               "invalid 'code' value not in [0..65535]");
    }

    {
        SCOPED_TRACE("option 0 is reserved");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": [ { \"code\": 0 } ]",
                               "invalid 'code' value 0: reserved");
    }

    {
        SCOPED_TRACE("multiple options");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": ["
                               "{ \"code\": 67 },"
                               "{ \"code\": 40 }"
                               "]",
                               "'options' list must include exactly one element");
    }

    {
        SCOPED_TRACE("unsupported backend type");
        testPoolOption6DelFail("\"pools\": ["
                               "{ \"pool\": \"2001:db8::/32\" } ],"
                               "\"options\": ["
                               "    { \"code\": 67 }"
                               "],"
                               "\"remote\": {"
                               "    \"type\": \"postgresql\""
                               "}",
                               "no such database found for selector: type=postgresql");
    }
}

}