LCOV - code coverage report
Current view: top level - test/sip - test_SIP.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 24 45 53.3 %
Date: 2024-04-18 08:01:59 Functions: 4 7 57.1 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
       4             :  *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
       5             :  *  Author: Olivier Gregoire <olivier.gregoire@savoirfairelinux.com>
       6             :  *
       7             :  *  This program is free software; you can redistribute it and/or modify
       8             :  *  it under the terms of the GNU General Public License as published by
       9             :  *  the Free Software Foundation; either version 3 of the License, or
      10             :  *  (at your option) any later version.
      11             :  *
      12             :  *  This program is distributed in the hope that it will be useful,
      13             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  *  GNU General Public License for more details.
      16             :  *
      17             :  *  You should have received a copy of the GNU General Public License
      18             :  *  along with this program; if not, write to the Free Software
      19             :  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
      20             :  */
      21             : 
      22             : #include <cstdlib>
      23             : #include <cstdio>
      24             : #include <iostream>
      25             : #include <fstream>
      26             : 
      27             : #include <pthread.h>
      28             : #include <string>
      29             : #include <thread>
      30             : 
      31             : #include "test_SIP.h"
      32             : #include "call_const.h"
      33             : 
      34             : using namespace jami;
      35             : using namespace std::literals;
      36             : 
      37             : static pthread_mutex_t count_mutex;
      38             : static pthread_cond_t count_nb_thread;
      39             : static int counter = 0;
      40             : 
      41             : CPPUNIT_TEST_SUITE_REGISTRATION(test_SIP);
      42             : 
      43             : /*
      44             : return an error if all call are not successful
      45             : */
      46             : void*
      47           0 : sippThreadWithCount(void* str)
      48             : {
      49             :     // number of time we use the mutex. Lock the utilisation of counter
      50           0 :     pthread_mutex_lock(&count_mutex);
      51           0 :     counter++;
      52           0 :     pthread_mutex_unlock(&count_mutex);
      53             : 
      54             :     // display what is send on the parameter of the method
      55           0 :     std::string* command = (std::string*) (str);
      56             : 
      57           0 :     std::cout << "test_SIP: " << command << std::endl;
      58             : 
      59             :     // Set up the sipp instance in this thread in order to catch return value
      60             :     // 0: All calls were successful
      61             :     // 1: At least one call failed
      62             :     // 97: exit on internal command. Calls may have been processed
      63             :     // 99: Normal exit without calls processed
      64             :     // -1: Fatal error
      65             :     // -2: Fatal error binding a socket
      66           0 :     int i = system(command->c_str()); // c_str() retrieve the *char of the string
      67             : 
      68           0 :     CPPUNIT_ASSERT(i);
      69             : 
      70           0 :     pthread_mutex_lock(&count_mutex);
      71           0 :     counter--;
      72             :     // ???
      73           0 :     if (counter == 0)
      74           0 :         pthread_cond_signal(&count_nb_thread);
      75             : 
      76           0 :     pthread_mutex_unlock(&count_mutex);
      77             : 
      78           0 :     pthread_exit(NULL);
      79             : }
      80             : 
      81             : RAIIThread
      82           0 : sippThread(const std::string& command)
      83             : {
      84           0 :     return std::thread([command] {
      85           0 :         std::cout << "test_SIP: " << command << std::endl;
      86             : 
      87             :         // Set up the sipp instance in this thread in order to catch return value
      88             :         // 0: All calls were successful
      89             :         // 1: At least one call failed
      90             :         // 97: exit on internal command. Calls may have been processed
      91             :         // 99: Normal exit without calls processed
      92             :         // -1: Fatal error
      93             :         // -2: Fatal error binding a socket
      94           0 :         auto ret = system(command.c_str());
      95           0 :         std::cout << "test_SIP: Command executed by system returned: " << ret << std::endl;
      96           0 :     });
      97             : }
      98             : 
      99             : void
     100           1 : test_SIP::setUp()
     101             : {
     102           1 :     std::cout << "setup test SIP" << std::endl;
     103           1 :     pthread_mutex_lock(&count_mutex);
     104           1 :     counter = 0;
     105           1 :     pthread_mutex_unlock(&count_mutex);
     106             : 
     107           1 :     running_ = true;
     108           2 :     eventLoop_ = RAIIThread(std::thread([this] {
     109           1 :         while (running_) {
     110           0 :             std::this_thread::sleep_for(std::chrono::milliseconds(50));
     111             :         }
     112           2 :     }));
     113           1 : }
     114             : 
     115             : void
     116           1 : test_SIP::tearDown()
     117             : {
     118           1 :     running_ = false;
     119           1 :     eventLoop_.join();
     120             : 
     121             :     // in order to stop any currently running threads
     122           1 :     std::cout << "test_SIP: Clean all remaining sipp instances" << std::endl;
     123           1 :     int ret = system("killall sipp");
     124           1 :     if (ret)
     125             :         std::cout << "test_SIP: Error from system call, killall sipp"
     126           1 :                   << ", ret=" << ret << '\n';
     127           1 :     Manager::instance().callFactory.clear();
     128           1 : }
     129             : void
     130           1 : test_SIP::testSIPURI()
     131             : {
     132           1 :     std::cout << ">>>> test SIPURI <<<< " << '\n';
     133             : 
     134           1 :     auto foo = sip_utils::stripSipUriPrefix("<sip:17771234567@callcentric.com>"sv);
     135           1 :     CPPUNIT_ASSERT_EQUAL("17771234567"sv, foo);
     136           1 : }

Generated by: LCOV version 1.14