Skip to content

Commit

Permalink
Add: first tests of pwpolicy.c
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmundell committed Feb 25, 2025
1 parent 888a97d commit 2795b4a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ if (BUILD_TESTS)

set (NETWORKING_TEST_LINKER_WRAP_OPTIONS "-Wl,-wrap,g_io_channel_new_file,-wrap,g_io_channel_shutdown")
add_unit_test (networking-test networking_tests.c gvm_base_shared ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS} ${NETWORKING_TEST_LINKER_WRAP_OPTIONS})
add_unit_test (pwpolicy-test pwpolicy_tests.c ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS})
add_unit_test (version-test version_tests.c ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS})
add_unit_test (nvti-test nvti_tests.c ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS})
add_unit_test (hosts-test hosts_tests.c gvm_base_shared gvm_util_shared ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS})
Expand Down
77 changes: 77 additions & 0 deletions base/pwpolicy_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* SPDX-FileCopyrightText: 2019-2023 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "pwpolicy.c"

#include <cgreen/assertions.h>
#include <cgreen/cgreen.h>
#include <cgreen/constraint_syntax_helpers.h>
#include <cgreen/internal/c_assertions.h>
#include <cgreen/mocks.h>

Describe (pwpolicy);
BeforeEach (pwpolicy)
{
}

AfterEach (pwpolicy)
{
}

/* parse_pattern_line */

Ensure (pwpolicy, parse_pattern_line_allows)
{
char *desc, *line;

desc = NULL;
line = g_strdup ("password");
assert_that (parse_pattern_line (line, "test", 111, NULL, "passw0rd", "username"),
is_null);
g_free (desc);
g_free (line);
}

Ensure (pwpolicy, parse_pattern_line_refuses)
{
char *desc, *line;

desc = NULL;
line = g_strdup ("passw0rd");
assert_that (parse_pattern_line (line, "test", 111, &desc, "passw0rd", "username"),
is_not_null);
g_free (desc);
g_free (line);
}

Ensure (pwpolicy, parse_pattern_line_comment)
{
char *desc, *line;

desc = NULL;
line = g_strdup ("# password");
assert_that (parse_pattern_line (line, "test", 111, NULL, "password", "username"),
is_null);
g_free (desc);
g_free (line);
}

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

suite = create_test_suite ();

add_test_with_context (suite, pwpolicy, parse_pattern_line_allows);
add_test_with_context (suite, pwpolicy, parse_pattern_line_refuses);
add_test_with_context (suite, pwpolicy, parse_pattern_line_comment);

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

Check warning on line 74 in base/pwpolicy_tests.c

View check run for this annotation

Codecov / codecov/patch

base/pwpolicy_tests.c#L74

Added line #L74 was not covered by tests

return run_test_suite (suite, create_text_reporter ());
}

0 comments on commit 2795b4a

Please sign in to comment.