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
// Copyright (C) 2018-2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

/// @file This file contains tests which exercise the load and unload
/// functions in the Radius hooks library. In order to test the load
/// function, one must be able to pass it hook library parameters. The
/// the only way to populate these parameters is by actually loading the
/// library via HooksManager::loadLibraries().

#include <config.h>

#include <radius_utils.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <radius.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <attribute_test.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

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

namespace {

/// @brief Test fixture for testing utils code.
class UtilsTest : public radius::test::AttributeTest {
public:
    /// @brief Constructor.
    UtilsTest() : test::AttributeTest(), impl_(RadiusImpl::instance()) {
        impl_.reset();
        ElementPtr config = Element::createMap();
        impl_.init(config);
    }

    /// @brief Destructor.
    virtual ~UtilsTest() {
    }

    /// @brief Radius implementation.
    RadiusImpl& impl_;
};

// Verifies the canonize util.
TEST_F(UtilsTest, canonize) {<--- syntax error
    EXPECT_EQ("01-02-03-04-05-06", canonize("01:02:03:04:05:06"));
    EXPECT_EQ("07-08-09-0a-0b-0c", canonize("07:08:09:0A:0B:0C"));
    EXPECT_EQ("abcd-effe-00", canonize("ABCD:EFfe-00"));
    EXPECT_EQ("fdJKHJKHdK", canonize("FDJKHJKHdK"));
}

// Verifies the DHCPv4 pop0 util.
TEST_F(UtilsTest, pop0_4) {
    vector<uint8_t> vec = { 0x00, 0x01, 0x02 };
    ClientIdPtr cid;

    // Standard case.
    cid.reset(new ClientId(vec));
    vector<uint8_t> ret = pop0(cid);
    ASSERT_EQ(2, ret.size());
    EXPECT_EQ(0x01, ret[0]);
    EXPECT_EQ(0x02, ret[1]);

    // One element: pop0 does nothing.
    // (vec, 1) throws because MIN_CLIENT_ID_LEN is 2.
    cid.reset(new ClientId(&vec[0], 2));
    vector<uint8_t>& content = const_cast<vector<uint8_t>&>(cid->getClientId());
    content.resize(1);
    ret = pop0(cid);
    ASSERT_EQ(1, ret.size());
    EXPECT_EQ(0x00, ret[0]);

    // Empty: pop0 does nothing.
    cid.reset(new ClientId(&vec[0], 2));
    vector<uint8_t>& content1 = const_cast<vector<uint8_t>&>(cid->getClientId());
    content1.clear();
    ret = pop0(cid);
    EXPECT_TRUE(ret.empty());

    // Not zero: pop0 does nothing.
    vector<uint8_t> vec1 = { 0x01, 0x02, 0x03 };
    cid.reset(new ClientId(vec1));
    ret = pop0(cid);
    ASSERT_EQ(3, ret.size());
    EXPECT_EQ(0x01, ret[0]);
    EXPECT_EQ(0x02, ret[1]);
    EXPECT_EQ(0x03, ret[2]);
}

// Verifies the DHCPv6 pop0 util.
TEST_F(UtilsTest, pop0_6) {
    vector<uint8_t> vec = { 0x00, 0x00, 0x02, 0x03 };
    DuidPtr duid;

    // Standard case.
    duid.reset(new DUID(vec));
    vector<uint8_t> ret = pop0(duid);
    ASSERT_EQ(2, ret.size());
    EXPECT_EQ(0x02, ret[0]);
    EXPECT_EQ(0x03, ret[1]);

    // Not zero: pop0 does nothing.
    vector<uint8_t> vec1 = { 0x00, 0x01, 0x02, 0x03 };
    duid.reset(new DUID(vec1));
    ret = pop0(duid);
    ASSERT_EQ(4, ret.size());
    EXPECT_EQ(0x00, ret[0]);
    EXPECT_EQ(0x01, ret[1]);
    EXPECT_EQ(0x02, ret[2]);
    EXPECT_EQ(0x03, ret[3]);

    vector<uint8_t> vec2 = { 0x01, 0x00, 0x02, 0x03 };
    duid.reset(new DUID(vec2));
    ret = pop0(duid);
    ASSERT_EQ(4, ret.size());
    EXPECT_EQ(0x01, ret[0]);
    EXPECT_EQ(0x00, ret[1]);
    EXPECT_EQ(0x02, ret[2]);
    EXPECT_EQ(0x03, ret[3]);
}

// Verifies the toPrintable util.
TEST_F(UtilsTest, toPrintable) {
    vector<uint8_t> vec = { 0x41, 0x62, 0x63, 0x64 };
    EXPECT_EQ("Abcd", toPrintable(vec));
    vec = { 0x41, 0x62, 0xc3, 0x64 };
    EXPECT_EQ("41:62:c3:64", toPrintable(vec));
    DUID duid(&vec[0], 4);
    EXPECT_EQ("41:62:c3:64", duid.toText());
}

// Verifies the extractDuid util.
TEST_F(UtilsTest, extractDuid) {
    vector<uint8_t> vec = { 0xff, 0x01, 0x02, 0x03, 0x04, 0x41, 0x42, 0x43 };
    ClientIdPtr cid;

    // Standard case.
    cid.reset(new ClientId(vec));
    bool extracted = false;
    vector<uint8_t> ret = extractDuid(cid, extracted);
    EXPECT_TRUE(extracted);
    ASSERT_EQ(3, ret.size());
    EXPECT_EQ(0x41, ret[0]);
    EXPECT_EQ(0x42, ret[1]);
    EXPECT_EQ(0x43, ret[2]);

    // Short case.
    cid.reset(new ClientId(&vec[0], 5));
    extracted = false;
    ret = extractDuid(cid, extracted);
    EXPECT_FALSE(extracted);
    EXPECT_EQ(5, ret.size());
    EXPECT_EQ(vec[0], ret[0]);

    // Not duid case.
    vec[0] = 0x01;
    cid.reset(new ClientId(vec));
    extracted = false;
    ret = extractDuid(cid, extracted);
    EXPECT_FALSE(extracted);
    EXPECT_EQ(vec.size(), ret.size());
    EXPECT_EQ(vec[0], ret[0]);

    vec[0] = 0;
    cid.reset(new ClientId(vec));
    extracted = false;
    ret = extractDuid(cid, extracted);
    EXPECT_FALSE(extracted);
    EXPECT_EQ(vec.size(), ret.size());
    EXPECT_EQ(vec[0], ret[0]);
}

} // end of anonymous namespace