| 1 | /* |
| 2 | * Copyright (C) 2025 Teluu Inc. (http://www.teluu.com) |
| 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 2 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, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <iostream> |
| 20 | #include <thread> |
| 21 | #include <chrono> |
| 22 | #include <cassert> |
| 23 | #include <memory> |
| 24 | |
| 25 | #include "instant_messaging.hpp" |
| 26 | |
| 27 | #define THIS_FILE "instant_messaging.cpp" |
| 28 | |
| 29 | using namespace pj; |
| 30 | |
| 31 | // Definition of static member |
| 32 | TestState InstantMessagingTests::testState; |
| 33 | |
| 34 | ReceiverAccount::ReceiverAccount() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | ReceiverAccount::~ReceiverAccount() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void ReceiverAccount::onInstantMessage(OnInstantMessageParam &prm) |
| 43 | { |
| 44 | std::cout << "*** ReceiverAccount: Instant message received from " |
| 45 | << prm.fromUri << "\n"; |
| 46 | std::cout << "*** Message content: " << prm.msgBody << "\n"; |
| 47 | |
| 48 | deferredResponse = DeferredResponse(prm); |
| 49 | |
| 50 | InstantMessagingTests::testState.messageReceived = true; |
| 51 | InstantMessagingTests::testState.receivedMessage = prm.msgBody; |
| 52 | |
| 53 | std::cout << "*** ReceiverAccount: Message deferred, will respond later\n"; |
| 54 | } |
| 55 | |
| 56 | void ReceiverAccount::sendDeferredResponse(int statusCode, const std::string& reason) |
| 57 | { |
| 58 | try { |
| 59 | SendResponseParam respParam; |
| 60 | respParam.deferredResponse = std::move(deferredResponse); |
| 61 | respParam.code = statusCode; |
| 62 | respParam.reason = reason; |
| 63 | |
| 64 | sendResponse(respParam); |
| 65 | |
| 66 | InstantMessagingTests::testState.responseSent = true; |
| 67 | std::cout << "*** ReceiverAccount: Deferred response sent - " |
| 68 | << statusCode << " " << reason << "\n"; |
| 69 | } catch (Error& err) { |
| 70 | std::cout << "*** ReceiverAccount: Error sending deferred response: " |
| 71 | << err.info() << "\n"; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void SenderAccount::onInstantMessageStatus(OnInstantMessageStatusParam &prm) |
| 76 | { |
| 77 | std::cout << "*** SenderAccount: Message status update - " |
| 78 | << prm.code << " " << prm.reason << "\n"; |
| 79 | |
| 80 | InstantMessagingTests::testState.responseReceived = true; |
| 81 | InstantMessagingTests::testState.responseStatusCode = prm.code; |
| 82 | InstantMessagingTests::testState.responseReason = prm.reason; |
| 83 | } |
| 84 | |
| 85 | InstantMessagingTests::InstantMessagingTests() |
| 86 | { |
| 87 | ep.libCreate(); |
| 88 | |
| 89 | EpConfig epCfg; |
| 90 | epCfg.logConfig.level = 4; |
| 91 | epCfg.uaConfig.userAgent = "pjsua++-test"; |
| 92 | ep.libInit(epCfg); |
| 93 | |
| 94 | TransportConfig tcfg; |
| 95 | tcfg.port = 5060; |
| 96 | ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg); |
| 97 | |
| 98 | ep.libStart(); |
| 99 | std::cout << "*** PJSUA2 STARTED ***\n"; |
| 100 | } |
| 101 | |
| 102 | InstantMessagingTests::~InstantMessagingTests() |
| 103 | { |
| 104 | senderBuddy.reset(); |
| 105 | senderAcc.reset(); |
| 106 | receiverAcc.reset(); |
| 107 | |
| 108 | try { |
| 109 | ep.libDestroy(); |
| 110 | } catch (const pj::Error& e) { |
| 111 | // Log the error but don't rethrow from destructor |
| 112 | std::cerr << "Error during libDestroy: " << e.reason << std::endl; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void InstantMessagingTests::immediateResponse() |
| 117 | { |
| 118 | testState = TestState{}; |
| 119 | |
| 120 | AccountConfig receiverCfg; |
| 121 | receiverCfg.idUri = "sip:receiver@localhost:5060"; |
| 122 | receiverCfg.sipConfig.autoRespondSipMessage = PJ_TRUE; |
| 123 | |
| 124 | receiverAcc = std::unique_ptr<ReceiverAccount>(new ReceiverAccount()); |
| 125 | receiverAcc->create(receiverCfg); |
| 126 | std::cout << "*** Receiver account created: " << receiverCfg.idUri << "\n"; |
| 127 | |
| 128 | AccountConfig senderCfg; |
| 129 | senderCfg.idUri = "sip:sender@localhost:5060"; |
| 130 | senderCfg.sipConfig.autoRespondSipMessage = PJ_TRUE; |
| 131 | |
| 132 | senderAcc = std::unique_ptr<SenderAccount>(new SenderAccount()); |
| 133 | senderAcc->create(senderCfg); |
| 134 | std::cout << "*** Sender account created: " << senderCfg.idUri << "\n"; |
| 135 | |
| 136 | std::cout << "\n=== TEST STEP 1: Creating buddy and sending SIP MESSAGE ===\n"; |
| 137 | |
| 138 | BuddyConfig buddyCfg; |
| 139 | buddyCfg.uri = "sip:receiver@localhost:5060"; |
| 140 | buddyCfg.subscribe = false; |
| 141 | buddyCfg.subscribe_dlg_event = false; |
| 142 | |
| 143 | senderBuddy = std::unique_ptr<Buddy>(new Buddy()); |
| 144 | senderBuddy->create(*senderAcc, buddyCfg); |
| 145 | |
| 146 | const std::string testMessage = "Hello world!"; |
| 147 | |
| 148 | SendInstantMessageParam msgParam; |
| 149 | msgParam.content = testMessage; |
| 150 | msgParam.contentType = "text/plain"; |
| 151 | |
| 152 | senderBuddy->sendInstantMessage(msgParam); |
| 153 | std::cout << "*** SenderAccount: Message sent via buddy - content: " << testMessage << "\n"; |
| 154 | |
| 155 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 156 | |
| 157 | assert(testState.messageReceived == true); |
| 158 | assert(testState.receivedMessage == testMessage); |
| 159 | assert(testState.responseSent == false); |
| 160 | assert(testState.responseReceived == true); |
| 161 | assert(testState.responseStatusCode == 200); |
| 162 | assert(testState.responseReason == "OK"); |
| 163 | |
| 164 | std::cout << "✓ Immediate response successfully received\n"; |
| 165 | std::cout << "✓ Response code: " << testState.responseStatusCode << "\n"; |
| 166 | std::cout << "✓ Response reason: " << testState.responseReason << "\n"; |
| 167 | |
| 168 | } |
| 169 | |
| 170 | void InstantMessagingTests::deferredResponse() |
| 171 | { |
| 172 | testState = TestState{}; |
| 173 | |
| 174 | AccountConfig receiverCfg; |
| 175 | receiverCfg.idUri = "sip:receiver@localhost:5060"; |
| 176 | receiverCfg.sipConfig.autoRespondSipMessage = PJ_FALSE; |
| 177 | |
| 178 | receiverAcc = std::unique_ptr<ReceiverAccount>(new ReceiverAccount()); |
| 179 | receiverAcc->create(receiverCfg); |
| 180 | std::cout << "*** Receiver account created: " << receiverCfg.idUri << "\n"; |
| 181 | |
| 182 | AccountConfig senderCfg; |
| 183 | senderCfg.idUri = "sip:sender@localhost:5060"; |
| 184 | senderCfg.sipConfig.autoRespondSipMessage = PJ_FALSE; |
| 185 | |
| 186 | senderAcc = std::unique_ptr<SenderAccount>(new SenderAccount()); |
| 187 | senderAcc->create(senderCfg); |
| 188 | std::cout << "*** Sender account created: " << senderCfg.idUri << "\n"; |
| 189 | |
| 190 | std::cout << "\n=== TEST STEP 1: Creating buddy and sending SIP MESSAGE ===\n"; |
| 191 | |
| 192 | BuddyConfig buddyCfg; |
| 193 | buddyCfg.uri = "sip:receiver@localhost:5060"; |
| 194 | buddyCfg.subscribe = false; |
| 195 | buddyCfg.subscribe_dlg_event = false; |
| 196 | |
| 197 | senderBuddy = std::unique_ptr<Buddy>(new Buddy()); |
| 198 | senderBuddy->create(*senderAcc, buddyCfg); |
| 199 | |
| 200 | const std::string testMessage = "Hello world!"; |
| 201 | |
| 202 | SendInstantMessageParam msgParam; |
| 203 | msgParam.content = testMessage; |
| 204 | msgParam.contentType = "text/plain"; |
| 205 | |
| 206 | senderBuddy->sendInstantMessage(msgParam); |
| 207 | std::cout << "*** SenderAccount: Message sent via buddy - content: " << testMessage << "\n"; |
| 208 | |
| 209 | // Wait for message to be received |
| 210 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 211 | |
| 212 | assert(testState.messageReceived == true); |
| 213 | assert(testState.receivedMessage == testMessage); |
| 214 | std::cout << "✓ Message successfully received by receiver account\n"; |
| 215 | |
| 216 | std::cout << "\n=== TEST STEP 2: Sending deferred response ===\n"; |
| 217 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 218 | |
| 219 | const int responseCode = 200; |
| 220 | const std::string responseReason = "OK"; |
| 221 | receiverAcc->sendDeferredResponse(responseCode, responseReason); |
| 222 | |
| 223 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 224 | |
| 225 | assert(testState.responseSent == true); |
| 226 | assert(testState.responseReceived == true); |
| 227 | assert(testState.responseStatusCode == responseCode); |
| 228 | assert(testState.responseReason == responseReason); |
| 229 | |
| 230 | std::cout << "✓ Deferred response successfully sent and received\n"; |
| 231 | std::cout << "✓ Response code: " << testState.responseStatusCode << "\n"; |
| 232 | std::cout << "✓ Response reason: " << testState.responseReason << "\n"; |
| 233 | } |