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 tets for port list.
Allow whitespace after 'T' or 'U'. Previously ':' had to follow
immidiately after.
  • Loading branch information
ArnoStiefvater committed Apr 20, 2020
1 parent 7167586 commit e4331a5
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 5 deletions.
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
13 changes: 9 additions & 4 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
123 changes: 123 additions & 0 deletions base/networking_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* Copyright (C) 2009-2019 Greenbone Networks GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "networking.c"

#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>

Describe (networking);
BeforeEach (networking)
{
}
AfterEach (networking)
{
}

Ensure (networking, validate_port_range)
{
/* No port range provided. */
assert_that (validate_port_range (NULL), is_equal_to (1));
assert_that (validate_port_range (""), is_equal_to (1));

/* '\0' on end. */
assert_that (validate_port_range ("\0"), is_equal_to (1));
assert_that (validate_port_range ("T:1-5,7,9,U:1-3,5,7,9,\\0"),
is_equal_to (1));

/* Newline in between range description.*/
assert_that (validate_port_range ("\nT:1-\n5,7,9,\nU:1-3,5\n,7,9\n"),
is_equal_to (1));

/* Port <= 0 or Port > 65535. */
assert_that (validate_port_range ("0"), is_equal_to (1));
assert_that (validate_port_range ("-9"), is_equal_to (1));
assert_that (validate_port_range ("1,0,6,7"), is_equal_to (1));
assert_that (validate_port_range ("2,-9,4"), is_equal_to (1));
assert_that (validate_port_range ("90000"), is_equal_to (1));

/* Illegal Ranges. */
assert_that (validate_port_range ("T:-"), is_equal_to (1));
assert_that (validate_port_range ("T:-9"), is_equal_to (1));
assert_that (validate_port_range ("T:0-"), is_equal_to (1));
assert_that (validate_port_range ("T:0-9"), is_equal_to (1));
assert_that (validate_port_range ("T:90000-"), is_equal_to (1));
assert_that (validate_port_range ("T:90000-90010"), is_equal_to (1));
assert_that (validate_port_range ("T:9-\\0"), is_equal_to (1));
assert_that (validate_port_range ("T:9-0"), is_equal_to (1));
assert_that (validate_port_range ("T:9-90000"), is_equal_to (1));
assert_that (validate_port_range ("T:100-9"), is_equal_to (1));
assert_that (validate_port_range ("0-"), is_equal_to (1));
assert_that (validate_port_range ("0-9"), is_equal_to (1));
assert_that (validate_port_range ("9-"), is_equal_to (1));
assert_that (validate_port_range ("9-\\0"), is_equal_to (1));
assert_that (validate_port_range ("9-8"), is_equal_to (1));
assert_that (validate_port_range ("90000-90010"), is_equal_to (1));
assert_that (validate_port_range ("100-9"), is_equal_to (1));
assert_that (validate_port_range ("T,U"), is_equal_to (1));
assert_that (validate_port_range ("T :\n: 1-2,U"), is_equal_to (1));
assert_that (validate_port_range ("T :: 1-2,U"), is_equal_to (1));
assert_that (validate_port_range ("T:2=2"), is_equal_to (1));

/* Legal single ports.*/
assert_that (validate_port_range ("6,6,6,6,10,20"), is_equal_to (0));
assert_that (validate_port_range ("T:7, U:7"), is_equal_to (0));
assert_that (validate_port_range ("T:7, U:9"), is_equal_to (0));
assert_that (validate_port_range ("9"), is_equal_to (0));
assert_that (validate_port_range ("U:,T:"), is_equal_to (0)); /* is ignored */
assert_that (validate_port_range ("1,2,,,,,,,\n\n\n\n\n\n,,,5"),
is_equal_to (0));

/* Example in Documentation. */
assert_that (validate_port_range ("T:1-5,7,9,U:1-3,5,7,9"), is_equal_to (0));

/* Treat newlines like commas. */
assert_that (validate_port_range ("1,2,\n,\n4,6"), is_equal_to (0));
assert_that (validate_port_range ("T:1-5,7,9,\nU:1-3,5\n,7,9"),
is_equal_to (0));

/* Ranges without type specifier. */
assert_that (validate_port_range ("6-9,7,7,10-20,20"), is_equal_to (0));

/* Allow whitespace after T/U and anywhere else. */
assert_that (
validate_port_range (" T: 1 -5, 7 ,9, \nU :1- 3,5 \n,7,9"),
is_equal_to (0));
assert_that (validate_port_range (
" T: 1 -5, 7 ,9, \nU :1- 3,5 \n,7,9, T : 5 -7"),
is_equal_to (0));
assert_that (validate_port_range (" T : 1"), is_equal_to (0));
}

/* Test suite. */

int
main (int argc, char **argv)
{
TestSuite *suite;

suite = create_test_suite ();

add_test_with_context (suite, networking, validate_port_range);

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

return run_test_suite (suite, create_text_reporter ());
}

0 comments on commit e4331a5

Please sign in to comment.