static void test_fd_passing_server(struct pomp_ctx *ctx, enum pomp_event event, struct pomp_conn *conn, const struct pomp_msg *msg, void *userdata) { static int fds[3][2] = {{-1, -1}, {-1, -1}, {-1, -1}}; int res = 0; struct test_data *data = userdata; TEST_IPC_LOG("%s : event=%d(%s) conn=%p msg=%p", __func__, event, pomp_event_str(event), conn, msg); switch (event) { case POMP_EVENT_CONNECTED: /* Create 3 pairs of pipes */ res = pipe(fds[0]); TEST_IPC_CHECK_EQUAL(data, res, 0); res = pipe(fds[1]); TEST_IPC_CHECK_EQUAL(data, res, 0); res = pipe(fds[2]); TEST_IPC_CHECK_EQUAL(data, res, 0); /* Send read sides to client */ res = pomp_conn_send(conn, 1, "%s%x%s%x%s%x", "pipe0", fds[0][0], "pipe1", fds[1][0], "pipe2", fds[2][0]); TEST_IPC_CHECK_EQUAL(data, res, 0); /* write some data on pipes */ res = (int)write(fds[0][1], "pipe0", 5); TEST_IPC_CHECK_EQUAL(data, res, 5); res = (int)write(fds[1][1], "pipe1", 5); TEST_IPC_CHECK_EQUAL(data, res, 5); res = (int)write(fds[2][1], "pipe2", 5); TEST_IPC_CHECK_EQUAL(data, res, 5); break; case POMP_EVENT_DISCONNECTED: /* Close all pipes */ res = close(fds[0][0]); TEST_IPC_CHECK_EQUAL(data, res, 0); res = close(fds[0][1]); TEST_IPC_CHECK_EQUAL(data, res, 0); res = close(fds[1][0]); TEST_IPC_CHECK_EQUAL(data, res, 0); res = close(fds[1][1]); TEST_IPC_CHECK_EQUAL(data, res, 0); res = close(fds[2][0]); TEST_IPC_CHECK_EQUAL(data, res, 0); res = close(fds[2][1]); TEST_IPC_CHECK_EQUAL(data, res, 0); data->stop = 1; break; case POMP_EVENT_MSG: break; default: break; } }
static void test_event_cb_t(struct pomp_ctx *ctx, enum pomp_event event, struct pomp_conn *conn, const struct pomp_msg *msg, void *userdata) { int fd; int res = 0; struct test_data *data = userdata; const char *eventstr = pomp_event_str(event); const struct sockaddr *addr = NULL; uint32_t addrlen = 0; const struct pomp_cred *cred = NULL; int isunix = 0; switch (event) { case POMP_EVENT_CONNECTED: data->connection++; /* Invalid get fd (NULL param) */ fd = pomp_conn_get_fd(NULL); CU_ASSERT_EQUAL(fd, -EINVAL); fd = pomp_conn_get_fd(conn); CU_ASSERT_TRUE(fd >= 0); addr = pomp_conn_get_local_addr(conn, &addrlen); CU_ASSERT_TRUE(addr != NULL); addr = pomp_conn_get_peer_addr(conn, &addrlen); CU_ASSERT_TRUE(addr != NULL); isunix = addr->sa_family == AF_UNIX; /* Invalid get addr (NULL param) */ addr = pomp_conn_get_local_addr(NULL, &addrlen); CU_ASSERT_TRUE(addr == NULL); addr = pomp_conn_get_local_addr(conn, NULL); CU_ASSERT_TRUE(addr == NULL); addr = pomp_conn_get_peer_addr(NULL, &addrlen); CU_ASSERT_TRUE(addr == NULL); addr = pomp_conn_get_peer_addr(conn, NULL); CU_ASSERT_TRUE(addr == NULL); if (!isunix) { /* Invalid get credentials (bad type or NULL param) */ cred = pomp_conn_get_peer_cred(conn); CU_ASSERT_TRUE(cred == NULL); cred = pomp_conn_get_peer_cred(NULL); CU_ASSERT_TRUE(cred == NULL); } else { cred = pomp_conn_get_peer_cred(conn); CU_ASSERT_TRUE(cred != NULL); } break; case POMP_EVENT_DISCONNECTED: data->disconnection++; break; case POMP_EVENT_MSG: data->msg++; if (pomp_msg_get_id(msg) == 1) { res = pomp_conn_send(conn, 2, "%s", eventstr); CU_ASSERT_EQUAL(res, 0); } if (data->isdgram) { res = pomp_conn_disconnect(conn); CU_ASSERT_EQUAL(res, -ENOTCONN); /* Internal function invalid arguments checks */ res = pomp_conn_send_msg_to(NULL, msg, NULL, 0); CU_ASSERT_EQUAL(res, -EINVAL); res = pomp_conn_send_msg_to(conn, NULL, NULL, 0); CU_ASSERT_EQUAL(res, -EINVAL); } else { /* Internal function invalid arguments checks */ res = pomp_conn_send_msg(NULL, msg); CU_ASSERT_EQUAL(res, -EINVAL); res = pomp_conn_send_msg(conn, NULL); CU_ASSERT_EQUAL(res, -EINVAL); } /* Internal function invalid arguments checks */ res = pomp_ctx_notify_event(NULL, POMP_EVENT_CONNECTED, conn); CU_ASSERT_EQUAL(res, -EINVAL); res = pomp_ctx_notify_event(ctx, POMP_EVENT_MSG, conn); CU_ASSERT_EQUAL(res, -EINVAL); res = pomp_ctx_notify_event(ctx, POMP_EVENT_CONNECTED, NULL); CU_ASSERT_EQUAL(res, -EINVAL); res = pomp_ctx_notify_msg(NULL, conn, msg); CU_ASSERT_EQUAL(res, -EINVAL); res = pomp_ctx_notify_msg(ctx, NULL, msg); CU_ASSERT_EQUAL(res, -EINVAL); res = pomp_ctx_notify_msg(ctx, conn, NULL); CU_ASSERT_EQUAL(res, -EINVAL); break; default: CU_ASSERT_TRUE_FATAL(0); break; } }
static void test_fd_passing_client(struct pomp_ctx *ctx, enum pomp_event event, struct pomp_conn *conn, const struct pomp_msg *msg, void *userdata) { int res = 0; int fds[3] = {-1, -1, -1}; char *str0 = NULL, *str1 = NULL, *str2 = NULL; char buf[32] = ""; struct test_data *data = userdata; TEST_IPC_LOG("%s : event=%d(%s) conn=%p msg=%p", __func__, event, pomp_event_str(event), conn, msg); switch (event) { case POMP_EVENT_CONNECTED: break; case POMP_EVENT_DISCONNECTED: data->stop = 1; break; case POMP_EVENT_MSG: if (pomp_msg_get_id(msg) == 1) { res = pomp_msg_read(msg, "%ms%x%ms%x%ms%x", &str0, &fds[0], &str1, &fds[1], &str2, &fds[2]); TEST_IPC_CHECK_EQUAL(data, res, 0); memset(buf, 0, sizeof(buf)); res = (int)read(fds[0], buf, sizeof(buf) - 1); TEST_IPC_CHECK_EQUAL(data, res, 5); TEST_IPC_CHECK_STRING_EQUAL(data, str0, "pipe0"); TEST_IPC_CHECK_STRING_EQUAL(data, buf, "pipe0"); memset(buf, 0, sizeof(buf)); res = (int)read(fds[1], buf, sizeof(buf) - 1); TEST_IPC_CHECK_EQUAL(data, res, 5); TEST_IPC_CHECK_STRING_EQUAL(data, str1, "pipe1"); TEST_IPC_CHECK_STRING_EQUAL(data, buf, "pipe1"); memset(buf, 0, sizeof(buf)); res = (int)read(fds[2], buf, sizeof(buf) - 1); TEST_IPC_CHECK_EQUAL(data, res, 5); TEST_IPC_CHECK_STRING_EQUAL(data, str2, "pipe2"); TEST_IPC_CHECK_STRING_EQUAL(data, buf, "pipe2"); free(str0); free(str1); free(str2); pomp_conn_disconnect(conn); } break; default: break; } }