Obsolete
/
libipc-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
libipc-old/tests/func_04_empty_message.c

59 lines
1.3 KiB
C
Raw Normal View History

2019-07-27 15:48:56 +02:00
#include "../src/ipc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SERVICE_NAME "pong"
#define SECURE_MALLOC(p, s, wat) p = malloc (s); if (p == NULL) { wat; }
// test the behavior of the server when the client never read its messages
2020-07-04 11:01:24 +02:00
void send_message (struct ipc_ctx *ctx)
2019-07-27 15:48:56 +02:00
{
SECURE_DECLARATION (struct ipc_message, m);
SECURE_MALLOC (m.payload, 1, exit(EXIT_FAILURE));
memcpy (m.payload, "", 0);
m.type = MSG_TYPE_DATA;
m.user_type = 42;
m.length = 0;
2020-07-04 11:01:24 +02:00
m.fd = ctx->pollfd[0].fd;
2019-07-27 15:48:56 +02:00
2020-07-04 11:01:24 +02:00
// ipc_write_fd = write now, without waiting the fd to become available
ipc_write_fd (ctx->pollfd[0].fd, &m);
2019-07-27 15:48:56 +02:00
ipc_message_empty (&m);
}
2020-07-04 11:01:24 +02:00
void read_message (struct ipc_ctx *ctx)
2019-07-27 15:48:56 +02:00
{
SECURE_DECLARATION (struct ipc_message, m);
2020-07-04 11:01:24 +02:00
ipc_read (ctx, 0 /* there is only one valid index */, &m);
2019-07-27 15:48:56 +02:00
if (m.length > 0) {
printf ("received message: %*.s\n", m.length, m.payload);
}
else {
printf ("received empty message as intended : %d bytes\n", m.length);
if (m.payload == NULL) {
printf ("message payload is NULL\n");
}
}
free (m.payload);
}
2020-07-04 11:01:24 +02:00
int main(void)
2019-07-27 15:48:56 +02:00
{
2020-07-04 11:01:24 +02:00
SECURE_DECLARATION(struct ipc_ctx, ctx);
2020-07-04 11:01:24 +02:00
TEST_IPC_Q(ipc_connection (&ctx, SERVICE_NAME), EXIT_FAILURE);
2019-07-27 15:48:56 +02:00
2020-07-04 11:01:24 +02:00
send_message (&ctx);
read_message (&ctx);
2020-07-04 11:01:24 +02:00
TEST_IPC_Q(ipc_close_all (&ctx), EXIT_FAILURE);
2019-07-27 15:48:56 +02:00
return EXIT_SUCCESS;
}