~ubuntu-branches/ubuntu/precise/sflphone/precise

1
2
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Savoir-Faire Linux Inc.
 *  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.
 */

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>

#include <pthread.h>
#include <string>

#include "siptest.h"
#include "manager.h"
#include "sip/sipvoiplink.h"

using std::cout;
using std::endl;

// anonymous namespace
namespace {
pthread_mutex_t count_mutex;
pthread_cond_t count_nb_thread;
int counter = 0;
}

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;

    // Set up the sipp instance in this thread in order to catch return value
    // 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
    // -2: Fatal error binding a socket
    int i = system(command->c_str());

    CPPUNIT_ASSERT(i!=0);

    pthread_mutex_lock(&count_mutex);
    counter--;

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

    pthread_mutex_unlock(&count_mutex);

    pthread_exit(NULL);
}


void *sippThread(void *str)
{
    std::string *command = (std::string *)(str);

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

    // Set up the sipp instance in this thread in order to catch return value
    // 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
    // -2: Fatal error binding a socket
    int i = system(command->c_str());

    CPPUNIT_ASSERT(i==0);

    pthread_exit(NULL);
}


void SIPTest::setUp()
{
    pthread_mutex_lock(&count_mutex);
    counter = 0;
    pthread_mutex_unlock(&count_mutex);
}

void SIPTest::tearDown()
{

    // in order to stop any currently running threads
    std::cout << "SIPTest: Clean all remaining sipp instances" << std::endl;
    int ret = system("killall sipp");

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


void SIPTest::testSimpleOutgoingIpCall()
{
    pthread_t thethread;

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

    int rc = pthread_create(&thethread, NULL, sippThread, (void *)(&command));

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

    std::string testaccount("IP2IP");
    std::string testcallid("callid1234");
    std::string testcallnumber("sip:test@127.0.0.1:5062");

    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);

    // call list should be empty for outgoing calls, only used for incoming calls
    CPPUNIT_ASSERT(Manager::instance().getCallList().size() == 0);

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

    std::map<std::string, std::string>::iterator iterCallDetails;
    std::map<std::string, std::string> callDetails = Manager::instance().getCallDetails(testcallid);

    iterCallDetails = callDetails.find("ACCOUNTID");
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == ""));
    iterCallDetails = callDetails.find("PEER_NUMBER");
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == "<sip:test@127.0.0.1:5062>"));
    iterCallDetails = callDetails.find("PEER_NAME");
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == ""));
    iterCallDetails = callDetails.find("DISPLAY_NAME");
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == ""));
    iterCallDetails = callDetails.find("CALL_STATE");
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == "CURRENT"));
    iterCallDetails = callDetails.find("CALL_TYPE");
    CPPUNIT_ASSERT((iterCallDetails != callDetails.end()) && (iterCallDetails->second == "1"));

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

    void *status;
    rc = pthread_join(thethread, &status);

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


void SIPTest::testSimpleIncomingIpCall()
{
    pthread_t thethread;
    void *status;

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

    int rc = pthread_create(&thethread, NULL, sippThread, (void *)(&command));

    if (rc)
        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);

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

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

    // TODO: hmmm, should IP2IP call be stored in call list....
    CPPUNIT_ASSERT(Manager::instance().getCallList().size() == 0);

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

    sleep(1);

    rc = pthread_join(thethread, &status);

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


void SIPTest::testTwoOutgoingIpCall()
{
    pthread_t firstCallThread, secondCallThread;
    void *status;

    // This scenario expect to be put on hold before hangup
    std::string firstCallCommand("sipp -sf tools/sippxml/test_1.xml -i 127.0.0.1 -p 5062 -m 1");

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

    int rc = pthread_create(&firstCallThread, NULL, sippThread, (void *)(&firstCallCommand));

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

    rc = pthread_create(&secondCallThread, NULL, sippThread, (void *)(&secondCallCommand));

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

    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
    // this call should be put on hold when making the second call
    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);

    rc = pthread_join(firstCallThread, &status);

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

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

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

    rc = pthread_join(secondCallThread, &status);

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

void SIPTest::testTwoIncomingIpCall()
{
    pthread_mutex_init(&count_mutex, NULL);
    pthread_cond_init(&count_nb_thread, NULL);

    pthread_t firstCallThread, secondCallThread;

    pthread_attr_t attr;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    // the first call is supposed to be put on hold when answering teh second incoming call
    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");

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

    int rc = pthread_create(&firstCallThread, &attr, sippThreadWithCount, (void *)(&firstCallCommand));

    if (rc)
        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(1);

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

    CPPUNIT_ASSERT(sipLink->callMap_.size() == 1);
    CallMap::iterator iterCallId = sipLink->callMap_.begin();
    std::string firstCallID = iterCallId->first;

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

    sleep(1);

    rc = pthread_create(&secondCallThread, &attr, sippThread, (void *)(&secondCallCommand));

    if (rc) {
        std::cout << "SIPTest: Error; return  code from pthread_create()" << std::endl;
    }

    sleep(1);

    CPPUNIT_ASSERT(sipLink->callMap_.size() == 2);
    iterCallId = sipLink->callMap_.begin();

    if (iterCallId->first == firstCallID)
        iterCallId++;

    std::string secondCallID = iterCallId->first;

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

    sleep(2);

    pthread_mutex_lock(&count_mutex);

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

    pthread_mutex_unlock(&count_mutex);

    pthread_mutex_destroy(&count_mutex);
    pthread_cond_destroy(&count_nb_thread);
}


void SIPTest::testHoldIpCall()
{
    pthread_t callThread;

    std::string callCommand("sipp -sf tools/sippxml/test_3.xml -i 127.0.0.1 -p 5062 -m 1");

    int rc = pthread_create(&callThread, NULL, sippThread, (void *)(&callCommand));

    if (rc) {
        std::cout << "SIPTest: ERROR; return code from pthread_create(): " << rc << std::endl;
    } else
        std::cout << "SIPTest: completed thread creation" << std::endl;


    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);

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

    sleep(1);

    Manager::instance().hangupCall(testCallID);
}


void SIPTest::testIncomingIpCallSdp()
{
    pthread_t thethread;
    void *status;

    // command to be executed by the thread, user agent client which initiate a call and hangup
    std::string command("sipp -sf tools/sippxml/test_4.xml 127.0.0.1 -i 127.0.0.1 -p 5062 -m 1");

    int rc = pthread_create(&thethread, NULL, sippThread, (void *)(&command));

    if (rc)
        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);

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

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

    // TODO: hmmm, should IP2IP call be stored in call list....
    CPPUNIT_ASSERT(Manager::instance().getCallList().size() == 0);

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


    sleep(1);

    rc = pthread_join(thethread, &status);

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