Skip to content

Commit

Permalink
Merged Pull Request '#71 feature/fix-parsing-ipv4-embedded/main->main…
Browse files Browse the repository at this point in the history
… : Fix ParseAddress_Ipv6_MixedIpv6Ipv4 test (on main)'

Fix ParseAddress_Ipv6_MixedIpv6Ipv4 test (on main)
  • Loading branch information
Automation51D authored Jan 8, 2025
2 parents 7c52914 + f159860 commit 061ac9f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ These can be run by calling
ctest
```

CTest module automatically creates a `BUILD_TESTING` option -- see https://cmake.org/cmake/help/latest/module/CTest.html

When testing some dependent code, running `CommonTests` can be skipped by turning OFF the `FIFTYONE_COMMON_CXX_BUILD_TESTING` option.

If CMake has been used in an MSVC environment, then the tests will be set up and discoverable in the Visual Studio solution `51DegreesCommon` created by CMake.

## Visual Studio
Expand Down
55 changes: 39 additions & 16 deletions ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

typedef void(*parseIterator)(
void *state,
EvidenceIpType type,
EvidenceIpType segmentType,
const char *start,
const char *end);

Expand All @@ -35,13 +35,13 @@ typedef void(*parseIterator)(
*/
static void callbackIpAddressCount(
void *state,
EvidenceIpType type,
EvidenceIpType segmentType,
const char *start,
const char *end) {
if (start <= end) {
if (type != FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_INVALID) {
if (segmentType != FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_INVALID) {
(*(int*)state)++;
if (type == FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV6) {
if (segmentType == FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV6) {
(*(int*)state)++;
}
}
Expand Down Expand Up @@ -99,27 +99,48 @@ static void parseIpV6Segment(

static void callbackIpAddressBuild(
void *state,
EvidenceIpType type,
EvidenceIpType segmentType,
const char *start,
const char *end) {
EvidenceIpAddress *address = (EvidenceIpAddress*)state;
if (type == FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV4) {
if (segmentType == FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV4) {
*address->current = getIpByte(atoi(start));
address->current++;
}
else if (type == FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV6) {
else if (segmentType == FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV6) {
parseIpV6Segment(address, start, end);
}
}

static EvidenceIpType getIpTypeFromSeparator(char separator) {
static EvidenceIpType getIpTypeFromSeparator(const char separator) {
switch (separator) {
case '.':
return FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV4;
case ':':
return FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV6;
default:
return FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_INVALID;
case '.':
return FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV4;
case ':':
return FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV6;
default:
return FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_INVALID;
}
}

static EvidenceIpType getSegmentTypeWithSeparator(
const char separator,
const EvidenceIpType ipType,
const EvidenceIpType lastSeparatorType) {
switch (ipType) {
case FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV4:
return FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV4;
case FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV6:
switch (separator) {
case ':':
return FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV6;
case '.':
return FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_IPV4;
default:
return lastSeparatorType;
}
default:
return getIpTypeFromSeparator(separator);
}
}

Expand All @@ -135,18 +156,20 @@ static EvidenceIpType iterateIpAddress(
parseIterator foundSegment) {
const char *current = start;
const char *nextSegment = current;
EvidenceIpType currentSegmentType = FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_INVALID;
while (current <= end && nextSegment < end) {
if (*current == ',' ||
*current == ':' ||
*current == '.' ||
*current == ' ' ||
*current == '\0') {
currentSegmentType = getSegmentTypeWithSeparator(*current, type, currentSegmentType);
if (type == FIFTYONE_DEGREES_EVIDENCE_IP_TYPE_INVALID) {
type = getIpTypeFromSeparator(*current);
type = currentSegmentType;
}
// Check if it is leading abbreviation
if (current - 1 >= start) {
foundSegment(state, type, nextSegment, current - 1);
foundSegment(state, currentSegmentType, nextSegment, current - 1);
}
nextSegment = current + 1;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/IpAddressTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ TEST(ParseAddress, ParseAddress_Ipv6_LeadingZeros)
"The value of the IPv6 address (Leading zeros in each segment) is not correctly parsed.";
}

TEST(ParseAddress, DISABLED_ParseAddress_Ipv6_MixedIpv6Ipv4)
TEST(ParseAddress, ParseAddress_Ipv6_MixedIpv6Ipv4)
{
// IPv4-mapped IPv6 address
const char * const rawAddress = "::ffff:192.168.1.1";
Expand Down

0 comments on commit 061ac9f

Please sign in to comment.