diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/client-server.c | 212 | ||||
-rw-r--r-- | tests/protocol.c | 112 | ||||
-rw-r--r-- | tests/sender-receiver.c | 573 |
3 files changed, 455 insertions, 442 deletions
diff --git a/tests/client-server.c b/tests/client-server.c index 97f2f6d..9954b86 100644 --- a/tests/client-server.c +++ b/tests/client-server.c @@ -14,119 +14,125 @@ #include <unistd.h> #include <pthread.h> -static inline char *new_address() -{ - char *address = NULL; - (void) asprintf(&address, "icipc-test-%d-%d", getpid(), rand()); - test_ptr_notnull(address); - return address; +static inline char *new_address() { + char *address = NULL; + (void)asprintf(&address, "icipc-test-%d-%d", getpid(), rand()); + test_ptr_notnull(address); + return address; } -static bool -increment_request_handler (struct icipc_server *self, int client_fd, - const char *name, const struct spa_pod *args, void *data) -{ - int32_t val = 0; - test_bool_true (spa_pod_is_int (args)); - test_bool_true (spa_pod_get_int (args, &val) == 0); - struct spa_pod_int res = SPA_POD_INIT_Int (val + 1); - return icipc_server_reply_ok (self, client_fd, (struct spa_pod *)&res); +static bool increment_request_handler( + struct icipc_server *self, + int client_fd, + const char *name, + const struct spa_pod *args, + void *data) { + int32_t val = 0; + test_bool_true(spa_pod_is_int(args)); + test_bool_true(spa_pod_get_int(args, &val) == 0); + struct spa_pod_int res = SPA_POD_INIT_Int(val + 1); + return icipc_server_reply_ok(self, client_fd, (struct spa_pod *)&res); } -static bool -error_request_handler (struct icipc_server *self, int client_fd, - const char *name, const struct spa_pod *args, void *data) -{ - return icipc_server_reply_error (self, client_fd, "error message"); +static bool error_request_handler( + struct icipc_server *self, + int client_fd, + const char *name, + const struct spa_pod *args, + void *data) { + return icipc_server_reply_error(self, client_fd, "error message"); } -struct reply_data { - int32_t incremented; - const char *error; - int n_replies; - pthread_mutex_t mutex; - pthread_cond_t cond; -}; - -static void -wait_for_reply (struct reply_data *data, int n_replies) -{ - pthread_mutex_lock (&data->mutex); - while (data->n_replies < n_replies) - pthread_cond_wait (&data->cond, &data->mutex); - pthread_mutex_unlock (&data->mutex); +typedef struct ReplyData { + int32_t incremented; + const char *error; + int n_replies; + pthread_mutex_t mutex; + pthread_cond_t cond; +} ReplyData; + +static void wait_for_reply(ReplyData *data, int n_replies) { + pthread_mutex_lock(&data->mutex); + while (data->n_replies < n_replies) + pthread_cond_wait(&data->cond, &data->mutex); + pthread_mutex_unlock(&data->mutex); } -static void -reply_handler (struct icipc_sender *self, const uint8_t *buffer, size_t size, void *p) -{ - struct reply_data *data = p; - test_ptr_notnull(data); - - pthread_mutex_lock (&data->mutex); - - const struct spa_pod *pod = icipc_client_send_request_finish (self, buffer, size, &data->error); - if (pod) { - test_bool_true (spa_pod_is_int (pod)); - test_bool_true (spa_pod_get_int (pod, &data->incremented) == 0); - } - data->n_replies++; - pthread_cond_signal (&data->cond); - - pthread_mutex_unlock (&data->mutex); +static void reply_handler( + struct icipc_sender *self, + const uint8_t * buffer, + size_t size, + void *p) { + ReplyData *data = p; + test_ptr_notnull(data); + + pthread_mutex_lock(&data->mutex); + + const struct spa_pod *pod = + icipc_client_send_request_finish(self, buffer, size, &data->error); + if (pod) { + test_bool_true(spa_pod_is_int(pod)); + test_bool_true(spa_pod_get_int(pod, &data->incremented) == 0); + } + data->n_replies++; + pthread_cond_signal(&data->cond); + + pthread_mutex_unlock(&data->mutex); } -static void -test_icipc_server_client () -{ - char *address = new_address(); - struct icipc_server *s = icipc_server_new (address, true); - test_ptr_notnull(s); - struct icipc_client *c = icipc_client_new (address, true); - test_ptr_notnull(c); - struct reply_data data; - pthread_mutex_init (&data.mutex, NULL); - pthread_cond_init (&data.cond, NULL); - - /* add request handlers */ - test_bool_true (icipc_server_set_request_handler (s, "INCREMENT", increment_request_handler, NULL)); - test_bool_true (icipc_server_set_request_handler (s, "ERROR", error_request_handler, NULL)); - - /* send an INCREMENT request of 3, and make sure the returned value is 4 */ - data.incremented = -1; - data.error = NULL; - data.n_replies = 0; - struct spa_pod_int i = SPA_POD_INIT_Int (3); - test_bool_true (icipc_client_send_request (c, "INCREMENT", (struct spa_pod *)&i, reply_handler, &data)); - wait_for_reply (&data, 1); - test_ptr_null(data.error); - test_cmpint(data.incremented, ==, 4); - - /* send an ERROR request, and make sure the returned value is an error */ - data.error = NULL; - data.n_replies = 0; - test_bool_true (icipc_client_send_request (c, "ERROR", NULL, reply_handler, &data)); - wait_for_reply (&data, 1); - test_str_eq(data.error, "error message"); - - /* send an unhandled request, and make sure the server replies with an error */ - data.error = NULL; - data.n_replies = 0; - test_bool_true (icipc_client_send_request (c, "UNHANDLED-REQUEST", NULL, reply_handler, &data)); - wait_for_reply (&data, 1); - test_str_eq(data.error, "request handler not found"); - - /* clean up */ - pthread_cond_destroy (&data.cond); - pthread_mutex_destroy (&data.mutex); - icipc_client_free (c); - icipc_server_free (s); - free(address); +static void test_icipc_server_client() { + char *address = new_address(); + struct icipc_server *s = icipc_server_new(address, true); + test_ptr_notnull(s); + struct icipc_client *c = icipc_client_new(address, true); + test_ptr_notnull(c); + ReplyData data; + pthread_mutex_init(&data.mutex, NULL); + pthread_cond_init(&data.cond, NULL); + + /* add request handlers */ + test_bool_true(icipc_server_set_request_handler + (s, "INCREMENT", increment_request_handler, NULL)); + test_bool_true(icipc_server_set_request_handler + (s, "ERROR", error_request_handler, NULL)); + + /* send an INCREMENT request of 3, and make sure the returned value is 4 */ + data.incremented = -1; + data.error = NULL; + data.n_replies = 0; + struct spa_pod_int i = SPA_POD_INIT_Int(3); + test_bool_true(icipc_client_send_request + (c, "INCREMENT", (struct spa_pod *)&i, reply_handler, + &data)); + wait_for_reply(&data, 1); + test_ptr_null(data.error); + test_cmpint(data.incremented, ==, 4); + + /* send an ERROR request, and make sure the returned value is an error */ + data.error = NULL; + data.n_replies = 0; + test_bool_true(icipc_client_send_request + (c, "ERROR", NULL, reply_handler, &data)); + wait_for_reply(&data, 1); + test_str_eq(data.error, "error message"); + + /* send an unhandled request, and make sure the server replies with an error */ + data.error = NULL; + data.n_replies = 0; + test_bool_true(icipc_client_send_request + (c, "UNHANDLED-REQUEST", NULL, reply_handler, &data)); + wait_for_reply(&data, 1); + test_str_eq(data.error, "request handler not found"); + + /* clean up */ + pthread_cond_destroy(&data.cond); + pthread_mutex_destroy(&data.mutex); + icipc_client_free(c); + icipc_server_free(s); + free(address); } -int -main (int argc, char *argv[]) -{ - test_icipc_server_client(); - return TEST_PASS; +int main(int argc, char *argv[]) { + test_icipc_server_client(); + return TEST_PASS; } diff --git a/tests/protocol.c b/tests/protocol.c index 334c511..1d29db3 100644 --- a/tests/protocol.c +++ b/tests/protocol.c @@ -11,65 +11,69 @@ #include <spa/pod/parser.h> #include <icipc.h> -static void -test_icipc_protocol () -{ - uint8_t b[1024]; +static void test_icipc_protocol() { + uint8_t b[1024]; - /* request null value */ - { - icipc_protocol_build_request (b, sizeof(b), "name", NULL); - const char *name = NULL; - const struct spa_pod *value = NULL; - test_bool_true (icipc_protocol_parse_request (b, sizeof(b), &name, &value)); - test_str_eq (name, "name"); - test_bool_true (spa_pod_is_none (value)); - } + /* request null value */ + { + icipc_protocol_build_request(b, sizeof(b), "name", NULL); + const char *name = NULL; + const struct spa_pod *value = NULL; + test_bool_true(icipc_protocol_parse_request + (b, sizeof(b), &name, &value)); + test_str_eq(name, "name"); + test_bool_true(spa_pod_is_none(value)); + } - /* request */ - { - struct spa_pod_int i = SPA_POD_INIT_Int (8); - icipc_protocol_build_request (b, sizeof(b), "name", (struct spa_pod *)&i); - const char *name = NULL; - const struct spa_pod_int *value = NULL; - test_bool_true (icipc_protocol_parse_request (b, sizeof(b), &name, (const struct spa_pod **)&value)); - test_str_eq (name, "name"); - test_cmpint (value->value, ==, 8); - } + /* request */ + { + struct spa_pod_int i = SPA_POD_INIT_Int(8); + icipc_protocol_build_request(b, sizeof(b), "name", + (struct spa_pod *)&i); + const char *name = NULL; + const struct spa_pod_int *value = NULL; + test_bool_true(icipc_protocol_parse_request + (b, sizeof(b), &name, + (const struct spa_pod **)&value)); + test_str_eq(name, "name"); + test_cmpint(value->value, ==, 8); + } - /* reply error */ - { - icipc_protocol_build_reply_error (b, sizeof(b), "error message"); - test_bool_true (icipc_protocol_is_reply_error (b, sizeof(b))); - const char *msg = NULL; - test_bool_true (icipc_protocol_parse_reply_error (b, sizeof(b), &msg)); - test_str_eq (msg, "error message"); - } + /* reply error */ + { + icipc_protocol_build_reply_error(b, sizeof(b), "error message"); + test_bool_true(icipc_protocol_is_reply_error(b, sizeof(b))); + const char *msg = NULL; + test_bool_true(icipc_protocol_parse_reply_error + (b, sizeof(b), &msg)); + test_str_eq(msg, "error message"); + } - /* reply ok null value */ - { - icipc_protocol_build_reply_ok (b, sizeof(b), NULL); - test_bool_true (icipc_protocol_is_reply_ok (b, sizeof(b))); - const struct spa_pod *value = NULL; - test_bool_true (icipc_protocol_parse_reply_ok (b, sizeof(b), &value)); - test_ptr_notnull (value); - test_bool_true (spa_pod_is_none (value)); - } + /* reply ok null value */ + { + icipc_protocol_build_reply_ok(b, sizeof(b), NULL); + test_bool_true(icipc_protocol_is_reply_ok(b, sizeof(b))); + const struct spa_pod *value = NULL; + test_bool_true(icipc_protocol_parse_reply_ok + (b, sizeof(b), &value)); + test_ptr_notnull(value); + test_bool_true(spa_pod_is_none(value)); + } - /* reply ok */ - { - struct spa_pod_int i = SPA_POD_INIT_Int (3); - icipc_protocol_build_reply_ok (b, sizeof(b), (struct spa_pod *)&i); - test_bool_true (icipc_protocol_is_reply_ok (b, sizeof(b))); - const struct spa_pod_int *value = NULL; - test_bool_true (icipc_protocol_parse_reply_ok (b, sizeof(b), (const struct spa_pod **)&value)); - test_cmpint (value->value, ==, 3); - } + /* reply ok */ + { + struct spa_pod_int i = SPA_POD_INIT_Int(3); + icipc_protocol_build_reply_ok(b, sizeof(b), + (struct spa_pod *)&i); + test_bool_true(icipc_protocol_is_reply_ok(b, sizeof(b))); + const struct spa_pod_int *value = NULL; + test_bool_true(icipc_protocol_parse_reply_ok + (b, sizeof(b), (const struct spa_pod **)&value)); + test_cmpint(value->value, ==, 3); + } } -int -main (int argc, char *argv[]) -{ - test_icipc_protocol(); - return TEST_PASS; +int main(int argc, char *argv[]) { + test_icipc_protocol(); + return TEST_PASS; } diff --git a/tests/sender-receiver.c b/tests/sender-receiver.c index 19a989d..9858ef7 100644 --- a/tests/sender-receiver.c +++ b/tests/sender-receiver.c @@ -12,312 +12,315 @@ #include <unistd.h> #include <pthread.h> -struct event_data { - const uint8_t * expected_data; - size_t expected_size; - int connections; - int n_events; - pthread_mutex_t mutex; - pthread_cond_t cond; -}; - -static inline char *new_address() -{ - char *address = NULL; - (void) asprintf(&address, "icipc-test-%d-%d", getpid(), rand()); - test_ptr_notnull(address); - return address; +typedef struct EventData { + const uint8_t *expected_data; + size_t expected_size; + int connections; + int n_events; + pthread_mutex_t mutex; + pthread_cond_t cond; +} EventData; + +static inline char *new_address() { + char *address = NULL; + (void)asprintf(&address, "icipc-test-%d-%d", getpid(), rand()); + test_ptr_notnull(address); + return address; } -static void -wait_for_event (struct event_data *data, int n_events) -{ - pthread_mutex_lock (&data->mutex); - while (data->n_events < n_events) - pthread_cond_wait (&data->cond, &data->mutex); - pthread_mutex_unlock (&data->mutex); +static void wait_for_event(EventData *data, int n_events) { + pthread_mutex_lock(&data->mutex); + while (data->n_events < n_events) + pthread_cond_wait(&data->cond, &data->mutex); + pthread_mutex_unlock(&data->mutex); } -static void -sender_state_callback (struct icipc_receiver *self, int sender_fd, - enum icipc_receiver_sender_state sender_state, void *p) -{ - struct event_data *data = p; - test_ptr_notnull (data); - - pthread_mutex_lock (&data->mutex); - switch (sender_state) { - case ICIPC_RECEIVER_SENDER_STATE_CONNECTED: - data->connections++; - break; - case ICIPC_RECEIVER_SENDER_STATE_DISCONNECTED: - data->connections--; - break; - default: - test_fail_if_reached (); - break; - } - data->n_events++; - pthread_cond_signal (&data->cond); - pthread_mutex_unlock (&data->mutex); +static void sender_state_callback( + struct icipc_receiver *self, + int sender_fd, + enum icipc_receiver_sender_state sender_state, + void *p) { + EventData *data = p; + test_ptr_notnull(data); + + pthread_mutex_lock(&data->mutex); + switch (sender_state) { + case ICIPC_RECEIVER_SENDER_STATE_CONNECTED: + data->connections++; + break; + case ICIPC_RECEIVER_SENDER_STATE_DISCONNECTED: + data->connections--; + break; + default: + test_fail_if_reached(); + break; + } + data->n_events++; + pthread_cond_signal(&data->cond); + pthread_mutex_unlock(&data->mutex); } -static void -reply_callback (struct icipc_sender *self, const uint8_t *buffer, size_t size, void *p) -{ - struct event_data *data = p; - test_ptr_notnull (data); - test_ptr_notnull (buffer); - - pthread_mutex_lock (&data->mutex); - test_cmpint(size, ==, data->expected_size); - test_cmpint(memcmp(buffer, data->expected_data, size), ==, 0); - data->n_events++; - pthread_cond_signal (&data->cond); - pthread_mutex_unlock (&data->mutex); +static void reply_callback( + struct icipc_sender *self, + const uint8_t * buffer, + size_t size, + void *p) { + EventData *data = p; + test_ptr_notnull(data); + test_ptr_notnull(buffer); + + pthread_mutex_lock(&data->mutex); + test_cmpint(size, ==, data->expected_size); + test_cmpint(memcmp(buffer, data->expected_data, size), ==, 0); + data->n_events++; + pthread_cond_signal(&data->cond); + pthread_mutex_unlock(&data->mutex); } -static void -test_icipc_receiver_basic () -{ - char *address = new_address(); - struct icipc_receiver *r = icipc_receiver_new (address, 16, NULL, NULL, 0); - test_ptr_notnull (r); - - /* start and stop */ - test_bool_false (icipc_receiver_is_running (r)); - test_bool_true (icipc_receiver_start (r)); - test_bool_true (icipc_receiver_is_running (r)); - icipc_receiver_stop (r); - test_bool_false (icipc_receiver_is_running (r)); - - /* clean up */ - icipc_receiver_free (r); - free(address); +static void test_icipc_receiver_basic() { + char *address = new_address(); + struct icipc_receiver *r = + icipc_receiver_new(address, 16, NULL, NULL, 0); + test_ptr_notnull(r); + + /* start and stop */ + test_bool_false(icipc_receiver_is_running(r)); + test_bool_true(icipc_receiver_start(r)); + test_bool_true(icipc_receiver_is_running(r)); + icipc_receiver_stop(r); + test_bool_false(icipc_receiver_is_running(r)); + + /* clean up */ + icipc_receiver_free(r); + free(address); } -static void -test_icipc_sender_basic () -{ - char *address = new_address(); - struct icipc_sender *s = icipc_sender_new (address, 16, NULL, NULL, 0); - test_ptr_notnull (s); +static void test_icipc_sender_basic() { + char *address = new_address(); + struct icipc_sender *s = icipc_sender_new(address, 16, NULL, NULL, 0); + test_ptr_notnull(s); - /* clean up */ - icipc_sender_free (s); - free(address); + /* clean up */ + icipc_sender_free(s); + free(address); } -static void -test_icipc_sender_connect () -{ - static struct icipc_receiver_events events = { - .sender_state = sender_state_callback, - .handle_message = NULL, - }; - struct event_data data = {0}; - - pthread_mutex_init (&data.mutex, NULL); - pthread_cond_init (&data.cond, NULL); - - char *address = new_address(); - struct icipc_receiver *r = icipc_receiver_new (address, 16, &events, &data, 0); - test_ptr_notnull (r); - struct icipc_sender *s = icipc_sender_new (address, 16, NULL, NULL, 0); - test_ptr_notnull (s); - - /* start receiver */ - test_bool_true (icipc_receiver_start (r)); - - /* connect sender */ - test_bool_true (icipc_sender_connect (s)); - test_bool_true (icipc_sender_is_connected (s)); - wait_for_event (&data, 1); - test_cmpint (data.connections, ==, 1); - - /* disconnect sender */ - icipc_sender_disconnect (s); - test_bool_false (icipc_sender_is_connected (s)); - wait_for_event (&data, 2); - test_cmpint (data.connections, ==, 0); - - /* stop receiver */ - icipc_receiver_stop (r); - - /* clean up */ - pthread_cond_destroy (&data.cond); - pthread_mutex_destroy (&data.mutex); - icipc_sender_free (s); - icipc_receiver_free (r); - free(address); +static void test_icipc_sender_connect() { + static struct icipc_receiver_events events = { + .sender_state = sender_state_callback, + .handle_message = NULL, + }; + EventData data = { 0 }; + + pthread_mutex_init(&data.mutex, NULL); + pthread_cond_init(&data.cond, NULL); + + char *address = new_address(); + struct icipc_receiver *r = + icipc_receiver_new(address, 16, &events, &data, 0); + test_ptr_notnull(r); + struct icipc_sender *s = icipc_sender_new(address, 16, NULL, NULL, 0); + test_ptr_notnull(s); + + /* start receiver */ + test_bool_true(icipc_receiver_start(r)); + + /* connect sender */ + test_bool_true(icipc_sender_connect(s)); + test_bool_true(icipc_sender_is_connected(s)); + wait_for_event(&data, 1); + test_cmpint(data.connections, ==, 1); + + /* disconnect sender */ + icipc_sender_disconnect(s); + test_bool_false(icipc_sender_is_connected(s)); + wait_for_event(&data, 2); + test_cmpint(data.connections, ==, 0); + + /* stop receiver */ + icipc_receiver_stop(r); + + /* clean up */ + pthread_cond_destroy(&data.cond); + pthread_mutex_destroy(&data.mutex); + icipc_sender_free(s); + icipc_receiver_free(r); + free(address); } -static void -lost_connection_handler (struct icipc_sender *self, int receiver_fd, void *p) -{ - struct event_data *data = p; - test_ptr_notnull (data); - - pthread_mutex_lock (&data->mutex); - data->n_events++; - pthread_cond_signal (&data->cond); - pthread_mutex_unlock (&data->mutex); +static void lost_connection_handler( + struct icipc_sender *self, + int receiver_fd, + void *p) { + EventData *data = p; + test_ptr_notnull(data); + + pthread_mutex_lock(&data->mutex); + data->n_events++; + pthread_cond_signal(&data->cond); + pthread_mutex_unlock(&data->mutex); } -static void -test_icipc_sender_lost_connection () -{ - struct event_data data = {0}; - pthread_mutex_init (&data.mutex, NULL); - pthread_cond_init (&data.cond, NULL); - - char *address = new_address(); - struct icipc_receiver *r = icipc_receiver_new (address, 16, NULL, NULL, 0); - test_ptr_notnull (r); - struct icipc_sender *s = icipc_sender_new (address, 16, lost_connection_handler, &data, 0); - test_ptr_notnull (s); - - /* connect sender */ - test_bool_true (icipc_sender_connect (s)); - test_bool_true (icipc_sender_is_connected (s)); - - /* destroy receiver and make sure the lost connection handler is triggered */ - data.n_events = 0; - icipc_receiver_free (r); - wait_for_event (&data, 1); - - /* make sure the connection was lost */ - test_bool_false (icipc_sender_is_connected (s)); - - /* create a new receiver */ - struct icipc_receiver *r2 = icipc_receiver_new (address, 16, NULL, NULL, 0); - test_ptr_notnull (r2); - - /* re-connect sender with new receiver */ - test_bool_true (icipc_sender_connect (s)); - test_bool_true (icipc_sender_is_connected (s)); - - /* clean up */ - pthread_cond_destroy (&data.cond); - pthread_mutex_destroy (&data.mutex); - icipc_sender_free (s); - icipc_receiver_free (r2); - free(address); +static void test_icipc_sender_lost_connection() { + EventData data = { 0 }; + pthread_mutex_init(&data.mutex, NULL); + pthread_cond_init(&data.cond, NULL); + + char *address = new_address(); + struct icipc_receiver *r = + icipc_receiver_new(address, 16, NULL, NULL, 0); + test_ptr_notnull(r); + struct icipc_sender *s = + icipc_sender_new(address, 16, lost_connection_handler, &data, 0); + test_ptr_notnull(s); + + /* connect sender */ + test_bool_true(icipc_sender_connect(s)); + test_bool_true(icipc_sender_is_connected(s)); + + /* destroy receiver and make sure the lost connection handler is triggered */ + data.n_events = 0; + icipc_receiver_free(r); + wait_for_event(&data, 1); + + /* make sure the connection was lost */ + test_bool_false(icipc_sender_is_connected(s)); + + /* create a new receiver */ + struct icipc_receiver *r2 = + icipc_receiver_new(address, 16, NULL, NULL, 0); + test_ptr_notnull(r2); + + /* re-connect sender with new receiver */ + test_bool_true(icipc_sender_connect(s)); + test_bool_true(icipc_sender_is_connected(s)); + + /* clean up */ + pthread_cond_destroy(&data.cond); + pthread_mutex_destroy(&data.mutex); + icipc_sender_free(s); + icipc_receiver_free(r2); + free(address); } -static void -test_icipc_sender_send () -{ - char *address = new_address(); - struct icipc_receiver *r = icipc_receiver_new (address, 2, NULL, NULL, 0); - test_ptr_notnull (r); - struct icipc_sender *s = icipc_sender_new (address, 2, NULL, NULL, 0); - test_ptr_notnull (s); - struct event_data data = {0}; - pthread_mutex_init (&data.mutex, NULL); - pthread_cond_init (&data.cond, NULL); - - /* start receiver */ - test_bool_true (icipc_receiver_start (r)); - - /* connect */ - test_bool_true (icipc_sender_connect (s)); - test_bool_true (icipc_sender_is_connected (s)); - - /* send 1 byte message (should not realloc) */ - data.n_events = 0; - data.expected_data = (const uint8_t *)"h"; - data.expected_size = 1; - test_bool_true (icipc_sender_send (s, (const uint8_t *)"h", 1, reply_callback, &data)); - wait_for_event (&data, 1); - - /* send 2 bytes message (should realloc once to 4) */ - data.n_events = 0; - data.expected_data = (const uint8_t *)"hi"; - data.expected_size = 2; - test_bool_true (icipc_sender_send (s, (const uint8_t *)"hi", 2, reply_callback, &data)); - wait_for_event (&data, 1); - - /* send 3 bytes message (should not realloc) */ - data.n_events = 0; - data.expected_data = (const uint8_t *)"hii"; - data.expected_size = 3; - test_bool_true (icipc_sender_send (s, (const uint8_t *)"hii", 3, reply_callback, &data)); - wait_for_event (&data, 1); - - /* send 28 bytes message (should realloc 3 times: first to 8, then to 16 and finally to 32) */ - data.n_events = 0; - data.expected_data = (const uint8_t *)"bigger than 16 bytes message"; - data.expected_size = 28; - test_bool_true (icipc_sender_send (s, (const uint8_t *)"bigger than 16 bytes message", 28, reply_callback, &data)); - wait_for_event (&data, 1); - - /* don't allow empty messages */ - data.n_events = 0; - test_bool_false (icipc_sender_send (s, (const uint8_t *)"", 0, NULL, NULL)); - - /* stop receiver */ - icipc_receiver_stop (r); - - /* clean up */ - pthread_cond_destroy (&data.cond); - pthread_mutex_destroy (&data.mutex); - icipc_sender_free (s); - icipc_receiver_free (r); - free(address); +static void test_icipc_sender_send() { + char *address = new_address(); + struct icipc_receiver *r = + icipc_receiver_new(address, 2, NULL, NULL, 0); + test_ptr_notnull(r); + struct icipc_sender *s = icipc_sender_new(address, 2, NULL, NULL, 0); + test_ptr_notnull(s); + EventData data = { 0 }; + pthread_mutex_init(&data.mutex, NULL); + pthread_cond_init(&data.cond, NULL); + + /* start receiver */ + test_bool_true(icipc_receiver_start(r)); + + /* connect */ + test_bool_true(icipc_sender_connect(s)); + test_bool_true(icipc_sender_is_connected(s)); + + /* send 1 byte message (should not realloc) */ + data.n_events = 0; + data.expected_data = (const uint8_t *)"h"; + data.expected_size = 1; + test_bool_true(icipc_sender_send + (s, (const uint8_t *)"h", 1, reply_callback, &data)); + wait_for_event(&data, 1); + + /* send 2 bytes message (should realloc once to 4) */ + data.n_events = 0; + data.expected_data = (const uint8_t *)"hi"; + data.expected_size = 2; + test_bool_true(icipc_sender_send + (s, (const uint8_t *)"hi", 2, reply_callback, &data)); + wait_for_event(&data, 1); + + /* send 3 bytes message (should not realloc) */ + data.n_events = 0; + data.expected_data = (const uint8_t *)"hii"; + data.expected_size = 3; + test_bool_true(icipc_sender_send + (s, (const uint8_t *)"hii", 3, reply_callback, &data)); + wait_for_event(&data, 1); + + /* send 28 bytes message (should realloc 3 times: + first to 8, then to 16 and finally to 32) */ + data.n_events = 0; + data.expected_data = (const uint8_t *)"bigger than 16 bytes message"; + data.expected_size = 28; + test_bool_true(icipc_sender_send + (s, (const uint8_t *)"bigger than 16 bytes message", 28, + reply_callback, &data)); + wait_for_event(&data, 1); + + /* don't allow empty messages */ + data.n_events = 0; + test_bool_false(icipc_sender_send + (s, (const uint8_t *)"", 0, NULL, NULL)); + + /* stop receiver */ + icipc_receiver_stop(r); + + /* clean up */ + pthread_cond_destroy(&data.cond); + pthread_mutex_destroy(&data.mutex); + icipc_sender_free(s); + icipc_receiver_free(r); + free(address); } -static void -test_icipc_multiple_senders_send () -{ - char *address = new_address(); - struct icipc_receiver *r = icipc_receiver_new (address, 16, NULL, NULL, 0); - test_ptr_notnull (r); - struct icipc_sender *senders[50]; - struct event_data data; - pthread_mutex_init (&data.mutex, NULL); - pthread_cond_init (&data.cond, NULL); - data.n_events = 0; - - /* start receiver */ - test_bool_true (icipc_receiver_start (r)); - - /* create and connect 50 senders */ - for (int i = 0; i < 50; i++) { - senders[i] = icipc_sender_new (address, 16, NULL, NULL, 0); - test_ptr_notnull (senders[i]); - test_bool_true (icipc_sender_connect (senders[i])); - test_bool_true (icipc_sender_is_connected (senders[i])); - } - - /* send 50 messages (1 per sender) */ - data.n_events = 0; - data.expected_data = (const uint8_t *)"hello"; - data.expected_size = 5; - for (int i = 0; i < 50; i++) - test_bool_true (icipc_sender_send (senders[i], (const uint8_t *)"hello", 5, reply_callback, &data)); - wait_for_event (&data, 50); - - /* stop receiver */ - icipc_receiver_stop (r); - - /* clean up */ - pthread_cond_destroy (&data.cond); - pthread_mutex_destroy (&data.mutex); - for (int i = 0; i < 50; i++) - icipc_sender_free (senders[i]); - icipc_receiver_free (r); - free(address); +static void test_icipc_multiple_senders_send() { + char *address = new_address(); + struct icipc_receiver *r = + icipc_receiver_new(address, 16, NULL, NULL, 0); + test_ptr_notnull(r); + struct icipc_sender *senders[50]; + EventData data; + pthread_mutex_init(&data.mutex, NULL); + pthread_cond_init(&data.cond, NULL); + data.n_events = 0; + + /* start receiver */ + test_bool_true(icipc_receiver_start(r)); + + /* create and connect 50 senders */ + for (int i = 0; i < 50; i++) { + senders[i] = icipc_sender_new(address, 16, NULL, NULL, 0); + test_ptr_notnull(senders[i]); + test_bool_true(icipc_sender_connect(senders[i])); + test_bool_true(icipc_sender_is_connected(senders[i])); + } + + /* send 50 messages (1 per sender) */ + data.n_events = 0; + data.expected_data = (const uint8_t *)"hello"; + data.expected_size = 5; + for (int i = 0; i < 50; i++) + test_bool_true(icipc_sender_send + (senders[i], (const uint8_t *)"hello", 5, + reply_callback, &data)); + wait_for_event(&data, 50); + + /* stop receiver */ + icipc_receiver_stop(r); + + /* clean up */ + pthread_cond_destroy(&data.cond); + pthread_mutex_destroy(&data.mutex); + for (int i = 0; i < 50; i++) + icipc_sender_free(senders[i]); + icipc_receiver_free(r); + free(address); } -int -main (int argc, char *argv[]) -{ - test_icipc_receiver_basic(); - test_icipc_sender_basic(); - test_icipc_sender_connect(); - test_icipc_sender_lost_connection(); - test_icipc_sender_send(); - test_icipc_multiple_senders_send(); - return TEST_PASS; +int main(int argc, char *argv[]) { + test_icipc_receiver_basic(); + test_icipc_sender_basic(); + test_icipc_sender_connect(); + test_icipc_sender_lost_connection(); + test_icipc_sender_send(); + test_icipc_multiple_senders_send(); + return TEST_PASS; } |