Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: The length of the IP address was not being set. #91

Merged
merged 8 commits into from
Mar 20, 2025
8 changes: 8 additions & 0 deletions ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ typedef struct {
IpAddress * const address;
byte *current;
int bytesPresent;
int abbreviationsFilled;
} fiftyoneDegreeIpAddressBuildState;
typedef fiftyoneDegreeIpAddressBuildState IpAddressBuildState;

Expand All @@ -80,6 +81,10 @@ static void parseIpV6Segment(
char first[3], second[3], val; // "FF" + '\0'
if (start > end) {
// This is an abbreviation, so fill it in.
if (buildState->abbreviationsFilled) {
return;
}
buildState->abbreviationsFilled++;
for (i = 0; i < IPV6_LENGTH - buildState->bytesPresent; i++) {
*buildState->current = (byte)0;
buildState->current++;
Expand Down Expand Up @@ -310,7 +315,9 @@ bool fiftyoneDegreesIpAddressParse(
address,
address->value,
byteCount,
0,
};

// Add the bytes from the source value and get the type of address.
iterateIpAddress(
start,
Expand All @@ -319,6 +326,7 @@ bool fiftyoneDegreesIpAddressParse(
&springCount,
type,
callbackIpAddressBuild);

return true;
}

Expand Down
1 change: 0 additions & 1 deletion ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ typedef enum e_fiftyone_degrees_ip_evidence_type {
typedef struct fiftyone_degrees_ip_address_t {
byte value[FIFTYONE_DEGREES_IPV6_LENGTH]; /**< Buffer to hold the IP
address bytes array. */
size_t length; /**< Length of the byte array. */
byte type; /**< The type of the IP. @see fiftyoneDegreesIpType */
} fiftyoneDegreesIpAddress;

Expand Down
88 changes: 63 additions & 25 deletions tests/IpParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
#include "../fiftyone.h"
#include <memory>

static bool CheckResult(byte* result, const byte* expected, uint16_t size) {
static bool CheckResult(
const byte* result,
const byte* expected,
const uint16_t size) {
bool match = true;
for (uint16_t i = 0; i < size; i++) {
match = match && *result == *expected;
for (uint16_t i = 0; i < size && match; i++) {
match = (*result == *expected);
result++;
expected++;
}
Expand All @@ -42,6 +45,9 @@ static bool parseIpAddressInPlace(const std::unique_ptr<IpAddress> &ipAddress, c

static std::unique_ptr<IpAddress> parseIpAddressStringOfLength(const char * const ipString, const size_t length) {
std::unique_ptr<IpAddress> ipPtr = std::make_unique<IpAddress>();
for (size_t i = 0; i < std::size(ipPtr->value); i++) {
ipPtr->value[i] = static_cast<byte>(0xA0 + i);
}
const bool parsed =parseIpAddressInPlace(ipPtr, ipString, length);
return parsed ? std::move(ipPtr) : nullptr;
}
Expand All @@ -60,13 +66,17 @@ TEST(ParseIp, ParseIp_Ipv4_Low)
byte expected[] = { 0, 0, 0, 0 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '0.0.0.0'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV4) <<
L"Expected type to be IPv4";
}
TEST(ParseIp, ParseIp_Ipv4_High)
{
auto const result = parseIpAddressString("255.255.255.255");
byte expected[] = { 255, 255, 255, 255 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected)))
<< L"Expected result to be '255.255.255.255'";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '255.255.255.255'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV4) <<
L"Expected type to be IPv4";
}
TEST(ParseIp, ParseIp_Ipv4_Endline)
{
Expand All @@ -77,6 +87,8 @@ TEST(ParseIp, ParseIp_Ipv4_Endline)
L"Expected result to be non-NULL.";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected) - 1)) <<
L"Expected result to be '0.0.0.0'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV4) <<
L"Expected type to be IPv4";
}
TEST(ParseIp, ParseIp_Ipv4_NoOverwrite12)
{
Expand All @@ -103,6 +115,8 @@ TEST(ParseIp, ParseIp_Ipv4_Trim0)
L"Expected result to be non-NULL.";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected) - 1)) <<
L"Expected result to be '192.168.101.217'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV4) <<
L"Expected type to be IPv4";
}
TEST(ParseIp, ParseIp_Ipv4_Trim1)
{
Expand All @@ -113,6 +127,8 @@ TEST(ParseIp, ParseIp_Ipv4_Trim1)
L"Expected result to be non-NULL.";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected) - 1)) <<
L"Expected result to be '192.168.101.217'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV4) <<
L"Expected type to be IPv4";
}
TEST(ParseIp, ParseIp_Ipv4_Trim2)
{
Expand All @@ -123,6 +139,8 @@ TEST(ParseIp, ParseIp_Ipv4_Trim2)
L"Expected result to be non-NULL.";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected) - 1)) <<
L"Expected result to be '192.168.101.21'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV4) <<
L"Expected type to be IPv4";
}
TEST(ParseIp, ParseIp_Ipv4_Trim3)
{
Expand All @@ -133,20 +151,26 @@ TEST(ParseIp, ParseIp_Ipv4_Trim3)
L"Expected result to be non-NULL.";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected) - 1)) <<
L"Expected result to be '192.168.101.2'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV4) <<
L"Expected type to be IPv4";
}
TEST(ParseIp, ParseIp_Ipv4_PortNumber)
{
auto const result = parseIpAddressString("1.2.3.4:80");
byte expected[] = { 1, 2, 3, 4 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected)))
<< L"Expected result to be '1.2.3.4'";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '1.2.3.4'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV4) <<
L"Expected type to be IPv4";
}
TEST(ParseIp, ParseIp_Ipv4_CIDRFormat)
{
auto const result = parseIpAddressString("1.2.3.4/32");
byte expected[] = { 1, 2, 3, 4 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected)))
<< L"Expected result to be '1.2.3.4'";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '1.2.3.4'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV4) <<
L"Expected type to be IPv4";
}
// ------------------------------------------------------------------------------
// IPv6 tests
Expand All @@ -155,22 +179,28 @@ TEST(ParseIp, ParseIp_Ipv6_Low)
{
auto const result = parseIpAddressString("0:0:0:0:0:0:0:0");
byte expected[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected)))
<< L"Expected result to be '0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0'";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV6) <<
L"Expected type to be IPv6";
}
TEST(ParseIp, ParseIp_Ipv6_Low_Abbreviated)
{
auto const result = parseIpAddressString("::");
byte expected[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected)))
<< L"Expected result to be '0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0'";
const byte expected[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV6) <<
L"Expected type to be IPv6";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0'";
}
TEST(ParseIp, ParseIp_Ipv6_High)
{
auto const result = parseIpAddressString("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF");
byte expected[] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected)))
<< L"Expected result to be '255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255'";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV6) <<
L"Expected type to be IPv6";
}
TEST(ParseIp, ParseIp_Ipv6_AbbreviatedStart)
{
Expand Down Expand Up @@ -201,29 +231,37 @@ TEST(ParseIp, ParseIp_Ipv6_AbbreviatedMiddle)
{
auto const result = parseIpAddressString("FFFF:FFFF::FFFF:FFFF");
byte expected[] = { 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected)))
<< L"Expected result to be '255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255'";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV6) <<
L"Expected type to be IPv6";
}
TEST(ParseIp, ParseIp_Ipv6_AbbreviatedEnd)
{
auto const result = parseIpAddressString("FFFF:FFFF:FFFF:FFFF::");
byte expected[] = { 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected)))
<< L"Expected result to be '255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0'";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV6) <<
L"Expected type to be IPv6";
}
TEST(ParseIp, ParseIp_Ipv6_PortNumber)
{
auto const result = parseIpAddressString("[2001::1]:80");
byte expected[] = { 32, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected)))
<< L"Expected result to be '32, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1'";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '32, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV6) <<
L"Expected type to be IPv6";
}
TEST(ParseIp, ParseIp_Ipv6_CIDRFormat)
{
auto const result = parseIpAddressString("2001::1/128");
byte expected[] = { 32, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected)))
<< L"Expected result to be '32, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1'";
EXPECT_TRUE(CheckResult(result->value, expected, sizeof(expected))) <<
L"Expected result to be '32, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1'";
EXPECT_EQ(result->type, FIFTYONE_DEGREES_IP_TYPE_IPV6) <<
L"Expected type to be IPv6";
}

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -398,4 +436,4 @@ TEST(CompareIp, CompareIp_Ip_Invalid) {
ipAddress2,
IP_TYPE_INVALID) == 0) << "Result should be 0 "
"where type is invalid\n";
}
}