From 58f38294c94b5dea454f7ce9b862e09134985c7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 28 Nov 2019 12:33:56 +0100 Subject: [PATCH 1/7] Add a gvm_libs_version function Allow users of gvm-libs request the current version string. --- CMakeLists.txt | 2 +- base/CMakeLists.txt | 4 ++-- base/version.c | 26 ++++++++++++++++++++++++++ base/version.h | 31 +++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 base/version.c create mode 100644 base/version.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 24c71b04a..ca303dd28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -211,7 +211,7 @@ else (PROJECT_BETA_RELEASE) set (LIBGVMCONFIG_VERSION "${PROJECT_VERSION_STRING}") endif (PROJECT_BETA_RELEASE) -set (GVMLIB_VERSION "${LIBGVMCONFIG_VERSION}") +add_definitions(-DGVM_LIBS_VERSION="${LIBGVMCONFIG_VERSION}") # Configure Doxyfile with version number configure_file (doc/Doxyfile.in ${CMAKE_BINARY_DIR}/doc/Doxyfile @ONLY) diff --git a/base/CMakeLists.txt b/base/CMakeLists.txt index ae16e750b..3a0d4393e 100644 --- a/base/CMakeLists.txt +++ b/base/CMakeLists.txt @@ -31,11 +31,11 @@ include_directories (${GLIB_INCLUDE_DIRS}) set (FILES array.c credentials.c cvss.c drop_privileges.c hosts.c logging.c networking.c nvti.c pidfile.c prefs.c proctitle.c pwpolicy.c - settings.c strings.c) + settings.c strings.c version.c) set (HEADERS array.h credentials.h cvss.h drop_privileges.h hosts.h logging.h networking.h nvti.h pidfile.h prefs.h proctitle.h pwpolicy.h - settings.h strings.h) + settings.h strings.h version.h) if (BUILD_STATIC) set (LIBGVM_BASE_NAME gvm_base_static) diff --git a/base/version.c b/base/version.c new file mode 100644 index 000000000..5cdc70427 --- /dev/null +++ b/base/version.c @@ -0,0 +1,26 @@ +/* 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 "version.h" + +const char * +gvm_libs_version (void) +{ + return GVM_LIBS_VERSION; +} diff --git a/base/version.h b/base/version.h new file mode 100644 index 000000000..030bb50ad --- /dev/null +++ b/base/version.h @@ -0,0 +1,31 @@ +/* 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. + */ + +/** + * @file + * @brief Version utilities. + */ + +#ifndef _GVM_VERSION_H +#define _GVM_VERSION_H + +const char * +gvm_libs_version (void); + +#endif From 06e436f1184d43f0c7c08d73a7437f2189335188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 28 Nov 2019 12:36:53 +0100 Subject: [PATCH 2/7] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ca259ec7..2c0f31238 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Add option to set finished hosts in OSP targets [#298](https://github.com/greenbone/gvm-libs/pull/298) - Add a fast memory-only XML parser [#299](https://github.com/greenbone/gvm-libs/pull/299) +- Add new function gvm_libs_version [#301](https://github.com/greenbone/gvm-libs/pull/301) ### Fixed - Fix sigsegv when no plugin_feed_info.inc file present. [#278](https://github.com/greenbone/gvm-libs/pull/278) From 26274d4ddac09081af9c7b88fede68e779edeed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 28 Nov 2019 14:49:47 +0100 Subject: [PATCH 3/7] Use find_package to search for cgreen Update cmake files to use find_package to search for cgreen and use variables from it. --- CMakeLists.txt | 2 ++ base/CMakeLists.txt | 5 ++++- util/CMakeLists.txt | 4 +++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ca303dd28..ac0692c3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -223,6 +223,8 @@ configure_file (VERSION.in ${CMAKE_BINARY_DIR}/VERSION @ONLY) enable_testing () +find_package (cgreen REQUIRED) + add_custom_target (tests DEPENDS array-test xmlutils-test) diff --git a/base/CMakeLists.txt b/base/CMakeLists.txt index 3a0d4393e..5345dfae7 100644 --- a/base/CMakeLists.txt +++ b/base/CMakeLists.txt @@ -78,7 +78,10 @@ add_executable (array-test add_test (array-test array-test) -target_link_libraries (array-test cgreen ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS}) +target_include_directories (array-test PRIVATE ${CGREEN_INCLUDE_DIRS}) + +target_link_libraries (array-test ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS}) + ## Install diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 96815d76f..5a734f1d9 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -177,7 +177,9 @@ add_executable (xmlutils-test add_test (xmlutils-test xmlutils-test) -target_link_libraries (xmlutils-test cgreen +target_include_directories (xmlutils-test PRIVATE ${CGREEN_INCLUDE_DIRS}) + +target_link_libraries (xmlutils-test ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${GIO_LDFLAGS} ${GPGME_LDFLAGS} ${ZLIB_LDFLAGS} ${RADIUS_LDFLAGS} ${LIBSSH_LDFLAGS} ${GNUTLS_LDFLAGS} ${GCRYPT_LDFLAGS} ${LDAP_LDFLAGS} ${REDIS_LDFLAGS} From d0cad6445f17fdccf4c2837a83a5b48c2002ecc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 28 Nov 2019 14:50:52 +0100 Subject: [PATCH 4/7] Add a simple test for gvm_libs_version --- CMakeLists.txt | 2 +- base/CMakeLists.txt | 9 ++++++++ base/version_tests.c | 51 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 base/version_tests.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ac0692c3f..146c1a9c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -226,7 +226,7 @@ enable_testing () find_package (cgreen REQUIRED) add_custom_target (tests - DEPENDS array-test xmlutils-test) + DEPENDS array-test xmlutils-test version-test) ## Program diff --git a/base/CMakeLists.txt b/base/CMakeLists.txt index 5345dfae7..7dc9e6965 100644 --- a/base/CMakeLists.txt +++ b/base/CMakeLists.txt @@ -82,6 +82,15 @@ target_include_directories (array-test PRIVATE ${CGREEN_INCLUDE_DIRS}) target_link_libraries (array-test ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS}) +add_executable (version-test + EXCLUDE_FROM_ALL + version_tests.c) + +add_test (version-test version-test) + +target_include_directories (version-test PRIVATE ${CGREEN_INCLUDE_DIRS}) + +target_link_libraries (version-test ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS}) ## Install diff --git a/base/version_tests.c b/base/version_tests.c new file mode 100644 index 000000000..452c89770 --- /dev/null +++ b/base/version_tests.c @@ -0,0 +1,51 @@ +/* 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 "version.c" + +#include +#include + +Describe (version); +BeforeEach (version) +{ +} +AfterEach (version) +{ +} + +Ensure (version, compare_version) +{ + assert_that (strcmp (gvm_libs_version (), GVM_LIBS_VERSION) == 0) +} + +int +main (int argc, char **argv) +{ + TestSuite *suite; + + suite = create_test_suite (); + + add_test_with_context (suite, version, compare_version); + + if (argc > 1) + return run_single_test (suite, argv[1], create_text_reporter ()); + + return run_test_suite (suite, create_text_reporter ()); +} From af603f3de4c04a38703cc4f49fb9ca7ddfed4d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 28 Nov 2019 15:16:31 +0100 Subject: [PATCH 5/7] Add cmake option to enable test builds --- CMakeLists.txt | 22 ++++++++++++---------- base/CMakeLists.txt | 26 ++++++++++++++------------ util/CMakeLists.txt | 28 +++++++++++++++------------- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 146c1a9c9..2cd976a69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ endif (NOT CMAKE_BUILD_TYPE) OPTION(BUILD_STATIC "Build static versions of the libraries" OFF) OPTION(ENABLE_COVERAGE "Enable support for coverage analysis" OFF) +OPTION(BUILD_TESTS "Build tests for the libraries" OFF) if (NOT BUILD_STATIC) set (BUILD_SHARED ON) @@ -221,12 +222,15 @@ configure_file (VERSION.in ${CMAKE_BINARY_DIR}/VERSION @ONLY) ## Testing -enable_testing () +if (BUILD_TESTS AND NOT SKIP_SRC) + enable_testing () -find_package (cgreen REQUIRED) + find_package (cgreen REQUIRED) -add_custom_target (tests - DEPENDS array-test xmlutils-test version-test) + add_custom_target (tests + DEPENDS array-test xmlutils-test version-test) + +endif (BUILD_TESTS AND NOT SKIP_SRC) ## Program @@ -241,11 +245,9 @@ endif (NOT SKIP_SRC) add_subdirectory (doc) -## Tests - -add_subdirectory (tests) -add_test (NAME testhosts COMMAND test-hosts localhost) - -enable_testing () +if (BUILD_TESTS AND NOT SKIP_SRC) + add_subdirectory (tests) + add_test (NAME testhosts COMMAND test-hosts localhost) +endif (BUILD_TESTS AND NOT SKIP_SRC) ## End diff --git a/base/CMakeLists.txt b/base/CMakeLists.txt index 7dc9e6965..cf36ae390 100644 --- a/base/CMakeLists.txt +++ b/base/CMakeLists.txt @@ -72,25 +72,27 @@ endif (GVM_SYSCONF_DIR) ## Tests -add_executable (array-test - EXCLUDE_FROM_ALL - array_tests.c) +if (BUILD_TESTS) + add_executable (array-test + EXCLUDE_FROM_ALL + array_tests.c) -add_test (array-test array-test) + add_test (array-test array-test) -target_include_directories (array-test PRIVATE ${CGREEN_INCLUDE_DIRS}) + target_include_directories (array-test PRIVATE ${CGREEN_INCLUDE_DIRS}) -target_link_libraries (array-test ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS}) + target_link_libraries (array-test ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS}) -add_executable (version-test - EXCLUDE_FROM_ALL - version_tests.c) + add_executable (version-test + EXCLUDE_FROM_ALL + version_tests.c) -add_test (version-test version-test) + add_test (version-test version-test) -target_include_directories (version-test PRIVATE ${CGREEN_INCLUDE_DIRS}) + target_include_directories (version-test PRIVATE ${CGREEN_INCLUDE_DIRS}) -target_link_libraries (version-test ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS}) + target_link_libraries (version-test ${CGREEN_LIBRARIES} ${GLIB_LDFLAGS} ${LINKER_HARDENING_FLAGS}) +endif (BUILD_TESTS) ## Install diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 5a734f1d9..246fa7ea2 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -171,24 +171,26 @@ endif (BUILD_SHARED) ## Tests -add_executable (xmlutils-test - EXCLUDE_FROM_ALL - xmlutils_tests.c) +if (BUILD_TESTS) + add_executable (xmlutils-test + EXCLUDE_FROM_ALL + xmlutils_tests.c) -add_test (xmlutils-test xmlutils-test) + add_test (xmlutils-test xmlutils-test) -target_include_directories (xmlutils-test PRIVATE ${CGREEN_INCLUDE_DIRS}) + target_include_directories (xmlutils-test PRIVATE ${CGREEN_INCLUDE_DIRS}) -target_link_libraries (xmlutils-test ${CGREEN_LIBRARIES} - ${GLIB_LDFLAGS} ${GIO_LDFLAGS} ${GPGME_LDFLAGS} ${ZLIB_LDFLAGS} - ${RADIUS_LDFLAGS} ${LIBSSH_LDFLAGS} ${GNUTLS_LDFLAGS} - ${GCRYPT_LDFLAGS} ${LDAP_LDFLAGS} ${REDIS_LDFLAGS} - ${LIBXML2_LDFLAGS} ${UUID_LDFLAGS} - ${LINKER_HARDENING_FLAGS}) + target_link_libraries (xmlutils-test ${CGREEN_LIBRARIES} + ${GLIB_LDFLAGS} ${GIO_LDFLAGS} ${GPGME_LDFLAGS} ${ZLIB_LDFLAGS} + ${RADIUS_LDFLAGS} ${LIBSSH_LDFLAGS} ${GNUTLS_LDFLAGS} + ${GCRYPT_LDFLAGS} ${LDAP_LDFLAGS} ${REDIS_LDFLAGS} + ${LIBXML2_LDFLAGS} ${UUID_LDFLAGS} + ${LINKER_HARDENING_FLAGS}) -add_custom_target (tests-xmlutils - DEPENDS xmlutils-test) + add_custom_target (tests-xmlutils + DEPENDS xmlutils-test) +endif (BUILD_TESTS) ## Install configure_file (libgvm_util.pc.in ${CMAKE_BINARY_DIR}/libgvm_util.pc @ONLY) From 9313961c059fa32736855f480e2f48cba15ba7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 28 Nov 2019 15:17:02 +0100 Subject: [PATCH 6/7] Update circleci config to build tests for test_unit check --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e01d33713..3a22eec94 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -15,7 +15,7 @@ jobs: - checkout - run: name: Configure and run unit tests - command: mkdir build && cd build/ && cmake -DCMAKE_BUILD_TYPE=Release .. && make install && make tests && CTEST_OUTPUT_ON_FAILURE=1 make test + command: mkdir build && cd build/ && cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=1 .. && make install && make tests && CTEST_OUTPUT_ON_FAILURE=1 make test build_gcc_core: docker: - image: greenbone/build-env-gvm-libs-master-debian-stretch-gcc-core From ef631cd7322400ee77c146d871116d13448128fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 28 Nov 2019 16:05:57 +0100 Subject: [PATCH 7/7] Use a more descriptive test name --- base/version_tests.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/base/version_tests.c b/base/version_tests.c index 452c89770..e51413d11 100644 --- a/base/version_tests.c +++ b/base/version_tests.c @@ -30,7 +30,7 @@ AfterEach (version) { } -Ensure (version, compare_version) +Ensure (version, gvm_libs_versions_returns_correct_version) { assert_that (strcmp (gvm_libs_version (), GVM_LIBS_VERSION) == 0) } @@ -42,7 +42,8 @@ main (int argc, char **argv) suite = create_test_suite (); - add_test_with_context (suite, version, compare_version); + add_test_with_context (suite, version, + gvm_libs_versions_returns_correct_version); if (argc > 1) return run_single_test (suite, argv[1], create_text_reporter ());