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
// 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 <database/server_collection.h>
#include <dhcpsrv/config_backend_dhcp4_mgr.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::cb;
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 callouts pertaining to DHCP server definitions.
class Server4CmdsTest : public ConfigCmdsDhcp4Test {
public:

    /// @brief Returns remote-server4-set command as text.
    ///
    /// @param server Pointer to the server 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 makeServer4SetConfig(const ServerPtr& server,
                                     const std::string& remote_map) {
        std::string config = "{"
            "\"command\": \"remote-server4-set\","
            "\"arguments\": {"
            "    \"servers\": [ { ";

        config += "\"server-tag\": \"" + server->getServerTagAsText() + "\"";
        if (!server->getDescription().empty()) {
            config += ", \"description\": \"" + server->getDescription() + "\"";
        }
        config += "} ] ";

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

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

    /// @brief Positive test scenario for setting or updating a server.
    ///
    /// @param server Pointer to the server 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.
    void testServer4Set(const ServerPtr& server,
                        const std::string& remote_map = "") {
        // Generate the command as text.
        std::string command_text = makeServer4SetConfig(server, remote_map);

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

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

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

        auto server_0 = servers->get(0);
        ASSERT_TRUE(server_0);
        ASSERT_EQ(Element::map, server_0->getType());

        EXPECT_EQ(server->toElement()->str(), server_0->str());

        // Make sure that the created/updated server is in the database
        // and has correct values.
        auto fetched_server = ConfigBackendDHCPv4Mgr::instance().getPool()->
            getServer4(BackendSelector(), server->getServerTag());
        ASSERT_TRUE(fetched_server);
        EXPECT_EQ(fetched_server->getServerTagAsText(),
                  server->getServerTagAsText());
        EXPECT_EQ(fetched_server->getDescription(), server->getDescription());
    }

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

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp4Impl::setServer4,
                                             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 server 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-server4-del command as text.
    ///
    /// @param server Pointer to the server 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 makeServer4DelConfig(const ServerPtr& server,
                                     const std::string& remote_map) {
        std::string config = "{"
            "\"command\": \"remote-server4-del\","
            "\"arguments\": {"
            "    \"servers\": [ { ";

        config += "\"server-tag\": \"" + server->getServerTagAsText() + "\"";
        config += "} ] ";

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

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

    /// @brief Positive test scenario for deleting a server.
    ///
    /// @param server Pointer to the server to be deleted.
    /// @param expected_count Expected number of deleted servers.
    /// @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 testServer4Del(const ServerPtr& server,
                        unsigned expected_count,
                        const std::string& remote_map = "") {
        // Generate the command as text.
        std::string command_text = makeServer4DelConfig(server, remote_map);

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp4Impl::delServer4,
                                             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
                     << " DHCPv4 server(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 server is not in the database.
        auto fetched_server = ConfigBackendDHCPv4Mgr::instance().getPool()->
            getServer4(BackendSelector(), server->getServerTag());
        ASSERT_FALSE(fetched_server);
    }

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

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

    /// @brief Returns remote-server4-get command as text.
    ///
    /// @param server Pointer to the server 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 makeServer4GetConfig(const ServerPtr& server,
                                     const std::string& remote_map) {
        std::string config = "{"
            "\"command\": \"remote-server4-get\","
            "\"arguments\": {"
            "    \"servers\": [ { ";

        config += "\"server-tag\": \"" + server->getServerTagAsText() + "\"";
        config += "} ] ";

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

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

    /// @brief Positive test scenario for getting a server.
    ///
    /// @param server Pointer to the server to be got.
    /// @param expected Pointer to the expected server.
    /// @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 testServer4Get(const ServerPtr& server,
                        const ServerPtr& expected,
                        const std::string& remote_map = "") {
        // Generate the command as text.
        std::string command_text = makeServer4GetConfig(server, remote_map);

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp4Impl::getServer4,
                                             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 << "DHCPv4 server '"
                     << server->getServerTagAsText() << "' "
                     << (!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 server.
        ConstElementPtr servers = args->get("servers");
        ASSERT_TRUE(servers);
        ASSERT_EQ(Element::list, servers->getType());
        ConstElementPtr count = args->get("count");
        ASSERT_TRUE(count);
        ASSERT_EQ(Element::integer, count->getType());

        if (!expected) {
            EXPECT_EQ(0, servers->size());
            EXPECT_EQ(0, count->intValue());
        } else {
            ASSERT_EQ(1, servers->size());
            EXPECT_EQ(1, count->intValue());

            ConstElementPtr got = servers->get(0);
            ASSERT_TRUE(got);
            ASSERT_EQ(Element::map, got->getType());
            EXPECT_EQ(expected->toElement()->str(), got->str());
        }
    }

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

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

    /// @brief Returns remote-server4-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.
    ///
    /// @return Command in the textual format.
    std::string makeServer4GetAllConfig(const std::string& remote_map) {
        std::string config = "{"
            "\"command\": \"remote-server4-get-all\"";

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

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

    /// @brief Positive test scenario for getting all servers.
    ///
    /// @param expected Expected result.
    /// @param expected_count Expected number of got servers.
    /// @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 testServer4GetAll(const ServerCollection& expected,
                           unsigned expected_count,
                           const std::string& remote_map = "") {
        // Generate the command as text.
        std::string command_text = makeServer4GetAllConfig(remote_map);

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response =
                        impl_.run(&TestConfigCmdsDhcp4Impl::getAllServers4,
                                  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
                     << " DHCPv4 server(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_count, count->intValue());

        // Validate returned servers.
        ConstElementPtr servers = args->get("servers");
        ASSERT_TRUE(servers);
        ASSERT_EQ(Element::list, servers->getType());

        // Build the set of received servers
        std::set<std::string> got;
        for (auto const& server : servers->listValue()) {
            ASSERT_TRUE(server);
            got.insert(server->str());
        }
        EXPECT_EQ(expected_count, got.size());
        std::set<std::string> exp;
        for (auto const& server : expected) {
            ASSERT_TRUE(server);
            exp.insert(server->toElement()->str());
        }
        EXPECT_EQ(exp, got);
    }

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

        // Invoke the callout and get the response.
        ConstElementPtr response;
        ASSERT_NO_THROW(response =
                        impl_.run(&TestConfigCmdsDhcp4Impl::getAllServers4,
                                  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 server.
TEST_F(Server4CmdsTest, server4Set) {<--- syntax error
    {
        SCOPED_TRACE("add new server");
        ServerPtr server = Server::create(ServerTag("foo"), "a server");
        testServer4Set(server);
    }

    {
        SCOPED_TRACE("add another server");
        ServerPtr server = Server::create(ServerTag("bar"));
        testServer4Set(server);
    }

    {
        SCOPED_TRACE("update existing server");
        ServerPtr server = Server::create(ServerTag("foo"), "changed server");
        testServer4Set(server);
    }

    {
        SCOPED_TRACE("update another server with specifying remote");
        ServerPtr server = Server::create(ServerTag("xyz"), "remote server");
        testServer4Set(server, "{ \"type\": \"mysql\" }");
    }
}

// This test verifies that malformed requests to add and update option
// definition are rejected and proper error messages are returned.
TEST_F(Server4CmdsTest, server4SetFail) {
    {
        SCOPED_TRACE("empty arguments");
        testServer4SetFail("", "invalid command 'remote-server4-set': 'arguments' is empty");
    }

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

    {
        SCOPED_TRACE("empty server map");
        testServer4SetFail("\"servers\": [ { } ]",
                           "missing 'server-tag' parameter");
    }

    {
        SCOPED_TRACE("malformed server");
        testServer4SetFail("\"servers\": [ { \"name\": \"foo\" } ]",
                           "missing 'server-tag' parameter");
    }

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

    {
        SCOPED_TRACE("blank server tag");
        testServer4SetFail("\"servers\": ["
                           "{ \"server-tag\": \" \" } ]",
                           "server-tag must not be empty");
    }

    {
        SCOPED_TRACE("too long server tag");
        std::ostringstream args;
        args << "\"servers\": ["
             << "{ \"server-tag\": \""
             << std::string(257, 'a')
             << "\" } ]";
        testServer4SetFail(args.str(),
                           "server-tag must not be longer than 256 characters");
    }

    {
        SCOPED_TRACE("multiple servers");
        testServer4SetFail("\"servers\": ["
                           "{ \"server-tag\": \"foo\" },"
                           "{ \"server-tag\": \"bar\" } ]",
                           "'servers' list must include exactly one element");
    }

    {
        SCOPED_TRACE("unsupported backend type");
        testServer4SetFail("\"servers\": [ { \"server-tag\": \"foo\" } ],"
                           "\"remote\": {"
                           "    \"type\": \"postgresql\""
                           "}",
                           "no such database found for selector: type=postgresql");
    }
}

// This test verifies that it is possible to delete server.
TEST_F(Server4CmdsTest, server4Del) {
    {
        SCOPED_TRACE("add and delete new server");
        ServerPtr server = Server::create(ServerTag("foo"), "a server");
        testServer4Set(server);
        testServer4Del(server, 1);
    }

    {
        SCOPED_TRACE("add and delete another server");
        ServerPtr server = Server::create(ServerTag("bar"));
        testServer4Set(server);
        testServer4Del(server, 1);
    }

    {
        SCOPED_TRACE("delete server");
        // Deleting not existing is not an error.
        ServerPtr server = Server::create(ServerTag("foo"), "does not exist");
        testServer4Del(server, 0);
    }

    {
        SCOPED_TRACE("delete another server with specifying remote");
        ServerPtr server = Server::create(ServerTag("foo"), "remote server");
        testServer4Set(server, "{ \"type\": \"mysql\" }");
        testServer4Del(server, 1, "{ \"type\": \"mysql\" }");
    }
}

// This test verifies that malformed requests to delete server
// are rejected and proper error messages are returned.
TEST_F(Server4CmdsTest, server4DelFail) {
    {
        SCOPED_TRACE("empty arguments");
        testServer4DelFail("", "invalid command 'remote-server4-del': 'arguments' is empty");
    }

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

    {
        SCOPED_TRACE("malformed server");
        testServer4DelFail("\"servers\": [ { \"name\": \"foo\" } ]",
                           "missing 'server-tag' parameter");
    }

    {
        SCOPED_TRACE("spurious parameter");
        testServer4DelFail("\"servers\": [ { \"server-tag\": \"foo\", "
                           "\"name\": \"foo\" } ]",
                           "spurious 'name' parameter");
    }

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

    {
        SCOPED_TRACE("blank server tag");
        testServer4DelFail("\"servers\": ["
                           "{ \"server-tag\": \" \" } ]",
                           "server-tag must not be empty");
    }

    {
        SCOPED_TRACE("too long server tag");
        std::ostringstream args;
        args << "\"servers\": ["
             << "{ \"server-tag\": \""
             << std::string(257, 'a')
             << "\" } ]";
        testServer4DelFail(args.str(),
                           "server-tag must not be longer than 256 characters");
    }

    {
        SCOPED_TRACE("multiple servers");
        testServer4DelFail("\"servers\": ["
                           "{ \"server-tag\": \"foo\" },"
                           "{ \"server-tag\": \"bar\" } ]",
                           "'servers' list must include exactly one element");
    }

    {
        SCOPED_TRACE("unsupported backend type");
        testServer4DelFail("\"servers\": [ { \"server-tag\": \"foo\" } ],"
                           "\"remote\": {"
                           "    \"type\": \"postgresql\""
                           "}",
                           "no such database found for selector: type=postgresql");
    }
}

// This test verifies that it is possible to get server.
TEST_F(Server4CmdsTest, server4Get) {
    {
        SCOPED_TRACE("add and get new server");
        ServerPtr server = Server::create(ServerTag("foo"), "a server");
        testServer4Set(server);
        testServer4Get(server, server);
    }

    {
        SCOPED_TRACE("add and get another server");
        ServerPtr server = Server::create(ServerTag("bar"));
        testServer4Set(server);
        testServer4Get(server, server);
    }

    {
        SCOPED_TRACE("get no server");
        // Getting not existing is not an error.
        ServerPtr server = Server::create(ServerTag("abc"), "does not exist");
        testServer4Get(server, ServerPtr());
    }

    {
        SCOPED_TRACE("delete another server with specifying remote");
        ServerPtr server = Server::create(ServerTag("xyz"), "remote server");
        testServer4Set(server, "{ \"type\": \"mysql\" }");
        testServer4Get(server, server, "{ \"type\": \"mysql\" }");
    }
}

// This test verifies that malformed requests to get server
// are rejected and proper error messages are returned.
TEST_F(Server4CmdsTest, server4GetFail) {
    {
        SCOPED_TRACE("empty arguments");
        testServer4GetFail("", "invalid command 'remote-server4-get': 'arguments' is empty");
    }

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

    {
        SCOPED_TRACE("malformed server");
        testServer4GetFail("\"servers\": [ { \"name\": \"foo\" } ]",
                           "missing 'server-tag' parameter");
    }

    {
        SCOPED_TRACE("spurious parameter");
        testServer4GetFail("\"servers\": [ { \"server-tag\": \"foo\", "
                           "\"name\": \"foo\" } ]",
                           "spurious 'name' parameter");
    }

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

    {
        SCOPED_TRACE("blank server tag");
        testServer4GetFail("\"servers\": ["
                           "{ \"server-tag\": \" \" } ]",
                           "server-tag must not be empty");
    }

    {
        SCOPED_TRACE("too long server tag");
        std::ostringstream args;
        args << "\"servers\": ["
             << "{ \"server-tag\": \""
             << std::string(257, 'a')
             << "\" } ]";
        testServer4GetFail(args.str(),
                           "server-tag must not be longer than 256 characters");
    }

    {
        SCOPED_TRACE("multiple servers");
        testServer4GetFail("\"servers\": ["
                           "{ \"server-tag\": \"foo\" },"
                           "{ \"server-tag\": \"bar\" } ]",
                           "'servers' list must include exactly one element");
    }

    {
        SCOPED_TRACE("unsupported backend type");
        testServer4DelFail("\"servers\": [ { \"server-tag\": \"foo\" } ],"
                           "\"remote\": {"
                           "    \"type\": \"postgresql\""
                           "}",
                           "no such database found for selector: type=postgresql");
    }
}

// This test verifies that it is possible to get all servers.
TEST_F(Server4CmdsTest, server4GetAll) {
    // Empty.
    ServerCollection expected;
    testServer4GetAll(expected, 0);

    // Add a server.
    ServerPtr server = Server::create(ServerTag("foo"), "a server");
    testServer4Set(server);
    expected.insert(server);
    testServer4GetAll(expected, 1);

    // Add a second server.
    server = Server::create(ServerTag("bar"));
    testServer4Set(server);
    expected.insert(server);
    testServer4GetAll(expected, 2);

    // Try with a remote.
    server = Server::create(ServerTag("abc"), "remote server");
    testServer4Set(server, "{ \"type\": \"mysql\" }");
    expected.insert(server);
    testServer4GetAll(expected, 3, "{ \"type\": \"mysql\" }");
}

// This test verifies that malformed requests to get all servers
// are rejected and proper error messages are returned.
TEST_F(Server4CmdsTest, server4GetAllFail) {
    {
        SCOPED_TRACE("empty arguments");
        testServer4GetAllFail(", \"arguments\": { }",
                              "invalid command 'remote-server4-get-all': 'arguments' is empty");
    }

    {
        SCOPED_TRACE("unsupported backend type");
        testServer4GetAllFail(", \"arguments\": { "
                              "\"remote\": {"
                              "    \"type\": \"postgresql\""
                              "} }",
                              "no such database found for selector: "
                              "type=postgresql");
    }
}

}