From 73180fc762f015935950f697822710af3f5bd23d Mon Sep 17 00:00:00 2001 From: ManikandanC Date: Fri, 6 Oct 2017 11:37:31 +0530 Subject: Use poll in the dlt-daemon for POSIX compliance The poll system call is now used in the daemon to enable DLT use in POSIX compliant systems. Also added introduced new unregister_app macro to avoid missing of logs in startup buffer. Signed-off-by: Frederic Berat Signed-off-by: ManikandanC Signed-off-by: Saya Sugiura Signed-off-by: S. Hameed --- tests/gtest_dlt_daemon_event_handler.cpp | 345 +++++++++++++++++-------------- 1 file changed, 193 insertions(+), 152 deletions(-) (limited to 'tests') diff --git a/tests/gtest_dlt_daemon_event_handler.cpp b/tests/gtest_dlt_daemon_event_handler.cpp index b4a00e0..e55d072 100644 --- a/tests/gtest_dlt_daemon_event_handler.cpp +++ b/tests/gtest_dlt_daemon_event_handler.cpp @@ -130,13 +130,15 @@ TEST(t_dlt_daemon_handle_event, nullpointer) TEST(t_dlt_event_handler_find_connection, normal) { int fd = 10; - DltEventHandler ev; - DltConnection connections; - DltConnection *ret; - DltReceiver receiver; + DltEventHandler ev = {}; + DltConnection connections = {}; + DltConnection *ret = nullptr; + DltReceiver receiver = {}; + + receiver.fd = fd; + ev.connections = &connections; ev.connections->receiver = &receiver; - ev.connections->receiver->fd = fd; ret = dlt_event_handler_find_connection(&ev, fd); EXPECT_EQ(10, ret->receiver->fd); @@ -145,21 +147,21 @@ TEST(t_dlt_event_handler_find_connection, normal) /* Begin Method: dlt_daemon_event_handler::dlt_daemon_add_connection*/ TEST(t_dlt_daemon_add_connection, normal) { - DltEventHandler ev1; - DltConnection *head; + DltEventHandler ev1 = {}; + DltConnection *head = nullptr; + DltConnection *connections1 = nullptr; + DltReceiver receiver = {}; + ev1.connections = (DltConnection *)malloc(sizeof(DltConnection)); head = (DltConnection*)ev1.connections; memset(ev1.connections, 0, sizeof(DltConnection)); ev1.connections->next = 0; ev1.connections->type = DLT_CONNECTION_CLIENT_MSG_SERIAL; - DltConnection *connections1; connections1 = (DltConnection *)malloc(sizeof(DltConnection)); memset(connections1, 0, sizeof(DltConnection)); connections1->next = 0; connections1->type = DLT_CONNECTION_GATEWAY; - - DltReceiver receiver; connections1->receiver = &receiver; EXPECT_EQ(DLT_CONNECTION_CLIENT_MSG_SERIAL, ev1.connections->type); @@ -174,15 +176,16 @@ TEST(t_dlt_daemon_add_connection, normal) /* Begin Method: dlt_daemon_event_handler::dlt_daemon_remove_connection*/ TEST(t_dlt_daemon_remove_connection, normal) { - DltEventHandler ev1; - DltConnection *head; + DltEventHandler ev1 = {}; + DltConnection *head = nullptr; + DltConnection *connections1 = nullptr; + ev1.connections = (DltConnection *)malloc(sizeof(DltConnection)); head = (DltConnection*)ev1.connections; memset(ev1.connections, 0, sizeof(DltConnection)); ev1.connections->next = 0; ev1.connections->type = DLT_CONNECTION_CLIENT_MSG_SERIAL; - DltConnection *connections1; connections1 = (DltConnection *)malloc(sizeof(DltConnection)); memset(connections1, 0, sizeof(DltConnection)); connections1->next = 0; @@ -204,12 +207,13 @@ TEST(t_dlt_daemon_remove_connection, normal) /* Begin Method: dlt_daemon_event_handler::dlt_event_handler_cleanup_connections*/ TEST(t_dlt_event_handler_cleanup_connections, normal) { - DltEventHandler ev1; + DltEventHandler ev1 = {}; + DltReceiver receiver1 = {}; + ev1.connections = (DltConnection *)malloc(sizeof(DltConnection)); memset(ev1.connections, 0, sizeof(DltConnection)); ev1.connections->next = 0; ev1.connections->type = DLT_CONNECTION_GATEWAY; - DltReceiver receiver1; ev1.connections->receiver = &receiver1; dlt_event_handler_cleanup_connections(&ev1); @@ -220,21 +224,26 @@ TEST(t_dlt_event_handler_cleanup_connections, normal) TEST(t_dlt_connection_check_activate, normal) { int ret; - DltEventHandler evhdl; - DltConnection con; - DltReceiver receiver; + DltEventHandler evhdl = {}; + DltReceiver receiver = {}; + DltConnection con = {}; + DltFilterConfiguration current = {}; + DltMessageFilter filter = {}; + + receiver.fd = 1; + filter.current = ¤t; con.receiver = &receiver; con.status = INACTIVE; con.type = DLT_CONNECTION_CLIENT_MSG_TCP; - con.receiver->fd = 1; - evhdl.epfd = epoll_create(1); - ret = dlt_connection_check_activate(&evhdl, &con, ACTIVATE); + + EXPECT_EQ(DLT_RETURN_OK, dlt_daemon_prepare_event_handling(&evhdl)); EXPECT_EQ(DLT_RETURN_OK, ret); ret = dlt_connection_check_activate(&evhdl, &con, DEACTIVATE); EXPECT_EQ(DLT_RETURN_OK, ret); + free(evhdl.pfd); } TEST(t_dlt_connection_check_activate, nullpointer) @@ -246,29 +255,32 @@ TEST(t_dlt_connection_check_activate, nullpointer) TEST(t_dlt_event_handler_register_connection, normal) { int ret = 0; - DltDaemonLocal daemon_local; + DltDaemonLocal daemon_local = {}; int mask = 0; - DltEventHandler ev1; - ev1.connections = (DltConnection *)malloc(sizeof(DltConnection)); - memset(ev1.connections, 0, sizeof(DltConnection)); - ev1.connections->next = 0; - ev1.connections->type = DLT_CONNECTION_CLIENT_MSG_SERIAL; - DltConnection *connections1; + DltEventHandler ev1 = {}; + DltConnection *connections1 = nullptr; + DltReceiver receiver = {}; + DltFilterConfiguration current = {}; + + EXPECT_EQ(DLT_RETURN_OK, dlt_daemon_prepare_event_handling(&ev1)); + connections1 = (DltConnection *)malloc(sizeof(DltConnection)); memset(connections1, 0, sizeof(DltConnection)); connections1->next = 0; connections1->type = DLT_CONNECTION_GATEWAY; - - DltReceiver receiver; connections1->receiver = &receiver; - EXPECT_EQ(DLT_CONNECTION_CLIENT_MSG_SERIAL, ev1.connections->type); - ev1.epfd = epoll_create(1); - ret = dlt_event_handler_register_connection(&ev1, &daemon_local, connections1, mask); + daemon_local.pFilter.current = ¤t; + + ret = dlt_event_handler_register_connection(&ev1, + &daemon_local, + connections1, + mask); EXPECT_EQ(DLT_RETURN_OK, ret); - close(ev1.epfd); - free(ev1.connections); + EXPECT_EQ(DLT_CONNECTION_GATEWAY, ev1.connections->type); + + free(ev1.pfd); free(connections1); } @@ -281,34 +293,37 @@ TEST(t_dlt_event_handler_register_connection, nullpointer) TEST(t_dlt_event_handler_unregister_connection, normal) { int ret = 0; - int fd = 0; + DltDaemonLocal daemon_local = {}; int mask = 0; - DltDaemonLocal daemon_local; - DltEventHandler ev1; - ev1.connections = (DltConnection *)malloc(sizeof(DltConnection)); - memset(ev1.connections, 0, sizeof(DltConnection)); - ev1.connections->next = 0; - ev1.connections->type = DLT_CONNECTION_GATEWAY; - DltConnection *connections1; + DltEventHandler ev1 = {}; + DltConnection *connections1 = nullptr; + DltReceiver receiver = {}; + DltFilterConfiguration current = {}; + + EXPECT_EQ(DLT_RETURN_OK, dlt_daemon_prepare_event_handling(&ev1)); + connections1 = (DltConnection *)malloc(sizeof(DltConnection)); memset(connections1, 0, sizeof(DltConnection)); + connections1->next = 0; connections1->type = DLT_CONNECTION_GATEWAY; - DltReceiver receiver; connections1->receiver = &receiver; + receiver.fd = 1; - DltReceiver receiver1; - ev1.connections->receiver = &receiver1; - ev1.connections->receiver->fd = fd; - ev1.epfd = epoll_create(1); - ret = dlt_event_handler_register_connection(&ev1, &daemon_local, connections1, mask); + daemon_local.pFilter.current = ¤t; + + ret = dlt_event_handler_register_connection(&ev1, + &daemon_local, + connections1, + mask); EXPECT_EQ(DLT_RETURN_OK, ret); + EXPECT_EQ(DLT_CONNECTION_GATEWAY, ev1.connections->type); - ret = dlt_event_handler_unregister_connection(&ev1, &daemon_local, fd); + ret = dlt_event_handler_unregister_connection(&ev1, &daemon_local, 1); EXPECT_EQ(DLT_RETURN_OK, ret); - close(ev1.epfd); - free(connections1); + + free(ev1.pfd); } /* Begin Method: dlt_daemon_connections::dlt_connection_create*/ @@ -316,56 +331,59 @@ TEST(t_dlt_connection_create, normal) { int fd = 1; int ret = 0; + DltDaemonLocal daemon_local = {}; + DltFilterConfiguration current = {}; - DltDaemonLocal daemon_local; - DltConnection connections; - DltReceiver receiver; - daemon_local.pEvent.connections = &connections; - daemon_local.pEvent.connections->receiver = &receiver; - daemon_local.pEvent.epfd = epoll_create(5); + + daemon_local.pFilter.current = ¤t; + + EXPECT_EQ(DLT_RETURN_OK, + dlt_daemon_prepare_event_handling(&daemon_local.pEvent)); ret = dlt_connection_create(&daemon_local, &daemon_local.pEvent, fd, - EPOLLIN, + POLLIN, DLT_CONNECTION_CLIENT_MSG_SERIAL); EXPECT_EQ(DLT_RETURN_OK, ret); + + dlt_event_handler_unregister_connection(&daemon_local.pEvent, + &daemon_local, + fd); + + free(daemon_local.pEvent.pfd); } /* Begin Method: dlt_daemon_connections::dlt_connection_destroy*/ TEST(t_dlt_connection_destroy, normal) { - DltConnection *to_destroy; + DltConnection *to_destroy = (DltConnection *)malloc(sizeof(DltConnection)); - to_destroy = (DltConnection *)malloc(sizeof(DltConnection)); memset(to_destroy, 0, sizeof(DltConnection)); to_destroy->next = 0; to_destroy->type = DLT_CONNECTION_CLIENT_MSG_SERIAL; to_destroy->receiver = (DltReceiver *)malloc(sizeof(DltReceiver)); memset(to_destroy->receiver, 0, sizeof(DltReceiver)); + to_destroy->receiver->fd = -1; - to_destroy->receiver->buffer = NULL; + to_destroy->receiver->buffer = nullptr; + dlt_connection_destroy(to_destroy); } /* Begin Method: dlt_daemon_connections::t_dlt_connection_destroy_receiver*/ TEST(t_dlt_connection_destroy_receiver, normal) { - DltConnection *to_destroy; + DltConnection to_destroy = {}; - to_destroy = (DltConnection *)malloc(sizeof(DltConnection)); - memset(to_destroy, 0, sizeof(DltConnection)); - to_destroy->next = 0; - to_destroy->type = DLT_CONNECTION_CLIENT_MSG_SERIAL; + to_destroy.receiver = (DltReceiver *)malloc(sizeof(DltReceiver)); + memset(to_destroy.receiver, 0, sizeof(DltReceiver)); - to_destroy->receiver = (DltReceiver *)malloc(sizeof(DltReceiver)); - memset(to_destroy->receiver, 0, sizeof(DltReceiver)); - to_destroy->receiver->fd = -1; - to_destroy->receiver->buffer = NULL; + to_destroy.receiver->fd = -1; + to_destroy.receiver->buffer = nullptr; - dlt_connection_destroy_receiver(to_destroy); - free(to_destroy); + dlt_connection_destroy_receiver(&to_destroy); } /* Begin Method: dlt_daemon_connections::t_dlt_connection_get_receiver*/ @@ -373,9 +391,13 @@ TEST(t_dlt_connection_get_receiver, normal) { int fd = 10; DltReceiver *ret; - DltDaemonLocal daemon_local; - ret = dlt_connection_get_receiver(&daemon_local, DLT_CONNECTION_CLIENT_MSG_TCP, fd); + DltDaemonLocal daemon_local = {}; + + ret = dlt_connection_get_receiver(&daemon_local, + DLT_CONNECTION_CLIENT_MSG_TCP, + fd); + ASSERT_NE(ret, nullptr); EXPECT_EQ(fd, ret->fd); } @@ -384,30 +406,18 @@ TEST(t_dlt_connection_get_next, normal) { int type_mask = (DLT_CON_MASK_CLIENT_MSG_TCP | DLT_CON_MASK_CLIENT_MSG_SERIAL); - DltConnection *ret; + DltConnection *ret = nullptr; + DltConnection node = {}; + DltConnection current = {}; - DltConnection *current; - current = (DltConnection *)malloc(sizeof(DltConnection)); - memset(current, 0, sizeof(DltConnection)); - current->next = NULL; - - DltConnection *node; - node = (DltConnection *)malloc(sizeof(DltConnection)); - memset(node, 0, sizeof(DltConnection)); - node->next = NULL; - - current->next = node; - - current->type = DLT_CONNECTION_ONE_S_TIMER; - node->type = DLT_CONNECTION_CLIENT_MSG_TCP; - - ret = dlt_connection_get_next(current, type_mask); + node.type = DLT_CONNECTION_CLIENT_MSG_TCP; + current.type = DLT_CONNECTION_ONE_S_TIMER; + current.next = &node; + ret = dlt_connection_get_next(¤t, type_mask); + ASSERT_NE(ret, nullptr); EXPECT_EQ(DLT_CONNECTION_CLIENT_MSG_TCP, ret->type); EXPECT_NE(DLT_CONNECTION_ONE_S_TIMER, ret->type); - - free(current); - free(node); } /* Begin Method: dlt_daemon_connections::(t_dlt_connection_get_next*/ @@ -416,44 +426,44 @@ TEST(t_dlt_connection_get_next, abnormal) int type_mask = (DLT_CON_MASK_CLIENT_MSG_TCP | DLT_CON_MASK_CLIENT_MSG_SERIAL); DltConnection *ret; + DltConnection node = {}; + DltConnection current = {}; - DltConnection *current; - current = (DltConnection *)malloc(sizeof(DltConnection)); - memset(current, 0, sizeof(DltConnection)); - current->next = NULL; - - DltConnection *node; - node = (DltConnection *)malloc(sizeof(DltConnection)); - memset(node, 0, sizeof(DltConnection)); - node->next = NULL; - - current->next = node; - - current->type = DLT_CONNECTION_CLIENT_MSG_SERIAL; - node->type = DLT_CONNECTION_CLIENT_MSG_TCP; - - ret = dlt_connection_get_next(current, type_mask); + node.type = DLT_CONNECTION_CLIENT_MSG_TCP; + current.type = DLT_CONNECTION_CLIENT_MSG_SERIAL; + current.next = &node; + ret = dlt_connection_get_next(¤t, type_mask); + ASSERT_NE(ret, nullptr); EXPECT_NE(DLT_CONNECTION_CLIENT_MSG_TCP, ret->type); EXPECT_EQ(DLT_CONNECTION_CLIENT_MSG_SERIAL, ret->type); - - free(current); - free(node); } /* Begin Method: dlt_daemon_connections::t_dlt_connection_send*/ TEST(t_dlt_connection_send, normal_1) { int ret = 0; - DltConnection conn; - DltReceiver receiver; + DltConnection conn = {}; + DltReceiver receiver = {}; + void *data1 = nullptr; + int size1 = 0; + DltDaemonLocal daemon_local = {}; + receiver.fd = connectServer(); - EXPECT_NE(-1,receiver.fd); + EXPECT_NE(-1, receiver.fd); + conn.receiver = &receiver; conn.type = DLT_CONNECTION_CLIENT_MSG_TCP; - void *data1; - int size1 = 0; - DltDaemonLocal daemon_local; + + daemon_local.msg.databuffer = (uint8_t *)malloc(sizeof(uint8_t)); + if (daemon_local.msg.databuffer == NULL) + close(receiver.fd); + + EXPECT_NE((uint8_t *)NULL, daemon_local.msg.databuffer); + + memset(daemon_local.msg.databuffer, 1, sizeof(uint8_t)); + daemon_local.msg.datasize = sizeof(uint8_t); + data1 = daemon_local.msg.databuffer; size1 = daemon_local.msg.datasize; @@ -465,8 +475,9 @@ TEST(t_dlt_connection_send, normal_1) TEST(t_dlt_connection_send, normal_2) { int ret = 0; - DltConnection conn; - DltReceiver receiver; + DltConnection conn = {}; + DltReceiver receiver = {}; + receiver.fd = 1; conn.receiver = &receiver; conn.type = DLT_CONNECTION_CLIENT_MSG_SERIAL; @@ -479,8 +490,10 @@ TEST(t_dlt_connection_send, normal_2) TEST(t_dlt_connection_send, abnormal) { int ret = 0; - DltConnection conn; - DltReceiver receiver; + DltConnection conn = {}; + DltReceiver receiver = {}; + + receiver.fd = -1; conn.receiver = &receiver; conn.type = DLT_CONNECTION_TYPE_MAX; ret = dlt_connection_send(&conn, @@ -493,18 +506,19 @@ TEST(t_dlt_connection_send, abnormal) TEST(t_dlt_connection_send_multiple, normal_1) { int ret = 0; - void *data1, *data2; + void *data1 = nullptr; + void *data2 = nullptr; int size1 = 0; int size2 = 0; - DltDaemonLocal daemon_local; + DltConnection conn = {}; + DltReceiver receiver = {}; + DltDaemonLocal daemon_local = {}; data1 = daemon_local.msg.headerbuffer + sizeof(DltStorageHeader); size1 = daemon_local.msg.headersize - sizeof(DltStorageHeader); data2 = daemon_local.msg.databuffer; size2 = daemon_local.msg.datasize; - DltConnection conn; - DltReceiver receiver; receiver.fd = connectServer(); EXPECT_NE(-1,receiver.fd); conn.receiver = &receiver; @@ -522,18 +536,19 @@ TEST(t_dlt_connection_send_multiple, normal_1) TEST(t_dlt_connection_send_multiple, normal_2) { int ret = 0; - void *data1, *data2; + void *data1 = nullptr; + void *data2 = nullptr; int size1 = 0; int size2 = 0; - DltDaemonLocal daemon_local; + DltConnection conn = {}; + DltReceiver receiver = {}; + DltDaemonLocal daemon_local = {}; data1 = daemon_local.msg.headerbuffer + sizeof(DltStorageHeader); size1 = daemon_local.msg.headersize - sizeof(DltStorageHeader); data2 = daemon_local.msg.databuffer; size2 = daemon_local.msg.datasize; - DltConnection conn; - DltReceiver receiver; receiver.fd = connectServer(); EXPECT_NE(-1,receiver.fd); conn.receiver = &receiver; @@ -552,10 +567,11 @@ TEST(t_dlt_connection_send_multiple, normal_2) TEST(t_dlt_connection_send_multiple, nullpointer) { int ret = 0; - void *data1, *data2; + void *data1 = nullptr; + void *data2 = nullptr; int size1 = 0; int size2 = 0; - DltDaemonLocal daemon_local; + DltDaemonLocal daemon_local = {}; data1 = daemon_local.msg.headerbuffer + sizeof(DltStorageHeader); size1 = daemon_local.msg.headersize - sizeof(DltStorageHeader); @@ -573,9 +589,10 @@ TEST(t_dlt_connection_send_multiple, nullpointer) int connectServer(void) { - int sockfd, portno; - struct sockaddr_in serv_addr; - struct hostent *server; + int sockfd = 0, portno = 0; + struct sockaddr_in serv_addr = {}; + struct hostent *server = nullptr; + portno = 8080; sockfd = socket(AF_INET, SOCK_STREAM, 0); server = gethostbyname("127.0.0.1"); @@ -585,61 +602,85 @@ int connectServer(void) (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); - if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) + + if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { - printf("Error in connect socket\n"); + printf("Error: %s (%d) occured in connect socket\n", + strerror(errno), + errno); + close(sockfd); return -1; } + return sockfd; } int main(int argc, char **argv) { - pid_t cpid; - cpid = fork(); + pid_t cpid = fork(); + if(cpid == -1) { printf("fork fail\n"); return -1; } + if (cpid) { - int i = 2; - int j; - char buffer[256]; - int sockfd, newsockfd, portno; - socklen_t clilen; - struct sockaddr_in serv_addr, cli_addr; + int i = GTEST_SOCKS_ACCEPTED; + int j = 0, optval = 1; + char buffer[256] = {}; + int sockfd = 0, newsockfd[GTEST_SOCKS_ACCEPTED] = {}, portno = 0; + socklen_t clilen = {}; + struct sockaddr_in serv_addr = {}, cli_addr = {}; sockfd = socket(AF_INET, SOCK_STREAM, 0); + if(sockfd == -1) { printf("Error in creating socket\n"); return -1; } + bzero((char *) &serv_addr, sizeof(serv_addr)); portno = 8080; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); + + if (setsockopt(sockfd, + SOL_SOCKET, + SO_REUSEADDR, + &optval, + sizeof(optval)) == -1) { + perror("setsockopt"); + close(sockfd); + exit(1); + } + j = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); if(j == -1) { perror("Bind Error\n"); return -1; } - listen(sockfd,5); + + listen(sockfd, 5); while(i) { clilen = sizeof(cli_addr); - newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); - if(newsockfd == -1) + newsockfd[i - 1] = accept(sockfd, + (struct sockaddr *)&cli_addr, + &clilen); + + if(newsockfd[i - 1] == -1) { printf("Error in accept"); return -1; } - bzero(buffer,256); - (void)(read(newsockfd,buffer,255) + 1); /* just ignore result */ + + bzero(buffer, 256); + (void)(read(newsockfd[i - 1], buffer, 255) + 1); /* just ignore result */ i--; close(newsockfd); } -- cgit v1.2.1