blob: 2a75f7b91bc6782c3f03358afc864a4fcd96426b [file] [log] [blame]
Adrien Béraudefe27372023-05-27 18:56:29 -04001/*
Adrien Béraudcb753622023-07-17 22:32:49 -04002 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
Adrien Béraudefe27372023-05-27 18:56:29 -04003 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraudefe27372023-05-27 18:56:29 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraudefe27372023-05-27 18:56:29 -04007 * (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
Adrien Béraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraudefe27372023-05-27 18:56:29 -040012 * 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 */
Morteza Namvar82960b32023-07-04 17:08:22 -040017#include <iostream>
18#include <filesystem>
19
20#include <opendht/log.h>
21
Adrien Béraudefe27372023-05-27 18:56:29 -040022#include <cppunit/TestAssert.h>
23#include <cppunit/TestFixture.h>
24#include <cppunit/extensions/HelperMacros.h>
25
Morteza Namvar82960b32023-07-04 17:08:22 -040026#include <asio/executor_work_guard.hpp>
27#include <asio/io_context.hpp>
28
Adrien Béraudefe27372023-05-27 18:56:29 -040029#include <condition_variable>
30
31#include "connectionmanager.h"
32#include "multiplexed_socket.h"
33#include "test_runner.h"
Morteza Namvar82960b32023-07-04 17:08:22 -040034#include "certstore.h"
Adrien Béraudefe27372023-05-27 18:56:29 -040035
36using namespace std::literals::chrono_literals;
37
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040038namespace dhtnet {
Adrien Béraudefe27372023-05-27 18:56:29 -040039namespace test {
40
Morteza Namvar82960b32023-07-04 17:08:22 -040041struct ConnectionHandler {
42 dht::crypto::Identity id;
43 std::shared_ptr<Logger> logger;
44 std::shared_ptr<tls::CertificateStore> certStore;
45 std::shared_ptr<dht::DhtRunner> dht;
46 std::shared_ptr<ConnectionManager> connectionManager;
47 std::shared_ptr<asio::io_context> ioContext;
48 std::thread ioContextRunner;
49};
50
Adrien Béraudefe27372023-05-27 18:56:29 -040051class ConnectionManagerTest : public CppUnit::TestFixture
52{
53public:
54 ConnectionManagerTest() {}
55 ~ConnectionManagerTest() { }
56 static std::string name() { return "ConnectionManager"; }
57 void setUp();
58 void tearDown();
59
Morteza Namvar82960b32023-07-04 17:08:22 -040060 std::unique_ptr<ConnectionHandler> alice;
61 std::unique_ptr<ConnectionHandler> bob;
Adrien Béraudefe27372023-05-27 18:56:29 -040062
Morteza Namvar82960b32023-07-04 17:08:22 -040063//Create a lock to be used in the test units
64 std::mutex mtx;
65 std::shared_ptr<asio::io_context> ioContext;
66 std::thread ioContextRunner;
67 std::shared_ptr<Logger> logger;
68 std::unique_ptr<IceTransportFactory> factory;
Adrien Béraudefe27372023-05-27 18:56:29 -040069private:
Morteza Namvar82960b32023-07-04 17:08:22 -040070
71 std::unique_ptr<ConnectionHandler> setupHandler(const std::string& name);
72
73 void testConnectDevice();
74 // void testAcceptConnection();
75 // void testMultipleChannels();
76 // void testMultipleChannelsOneDeclined();
77 // void testMultipleChannelsSameName();
78 // void testDeclineConnection();
79 // void testSendReceiveData();
80 // void testAcceptsICERequest();
81 // void testDeclineICERequest();
82 // void testChannelRcvShutdown();
83 // void testChannelSenderShutdown();
84 // void testCloseConnectionWith();
85 // void testShutdownCallbacks();
86 // void testFloodSocket();
87 // void testDestroyWhileSending();
88 // void testIsConnecting();
89 // void testCanSendBeacon();
90 // void testCannotSendBeacon();
91 // void testConnectivityChangeTriggerBeacon();
92 // void testOnNoBeaconTriggersShutdown();
93 // void testShutdownWhileNegotiating();
Adrien Béraudefe27372023-05-27 18:56:29 -040094
95 CPPUNIT_TEST_SUITE(ConnectionManagerTest);
Morteza Namvar82960b32023-07-04 17:08:22 -040096
97 // CPPUNIT_TEST(testAcceptsICERequest);
98 // CPPUNIT_TEST(testDeclineICERequest);
99
100 CPPUNIT_TEST(testConnectDevice);
101
102 // CPPUNIT_TEST(testIsConnecting);
103
104 // CPPUNIT_TEST(testAcceptConnection);
105 // CPPUNIT_TEST(testDeclineConnection);
106
107 // CPPUNIT_TEST(testMultipleChannels);
108 // CPPUNIT_TEST(testMultipleChannelsOneDeclined);
109 // CPPUNIT_TEST(testMultipleChannelsSameName);
110
111 // CPPUNIT_TEST(testSendReceiveData);
112
113 // CPPUNIT_TEST(testChannelRcvShutdown);
114 // CPPUNIT_TEST(testChannelSenderShutdown);
115
116 // CPPUNIT_TEST(testCloseConnectionWith);
117 // CPPUNIT_TEST(testShutdownCallbacks);
118 // CPPUNIT_TEST(testFloodSocket);
119 // CPPUNIT_TEST(testDestroyWhileSending);
120
121 // CPPUNIT_TEST(testCanSendBeacon);
122 // CPPUNIT_TEST(testCannotSendBeacon);
123 // CPPUNIT_TEST(testConnectivityChangeTriggerBeacon);
124 // CPPUNIT_TEST(testOnNoBeaconTriggersShutdown);
125 // CPPUNIT_TEST(testShutdownWhileNegotiating);
Adrien Béraudefe27372023-05-27 18:56:29 -0400126 CPPUNIT_TEST_SUITE_END();
127};
128
129CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConnectionManagerTest, ConnectionManagerTest::name());
130
Morteza Namvar82960b32023-07-04 17:08:22 -0400131std::unique_ptr<ConnectionHandler>
132ConnectionManagerTest::setupHandler(const std::string& name) {
133 auto h = std::make_unique<ConnectionHandler>();
Adrien Béraudc631a832023-07-26 22:19:00 -0400134 auto ca = dht::crypto::generateIdentity("ca");
135 h->id = dht::crypto::generateIdentity(name, ca);
Morteza Namvar82960b32023-07-04 17:08:22 -0400136 h->logger = logger;
137 h->certStore = std::make_shared<tls::CertificateStore>(name, h->logger);
138 h->ioContext = std::make_shared<asio::io_context>();
139 h->ioContextRunner = std::thread([context = h->ioContext]() {
140 try {
141 auto work = asio::make_work_guard(*context);
142 context->run();
143 } catch (const std::exception& ex) {
144 //print the error;
145 }
146 });
147
148 dht::DhtRunner::Config dhtConfig;
149 dhtConfig.dht_config.id = h->id;
150 dhtConfig.threaded = true;
151
152 dht::DhtRunner::Context dhtContext;
153 dhtContext.certificateStore = [c=h->certStore](const dht::InfoHash& pk_id) {
154 std::vector<std::shared_ptr<dht::crypto::Certificate>> ret;
155 if (auto cert = c->getCertificate(pk_id.toString()))
156 ret.emplace_back(std::move(cert));
157 return ret;
158 };
159 dhtContext.logger = h->logger;
160
161 h->dht = std::make_shared<dht::DhtRunner>();
162 h->dht->run(dhtConfig, std::move(dhtContext));
Adrien Béraude8541392023-07-27 15:48:04 -0400163 h->dht->bootstrap("bootstrap.jami.net");
Morteza Namvar82960b32023-07-04 17:08:22 -0400164
165 auto config = std::make_shared<ConnectionManager::Config>();
166 config->dht = h->dht;
167 config->id = h->id;
168 config->ioContext = h->ioContext;
169 config->factory = factory.get();
170 config->logger = logger;
171 config->certStore = h->certStore.get();
172
173 std::filesystem::path currentPath = std::filesystem::current_path();
174 std::filesystem::path tempDirPath = currentPath / "temp";
175
176 config->cachePath = tempDirPath.string();
177
178 h->connectionManager = std::make_shared<ConnectionManager>(config);
179 h->connectionManager->onICERequest([](const DeviceId&) {
180 return true;
181 });
182
183 return h;
184}
185
Adrien Béraudefe27372023-05-27 18:56:29 -0400186void
187ConnectionManagerTest::setUp()
188{
Morteza Namvar82960b32023-07-04 17:08:22 -0400189 logger = dht::log::getStdLogger();
Adrien Béraudc631a832023-07-26 22:19:00 -0400190
191 logger->debug("Using PJSIP version {} for {}", pj_get_version(), PJ_OS_NAME);
192 logger->debug("Using GnuTLS version {}", gnutls_check_version(nullptr));
193 logger->debug("Using OpenDHT version {}", dht::version());
194
Morteza Namvar82960b32023-07-04 17:08:22 -0400195 ioContext = std::make_shared<asio::io_context>();
196 ioContextRunner = std::thread([context = ioContext]() {
197 try {
198 auto work = asio::make_work_guard(*context);
199 context->run();
200 } catch (const std::exception& ex) {
201 //print the error;
202 }
203 });
Adrien Béraud89933c12023-07-26 14:53:30 -0400204 factory = std::make_unique<IceTransportFactory>(logger);
Morteza Namvar82960b32023-07-04 17:08:22 -0400205 alice = setupHandler("alice");
206 bob = setupHandler("bob");
Adrien Béraudefe27372023-05-27 18:56:29 -0400207}
208
Morteza Namvar82960b32023-07-04 17:08:22 -0400209/*
210 auto identifier = bobId.first->getPublicKey().getLongId();
211 auto identifier = bobId.second->getLongId();
212 auto identifier = bobId.second->getPublicKey().getLongId();
213*/
214
215
Adrien Béraudefe27372023-05-27 18:56:29 -0400216void
217ConnectionManagerTest::tearDown()
218{
Morteza Namvar82960b32023-07-04 17:08:22 -0400219 //wait_for_removal_of({aliceId, bobId}); //?????????????????????????????????
220 ioContext->stop();
221 if (ioContextRunner.joinable())
222 ioContextRunner.join();
Adrien Béraudefe27372023-05-27 18:56:29 -0400223}
224
Morteza Namvar82960b32023-07-04 17:08:22 -0400225/*
Adrien Béraudefe27372023-05-27 18:56:29 -0400226void
227ConnectionManagerTest::testConnectDevice()
228{
Morteza Namvar82960b32023-07-04 17:08:22 -0400229 auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
Adrien Béraudefe27372023-05-27 18:56:29 -0400230 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
Morteza Namvar82960b32023-07-04 17:08:22 -0400231
Adrien Béraudefe27372023-05-27 18:56:29 -0400232 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
233
234 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
235 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
236
237 std::mutex mtx;
238 std::unique_lock<std::mutex> lk {mtx};
239 std::condition_variable cv, cvReceive;
240 bool successfullyConnected = false;
241 bool successfullyReceive = false;
242
243 bobAccount->connectionManager().onChannelRequest(
244 [&successfullyReceive, &cvReceive](const std::shared_ptr<dht::crypto::Certificate>&,
245 const std::string& name) {
246 successfullyReceive = name == "git://*";
247 cvReceive.notify_one();
248 return true;
249 });
250
251 aliceAccount->connectionManager().connectDevice(bobDeviceId,
252 "git://*",
253 [&](std::shared_ptr<ChannelSocket> socket,
254 const DeviceId&) {
255 if (socket) {
256 successfullyConnected = true;
257 }
258 cv.notify_one();
259 });
Morteza Namvar82960b32023-07-04 17:08:22 -0400260
Adrien Béraudefe27372023-05-27 18:56:29 -0400261 CPPUNIT_ASSERT(cvReceive.wait_for(lk, 60s, [&] { return successfullyReceive; }));
Adrien Béraudefe27372023-05-27 18:56:29 -0400262 CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] { return successfullyConnected; }));
Morteza Namvar82960b32023-07-04 17:08:22 -0400263
264
Adrien Béraudefe27372023-05-27 18:56:29 -0400265}
Morteza Namvar82960b32023-07-04 17:08:22 -0400266*/
Adrien Béraudefe27372023-05-27 18:56:29 -0400267
268void
Morteza Namvar82960b32023-07-04 17:08:22 -0400269ConnectionManagerTest::testConnectDevice()
Adrien Béraudefe27372023-05-27 18:56:29 -0400270{
Morteza Namvar82960b32023-07-04 17:08:22 -0400271 std::unique_lock<std::mutex> lock {mtx};
272 std::condition_variable bobConVar;
273 bool isBobRecvChanlReq = false;
Adrien Béraudefe27372023-05-27 18:56:29 -0400274
Morteza Namvar82960b32023-07-04 17:08:22 -0400275 alice->connectionManager->onDhtConnected(alice->id.first->getPublicKey());
276 bob->connectionManager->onDhtConnected(bob->id.first->getPublicKey());
Adrien Béraudefe27372023-05-27 18:56:29 -0400277
Morteza Namvar82960b32023-07-04 17:08:22 -0400278 bob->connectionManager->onChannelRequest([&isBobRecvChanlReq, &bobConVar]
279 (const std::shared_ptr<dht::crypto::Certificate>&, const std::string& name)
280 {
281 isBobRecvChanlReq = name == "dumyName";
282 bobConVar.notify_one();
283 return true;
284 });
Adrien Béraudefe27372023-05-27 18:56:29 -0400285
Morteza Namvar82960b32023-07-04 17:08:22 -0400286 std::condition_variable alicConVar;
287 bool isAlicConnected = false;
288 auto conctDevicCalBack = [&](std::shared_ptr<ChannelSocket> socket, const DeviceId&) {
289 if (socket) { isAlicConnected = true; }
290 alicConVar.notify_one();
291 };
Adrien Béraudefe27372023-05-27 18:56:29 -0400292
Morteza Namvar82960b32023-07-04 17:08:22 -0400293 alice->connectionManager->connectDevice(bob->id.second, "dumyName", conctDevicCalBack);
Adrien Béraudefe27372023-05-27 18:56:29 -0400294
Morteza Namvar82960b32023-07-04 17:08:22 -0400295 //Step 4: to check if Alice connected to Bob?
296 CPPUNIT_ASSERT(alicConVar.wait_for(lock, 60s, [&] { return isAlicConnected; }));
Adrien Béraudefe27372023-05-27 18:56:29 -0400297}
298
Adrien Béraudefe27372023-05-27 18:56:29 -0400299
Adrien Béraudefe27372023-05-27 18:56:29 -0400300
Morteza Namvar82960b32023-07-04 17:08:22 -0400301// /*
302// void
303// ConnectionManagerTest::testAcceptConnection()
304// {
305// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
306// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
307// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
Adrien Béraudefe27372023-05-27 18:56:29 -0400308
Morteza Namvar82960b32023-07-04 17:08:22 -0400309// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
310// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
Adrien Béraudefe27372023-05-27 18:56:29 -0400311
Morteza Namvar82960b32023-07-04 17:08:22 -0400312// std::mutex mtx;
313// std::unique_lock<std::mutex> lk {mtx};
314// std::condition_variable cv;
315// bool successfullyConnected = false;
316// bool successfullyReceive = false;
317// bool receiverConnected = false;
Adrien Béraudefe27372023-05-27 18:56:29 -0400318
Morteza Namvar82960b32023-07-04 17:08:22 -0400319// bobAccount->connectionManager().onChannelRequest(
320// [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
321// const std::string& name) {
322// successfullyReceive = name == "git://*";
323// return true;
324// });
Adrien Béraudefe27372023-05-27 18:56:29 -0400325
Morteza Namvar82960b32023-07-04 17:08:22 -0400326// bobAccount->connectionManager().onConnectionReady(
327// [&receiverConnected](const DeviceId&,
328// const std::string& name,
329// std::shared_ptr<ChannelSocket> socket) {
330// receiverConnected = socket && (name == "git://*");
331// });
Adrien Béraudefe27372023-05-27 18:56:29 -0400332
Morteza Namvar82960b32023-07-04 17:08:22 -0400333// aliceAccount->connectionManager().connectDevice(bobDeviceId,
334// "git://*",
335// [&](std::shared_ptr<ChannelSocket> socket,
336// const DeviceId&) {
337// if (socket) {
338// successfullyConnected = true;
339// }
340// cv.notify_one();
341// });
Adrien Béraudefe27372023-05-27 18:56:29 -0400342
Morteza Namvar82960b32023-07-04 17:08:22 -0400343// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
344// return successfullyReceive && successfullyConnected && receiverConnected;
345// }));
346// }
347// */
348// void
349// ConnectionManagerTest::testAcceptConnection()
350// {
Adrien Béraudefe27372023-05-27 18:56:29 -0400351
Morteza Namvar82960b32023-07-04 17:08:22 -0400352// std::condition_variable cv;
353// bool successfullyConnected = false;
354// bool successfullyReceive = false;
Adrien Béraudefe27372023-05-27 18:56:29 -0400355
Morteza Namvar82960b32023-07-04 17:08:22 -0400356// auto chanlReqCalBack = [&successfullyReceive]
357// (const std::shared_ptr<dht::crypto::Certificate>&, const std::string& name)
358// {
359// successfullyReceive = name == "dumyname";
360// return true;
361// };
Adrien Béraudefe27372023-05-27 18:56:29 -0400362
Morteza Namvar82960b32023-07-04 17:08:22 -0400363// bobConMngr.onChannelRequest(chanlReqCalBack);
Adrien Béraudefe27372023-05-27 18:56:29 -0400364
Adrien Béraudefe27372023-05-27 18:56:29 -0400365
Adrien Béraudefe27372023-05-27 18:56:29 -0400366
Morteza Namvar82960b32023-07-04 17:08:22 -0400367// bool receiverConnected = false;
Adrien Béraudefe27372023-05-27 18:56:29 -0400368
Morteza Namvar82960b32023-07-04 17:08:22 -0400369// auto conctReadyCalBack =[&receiverConnected]
370// (const DeviceId& deviceId, const std::string& name, std::shared_ptr<ChannelSocket> socket)
371// {
372// receiverConnected = socket && (name == "dumyname");
373// };
374
375// bobConMngr.onConnectionReady(conctReadyCalBack);
Adrien Béraudefe27372023-05-27 18:56:29 -0400376
Adrien Béraudefe27372023-05-27 18:56:29 -0400377
Morteza Namvar82960b32023-07-04 17:08:22 -0400378// auto conctDevicCalBack =[&]
379// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
380// {
381// if (socket)
382// successfullyConnected = true;
383// cv.notify_one();
384// };
Adrien Béraudefe27372023-05-27 18:56:29 -0400385
Morteza Namvar82960b32023-07-04 17:08:22 -0400386// alicConMngr.connectDevice(bobDevicId, "dumyname", conctDevicCalBack);
Adrien Béraudefe27372023-05-27 18:56:29 -0400387
Adrien Béraudefe27372023-05-27 18:56:29 -0400388
Adrien Béraudefe27372023-05-27 18:56:29 -0400389
Morteza Namvar82960b32023-07-04 17:08:22 -0400390// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
391// return successfullyReceive && successfullyConnected && receiverConnected;
392// }));
Adrien Béraudefe27372023-05-27 18:56:29 -0400393
Morteza Namvar82960b32023-07-04 17:08:22 -0400394// }
Adrien Béraudefe27372023-05-27 18:56:29 -0400395
Morteza Namvar82960b32023-07-04 17:08:22 -0400396// /*
397// void
398// ConnectionManagerTest::testDeclineConnection()
399// {
400// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
401// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
402// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
403
404// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
405// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
406
407// std::mutex mtx;
408// std::unique_lock<std::mutex> lk {mtx};
409// std::condition_variable cv;
410// bool successfullyConnected = false;
411// bool successfullyReceive = false;
412// bool receiverConnected = false;
413
414// bobAccount->connectionManager().onChannelRequest(
415// [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
416// const std::string&) {
417// successfullyReceive = true;
418// return false;
419// });
420
421// bobAccount->connectionManager().onConnectionReady(
422// [&receiverConnected](const DeviceId&,
423// const std::string&,
424// std::shared_ptr<ChannelSocket> socket) {
425// if (socket)
426// receiverConnected = true;
427// });
428
429// aliceAccount->connectionManager().connectDevice(bobDeviceId,
430// "git://*",
431// [&](std::shared_ptr<ChannelSocket> socket,
432// const DeviceId&) {
433// if (socket) {
434// successfullyConnected = true;
435// }
436// cv.notify_one();
437// });
438// cv.wait_for(lk, 30s);
439// CPPUNIT_ASSERT(successfullyReceive);
440// CPPUNIT_ASSERT(!successfullyConnected);
441// CPPUNIT_ASSERT(!receiverConnected);
442// }
443// */
444// void
445// ConnectionManagerTest::testDeclineConnection()
446// {
447
448// std::condition_variable cv;
449// bool successfullyConnected = false;
450// bool successfullyReceive = false;
451// bool receiverConnected = false;
452
453
454// auto chanlReqCalBack = [&successfullyReceive]
455// (const std::shared_ptr<dht::crypto::Certificate>&, const std::string&)
456// {
457// successfullyReceive = true;
458// return false; //this is the point??????????????????????????????????????????????????????
459// };
460
461// bobConMngr.onChannelRequest(chanlReqCalBack);
462
463
464// auto conctReadyCalBack =[&receiverConnected]
465// (const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket)
466// {
467// if (socket)
468// receiverConnected = true;
469// };
470
471// bobConMngr.onConnectionReady(conctReadyCalBack);
472
473
474// auto conctDevicCalBack =[&]
475// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
476// {
477// if (socket)
478// successfullyConnected = true;
479// cv.notify_one();
480// };
481
482
483// alicConMngr.connectDevice(bobDevicId, "dumyname", conctDevicCalBack);
484
485
486// cv.wait_for(lk, 30s);
487// CPPUNIT_ASSERT(successfullyReceive);
488// CPPUNIT_ASSERT(!successfullyConnected);
489// CPPUNIT_ASSERT(!receiverConnected);
490// }
491
492
493
494
495// /*
496// void
497// ConnectionManagerTest::testMultipleChannels()
498// {
499// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
500// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
501// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
502
503// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
504// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
505
506// std::mutex mtx;
507// std::unique_lock<std::mutex> lk {mtx};
508// std::condition_variable cv;
509// bool successfullyConnected = false;
510// bool successfullyConnected2 = false;
511// int receiverConnected = 0;
512
513// bobAccount->connectionManager().onChannelRequest(
514// [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
515
516// bobAccount->connectionManager().onConnectionReady(
517// [&receiverConnected](const DeviceId&,
518// const std::string&,
519// std::shared_ptr<ChannelSocket> socket) {
520// if (socket)
521// receiverConnected += 1;
522// });
523
524// aliceAccount->connectionManager().connectDevice(bobDeviceId,
525// "git://*",
526// [&](std::shared_ptr<ChannelSocket> socket,
527// const DeviceId&) {
528// if (socket) {
529// successfullyConnected = true;
530// }
531// cv.notify_one();
532// });
533
534// aliceAccount->connectionManager().connectDevice(bobDeviceId,
535// "sip://*",
536// [&](std::shared_ptr<ChannelSocket> socket,
537// const DeviceId&) {
538// if (socket) {
539// successfullyConnected2 = true;
540// }
541// cv.notify_one();
542// });
543
544// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
545// return successfullyConnected && successfullyConnected2 && receiverConnected == 2;
546// }));
547// CPPUNIT_ASSERT(aliceAccount->connectionManager().activeSockets() == 1);
548// }
549// */
550// void
551// ConnectionManagerTest::testMultipleChannels()
552// {
553// std::condition_variable cv;
554// bool successfullyConnected = false;
555// bool successfullyConnected2 = false;
556// int receiverConnected = 0;
557
558// auto chanlReqCalBack = []
559// (const std::shared_ptr<dht::crypto::Certificate>&, const std::string&)
560// {
561// return true;
562// };
563
564// bobConMngr.onChannelRequest(chanlReqCalBack);
565
566
567// auto conctReadyCalBack =[&receiverConnected]
568// (const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket)
569// {
570// if (socket)
571// receiverConnected += 1;
572// };
573
574// bobConMngr.onConnectionReady(conctReadyCalBack);
575
576
577// auto conctDevicCalBack =[&]
578// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
579// {
580// if (socket)
581// successfullyConnected = true;
582// cv.notify_one();
583// };
584
585// alicConMngr.connectDevice(bobDevicId, "git://*", conctDevicCalBack);
586
587
588// auto conctDevicCalBack =[&]
589// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
590// {
591// if (socket)
592// successfullyConnected2 = true;
593// cv.notify_one();
594// };
595
596// alicConMngr.connectDevice(bobDevicId, "sip://*", conctDevicCalBack);
597
598
599// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
600// return successfullyConnected && successfullyConnected2 && receiverConnected == 2;
601// }));
602// CPPUNIT_ASSERT(alicConMngr.activeSockets() == 1);
603// }
604
605// /*void
606// ConnectionManagerTest::testMultipleChannelsOneDeclined()
607// {
608// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
609// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
610// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
611
612// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
613// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
614
615// std::mutex mtx;
616// std::unique_lock<std::mutex> lk {mtx};
617// std::condition_variable cv;
618// bool successfullyNotConnected = false;
619// bool successfullyConnected2 = false;
620// int receiverConnected = 0;
621
622// bobAccount->connectionManager().onChannelRequest(
623// [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string& name) {
624// if (name == "git://*")
625// return false;
626// return true;
627// });
628
629// bobAccount->connectionManager().onConnectionReady(
630// [&](const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket) {
631// if (socket)
632// receiverConnected += 1;
633// cv.notify_one();
634// });
635
636// aliceAccount->connectionManager().connectDevice(bobDeviceId,
637// "git://*",
638// [&](std::shared_ptr<ChannelSocket> socket,
639// const DeviceId&) {
640// if (!socket)
641// successfullyNotConnected = true;
642// cv.notify_one();
643// });
644
645// aliceAccount->connectionManager().connectDevice(bobDeviceId,
646// "sip://*",
647// [&](std::shared_ptr<ChannelSocket> socket,
648// const DeviceId&) {
649// if (socket)
650// successfullyConnected2 = true;
651// cv.notify_one();
652// });
653
654// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
655// return successfullyNotConnected && successfullyConnected2 && receiverConnected == 1;
656// }));
657// CPPUNIT_ASSERT(aliceAccount->connectionManager().activeSockets() == 1);
658// }
659// */
660// void
661// ConnectionManagerTest::testMultipleChannelsOneDeclined()
662// {
663
664// std::condition_variable cv;
665// bool successfullyNotConnected = false;
666// bool successfullyConnected2 = false;
667// int receiverConnected = 0;
668
669
670// auto chanlReqCalBack = []
671// (const std::shared_ptr<dht::crypto::Certificate>&, const std::string& name)
672// {
673// if (name == "git://*")
674// return false;
675// return true;
676// };
677
678// bobConMngr.onChannelRequest(chanlReqCalBack);
679
680
681// auto conctReadyCalBack =[&receiverConnected]
682// (const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket)
683// {
684// if (socket)
685// receiverConnected += 1;
686// };
687
688// bobConMngr.onConnectionReady(conctReadyCalBack);
689
690
691// auto conctDevicCalBack =[&]
692// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
693// {
694// if (socket)
695// successfullyConnected = true;
696// cv.notify_one();
697// };
698
699// alicConMngr.connectDevice(bobDevicId, "git://*", conctDevicCalBack);
700
701
702// auto conctDevicCalBack =[&]
703// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
704// {
705// if (socket)
706// successfullyConnected2 = true;
707// cv.notify_one();
708// };
709
710// alicConMngr.connectDevice(bobDevicId, "sip://*", conctDevicCalBack);
711
712
713
714// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
715// return successfullyNotConnected && successfullyConnected2 && receiverConnected == 1;
716// }));
717// CPPUNIT_ASSERT(aliceAccount->connectionManager().activeSockets() == 1);
718// }
719
720// /*
721// void
722// ConnectionManagerTest::testMultipleChannelsSameName()
723// {
724// std::condition_variable cv;
725// bool successfullyConnected = false;
726// bool successfullyConnected2 = false;
727// int receiverConnected = 0;
728
729// bobAccount->connectionManager().onChannelRequest(
730// [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
731
732// bobAccount->connectionManager().onConnectionReady(
733// [&receiverConnected](const DeviceId&,
734// const std::string&,
735// std::shared_ptr<ChannelSocket> socket) {
736// if (socket)
737// receiverConnected += 1;
738// });
739
740// aliceAccount->connectionManager().connectDevice(bobDeviceId,
741// "git://*",
742// [&](std::shared_ptr<ChannelSocket> socket,
743// const DeviceId&) {
744// if (socket) {
745// successfullyConnected = true;
746// }
747// cv.notify_one();
748// });
749
750// // We can open two sockets with the same name, it will be two different channel
751// aliceAccount->connectionManager().connectDevice(bobDeviceId,
752// "git://*",
753// [&](std::shared_ptr<ChannelSocket> socket,
754// const DeviceId&) {
755// if (socket) {
756// successfullyConnected2 = true;
757// }
758// cv.notify_one();
759// });
760
761// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
762// return successfullyConnected && successfullyConnected2 && receiverConnected == 2;
763// }));
764// }
765// */
766// void
767// ConnectionManagerTest::testMultipleChannelsSameName()
768// {
769// std::condition_variable cv;
770// bool successfullyConnected = false;
771// bool successfullyConnected2 = false;
772// int receiverConnected = 0;
773
774
775
776// auto chanlReqCalBack = []
777// (const std::shared_ptr<dht::crypto::Certificate>&, const std::string& name)
778// {
779// return true;
780// };
781
782// bobConMngr.onChannelRequest(chanlReqCalBack);
783
784
785// auto conctReadyCalBack =[&receiverConnected]
786// (const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket)
787// {
788// if (socket)
789// receiverConnected += 1;
790// };
791
792// bobConMngr.onConnectionReady(conctReadyCalBack);
793
794
795// auto conctDevicCalBack =[&]
796// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
797// {
798// if (socket)
799// successfullyConnected = true;
800// cv.notify_one();
801// };
802
803// alicConMngr.connectDevice(bobDevicId, "git://*", conctDevicCalBack);
804
805
806// auto conctDevicCalBack =[&]
807// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
808// {
809// if (socket)
810// successfullyConnected2 = true;
811// cv.notify_one();
812// };
813
814// alicConMngr.connectDevice(bobDevicId, "sip://*", conctDevicCalBack);
815
816
817// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
818// return successfullyConnected && successfullyConnected2 && receiverConnected == 2;
819// }));
820// }
821
822// //Explain this more
823// /*
824// void
825// ConnectionManagerTest::testSendReceiveData()
826// {
827// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
828// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
829// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
830
831// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
832// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
833
834// std::mutex mtx;
835// std::unique_lock<std::mutex> lk {mtx};
836// std::condition_variable cv;
837// std::atomic_int events(0);
838// bool successfullyConnected = false, successfullyConnected2 = false, successfullyReceive = false,
839// receiverConnected = false;
840// const uint8_t buf_other[] = {0x64, 0x65, 0x66, 0x67};
841// const uint8_t buf_test[] = {0x68, 0x69, 0x70, 0x71};
842// bool dataOk = false, dataOk2 = false;
843
844// bobAccount->connectionManager().onChannelRequest(
845// [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
846// const std::string&) {
847// successfullyReceive = true;
848// return true;
849// });
850
851// bobAccount->connectionManager().onConnectionReady(
852// [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
853// if (socket && (name == "test" || name == "other")) {
854// receiverConnected = true;
855// std::error_code ec;
856// auto res = socket->waitForData(std::chrono::milliseconds(5000), ec);
857// if (res == 4) {
858// uint8_t buf[4];
859// socket->read(&buf[0], 4, ec);
860// if (name == "test")
861// dataOk = std::equal(std::begin(buf), std::end(buf), std::begin(buf_test));
862// else
863// dataOk2 = std::equal(std::begin(buf), std::end(buf), std::begin(buf_other));
864// events++;
865// cv.notify_one();
866// }
867// }
868// });
869
870// aliceAccount->connectionManager().connectDevice(bobDeviceId,
871// "test",
872// [&](std::shared_ptr<ChannelSocket> socket,
873// const DeviceId&) {
874// if (socket) {
875// successfullyConnected = true;
876// std::error_code ec;
877// socket->write(&buf_test[0], 4, ec);
878// }
879// events++;
880// cv.notify_one();
881// });
882
883// aliceAccount->connectionManager().connectDevice(bobDeviceId,
884// "other",
885// [&](std::shared_ptr<ChannelSocket> socket,
886// const DeviceId&) {
887// if (socket) {
888// successfullyConnected2 = true;
889// std::error_code ec;
890// socket->write(&buf_other[0], 4, ec);
891// }
892// events++;
893// cv.notify_one();
894// });
895
896// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
897// return events == 4 && successfullyReceive && successfullyConnected && successfullyConnected2
898// && dataOk && dataOk2;
899// }));
900// }
901// */
902// void
903// ConnectionManagerTest::testSendReceiveData()
904// {
905
906// std::condition_variable cv;
907// std::atomic_int events(0);
908// bool successfullyConnected = false;
909// bool successfullyConnected2 = false;
910// bool successfullyReceive = false;
911// bool receiverConnected = false;
912
913// const uint8_t buf_other[] = {0x64, 0x65, 0x66, 0x67};
914// const uint8_t buf_test[] = {0x68, 0x69, 0x70, 0x71};
915// bool dataOk = false;
916// bool dataOk2 = false;
917
918
919// auto chanlReqCalBack = [&successfullyReceive]
920// (const std::shared_ptr<dht::crypto::Certificate>&, const std::string&)
921// {
922// successfullyReceive = true;
923// return true;
924// };
925
926// bobConMngr.onChannelRequest(chanlReqCalBack);
927
928
929// auto conctReadyCalBack =[&]
930// (const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket)
931// {
932// if (socket && (name == "test" || name == "other")) {
933// receiverConnected = true;
934// std::error_code ec;
935// auto res = socket->waitForData(std::chrono::milliseconds(5000), ec);
936// if (res == 4) {
937// uint8_t buf[4];
938// socket->read(&buf[0], 4, ec);
939// if (name == "test")
940// dataOk = std::equal(std::begin(buf), std::end(buf), std::begin(buf_test));
941// else
942// dataOk2 = std::equal(std::begin(buf), std::end(buf), std::begin(buf_other));
943// events++;
944// cv.notify_one();
945// }
946// }
947// };
948
949// bobConMngr.onConnectionReady(conctReadyCalBack);
950
951
952// auto conctDevicCalBack =[&]
953// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
954// {
955// if (socket) {
956// successfullyConnected = true;
957// std::error_code ec;
958// socket->write(&buf_test[0], 4, ec);
959// }
960// events++;
961// cv.notify_one();
962// };
963
964
965// alicConMngr.connectDevice(bobDevicId, "test", conctDevicCalBack);
966
967
968// auto conctDevicCalBack2 = [&]
969// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
970// {
971// if (socket) {
972// successfullyConnected2 = true;
973// std::error_code ec;
974// socket->write(&buf_other[0], 4, ec);
975// }
976// events++;
977// cv.notify_one();
978// };
979
980// alicConMngr.connectDevice(bobDevicId, "other", conctDevicCalBack2);
981
982
983// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
984// return events == 4 && successfullyReceive && successfullyConnected && successfullyConnected2
985// && dataOk && dataOk2;
986// }));
987// }
988
989
990
991// /* void
992// ConnectionManagerTest::testAcceptsICERequest()
993// {
994// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
995// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
996// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
997
998// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
999
1000// std::mutex mtx;
1001// std::unique_lock<std::mutex> lk {mtx};
1002// std::condition_variable cv;
1003// bool successfullyConnected = false;
1004// bool successfullyReceive = false;
1005// bool receiverConnected = false;
1006
1007// bobAccount->connectionManager().onChannelRequest(
1008// [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1009// bobAccount->connectionManager().onICERequest([&](const DeviceId&) {
1010// successfullyReceive = true;
1011// return true;
1012// });
1013
1014// bobAccount->connectionManager().onConnectionReady(
1015// [&receiverConnected](const DeviceId&,
1016// const std::string& name,
1017// std::shared_ptr<ChannelSocket> socket) {
1018// receiverConnected = socket && (name == "git://*");
1019// });
1020
1021// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1022// "git://*",
1023// [&](std::shared_ptr<ChannelSocket> socket,
1024// const DeviceId&) {
1025// if (socket) {
1026// successfullyConnected = true;
1027// }
1028// cv.notify_one();
1029// });
1030
1031// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] {
1032// return successfullyReceive && successfullyConnected && receiverConnected;
1033// }));
1034// } */
1035// void
1036// ConnectionManagerTest::testAcceptsICERequest()
1037// {
1038
1039// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1040
1041// std::mutex mtx;
1042// std::unique_lock<std::mutex> lk {mtx};
1043// std::condition_variable cv;
1044// bool successfullyConnected = false;
1045// bool successfullyReceive = false;
1046// bool receiverConnected = false;
1047
1048// bobAccount->connectionManager().onChannelRequest(
1049// [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1050
1051// bobAccount->connectionManager().onICERequest([&](const DeviceId&) {
1052// successfullyReceive = true;
1053// return true;
1054// });
1055
1056// bobAccount->connectionManager().onConnectionReady(
1057// [&receiverConnected](const DeviceId&,
1058// const std::string& name,
1059// std::shared_ptr<ChannelSocket> socket) {
1060// receiverConnected = socket && (name == "git://*");
1061// });
1062
1063// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1064// "git://*",
1065// [&](std::shared_ptr<ChannelSocket> socket,
1066// const DeviceId&) {
1067// if (socket) {
1068// successfullyConnected = true;
1069// }
1070// cv.notify_one();
1071// });
1072
1073// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] {
1074// return successfullyReceive && successfullyConnected && receiverConnected;
1075// }));
1076// }
1077
1078// /* void
1079// ConnectionManagerTest::testDeclineICERequest()
1080// {
1081// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1082// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1083// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1084
1085// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1086
1087// std::mutex mtx;
1088// std::unique_lock<std::mutex> lk {mtx};
1089// std::condition_variable cv;
1090// bool successfullyConnected = false;
1091// bool successfullyReceive = false;
1092// bool receiverConnected = false;
1093
1094// bobAccount->connectionManager().onChannelRequest(
1095// [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1096// bobAccount->connectionManager().onICERequest([&](const DeviceId&) {
1097// successfullyReceive = true;
1098// return false;
1099// });
1100
1101// bobAccount->connectionManager().onConnectionReady(
1102// [&receiverConnected](const DeviceId&,
1103// const std::string& name,
1104// std::shared_ptr<ChannelSocket> socket) {
1105// receiverConnected = socket && (name == "git://*");
1106// });
1107
1108// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1109// "git://*",
1110// [&](std::shared_ptr<ChannelSocket> socket,
1111// const DeviceId&) {
1112// if (socket) {
1113// successfullyConnected = true;
1114// }
1115// cv.notify_one();
1116// });
1117
1118// cv.wait_for(lk, 30s);
1119// CPPUNIT_ASSERT(successfullyReceive);
1120// CPPUNIT_ASSERT(!receiverConnected);
1121// CPPUNIT_ASSERT(!successfullyConnected);
1122// } */
1123
1124// //why you invoke other functions when you want to test ICErequest?
1125// void
1126// ConnectionManagerTest::testDeclineICERequest()
1127// {
1128
1129// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1130
1131
1132// std::condition_variable cv;
1133// bool successfullyConnected = false;
1134// bool successfullyReceive = false;
1135// bool receiverConnected = false;
1136
1137// bobAccount->connectionManager().onChannelRequest(
1138// [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1139
1140// bobAccount->connectionManager().onICERequest([&](const DeviceId&) {
1141// successfullyReceive = true;
1142// return false; //???????????????????????? is this the point?
1143// });
1144
1145// bobAccount->connectionManager().onConnectionReady(
1146// [&receiverConnected](const DeviceId&,
1147// const std::string& name,
1148// std::shared_ptr<ChannelSocket> socket) {
1149// receiverConnected = socket && (name == "git://*");
1150// });
1151
1152// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1153// "git://*",
1154// [&](std::shared_ptr<ChannelSocket> socket,
1155// const DeviceId&) {
1156// if (socket) {
1157// successfullyConnected = true;
1158// }
1159// cv.notify_one();
1160// });
1161
1162// cv.wait_for(lk, 30s);
1163// CPPUNIT_ASSERT(successfullyReceive);
1164// CPPUNIT_ASSERT(!receiverConnected);
1165// CPPUNIT_ASSERT(!successfullyConnected);
1166// }
1167
1168// //I think you testing something other than the current class!
1169// /*
1170// void
1171// ConnectionManagerTest::testChannelRcvShutdown()
1172// {
1173// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1174// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1175// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1176
1177// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1178// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1179
1180// std::mutex mtx;
1181// std::unique_lock<std::mutex> lk {mtx};
1182// std::condition_variable cv;
1183// bool successfullyConnected = false;
1184// bool shutdownReceived = false;
1185
1186// std::shared_ptr<ChannelSocket> bobSock;
1187
1188// bobAccount->connectionManager().onChannelRequest(
1189// [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1190
1191// bobAccount->connectionManager().onConnectionReady(
1192// [&](const DeviceId& did, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
1193// if (socket && name == "git://*" && did != bobDeviceId) {
1194// bobSock = socket;
1195// cv.notify_one();
1196// }
1197// });
1198
1199// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1200// "git://*",
1201// [&](std::shared_ptr<ChannelSocket> socket,
1202// const DeviceId&) {
1203// if (socket) {
1204// socket->onShutdown([&] {
1205// shutdownReceived = true;
1206// cv.notify_one();
1207// });
1208// successfullyConnected = true;
1209// cv.notify_one();
1210// }
1211// });
1212
1213// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return bobSock && successfullyConnected; }));
1214// bobSock->shutdown();
1215// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return shutdownReceived; }));
1216// }
1217// */
1218// void
1219// ConnectionManagerTest::testChannelRcvShutdown()
1220// {
1221
1222// std::condition_variable cv;
1223// bool successfullyConnected = false;
1224// bool shutdownReceived = false;
1225
1226// std::shared_ptr<ChannelSocket> bobSock;
1227
1228// bobAccount->connectionManager().onChannelRequest(
1229// [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1230
1231// bobAccount->connectionManager().onConnectionReady(
1232// [&](const DeviceId& did, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
1233// if (socket && name == "git://*" && did != bobDeviceId) {
1234// bobSock = socket;
1235// cv.notify_one();
1236// }
1237// });
1238
1239// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1240// "git://*",
1241// [&](std::shared_ptr<ChannelSocket> socket,
1242// const DeviceId&) {
1243// if (socket) {
1244// socket->onShutdown([&] {
1245// shutdownReceived = true;
1246// cv.notify_one();
1247// });
1248// successfullyConnected = true;
1249// cv.notify_one();
1250// }
1251// });
1252
1253
1254// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return bobSock && successfullyConnected; }));
1255
1256// bobSock->shutdown();
1257
1258// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return shutdownReceived; }));
1259// }
1260
1261// //I think you testing something other than the current class!
1262// void
1263// ConnectionManagerTest::testChannelSenderShutdown()
1264// {
1265// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1266// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1267// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1268
1269// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1270// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1271
1272// std::mutex mtx;
1273// std::unique_lock<std::mutex> lk {mtx};
1274// std::condition_variable rcv, scv;
1275// bool successfullyConnected = false;
1276// bool successfullyReceive = false;
1277// bool receiverConnected = false;
1278// bool shutdownReceived = false;
1279
1280// bobAccount->connectionManager().onChannelRequest(
1281// [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
1282// const std::string& name) {
1283// successfullyReceive = name == "git://*";
1284// return true;
1285// });
1286
1287// bobAccount->connectionManager().onConnectionReady(
1288// [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
1289// if (socket) {
1290// socket->onShutdown([&] {
1291// shutdownReceived = true;
1292// scv.notify_one();
1293// });
1294// }
1295// receiverConnected = socket && (name == "git://*");
1296// });
1297
1298// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1299// "git://*",
1300// [&](std::shared_ptr<ChannelSocket> socket,
1301// const DeviceId&) {
1302// if (socket) {
1303// successfullyConnected = true;
1304// rcv.notify_one();
1305// socket->shutdown();
1306// }
1307// });
1308
1309// rcv.wait_for(lk, 30s);
1310// scv.wait_for(lk, 30s);
1311// CPPUNIT_ASSERT(shutdownReceived);
1312// CPPUNIT_ASSERT(successfullyReceive);
1313// CPPUNIT_ASSERT(successfullyConnected);
1314// CPPUNIT_ASSERT(receiverConnected);
1315// }
1316
1317// //how to get URI?
1318// //The call back function has different logic. Also, there are cuncurrenct tasks here. Please explain them.
1319// void
1320// ConnectionManagerTest::testCloseConnectionWith()
1321// {
1322
1323
1324// auto bobUri = bobAccount->getUsername();
1325
1326// std::condition_variable rcv, scv;
1327// std::atomic_int events(0);
1328// bool successfullyConnected = false;
1329// bool successfullyReceive = false;
1330// bool receiverConnected = false;
1331
1332// auto chanlReqCalBack = [&successfullyReceive]
1333// (const std::shared_ptr<dht::crypto::Certificate>&, const std::string& name)
1334// {
1335// successfullyReceive = name == "git://*";
1336// return true;
1337// };
1338
1339// bobConMngr.onChannelRequest(chanlReqCalBack);
1340
1341
1342// auto conctReadyCalBack =[&]
1343// (const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket)
1344// {
1345// if (socket) {
1346// socket->onShutdown([&] {
1347// events += 1;// this is an atomic variable why not using atomic operation i.e event.fetch_add(1)
1348// scv.notify_one();
1349// });
1350// }
1351// receiverConnected = socket && (name == "git://*");
1352// }
1353
1354// bobConMngr.onConnectionReady(conctReadyCalBack);
1355
1356
1357// auto conctDevicCalBack =[&]
1358// (std::shared_ptr<ChannelSocket> socket,const DeviceId&)
1359// {
1360// if (socket) {
1361// socket->onShutdown([&] {
1362// events += 1;
1363// scv.notify_one();
1364// });
1365// successfullyConnected = true;
1366// rcv.notify_one();
1367// }
1368// };
1369
1370// alicConMngr.connectDevice(bobDevicId, "git://*", conctDevicCalBack);
1371
1372
1373// CPPUNIT_ASSERT(rcv.wait_for(lk, 60s, [&] {
1374// return successfullyReceive && successfullyConnected && receiverConnected;
1375// }));
1376
1377// // This should trigger onShutdown
1378// alicConMngr.closeConnectionsWith(bobUri);
1379
1380// CPPUNIT_ASSERT(scv.wait_for(lk, 60s, [&] {
1381// return events == 2;
1382// }));
1383// }
1384
1385// //explain algorithm
1386// void
1387// ConnectionManagerTest::testShutdownCallbacks()
1388// {
1389
1390// auto aliceUri = aliceAccount->getUsername();
1391
1392// std::condition_variable rcv, chan2cv;
1393// bool successfullyConnected = false;
1394// bool successfullyReceive = false;
1395// bool receiverConnected = false;
1396
1397// auto chanlReqCalBack = [&successfullyReceive, &chan2cv]
1398// (const std::shared_ptr<dht::crypto::Certificate>&, const std::string& name)
1399// {
1400// if (name == "1") {
1401// successfullyReceive = true;
1402// } else {
1403// chan2cv.notify_one();
1404// // Do not return directly. Let the connection be closed
1405// std::this_thread::sleep_for(10s);
1406// }
1407// return true;
1408// };
1409
1410// bobConMngr.onChannelRequest(chanlReqCalBack);
1411
1412
1413// auto conctReadyCalBack =[&]
1414// (const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket)
1415// {
1416// receiverConnected = socket && (name == "1");
1417// };
1418
1419// bobConMngr.onConnectionReady(conctReadyCalBack);
1420
1421
1422// auto conctDevicCalBack =[&]
1423// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
1424// {
1425// if (socket) {
1426// successfullyConnected = true;
1427// rcv.notify_one();
1428// }
1429// };
1430
1431// alicConMngr.connectDevice(bobDeviceId, "1", conctDevicCalBack);
1432
1433
1434// // Connect first channel. This will initiate a mx sock ?????????????????????????????????????????????????????
1435// CPPUNIT_ASSERT(rcv.wait_for(lk, 30s, [&] {
1436// return successfullyReceive && successfullyConnected && receiverConnected;
1437// }));
1438
1439// // Connect another channel, but close the connection
1440// bool channel2NotConnected = false;
1441
1442
1443// auto conctDevicCalBack2 =[&]
1444// (std::shared_ptr<ChannelSocket> socket, const DeviceId&)
1445// {
1446// channel2NotConnected = !socket;
1447// rcv.notify_one();
1448// };
1449
1450// alicConMngr.connectDevice(bobDeviceId, "2", conctDevicCalBack2);
1451
1452// chan2cv.wait_for(lk, 30s);
1453
1454// // This should trigger onShutdown for second callback
1455// bobConMngr.closeConnectionsWith(aliceUri);
1456// CPPUNIT_ASSERT(rcv.wait_for(lk, 30s, [&] { return channel2NotConnected; }));
1457// }
1458
1459// //What is the story?
1460// void
1461// ConnectionManagerTest::testFloodSocket()
1462// {
1463// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1464// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1465// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1466// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1467// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1468
1469// std::mutex mtx;
1470// std::unique_lock<std::mutex> lk {mtx};
1471// std::condition_variable cv;
1472// bool successfullyConnected = false;
1473// bool successfullyReceive = false;
1474// bool receiverConnected = false;
1475// std::shared_ptr<ChannelSocket> rcvSock1, rcvSock2, rcvSock3, sendSock, sendSock2, sendSock3;
1476
1477// bobAccount->connectionManager().onChannelRequest(
1478// [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
1479// const std::string& name) {
1480// successfullyReceive = name == "1";
1481// return true;
1482// });
1483// bobAccount->connectionManager().onConnectionReady(
1484// [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
1485// receiverConnected = socket != nullptr;
1486// if (name == "1")
1487// rcvSock1 = socket;
1488// else if (name == "2")
1489// rcvSock2 = socket;
1490// else if (name == "3")
1491// rcvSock3 = socket;
1492// });
1493// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1494// "1",
1495// [&](std::shared_ptr<ChannelSocket> socket,
1496// const DeviceId&) {
1497// if (socket) {
1498// sendSock = socket;
1499// successfullyConnected = true;
1500// }
1501// cv.notify_one();
1502// });
1503// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] {
1504// return successfullyReceive && successfullyConnected && receiverConnected;
1505// }));
1506// CPPUNIT_ASSERT(receiverConnected);
1507// successfullyConnected = false;
1508// receiverConnected = false;
1509// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1510// "2",
1511// [&](std::shared_ptr<ChannelSocket> socket,
1512// const DeviceId&) {
1513// if (socket) {
1514// sendSock2 = socket;
1515// successfullyConnected = true;
1516// }
1517// cv.notify_one();
1518// });
1519// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return successfullyConnected && receiverConnected; }));
1520// successfullyConnected = false;
1521// receiverConnected = false;
1522// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1523// "3",
1524// [&](std::shared_ptr<ChannelSocket> socket,
1525// const DeviceId&) {
1526// if (socket) {
1527// sendSock3 = socket;
1528// successfullyConnected = true;
1529// }
1530// cv.notify_one();
1531// });
1532// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return successfullyConnected && receiverConnected; }));
1533
1534// std::mutex mtxRcv {};
1535// std::string alphabet, shouldRcv, rcv1, rcv2, rcv3;
1536// for (int i = 0; i < 100; ++i)
1537// alphabet += "QWERTYUIOPASDFGHJKLZXCVBNM";
1538
1539// // Qx8000
1540// // Wx8000
1541// // Ex8000
1542// // ...
1543// // Qx8000
1544// // Wx8000
1545// // Ex8000
1546// // ... x 99
1547// rcvSock1->setOnRecv([&](const uint8_t* buf, size_t len) {
1548// rcv1 += std::string(buf, buf + len);
1549// return len;
1550// });
1551// rcvSock2->setOnRecv([&](const uint8_t* buf, size_t len) {
1552// rcv2 += std::string(buf, buf + len);
1553// return len;
1554// });
1555// rcvSock3->setOnRecv([&](const uint8_t* buf, size_t len) {
1556// rcv3 += std::string(buf, buf + len);
1557// return len;
1558// });
1559// for (uint64_t i = 0; i < alphabet.size(); ++i) {
1560// auto send = std::string(8000, alphabet[i]);
1561// shouldRcv += send;
1562// std::error_code ec;
1563// sendSock->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
1564// sendSock2->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
1565// sendSock3->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
1566// CPPUNIT_ASSERT(!ec);
1567// }
1568// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
1569// return shouldRcv == rcv1 && shouldRcv == rcv2 && shouldRcv == rcv3;
1570// }));
1571// }
1572
1573// void
1574// ConnectionManagerTest::testDestroyWhileSending()
1575// {
1576// // Same as test before, but destroy the accounts while sending.
1577// // This test if a segfault occurs
1578// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1579// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1580// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1581// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1582// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1583// std::mutex mtx;
1584// std::unique_lock<std::mutex> lk {mtx};
1585// std::condition_variable cv;
1586// bool successfullyConnected = false;
1587// bool successfullyReceive = false;
1588// bool receiverConnected = false;
1589// std::shared_ptr<ChannelSocket> rcvSock1, rcvSock2, rcvSock3, sendSock, sendSock2, sendSock3;
1590// bobAccount->connectionManager().onChannelRequest(
1591// [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
1592// const std::string& name) {
1593// successfullyReceive = name == "1";
1594// return true;
1595// });
1596// bobAccount->connectionManager().onConnectionReady(
1597// [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
1598// receiverConnected = socket != nullptr;
1599// if (name == "1")
1600// rcvSock1 = socket;
1601// else if (name == "2")
1602// rcvSock2 = socket;
1603// else if (name == "3")
1604// rcvSock3 = socket;
1605// });
1606// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1607// "1",
1608// [&](std::shared_ptr<ChannelSocket> socket,
1609// const DeviceId&) {
1610// if (socket) {
1611// sendSock = socket;
1612// successfullyConnected = true;
1613// }
1614// cv.notify_one();
1615// });
1616// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] {
1617// return successfullyReceive && successfullyConnected && receiverConnected;
1618// }));
1619// successfullyConnected = false;
1620// receiverConnected = false;
1621// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1622// "2",
1623// [&](std::shared_ptr<ChannelSocket> socket,
1624// const DeviceId&) {
1625// if (socket) {
1626// sendSock2 = socket;
1627// successfullyConnected = true;
1628// }
1629// cv.notify_one();
1630// });
1631// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return successfullyConnected && receiverConnected; }));
1632// successfullyConnected = false;
1633// receiverConnected = false;
1634// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1635// "3",
1636// [&](std::shared_ptr<ChannelSocket> socket,
1637// const DeviceId&) {
1638// if (socket) {
1639// sendSock3 = socket;
1640// successfullyConnected = true;
1641// }
1642// cv.notify_one();
1643// });
1644// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return successfullyConnected && receiverConnected; }));
1645// std::mutex mtxRcv {};
1646// std::string alphabet;
1647// for (int i = 0; i < 100; ++i)
1648// alphabet += "QWERTYUIOPASDFGHJKLZXCVBNM";
1649// rcvSock1->setOnRecv([&](const uint8_t*, size_t len) { return len; });
1650// rcvSock2->setOnRecv([&](const uint8_t*, size_t len) { return len; });
1651// rcvSock3->setOnRecv([&](const uint8_t*, size_t len) { return len; });
1652// for (uint64_t i = 0; i < alphabet.size(); ++i) {
1653// auto send = std::string(8000, alphabet[i]);
1654// std::error_code ec;
1655// sendSock->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
1656// sendSock2->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
1657// sendSock3->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
1658// CPPUNIT_ASSERT(!ec);
1659// }
1660
1661
1662
1663// // No need to wait, immediately destroy, no segfault must occurs
1664// }
1665
1666
1667// //why you don't use this function in other test units to validate a test?
1668// /*
1669// void
1670// ConnectionManagerTest::testIsConnecting()
1671// {
1672// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1673// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1674// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1675
1676// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1677// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1678
1679// std::mutex mtx;
1680// std::unique_lock<std::mutex> lk {mtx};
1681// std::condition_variable cv;
1682// bool successfullyConnected = false, successfullyReceive = false;
1683
1684// bobAccount->connectionManager().onChannelRequest(
1685// [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) {
1686// successfullyReceive = true;
1687// cv.notify_one();
1688// std::this_thread::sleep_for(2s);
1689// return true;
1690// });
1691
1692// CPPUNIT_ASSERT(!aliceAccount->connectionManager().isConnecting(bobDeviceId, "sip"));
1693
1694// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1695// "sip",
1696// [&](std::shared_ptr<ChannelSocket> socket,
1697// const DeviceId&) {
1698// if (socket) {
1699// successfullyConnected = true;
1700// }
1701// cv.notify_one();
1702// });
1703// // connectDevice is full async, so isConnecting will be true after a few ms.
1704// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] { return successfullyReceive; }));
1705// CPPUNIT_ASSERT(aliceAccount->connectionManager().isConnecting(bobDeviceId, "sip"));
1706// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] { return successfullyConnected; }));
1707// std::this_thread::sleep_for(
1708// std::chrono::milliseconds(100)); // Just to wait for the callback to finish
1709// CPPUNIT_ASSERT(!aliceAccount->connectionManager().isConnecting(bobDeviceId, "sip"));
1710// }
1711// */
1712// void
1713// ConnectionManagerTest::testIsConnecting()
1714// {
1715
1716// std::condition_variable cv;
1717// bool successfullyConnected = false;
1718// bool successfullyReceive = false;
1719
1720// bobAccount->connectionManager().onChannelRequest(
1721// [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) {
1722// successfullyReceive = true;
1723// cv.notify_one();
1724// std::this_thread::sleep_for(2s);
1725// return true;
1726// });
1727
1728// CPPUNIT_ASSERT(!aliceAccount->connectionManager().isConnecting(bobDeviceId, "sip"));
1729
1730// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1731// "sip",
1732// [&](std::shared_ptr<ChannelSocket> socket,
1733// const DeviceId&) {
1734// if (socket) {
1735// successfullyConnected = true;
1736// }
1737// cv.notify_one();
1738// });
1739
1740// // connectDevice is full async, so isConnecting will be true after a few ms.
1741// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] { return successfullyReceive; }));
1742// CPPUNIT_ASSERT(aliceAccount->connectionManager().isConnecting(bobDeviceId, "sip"));
1743// CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] { return successfullyConnected; }));
1744// std::this_thread::sleep_for(
1745// std::chrono::milliseconds(100)); // Just to wait for the callback to finish
1746// CPPUNIT_ASSERT(!aliceAccount->connectionManager().isConnecting(bobDeviceId, "sip"));
1747// }
1748
1749
1750
1751// void
1752// ConnectionManagerTest::testCanSendBeacon()
1753// {
1754// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1755// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1756// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1757
1758// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1759// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1760
1761// std::mutex mtx;
1762// std::unique_lock<std::mutex> lk {mtx};
1763// std::condition_variable cv;
1764// bool successfullyConnected = false;
1765
1766// std::shared_ptr<MultiplexedSocket> aliceSocket, bobSocket;
1767// bobAccount->connectionManager().onChannelRequest(
1768// [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1769// bobAccount->connectionManager().onConnectionReady(
1770// [&](const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket) {
1771// if (socket && socket->name() == "sip")
1772// bobSocket = socket->underlyingSocket();
1773// cv.notify_one();
1774// });
1775
1776// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1777// "sip",
1778// [&](std::shared_ptr<ChannelSocket> socket,
1779// const DeviceId&) {
1780// if (socket) {
1781// aliceSocket = socket->underlyingSocket();
1782// successfullyConnected = true;
1783// }
1784// cv.notify_one();
1785// });
1786// // connectDevice is full async, so isConnecting will be true after a few ms.
1787// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return aliceSocket && bobSocket && successfullyConnected; }));
1788// CPPUNIT_ASSERT(aliceSocket->canSendBeacon());
1789
1790// // Because onConnectionReady is true before version is sent, we can wait a bit
1791// // before canSendBeacon is true.
1792// auto start = std::chrono::steady_clock::now();
1793// auto aliceCanSendBeacon = false;
1794// auto bobCanSendBeacon = false;
1795// do {
1796// aliceCanSendBeacon = aliceSocket->canSendBeacon();
1797// bobCanSendBeacon = bobSocket->canSendBeacon();
1798// if (!bobCanSendBeacon || !aliceCanSendBeacon)
1799// std::this_thread::sleep_for(1s);
1800// } while ((not bobCanSendBeacon or not aliceCanSendBeacon)
1801// and std::chrono::steady_clock::now() - start < 5s);
1802
1803// CPPUNIT_ASSERT(bobCanSendBeacon && aliceCanSendBeacon);
1804// }
1805
1806// void
1807// ConnectionManagerTest::testCannotSendBeacon()
1808// {
1809// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1810// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1811// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1812
1813// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1814// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1815
1816// std::mutex mtx;
1817// std::unique_lock<std::mutex> lk {mtx};
1818// std::condition_variable cv;
1819// bool successfullyConnected = false;
1820
1821// std::shared_ptr<MultiplexedSocket> aliceSocket, bobSocket;
1822// bobAccount->connectionManager().onChannelRequest(
1823// [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1824// bobAccount->connectionManager().onConnectionReady(
1825// [&](const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket) {
1826// if (socket && socket->name() == "sip")
1827// bobSocket = socket->underlyingSocket();
1828// cv.notify_one();
1829// });
1830
1831// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1832// "sip",
1833// [&](std::shared_ptr<ChannelSocket> socket,
1834// const DeviceId&) {
1835// if (socket) {
1836// aliceSocket = socket->underlyingSocket();
1837// successfullyConnected = true;
1838// }
1839// cv.notify_one();
1840// });
1841// // connectDevice is full async, so isConnecting will be true after a few ms.
1842// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return aliceSocket && bobSocket; }));
1843
1844// int version = 1412;
1845// bobSocket->setOnVersionCb([&](auto v) {
1846// version = v;
1847// cv.notify_one();
1848// });
1849// aliceSocket->setVersion(0);
1850// aliceSocket->sendVersion();
1851// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return version == 0; }));
1852// CPPUNIT_ASSERT(!bobSocket->canSendBeacon());
1853// }
1854
1855// void
1856// ConnectionManagerTest::testConnectivityChangeTriggerBeacon()
1857// {
1858// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1859// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1860// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1861
1862// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1863// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1864
1865// std::mutex mtx;
1866// std::unique_lock<std::mutex> lk {mtx};
1867// std::condition_variable cv;
1868// bool successfullyConnected = false;
1869
1870// std::shared_ptr<MultiplexedSocket> aliceSocket, bobSocket;
1871// bobAccount->connectionManager().onChannelRequest(
1872// [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1873// bobAccount->connectionManager().onConnectionReady(
1874// [&](const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket) {
1875// if (socket && socket->name() == "sip")
1876// bobSocket = socket->underlyingSocket();
1877// cv.notify_one();
1878// });
1879
1880// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1881// "sip",
1882// [&](std::shared_ptr<ChannelSocket> socket,
1883// const DeviceId&) {
1884// if (socket) {
1885// aliceSocket = socket->underlyingSocket();
1886// successfullyConnected = true;
1887// }
1888// cv.notify_one();
1889// });
1890// // connectDevice is full async, so isConnecting will be true after a few ms.
1891// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return aliceSocket && bobSocket; }));
1892
1893// bool hasRequest = false;
1894// bobSocket->setOnBeaconCb([&](auto p) {
1895// if (p)
1896// hasRequest = true;
1897// cv.notify_one();
1898// });
1899// aliceAccount->connectionManager().connectivityChanged();
1900// CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return hasRequest; }));
1901// }
1902
1903// void
1904// ConnectionManagerTest::testOnNoBeaconTriggersShutdown()
1905// {
1906// auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1907// auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1908// auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1909
1910// bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1911// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1912
1913// std::mutex mtx;
1914// std::unique_lock<std::mutex> lk {mtx};
1915// std::condition_variable cv;
1916// bool successfullyConnected = false;
1917
1918// std::shared_ptr<MultiplexedSocket> aliceSocket, bobSocket;
1919// bobAccount->connectionManager().onChannelRequest(
1920// [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1921// bobAccount->connectionManager().onConnectionReady(
1922// [&](const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket) {
1923// if (socket && socket->name() == "sip")
1924// bobSocket = socket->underlyingSocket();
1925// cv.notify_one();
1926// });
1927
1928// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1929// "sip",
1930// [&](std::shared_ptr<ChannelSocket> socket,
1931// const DeviceId&) {
1932// if (socket) {
1933// aliceSocket = socket->underlyingSocket();
1934// successfullyConnected = true;
1935// }
1936// cv.notify_one();
1937// });
1938// // connectDevice is full async, so isConnecting will be true after a few ms.
1939// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return aliceSocket && bobSocket; }));
1940
1941// bool isClosed = false;
1942// aliceSocket->onShutdown([&] {
1943// isClosed = true;
1944// cv.notify_one();
1945// });
1946// bobSocket->answerToBeacon(false);
1947// aliceAccount->connectionManager().connectivityChanged();
1948// CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return isClosed; }));*/
1949// }
1950
1951
1952
1953// //why you didn't invoke on Channel request?
1954// void
1955// ConnectionManagerTest::testShutdownWhileNegotiating()
1956// {
1957
1958// std::condition_variable cv;
1959// bool successfullyReceive = false;
1960// bool notConnected = false;
1961
1962// aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1963
1964// //Why???????????
1965// bobAccount->connectionManager().onICERequest([&](const DeviceId&) {
1966// successfullyReceive = true;
1967// cv.notify_one();
1968// return true;
1969// });
1970
1971// aliceAccount->connectionManager().connectDevice(bobDeviceId,
1972// "git://*",
1973// [&](std::shared_ptr<ChannelSocket> socket,
1974// const DeviceId&) {
1975// notConnected = !socket;
1976// cv.notify_one();
1977// });
1978
1979// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return successfullyReceive; }));
1980
1981
1982// aliceAccount->connectionManager().reset(); //use it but check it first
1983// Manager::instance().setAccountActive(aliceId, false, true);//?????????????????????????????????????
1984
1985// CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return notConnected; }));
1986// }
Adrien Béraudefe27372023-05-27 18:56:29 -04001987
1988} // namespace test
Sébastien Blin464bdff2023-07-19 08:02:53 -04001989} // namespace dhtnet
Adrien Béraudefe27372023-05-27 18:56:29 -04001990
Adrien Béraud1ae60aa2023-07-07 09:55:09 -04001991JAMI_TEST_RUNNER(dhtnet::test::ConnectionManagerTest::name())