2
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
3
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
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.
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.
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.
19
* Additional permission under GNU GPL version 3 section 7:
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.
41
#include "sip/sipvoiplink.h"
46
pthread_mutex_t count_mutex;
47
pthread_cond_t count_nb_thread;
51
void *sippThreadWithCount(void *str)
54
pthread_mutex_lock(&count_mutex);
56
pthread_mutex_unlock(&count_mutex);
58
std::string *command = (std::string *)(str);
60
std::cout << "SIPTest: " << command << std::endl;
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
68
// -2: Fatal error binding a socket
69
int i = system(command->c_str());
73
pthread_mutex_lock(&count_mutex);
76
pthread_cond_signal(&count_nb_thread);
77
pthread_mutex_unlock(&count_mutex);
84
void *sippThread(void *str)
87
std::string *command = (std::string *)(str);
89
std::cout << "SIPTest: " << command << std::endl;
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
97
// -2: Fatal error binding a socket
98
int i = system(command->c_str());
100
CPPUNIT_ASSERT(i==0);
107
void SIPTest::setUp()
109
pthread_mutex_lock(&count_mutex);
111
pthread_mutex_unlock(&count_mutex);
114
void SIPTest::tearDown()
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");
121
std::cout << "SIPTest: Error from system call, killall sipp" << std::endl;
125
void SIPTest::testSimpleOutgoingIpCall ()
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");
133
int rc = pthread_create(&thethread, NULL, sippThread, (void *)(&command));
135
std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
138
std::string testaccount("IP2IP");
139
std::string testcallid("callid1234");
140
std::string testcallnumber("sip:test@127.0.0.1:5062");
142
CPPUNIT_ASSERT(!Manager::instance().hasCurrentCall());
144
// start a new call sending INVITE message to sipp instance
145
Manager::instance().outgoingCall(testaccount, testcallid, testcallnumber);
147
// must sleep here until receiving 180 and 200 message from peer
150
// call list should be empty for outgoing calls, only used for incoming calls
151
CPPUNIT_ASSERT(Manager::instance().getCallList().size() == 0);
153
CPPUNIT_ASSERT(Manager::instance().hasCurrentCall());
154
CPPUNIT_ASSERT(Manager::instance().getCurrentCallId() == testcallid);
156
std::map<std::string, std::string>::iterator iterCallDetails;
157
std::map<std::string, std::string> callDetails = Manager::instance().getCallDetails (testcallid);
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"));
172
Manager::instance().hangupCall(testcallid);
174
rc = pthread_join(thethread, &status);
176
std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
179
std::cout << "SIPTest: completed join with thread" << std::endl;
185
void SIPTest::testSimpleIncomingIpCall ()
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");
194
int rc = pthread_create(&thethread, NULL, sippThread, (void *)(&command));
196
std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
200
// sleep a while to make sure that sipp insdtance is initialized and sflphoned received
201
// the incoming invite.
204
// gtrab call id from sipvoiplink
205
SIPVoIPLink *siplink = SIPVoIPLink::instance ("");
207
CPPUNIT_ASSERT(siplink->_callMap.size() == 1);
208
CallMap::iterator iterCallId = siplink->_callMap.begin();
209
std::string testcallid = iterCallId->first;
211
// TODO: hmmm, should IP2IP call be stored in call list....
212
CPPUNIT_ASSERT(Manager::instance().getCallList().size() == 0);
215
CPPUNIT_ASSERT(Manager::instance().answerCall(testcallid));
220
rc = pthread_join(thethread, &status);
222
std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
225
std::cout << "SIPTest: completed join with thread" << std::endl;
229
void SIPTest::testTwoOutgoingIpCall ()
231
pthread_t firstCallThread, secondCallThread;
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");
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");
240
int rc = pthread_create(&firstCallThread, NULL, sippThread, (void *)(&firstCallCommand));
242
std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
245
rc = pthread_create(&secondCallThread, NULL, sippThread, (void *)(&secondCallCommand));
247
std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
252
std::string testAccount("IP2IP");
254
std::string firstCallID("callid1234");
255
std::string firstCallNumber("sip:test@127.0.0.1:5062");
257
std::string secondCallID("callid2345");
258
std::string secondCallNumber("sip:test@127.0.0.1:5064");
260
CPPUNIT_ASSERT(!Manager::instance().hasCurrentCall());
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);
266
// must sleep here until receiving 180 and 200 message from peer
269
Manager::instance().outgoingCall(testAccount, secondCallID, secondCallNumber);
273
Manager::instance().hangupCall(firstCallID);
275
rc = pthread_join(firstCallThread, &status);
277
std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
279
std::cout << "SIPTest: completed join with thread" << std::endl;
281
Manager::instance().hangupCall(secondCallID);
283
rc = pthread_join(secondCallThread, &status);
285
std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
288
std::cout << "SIPTest: completed join with thread" << std::endl;
291
void SIPTest::testTwoIncomingIpCall ()
294
pthread_mutex_init(&count_mutex, NULL);
295
pthread_cond_init (&count_nb_thread, NULL);
297
pthread_t firstCallThread, secondCallThread;
302
pthread_attr_init(&attr);
303
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
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");
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");
311
int rc = pthread_create(&firstCallThread, &attr, sippThreadWithCount, (void *)(&firstCallCommand));
313
std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
317
// sleep a while to make sure that sipp insdtance is initialized and sflphoned received
318
// the incoming invite.
321
// gtrab call id from sipvoiplink
322
SIPVoIPLink *sipLink = SIPVoIPLink::instance ("");
324
CPPUNIT_ASSERT(sipLink->_callMap.size() == 1);
325
CallMap::iterator iterCallId = sipLink->_callMap.begin();
326
std::string firstCallID = iterCallId->first;
329
CPPUNIT_ASSERT(Manager::instance().answerCall(firstCallID));
333
rc = pthread_create(&secondCallThread, &attr, sippThread, (void *)(&secondCallCommand));
335
std::cout << "SIPTest: Error; return code from pthread_create()" << std::endl;
340
CPPUNIT_ASSERT(sipLink->_callMap.size() == 2);
341
iterCallId = sipLink->_callMap.begin();
342
if(iterCallId->first == firstCallID)
344
std::string secondCallID = iterCallId->first;
346
CPPUNIT_ASSERT(Manager::instance().answerCall(secondCallID));
350
pthread_mutex_lock(&count_mutex);
352
pthread_cond_wait(&count_nb_thread, &count_mutex);
353
pthread_mutex_unlock(&count_mutex);
356
rc = pthread_join(firstCallThread, &status);
358
std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
361
std::cout << "SIPTest: completed join with thread 1" << std::endl;
363
rc = pthread_join(secondCallThread, &status);
365
std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
368
std::cout << "SIPTest: completed join with thread 2" << std::endl;
371
pthread_mutex_destroy(&count_mutex);
372
pthread_cond_destroy(&count_nb_thread);
377
void SIPTest::testHoldIpCall()
379
pthread_t callThread;
381
std::string callCommand("sipp -sf sippxml/test_3.xml -i 127.0.0.1 -p 5062 -m 1");
383
int rc = pthread_create(&callThread, NULL, sippThread, (void *)(&callCommand));
385
std::cout << "SIPTest: ERROR; return code from pthread_create(): " << rc << std::endl;
388
std::cout << "SIPTest: completed thread creation" << std::endl;
391
std::string testAccount("IP2IP");
393
std::string testCallID("callid1234");
394
std::string testCallNumber("sip:test@127.0.0.1:5062");
396
Manager::instance().outgoingCall(testAccount, testCallID, testCallNumber);
400
Manager::instance().onHoldCall(testCallID);
404
Manager::instance().offHoldCall(testCallID);
408
Manager::instance().hangupCall(testCallID);
412
void SIPTest::testIncomingIpCallSdp ()
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");
421
int rc = pthread_create(&thethread, NULL, sippThread, (void *)(&command));
423
std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
427
// sleep a while to make sure that sipp insdtance is initialized and sflphoned received
428
// the incoming invite.
431
// gtrab call id from sipvoiplink
432
SIPVoIPLink *siplink = SIPVoIPLink::instance ("");
434
CPPUNIT_ASSERT(siplink->_callMap.size() == 1);
435
CallMap::iterator iterCallId = siplink->_callMap.begin();
436
std::string testcallid = iterCallId->first;
438
// TODO: hmmm, should IP2IP call be stored in call list....
439
CPPUNIT_ASSERT(Manager::instance().getCallList().size() == 0);
442
CPPUNIT_ASSERT(Manager::instance().answerCall(testcallid));
447
rc = pthread_join(thethread, &status);
449
std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
452
std::cout << "SIPTest: completed join with thread" << std::endl;