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