blob: ab4274484c3d584b643b0e237a585cf8890e2fc0 [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 */
Adrien Béraudefe27372023-05-27 18:56:29 -040017#include <cppunit/TestAssert.h>
18#include <cppunit/TestFixture.h>
19#include <cppunit/extensions/HelperMacros.h>
20
21#include <condition_variable>
22
23#include "connectionmanager.h"
24#include "multiplexed_socket.h"
25#include "test_runner.h"
26
27using namespace std::literals::chrono_literals;
28
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040029namespace dhtnet {
Adrien Béraudefe27372023-05-27 18:56:29 -040030namespace test {
31
32class ConnectionManagerTest : public CppUnit::TestFixture
33{
34public:
35 ConnectionManagerTest() {}
36 ~ConnectionManagerTest() { }
37 static std::string name() { return "ConnectionManager"; }
38 void setUp();
39 void tearDown();
40
41 std::string aliceId;
42 std::string bobId;
43
44private:
45 void testConnectDevice();
46 void testAcceptConnection();
47 void testMultipleChannels();
48 void testMultipleChannelsOneDeclined();
49 void testMultipleChannelsSameName();
50 void testDeclineConnection();
51 void testSendReceiveData();
52 void testAcceptsICERequest();
53 void testDeclineICERequest();
54 void testChannelRcvShutdown();
55 void testChannelSenderShutdown();
56 void testCloseConnectionWith();
57 void testShutdownCallbacks();
58 void testFloodSocket();
59 void testDestroyWhileSending();
60 void testIsConnecting();
61 void testCanSendBeacon();
62 void testCannotSendBeacon();
63 void testConnectivityChangeTriggerBeacon();
64 void testOnNoBeaconTriggersShutdown();
65 void testShutdownWhileNegotiating();
66
67 CPPUNIT_TEST_SUITE(ConnectionManagerTest);
68 CPPUNIT_TEST(testConnectDevice);
69 CPPUNIT_TEST(testAcceptConnection);
70 CPPUNIT_TEST(testMultipleChannels);
71 CPPUNIT_TEST(testMultipleChannelsOneDeclined);
72 CPPUNIT_TEST(testMultipleChannelsSameName);
73 CPPUNIT_TEST(testDeclineConnection);
74 CPPUNIT_TEST(testSendReceiveData);
75 CPPUNIT_TEST(testAcceptsICERequest);
76 CPPUNIT_TEST(testDeclineICERequest);
77 CPPUNIT_TEST(testChannelRcvShutdown);
78 CPPUNIT_TEST(testChannelSenderShutdown);
79 CPPUNIT_TEST(testCloseConnectionWith);
80 CPPUNIT_TEST(testShutdownCallbacks);
81 CPPUNIT_TEST(testFloodSocket);
82 CPPUNIT_TEST(testDestroyWhileSending);
83 CPPUNIT_TEST(testIsConnecting);
84 CPPUNIT_TEST(testCanSendBeacon);
85 CPPUNIT_TEST(testCannotSendBeacon);
86 CPPUNIT_TEST(testConnectivityChangeTriggerBeacon);
87 CPPUNIT_TEST(testOnNoBeaconTriggersShutdown);
88 CPPUNIT_TEST(testShutdownWhileNegotiating);
89 CPPUNIT_TEST_SUITE_END();
90};
91
92CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConnectionManagerTest, ConnectionManagerTest::name());
93
94void
95ConnectionManagerTest::setUp()
96{
97 /*auto actors = load_actors_and_wait_for_announcement("actors/alice-bob.yml");
98 aliceId = actors["alice"];
99 bobId = actors["bob"];
100
101 // Pin certificate from one to another certstore (because we do not perform any DHT operation in this test)
102 auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
103 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
104 bobAccount->certStore().pinCertificate(aliceAccount->identity().second);
105 aliceAccount->certStore().pinCertificate(bobAccount->identity().second);*/
106}
107
108void
109ConnectionManagerTest::tearDown()
110{
111 //wait_for_removal_of({aliceId, bobId});
112}
113
114void
115ConnectionManagerTest::testConnectDevice()
116{
117 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
118 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
119 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
120
121 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
122 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
123
124 std::mutex mtx;
125 std::unique_lock<std::mutex> lk {mtx};
126 std::condition_variable cv, cvReceive;
127 bool successfullyConnected = false;
128 bool successfullyReceive = false;
129
130 bobAccount->connectionManager().onChannelRequest(
131 [&successfullyReceive, &cvReceive](const std::shared_ptr<dht::crypto::Certificate>&,
132 const std::string& name) {
133 successfullyReceive = name == "git://*";
134 cvReceive.notify_one();
135 return true;
136 });
137
138 aliceAccount->connectionManager().connectDevice(bobDeviceId,
139 "git://*",
140 [&](std::shared_ptr<ChannelSocket> socket,
141 const DeviceId&) {
142 if (socket) {
143 successfullyConnected = true;
144 }
145 cv.notify_one();
146 });
147 CPPUNIT_ASSERT(cvReceive.wait_for(lk, 60s, [&] { return successfullyReceive; }));
148 CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] { return successfullyConnected; }));*/
149}
150
151void
152ConnectionManagerTest::testAcceptConnection()
153{
154 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
155 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
156 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
157
158 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
159 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
160
161 std::mutex mtx;
162 std::unique_lock<std::mutex> lk {mtx};
163 std::condition_variable cv;
164 bool successfullyConnected = false;
165 bool successfullyReceive = false;
166 bool receiverConnected = false;
167
168 bobAccount->connectionManager().onChannelRequest(
169 [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
170 const std::string& name) {
171 successfullyReceive = name == "git://*";
172 return true;
173 });
174
175 bobAccount->connectionManager().onConnectionReady(
176 [&receiverConnected](const DeviceId&,
177 const std::string& name,
178 std::shared_ptr<ChannelSocket> socket) {
179 receiverConnected = socket && (name == "git://*");
180 });
181
182 aliceAccount->connectionManager().connectDevice(bobDeviceId,
183 "git://*",
184 [&](std::shared_ptr<ChannelSocket> socket,
185 const DeviceId&) {
186 if (socket) {
187 successfullyConnected = true;
188 }
189 cv.notify_one();
190 });
191
192 CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
193 return successfullyReceive && successfullyConnected && receiverConnected;
194 }));*/
195}
196
197void
198ConnectionManagerTest::testMultipleChannels()
199{
200 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
201 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
202 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
203
204 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
205 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
206
207 std::mutex mtx;
208 std::unique_lock<std::mutex> lk {mtx};
209 std::condition_variable cv;
210 bool successfullyConnected = false;
211 bool successfullyConnected2 = false;
212 int receiverConnected = 0;
213
214 bobAccount->connectionManager().onChannelRequest(
215 [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
216
217 bobAccount->connectionManager().onConnectionReady(
218 [&receiverConnected](const DeviceId&,
219 const std::string&,
220 std::shared_ptr<ChannelSocket> socket) {
221 if (socket)
222 receiverConnected += 1;
223 });
224
225 aliceAccount->connectionManager().connectDevice(bobDeviceId,
226 "git://*",
227 [&](std::shared_ptr<ChannelSocket> socket,
228 const DeviceId&) {
229 if (socket) {
230 successfullyConnected = true;
231 }
232 cv.notify_one();
233 });
234
235 aliceAccount->connectionManager().connectDevice(bobDeviceId,
236 "sip://*",
237 [&](std::shared_ptr<ChannelSocket> socket,
238 const DeviceId&) {
239 if (socket) {
240 successfullyConnected2 = true;
241 }
242 cv.notify_one();
243 });
244
245 CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
246 return successfullyConnected && successfullyConnected2 && receiverConnected == 2;
247 }));
248 CPPUNIT_ASSERT(aliceAccount->connectionManager().activeSockets() == 1);*/
249}
250
251void
252ConnectionManagerTest::testMultipleChannelsOneDeclined()
253{
254 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
255 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
256 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
257
258 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
259 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
260
261 std::mutex mtx;
262 std::unique_lock<std::mutex> lk {mtx};
263 std::condition_variable cv;
264 bool successfullyNotConnected = false;
265 bool successfullyConnected2 = false;
266 int receiverConnected = 0;
267
268 bobAccount->connectionManager().onChannelRequest(
269 [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string& name) {
270 if (name == "git://*")
271 return false;
272 return true;
273 });
274
275 bobAccount->connectionManager().onConnectionReady(
276 [&](const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket) {
277 if (socket)
278 receiverConnected += 1;
279 cv.notify_one();
280 });
281
282 aliceAccount->connectionManager().connectDevice(bobDeviceId,
283 "git://*",
284 [&](std::shared_ptr<ChannelSocket> socket,
285 const DeviceId&) {
286 if (!socket)
287 successfullyNotConnected = true;
288 cv.notify_one();
289 });
290
291 aliceAccount->connectionManager().connectDevice(bobDeviceId,
292 "sip://*",
293 [&](std::shared_ptr<ChannelSocket> socket,
294 const DeviceId&) {
295 if (socket)
296 successfullyConnected2 = true;
297 cv.notify_one();
298 });
299
300 CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
301 return successfullyNotConnected && successfullyConnected2 && receiverConnected == 1;
302 }));
303 CPPUNIT_ASSERT(aliceAccount->connectionManager().activeSockets() == 1);*/
304}
305
306void
307ConnectionManagerTest::testMultipleChannelsSameName()
308{
309 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
310 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
311 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
312
313 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
314 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
315
316 std::mutex mtx;
317 std::unique_lock<std::mutex> lk {mtx};
318 std::condition_variable cv;
319 bool successfullyConnected = false;
320 bool successfullyConnected2 = false;
321 int receiverConnected = 0;
322
323 bobAccount->connectionManager().onChannelRequest(
324 [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
325
326 bobAccount->connectionManager().onConnectionReady(
327 [&receiverConnected](const DeviceId&,
328 const std::string&,
329 std::shared_ptr<ChannelSocket> socket) {
330 if (socket)
331 receiverConnected += 1;
332 });
333
334 aliceAccount->connectionManager().connectDevice(bobDeviceId,
335 "git://*",
336 [&](std::shared_ptr<ChannelSocket> socket,
337 const DeviceId&) {
338 if (socket) {
339 successfullyConnected = true;
340 }
341 cv.notify_one();
342 });
343
344 // We can open two sockets with the same name, it will be two different channel
345 aliceAccount->connectionManager().connectDevice(bobDeviceId,
346 "git://*",
347 [&](std::shared_ptr<ChannelSocket> socket,
348 const DeviceId&) {
349 if (socket) {
350 successfullyConnected2 = true;
351 }
352 cv.notify_one();
353 });
354
355 CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
356 return successfullyConnected && successfullyConnected2 && receiverConnected == 2;
357 }));*/
358}
359
360void
361ConnectionManagerTest::testSendReceiveData()
362{
363 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
364 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
365 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
366
367 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
368 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
369
370 std::mutex mtx;
371 std::unique_lock<std::mutex> lk {mtx};
372 std::condition_variable cv;
373 std::atomic_int events(0);
374 bool successfullyConnected = false, successfullyConnected2 = false, successfullyReceive = false,
375 receiverConnected = false;
376 const uint8_t buf_other[] = {0x64, 0x65, 0x66, 0x67};
377 const uint8_t buf_test[] = {0x68, 0x69, 0x70, 0x71};
378 bool dataOk = false, dataOk2 = false;
379
380 bobAccount->connectionManager().onChannelRequest(
381 [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
382 const std::string&) {
383 successfullyReceive = true;
384 return true;
385 });
386
387 bobAccount->connectionManager().onConnectionReady(
388 [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
389 if (socket && (name == "test" || name == "other")) {
390 receiverConnected = true;
391 std::error_code ec;
392 auto res = socket->waitForData(std::chrono::milliseconds(5000), ec);
393 if (res == 4) {
394 uint8_t buf[4];
395 socket->read(&buf[0], 4, ec);
396 if (name == "test")
397 dataOk = std::equal(std::begin(buf), std::end(buf), std::begin(buf_test));
398 else
399 dataOk2 = std::equal(std::begin(buf), std::end(buf), std::begin(buf_other));
400 events++;
401 cv.notify_one();
402 }
403 }
404 });
405
406 aliceAccount->connectionManager().connectDevice(bobDeviceId,
407 "test",
408 [&](std::shared_ptr<ChannelSocket> socket,
409 const DeviceId&) {
410 if (socket) {
411 successfullyConnected = true;
412 std::error_code ec;
413 socket->write(&buf_test[0], 4, ec);
414 }
415 events++;
416 cv.notify_one();
417 });
418
419 aliceAccount->connectionManager().connectDevice(bobDeviceId,
420 "other",
421 [&](std::shared_ptr<ChannelSocket> socket,
422 const DeviceId&) {
423 if (socket) {
424 successfullyConnected2 = true;
425 std::error_code ec;
426 socket->write(&buf_other[0], 4, ec);
427 }
428 events++;
429 cv.notify_one();
430 });
431
432 CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
433 return events == 4 && successfullyReceive && successfullyConnected && successfullyConnected2
434 && dataOk && dataOk2;
435 }));*/
436}
437
438void
439ConnectionManagerTest::testDeclineConnection()
440{
441 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
442 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
443 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
444
445 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
446 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
447
448 std::mutex mtx;
449 std::unique_lock<std::mutex> lk {mtx};
450 std::condition_variable cv;
451 bool successfullyConnected = false;
452 bool successfullyReceive = false;
453 bool receiverConnected = false;
454
455 bobAccount->connectionManager().onChannelRequest(
456 [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
457 const std::string&) {
458 successfullyReceive = true;
459 return false;
460 });
461
462 bobAccount->connectionManager().onConnectionReady(
463 [&receiverConnected](const DeviceId&,
464 const std::string&,
465 std::shared_ptr<ChannelSocket> socket) {
466 if (socket)
467 receiverConnected = true;
468 });
469
470 aliceAccount->connectionManager().connectDevice(bobDeviceId,
471 "git://*",
472 [&](std::shared_ptr<ChannelSocket> socket,
473 const DeviceId&) {
474 if (socket) {
475 successfullyConnected = true;
476 }
477 cv.notify_one();
478 });
479 cv.wait_for(lk, 30s);
480 CPPUNIT_ASSERT(successfullyReceive);
481 CPPUNIT_ASSERT(!successfullyConnected);
482 CPPUNIT_ASSERT(!receiverConnected);*/
483}
484
485void
486ConnectionManagerTest::testAcceptsICERequest()
487{
488 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
489 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
490 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
491
492 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
493
494 std::mutex mtx;
495 std::unique_lock<std::mutex> lk {mtx};
496 std::condition_variable cv;
497 bool successfullyConnected = false;
498 bool successfullyReceive = false;
499 bool receiverConnected = false;
500
501 bobAccount->connectionManager().onChannelRequest(
502 [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
503 bobAccount->connectionManager().onICERequest([&](const DeviceId&) {
504 successfullyReceive = true;
505 return true;
506 });
507
508 bobAccount->connectionManager().onConnectionReady(
509 [&receiverConnected](const DeviceId&,
510 const std::string& name,
511 std::shared_ptr<ChannelSocket> socket) {
512 receiverConnected = socket && (name == "git://*");
513 });
514
515 aliceAccount->connectionManager().connectDevice(bobDeviceId,
516 "git://*",
517 [&](std::shared_ptr<ChannelSocket> socket,
518 const DeviceId&) {
519 if (socket) {
520 successfullyConnected = true;
521 }
522 cv.notify_one();
523 });
524
525 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] {
526 return successfullyReceive && successfullyConnected && receiverConnected;
527 }));*/
528}
529
530void
531ConnectionManagerTest::testDeclineICERequest()
532{
533 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
534 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
535 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
536
537 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
538
539 std::mutex mtx;
540 std::unique_lock<std::mutex> lk {mtx};
541 std::condition_variable cv;
542 bool successfullyConnected = false;
543 bool successfullyReceive = false;
544 bool receiverConnected = false;
545
546 bobAccount->connectionManager().onChannelRequest(
547 [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
548 bobAccount->connectionManager().onICERequest([&](const DeviceId&) {
549 successfullyReceive = true;
550 return false;
551 });
552
553 bobAccount->connectionManager().onConnectionReady(
554 [&receiverConnected](const DeviceId&,
555 const std::string& name,
556 std::shared_ptr<ChannelSocket> socket) {
557 receiverConnected = socket && (name == "git://*");
558 });
559
560 aliceAccount->connectionManager().connectDevice(bobDeviceId,
561 "git://*",
562 [&](std::shared_ptr<ChannelSocket> socket,
563 const DeviceId&) {
564 if (socket) {
565 successfullyConnected = true;
566 }
567 cv.notify_one();
568 });
569
570 cv.wait_for(lk, 30s);
571 CPPUNIT_ASSERT(successfullyReceive);
572 CPPUNIT_ASSERT(!receiverConnected);
573 CPPUNIT_ASSERT(!successfullyConnected);*/
574}
575
576void
577ConnectionManagerTest::testChannelRcvShutdown()
578{
579 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
580 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
581 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
582
583 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
584 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
585
586 std::mutex mtx;
587 std::unique_lock<std::mutex> lk {mtx};
588 std::condition_variable cv;
589 bool successfullyConnected = false;
590 bool shutdownReceived = false;
591
592 std::shared_ptr<ChannelSocket> bobSock;
593
594 bobAccount->connectionManager().onChannelRequest(
595 [](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
596
597 bobAccount->connectionManager().onConnectionReady(
598 [&](const DeviceId& did, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
599 if (socket && name == "git://*" && did != bobDeviceId) {
600 bobSock = socket;
601 cv.notify_one();
602 }
603 });
604
605 aliceAccount->connectionManager().connectDevice(bobDeviceId,
606 "git://*",
607 [&](std::shared_ptr<ChannelSocket> socket,
608 const DeviceId&) {
609 if (socket) {
610 socket->onShutdown([&] {
611 shutdownReceived = true;
612 cv.notify_one();
613 });
614 successfullyConnected = true;
615 cv.notify_one();
616 }
617 });
618
619 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return bobSock && successfullyConnected; }));
620 bobSock->shutdown();
621 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return shutdownReceived; }));*/
622}
623
624void
625ConnectionManagerTest::testChannelSenderShutdown()
626{
627 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
628 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
629 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
630
631 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
632 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
633
634 std::mutex mtx;
635 std::unique_lock<std::mutex> lk {mtx};
636 std::condition_variable rcv, scv;
637 bool successfullyConnected = false;
638 bool successfullyReceive = false;
639 bool receiverConnected = false;
640 bool shutdownReceived = false;
641
642 bobAccount->connectionManager().onChannelRequest(
643 [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
644 const std::string& name) {
645 successfullyReceive = name == "git://*";
646 return true;
647 });
648
649 bobAccount->connectionManager().onConnectionReady(
650 [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
651 if (socket) {
652 socket->onShutdown([&] {
653 shutdownReceived = true;
654 scv.notify_one();
655 });
656 }
657 receiverConnected = socket && (name == "git://*");
658 });
659
660 aliceAccount->connectionManager().connectDevice(bobDeviceId,
661 "git://*",
662 [&](std::shared_ptr<ChannelSocket> socket,
663 const DeviceId&) {
664 if (socket) {
665 successfullyConnected = true;
666 rcv.notify_one();
667 socket->shutdown();
668 }
669 });
670
671 rcv.wait_for(lk, 30s);
672 scv.wait_for(lk, 30s);
673 CPPUNIT_ASSERT(shutdownReceived);
674 CPPUNIT_ASSERT(successfullyReceive);
675 CPPUNIT_ASSERT(successfullyConnected);
676 CPPUNIT_ASSERT(receiverConnected);*/
677}
678
679void
680ConnectionManagerTest::testCloseConnectionWith()
681{
682 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
683 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
684 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
685 auto bobUri = bobAccount->getUsername();
686
687 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
688 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
689
690 std::mutex mtx;
691 std::unique_lock<std::mutex> lk {mtx};
692 std::condition_variable rcv, scv;
693 std::atomic_int events(0);
694 bool successfullyConnected = false;
695 bool successfullyReceive = false;
696 bool receiverConnected = false;
697
698 bobAccount->connectionManager().onChannelRequest(
699 [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
700 const std::string& name) {
701 successfullyReceive = name == "git://*";
702 return true;
703 });
704
705 bobAccount->connectionManager().onConnectionReady(
706 [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
707 if (socket) {
708 socket->onShutdown([&] {
709 events += 1;
710 scv.notify_one();
711 });
712 }
713 receiverConnected = socket && (name == "git://*");
714 });
715
716 aliceAccount->connectionManager().connectDevice(bobDeviceId,
717 "git://*",
718 [&](std::shared_ptr<ChannelSocket> socket,
719 const DeviceId&) {
720 if (socket) {
721 socket->onShutdown([&] {
722 events += 1;
723 scv.notify_one();
724 });
725 successfullyConnected = true;
726 rcv.notify_one();
727 }
728 });
729
730 rcv.wait_for(lk, 30s);
731 // This should trigger onShutdown
732 aliceAccount->connectionManager().closeConnectionsWith(bobUri);
733 CPPUNIT_ASSERT(scv.wait_for(lk, 60s, [&] {
734 return events == 2 && successfullyReceive && successfullyConnected && receiverConnected;
735 }));*/
736}
737
738void
739ConnectionManagerTest::testShutdownCallbacks()
740{
741 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
742 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
743 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
744 auto aliceUri = aliceAccount->getUsername();
745
746 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
747 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
748
749 std::mutex mtx;
750 std::unique_lock<std::mutex> lk {mtx};
751 std::condition_variable rcv, chan2cv;
752 bool successfullyConnected = false;
753 bool successfullyReceive = false;
754 bool receiverConnected = false;
755
756 bobAccount->connectionManager().onChannelRequest(
757 [&successfullyReceive, &chan2cv](const std::shared_ptr<dht::crypto::Certificate>&,
758 const std::string& name) {
759 if (name == "1") {
760 successfullyReceive = true;
761 } else {
762 chan2cv.notify_one();
763 // Do not return directly. Let the connection be closed
764 std::this_thread::sleep_for(10s);
765 }
766 return true;
767 });
768
769 bobAccount->connectionManager().onConnectionReady(
770 [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
771 receiverConnected = socket && (name == "1");
772 });
773
774 aliceAccount->connectionManager().connectDevice(bobDeviceId,
775 "1",
776 [&](std::shared_ptr<ChannelSocket> socket,
777 const DeviceId&) {
778 if (socket) {
779 successfullyConnected = true;
780 rcv.notify_one();
781 }
782 });
783 // Connect first channel. This will initiate a mx sock
784 CPPUNIT_ASSERT(rcv.wait_for(lk, 30s, [&] {
785 return successfullyReceive && successfullyConnected && receiverConnected;
786 }));
787
788 // Connect another channel, but close the connection
789 bool channel2NotConnected = false;
790 aliceAccount->connectionManager().connectDevice(bobDeviceId,
791 "2",
792 [&](std::shared_ptr<ChannelSocket> socket,
793 const DeviceId&) {
794 channel2NotConnected = !socket;
795 rcv.notify_one();
796 });
797 chan2cv.wait_for(lk, 30s);
798
799 // This should trigger onShutdown for second callback
800 bobAccount->connectionManager().closeConnectionsWith(aliceUri);
801 CPPUNIT_ASSERT(rcv.wait_for(lk, 30s, [&] { return channel2NotConnected; }));*/
802}
803
804void
805ConnectionManagerTest::testFloodSocket()
806{
807 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
808 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
809 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
810 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
811 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
812 std::mutex mtx;
813 std::unique_lock<std::mutex> lk {mtx};
814 std::condition_variable cv;
815 bool successfullyConnected = false;
816 bool successfullyReceive = false;
817 bool receiverConnected = false;
818 std::shared_ptr<ChannelSocket> rcvSock1, rcvSock2, rcvSock3, sendSock, sendSock2, sendSock3;
819 bobAccount->connectionManager().onChannelRequest(
820 [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
821 const std::string& name) {
822 successfullyReceive = name == "1";
823 return true;
824 });
825 bobAccount->connectionManager().onConnectionReady(
826 [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
827 receiverConnected = socket != nullptr;
828 if (name == "1")
829 rcvSock1 = socket;
830 else if (name == "2")
831 rcvSock2 = socket;
832 else if (name == "3")
833 rcvSock3 = socket;
834 });
835 aliceAccount->connectionManager().connectDevice(bobDeviceId,
836 "1",
837 [&](std::shared_ptr<ChannelSocket> socket,
838 const DeviceId&) {
839 if (socket) {
840 sendSock = socket;
841 successfullyConnected = true;
842 }
843 cv.notify_one();
844 });
845 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] {
846 return successfullyReceive && successfullyConnected && receiverConnected;
847 }));
848 CPPUNIT_ASSERT(receiverConnected);
849 successfullyConnected = false;
850 receiverConnected = false;
851 aliceAccount->connectionManager().connectDevice(bobDeviceId,
852 "2",
853 [&](std::shared_ptr<ChannelSocket> socket,
854 const DeviceId&) {
855 if (socket) {
856 sendSock2 = socket;
857 successfullyConnected = true;
858 }
859 cv.notify_one();
860 });
861 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return successfullyConnected && receiverConnected; }));
862 successfullyConnected = false;
863 receiverConnected = false;
864 aliceAccount->connectionManager().connectDevice(bobDeviceId,
865 "3",
866 [&](std::shared_ptr<ChannelSocket> socket,
867 const DeviceId&) {
868 if (socket) {
869 sendSock3 = socket;
870 successfullyConnected = true;
871 }
872 cv.notify_one();
873 });
874 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return successfullyConnected && receiverConnected; }));
875 std::mutex mtxRcv {};
876 std::string alphabet, shouldRcv, rcv1, rcv2, rcv3;
877 for (int i = 0; i < 100; ++i)
878 alphabet += "QWERTYUIOPASDFGHJKLZXCVBNM";
879 rcvSock1->setOnRecv([&](const uint8_t* buf, size_t len) {
880 rcv1 += std::string(buf, buf + len);
881 return len;
882 });
883 rcvSock2->setOnRecv([&](const uint8_t* buf, size_t len) {
884 rcv2 += std::string(buf, buf + len);
885 return len;
886 });
887 rcvSock3->setOnRecv([&](const uint8_t* buf, size_t len) {
888 rcv3 += std::string(buf, buf + len);
889 return len;
890 });
891 for (uint64_t i = 0; i < alphabet.size(); ++i) {
892 auto send = std::string(8000, alphabet[i]);
893 shouldRcv += send;
894 std::error_code ec;
895 sendSock->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
896 sendSock2->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
897 sendSock3->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
898 CPPUNIT_ASSERT(!ec);
899 }
900 CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] {
901 return shouldRcv == rcv1 && shouldRcv == rcv2 && shouldRcv == rcv3;
902 }));*/
903}
904
905void
906ConnectionManagerTest::testDestroyWhileSending()
907{
908 // Same as test before, but destroy the accounts while sending.
909 // This test if a segfault occurs
910 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
911 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
912 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
913 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
914 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
915 std::mutex mtx;
916 std::unique_lock<std::mutex> lk {mtx};
917 std::condition_variable cv;
918 bool successfullyConnected = false;
919 bool successfullyReceive = false;
920 bool receiverConnected = false;
921 std::shared_ptr<ChannelSocket> rcvSock1, rcvSock2, rcvSock3, sendSock, sendSock2, sendSock3;
922 bobAccount->connectionManager().onChannelRequest(
923 [&successfullyReceive](const std::shared_ptr<dht::crypto::Certificate>&,
924 const std::string& name) {
925 successfullyReceive = name == "1";
926 return true;
927 });
928 bobAccount->connectionManager().onConnectionReady(
929 [&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> socket) {
930 receiverConnected = socket != nullptr;
931 if (name == "1")
932 rcvSock1 = socket;
933 else if (name == "2")
934 rcvSock2 = socket;
935 else if (name == "3")
936 rcvSock3 = socket;
937 });
938 aliceAccount->connectionManager().connectDevice(bobDeviceId,
939 "1",
940 [&](std::shared_ptr<ChannelSocket> socket,
941 const DeviceId&) {
942 if (socket) {
943 sendSock = socket;
944 successfullyConnected = true;
945 }
946 cv.notify_one();
947 });
948 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] {
949 return successfullyReceive && successfullyConnected && receiverConnected;
950 }));
951 successfullyConnected = false;
952 receiverConnected = false;
953 aliceAccount->connectionManager().connectDevice(bobDeviceId,
954 "2",
955 [&](std::shared_ptr<ChannelSocket> socket,
956 const DeviceId&) {
957 if (socket) {
958 sendSock2 = socket;
959 successfullyConnected = true;
960 }
961 cv.notify_one();
962 });
963 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return successfullyConnected && receiverConnected; }));
964 successfullyConnected = false;
965 receiverConnected = false;
966 aliceAccount->connectionManager().connectDevice(bobDeviceId,
967 "3",
968 [&](std::shared_ptr<ChannelSocket> socket,
969 const DeviceId&) {
970 if (socket) {
971 sendSock3 = socket;
972 successfullyConnected = true;
973 }
974 cv.notify_one();
975 });
976 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return successfullyConnected && receiverConnected; }));
977 std::mutex mtxRcv {};
978 std::string alphabet;
979 for (int i = 0; i < 100; ++i)
980 alphabet += "QWERTYUIOPASDFGHJKLZXCVBNM";
981 rcvSock1->setOnRecv([&](const uint8_t*, size_t len) { return len; });
982 rcvSock2->setOnRecv([&](const uint8_t*, size_t len) { return len; });
983 rcvSock3->setOnRecv([&](const uint8_t*, size_t len) { return len; });
984 for (uint64_t i = 0; i < alphabet.size(); ++i) {
985 auto send = std::string(8000, alphabet[i]);
986 std::error_code ec;
987 sendSock->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
988 sendSock2->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
989 sendSock3->write(reinterpret_cast<unsigned char*>(send.data()), send.size(), ec);
990 CPPUNIT_ASSERT(!ec);
991 }*/
992
993 // No need to wait, immediately destroy, no segfault must occurs
994}
995
996void
997ConnectionManagerTest::testIsConnecting()
998{
999 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1000 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1001 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1002
1003 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1004 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1005
1006 std::mutex mtx;
1007 std::unique_lock<std::mutex> lk {mtx};
1008 std::condition_variable cv;
1009 bool successfullyConnected = false, successfullyReceive = false;
1010
1011 bobAccount->connectionManager().onChannelRequest(
1012 [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) {
1013 successfullyReceive = true;
1014 cv.notify_one();
1015 std::this_thread::sleep_for(2s);
1016 return true;
1017 });
1018
1019 CPPUNIT_ASSERT(!aliceAccount->connectionManager().isConnecting(bobDeviceId, "sip"));
1020 aliceAccount->connectionManager().connectDevice(bobDeviceId,
1021 "sip",
1022 [&](std::shared_ptr<ChannelSocket> socket,
1023 const DeviceId&) {
1024 if (socket) {
1025 successfullyConnected = true;
1026 }
1027 cv.notify_one();
1028 });
1029 // connectDevice is full async, so isConnecting will be true after a few ms.
1030 CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] { return successfullyReceive; }));
1031 CPPUNIT_ASSERT(aliceAccount->connectionManager().isConnecting(bobDeviceId, "sip"));
1032 CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] { return successfullyConnected; }));
1033 std::this_thread::sleep_for(
1034 std::chrono::milliseconds(100)); // Just to wait for the callback to finish
1035 CPPUNIT_ASSERT(!aliceAccount->connectionManager().isConnecting(bobDeviceId, "sip"));*/
1036}
1037
1038void
1039ConnectionManagerTest::testCanSendBeacon()
1040{
1041 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1042 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1043 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1044
1045 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1046 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1047
1048 std::mutex mtx;
1049 std::unique_lock<std::mutex> lk {mtx};
1050 std::condition_variable cv;
1051 bool successfullyConnected = false;
1052
1053 std::shared_ptr<MultiplexedSocket> aliceSocket, bobSocket;
1054 bobAccount->connectionManager().onChannelRequest(
1055 [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1056 bobAccount->connectionManager().onConnectionReady(
1057 [&](const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket) {
1058 if (socket && socket->name() == "sip")
1059 bobSocket = socket->underlyingSocket();
1060 cv.notify_one();
1061 });
1062
1063 aliceAccount->connectionManager().connectDevice(bobDeviceId,
1064 "sip",
1065 [&](std::shared_ptr<ChannelSocket> socket,
1066 const DeviceId&) {
1067 if (socket) {
1068 aliceSocket = socket->underlyingSocket();
1069 successfullyConnected = true;
1070 }
1071 cv.notify_one();
1072 });
1073 // connectDevice is full async, so isConnecting will be true after a few ms.
1074 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return aliceSocket && bobSocket && successfullyConnected; }));
1075 CPPUNIT_ASSERT(aliceSocket->canSendBeacon());
1076
1077 // Because onConnectionReady is true before version is sent, we can wait a bit
1078 // before canSendBeacon is true.
1079 auto start = std::chrono::steady_clock::now();
1080 auto aliceCanSendBeacon = false;
1081 auto bobCanSendBeacon = false;
1082 do {
1083 aliceCanSendBeacon = aliceSocket->canSendBeacon();
1084 bobCanSendBeacon = bobSocket->canSendBeacon();
1085 if (!bobCanSendBeacon || !aliceCanSendBeacon)
1086 std::this_thread::sleep_for(1s);
1087 } while ((not bobCanSendBeacon or not aliceCanSendBeacon)
1088 and std::chrono::steady_clock::now() - start < 5s);
1089
1090 CPPUNIT_ASSERT(bobCanSendBeacon && aliceCanSendBeacon);*/
1091}
1092
1093void
1094ConnectionManagerTest::testCannotSendBeacon()
1095{
1096 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1097 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1098 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1099
1100 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1101 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1102
1103 std::mutex mtx;
1104 std::unique_lock<std::mutex> lk {mtx};
1105 std::condition_variable cv;
1106 bool successfullyConnected = false;
1107
1108 std::shared_ptr<MultiplexedSocket> aliceSocket, bobSocket;
1109 bobAccount->connectionManager().onChannelRequest(
1110 [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1111 bobAccount->connectionManager().onConnectionReady(
1112 [&](const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket) {
1113 if (socket && socket->name() == "sip")
1114 bobSocket = socket->underlyingSocket();
1115 cv.notify_one();
1116 });
1117
1118 aliceAccount->connectionManager().connectDevice(bobDeviceId,
1119 "sip",
1120 [&](std::shared_ptr<ChannelSocket> socket,
1121 const DeviceId&) {
1122 if (socket) {
1123 aliceSocket = socket->underlyingSocket();
1124 successfullyConnected = true;
1125 }
1126 cv.notify_one();
1127 });
1128 // connectDevice is full async, so isConnecting will be true after a few ms.
1129 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return aliceSocket && bobSocket; }));
1130
1131 int version = 1412;
1132 bobSocket->setOnVersionCb([&](auto v) {
1133 version = v;
1134 cv.notify_one();
1135 });
1136 aliceSocket->setVersion(0);
1137 aliceSocket->sendVersion();
1138 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return version == 0; }));
1139 CPPUNIT_ASSERT(!bobSocket->canSendBeacon());*/
1140}
1141
1142void
1143ConnectionManagerTest::testConnectivityChangeTriggerBeacon()
1144{
1145 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1146 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1147 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1148
1149 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1150 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1151
1152 std::mutex mtx;
1153 std::unique_lock<std::mutex> lk {mtx};
1154 std::condition_variable cv;
1155 bool successfullyConnected = false;
1156
1157 std::shared_ptr<MultiplexedSocket> aliceSocket, bobSocket;
1158 bobAccount->connectionManager().onChannelRequest(
1159 [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1160 bobAccount->connectionManager().onConnectionReady(
1161 [&](const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket) {
1162 if (socket && socket->name() == "sip")
1163 bobSocket = socket->underlyingSocket();
1164 cv.notify_one();
1165 });
1166
1167 aliceAccount->connectionManager().connectDevice(bobDeviceId,
1168 "sip",
1169 [&](std::shared_ptr<ChannelSocket> socket,
1170 const DeviceId&) {
1171 if (socket) {
1172 aliceSocket = socket->underlyingSocket();
1173 successfullyConnected = true;
1174 }
1175 cv.notify_one();
1176 });
1177 // connectDevice is full async, so isConnecting will be true after a few ms.
1178 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return aliceSocket && bobSocket; }));
1179
1180 bool hasRequest = false;
1181 bobSocket->setOnBeaconCb([&](auto p) {
1182 if (p)
1183 hasRequest = true;
1184 cv.notify_one();
1185 });
1186 aliceAccount->connectionManager().connectivityChanged();
1187 CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return hasRequest; }));*/
1188}
1189
1190void
1191ConnectionManagerTest::testOnNoBeaconTriggersShutdown()
1192{
1193 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1194 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1195 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1196
1197 bobAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1198 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1199
1200 std::mutex mtx;
1201 std::unique_lock<std::mutex> lk {mtx};
1202 std::condition_variable cv;
1203 bool successfullyConnected = false;
1204
1205 std::shared_ptr<MultiplexedSocket> aliceSocket, bobSocket;
1206 bobAccount->connectionManager().onChannelRequest(
1207 [&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string&) { return true; });
1208 bobAccount->connectionManager().onConnectionReady(
1209 [&](const DeviceId&, const std::string&, std::shared_ptr<ChannelSocket> socket) {
1210 if (socket && socket->name() == "sip")
1211 bobSocket = socket->underlyingSocket();
1212 cv.notify_one();
1213 });
1214
1215 aliceAccount->connectionManager().connectDevice(bobDeviceId,
1216 "sip",
1217 [&](std::shared_ptr<ChannelSocket> socket,
1218 const DeviceId&) {
1219 if (socket) {
1220 aliceSocket = socket->underlyingSocket();
1221 successfullyConnected = true;
1222 }
1223 cv.notify_one();
1224 });
1225 // connectDevice is full async, so isConnecting will be true after a few ms.
1226 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return aliceSocket && bobSocket; }));
1227
1228 bool isClosed = false;
1229 aliceSocket->onShutdown([&] {
1230 isClosed = true;
1231 cv.notify_one();
1232 });
1233 bobSocket->answerToBeacon(false);
1234 aliceAccount->connectionManager().connectivityChanged();
1235 CPPUNIT_ASSERT(cv.wait_for(lk, 10s, [&] { return isClosed; }));*/
1236}
1237
1238void
1239ConnectionManagerTest::testShutdownWhileNegotiating()
1240{
1241 /*auto aliceAccount = Manager::instance().getAccount<JamiAccount>(aliceId);
1242 auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
1243 auto bobDeviceId = DeviceId(std::string(bobAccount->currentDeviceId()));
1244
1245 aliceAccount->connectionManager().onICERequest([](const DeviceId&) { return true; });
1246
1247 std::mutex mtx;
1248 std::unique_lock<std::mutex> lk {mtx};
1249 std::condition_variable cv;
1250 bool successfullyReceive = false;
1251 bool notConnected = false;
1252
1253 bobAccount->connectionManager().onICERequest([&](const DeviceId&) {
1254 successfullyReceive = true;
1255 cv.notify_one();
1256 return true;
1257 });
1258
1259 aliceAccount->connectionManager().connectDevice(bobDeviceId,
1260 "git://*",
1261 [&](std::shared_ptr<ChannelSocket> socket,
1262 const DeviceId&) {
1263 notConnected = !socket;
1264 cv.notify_one();
1265 });
1266
1267 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return successfullyReceive; }));
1268 Manager::instance().setAccountActive(aliceId, false, true);
1269 CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return notConnected; }));*/
1270}
1271
1272} // namespace test
Sébastien Blin464bdff2023-07-19 08:02:53 -04001273} // namespace dhtnet
Adrien Béraudefe27372023-05-27 18:56:29 -04001274
Adrien Béraud1ae60aa2023-07-07 09:55:09 -04001275JAMI_TEST_RUNNER(dhtnet::test::ConnectionManagerTest::name())