|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU Lesser General Public License as published by |
|
7 * the Free Software Foundation, version 2.1 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU Lesser General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU Lesser General Public License |
|
15 * along with this program. If not, |
|
16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/". |
|
17 * |
|
18 * Description: |
|
19 * |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Module Test cases for Service Framework IPC |
|
24 */ |
|
25 |
|
26 #include <QtTest/QtTest> |
|
27 #include <xqserviceipc.h> |
|
28 #include "commondefs.h" |
|
29 |
|
30 using namespace QtService; |
|
31 // CONSTANTS |
|
32 const int WAIT_TIME = 50000; // 5 seconds, must be a multiple of WAIT_INTERVAL |
|
33 const int WAIT_INTERVAL = 100; // 100 ms |
|
34 |
|
35 class QIPCTestThread: public QThread |
|
36 { |
|
37 private: |
|
38 QSemaphore* iSem; |
|
39 QString iName; |
|
40 int iCount; |
|
41 ServiceFwIPC* iIPC; |
|
42 public: |
|
43 QIPCTestThread( QSemaphore* aSem, QString aName ) |
|
44 { |
|
45 iSem = aSem; |
|
46 iName = aName; |
|
47 } |
|
48 ~QIPCTestThread() |
|
49 { |
|
50 } |
|
51 |
|
52 protected: |
|
53 void run() |
|
54 { |
|
55 TServiceIPCBackends backend; |
|
56 #ifdef __SYMBIAN32__ |
|
57 backend = ESymbianServer; |
|
58 #else |
|
59 backend = ELocalSocket; |
|
60 #endif //__SYMBIAN32__ |
|
61 |
|
62 iIPC = new ServiceFwIPC( backend, NULL ); |
|
63 iIPC->connect( TEST_SERVER_NAME ); |
|
64 |
|
65 for( int i=0; i<100; ++i ) |
|
66 { |
|
67 // Send 100 echo messages and verify the return |
|
68 QString echo="echoechoecho_"; |
|
69 echo+=iName; |
|
70 bool sent = iIPC->sendSync( REQUEST_1, echo.toAscii() ); |
|
71 if( sent == false ) |
|
72 { |
|
73 qDebug() << "Send error~!"; |
|
74 } |
|
75 |
|
76 // Wait for a reply |
|
77 iIPC->waitForRead(); |
|
78 QString rtn = QString(iIPC->readAll()); |
|
79 qDebug() << "ECHO Returned: " << rtn; |
|
80 if( rtn != echo ) |
|
81 { |
|
82 QFAIL("Thread verification error"); |
|
83 } |
|
84 // sleep a little and send again |
|
85 } |
|
86 delete iIPC; |
|
87 |
|
88 iSem->release(1); |
|
89 } |
|
90 }; |
|
91 |
|
92 class QIPClientTest : public QObject |
|
93 { |
|
94 Q_OBJECT |
|
95 // Member data |
|
96 private: |
|
97 ServiceFwIPC* iIPC; |
|
98 bool connected; |
|
99 bool iWaitForDownloadComplete; |
|
100 int iTestLength; |
|
101 |
|
102 private slots: |
|
103 // Automatically run at the start of entire test set |
|
104 void initTestCase() |
|
105 { |
|
106 iIPC = NULL; |
|
107 connected = false; |
|
108 } |
|
109 |
|
110 // Automatically run at the start of each test case |
|
111 //void init(); |
|
112 |
|
113 // Automatically run at the end of each test case |
|
114 //void cleanup(); |
|
115 |
|
116 // Automatically run at the end of entire test set |
|
117 //void cleanupTestCase(); |
|
118 |
|
119 // =============================================================== |
|
120 // Test construction of the IPC client |
|
121 // =============================================================== |
|
122 void testConstruct() |
|
123 { |
|
124 TServiceIPCBackends backend; |
|
125 #ifdef __SYMBIAN32__ |
|
126 backend = ESymbianServer; |
|
127 #else |
|
128 backend = ELocalSocket; |
|
129 #endif //__SYMBIAN32__ |
|
130 iIPC = new ServiceFwIPC( backend, this); |
|
131 |
|
132 if( iIPC == NULL ) |
|
133 { |
|
134 QFAIL("Unable to create IPC object"); |
|
135 } |
|
136 } |
|
137 |
|
138 // =============================================================== |
|
139 // Test connection to the client |
|
140 // =============================================================== |
|
141 void testStartServerAndConnect() |
|
142 { |
|
143 connected = iIPC->connect( TEST_SERVER_NAME ); |
|
144 if( !connected ) |
|
145 { |
|
146 // Start the server and sleep 100ms to let it start |
|
147 iIPC->startServer( TEST_SERVER_NAME,TEST_SERVER_EXE ); |
|
148 QTest::qSleep(100); |
|
149 connected = iIPC->connect( TEST_SERVER_NAME ); |
|
150 |
|
151 } |
|
152 if( connected == false ) |
|
153 { |
|
154 QFAIL("Connection failure, cannot continue"); |
|
155 } |
|
156 } |
|
157 |
|
158 // =============================================================== |
|
159 // Test simple IPC |
|
160 // =============================================================== |
|
161 void testSimpleIPC() |
|
162 { |
|
163 if( connected ) |
|
164 { |
|
165 // Send a simple "ECHO" message |
|
166 qDebug() << "Test Case 1: ECHO client"; |
|
167 QString echo="echoechoecho"; |
|
168 bool sent = iIPC->sendSync( REQUEST_1, echo.toAscii() ); |
|
169 if( sent == false ) |
|
170 { |
|
171 QFAIL("ECHO send failure"); |
|
172 } |
|
173 |
|
174 // Wait for a reply |
|
175 iIPC->waitForRead(); |
|
176 QString rtn = QString(iIPC->readAll()); |
|
177 qDebug() << "ECHO Returned: " << rtn; |
|
178 if( rtn != echo ) |
|
179 { |
|
180 QFAIL("ECHO receive failure"); |
|
181 } |
|
182 } |
|
183 else |
|
184 { |
|
185 QFAIL("Cannot run!"); |
|
186 } |
|
187 } |
|
188 |
|
189 // =============================================================== |
|
190 // Test simple IPC, big buffer of messages 1k characters |
|
191 // =============================================================== |
|
192 void testSimpleIPC2() |
|
193 { |
|
194 if( connected ) |
|
195 { |
|
196 // Send a large 1k message |
|
197 qDebug() << "Test Case 2: Sending a big message"; |
|
198 QString bigString="This is going to be a very long string!!"; // 40 characters |
|
199 QByteArray ary; |
|
200 for( int i=0; i< 25; ++i ) |
|
201 { |
|
202 ary.append( bigString.toAscii() ); |
|
203 } |
|
204 bool sent = iIPC->sendSync( REQUEST_2, ary ); |
|
205 if( sent == false ) |
|
206 { |
|
207 QFAIL("Send large string failure"); |
|
208 } |
|
209 |
|
210 // Wait for a reply, the reply should be the length of data received |
|
211 iIPC->waitForRead(); |
|
212 QString rtn = QString(iIPC->readAll()); |
|
213 qDebug() << "ECHO Returned: " << rtn; |
|
214 if( rtn != "1000" ) |
|
215 { |
|
216 QFAIL("Big String receive failure (1k)"); |
|
217 } |
|
218 } |
|
219 else |
|
220 { |
|
221 QFAIL("Cannot run!"); |
|
222 } |
|
223 } |
|
224 |
|
225 // =============================================================== |
|
226 // Test simple IPC, big buffer of messages 2k characters |
|
227 // =============================================================== |
|
228 void testSimpleIPC3() |
|
229 { |
|
230 if( connected ) |
|
231 { |
|
232 // Send a large 1k message |
|
233 qDebug() << "Test Case 3: Sending a very big message"; |
|
234 QString bigString="This is going to be a very long string!!"; // 40 characters |
|
235 QByteArray ary; |
|
236 for( int i=0; i< 50; ++i ) |
|
237 { |
|
238 ary.append( bigString.toAscii() ); |
|
239 } |
|
240 bool sent = iIPC->sendSync( REQUEST_2, ary ); |
|
241 if( sent == false ) |
|
242 { |
|
243 QFAIL("Send large string failure"); |
|
244 } |
|
245 |
|
246 // Wait for a reply, the reply should be the length of data received |
|
247 iIPC->waitForRead(); |
|
248 QString rtn = QString(iIPC->readAll()); |
|
249 qDebug() << "ECHO Returned: " << rtn; |
|
250 if( rtn != "2000" ) |
|
251 { |
|
252 QFAIL("Big String receive failure (2k)"); |
|
253 } |
|
254 } |
|
255 else |
|
256 { |
|
257 QFAIL("Cannot run!"); |
|
258 } |
|
259 } |
|
260 |
|
261 // =============================================================== |
|
262 // Test simple IPC, big buffer of messages 4k characters |
|
263 // =============================================================== |
|
264 void testSimpleIPC4() |
|
265 { |
|
266 if( connected ) |
|
267 { |
|
268 // Send a large 1k message |
|
269 qDebug() << "Test Case 4: Sending a very big message"; |
|
270 QString bigString="This is going to be a very long string!!"; // 40 characters |
|
271 QByteArray ary; |
|
272 for( int i=0; i< 100; ++i ) |
|
273 { |
|
274 ary.append( bigString.toAscii() ); |
|
275 } |
|
276 bool sent = iIPC->sendSync( REQUEST_2, ary ); |
|
277 if( sent == false ) |
|
278 { |
|
279 QFAIL("Send large string failure"); |
|
280 } |
|
281 |
|
282 // Wait for a reply, the reply should be the length of data received |
|
283 iIPC->waitForRead(); |
|
284 QString rtn = QString(iIPC->readAll()); |
|
285 qDebug() << "ECHO Returned: " << rtn; |
|
286 if( rtn != "4000" ) |
|
287 { |
|
288 QFAIL("Big String receive failure (4k)"); |
|
289 } |
|
290 } |
|
291 else |
|
292 { |
|
293 QFAIL("Cannot run!"); |
|
294 } |
|
295 } |
|
296 |
|
297 // =============================================================== |
|
298 // Test simple IPC, big buffer of messages 8k characters |
|
299 // =============================================================== |
|
300 void testSimpleIPC5() |
|
301 { |
|
302 if( connected ) |
|
303 { |
|
304 // Send a large 1k message |
|
305 qDebug() << "Test Case 5: Sending a very big message"; |
|
306 QString bigString="This is going to be a very long string!!"; // 40 characters |
|
307 QByteArray ary; |
|
308 for( int i=0; i< 200; ++i ) |
|
309 { |
|
310 ary.append( bigString.toAscii() ); |
|
311 } |
|
312 bool sent = iIPC->sendSync( REQUEST_2, ary ); |
|
313 if( sent == false ) |
|
314 { |
|
315 QFAIL("Send large string failure"); |
|
316 } |
|
317 |
|
318 // Wait for a reply, the reply should be the length of data received |
|
319 iIPC->waitForRead(); |
|
320 QString rtn = QString(iIPC->readAll()); |
|
321 qDebug() << "ECHO Returned: " << rtn; |
|
322 if( rtn != "8000" ) |
|
323 { |
|
324 QFAIL("Big String receive failure (8k)"); |
|
325 } |
|
326 } |
|
327 else |
|
328 { |
|
329 QFAIL("Cannot run!"); |
|
330 } |
|
331 } |
|
332 |
|
333 // =============================================================== |
|
334 // Test simple IPC, ECHO service with server returning data async |
|
335 // =============================================================== |
|
336 void testSimpleIPC6() |
|
337 { |
|
338 // Test ECHO service with async IPC on server side, callback later |
|
339 if( connected ) |
|
340 { |
|
341 // Send a simple "ECHO" message |
|
342 qDebug() << "Test Case 6: ECHO client async server"; |
|
343 QString echo="echoechoecho"; |
|
344 bool sent = iIPC->sendSync( REQUEST_3, echo.toAscii() ); |
|
345 if( sent == false ) |
|
346 { |
|
347 QFAIL("ECHO send failure"); |
|
348 } |
|
349 |
|
350 // Wait for a reply |
|
351 iIPC->waitForRead(); |
|
352 QString rtn = QString(iIPC->readAll()); |
|
353 qDebug() << "ECHO Returned: " << rtn; |
|
354 if( rtn != echo ) |
|
355 { |
|
356 QFAIL("ECHO receive failure"); |
|
357 } |
|
358 } |
|
359 else |
|
360 { |
|
361 QFAIL("Cannot run!"); |
|
362 } |
|
363 } |
|
364 |
|
365 // =============================================================== |
|
366 // Test simple IPC, big buffer of messages 1k/2k/4k/8k characters returned async |
|
367 // =============================================================== |
|
368 void testSimpleIPC7() |
|
369 { |
|
370 if( connected ) |
|
371 { |
|
372 // Send a large 1k message |
|
373 qDebug() << "Test Case 7: Sending a big message"; |
|
374 QString bigString="This is going to be a very long string!!"; // 40 characters |
|
375 QByteArray ary; |
|
376 for( int i=0; i< 25; ++i ) |
|
377 { |
|
378 ary.append( bigString.toAscii() ); |
|
379 } |
|
380 bool sent = iIPC->sendSync( REQUEST_4, ary ); |
|
381 if( sent == false ) |
|
382 { |
|
383 QFAIL("Send large string failure"); |
|
384 } |
|
385 |
|
386 // Wait for a reply, the reply should be the length of data received |
|
387 iIPC->waitForRead(); |
|
388 QString rtn = QString(iIPC->readAll()); |
|
389 qDebug() << "ECHO Returned: " << rtn; |
|
390 if( rtn != "1000" ) |
|
391 { |
|
392 QFAIL("Big String receive failure (1k)"); |
|
393 } |
|
394 |
|
395 // Reset for 2000 character case |
|
396 ary.clear(); |
|
397 for( int i=0; i< 50; ++i ) |
|
398 { |
|
399 ary.append( bigString.toAscii() ); |
|
400 } |
|
401 sent = iIPC->sendSync( REQUEST_4, ary ); |
|
402 if( sent == false ) |
|
403 { |
|
404 QFAIL("Send large string failure"); |
|
405 } |
|
406 |
|
407 // Wait for a reply, the reply should be the length of data received |
|
408 iIPC->waitForRead(); |
|
409 rtn = QString(iIPC->readAll()); |
|
410 qDebug() << "ECHO Returned: " << rtn; |
|
411 if( rtn != "2000" ) |
|
412 { |
|
413 QFAIL("Big String receive failure (2k)"); |
|
414 } |
|
415 |
|
416 // Reset for 4000 character case |
|
417 ary.clear(); |
|
418 for( int i=0; i< 100; ++i ) |
|
419 { |
|
420 ary.append( bigString.toAscii() ); |
|
421 } |
|
422 sent = iIPC->sendSync( REQUEST_4, ary ); |
|
423 if( sent == false ) |
|
424 { |
|
425 QFAIL("Send large string failure"); |
|
426 } |
|
427 |
|
428 // Wait for a reply, the reply should be the length of data received |
|
429 iIPC->waitForRead(); |
|
430 rtn = QString(iIPC->readAll()); |
|
431 qDebug() << "ECHO Returned: " << rtn; |
|
432 if( rtn != "4000" ) |
|
433 { |
|
434 QFAIL("Big String receive failure (4k)"); |
|
435 } |
|
436 |
|
437 // Reset for 4000 character case |
|
438 ary.clear(); |
|
439 for( int i=0; i< 200; ++i ) |
|
440 { |
|
441 ary.append( bigString.toAscii() ); |
|
442 } |
|
443 sent = iIPC->sendSync( REQUEST_4, ary ); |
|
444 if( sent == false ) |
|
445 { |
|
446 QFAIL("Send large string failure"); |
|
447 } |
|
448 |
|
449 // Wait for a reply, the reply should be the length of data received |
|
450 iIPC->waitForRead(); |
|
451 rtn = QString(iIPC->readAll()); |
|
452 qDebug() << "ECHO Returned: " << rtn; |
|
453 if( rtn != "8000" ) |
|
454 { |
|
455 QFAIL("Big String receive failure (8k)"); |
|
456 } |
|
457 } |
|
458 else |
|
459 { |
|
460 QFAIL("Cannot run!"); |
|
461 } |
|
462 } |
|
463 |
|
464 // =============================================================== |
|
465 // Test receiving big buffers on client side sync |
|
466 // =============================================================== |
|
467 void testSimpleIPC8() |
|
468 { |
|
469 if( connected ) |
|
470 { |
|
471 // Send a large 1k message |
|
472 qDebug() << "Test Case 8: Receiving a very big buffer"; |
|
473 |
|
474 QByteArray ary; |
|
475 ary.setNum(1000); |
|
476 bool sent = iIPC->sendSync( REQUEST_5, ary ); |
|
477 if( sent == false ) |
|
478 { |
|
479 QFAIL("Sending request failed"); |
|
480 } |
|
481 |
|
482 // Wait for a reply, the reply should be the length of data received |
|
483 iIPC->waitForRead(); |
|
484 QString rtn = QString(iIPC->readAll()); |
|
485 if( rtn.length() != 1000 ) |
|
486 { |
|
487 QFAIL("Receiving a very long string failed"); |
|
488 } |
|
489 else |
|
490 { |
|
491 qDebug() << "Test Case 8: Receiving a very big buffer 1000 ok"; |
|
492 } |
|
493 // Ask for a 2k return buffer |
|
494 // |
|
495 ary.clear(); |
|
496 ary.setNum(2000); |
|
497 sent = iIPC->sendSync( REQUEST_5, ary ); |
|
498 if( sent == false ) |
|
499 { |
|
500 QFAIL("Sending request failed"); |
|
501 } |
|
502 |
|
503 // Wait for a reply, the reply should be the length of data received |
|
504 iIPC->waitForRead(); |
|
505 rtn = QString(iIPC->readAll()); |
|
506 if( rtn.length() != 2000 ) |
|
507 { |
|
508 QFAIL("Receiving a very long string failed"); |
|
509 } |
|
510 else |
|
511 { |
|
512 qDebug() << "Test Case 8: Receiving a very big buffer 2000 ok"; |
|
513 } |
|
514 // Ask for a 2k return buffer |
|
515 // |
|
516 ary.clear(); |
|
517 ary.setNum(4000); |
|
518 sent = iIPC->sendSync( REQUEST_5, ary ); |
|
519 if( sent == false ) |
|
520 { |
|
521 QFAIL("Sending request failed"); |
|
522 } |
|
523 |
|
524 // Wait for a reply, the reply should be the length of data received |
|
525 iIPC->waitForRead(); |
|
526 rtn = QString(iIPC->readAll()); |
|
527 if( rtn.length() != 4000 ) |
|
528 { |
|
529 QFAIL("Receiving a very long string failed"); |
|
530 } |
|
531 else |
|
532 { |
|
533 qDebug() << "Test Case 8: Receiving a very big buffer 4000 ok"; |
|
534 } |
|
535 |
|
536 ary.clear(); |
|
537 ary.setNum(8000); |
|
538 sent = iIPC->sendSync( REQUEST_5, ary ); |
|
539 if( sent == false ) |
|
540 { |
|
541 QFAIL("Sending request failed"); |
|
542 } |
|
543 |
|
544 // Wait for a reply, the reply should be the length of data received |
|
545 iIPC->waitForRead(); |
|
546 rtn = QString(iIPC->readAll()); |
|
547 if( rtn.length() != 8000 ) |
|
548 { |
|
549 QFAIL("Receiving a very long string failed"); |
|
550 } |
|
551 else |
|
552 { |
|
553 qDebug() << "Test Case 8: Receiving a very big buffer 8000 ok"; |
|
554 } |
|
555 } |
|
556 else |
|
557 { |
|
558 QFAIL("Cannot run!"); |
|
559 } |
|
560 } |
|
561 |
|
562 // =============================================================== |
|
563 // Test receiving big buffers on client side async |
|
564 // =============================================================== |
|
565 void testSimpleIPC9() |
|
566 { |
|
567 if( connected ) |
|
568 { |
|
569 doReceiveAsyncBigBuffer( 1000 ); |
|
570 #ifndef __SYMBIAN32__ |
|
571 doReceiveAsyncBigBuffer( 2000 ); |
|
572 doReceiveAsyncBigBuffer( 4000 ); |
|
573 doReceiveAsyncBigBuffer( 8000 ); |
|
574 #endif // __SYMBIAN32__ |
|
575 } |
|
576 else |
|
577 { |
|
578 QFAIL("Cannot run!"); |
|
579 } |
|
580 } |
|
581 |
|
582 // =============================================================== |
|
583 // Test disconnect and destruction of the IPC client |
|
584 // =============================================================== |
|
585 void testSimpleIPC10() |
|
586 { |
|
587 // Test multi-threaded client read/write |
|
588 QSemaphore sem(0); |
|
589 |
|
590 QIPCTestThread* thread1 = new QIPCTestThread(&sem,"thread1"); |
|
591 QIPCTestThread* thread2 = new QIPCTestThread(&sem,"thread2"); |
|
592 thread1->start(); |
|
593 thread2->start(); |
|
594 // Wait until both threads die |
|
595 sem.acquire(2); |
|
596 delete thread1; |
|
597 delete thread2; |
|
598 } |
|
599 |
|
600 // =============================================================== |
|
601 // Test disconnect and destruction of the IPC client |
|
602 // =============================================================== |
|
603 void testDisconnect() |
|
604 { |
|
605 iIPC->sendSync( KILL_SERVER, "" ); |
|
606 iIPC->waitForRead(); |
|
607 iIPC->disconnect(); |
|
608 QTest::qWait(2500); |
|
609 delete iIPC; |
|
610 iIPC = NULL; |
|
611 } |
|
612 |
|
613 // Utility functions |
|
614 private: |
|
615 void WaitForTestComplete() |
|
616 { |
|
617 int num = WAIT_TIME / WAIT_INTERVAL; |
|
618 num*=3; |
|
619 iWaitForDownloadComplete = true; |
|
620 while ( num-- > 0 && iWaitForDownloadComplete ) |
|
621 { |
|
622 QTest::qWait(WAIT_INTERVAL); |
|
623 } |
|
624 if( iWaitForDownloadComplete == true ) |
|
625 { |
|
626 qDebug() << "Timed out"; |
|
627 } |
|
628 QVERIFY2( iWaitForDownloadComplete == false, "Test case timed out" ); |
|
629 iWaitForDownloadComplete = false; |
|
630 } |
|
631 |
|
632 void doReceiveAsyncBigBuffer( int aSize ) |
|
633 { |
|
634 qDebug() << "Test Case 9: Receiving a very big buffer of size: " << aSize;\ |
|
635 iTestLength = aSize; |
|
636 QByteArray ary; |
|
637 ary.setNum(aSize); |
|
638 iIPC->sendAsync( REQUEST_5, ary ); |
|
639 // Wait for signal |
|
640 QObject::connect(iIPC, SIGNAL( readyRead() ), |
|
641 this, SLOT( handleReadyRead() ) ); |
|
642 WaitForTestComplete(); |
|
643 // Cleanup signals |
|
644 QObject::disconnect(iIPC, SIGNAL( readyRead() ), |
|
645 this, SLOT( handleReadyRead() ) ); |
|
646 } |
|
647 |
|
648 public slots: |
|
649 void handleReadyRead() |
|
650 { |
|
651 QString rtn = iIPC->readAll(); |
|
652 if( rtn.length() != iTestLength ) |
|
653 { |
|
654 QFAIL("Receiving a very long string failed"); |
|
655 } |
|
656 else |
|
657 { |
|
658 qDebug() << "Test Case 9: Receiving a very big buffer " << iTestLength << " ok"; |
|
659 } |
|
660 iWaitForDownloadComplete = false; |
|
661 } |
|
662 }; |
|
663 |
|
664 QTEST_MAIN(QIPClientTest); |
|
665 #include "serviceipctest.moc" |