From 28812dda5db637a12366a672272e8a498180c5f7 Mon Sep 17 00:00:00 2001 From: Christoph Lipka Date: Wed, 12 Oct 2016 16:26:30 +0900 Subject: Unit Test: Event handling Unit tests for DLT Daemon connection and event handling Signed-off-by: Christoph Lipka --- src/daemon/dlt_daemon_connection.c | 6 +- src/daemon/dlt_daemon_connection.h | 12 + src/daemon/dlt_daemon_event_handler.c | 24 +- src/daemon/dlt_daemon_event_handler.h | 7 + tests/CMakeLists.txt | 2 + tests/gtest_dlt_daemon_event_handler.cpp | 656 +++++++++++++++++++++++++++++++ 6 files changed, 701 insertions(+), 6 deletions(-) create mode 100644 tests/gtest_dlt_daemon_event_handler.cpp diff --git a/src/daemon/dlt_daemon_connection.c b/src/daemon/dlt_daemon_connection.c index 755e76a..51f975d 100644 --- a/src/daemon/dlt_daemon_connection.c +++ b/src/daemon/dlt_daemon_connection.c @@ -61,7 +61,7 @@ * @return The amount of bytes send on success, -1 otherwise. * errno is appropriately set. */ -static int dlt_connection_send(DltConnection *conn, +STATIC int dlt_connection_send(DltConnection *conn, void *msg, size_t msg_size) { @@ -158,7 +158,7 @@ DltConnection *dlt_connection_get_next(DltConnection *current, int type_mask) return current; } -static void dlt_connection_destroy_receiver(DltConnection *con) +STATIC void dlt_connection_destroy_receiver(DltConnection *con) { if (!con) return; @@ -189,7 +189,7 @@ static void dlt_connection_destroy_receiver(DltConnection *con) * * @return DltReceiver structure or NULL if none corresponds to the type. */ -static DltReceiver *dlt_connection_get_receiver(DltDaemonLocal *daemon_local, +STATIC DltReceiver *dlt_connection_get_receiver(DltDaemonLocal *daemon_local, DltConnectionType type, int fd) { diff --git a/src/daemon/dlt_daemon_connection.h b/src/daemon/dlt_daemon_connection.h index aa21a11..00759c6 100644 --- a/src/daemon/dlt_daemon_connection.h +++ b/src/daemon/dlt_daemon_connection.h @@ -48,4 +48,16 @@ void dlt_connection_destroy(DltConnection *); void *dlt_connection_get_callback(DltConnection *); +#ifdef DLT_UNIT_TESTS +int dlt_connection_send(DltConnection *conn, + void *msg, + size_t msg_size); + +void dlt_connection_destroy_receiver(DltConnection *con); + +DltReceiver *dlt_connection_get_receiver(DltDaemonLocal *daemon_local, + DltConnectionType type, + int fd); +#endif + #endif /* DLT_DAEMON_CONNECTION_H */ diff --git a/src/daemon/dlt_daemon_event_handler.c b/src/daemon/dlt_daemon_event_handler.c index 80817a4..9ee3bc1 100644 --- a/src/daemon/dlt_daemon_event_handler.c +++ b/src/daemon/dlt_daemon_event_handler.c @@ -61,6 +61,10 @@ */ int dlt_daemon_prepare_event_handling(DltEventHandler *ev) { + if (ev == NULL) + { + return DLT_RETURN_ERROR; + } ev->epfd = epoll_create(DLT_EPOLL_MAX_EVENTS); if (ev->epfd < 0) @@ -88,6 +92,10 @@ int dlt_daemon_handle_event(DltEventHandler *pEvent, DltDaemon *daemon, DltDaemonLocal *daemon_local) { + if ((pEvent == NULL) || (daemon == NULL) || (daemon_local == NULL)) + { + return DLT_RETURN_ERROR; + } int nfds = 0; int i = 0; char str[DLT_DAEMON_TEXTBUFSIZE]; @@ -187,6 +195,7 @@ int dlt_daemon_handle_event(DltEventHandler *pEvent, DltConnection *dlt_event_handler_find_connection(DltEventHandler *ev, int fd) { + DltConnection *temp = ev->connections; while ((temp != NULL) && (temp->receiver->fd != fd)) @@ -207,9 +216,13 @@ DltConnection *dlt_event_handler_find_connection(DltEventHandler *ev, * * @return 0 on success, -1 if the connection is not found. */ -static int dlt_daemon_remove_connection(DltEventHandler *ev, +STATIC int dlt_daemon_remove_connection(DltEventHandler *ev, DltConnection *to_remove) { + if (ev == NULL || to_remove == NULL) + { + return DLT_RETURN_ERROR; + } DltConnection **curr = &ev->connections; /* Find the address where to_remove value is registered */ @@ -262,16 +275,16 @@ void dlt_event_handler_cleanup_connections(DltEventHandler *ev) * @param ev The event handler structure where the connection list is. * @param connection The connection to be added. */ -static void dlt_daemon_add_connection(DltEventHandler *ev, +STATIC void dlt_daemon_add_connection(DltEventHandler *ev, DltConnection *connection) { + DltConnection **temp = &ev->connections; while (*temp != NULL) { temp = &(*temp)->next; } - *temp = connection; } @@ -429,6 +442,11 @@ int dlt_event_handler_unregister_connection(DltEventHandler *evhdl, DltDaemonLocal *daemon_local, int fd) { + if (evhdl == NULL || daemon_local == NULL) + { + return DLT_RETURN_ERROR; + } + /* Look for the pointer in the client list. * There shall be only one event handler with the same fd. */ diff --git a/src/daemon/dlt_daemon_event_handler.h b/src/daemon/dlt_daemon_event_handler.h index 532cd1f..ea88da9 100644 --- a/src/daemon/dlt_daemon_event_handler.h +++ b/src/daemon/dlt_daemon_event_handler.h @@ -56,4 +56,11 @@ int dlt_event_handler_unregister_connection(DltEventHandler *, int dlt_connection_check_activate(DltEventHandler *, DltConnection *, int); +#ifdef DLT_UNIT_TESTS +int dlt_daemon_remove_connection(DltEventHandler *ev, + DltConnection *to_remove); + +void dlt_daemon_add_connection(DltEventHandler *ev, + DltConnection *connection); +#endif #endif /* DLT_DAEMON_EVENT_HANDLER_H */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 99975dc..ab7ed9d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,6 +18,7 @@ add_executable(dlt_test_receiver dlt_test_receiver.c) add_executable(dlt_env_ll_unit_test dlt_env_ll_unit_test.cpp) add_executable(dlt-test-preregister-context dlt-test-preregister-context.c) add_executable(gtest_dlt_daemon_gateway gtest_dlt_daemon_gateway.cpp) +add_executable(gtest_dlt_daemon_event_handler gtest_dlt_daemon_event_handler.cpp) target_link_libraries(gtest_dlt_common gtest gtest_main dlt) target_link_libraries(gtest_dlt_user gtest gtest_main dlt) target_link_libraries(gtest_dlt_daemon_common gtest gtest_main dlt) @@ -25,6 +26,7 @@ target_link_libraries(dlt_test_receiver dlt) target_link_libraries(dlt_env_ll_unit_test gtest gtest_main dlt) target_link_libraries(dlt-test-preregister-context gtest gtest_main dlt) target_link_libraries(gtest_dlt_daemon_gateway gtest gtest_main dlt_daemon) +target_link_libraries(gtest_dlt_daemon_event_handler gtest gtest_main dlt_daemon) if(${WITH_DLT_CXX11_EXT}) add_executable(dlt-test-cpp-extension dlt-test-cpp-extension.cpp) diff --git a/tests/gtest_dlt_daemon_event_handler.cpp b/tests/gtest_dlt_daemon_event_handler.cpp new file mode 100644 index 0000000..86cfb1d --- /dev/null +++ b/tests/gtest_dlt_daemon_event_handler.cpp @@ -0,0 +1,656 @@ +/* + * @licence app begin@ + * SPDX license identifier: MPL-2.0 + * + * Copyright (C) 2016 Advanced Driver Information Technology. + * This code is developed by Advanced Driver Information Technology. + * Copyright of Advanced Driver Information Technology, Bosch and DENSO. + * + * This file is part of GENIVI Project DLT - Diagnostic Log and Trace. + * + * This Source Code Form is subject to the terms of the + * Mozilla Public License (MPL), v. 2.0. + * If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + * + * For further information see http://www.genivi.org/. + * @licence end@ + */ + +/*! + * \author Onkar Palkar onkar.palkar@wipro.com + * + * \copyright Copyright © 2016 Advanced Driver Information Technology. + * + * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/. + * + * \file gtest_dlt_daemon_event_handler.cpp + */ + +/******************************************************************************* +** ** +** SRC-MODULE: gtest_dlt_daemon_event_handler.cpp ** +** ** +** TARGET : linux ** +** ** +** PROJECT : DLT ** +** ** +** AUTHOR : Onkar Palkar onkar.palkar@wipro.com ** +** PURPOSE : Unit test connection and event handling ** +** ** +** REMARKS : ** +** ** +** PLATFORM DEPENDANT [yes/no]: yes ** +** ** +** TO BE CHANGED BY USER [yes/no]: no ** +** ** +*******************************************************************************/ + +/******************************************************************************* +** Author Identity ** +******************************************************************************** +** ** +** Initials Name Company ** +** -------- ------------------------- ---------------------------------- ** +** op Onkar Palkar Wipro ** +*******************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int connectServer(void); + +extern "C" +{ + #include "dlt-daemon.h" + #include "dlt_user.h" + #include "dlt_user_shared.h" + #include "dlt_user_shared_cfg.h" + #include "dlt_user_cfg.h" + #include "dlt-daemon_cfg.h" + #include "dlt_version.h" + #include "dlt_gateway.h" + #include "dlt_daemon_common.h" + #include "dlt_daemon_connection_types.h" + #include "dlt_daemon_event_handler.h" + #include "dlt_daemon_connection.h" + #include "dlt_daemon_event_handler_types.h" + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include "dlt_config_file_parser.h" + #include "dlt_common.h" + #include "dlt_daemon_client.h" +} + +/* Begin Method: dlt_daemon_event_handler::t_dlt_daemon_prepare_event_handling*/ +TEST(t_dlt_daemon_prepare_event_handling, normal) +{ + DltEventHandler ev; + EXPECT_EQ(DLT_RETURN_OK, dlt_daemon_prepare_event_handling(&ev)); +} + +TEST(t_dlt_daemon_prepare_event_handling, nullpointer) +{ + // NULL-Pointer, expect -1 + EXPECT_EQ(DLT_RETURN_ERROR, dlt_daemon_prepare_event_handling(NULL)); +} + +/* Begin Method: dlt_daemon_event_handler::t_dlt_daemon_handle_event*/ +TEST(t_dlt_daemon_handle_event, normal) +{ + DltDaemonLocal daemon_local; + DltDaemon daemon; + + EXPECT_EQ(DLT_RETURN_OK, dlt_daemon_prepare_event_handling(&daemon_local.pEvent)); + EXPECT_EQ(DLT_RETURN_OK, dlt_daemon_handle_event(&daemon_local.pEvent, + &daemon, + &daemon_local)); +} + +TEST(t_dlt_daemon_handle_event, nullpointer) +{ + EXPECT_EQ(DLT_RETURN_ERROR, dlt_daemon_handle_event(NULL,NULL,NULL)); +} + +/* Begin Method: dlt_daemon_event_handler::dlt_event_handler_find_connection*/ +TEST(t_dlt_event_handler_find_connection, normal) +{ + int fd = 10; + DltEventHandler ev; + DltConnection connections; + DltConnection *ret; + DltReceiver receiver; + 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); +} + +/* Begin Method: dlt_daemon_event_handler::dlt_daemon_add_connection*/ +TEST(t_dlt_daemon_add_connection, normal) +{ + DltEventHandler ev1; + DltConnection *head; + 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); + dlt_daemon_add_connection(&ev1, connections1); + head = (DltConnection*)ev1.connections->next; + EXPECT_EQ(DLT_CONNECTION_GATEWAY, head->type); + + free(ev1.connections); + free(connections1); +} + +/* Begin Method: dlt_daemon_event_handler::dlt_daemon_remove_connection*/ +TEST(t_dlt_daemon_remove_connection, normal) +{ + DltEventHandler ev1; + DltConnection *head; + 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); + + dlt_daemon_add_connection(&ev1, connections1); + head = (DltConnection*)ev1.connections->next; + + EXPECT_EQ(DLT_CONNECTION_GATEWAY, head->type); + + EXPECT_EQ(DLT_RETURN_OK, dlt_daemon_remove_connection(&ev1, connections1)); +} + +/* Begin Method: dlt_daemon_event_handler::dlt_event_handler_cleanup_connections*/ +TEST(t_dlt_event_handler_cleanup_connections, normal) +{ + DltEventHandler ev1; + 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); +} + +/* Begin Method: dlt_daemon_event_handler::dlt_connection_check_activate*/ +TEST(t_dlt_connection_check_activate, normal) +{ + int ret; + DltEventHandler evhdl; + DltConnection con; + DltReceiver receiver; + 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, ret); + + ret = dlt_connection_check_activate(&evhdl, &con, DEACTIVATE); + EXPECT_EQ(DLT_RETURN_OK, ret); + +} + +TEST(t_dlt_connection_check_activate, nullpointer) +{ + EXPECT_EQ(DLT_RETURN_ERROR, dlt_connection_check_activate(NULL, NULL, DEACTIVATE)); +} + +/* Begin Method: dlt_daemon_event_handler::dlt_event_handler_register_connection*/ +TEST(t_dlt_event_handler_register_connection, normal) +{ + int ret = 0; + 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; + 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); + + EXPECT_EQ(DLT_RETURN_OK, ret); + close(ev1.epfd); + free(ev1.connections); + free(connections1); +} + +TEST(t_dlt_event_handler_register_connection, nullpointer) +{ + EXPECT_EQ(DLT_RETURN_ERROR, dlt_event_handler_register_connection(NULL, NULL, NULL, 1)); +} + +/* Begin Method: dlt_daemon_event_handler::dlt_event_handler_unregister_connection*/ +TEST(t_dlt_event_handler_unregister_connection, normal) +{ + int ret = 0; + int fd = 0; + 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; + connections1 = (DltConnection *)malloc(sizeof(DltConnection)); + memset(connections1, 0, sizeof(DltConnection)); + connections1->next = 0; + connections1->type = DLT_CONNECTION_GATEWAY; + DltReceiver receiver; + connections1->receiver = &receiver; + + + 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); + EXPECT_EQ(DLT_RETURN_OK, ret); + + ret = dlt_event_handler_unregister_connection(&ev1, &daemon_local, fd); + EXPECT_EQ(DLT_RETURN_OK, ret); + close(ev1.epfd); + free(connections1); +} + +/* Begin Method: dlt_daemon_connections::dlt_connection_create*/ +TEST(t_dlt_connection_create, normal) +{ + int fd = 1; + int ret = 0; + + 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); + + ret = dlt_connection_create(&daemon_local, + &daemon_local.pEvent, + fd, + EPOLLIN, + DLT_CONNECTION_CLIENT_MSG_SERIAL); + EXPECT_EQ(DLT_RETURN_OK, ret); +} + +/* Begin Method: dlt_daemon_connections::dlt_connection_destroy*/ +TEST(t_dlt_connection_destroy, normal) +{ + 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->fd = -1; + to_destroy->receiver->buffer = NULL; + 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; + + 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; + + dlt_connection_destroy_receiver(to_destroy); + free(to_destroy); +} + +/* Begin Method: dlt_daemon_connections::t_dlt_connection_get_receiver*/ +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); + + EXPECT_EQ(fd, ret->fd); +} + +/* Begin Method: dlt_daemon_connections::(t_dlt_connection_get_next*/ +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 *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); + + 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*/ +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 *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); + + 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; + receiver.fd = connectServer(); + EXPECT_NE(-1,receiver.fd); + conn.receiver = &receiver; + conn.type = DLT_CONNECTION_CLIENT_MSG_TCP; + void *data1; + int size1 = 0; + DltDaemonLocal daemon_local; + data1 = daemon_local.msg.databuffer; + size1 = daemon_local.msg.datasize; + + ret = dlt_connection_send(&conn, data1, size1); + + EXPECT_EQ(DLT_RETURN_OK, ret); +} + +TEST(t_dlt_connection_send, normal_2) +{ + int ret = 0; + DltConnection conn; + DltReceiver receiver; + receiver.fd = 1; + conn.receiver = &receiver; + conn.type = DLT_CONNECTION_CLIENT_MSG_SERIAL; + ret = dlt_connection_send(&conn, + (void *)dltSerialHeader, + sizeof(dltSerialHeader)); + EXPECT_EQ(sizeof(dltSerialHeader), ret); +} + +TEST(t_dlt_connection_send, abnormal) +{ + int ret = 0; + DltConnection conn; + DltReceiver receiver; + conn.receiver = &receiver; + conn.type = DLT_CONNECTION_TYPE_MAX; + ret = dlt_connection_send(&conn, + (void *)dltSerialHeader, + sizeof(dltSerialHeader)); + EXPECT_EQ(DLT_RETURN_ERROR, ret); +} + +/* Begin Method: dlt_daemon_connections::t_dlt_connection_send_multiple*/ +TEST(t_dlt_connection_send_multiple, normal_1) +{ + int ret = 0; + void *data1, *data2; + int size1 = 0; + int size2 = 0; + 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; + conn.type = DLT_CONNECTION_CLIENT_MSG_TCP; + ret = dlt_connection_send_multiple(&conn, + data1, + size1, + data2, + size2, + 1); + EXPECT_EQ(DLT_RETURN_OK, ret); + close(receiver.fd); +} + +TEST(t_dlt_connection_send_multiple, normal_2) +{ + int ret = 0; + void *data1, *data2; + int size1 = 0; + int size2 = 0; + 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; + conn.type = DLT_CONNECTION_CLIENT_MSG_TCP; + + ret = dlt_connection_send_multiple(&conn, + data1, + size1, + data2, + size2, + 0); + EXPECT_EQ(DLT_RETURN_OK, ret); + close(receiver.fd); +} + +TEST(t_dlt_connection_send_multiple, nullpointer) +{ + int ret = 0; + void *data1, *data2; + int size1 = 0; + int size2 = 0; + 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; + + ret = dlt_connection_send_multiple(NULL, + data1, + size1, + data2, + size2, + 0); + EXPECT_EQ(DLT_RETURN_ERROR, ret); +} + +int connectServer(void) +{ + int sockfd, portno; + struct sockaddr_in serv_addr; + struct hostent *server; + portno = 8080; + sockfd = socket(AF_INET, SOCK_STREAM, 0); + server = gethostbyname("127.0.0.1"); + bzero((char *) &serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + bcopy((char *)server->h_addr, + (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) + { + printf("Error in connect socket\n"); + return -1; + } + return sockfd; +} + +int main(int argc, char **argv) +{ + pid_t cpid; + 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; + 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); + j = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); + if(j == -1) + { + perror("Bind Error\n"); + return -1; + } + listen(sockfd,5); + while(i) + { + clilen = sizeof(cli_addr); + newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); + if(newsockfd == -1) + { + printf("Error in accept"); + return -1; + } + bzero(buffer,256); + (void)(read(newsockfd,buffer,255) + 1); /* just ignore result */ + i--; + close(newsockfd); + } + close(sockfd); + } + else + { + + ::testing::InitGoogleTest(&argc, argv); + ::testing::FLAGS_gtest_break_on_failure = false; + //::testing::FLAGS_gtest_filter = "t_dlt_connection_send_multiple*"; + return RUN_ALL_TESTS(); + } + return 0; +} -- cgit v1.2.1