~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-common/test/siptest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-12-24 16:33:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101224163355-tkvvikqxbrbav6up
Tags: 0.9.11-1
* New upstream release
* Add new build dependencies on libwebkit-dev and libyaml-dev

* Bump Standards-Version up to 3.9.1
* Bump debhelper compatibility to 8
* Patch another typo in the upstream code (lintian notice)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
 
3
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 3 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 *
 
19
 *  Additional permission under GNU GPL version 3 section 7:
 
20
 *
 
21
 *  If you modify this program, or any covered work, by linking or
 
22
 *  combining it with the OpenSSL project's OpenSSL library (or a
 
23
 *  modified version of that library), containing parts covered by the
 
24
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 
25
 *  grants you additional permission to convey the resulting work.
 
26
 *  Corresponding Source for a non-source form of such a combination
 
27
 *  shall include the source code for the parts of OpenSSL used as well
 
28
 *  as that of the covered work.
 
29
 */
 
30
 
 
31
#include <stdlib.h>
 
32
#include <stdio.h>
 
33
#include <iostream>
 
34
#include <fstream>
 
35
 
 
36
#include <pthread.h>
 
37
#include <string>
 
38
 
 
39
#include "siptest.h"
 
40
#include "manager.h" 
 
41
#include "sip/sipvoiplink.h"
 
42
 
 
43
using std::cout;
 
44
using std::endl;
 
45
 
 
46
pthread_mutex_t count_mutex;
 
47
pthread_cond_t count_nb_thread;
 
48
int counter = 0;
 
49
 
 
50
 
 
51
void *sippThreadWithCount(void *str)
 
52
{
 
53
 
 
54
    pthread_mutex_lock(&count_mutex);
 
55
    counter++;
 
56
    pthread_mutex_unlock(&count_mutex);
 
57
 
 
58
    std::string *command = (std::string *)(str);
 
59
 
 
60
    std::cout << "SIPTest: " << command << std::endl;
 
61
 
 
62
    // Set up the sipp instance in this thread in order to catch return value 
 
63
    // 0: All calls were successful
 
64
    // 1: At least one call failed
 
65
    // 97: exit on internal command. Calls may have been processed
 
66
    // 99: Normal exit without calls processed
 
67
    // -1: Fatal error
 
68
    // -2: Fatal error binding a socket 
 
69
    int i = system(command->c_str());
 
70
 
 
71
    CPPUNIT_ASSERT(i!=0);
 
72
 
 
73
    pthread_mutex_lock(&count_mutex);
 
74
    counter--;
 
75
    if(counter == 0)
 
76
        pthread_cond_signal(&count_nb_thread);
 
77
    pthread_mutex_unlock(&count_mutex);
 
78
 
 
79
    pthread_exit(NULL);
 
80
 
 
81
}
 
82
 
 
83
 
 
84
void *sippThread(void *str)
 
85
{
 
86
 
 
87
    std::string *command = (std::string *)(str); 
 
88
 
 
89
    std::cout << "SIPTest: " << command << std::endl;
 
90
 
 
91
    // Set up the sipp instance in this thread in order to catch return value 
 
92
    // 0: All calls were successful
 
93
    // 1: At least one call failed
 
94
    // 97: exit on internal command. Calls may have been processed
 
95
    // 99: Normal exit without calls processed
 
96
    // -1: Fatal error
 
97
    // -2: Fatal error binding a socket 
 
98
    int i = system(command->c_str());
 
99
        
 
100
    CPPUNIT_ASSERT(i==0);
 
101
 
 
102
    pthread_exit(NULL);
 
103
 
 
104
}
 
105
 
 
106
 
 
107
void SIPTest::setUp()
 
108
{
 
109
    pthread_mutex_lock(&count_mutex);
 
110
    counter = 0;
 
111
    pthread_mutex_unlock(&count_mutex);
 
112
}
 
113
 
 
114
void SIPTest::tearDown()
 
115
{
 
116
 
 
117
    // in order to stop any currently running threads
 
118
    std::cout << "SIPTest: Clean all remaining sipp instances" << std::endl;
 
119
    int ret = system("killall sipp");
 
120
    if(!ret)
 
121
        std::cout << "SIPTest: Error from system call, killall sipp" << std::endl;
 
122
}
 
123
 
 
124
 
 
125
void SIPTest::testSimpleOutgoingIpCall ()
 
126
{
 
127
    pthread_t thethread;
 
128
    void *status;
 
129
 
 
130
    // command to be executed by the thread, user agent server waiting for a call
 
131
    std::string command("sipp -sn uas -i 127.0.0.1 -p 5062 -m 1");
 
132
 
 
133
    int rc = pthread_create(&thethread, NULL, sippThread, (void *)(&command));
 
134
    if (rc) {
 
135
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
 
136
    }
 
137
    
 
138
    std::string testaccount("IP2IP");
 
139
    std::string testcallid("callid1234");
 
140
    std::string testcallnumber("sip:test@127.0.0.1:5062");
 
141
 
 
142
    CPPUNIT_ASSERT(!Manager::instance().hasCurrentCall());
 
143
 
 
144
    // start a new call sending INVITE message to sipp instance
 
145
    Manager::instance().outgoingCall(testaccount, testcallid, testcallnumber);
 
146
 
 
147
    // must sleep here until receiving 180 and 200 message from peer
 
148
    sleep(2);
 
149
 
 
150
    // call list should be empty for outgoing calls, only used for incoming calls
 
151
    CPPUNIT_ASSERT(Manager::instance().getCallList().size() == 0);
 
152
 
 
153
    CPPUNIT_ASSERT(Manager::instance().hasCurrentCall());
 
154
    CPPUNIT_ASSERT(Manager::instance().getCurrentCallId() == testcallid);
 
155
 
 
156
    std::map<std::string, std::string>::iterator iterCallDetails;
 
157
    std::map<std::string, std::string> callDetails = Manager::instance().getCallDetails (testcallid);
 
158
   
 
159
    iterCallDetails = callDetails.find("ACCOUNTID");
 
160
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == ""));
 
161
    iterCallDetails = callDetails.find("PEER_NUMBER");
 
162
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == "<sip:test@127.0.0.1:5062>")); 
 
163
    iterCallDetails = callDetails.find("PEER_NAME");
 
164
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == ""));
 
165
    iterCallDetails = callDetails.find("DISPLAY_NAME");
 
166
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == ""));
 
167
    iterCallDetails = callDetails.find("CALL_STATE");
 
168
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == "CURRENT"));
 
169
    iterCallDetails = callDetails.find("CALL_TYPE");
 
170
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == "1"));
 
171
 
 
172
    Manager::instance().hangupCall(testcallid);
 
173
 
 
174
    rc = pthread_join(thethread, &status);
 
175
    if (rc) {
 
176
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
 
177
    }
 
178
    else
 
179
        std::cout << "SIPTest: completed join with thread" << std::endl;
 
180
 
 
181
 
 
182
}
 
183
 
 
184
 
 
185
void SIPTest::testSimpleIncomingIpCall ()
 
186
{
 
187
 
 
188
    pthread_t thethread;
 
189
    void *status;
 
190
 
 
191
    // command to be executed by the thread, user agent client which initiate a call and hangup
 
192
    std::string command("sipp -sn uac 127.0.0.1 -i 127.0.0.1 -p 5062 -m 1");
 
193
 
 
194
    int rc = pthread_create(&thethread, NULL, sippThread, (void *)(&command));
 
195
    if (rc) {
 
196
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
 
197
    }
 
198
    
 
199
 
 
200
    // sleep a while to make sure that sipp insdtance is initialized and sflphoned received
 
201
    // the incoming invite.
 
202
    sleep(2);
 
203
 
 
204
    // gtrab call id from sipvoiplink 
 
205
    SIPVoIPLink *siplink = SIPVoIPLink::instance (""); 
 
206
 
 
207
    CPPUNIT_ASSERT(siplink->_callMap.size() == 1);
 
208
    CallMap::iterator iterCallId = siplink->_callMap.begin();
 
209
    std::string testcallid = iterCallId->first;
 
210
  
 
211
    // TODO: hmmm, should IP2IP call be stored in call list....
 
212
    CPPUNIT_ASSERT(Manager::instance().getCallList().size() == 0);
 
213
 
 
214
    // Answer this call
 
215
    CPPUNIT_ASSERT(Manager::instance().answerCall(testcallid));
 
216
    
 
217
 
 
218
    sleep(1);
 
219
 
 
220
    rc = pthread_join(thethread, &status);
 
221
    if (rc) {
 
222
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
 
223
    }
 
224
    else
 
225
        std::cout << "SIPTest: completed join with thread" << std::endl;
 
226
}
 
227
 
 
228
 
 
229
void SIPTest::testTwoOutgoingIpCall ()
 
230
{
 
231
    pthread_t firstCallThread, secondCallThread;
 
232
    void *status;
 
233
 
 
234
    // This scenario expect to be put on hold before hangup 
 
235
    std::string firstCallCommand("sipp -sf sippxml/test_1.xml -i 127.0.0.1 -p 5062 -m 1");
 
236
 
 
237
    // The second call uses the default user agent scenario
 
238
    std::string secondCallCommand("sipp -sn uas -i 127.0.0.1 -p 5064 -m 1");
 
239
 
 
240
    int rc = pthread_create(&firstCallThread, NULL, sippThread, (void *)(&firstCallCommand));
 
241
    if (rc) {
 
242
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
 
243
    }
 
244
 
 
245
    rc = pthread_create(&secondCallThread, NULL, sippThread, (void *)(&secondCallCommand));
 
246
    if(rc) {
 
247
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
 
248
    }
 
249
 
 
250
    sleep(1);
 
251
 
 
252
    std::string testAccount("IP2IP");
 
253
 
 
254
    std::string firstCallID("callid1234");
 
255
    std::string firstCallNumber("sip:test@127.0.0.1:5062");
 
256
 
 
257
    std::string secondCallID("callid2345");
 
258
    std::string secondCallNumber("sip:test@127.0.0.1:5064");
 
259
 
 
260
    CPPUNIT_ASSERT(!Manager::instance().hasCurrentCall());
 
261
 
 
262
    // start a new call sending INVITE message to sipp instance
 
263
    // this call should be put on hold when making the second call 
 
264
    Manager::instance().outgoingCall(testAccount, firstCallID, firstCallNumber);
 
265
 
 
266
    // must sleep here until receiving 180 and 200 message from peer
 
267
    sleep(1);
 
268
 
 
269
    Manager::instance().outgoingCall(testAccount, secondCallID, secondCallNumber);
 
270
 
 
271
    sleep(1);
 
272
 
 
273
    Manager::instance().hangupCall(firstCallID);
 
274
 
 
275
    rc = pthread_join(firstCallThread, &status);
 
276
    if(rc) {
 
277
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
 
278
    }
 
279
        std::cout << "SIPTest: completed join with thread" << std::endl;
 
280
 
 
281
    Manager::instance().hangupCall(secondCallID);
 
282
 
 
283
    rc = pthread_join(secondCallThread, &status);
 
284
    if (rc) {
 
285
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
 
286
    }
 
287
    else
 
288
        std::cout << "SIPTest: completed join with thread" << std::endl;
 
289
}
 
290
 
 
291
void SIPTest::testTwoIncomingIpCall ()
 
292
{
 
293
 
 
294
    pthread_mutex_init(&count_mutex, NULL);
 
295
    pthread_cond_init (&count_nb_thread, NULL);
 
296
 
 
297
    pthread_t firstCallThread, secondCallThread;
 
298
    void *status;
 
299
 
 
300
    pthread_attr_t attr;
 
301
 
 
302
    pthread_attr_init(&attr);
 
303
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
304
 
 
305
    // the first call is supposed to be put on hold when answering teh second incoming call
 
306
    std::string firstCallCommand("sipp -sf sippxml/test_2.xml 127.0.0.1 -i 127.0.0.1 -p 5064 -m 1 > testfile1.txt");
 
307
 
 
308
    // command to be executed by the thread, user agent client which initiate a call and hangup
 
309
    std::string secondCallCommand("sipp -sn uac 127.0.0.1 -i 127.0.0.1 -p 5062 -m 1 -d 250 > testfile2.txt");
 
310
 
 
311
    int rc = pthread_create(&firstCallThread, &attr, sippThreadWithCount, (void *)(&firstCallCommand));
 
312
    if (rc) {
 
313
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
 
314
    }
 
315
 
 
316
 
 
317
    // sleep a while to make sure that sipp insdtance is initialized and sflphoned received
 
318
    // the incoming invite.
 
319
    sleep(1);
 
320
 
 
321
    // gtrab call id from sipvoiplink 
 
322
    SIPVoIPLink *sipLink = SIPVoIPLink::instance ("");
 
323
 
 
324
    CPPUNIT_ASSERT(sipLink->_callMap.size() == 1);
 
325
    CallMap::iterator iterCallId = sipLink->_callMap.begin();
 
326
    std::string firstCallID = iterCallId->first;
 
327
 
 
328
    // Answer this call
 
329
    CPPUNIT_ASSERT(Manager::instance().answerCall(firstCallID));
 
330
 
 
331
    sleep(1);
 
332
 
 
333
    rc = pthread_create(&secondCallThread, &attr, sippThread, (void *)(&secondCallCommand));
 
334
    if(rc) {
 
335
        std::cout << "SIPTest: Error; return  code from pthread_create()" << std::endl;
 
336
    }
 
337
 
 
338
    sleep(1);
 
339
 
 
340
    CPPUNIT_ASSERT(sipLink->_callMap.size() == 2);
 
341
    iterCallId = sipLink->_callMap.begin();
 
342
    if(iterCallId->first == firstCallID)
 
343
        iterCallId++;
 
344
    std::string secondCallID = iterCallId->first;
 
345
 
 
346
    CPPUNIT_ASSERT(Manager::instance().answerCall(secondCallID));
 
347
 
 
348
    sleep(2);
 
349
 
 
350
    pthread_mutex_lock(&count_mutex);
 
351
    while(counter > 0)
 
352
        pthread_cond_wait(&count_nb_thread, &count_mutex);
 
353
    pthread_mutex_unlock(&count_mutex);
 
354
 
 
355
    /*
 
356
    rc = pthread_join(firstCallThread, &status);
 
357
    if (rc) {
 
358
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
 
359
    }
 
360
    else
 
361
        std::cout << "SIPTest: completed join with thread 1" << std::endl;
 
362
 
 
363
    rc = pthread_join(secondCallThread, &status);
 
364
    if (rc) {
 
365
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
 
366
    }
 
367
    else
 
368
        std::cout << "SIPTest: completed join with thread 2" << std::endl;
 
369
    */
 
370
 
 
371
    pthread_mutex_destroy(&count_mutex);
 
372
    pthread_cond_destroy(&count_nb_thread);
 
373
 
 
374
}
 
375
 
 
376
 
 
377
void SIPTest::testHoldIpCall()
 
378
{
 
379
    pthread_t callThread;
 
380
 
 
381
    std::string callCommand("sipp -sf sippxml/test_3.xml -i 127.0.0.1 -p 5062 -m 1");
 
382
 
 
383
    int rc = pthread_create(&callThread, NULL, sippThread, (void *)(&callCommand));
 
384
    if(rc) {
 
385
        std::cout << "SIPTest: ERROR; return code from pthread_create(): " << rc << std::endl;
 
386
    }
 
387
    else
 
388
        std::cout << "SIPTest: completed thread creation" << std::endl;
 
389
 
 
390
 
 
391
    std::string testAccount("IP2IP");
 
392
 
 
393
    std::string testCallID("callid1234");
 
394
    std::string testCallNumber("sip:test@127.0.0.1:5062");
 
395
 
 
396
    Manager::instance().outgoingCall(testAccount, testCallID, testCallNumber);
 
397
 
 
398
    sleep(1);
 
399
 
 
400
    Manager::instance().onHoldCall(testCallID);
 
401
 
 
402
    sleep(1);
 
403
 
 
404
    Manager::instance().offHoldCall(testCallID);        
 
405
 
 
406
    sleep(1);
 
407
 
 
408
    Manager::instance().hangupCall(testCallID);
 
409
}
 
410
 
 
411
 
 
412
void SIPTest::testIncomingIpCallSdp ()
 
413
{
 
414
 
 
415
    pthread_t thethread;
 
416
    void *status;
 
417
 
 
418
    // command to be executed by the thread, user agent client which initiate a call and hangup
 
419
    std::string command("sipp -sf sippxml/test_4.xml 127.0.0.1 -i 127.0.0.1 -p 5062 -m 1");
 
420
 
 
421
    int rc = pthread_create(&thethread, NULL, sippThread, (void *)(&command));
 
422
    if (rc) {
 
423
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
 
424
    }
 
425
 
 
426
 
 
427
    // sleep a while to make sure that sipp insdtance is initialized and sflphoned received
 
428
    // the incoming invite.
 
429
    sleep(2);
 
430
 
 
431
    // gtrab call id from sipvoiplink 
 
432
    SIPVoIPLink *siplink = SIPVoIPLink::instance ("");
 
433
 
 
434
    CPPUNIT_ASSERT(siplink->_callMap.size() == 1);
 
435
    CallMap::iterator iterCallId = siplink->_callMap.begin();
 
436
    std::string testcallid = iterCallId->first;
 
437
 
 
438
    // TODO: hmmm, should IP2IP call be stored in call list....
 
439
    CPPUNIT_ASSERT(Manager::instance().getCallList().size() == 0);
 
440
 
 
441
    // Answer this call
 
442
    CPPUNIT_ASSERT(Manager::instance().answerCall(testcallid));
 
443
 
 
444
 
 
445
    sleep(1);
 
446
 
 
447
    rc = pthread_join(thethread, &status);
 
448
    if (rc) {
 
449
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
 
450
    }
 
451
    else
 
452
        std::cout << "SIPTest: completed join with thread" << std::endl;
 
453
}