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