Skip to content

Commit

Permalink
lib UPDATE include test files
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvasko committed May 22, 2024
1 parent 9c5f344 commit 344d0b9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 20 deletions.
52 changes: 35 additions & 17 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
43 changes: 40 additions & 3 deletions lib/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 344d0b9

Please sign in to comment.