Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021-2024 Savoir-faire Linux Inc. |
| 3 | * Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <cppunit/TestAssert.h> |
| 20 | #include <cppunit/TestFixture.h> |
| 21 | #include <cppunit/extensions/HelperMacros.h> |
| 22 | #include <regex> |
| 23 | |
| 24 | #include <condition_variable> |
| 25 | #include <asio/executor_work_guard.hpp> |
| 26 | #include <asio/io_context.hpp> |
| 27 | |
| 28 | #include "opendht/dhtrunner.h" |
François-Simon Fauteux-Chapleau | fbb4608 | 2024-04-15 10:00:25 -0400 | [diff] [blame] | 29 | #include "opendht/sockaddr.h" |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 30 | #include "opendht/thread_pool.h" |
| 31 | #include "test_runner.h" |
| 32 | #include "upnp/upnp_context.h" |
| 33 | #include "ice_transport.h" |
| 34 | #include "ice_transport_factory.h" |
| 35 | |
| 36 | |
| 37 | namespace dhtnet { |
| 38 | namespace test { |
| 39 | |
| 40 | class IceTest : public CppUnit::TestFixture |
| 41 | { |
| 42 | public: |
| 43 | IceTest() |
| 44 | { |
| 45 | |
| 46 | } |
| 47 | ~IceTest() {} |
| 48 | static std::string name() { return "Ice"; } |
| 49 | void setUp(); |
| 50 | void tearDown(); |
| 51 | |
| 52 | // For future tests with publicIp |
| 53 | std::shared_ptr<dht::DhtRunner> dht_ {}; |
| 54 | std::unique_ptr<dhtnet::IpAddr> turnV4_ {}; |
| 55 | |
| 56 | std::shared_ptr<asio::io_context> ioContext; |
| 57 | std::shared_ptr<std::thread> ioContextRunner; |
| 58 | std::shared_ptr<IceTransportFactory> factory; |
| 59 | std::shared_ptr<upnp::UPnPContext> upnpContext; |
| 60 | |
| 61 | private: |
| 62 | void testRawIceConnection(); |
| 63 | void testTurnMasterIceConnection(); |
| 64 | void testTurnSlaveIceConnection(); |
| 65 | void testReceiveTooManyCandidates(); |
| 66 | void testCompleteOnFailure(); |
| 67 | |
| 68 | CPPUNIT_TEST_SUITE(IceTest); |
| 69 | CPPUNIT_TEST(testRawIceConnection); |
| 70 | CPPUNIT_TEST(testTurnMasterIceConnection); |
| 71 | CPPUNIT_TEST(testTurnSlaveIceConnection); |
| 72 | CPPUNIT_TEST(testReceiveTooManyCandidates); |
| 73 | CPPUNIT_TEST(testCompleteOnFailure); |
| 74 | CPPUNIT_TEST_SUITE_END(); |
| 75 | }; |
| 76 | |
| 77 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(IceTest, IceTest::name()); |
| 78 | |
| 79 | void |
| 80 | IceTest::setUp() |
| 81 | { |
| 82 | if (!dht_) { |
| 83 | dht_ = std::make_shared<dht::DhtRunner>(); |
| 84 | dht::DhtRunner::Config config {}; |
| 85 | dht::DhtRunner::Context context {}; |
François-Simon Fauteux-Chapleau | fbb4608 | 2024-04-15 10:00:25 -0400 | [diff] [blame] | 86 | |
| 87 | std::mutex mtx; |
| 88 | std::unique_lock lk(mtx); |
| 89 | std::condition_variable cv; |
| 90 | context.publicAddressChangedCb = [&](std::vector<dht::SockAddr> addr) { |
| 91 | if (addr.size() != 0) |
| 92 | cv.notify_all(); |
| 93 | }; |
| 94 | |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 95 | dht_->run(0, config, std::move(context)); |
| 96 | dht_->bootstrap("bootstrap.jami.net:4222"); |
François-Simon Fauteux-Chapleau | fbb4608 | 2024-04-15 10:00:25 -0400 | [diff] [blame] | 97 | // Wait for the DHT's public address to be available, otherwise the assertion that |
| 98 | // `addr4.size() != 0` at the beginning of several of the tests will fail. |
| 99 | cv.wait_for(lk, std::chrono::seconds(5), [&] { |
| 100 | return dht_->getPublicAddress().size() != 0; |
| 101 | }); |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 102 | } |
| 103 | if (!turnV4_) { |
| 104 | turnV4_ = std::make_unique<dhtnet::IpAddr>("turn.jami.net", AF_INET); |
| 105 | } |
| 106 | if (!upnpContext) { |
| 107 | if (!ioContext) { |
| 108 | ioContext = std::make_shared<asio::io_context>(); |
François-Simon Fauteux-Chapleau | 0f82547 | 2024-03-27 16:19:37 -0400 | [diff] [blame] | 109 | ioContextRunner = std::make_shared<std::thread>([&] { |
| 110 | auto work = asio::make_work_guard(*ioContext); |
| 111 | ioContext->run(); |
| 112 | }); |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 113 | } |
| 114 | upnpContext = std::make_shared<dhtnet::upnp::UPnPContext>(ioContext, nullptr); |
| 115 | } |
| 116 | if (!factory) { |
| 117 | factory = std::make_shared<IceTransportFactory>(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | void |
| 123 | IceTest::tearDown() |
| 124 | { |
| 125 | upnpContext->shutdown(); |
| 126 | ioContext->stop(); |
| 127 | if (ioContextRunner && ioContextRunner->joinable()) { |
| 128 | ioContextRunner->join(); |
| 129 | } |
| 130 | dht_.reset(); |
| 131 | turnV4_.reset(); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | IceTest::testRawIceConnection() |
| 136 | { |
| 137 | dhtnet::IceTransportOptions ice_config; |
| 138 | ice_config.upnpEnable = true; |
| 139 | ice_config.tcpEnable = true; |
| 140 | std::shared_ptr<dhtnet::IceTransport> ice_master, ice_slave; |
| 141 | std::mutex mtx, mtx_create, mtx_resp, mtx_init; |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 142 | std::unique_lock lk {mtx}, lk_create {mtx_create}, lk_resp {mtx_resp}, |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 143 | lk_init {mtx_init}; |
| 144 | std::condition_variable cv, cv_create, cv_resp, cv_init; |
| 145 | std::string init = {}; |
| 146 | std::string response = {}; |
| 147 | bool iceMasterReady = false, iceSlaveReady = false; |
| 148 | ice_config.onInitDone = [&](bool ok) { |
| 149 | CPPUNIT_ASSERT(ok); |
| 150 | dht::ThreadPool::io().run([&] { |
| 151 | CPPUNIT_ASSERT(cv_create.wait_for(lk_create, std::chrono::seconds(10), [&] { |
| 152 | return ice_master != nullptr; |
| 153 | })); |
| 154 | auto iceAttributes = ice_master->getLocalAttributes(); |
| 155 | std::stringstream icemsg; |
| 156 | icemsg << iceAttributes.ufrag << "\n"; |
| 157 | icemsg << iceAttributes.pwd << "\n"; |
| 158 | for (const auto& addr : ice_master->getLocalCandidates(1)) { |
| 159 | icemsg << addr << "\n"; |
| 160 | fmt::print("Added local ICE candidate {}\n", addr); |
| 161 | } |
| 162 | init = icemsg.str(); |
| 163 | cv_init.notify_one(); |
| 164 | CPPUNIT_ASSERT(cv_resp.wait_for(lk_resp, std::chrono::seconds(10), [&] { |
| 165 | return !response.empty(); |
| 166 | })); |
| 167 | auto sdp = ice_master->parseIceCandidates(response); |
| 168 | CPPUNIT_ASSERT( |
| 169 | ice_master->startIce({sdp.rem_ufrag, sdp.rem_pwd}, std::move(sdp.rem_candidates))); |
| 170 | }); |
| 171 | }; |
| 172 | ice_config.onNegoDone = [&](bool ok) { |
| 173 | iceMasterReady = ok; |
| 174 | cv.notify_one(); |
| 175 | }; |
| 176 | ice_config.master = true; |
| 177 | ice_config.streamsCount = 1; |
| 178 | ice_config.compCountPerStream = 1; |
| 179 | ice_config.upnpContext = upnpContext; |
| 180 | ice_config.factory = factory; |
| 181 | |
| 182 | ice_master = factory->createTransport("master ICE"); |
| 183 | ice_master->initIceInstance(ice_config); |
| 184 | cv_create.notify_all(); |
| 185 | ice_config.onInitDone = [&](bool ok) { |
| 186 | CPPUNIT_ASSERT(ok); |
| 187 | dht::ThreadPool::io().run([&] { |
| 188 | CPPUNIT_ASSERT(cv_create.wait_for(lk_create, std::chrono::seconds(10), [&] { |
| 189 | return ice_slave != nullptr; |
| 190 | })); |
| 191 | auto iceAttributes = ice_slave->getLocalAttributes(); |
| 192 | std::stringstream icemsg; |
| 193 | icemsg << iceAttributes.ufrag << "\n"; |
| 194 | icemsg << iceAttributes.pwd << "\n"; |
| 195 | for (const auto& addr : ice_slave->getLocalCandidates(1)) { |
| 196 | icemsg << addr << "\n"; |
| 197 | fmt::print("Added local ICE candidate {}\n", addr); |
| 198 | } |
| 199 | response = icemsg.str(); |
| 200 | cv_resp.notify_one(); |
| 201 | CPPUNIT_ASSERT( |
| 202 | cv_init.wait_for(lk_resp, std::chrono::seconds(10), [&] { return !init.empty(); })); |
| 203 | auto sdp = ice_slave->parseIceCandidates(init); |
| 204 | CPPUNIT_ASSERT( |
| 205 | ice_slave->startIce({sdp.rem_ufrag, sdp.rem_pwd}, std::move(sdp.rem_candidates))); |
| 206 | }); |
| 207 | }; |
| 208 | ice_config.onNegoDone = [&](bool ok) { |
| 209 | iceSlaveReady = ok; |
| 210 | cv.notify_one(); |
| 211 | }; |
| 212 | ice_config.master = false; |
| 213 | ice_config.streamsCount = 1; |
| 214 | ice_config.compCountPerStream = 1; |
| 215 | ice_config.upnpContext = upnpContext; |
| 216 | ice_config.factory = factory; |
| 217 | |
| 218 | ice_slave = factory->createTransport("slave ICE"); |
| 219 | ice_slave->initIceInstance(ice_config); |
| 220 | |
| 221 | cv_create.notify_all(); |
| 222 | CPPUNIT_ASSERT( |
| 223 | cv.wait_for(lk, std::chrono::seconds(10), [&] { return iceMasterReady && iceSlaveReady; })); |
| 224 | } |
| 225 | |
| 226 | void |
| 227 | IceTest::testTurnMasterIceConnection() |
| 228 | { |
| 229 | const auto& addr4 = dht_->getPublicAddress(AF_INET); |
| 230 | CPPUNIT_ASSERT(addr4.size() != 0); |
| 231 | CPPUNIT_ASSERT(turnV4_); |
| 232 | dhtnet::IceTransportOptions ice_config; |
| 233 | ice_config.upnpEnable = true; |
| 234 | ice_config.tcpEnable = true; |
| 235 | std::shared_ptr<dhtnet::IceTransport> ice_master, ice_slave; |
| 236 | std::mutex mtx, mtx_create, mtx_resp, mtx_init; |
| 237 | std::condition_variable cv, cv_create, cv_resp, cv_init; |
| 238 | std::string init = {}; |
| 239 | std::string response = {}; |
| 240 | bool iceMasterReady = false, iceSlaveReady = false; |
| 241 | |
| 242 | // Master |
| 243 | ice_config.onInitDone = [&](bool ok) { |
| 244 | CPPUNIT_ASSERT(ok); |
| 245 | dht::ThreadPool::io().run([&] { |
| 246 | /*{ |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 247 | std::unique_lock lk_create {mtx_create}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 248 | CPPUNIT_ASSERT(cv_create.wait_for(lk_create, std::chrono::seconds(10), [&] { |
| 249 | return ice_master != nullptr; |
| 250 | })); |
| 251 | }*/ |
| 252 | auto iceAttributes = ice_master->getLocalAttributes(); |
| 253 | std::stringstream icemsg; |
| 254 | icemsg << iceAttributes.ufrag << "\n"; |
| 255 | icemsg << iceAttributes.pwd << "\n"; |
| 256 | |
| 257 | for (const auto& addr : ice_master->getLocalCandidates(1)) { |
| 258 | |
| 259 | if (addr.find("host") == std::string::npos) { |
| 260 | // We only want to add relayed + public ip |
| 261 | icemsg << addr << "\n"; |
| 262 | fmt::print("Added local ICE candidate {}\n", addr); |
| 263 | } else { |
| 264 | // Replace host by non existing IP (we still need host to not fail the start) |
| 265 | std::regex e("((?:[0-9]{1,3}\\.){3}[0-9]{1,3})"); |
| 266 | auto newaddr = std::regex_replace(addr, e, "100.100.100.100"); |
| 267 | if (newaddr != addr) |
| 268 | icemsg << newaddr << "\n"; |
| 269 | } |
| 270 | } |
| 271 | { |
| 272 | std::lock_guard lk {mtx_init}; |
| 273 | init = icemsg.str(); |
| 274 | cv_init.notify_one(); |
| 275 | } |
| 276 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 277 | std::unique_lock lk_resp {mtx_resp}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 278 | CPPUNIT_ASSERT(cv_resp.wait_for(lk_resp, std::chrono::seconds(10), [&] { |
| 279 | return !response.empty(); |
| 280 | })); |
| 281 | auto sdp = ice_master->parseIceCandidates(response); |
| 282 | CPPUNIT_ASSERT( |
| 283 | ice_master->startIce({sdp.rem_ufrag, sdp.rem_pwd}, std::move(sdp.rem_candidates))); |
| 284 | } |
| 285 | }); |
| 286 | }; |
| 287 | ice_config.onNegoDone = [&](bool ok) { |
| 288 | std::lock_guard lk {mtx}; |
| 289 | iceMasterReady = ok; |
| 290 | cv.notify_one(); |
| 291 | }; |
| 292 | ice_config.accountPublicAddr = dhtnet::IpAddr(*addr4[0].get()); |
| 293 | ice_config.accountLocalAddr = dhtnet::ip_utils::getLocalAddr(AF_INET); |
| 294 | ice_config.turnServers.emplace_back(dhtnet::TurnServerInfo() |
| 295 | .setUri(turnV4_->toString(true)) |
| 296 | .setUsername("ring") |
| 297 | .setPassword("ring") |
| 298 | .setRealm("ring")); |
| 299 | ice_config.master = true; |
| 300 | ice_config.streamsCount = 1; |
| 301 | ice_config.compCountPerStream = 1; |
| 302 | ice_config.upnpContext = upnpContext; |
| 303 | ice_config.factory = factory; |
| 304 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 305 | std::unique_lock lk_create {mtx_create}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 306 | ice_master = factory->createTransport("master ICE"); |
| 307 | ice_master->initIceInstance(ice_config); |
| 308 | cv_create.notify_all(); |
| 309 | } |
| 310 | |
| 311 | // Slave |
| 312 | ice_config.turnServers = {}; |
| 313 | ice_config.onInitDone = [&](bool ok) { |
| 314 | CPPUNIT_ASSERT(ok); |
| 315 | dht::ThreadPool::io().run([&] { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 316 | /*std::unique_lock lk_create {mtx_create}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 317 | CPPUNIT_ASSERT(cv_create.wait_for(lk_create, std::chrono::seconds(10), [&] { |
| 318 | return ice_slave != nullptr; |
| 319 | }));*/ |
| 320 | auto iceAttributes = ice_slave->getLocalAttributes(); |
| 321 | std::stringstream icemsg; |
| 322 | icemsg << iceAttributes.ufrag << "\n"; |
| 323 | icemsg << iceAttributes.pwd << "\n"; |
| 324 | for (const auto& addr : ice_slave->getLocalCandidates(1)) { |
| 325 | if (addr.find("host") == std::string::npos) { |
| 326 | // We only want to add relayed + public ip |
| 327 | icemsg << addr << "\n"; |
| 328 | fmt::print("Added local ICE candidate {}\n", addr); |
| 329 | } else { |
| 330 | // Replace host by non existing IP (we still need host to not fail the start) |
| 331 | std::regex e("((?:[0-9]{1,3}\\.){3}[0-9]{1,3})"); |
| 332 | auto newaddr = std::regex_replace(addr, e, "100.100.100.100"); |
| 333 | if (newaddr != addr) |
| 334 | icemsg << newaddr << "\n"; |
| 335 | } |
| 336 | } |
| 337 | { |
| 338 | std::lock_guard lk {mtx_resp}; |
| 339 | response = icemsg.str(); |
| 340 | cv_resp.notify_one(); |
| 341 | } |
| 342 | { |
| 343 | std::unique_lock lk {mtx_init}; |
| 344 | CPPUNIT_ASSERT( |
| 345 | cv_init.wait_for(lk, std::chrono::seconds(10), [&] { return !init.empty(); })); |
| 346 | auto sdp = ice_slave->parseIceCandidates(init); |
| 347 | CPPUNIT_ASSERT( |
| 348 | ice_slave->startIce({sdp.rem_ufrag, sdp.rem_pwd}, std::move(sdp.rem_candidates))); |
| 349 | } |
| 350 | }); |
| 351 | }; |
| 352 | ice_config.onNegoDone = [&](bool ok) { |
| 353 | std::lock_guard lk {mtx}; |
| 354 | iceSlaveReady = ok; |
| 355 | cv.notify_one(); |
| 356 | }; |
| 357 | ice_config.master = false; |
| 358 | ice_config.streamsCount = 1; |
| 359 | ice_config.compCountPerStream = 1; |
| 360 | ice_config.upnpContext = upnpContext; |
| 361 | ice_config.factory = factory; |
| 362 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 363 | std::unique_lock lk_create {mtx_create}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 364 | ice_slave = factory->createTransport("slave ICE"); |
| 365 | ice_slave->initIceInstance(ice_config); |
| 366 | cv_create.notify_all(); |
| 367 | } |
| 368 | std::unique_lock lk {mtx}; |
| 369 | CPPUNIT_ASSERT( |
| 370 | cv.wait_for(lk, std::chrono::seconds(10), [&] { return iceMasterReady; })); |
| 371 | CPPUNIT_ASSERT( |
| 372 | cv.wait_for(lk, std::chrono::seconds(10), [&] { return iceSlaveReady; })); |
| 373 | |
| 374 | CPPUNIT_ASSERT(ice_master->getLocalAddress(1).toString(false) == turnV4_->toString(false)); |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | IceTest::testTurnSlaveIceConnection() |
| 379 | { |
| 380 | const auto& addr4 = dht_->getPublicAddress(AF_INET); |
| 381 | CPPUNIT_ASSERT(addr4.size() != 0); |
| 382 | CPPUNIT_ASSERT(turnV4_); |
| 383 | dhtnet::IceTransportOptions ice_config; |
| 384 | ice_config.upnpEnable = true; |
| 385 | ice_config.tcpEnable = true; |
| 386 | std::shared_ptr<dhtnet::IceTransport> ice_master, ice_slave; |
| 387 | std::mutex mtx, mtx_create, mtx_resp, mtx_init; |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 388 | std::unique_lock lk {mtx}, lk_create {mtx_create}, lk_resp {mtx_resp}, |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 389 | lk_init {mtx_init}; |
| 390 | std::condition_variable cv, cv_create, cv_resp, cv_init; |
| 391 | std::string init = {}; |
| 392 | std::string response = {}; |
| 393 | bool iceMasterReady = false, iceSlaveReady = false; |
| 394 | ice_config.onInitDone = [&](bool ok) { |
| 395 | CPPUNIT_ASSERT(ok); |
| 396 | dht::ThreadPool::io().run([&] { |
| 397 | CPPUNIT_ASSERT(cv_create.wait_for(lk_create, std::chrono::seconds(10), [&] { |
| 398 | return ice_master != nullptr; |
| 399 | })); |
| 400 | auto iceAttributes = ice_master->getLocalAttributes(); |
| 401 | std::stringstream icemsg; |
| 402 | icemsg << iceAttributes.ufrag << "\n"; |
| 403 | icemsg << iceAttributes.pwd << "\n"; |
| 404 | for (const auto& addr : ice_master->getLocalCandidates(1)) { |
| 405 | if (addr.find("host") == std::string::npos) { |
| 406 | // We only want to add relayed + public ip |
| 407 | icemsg << addr << "\n"; |
| 408 | fmt::print("Added local ICE candidate {}\n", addr); |
| 409 | } else { |
| 410 | // Replace host by non existing IP (we still need host to not fail the start) |
| 411 | std::regex e("((?:[0-9]{1,3}\\.){3}[0-9]{1,3})"); |
| 412 | auto newaddr = std::regex_replace(addr, e, "100.100.100.100"); |
| 413 | if (newaddr != addr) |
| 414 | icemsg << newaddr << "\n"; |
| 415 | } |
| 416 | } |
| 417 | init = icemsg.str(); |
| 418 | cv_init.notify_one(); |
| 419 | CPPUNIT_ASSERT(cv_resp.wait_for(lk_resp, std::chrono::seconds(10), [&] { |
| 420 | return !response.empty(); |
| 421 | })); |
| 422 | auto sdp = ice_master->parseIceCandidates(response); |
| 423 | CPPUNIT_ASSERT( |
| 424 | ice_master->startIce({sdp.rem_ufrag, sdp.rem_pwd}, std::move(sdp.rem_candidates))); |
| 425 | }); |
| 426 | }; |
| 427 | ice_config.onNegoDone = [&](bool ok) { |
| 428 | iceMasterReady = ok; |
| 429 | cv.notify_one(); |
| 430 | }; |
| 431 | ice_config.accountPublicAddr = dhtnet::IpAddr(*addr4[0].get()); |
| 432 | ice_config.accountLocalAddr = dhtnet::ip_utils::getLocalAddr(AF_INET); |
| 433 | ice_config.master = true; |
| 434 | ice_config.streamsCount = 1; |
| 435 | ice_config.compCountPerStream = 1; |
| 436 | ice_config.upnpContext = upnpContext; |
| 437 | ice_config.factory = factory; |
| 438 | ice_master = factory->createTransport("master ICE"); |
| 439 | ice_master->initIceInstance(ice_config); |
| 440 | cv_create.notify_all(); |
| 441 | ice_config.onInitDone = [&](bool ok) { |
| 442 | CPPUNIT_ASSERT(ok); |
| 443 | dht::ThreadPool::io().run([&] { |
| 444 | CPPUNIT_ASSERT(cv_create.wait_for(lk_create, std::chrono::seconds(10), [&] { |
| 445 | return ice_slave != nullptr; |
| 446 | })); |
| 447 | auto iceAttributes = ice_slave->getLocalAttributes(); |
| 448 | std::stringstream icemsg; |
| 449 | icemsg << iceAttributes.ufrag << "\n"; |
| 450 | icemsg << iceAttributes.pwd << "\n"; |
| 451 | for (const auto& addr : ice_slave->getLocalCandidates(1)) { |
| 452 | if (addr.find("host") == std::string::npos) { |
| 453 | // We only want to add relayed + public ip |
| 454 | icemsg << addr << "\n"; |
| 455 | fmt::print("Added local ICE candidate {}\n", addr); |
| 456 | } else { |
| 457 | // Replace host by non existing IP (we still need host to not fail the start) |
| 458 | std::regex e("((?:[0-9]{1,3}\\.){3}[0-9]{1,3})"); |
| 459 | auto newaddr = std::regex_replace(addr, e, "100.100.100.100"); |
| 460 | if (newaddr != addr) |
| 461 | icemsg << newaddr << "\n"; |
| 462 | } |
| 463 | } |
| 464 | response = icemsg.str(); |
| 465 | cv_resp.notify_one(); |
| 466 | CPPUNIT_ASSERT( |
| 467 | cv_init.wait_for(lk_resp, std::chrono::seconds(10), [&] { return !init.empty(); })); |
| 468 | auto sdp = ice_slave->parseIceCandidates(init); |
| 469 | CPPUNIT_ASSERT( |
| 470 | ice_slave->startIce({sdp.rem_ufrag, sdp.rem_pwd}, std::move(sdp.rem_candidates))); |
| 471 | }); |
| 472 | }; |
| 473 | ice_config.onNegoDone = [&](bool ok) { |
| 474 | iceSlaveReady = ok; |
| 475 | cv.notify_one(); |
| 476 | }; |
| 477 | ice_config.turnServers.emplace_back(dhtnet::TurnServerInfo() |
| 478 | .setUri(turnV4_->toString(true)) |
| 479 | .setUsername("ring") |
| 480 | .setPassword("ring") |
| 481 | .setRealm("ring")); |
| 482 | ice_config.master = false; |
| 483 | ice_config.streamsCount = 1; |
| 484 | ice_config.compCountPerStream = 1; |
| 485 | ice_config.upnpContext = upnpContext; |
| 486 | ice_config.factory = factory; |
| 487 | ice_slave = factory->createTransport("slave ICE"); |
| 488 | ice_slave->initIceInstance(ice_config); |
| 489 | cv_create.notify_all(); |
| 490 | CPPUNIT_ASSERT( |
| 491 | cv.wait_for(lk, std::chrono::seconds(10), [&] { return iceMasterReady && iceSlaveReady; })); |
| 492 | CPPUNIT_ASSERT(ice_slave->getLocalAddress(1).toString(false) == turnV4_->toString(false)); |
| 493 | } |
| 494 | |
| 495 | void |
| 496 | IceTest::testReceiveTooManyCandidates() |
| 497 | { |
| 498 | const auto& addr4 = dht_->getPublicAddress(AF_INET); |
| 499 | CPPUNIT_ASSERT(addr4.size() != 0); |
| 500 | CPPUNIT_ASSERT(turnV4_); |
| 501 | dhtnet::IceTransportOptions ice_config; |
| 502 | ice_config.upnpEnable = true; |
| 503 | ice_config.tcpEnable = true; |
| 504 | std::shared_ptr<dhtnet::IceTransport> ice_master, ice_slave; |
| 505 | std::mutex mtx, mtx_create, mtx_resp, mtx_init; |
| 506 | std::condition_variable cv, cv_create, cv_resp, cv_init; |
| 507 | std::string init = {}; |
| 508 | std::string response = {}; |
| 509 | bool iceMasterReady = false, iceSlaveReady = false; |
| 510 | ice_config.onInitDone = [&](bool ok) { |
| 511 | CPPUNIT_ASSERT(ok); |
| 512 | dht::ThreadPool::io().run([&] { |
| 513 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 514 | std::unique_lock lk_create {mtx_create}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 515 | CPPUNIT_ASSERT(cv_create.wait_for(lk_create, std::chrono::seconds(10), [&] { |
| 516 | return ice_master != nullptr; |
| 517 | })); |
| 518 | } |
| 519 | auto iceAttributes = ice_master->getLocalAttributes(); |
| 520 | std::stringstream icemsg; |
| 521 | icemsg << iceAttributes.ufrag << "\n"; |
| 522 | icemsg << iceAttributes.pwd << "\n"; |
| 523 | for (const auto& addr : ice_master->getLocalCandidates(1)) { |
| 524 | icemsg << addr << "\n"; |
| 525 | fmt::print("Added local ICE candidate {}\n", addr); |
| 526 | } |
| 527 | init = icemsg.str(); |
| 528 | cv_init.notify_one(); |
| 529 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 530 | std::unique_lock lk_resp {mtx_resp}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 531 | CPPUNIT_ASSERT(cv_resp.wait_for(lk_resp, std::chrono::seconds(10), [&] { |
| 532 | return !response.empty(); |
| 533 | })); |
| 534 | auto sdp = ice_master->parseIceCandidates(response); |
| 535 | CPPUNIT_ASSERT( |
| 536 | ice_master->startIce({sdp.rem_ufrag, sdp.rem_pwd}, std::move(sdp.rem_candidates))); |
| 537 | } |
| 538 | }); |
| 539 | }; |
| 540 | ice_config.onNegoDone = [&](bool ok) { |
| 541 | iceMasterReady = ok; |
| 542 | cv.notify_one(); |
| 543 | }; |
| 544 | ice_config.accountPublicAddr = dhtnet::IpAddr(*addr4[0].get()); |
| 545 | ice_config.accountLocalAddr = dhtnet::ip_utils::getLocalAddr(AF_INET); |
| 546 | ice_config.turnServers.emplace_back(dhtnet::TurnServerInfo() |
| 547 | .setUri(turnV4_->toString(true)) |
| 548 | .setUsername("ring") |
| 549 | .setPassword("ring") |
| 550 | .setRealm("ring")); |
| 551 | ice_config.master = true; |
| 552 | ice_config.streamsCount = 1; |
| 553 | ice_config.compCountPerStream = 1; |
| 554 | ice_config.upnpContext = upnpContext; |
| 555 | ice_config.factory = factory; |
| 556 | |
| 557 | ice_master = factory->createTransport("master ICE"); |
| 558 | ice_master->initIceInstance(ice_config); |
| 559 | cv_create.notify_all(); |
| 560 | ice_config.onInitDone = [&](bool ok) { |
| 561 | CPPUNIT_ASSERT(ok); |
| 562 | dht::ThreadPool::io().run([&] { |
| 563 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 564 | std::unique_lock lk_create {mtx_create}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 565 | CPPUNIT_ASSERT(cv_create.wait_for(lk_create, std::chrono::seconds(10), [&] { |
| 566 | return ice_slave != nullptr; |
| 567 | })); |
| 568 | } |
| 569 | auto iceAttributes = ice_slave->getLocalAttributes(); |
| 570 | std::stringstream icemsg; |
| 571 | icemsg << iceAttributes.ufrag << "\n"; |
| 572 | icemsg << iceAttributes.pwd << "\n"; |
| 573 | for (const auto& addr : ice_master->getLocalCandidates(1)) { |
| 574 | icemsg << addr << "\n"; |
| 575 | fmt::print("Added local ICE candidate {}\n", addr); |
| 576 | } |
| 577 | for (auto i = 0; i < std::min(256, PJ_ICE_ST_MAX_CAND); ++i) { |
| 578 | icemsg << "Hc0a800a5 1 TCP 2130706431 192.168.0." << i |
| 579 | << " 43613 typ host tcptype passive" |
| 580 | << "\n"; |
| 581 | icemsg << "Hc0a800a5 1 TCP 2130706431 192.168.0." << i |
| 582 | << " 9 typ host tcptype active" |
| 583 | << "\n"; |
| 584 | } |
| 585 | { |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 586 | std::lock_guard lk_resp {mtx_resp}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 587 | response = icemsg.str(); |
| 588 | cv_resp.notify_one(); |
| 589 | } |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 590 | std::unique_lock lk_init {mtx_init}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 591 | CPPUNIT_ASSERT( |
| 592 | cv_init.wait_for(lk_init, std::chrono::seconds(10), [&] { return !init.empty(); })); |
| 593 | auto sdp = ice_slave->parseIceCandidates(init); |
| 594 | CPPUNIT_ASSERT( |
| 595 | ice_slave->startIce({sdp.rem_ufrag, sdp.rem_pwd}, std::move(sdp.rem_candidates))); |
| 596 | }); |
| 597 | }; |
| 598 | ice_config.onNegoDone = [&](bool ok) { |
| 599 | iceSlaveReady = ok; |
| 600 | cv.notify_one(); |
| 601 | }; |
| 602 | ice_config.master = false; |
| 603 | ice_config.streamsCount = 1; |
| 604 | ice_config.compCountPerStream = 1; |
| 605 | ice_config.upnpContext = upnpContext; |
| 606 | ice_config.factory = factory; |
| 607 | |
| 608 | ice_slave = factory->createTransport("slave ICE"); |
| 609 | ice_slave->initIceInstance(ice_config); |
| 610 | cv_create.notify_all(); |
| 611 | |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 612 | std::unique_lock lk {mtx}; |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 613 | CPPUNIT_ASSERT( |
| 614 | cv.wait_for(lk, std::chrono::seconds(10), [&] { return iceMasterReady && iceSlaveReady; })); |
| 615 | } |
| 616 | |
| 617 | void |
| 618 | IceTest::testCompleteOnFailure() |
| 619 | { |
| 620 | const auto& addr4 = dht_->getPublicAddress(AF_INET); |
| 621 | CPPUNIT_ASSERT(addr4.size() != 0); |
| 622 | CPPUNIT_ASSERT(turnV4_); |
| 623 | dhtnet::IceTransportOptions ice_config; |
| 624 | ice_config.upnpEnable = true; |
| 625 | ice_config.tcpEnable = true; |
| 626 | std::shared_ptr<dhtnet::IceTransport> ice_master, ice_slave; |
| 627 | std::mutex mtx, mtx_create, mtx_resp, mtx_init; |
Adrien Béraud | 024c46f | 2024-03-02 23:53:18 -0500 | [diff] [blame] | 628 | std::unique_lock lk {mtx}, lk_create {mtx_create}, lk_resp {mtx_resp}, |
Amna | 0b50a03 | 2024-02-13 14:42:26 -0500 | [diff] [blame] | 629 | lk_init {mtx_init}; |
| 630 | std::condition_variable cv, cv_create, cv_resp, cv_init; |
| 631 | std::string init = {}; |
| 632 | std::string response = {}; |
| 633 | bool iceMasterNotReady = false, iceSlaveNotReady = false; |
| 634 | ice_config.onInitDone = [&](bool ok) { |
| 635 | CPPUNIT_ASSERT(ok); |
| 636 | dht::ThreadPool::io().run([&] { |
| 637 | CPPUNIT_ASSERT(cv_create.wait_for(lk_create, std::chrono::seconds(10), [&] { |
| 638 | return ice_master != nullptr; |
| 639 | })); |
| 640 | auto iceAttributes = ice_master->getLocalAttributes(); |
| 641 | std::stringstream icemsg; |
| 642 | icemsg << iceAttributes.ufrag << "\n"; |
| 643 | icemsg << iceAttributes.pwd << "\n"; |
| 644 | for (const auto& addr : ice_master->getLocalCandidates(1)) { |
| 645 | if (addr.find("relay") != std::string::npos) { |
| 646 | // We only want to relayed and modify the rest (to have CONNREFUSED) |
| 647 | icemsg << addr << "\n"; |
| 648 | fmt::print("Added local ICE candidate {}\n", addr); |
| 649 | } else { |
| 650 | // Replace host by non existing IP (we still need host to not fail the start) |
| 651 | std::regex e("((?:[0-9]{1,3}\\.){3}[0-9]{1,3})"); |
| 652 | auto newaddr = std::regex_replace(addr, e, "100.100.100.100"); |
| 653 | if (newaddr != addr) |
| 654 | icemsg << newaddr << "\n"; |
| 655 | } |
| 656 | } |
| 657 | init = icemsg.str(); |
| 658 | cv_init.notify_one(); |
| 659 | CPPUNIT_ASSERT(cv_resp.wait_for(lk_resp, std::chrono::seconds(10), [&] { |
| 660 | return !response.empty(); |
| 661 | })); |
| 662 | auto sdp = ice_master->parseIceCandidates(response); |
| 663 | CPPUNIT_ASSERT( |
| 664 | ice_master->startIce({sdp.rem_ufrag, sdp.rem_pwd}, std::move(sdp.rem_candidates))); |
| 665 | }); |
| 666 | }; |
| 667 | ice_config.onNegoDone = [&](bool ok) { |
| 668 | iceMasterNotReady = !ok; |
| 669 | cv.notify_one(); |
| 670 | }; |
| 671 | ice_config.accountPublicAddr = dhtnet::IpAddr(*addr4[0].get()); |
| 672 | ice_config.accountLocalAddr = dhtnet::ip_utils::getLocalAddr(AF_INET); |
| 673 | ice_config.master = true; |
| 674 | ice_config.streamsCount = 1; |
| 675 | ice_config.compCountPerStream = 1; |
| 676 | ice_config.upnpContext = upnpContext; |
| 677 | ice_config.factory = factory; |
| 678 | ice_master = factory->createTransport("master ICE"); |
| 679 | ice_master->initIceInstance(ice_config); |
| 680 | cv_create.notify_all(); |
| 681 | ice_config.onInitDone = [&](bool ok) { |
| 682 | CPPUNIT_ASSERT(ok); |
| 683 | dht::ThreadPool::io().run([&] { |
| 684 | CPPUNIT_ASSERT(cv_create.wait_for(lk_create, std::chrono::seconds(10), [&] { |
| 685 | return ice_slave != nullptr; |
| 686 | })); |
| 687 | auto iceAttributes = ice_slave->getLocalAttributes(); |
| 688 | std::stringstream icemsg; |
| 689 | icemsg << iceAttributes.ufrag << "\n"; |
| 690 | icemsg << iceAttributes.pwd << "\n"; |
| 691 | for (const auto& addr : ice_slave->getLocalCandidates(1)) { |
| 692 | if (addr.find("relay") != std::string::npos) { |
| 693 | // We only want to relayed and modify the rest (to have CONNREFUSED) |
| 694 | icemsg << addr << "\n"; |
| 695 | fmt::print("Added local ICE candidate {}\n", addr); |
| 696 | } else { |
| 697 | // Replace host by non existing IP (we still need host to not fail the start) |
| 698 | std::regex e("((?:[0-9]{1,3}\\.){3}[0-9]{1,3})"); |
| 699 | auto newaddr = std::regex_replace(addr, e, "100.100.100.100"); |
| 700 | if (newaddr != addr) |
| 701 | icemsg << newaddr << "\n"; |
| 702 | } |
| 703 | } |
| 704 | response = icemsg.str(); |
| 705 | cv_resp.notify_one(); |
| 706 | CPPUNIT_ASSERT( |
| 707 | cv_init.wait_for(lk_resp, std::chrono::seconds(10), [&] { return !init.empty(); })); |
| 708 | auto sdp = ice_slave->parseIceCandidates(init); |
| 709 | CPPUNIT_ASSERT( |
| 710 | ice_slave->startIce({sdp.rem_ufrag, sdp.rem_pwd}, std::move(sdp.rem_candidates))); |
| 711 | }); |
| 712 | }; |
| 713 | ice_config.onNegoDone = [&](bool ok) { |
| 714 | iceSlaveNotReady = !ok; |
| 715 | cv.notify_one(); |
| 716 | }; |
| 717 | ice_config.turnServers.emplace_back(dhtnet::TurnServerInfo() |
| 718 | .setUri(turnV4_->toString(true)) |
| 719 | .setUsername("ring") |
| 720 | .setPassword("ring") |
| 721 | .setRealm("ring")); |
| 722 | ice_config.master = false; |
| 723 | ice_config.streamsCount = 1; |
| 724 | ice_config.compCountPerStream = 1; |
| 725 | ice_config.upnpContext = upnpContext; |
| 726 | ice_config.factory = factory; |
| 727 | ice_slave = factory->createTransport("slave ICE"); |
| 728 | ice_slave->initIceInstance(ice_config); |
| 729 | cv_create.notify_all(); |
| 730 | // Check that nego failed and callback called |
| 731 | CPPUNIT_ASSERT(cv.wait_for(lk, std::chrono::seconds(120), [&] { |
| 732 | return iceMasterNotReady && iceSlaveNotReady; |
| 733 | })); |
| 734 | } |
| 735 | |
| 736 | } // namespace test |
| 737 | } |
| 738 | |
| 739 | JAMI_TEST_RUNNER(dhtnet::test::IceTest::name()) |