Skip to content

Commit

Permalink
Add: Add tests for "name" and "comment" field validations
Browse files Browse the repository at this point in the history
Implement the first gsad unit tests for checking the "name" and "comment
field validation. The tests are implemented using cgreen and will be
configured if the cmake variable BUILD_TESTING is set. Calling `make
tests` will build the tests and `make test` will run them.
  • Loading branch information
bjoernricks committed Dec 20, 2022
1 parent baa2a46 commit 485ca6c
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,22 @@ add_custom_target (ctags COMMENT "Building tags..."
add_custom_target (tags COMMENT "Building tags files...")
add_dependencies (tags etags ctags)

## Testing

if (BUILD_TESTING)
find_package(cgreen REQUIRED)

add_executable(gsad-validator-test EXCLUDE_FROM_ALL gsad_validator_test.c validator.c)

target_link_libraries (gsad-validator-test ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS})


add_test (gsad-validator-test gsad-validator-test)

add_custom_target (tests
DEPENDS
gsad-validator-test
)
endif(BUILD_TESTING)

## End
86 changes: 86 additions & 0 deletions src/gsad_validator_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* Copyright (C) 2022 Greenbone Networks GmbH
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "gsad_validator.c"

#include <cgreen/cgreen.h>

Describe (gsad_validator);
BeforeEach (gsad_validator)
{
init_validator ();
}
AfterEach (gsad_validator)
{
reset_validator ();
}

Ensure (gsad_validator, validate_name)
{
validator_t validator = get_validator ();
assert_that (gvm_validate (validator, "name", "foo"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", "12345"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", "äüöÄÜÖß"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", "()[]"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", "-–"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", ":;.…?!"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", "“”\"‘’''"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", " "), is_equal_to (0));
assert_that (gvm_validate (validator, "name", "a\tb"), is_equal_to (2));
assert_that (gvm_validate (validator, "name", "a\rb"), is_equal_to (2));
assert_that (gvm_validate (validator, "name", "a\nb"), is_equal_to (2));
assert_that (gvm_validate (validator, "name", "/"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", "\\"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", "@"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", "$%&=<>^+*#"), is_equal_to (0));
assert_that (gvm_validate (validator, "name", "foo bar 123 baz"),
is_equal_to (0));
}

Ensure (gsad_validator, validate_comment)
{
validator_t validator = get_validator ();
assert_that (gvm_validate (validator, "comment", "foo"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "12345"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "äüöÄÜÖß"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "()[]"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "-–"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", ":;.…?!"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "“”\"‘’''"),
is_equal_to (0));
assert_that (gvm_validate (validator, "comment", " "), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "a\tb"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "a\rb"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "a\nb"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "/"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "\\"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "@"), is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "$%&=<>^+*#"),
is_equal_to (0));
assert_that (gvm_validate (validator, "comment", "foo bar\nbaz\t123"),
is_equal_to (0));
}

int
main (int argc, char **argv)
{
TestSuite *suite = create_test_suite ();
add_test_with_context (suite, gsad_validator, validate_name);
add_test_with_context (suite, gsad_validator, validate_comment);
return run_test_suite (suite, create_text_reporter ());
}

0 comments on commit 485ca6c

Please sign in to comment.