From e6edfd0e43e517ccef6f1417c92e767fd2637c89 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Fri, 4 Feb 2022 00:53:22 +0100 Subject: [PATCH] Fixing some test functions: no leaks when ending these programs. --- tests/func_01_connection_establishment.c | 1 + tests/func_01_connection_establishmentd.c | 1 + .../func_03_multiple-communications-client.c | 4 ++- .../func_03_multiple-communications-server.c | 31 ++++++++++++++----- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/tests/func_01_connection_establishment.c b/tests/func_01_connection_establishment.c index 5f57b92..454bdf5 100644 --- a/tests/func_01_connection_establishment.c +++ b/tests/func_01_connection_establishment.c @@ -32,6 +32,7 @@ int main(int argc, char * argv[]) printf ("error: %s\n", ipc_errors_get(ret.error_code)); return EXIT_FAILURE; } + ipc_ctx_free (&ctx); return EXIT_SUCCESS; } diff --git a/tests/func_01_connection_establishmentd.c b/tests/func_01_connection_establishmentd.c index 3ad7ff1..7717e6d 100644 --- a/tests/func_01_connection_establishmentd.c +++ b/tests/func_01_connection_establishmentd.c @@ -11,6 +11,7 @@ int main(int argc, char * argv[]) argc = (int) argc; argv = (char **) argv; + SECURE_DECLARATION(struct ipc_ctx, ctx); int timer = 10000; // 10 seconds timer diff --git a/tests/func_03_multiple-communications-client.c b/tests/func_03_multiple-communications-client.c index 422d9f9..e728c4e 100644 --- a/tests/func_03_multiple-communications-client.c +++ b/tests/func_03_multiple-communications-client.c @@ -58,7 +58,7 @@ void read_message (struct ipc_ctx *ctx) SECURE_DECLARATION (struct ipc_message, m); TEST_IPC_Q(ipc_read (ctx, 0 /* only one server here */, &m), EXIT_FAILURE); - printf ("received message: %*.s\n", m.length, m.payload); + printf ("received message: %.*s\n", m.length, m.payload); free (m.payload); #endif } @@ -76,11 +76,13 @@ int main(void) read_message (&ctx1); TEST_IPC_Q (ipc_close_all (&ctx1), EXIT_FAILURE); + ipc_ctx_free (&ctx1); send_message (&ctx2); read_message (&ctx2); TEST_IPC_Q (ipc_close_all (&ctx2), EXIT_FAILURE); + ipc_ctx_free (&ctx2); return EXIT_SUCCESS; } diff --git a/tests/func_03_multiple-communications-server.c b/tests/func_03_multiple-communications-server.c index 688d839..607dec2 100644 --- a/tests/func_03_multiple-communications-server.c +++ b/tests/func_03_multiple-communications-server.c @@ -6,6 +6,20 @@ #include #define SERVICE_NAME "pong" +struct ipc_ctx *pctx = NULL; + +void exit_program(int signal) +{ + printf("Quitting, signal: %d\n", signal); + + // the application will shut down, and close the service + TEST_IPC_Q(ipc_close_all (pctx), EXIT_FAILURE); + + // free remaining ctx + ipc_ctx_free (pctx); + + exit(EXIT_SUCCESS); +} int main_loop(int argc, char * argv[]) { @@ -13,6 +27,7 @@ int main_loop(int argc, char * argv[]) argv = argv; SECURE_DECLARATION (struct ipc_ctx, ctx); + pctx = &ctx; int timer = 10000; // in ms printf ("func 03 - server init...\n"); @@ -41,8 +56,12 @@ int main_loop(int argc, char * argv[]) }; break; case IPC_EVENT_TYPE_MESSAGE : { - printf ("received message: %s\n", ((struct ipc_message*) event.m)->payload); - ipc_write (&ctx, (struct ipc_message*) event.m); + struct ipc_message *m = (struct ipc_message*) event.m; + printf ("received message (%d bytes): %.*s\n" + , m->length + , m->length + , m->payload); + ipc_write (&ctx, m); } break; case IPC_EVENT_TYPE_TX : { @@ -68,21 +87,17 @@ int main_loop(int argc, char * argv[]) return 0; } -void exit_program(int signal) -{ - printf("Quitting, signal: %d\n", signal); - exit(EXIT_SUCCESS); -} - int main(int argc, char * argv[]) { + // In case we want to quit the program, do it cleanly. signal (SIGHUP, exit_program); signal (SIGALRM, exit_program); signal (SIGUSR1, exit_program); signal (SIGUSR2, exit_program); signal (SIGTERM, exit_program); signal (SIGINT, exit_program); + main_loop (argc, argv); return EXIT_SUCCESS; }