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