From 57f61eb60da49aaeac4a426508a2242548578c2a Mon Sep 17 00:00:00 2001 From: rim Date: Thu, 25 Apr 2024 06:22:48 +0300 Subject: [PATCH] Allow tests run on every commit and miltiple fixes. - remove unused tpt_timer_add(); - fix build compilation on Linux for md5, sha1, sha2, gost3411-2012, base64, ecdsa; - remove TP_F_EDGE and TP_F_EXCLUSIVE since it is not supported outside Linux; - add guthub workflows to build and run tests; - add build+test badges in readme; --- .github/workflows/build-macos-latest.yml | 75 +++++++++++++++++++++ .github/workflows/build-ubuntu-latest.yml | 63 ++++++++++++++++++ CMakeLists.txt | 25 ++++++- include/crypto/dsa/ecdsa.h | 7 +- include/crypto/hash/gost3411-2012.h | 23 ++++--- include/crypto/hash/md5.h | 6 +- include/crypto/hash/sha1.h | 21 +++--- include/crypto/hash/sha2.h | 15 +++-- include/math/big_num.h | 5 +- include/threadpool/threadpool.h | 15 ++--- include/utils/base64.h | 7 +- lib.project | 11 ++- readme.md | 7 +- src/threadpool/threadpool.c | 30 +++------ src/utils/info.c | 2 +- tests/CMakeLists.txt | 44 ++++++++++++ tests/base64/main.c | 12 +--- tests/ecdsa/main.c | 13 +--- tests/hash/main.c | 17 +---- tests/threadpool/main.c | 81 +++++++++++++---------- tests/threadpool/test-threadpool.project | 4 +- 21 files changed, 343 insertions(+), 140 deletions(-) create mode 100644 .github/workflows/build-macos-latest.yml create mode 100644 .github/workflows/build-ubuntu-latest.yml create mode 100644 tests/CMakeLists.txt diff --git a/.github/workflows/build-macos-latest.yml b/.github/workflows/build-macos-latest.yml new file mode 100644 index 0000000..5d7c3c2 --- /dev/null +++ b/.github/workflows/build-macos-latest.yml @@ -0,0 +1,75 @@ +name: build-macos-latest + +on: [push, pull_request] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: [Release, Debug] + + +jobs: + build: + # The CMake configure and build commands are platform agnostic and should work equally + # well on Windows or Mac. You can convert this to a matrix build if you need + # cross-platform coverage. + # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + runs-on: macos-latest + strategy: + matrix: + compiler: [gcc, clang] + include: + - compiler: gcc + cc: gcc + cxx: g++ + - compiler: clang + cc: clang + cxx: clang++ + steps: + - uses: actions/checkout@v2 + with: + submodules: 'recursive' + + - name: Install libraries + run: | + checkPkgAndInstall() + { + while [ $# -ne 0 ] + do + rm -f '/usr/local/bin/2to3' + if brew ls --versions $1 ; then + brew upgrade $1 + else + brew install $1 + fi + shift + done + } + checkPkgAndInstall cunit + + - name: Create Build Environment + # Some projects don't allow in-source building, so create a separate build directory + # We'll use this as our working directory for all subsequent commands + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Configure CMake + # Use a bash shell so we can use the same syntax for environment variable + # access regardless of the host operating system + shell: bash + working-directory: ${{github.workspace}}/build + # Note the current convention is to use the -S and -B options here to specify source + # and build directories, but this is only available with CMake 3.13 and higher. + # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DTESTS=1 + + - name: Build + working-directory: ${{github.workspace}}/build + shell: bash + # Execute the build. You can specify a specific target with "--target " + run: cmake --build . --config $BUILD_TYPE -j 16 + + - name: Test + working-directory: ${{github.workspace}}/build + shell: bash + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest -C $BUILD_TYPE -j 16 --output-on-failure --test-dir tests diff --git a/.github/workflows/build-ubuntu-latest.yml b/.github/workflows/build-ubuntu-latest.yml new file mode 100644 index 0000000..f33b96b --- /dev/null +++ b/.github/workflows/build-ubuntu-latest.yml @@ -0,0 +1,63 @@ +name: build-ubuntu-latest + +on: [push, pull_request] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: [Release, Debug] + + +jobs: + build: + # The CMake configure and build commands are platform agnostic and should work equally + # well on Windows or Mac. You can convert this to a matrix build if you need + # cross-platform coverage. + # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + runs-on: ubuntu-latest + strategy: + matrix: + compiler: [gcc, clang] + include: + - compiler: gcc + cc: gcc + cxx: g++ + - compiler: clang + cc: clang + cxx: clang++ + steps: + - uses: actions/checkout@v2 + with: + submodules: 'recursive' + + - name: Install libraries + run: | + sudo apt-get update + sudo apt-get install cmake libcunit1 libcunit1-doc libcunit1-dev + + - name: Create Build Environment + # Some projects don't allow in-source building, so create a separate build directory + # We'll use this as our working directory for all subsequent commands + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Configure CMake + # Use a bash shell so we can use the same syntax for environment variable + # access regardless of the host operating system + shell: bash + working-directory: ${{github.workspace}}/build + # Note the current convention is to use the -S and -B options here to specify source + # and build directories, but this is only available with CMake 3.13 and higher. + # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DTESTS=1 + + - name: Build + working-directory: ${{github.workspace}}/build + shell: bash + # Execute the build. You can specify a specific target with "--target " + run: cmake --build . --config $BUILD_TYPE -j 16 + + - name: Test + working-directory: ${{github.workspace}}/build + shell: bash + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest -C $BUILD_TYPE -j 16 --output-on-failure --test-dir tests diff --git a/CMakeLists.txt b/CMakeLists.txt index 80627ff..862813c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ # ############################# INITIAL SECTION ########################## -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.20) ############################# OPTIONS SECTION ########################## @@ -13,8 +13,20 @@ cmake_minimum_required(VERSION 3.10) include(CheckIncludeFiles) include(CheckFunctionExists) include(CheckSymbolExists) +include(CheckCCompilerFlag) + +find_library(PTHREAD_LIBRARY pthread) +list(APPEND CMAKE_REQUIRED_LIBRARIES ${PTHREAD_LIBRARY}) ############################# MACRO SECTION ############################ +macro(try_c_flag prop flag) + # Try flag once on the C compiler + check_c_compiler_flag("-Werror ${flag}" C_FLAG_${prop}) + if (C_FLAG_${prop}) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}") + endif() +endmacro() + macro(chk_include_files incfile prop) string(TOUPPER HAVE_${prop} __tmp) check_include_files(${incfile} ${__tmp}) @@ -49,15 +61,17 @@ message(STATUS "liblcb configuring...") # Platform specific configuration. if (CMAKE_SYSTEM_NAME MATCHES "^.*BSD$|DragonFly") add_definitions(-D_BSD_SOURCE -DFREEBSD) - include_directories("/usr/local/include/") + include_directories("/usr/local/include") endif() if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") add_definitions(-D_BSD_SOURCE -DDARWIN) + include_directories("/usr/local/include") endif() if (APPLE) # For IPV6_PKTINFO. add_definitions(-D__APPLE_USE_RFC_3542) + include_directories("/usr/local/include") endif() @@ -93,13 +107,18 @@ chk_symbol_exists(sys/socket.h SOCK_CLOEXEC) chk_symbol_exists(sys/socket.h SOCK_NONBLOCK) # Disable some warnings. -try_c_flag(W_NO_UNUSED_RESULT "-Wno-unused-result") +try_c_flag(WSWITCHDEFAULT "-Wno-switch-default") +try_c_flag(WUNUSED_RESULT "-Wno-unused-result") +try_c_flag(WUNSAFE_BUFFER_USAGE "-Wno-unsafe-buffer-usage") message(STATUS "liblcb configuring done!") ################################ SUBDIRS SECTION ####################### +if (TESTS) + add_subdirectory(tests) +endif() ############################ TARGETS SECTION ########################### diff --git a/include/crypto/dsa/ecdsa.h b/include/crypto/dsa/ecdsa.h index 3e7f5bb..118d362 100644 --- a/include/crypto/dsa/ecdsa.h +++ b/include/crypto/dsa/ecdsa.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2013 - 2020 Rozhuk Ivan + * Copyright (c) 2013-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -54,10 +54,15 @@ #ifdef _WINDOWS # define EINVAL ERROR_INVALID_PARAMETER #else +# include # include # include #endif +#ifndef nitems /* SIZEOF() */ +# define nitems(__val) (sizeof(__val) / sizeof(__val[0])) +#endif + #include "math/elliptic_curve.h" diff --git a/include/crypto/hash/gost3411-2012.h b/include/crypto/hash/gost3411-2012.h index bc51a40..e18778a 100644 --- a/include/crypto/hash/gost3411-2012.h +++ b/include/crypto/hash/gost3411-2012.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2016-2023 Rozhuk Ivan + * Copyright (c) 2016-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -38,11 +38,6 @@ #define __GOST3411_2012_H__INCLUDED__ #include -#ifdef __linux__ -# include -#else -# include -#endif #include #include /* bcopy, bzero, memcpy, memmove, memset, strerror... */ #include @@ -60,6 +55,14 @@ # include /* AVX */ #endif +#ifndef __unused +# define __unused __attribute__((__unused__)) +#endif + +#ifndef nitems /* SIZEOF() */ +# define nitems(__val) (sizeof(__val) / sizeof(__val[0])) +#endif + #if defined(_MSC_VER) || defined(__INTEL_COMPILER) # define GOST3411_2012_ALIGN(__n) __declspec(align(__n)) /* DECLSPEC_ALIGN() */ #else /* GCC/clang */ @@ -1728,18 +1731,18 @@ gost3411_2012_init(const size_t bits, gost3411_2012_ctx_p ctx) { __get_cpuid_count(1, 0, &eax, &ebx, &ecx, &edx); # ifdef __SSE4_1__ ctx->use_sse |= (ecx & (((uint32_t)1) << 19)); -# elifdef __SSSE3__ +# elif defined(__SSSE3__) ctx->use_sse |= (ecx & (((uint32_t)1) << 9)); -# elifdef __SSE3__ +# elif defined(__SSE3__) ctx->use_sse |= (ecx & (((uint32_t)1) << 0)); -# elifdef __SSE2__ +# elif defined(__SSE2__) ctx->use_sse |= (edx & (((uint32_t)1) << 26)); # endif #endif #ifdef __AVX2__ __get_cpuid_count(7, 0, &eax, &ebx, &ecx, &edx); ctx->use_avx |= (ebx & (((uint32_t)1) << 5)); /* AVX2. */ -#elifdef __AVX__ +#elif defined(__AVX__) __get_cpuid_count(1, 0, &eax, &ebx, &ecx, &edx); ctx->use_avx |= (ecx & (((uint32_t)1) << 28)); /* AVX. */ #endif diff --git a/include/crypto/hash/md5.h b/include/crypto/hash/md5.h index 82b7abf..30825ff 100644 --- a/include/crypto/hash/md5.h +++ b/include/crypto/hash/md5.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2003-2023 Rozhuk Ivan + * Copyright (c) 2003-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,6 +39,10 @@ #include /* bcopy, bzero, memcpy, memmove, memset, strerror... */ #include +#ifndef nitems /* SIZEOF() */ +# define nitems(__val) (sizeof(__val) / sizeof(__val[0])) +#endif + static void *(*volatile md5_memset_volatile)(void*, int, size_t) = memset; #define md5_bzero(__mem, __size) md5_memset_volatile((__mem), 0x00, (__size)) diff --git a/include/crypto/hash/sha1.h b/include/crypto/hash/sha1.h index a383be8..86e76bc 100644 --- a/include/crypto/hash/sha1.h +++ b/include/crypto/hash/sha1.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2003-2023 Rozhuk Ivan + * Copyright (c) 2003-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -65,11 +65,6 @@ #define __SHA1_H__INCLUDED__ #include -#ifdef __linux__ -# include -#else -# include -#endif #include #include /* bcopy, bzero, memcpy, memmove, memset, strerror... */ #include @@ -84,6 +79,14 @@ # include /* AVX */ #endif +#ifndef bswap64 +# define bswap64 __builtin_bswap64 +#endif + +#ifndef nitems /* SIZEOF() */ +# define nitems(__val) (sizeof(__val) / sizeof(__val[0])) +#endif + #if defined(__SHA__) && defined(__SSSE3__) && defined(__SSE4_1__) # include # define SHA1_ENABLE_SIMD 1 @@ -185,11 +188,11 @@ sha1_init(sha1_ctx_p ctx) { ctx->use_sse = 0; # ifdef __SSE4_1__ ctx->use_sse |= (ecx & (((uint32_t)1) << 19)); -# elifdef __SSSE3__ +# elif defined(__SSSE3__) ctx->use_sse |= (ecx & (((uint32_t)1) << 9)); -# elifdef __SSE3__ +# elif defined(__SSE3__) ctx->use_sse |= (ecx & (((uint32_t)1) << 0)); -# elifdef __SSE2__ +# elif defined(__SSE2__) ctx->use_sse |= (edx & (((uint32_t)1) << 26)); # endif #endif diff --git a/include/crypto/hash/sha2.h b/include/crypto/hash/sha2.h index f114213..8af92f6 100644 --- a/include/crypto/hash/sha2.h +++ b/include/crypto/hash/sha2.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2013-2023 Rozhuk Ivan + * Copyright (c) 2013-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,11 +35,6 @@ #define __SHA2_H__INCLUDED__ #include -#ifdef __linux__ -# include -#else -# include -#endif #include #include /* bcopy, bzero, memcpy, memmove, memset, strerror... */ #include @@ -56,6 +51,14 @@ # define SHA2_ENABLE_SIMD 1 #endif +#ifndef bswap64 +# define bswap64 __builtin_bswap64 +#endif + +#ifndef nitems /* SIZEOF() */ +# define nitems(__val) (sizeof(__val) / sizeof(__val[0])) +#endif + #if defined(_MSC_VER) || defined(__INTEL_COMPILER) # define SHA2_ALIGN(__n) __declspec(align(__n)) /* DECLSPEC_ALIGN() */ #else /* GCC/clang */ diff --git a/include/math/big_num.h b/include/math/big_num.h index 5939aee..5293003 100644 --- a/include/math/big_num.h +++ b/include/math/big_num.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2013 - 2017 Rozhuk Ivan + * Copyright (c) 2013-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -52,6 +52,9 @@ # include # include /* bcopy, bzero, memcpy, memmove, memset, strnlen, strerror... */ # include /* snprintf, fprintf */ +# ifndef __unused +# define __unused __attribute__((__unused__)) +# endif #endif diff --git a/include/threadpool/threadpool.h b/include/threadpool/threadpool.h index e3cc191..07edcbc 100644 --- a/include/threadpool/threadpool.h +++ b/include/threadpool/threadpool.h @@ -53,8 +53,8 @@ typedef struct thread_pool_event_s { /* Thread pool event. */ } tp_event_t, *tp_event_p; /* Events val FreeBSD __linux__ */ -#define TP_EV_READ 0 /* EVFILT_READ EPOLLET | EPOLLIN | EPOLLRDHUP | EPOLLERR */ -#define TP_EV_WRITE 1 /* EVFILT_WRITE EPOLLET | EPOLLOUT | EPOLLERR */ +#define TP_EV_READ 0 /* EVFILT_READ EPOLLIN | EPOLLRDHUP | EPOLLERR */ +#define TP_EV_WRITE 1 /* EVFILT_WRITE EPOLLOUT | EPOLLERR */ #define TP_EV_TIMER 2 /* EVFILT_TIMER TP_EV_READ + timerfd_create */ #define TP_EV_LAST TP_EV_TIMER #define TP_EV_MASK 0x0003u /* For internal use: event set mask. */ @@ -63,9 +63,11 @@ typedef struct thread_pool_event_s { /* Thread pool event. */ /* Only for set. val FreeBSD __linux__ */ #define TP_F_ONESHOT (((uint16_t)1) << 0) /* Set: EV_ONESHOT EPOLLONESHOT */ /* Delete event after recv. */ #define TP_F_DISPATCH (((uint16_t)1) << 1) /* Set: EV_DISPATCH EPOLLONESHOT */ /* DISABLE event after recv. */ -#define TP_F_EDGE (((uint16_t)1) << 2) /* Set: EV_CLEAR EPOLLET */ /* Report only if available data changed.*/ - /* If not set will report if data/space available untill disable/delete event. */ +#if 0 /* FreeBSD does not have these features. */ +#define TP_F_EDGE (((uint16_t)1) << 2) /* Set: not yet EPOLLET */ /* Report only if available data changed.*/ + /* If not set - will report if data/space available untill disable/delete event. */ #define TP_F_EXCLUSIVE (((uint16_t)1) << 3) /* Set: not yet EPOLLEXCLUSIVE */ /* Wakeup only one epoll(). Only on tpt_ev_add. */ +#endif #define TP_F_S_MASK 0x000fu /* For internal use: flags set mask. */ /* Return only. */ #define TP_F_EOF (((uint16_t)1) << 8) /* Ret: EV_EOF EPOLLRDHUP */ @@ -179,11 +181,6 @@ int tpt_ev_enable_args(int enable, uint16_t event, uint16_t flags, uint32_t fflags, uint64_t data, tp_udata_p tp_udata); int tpt_ev_enable_args1(int enable, uint16_t event, tp_udata_p tp_udata); -/* fflags: TP_FF_T_* */ -int tpt_timer_add(tpt_p tpt, int enable, uintptr_t ident, - uint64_t timeout, uint16_t flags, uint32_t fflags, - tp_cb cb_func, tp_udata_p tp_udata); - #ifdef NOT_YET__FreeBSD__ /* Per thread queue functions. Only for kqueue! */ int tpt_ev_q_add(tpt_p tpt, uint16_t event, uint16_t flags, diff --git a/include/utils/base64.h b/include/utils/base64.h index 5cad92c..aa890c5 100644 --- a/include/utils/base64.h +++ b/include/utils/base64.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2003-2023 Rozhuk Ivan + * Copyright (c) 2003-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,9 +29,14 @@ #ifndef __BASE64_H__ #define __BASE64_H__ +#include #include #include +#ifndef nitems /* SIZEOF() */ +# define nitems(__val) (sizeof(__val) / sizeof(__val[0])) +#endif + /* * BASE64 coding: * 214 46 138 diff --git a/lib.project b/lib.project index 1a6df72..c48bbd4 100644 --- a/lib.project +++ b/lib.project @@ -1,5 +1,12 @@ + + + + + + + @@ -132,7 +139,7 @@ - + @@ -141,7 +148,7 @@ - + diff --git a/readme.md b/readme.md index f9bbf88..df764e5 100644 --- a/readme.md +++ b/readme.md @@ -1,9 +1,14 @@ # liblcb + +[![Build-macOS-latest Actions Status](https://github.com/rozhuk-im/liblcb/workflows/build-macos-latest/badge.svg)](https://github.com/rozhuk-im/liblcb/actions) +[![Build-Ubuntu-latest Actions Status](https://github.com/rozhuk-im/liblcb/workflows/build-ubuntu-latest/badge.svg)](https://github.com/rozhuk-im/liblcb/actions) + + Light Code Base Rozhuk Ivan 2011-2024 -Linked code library. +Statically linked code library. Compile and include only things that you need. diff --git a/src/threadpool/threadpool.c b/src/threadpool/threadpool.c index 62477d9..4aae160 100644 --- a/src/threadpool/threadpool.c +++ b/src/threadpool/threadpool.c @@ -218,9 +218,11 @@ tp_flags_to_kq(uint16_t flags) { if (0 != (TP_F_DISPATCH & flags)) { ret |= EV_DISPATCH; } +#ifdef TP_F_EDGE if (0 != (TP_F_EDGE & flags)) { ret |= EV_CLEAR; } +#endif return (ret); } @@ -524,13 +526,17 @@ tp_flags_to_ep(const int op, const uint16_t flags) { if (0 != ((TP_F_ONESHOT | TP_F_DISPATCH) & flags)) { ret |= EPOLLONESHOT; } +#ifdef TP_F_EDGE if (0 != (TP_F_EDGE & flags)) { ret |= EPOLLET; } +#endif +#ifdef TP_F_EXCLUSIVE if (0 != (TP_F_EXCLUSIVE & flags) && TP_CTL_ADD == op) { ret |= EPOLLEXCLUSIVE; } +#endif return (ret); } @@ -553,7 +559,8 @@ tpt_data_event_init(tpt_p tpt) { /* Add pool virtual thread to normal thread. */ mem_bzero(&ev, sizeof(tp_event_t)); ev.event = TP_EV_READ; - ev.flags = TP_F_EXCLUSIVE; + /* ev.flags = TP_F_EXCLUSIVE; Should be here, but it is not + * implemented on BSD and does not work with epoll() fd on linux. */ tpt->pvt_udata.cb_func = NULL; tpt->pvt_udata.ident = tpt->tp->pvt->io_fd; tpt->pvt_udata.tpt = tpt; @@ -1444,24 +1451,3 @@ tpt_ev_enable_args1(int enable, uint16_t event, tp_udata_p tp_udata) { ((0 != enable) ? TP_CTL_ENABLE : TP_CTL_DISABLE), event, 0, 0, 0, tp_udata)); } - - -int -tpt_timer_add(tpt_p tpt, int enable, uintptr_t ident, - uint64_t timeout, uint16_t flags, uint32_t fflags, tp_cb cb_func, - tp_udata_p tp_udata) { - int error; - - if (NULL == tp_udata) - return (EINVAL); - tp_udata->cb_func = cb_func; - tp_udata->ident = ident; - tp_udata->tpt = tpt; - error = tpt_ev_post_validate_args(TP_CTL_ADD, TP_EV_TIMER, - flags, fflags, timeout, tp_udata); - if (0 != error || - 0 != enable) - return (error); - tpt_ev_enable_args1(0, TP_EV_TIMER, tp_udata); - return (0); -} diff --git a/src/utils/info.c b/src/utils/info.c index 5a30892..874508f 100644 --- a/src/utils/info.c +++ b/src/utils/info.c @@ -40,7 +40,7 @@ #include #include #ifdef BSD /* BSD specific code. */ -#include +# include #endif /* BSD specific code. */ #include "utils/macro.h" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..9fd38a9 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,44 @@ + +############################# INITIAL SECTION ########################## +cmake_minimum_required(VERSION 3.20) + +############################# OPTIONS SECTION ########################## + + +############################# INCLUDE SECTION ########################## +find_path(CUNIT_INCLUDE_DIR NAMES CUnit/CUnit.h) +find_library(CUNIT_LIBRARY NAMES cunit libcunit cunitlib) + +############################# MACRO SECTION ############################ + + +############################# CONFIG SECTION ########################### + +message(STATUS "liblcb tests enabled!") + +################################ SUBDIRS SECTION ####################### + + +############################ TARGETS SECTION ########################### +# Testing binary. +add_executable(test_base64 base64/main.c) +add_executable(test_ecdsa ecdsa/main.c) +add_executable(test_hash hash/main.c) +add_executable(test_threadpool threadpool/main.c + ../src/threadpool/threadpool.c + ../src/threadpool/threadpool_msg_sys.c) +target_include_directories(test_threadpool PRIVATE ${CUNIT_INCLUDE_DIR}) +target_link_libraries(test_threadpool ${CUNIT_LIBRARY} ${CMAKE_REQUIRED_LIBRARIES}) + +# Enable testing functionality. +enable_testing() + +# Define tests. +add_test(NAME test_base64 COMMAND $) +add_test(NAME test_ecdsa COMMAND $) +add_test(NAME test_hash COMMAND $) +add_test(NAME test_threadpool COMMAND $) + + +##################### INSTALLATION ##################################### + diff --git a/tests/base64/main.c b/tests/base64/main.c index d0ae552..af62357 100644 --- a/tests/base64/main.c +++ b/tests/base64/main.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2023 Rozhuk Ivan + * Copyright (c) 2023-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,22 +28,12 @@ */ #include - -#ifdef __linux__ /* Linux specific code. */ -# define _GNU_SOURCE /* See feature_test_macros(7) */ -# define __USE_GNU 1 -#endif /* Linux specific code. */ - #include #include -#include /* malloc, exit */ #include /* snprintf, fprintf */ -#include /* close, write, sysconf */ #include /* bcopy, bzero, memcpy, memmove, memset, strerror... */ -#include #include -#include #define BASE64_SELF_TEST 1 diff --git a/tests/ecdsa/main.c b/tests/ecdsa/main.c index 4f75376..db1228f 100644 --- a/tests/ecdsa/main.c +++ b/tests/ecdsa/main.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2016-2023 Rozhuk Ivan + * Copyright (c) 2016-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,25 +28,14 @@ */ #include - -#ifdef __linux__ /* Linux specific code. */ -# define _GNU_SOURCE /* See feature_test_macros(7) */ -# define __USE_GNU 1 -#endif /* Linux specific code. */ - #include #include /* For getrusage. */ -#include -#include - #include #include /* malloc, exit */ #include /* snprintf, fprintf */ #include /* close, write, sysconf */ #include /* bcopy, bzero, memcpy, memmove, memset, strerror... */ -#include #include -#include #define BN_DIGIT_BIT_CNT 64 diff --git a/tests/hash/main.c b/tests/hash/main.c index 3ec3381..6a21e61 100644 --- a/tests/hash/main.c +++ b/tests/hash/main.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2016-2023 Rozhuk Ivan + * Copyright (c) 2016-2024 Rozhuk Ivan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,24 +28,13 @@ */ #include - -#ifdef __linux__ /* Linux specific code. */ -# define _GNU_SOURCE /* See feature_test_macros(7) */ -# define __USE_GNU 1 -#endif /* Linux specific code. */ - #include - #include -#include /* malloc, exit */ #include /* snprintf, fprintf */ -#include /* close, write, sysconf */ -#include /* bcopy, bzero, memcpy, memmove, memset, strerror... */ -#include -#include -#include +#undef __SSE2__ + #define MD5_SELF_TEST 1 #define SHA1_SELF_TEST 1 #define SHA2_SELF_TEST 1 diff --git a/tests/threadpool/main.c b/tests/threadpool/main.c index 6c68b4e..9e3274e 100644 --- a/tests/threadpool/main.c +++ b/tests/threadpool/main.c @@ -30,12 +30,6 @@ /* Required: devel/cunit */ #include - -#ifdef __linux__ /* Linux specific code. */ -# define _GNU_SOURCE /* See feature_test_macros(7) */ -# define __USE_GNU 1 -#endif /* Linux specific code. */ - #include #include /* For getrusage. */ #include @@ -53,6 +47,7 @@ #include #include +#include "al/os.h" #include "threadpool/threadpool.h" #include "threadpool/threadpool_msg_sys.h" @@ -67,13 +62,8 @@ #define THREADS_COUNT_MAX 16 #define TEST_EV_CNT_MAX 12 #define TEST_TIMER_ID 36434632 /* Random value. */ -#define TEST_TIMER_INTERVAL 14 -#define TEST_SLEEP_TIME_NS 500000000 -#ifdef DEBUG__ -# define TEST_SLEEP_TIME_S 5 -#else -# define TEST_SLEEP_TIME_S 0 -#endif +#define TEST_TIMER_INTERVAL 30 +#define TEST_SLEEP_TIME_MS 1000 static tp_p tp = NULL; static size_t threads_count; @@ -106,22 +96,29 @@ static void test_tpt_msg_cbsend2(void); static void test_tpt_ev_add_ex_rd_0(void); static void test_tpt_ev_add_ex_rd_oneshot(void); static void test_tpt_ev_add_ex_rd_dispatch(void); +#ifdef TP_F_EDGE static void test_tpt_ev_add_ex_rd_edge(void); +#endif static void test_tpt_ev_add_ex_rw_0(void); static void test_tpt_ev_add_ex_rw_oneshot(void); static void test_tpt_ev_add_ex_rw_dispatch(void); +#ifdef TP_F_EDGE static void test_tpt_ev_add_ex_rw_edge(void); +#endif static void test_tpt_ev_add_ex_tmr_0(void); static void test_tpt_ev_add_ex_tmr_oneshot(void); static void test_tpt_ev_add_ex_tmr_dispatch(void); +#ifdef TP_F_EDGE static void test_tpt_ev_add_ex_tmr_edge(void); +#endif int main(int argc __unused, char *argv[] __unused) { + int error = 0; CU_pSuite psuite = NULL; openlog(PACKAGE_NAME, (LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID), LOG_USER); @@ -165,15 +162,21 @@ main(int argc __unused, char *argv[] __unused) { NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_READ, 0)", test_tpt_ev_add_ex_rd_0) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_READ, TP_F_ONESHOT)", test_tpt_ev_add_ex_rd_oneshot) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_READ, TP_F_DISPATCH)", test_tpt_ev_add_ex_rd_dispatch) || +#ifdef TP_F_EDGE NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_READ, TP_F_EDGE)", test_tpt_ev_add_ex_rd_edge) || +#endif NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_WRITE, 0)", test_tpt_ev_add_ex_rw_0) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_WRITE, TP_F_ONESHOT)", test_tpt_ev_add_ex_rw_oneshot) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_WRITE, TP_F_DISPATCH)", test_tpt_ev_add_ex_rw_dispatch) || +#ifdef TP_F_EDGE NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_WRITE, TP_F_EDGE)", test_tpt_ev_add_ex_rw_edge) || +#endif NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_TIMER, 0)", test_tpt_ev_add_ex_tmr_0) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_TIMER, TP_F_ONESHOT)", test_tpt_ev_add_ex_tmr_oneshot) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_TIMER, TP_F_DISPATCH)", test_tpt_ev_add_ex_tmr_dispatch) || +#ifdef TP_F_EDGE NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_TIMER, TP_F_EDGE)", test_tpt_ev_add_ex_tmr_edge) || +#endif NULL == CU_add_test(psuite, "test of test_tp_destroy()", test_tp_destroy) || 0 || NULL == CU_add_test(psuite, "test of test_tp_init16() - threads count = 16", test_tp_init16) || @@ -195,15 +198,21 @@ main(int argc __unused, char *argv[] __unused) { NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_READ, 0)", test_tpt_ev_add_ex_rd_0) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_READ, TP_F_ONESHOT)", test_tpt_ev_add_ex_rd_oneshot) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_READ, TP_F_DISPATCH)", test_tpt_ev_add_ex_rd_dispatch) || +#ifdef TP_F_EDGE NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_READ, TP_F_EDGE)", test_tpt_ev_add_ex_rd_edge) || +#endif NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_WRITE, 0)", test_tpt_ev_add_ex_rw_0) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_WRITE, TP_F_ONESHOT)", test_tpt_ev_add_ex_rw_oneshot) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_WRITE, TP_F_DISPATCH)", test_tpt_ev_add_ex_rw_dispatch) || +#ifdef TP_F_EDGE NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_WRITE, TP_F_EDGE)", test_tpt_ev_add_ex_rw_edge) || +#endif NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_TIMER, 0)", test_tpt_ev_add_ex_tmr_0) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_TIMER, TP_F_ONESHOT)", test_tpt_ev_add_ex_tmr_oneshot) || NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_TIMER, TP_F_DISPATCH)", test_tpt_ev_add_ex_tmr_dispatch) || +#ifdef TP_F_EDGE NULL == CU_add_test(psuite, "test of tpt_ev_add_args(TP_EV_TIMER, TP_F_EDGE)", test_tpt_ev_add_ex_tmr_edge) || +#endif NULL == CU_add_test(psuite, "test of test_tp_destroy()", test_tp_destroy) || 0) { goto err_out; @@ -215,32 +224,30 @@ main(int argc __unused, char *argv[] __unused) { printf("\n"); CU_basic_show_failures(CU_get_failure_list()); printf("\n\n"); + error = CU_get_number_of_tests_failed(); /* Run all tests using the automated interface. */ - CU_automated_run_tests(); - CU_list_tests_to_file(); + //CU_automated_run_tests(); + //CU_list_tests_to_file(); + //error = CU_get_number_of_tests_failed(); err_out: /* Clean up registry and return. */ CU_cleanup_registry(); closelog(); - return ((int)CU_get_error()); + return (error); } static void -test_sleep(const time_t sec, const long nsec) { +test_sleep(const uint64_t msec) { struct timespec rqts; /* 1 sec = 1000000000 nanoseconds. */ - if (nsec < 1000000000) { - rqts.tv_sec = sec; - rqts.tv_nsec = nsec; - } else { - rqts.tv_nsec = (nsec % 1000000000l); - rqts.tv_sec = (sec + (nsec / 1000000000l)); - } + rqts.tv_sec = (msec / 1000ul); + rqts.tv_nsec = ((msec % 1000ul) * 1000000ul); + for (; 0 != nanosleep(&rqts, &rqts);) { if (EINTR != errno) break; @@ -278,13 +285,13 @@ test_tp_init1(void) { tp_settings_def(&s); threads_count = 1; s.threads_max = 1; - s.flags = (TP_S_F_BIND2CPU ); + s.flags = (TP_S_F_BIND2CPU); error = tp_create(&s, &tp); CU_ASSERT(0 == error) if (0 != error) return; /* Wait for all threads start. */ - test_sleep(1, 0); + test_sleep(1500); } static void @@ -311,7 +318,7 @@ test_tp_init16(void) { if (0 != error) return; /* Wait for all threads start. */ - test_sleep(1, 0); + test_sleep(1500); } static void @@ -400,7 +407,7 @@ test_tpt_msg_send(void) { } } /* Wait for all threads process. */ - test_sleep(TEST_SLEEP_TIME_S, TEST_SLEEP_TIME_NS); + test_sleep(TEST_SLEEP_TIME_MS); for (i = 0; i < threads_count; i ++) { if (i != thr_arr[i]) { CU_FAIL("tpt_msg_send() - not work.") @@ -434,7 +441,7 @@ test_tpt_msg_bsend_ex1(void) { return; /* Fail. */ } /* Wait for all threads process. */ - test_sleep(TEST_SLEEP_TIME_S, TEST_SLEEP_TIME_NS); + test_sleep(TEST_SLEEP_TIME_MS); if (threads_count != send_msg_cnt || 0 != error_cnt) { @@ -541,7 +548,7 @@ test_tpt_msg_cbsend1(void) { return; /* Fail. */ } /* Wait for all threads process. */ - test_sleep(TEST_SLEEP_TIME_S, TEST_SLEEP_TIME_NS); + test_sleep(TEST_SLEEP_TIME_MS); for (i = 0; i < (threads_count + 1); i ++) { if (i != thr_arr[i]) { CU_FAIL("tpt_msg_cbsend() - not work.") @@ -563,7 +570,7 @@ test_tpt_msg_cbsend2(void) { return; /* Fail. */ } /* Wait for all threads process. */ - test_sleep(TEST_SLEEP_TIME_S, TEST_SLEEP_TIME_NS); + test_sleep(TEST_SLEEP_TIME_MS); for (i = 0; i < (threads_count + 1); i ++) { if (i != thr_arr[i]) { CU_FAIL("tpt_msg_cbsend(TP_CBMSG_F_ONE_BY_ONE) - not work.") @@ -614,7 +621,7 @@ test_tpt_ev_add_ex_rd(uint16_t flags, uint8_t res, int remove_ok) { } CU_ASSERT(1 == write(pipe_fd[1], "1", 1)) /* Wait for all threads process. */ - test_sleep(TEST_SLEEP_TIME_S, TEST_SLEEP_TIME_NS); + test_sleep(TEST_SLEEP_TIME_MS); if (res != thr_arr[0]) { CU_FAIL("tpt_ev_add_args(TP_EV_READ) - not work") /* Fail. */ LOG_CONS_INFO_FMT("%i", (int)thr_arr[0]); @@ -641,11 +648,13 @@ test_tpt_ev_add_ex_rd_dispatch(void) { test_tpt_ev_add_ex_rd(TP_F_DISPATCH, 1, 1); } +#ifdef TP_F_EDGE static void test_tpt_ev_add_ex_rd_edge(void) { test_tpt_ev_add_ex_rd(TP_F_EDGE, 1, 1); } +#endif static void @@ -689,7 +698,7 @@ test_tpt_ev_add_ex_rw(uint16_t flags, uint8_t res, int remove_ok) { } CU_ASSERT(1 == write(pipe_fd[1], "1", 1)) /* Wait for all threads process. */ - test_sleep(TEST_SLEEP_TIME_S, TEST_SLEEP_TIME_NS); + test_sleep(TEST_SLEEP_TIME_MS); if (res != thr_arr[0]) { CU_FAIL("tpt_ev_add_args(TP_EV_WRITE) - not work") /* Fail. */ LOG_CONS_INFO_FMT("%i", (int)thr_arr[0]); @@ -716,11 +725,13 @@ test_tpt_ev_add_ex_rw_dispatch(void) { test_tpt_ev_add_ex_rw(TP_F_DISPATCH, 1, 1); } +#ifdef TP_F_EDGE static void test_tpt_ev_add_ex_rw_edge(void) { test_tpt_ev_add_ex_rw(TP_F_EDGE, 1, 1); } +#endif static void @@ -756,7 +767,7 @@ test_tpt_ev_add_ex_tmr(uint16_t flags, uint8_t res, int remove_ok) { return; /* Fail. */ } /* Wait for all threads process. */ - test_sleep(0, 300000000); + test_sleep((TEST_SLEEP_TIME_MS + (res * TEST_TIMER_INTERVAL * 2))); if (res != thr_arr[0]) { CU_FAIL("tpt_ev_add_args(TP_EV_TIMER) - not work") /* Fail. */ LOG_CONS_INFO_FMT("%i", (int)thr_arr[0]); @@ -782,8 +793,10 @@ test_tpt_ev_add_ex_tmr_dispatch(void) { test_tpt_ev_add_ex_tmr(TP_F_DISPATCH, 1, 1); } +#ifdef TP_F_EDGE static void test_tpt_ev_add_ex_tmr_edge(void) { test_tpt_ev_add_ex_tmr(TP_F_EDGE, TEST_EV_CNT_MAX, 1); } +#endif diff --git a/tests/threadpool/test-threadpool.project b/tests/threadpool/test-threadpool.project index 2e17bdb..18fd5bb 100644 --- a/tests/threadpool/test-threadpool.project +++ b/tests/threadpool/test-threadpool.project @@ -10,7 +10,7 @@ - + @@ -20,7 +20,7 @@ - +