siptest.cpp 12.1 KB
Newer Older
Emmanuel Milou's avatar
Emmanuel Milou committed
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Savoir-Faire Linux Inc.
Emmanuel Milou's avatar
Emmanuel Milou committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

31
#include <stdlib.h>
Emmanuel Milou's avatar
Emmanuel Milou committed
32
33
34
35
#include <stdio.h>
#include <iostream>
#include <fstream>

36
37
38
#include <pthread.h>
#include <string>

Emmanuel Milou's avatar
Emmanuel Milou committed
39
#include "siptest.h"
40
#include "manager.h"
41
#include "sip/sipvoiplink.h"
Emmanuel Milou's avatar
Emmanuel Milou committed
42

43
44
// anonymous namespace
namespace {
45
46
47
pthread_mutex_t count_mutex;
pthread_cond_t count_nb_thread;
int counter = 0;
48
}
49
50
51
52
53
54
55
56
57
58
59

void *sippThreadWithCount(void *str)
{
    pthread_mutex_lock(&count_mutex);
    counter++;
    pthread_mutex_unlock(&count_mutex);

    std::string *command = (std::string *)(str);

    std::cout << "SIPTest: " << command << std::endl;

60
    // Set up the sipp instance in this thread in order to catch return value
61
62
63
64
65
    // 0: All calls were successful
    // 1: At least one call failed
    // 97: exit on internal command. Calls may have been processed
    // 99: Normal exit without calls processed
    // -1: Fatal error
66
    // -2: Fatal error binding a socket
67
68
    int i = system(command->c_str());

Tristan Matthews's avatar
Tristan Matthews committed
69
    CPPUNIT_ASSERT(i);
70
71
72

    pthread_mutex_lock(&count_mutex);
    counter--;
73
74
75
76

    if (counter == 0)
        pthread_cond_signal(&count_nb_thread);

77
78
79
80
81
82
    pthread_mutex_unlock(&count_mutex);

    pthread_exit(NULL);
}


83
void *sippThread(void *str)
84
{
Tristan Matthews's avatar
Tristan Matthews committed
85
    std::string *command = static_cast<std::string *>(str);
86
    std::cout << "SIPTest: " << command << std::endl;
87

88
    // Set up the sipp instance in this thread in order to catch return value
89
90
91
92
93
    // 0: All calls were successful
    // 1: At least one call failed
    // 97: exit on internal command. Calls may have been processed
    // 99: Normal exit without calls processed
    // -1: Fatal error
94
    // -2: Fatal error binding a socket
95
    int i = system(command->c_str());
96

97
98
99
100
    std::stringstream output;
    output << i;

    std::cout << "SIPTest: Command executed by system returned: " << output.str() << std::endl;
101
102
103
104
    pthread_exit(NULL);
}


Emmanuel Milou's avatar
Emmanuel Milou committed
105
106
void SIPTest::setUp()
{
107
108
109
    pthread_mutex_lock(&count_mutex);
    counter = 0;
    pthread_mutex_unlock(&count_mutex);
Emmanuel Milou's avatar
Emmanuel Milou committed
110
111
112
113
}

void SIPTest::tearDown()
{
114
115
    // in order to stop any currently running threads
    std::cout << "SIPTest: Clean all remaining sipp instances" << std::endl;
116
    int ret = system("killall sipp");
117
118
119

    if (!ret)
        std::cout << "SIPTest: Error from system call, killall sipp" << std::endl;
120
121
122
}


123
void SIPTest::testSimpleOutgoingIpCall()
124
{
125
    pthread_t thethread;
126

127
    // command to be executed by the thread, user agent server waiting for a call
128
    std::string command("sipp -sn uas -i 127.0.0.1 -p 5068 -m 1 -bg");
129

Tristan Matthews's avatar
Tristan Matthews committed
130
    int rc = pthread_create(&thethread, NULL, sippThread, &command);
131

132
    if (rc)
133
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
134

135
136
    std::string testaccount("IP2IP");
    std::string testcallid("callid1234");
137
    std::string testcallnumber("sip:test@127.0.0.1:5068");
138
139
140
141
142
143
144
145
146
147
148
149
150
151

    CPPUNIT_ASSERT(!Manager::instance().hasCurrentCall());

    // start a new call sending INVITE message to sipp instance
    Manager::instance().outgoingCall(testaccount, testcallid, testcallnumber);

    // must sleep here until receiving 180 and 200 message from peer
    sleep(2);

    CPPUNIT_ASSERT(Manager::instance().hasCurrentCall());
    CPPUNIT_ASSERT(Manager::instance().getCurrentCallId() == testcallid);

    Manager::instance().hangupCall(testcallid);

Tristan Matthews's avatar
Tristan Matthews committed
152
    rc = pthread_join(thethread, NULL);
153

154
    if (rc)
155
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
156
    else
157
158
        std::cout << "SIPTest: completed join with thread" << std::endl;
}
159
160


161
void SIPTest::testSimpleIncomingIpCall()
162
163
164
{
    pthread_t thethread;

165
    // command to be executed by the thread, user agent client which initiate a call and hangup
166
    std::string command("sipp -sn uac 127.0.0.1 -i 127.0.0.1 -p 5062 -m 1i -bg");
167

Tristan Matthews's avatar
Tristan Matthews committed
168
    int rc = pthread_create(&thethread, NULL, sippThread, &command);
169

170
    if (rc)
171
172
173
174
175
176
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;

    // sleep a while to make sure that sipp insdtance is initialized and sflphoned received
    // the incoming invite.
    sleep(2);

177
178
    // gtrab call id from sipvoiplink
    SIPVoIPLink *siplink = SIPVoIPLink::instance();
179

180
181
    CPPUNIT_ASSERT(siplink->callMap_.size() == 1);
    CallMap::iterator iterCallId = siplink->callMap_.begin();
182
    std::string testcallid = iterCallId->first;
183

184
185
    // Answer this call
    CPPUNIT_ASSERT(Manager::instance().answerCall(testcallid));
186

187
    sleep(1);
188

Tristan Matthews's avatar
Tristan Matthews committed
189
    rc = pthread_join(thethread, NULL);
190

191
    if (rc)
192
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
193
    else
194
195
        std::cout << "SIPTest: completed join with thread" << std::endl;
}
196

197

198
void SIPTest::testTwoOutgoingIpCall()
199
{
200
    // This scenario expect to be put on hold before hangup
201
    std::string firstCallCommand("sipp -sf tools/sippxml/test_1.xml -i 127.0.0.1 -p 5062 -m 1");
202
203
204
205

    // The second call uses the default user agent scenario
    std::string secondCallCommand("sipp -sn uas -i 127.0.0.1 -p 5064 -m 1");

206
    pthread_t firstCallThread;
Tristan Matthews's avatar
Tristan Matthews committed
207
    int rc = pthread_create(&firstCallThread, NULL, sippThread, &firstCallCommand);
208

209
    if (rc)
210
211
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;

Tristan Matthews's avatar
Tristan Matthews committed
212
213
    pthread_t secondCallThread;
    rc = pthread_create(&secondCallThread, NULL, sippThread, &secondCallCommand);
214

215
    if (rc)
216
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;
217
218
219
220
221
222
223
224
225
226
227
228
229
230

    sleep(1);

    std::string testAccount("IP2IP");

    std::string firstCallID("callid1234");
    std::string firstCallNumber("sip:test@127.0.0.1:5062");

    std::string secondCallID("callid2345");
    std::string secondCallNumber("sip:test@127.0.0.1:5064");

    CPPUNIT_ASSERT(!Manager::instance().hasCurrentCall());

    // start a new call sending INVITE message to sipp instance
231
    // this call should be put on hold when making the second call
232
233
234
235
236
237
238
239
240
241
242
    Manager::instance().outgoingCall(testAccount, firstCallID, firstCallNumber);

    // must sleep here until receiving 180 and 200 message from peer
    sleep(1);

    Manager::instance().outgoingCall(testAccount, secondCallID, secondCallNumber);

    sleep(1);

    Manager::instance().hangupCall(firstCallID);

Tristan Matthews's avatar
Tristan Matthews committed
243
    rc = pthread_join(firstCallThread, NULL);
244

245
    if (rc)
246
247
248
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;

    std::cout << "SIPTest: completed join with thread" << std::endl;
249
250
251

    Manager::instance().hangupCall(secondCallID);

Tristan Matthews's avatar
Tristan Matthews committed
252
    rc = pthread_join(secondCallThread, NULL);
253

254
    if (rc)
255
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
256
    else
257
258
259
        std::cout << "SIPTest: completed join with thread" << std::endl;
}

260
void SIPTest::testTwoIncomingIpCall()
261
{
262
    pthread_mutex_init(&count_mutex, NULL);
263
    pthread_cond_init(&count_nb_thread, NULL);
264

265
266
267
268
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

Alexandre Savard's avatar
[#4233]    
Alexandre Savard committed
269
    // the first call is supposed to be put on hold when answering teh second incoming call
270
    std::string firstCallCommand("sipp -sf tools/sippxml/test_2.xml 127.0.0.1 -i 127.0.0.1 -p 5064 -m 1 > testfile1.txt -bg");
Alexandre Savard's avatar
[#4233]    
Alexandre Savard committed
271

272
    // command to be executed by the thread, user agent client which initiate a call and hangup
273
    std::string secondCallCommand("sipp -sn uac 127.0.0.1 -i 127.0.0.1 -p 5062 -m 1 -d 250 > testfile2.txt -bg");
274

Tristan Matthews's avatar
Tristan Matthews committed
275
276
    pthread_t firstCallThread;
    int rc = pthread_create(&firstCallThread, &attr, sippThreadWithCount, &firstCallCommand);
277

278
    if (rc)
279
280
281
282
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;

    // sleep a while to make sure that sipp insdtance is initialized and sflphoned received
    // the incoming invite.
283
    sleep(1);
284

285
286
    // gtrab call id from sipvoiplink
    SIPVoIPLink *sipLink = SIPVoIPLink::instance();
287

288
289
    CPPUNIT_ASSERT(sipLink->callMap_.size() == 1);
    CallMap::iterator iterCallId = sipLink->callMap_.begin();
Alexandre Savard's avatar
[#4233]    
Alexandre Savard committed
290
    std::string firstCallID = iterCallId->first;
291
292

    // Answer this call
Alexandre Savard's avatar
[#4233]    
Alexandre Savard committed
293
    CPPUNIT_ASSERT(Manager::instance().answerCall(firstCallID));
294

Alexandre Savard's avatar
[#4233]    
Alexandre Savard committed
295
    sleep(1);
Tristan Matthews's avatar
Tristan Matthews committed
296
297
    pthread_t secondCallThread;
    rc = pthread_create(&secondCallThread, &attr, sippThread, &secondCallCommand);
Alexandre Savard's avatar
[#4233]    
Alexandre Savard committed
298

Tristan Matthews's avatar
Tristan Matthews committed
299
    if (rc)
300
        std::cout << "SIPTest: Error; return  code from pthread_create()" << std::endl;
301
302

    sleep(1);
303

304
305
    CPPUNIT_ASSERT(sipLink->callMap_.size() == 2);
    iterCallId = sipLink->callMap_.begin();
306
307

    if (iterCallId->first == firstCallID)
Tristan Matthews's avatar
Tristan Matthews committed
308
        ++iterCallId;
309

Tristan Matthews's avatar
Tristan Matthews committed
310
    std::string secondCallID(iterCallId->first);
Alexandre Savard's avatar
[#4233]    
Alexandre Savard committed
311
312
313

    CPPUNIT_ASSERT(Manager::instance().answerCall(secondCallID));

314
315
316
    sleep(2);

    pthread_mutex_lock(&count_mutex);
317
318
319
320

    while (counter > 0)
        pthread_cond_wait(&count_nb_thread, &count_mutex);

321
    pthread_mutex_unlock(&count_mutex);
Alexandre Savard's avatar
[#4233]    
Alexandre Savard committed
322

323
324
    pthread_mutex_destroy(&count_mutex);
    pthread_cond_destroy(&count_nb_thread);
325
}
Alexandre Savard's avatar
[#4233]    
Alexandre Savard committed
326

327
328
329

void SIPTest::testHoldIpCall()
{
330
    std::string callCommand("sipp -sf tools/sippxml/test_3.xml -i 127.0.0.1 -p 5062 -m 1 -bg");
331

Tristan Matthews's avatar
Tristan Matthews committed
332
    pthread_t callThread;
333
    int rc = pthread_create(&callThread, NULL, sippThread, (void *)(&callCommand));
334

Tristan Matthews's avatar
Tristan Matthews committed
335
    if (rc)
336
        std::cout << "SIPTest: ERROR; return code from pthread_create(): " << rc << std::endl;
Tristan Matthews's avatar
Tristan Matthews committed
337
    else
338
        std::cout << "SIPTest: completed thread creation" << std::endl;
339

340
341
342
343
344
345
346
347
348
349
350
351
352
353

    std::string testAccount("IP2IP");

    std::string testCallID("callid1234");
    std::string testCallNumber("sip:test@127.0.0.1:5062");

    Manager::instance().outgoingCall(testAccount, testCallID, testCallNumber);

    sleep(1);

    Manager::instance().onHoldCall(testCallID);

    sleep(1);

354
    Manager::instance().offHoldCall(testCallID);
355
356
357

    sleep(1);

358
    Manager::instance().hangupCall(testCallID);
359
360
}

361

362
void SIPTest::testIncomingIpCallSdp()
363
364
{
    // command to be executed by the thread, user agent client which initiate a call and hangup
365
    std::string command("sipp -sf tools/sippxml/test_4.xml 127.0.0.1 -i 127.0.0.1 -p 5062 -m 1i -bg");
366

Tristan Matthews's avatar
Tristan Matthews committed
367
    pthread_t thethread;
368
    int rc = pthread_create(&thethread, NULL, sippThread, (void *)(&command));
369

370
    if (rc)
371
372
373
374
375
376
        std::cout << "SIPTest: ERROR; return code from pthread_create()" << std::endl;

    // sleep a while to make sure that sipp insdtance is initialized and sflphoned received
    // the incoming invite.
    sleep(2);

377
378
    // gtrab call id from sipvoiplink
    SIPVoIPLink *siplink = SIPVoIPLink::instance();
379

380
381
    CPPUNIT_ASSERT(siplink->callMap_.size() == 1);
    CallMap::iterator iterCallId = siplink->callMap_.begin();
382
383
384
    std::string testcallid = iterCallId->first;

    // TODO: hmmm, should IP2IP call be stored in call list....
Tristan Matthews's avatar
Tristan Matthews committed
385
    CPPUNIT_ASSERT(Manager::instance().getCallList().empty());
386
387
388
389
390
391
392

    // Answer this call
    CPPUNIT_ASSERT(Manager::instance().answerCall(testcallid));


    sleep(1);

Tristan Matthews's avatar
Tristan Matthews committed
393
    rc = pthread_join(thethread, NULL);
394

395
    if (rc)
396
        std::cout << "SIPTest: ERROR; return code from pthread_join(): " << rc << std::endl;
397
    else
398
399
        std::cout << "SIPTest: completed join with thread" << std::endl;
}