summaryrefslogtreecommitdiffstats
path: root/src/lib/database/tests/dbaccess_parser_unittest.cc
blob: dca15890f5f5ff596ed7f9d950a99778d9ce2dd1 (plain)
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
// Copyright (C) 2012-2020 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <cc/command_interpreter.h>
#include <database/database_connection.h>
#include <database/db_exceptions.h>
#include <database/dbaccess_parser.h>
#include <log/logger_support.h>

#include <gtest/gtest.h>

#include <map>
#include <string>

using namespace std;
using namespace isc;
using namespace isc::db;
using namespace isc::data;
using namespace isc::config;

namespace {

/// @brief Database Access Parser test fixture class
class DbAccessParserTest : public ::testing::Test {
public:
    /// @brief Constructor
    DbAccessParserTest() {
    }
    /// @brief Destructor
    ///
    /// As some of the tests have the side-effect of altering the logging
    /// settings (when the parser's "parse" method is called), ensure that
    /// the logging is reset to the default after each test completes.
    ~DbAccessParserTest() {
        isc::log::setDefaultLoggingOutput();
    }

    /// @brief Build JSON String
    ///
    /// Given a array of "const char*" strings representing in order, keyword,
    /// value, keyword, value, ... and terminated by a NULL, return a string
    /// that represents the JSON map for the keywords and values.
    ///
    /// E.g. given the array of strings: alpha, one, beta, two, NULL, it would
    /// return the string '{ "alpha": "one", "beta": "two" }'
    ///
    /// @param keyval Array of "const char*" strings in the order keyword,
    ///        value, keyword, value ...  A NULL entry terminates the list.
    ///
    /// @return JSON map for the keyword value array.
    std::string toJson(const char* keyval[]) {
        const std::string quote = "\"";
        const std::string colon = ":";
        const std::string space = " ";

        string result = "{ ";

        for (size_t i = 0; keyval[i] != NULL; i+= 2) {
            // Get the value.  This should not be NULL.  As ASSERT_NE will
            // cause a return - which gives compilation problems as a return
            // statement is expected to return a string - use EXPECT_NE and
            // explicitly return if the expected array is incorrect.
            EXPECT_NE(static_cast<const char*>(NULL), keyval[i + 1]) <<
                "Supplied reference keyword/value list does not contain values "
                "for all keywords";
            if (keyval[i + 1] == NULL) {
                return (std::string(""));
            }

            // Add the separating comma if not the first.
            if (i != 0) {
                result += ", ";
            }

            // Add the keyword and value - make sure that they are quoted.
            // The parameters which are not quoted are persist, readonly and
            // lfc-interval as they are boolean and integer respectively.
            result += quote + keyval[i] + quote + colon + space;
            if (!quoteValue(std::string(keyval[i]))) {
                result += keyval[i + 1];

            } else {
                result += quote + keyval[i + 1] + quote;
            }
        }

        // Add the terminating brace
        result += " }";

        return (result);
    }

    /// @brief Check for Keywords
    ///
    /// Takes a database access string and checks it against a list of keywords
    /// and values.  It checks that:
    ///
    /// a. Every keyword in the string appears once and only once in the
    ///    list.
    /// b. Every keyword in the list appears in the string.
    /// c. Every keyword's value is the same as that in the string.
    ///
    /// To parse the access string, we use the parsing function in the
    /// DHCP lease manager.
    ///
    /// @param trace_string String that will be used to set the value of a
    ///        SCOPED_TRACE for this call.
    /// @param dbaccess set of database access parameters to check
    /// @param keyval Array of "const char*" strings in the order keyword,
    ///        value, keyword, value ...  A NULL entry terminates the list.
    void checkAccessString(const char* trace_string,
                           const DatabaseConnection::ParameterMap& parameters,
                           const char* keyval[]) {
        SCOPED_TRACE(trace_string);

        // Construct a map of keyword value pairs.
        std::map<string, string> expected;
        size_t expected_count = 0;
        for (size_t i = 0; keyval[i] != NULL; i += 2) {
            // Get the value.  This should not be NULL
            ASSERT_NE(static_cast<const char*>(NULL), keyval[i + 1]) <<
                "Supplied reference keyword/value list does not contain values "
                "for all keywords";
            expected[keyval[i]] = keyval[i + 1];

            // One more keyword processed
            ++expected_count;
        }

        // Check no duplicates in the test set of reference keywords.
        ASSERT_EQ(expected_count, expected.size()) <<
            "Supplied reference keyword/value list contains duplicate keywords";

        // The passed parameter map should have the same number of entries as
        // the reference set of keywords.
        EXPECT_EQ(expected_count, parameters.size());

        // Check that the keywords and keyword values are the same: loop
        // through the keywords in the database access string.
        for (DatabaseConnection::ParameterMap::const_iterator actual = parameters.begin();
             actual != parameters.end(); ++actual) {

            // Does the keyword exist in the set of expected keywords?
            std::map<string, string>::iterator corresponding =
                expected.find(actual->first);
            ASSERT_TRUE(corresponding != expected.end());

            // Keyword exists, is the value the same?
            EXPECT_EQ(corresponding->second, actual->second);
        }
    }

private:

    /// @brief Checks if the value of the specified parameter should be
    /// quoted in the configuration.
    ///
    /// @param parameter A parameter for which it should be checked whether
    /// the value should be quoted or not.
    ///
    /// @return true if the value of the parameter should be quoted.
     bool quoteValue(const std::string& parameter) const {
         return ((parameter != "persist") && (parameter != "lfc-interval") &&
                 (parameter != "connect-timeout") &&
                 (parameter != "port") &&
                 (parameter != "max-row-errors") &&
                 (parameter != "readonly"));
    }

};


/// @brief Version of parser with protected methods public
///
/// Some of the methods in DbAccessParser are not required to be public in Kea.
/// Instead of being declared "private", they are declared "protected" so that
/// they can be accessed through a derived class in the unit tests.
class TestDbAccessParser : public DbAccessParser {
public:

    /// @brief Constructor
    TestDbAccessParser()
        : DbAccessParser()
    {}

    /// @brief Destructor
    virtual ~TestDbAccessParser()
    {}

    /// @brief Parse configuration value
    ///
    /// @param database_config Configuration to be parsed.
    void parse(ConstElementPtr database_config) {
        std::string db_access_string;
        DbAccessParser::parse(db_access_string, database_config);
    }

    /// @brief Get database access parameters
    ///
    /// Used in testing to check that the configuration information has been
    /// parsed corrected.
    ///
    /// @return Map of keyword/value pairs representing database access
    ///         information.
    const DatabaseConnection::ParameterMap& getDbAccessParameters() const {
        return (DbAccessParser::getDbAccessParameters());
    }

    /// @brief Construct database access string
    ///
    /// Constructs the database access string from the stored parameters.
    ///
    /// @return Database access string
    std::string getDbAccessString() const {
        return (DbAccessParser::getDbAccessString());
    }
};

// Check that the parser works with a simple configuration.
TEST_F(DbAccessParserTest, validTypeMemfile) {
    const char* config[] = {"type", "memfile",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Valid memfile", parser.getDbAccessParameters(), config);
}

// Check that the parser works with a simple configuration for host database.
TEST_F(DbAccessParserTest, hosts) {
    const char* config[] = {"type", "memfile",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Valid memfile", parser.getDbAccessParameters(), config);
}

// Check that the parser works with a simple configuration that
// includes empty elements.
TEST_F(DbAccessParserTest, emptyKeyword) {
    const char* config[] = {"type", "memfile",
                            "name", "",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Valid memfile", parser.getDbAccessParameters(), config);
}

// Check that the parser works with more complex configuration when
// lease file path is specified for DHCPv4.
TEST_F(DbAccessParserTest, persistV4Memfile) {
    const char* config[] = {"type", "memfile",
                            "persist", "true",
                            "name", "/opt/var/lib/kea/kea-leases4.csv",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));

    checkAccessString("Valid memfile", parser.getDbAccessParameters(),
                      config);
}

// Check that the parser works with more complex configuration when
// lease file path is specified for DHCPv6.
TEST_F(DbAccessParserTest, persistV6Memfile) {
    const char* config[] = {"type", "memfile",
                            "persist", "true",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));

    checkAccessString("Valid memfile", parser.getDbAccessParameters(),
                      config);
}

// This test checks that the parser accepts the valid value of the
// lfc-interval parameter.
TEST_F(DbAccessParserTest, validLFCInterval) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "lfc-interval", "3600",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Valid LFC Interval", parser.getDbAccessParameters(),
                      config);
}

// This test checks that the parser rejects the negative value of the
// lfc-interval parameter.
TEST_F(DbAccessParserTest, negativeLFCInterval) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "lfc-interval", "-1",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
}

// This test checks that the parser rejects the too large (greater than
// the max uint32_t) value of the lfc-interval parameter.
TEST_F(DbAccessParserTest, largeLFCInterval) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "lfc-interval", "4294967296",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
}

// This test checks that the parser accepts the valid value of the
// timeout parameter.
TEST_F(DbAccessParserTest, validTimeout) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "connect-timeout", "3600",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Valid timeout", parser.getDbAccessParameters(),
                      config);
}

// This test checks that the parser rejects the negative value of the
// timeout parameter.
TEST_F(DbAccessParserTest, negativeTimeout) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "connect-timeout", "-1",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
}

// This test checks that the parser rejects a too large (greater than
// the max uint32_t) value of the timeout parameter.
TEST_F(DbAccessParserTest, largeTimeout) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "connect-timeout", "4294967296",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
}

// This test checks that the parser accepts the valid value of the
// port parameter.
TEST_F(DbAccessParserTest, validPort) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "port", "3306",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Valid port", parser.getDbAccessParameters(),
                      config);
}

// This test checks that the parser rejects the negative value of the
// port parameter.
TEST_F(DbAccessParserTest, negativePort) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "port", "-1",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
}

// This test checks that the parser rejects a too large (greater than
// the max uint16_t) value of the timeout parameter.
TEST_F(DbAccessParserTest, largePort) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "port", "65536",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
}

// This test checks that the parser accepts a value of zero for the
// max-row-errors parameter.
TEST_F(DbAccessParserTest, zeroMaxRowErrors) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "max-row-errors", "0",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Zero max-row-errors", parser.getDbAccessParameters(),
                      config);
}

// This test checks that the parser accepts the valid value of the
// max-row-errors parameter.
TEST_F(DbAccessParserTest, validZeroMaxRowErrors) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "max-row-errors", "50",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Valid max-row-errors", parser.getDbAccessParameters(),
                      config);
}


// This test checks that the parser rejects the negative value of the
// max-row-errors parameter.
TEST_F(DbAccessParserTest, negativeMaxRowErrors) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "max-row-errors", "-1",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
}

// This test checks that the parser rejects a too large (greater than
// the max uint32_t) value of the max-row-errors parameter.
TEST_F(DbAccessParserTest, largeMaxRowErrors) {
    const char* config[] = {"type", "memfile",
                            "name", "/opt/var/lib/kea/kea-leases6.csv",
                            "max-row-errors", "4294967296",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
}

// Check that the parser works with a valid MySQL configuration
TEST_F(DbAccessParserTest, validTypeMysql) {
    const char* config[] = {"type",     "mysql",
                            "host",     "erewhon",
                            "port",     "3306",
                            "user",     "kea",
                            "password", "keapassword",
                            "name",     "keatest",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Valid mysql", parser.getDbAccessParameters(), config);
}

// A missing 'type' keyword should cause an exception to be thrown.
TEST_F(DbAccessParserTest, missingTypeKeyword) {
    const char* config[] = {"host",     "erewhon",
                            "port",     "3306",
                            "user",     "kea",
                            "password", "keapassword",
                            "name",     "keatest",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
}

// Check reconfiguration.  Checks that incremental changes applied to the
// database configuration are incremental.
TEST_F(DbAccessParserTest, incrementalChanges) {
    const char* config1[] = {"type", "memfile",
                             NULL};

    // Applying config2 will cause a wholesale change.
    const char* config2[] = {"type",     "mysql",
                             "host",     "erewhon",
                             "port",     "3306",
                             "user",     "kea",
                             "password", "keapassword",
                             "name",     "keatest",
                             NULL};

    // Applying incremental2 should cause a change to config3.
    const char* incremental2[] = {"user",     "me",
                                  "password", "meagain",
                                  NULL};
    const char* config3[] = {"type",     "mysql",
                             "host",     "erewhon",
                             "port",     "3306",
                             "user",     "me",
                             "password", "meagain",
                             "name",     "keatest",
                             NULL};

    // incremental3 will cause an exception.  There should be no change
    // to the returned value.
    const char* incremental3[] = {"type",     "invalid",
                                  "user",     "you",
                                  "password", "youagain",
                                  NULL};

    // incremental4 is a compatible change and should cause a transition
    // to config4.
    const char* incremental4[] = {"user",     "them",
                                  "password", "",
                                  NULL};
    const char* config4[] = {"type",     "mysql",
                             "host",     "erewhon",
                             "port",     "3306",
                             "user",     "them",
                             "password", "",
                             "name",     "keatest",
                             NULL};

    TestDbAccessParser parser;

    // First configuration string should cause a representation of that string
    // to be held.
    string json_config = toJson(config1);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Initial configuration", parser.getDbAccessParameters(),
                      config1);

    // Applying a wholesale change will cause the access string to change
    // to a representation of the new configuration.
    json_config = toJson(config2);
    json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Subsequent configuration", parser.getDbAccessParameters(),
                      config2);

    // Applying an incremental change will cause the representation to change
    // incrementally.
    json_config = toJson(incremental2);
    json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Incremental configuration", parser.getDbAccessParameters(),
                      config3);

    // Applying the next incremental change should cause an exception to be
    // thrown and there be no change to the access string.
    json_config = toJson(incremental3);
    json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
    checkAccessString("Incompatible incremental change", parser.getDbAccessParameters(),
                      config3);

    // Applying an incremental change will cause the representation to change
    // incrementally.
    json_config = toJson(incremental4);
    json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    EXPECT_NO_THROW(parser.parse(json_elements));
    checkAccessString("Compatible incremental change", parser.getDbAccessParameters(),
                      config4);
}

// Check that the database access string is constructed correctly.
TEST_F(DbAccessParserTest, getDbAccessString) {
    const char* config[] = {"type",     "mysql",
                            "host",     "",
                            "name",     "keatest",
                            "password", "password with spaces",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));

    // Get the database access string
    std::string dbaccess = parser.getDbAccessString();

    // String should be either "type=mysql name=keatest" or
    // "name=keatest type=mysql". The "host" entry is null, so should not be
    // output.
    EXPECT_EQ(dbaccess, "name=keatest password='password with spaces' type=mysql");
}

// Check that the configuration is accepted for the valid value
// of "readonly".
TEST_F(DbAccessParserTest, validReadOnly) {
    const char* config[] = {"type", "mysql",
                            "user", "keatest",
                            "password", "keatest",
                            "name", "keatest",
                            "readonly", "true",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_NO_THROW(parser.parse(json_elements));

    checkAccessString("Valid readonly parameter",
                      parser.getDbAccessParameters(),
                      config);
}

// Check that for the invalid value of the "readonly" parameter
// an exception is thrown.
TEST_F(DbAccessParserTest, invalidReadOnly) {
    const char* config[] = {"type", "mysql",
                            "user", "keatest",
                            "password", "keatest",
                            "name", "keatest",
                            "readonly", "1",
                            NULL};

    string json_config = toJson(config);
    ConstElementPtr json_elements = Element::fromJSON(json_config);
    EXPECT_TRUE(json_elements);

    TestDbAccessParser parser;
    EXPECT_THROW(parser.parse(json_elements), DbConfigError);
}

// Check that multiple host storages are correctly parsed.
TEST_F(DbAccessParserTest, multipleHost) {
    const char* config1[] = {"type", "mysql",
                             "name", "keatest1",
                             NULL};
    const char* config2[] = {"type", "mysql",
                             "name", "keatest2",
                             NULL};

    string json_config1 = toJson(config1);
    string json_config2 = toJson(config2);
    ConstElementPtr json_elements1 = Element::fromJSON(json_config1);
    ConstElementPtr json_elements2 = Element::fromJSON(json_config2);

    TestDbAccessParser parser1;
    TestDbAccessParser parser2;
    EXPECT_NO_THROW(parser1.parse(json_elements1));
    EXPECT_NO_THROW(parser2.parse(json_elements2));

    checkAccessString("First config",
                      parser1.getDbAccessParameters(),
                      config1);
    checkAccessString("Second config",
                      parser2.getDbAccessParameters(),
                      config2);
}

};  // Anonymous namespace