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
// Copyright (C) 2015-2023 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/data.h>
#include <dhcp/testutils/iface_mgr_test_config.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/parsers/ifaces_config_parser.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::data;
using namespace isc::dhcp;
using namespace isc::dhcp::test;
using namespace isc::test;

namespace {

/// @brief Test fixture class for @c IfacesConfigParser
class IfacesConfigParserTest : public ::testing::Test {
protected:

    /// @brief Setup for each test.
    ///
    /// Clears the configuration in the @c CfgMgr.
    virtual void SetUp();

    /// @brief Cleans up after each test.
    ///
    /// Clears the configuration in the @c CfgMgr.
    virtual void TearDown();

};

void
IfacesConfigParserTest::SetUp() {
    CfgMgr::instance().clear();
    IfaceMgr::instance().setTestMode(true);
}

void
IfacesConfigParserTest::TearDown() {
    CfgMgr::instance().clear();
    IfaceMgr::instance().setTestMode(false);
    IfaceMgr::instance().clearIfaces();
    IfaceMgr::instance().closeSockets();
    IfaceMgr::instance().detectIfaces();
}

// This test checks that the parser correctly parses the list of interfaces
// on which the server should listen.
TEST_F(IfacesConfigParserTest, interfaces) {<--- syntax error
    // Creates fake interfaces with fake addresses.
    IfaceMgrTestConfig test_config(true);

    // Configuration with one interface.
    std::string config =
        "{ \"interfaces\": [ \"eth0\" ], \"re-detect\": false }";

    ElementPtr config_element = Element::fromJSON(config);

    // Parse the configuration.
    IfacesConfigParser parser(AF_INET, false);
    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_TRUE(cfg_iface);
    ASSERT_NO_THROW(parser.parse(cfg_iface, config_element));

    // Check it can be unparsed.
    runToElementTest<CfgIface>(config, *cfg_iface);

    // Open sockets according to the parsed configuration.
    SrvConfigPtr cfg = CfgMgr::instance().getStagingCfg();
    ASSERT_TRUE(cfg);
    ASSERT_NO_THROW(cfg->getCfgIface()->openSockets(AF_INET, 10000));

    // Only eth0 should have an open socket.
    EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET));
    EXPECT_FALSE(test_config.socketOpen("eth1", AF_INET));

    // Reset configuration.
    cfg->getCfgIface()->closeSockets();
    CfgMgr::instance().clear();

    // Try similar configuration but this time add a wildcard interface
    // to see if sockets will open on all interfaces.
    config = "{ \"interfaces\": [ \"eth0\", \"*\" ], \"re-detect\": false }";
    config_element = Element::fromJSON(config);

    cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_TRUE(cfg_iface);
    ASSERT_NO_THROW(parser.parse(cfg_iface, config_element));

    runToElementTest<CfgIface>(config, *cfg_iface);

    cfg = CfgMgr::instance().getStagingCfg();
    ASSERT_NO_THROW(cfg->getCfgIface()->openSockets(AF_INET, 10000));

    EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET));
    EXPECT_TRUE(test_config.socketOpen("eth1", AF_INET));
}

// This test checks that the parser does not re-detect interfaces in test mode.
TEST_F(IfacesConfigParserTest, testMode) {
    // Creates fake interfaces with fake addresses.
    IfaceMgrTestConfig test_config(true);

    // Configuration with wildcard..
    std::string config =
         "{ \"interfaces\": [ \"*\" ], \"re-detect\": true }";

    ElementPtr config_element = Element::fromJSON(config);

    // Parse the configuration in test mode.
    IfacesConfigParser parser(AF_INET, true);
    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_TRUE(cfg_iface);
    ASSERT_NO_THROW(parser.parse(cfg_iface, config_element));

    // Verify we still have the eth1961 interface.
    EXPECT_TRUE(IfaceMgr::instance().getIface("eth1961"));

    // Reparse in not test mode.
    IfacesConfigParser parser2(AF_INET, false);
    CfgMgr::instance().clear();
    cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_TRUE(cfg_iface);
    ASSERT_NO_THROW(parser2.parse(cfg_iface, config_element));

    // The eth1961 interface no longer exists.
    EXPECT_FALSE(IfaceMgr::instance().getIface("eth1961"));
}

// This test checks that the parsed structure can be converted back to Element
// tree.
TEST_F(IfacesConfigParserTest, toElement) {
    // Creates fake interfaces with fake addresses.
    IfaceMgrTestConfig test_config(true);

    // Configuration with one interface.
    std::string config =
        "{ \"user-context\": { \"foo\": \"bar\" }, "
        "  \"interfaces\": [ \"eth0\" ], "
        "  \"dhcp-socket-type\": \"udp\","
        "  \"outbound-interface\": \"use-routing\", "
        "  \"re-detect\": false }";

    ElementPtr config_element = Element::fromJSON(config);

    // Parse the configuration.
    IfacesConfigParser parser(AF_INET, false);
    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_TRUE(cfg_iface);
    ASSERT_NO_THROW(parser.parse(cfg_iface, config_element));

    // Check it can be unparsed.
    runToElementTest<CfgIface>(config, *cfg_iface);
}


// This test verifies that it is possible to select the raw socket
// use in the configuration for interfaces.
TEST_F(IfacesConfigParserTest, socketTypeRaw) {
    // Create the reference configuration, which we will compare
    // the parsed configuration to.
    CfgIface cfg_ref;

    // Configuration with a raw socket selected.
    std::string config = "{ ""\"interfaces\": [ ],"
        " \"dhcp-socket-type\": \"raw\","
        " \"re-detect\": false }";

    ElementPtr config_element = Element::fromJSON(config);

    // Parse the configuration.
    IfacesConfigParser parser(AF_INET, false);
    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_NO_THROW(parser.parse(cfg_iface, config_element));

    // Compare the resulting configuration with a reference
    // configuration using the raw socket.
    SrvConfigPtr cfg = CfgMgr::instance().getStagingCfg();
    ASSERT_TRUE(cfg);
    cfg_ref.useSocketType(AF_INET, CfgIface::SOCKET_RAW);
    EXPECT_TRUE(*cfg->getCfgIface() == cfg_ref);
}

// This test verifies that it is possible to select the datagram socket
// use in the configuration for interfaces.
TEST_F(IfacesConfigParserTest, socketTypeDatagram) {
    // Create the reference configuration, which we will compare
    // the parsed configuration to.
    CfgIface cfg_ref;

    // Configuration with a datagram socket selected.
    std::string config = "{ \"interfaces\": [ ],"
        " \"dhcp-socket-type\": \"udp\","
        " \"re-detect\": false }";

    ElementPtr config_element = Element::fromJSON(config);

    // Parse the configuration.
    IfacesConfigParser parser(AF_INET, false);
    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_TRUE(cfg_iface);
    ASSERT_NO_THROW(parser.parse(cfg_iface, config_element));

    // Check it can be unparsed.
    runToElementTest<CfgIface>(config, *cfg_iface);

    // Compare the resulting configuration with a reference
    // configuration using the raw socket.
    SrvConfigPtr cfg = CfgMgr::instance().getStagingCfg();
    ASSERT_TRUE(cfg);
    cfg_ref.useSocketType(AF_INET, CfgIface::SOCKET_UDP);
    ASSERT_TRUE(cfg->getCfgIface());
    EXPECT_TRUE(*cfg->getCfgIface() == cfg_ref);
}

// Test that the configuration rejects the invalid socket type.
TEST_F(IfacesConfigParserTest, socketTypeInvalid) {
    // For DHCPv4 we only accept the raw socket or datagram socket.
    IfacesConfigParser parser4(AF_INET, false);
    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    std::string config = "{ \"interfaces\": [ ],"
        "\"dhcp-socket-type\": \"default\","
        " \"re-detect\": false }";
    ElementPtr config_element = Element::fromJSON(config);
    ASSERT_THROW(parser4.parse(cfg_iface, config_element), DhcpConfigError);

    // For DHCPv6 we don't accept any socket type.
    IfacesConfigParser parser6(AF_INET6, false);
    config = "{ \"interfaces\": [ ],"
        " \"dhcp-socket-type\": \"udp\","
        " \"re-detect\": false }";
    config_element = Element::fromJSON(config);
    ASSERT_THROW(parser6.parse(cfg_iface, config_element), DhcpConfigError);
}

// Tests that outbound-interface is parsed properly.
TEST_F(IfacesConfigParserTest, outboundInterface) {
    // For DHCPv4 we accept 'use-routing' or 'same-as-inbound'.
    IfacesConfigParser parser4(AF_INET, false);

    // For DHCPv6 we don't accept this at all.
    IfacesConfigParser parser6(AF_INET6, false);

    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();

    // The default should be to use the same as client's query packet.
    EXPECT_EQ(CfgIface::SAME_AS_INBOUND, cfg_iface->getOutboundIface());

    // Value 1: use-routing
    std::string config = "{ \"interfaces\": [ ],"
        "\"outbound-interface\": \"use-routing\","
        " \"re-detect\": false }";
    ElementPtr config_element = Element::fromJSON(config);
    ASSERT_NO_THROW(parser4.parse(cfg_iface, config_element));
    EXPECT_EQ(CfgIface::USE_ROUTING, cfg_iface->getOutboundIface());
    EXPECT_THROW(parser6.parse(cfg_iface, config_element), DhcpConfigError);

    // Value 2: same-as-inbound
    config = "{ \"interfaces\": [ ],"
        "\"outbound-interface\": \"same-as-inbound\","
        " \"re-detect\": false }";
    config_element = Element::fromJSON(config);
    ASSERT_NO_THROW(parser4.parse(cfg_iface, config_element));
    EXPECT_EQ(CfgIface::SAME_AS_INBOUND, cfg_iface->getOutboundIface());
    EXPECT_THROW(parser6.parse(cfg_iface, config_element), DhcpConfigError);

    // Other values are not supported.
    config = "{ \"interfaces\": [ ],"
        "\"outbound-interface\": \"default\","
        " \"re-detect\": false }";
    config_element = Element::fromJSON(config);
    EXPECT_THROW(parser4.parse(cfg_iface, config_element), DhcpConfigError);
    EXPECT_THROW(parser6.parse(cfg_iface, config_element), DhcpConfigError);
}

// Tests that service-sockets-require-all is parsed properly.
TEST_F(IfacesConfigParserTest, serviceSocketRequireAll) {
    // Create the reference configuration, which we will compare
    // the parsed configuration to.
    CfgIface cfg_ref;

    // Configuration with a require all sockets to open selected.
    std::string config = "{ \"interfaces\": [ ],"
        " \"re-detect\": false,"
        " \"service-sockets-require-all\": true }";

    ElementPtr config_element = Element::fromJSON(config);

    // Parse the configuration.
    IfacesConfigParser parser(AF_INET, false);
    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_TRUE(cfg_iface);
    ASSERT_NO_THROW(parser.parse(cfg_iface, config_element));
    EXPECT_TRUE(cfg_iface->getServiceSocketsRequireAll());

    // Check it can be unparsed.
    runToElementTest<CfgIface>(config, *cfg_iface);
}

// Tests that service-sockets-max-retries is parsed properly.
TEST_F(IfacesConfigParserTest, serviceSocketMaxRetries) {
    // Create the reference configuration, which we will compare
    // the parsed configuration to.
    CfgIface cfg_ref;

    // Configuration with a non-zero retries selected.
    std::string config = "{ \"interfaces\": [ ],"
        " \"re-detect\": false,"
        " \"service-sockets-max-retries\": 42 }";

    ElementPtr config_element = Element::fromJSON(config);

    // Parse the configuration.
    IfacesConfigParser parser(AF_INET, false);
    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_TRUE(cfg_iface);
    ASSERT_NO_THROW(parser.parse(cfg_iface, config_element));
    EXPECT_FALSE(cfg_iface->getServiceSocketsRequireAll());

    // Configuration should contain a number of retries and a wait time.
    std::string expected_config = "{ \"interfaces\": [ ],"
        " \"re-detect\": false,"
        " \"service-sockets-retry-wait-time\": 5000,"
        " \"service-sockets-max-retries\": 42 }";

    // Check it can be unparsed.
    runToElementTest<CfgIface>(expected_config, *cfg_iface);
}

// Tests that service-sockets-retry-wait-time is parsed properly if
// service-sockets-max-retries is provided.
TEST_F(IfacesConfigParserTest, serviceSocketRetryWaitTime) {
    // Create the reference configuration, which we will compare
    // the parsed configuration to.
    CfgIface cfg_ref;

    // Configuration with a non-zero number of retries and a non-default wait time.
    std::string config = "{ \"interfaces\": [ ],"
        " \"re-detect\": false,"
        " \"service-sockets-retry-wait-time\": 4224,"
        " \"service-sockets-max-retries\": 42 }";

    ElementPtr config_element = Element::fromJSON(config);

    // Parse the configuration.
    IfacesConfigParser parser(AF_INET, false);
    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_TRUE(cfg_iface);
    ASSERT_NO_THROW(parser.parse(cfg_iface, config_element));
    EXPECT_FALSE(cfg_iface->getServiceSocketsRequireAll());

    // Check it can be unparsed.
    runToElementTest<CfgIface>(config, *cfg_iface);
}

// Tests that service-sockets-retry-wait-time is ignored if
// service-sockets-max-retries is not provided.
TEST_F(IfacesConfigParserTest, serviceSocketRetryWaitTimeWithoutMaxRetries) {
    // Create the reference configuration, which we will compare
    // the parsed configuration to.
    CfgIface cfg_ref;

    // Configuration with zero (default) retries and a non-default wait time.
    std::string config = "{ \"interfaces\": [ ],"
        " \"re-detect\": false,"
        " \"service-sockets-retry-wait-time\": 4224 }";

    ElementPtr config_element = Element::fromJSON(config);

    // Parse the configuration.
    IfacesConfigParser parser(AF_INET, false);
    CfgIfacePtr cfg_iface = CfgMgr::instance().getStagingCfg()->getCfgIface();
    ASSERT_TRUE(cfg_iface);
    ASSERT_NO_THROW(parser.parse(cfg_iface, config_element));
    EXPECT_FALSE(cfg_iface->getServiceSocketsRequireAll());

    // Retry wait time is not applicable; it is skipped.
    std::string expected_config = "{ \"interfaces\": [ ],"
        " \"re-detect\": false }";

    // Check it can be unparsed.
    runToElementTest<CfgIface>(expected_config, *cfg_iface);
}

} // end of anonymous namespace