- cleanup some printf's
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/ipc_shm.c | 10 +-
lib/ipcc.c | 4 +-
tests/.gitignore | 1 +
tests/Makefile.am | 8 ++-
tests/check_ipc.c | 234 +++++++++++++++++++++++++++++++++++++++++++++++++++++
tests/check_rb.c | 14 ++-
6 files changed, 258 insertions(+), 13 deletions(-)
create mode 100644 tests/check_ipc.c
diff --git a/lib/ipc_shm.c b/lib/ipc_shm.c
index b679a81..b5ccd21 100644
--- a/lib/ipc_shm.c
+++ b/lib/ipc_shm.c
@@ -86,7 +86,7 @@ static int32_t _ipcc_shm_connect_to_service_(struct qb_ipcc_connection
*c)
if (c->needs_sock_for_poll) {
qb_ipc_us_send(c->sock, &start, 1);
}
- printf("sent request to server %d\n", res);
+ qb_util_log(LOG_DEBUG, "sent request to server %d\n", res);
size = qb_rb_chunk_read(c->u.shm.response.rb, c->receive_buf,
c->max_msg_size, 100000);
@@ -96,7 +96,7 @@ static int32_t _ipcc_shm_connect_to_service_(struct qb_ipcc_connection
*c)
perror("_ipcc_shm_connect_to_service_:qb_rb_chunk_read");
goto cleanup;
}
- printf("received response from server %zd\n", size);
+ qb_util_log(LOG_DEBUG, "received response from server %zd\n", size);
msg_res = (struct mar_res_setup *)c->receive_buf;
res = msg_res->hdr.error;
if (res == 0) {
@@ -164,7 +164,7 @@ int32_t qb_ipcc_shm_connect(struct qb_ipcc_connection * c)
return 0;
}
- printf("%s:%d\n", __FILE__, __LINE__);
+ qb_util_log(LOG_DEBUG, "connection failed %d\n", res);
qb_rb_close(c->u.shm.dispatch.rb);
@@ -205,7 +205,7 @@ static void qb_ipcs_shm_destroy(struct qb_ipcs_service *s)
struct qb_list_head *iter;
struct qb_list_head *iter_next;
- printf("%s\n", __func__);
+ qb_util_log(LOG_DEBUG, "destroying server\n");
for (iter = s->connections.next;
iter != &s->connections; iter = iter_next) {
@@ -232,7 +232,7 @@ static int32_t qb_ipcs_shm_connect(struct qb_ipcs_service *s,
c->pid = init->pid;
c->service = s;
- printf("connecting to client [%d]\n", c->pid);
+ qb_util_log(LOG_DEBUG, "connecting to client [%d]\n", c->pid);
/* setup the response message queue
*/
diff --git a/lib/ipcc.c b/lib/ipcc.c
index 53e75d8..866d285 100644
--- a/lib/ipcc.c
+++ b/lib/ipcc.c
@@ -22,6 +22,7 @@
#include <mqueue.h>
#include "ipc_int.h"
+#include "util_int.h"
#include <qb/qbipcc.h>
qb_ipcc_connection_t *qb_ipcc_connect(const char *name, enum qb_ipc_type type)
@@ -126,7 +127,8 @@ ssize_t qb_ipcc_recv(struct qb_ipcc_connection * c, const void
*msg_ptr,
void qb_ipcc_disconnect(struct qb_ipcc_connection *c)
{
- printf("%s()", __func__);
+ qb_util_log(LOG_DEBUG, "%s()", __func__);
+
qb_ipcc_us_disconnect(c->sock);
if (c->funcs.disconnect) {
c->funcs.disconnect(c);
diff --git a/tests/.gitignore b/tests/.gitignore
index 1caf8ee..a8c14a8 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -1,4 +1,5 @@
check_plugin
+check_ipc
check_hash
check_rb
bmc
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bcb05aa..d42c1ab 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -53,16 +53,20 @@ rbreader_LDADD = -lrt $(top_builddir)/lib/libqb.la
if HAVE_CHECK
-TESTS = check_rb check_hash
+TESTS = check_rb check_ipc check_hash
#check_plugin
-check_PROGRAMS = check_rb check_hash
+check_PROGRAMS = check_rb check_ipc check_hash
#check_plugin
check_rb_SOURCES = check_rb.c $(top_builddir)/include/qb/qbrb.h
check_rb_CFLAGS = @CHECK_CFLAGS@ -I$(top_srcdir)/include
check_rb_LDADD = $(top_builddir)/lib/libqb.la -lrt @CHECK_LIBS@
+check_ipc_SOURCES = check_ipc.c $(top_builddir)/include/qb/qbipcc.h
$(top_builddir)/include/qb/qbipcs.h
+check_ipc_CFLAGS = @CHECK_CFLAGS@ -I$(top_srcdir)/include
+check_ipc_LDADD = $(top_builddir)/lib/libqb.la -lrt @CHECK_LIBS@
+
check_hash_SOURCES = check_hash.c $(top_builddir)/include/qb/qbhash.h
check_hash_CFLAGS = @CHECK_CFLAGS@ -I$(top_srcdir)/include
check_hash_LDADD = $(top_builddir)/lib/libqb.la -lrt @CHECK_LIBS@
diff --git a/tests/check_ipc.c b/tests/check_ipc.c
new file mode 100644
index 0000000..bc75d44
--- /dev/null
+++ b/tests/check_ipc.c
@@ -0,0 +1,234 @@
+/*
+ * Copyright (c) 2010 Red Hat, Inc.
+ *
+ * All rights reserved.
+ *
+ * Author: Angus Salkeld <asalkeld(a)redhat.com>
+ *
+ * This file is part of libqb.
+ *
+ * libqb is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * libqb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libqb. If not, see <
http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <syslog.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <signal.h>
+#include <check.h>
+#include <qb/qbipcc.h>
+#include <qb/qbipcs.h>
+#include <qb/qbpoll.h>
+
+#define MAX_MSG_SIZE (8192*16)
+static qb_ipcc_connection_t *conn;
+
+/* Test Cases
+ *
+ * 1) basic send & recv differnet message sizes
+ *
+ * 2) send message to start dispatch (confirm receipt)
+ *
+ * 3) flow control
+ *
+ * 4) authentication
+ *
+ * 5) thread safety
+ *
+ * 6) cleanup
+ *
+ * 7) service availabilty
+ *
+ * 8) multiple services
+ */
+static qb_handle_t bms_poll_handle;
+static qb_ipcs_service_pt s1;
+
+static void sigterm_handler(int32_t num)
+{
+ qb_ipcs_destroy(s1);
+ qb_poll_stop(bms_poll_handle);
+ exit(0);
+}
+
+static void s1_msg_process_fn(qb_ipcs_connection_pt c,
+ void *data, size_t size)
+{
+ //struct qb_ipc_request_header *req_pt = (struct qb_ipc_request_header *)data;
+ struct qb_ipc_response_header response;
+ ssize_t res;
+
+ response.size = sizeof(struct qb_ipc_response_header);
+ response.id = 13;
+ response.error = 0;
+ res = qb_ipcs_response_send(c, &response,
+ sizeof(response));
+ if (res < 0) {
+ perror("qb_ipcs_response_send");
+ }
+}
+
+static void ipc_log_fn(const char *file_name,
+ int32_t file_line, int32_t severity, const char *msg)
+{
+ if (severity < LOG_INFO)
+ fprintf(stderr, "%s:%d [%d] %s\n", file_name, file_line, severity, msg);
+}
+
+static void run_ipc_server(void)
+{
+ struct qb_ipcs_service_handlers sh = {
+ .connection_authenticate = NULL,
+ .connection_created = NULL,
+ .msg_process = s1_msg_process_fn,
+ .connection_destroyed = NULL,
+ };
+ signal(SIGTERM, sigterm_handler);
+ qb_util_set_log_function(ipc_log_fn);
+
+ bms_poll_handle = qb_poll_create();
+
+ s1 = qb_ipcs_create("bm1", QB_IPC_SHM, MAX_MSG_SIZE);
+ if (s1 == 0) {
+ perror("qb_ipcs_create");
+ exit(1);
+ }
+ qb_ipcs_service_handlers_set(s1, &sh);
+ qb_ipcs_run(s1, bms_poll_handle);
+ qb_poll_run(bms_poll_handle);
+}
+
+static int32_t run_function_in_new_process(void (*run_ipc_server_fn)(void))
+{
+ pid_t pid = fork ();
+
+ if (pid == -1) {
+ fprintf (stderr, "Can't fork\n");
+ return -1;
+ }
+
+ if (pid == 0) {
+ run_ipc_server_fn();
+ return 0;
+ }
+ return pid;
+}
+
+static int32_t stop_process(pid_t pid)
+{
+ kill(pid, SIGTERM);
+ waitpid(pid, NULL, 0);
+ return 0;
+}
+
+
+static char buffer[1024 * 1024];
+static int32_t bmc_send_nozc(uint32_t size)
+{
+ struct qb_ipc_request_header *req_header = (struct qb_ipc_request_header *)buffer;
+ struct qb_ipc_response_header res_header;
+ int32_t res;
+
+ req_header->id = QB_IPC_MSG_USER_START + 3;
+ req_header->size = sizeof(struct qb_ipc_request_header) + size;
+
+repeat_send:
+ res = qb_ipcc_send(conn, req_header, req_header->size);
+ if (res < 0) {
+ if (res == -EAGAIN || res == -ENOMEM) {
+ goto repeat_send;
+ } else if (res == -EINVAL || res == -EINTR) {
+ perror("qb_ipcc_send");
+ return -1;
+ } else {
+ errno = -res;
+ perror("qb_ipcc_send");
+ goto repeat_send;
+ }
+ }
+
+ repeat_recv:
+ res = qb_ipcc_recv(conn,
+ &res_header,
+ sizeof(struct qb_ipc_response_header));
+ if (res == -EAGAIN) {
+ goto repeat_recv;
+ }
+ if (res == -EINTR) {
+ return -1;
+ }
+ ck_assert_int_eq(res, sizeof(struct qb_ipc_response_header));
+ ck_assert_int_eq(res_header.id, 13);
+ ck_assert_int_eq(res_header.size, sizeof(struct qb_ipc_response_header));
+ return 0;
+}
+
+START_TEST(test_ipc_txrx_shm)
+{
+ int32_t j;
+ size_t size;
+ pid_t pid = run_function_in_new_process(run_ipc_server);
+ fail_if(pid == -1);
+
+ do {
+ conn = qb_ipcc_connect("bm1", QB_IPC_SHM);
+ if (conn == NULL) usleep(10000);
+ } while (conn == NULL);
+
+ fail_if(conn == NULL);
+ for (j = 1; j < 19; j++) {
+ size = (10 * j * j * j) + sizeof(struct qb_ipc_request_header);
+ if (size >= MAX_MSG_SIZE)
+ break;
+ if (bmc_send_nozc(size) == -1) {
+ break;
+ }
+ }
+
+ qb_ipcc_disconnect(conn);
+
+ stop_process(pid);
+}
+END_TEST
+
+static Suite *ipc_suite(void)
+{
+ TCase *tc_load;
+ Suite *s = suite_create("ipc");
+
+ tc_load = tcase_create("test01");
+ tcase_add_test(tc_load, test_ipc_txrx_shm);
+ suite_add_tcase(s, tc_load);
+
+ return s;
+}
+
+int32_t main(void)
+{
+ int32_t number_failed;
+
+ Suite *s = ipc_suite();
+ SRunner *sr = srunner_create(s);
+
+ qb_util_set_log_function(ipc_log_fn);
+
+ srunner_run_all(sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
diff --git a/tests/check_rb.c b/tests/check_rb.c
index f242eee..704714b 100644
--- a/tests/check_rb.c
+++ b/tests/check_rb.c
@@ -23,6 +23,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <syslog.h>
#include <errno.h>
#include <check.h>
#include <qb/qbrb.h>
@@ -81,8 +82,8 @@ START_TEST(test_ring_buffer1)
}
qb_rb_close(rb);
}
-
END_TEST
+
/*
* nice size (int64)
*/
@@ -126,8 +127,8 @@ START_TEST(test_ring_buffer2)
}
qb_rb_close(t);
}
-
END_TEST
+
/*
* odd size (10)
*/
@@ -157,8 +158,9 @@ START_TEST(test_ring_buffer3)
}
qb_rb_close(t);
}
+END_TEST
-END_TEST START_TEST(test_ring_buffer4)
+START_TEST(test_ring_buffer4)
{
qb_ringbuffer_t *t;
char data[] = "1234567891";
@@ -185,8 +187,9 @@ END_TEST START_TEST(test_ring_buffer4)
}
qb_rb_close(t);
}
+END_TEST
-END_TEST static Suite *rb_suite(void)
+static Suite *rb_suite(void)
{
TCase *tc_load;
Suite *s = suite_create("ringbuffer");
@@ -204,7 +207,8 @@ END_TEST static Suite *rb_suite(void)
static void libqb_log_fn(const char *file_name,
int32_t file_line, int32_t severity, const char *msg)
{
- printf("libqb: %s:%d %s\n", file_name, file_line, msg);
+ if (severity < LOG_INFO)
+ printf("libqb: %s:%d %s\n", file_name, file_line, msg);
}
int32_t main(void)
--
1.7.2.2