From: Sebastian Harl Date: Fri, 25 Apr 2014 07:07:35 +0000 (+0200) Subject: connection_test: Added a simple test for sdb_connection_accept(). X-Git-Tag: sysdb-0.1.0~81^2~2 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=0e03d0902a1b3a33f9596ce0c96e6f5a6847de57;p=sysdb.git connection_test: Added a simple test for sdb_connection_accept(). --- diff --git a/t/frontend/connection_test.c b/t/frontend/connection_test.c index 9936511..9851a86 100644 --- a/t/frontend/connection_test.c +++ b/t/frontend/connection_test.c @@ -41,7 +41,11 @@ #include #include +#include + #include +#include +#include /* * private helper functions @@ -110,10 +114,92 @@ mock_conn_truncate(sdb_conn_t *conn) ftruncate(conn->fd, 0); } /* mock_conn_truncate */ +static int +mock_unixsock_listener(char *sock_path) +{ + struct sockaddr_un sa; + char *filename; + int fd, status; + + filename = tmpnam(sock_path); + fail_unless(filename != NULL, + "INTERNAL ERROR: tmpnam() = NULL; expected: a string"); + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + fail_unless(fd >= 0, + "INTERNAL ERROR: socket() = %d; expected: >=0", fd); + + memset(&sa, 0, sizeof(sa)); + sa.sun_family = AF_UNIX; + strncpy(sa.sun_path, filename, sizeof(sa.sun_path)); + + status = bind(fd, (struct sockaddr *)&sa, sizeof(sa)); + fail_unless(status == 0, + "INTERNAL ERROR: bind() = %d; expected: 0", status); + status = listen(fd, 32); + fail_unless(status == 0, + "INTERNAL ERROR: listen() = %d; expected: 0", status); + + return fd; +} /* mock_unixsock */ + +static void * +mock_client(void *arg) +{ + char *socket_path = arg; + + struct sockaddr_un sa; + int fd, check; + + fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0); + fail_unless(fd >= 0, + "INTERNAL ERROR: socket() = %d; expected: >= 0", fd); + + memset(&sa, 0, sizeof(sa)); + sa.sun_family = AF_UNIX; + strncpy(sa.sun_path, socket_path, sizeof(sa.sun_path)); + + check = connect(fd, (struct sockaddr *)&sa, sizeof(sa)); + fail_unless(check == 0, + "INTERNAL ERROR: connect() = %d; expected: 0", check); + + close(fd); + return NULL; +} /* mock_client */ + /* * tests */ +START_TEST(test_conn_accept) +{ + char socket_path[L_tmpnam]; + int fd, check; + + sdb_conn_t *conn; + + pthread_t thr; + + conn = sdb_connection_accept(-1); + fail_unless(conn == NULL, + "sdb_connection_accept(-1) = %p; expected: NULL", conn); + + memset(&socket_path, 0, sizeof(socket_path)); + fd = mock_unixsock_listener(socket_path); + check = pthread_create(&thr, /* attr = */ NULL, mock_client, socket_path); + fail_unless(check == 0, + "INTERNAL ERROR: pthread_create() = %i; expected: 0", check); + + conn = sdb_connection_accept(fd); + fail_unless(conn != NULL, + "sdb_connection_accept(%d) = %p; expected: ", fd, conn); + + unlink(socket_path); + sdb_connection_close(conn); + pthread_join(thr, NULL); +} +END_TEST + START_TEST(test_conn_setup) { sdb_conn_t *conn = mock_conn_create(); @@ -188,6 +274,7 @@ fe_conn_suite(void) TCase *tc; tc = tcase_create("core"); + tcase_add_test(tc, test_conn_accept); tcase_add_test(tc, test_conn_setup); suite_add_tcase(s, tc);