From f37ea6b1e6a00050376b32aabac3f3c8bf9f6442 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 4 Jun 2019 08:42:11 +0200 Subject: [PATCH 01/10] Add a few basic unit tests, to try out CGreen --- src/CMakeLists.txt | 49 ++++++++++++++++++++++++++++++++++++++++++++++ src/gvmd.c | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a56f89a02..81c418717 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -89,6 +89,48 @@ else (BACKEND STREQUAL SQLITE3) set (BINARY_NAME "gvmd-pg") endif (BACKEND STREQUAL SQLITE3) +add_executable (${BINARY_NAME}-test + tests.c + manage_tests.c + manage_utils_tests.c + + gvmd.c gmpd.c + sql.c + manage_ranges_all_tcp_nmap_5_51_top_100.c + manage_ranges_all_tcp_nmap_5_51_top_1000.c + manage_ranges_iana_tcp_2012.c manage_ranges_iana_tcp_udp_2012.c + manage_ranges_nmap_5_51_top_2000_top_100.c + manage_acl.c manage_config_discovery.c + manage_config_host_discovery.c manage_config_system_discovery.c + manage_sql.c manage_sql_nvts.c manage_sql_secinfo.c + manage_sql_tickets.c + manage_migrators.c scanner.c + ${BACKEND_FILES} + lsc_user.c lsc_crypt.c utils.c comm.c + otp.c + gmp.c gmp_base.c gmp_delete.c gmp_get.c gmp_tickets.c) + +add_executable (${BINARY_NAME}-test + tests.c + manage_tests.c + manage_utils_tests.c + + gvmd.c gmpd.c + sql.c + manage_ranges_all_tcp_nmap_5_51_top_100.c + manage_ranges_all_tcp_nmap_5_51_top_1000.c + manage_ranges_iana_tcp_2012.c manage_ranges_iana_tcp_udp_2012.c + manage_ranges_nmap_5_51_top_2000_top_100.c + manage_acl.c manage_config_discovery.c + manage_config_host_discovery.c manage_config_system_discovery.c + manage_sql.c manage_sql_nvts.c manage_sql_secinfo.c + manage_sql_tickets.c + manage_migrators.c scanner.c + ${BACKEND_FILES} + lsc_user.c lsc_crypt.c utils.c comm.c + otp.c + gmp.c gmp_base.c gmp_delete.c gmp_get.c gmp_tickets.c) + add_executable (${BINARY_NAME} gvmd.c gmpd.c manage_utils.c manage.c sql.c @@ -118,10 +160,16 @@ else (BACKEND STREQUAL SQLITE3) ${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS} ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS}) + target_link_libraries (${BINARY_NAME}-test cgreen m + ${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS} + ${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} + ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS} + ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS}) target_link_libraries (gvm-pg-server ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS}) endif (BACKEND STREQUAL SQLITE3) set_target_properties (${BINARY_NAME} PROPERTIES LINKER_LANGUAGE C) +set_target_properties (${BINARY_NAME}-test PROPERTIES LINKER_LANGUAGE C) if (DEBUG_FUNCTION_NAMES) add_definitions (-DDEBUG_FUNCTION_NAMES) @@ -133,6 +181,7 @@ endif (GVMD_VERSION) if (NOT CMAKE_BUILD_TYPE MATCHES "Release") target_compile_options (${BINARY_NAME} PUBLIC ${C_FLAGS_DEBUG_GVMD}) + target_compile_options (${BINARY_NAME}-test PUBLIC ${C_FLAGS_DEBUG_GVMD}) # If we got GIT_REVISION at configure time, # assume we can get it at build time as well diff --git a/src/gvmd.c b/src/gvmd.c index a6c74ab4d..ae248b961 100644 --- a/src/gvmd.c +++ b/src/gvmd.c @@ -1683,7 +1683,7 @@ manager_listen (const char *address_str_unix, const char *address_str_tls, * @return EXIT_SUCCESS on success, EXIT_FAILURE on failure. */ int -main (int argc, char** argv) +gvmd (int argc, char** argv) { /* Process options. */ From 88e5d07cc6479bb81da09ee839482eb5074a90d3 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 4 Jun 2019 08:43:40 +0200 Subject: [PATCH 02/10] Add new files for last commit --- src/manage_tests.c | 95 ++++++++++++++++++++++++++++++++++++++++ src/manage_utils_tests.c | 70 +++++++++++++++++++++++++++++ src/tests.c | 46 +++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 src/manage_tests.c create mode 100644 src/manage_utils_tests.c create mode 100644 src/tests.c diff --git a/src/manage_tests.c b/src/manage_tests.c new file mode 100644 index 000000000..74ec2ecf2 --- /dev/null +++ b/src/manage_tests.c @@ -0,0 +1,95 @@ +/* Copyright (C) 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 "manage.c" + +#include + +Describe (manage); +BeforeEach (manage) {} +AfterEach (manage) {} + +/* truncate_certificate */ + +Ensure (manage, truncate_certificate_given_truncated) +{ + const gchar *given; + gchar *truncated; + + given = "-----BEGIN CERTIFICATE-----\n" + "MIIEjTCCAvWgAwIBAgIMWtd9bxgrX+9SgEHXMA0GCSqGSIb3DQEBCwUAMGIxKjAo\n" + "BgNVBAsTIUNlcnRpZmljYXRlIEF1dGhvcml0eSBmb3IgYy5sb2NhbDESMBAGA1UE\n" + "ChMJR1ZNIFVzZXJzMRMwEQYDVQQHEwpPc25hYnJ1ZWNrMQswCQYDVQQGEwJERTAe\n" + "Fw0xODA0MTgxNzE2MzFaFw0yODA0MTcxNzE2MzFaMGIxKjAoBgNVBAsTIUNlcnRp\n" + "ZmljYXRlIEF1dGhvcml0eSBmb3IgYy5sb2NhbDESMBAGA1UEChMJR1ZNIFVzZXJz\n" + "MRMwEQYDVQQHEwpPc25hYnJ1ZWNrMQswCQYDVQQGEwJERTCCAaIwDQYJKoZIhvcN\n" + "AQEBBQADggGPADCCAYoCggGBAN7Xjg8ZUAVg3URxV8DJ7DhArjEzR7m1BKYC3PPu\n" + "yaAnRZqed4eZo9t6Gk+EvZxjkyN79Sooz9xpYV43naBLzTJlgbTIhkKDi9t9kB9O\n" + "5kA8b5YxKDHaVmmJ1oxR3k115fLtBcwyjt6juL4FvyP+zJ7v1bLcXSjgUytuAce1\n" + "C2BTLP8IaLde1bkhxINnD6moEarsZex0THQffPof6nI1gaPiDOXorzWCTegMnT1s\n" + "26jRvQog8H7Tw+TvGwENW28MwrTy5ZnzwWIND64vmPy3oC5LQhTacd++84CstuZ9\n" + "nI4mXh++gXRqP7lx9CSpVH+z7/Lo9S3JkWvl756m1ieJtX6bJtAadDdOsofbgasN\n" + "xhJ42oxjjxdYdH5s0AX2frv+OvnBIWCGN9/6Tws1VCAF1SjIB7GRuyM7FcUoONtx\n" + "svQiwNal/hOCN6DbCSM/ff76G4VwKOUlpY3GJdveTugum7V7VN9hYBSBcK45diAd\n" + "b0ZZiRSq9T61/zFayeVQWPiWfwIDAQABo0MwQTAPBgNVHRMBAf8EBTADAQH/MA8G\n" + "A1UdDwEB/wQFAwMHBgAwHQYDVR0OBBYEFBHD0+uQ+JXQmoUvLIJGldpGgaUdMA0G\n" + "CSqGSIb3DQEBCwUAA4IBgQCqW2XCz2zMW14oKUu0jq33MKUE0MKG2VUy/JjVyUl9\n" + "Vg2ZIuDFnX3qpGZJaHDOeFz3xYGcLny0QuKm4I+zYL6/rmDMhcHyuO3N+cOc+x4X\n" + "4PRz8jydhrOMED16Tg0+o5L3JDplWpmsqUKu+sY378ZNdGPBIE1LIIzOjH296SWe\n" + "0fztTTHLr56ftmakwC241Etmgf8ow95kxhFxbxB0hUFcIkCvi0S9eZ4ip0v/Yo2z\n" + "lZ/DYl9GnkdnwlHB/f1/iZzrn7arEKwhqE8L/STJH+K0nJT4IGQZnyUfId7Jb+lO\n" + "HWIyYyrUHkqIRqfybZrDXPTYGW/NvheOm8OTQmz65ySLWWNVpy2TRoLD3198GSF9\n" + "fnkIVNvsMB5h5uCzboV+HqkYX72wg1Vfda0/8M/riYbEaxNcKKfuReoPNoCOBC8h\n" + "NKOM6mBOCkc7MifVDVwCxaVlvGX5fKzHDhfSoNreotdL2mFJfk15Jjk4w3bmgiVT\n" + "u1UuTizi5guqzOf+57s4o7Q=\n" + "-----END CERTIFICATE-----\n"; + + truncated = truncate_certificate (given); + assert_that (truncated, is_equal_to_string (given)); + g_free (truncated); +} + +/* delete_reports */ + +// TODO +// +// To test this kind of function we need to isolate the code in the manage.c +// module. So we need to create stubs/mocks in manage_tests.c that simulate +// init_report_iterator, next_report, delete_report_internal and +// cleanup_iterator. Then we can use these stubs/mocks to create simple +// tests of delete_reports, like delete_reports_deletes_each_report_once or +// delete_reports_returns_negative_1_on_error. +// +// Should be easier to do after splitting Manager source code up. + +/* Test suite. */ + +TestSuite * +manage_tests () +{ + TestSuite *suite; + + suite = create_test_suite (); + + add_test_with_context (suite, manage, truncate_certificate_given_truncated); + + add_test_with_context (suite, manage, delete_reports_deletes_each_report); + + return suite; +} diff --git a/src/manage_utils_tests.c b/src/manage_utils_tests.c new file mode 100644 index 000000000..ffc3e41c0 --- /dev/null +++ b/src/manage_utils_tests.c @@ -0,0 +1,70 @@ +/* Copyright (C) 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 "manage_utils.c" + +#include + +Describe (manage_utils); +BeforeEach (manage_utils) {} +AfterEach (manage_utils) {} + +/* time_offset */ + +Ensure (manage_utils, time_offset_returns_0_when_zone_is_null) +{ + assert_that (time_offset (NULL, 1559561396), is_equal_to (0)); +} + +Ensure (manage_utils, time_offset_returns_0_when_zone_is_utc) +{ + assert_that (time_offset ("UTC", 1559561396), is_equal_to (0)); +} + +Ensure (manage_utils, time_offset_returns_correct_value) +{ + assert_that (time_offset ("Africa/Johannesburg", 1559561396), is_equal_to (7200)); +} + +/* current_offset */ + +Ensure (manage_utils, current_offset_returns_correct_values) +{ + assert_that (time_offset (NULL, 1559561396), is_equal_to (0)); + assert_that (time_offset ("UTC", 1559561396), is_equal_to (0)); + assert_that (time_offset ("Africa/Johannesburg", 1559561396), is_equal_to (7200)); +} + +/* Test suite. */ + +TestSuite * +manage_utils_tests () +{ + TestSuite *suite; + + suite = create_test_suite (); + + add_test_with_context (suite, manage_utils, time_offset_returns_0_when_zone_is_null); + add_test_with_context (suite, manage_utils, time_offset_returns_0_when_zone_is_utc); + add_test_with_context (suite, manage_utils, time_offset_returns_correct_value); + + add_test_with_context (suite, manage_utils, current_offset_returns_correct_values); + + return suite; +} diff --git a/src/tests.c b/src/tests.c new file mode 100644 index 000000000..dc38dec35 --- /dev/null +++ b/src/tests.c @@ -0,0 +1,46 @@ +/* Copyright (C) 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 + +Describe (tests); +BeforeEach (tests) {} +AfterEach (tests) {} + +TestSuite * +manage_tests (); + +TestSuite * +manage_utils_tests (); + +int +main (int argc, char **argv) +{ + TestSuite *suite; + + suite = create_test_suite (); + + add_suite (suite, manage_tests ()); + add_suite (suite, manage_utils_tests ()); + + if (argc > 1) + return run_single_test (suite, argv[1], create_text_reporter()); + + return run_test_suite (suite, create_text_reporter ()); +} From cd1d131a5a4ee901cf408f531775b1eda3f069dd Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 4 Jun 2019 14:07:18 +0200 Subject: [PATCH 03/10] Add a more complex test, for gmp_tickets.c This adds a test for create_ticket_run, which is complex because create_ticket_run calls into other parts of the gvmd code (copy_ticket, ticket_uuid, log_event, etc). The test creates 'mock' versions of those functions. A gcc attribute is used as an easy way to override the mocked functions. The alternative would be to link against less of the gvmd code and to fill the gaps with stub functions. But our code is not very cleanly separated to this is a lot of work. Should get easier as we clean things up, if the attribute is not suitable. --- src/CMakeLists.txt | 18 ++++--- src/gmp_base.h | 2 +- src/gmp_tickets_tests.c | 112 ++++++++++++++++++++++++++++++++++++++++ src/manage_tickets.h | 6 +-- src/tests.c | 2 +- 5 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 src/gmp_tickets_tests.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 81c418717..ba7de69fa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -110,13 +110,10 @@ add_executable (${BINARY_NAME}-test otp.c gmp.c gmp_base.c gmp_delete.c gmp_get.c gmp_tickets.c) -add_executable (${BINARY_NAME}-test - tests.c - manage_tests.c - manage_utils_tests.c - +add_executable (gmp-tickets-test + gmp_tickets_tests.c gvmd.c gmpd.c - sql.c + manage_utils.c manage.c sql.c manage_ranges_all_tcp_nmap_5_51_top_100.c manage_ranges_all_tcp_nmap_5_51_top_1000.c manage_ranges_iana_tcp_2012.c manage_ranges_iana_tcp_udp_2012.c @@ -129,7 +126,7 @@ add_executable (${BINARY_NAME}-test ${BACKEND_FILES} lsc_user.c lsc_crypt.c utils.c comm.c otp.c - gmp.c gmp_base.c gmp_delete.c gmp_get.c gmp_tickets.c) + gmp.c gmp_base.c gmp_delete.c gmp_get.c) add_executable (${BINARY_NAME} gvmd.c gmpd.c @@ -165,11 +162,17 @@ else (BACKEND STREQUAL SQLITE3) ${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS} ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS}) + target_link_libraries (gmp-tickets-test cgreen m + ${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS} + ${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} + ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS} + ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS}) target_link_libraries (gvm-pg-server ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS}) endif (BACKEND STREQUAL SQLITE3) set_target_properties (${BINARY_NAME} PROPERTIES LINKER_LANGUAGE C) set_target_properties (${BINARY_NAME}-test PROPERTIES LINKER_LANGUAGE C) +set_target_properties (gmp-tickets-test PROPERTIES LINKER_LANGUAGE C) if (DEBUG_FUNCTION_NAMES) add_definitions (-DDEBUG_FUNCTION_NAMES) @@ -182,6 +185,7 @@ endif (GVMD_VERSION) if (NOT CMAKE_BUILD_TYPE MATCHES "Release") target_compile_options (${BINARY_NAME} PUBLIC ${C_FLAGS_DEBUG_GVMD}) target_compile_options (${BINARY_NAME}-test PUBLIC ${C_FLAGS_DEBUG_GVMD}) + target_compile_options (gmp-tickets-test PUBLIC ${C_FLAGS_DEBUG_GVMD}) # If we got GIT_REVISION at configure time, # assume we can get it at build time as well diff --git a/src/gmp_base.h b/src/gmp_base.h index 606e4490f..c2a9f31b8 100644 --- a/src/gmp_base.h +++ b/src/gmp_base.h @@ -112,7 +112,7 @@ internal_error_send_to_client (GError **); while (0) void -log_event (const char *, const char *, const char *, const char *); +log_event (const char *, const char *, const char *, const char *) __attribute__((weak)); void log_event_fail (const char *, const char *, const char *, const char *); diff --git a/src/gmp_tickets_tests.c b/src/gmp_tickets_tests.c new file mode 100644 index 000000000..9fc407722 --- /dev/null +++ b/src/gmp_tickets_tests.c @@ -0,0 +1,112 @@ +/* Copyright (C) 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 "gmp_tickets.c" + +#include +#include + +Describe (gmp_tickets); +BeforeEach (gmp_tickets) {} +AfterEach (gmp_tickets) {} + +/* create_ticket_run */ + +int +dummy_client_writer (const char *message, void *data) +{ + return 0; +} + +void +log_event (const char *type, const char *type_name, const char *id, + const char *action) +{ + return; +} + +gchar * +ticket_uuid (ticket_t ticket) +{ + return g_strdup ("9b5da19e-86b4-11e9-b0d2-28d24461215b"); +} + +int +copy_ticket (const char *comment, const char *ticket_id, ticket_t *new_ticket) +{ + mock (); + return 0; +} + +int +create_ticket (const char *comment, const char *result_id, + const char *user_id, const char *open_note, + ticket_t *ticket) +{ + mock (); + return 0; +} + +Ensure (gmp_tickets, create_ticket_run_calls_copy_ticket_when_given_copy) +{ + gmp_parser_t gmp_parser; + GError *error; + const gchar *uuid; + + /* Check that create_ticket_run calls only copy_ticket when given COPY. */ + + uuid = "9b5da19e-86b4-11e9-b0d2-28d24461215b"; + gmp_parser.client_writer = dummy_client_writer; + + /* */ + create_ticket_start (&gmp_parser, NULL, NULL); + + create_ticket_element_start (&gmp_parser, "copy", NULL, NULL); + create_ticket_element_text (uuid, strlen (uuid)); + create_ticket_element_end (&gmp_parser, &error, "copy"); + + create_ticket_element_start (&gmp_parser, "comment", NULL, NULL); + create_ticket_element_text (uuid, strlen (uuid)); + create_ticket_element_end (&gmp_parser, &error, "comment"); + + /* */ + xml_handle_end_element (create_ticket_data.context, "create_ticket"); + assert_that (create_ticket_data.context->done, is_not_equal_to (0)); + + expect (copy_ticket); + never_expect (create_ticket); + create_ticket_run (&gmp_parser, &error); +} + +/* Test suite. */ + +int +main (int argc, char **argv) +{ + TestSuite *suite; + + suite = create_test_suite (); + + add_test_with_context (suite, gmp_tickets, create_ticket_run_calls_copy_ticket_when_given_copy); + + if (argc > 1) + return run_single_test (suite, argv[1], create_text_reporter ()); + + return run_test_suite (suite, create_text_reporter ()); +} diff --git a/src/manage_tickets.h b/src/manage_tickets.h index 88a7e39eb..9b99ca545 100644 --- a/src/manage_tickets.h +++ b/src/manage_tickets.h @@ -117,13 +117,13 @@ int trash_ticket_writable (ticket_t); int -create_ticket (const char *, const char *, const char *, const char *, ticket_t *); +create_ticket (const char *, const char *, const char *, const char *, ticket_t *) __attribute__((weak)); int -copy_ticket (const char *, const char *, ticket_t *); +copy_ticket (const char *, const char *, ticket_t *) __attribute__((weak)); char* -ticket_uuid (ticket_t); +ticket_uuid (ticket_t) __attribute__((weak)); int modify_ticket (const gchar *, const gchar *, const gchar *, diff --git a/src/tests.c b/src/tests.c index dc38dec35..38ccb9f03 100644 --- a/src/tests.c +++ b/src/tests.c @@ -40,7 +40,7 @@ main (int argc, char **argv) add_suite (suite, manage_utils_tests ()); if (argc > 1) - return run_single_test (suite, argv[1], create_text_reporter()); + return run_single_test (suite, argv[1], create_text_reporter ()); return run_test_suite (suite, create_text_reporter ()); } From 7399bbe92fd1e37f7faef8e0422d701e6fe8de79 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 4 Jun 2019 14:37:43 +0200 Subject: [PATCH 04/10] Separate out gvmd main so tests can have a main --- src/CMakeLists.txt | 2 +- src/gvmd.h | 31 +++++++++++++++++++++++++++++++ src/main.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/gvmd.h create mode 100644 src/main.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ba7de69fa..b0aa8b085 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -129,7 +129,7 @@ add_executable (gmp-tickets-test gmp.c gmp_base.c gmp_delete.c gmp_get.c) add_executable (${BINARY_NAME} - gvmd.c gmpd.c + main.c gvmd.c gmpd.c manage_utils.c manage.c sql.c manage_ranges_all_tcp_nmap_5_51_top_100.c manage_ranges_all_tcp_nmap_5_51_top_1000.c diff --git a/src/gvmd.h b/src/gvmd.h new file mode 100644 index 000000000..81a105f8e --- /dev/null +++ b/src/gvmd.h @@ -0,0 +1,31 @@ +/* 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. + */ + +/* + * @file gvmd.h + * @brief Headers for Greenbone Vulnerability Manager entry point. + */ + +#ifndef _GVMD_H +#define _GVMD_H + +int +gvmd (int, char **); + +#endif /* not _GVMD_H */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 000000000..558bea235 --- /dev/null +++ b/src/main.c @@ -0,0 +1,41 @@ +/* Copyright (C) 2009-2018 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. + */ + +/** + * @file main.c + * @brief Main function of gvmd. + * + * This file separates out the "main" function of gvmd. + */ + +#include "gvmd.h" + +/** + * @brief Main function. + * + * @param[in] argc The number of arguments in argv. + * @param[in] argv The list of arguments to the program. + * + * @return EXIT_SUCCESS on success, EXIT_FAILURE on failure. + */ +int +main (int argc, char **argv) +{ + return gvmd (argc, argv); +} From 73eb2421c04393a317cc8c77cb6271983b66a25d Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 4 Jun 2019 14:38:23 +0200 Subject: [PATCH 05/10] Remove stray test addition --- src/manage_tests.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/manage_tests.c b/src/manage_tests.c index 74ec2ecf2..550589d29 100644 --- a/src/manage_tests.c +++ b/src/manage_tests.c @@ -89,7 +89,5 @@ manage_tests () add_test_with_context (suite, manage, truncate_certificate_given_truncated); - add_test_with_context (suite, manage, delete_reports_deletes_each_report); - return suite; } From 42a9532740444ac6d090e06ad306baa5ad7f93a9 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Tue, 4 Jun 2019 15:09:54 +0200 Subject: [PATCH 06/10] Use CMake to run tests --- CMakeLists.txt | 2 ++ src/CMakeLists.txt | 45 +++++++++++++++++++++++++++++++++------ src/manage_tests.c | 9 +++++--- src/manage_utils_tests.c | 9 +++++--- src/tests.c | 46 ---------------------------------------- 5 files changed, 52 insertions(+), 59 deletions(-) delete mode 100644 src/tests.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1aca5286b..c0dcbd00f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,8 @@ SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) include (FindPkgConfig) +enable_testing () + if (NOT CMAKE_BUILD_TYPE) set (CMAKE_BUILD_TYPE Debug) endif (NOT CMAKE_BUILD_TYPE) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b0aa8b085..f92000358 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -89,13 +89,32 @@ else (BACKEND STREQUAL SQLITE3) set (BINARY_NAME "gvmd-pg") endif (BACKEND STREQUAL SQLITE3) -add_executable (${BINARY_NAME}-test - tests.c - manage_tests.c +add_executable (manage-utils-test manage_utils_tests.c gvmd.c gmpd.c - sql.c + manage.c sql.c + manage_ranges_all_tcp_nmap_5_51_top_100.c + manage_ranges_all_tcp_nmap_5_51_top_1000.c + manage_ranges_iana_tcp_2012.c manage_ranges_iana_tcp_udp_2012.c + manage_ranges_nmap_5_51_top_2000_top_100.c + manage_acl.c manage_config_discovery.c + manage_config_host_discovery.c manage_config_system_discovery.c + manage_sql.c manage_sql_nvts.c manage_sql_secinfo.c + manage_sql_tickets.c + manage_migrators.c scanner.c + ${BACKEND_FILES} + lsc_user.c lsc_crypt.c utils.c comm.c + otp.c + gmp.c gmp_base.c gmp_delete.c gmp_get.c gmp_tickets.c) + +add_test (manage-utils-test manage-utils-test) + +add_executable (manage-test + manage_tests.c + + gvmd.c gmpd.c + manage_utils.c sql.c manage_ranges_all_tcp_nmap_5_51_top_100.c manage_ranges_all_tcp_nmap_5_51_top_1000.c manage_ranges_iana_tcp_2012.c manage_ranges_iana_tcp_udp_2012.c @@ -110,8 +129,11 @@ add_executable (${BINARY_NAME}-test otp.c gmp.c gmp_base.c gmp_delete.c gmp_get.c gmp_tickets.c) +add_test (manage-test manage-test) + add_executable (gmp-tickets-test gmp_tickets_tests.c + gvmd.c gmpd.c manage_utils.c manage.c sql.c manage_ranges_all_tcp_nmap_5_51_top_100.c @@ -128,6 +150,8 @@ add_executable (gmp-tickets-test otp.c gmp.c gmp_base.c gmp_delete.c gmp_get.c) +add_test (gmp-tickets-test gmp-tickets-test) + add_executable (${BINARY_NAME} main.c gvmd.c gmpd.c manage_utils.c manage.c sql.c @@ -157,7 +181,12 @@ else (BACKEND STREQUAL SQLITE3) ${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS} ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS}) - target_link_libraries (${BINARY_NAME}-test cgreen m + target_link_libraries (manage-test cgreen m + ${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS} + ${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} + ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS} + ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS}) + target_link_libraries (manage-utils-test cgreen m ${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS} ${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS} @@ -171,7 +200,8 @@ else (BACKEND STREQUAL SQLITE3) endif (BACKEND STREQUAL SQLITE3) set_target_properties (${BINARY_NAME} PROPERTIES LINKER_LANGUAGE C) -set_target_properties (${BINARY_NAME}-test PROPERTIES LINKER_LANGUAGE C) +set_target_properties (manage-test PROPERTIES LINKER_LANGUAGE C) +set_target_properties (manage-utils-test PROPERTIES LINKER_LANGUAGE C) set_target_properties (gmp-tickets-test PROPERTIES LINKER_LANGUAGE C) if (DEBUG_FUNCTION_NAMES) @@ -184,7 +214,8 @@ endif (GVMD_VERSION) if (NOT CMAKE_BUILD_TYPE MATCHES "Release") target_compile_options (${BINARY_NAME} PUBLIC ${C_FLAGS_DEBUG_GVMD}) - target_compile_options (${BINARY_NAME}-test PUBLIC ${C_FLAGS_DEBUG_GVMD}) + target_compile_options (manage-test PUBLIC ${C_FLAGS_DEBUG_GVMD}) + target_compile_options (manage-utils-test PUBLIC ${C_FLAGS_DEBUG_GVMD}) target_compile_options (gmp-tickets-test PUBLIC ${C_FLAGS_DEBUG_GVMD}) # If we got GIT_REVISION at configure time, diff --git a/src/manage_tests.c b/src/manage_tests.c index 550589d29..e158b3b5a 100644 --- a/src/manage_tests.c +++ b/src/manage_tests.c @@ -80,8 +80,8 @@ Ensure (manage, truncate_certificate_given_truncated) /* Test suite. */ -TestSuite * -manage_tests () +int +main (int argc, char **argv) { TestSuite *suite; @@ -89,5 +89,8 @@ manage_tests () add_test_with_context (suite, manage, truncate_certificate_given_truncated); - return suite; + if (argc > 1) + return run_single_test (suite, argv[1], create_text_reporter ()); + + return run_test_suite (suite, create_text_reporter ()); } diff --git a/src/manage_utils_tests.c b/src/manage_utils_tests.c index ffc3e41c0..6c2713a05 100644 --- a/src/manage_utils_tests.c +++ b/src/manage_utils_tests.c @@ -53,8 +53,8 @@ Ensure (manage_utils, current_offset_returns_correct_values) /* Test suite. */ -TestSuite * -manage_utils_tests () +int +main (int argc, char **argv) { TestSuite *suite; @@ -66,5 +66,8 @@ manage_utils_tests () add_test_with_context (suite, manage_utils, current_offset_returns_correct_values); - return suite; + if (argc > 1) + return run_single_test (suite, argv[1], create_text_reporter ()); + + return run_test_suite (suite, create_text_reporter ()); } diff --git a/src/tests.c b/src/tests.c deleted file mode 100644 index 38ccb9f03..000000000 --- a/src/tests.c +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright (C) 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 - -Describe (tests); -BeforeEach (tests) {} -AfterEach (tests) {} - -TestSuite * -manage_tests (); - -TestSuite * -manage_utils_tests (); - -int -main (int argc, char **argv) -{ - TestSuite *suite; - - suite = create_test_suite (); - - add_suite (suite, manage_tests ()); - add_suite (suite, manage_utils_tests ()); - - if (argc > 1) - return run_single_test (suite, argv[1], create_text_reporter ()); - - return run_test_suite (suite, create_text_reporter ()); -} From 580c0b1168c11af39787844e24dd56e8d8dee896 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Wed, 5 Jun 2019 12:15:13 +0200 Subject: [PATCH 07/10] Add target 'tests' to build the tests --- CMakeLists.txt | 6 ++++-- src/CMakeLists.txt | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c0dcbd00f..950615abf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,8 +34,6 @@ SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) include (FindPkgConfig) -enable_testing () - if (NOT CMAKE_BUILD_TYPE) set (CMAKE_BUILD_TYPE Debug) endif (NOT CMAKE_BUILD_TYPE) @@ -292,6 +290,10 @@ endif (NOT SKIP_SRC) add_subdirectory (doc) +## Testing + +enable_testing () + ## Install install (CODE "file (MAKE_DIRECTORY \$ENV{DESTDIR}${GVMD_DATA_DIR}/report_formats)") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f92000358..dc2499ad8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -90,6 +90,7 @@ else (BACKEND STREQUAL SQLITE3) endif (BACKEND STREQUAL SQLITE3) add_executable (manage-utils-test + EXCLUDE_FROM_ALL manage_utils_tests.c gvmd.c gmpd.c @@ -111,6 +112,7 @@ add_executable (manage-utils-test add_test (manage-utils-test manage-utils-test) add_executable (manage-test + EXCLUDE_FROM_ALL manage_tests.c gvmd.c gmpd.c @@ -132,6 +134,7 @@ add_executable (manage-test add_test (manage-test manage-test) add_executable (gmp-tickets-test + EXCLUDE_FROM_ALL gmp_tickets_tests.c gvmd.c gmpd.c @@ -152,6 +155,9 @@ add_executable (gmp-tickets-test add_test (gmp-tickets-test gmp-tickets-test) +add_custom_target (tests + DEPENDS gmp-tickets-test manage-test manage-utils-test) + add_executable (${BINARY_NAME} main.c gvmd.c gmpd.c manage_utils.c manage.c sql.c From 36f8fc00fcf02f869cf72349703345be5b1f6621 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Wed, 5 Jun 2019 12:20:48 +0200 Subject: [PATCH 08/10] Neaten formatting --- src/gmp_base.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gmp_base.h b/src/gmp_base.h index c2a9f31b8..2101c011e 100644 --- a/src/gmp_base.h +++ b/src/gmp_base.h @@ -112,7 +112,8 @@ internal_error_send_to_client (GError **); while (0) void -log_event (const char *, const char *, const char *, const char *) __attribute__((weak)); +log_event (const char *, const char *, const char *, const char *) + __attribute__((weak)); void log_event_fail (const char *, const char *, const char *, const char *); From 128e549fcba93badb74d339cec4d419c3e59d6f3 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Wed, 5 Jun 2019 12:27:07 +0200 Subject: [PATCH 09/10] Neaten formatting, second attempt --- src/gmp_base.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gmp_base.h b/src/gmp_base.h index 2101c011e..84c289a2b 100644 --- a/src/gmp_base.h +++ b/src/gmp_base.h @@ -113,7 +113,7 @@ internal_error_send_to_client (GError **); void log_event (const char *, const char *, const char *, const char *) - __attribute__((weak)); + __attribute__ ((weak)); void log_event_fail (const char *, const char *, const char *, const char *); From 5145cad39ae093bf431bf5bf27294eaf6731f02b Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Mon, 10 Jun 2019 17:41:29 +0200 Subject: [PATCH 10/10] Add test of current_offset This test requires a system function to be wrapped, and specifies the return of the system function in the test (which is more usefull than specifying it in the wrapper). --- src/CMakeLists.txt | 3 ++- src/manage_utils_tests.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dc2499ad8..ca1ddf2d4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -196,7 +196,8 @@ else (BACKEND STREQUAL SQLITE3) ${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS} ${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} ${LIBGVM_BASE_LDFLAGS} ${LIBGVM_UTIL_LDFLAGS} ${LIBGVM_OSP_LDFLAGS} ${LIBGVM_GMP_LDFLAGS} - ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS}) + ${LIBICAL_LDFLAGS} ${LINKER_HARDENING_FLAGS} + -Wl,--wrap=time) target_link_libraries (gmp-tickets-test cgreen m ${GNUTLS_LDFLAGS} ${GPGME_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT} ${LINKER_HARDENING_FLAGS} ${LINKER_DEBUG_FLAGS} ${PostgreSQL_LIBRARIES} ${GLIB_LDFLAGS} ${GTHREAD_LDFLAGS} diff --git a/src/manage_utils_tests.c b/src/manage_utils_tests.c index 6c2713a05..a8fab2641 100644 --- a/src/manage_utils_tests.c +++ b/src/manage_utils_tests.c @@ -20,6 +20,7 @@ #include "manage_utils.c" #include +#include Describe (manage_utils); BeforeEach (manage_utils) {} @@ -51,6 +52,21 @@ Ensure (manage_utils, current_offset_returns_correct_values) assert_that (time_offset ("Africa/Johannesburg", 1559561396), is_equal_to (7200)); } +/* next_time */ + +time_t +__wrap_time (time_t *tloc) +{ + return mock (); +} + +Ensure (manage_utils, next_time_returns_correct_value) +{ + always_expect (__wrap_time, will_return (1560176823)); + assert_that (next_time (1560176800, 40, 0, 0, "Africa/Johannesburg", 0), + is_equal_to (1560176840)); +} + /* Test suite. */ int @@ -66,6 +82,8 @@ main (int argc, char **argv) add_test_with_context (suite, manage_utils, current_offset_returns_correct_values); + add_test_with_context (suite, manage_utils, next_time_returns_correct_value); + if (argc > 1) return run_single_test (suite, argv[1], create_text_reporter ());