summaryrefslogtreecommitdiff
path: root/AudioManagerDaemon/test
diff options
context:
space:
mode:
authorchristian mueller <christian.ei.mueller@bmw.de>2011-12-20 23:58:20 +0100
committerchristian mueller <christian.ei.mueller@bmw.de>2011-12-20 23:58:20 +0100
commit18d1c7ee814993ae3fbee5635650e55472dac587 (patch)
tree598b6302cb883e9eaa58469d6e6e68a6e92a1c6c /AudioManagerDaemon/test
parent1c992fd131eab06ee4f5a7090377031975092aba (diff)
downloadaudiomanager-18d1c7ee814993ae3fbee5635650e55472dac587.tar.gz
* first version of the sockethandler with test
Diffstat (limited to 'AudioManagerDaemon/test')
-rw-r--r--AudioManagerDaemon/test/CMakeLists.txt1
-rw-r--r--AudioManagerDaemon/test/sockethandler/CMakeLists.txt69
-rw-r--r--AudioManagerDaemon/test/sockethandler/sockethandlerTest.cpp244
-rw-r--r--AudioManagerDaemon/test/sockethandler/sockethandlerTest.h56
4 files changed, 370 insertions, 0 deletions
diff --git a/AudioManagerDaemon/test/CMakeLists.txt b/AudioManagerDaemon/test/CMakeLists.txt
index 3bcc777..e2e22c0 100644
--- a/AudioManagerDaemon/test/CMakeLists.txt
+++ b/AudioManagerDaemon/test/CMakeLists.txt
@@ -15,3 +15,4 @@ cmake_minimum_required(VERSION 2.6)
add_subdirectory (database)
add_subdirectory (routingInterface)
add_subdirectory (controlInterface)
+add_subdirectory (sockethandler) \ No newline at end of file
diff --git a/AudioManagerDaemon/test/sockethandler/CMakeLists.txt b/AudioManagerDaemon/test/sockethandler/CMakeLists.txt
new file mode 100644
index 0000000..a7dd63d
--- /dev/null
+++ b/AudioManagerDaemon/test/sockethandler/CMakeLists.txt
@@ -0,0 +1,69 @@
+#
+# Copyright (C) 2011, BMW AG
+#
+# GeniviAudioMananger ControlInterfaceTest
+#
+# \file CMakeLists.txt
+#
+# \date 20-Oct-2011 3:42:04 PM
+# \author Christian Mueller (christian.ei.mueller@bmw.de)
+#
+# \section License
+# GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
+# Copyright (C) 2011, BMW AG Christian Mueller Christian.ei.mueller@bmw.de
+#
+# This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
+# This program 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, version 2.1, for more details.
+# You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
+# Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
+# Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
+# As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
+# Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
+#
+
+cmake_minimum_required(VERSION 2.6)
+
+PROJECT(socketHandlerTest)
+
+set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
+set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -DUNIT_TEST=1 -DDLT_CONTEXT=AudioManager")
+
+set(STD_INCLUDE_DIRS "/usr/include")
+set(DBUS_FOLDER ${CMAKE_SOURCE_DIR}/../../../dbusInterfaces)
+set(AUDIO_INCLUDES_FOLDER ${CMAKE_SOURCE_DIR}/../../../includes)
+set(DBUS_INCLUDES_FOLDER ${CMAKE_SOURCE_DIR}/../../../../dbusIncludes)
+set(INCLUDES_FOLDER ${CMAKE_SOURCE_DIR}/../../include)
+set(EXECUTABLE_OUTPUT_PATH ../../../../test/)
+
+FIND_PACKAGE(PkgConfig)
+pkg_check_modules(DLT REQUIRED automotive-dlt)
+
+INCLUDE_DIRECTORIES(
+ ${CMAKE_CURRENT_BINARY_DIR}
+ ${AUDIO_INCLUDES_FOLDER}
+ ${DBUS_ARCH_INCLUDE_DIR}
+ ${DBUS_INCLUDES_FOLDER}
+ ${CMAKE_SOURCE_DIR}
+ ${STD_INCLUDE_DIRS}
+ ${DLT_INCLUDE_DIRS}
+ ${DBUS_INCLUDE_DIR}
+ ${INCLUDES_FOLDER}
+)
+
+file(GLOB Socket_SRCS_CXX
+ "../../src/SocketHandler.cpp"
+ "*.cpp"
+)
+
+ADD_EXECUTABLE(socketHandlerTest ${Socket_SRCS_CXX})
+
+TARGET_LINK_LIBRARIES(socketHandlerTest
+ ${DLT_LIBRARIES}
+ ${DBUS_LIBRARY}
+ ${CMAKE_THREAD_LIBS_INIT}
+ gtest
+ gmock
+)
+
+
+
diff --git a/AudioManagerDaemon/test/sockethandler/sockethandlerTest.cpp b/AudioManagerDaemon/test/sockethandler/sockethandlerTest.cpp
new file mode 100644
index 0000000..2cab649
--- /dev/null
+++ b/AudioManagerDaemon/test/sockethandler/sockethandlerTest.cpp
@@ -0,0 +1,244 @@
+/*
+ * sockethandlerTest.cpp
+ *
+ * Created on: Dec 19, 2011
+ * Author: christian
+ */
+
+#include "sockethandlerTest.h"
+#include <stdio.h>
+#include <sys/socket.h> /* for socket(), connect(), (), and recv() */
+#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
+#include <sys/ioctl.h>
+#include <string.h>
+#include <netdb.h>
+
+
+using namespace testing;
+using namespace am;
+
+sockethandlerTest::sockethandlerTest()
+{
+}
+
+sockethandlerTest::~sockethandlerTest()
+{
+}
+
+void fdCallBack::connectSocket(int fd, const short events)
+{
+ std::cout<<"Socket connection received and open"<<std::endl;
+
+ //accept the connection
+ mSocketConnection = accept(fd, NULL, NULL);
+ short event = 0;
+ event |=POLLIN;
+
+ TBasicPollCallback* buf=&pSocketDataCallback;
+ //add new socketConnection to the handler
+ mSocketHandler->addFDPoll(mSocketConnection,event,buf);
+}
+
+
+
+
+fdCallBack::fdCallBack(SocketHandler *SocketHandler)
+:mSocketConnection(0),
+ mSocketHandler(SocketHandler),
+ pSocketDataCallback(this, &fdCallBack::handleSocketData),
+ pSocketConnectionCallback(this, &fdCallBack::connectSocket)
+{
+}
+
+
+
+void am::fdCallBack::handleSocketData(int fd, const short events)
+{
+ char buffer[3000];
+ std::string msg;
+
+ //there is something for us, read it
+ int read=recv(mSocketConnection,buffer,sizeof(buffer),NULL);
+ msg=std::string(buffer,read);
+ if (msg.compare("stopit")==0)
+ {
+ mSocketHandler->stop_listening();
+ }
+ else if (msg.compare("answer")==0)
+ {
+ std::string answer="myAnswer";
+ send(mSocketConnection,answer.c_str(),answer.size(),NULL);
+ }
+}
+
+fdCallBack::~fdCallBack()
+{
+}
+
+am::timerCallBack::timerCallBack(SocketHandler *myHandler)
+ :pTimer1Callback(this, &timerCallBack::timer1Callback),
+ pTimer2Callback(this, &timerCallBack::timer2Callback),
+ pTimer3Callback(this, &timerCallBack::timer3Callback),
+ pTimer4Callback(this, &timerCallBack::timer4Callback),
+ mSocketHandler(myHandler)
+
+{
+}
+
+
+
+am::timerCallBack::~timerCallBack()
+{
+}
+
+
+
+void am::timerCallBack::timer1Callback(SocketHandler::timerHandle_t handle)
+{
+ std::cout<<"callback1 called"<<std::endl;
+ timespec timeout;
+ timeout.tv_nsec=0;
+ timeout.tv_sec=1;
+ TBasicTimerCallback *buf=&pTimer1Callback;
+ SocketHandler::timerHandle_t handle_;
+ mSocketHandler->addTimer(timeout,buf,handle_);
+}
+
+
+
+void am::timerCallBack::timer2Callback(SocketHandler::timerHandle_t handle)
+{
+ std::cout<<"callback2 called"<<std::endl;
+ timespec timeout;
+ timeout.tv_nsec=0;
+ timeout.tv_sec=1;
+ TBasicTimerCallback *buf=&pTimer2Callback;
+ SocketHandler::timerHandle_t handle_;
+ mSocketHandler->addTimer(timeout,buf,handle_);
+}
+
+void am::timerCallBack::timer3Callback(SocketHandler::timerHandle_t handle)
+{
+ std::cout<<"callback3 called"<<std::endl;
+}
+
+void am::timerCallBack::timer4Callback(SocketHandler::timerHandle_t handle)
+{
+ std::cout<<"callback4 called"<<std::endl;
+}
+
+void* playWithSocketServer(void* data)
+{
+ int yes = 1;
+
+ //get a SocketHandler
+ SocketHandler myHandler;
+
+ //get a class that handles the callbacks from the handler
+ fdCallBack testCallback(&myHandler);
+
+ //prepare the socket, bind etc...
+ struct sockaddr_in servAddr;
+ int socketHandle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ setsockopt(socketHandle, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
+ servAddr.sin_family = AF_INET;
+ servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ servAddr.sin_port = htons(6060);
+ bind(socketHandle, (struct sockaddr*)(&servAddr), sizeof (servAddr));
+ listen(socketHandle, 3);
+
+ //prepare the event (we want POLLIN because we need to listen)
+ short event = 0;
+ event |=POLLIN;
+
+ TBasicPollCallback* buf=&testCallback.pSocketConnectionCallback;
+ //add the callback to the Sockethandler
+ myHandler.addFDPoll(socketHandle, event, buf);
+
+ //start the mainloop
+ myHandler.start_listenting();
+ close(socketHandle);
+}
+
+
+
+TEST(sockethandlerTest,playWithSockets)
+{
+ pthread_t serverThread;
+ char buffer[3000];
+
+ //creates a thread that handles the serverpart
+ pthread_create(&serverThread,NULL,playWithSocketServer,NULL);
+
+ sleep(1); //give a little time to settle everything
+ //make everything ready to send data
+ struct sockaddr_in servAddr;
+ int socketHandle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ struct hostent *host;
+ host = (struct hostent*) gethostbyname("localhost");
+
+ memset(&servAddr, 0, sizeof(servAddr));
+ memcpy(&servAddr.sin_addr, host->h_addr_list[0], host->h_length);
+ servAddr.sin_family = AF_INET;
+ servAddr.sin_port = htons(6060);
+
+ //connect to the socket
+ int k =connect(socketHandle,(struct sockaddr *) &servAddr, sizeof(servAddr));
+
+ std::string msg="answer";
+
+ //send first the answer message and wait for the reply
+ int p=send(socketHandle,msg.c_str(),msg.size(),NULL);
+ int read=recv(socketHandle,buffer,sizeof(buffer),NULL);
+ msg=std::string(buffer,read);
+ ASSERT_TRUE(msg.compare("myAnswer")==0);
+
+ msg="stopit";
+ //now send a message causing the handler to stop and end the loop
+ p=send(socketHandle,msg.c_str(),msg.size(),NULL);
+ pthread_join(serverThread,NULL);
+}
+
+TEST(sockethandlerTest,playWithTimers)
+{
+ SocketHandler myHandler;
+ timerCallBack testCallback(&myHandler);
+ timespec timeoutTime, timeout2, timeout3, timeout4;
+ timeoutTime.tv_sec=3;
+ timeoutTime.tv_nsec=0;
+ timeout2.tv_nsec=0;
+ timeout2.tv_sec=1;
+ timeout3.tv_nsec=000000000;
+ timeout3.tv_sec=2;
+ timeout4.tv_nsec=0;
+ timeout4.tv_sec=30;
+ TBasicTimerCallback* buf=&testCallback.pTimer1Callback;
+ TBasicTimerCallback* buf2=&testCallback.pTimer2Callback;
+ TBasicTimerCallback* buf3=&testCallback.pTimer3Callback;
+ TBasicTimerCallback* buf4=&testCallback.pTimer4Callback;
+ SocketHandler::timerHandle_t handle;
+ myHandler.addTimer(timeoutTime,buf,handle);
+ myHandler.addTimer(timeout2,buf2,handle);
+ myHandler.addTimer(timeout3,buf3,handle);
+ myHandler.addTimer(timeout4,buf4,handle);
+ myHandler.start_listenting();
+
+}
+
+void sockethandlerTest::SetUp()
+{
+}
+
+void sockethandlerTest::TearDown()
+{
+}
+
+int main(int argc, char **argv)
+{
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
+
+
+
+
diff --git a/AudioManagerDaemon/test/sockethandler/sockethandlerTest.h b/AudioManagerDaemon/test/sockethandler/sockethandlerTest.h
new file mode 100644
index 0000000..5b86ad9
--- /dev/null
+++ b/AudioManagerDaemon/test/sockethandler/sockethandlerTest.h
@@ -0,0 +1,56 @@
+/*
+ * sockethandlerTest.h
+ *
+ * Created on: Dec 19, 2011
+ * Author: christian
+ */
+
+#ifndef SOCKETHANDLERTEST_H_
+#define SOCKETHANDLERTEST_H_
+
+#include <gtest/gtest.h>
+#include "SocketHandler.h"
+
+namespace am {
+
+class fdCallBack
+{
+public:
+ fdCallBack(SocketHandler *SocketHandler);
+ virtual ~fdCallBack();
+ void connectSocket(int fd,const short events);
+ void handleSocketData(int fd,const short events);
+ TSpecificPollCallback<fdCallBack> pSocketDataCallback;
+ TSpecificPollCallback<fdCallBack> pSocketConnectionCallback;
+private:
+ int mSocketConnection;
+ SocketHandler *mSocketHandler;
+};
+
+class timerCallBack
+{
+public:
+ timerCallBack(SocketHandler *SocketHandler);
+ virtual ~timerCallBack();
+ void timer1Callback(SocketHandler::timerHandle_t handle);
+ void timer2Callback(SocketHandler::timerHandle_t handle);
+ void timer3Callback(SocketHandler::timerHandle_t handle);
+ void timer4Callback(SocketHandler::timerHandle_t handle);
+ TSpecificTimerCallback<timerCallBack> pTimer1Callback;
+ TSpecificTimerCallback<timerCallBack> pTimer2Callback;
+ TSpecificTimerCallback<timerCallBack> pTimer3Callback;
+ TSpecificTimerCallback<timerCallBack> pTimer4Callback;
+ SocketHandler *mSocketHandler;
+};
+
+class sockethandlerTest: public ::testing::Test
+{
+public:
+ sockethandlerTest();
+ virtual ~sockethandlerTest();
+ void SetUp();
+ void TearDown();
+};
+
+} /* namespace am */
+#endif /* SOCKETHANDLERTEST_H_ */