diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/README.md | 4 | ||||
-rw-r--r-- | tests/difficulty/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tests/difficulty/difficulty.cpp | 64 | ||||
-rw-r--r-- | tests/difficulty/gen_wide_data.py | 47 | ||||
-rwxr-xr-x | tests/difficulty/wide_difficulty.py | 22 | ||||
-rw-r--r-- | tests/fuzz/cold-outputs.cpp | 2 | ||||
-rw-r--r-- | tests/fuzz/cold-transaction.cpp | 2 | ||||
-rw-r--r-- | tests/fuzz/signature.cpp | 2 | ||||
-rw-r--r-- | tests/hash-target.cpp | 12 | ||||
-rw-r--r-- | tests/performance_tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/performance_tests/check_hash.h | 66 | ||||
-rw-r--r-- | tests/performance_tests/main.cpp | 9 | ||||
-rw-r--r-- | tests/unit_tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/unit_tests/difficulty.cpp | 68 | ||||
-rw-r--r-- | tests/unit_tests/multisig.cpp | 2 | ||||
-rw-r--r-- | tests/unit_tests/net.cpp | 90 | ||||
-rw-r--r-- | tests/unit_tests/serialization.cpp | 23 |
17 files changed, 403 insertions, 15 deletions
diff --git a/tests/README.md b/tests/README.md index 001ab6154..053dd2244 100644 --- a/tests/README.md +++ b/tests/README.md @@ -18,7 +18,7 @@ Tests are located in `tests/core_tests/`, and follow a straightforward naming co To run only Monero's core tests (after building): ``` -cd build/debug/tests/core +cd build/debug/tests/core_tests ctest ``` @@ -103,6 +103,8 @@ cd build/debug/tests/performance_tests ./performance_tests ``` +The path may be build/Linux/master/debug (adapt as necessary for your platform). + If the `performance_tests` binary does not exist, try running `make` in the `build/debug/tests/performance_tests` directory. To run the same tests on a release build, replace `debug` with `release`. diff --git a/tests/difficulty/CMakeLists.txt b/tests/difficulty/CMakeLists.txt index 2ed495806..fb0dd6b9e 100644 --- a/tests/difficulty/CMakeLists.txt +++ b/tests/difficulty/CMakeLists.txt @@ -45,3 +45,6 @@ set_property(TARGET difficulty-tests add_test( NAME difficulty COMMAND difficulty-tests "${CMAKE_CURRENT_SOURCE_DIR}/data.txt") +add_test( + NAME wide_difficulty + COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/wide_difficulty.py" "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/gen_wide_data.py" "${CMAKE_CURRENT_BINARY_DIR}/difficulty-tests" "${CMAKE_CURRENT_BINARY_DIR}/wide_data.txt") diff --git a/tests/difficulty/difficulty.cpp b/tests/difficulty/difficulty.cpp index ee20e27e4..9985b8710 100644 --- a/tests/difficulty/difficulty.cpp +++ b/tests/difficulty/difficulty.cpp @@ -43,16 +43,62 @@ using namespace std; #define DEFAULT_TEST_DIFFICULTY_TARGET 120 +static int test_wide_difficulty(const char *filename) +{ + std::vector<uint64_t> timestamps; + std::vector<cryptonote::difficulty_type> cumulative_difficulties; + fstream data(filename, fstream::in); + data.exceptions(fstream::badbit); + data.clear(data.rdstate()); + uint64_t timestamp; + cryptonote::difficulty_type difficulty, cumulative_difficulty = 0; + size_t n = 0; + while (data >> timestamp >> difficulty) { + size_t begin, end; + if (n < DIFFICULTY_WINDOW + DIFFICULTY_LAG) { + begin = 0; + end = min(n, (size_t) DIFFICULTY_WINDOW); + } else { + end = n - DIFFICULTY_LAG; + begin = end - DIFFICULTY_WINDOW; + } + cryptonote::difficulty_type res = cryptonote::next_difficulty( + std::vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end), + std::vector<cryptonote::difficulty_type>(cumulative_difficulties.begin() + begin, cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET); + if (res != difficulty) { + cerr << "Wrong wide difficulty for block " << n << endl + << "Expected: " << difficulty << endl + << "Found: " << res << endl; + return 1; + } + timestamps.push_back(timestamp); + cumulative_difficulties.push_back(cumulative_difficulty += difficulty); + ++n; + } + if (!data.eof()) { + data.clear(fstream::badbit); + } + return 0; +} + int main(int argc, char *argv[]) { - if (argc != 2) { + if (argc < 2) { cerr << "Wrong arguments" << endl; return 1; } + if (argc == 3 && strcmp(argv[1], "--wide") == 0) + { + return test_wide_difficulty(argv[2]); + } + vector<uint64_t> timestamps, cumulative_difficulties; + std::vector<cryptonote::difficulty_type> wide_cumulative_difficulties; fstream data(argv[1], fstream::in); data.exceptions(fstream::badbit); data.clear(data.rdstate()); - uint64_t timestamp, difficulty, cumulative_difficulty = 0; + uint64_t timestamp; + uint64_t difficulty, cumulative_difficulty = 0; + cryptonote::difficulty_type wide_cumulative_difficulty = 0; size_t n = 0; while (data >> timestamp >> difficulty) { size_t begin, end; @@ -63,17 +109,27 @@ int main(int argc, char *argv[]) { end = n - DIFFICULTY_LAG; begin = end - DIFFICULTY_WINDOW; } - uint64_t res = cryptonote::next_difficulty( + uint64_t res = cryptonote::next_difficulty_64( vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end), - vector<uint64_t>(cumulative_difficulties.begin() + begin, cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET); + std::vector<uint64_t>(cumulative_difficulties.begin() + begin, cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET); if (res != difficulty) { cerr << "Wrong difficulty for block " << n << endl << "Expected: " << difficulty << endl << "Found: " << res << endl; return 1; } + cryptonote::difficulty_type wide_res = cryptonote::next_difficulty( + std::vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end), + std::vector<cryptonote::difficulty_type>(wide_cumulative_difficulties.begin() + begin, wide_cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET); + if (wide_res.convert_to<uint64_t>() != res) { + cerr << "Wrong wide difficulty for block " << n << endl + << "Expected: " << res << endl + << "Found: " << wide_res << endl; + return 1; + } timestamps.push_back(timestamp); cumulative_difficulties.push_back(cumulative_difficulty += difficulty); + wide_cumulative_difficulties.push_back(wide_cumulative_difficulty += difficulty); ++n; } if (!data.eof()) { diff --git a/tests/difficulty/gen_wide_data.py b/tests/difficulty/gen_wide_data.py new file mode 100644 index 000000000..64af4e208 --- /dev/null +++ b/tests/difficulty/gen_wide_data.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +from __future__ import print_function +import random + +DIFFICULTY_TARGET = 120 +DIFFICULTY_WINDOW = 720 +DIFFICULTY_LAG = 15 +DIFFICULTY_CUT = 60 + +def difficulty(): + times = [] + diffs = [] + while True: + if len(times) <= 1: + diff = 1 + else: + begin = max(len(times) - DIFFICULTY_WINDOW - DIFFICULTY_LAG, 0) + end = min(begin + DIFFICULTY_WINDOW, len(times)) + length = end - begin + assert length >= 2 + if length <= DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT: + cut_begin = 0 + cut_end = length + else: + excess = length - (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) + cut_begin = (excess + 1) // 2 + cut_end = length - excess // 2 + assert cut_begin + 2 <= cut_end + wnd = times[begin:end] + wnd.sort() + dtime = wnd[cut_end - 1] - wnd[cut_begin] + dtime = max(dtime, 1) + ddiff = sum(diffs[begin + cut_begin + 1:begin + cut_end]) + diff = (ddiff * DIFFICULTY_TARGET + dtime - 1) // dtime + times.append((yield diff)) + diffs.append(diff) + +random.seed(1) +time = 1000 +gen = difficulty() +diff = next(gen) +for i in range(100000): + power = 100 if i < 10000 else 100000000 if i < 500 else 1000000000000 if i < 1000 else 1000000000000000 if i < 2000 else 10000000000000000000 if i < 4000 else 1000000000000000000000000 + time += random.randint(-diff // power - 10, 3 * diff // power + 10) + print(time, diff) + diff = gen.send(time) diff --git a/tests/difficulty/wide_difficulty.py b/tests/difficulty/wide_difficulty.py new file mode 100755 index 000000000..41a2a632d --- /dev/null +++ b/tests/difficulty/wide_difficulty.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +from __future__ import print_function +import sys +import subprocess + +python = sys.argv[1] +py = sys.argv[2] +c = sys.argv[3] +data = sys.argv[4] + +first = python + " " + py + " > " + data +second = [c, '--wide', data] + +try: + print('running: ', first) + subprocess.check_call(first, shell=True) + print('running: ', second) + subprocess.check_call(second) +except: + sys.exit(1) + diff --git a/tests/fuzz/cold-outputs.cpp b/tests/fuzz/cold-outputs.cpp index f3f35e140..f4050c948 100644 --- a/tests/fuzz/cold-outputs.cpp +++ b/tests/fuzz/cold-outputs.cpp @@ -53,7 +53,7 @@ int ColdOutputsFuzzer::init() try { - wallet.init(""); + wallet.init("", boost::none, boost::asio::ip::tcp::endpoint{}, 0, true, epee::net_utils::ssl_support_t::e_ssl_support_disabled); wallet.set_subaddress_lookahead(1, 1); wallet.generate("", "", spendkey, true, false); } diff --git a/tests/fuzz/cold-transaction.cpp b/tests/fuzz/cold-transaction.cpp index 83493f8a0..08117281b 100644 --- a/tests/fuzz/cold-transaction.cpp +++ b/tests/fuzz/cold-transaction.cpp @@ -54,7 +54,7 @@ int ColdTransactionFuzzer::init() try { - wallet.init(""); + wallet.init("", boost::none, boost::asio::ip::tcp::endpoint{}, 0, true, epee::net_utils::ssl_support_t::e_ssl_support_disabled); wallet.set_subaddress_lookahead(1, 1); wallet.generate("", "", spendkey, true, false); } diff --git a/tests/fuzz/signature.cpp b/tests/fuzz/signature.cpp index ab8faa29f..038378ae2 100644 --- a/tests/fuzz/signature.cpp +++ b/tests/fuzz/signature.cpp @@ -54,7 +54,7 @@ int SignatureFuzzer::init() try { - wallet.init(""); + wallet.init("", boost::none, boost::asio::ip::tcp::endpoint{}, 0, true, epee::net_utils::ssl_support_t::e_ssl_support_disabled); wallet.set_subaddress_lookahead(1, 1); wallet.generate("", "", spendkey, true, false); diff --git a/tests/hash-target.cpp b/tests/hash-target.cpp index 70368ce24..12acc5a67 100644 --- a/tests/hash-target.cpp +++ b/tests/hash-target.cpp @@ -40,7 +40,7 @@ using cryptonote::check_hash; int main(int argc, char *argv[]) { crypto::hash h; - for (uint64_t diff = 1;; diff += 1 + (diff >> 8)) { + for (cryptonote::difficulty_type diff = 1;; diff += 1 + (diff >> 8)) { for (uint16_t b = 0; b < 256; b++) { memset(&h, b, sizeof(crypto::hash)); if (check_hash(h, diff) != (b == 0 || diff <= 255 / b)) { @@ -50,7 +50,7 @@ int main(int argc, char *argv[]) { memset(&h, 0, sizeof(crypto::hash)); ((char *) &h)[31] = b; if (check_hash(h, diff) != (diff <= 255 / b)) { - return 1; + return 2; } } } @@ -58,11 +58,11 @@ int main(int argc, char *argv[]) { uint64_t val = 0; for (int i = 31; i >= 0; i--) { val = val * 256 + 255; - ((char *) &h)[i] = static_cast<char>(val / diff); - val %= diff; + ((char *) &h)[i] = static_cast<char>(static_cast<uint64_t>(val / diff)); + val %= diff.convert_to<uint64_t>(); } if (check_hash(h, diff) != true) { - return 1; + return 3; } if (diff > 1) { for (int i = 0;; i++) { @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) { } } if (check_hash(h, diff) != false) { - return 1; + return 4; } } } diff --git a/tests/performance_tests/CMakeLists.txt b/tests/performance_tests/CMakeLists.txt index aa424da80..b36df10dc 100644 --- a/tests/performance_tests/CMakeLists.txt +++ b/tests/performance_tests/CMakeLists.txt @@ -31,6 +31,7 @@ set(performance_tests_sources set(performance_tests_headers check_tx_signature.h + check_hash.h cn_slow_hash.h construct_tx.h derive_public_key.h diff --git a/tests/performance_tests/check_hash.h b/tests/performance_tests/check_hash.h new file mode 100644 index 000000000..d24001903 --- /dev/null +++ b/tests/performance_tests/check_hash.h @@ -0,0 +1,66 @@ +// Copyright (c) 2019, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#pragma once + +#include "string_tools.h" +#include "cryptonote_basic/difficulty.h" + +template<uint64_t hash_target_high, uint64_t hash_target_low, uint64_t difficulty_high, uint64_t difficulty_low> +class test_check_hash +{ +public: + static const size_t loop_count = 100000; + + bool init() + { + cryptonote::difficulty_type hash_target = hash_target_high; + hash_target = (hash_target << 64) | hash_target_low; + difficulty = difficulty_high; + difficulty = (difficulty << 64) | difficulty_low; + boost::multiprecision::uint256_t hash_value = std::numeric_limits<boost::multiprecision::uint256_t>::max() / hash_target; + ((uint64_t*)&hash)[0] = (hash_value << 64 >> 64).convert_to<uint64_t>(); + hash_value >>= 64; + ((uint64_t*)&hash)[1] = (hash_value << 64 >> 64).convert_to<uint64_t>(); + hash_value >>= 64; + ((uint64_t*)&hash)[2] = (hash_value << 64 >> 64).convert_to<uint64_t>(); + hash_value >>= 64; + ((uint64_t*)&hash)[3] = (hash_value << 64 >> 64).convert_to<uint64_t>(); + return true; + } + + bool test() + { + cryptonote::check_hash_128(hash, difficulty); + return true; + } + +private: + crypto::hash hash; + cryptonote::difficulty_type difficulty; +}; diff --git a/tests/performance_tests/main.cpp b/tests/performance_tests/main.cpp index e6558a364..c32e0df20 100644 --- a/tests/performance_tests/main.cpp +++ b/tests/performance_tests/main.cpp @@ -38,6 +38,7 @@ // tests #include "construct_tx.h" #include "check_tx_signature.h" +#include "check_hash.h" #include "cn_slow_hash.h" #include "derive_public_key.h" #include "derive_secret_key.h" @@ -181,6 +182,14 @@ int main(int argc, char** argv) TEST_PERFORMANCE4(filter, p, test_check_tx_signature_aggregated_bulletproofs, 2, 2, 56, 16); TEST_PERFORMANCE4(filter, p, test_check_tx_signature_aggregated_bulletproofs, 10, 2, 56, 16); + TEST_PERFORMANCE4(filter, p, test_check_hash, 0, 1, 0, 1); + TEST_PERFORMANCE4(filter, p, test_check_hash, 0, 0xffffffffffffffff, 0, 0xffffffffffffffff); + TEST_PERFORMANCE4(filter, p, test_check_hash, 0, 0xffffffffffffffff, 0, 1); + TEST_PERFORMANCE4(filter, p, test_check_hash, 1, 0, 1, 0); + TEST_PERFORMANCE4(filter, p, test_check_hash, 1, 0, 0, 1); + TEST_PERFORMANCE4(filter, p, test_check_hash, 0xffffffffffffffff, 0xffffffffffffffff, 0, 1); + TEST_PERFORMANCE4(filter, p, test_check_hash, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff); + TEST_PERFORMANCE0(filter, p, test_is_out_to_acc); TEST_PERFORMANCE0(filter, p, test_is_out_to_acc_precomp); TEST_PERFORMANCE0(filter, p, test_generate_key_image_helper); diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index bfaea6add..56a1f8c4d 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -43,6 +43,7 @@ set(unit_tests_sources crypto.cpp decompose_amount_into_digits.cpp device.cpp + difficulty.cpp dns_resolver.cpp epee_boosted_tcp_server.cpp epee_levin_protocol_handler_async.cpp diff --git a/tests/unit_tests/difficulty.cpp b/tests/unit_tests/difficulty.cpp new file mode 100644 index 000000000..090fecc84 --- /dev/null +++ b/tests/unit_tests/difficulty.cpp @@ -0,0 +1,68 @@ +// Copyright (c) 2019, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "gtest/gtest.h" +#include "cryptonote_basic/difficulty.h" + +static cryptonote::difficulty_type MKDIFF(uint64_t high, uint64_t low) +{ + cryptonote::difficulty_type d = high; + d = (d << 64) | low; + return d; +} + +static crypto::hash MKHASH(uint64_t high, uint64_t low) +{ + cryptonote::difficulty_type hash_target = high; + hash_target = (hash_target << 64) | low; + boost::multiprecision::uint256_t hash_value = std::numeric_limits<boost::multiprecision::uint256_t>::max() / hash_target; + crypto::hash h; + ((uint64_t*)&h)[0] = hash_value.convert_to<uint64_t>(); + hash_value >>= 64; + ((uint64_t*)&h)[1] = hash_value.convert_to<uint64_t>(); + hash_value >>= 64; + ((uint64_t*)&h)[2] = hash_value.convert_to<uint64_t>(); + hash_value >>= 64; + ((uint64_t*)&h)[3] = hash_value.convert_to<uint64_t>(); + return h; +} + +TEST(difficulty, check_hash) +{ + ASSERT_TRUE(cryptonote::check_hash(MKHASH(0, 1), MKDIFF(0, 1))); + ASSERT_FALSE(cryptonote::check_hash(MKHASH(0, 1), MKDIFF(0, 2))); + + ASSERT_TRUE(cryptonote::check_hash(MKHASH(0, 0xffffffffffffffff), MKDIFF(0, 0xffffffffffffffff))); + ASSERT_FALSE(cryptonote::check_hash(MKHASH(0, 0xffffffffffffffff), MKDIFF(1, 0))); + + ASSERT_TRUE(cryptonote::check_hash(MKHASH(1, 1), MKDIFF(1, 1))); + ASSERT_FALSE(cryptonote::check_hash(MKHASH(1, 1), MKDIFF(1, 2))); + + ASSERT_TRUE(cryptonote::check_hash(MKHASH(0xffffffffffffffff, 1), MKDIFF(0xffffffffffffffff, 1))); + ASSERT_FALSE(cryptonote::check_hash(MKHASH(0xffffffffffffffff, 1), MKDIFF(0xffffffffffffffff, 2))); +} diff --git a/tests/unit_tests/multisig.cpp b/tests/unit_tests/multisig.cpp index 9a74b3dce..c5917200e 100644 --- a/tests/unit_tests/multisig.cpp +++ b/tests/unit_tests/multisig.cpp @@ -71,7 +71,7 @@ static void make_wallet(unsigned int idx, tools::wallet2 &wallet) try { - wallet.init(""); + wallet.init("", boost::none, boost::asio::ip::tcp::endpoint{}, 0, true, epee::net_utils::ssl_support_t::e_ssl_support_disabled); wallet.set_subaddress_lookahead(1, 1); wallet.generate("", "", spendkey, true, false); ASSERT_TRUE(test_addresses[idx].address == wallet.get_account().get_public_address_str(cryptonote::TESTNET)); diff --git a/tests/unit_tests/net.cpp b/tests/unit_tests/net.cpp index a38ecfe81..77fb71d96 100644 --- a/tests/unit_tests/net.cpp +++ b/tests/unit_tests/net.cpp @@ -33,6 +33,7 @@ #include <boost/asio/io_service.hpp> #include <boost/asio/ip/tcp.hpp> #include <boost/asio/read.hpp> +#include <boost/asio/steady_timer.hpp> #include <boost/asio/write.hpp> #include <boost/endian/conversion.hpp> #include <boost/system/error_code.hpp> @@ -45,6 +46,7 @@ #include "net/error.h" #include "net/net_utils_base.h" #include "net/socks.h" +#include "net/socks_connect.h" #include "net/parse.h" #include "net/tor_address.h" #include "p2p/net_peerlist_boost_serialization.h" @@ -742,4 +744,92 @@ TEST(socks_client, resolve_command) while (test_client->called_ == 1); } +TEST(socks_connector, host) +{ + io_thread io{}; + boost::asio::steady_timer timeout{io.io_service}; + timeout.expires_from_now(std::chrono::seconds{5}); + + boost::unique_future<boost::asio::ip::tcp::socket> sock = + net::socks::connector{io.acceptor.local_endpoint()}("example.com", "8080", timeout); + + while (!io.connected); + const std::uint8_t expected_bytes[] = { + 4, 1, 0x1f, 0x90, 0x00, 0x00, 0x00, 0x01, 0x00, + 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm', 0x00 + }; + + std::uint8_t actual_bytes[sizeof(expected_bytes)]; + boost::asio::read(io.server, boost::asio::buffer(actual_bytes)); + EXPECT_TRUE(std::memcmp(expected_bytes, actual_bytes, sizeof(actual_bytes)) == 0); + + const std::uint8_t reply_bytes[] = {0, 90, 0, 0, 0, 0, 0, 0}; + boost::asio::write(io.server, boost::asio::buffer(reply_bytes)); + + ASSERT_EQ(boost::future_status::ready, sock.wait_for(boost::chrono::seconds{3})); + EXPECT_TRUE(sock.get().is_open()); +} + +TEST(socks_connector, ipv4) +{ + io_thread io{}; + boost::asio::steady_timer timeout{io.io_service}; + timeout.expires_from_now(std::chrono::seconds{5}); + + boost::unique_future<boost::asio::ip::tcp::socket> sock = + net::socks::connector{io.acceptor.local_endpoint()}("250.88.125.99", "8080", timeout); + + while (!io.connected); + const std::uint8_t expected_bytes[] = { + 4, 1, 0x1f, 0x90, 0xfa, 0x58, 0x7d, 0x63, 0x00 + }; + + std::uint8_t actual_bytes[sizeof(expected_bytes)]; + boost::asio::read(io.server, boost::asio::buffer(actual_bytes)); + EXPECT_TRUE(std::memcmp(expected_bytes, actual_bytes, sizeof(actual_bytes)) == 0); + + const std::uint8_t reply_bytes[] = {0, 90, 0, 0, 0, 0, 0, 0}; + boost::asio::write(io.server, boost::asio::buffer(reply_bytes)); + + ASSERT_EQ(boost::future_status::ready, sock.wait_for(boost::chrono::seconds{3})); + EXPECT_TRUE(sock.get().is_open()); +} + +TEST(socks_connector, error) +{ + io_thread io{}; + boost::asio::steady_timer timeout{io.io_service}; + timeout.expires_from_now(std::chrono::seconds{5}); + + boost::unique_future<boost::asio::ip::tcp::socket> sock = + net::socks::connector{io.acceptor.local_endpoint()}("250.88.125.99", "8080", timeout); + + while (!io.connected); + const std::uint8_t expected_bytes[] = { + 4, 1, 0x1f, 0x90, 0xfa, 0x58, 0x7d, 0x63, 0x00 + }; + + std::uint8_t actual_bytes[sizeof(expected_bytes)]; + boost::asio::read(io.server, boost::asio::buffer(actual_bytes)); + EXPECT_TRUE(std::memcmp(expected_bytes, actual_bytes, sizeof(actual_bytes)) == 0); + + const std::uint8_t reply_bytes[] = {0, 91, 0, 0, 0, 0, 0, 0}; + boost::asio::write(io.server, boost::asio::buffer(reply_bytes)); + + ASSERT_EQ(boost::future_status::ready, sock.wait_for(boost::chrono::seconds{3})); + EXPECT_THROW(sock.get().is_open(), boost::system::system_error); +} + +TEST(socks_connector, timeout) +{ + io_thread io{}; + boost::asio::steady_timer timeout{io.io_service}; + timeout.expires_from_now(std::chrono::milliseconds{10}); + + boost::unique_future<boost::asio::ip::tcp::socket> sock = + net::socks::connector{io.acceptor.local_endpoint()}("250.88.125.99", "8080", timeout); + + ASSERT_EQ(boost::future_status::ready, sock.wait_for(boost::chrono::seconds{3})); + EXPECT_THROW(sock.get().is_open(), boost::system::system_error); +} diff --git a/tests/unit_tests/serialization.cpp b/tests/unit_tests/serialization.cpp index eb70caefc..27b14ffff 100644 --- a/tests/unit_tests/serialization.cpp +++ b/tests/unit_tests/serialization.cpp @@ -1187,3 +1187,26 @@ TEST(Serialization, portability_signed_tx) ASSERT_TRUE(epee::string_tools::pod_to_hex(ki1) == "d54cbd435a8d636ad9b01b8d4f3eb13bd0cf1ce98eddf53ab1617f9b763e66c0"); ASSERT_TRUE(epee::string_tools::pod_to_hex(ki2) == "6c3cd6af97c4070a7aef9b1344e7463e29c7cd245076fdb65da447a34da3ca76"); } + +TEST(Serialization, difficulty_type) +{ + std::vector<cryptonote::difficulty_type> v_original; + + for(int i = 0; i != 100; i++) + { + v_original.push_back(cryptonote::difficulty_type("117868131154734361989189100")); + if(v_original.size() > 1) + v_original.back() *= v_original[v_original.size()-2]; + } + + std::stringstream ss; + boost::archive::portable_binary_oarchive a(ss); + a << v_original; + + std::vector<cryptonote::difficulty_type> v_unserialized; + + boost::archive::portable_binary_iarchive a2(ss); + a2 >> v_unserialized; + + ASSERT_EQ(v_original, v_unserialized); +} |