Commit 81a2df84 authored by Stephen Morris's avatar Stephen Morris
Browse files

[trac998] Remove all constructors but the one from a string

parent 59b380d3
......@@ -135,46 +135,6 @@ private:
static const size_t IPV4_SIZE = sizeof(struct in_addr);
public:
/// \brief IPv4 Constructor
///
/// Constructs an IPCheck object from a network address given as a
/// 32-bit value in network byte order and a prefix length.
///
/// \param address IP address to check for (as an address in host-byte
/// order). Note host-byte order - this is different to the IPv6
/// constructor.
/// \param prefixlen The prefix length specified as an integer between 0
/// and 32. This determines the number of bits of the address to
/// check. (A value of zero imples match all IPv4 addresses.)
IPCheck(uint32_t address, int prefixlen = 8 * IPV4_SIZE) :
address_(IPV4_SIZE), mask_(), family_(AF_INET)
{
// The address is stored in network-byte order, so the MS byte should
// be stored at the lowest address in the array.
address_[3] = static_cast<uint8_t>((address ) & 0xff);
address_[2] = static_cast<uint8_t>((address >> 8) & 0xff);
address_[1] = static_cast<uint8_t>((address >> 16) & 0xff);
address_[0] = static_cast<uint8_t>((address >> 24) & 0xff);
setMask(prefixlen);
}
/// \brief IPv6 Constructor
///
/// Constructs an IPv6 Check object from a network address given as a
/// 16-byte array in network-byte order and a prefix length.
///
/// \param address IP address to check for (as an address in network-byte
/// order).
/// \param prefixlen The prefix length specified as an integer between 0
/// and 128. This determines the number of bits of the address to
/// check.
IPCheck(const uint8_t* address, int prefixlen = 8 * IPV6_SIZE) :
address_(address, address + IPV6_SIZE), mask_(), family_(AF_INET6)
{
setMask(prefixlen);
}
/// \brief String Constructor
///
/// Constructs an IP Check object from an address or address prefix in the
......
......@@ -34,7 +34,8 @@ const size_t IPV6_SIZE = 16;
struct GeneralAddress {
vector<uint8_t> addr; // Address type. Size indicates what it holds
// Convert uint32_t address to a uint8_t vector
// Convert uint32_t addressi in host-byte order to a uint8_t vector
// in network-byte order.
vector<uint8_t> convertUint32(uint32_t address) {
BOOST_STATIC_ASSERT(sizeof(uint32_t) == IPV4_SIZE);
......@@ -74,7 +75,7 @@ struct GeneralAddress {
// A couple of convenience methods for checking equality with different
// representations of an address.
// Check that the IPV4 address is the same as that given.
// Check that the IPV4 address is the same as that given.
bool equals(uint32_t address) {
if (addr.size() == IPV4_SIZE) {
const vector<uint8_t> byte_address = convertUint32(address);
......@@ -168,43 +169,6 @@ TEST(IPFunctionCheck, SplitIPAddress) {
// *** IPV4 Tests ***
// Check that the constructor stores the elements correctly and, for the
// address and mask, in network-byte order.
TEST(IPCheck, V4ConstructorAddress) {
GeneralAddress address(0x12345678);
// Address is presented in network byte order in constructor, so no
// conversion is needed for this test.
IPCheck<GeneralAddress> acl(0x12345678);
vector<uint8_t> stored = acl.getAddress();
EXPECT_EQ(AF_INET, acl.getFamily());
EXPECT_TRUE(address.equals(stored));
}
TEST(IPCheck, V4ConstructorMask) {
// The mask is stored in network byte order. The conversion to a byte
// array within the IPCheck object should take care of the ordering.
IPCheck<GeneralAddress> acl1(1, 1); // Address of 1 is placeholder
GeneralAddress mask1(0x80000000); // Expected mask
vector<uint8_t> stored1 = acl1.getMask();
EXPECT_TRUE(mask1.equals(stored1));
EXPECT_EQ(1, acl1.getPrefixlen());
// Different check
IPCheck<GeneralAddress> acl2(1, 24);
GeneralAddress mask2(0xffffff00);
vector<uint8_t> stored2 = acl2.getMask();
EXPECT_TRUE(mask2.equals(stored2));
EXPECT_EQ(24, acl2.getPrefixlen());
// ... and some invalid network masks
EXPECT_THROW(IPCheck<GeneralAddress>(1, 33), isc::OutOfRange);
vector<uint8_t> dummy(IPV6_SIZE);
EXPECT_THROW(IPCheck<GeneralAddress>(&dummy[0], 129), isc::OutOfRange);
}
TEST(IPCheck, V4StringConstructor) {
// Constructor with no mask given
IPCheck<GeneralAddress> acl1("192.0.2.255");
......@@ -287,37 +251,37 @@ TEST(IPCheck, V4AssignmentOperator) {
TEST(IPCheck, V4Compare) {
// Exact address - match if given address matches stored address.
IPCheck<GeneralAddress> acl1(0x23457f13, 32);
EXPECT_TRUE(acl1.matches(0x23457f13));
EXPECT_FALSE(acl1.matches(0x23457f12));
IPCheck<GeneralAddress> acl1("192.0.2.255/32");
EXPECT_TRUE(acl1.matches(0xc00002ff));
EXPECT_FALSE(acl1.matches(0xc00002fe));
EXPECT_FALSE(acl1.matches(0x13457f13));
// Match if the address matches a mask
IPCheck<GeneralAddress> acl2(0x23450000, 16);
EXPECT_TRUE(acl2.matches(0x23450000));
EXPECT_TRUE(acl2.matches(0x23450001));
EXPECT_TRUE(acl2.matches(0x2345ffff));
EXPECT_FALSE(acl2.matches(0x23460000));
EXPECT_FALSE(acl2.matches(0x2346ffff));
IPCheck<GeneralAddress> acl2("192.0.2.255/27");
EXPECT_TRUE(acl2.matches(0xc00002ff));
EXPECT_TRUE(acl2.matches(0xc00002fe));
EXPECT_TRUE(acl2.matches(0xc00002ee));
EXPECT_FALSE(acl2.matches(0xc00002de));
EXPECT_FALSE(acl2.matches(0xd00002fe));
EXPECT_FALSE(acl2.matches(0x13457f13));
// Match if "any4" is specified
IPCheck<GeneralAddress> acl3("any4");
EXPECT_TRUE(acl3.matches(0x23450000));
EXPECT_TRUE(acl3.matches(0x23450001));
EXPECT_TRUE(acl3.matches(0x2345ffff));
EXPECT_TRUE(acl3.matches(0x23460000));
EXPECT_TRUE(acl3.matches(0x2346ffff));
IPCheck<GeneralAddress> acl4(0x23450000, 0);
EXPECT_TRUE(acl4.matches(0x23450000));
EXPECT_TRUE(acl4.matches(0x23450001));
EXPECT_TRUE(acl4.matches(0x2345ffff));
EXPECT_TRUE(acl4.matches(0x23460000));
EXPECT_TRUE(acl4.matches(0x2346ffff));
EXPECT_TRUE(acl3.matches(0xc00002ff));
EXPECT_TRUE(acl3.matches(0xc00002fe));
EXPECT_TRUE(acl3.matches(0xc00002ee));
EXPECT_TRUE(acl3.matches(0xc00002de));
EXPECT_TRUE(acl3.matches(0xd00002fe));
EXPECT_TRUE(acl3.matches(0x13457f13));
IPCheck<GeneralAddress> acl4("0.0.0.0/0");
EXPECT_TRUE(acl4.matches(0xc00002ff));
EXPECT_TRUE(acl4.matches(0xc00002fe));
EXPECT_TRUE(acl4.matches(0xc00002ee));
EXPECT_TRUE(acl4.matches(0xc00002de));
EXPECT_TRUE(acl4.matches(0xd00002fe));
EXPECT_TRUE(acl4.matches(0x13457f13));
}
// *** IPV6 Tests ***
// Some constants used in the tests
......@@ -383,41 +347,6 @@ const uint8_t MASK_128[] = {
} // Anonymous namespace
TEST(IPCheck, V6ConstructorAddress) {
IPCheck<GeneralAddress> acl1(V6ADDR_1);
vector<uint8_t> stored = acl1.getAddress();
EXPECT_EQ(sizeof(V6ADDR_1), stored.size());
EXPECT_TRUE(equal(stored.begin(), stored.end(), V6ADDR_1));
}
TEST(IPCheck, V6ConstructorMask) {
// Valid masks...
IPCheck<GeneralAddress> acl1(V6ADDR_1, 1);
vector<uint8_t> stored = acl1.getMask();
EXPECT_EQ(sizeof(MASK_1), stored.size());
EXPECT_TRUE(equal(stored.begin(), stored.end(), MASK_1));
IPCheck<GeneralAddress> acl2(V6ADDR_1, 8);
stored = acl2.getMask();
EXPECT_TRUE(equal(stored.begin(), stored.end(), MASK_8));
IPCheck<GeneralAddress> acl3(V6ADDR_1, 48);
stored = acl3.getMask();
EXPECT_TRUE(equal(stored.begin(), stored.end(), MASK_48));
IPCheck<GeneralAddress> acl4(V6ADDR_1, 51);
stored = acl4.getMask();
EXPECT_TRUE(equal(stored.begin(), stored.end(), MASK_51));
IPCheck<GeneralAddress> acl5(V6ADDR_1, 128);
stored = acl5.getMask();
EXPECT_TRUE(equal(stored.begin(), stored.end(), MASK_128));
// ... and some invalid network masks
EXPECT_THROW(IPCheck<GeneralAddress>(V6ADDR_1, 129), isc::OutOfRange);
}
TEST(IPCheck, V6StringConstructor) {
IPCheck<GeneralAddress> acl1(V6ADDR_1_STRING);
vector<uint8_t> address = acl1.getAddress();
......@@ -556,3 +485,4 @@ TEST(IPCheck, MixedMode) {
EXPECT_TRUE(acl4.matches(test1));
EXPECT_FALSE(acl4.matches(test2));
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment