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