Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cgreen based unit tests for gvmd #579

Merged
merged 13 commits into from
Jun 14, 2019
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,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)")
Expand Down
93 changes: 92 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,54 @@ else (BACKEND STREQUAL SQLITE3)
set (BINARY_NAME "gvmd-pg")
endif (BACKEND STREQUAL SQLITE3)

add_executable (${BINARY_NAME}
add_executable (manage-utils-test
EXCLUDE_FROM_ALL
manage_utils_tests.c

gvmd.c gmpd.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
EXCLUDE_FROM_ALL
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
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-test manage-test)

add_executable (gmp-tickets-test
EXCLUDE_FROM_ALL
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
Expand All @@ -104,6 +151,28 @@ add_executable (${BINARY_NAME}
${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)

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
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)

if (BACKEND STREQUAL SQLITE3)
Expand All @@ -118,10 +187,29 @@ 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 (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}
${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}
${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 (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)
add_definitions (-DDEBUG_FUNCTION_NAMES)
Expand All @@ -133,6 +221,9 @@ endif (GVMD_VERSION)

if (NOT CMAKE_BUILD_TYPE MATCHES "Release")
target_compile_options (${BINARY_NAME} 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,
# assume we can get it at build time as well
Expand Down
3 changes: 2 additions & 1 deletion src/gmp_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ 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 *);
Expand Down
112 changes: 112 additions & 0 deletions src/gmp_tickets_tests.c
Original file line number Diff line number Diff line change
@@ -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 <cgreen/cgreen.h>
#include <cgreen/mocks.h>

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> */
create_ticket_start (&gmp_parser, NULL, NULL);
timopollmeier marked this conversation as resolved.
Show resolved Hide resolved

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");

/* </CREATE_TICKET> */
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 ());
}
2 changes: 1 addition & 1 deletion src/gvmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,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. */

Expand Down
31 changes: 31 additions & 0 deletions src/gvmd.h
Original file line number Diff line number Diff line change
@@ -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 */
41 changes: 41 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -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);
}
Loading