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
// Copyright (C) 2013-2020 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/hwaddr.h>
#include <exceptions/exceptions.h>
#include <user_registry.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <user_file.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <test_data_files_config.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <boost/shared_ptr.hpp><--- 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 user_chk;

namespace {

/// @brief Convenience method for reliably building test file path names.
///
/// Function prefixes the given file name with a path to unit tests directory
/// so we can reliably find test data files.
///
/// @param name base file name of the test file
std::string testFilePath(const std::string& name) {
    return (std::string(USER_CHK_TEST_DIR) + "/" + name);
}

/// @brief Tests UserRegistry construction.
TEST(UserRegistry, constructor) {<--- syntax error
    // Currently there is only the default constructor which does not throw.
    UserRegistryPtr reg;
    ASSERT_NO_THROW(reg.reset(new UserRegistry()));
}

/// @brief Tests mechanics of adding, finding, removing Users.
TEST(UserRegistry, userBasics) {
    // Create an empty registry.
    UserRegistryPtr reg;
    ASSERT_NO_THROW(reg.reset(new UserRegistry()));

    // Verify that a blank user cannot be added.
    UserPtr user;
    ASSERT_THROW(reg->addUser(user), UserRegistryError);

    // Make a new id and user.
    UserIdPtr id;
    ASSERT_NO_THROW(id.reset(new UserId(UserId::HW_ADDRESS, "01010101")));
    ASSERT_NO_THROW(user.reset(new User(*id)));

    // Verify that we can add a user.
    ASSERT_NO_THROW(reg->addUser(user));

    // Verify that the user can be found.
    UserPtr found_user;
    ASSERT_NO_THROW(found_user = reg->findUser(*id));
    EXPECT_TRUE(found_user);
    EXPECT_EQ(found_user->getUserId(), *id);

    // Verify that searching for a non-existing user returns empty user pointer.
    UserIdPtr id2;
    ASSERT_NO_THROW(id2.reset(new UserId(UserId::HW_ADDRESS, "02020202")));
    ASSERT_NO_THROW(found_user = reg->findUser(*id2));
    EXPECT_FALSE(found_user);

    // Verify that the user can be deleted.
    ASSERT_NO_THROW(reg->removeUser(*id));
    ASSERT_NO_THROW(found_user = reg->findUser(*id));
    EXPECT_FALSE(found_user);
}

/// @brief Tests finding users by isc::dhcp::HWaddr instance.
TEST(UserRegistry, findByHWAddr) {
    // Create the registry.
    UserRegistryPtr reg;
    ASSERT_NO_THROW(reg.reset(new UserRegistry()));

    // Make a new user and add it.
    UserPtr user;
    ASSERT_NO_THROW(user.reset(new User(UserId::HW_ADDRESS, "01010101")));
    ASSERT_NO_THROW(reg->addUser(user));

    // Make a HWAddr instance using the same id value.
    isc::dhcp::HWAddr hwaddr(user->getUserId().getId(), isc::dhcp::HTYPE_ETHER);

    // Verify user can be found by HWAddr.
    UserPtr found_user;
    ASSERT_NO_THROW(found_user = reg->findUser(hwaddr));
    EXPECT_TRUE(found_user);
    EXPECT_EQ(found_user->getUserId(), user->getUserId());
}

/// @brief Tests finding users by isc::dhcp::DUID instance.
TEST(UserRegistry, findByDUID) {
    // Create the registry.
    UserRegistryPtr reg;
    ASSERT_NO_THROW(reg.reset(new UserRegistry()));

    // Make a new user and add it.
    UserPtr user;
    ASSERT_NO_THROW(user.reset(new User(UserId::DUID, "01010101")));
    ASSERT_NO_THROW(reg->addUser(user));

    // Make a DUID instance using the same id value.
    isc::dhcp::DUID duid(user->getUserId().getId());

    // Verify user can be found by DUID.
    UserPtr found_user;
    ASSERT_NO_THROW(found_user = reg->findUser(duid));
    EXPECT_TRUE(found_user);
    EXPECT_EQ(found_user->getUserId(), user->getUserId());
}

/// @brief Tests mixing users of different types.
TEST(UserRegistry, oneOfEach) {
    // Create the registry.
    UserRegistryPtr reg;
    ASSERT_NO_THROW(reg.reset(new UserRegistry()));

    // Make user ids.
    UserIdPtr idh, idd;
    ASSERT_NO_THROW(idh.reset(new UserId(UserId::HW_ADDRESS, "01010101")));
    ASSERT_NO_THROW(idd.reset(new UserId(UserId::DUID, "03030303")));

    // Make and add HW_ADDRESS user.
    UserPtr user;
    user.reset(new User(*idh));
    ASSERT_NO_THROW(reg->addUser(user));

    // Make and add DUID user.
    user.reset(new User(*idd));
    ASSERT_NO_THROW(reg->addUser(user));

    // Verify we can find both.
    UserPtr found_user;
    ASSERT_NO_THROW(found_user = reg->findUser(*idh));
    ASSERT_NO_THROW(found_user = reg->findUser(*idd));
}

/// @brief Tests loading the registry from a file.
TEST(UserRegistry, refreshFromFile) {
    // Create the registry.
    UserRegistryPtr reg;
    ASSERT_NO_THROW(reg.reset(new UserRegistry()));

    UserDataSourcePtr user_file;

    // Verify that data source cannot be set to null source.
    ASSERT_THROW(reg->setSource(user_file), UserRegistryError);

    // Create the data source.
    ASSERT_NO_THROW(user_file.reset(new UserFile
                                    (testFilePath("test_users_1.txt"))));

    // Set the registry's data source and refresh the registry.
    ASSERT_NO_THROW(reg->setSource(user_file));
    ASSERT_NO_THROW(reg->refresh());

    // Verify we can find all the expected users.
    UserIdPtr id;
    ASSERT_NO_THROW(id.reset(new UserId(UserId::HW_ADDRESS, "01ac00f03344")));
    EXPECT_TRUE(reg->findUser(*id));

    ASSERT_NO_THROW(id.reset(new UserId(UserId::DUID, "225060de0a0b")));
    EXPECT_TRUE(reg->findUser(*id));
}

/// @brief Tests preservation of registry upon refresh failure.
TEST(UserRegistry, refreshFail) {
    // Create the registry.
    UserRegistryPtr reg;
    ASSERT_NO_THROW(reg.reset(new UserRegistry()));

    // Create the data source.
    UserDataSourcePtr user_file;
    ASSERT_NO_THROW(user_file.reset(new UserFile
                                    (testFilePath("test_users_1.txt"))));

    // Set the registry's data source and refresh the registry.
    ASSERT_NO_THROW(reg->setSource(user_file));
    ASSERT_NO_THROW(reg->refresh());

    // Make user ids of expected users.
    UserIdPtr id1, id2;
    ASSERT_NO_THROW(id1.reset(new UserId(UserId::HW_ADDRESS, "01ac00f03344")));
    ASSERT_NO_THROW(id2.reset(new UserId(UserId::DUID, "225060de0a0b")));

    // Verify we can find all the expected users.
    EXPECT_TRUE(reg->findUser(*id1));
    EXPECT_TRUE(reg->findUser(*id2));

    // Replace original data source with a new one containing an invalid entry.
    ASSERT_NO_THROW(user_file.reset(new UserFile
                                    (testFilePath("test_users_err.txt"))));
    ASSERT_NO_THROW(reg->setSource(user_file));

    // Refresh should throw due to invalid data.
    EXPECT_THROW(reg->refresh(), UserRegistryError);

    // Verify we can still find all the original users.
    EXPECT_TRUE(reg->findUser(*id1));
    EXPECT_TRUE(reg->findUser(*id2));
}

} // end of anonymous namespace