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
// 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 <dhcp/testutils/iface_mgr_test_config.h>
#include <dhcp6/tests/dhcp6_test_utils.h>
#include <dhcp6/tests/dhcp6_client.h>
#include <dhcp/option6_addrlst.h>
#include <dhcp/option6_client_fqdn.h>
#include <stats/stats_mgr.h>

using namespace isc;
using namespace isc::dhcp;
using namespace isc::dhcp::test;

namespace {

/// @brief Set of JSON configurations used by the Information-Request unit tests.
///
/// - Configuration 0:
///   - one subnet used on eth0 interface
///     - with address and prefix pools
///     - dns-servers option
/// - Configuration 1:
///   - one subnet used on eth0 interface
///     - no addresses or prefixes
///     - domain-search option
/// - Configuration 2:
///   - one subnet used on eth0 interface
///     - dns-servers option for subnet
///   - sip-servers defined in global scope
/// - Configuration 3:
///   - nis-server, nis-domain specified in global scope
///   - no subnets defined
const char* CONFIGS[] = {
    // Configuration 0
    "{ \"interfaces-config\": {"
        "  \"interfaces\": [ \"*\" ]"
        "},"
        "\"preferred-lifetime\": 3000,"
        "\"rebind-timer\": 2000, "
        "\"renew-timer\": 1000, "
        "\"subnet6\": [ { "
        "    \"id\": 1, "
        "    \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
        "    \"pd-pools\": ["
        "        { \"prefix\": \"2001:db8:3::\", "
        "          \"prefix-len\": 48, "
        "          \"delegated-len\": 64"
        "        } ],"
        "    \"option-data\": [ {"
        "        \"name\": \"dns-servers\","
        "        \"data\": \"2001:db8::1, 2001:db8::2\""
        "    } ],"
        "    \"subnet\": \"2001:db8::/32\", "
        "    \"interface\": \"eth0\""
        " } ],"
        "\"valid-lifetime\": 4000 }",

    // Configuration 1
    "{ \"interfaces-config\": {"
        "  \"interfaces\": [ \"*\" ]"
        "},"
        "\"preferred-lifetime\": 3000,"
        "\"rebind-timer\": 2000, "
        "\"renew-timer\": 1000, "
        "\"subnet6\": [ { "
        "    \"id\": 1, "
        "    \"option-data\": [ {"
        "    \"name\": \"sip-server-addr\","
        "    \"data\": \"2001:db8::abcd\""
        "    } ],"
        "    \"subnet\": \"2001:db8::/32\", "
        "    \"interface\": \"eth0\""
        " } ],"
        "\"valid-lifetime\": 4000 }",

    // Configuration 2
    "{ \"interfaces-config\": {"
        "  \"interfaces\": [ \"*\" ]"
        "},"
        "\"preferred-lifetime\": 3000,"
        "\"rebind-timer\": 2000, "
        "\"renew-timer\": 1000, "
        "    \"option-data\": [ {"
        "        \"name\": \"sip-server-addr\","
        "        \"data\": \"2001:db8::1\""
        "    } ],"
        "\"subnet6\": [ { "
        "    \"id\": 1, "
        "    \"subnet\": \"2001:db8::/32\", "
        "    \"interface\": \"eth0\","
        "    \"option-data\": [ {"
        "        \"name\": \"dns-servers\","
        "        \"data\": \"2001:db8::2\""
        "    } ]"
        " } ],"
        "\"valid-lifetime\": 4000 }",

    // Configuration 3
    "{ \"interfaces-config\": {"
        "  \"interfaces\": [ \"*\" ]"
        "},"
        "\"option-data\": [ {"
        "    \"name\": \"nis-servers\","
        "    \"data\": \"2001:db8::1, 2001:db8::2\""
        " } ]"
        "}"
};

/// @brief Test fixture class for testing 4-way exchange: Solicit-Advertise,
/// Request-Reply.
class InfRequestTest : public Dhcpv6SrvTest {
public:
    /// @brief Constructor.
    ///
    /// Sets up fake interfaces.
    InfRequestTest()
        : Dhcpv6SrvTest(),
          iface_mgr_test_config_(true) {

        // Let's wipe all existing statistics.
        isc::stats::StatsMgr::instance().removeAll();
    }

    /// @brief Destructor.
    ///
    /// Removes any statistics that may have been set.
    ~InfRequestTest() {
        // Let's wipe all existing statistics.
        isc::stats::StatsMgr::instance().removeAll();
    }

    /// @brief Interface Manager's fake configuration control.
    IfaceMgrTestConfig iface_mgr_test_config_;
};

/// Check that server processes correctly an incoming inf-request in a
/// typical subnet that has also address and prefix pools.
TEST_F(InfRequestTest, infRequestBasic) {<--- syntax error
    Dhcp6Client client;

    // Configure client to request IA_PD.
    configure(CONFIGS[0], *client.getServer());
    // Make sure we ended-up having expected number of subnets configured.
    const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
        getCfgSubnets6()->getAll();
    ASSERT_EQ(1, subnets->size());

    // Perform 2-way exchange (Inf-request/reply)
    client.requestOption(D6O_NAME_SERVERS);
    ASSERT_NO_THROW(client.doInfRequest());

    // Confirm that there's a response
    Pkt6Ptr response = client.getContext().response_;
    ASSERT_TRUE(response);

    // Check that it contains our client-id
    OptionPtr client_id = response->getOption(D6O_CLIENTID);
    ASSERT_TRUE(client_id);
    EXPECT_TRUE(compareOptions(client_id, client.getClientId()));

    // Check that it contains proper server-id
    OptionPtr server_id = response->getOption(D6O_SERVERID);
    ASSERT_TRUE(server_id);
    EXPECT_TRUE(compareOptions(server_id, client.getServer()->getServerID()));

    // Check that we received requested DNS servers option
    Option6AddrLstPtr dns = boost::dynamic_pointer_cast<Option6AddrLst>
                            (response->getOption(D6O_NAME_SERVERS));
    ASSERT_TRUE(dns);
    Option6AddrLst::AddressContainer addrs = dns->getAddresses();
    ASSERT_EQ(2, addrs.size());
    EXPECT_EQ("2001:db8::1", addrs[0].toText());
    EXPECT_EQ("2001:db8::2", addrs[1].toText());
}

/// Check that server processes correctly an incoming inf-request
/// that does not hold client-id. It's so called anonymous inf-request.
/// Uncommon, but certainly valid behavior.
TEST_F(InfRequestTest, infRequestAnonymous) {
    Dhcp6Client client;

    // Configure client to request IA_PD.
    configure(CONFIGS[0], *client.getServer());
    // Make sure we ended-up having expected number of subnets configured.
    const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
        getCfgSubnets6()->getAll();
    ASSERT_EQ(1, subnets->size());

    // Perform 2-way exchange (Inf-request/reply)
    client.requestOption(D6O_NAME_SERVERS);
    client.useClientId(false);
    ASSERT_NO_THROW(client.doInfRequest());

    // Confirm that there's a response
    Pkt6Ptr response = client.getContext().response_;
    ASSERT_TRUE(response);

    // Check that we received the requested DNS servers option
    Option6AddrLstPtr dns = boost::dynamic_pointer_cast<Option6AddrLst>
                            (response->getOption(D6O_NAME_SERVERS));
    ASSERT_TRUE(dns);
    Option6AddrLst::AddressContainer addrs = dns->getAddresses();
    ASSERT_EQ(2, addrs.size());
    EXPECT_EQ("2001:db8::1", addrs[0].toText());
    EXPECT_EQ("2001:db8::2", addrs[1].toText());
}

/// Check that server processes correctly an incoming inf-request
/// if there is a subnet without any addresses or prefixes configured.
TEST_F(InfRequestTest, infRequestStateless) {
    Dhcp6Client client;

    // Configure client to request IA_PD.
    configure(CONFIGS[1], *client.getServer());
    // Make sure we ended-up having expected number of subnets configured.
    const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
        getCfgSubnets6()->getAll();
    ASSERT_EQ(1, subnets->size());

    // Perform 2-way exchange (Inf-request/reply)
    client.requestOption(D6O_SIP_SERVERS_ADDR);
    ASSERT_NO_THROW(client.doInfRequest());

    // Confirm that there's a response
    Pkt6Ptr response = client.getContext().response_;
    ASSERT_TRUE(response);

    // Check that we received the requested SIP servers option
    Option6AddrLstPtr sip = boost::dynamic_pointer_cast<Option6AddrLst>
                            (response->getOption(D6O_SIP_SERVERS_ADDR));
    ASSERT_TRUE(sip);
    Option6AddrLst::AddressContainer addrs = sip->getAddresses();
    ASSERT_EQ(1, addrs.size());
    EXPECT_EQ("2001:db8::abcd", addrs[0].toText());
}

/// Check that server processes correctly an incoming inf-request
/// if there are options defined at both global and subnet scope.
TEST_F(InfRequestTest, infRequestSubnetAndGlobal) {
    Dhcp6Client client;

    // Configure client to request IA_PD.
    configure(CONFIGS[2], *client.getServer());
    // Make sure we ended-up having expected number of subnets configured.
    const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
        getCfgSubnets6()->getAll();
    ASSERT_EQ(1, subnets->size());

    // Perform 2-way exchange (Inf-request/reply)
    client.requestOption(D6O_SIP_SERVERS_ADDR);
    client.requestOption(D6O_NAME_SERVERS);
    ASSERT_NO_THROW(client.doInfRequest());

    // Confirm that there's a response
    Pkt6Ptr response = client.getContext().response_;
    ASSERT_TRUE(response);

    // Check that we received the requested sip servers option
    Option6AddrLstPtr sip = boost::dynamic_pointer_cast<Option6AddrLst>
                            (response->getOption(D6O_SIP_SERVERS_ADDR));
    ASSERT_TRUE(sip);
    Option6AddrLst::AddressContainer addrs = sip->getAddresses();
    ASSERT_EQ(1, addrs.size());
    EXPECT_EQ("2001:db8::1", addrs[0].toText());

    // Check that we received the requested dns servers option
    Option6AddrLstPtr dns = boost::dynamic_pointer_cast<Option6AddrLst>
                            (response->getOption(D6O_NAME_SERVERS));
    ASSERT_TRUE(dns);
    addrs = dns->getAddresses();
    ASSERT_EQ(1, addrs.size());
    EXPECT_EQ("2001:db8::2", addrs[0].toText());
}

/// Check that server processes correctly an incoming inf-request
/// if there are options defined at global scope only (no subnets).
TEST_F(InfRequestTest, infRequestNoSubnets) {
    Dhcp6Client client;

    // Configure client to request IA_PD.
    configure(CONFIGS[3], *client.getServer());
    // Make sure we ended-up having expected number of subnets configured.
    const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
        getCfgSubnets6()->getAll();
    ASSERT_EQ(0, subnets->size());

    // Perform 2-way exchange (Inf-request/reply)
    client.requestOption(D6O_NIS_SERVERS);
    ASSERT_NO_THROW(client.doInfRequest());

    // Confirm that there's a response
    Pkt6Ptr response = client.getContext().response_;
    ASSERT_TRUE(response);

    // Check that we received the requested sip servers option
    Option6AddrLstPtr nis = boost::dynamic_pointer_cast<Option6AddrLst>
                            (response->getOption(D6O_NIS_SERVERS));
    ASSERT_TRUE(nis);
    Option6AddrLst::AddressContainer addrs = nis->getAddresses();
    ASSERT_EQ(2, addrs.size());
    EXPECT_EQ("2001:db8::1", addrs[0].toText());
    EXPECT_EQ("2001:db8::2", addrs[1].toText());
}

/// Check that server processes correctly an incoming inf-request in a
/// typical subnet that has also address and prefix pools.
TEST_F(InfRequestTest, infRequestStats) {
    Dhcp6Client client;

    // Configure client to request IA_PD.
    configure(CONFIGS[0], *client.getServer());
    // Make sure we ended-up having expected number of subnets configured.
    const Subnet6Collection* subnets = CfgMgr::instance().getCurrentCfg()->
        getCfgSubnets6()->getAll();
    ASSERT_EQ(1, subnets->size());

    // Check that the tested statistics is initially set to 0
    using namespace isc::stats;
    StatsMgr& mgr = StatsMgr::instance();
    ObservationPtr pkt6_rcvd = mgr.getObservation("pkt6-received");
    ObservationPtr pkt6_infreq_rcvd = mgr.getObservation("pkt6-infrequest-received");
    ObservationPtr pkt6_reply_sent = mgr.getObservation("pkt6-reply-sent");
    ObservationPtr pkt6_sent = mgr.getObservation("pkt6-sent");
    ASSERT_TRUE(pkt6_rcvd);
    ASSERT_TRUE(pkt6_infreq_rcvd);
    ASSERT_TRUE(pkt6_reply_sent);
    ASSERT_TRUE(pkt6_sent);
    EXPECT_EQ(0, pkt6_rcvd->getInteger().first);
    EXPECT_EQ(0, pkt6_infreq_rcvd->getInteger().first);
    EXPECT_EQ(0, pkt6_reply_sent->getInteger().first);
    EXPECT_EQ(0, pkt6_sent->getInteger().first);

    // Perform 2-way exchange (Inf-request/reply)
    client.requestOption(D6O_NAME_SERVERS);
    ASSERT_NO_THROW(client.doInfRequest());

    // Confirm that there's a response
    Pkt6Ptr response = client.getContext().response_;
    ASSERT_TRUE(response);

    pkt6_rcvd = mgr.getObservation("pkt6-received");
    pkt6_infreq_rcvd = mgr.getObservation("pkt6-infrequest-received");
    pkt6_reply_sent = mgr.getObservation("pkt6-reply-sent");
    pkt6_sent = mgr.getObservation("pkt6-sent");

    ASSERT_TRUE(pkt6_rcvd);
    ASSERT_TRUE(pkt6_infreq_rcvd);
    ASSERT_TRUE(pkt6_reply_sent);
    ASSERT_TRUE(pkt6_sent);

    // They also must have expected values.
    EXPECT_EQ(1, pkt6_rcvd->getInteger().first);
    EXPECT_EQ(1, pkt6_infreq_rcvd->getInteger().first);
    EXPECT_EQ(1, pkt6_reply_sent->getInteger().first);
    EXPECT_EQ(1, pkt6_sent->getInteger().first);
}

} // end of anonymous namespace