Skip to content

Commit

Permalink
add unit tests and allow whitespace after port type specifier
Browse files Browse the repository at this point in the history
Unit tests for port_in_port_ranges().
Make valid port range string a requirement for this function.
Allow whitespace after 'T' or 'U'. Previously ':' had to follow
immidiately after.
  • Loading branch information
ArnoStiefvater committed Apr 20, 2020
1 parent e4331a5 commit f56b1cc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
39 changes: 31 additions & 8 deletions base/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,19 @@ 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);
assert (!err);
if (err)
g_warning ("%s: Invalid port_range string supplied. range_t may be filled "
"with incorrect values.",
__func__);

ranges = make_array ();

while (*port_range && isblank (*port_range))
Expand Down Expand Up @@ -633,19 +641,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
51 changes: 51 additions & 0 deletions base/networking_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,56 @@ Ensure (networking, validate_port_range)
assert_that (validate_port_range (" T : 1"), is_equal_to (0));
}

Ensure (networking, port_range_ranges)
{
/* Valid port ranges. U:,T: are empty ranges which are ignored. */
const gchar *valid_portrange1 = "1,10-13,10-10,T:1-2,U:10-12,U:,T:";
assert_that (validate_port_range (valid_portrange1), is_equal_to (0));

assert_that (port_range_ranges (NULL), is_null);
assert_that (port_range_ranges (valid_portrange1), is_not_null);

array_t *valid_portrange1_ranges = port_range_ranges (valid_portrange1);
assert_that (valid_portrange1_ranges, is_not_null);
assert_that (valid_portrange1_ranges->len, is_equal_to (5));

assert_that (valid_portrange1_ranges->len, is_equal_to (5));
range_t *valid_portrange1_range1 =
(range_t *) g_ptr_array_index (valid_portrange1_ranges, 0);
assert_that (valid_portrange1_range1->start, is_equal_to (1));
assert_that (valid_portrange1_range1->end, is_equal_to (1));
assert_that (valid_portrange1_range1->exclude, is_equal_to (0));
assert_that (valid_portrange1_range1->type, is_equal_to (PORT_PROTOCOL_TCP));

range_t *valid_portrange1_range2 =
(range_t *) g_ptr_array_index (valid_portrange1_ranges, 1);
assert_that (valid_portrange1_range2->start, is_equal_to (10));
assert_that (valid_portrange1_range2->end, is_equal_to (13));
assert_that (valid_portrange1_range2->exclude, is_equal_to (0));
assert_that (valid_portrange1_range2->type, is_equal_to (PORT_PROTOCOL_TCP));

range_t *valid_portrange1_range3 =
(range_t *) g_ptr_array_index (valid_portrange1_ranges, 2);
assert_that (valid_portrange1_range3->start, is_equal_to (10));
assert_that (valid_portrange1_range3->end, is_equal_to (10));
assert_that (valid_portrange1_range3->exclude, is_equal_to (0));
assert_that (valid_portrange1_range3->type, is_equal_to (PORT_PROTOCOL_TCP));

range_t *valid_portrange1_range4 =
(range_t *) g_ptr_array_index (valid_portrange1_ranges, 3);
assert_that (valid_portrange1_range4->start, is_equal_to (1));
assert_that (valid_portrange1_range4->end, is_equal_to (2));
assert_that (valid_portrange1_range4->exclude, is_equal_to (0));
assert_that (valid_portrange1_range4->type, is_equal_to (PORT_PROTOCOL_TCP));

range_t *valid_portrange1_range5 =
(range_t *) g_ptr_array_index (valid_portrange1_ranges, 4);
assert_that (valid_portrange1_range5->start, is_equal_to (10));
assert_that (valid_portrange1_range5->end, is_equal_to (12));
assert_that (valid_portrange1_range5->exclude, is_equal_to (0));
assert_that (valid_portrange1_range5->type, is_equal_to (PORT_PROTOCOL_UDP));
}

/* Test suite. */

int
Expand All @@ -115,6 +165,7 @@ main (int argc, char **argv)
suite = create_test_suite ();

add_test_with_context (suite, networking, validate_port_range);
add_test_with_context (suite, networking, port_range_ranges);

if (argc > 1)
return run_single_test (suite, argv[1], create_text_reporter ());
Expand Down

0 comments on commit f56b1cc

Please sign in to comment.