aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit_tests')
-rw-r--r--tests/unit_tests/CMakeLists.txt1
-rw-r--r--tests/unit_tests/ban.cpp10
-rw-r--r--tests/unit_tests/epee_levin_protocol_handler_async.cpp1
-rw-r--r--tests/unit_tests/epee_utils.cpp32
-rw-r--r--tests/unit_tests/ringct.cpp10
-rw-r--r--tests/unit_tests/threadpool.cpp146
6 files changed, 195 insertions, 5 deletions
diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt
index 8cc074bb2..6d79ba74b 100644
--- a/tests/unit_tests/CMakeLists.txt
+++ b/tests/unit_tests/CMakeLists.txt
@@ -62,6 +62,7 @@ set(unit_tests_sources
test_tx_utils.cpp
test_peerlist.cpp
test_protocol_pack.cpp
+ threadpool.cpp
hardfork.cpp
unbound.cpp
uri.cpp
diff --git a/tests/unit_tests/ban.cpp b/tests/unit_tests/ban.cpp
index 15bc0bce3..e3dbdaef1 100644
--- a/tests/unit_tests/ban.cpp
+++ b/tests/unit_tests/ban.cpp
@@ -55,7 +55,7 @@ public:
bool have_block(const crypto::hash& id) const {return true;}
void get_blockchain_top(uint64_t& height, crypto::hash& top_id)const{height=0;top_id=crypto::null_hash;}
bool handle_incoming_tx(const cryptonote::blobdata& tx_blob, cryptonote::tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay) { return true; }
- bool handle_incoming_txs(const std::list<cryptonote::blobdata>& tx_blob, std::vector<cryptonote::tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay) { return true; }
+ bool handle_incoming_txs(const std::vector<cryptonote::blobdata>& tx_blob, std::vector<cryptonote::tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay) { return true; }
bool handle_incoming_block(const cryptonote::blobdata& block_blob, cryptonote::block_verification_context& bvc, bool update_miner_blocktemplate = true) { return true; }
void pause_mine(){}
void resume_mine(){}
@@ -65,7 +65,7 @@ public:
cryptonote::blockchain_storage &get_blockchain_storage() { throw std::runtime_error("Called invalid member function: please never call get_blockchain_storage on the TESTING class test_core."); }
bool get_test_drop_download() const {return true;}
bool get_test_drop_download_height() const {return true;}
- bool prepare_handle_incoming_blocks(const std::list<cryptonote::block_complete_entry> &blocks) { return true; }
+ bool prepare_handle_incoming_blocks(const std::vector<cryptonote::block_complete_entry> &blocks) { return true; }
bool cleanup_handle_incoming_blocks(bool force_sync = false) { return true; }
uint64_t get_target_blockchain_height() const { return 1; }
size_t get_block_sync_size(uint64_t height) const { return BLOCKS_SYNCHRONIZING_DEFAULT_COUNT; }
@@ -73,8 +73,8 @@ public:
cryptonote::network_type get_nettype() const { return cryptonote::MAINNET; }
bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx_blob) const { return false; }
bool pool_has_tx(const crypto::hash &txid) const { return false; }
- bool get_blocks(uint64_t start_offset, size_t count, std::list<std::pair<cryptonote::blobdata, cryptonote::block>>& blocks, std::list<cryptonote::blobdata>& txs) const { return false; }
- bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::list<cryptonote::transaction>& txs, std::list<crypto::hash>& missed_txs) const { return false; }
+ bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata, cryptonote::block>>& blocks, std::vector<cryptonote::blobdata>& txs) const { return false; }
+ bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<cryptonote::transaction>& txs, std::vector<crypto::hash>& missed_txs) const { return false; }
bool get_block_by_hash(const crypto::hash &h, cryptonote::block &blk, bool *orphan = NULL) const { return false; }
uint8_t get_ideal_hard_fork_version() const { return 0; }
uint8_t get_ideal_hard_fork_version(uint64_t height) const { return 0; }
@@ -82,7 +82,7 @@ public:
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const { return 0; }
cryptonote::difficulty_type get_block_cumulative_difficulty(uint64_t height) const { return 0; }
bool fluffy_blocks_enabled() const { return false; }
- uint64_t prevalidate_block_hashes(uint64_t height, const std::list<crypto::hash> &hashes) { return 0; }
+ uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes) { return 0; }
void stop() {}
};
diff --git a/tests/unit_tests/epee_levin_protocol_handler_async.cpp b/tests/unit_tests/epee_levin_protocol_handler_async.cpp
index 38a8360d7..72d8f3205 100644
--- a/tests/unit_tests/epee_levin_protocol_handler_async.cpp
+++ b/tests/unit_tests/epee_levin_protocol_handler_async.cpp
@@ -150,6 +150,7 @@ namespace
}
virtual bool close() { /*std::cout << "test_connection::close()" << std::endl; */return true; }
+ virtual bool send_done() { /*std::cout << "test_connection::send_done()" << std::endl; */return true; }
virtual bool call_run_once_service_io() { std::cout << "test_connection::call_run_once_service_io()" << std::endl; return true; }
virtual bool request_callback() { std::cout << "test_connection::request_callback()" << std::endl; return true; }
virtual boost::asio::io_service& get_io_service() { std::cout << "test_connection::get_io_service()" << std::endl; return m_io_service; }
diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp
index 3969f50f6..3474000d8 100644
--- a/tests/unit_tests/epee_utils.cpp
+++ b/tests/unit_tests/epee_utils.cpp
@@ -45,6 +45,7 @@
#include "boost/archive/portable_binary_oarchive.hpp"
#include "hex.h"
#include "net/net_utils_base.h"
+#include "net/local_ip.h"
#include "p2p/net_peerlist_boost_serialization.h"
#include "span.h"
#include "string_tools.h"
@@ -648,3 +649,34 @@ TEST(NetUtils, NetworkAddress)
EXPECT_THROW(address1.as<epee::net_utils::ipv4_network_address>(), std::bad_cast);
EXPECT_NO_THROW(address1.as<custom_address>());
}
+
+static bool is_local(const char *s)
+{
+ uint32_t ip;
+ CHECK_AND_ASSERT_THROW_MES(epee::string_tools::get_ip_int32_from_string(ip, s), std::string("Invalid IP address: ") + s);
+ return epee::net_utils::is_ip_local(ip);
+}
+
+TEST(NetUtils, PrivateRanges)
+{
+ ASSERT_EQ(is_local("10.0.0.0"), true);
+ ASSERT_EQ(is_local("10.255.0.0"), true);
+ ASSERT_EQ(is_local("127.0.0.0"), false); // loopback is not considered local
+ ASSERT_EQ(is_local("192.167.255.255"), false);
+ ASSERT_EQ(is_local("192.168.0.0"), true);
+ ASSERT_EQ(is_local("192.168.255.255"), true);
+ ASSERT_EQ(is_local("192.169.0.0"), false);
+ ASSERT_EQ(is_local("172.0.0.0"), false);
+ ASSERT_EQ(is_local("172.15.255.255"), false);
+ ASSERT_EQ(is_local("172.16.0.0"), true);
+ ASSERT_EQ(is_local("172.16.255.255"), true);
+ ASSERT_EQ(is_local("172.31.255.255"), true);
+ ASSERT_EQ(is_local("172.32.0.0"), false);
+ ASSERT_EQ(is_local("0.0.0.0"), false);
+ ASSERT_EQ(is_local("255.255.255.254"), false);
+ ASSERT_EQ(is_local("11.255.255.255"), false);
+ ASSERT_EQ(is_local("0.0.0.10"), false);
+ ASSERT_EQ(is_local("0.0.168.192"), false);
+ ASSERT_EQ(is_local("0.0.30.172"), false);
+ ASSERT_EQ(is_local("0.0.30.127"), false);
+}
diff --git a/tests/unit_tests/ringct.cpp b/tests/unit_tests/ringct.cpp
index b7fcbbcab..0f4bd3edf 100644
--- a/tests/unit_tests/ringct.cpp
+++ b/tests/unit_tests/ringct.cpp
@@ -1061,3 +1061,13 @@ TEST(ringct, key_ostream)
out.str()
);
}
+
+TEST(ringct, zeroCommmit)
+{
+ static const uint64_t amount = crypto::rand<uint64_t>();
+ const rct::key z = rct::zeroCommit(amount);
+ const rct::key a = rct::scalarmultBase(rct::identity());
+ const rct::key b = rct::scalarmultH(rct::d2h(amount));
+ const rct::key manual = rct::addKeys(a, b);
+ ASSERT_EQ(z, manual);
+}
diff --git a/tests/unit_tests/threadpool.cpp b/tests/unit_tests/threadpool.cpp
new file mode 100644
index 000000000..1307cd738
--- /dev/null
+++ b/tests/unit_tests/threadpool.cpp
@@ -0,0 +1,146 @@
+// Copyright (c) 2018, 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 <atomic>
+#include "gtest/gtest.h"
+#include "misc_language.h"
+#include "common/threadpool.h"
+
+TEST(threadpool, wait_nothing)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
+ tools::threadpool::waiter waiter;
+ waiter.wait(tpool.get());
+}
+
+TEST(threadpool, wait_waits)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
+ tools::threadpool::waiter waiter;
+ std::atomic<bool> b(false);
+ tpool->submit(&waiter, [&b](){ epee::misc_utils::sleep_no_w(1000); b = true; });
+ ASSERT_FALSE(b);
+ waiter.wait(tpool.get());
+ ASSERT_TRUE(b);
+}
+
+TEST(threadpool, one_thread)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(1));
+ tools::threadpool::waiter waiter;
+
+ std::atomic<unsigned int> counter(0);
+ for (size_t n = 0; n < 4096; ++n)
+ {
+ tpool->submit(&waiter, [&counter](){++counter;});
+ }
+ waiter.wait(tpool.get());
+ ASSERT_EQ(counter, 4096);
+}
+
+TEST(threadpool, many_threads)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(256));
+ tools::threadpool::waiter waiter;
+
+ std::atomic<unsigned int> counter(0);
+ for (size_t n = 0; n < 4096; ++n)
+ {
+ tpool->submit(&waiter, [&counter](){++counter;});
+ }
+ waiter.wait(tpool.get());
+ ASSERT_EQ(counter, 4096);
+}
+
+static uint64_t fibonacci(std::shared_ptr<tools::threadpool> tpool, uint64_t n)
+{
+ if (n <= 1)
+ return n;
+ uint64_t f1, f2;
+ tools::threadpool::waiter waiter;
+ tpool->submit(&waiter, [&tpool, &f1, n](){ f1 = fibonacci(tpool, n-1); });
+ tpool->submit(&waiter, [&tpool, &f2, n](){ f2 = fibonacci(tpool, n-2); });
+ waiter.wait(tpool.get());
+ return f1 + f2;
+}
+
+TEST(threadpool, reentrency)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
+ tools::threadpool::waiter waiter;
+
+ uint64_t f = fibonacci(tpool, 13);
+ waiter.wait(tpool.get());
+ ASSERT_EQ(f, 233);
+}
+
+TEST(threadpool, reentrancy)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
+ tools::threadpool::waiter waiter;
+
+ uint64_t f = fibonacci(tpool, 13);
+ waiter.wait(tpool.get());
+ ASSERT_EQ(f, 233);
+}
+
+TEST(threadpool, leaf_throws)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
+ tools::threadpool::waiter waiter;
+
+ bool thrown = false, executed = false;
+ tpool->submit(&waiter, [&](){
+ try { tpool->submit(&waiter, [&](){ executed = true; }); }
+ catch(const std::exception &e) { thrown = true; }
+ }, true);
+ waiter.wait(tpool.get());
+ ASSERT_TRUE(thrown);
+ ASSERT_FALSE(executed);
+}
+
+TEST(threadpool, leaf_reentrancy)
+{
+ std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
+ tools::threadpool::waiter waiter;
+
+ std::atomic<int> counter(0);
+ for (int i = 0; i < 1000; ++i)
+ {
+ tpool->submit(&waiter, [&](){
+ tools::threadpool::waiter waiter;
+ for (int j = 0; j < 500; ++j)
+ {
+ tpool->submit(&waiter, [&](){ ++counter; }, true);
+ }
+ waiter.wait(tpool.get());
+ });
+ }
+ waiter.wait(tpool.get());
+ ASSERT_EQ(counter, 500000);
+}