* first version of SocketHandler working with DBus
[profile/ivi/genivi/genivi-audio-manager.git] / AudioManagerDaemon / test / sockethandler / sockethandlerTest.cpp
1 /*
2  * sockethandlerTest.cpp
3  *
4  *  Created on: Dec 19, 2011
5  *      Author: christian
6  */
7
8 #include "sockethandlerTest.h"
9 #include <stdio.h>
10 #include <sys/socket.h> /* for socket(), connect(), (), and recv() */
11 #include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
12 #include <sys/ioctl.h>
13 #include <string.h>
14 #include <netdb.h>
15
16
17 using namespace testing;
18 using namespace am;
19
20 sockethandlerTest::sockethandlerTest()
21 {
22 }
23
24 sockethandlerTest::~sockethandlerTest()
25 {
26 }
27
28 void fdCallBack::connectSocket(int fd, const short  events,void * userData)
29 {
30         std::cout<<"Socket connection received and open"<<std::endl;
31
32         //accept the connection
33         mSocketConnection = accept(fd, NULL, NULL);
34         short event = 0;
35         event |=POLLIN;
36
37         shPollCallBack* buf=&pSocketDataCallback;
38         //add new socketConnection to the handler
39         mSocketHandler->addFDPoll(mSocketConnection,event,buf,NULL);
40 }
41
42
43
44
45 fdCallBack::fdCallBack(SocketHandler *SocketHandler)
46 :mSocketConnection(0),
47  mSocketHandler(SocketHandler),
48  pSocketDataCallback(this, &fdCallBack::handleSocketData),
49  pSocketConnectionCallback(this, &fdCallBack::connectSocket)
50 {
51 }
52
53
54
55 void am::fdCallBack::handleSocketData(int fd, const short  events, void* userdata)
56 {
57         char buffer[3000];
58         std::string msg;
59
60         //there is something for us, read it
61         int read=recv(mSocketConnection,buffer,sizeof(buffer),NULL);
62         msg=std::string(buffer,read);
63         if (msg.compare("stopit")==0)
64         {
65                 mSocketHandler->stop_listening();
66         }
67         else if (msg.compare("answer")==0)
68         {
69                 std::string answer="myAnswer";
70                 send(mSocketConnection,answer.c_str(),answer.size(),NULL);
71         }
72 }
73
74 fdCallBack::~fdCallBack()
75 {
76 }
77
78 am::timerCallBack::timerCallBack(SocketHandler *myHandler)
79  :pTimer1Callback(this, &timerCallBack::timer1Callback),
80   pTimer2Callback(this, &timerCallBack::timer2Callback),
81   pTimer3Callback(this, &timerCallBack::timer3Callback),
82   pTimer4Callback(this, &timerCallBack::timer4Callback),
83   mSocketHandler(myHandler)
84
85 {
86 }
87
88
89
90 am::timerCallBack::~timerCallBack()
91 {
92 }
93
94
95
96 void am::timerCallBack::timer1Callback(SocketHandler::sh_timerHandle_t handle, void* userData)
97 {
98         std::cout<<"callback1 called"<<std::endl;
99         timespec timeout;
100         timeout.tv_nsec=0;
101         timeout.tv_sec=1;
102         TBasicTimerCallback *buf=&pTimer1Callback;
103         SocketHandler::sh_timerHandle_t handle_;
104         mSocketHandler->addTimer(timeout,buf,handle_,NULL);
105 }
106
107
108
109 void am::timerCallBack::timer2Callback(SocketHandler::sh_timerHandle_t handle, void* userData)
110 {
111         std::cout<<"callback2 called"<<std::endl;
112         timespec timeout;
113         timeout.tv_nsec=0;
114         timeout.tv_sec=1;
115         TBasicTimerCallback *buf=&pTimer2Callback;
116         SocketHandler::sh_timerHandle_t handle_;
117         mSocketHandler->addTimer(timeout,buf,handle_,NULL);
118 }
119
120 void am::timerCallBack::timer3Callback(SocketHandler::sh_timerHandle_t handle, void* userData)
121 {
122         std::cout<<"callback3 called"<<std::endl;
123 }
124
125 void am::timerCallBack::timer4Callback(SocketHandler::sh_timerHandle_t handle, void* userData)
126 {
127         std::cout<<"callback4 called"<<std::endl;
128 }
129
130 void* playWithSocketServer(void* data)
131 {
132         int yes = 1;
133
134         //get a SocketHandler
135         SocketHandler myHandler;
136
137         //get a class that handles the callbacks from the handler
138         fdCallBack testCallback(&myHandler);
139
140         //prepare the socket, bind etc...
141         struct sockaddr_in servAddr;
142         int socketHandle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
143         setsockopt(socketHandle, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
144         servAddr.sin_family      = AF_INET;
145         servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
146         servAddr.sin_port = htons(6060);
147         bind(socketHandle, (struct sockaddr*)(&servAddr), sizeof (servAddr));
148         listen(socketHandle, 3);
149
150         //prepare the event (we want POLLIN because we need to listen)
151         short event = 0;
152         event |=POLLIN;
153
154         shPollCallBack* buf=&testCallback.pSocketConnectionCallback;
155         //add the callback to the Sockethandler
156         myHandler.addFDPoll(socketHandle, event, buf, NULL);
157
158         //start the mainloop
159         myHandler.start_listenting();
160         close(socketHandle);
161 }
162
163
164
165 TEST(sockethandlerTest,playWithSockets)
166 {
167         pthread_t serverThread;
168         char buffer[3000];
169
170         //creates a thread that handles the serverpart
171         pthread_create(&serverThread,NULL,playWithSocketServer,NULL);
172
173         sleep(1); //give a little time to settle everything
174         //make everything ready to send data
175         struct sockaddr_in servAddr;
176         int socketHandle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
177         struct hostent *host;
178         host = (struct hostent*) gethostbyname("localhost");
179
180         memset(&servAddr, 0, sizeof(servAddr));
181         memcpy(&servAddr.sin_addr, host->h_addr_list[0], host->h_length);
182         servAddr.sin_family      = AF_INET;
183         servAddr.sin_port = htons(6060);
184
185         //connect to the socket
186         int k =connect(socketHandle,(struct sockaddr *) &servAddr, sizeof(servAddr));
187
188         std::string msg="answer";
189
190         //send first the answer message and wait for the reply
191         int p=send(socketHandle,msg.c_str(),msg.size(),NULL);
192         int read=recv(socketHandle,buffer,sizeof(buffer),NULL);
193         msg=std::string(buffer,read);
194         ASSERT_TRUE(msg.compare("myAnswer")==0);
195
196         msg="stopit";
197         //now send a message causing the handler to stop and end the loop
198         p=send(socketHandle,msg.c_str(),msg.size(),NULL);
199         pthread_join(serverThread,NULL);
200 }
201
202 TEST(sockethandlerTest,playWithTimers)
203 {
204         SocketHandler myHandler;
205         timerCallBack testCallback(&myHandler);
206         timespec timeoutTime, timeout2, timeout3, timeout4;
207         timeoutTime.tv_sec=3;
208         timeoutTime.tv_nsec=0;
209         timeout2.tv_nsec=0;
210         timeout2.tv_sec=1;
211         timeout3.tv_nsec=000000000;
212         timeout3.tv_sec=2;
213         timeout4.tv_nsec=0;
214         timeout4.tv_sec=30;
215         TBasicTimerCallback* buf=&testCallback.pTimer1Callback;
216         TBasicTimerCallback* buf2=&testCallback.pTimer2Callback;
217         TBasicTimerCallback* buf3=&testCallback.pTimer3Callback;
218         TBasicTimerCallback* buf4=&testCallback.pTimer4Callback;
219         SocketHandler::sh_timerHandle_t handle;
220         myHandler.addTimer(timeoutTime,buf,handle,NULL);
221         myHandler.addTimer(timeout2,buf2,handle,NULL);
222         myHandler.addTimer(timeout3,buf3,handle,NULL);
223         myHandler.addTimer(timeout4,buf4,handle,NULL);
224         myHandler.start_listenting();
225
226 }
227
228 void sockethandlerTest::SetUp()
229 {
230 }
231
232 void sockethandlerTest::TearDown()
233 {
234 }
235
236 int main(int argc, char **argv)
237 {
238         ::testing::InitGoogleTest(&argc, argv);
239         return RUN_ALL_TESTS();
240 }
241
242
243
244