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