Skip to content

Commit

Permalink
Merge pull request #325 from ArnoStiefvater/ports-unit-tests
Browse files Browse the repository at this point in the history
add unit tests and small changes for port range functions
  • Loading branch information
mattmundell authored Apr 21, 2020
2 parents 7167586 + 371c5be commit 067e49d
Show file tree
Hide file tree
Showing 6 changed files with 1,001 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add unit tests for osp. [#315](https://github.com/greenbone/gvm-libs/pull/315)
- Add support for test_alive_hosts_only feature of openvas. [#320](https://github.com/greenbone/gvm-libs/pull/320)
- Add function to set and get the NVT QoD. [#321](https://github.com/greenbone/gvm-libs/pull/321)
- Add unit tests for networking.c port list functions. [#325](https://github.com/greenbone/gvm-libs/pull/325)

### Fixed
- Fix is_cidr_block(). [#322](https://github.com/greenbone/gvm-libs/pull/322)
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ if (BUILD_TESTS AND NOT SKIP_SRC)
find_package (cgreen REQUIRED)

add_custom_target (tests
DEPENDS array-test xmlutils-test version-test osp-test nvti-test)
DEPENDS array-test networking-test xmlutils-test version-test osp-test nvti-test)

endif (BUILD_TESTS AND NOT SKIP_SRC)

Expand Down
7 changes: 7 additions & 0 deletions base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ if (BUILD_TESTS)

target_link_libraries (array-test ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS})

add_executable (networking-test
EXCLUDE_FROM_ALL
networking_tests.c)
add_test (networking-test networking-test)
target_include_directories (networking-test PRIVATE ${CGREEN_INCLUDE_DIRS} )
target_link_libraries (networking-test gvm_base_shared ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS})

add_executable (version-test
EXCLUDE_FROM_ALL
version_tests.c)
Expand Down
51 changes: 38 additions & 13 deletions base/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,17 @@ validate_port_range (const char *port_range)

element = g_strstrip (*point);

/* Strip off any leading type specifier. */
/* Strip off any leading type specifier and following whitespace. */

if ((strlen (element) >= 2)
&& ((element[0] == 'T') || (element[0] == 'U'))
&& (element[1] == ':'))
element = element + 2;
&& ((element[0] == 'T') || (element[0] == 'U')))
{
element++;
while (*element && isblank (*element))
element++;
if (*element == ':')
element++;
}

/* Look for a hyphen. */

Expand Down Expand Up @@ -589,18 +594,23 @@ validate_port_range (const char *port_range)
*
* @param[in] port_range Valid port_range string.
*
* @return Range array.
* @return Range array or NULL if port_range invalid or NULL.
*/
array_t *
port_range_ranges (const char *port_range)
{
gchar **split, **point, *range_start, *current;
array_t *ranges;
int tcp;
int tcp, err;

if (!port_range)
return NULL;

/* port_range needs to be a valid port_range string. */
err = validate_port_range (port_range);
if (err)
return NULL;

ranges = make_array ();

while (*port_range && isblank (*port_range))
Expand Down Expand Up @@ -628,19 +638,34 @@ port_range_ranges (const char *port_range)
{
gchar *hyphen, *element;
range_t *range;
int element_strlen;

element = g_strstrip (*point);
if (strlen (element) >= 2)
element_strlen = strlen (element);

if (element_strlen >= 2)
{
if ((element[0] == 'T') && (element[1] == ':'))
if ((element[0] == 'T'))
{
tcp = 1;
element = element + 2;
element++;
while (*element && isblank (*element))
element++;
if (*element == ':')
{
element++;
tcp = 1;
}
}
else if ((element[0] == 'U') && (element[1] == ':'))
else if ((element[0] == 'U'))
{
tcp = 0;
element = element + 2;
element++;
while (*element && isblank (*element))
element++;
if (*element == ':')
{
element++;
tcp = 0;
}
}
/* Else tcp stays as it is. */
}
Expand Down
2 changes: 1 addition & 1 deletion base/networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct range
{
gchar *comment; /**< Comment. */
gchar *id; /**< UUID. */
int end; /**< End port. 0 for single port. */
int end; /**< End port. For single port end == start. */
int exclude; /**< Whether to exclude range. */
int start; /**< Start port. */
port_protocol_t type; /**< Port protocol. */
Expand Down
Loading

0 comments on commit 067e49d

Please sign in to comment.