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