From 344d0b91d1574eb20d889989b1ca96421bbe5e67 Mon Sep 17 00:00:00 2001 From: Michal Vasko Date: Wed, 22 May 2024 12:16:44 +0200 Subject: [PATCH] lib UPDATE include test files --- lib/CMakeLists.txt | 52 +++++++++++++++++++++++++++++++--------------- lib/generate.sh | 43 +++++++++++++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 20 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index abfcfe3a..d70b412b 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -28,23 +28,41 @@ int np2_server(int argc, char *argv[]);") endif() if(NETOPEER2_LIB_TESTS) + # include expected test dir and test files there + set(NETOPEER2_TESTS_FUNC "\ +/** + * @brief Directory where to put test_files for the tests to work. + */ +#define NP2_TEST_FILE_DIR \"${CMAKE_CURRENT_SOURCE_DIR}/modules\" +") + set(NETOPEER2_TESTS_FUNC "${NETOPEER2_TESTS_FUNC} +/** + * @brief YANG and data files required by tests. + */ +extern struct np2_file { + const char *file; + const char *data; + int len; +} test_files[]; +") + # include declarations of functions that need to be implemented - set(NETOPEER2_TESTS_FUNC -"/** - * @brief Start netopeer2-server for all the test clients. - * - * After the server is started, this function MUST return meaning a child - * process or something similar needs to be created. - * - * Function implementation NEEDS TO BE PROVIDED. - * - * @param[in] pidfile_path Path to the server PID file. - * @param[in] sock_path Path ot the server UNIX socket the clients can connect to. - * @param[in] server_dir Directory for the server to use for its files. - * @param[in] extdata_path Path to the server extension data file. - * @return 0 on success. - * @return non-zero on error. - */ + set(NETOPEER2_TESTS_FUNC "${NETOPEER2_TESTS_FUNC} +/** + * @brief Start netopeer2-server for all the test clients. + * + * After the server is started, this function MUST return meaning a child + * process or something similar needs to be created. + * + * Function implementation NEEDS TO BE PROVIDED. + * + * @param[in] pidfile_path Path to the server PID file. + * @param[in] sock_path Path ot the server UNIX socket the clients can connect to. + * @param[in] server_dir Directory for the server to use for its files. + * @param[in] extdata_path Path to the server extension data file. + * @return 0 on success. + * @return non-zero on error. + */ int np2_server_test_start(const char *pidfile_path, const char *sock_path, const char *server_dir, const char *extdata_path); @@ -86,7 +104,7 @@ int np2_${TEST}(int argc, char *argv[]);") target_link_libraries(netopeer2_lib_test ${CMOCKA_LIBRARIES}) endif() -configure_file(${PROJECT_SOURCE_DIR}/netopeer2.h.in ${PROJECT_BINARY_DIR}/include/netopeer2.h ESCAPE_QUOTES @ONLY) +configure_file(${PROJECT_SOURCE_DIR}/netopeer2.h.in ${PROJECT_BINARY_DIR}/include/netopeer2.h @ONLY) # generate YANG header files add_custom_command(OUTPUT np2_sr_yang.h diff --git a/lib/generate.sh b/lib/generate.sh index b3b8bb86..f6a34893 100755 --- a/lib/generate.sh +++ b/lib/generate.sh @@ -8,11 +8,13 @@ fi # start the YANG array MAIN_YANG_ARRAY=" -struct { +struct np2_file { const char *file; const char *data; int len; -} yang_files[] = { +}; + +struct np2_file yang_files[] = { " # generate headers from all the YANG modules @@ -49,7 +51,42 @@ for YANG_PATH in ${NP2_MODDIR}/*.yang ${LN2_MODDIR}/*.yang; do done # end the YANG array -MAIN_YANG_ARRAY="${MAIN_YANG_ARRAY} {.file = NULL, .data = NULL}\n};\n\n" +MAIN_YANG_ARRAY="${MAIN_YANG_ARRAY} {.file = NULL, .data = NULL, .len = 0}\n};\n\n" + + +# generate headers from all the test files +MAIN_YANG_ARRAY="${MAIN_YANG_ARRAY}struct np2_file test_files[] = { +" + +for FILE_PATH in ${NP2_MODULE_DIR}/../tests/modules/*; do + # get file name + FILE="$(basename "${FILE_PATH}")" + + # generate HEX + HEX=$(echo "$(cat "${FILE_PATH}")" | xxd -i -c1) + LENGTH=$((${#HEX}/8)) + + # generate array name + ARRAY_NAME="$(echo "${FILE}" | tr -- "-@." "_")" + + # generate header file name without the revision + HEADER_FILE="${ARRAY_NAME}.h" + + # print into a C header file + echo -e "const char ${ARRAY_NAME}[] = {\n$HEX\n};\nconst int ${ARRAY_NAME}_l = ${LENGTH};" > "${BINDIR}/${HEADER_FILE}" + + # do not include duplicate files + if [[ ! "$MAIN_INCLUDE_LINES" =~ "\"$HEADER_FILE\"" ]]; then + # build all the include lines in the main header + MAIN_INCLUDE_LINES="${MAIN_INCLUDE_LINES}#include \"${HEADER_FILE}\"\n" + fi + + # build the array of modules + MAIN_YANG_ARRAY="${MAIN_YANG_ARRAY} {.file = \"${FILE}\", .data = ${ARRAY_NAME}, .len = ${ARRAY_NAME}_l},\n" +done + +# end the test array +MAIN_YANG_ARRAY="${MAIN_YANG_ARRAY} {.file = NULL, .data = NULL, .len = 0}\n};\n\n" # import module arrays