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, 5, exit(EXIT_FAILURE));
|
|
|
|
memcpy (m.payload, "salut", 5);
|
|
|
|
m.type = MSG_TYPE_DATA;
|
|
|
|
m.user_type = 42;
|
|
|
|
m.length = 5;
|
|
|
|
|
2020-07-04 11:01:24 +02:00
|
|
|
ipc_write_fd (ctx->pollfd[0].fd /* only one connection */, &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
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
SECURE_DECLARATION(struct ipc_event, event);
|
2020-07-04 11:01:24 +02:00
|
|
|
SECURE_DECLARATION (struct ipc_message, m);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-07-04 11:01:24 +02:00
|
|
|
int timer = 10000;
|
2020-01-01 12:11:34 +01:00
|
|
|
|
2020-07-04 11:01:24 +02:00
|
|
|
// ctx, index, message
|
|
|
|
TEST_IPC_Q(ipc_read (ctx, 0 /* only one server here */, &m), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-07-06 08:43:06 +02:00
|
|
|
ipc_wait_event (ctx, &event, &timer);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
case IPC_EVENT_TYPE_MESSAGE : {
|
|
|
|
printf ("received message: %*.s\n", m.length, ((struct ipc_message*) event.m)->payload);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_CONNECTION :
|
|
|
|
case IPC_EVENT_TYPE_DISCONNECTION :
|
|
|
|
case IPC_EVENT_TYPE_NOT_SET :
|
|
|
|
case IPC_EVENT_TYPE_ERROR :
|
|
|
|
case IPC_EVENT_TYPE_EXTRA_SOCKET :
|
|
|
|
default :
|
|
|
|
printf ("not ok - should not happen\n");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-04 11:01:24 +02:00
|
|
|
ipc_ctx_free (ctx);
|
2019-07-27 15:48:56 +02:00
|
|
|
#else
|
|
|
|
SECURE_DECLARATION (struct ipc_message, m);
|
|
|
|
|
2020-07-04 11:01:24 +02:00
|
|
|
TEST_IPC_Q(ipc_read (ctx, 0 /* only one server here */, &m), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
printf ("received message: %*.s\n", m.length, m.payload);
|
|
|
|
free (m.payload);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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, ctx1);
|
|
|
|
SECURE_DECLARATION(struct ipc_ctx, ctx2);
|
2019-07-27 15:48:56 +02:00
|
|
|
SECURE_DECLARATION(struct ipc_event, event);
|
|
|
|
|
2020-07-04 11:01:24 +02:00
|
|
|
TEST_IPC_Q (ipc_connection (&ctx1, SERVICE_NAME), EXIT_FAILURE);
|
|
|
|
TEST_IPC_Q (ipc_connection (&ctx2, SERVICE_NAME), EXIT_FAILURE);
|
2020-01-01 12:11:34 +01:00
|
|
|
|
2020-07-04 11:01:24 +02:00
|
|
|
send_message (&ctx1);
|
|
|
|
read_message (&ctx1);
|
2020-01-01 12:11:34 +01:00
|
|
|
|
2020-07-04 11:01:24 +02:00
|
|
|
TEST_IPC_Q (ipc_close_all (&ctx1), EXIT_FAILURE);
|
2020-01-01 12:11:34 +01:00
|
|
|
|
2020-07-04 11:01:24 +02:00
|
|
|
send_message (&ctx2);
|
|
|
|
read_message (&ctx2);
|
2020-01-01 12:11:34 +01:00
|
|
|
|
2020-07-04 11:01:24 +02:00
|
|
|
TEST_IPC_Q (ipc_close_all (&ctx2), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|