| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <atomic> |
| 32 | #include <boost/archive/portable_binary_oarchive.hpp> |
| 33 | #include <boost/archive/portable_binary_iarchive.hpp> |
| 34 | #include <boost/asio/buffer.hpp> |
| 35 | #include <boost/asio/io_context.hpp> |
| 36 | #include <boost/asio/ip/tcp.hpp> |
| 37 | #include <boost/asio/read.hpp> |
| 38 | #include <boost/asio/steady_timer.hpp> |
| 39 | #include <boost/asio/write.hpp> |
| 40 | #include <boost/endian/conversion.hpp> |
| 41 | #include <boost/range/adaptor/sliced.hpp> |
| 42 | #include <boost/range/combine.hpp> |
| 43 | #include <boost/system/error_code.hpp> |
| 44 | #include <boost/thread/scoped_thread.hpp> |
| 45 | #include <boost/thread/thread.hpp> |
| 46 | #include <boost/uuid/nil_generator.hpp> |
| 47 | #include <boost/uuid/random_generator.hpp> |
| 48 | #include <boost/uuid/uuid.hpp> |
| 49 | #include <cstdint> |
| 50 | #include <cstring> |
| 51 | #include <functional> |
| 52 | #include <gtest/gtest.h> |
| 53 | #include <map> |
| 54 | #include <memory> |
| 55 | #include <type_traits> |
| 56 | |
| 57 | #include "crypto/crypto.h" |
| 58 | #include "net/dandelionpp.h" |
| 59 | #include "net/error.h" |
| 60 | #include "net/i2p_address.h" |
| 61 | #include "net/net_utils_base.h" |
| 62 | #include "net/socks.h" |
| 63 | #include "net/socks_connect.h" |
| 64 | #include "net/parse.h" |
| 65 | #include "net/tor_address.h" |
| 66 | #include "net/zmq.h" |
| 67 | #include "p2p/net_peerlist_boost_serialization.h" |
| 68 | #include "serialization/keyvalue_serialization.h" |
| 69 | #include "storages/portable_storage.h" |
| 70 | |
| 71 | namespace |
| 72 | { |
| 73 | static constexpr const char v2_onion[] = |
| 74 | "xmrto2bturnore26.onion"; |
| 75 | static constexpr const char v3_onion[] = |
| 76 | "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion"; |
| 77 | } |
| 78 | |
| 79 | TEST(tor_address, constants) |
| 80 | { |
| 81 | static_assert(!net::tor_address::is_local(), "bad is_local() response"); |
| 82 | static_assert(!net::tor_address::is_loopback(), "bad is_loopback() response"); |
| 83 | static_assert(net::tor_address::get_type_id() == epee::net_utils::address_type::tor, "bad get_type_id() response"); |
| 84 | |
| 85 | EXPECT_FALSE(net::tor_address::is_local()); |
| 86 | EXPECT_FALSE(net::tor_address::is_loopback()); |
| 87 | EXPECT_EQ(epee::net_utils::address_type::tor, net::tor_address::get_type_id()); |
| 88 | EXPECT_EQ(epee::net_utils::address_type::tor, net::tor_address::get_type_id()); |
| 89 | } |
| 90 | |
| 91 | TEST(tor_address, invalid) |
| 92 | { |
| 93 | EXPECT_TRUE(net::tor_address::make("").has_error()); |
| 94 | EXPECT_TRUE(net::tor_address::make(":").has_error()); |
| 95 | EXPECT_TRUE(net::tor_address::make(".onion").has_error()); |
| 96 | EXPECT_TRUE(net::tor_address::make(".onion:").has_error()); |
| 97 | EXPECT_TRUE(net::tor_address::make(v2_onion + 1).has_error()); |
| 98 | EXPECT_TRUE(net::tor_address::make(v3_onion + 1).has_error()); |
| 99 | EXPECT_TRUE(net::tor_address::make(boost::string_ref{v2_onion, sizeof(v2_onion) - 2}).has_error()); |
| 100 | EXPECT_TRUE(net::tor_address::make(boost::string_ref{v3_onion, sizeof(v3_onion) - 2}).has_error()); |
| 101 | EXPECT_TRUE(net::tor_address::make(std::string{v2_onion} + ":-").has_error()); |
| 102 | EXPECT_TRUE(net::tor_address::make(std::string{v2_onion} + ":900a").has_error()); |
| 103 | EXPECT_TRUE(net::tor_address::make(std::string{v3_onion} + ":65536").has_error()); |
| 104 | EXPECT_TRUE(net::tor_address::make(std::string{v3_onion} + ":-1").has_error()); |
| 105 | |
| 106 | std::string onion{v3_onion}; |
| 107 | onion.at(10) = 1; |
| 108 | EXPECT_TRUE(net::tor_address::make(onion).has_error()); |
| 109 | } |
| 110 | |
| 111 | TEST(tor_address, unblockable_types) |
| 112 | { |
| 113 | net::tor_address tor{}; |
| 114 | |
| 115 | ASSERT_NE(nullptr, tor.host_str()); |
| 116 | EXPECT_STREQ("<unknown tor host>", tor.host_str()); |
| 117 | EXPECT_STREQ("<unknown tor host>", tor.str().c_str()); |
| 118 | EXPECT_EQ(0u, tor.port()); |
| 119 | EXPECT_TRUE(tor.is_unknown()); |
| 120 | EXPECT_FALSE(tor.is_local()); |
| 121 | EXPECT_FALSE(tor.is_loopback()); |
| 122 | EXPECT_EQ(epee::net_utils::address_type::tor, tor.get_type_id()); |
| 123 | EXPECT_EQ(epee::net_utils::zone::tor, tor.get_zone()); |
| 124 | |
| 125 | tor = net::tor_address::unknown(); |
| 126 | ASSERT_NE(nullptr, tor.host_str()); |
| 127 | EXPECT_STREQ("<unknown tor host>", tor.host_str()); |
| 128 | EXPECT_STREQ("<unknown tor host>", tor.str().c_str()); |
| 129 | EXPECT_EQ(0u, tor.port()); |
| 130 | EXPECT_TRUE(tor.is_unknown()); |
| 131 | EXPECT_FALSE(tor.is_local()); |
| 132 | EXPECT_FALSE(tor.is_loopback()); |
| 133 | EXPECT_EQ(epee::net_utils::address_type::tor, tor.get_type_id()); |
| 134 | EXPECT_EQ(epee::net_utils::zone::tor, tor.get_zone()); |
| 135 | |
| 136 | EXPECT_EQ(net::tor_address{}, net::tor_address::unknown()); |
| 137 | } |
| 138 | |
| 139 | TEST(tor_address, valid) |
| 140 | { |
| 141 | const auto address1 = net::tor_address::make(v3_onion); |
| 142 | |
| 143 | ASSERT_TRUE(address1.has_value()); |
| 144 | EXPECT_EQ(0u, address1->port()); |
| 145 | EXPECT_STREQ(v3_onion, address1->host_str()); |
| 146 | EXPECT_STREQ(v3_onion, address1->str().c_str()); |
| 147 | EXPECT_TRUE(address1->is_blockable()); |
| 148 | |
| 149 | net::tor_address address2{*address1}; |
| 150 | |
| 151 | EXPECT_EQ(0u, address2.port()); |
| 152 | EXPECT_STREQ(v3_onion, address2.host_str()); |
| 153 | EXPECT_STREQ(v3_onion, address2.str().c_str()); |
| 154 | EXPECT_TRUE(address2.is_blockable()); |
| 155 | EXPECT_TRUE(address2.equal(*address1)); |
| 156 | EXPECT_TRUE(address1->equal(address2)); |
| 157 | EXPECT_TRUE(address2 == *address1); |
| 158 | EXPECT_TRUE(*address1 == address2); |
| 159 | EXPECT_FALSE(address2 != *address1); |
| 160 | EXPECT_FALSE(*address1 != address2); |
| 161 | EXPECT_TRUE(address2.is_same_host(*address1)); |
| 162 | EXPECT_TRUE(address1->is_same_host(address2)); |
| 163 | EXPECT_FALSE(address2.less(*address1)); |
| 164 | EXPECT_FALSE(address1->less(address2)); |
| 165 | |
| 166 | address2 = MONERO_UNWRAP(net::tor_address::make(std::string{v2_onion} + ":6545")); |
| 167 | |
| 168 | EXPECT_EQ(6545, address2.port()); |
| 169 | EXPECT_STREQ(v2_onion, address2.host_str()); |
| 170 | EXPECT_EQ(std::string{v2_onion} + ":6545", address2.str().c_str()); |
| 171 | EXPECT_TRUE(address2.is_blockable()); |
| 172 | EXPECT_FALSE(address2.equal(*address1)); |
| 173 | EXPECT_FALSE(address1->equal(address2)); |
| 174 | EXPECT_FALSE(address2 == *address1); |
| 175 | EXPECT_FALSE(*address1 == address2); |
| 176 | EXPECT_TRUE(address2 != *address1); |
| 177 | EXPECT_TRUE(*address1 != address2); |
| 178 | EXPECT_FALSE(address2.is_same_host(*address1)); |
| 179 | EXPECT_FALSE(address1->is_same_host(address2)); |
| 180 | EXPECT_FALSE(address2.less(*address1)); |
| 181 | EXPECT_TRUE(address1->less(address2)); |
| 182 | |
| 183 | net::tor_address address3 = MONERO_UNWRAP(net::tor_address::make(std::string{v3_onion} + ":", 65535)); |
| 184 | |
| 185 | EXPECT_EQ(65535, address3.port()); |
| 186 | EXPECT_STREQ(v3_onion, address3.host_str()); |
| 187 | EXPECT_EQ(std::string{v3_onion} + ":65535", address3.str().c_str()); |
| 188 | EXPECT_TRUE(address3.is_blockable()); |
| 189 | EXPECT_FALSE(address3.equal(*address1)); |
| 190 | EXPECT_FALSE(address1->equal(address3)); |
| 191 | EXPECT_FALSE(address3 == *address1); |
| 192 | EXPECT_FALSE(*address1 == address3); |
| 193 | EXPECT_TRUE(address3 != *address1); |
| 194 | EXPECT_TRUE(*address1 != address3); |
| 195 | EXPECT_TRUE(address3.is_same_host(*address1)); |
| 196 | EXPECT_TRUE(address1->is_same_host(address3)); |
| 197 | EXPECT_FALSE(address3.less(*address1)); |
| 198 | EXPECT_TRUE(address1->less(address3)); |
| 199 | |
| 200 | EXPECT_FALSE(address3.equal(address2)); |
| 201 | EXPECT_FALSE(address2.equal(address3)); |
| 202 | EXPECT_FALSE(address3 == address2); |
| 203 | EXPECT_FALSE(address2 == address3); |
| 204 | EXPECT_TRUE(address3 != address2); |
| 205 | EXPECT_TRUE(address2 != address3); |
| 206 | EXPECT_FALSE(address3.is_same_host(address2)); |
| 207 | EXPECT_FALSE(address2.is_same_host(address3)); |
| 208 | EXPECT_TRUE(address3.less(address2)); |
| 209 | EXPECT_FALSE(address2.less(address3)); |
| 210 | } |
| 211 | |
| 212 | TEST(tor_address, generic_network_address) |
| 213 | { |
| 214 | const epee::net_utils::network_address tor1{MONERO_UNWRAP(net::tor_address::make(v3_onion, 8080))}; |
| 215 | const epee::net_utils::network_address tor2{MONERO_UNWRAP(net::tor_address::make(v3_onion, 8080))}; |
| 216 | const epee::net_utils::network_address ip{epee::net_utils::ipv4_network_address{100, 200}}; |
| 217 | |
| 218 | EXPECT_EQ(tor1, tor2); |
| 219 | EXPECT_NE(ip, tor1); |
| 220 | EXPECT_LT(ip, tor1); |
| 221 | |
| 222 | EXPECT_STREQ(v3_onion, tor1.host_str().c_str()); |
| 223 | EXPECT_EQ(std::string{v3_onion} + ":8080", tor1.str()); |
| 224 | EXPECT_EQ(epee::net_utils::address_type::tor, tor1.get_type_id()); |
| 225 | EXPECT_EQ(epee::net_utils::address_type::tor, tor2.get_type_id()); |
| 226 | EXPECT_EQ(epee::net_utils::address_type::ipv4, ip.get_type_id()); |
| 227 | EXPECT_EQ(epee::net_utils::zone::tor, tor1.get_zone()); |
| 228 | EXPECT_EQ(epee::net_utils::zone::tor, tor2.get_zone()); |
| 229 | EXPECT_EQ(epee::net_utils::zone::public_, ip.get_zone()); |
| 230 | EXPECT_TRUE(tor1.is_blockable()); |
| 231 | EXPECT_TRUE(tor2.is_blockable()); |
| 232 | EXPECT_TRUE(ip.is_blockable()); |
| 233 | } |
| 234 | |
| 235 | namespace |
| 236 | { |
| 237 | struct test_command_tor |
| 238 | { |
| 239 | net::tor_address tor; |
| 240 | |
| 241 | BEGIN_KV_SERIALIZE_MAP() |
| 242 | KV_SERIALIZE(tor); |
| 243 | END_KV_SERIALIZE_MAP() |
| 244 | }; |
| 245 | } |
| 246 | |
| 247 | TEST(tor_address, epee_serializev_v2) |
| 248 | { |
| 249 | epee::byte_slice buffer{}; |
| 250 | { |
| 251 | test_command_tor command{MONERO_UNWRAP(net::tor_address::make(v2_onion, 10))}; |
| 252 | EXPECT_FALSE(command.tor.is_unknown()); |
| 253 | EXPECT_NE(net::tor_address{}, command.tor); |
| 254 | EXPECT_STREQ(v2_onion, command.tor.host_str()); |
| 255 | EXPECT_EQ(10u, command.tor.port()); |
| 256 | |
| 257 | epee::serialization::portable_storage stg{}; |
| 258 | EXPECT_TRUE(command.store(stg)); |
| 259 | EXPECT_TRUE(stg.store_to_binary(buffer)); |
| 260 | } |
| 261 | |
| 262 | test_command_tor command{}; |
| 263 | { |
| 264 | EXPECT_TRUE(command.tor.is_unknown()); |
| 265 | EXPECT_EQ(net::tor_address{}, command.tor); |
| 266 | EXPECT_STREQ(net::tor_address::unknown_str(), command.tor.host_str()); |
| 267 | EXPECT_EQ(0u, command.tor.port()); |
| 268 | |
| 269 | epee::serialization::portable_storage stg{}; |
| 270 | EXPECT_TRUE(stg.load_from_binary(epee::to_span(buffer))); |
| 271 | EXPECT_TRUE(command.load(stg)); |
| 272 | } |
| 273 | EXPECT_FALSE(command.tor.is_unknown()); |
| 274 | EXPECT_NE(net::tor_address{}, command.tor); |
| 275 | EXPECT_STREQ(v2_onion, command.tor.host_str()); |
| 276 | EXPECT_EQ(10u, command.tor.port()); |
| 277 | |
| 278 | // make sure that exceeding max buffer doesn't destroy tor_address::_load |
| 279 | { |
| 280 | epee::serialization::portable_storage stg{}; |
| 281 | stg.load_from_binary(epee::to_span(buffer)); |
| 282 | |
| 283 | std::string host{}; |
| 284 | ASSERT_TRUE(stg.get_value("host", host, stg.open_section("tor", nullptr, false))); |
| 285 | EXPECT_EQ(std::strlen(v2_onion), host.size()); |
| 286 | |
| 287 | host.push_back('k'); |
| 288 | EXPECT_TRUE(stg.set_value("host", std::move(host), stg.open_section("tor", nullptr, false))); |
| 289 | EXPECT_TRUE(command.load(stg)); // poor error reporting from `KV_SERIALIZE` |
| 290 | } |
| 291 | |
| 292 | EXPECT_TRUE(command.tor.is_unknown()); |
| 293 | EXPECT_EQ(net::tor_address{}, command.tor); |
| 294 | EXPECT_STREQ(net::tor_address::unknown_str(), command.tor.host_str()); |
| 295 | EXPECT_EQ(0u, command.tor.port()); |
| 296 | } |
| 297 | |
| 298 | TEST(tor_address, epee_serializev_v3) |
| 299 | { |
| 300 | epee::byte_slice buffer{}; |
| 301 | { |
| 302 | test_command_tor command{MONERO_UNWRAP(net::tor_address::make(v3_onion, 10))}; |
| 303 | EXPECT_FALSE(command.tor.is_unknown()); |
| 304 | EXPECT_NE(net::tor_address{}, command.tor); |
| 305 | EXPECT_STREQ(v3_onion, command.tor.host_str()); |
| 306 | EXPECT_EQ(10u, command.tor.port()); |
| 307 | |
| 308 | epee::serialization::portable_storage stg{}; |
| 309 | EXPECT_TRUE(command.store(stg)); |
| 310 | EXPECT_TRUE(stg.store_to_binary(buffer)); |
| 311 | } |
| 312 | |
| 313 | test_command_tor command{}; |
| 314 | { |
| 315 | EXPECT_TRUE(command.tor.is_unknown()); |
| 316 | EXPECT_EQ(net::tor_address{}, command.tor); |
| 317 | EXPECT_STREQ(net::tor_address::unknown_str(), command.tor.host_str()); |
| 318 | EXPECT_EQ(0u, command.tor.port()); |
| 319 | |
| 320 | epee::serialization::portable_storage stg{}; |
| 321 | EXPECT_TRUE(stg.load_from_binary(epee::to_span(buffer))); |
| 322 | EXPECT_TRUE(command.load(stg)); |
| 323 | } |
| 324 | EXPECT_FALSE(command.tor.is_unknown()); |
| 325 | EXPECT_NE(net::tor_address{}, command.tor); |
| 326 | EXPECT_STREQ(v3_onion, command.tor.host_str()); |
| 327 | EXPECT_EQ(10u, command.tor.port()); |
| 328 | |
| 329 | // make sure that exceeding max buffer doesn't destroy tor_address::_load |
| 330 | { |
| 331 | epee::serialization::portable_storage stg{}; |
| 332 | stg.load_from_binary(epee::to_span(buffer)); |
| 333 | |
| 334 | std::string host{}; |
| 335 | ASSERT_TRUE(stg.get_value("host", host, stg.open_section("tor", nullptr, false))); |
| 336 | EXPECT_EQ(std::strlen(v3_onion), host.size()); |
| 337 | |
| 338 | host.push_back('k'); |
| 339 | EXPECT_TRUE(stg.set_value("host", std::move(host), stg.open_section("tor", nullptr, false))); |
| 340 | EXPECT_TRUE(command.load(stg)); // poor error reporting from `KV_SERIALIZE` |
| 341 | } |
| 342 | |
| 343 | EXPECT_TRUE(command.tor.is_unknown()); |
| 344 | EXPECT_EQ(net::tor_address{}, command.tor); |
| 345 | EXPECT_STRNE(v3_onion, command.tor.host_str()); |
| 346 | EXPECT_EQ(0u, command.tor.port()); |
| 347 | } |
| 348 | |
| 349 | TEST(tor_address, epee_serialize_unknown) |
| 350 | { |
| 351 | epee::byte_slice buffer{}; |
| 352 | { |
| 353 | test_command_tor command{net::tor_address::unknown()}; |
| 354 | EXPECT_TRUE(command.tor.is_unknown()); |
| 355 | EXPECT_EQ(net::tor_address{}, command.tor); |
| 356 | EXPECT_STREQ(net::tor_address::unknown_str(), command.tor.host_str()); |
| 357 | EXPECT_EQ(0u, command.tor.port()); |
| 358 | |
| 359 | epee::serialization::portable_storage stg{}; |
| 360 | EXPECT_TRUE(command.store(stg)); |
| 361 | EXPECT_TRUE(stg.store_to_binary(buffer)); |
| 362 | } |
| 363 | |
| 364 | test_command_tor command{}; |
| 365 | { |
| 366 | EXPECT_TRUE(command.tor.is_unknown()); |
| 367 | EXPECT_EQ(net::tor_address{}, command.tor); |
| 368 | EXPECT_STRNE(v3_onion, command.tor.host_str()); |
| 369 | EXPECT_EQ(0u, command.tor.port()); |
| 370 | |
| 371 | epee::serialization::portable_storage stg{}; |
| 372 | EXPECT_TRUE(stg.load_from_binary(epee::to_span(buffer))); |
| 373 | EXPECT_TRUE(command.load(stg)); |
| 374 | } |
| 375 | EXPECT_TRUE(command.tor.is_unknown()); |
| 376 | EXPECT_EQ(net::tor_address{}, command.tor); |
| 377 | EXPECT_STREQ(net::tor_address::unknown_str(), command.tor.host_str()); |
| 378 | EXPECT_EQ(0u, command.tor.port()); |
| 379 | |
| 380 | // make sure that exceeding max buffer doesn't destroy tor_address::_load |
| 381 | { |
| 382 | epee::serialization::portable_storage stg{}; |
| 383 | stg.load_from_binary(epee::to_span(buffer)); |
| 384 | |
| 385 | std::string host{}; |
| 386 | ASSERT_TRUE(stg.get_value("host", host, stg.open_section("tor", nullptr, false))); |
| 387 | EXPECT_EQ(std::strlen(net::tor_address::unknown_str()), host.size()); |
| 388 | |
| 389 | host.push_back('k'); |
| 390 | EXPECT_TRUE(stg.set_value("host", std::move(host), stg.open_section("tor", nullptr, false))); |
| 391 | EXPECT_TRUE(command.load(stg)); // poor error reporting from `KV_SERIALIZE` |
| 392 | } |
| 393 | |
| 394 | EXPECT_TRUE(command.tor.is_unknown()); |
| 395 | EXPECT_EQ(net::tor_address{}, command.tor); |
| 396 | EXPECT_STRNE(v3_onion, command.tor.host_str()); |
| 397 | EXPECT_EQ(0u, command.tor.port()); |
| 398 | } |
| 399 | |
| 400 | TEST(tor_address, boost_serialize_v2) |
| 401 | { |
| 402 | std::string buffer{}; |
| 403 | { |
| 404 | const net::tor_address tor = MONERO_UNWRAP(net::tor_address::make(v2_onion, 10)); |
| 405 | EXPECT_FALSE(tor.is_unknown()); |
| 406 | EXPECT_NE(net::tor_address{}, tor); |
| 407 | EXPECT_STREQ(v2_onion, tor.host_str()); |
| 408 | EXPECT_EQ(10u, tor.port()); |
| 409 | |
| 410 | std::ostringstream stream{}; |
| 411 | { |
| 412 | boost::archive::portable_binary_oarchive archive{stream}; |
| 413 | archive << tor; |
| 414 | } |
| 415 | buffer = stream.str(); |
| 416 | } |
| 417 | |
| 418 | net::tor_address tor{}; |
| 419 | { |
| 420 | EXPECT_TRUE(tor.is_unknown()); |
| 421 | EXPECT_EQ(net::tor_address{}, tor); |
| 422 | EXPECT_STREQ(net::tor_address::unknown_str(), tor.host_str()); |
| 423 | EXPECT_EQ(0u, tor.port()); |
| 424 | |
| 425 | std::istringstream stream{buffer}; |
| 426 | boost::archive::portable_binary_iarchive archive{stream}; |
| 427 | archive >> tor; |
| 428 | } |
| 429 | EXPECT_FALSE(tor.is_unknown()); |
| 430 | EXPECT_NE(net::tor_address{}, tor); |
| 431 | EXPECT_STREQ(v2_onion, tor.host_str()); |
| 432 | EXPECT_EQ(10u, tor.port()); |
| 433 | } |
| 434 | |
| 435 | TEST(tor_address, boost_serialize_v3) |
| 436 | { |
| 437 | std::string buffer{}; |
| 438 | { |
| 439 | const net::tor_address tor = MONERO_UNWRAP(net::tor_address::make(v3_onion, 10)); |
| 440 | EXPECT_FALSE(tor.is_unknown()); |
| 441 | EXPECT_NE(net::tor_address{}, tor); |
| 442 | EXPECT_STREQ(v3_onion, tor.host_str()); |
| 443 | EXPECT_EQ(10u, tor.port()); |
| 444 | |
| 445 | std::ostringstream stream{}; |
| 446 | { |
| 447 | boost::archive::portable_binary_oarchive archive{stream}; |
| 448 | archive << tor; |
| 449 | } |
| 450 | buffer = stream.str(); |
| 451 | } |
| 452 | |
| 453 | net::tor_address tor{}; |
| 454 | { |
| 455 | EXPECT_TRUE(tor.is_unknown()); |
| 456 | EXPECT_EQ(net::tor_address{}, tor); |
| 457 | EXPECT_STREQ(net::tor_address::unknown_str(), tor.host_str()); |
| 458 | EXPECT_EQ(0u, tor.port()); |
| 459 | |
| 460 | std::istringstream stream{buffer}; |
| 461 | boost::archive::portable_binary_iarchive archive{stream}; |
| 462 | archive >> tor; |
| 463 | } |
| 464 | EXPECT_FALSE(tor.is_unknown()); |
| 465 | EXPECT_NE(net::tor_address{}, tor); |
| 466 | EXPECT_STREQ(v3_onion, tor.host_str()); |
| 467 | EXPECT_EQ(10u, tor.port()); |
| 468 | } |
| 469 | |
| 470 | TEST(tor_address, boost_serialize_unknown) |
| 471 | { |
| 472 | std::string buffer{}; |
| 473 | { |
| 474 | const net::tor_address tor{}; |
| 475 | EXPECT_TRUE(tor.is_unknown()); |
| 476 | EXPECT_EQ(net::tor_address::unknown(), tor); |
| 477 | EXPECT_STREQ(net::tor_address::unknown_str(), tor.host_str()); |
| 478 | EXPECT_EQ(0u, tor.port()); |
| 479 | |
| 480 | std::ostringstream stream{}; |
| 481 | { |
| 482 | boost::archive::portable_binary_oarchive archive{stream}; |
| 483 | archive << tor; |
| 484 | } |
| 485 | buffer = stream.str(); |
| 486 | } |
| 487 | |
| 488 | net::tor_address tor{}; |
| 489 | { |
| 490 | EXPECT_TRUE(tor.is_unknown()); |
| 491 | EXPECT_EQ(net::tor_address{}, tor); |
| 492 | EXPECT_STREQ(net::tor_address::unknown_str(), tor.host_str()); |
| 493 | EXPECT_EQ(0u, tor.port()); |
| 494 | |
| 495 | std::istringstream stream{buffer}; |
| 496 | boost::archive::portable_binary_iarchive archive{stream}; |
| 497 | archive >> tor; |
| 498 | } |
| 499 | EXPECT_TRUE(tor.is_unknown()); |
| 500 | EXPECT_EQ(net::tor_address::unknown(), tor); |
| 501 | EXPECT_STREQ(net::tor_address::unknown_str(), tor.host_str()); |
| 502 | EXPECT_EQ(0u, tor.port()); |
| 503 | } |
| 504 | |
| 505 | TEST(get_network_address, onion) |
| 506 | { |
| 507 | expect<epee::net_utils::network_address> address = |
| 508 | net::get_network_address("onion", 0); |
| 509 | EXPECT_EQ(net::error::unsupported_address, address); |
| 510 | |
| 511 | address = net::get_network_address(".onion", 0); |
| 512 | EXPECT_EQ(net::error::invalid_tor_address, address); |
| 513 | |
| 514 | address = net::get_network_address(v3_onion, 1000); |
| 515 | ASSERT_TRUE(bool(address)); |
| 516 | EXPECT_EQ(epee::net_utils::address_type::tor, address->get_type_id()); |
| 517 | EXPECT_STREQ(v3_onion, address->host_str().c_str()); |
| 518 | EXPECT_EQ(std::string{v3_onion} + ":1000", address->str()); |
| 519 | |
| 520 | address = net::get_network_address(std::string{v3_onion} + ":2000", 1000); |
| 521 | ASSERT_TRUE(bool(address)); |
| 522 | EXPECT_EQ(epee::net_utils::address_type::tor, address->get_type_id()); |
| 523 | EXPECT_STREQ(v3_onion, address->host_str().c_str()); |
| 524 | EXPECT_EQ(std::string{v3_onion} + ":2000", address->str()); |
| 525 | |
| 526 | address = net::get_network_address(std::string{v3_onion} + ":65536", 1000); |
| 527 | EXPECT_EQ(net::error::invalid_port, address); |
| 528 | } |
| 529 | |
| 530 | namespace |
| 531 | { |
| 532 | static constexpr const char b32_i2p[] = |
| 533 | "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopn.b32.i2p"; |
| 534 | static constexpr const char b32_i2p_2[] = |
| 535 | "xmrto2bturnore26xmrto2bturnore26xmrto2bturnore26xmr2.b32.i2p"; |
| 536 | } |
| 537 | |
| 538 | TEST(i2p_address, constants) |
| 539 | { |
| 540 | static_assert(!net::i2p_address::is_local(), "bad is_local() response"); |
| 541 | static_assert(!net::i2p_address::is_loopback(), "bad is_loopback() response"); |
| 542 | static_assert(net::i2p_address::get_type_id() == epee::net_utils::address_type::i2p, "bad get_type_id() response"); |
| 543 | |
| 544 | EXPECT_FALSE(net::i2p_address::is_local()); |
| 545 | EXPECT_FALSE(net::i2p_address::is_loopback()); |
| 546 | EXPECT_EQ(epee::net_utils::address_type::i2p, net::i2p_address::get_type_id()); |
| 547 | EXPECT_EQ(epee::net_utils::address_type::i2p, net::i2p_address::get_type_id()); |
| 548 | } |
| 549 | |
| 550 | TEST(i2p_address, invalid) |
| 551 | { |
| 552 | EXPECT_TRUE(net::i2p_address::make("").has_error()); |
| 553 | EXPECT_TRUE(net::i2p_address::make(":").has_error()); |
| 554 | EXPECT_TRUE(net::i2p_address::make(".b32.i2p").has_error()); |
| 555 | EXPECT_TRUE(net::i2p_address::make(".b32.i2p:").has_error()); |
| 556 | EXPECT_TRUE(net::i2p_address::make(b32_i2p + 1).has_error()); |
| 557 | EXPECT_TRUE(net::i2p_address::make(boost::string_ref{b32_i2p, sizeof(b32_i2p) - 2}).has_error()); |
| 558 | |
| 559 | std::string i2p{b32_i2p}; |
| 560 | i2p.at(10) = 1; |
| 561 | EXPECT_TRUE(net::i2p_address::make(i2p).has_error()); |
| 562 | } |
| 563 | |
| 564 | TEST(i2p_address, unblockable_types) |
| 565 | { |
| 566 | net::i2p_address i2p{}; |
| 567 | |
| 568 | ASSERT_NE(nullptr, i2p.host_str()); |
| 569 | EXPECT_STREQ("<unknown i2p host>", i2p.host_str()); |
| 570 | EXPECT_STREQ("<unknown i2p host>", i2p.str().c_str()); |
| 571 | EXPECT_EQ(1u, i2p.port()); |
| 572 | EXPECT_TRUE(i2p.is_unknown()); |
| 573 | EXPECT_FALSE(i2p.is_local()); |
| 574 | EXPECT_FALSE(i2p.is_loopback()); |
| 575 | EXPECT_EQ(epee::net_utils::address_type::i2p, i2p.get_type_id()); |
| 576 | EXPECT_EQ(epee::net_utils::zone::i2p, i2p.get_zone()); |
| 577 | |
| 578 | i2p = net::i2p_address::unknown(); |
| 579 | ASSERT_NE(nullptr, i2p.host_str()); |
| 580 | EXPECT_STREQ("<unknown i2p host>", i2p.host_str()); |
| 581 | EXPECT_STREQ("<unknown i2p host>", i2p.str().c_str()); |
| 582 | EXPECT_EQ(1u, i2p.port()); |
| 583 | EXPECT_TRUE(i2p.is_unknown()); |
| 584 | EXPECT_FALSE(i2p.is_local()); |
| 585 | EXPECT_FALSE(i2p.is_loopback()); |
| 586 | EXPECT_EQ(epee::net_utils::address_type::i2p, i2p.get_type_id()); |
| 587 | EXPECT_EQ(epee::net_utils::zone::i2p, i2p.get_zone()); |
| 588 | |
| 589 | EXPECT_EQ(net::i2p_address{}, net::i2p_address::unknown()); |
| 590 | } |
| 591 | |
| 592 | TEST(i2p_address, valid) |
| 593 | { |
| 594 | const auto address1 = net::i2p_address::make(b32_i2p); |
| 595 | |
| 596 | ASSERT_TRUE(address1.has_value()); |
| 597 | EXPECT_EQ(1u, address1->port()); |
| 598 | EXPECT_STREQ(b32_i2p, address1->host_str()); |
| 599 | EXPECT_STREQ(b32_i2p, address1->str().c_str()); |
| 600 | EXPECT_TRUE(address1->is_blockable()); |
| 601 | |
| 602 | net::i2p_address address2{*address1}; |
| 603 | |
| 604 | EXPECT_EQ(1u, address2.port()); |
| 605 | EXPECT_STREQ(b32_i2p, address2.host_str()); |
| 606 | EXPECT_STREQ(b32_i2p, address2.str().c_str()); |
| 607 | EXPECT_TRUE(address2.is_blockable()); |
| 608 | EXPECT_TRUE(address2.equal(*address1)); |
| 609 | EXPECT_TRUE(address1->equal(address2)); |
| 610 | EXPECT_TRUE(address2 == *address1); |
| 611 | EXPECT_TRUE(*address1 == address2); |
| 612 | EXPECT_FALSE(address2 != *address1); |
| 613 | EXPECT_FALSE(*address1 != address2); |
| 614 | EXPECT_TRUE(address2.is_same_host(*address1)); |
| 615 | EXPECT_TRUE(address1->is_same_host(address2)); |
| 616 | EXPECT_FALSE(address2.less(*address1)); |
| 617 | EXPECT_FALSE(address1->less(address2)); |
| 618 | |
| 619 | address2 = MONERO_UNWRAP(net::i2p_address::make(std::string{b32_i2p_2} + ":6545")); |
| 620 | |
| 621 | EXPECT_EQ(1u, address2.port()); |
| 622 | EXPECT_STREQ(b32_i2p_2, address2.host_str()); |
| 623 | EXPECT_EQ(std::string{b32_i2p_2}, address2.str().c_str()); |
| 624 | EXPECT_TRUE(address2.is_blockable()); |
| 625 | EXPECT_FALSE(address2.equal(*address1)); |
| 626 | EXPECT_FALSE(address1->equal(address2)); |
| 627 | EXPECT_FALSE(address2 == *address1); |
| 628 | EXPECT_FALSE(*address1 == address2); |
| 629 | EXPECT_TRUE(address2 != *address1); |
| 630 | EXPECT_TRUE(*address1 != address2); |
| 631 | EXPECT_FALSE(address2.is_same_host(*address1)); |
| 632 | EXPECT_FALSE(address1->is_same_host(address2)); |
| 633 | EXPECT_FALSE(address2.less(*address1)); |
| 634 | EXPECT_TRUE(address1->less(address2)); |
| 635 | |
| 636 | net::i2p_address address3 = MONERO_UNWRAP(net::i2p_address::make(std::string{b32_i2p} + ":65535")); |
| 637 | |
| 638 | EXPECT_EQ(1u, address3.port()); |
| 639 | EXPECT_STREQ(b32_i2p, address3.host_str()); |
| 640 | EXPECT_EQ(std::string{b32_i2p}, address3.str().c_str()); |
| 641 | EXPECT_TRUE(address3.is_blockable()); |
| 642 | EXPECT_TRUE(address3.equal(*address1)); |
| 643 | EXPECT_TRUE(address1->equal(address3)); |
| 644 | EXPECT_TRUE(address3 == *address1); |
| 645 | EXPECT_TRUE(*address1 == address3); |
| 646 | EXPECT_FALSE(address3 != *address1); |
| 647 | EXPECT_FALSE(*address1 != address3); |
| 648 | EXPECT_TRUE(address3.is_same_host(*address1)); |
| 649 | EXPECT_TRUE(address1->is_same_host(address3)); |
| 650 | EXPECT_FALSE(address3.less(*address1)); |
| 651 | EXPECT_FALSE(address1->less(address3)); |
| 652 | |
| 653 | EXPECT_FALSE(address3.equal(address2)); |
| 654 | EXPECT_FALSE(address2.equal(address3)); |
| 655 | EXPECT_FALSE(address3 == address2); |
| 656 | EXPECT_FALSE(address2 == address3); |
| 657 | EXPECT_TRUE(address3 != address2); |
| 658 | EXPECT_TRUE(address2 != address3); |
| 659 | EXPECT_FALSE(address3.is_same_host(address2)); |
| 660 | EXPECT_FALSE(address2.is_same_host(address3)); |
| 661 | EXPECT_TRUE(address3.less(address2)); |
| 662 | EXPECT_FALSE(address2.less(address3)); |
| 663 | } |
| 664 | |
| 665 | TEST(i2p_address, generic_network_address) |
| 666 | { |
| 667 | const epee::net_utils::network_address i2p1{MONERO_UNWRAP(net::i2p_address::make(b32_i2p))}; |
| 668 | const epee::net_utils::network_address i2p2{MONERO_UNWRAP(net::i2p_address::make(b32_i2p))}; |
| 669 | const epee::net_utils::network_address ip{epee::net_utils::ipv4_network_address{100, 200}}; |
| 670 | |
| 671 | EXPECT_EQ(i2p1, i2p2); |
| 672 | EXPECT_NE(ip, i2p1); |
| 673 | EXPECT_LT(ip, i2p1); |
| 674 | |
| 675 | EXPECT_STREQ(b32_i2p, i2p1.host_str().c_str()); |
| 676 | EXPECT_STREQ(b32_i2p, i2p1.str().c_str()); |
| 677 | EXPECT_EQ(epee::net_utils::address_type::i2p, i2p1.get_type_id()); |
| 678 | EXPECT_EQ(epee::net_utils::address_type::i2p, i2p2.get_type_id()); |
| 679 | EXPECT_EQ(epee::net_utils::address_type::ipv4, ip.get_type_id()); |
| 680 | EXPECT_EQ(epee::net_utils::zone::i2p, i2p1.get_zone()); |
| 681 | EXPECT_EQ(epee::net_utils::zone::i2p, i2p2.get_zone()); |
| 682 | EXPECT_EQ(epee::net_utils::zone::public_, ip.get_zone()); |
| 683 | EXPECT_TRUE(i2p1.is_blockable()); |
| 684 | EXPECT_TRUE(i2p2.is_blockable()); |
| 685 | EXPECT_TRUE(ip.is_blockable()); |
| 686 | } |
| 687 | |
| 688 | namespace |
| 689 | { |
| 690 | struct test_command_i2p |
| 691 | { |
| 692 | net::i2p_address i2p; |
| 693 | |
| 694 | BEGIN_KV_SERIALIZE_MAP() |
| 695 | KV_SERIALIZE(i2p); |
| 696 | END_KV_SERIALIZE_MAP() |
| 697 | }; |
| 698 | } |
| 699 | |
| 700 | TEST(i2p_address, epee_serializev_b32) |
| 701 | { |
| 702 | epee::byte_slice buffer{}; |
| 703 | { |
| 704 | test_command_i2p command{MONERO_UNWRAP(net::i2p_address::make(b32_i2p))}; |
| 705 | EXPECT_FALSE(command.i2p.is_unknown()); |
| 706 | EXPECT_NE(net::i2p_address{}, command.i2p); |
| 707 | EXPECT_STREQ(b32_i2p, command.i2p.host_str()); |
| 708 | EXPECT_EQ(1u, command.i2p.port()); |
| 709 | |
| 710 | epee::serialization::portable_storage stg{}; |
| 711 | EXPECT_TRUE(command.store(stg)); |
| 712 | EXPECT_TRUE(stg.store_to_binary(buffer)); |
| 713 | } |
| 714 | |
| 715 | test_command_i2p command{}; |
| 716 | { |
| 717 | EXPECT_TRUE(command.i2p.is_unknown()); |
| 718 | EXPECT_EQ(net::i2p_address{}, command.i2p); |
| 719 | EXPECT_STREQ(net::i2p_address::unknown_str(), command.i2p.host_str()); |
| 720 | EXPECT_EQ(1u, command.i2p.port()); |
| 721 | |
| 722 | epee::serialization::portable_storage stg{}; |
| 723 | EXPECT_TRUE(stg.load_from_binary(epee::to_span(buffer))); |
| 724 | EXPECT_TRUE(command.load(stg)); |
| 725 | } |
| 726 | EXPECT_FALSE(command.i2p.is_unknown()); |
| 727 | EXPECT_NE(net::i2p_address{}, command.i2p); |
| 728 | EXPECT_STREQ(b32_i2p, command.i2p.host_str()); |
| 729 | EXPECT_EQ(1u, command.i2p.port()); |
| 730 | |
| 731 | // make sure that exceeding max buffer doesn't destroy i2p_address::_load |
| 732 | { |
| 733 | epee::serialization::portable_storage stg{}; |
| 734 | stg.load_from_binary(epee::to_span(buffer)); |
| 735 | |
| 736 | std::string host{}; |
| 737 | ASSERT_TRUE(stg.get_value("host", host, stg.open_section("i2p", nullptr, false))); |
| 738 | EXPECT_EQ(std::strlen(b32_i2p), host.size()); |
| 739 | |
| 740 | host.push_back('k'); |
| 741 | EXPECT_TRUE(stg.set_value("host", std::string{host}, stg.open_section("i2p", nullptr, false))); |
| 742 | EXPECT_TRUE(command.load(stg)); // poor error reporting from `KV_SERIALIZE` |
| 743 | } |
| 744 | |
| 745 | EXPECT_TRUE(command.i2p.is_unknown()); |
| 746 | EXPECT_EQ(net::i2p_address{}, command.i2p); |
| 747 | EXPECT_STRNE(b32_i2p, command.i2p.host_str()); |
| 748 | EXPECT_EQ(1u, command.i2p.port()); |
| 749 | } |
| 750 | |
| 751 | TEST(i2p_address, epee_serialize_unknown) |
| 752 | { |
| 753 | epee::byte_slice buffer{}; |
| 754 | { |
| 755 | test_command_i2p command{net::i2p_address::unknown()}; |
| 756 | EXPECT_TRUE(command.i2p.is_unknown()); |
| 757 | EXPECT_EQ(net::i2p_address{}, command.i2p); |
| 758 | EXPECT_STREQ(net::i2p_address::unknown_str(), command.i2p.host_str()); |
| 759 | EXPECT_EQ(1u, command.i2p.port()); |
| 760 | |
| 761 | epee::serialization::portable_storage stg{}; |
| 762 | EXPECT_TRUE(command.store(stg)); |
| 763 | EXPECT_TRUE(stg.store_to_binary(buffer)); |
| 764 | } |
| 765 | |
| 766 | test_command_i2p command{}; |
| 767 | { |
| 768 | EXPECT_TRUE(command.i2p.is_unknown()); |
| 769 | EXPECT_EQ(net::i2p_address{}, command.i2p); |
| 770 | EXPECT_STRNE(b32_i2p, command.i2p.host_str()); |
| 771 | EXPECT_EQ(1u, command.i2p.port()); |
| 772 | |
| 773 | epee::serialization::portable_storage stg{}; |
| 774 | EXPECT_TRUE(stg.load_from_binary(epee::to_span(buffer))); |
| 775 | EXPECT_TRUE(command.load(stg)); |
| 776 | } |
| 777 | EXPECT_TRUE(command.i2p.is_unknown()); |
| 778 | EXPECT_EQ(net::i2p_address{}, command.i2p); |
| 779 | EXPECT_STREQ(net::i2p_address::unknown_str(), command.i2p.host_str()); |
| 780 | EXPECT_EQ(1u, command.i2p.port()); |
| 781 | |
| 782 | // make sure that exceeding max buffer doesn't destroy i2p_address::_load |
| 783 | { |
| 784 | epee::serialization::portable_storage stg{}; |
| 785 | stg.load_from_binary(epee::to_span(buffer)); |
| 786 | |
| 787 | std::string host{}; |
| 788 | ASSERT_TRUE(stg.get_value("host", host, stg.open_section("i2p", nullptr, false))); |
| 789 | EXPECT_EQ(std::strlen(net::i2p_address::unknown_str()), host.size()); |
| 790 | |
| 791 | host.push_back('k'); |
| 792 | EXPECT_TRUE(stg.set_value("host", std::string{host}, stg.open_section("i2p", nullptr, false))); |
| 793 | EXPECT_TRUE(command.load(stg)); // poor error reporting from `KV_SERIALIZE` |
| 794 | } |
| 795 | |
| 796 | EXPECT_TRUE(command.i2p.is_unknown()); |
| 797 | EXPECT_EQ(net::i2p_address{}, command.i2p); |
| 798 | EXPECT_STRNE(b32_i2p, command.i2p.host_str()); |
| 799 | EXPECT_EQ(1u, command.i2p.port()); |
| 800 | } |
| 801 | |
| 802 | TEST(i2p_address, boost_serialize_b32) |
| 803 | { |
| 804 | std::string buffer{}; |
| 805 | { |
| 806 | const net::i2p_address i2p = MONERO_UNWRAP(net::i2p_address::make(b32_i2p)); |
| 807 | EXPECT_FALSE(i2p.is_unknown()); |
| 808 | EXPECT_NE(net::i2p_address{}, i2p); |
| 809 | EXPECT_STREQ(b32_i2p, i2p.host_str()); |
| 810 | EXPECT_EQ(1u, i2p.port()); |
| 811 | |
| 812 | std::ostringstream stream{}; |
| 813 | { |
| 814 | boost::archive::portable_binary_oarchive archive{stream}; |
| 815 | archive << i2p; |
| 816 | } |
| 817 | buffer = stream.str(); |
| 818 | } |
| 819 | |
| 820 | net::i2p_address i2p{}; |
| 821 | { |
| 822 | EXPECT_TRUE(i2p.is_unknown()); |
| 823 | EXPECT_EQ(net::i2p_address{}, i2p); |
| 824 | EXPECT_STREQ(net::i2p_address::unknown_str(), i2p.host_str()); |
| 825 | EXPECT_EQ(1u, i2p.port()); |
| 826 | |
| 827 | std::istringstream stream{buffer}; |
| 828 | boost::archive::portable_binary_iarchive archive{stream}; |
| 829 | archive >> i2p; |
| 830 | } |
| 831 | EXPECT_FALSE(i2p.is_unknown()); |
| 832 | EXPECT_NE(net::i2p_address{}, i2p); |
| 833 | EXPECT_STREQ(b32_i2p, i2p.host_str()); |
| 834 | EXPECT_EQ(1u, i2p.port()); |
| 835 | } |
| 836 | |
| 837 | TEST(i2p_address, boost_serialize_unknown) |
| 838 | { |
| 839 | std::string buffer{}; |
| 840 | { |
| 841 | const net::i2p_address i2p{}; |
| 842 | EXPECT_TRUE(i2p.is_unknown()); |
| 843 | EXPECT_EQ(net::i2p_address::unknown(), i2p); |
| 844 | EXPECT_STREQ(net::i2p_address::unknown_str(), i2p.host_str()); |
| 845 | EXPECT_EQ(1u, i2p.port()); |
| 846 | |
| 847 | std::ostringstream stream{}; |
| 848 | { |
| 849 | boost::archive::portable_binary_oarchive archive{stream}; |
| 850 | archive << i2p; |
| 851 | } |
| 852 | buffer = stream.str(); |
| 853 | } |
| 854 | |
| 855 | net::i2p_address i2p{}; |
| 856 | { |
| 857 | EXPECT_TRUE(i2p.is_unknown()); |
| 858 | EXPECT_EQ(net::i2p_address{}, i2p); |
| 859 | EXPECT_STREQ(net::i2p_address::unknown_str(), i2p.host_str()); |
| 860 | EXPECT_EQ(1u, i2p.port()); |
| 861 | |
| 862 | std::istringstream stream{buffer}; |
| 863 | boost::archive::portable_binary_iarchive archive{stream}; |
| 864 | archive >> i2p; |
| 865 | } |
| 866 | EXPECT_TRUE(i2p.is_unknown()); |
| 867 | EXPECT_EQ(net::i2p_address::unknown(), i2p); |
| 868 | EXPECT_STREQ(net::i2p_address::unknown_str(), i2p.host_str()); |
| 869 | EXPECT_EQ(1u, i2p.port()); |
| 870 | } |
| 871 | |
| 872 | TEST(get_network_address, i2p) |
| 873 | { |
| 874 | expect<epee::net_utils::network_address> address = |
| 875 | net::get_network_address("i2p", 0); |
| 876 | EXPECT_EQ(net::error::unsupported_address, address); |
| 877 | |
| 878 | address = net::get_network_address(".b32.i2p", 0); |
| 879 | EXPECT_EQ(net::error::invalid_i2p_address, address); |
| 880 | |
| 881 | address = net::get_network_address(b32_i2p, 1000); |
| 882 | ASSERT_TRUE(bool(address)); |
| 883 | EXPECT_EQ(epee::net_utils::address_type::i2p, address->get_type_id()); |
| 884 | EXPECT_STREQ(b32_i2p, address->host_str().c_str()); |
| 885 | EXPECT_EQ(std::string{b32_i2p}, address->str()); |
| 886 | |
| 887 | address = net::get_network_address(std::string{b32_i2p} + ":2000", 1000); |
| 888 | ASSERT_TRUE(bool(address)); |
| 889 | EXPECT_EQ(epee::net_utils::address_type::i2p, address->get_type_id()); |
| 890 | EXPECT_STREQ(b32_i2p, address->host_str().c_str()); |
| 891 | EXPECT_EQ(std::string{b32_i2p}, address->str()); |
| 892 | } |
| 893 | |
| 894 | TEST(get_network_address, ipv4) |
| 895 | { |
| 896 | expect<epee::net_utils::network_address> address = |
| 897 | net::get_network_address("0.0.0.", 0); |
| 898 | EXPECT_EQ(net::error::unsupported_address, address); |
| 899 | |
| 900 | address = net::get_network_address("0.0.0.257", 0); |
| 901 | EXPECT_EQ(net::error::unsupported_address, address); |
| 902 | |
| 903 | address = net::get_network_address("0.0.0.254", 1000); |
| 904 | ASSERT_TRUE(bool(address)); |
| 905 | EXPECT_EQ(epee::net_utils::address_type::ipv4, address->get_type_id()); |
| 906 | EXPECT_STREQ("0.0.0.254", address->host_str().c_str()); |
| 907 | EXPECT_STREQ("0.0.0.254:1000", address->str().c_str()); |
| 908 | |
| 909 | address = net::get_network_address("23.0.0.254:2000", 1000); |
| 910 | ASSERT_TRUE(bool(address)); |
| 911 | EXPECT_EQ(epee::net_utils::address_type::ipv4, address->get_type_id()); |
| 912 | EXPECT_STREQ("23.0.0.254", address->host_str().c_str()); |
| 913 | EXPECT_STREQ("23.0.0.254:2000", address->str().c_str()); |
| 914 | } |
| 915 | |
| 916 | TEST(get_network_address, ipv4subnet) |
| 917 | { |
| 918 | expect<epee::net_utils::ipv4_network_subnet> address = net::get_ipv4_subnet_address("0.0.0.0", true); |
| 919 | EXPECT_STREQ("0.0.0.0/32", address->str().c_str()); |
| 920 | |
| 921 | address = net::get_ipv4_subnet_address("0.0.0.0"); |
| 922 | EXPECT_TRUE(!address); |
| 923 | |
| 924 | address = net::get_ipv4_subnet_address("0.0.0.0/32"); |
| 925 | EXPECT_STREQ("0.0.0.0/32", address->str().c_str()); |
| 926 | |
| 927 | address = net::get_ipv4_subnet_address("0.0.0.0/0"); |
| 928 | EXPECT_STREQ("0.0.0.0/0", address->str().c_str()); |
| 929 | |
| 930 | address = net::get_ipv4_subnet_address("12.34.56.78/16"); |
| 931 | EXPECT_STREQ("12.34.0.0/16", address->str().c_str()); |
| 932 | } |
| 933 | |
| 934 | namespace |
| 935 | { |
| 936 | void na_host_and_port_test(std::string addr, std::string exp_host, std::string exp_port) |
| 937 | { |
| 938 | std::string host{"xxxxx"}; |
| 939 | std::string port{"xxxxx"}; |
| 940 | net::get_network_address_host_and_port(addr, host, port); |
| 941 | EXPECT_EQ(exp_host, host); |
| 942 | EXPECT_EQ(exp_port, port); |
| 943 | } |
| 944 | } // anonymous namespace |
| 945 | |
| 946 | TEST(get_network_address_host_and_port, ipv4) |
| 947 | { |
| 948 | na_host_and_port_test("9.9.9.9", "9.9.9.9", "xxxxx"); |
| 949 | na_host_and_port_test("9.9.9.9:18081", "9.9.9.9", "18081"); |
| 950 | } |
| 951 | |
| 952 | TEST(get_network_address_host_and_port, ipv6) |
| 953 | { |
| 954 | na_host_and_port_test("::ffff", "::ffff", "xxxxx"); |
| 955 | na_host_and_port_test("[::ffff]", "::ffff", "xxxxx"); |
| 956 | na_host_and_port_test("[::ffff]:00231", "::ffff", "00231"); |
| 957 | na_host_and_port_test("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "xxxxx"); |
| 958 | na_host_and_port_test("[7777:7777:7777:7777:7777:7777:7777:7777]", "7777:7777:7777:7777:7777:7777:7777:7777", "xxxxx"); |
| 959 | na_host_and_port_test("[7777:7777:7777:7777:7777:7777:7777:7777]:48080", "7777:7777:7777:7777:7777:7777:7777:7777", "48080"); |
| 960 | } |
| 961 | |
| 962 | TEST(get_network_address_host_and_port, hostname) |
| 963 | { |
| 964 | na_host_and_port_test("localhost", "localhost", "xxxxx"); |
| 965 | na_host_and_port_test("bar:29080", "bar", "29080"); // Issue https://github.com/monero-project/monero/issues/8633 |
| 966 | na_host_and_port_test("xmrchain.net:18081", "xmrchain.net", "18081"); |
| 967 | } |
| 968 | |
| 969 | namespace |
| 970 | { |
| 971 | using stream_type = boost::asio::ip::tcp; |
| 972 | |
| 973 | struct io_thread |
| 974 | { |
| 975 | boost::asio::io_context io_service; |
| 976 | boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work; |
| 977 | stream_type::socket server; |
| 978 | stream_type::acceptor acceptor; |
| 979 | boost::thread io; |
| 980 | std::atomic<bool> connected; |
| 981 | |
| 982 | io_thread() |
| 983 | : io_service(), |
| 984 | work(io_service.get_executor()), |
| 985 | server(io_service), |
| 986 | acceptor(io_service), |
| 987 | io([this] () { try { this->io_service.run(); } catch (const std::exception& e) { MERROR(e.what()); }}), |
| 988 | connected(false) |
| 989 | { |
| 990 | acceptor.open(boost::asio::ip::tcp::v4()); |
| 991 | acceptor.bind(stream_type::endpoint{boost::asio::ip::address_v4::loopback(), 0}); |
| 992 | acceptor.listen(); |
| 993 | acceptor.async_accept(server, [this] (boost::system::error_code error) { |
| 994 | this->connected = true; |
| 995 | if (error) |
| 996 | throw boost::system::system_error{error}; |
| 997 | }); |
| 998 | } |
| 999 | |
| 1000 | ~io_thread() noexcept |
| 1001 | { |
| 1002 | io_service.stop(); |
| 1003 | if (io.joinable()) |
| 1004 | io.join(); |
| 1005 | } |
| 1006 | }; |
| 1007 | |
| 1008 | struct checked_client |
| 1009 | { |
| 1010 | std::atomic<bool>* called_; |
| 1011 | bool expected_; |
| 1012 | |
| 1013 | void operator()(boost::system::error_code error, net::socks::client::stream_type::socket&&) const |
| 1014 | { |
| 1015 | EXPECT_EQ(expected_, bool(error)) << "Socks server: " << error.message(); |
| 1016 | ASSERT_TRUE(called_ != nullptr); |
| 1017 | (*called_) = true; |
| 1018 | } |
| 1019 | }; |
| 1020 | } |
| 1021 | |
| 1022 | TEST(socks_client, unsupported_command) |
| 1023 | { |
| 1024 | boost::asio::io_context io_service{}; |
| 1025 | stream_type::socket client{io_service}; |
| 1026 | |
| 1027 | auto test_client = net::socks::make_connect_client( |
| 1028 | std::move(client), net::socks::version::v4, std::bind( [] {} ) |
| 1029 | ); |
| 1030 | ASSERT_TRUE(bool(test_client)); |
| 1031 | EXPECT_TRUE(test_client->buffer().empty()); |
| 1032 | |
| 1033 | EXPECT_FALSE(test_client->set_connect_command("example.com", 8080)); |
| 1034 | EXPECT_TRUE(test_client->buffer().empty()); |
| 1035 | |
| 1036 | EXPECT_FALSE(test_client->set_resolve_command("example.com")); |
| 1037 | EXPECT_TRUE(test_client->buffer().empty()); |
| 1038 | } |
| 1039 | |
| 1040 | TEST(socks_client, no_command) |
| 1041 | { |
| 1042 | boost::asio::io_context io_service{}; |
| 1043 | stream_type::socket client{io_service}; |
| 1044 | |
| 1045 | auto test_client = net::socks::make_connect_client( |
| 1046 | std::move(client), net::socks::version::v4a, std::bind( [] {} ) |
| 1047 | ); |
| 1048 | ASSERT_TRUE(bool(test_client)); |
| 1049 | EXPECT_FALSE(net::socks::client::send(std::move(test_client))); |
| 1050 | } |
| 1051 | |
| 1052 | TEST(socks_client, connect_command) |
| 1053 | { |
| 1054 | io_thread io{}; |
| 1055 | stream_type::socket client{io.io_service}; |
| 1056 | |
| 1057 | std::atomic<bool> called{false}; |
| 1058 | auto test_client = net::socks::make_connect_client( |
| 1059 | std::move(client), net::socks::version::v4a, checked_client{std::addressof(called), false} |
| 1060 | ); |
| 1061 | ASSERT_TRUE(bool(test_client)); |
| 1062 | |
| 1063 | ASSERT_TRUE(test_client->set_connect_command("example.com", 8080)); |
| 1064 | EXPECT_FALSE(test_client->buffer().empty()); |
| 1065 | ASSERT_TRUE(net::socks::client::connect_and_send(std::move(test_client), io.acceptor.local_endpoint())); |
| 1066 | while (!io.connected) |
| 1067 | ASSERT_FALSE(called); |
| 1068 | |
| 1069 | const std::uint8_t expected_bytes[] = { |
| 1070 | 4, 1, 0x1f, 0x90, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 1071 | 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm', 0x00 |
| 1072 | }; |
| 1073 | |
| 1074 | std::uint8_t actual_bytes[sizeof(expected_bytes)]; |
| 1075 | boost::asio::read(io.server, boost::asio::buffer(actual_bytes)); |
| 1076 | EXPECT_TRUE(std::memcmp(expected_bytes, actual_bytes, sizeof(actual_bytes)) == 0); |
| 1077 | |
| 1078 | const std::uint8_t reply_bytes[] = {0, 90, 0, 0, 0, 0, 0, 0}; |
| 1079 | boost::asio::write(io.server, boost::asio::buffer(reply_bytes)); |
| 1080 | |
| 1081 | // yikes! |
| 1082 | while (!called); |
| 1083 | } |
| 1084 | |
| 1085 | TEST(socks_client, connect_command_failed) |
| 1086 | { |
| 1087 | io_thread io{}; |
| 1088 | stream_type::socket client{io.io_service}; |
| 1089 | |
| 1090 | std::atomic<bool> called{false}; |
| 1091 | auto test_client = net::socks::make_connect_client( |
| 1092 | std::move(client), net::socks::version::v4, checked_client{std::addressof(called), true} |
| 1093 | ); |
| 1094 | ASSERT_TRUE(bool(test_client)); |
| 1095 | |
| 1096 | ASSERT_TRUE( |
| 1097 | test_client->set_connect_command( |
| 1098 | epee::net_utils::ipv4_network_address{boost::endian::native_to_big(std::uint32_t(5000)), 3000} |
| 1099 | ) |
| 1100 | ); |
| 1101 | EXPECT_FALSE(test_client->buffer().empty()); |
| 1102 | ASSERT_TRUE(net::socks::client::connect_and_send(std::move(test_client), io.acceptor.local_endpoint())); |
| 1103 | while (!io.connected) |
| 1104 | ASSERT_FALSE(called); |
| 1105 | |
| 1106 | const std::uint8_t expected_bytes[] = { |
| 1107 | 4, 1, 0x0b, 0xb8, 0x00, 0x00, 0x13, 0x88, 0x00 |
| 1108 | }; |
| 1109 | |
| 1110 | std::uint8_t actual_bytes[sizeof(expected_bytes)]; |
| 1111 | boost::asio::read(io.server, boost::asio::buffer(actual_bytes)); |
| 1112 | EXPECT_TRUE(std::memcmp(expected_bytes, actual_bytes, sizeof(actual_bytes)) == 0); |
| 1113 | |
| 1114 | const std::uint8_t reply_bytes[] = {0, 91, 0, 0, 0, 0, 0, 0}; |
| 1115 | boost::asio::write(io.server, boost::asio::buffer(reply_bytes)); |
| 1116 | |
| 1117 | // yikes! |
| 1118 | while (!called); |
| 1119 | } |
| 1120 | |
| 1121 | TEST(socks_client, resolve_command) |
| 1122 | { |
| 1123 | static std::uint8_t reply_bytes[] = {0, 90, 0, 0, 0xff, 0, 0xad, 0}; |
| 1124 | |
| 1125 | struct resolve_client : net::socks::client |
| 1126 | { |
| 1127 | std::atomic<unsigned> called_; |
| 1128 | bool expected_; |
| 1129 | |
| 1130 | resolve_client(stream_type::socket&& proxy) |
| 1131 | : net::socks::client(std::move(proxy), net::socks::version::v4a_tor) |
| 1132 | , called_(0) |
| 1133 | , expected_(false) |
| 1134 | {}; |
| 1135 | |
| 1136 | virtual void done(boost::system::error_code error, std::shared_ptr<client> self) override |
| 1137 | { |
| 1138 | EXPECT_EQ(this, self.get()); |
| 1139 | EXPECT_EQ(expected_, bool(error)) << "Resolve failure: " << error.message(); |
| 1140 | |
| 1141 | if (!error) |
| 1142 | { |
| 1143 | ASSERT_EQ(sizeof(reply_bytes), buffer().size()); |
| 1144 | EXPECT_EQ(0u, std::memcmp(buffer().data(), reply_bytes, sizeof(reply_bytes))); |
| 1145 | } |
| 1146 | |
| 1147 | ++called_; |
| 1148 | } |
| 1149 | }; |
| 1150 | |
| 1151 | io_thread io{}; |
| 1152 | stream_type::socket client{io.io_service}; |
| 1153 | |
| 1154 | auto test_client = std::make_shared<resolve_client>(std::move(client)); |
| 1155 | ASSERT_TRUE(bool(test_client)); |
| 1156 | |
| 1157 | ASSERT_TRUE(test_client->set_resolve_command("example.com")); |
| 1158 | EXPECT_FALSE(test_client->buffer().empty()); |
| 1159 | ASSERT_TRUE(net::socks::client::connect_and_send(test_client, io.acceptor.local_endpoint())); |
| 1160 | while (!io.connected) |
| 1161 | ASSERT_EQ(0u, test_client->called_); |
| 1162 | |
| 1163 | const std::uint8_t expected_bytes[] = { |
| 1164 | 4, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 1165 | 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm', 0x00 |
| 1166 | }; |
| 1167 | |
| 1168 | std::uint8_t actual_bytes[sizeof(expected_bytes)]; |
| 1169 | boost::asio::read(io.server, boost::asio::buffer(actual_bytes)); |
| 1170 | EXPECT_TRUE(std::memcmp(expected_bytes, actual_bytes, sizeof(actual_bytes)) == 0); |
| 1171 | |
| 1172 | boost::asio::write(io.server, boost::asio::buffer(reply_bytes)); |
| 1173 | |
| 1174 | // yikes! |
| 1175 | while (test_client->called_ == 0); |
| 1176 | |
| 1177 | test_client->expected_ = true; |
| 1178 | ASSERT_TRUE(test_client->set_resolve_command("example.com")); |
| 1179 | EXPECT_FALSE(test_client->buffer().empty()); |
| 1180 | ASSERT_TRUE(net::socks::client::send(test_client)); |
| 1181 | |
| 1182 | boost::asio::read(io.server, boost::asio::buffer(actual_bytes)); |
| 1183 | EXPECT_TRUE(std::memcmp(expected_bytes, actual_bytes, sizeof(actual_bytes)) == 0); |
| 1184 | |
| 1185 | reply_bytes[1] = 91; |
| 1186 | boost::asio::write(io.server, boost::asio::buffer(reply_bytes)); |
| 1187 | |
| 1188 | // yikes! |
| 1189 | while (test_client->called_ == 1); |
| 1190 | } |
| 1191 | |
| 1192 | TEST(socks_connector, host) |
| 1193 | { |
| 1194 | io_thread io{}; |
| 1195 | boost::asio::steady_timer timeout{io.io_service}; |
| 1196 | timeout.expires_after(std::chrono::seconds{5}); |
| 1197 | |
| 1198 | boost::unique_future<boost::asio::ip::tcp::socket> sock = |
| 1199 | net::socks::connector{io.acceptor.local_endpoint()}("example.com", "8080", timeout); |
| 1200 | |
| 1201 | while (!io.connected) |
| 1202 | ASSERT_FALSE(sock.is_ready()); |
| 1203 | const std::uint8_t expected_bytes[] = { |
| 1204 | 4, 1, 0x1f, 0x90, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 1205 | 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm', 0x00 |
| 1206 | }; |
| 1207 | |
| 1208 | std::uint8_t actual_bytes[sizeof(expected_bytes)]; |
| 1209 | boost::asio::read(io.server, boost::asio::buffer(actual_bytes)); |
| 1210 | EXPECT_TRUE(std::memcmp(expected_bytes, actual_bytes, sizeof(actual_bytes)) == 0); |
| 1211 | |
| 1212 | const std::uint8_t reply_bytes[] = {0, 90, 0, 0, 0, 0, 0, 0}; |
| 1213 | boost::asio::write(io.server, boost::asio::buffer(reply_bytes)); |
| 1214 | |
| 1215 | ASSERT_EQ(boost::future_status::ready, sock.wait_for(boost::chrono::seconds{3})); |
| 1216 | EXPECT_TRUE(sock.get().is_open()); |
| 1217 | } |
| 1218 | |
| 1219 | TEST(socks_connector, ipv4) |
| 1220 | { |
| 1221 | io_thread io{}; |
| 1222 | boost::asio::steady_timer timeout{io.io_service}; |
| 1223 | timeout.expires_after(std::chrono::seconds{5}); |
| 1224 | |
| 1225 | boost::unique_future<boost::asio::ip::tcp::socket> sock = |
| 1226 | net::socks::connector{io.acceptor.local_endpoint()}("250.88.125.99", "8080", timeout); |
| 1227 | |
| 1228 | while (!io.connected) |
| 1229 | ASSERT_FALSE(sock.is_ready()); |
| 1230 | const std::uint8_t expected_bytes[] = { |
| 1231 | 4, 1, 0x1f, 0x90, 0xfa, 0x58, 0x7d, 0x63, 0x00 |
| 1232 | }; |
| 1233 | |
| 1234 | std::uint8_t actual_bytes[sizeof(expected_bytes)]; |
| 1235 | boost::asio::read(io.server, boost::asio::buffer(actual_bytes)); |
| 1236 | EXPECT_TRUE(std::memcmp(expected_bytes, actual_bytes, sizeof(actual_bytes)) == 0); |
| 1237 | |
| 1238 | const std::uint8_t reply_bytes[] = {0, 90, 0, 0, 0, 0, 0, 0}; |
| 1239 | boost::asio::write(io.server, boost::asio::buffer(reply_bytes)); |
| 1240 | |
| 1241 | ASSERT_EQ(boost::future_status::ready, sock.wait_for(boost::chrono::seconds{3})); |
| 1242 | EXPECT_TRUE(sock.get().is_open()); |
| 1243 | } |
| 1244 | |
| 1245 | TEST(socks_connector, error) |
| 1246 | { |
| 1247 | io_thread io{}; |
| 1248 | boost::asio::steady_timer timeout{io.io_service}; |
| 1249 | timeout.expires_after(std::chrono::seconds{5}); |
| 1250 | |
| 1251 | boost::unique_future<boost::asio::ip::tcp::socket> sock = |
| 1252 | net::socks::connector{io.acceptor.local_endpoint()}("250.88.125.99", "8080", timeout); |
| 1253 | |
| 1254 | while (!io.connected) |
| 1255 | ASSERT_FALSE(sock.is_ready()); |
| 1256 | const std::uint8_t expected_bytes[] = { |
| 1257 | 4, 1, 0x1f, 0x90, 0xfa, 0x58, 0x7d, 0x63, 0x00 |
| 1258 | }; |
| 1259 | |
| 1260 | std::uint8_t actual_bytes[sizeof(expected_bytes)]; |
| 1261 | boost::asio::read(io.server, boost::asio::buffer(actual_bytes)); |
| 1262 | EXPECT_TRUE(std::memcmp(expected_bytes, actual_bytes, sizeof(actual_bytes)) == 0); |
| 1263 | |
| 1264 | const std::uint8_t reply_bytes[] = {0, 91, 0, 0, 0, 0, 0, 0}; |
| 1265 | boost::asio::write(io.server, boost::asio::buffer(reply_bytes)); |
| 1266 | |
| 1267 | ASSERT_EQ(boost::future_status::ready, sock.wait_for(boost::chrono::seconds{3})); |
| 1268 | EXPECT_THROW(sock.get().is_open(), boost::system::system_error); |
| 1269 | } |
| 1270 | |
| 1271 | TEST(socks_connector, timeout) |
| 1272 | { |
| 1273 | io_thread io{}; |
| 1274 | boost::asio::steady_timer timeout{io.io_service}; |
| 1275 | timeout.expires_after(std::chrono::milliseconds{10}); |
| 1276 | |
| 1277 | boost::unique_future<boost::asio::ip::tcp::socket> sock = |
| 1278 | net::socks::connector{io.acceptor.local_endpoint()}("250.88.125.99", "8080", timeout); |
| 1279 | |
| 1280 | ASSERT_EQ(boost::future_status::ready, sock.wait_for(boost::chrono::seconds{3})); |
| 1281 | EXPECT_THROW(sock.get().is_open(), boost::system::system_error); |
| 1282 | } |
| 1283 | |
| 1284 | TEST(dandelionpp_map, traits) |
| 1285 | { |
| 1286 | EXPECT_TRUE(std::is_default_constructible<net::dandelionpp::connection_map>()); |
| 1287 | EXPECT_TRUE(std::is_move_constructible<net::dandelionpp::connection_map>()); |
| 1288 | EXPECT_TRUE(std::is_move_assignable<net::dandelionpp::connection_map>()); |
| 1289 | EXPECT_FALSE(std::is_copy_constructible<net::dandelionpp::connection_map>()); |
| 1290 | EXPECT_FALSE(std::is_copy_assignable<net::dandelionpp::connection_map>()); |
| 1291 | } |
| 1292 | |
| 1293 | TEST(dandelionpp_map, empty) |
| 1294 | { |
| 1295 | const net::dandelionpp::connection_map mapper{}; |
| 1296 | |
| 1297 | EXPECT_EQ(mapper.begin(), mapper.end()); |
| 1298 | EXPECT_EQ(0u, mapper.size()); |
| 1299 | |
| 1300 | const net::dandelionpp::connection_map cloned = mapper.clone(); |
| 1301 | EXPECT_EQ(cloned.begin(), cloned.end()); |
| 1302 | EXPECT_EQ(0u, cloned.size()); |
| 1303 | } |
| 1304 | |
| 1305 | TEST(dandelionpp_map, zero_stems) |
| 1306 | { |
| 1307 | std::vector<boost::uuids::uuid> connections{6}; |
| 1308 | for (auto &c: connections) |
| 1309 | c = boost::uuids::random_generator{}(); |
| 1310 | |
| 1311 | net::dandelionpp::connection_map mapper{connections, 0}; |
| 1312 | EXPECT_EQ(mapper.begin(), mapper.end()); |
| 1313 | EXPECT_EQ(0u, mapper.size()); |
| 1314 | |
| 1315 | for (const boost::uuids::uuid& connection : connections) |
| 1316 | EXPECT_TRUE(mapper.get_stem(connection).is_nil()); |
| 1317 | |
| 1318 | EXPECT_FALSE(mapper.update(connections)); |
| 1319 | EXPECT_EQ(mapper.begin(), mapper.end()); |
| 1320 | EXPECT_EQ(0u, mapper.size()); |
| 1321 | |
| 1322 | for (const boost::uuids::uuid& connection : connections) |
| 1323 | EXPECT_TRUE(mapper.get_stem(connection).is_nil()); |
| 1324 | |
| 1325 | const net::dandelionpp::connection_map cloned = mapper.clone(); |
| 1326 | EXPECT_EQ(cloned.end(), cloned.begin()); |
| 1327 | EXPECT_EQ(0u, cloned.size()); |
| 1328 | } |
| 1329 | |
| 1330 | TEST(dandelionpp_map, dropped_connection) |
| 1331 | { |
| 1332 | std::vector<boost::uuids::uuid> connections{6}; |
| 1333 | for (auto &c: connections) |
| 1334 | c = boost::uuids::random_generator{}(); |
| 1335 | std::sort(connections.begin(), connections.end()); |
| 1336 | |
| 1337 | // select 3 of 6 outgoing connections |
| 1338 | net::dandelionpp::connection_map mapper{connections, 3}; |
| 1339 | EXPECT_EQ(3u, mapper.size()); |
| 1340 | EXPECT_EQ(3, mapper.end() - mapper.begin()); |
| 1341 | { |
| 1342 | std::set<boost::uuids::uuid> used; |
| 1343 | for (const boost::uuids::uuid& connection : mapper) |
| 1344 | { |
| 1345 | EXPECT_TRUE(used.insert(connection).second); |
| 1346 | EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection)); |
| 1347 | } |
| 1348 | } |
| 1349 | { |
| 1350 | const net::dandelionpp::connection_map cloned = mapper.clone(); |
| 1351 | EXPECT_EQ(3u, cloned.size()); |
| 1352 | ASSERT_EQ(mapper.end() - mapper.begin(), cloned.end() - cloned.begin()); |
| 1353 | for (auto elem : boost::combine(mapper, cloned)) |
| 1354 | EXPECT_EQ(boost::get<0>(elem), boost::get<1>(elem)); |
| 1355 | } |
| 1356 | EXPECT_FALSE(mapper.update(connections)); |
| 1357 | EXPECT_EQ(3u, mapper.size()); |
| 1358 | ASSERT_EQ(3, mapper.end() - mapper.begin()); |
| 1359 | { |
| 1360 | std::set<boost::uuids::uuid> used; |
| 1361 | for (const boost::uuids::uuid& connection : mapper) |
| 1362 | { |
| 1363 | EXPECT_FALSE(connection.is_nil()); |
| 1364 | EXPECT_TRUE(used.insert(connection).second); |
| 1365 | EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection)); |
| 1366 | } |
| 1367 | } |
| 1368 | std::map<boost::uuids::uuid, boost::uuids::uuid> mapping; |
| 1369 | std::vector<boost::uuids::uuid> in_connections{9}; |
| 1370 | for (auto &c: in_connections) |
| 1371 | c = boost::uuids::random_generator{}(); |
| 1372 | { |
| 1373 | std::map<boost::uuids::uuid, std::size_t> used; |
| 1374 | std::multimap<boost::uuids::uuid, boost::uuids::uuid> inverse_mapping; |
| 1375 | for (const boost::uuids::uuid& connection : in_connections) |
| 1376 | { |
| 1377 | const boost::uuids::uuid out = mapper.get_stem(connection); |
| 1378 | EXPECT_FALSE(out.is_nil()); |
| 1379 | EXPECT_TRUE(mapping.emplace(connection, out).second); |
| 1380 | inverse_mapping.emplace(out, connection); |
| 1381 | used[out]++; |
| 1382 | } |
| 1383 | |
| 1384 | EXPECT_EQ(3u, used.size()); |
| 1385 | for (const std::pair<const boost::uuids::uuid, std::size_t>& entry : used) |
| 1386 | EXPECT_EQ(3u, entry.second); |
| 1387 | |
| 1388 | for (const boost::uuids::uuid& connection : in_connections) |
| 1389 | EXPECT_EQ(mapping[connection], mapper.get_stem(connection)); |
| 1390 | |
| 1391 | // drop 1 connection, and select replacement from 1 of unused 3. |
| 1392 | const boost::uuids::uuid lost_connection = *(++mapper.begin()); |
| 1393 | const auto elem = std::lower_bound(connections.begin(), connections.end(), lost_connection); |
| 1394 | ASSERT_NE(connections.end(), elem); |
| 1395 | ASSERT_EQ(lost_connection, *elem); |
| 1396 | connections.erase(elem); |
| 1397 | |
| 1398 | EXPECT_TRUE(mapper.update(connections)); |
| 1399 | EXPECT_EQ(3u, mapper.size()); |
| 1400 | ASSERT_EQ(3, mapper.end() - mapper.begin()); |
| 1401 | |
| 1402 | for (const boost::uuids::uuid& connection : mapper) |
| 1403 | { |
| 1404 | EXPECT_FALSE(connection.is_nil()); |
| 1405 | EXPECT_NE(lost_connection, connection); |
| 1406 | } |
| 1407 | |
| 1408 | const boost::uuids::uuid newly_mapped = *(++mapper.begin()); |
| 1409 | EXPECT_FALSE(newly_mapped.is_nil()); |
| 1410 | EXPECT_NE(lost_connection, newly_mapped); |
| 1411 | |
| 1412 | for (auto elems = inverse_mapping.equal_range(lost_connection); elems.first != elems.second; ++elems.first) |
| 1413 | mapping[elems.first->second] = newly_mapped; |
| 1414 | } |
| 1415 | { |
| 1416 | const net::dandelionpp::connection_map cloned = mapper.clone(); |
| 1417 | EXPECT_EQ(3u, cloned.size()); |
| 1418 | ASSERT_EQ(mapper.end() - mapper.begin(), cloned.end() - cloned.begin()); |
| 1419 | for (auto elem : boost::combine(mapper, cloned)) |
| 1420 | EXPECT_EQ(boost::get<0>(elem), boost::get<1>(elem)); |
| 1421 | } |
| 1422 | // mappings should remain evenly distributed amongst 2, with 3 sitting in waiting |
| 1423 | { |
| 1424 | std::set<boost::uuids::uuid> used; |
| 1425 | for (const boost::uuids::uuid& connection : mapper) |
| 1426 | { |
| 1427 | EXPECT_FALSE(connection.is_nil()); |
| 1428 | EXPECT_TRUE(used.insert(connection).second); |
| 1429 | EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection)); |
| 1430 | } |
| 1431 | } |
| 1432 | { |
| 1433 | std::map<boost::uuids::uuid, std::size_t> used; |
| 1434 | for (const boost::uuids::uuid& connection : in_connections) |
| 1435 | { |
| 1436 | const boost::uuids::uuid& out = mapper.get_stem(connection); |
| 1437 | EXPECT_FALSE(out.is_nil()); |
| 1438 | EXPECT_EQ(mapping[connection], out); |
| 1439 | used[out]++; |
| 1440 | } |
| 1441 | |
| 1442 | EXPECT_EQ(3u, used.size()); |
| 1443 | for (const std::pair<const boost::uuids::uuid, std::size_t>& entry : used) |
| 1444 | EXPECT_EQ(3u, entry.second); |
| 1445 | } |
| 1446 | { |
| 1447 | const net::dandelionpp::connection_map cloned = mapper.clone(); |
| 1448 | EXPECT_EQ(3u, cloned.size()); |
| 1449 | ASSERT_EQ(mapper.end() - mapper.begin(), cloned.end() - cloned.begin()); |
| 1450 | for (auto elem : boost::combine(mapper, cloned)) |
| 1451 | EXPECT_EQ(boost::get<0>(elem), boost::get<1>(elem)); |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | TEST(dandelionpp_map, dropped_connection_remapped) |
| 1456 | { |
| 1457 | boost::uuids::random_generator random_uuid{}; |
| 1458 | |
| 1459 | std::vector<boost::uuids::uuid> connections{3}; |
| 1460 | for (auto &e: connections) |
| 1461 | e = random_uuid(); |
| 1462 | std::sort(connections.begin(), connections.end()); |
| 1463 | |
| 1464 | // select 3 of 3 outgoing connections |
| 1465 | net::dandelionpp::connection_map mapper{connections, 3}; |
| 1466 | EXPECT_EQ(3u, mapper.size()); |
| 1467 | EXPECT_EQ(3, mapper.end() - mapper.begin()); |
| 1468 | { |
| 1469 | std::set<boost::uuids::uuid> used; |
| 1470 | for (const boost::uuids::uuid& connection : mapper) |
| 1471 | { |
| 1472 | EXPECT_FALSE(connection.is_nil()); |
| 1473 | EXPECT_TRUE(used.insert(connection).second); |
| 1474 | EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection)); |
| 1475 | } |
| 1476 | } |
| 1477 | EXPECT_FALSE(mapper.update(connections)); |
| 1478 | EXPECT_EQ(3u, mapper.size()); |
| 1479 | ASSERT_EQ(3, mapper.end() - mapper.begin()); |
| 1480 | { |
| 1481 | std::set<boost::uuids::uuid> used; |
| 1482 | for (const boost::uuids::uuid& connection : mapper) |
| 1483 | { |
| 1484 | EXPECT_FALSE(connection.is_nil()); |
| 1485 | EXPECT_TRUE(used.insert(connection).second); |
| 1486 | EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection)); |
| 1487 | } |
| 1488 | } |
| 1489 | std::map<boost::uuids::uuid, boost::uuids::uuid> mapping; |
| 1490 | std::vector<boost::uuids::uuid> in_connections{9}; |
| 1491 | for (auto &e: in_connections) |
| 1492 | e = random_uuid(); |
| 1493 | { |
| 1494 | std::map<boost::uuids::uuid, std::size_t> used; |
| 1495 | std::multimap<boost::uuids::uuid, boost::uuids::uuid> inverse_mapping; |
| 1496 | for (const boost::uuids::uuid& connection : in_connections) |
| 1497 | { |
| 1498 | const boost::uuids::uuid out = mapper.get_stem(connection); |
| 1499 | EXPECT_FALSE(out.is_nil()); |
| 1500 | EXPECT_TRUE(mapping.emplace(connection, out).second); |
| 1501 | inverse_mapping.emplace(out, connection); |
| 1502 | used[out]++; |
| 1503 | } |
| 1504 | |
| 1505 | EXPECT_EQ(3u, used.size()); |
| 1506 | for (const std::pair<const boost::uuids::uuid, std::size_t>& entry : used) |
| 1507 | EXPECT_EQ(3u, entry.second); |
| 1508 | |
| 1509 | for (const boost::uuids::uuid& connection : in_connections) |
| 1510 | EXPECT_EQ(mapping[connection], mapper.get_stem(connection)); |
| 1511 | |
| 1512 | // drop 1 connection leaving "hole" |
| 1513 | const boost::uuids::uuid lost_connection = *(++mapper.begin()); |
| 1514 | const auto elem = std::lower_bound(connections.begin(), connections.end(), lost_connection); |
| 1515 | ASSERT_NE(connections.end(), elem); |
| 1516 | ASSERT_EQ(lost_connection, *elem); |
| 1517 | connections.erase(elem); |
| 1518 | |
| 1519 | EXPECT_TRUE(mapper.update(connections)); |
| 1520 | EXPECT_EQ(2u, mapper.size()); |
| 1521 | EXPECT_EQ(3, mapper.end() - mapper.begin()); |
| 1522 | |
| 1523 | for (auto elems = inverse_mapping.equal_range(lost_connection); elems.first != elems.second; ++elems.first) |
| 1524 | mapping[elems.first->second] = boost::uuids::nil_uuid(); |
| 1525 | } |
| 1526 | // remap 3 connections and map 1 new connection to 2 remaining out connections |
| 1527 | in_connections.resize(10); |
| 1528 | in_connections[9] = random_uuid(); |
| 1529 | { |
| 1530 | std::map<boost::uuids::uuid, std::size_t> used; |
| 1531 | for (const boost::uuids::uuid& connection : in_connections) |
| 1532 | { |
| 1533 | const boost::uuids::uuid& out = mapper.get_stem(connection); |
| 1534 | EXPECT_FALSE(out.is_nil()); |
| 1535 | used[out]++; |
| 1536 | |
| 1537 | boost::uuids::uuid& expected = mapping[connection]; |
| 1538 | if (!expected.is_nil()) |
| 1539 | EXPECT_EQ(expected, out); |
| 1540 | else |
| 1541 | expected = out; |
| 1542 | } |
| 1543 | |
| 1544 | EXPECT_EQ(2u, used.size()); |
| 1545 | for (const std::pair<const boost::uuids::uuid, std::size_t>& entry : used) |
| 1546 | EXPECT_EQ(5u, entry.second); |
| 1547 | } |
| 1548 | // select 3 of 3 connections but do not remap existing links |
| 1549 | connections.resize(3); |
| 1550 | connections[2] = random_uuid(); |
| 1551 | EXPECT_TRUE(mapper.update(connections)); |
| 1552 | EXPECT_EQ(3u, mapper.size()); |
| 1553 | EXPECT_EQ(3, mapper.end() - mapper.begin()); |
| 1554 | { |
| 1555 | std::map<boost::uuids::uuid, std::size_t> used; |
| 1556 | for (const boost::uuids::uuid& connection : in_connections) |
| 1557 | { |
| 1558 | const boost::uuids::uuid& out = mapper.get_stem(connection); |
| 1559 | EXPECT_FALSE(out.is_nil()); |
| 1560 | used[out]++; |
| 1561 | |
| 1562 | EXPECT_EQ(mapping[connection], out); |
| 1563 | } |
| 1564 | |
| 1565 | EXPECT_EQ(2u, used.size()); |
| 1566 | for (const std::pair<const boost::uuids::uuid, std::size_t>& entry : used) |
| 1567 | EXPECT_EQ(5u, entry.second); |
| 1568 | } |
| 1569 | // map 8 new incoming connections across 3 outgoing links |
| 1570 | in_connections.resize(18); |
| 1571 | for (size_t i = 10; i < in_connections.size(); ++i) |
| 1572 | in_connections[i] = random_uuid(); |
| 1573 | { |
| 1574 | std::map<boost::uuids::uuid, std::size_t> used; |
| 1575 | for (const boost::uuids::uuid& connection : in_connections) |
| 1576 | { |
| 1577 | const boost::uuids::uuid& out = mapper.get_stem(connection); |
| 1578 | EXPECT_FALSE(out.is_nil()); |
| 1579 | used[out]++; |
| 1580 | |
| 1581 | boost::uuids::uuid& expected = mapping[connection]; |
| 1582 | if (!expected.is_nil()) |
| 1583 | EXPECT_EQ(expected, out); |
| 1584 | else |
| 1585 | expected = out; |
| 1586 | } |
| 1587 | |
| 1588 | EXPECT_EQ(3u, used.size()); |
| 1589 | for (const std::pair<const boost::uuids::uuid, std::size_t>& entry : used) |
| 1590 | EXPECT_EQ(6u, entry.second); |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | TEST(dandelionpp_map, dropped_all_connections) |
| 1595 | { |
| 1596 | boost::uuids::random_generator random_uuid{}; |
| 1597 | |
| 1598 | std::vector<boost::uuids::uuid> connections{8}; |
| 1599 | for (auto &e: connections) |
| 1600 | e = random_uuid(); |
| 1601 | std::sort(connections.begin(), connections.end()); |
| 1602 | |
| 1603 | // select 3 of 8 outgoing connections |
| 1604 | net::dandelionpp::connection_map mapper{connections, 3}; |
| 1605 | EXPECT_EQ(3u, mapper.size()); |
| 1606 | EXPECT_EQ(3, mapper.end() - mapper.begin()); |
| 1607 | { |
| 1608 | std::set<boost::uuids::uuid> used; |
| 1609 | for (const boost::uuids::uuid& connection : mapper) |
| 1610 | { |
| 1611 | EXPECT_FALSE(connection.is_nil()); |
| 1612 | EXPECT_TRUE(used.insert(connection).second); |
| 1613 | EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection)); |
| 1614 | } |
| 1615 | } |
| 1616 | EXPECT_FALSE(mapper.update(connections)); |
| 1617 | EXPECT_EQ(3u, mapper.size()); |
| 1618 | ASSERT_EQ(3, mapper.end() - mapper.begin()); |
| 1619 | { |
| 1620 | std::set<boost::uuids::uuid> used; |
| 1621 | for (const boost::uuids::uuid& connection : mapper) |
| 1622 | { |
| 1623 | EXPECT_FALSE(connection.is_nil()); |
| 1624 | EXPECT_TRUE(used.insert(connection).second); |
| 1625 | EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection)); |
| 1626 | } |
| 1627 | } |
| 1628 | std::vector<boost::uuids::uuid> in_connections{9}; |
| 1629 | for (auto &e: in_connections) |
| 1630 | e = random_uuid(); |
| 1631 | { |
| 1632 | std::map<boost::uuids::uuid, std::size_t> used; |
| 1633 | std::map<boost::uuids::uuid, boost::uuids::uuid> mapping; |
| 1634 | for (const boost::uuids::uuid& connection : in_connections) |
| 1635 | { |
| 1636 | const boost::uuids::uuid out = mapper.get_stem(connection); |
| 1637 | EXPECT_FALSE(out.is_nil()); |
| 1638 | EXPECT_TRUE(mapping.emplace(connection, out).second); |
| 1639 | used[out]++; |
| 1640 | } |
| 1641 | |
| 1642 | EXPECT_EQ(3u, used.size()); |
| 1643 | for (const std::pair<const boost::uuids::uuid, std::size_t>& entry : used) |
| 1644 | EXPECT_EQ(3u, entry.second); |
| 1645 | |
| 1646 | for (const boost::uuids::uuid& connection : in_connections) |
| 1647 | EXPECT_EQ(mapping[connection], mapper.get_stem(connection)); |
| 1648 | |
| 1649 | // drop all connections |
| 1650 | connections.clear(); |
| 1651 | |
| 1652 | EXPECT_TRUE(mapper.update(connections)); |
| 1653 | EXPECT_EQ(0u, mapper.size()); |
| 1654 | EXPECT_EQ(3, mapper.end() - mapper.begin()); |
| 1655 | } |
| 1656 | // remap 7 connections to nothing |
| 1657 | for (const boost::uuids::uuid& connection : boost::adaptors::slice(in_connections, 0, 7)) |
| 1658 | EXPECT_TRUE(mapper.get_stem(connection).is_nil()); |
| 1659 | |
| 1660 | // select 3 of 30 connections, only 7 should be remapped to new indexes (but all to new uuids) |
| 1661 | connections.resize(30); |
| 1662 | for (auto &e: connections) |
| 1663 | e = random_uuid(); |
| 1664 | EXPECT_TRUE(mapper.update(connections)); |
| 1665 | { |
| 1666 | std::map<boost::uuids::uuid, std::size_t> used; |
| 1667 | for (const boost::uuids::uuid& connection : in_connections) |
| 1668 | { |
| 1669 | const boost::uuids::uuid& out = mapper.get_stem(connection); |
| 1670 | EXPECT_FALSE(out.is_nil()); |
| 1671 | used[out]++; |
| 1672 | } |
| 1673 | |
| 1674 | EXPECT_EQ(3u, used.size()); |
| 1675 | for (const std::pair<const boost::uuids::uuid, std::size_t>& entry : used) |
| 1676 | EXPECT_EQ(3u, entry.second); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | TEST(zmq, error_codes) |
| 1681 | { |
| 1682 | EXPECT_EQ( |
| 1683 | std::addressof(net::zmq::error_category()), |
| 1684 | std::addressof(net::zmq::make_error_code(0).category()) |
| 1685 | ); |
| 1686 | EXPECT_EQ( |
| 1687 | std::make_error_condition(std::errc::not_a_socket), |
| 1688 | net::zmq::make_error_code(ENOTSOCK) |
| 1689 | ); |
| 1690 | |
| 1691 | EXPECT_TRUE( |
| 1692 | []() -> expect<void> |
| 1693 | { |
| 1694 | MONERO_ZMQ_CHECK(zmq_msg_send(nullptr, nullptr, 0)); |
| 1695 | return success(); |
| 1696 | }().matches(std::errc::not_a_socket) |
| 1697 | ); |
| 1698 | |
| 1699 | bool thrown = false; |
| 1700 | try |
| 1701 | { |
| 1702 | MONERO_ZMQ_THROW("stuff"); |
| 1703 | } |
| 1704 | catch (const std::system_error& e) |
| 1705 | { |
| 1706 | thrown = true; |
| 1707 | EXPECT_EQ(std::make_error_condition(std::errc::not_a_socket), e.code()); |
| 1708 | } |
| 1709 | EXPECT_TRUE(thrown); |
| 1710 | } |
| 1711 | |
| 1712 | TEST(zmq, read_write) |
| 1713 | { |
| 1714 | net::zmq::context context{zmq_init(1)}; |
| 1715 | ASSERT_NE(nullptr, context); |
| 1716 | |
| 1717 | net::zmq::socket send_socket{zmq_socket(context.get(), ZMQ_REQ)}; |
| 1718 | net::zmq::socket recv_socket{zmq_socket(context.get(), ZMQ_REP)}; |
| 1719 | ASSERT_NE(nullptr, send_socket); |
| 1720 | ASSERT_NE(nullptr, recv_socket); |
| 1721 | |
| 1722 | ASSERT_EQ(0u, zmq_bind(recv_socket.get(), "inproc://testing")); |
| 1723 | ASSERT_EQ(0u, zmq_connect(send_socket.get(), "inproc://testing")); |
| 1724 | |
| 1725 | std::string message; |
| 1726 | message.resize(1024); |
| 1727 | crypto::rand(message.size(), reinterpret_cast<std::uint8_t*>(std::addressof(message[0]))); |
| 1728 | |
| 1729 | ASSERT_TRUE(bool(net::zmq::send(epee::strspan<std::uint8_t>(message), send_socket.get()))); |
| 1730 | |
| 1731 | const expect<std::string> received = net::zmq::receive(recv_socket.get()); |
| 1732 | ASSERT_TRUE(bool(received)); |
| 1733 | EXPECT_EQ(message, *received); |
| 1734 | } |
| 1735 | |
| 1736 | TEST(zmq, read_write_slice) |
| 1737 | { |
| 1738 | net::zmq::context context{zmq_init(1)}; |
| 1739 | ASSERT_NE(nullptr, context); |
| 1740 | |
| 1741 | net::zmq::socket send_socket{zmq_socket(context.get(), ZMQ_REQ)}; |
| 1742 | net::zmq::socket recv_socket{zmq_socket(context.get(), ZMQ_REP)}; |
| 1743 | ASSERT_NE(nullptr, send_socket); |
| 1744 | ASSERT_NE(nullptr, recv_socket); |
| 1745 | |
| 1746 | ASSERT_EQ(0u, zmq_bind(recv_socket.get(), "inproc://testing")); |
| 1747 | ASSERT_EQ(0u, zmq_connect(send_socket.get(), "inproc://testing")); |
| 1748 | |
| 1749 | std::string message; |
| 1750 | message.resize(1024); |
| 1751 | crypto::rand(message.size(), reinterpret_cast<std::uint8_t*>(std::addressof(message[0]))); |
| 1752 | |
| 1753 | { |
| 1754 | epee::byte_slice slice_message{{epee::strspan<std::uint8_t>(message)}}; |
| 1755 | ASSERT_TRUE(bool(net::zmq::send(std::move(slice_message), send_socket.get()))); |
| 1756 | EXPECT_TRUE(slice_message.empty()); |
| 1757 | } |
| 1758 | |
| 1759 | const expect<std::string> received = net::zmq::receive(recv_socket.get()); |
| 1760 | ASSERT_TRUE(bool(received)); |
| 1761 | EXPECT_EQ(message, *received); |
| 1762 | } |
| 1763 | |
| 1764 | TEST(zmq, write_slice_fail) |
| 1765 | { |
| 1766 | std::string message; |
| 1767 | message.resize(1024); |
| 1768 | crypto::rand(message.size(), reinterpret_cast<std::uint8_t*>(std::addressof(message[0]))); |
| 1769 | |
| 1770 | epee::byte_slice slice_message{std::move(message)}; |
| 1771 | EXPECT_FALSE(bool(net::zmq::send(std::move(slice_message), nullptr))); |
| 1772 | EXPECT_TRUE(slice_message.empty()); |
| 1773 | } |
| 1774 | |
| 1775 | TEST(zmq, read_write_multipart) |
| 1776 | { |
| 1777 | net::zmq::context context{zmq_init(1)}; |
| 1778 | ASSERT_NE(nullptr, context); |
| 1779 | |
| 1780 | net::zmq::socket send_socket{zmq_socket(context.get(), ZMQ_REQ)}; |
| 1781 | net::zmq::socket recv_socket{zmq_socket(context.get(), ZMQ_REP)}; |
| 1782 | ASSERT_NE(nullptr, send_socket); |
| 1783 | ASSERT_NE(nullptr, recv_socket); |
| 1784 | |
| 1785 | ASSERT_EQ(0u, zmq_bind(recv_socket.get(), "inproc://testing")); |
| 1786 | ASSERT_EQ(0u, zmq_connect(send_socket.get(), "inproc://testing")); |
| 1787 | |
| 1788 | std::string message; |
| 1789 | message.resize(999); |
| 1790 | crypto::rand(message.size(), reinterpret_cast<std::uint8_t*>(std::addressof(message[0]))); |
| 1791 | |
| 1792 | for (unsigned i = 0; i < 3; ++i) |
| 1793 | { |
| 1794 | const expect<std::string> received = net::zmq::receive(recv_socket.get(), ZMQ_DONTWAIT); |
| 1795 | ASSERT_FALSE(bool(received)); |
| 1796 | EXPECT_EQ(net::zmq::make_error_code(EAGAIN), received.error()); |
| 1797 | |
| 1798 | const epee::span<const std::uint8_t> bytes{ |
| 1799 | reinterpret_cast<const std::uint8_t*>(std::addressof(message[0])) + (i * 333), 333 |
| 1800 | }; |
| 1801 | ASSERT_TRUE(bool(net::zmq::send(bytes, send_socket.get(), (i == 2 ? 0 : ZMQ_SNDMORE)))); |
| 1802 | } |
| 1803 | |
| 1804 | const expect<std::string> received = net::zmq::receive(recv_socket.get(), ZMQ_DONTWAIT); |
| 1805 | ASSERT_TRUE(bool(received)); |
| 1806 | EXPECT_EQ(message, *received); |
| 1807 | } |
| 1808 | |
| 1809 | TEST(zmq, read_write_termination) |
| 1810 | { |
| 1811 | net::zmq::context context{zmq_init(1)}; |
| 1812 | ASSERT_NE(nullptr, context); |
| 1813 | |
| 1814 | // must be declared before sockets and after context |
| 1815 | boost::scoped_thread<> thread{}; |
| 1816 | |
| 1817 | net::zmq::socket send_socket{zmq_socket(context.get(), ZMQ_REQ)}; |
| 1818 | net::zmq::socket recv_socket{zmq_socket(context.get(), ZMQ_REP)}; |
| 1819 | ASSERT_NE(nullptr, send_socket); |
| 1820 | ASSERT_NE(nullptr, recv_socket); |
| 1821 | |
| 1822 | ASSERT_EQ(0u, zmq_bind(recv_socket.get(), "inproc://testing")); |
| 1823 | ASSERT_EQ(0u, zmq_connect(send_socket.get(), "inproc://testing")); |
| 1824 | |
| 1825 | std::string message; |
| 1826 | message.resize(1024); |
| 1827 | crypto::rand(message.size(), reinterpret_cast<std::uint8_t*>(std::addressof(message[0]))); |
| 1828 | |
| 1829 | ASSERT_TRUE(bool(net::zmq::send(epee::strspan<std::uint8_t>(message), send_socket.get(), ZMQ_SNDMORE))); |
| 1830 | |
| 1831 | expect<std::string> received = net::zmq::receive(recv_socket.get(), ZMQ_DONTWAIT); |
| 1832 | ASSERT_FALSE(bool(received)); |
| 1833 | EXPECT_EQ(net::zmq::make_error_code(EAGAIN), received.error()); |
| 1834 | |
| 1835 | thread = boost::scoped_thread<>{ |
| 1836 | boost::thread{ |
| 1837 | [&context] () { context.reset(); } |
| 1838 | } |
| 1839 | }; |
| 1840 | |
| 1841 | received = net::zmq::receive(recv_socket.get()); |
| 1842 | ASSERT_FALSE(bool(received)); |
| 1843 | EXPECT_EQ(net::zmq::make_error_code(ETERM), received.error()); |
| 1844 | } |
| 1845 |