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
// Copyright (C) 2014-2024 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 <dhcp/dhcp4.h>
#include <dhcp/dhcp6.h>
#include <dhcp/option_space.h>
#include <dhcpsrv/cfg_option_def.h>
#include <testutils/test_to_element.h>
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc;
using namespace isc::dhcp;

namespace {

// This test checks that two option definition configurations can be
// compared for equality.
TEST(CfgOptionDefTest, equal) {
    CfgOptionDef cfg1;
    CfgOptionDef cfg2;

    // Default objects must be equal.
    ASSERT_TRUE(cfg1 == cfg2);
    ASSERT_FALSE(cfg1 != cfg2);

    // Let's add the same option but to two different option spaces.
    cfg1.add(OptionDefinitionPtr(new OptionDefinition("option-foo", 5, "isc",
                                                      "uint16")));
    cfg2.add(OptionDefinitionPtr(new OptionDefinition("option-foo", 5, "dns",
                                                      "uint16")));
    // Configurations must be unequal.
    ASSERT_FALSE(cfg1 == cfg2);
    ASSERT_TRUE(cfg1 != cfg2);

    // Now, let's add them again but to original option spaces. Both objects
    // should now contain the same options under two option spaces. The
    // order should not matter so configurations should be equal.
    cfg1.add(OptionDefinitionPtr(new OptionDefinition("option-foo", 5, "dns",
                                                      "uint16")));
    cfg2.add(OptionDefinitionPtr(new OptionDefinition("option-foo", 5, "isc",
                                                      "uint16")));

    EXPECT_TRUE(cfg1 == cfg2);
    EXPECT_FALSE(cfg1 != cfg2);

}

// This test verifies that multiple option definitions can be added
// under different option spaces and then removed.
TEST(CfgOptionDefTest, getAllThenDelete) {<--- syntax error
    CfgOptionDef cfg;

    // Create a set of option definitions with codes between 100 and 109.
    for (uint16_t code = 100; code < 110; ++code) {
        std::ostringstream option_name;
        // Option name is unique, e.g. option-100, option-101 etc.
        option_name << "option-" << code;
        OptionDefinitionPtr def(new OptionDefinition(option_name.str(), code,
                                                     "isc", "uint16"));
        // We're setting id of 123 for all option definitions in this
        // code range.
        def->setId(123);

        // Add option definition to "isc" option space.
        // Option codes are not duplicated so expect no error
        // when adding them.
        ASSERT_NO_THROW(cfg.add(def));
    }

    // Create a set of option definitions with codes between 105 and 114 and
    // add them to the different option space.
    for (uint16_t code = 105; code < 115; ++code) {
        std::ostringstream option_name;
        option_name << "option-" << code;
        OptionDefinitionPtr def(new OptionDefinition(option_name.str(), code,
                                                     "abcde", "uint16"));
        // We're setting id of 234 for all option definitions in this
        // code range.
        def->setId(234);

        ASSERT_NO_THROW(cfg.add(def));
    }

    // Sanity check that all 10 option definitions are there.
    OptionDefContainerPtr option_defs1 = cfg.getAll("isc");
    ASSERT_TRUE(option_defs1);
    ASSERT_EQ(10, option_defs1->size());

    // Iterate over all option definitions and check that they have
    // valid codes. Also, their order should be the same as they
    // were added (codes 100-109).
    uint16_t code = 100;
    for (auto const& it : *option_defs1) {
        OptionDefinitionPtr def(it);
        ASSERT_TRUE(def);
        EXPECT_EQ(code, def->getCode());
        ++code;
    }

    // Sanity check that all 10 option definitions are there.
    OptionDefContainerPtr option_defs2 = cfg.getAll("abcde");
    ASSERT_TRUE(option_defs2);
    ASSERT_EQ(10, option_defs2->size());

    // Check that the option codes are valid.
    code = 105;
    for (auto const& it : *option_defs2) {
        OptionDefinitionPtr def(it);
        ASSERT_TRUE(def);
        EXPECT_EQ(code, def->getCode());
        ++code;
    }

    // Let's make one more check that the empty set is returned when
    // invalid option space is used.
    OptionDefContainerPtr option_defs3 = cfg.getAll("non-existing");
    ASSERT_TRUE(option_defs3);
    EXPECT_TRUE(option_defs3->empty());

    // Check that we can delete option definitions by id.
    uint64_t num_deleted = 0;
    ASSERT_NO_THROW(num_deleted = cfg.del(123));
    EXPECT_EQ(10, num_deleted);

    option_defs1 = cfg.getAll("isc");
    ASSERT_TRUE(option_defs1);
    ASSERT_EQ(0, option_defs1->size());

    option_defs2 = cfg.getAll("abcde");
    ASSERT_TRUE(option_defs2);
    ASSERT_EQ(10, option_defs2->size());

    // Second attempt to delete the same option definitions should
    // result in 0 deletions.
    ASSERT_NO_THROW(num_deleted = cfg.del(123));
    EXPECT_EQ(0, num_deleted);

    // Delete all other option definitions.
    ASSERT_NO_THROW(num_deleted = cfg.del(234));
    EXPECT_EQ(10, num_deleted);

    option_defs2 = cfg.getAll("abcde");
    ASSERT_TRUE(option_defs2);
    ASSERT_EQ(0, option_defs2->size());
}

// This test verifies that single option definition is correctly
// returned with get function.
TEST(CfgOptionDefTest, get) {
    CfgOptionDef cfg;
    // Create a set of option definitions with codes between 100 and 109.
    for (uint16_t code = 100; code < 110; ++code) {
        std::ostringstream option_name;
        // Option name is unique, e.g. option-100, option-101 etc.
        option_name << "option-" << code;
        OptionDefinitionPtr def(new OptionDefinition(option_name.str(), code,
                                                     "isc", "uint16"));
        // Add option definition to "isc" option space.
        // Option codes are not duplicated so expect no error
        // when adding them.
        ASSERT_NO_THROW(cfg.add(def));
    }

    // Create a set of option definitions with codes between 105 and 114 and
    // add them to the different option space.
    for (uint16_t code = 105; code < 115; ++code) {
        std::ostringstream option_name;
        option_name << "option-other-" << code;
        OptionDefinitionPtr def(new OptionDefinition(option_name.str(), code,
                                                     "abcde", "uint16"));
        ASSERT_NO_THROW(cfg.add(def));
    }

    // Try to get option definitions one by one using all codes
    // that we expect to be there.
    for (uint16_t code = 100; code < 110; ++code) {
        OptionDefinitionPtr def = cfg.get("isc", code);
        ASSERT_TRUE(def);
        // Check that the option name is in the format of 'option-[code]'.
        // That way we make sure that the options that have the same codes
        // within different option spaces are different.
        std::ostringstream option_name;
        option_name << "option-" << code;
        EXPECT_EQ(option_name.str(), def->getName());
        EXPECT_EQ(code, def->getCode());

        // Try to get the same option definition using an option name as
        // a key.
        def = cfg.get("isc", option_name.str());
        ASSERT_TRUE(def);
        EXPECT_EQ(code, def->getCode());
    }

    // Check that the option codes are valid.
    for (uint16_t code = 105; code < 115; ++code) {
        OptionDefinitionPtr def = cfg.get("abcde", code);
        ASSERT_TRUE(def);
        // Check that the option name is in the format of 'option-other-[code]'.
        // That way we make sure that the options that have the same codes
        // within different option spaces are different.
        std::ostringstream option_name;
        option_name << "option-other-" << code;
        EXPECT_EQ(option_name.str(), def->getName());
        EXPECT_EQ(code, def->getCode());

        // Try to get the same option definition using an option name as
        // a key.
        def = cfg.get("abcde", option_name.str());
        ASSERT_TRUE(def);
        EXPECT_EQ(code, def->getCode());
    }

    // Check that an option definition can be added to the standard
    // (dhcp4 and dhcp6) option spaces when the option code is not
    // reserved by the standard option.
    OptionDefinitionPtr def6(new OptionDefinition("option-foo", 1000,
                                                  DHCP6_OPTION_SPACE,
                                                  "uint16"));
    EXPECT_NO_THROW(cfg.add(def6));

    OptionDefinitionPtr def4(new OptionDefinition("option-foo", 222,
                                                  DHCP4_OPTION_SPACE,
                                                  "uint16"));
    EXPECT_NO_THROW(cfg.add(def4));

    // Try to query the option definition from an non-existing
    // option space and expect NULL pointer.
    OptionDefinitionPtr def = cfg.get("non-existing", 56);
    EXPECT_FALSE(def);

    // Try to get the non-existing option definition from an
    // existing option space.
    EXPECT_FALSE(cfg.get("isc", 56));
}

// This test verifies that it is not allowed to override a definition of the
// standard option which has its definition defined in libdhcp++, but it is
// allowed to create a definition for the standard option which doesn't have
// its definition in libdhcp++.
TEST(CfgOptionDefTest, overrideStdOptionDef) {
    CfgOptionDef cfg;
    OptionDefinitionPtr def;
    // There is a definition for routers option in libdhcp++, so an attempt
    // to add (override) another definition for this option should fail.
    def.reset(new OptionDefinition("routers", DHO_ROUTERS,
                                   DHCP4_OPTION_SPACE, "uint32"));
    EXPECT_THROW(cfg.add(def), isc::BadValue);

    // Check code duplicate (same code, different name).
    def.reset(new OptionDefinition("routers-bis", DHO_ROUTERS,
                                   DHCP4_OPTION_SPACE, "uint32"));
    EXPECT_THROW(cfg.add(def), isc::BadValue);

    // Check name duplicate (different code, same name).
    def.reset(new OptionDefinition("routers", 170, DHCP4_OPTION_SPACE,
                                   "uint32"));
    EXPECT_THROW(cfg.add(def), isc::BadValue);

    /// There is no definition for unassigned option 170.
    def.reset(new OptionDefinition("unassigned-option-170", 170,
                                   DHCP4_OPTION_SPACE, "string"));
    EXPECT_NO_THROW(cfg.add(def));

    // It is not allowed to override the definition of the option which
    // has its definition in the libdhcp++.
    def.reset(new OptionDefinition("sntp-servers", D6O_SNTP_SERVERS,
                                   DHCP6_OPTION_SPACE, "ipv4-address"));
    EXPECT_THROW(cfg.add(def), isc::BadValue);
    // There is no definition for option 163 in libdhcp++ yet, so it should
    // be possible provide a custom definition.
    def.reset(new OptionDefinition("geolocation", 163, DHCP6_OPTION_SPACE,
                                   "uint32"));
    EXPECT_NO_THROW(cfg.add(def));
}

// This test verifies that the function that adds new option definition
// throws exceptions when arguments are invalid.
TEST(CfgOptionDefTest, addNegative) {
    CfgOptionDef cfg;

    OptionDefinitionPtr def(new OptionDefinition("option-foo", 1000, "isc",
                                                 "uint16"));

    // Try NULL option definition.
    ASSERT_THROW(cfg.add(OptionDefinitionPtr()),
                 isc::dhcp::MalformedOptionDefinition);
    // Try adding option definition twice and make sure that it
    // fails on the second attempt.
    ASSERT_NO_THROW(cfg.add(def));
    EXPECT_THROW(cfg.add(def), DuplicateOptionDefinition);
}

// This test verifies that the function that unparses configuration
// works as expected.
TEST(CfgOptionDefTest, unparse) {
    CfgOptionDef cfg;

    // Add some options.
    cfg.add(OptionDefinitionPtr(new
        OptionDefinition("option-foo", 5, "isc", "uint16")));
    cfg.add(OptionDefinitionPtr(new
        OptionDefinition("option-bar", 5, "dns", "uint16", true)));
    cfg.add(OptionDefinitionPtr(new
        OptionDefinition("option-baz", 6, "isc", "uint16", "dns")));
    OptionDefinitionPtr rec(new OptionDefinition("option-rec", 6, "dns", "record"));
    std::string json = "{ \"comment\": \"foo\", \"bar\": 1 }";
    rec->setContext(data::Element::fromJSON(json));
    rec->addRecordField("uint16");
    rec->addRecordField("uint16");
    cfg.add(rec);

    // Unparse
    std::string expected = "[\n"
        "{\n"
        "    \"name\": \"option-bar\",\n"
        "    \"code\": 5,\n"
        "    \"type\": \"uint16\",\n"
        "    \"array\": true,\n"
        "    \"record-types\": \"\",\n"
        "    \"encapsulate\": \"\",\n"
        "    \"space\": \"dns\"\n"
        "},{\n"
        "    \"name\": \"option-rec\",\n"
        "    \"code\": 6,\n"
        "    \"type\": \"record\",\n"
        "    \"array\": false,\n"
        "    \"record-types\": \"uint16, uint16\",\n"
        "    \"encapsulate\": \"\",\n"
        "    \"space\": \"dns\",\n"
        "    \"user-context\": { \"comment\": \"foo\", \"bar\": 1 }\n"
        "},{\n"
        "    \"name\": \"option-foo\",\n"
        "    \"code\": 5,\n"
        "    \"type\": \"uint16\",\n"
        "    \"array\": false,\n"
        "    \"record-types\": \"\",\n"
        "    \"encapsulate\": \"\",\n"
        "    \"space\": \"isc\"\n"
        "},{\n"
        "    \"name\": \"option-baz\",\n"
        "    \"code\": 6,\n"
        "    \"type\": \"uint16\",\n"
        "    \"array\": false,\n"
        "    \"record-types\": \"\",\n"
        "    \"encapsulate\": \"dns\",\n"
        "    \"space\": \"isc\"\n"
        "}]\n";
    isc::test::runToElementTest<CfgOptionDef>(expected, cfg);
}

// This test verifies that configured option definitions can be merged
// correctly.
TEST(CfgOptionDefTest, merge) {
    CfgOptionDef to;         // Configuration we are merging to.

    // Add some options to the "to" config.
    to.add((OptionDefinitionPtr(new OptionDefinition("one", 1, "isc", "uint16"))));
    to.add((OptionDefinitionPtr(new OptionDefinition("two", 2, "isc", "uint16"))));
    to.add((OptionDefinitionPtr(new OptionDefinition("three", 3, "fluff", "uint16"))));
    to.add((OptionDefinitionPtr(new OptionDefinition("four", 4, "fluff", "uint16"))));

    // Clone the "to" config and use that for merging.
    CfgOptionDef to_clone;
    to.copyTo(to_clone);

    // Ensure they are equal before we do anything.
    ASSERT_TRUE(to.equals(to_clone));

    // Merge from an empty config.
    CfgOptionDef empty_from;
    ASSERT_NO_THROW(to_clone.merge(empty_from));

    // Should have the same content as before.
    ASSERT_TRUE(to.equals(to_clone));

    // Construct a non-empty "from" config.
    // Options "two" and "three" will be updated definitions and "five" will be new.
    CfgOptionDef from;
    from.add((OptionDefinitionPtr(new OptionDefinition("two", 22, "isc", "uint16"))));
    from.add((OptionDefinitionPtr(new OptionDefinition("three", 3, "fluff", "string"))));
    from.add((OptionDefinitionPtr(new OptionDefinition("five", 5, "fluff", "uint16"))));

    // Now let's clone "from" config and use that manually construct the expected config.
    CfgOptionDef expected;
    from.copyTo(expected);
    expected.add((OptionDefinitionPtr(new OptionDefinition("one", 1, "isc", "uint16"))));
    expected.add((OptionDefinitionPtr(new OptionDefinition("four", 4, "fluff", "uint16"))));

    // Do the merge.
    ASSERT_NO_THROW(to_clone.merge(from));

    // Verify we have the expected content.
    ASSERT_TRUE(expected.equals(to_clone));
}

}