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
// Copyright (C) 2018-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 <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <dhcpsrv/subnet_id.h>
#include <testutils/gtest_utils.h>
#include <yang/adaptor_subnet.h>

using namespace std;
using namespace isc;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::yang;

namespace {

// Verifies how collectID handles a subnet entry without ID.
TEST(AdaptorSubnetTest, collectNoId) {
    string config = "{\n"
        " \"subnet\": \"192.0.2.0/24\"\n"
        "}";
    ConstElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    SubnetIDSet set;
    bool ret = true;
    ASSERT_NO_THROW_LOG(ret = AdaptorSubnet::collectID(json, set));
    EXPECT_FALSE(ret);
    EXPECT_EQ(0, set.size());
}

// Verifies how collectID handles a subnet entry with an ID.
TEST(AdaptorSubnetTest, collectId) {<--- syntax error
    string config = "{\n"
        " \"subnet\": \"192.0.2.0/24\",\n"
        " \"id\": 123\n"
        "}";
    ConstElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    SubnetIDSet set;
    bool ret = false;
    ASSERT_NO_THROW_LOG(ret = AdaptorSubnet::collectID(json, set));
    EXPECT_TRUE(ret);
    EXPECT_EQ(1, set.size());
    EXPECT_EQ(1, set.count(123));
}

// Verifies how collectID handles a subnet entry with an ID which is
// already known: the set is not updated.
TEST(AdaptorSubnetTest, collectKnownId) {
    string config = "{\n"
        " \"subnet\": \"192.0.2.0/24\",\n"
        " \"id\": 123\n"
        "}";
    ConstElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    SubnetIDSet set = { 123 };
    bool ret = false;
    ASSERT_NO_THROW_LOG(ret = AdaptorSubnet::collectID(json, set));
    EXPECT_TRUE(ret);
    EXPECT_EQ(1, set.size());
    EXPECT_EQ(1, set.count(123));
}

// Verifies how assignID handles a subnet entry without ID: the next ID
// is assigned, the set is updated and the next ID is incremented.
TEST(AdaptorSubnetTest, assignNoId) {
    string config = "{\n"
        " \"subnet\": \"192.0.2.0/24\"\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    SubnetIDSet set;
    SubnetID next_id = 123;
    ASSERT_NO_THROW_LOG(AdaptorSubnet::assignID(json, set, next_id));
    EXPECT_FALSE(copied->equals(*json));
    EXPECT_EQ(1, set.size());
    EXPECT_EQ(1, set.count(123));
    EXPECT_EQ(124, next_id);
    ConstElementPtr id = json->get("id");
    ASSERT_TRUE(id);
    ASSERT_EQ(Element::integer, id->getType());
    EXPECT_EQ(123, id->intValue());
}

// Verifies how assignID handles a subnet entry without ID but with the
// candidate ID already used: the used value is skipped.
TEST(AdaptorSubnetTest, assignNoIdUsed) {
    string config = "{\n"
        " \"subnet\": \"192.0.2.0/24\"\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    SubnetIDSet set = { 123 };
    SubnetID next_id = 123;
    ASSERT_NO_THROW_LOG(AdaptorSubnet::assignID(json, set, next_id));
    EXPECT_FALSE(copied->equals(*json));
    EXPECT_EQ(2, set.size());
    EXPECT_EQ(1, set.count(123));
    EXPECT_EQ(1, set.count(124));
    EXPECT_EQ(125, next_id);
    ConstElementPtr id = json->get("id");
    ASSERT_TRUE(id);
    ASSERT_EQ(Element::integer, id->getType());
    EXPECT_EQ(124, id->intValue());
}

// Verifies how assignID handles a subnet entry with ID: no change.
TEST(AdaptorSubnetTest, assignId) {
    string config = "{\n"
        " \"subnet\": \"192.0.2.0/24\",\n"
        " \"id\": 123\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    SubnetIDSet set; // ignored.
    SubnetID next_id = 123; // ignored.
    ASSERT_NO_THROW_LOG(AdaptorSubnet::assignID(json, set, next_id));
    EXPECT_TRUE(copied->equals(*json));
    EXPECT_EQ(0, set.size());
    EXPECT_EQ(123, next_id);
}

// Verifies how updateRelay handles a subnet entry without relay: no change.
TEST(AdaptorSubnetTest, updateNoRelay) {
    string config = "{\n"
        " \"subnet\": \"192.0.2.0/24\"\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    ASSERT_NO_THROW_LOG(AdaptorSubnet::updateRelay(json));
    EXPECT_TRUE(copied->equals(*json));
}

// Verifies how updateRelay handles a subnet entry with empty relay:
// the relay entry is useless and removed.
TEST(AdaptorSubnetTest, updateEmptyRelay) {
    string config = "{\n"
        " \"subnet\": \"192.0.2.0/24\",\n"
        " \"relay\": { }\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    ASSERT_NO_THROW_LOG(AdaptorSubnet::updateRelay(json));
    EXPECT_FALSE(copied->equals(*json));
    EXPECT_FALSE(json->get("relay"));
}

// Verifies how updateRelay handles a subnet entry with relay which
// has empty addresses: the relay entry is useless and removed.
TEST(AdaptorSubnetTest, updateRelayEmptyAddresses) {
    string config = "{\n"
        " \"subnet\": \"192.0.2.0/24\",\n"
        " \"relay\": {\n"
        "     \"ip-addresses\": [ ]\n"
        " }\n"
        "}";
    ElementPtr json;
    ASSERT_NO_THROW_LOG(json = Element::fromJSON(config));
    ConstElementPtr copied = copy(json);
    ASSERT_NO_THROW_LOG(AdaptorSubnet::updateRelay(json));
    EXPECT_FALSE(copied->equals(*json));
    EXPECT_FALSE(json->get("relay"));
}

}  // anonymous namespace