2020-05-07 05:36:54 +03:00
// Copyright (c) 2014-2020, The Monero Project
2015-12-24 18:05:19 +02:00
//
// 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.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
# include "gtest/gtest.h"
# include "cryptonote_core/cryptonote_core.h"
# include "p2p/net_node.h"
2017-11-30 00:53:58 +02:00
# include "p2p/net_node.inl"
2019-11-02 22:36:03 +02:00
# include "cryptonote_core/i_core_events.h"
2015-12-24 18:05:19 +02:00
# include "cryptonote_protocol/cryptonote_protocol_handler.h"
2017-11-30 00:53:58 +02:00
# include "cryptonote_protocol/cryptonote_protocol_handler.inl"
2015-12-24 18:05:19 +02:00
2017-08-25 18:14:46 +03:00
# define MAKE_IPV4_ADDRESS(a,b,c,d) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),0}
2019-09-16 22:20:23 +03:00
# define MAKE_IPV4_ADDRESS_PORT(a,b,c,d,e) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),e}
2019-03-29 12:47:53 +02:00
# define MAKE_IPV4_SUBNET(a,b,c,d,e) epee::net_utils::ipv4_network_subnet{MAKE_IP(a,b,c,d),e}
2017-06-19 13:26:02 +03:00
2015-12-24 18:05:19 +02:00
namespace cryptonote {
class blockchain_storage ;
}
2019-11-02 22:36:03 +02:00
class test_core : public cryptonote : : i_core_events
2015-12-24 18:05:19 +02:00
{
public :
2020-11-20 13:37:19 +02:00
virtual bool is_synchronized ( ) const final { return true ; }
2015-12-24 18:05:19 +02:00
void on_synchronized ( ) { }
2017-08-22 16:54:18 +03:00
void safesyncmode ( const bool ) { }
2020-11-20 13:37:19 +02:00
virtual uint64_t get_current_blockchain_height ( ) const final { return 1 ; }
2015-12-24 18:05:19 +02:00
void set_target_blockchain_height ( uint64_t ) { }
bool init ( const boost : : program_options : : variables_map & vm ) { return true ; }
bool deinit ( ) { return true ; }
bool get_short_chain_history ( std : : list < crypto : : hash > & ids ) const { return true ; }
2020-12-31 23:12:30 +02:00
bool have_block ( const crypto : : hash & id , int * where = NULL ) const { return false ; }
bool have_block_unlocked ( const crypto : : hash & id , int * where = NULL ) const { return false ; }
2017-09-10 19:35:59 +03:00
void get_blockchain_top ( uint64_t & height , crypto : : hash & top_id ) const { height = 0 ; top_id = crypto : : null_hash ; }
2019-11-02 22:36:03 +02:00
bool handle_incoming_tx ( const cryptonote : : tx_blob_entry & tx_blob , cryptonote : : tx_verification_context & tvc , cryptonote : : relay_method tx_relay , bool relayed ) { return true ; }
bool handle_incoming_txs ( const std : : vector < cryptonote : : tx_blob_entry > & tx_blob , std : : vector < cryptonote : : tx_verification_context > & tvc , cryptonote : : relay_method tx_relay , bool relayed ) { return true ; }
2018-11-23 15:05:48 +02:00
bool handle_incoming_block ( const cryptonote : : blobdata & block_blob , const cryptonote : : block * block , cryptonote : : block_verification_context & bvc , bool update_miner_blocktemplate = true ) { return true ; }
2015-12-24 18:05:19 +02:00
void pause_mine ( ) { }
void resume_mine ( ) { }
bool on_idle ( ) { return true ; }
2019-10-15 16:15:19 +03:00
bool find_blockchain_supplement ( const std : : list < crypto : : hash > & qblock_ids , bool clip_pruned , cryptonote : : NOTIFY_RESPONSE_CHAIN_ENTRY : : request & resp ) { return true ; }
2015-12-24 18:05:19 +02:00
bool handle_get_objects ( cryptonote : : NOTIFY_REQUEST_GET_OBJECTS : : request & arg , cryptonote : : NOTIFY_RESPONSE_GET_OBJECTS : : request & rsp , cryptonote : : cryptonote_connection_context & context ) { return true ; }
2016-03-21 12:12:12 +02:00
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. " ) ; }
2015-12-24 18:05:19 +02:00
bool get_test_drop_download ( ) const { return true ; }
bool get_test_drop_download_height ( ) const { return true ; }
2018-11-23 15:05:48 +02:00
bool prepare_handle_incoming_blocks ( const std : : vector < cryptonote : : block_complete_entry > & blocks_entry , std : : vector < cryptonote : : block > & blocks ) { return true ; }
2015-12-24 18:05:19 +02:00
bool cleanup_handle_incoming_blocks ( bool force_sync = false ) { return true ; }
uint64_t get_target_blockchain_height ( ) const { return 1 ; }
2017-08-16 23:19:53 +03:00
size_t get_block_sync_size ( uint64_t height ) const { return BLOCKS_SYNCHRONIZING_DEFAULT_COUNT ; }
2019-11-02 22:36:03 +02:00
virtual void on_transactions_relayed ( epee : : span < const cryptonote : : blobdata > tx_blobs , cryptonote : : relay_method tx_relay ) { }
2018-02-16 13:04:04 +02:00
cryptonote : : network_type get_nettype ( ) const { return cryptonote : : MAINNET ; }
2019-11-02 22:36:03 +02:00
bool get_pool_transaction ( const crypto : : hash & id , cryptonote : : blobdata & tx_blob , cryptonote : : relay_category tx_category ) const { return false ; }
2017-05-14 16:06:55 +03:00
bool pool_has_tx ( const crypto : : hash & txid ) const { return false ; }
2018-04-16 02:16:02 +03:00
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 ; }
2017-02-11 21:38:18 +02:00
bool get_block_by_hash ( const crypto : : hash & h , cryptonote : : block & blk , bool * orphan = NULL ) const { return false ; }
2018-04-16 05:02:26 +03:00
uint8_t get_ideal_hard_fork_version ( ) const { return 0 ; }
2017-08-10 12:10:04 +03:00
uint8_t get_ideal_hard_fork_version ( uint64_t height ) const { return 0 ; }
uint8_t get_hard_fork_version ( uint64_t height ) const { return 0 ; }
2018-03-20 13:30:09 +02:00
uint64_t get_earliest_ideal_height_for_version ( uint8_t version ) const { return 0 ; }
2017-08-12 17:24:39 +03:00
cryptonote : : difficulty_type get_block_cumulative_difficulty ( uint64_t height ) const { return 0 ; }
2017-09-02 19:11:20 +03:00
bool fluffy_blocks_enabled ( ) const { return false ; }
2019-10-15 16:15:19 +03:00
uint64_t prevalidate_block_hashes ( uint64_t height , const std : : vector < crypto : : hash > & hashes , const std : : vector < uint64_t > & weights ) { return 0 ; }
2018-11-03 00:27:25 +02:00
bool pad_transactions ( ) { return false ; }
Pruning
The blockchain prunes seven eighths of prunable tx data.
This saves about two thirds of the blockchain size, while
keeping the node useful as a sync source for an eighth
of the blockchain.
No other data is currently pruned.
There are three ways to prune a blockchain:
- run monerod with --prune-blockchain
- run "prune_blockchain" in the monerod console
- run the monero-blockchain-prune utility
The first two will prune in place. Due to how LMDB works, this
will not reduce the blockchain size on disk. Instead, it will
mark parts of the file as free, so that future data will use
that free space, causing the file to not grow until free space
grows scarce.
The third way will create a second database, a pruned copy of
the original one. Since this is a new file, this one will be
smaller than the original one.
Once the database is pruned, it will stay pruned as it syncs.
That is, there is no need to use --prune-blockchain again, etc.
2018-04-30 01:30:51 +03:00
uint32_t get_blockchain_pruning_seed ( ) const { return 0 ; }
bool prune_blockchain ( uint32_t pruning_seed = 0 ) { return true ; }
2019-10-15 16:15:19 +03:00
bool is_within_compiled_block_hash_area ( uint64_t height ) const { return false ; }
bool has_block_weights ( uint64_t height , uint64_t nblocks ) const { return false ; }
2019-11-27 02:09:25 +02:00
bool get_txpool_complement ( const std : : vector < crypto : : hash > & hashes , std : : vector < cryptonote : : blobdata > & txes ) { return false ; }
bool get_pool_transaction_hashes ( std : : vector < crypto : : hash > & txs , bool include_unrelayed_txes = true ) const { return false ; }
2020-12-27 16:13:26 +02:00
crypto : : hash get_block_id_by_height ( uint64_t height ) const { return crypto : : null_hash ; }
2017-11-30 00:53:58 +02:00
void stop ( ) { }
2015-12-24 18:05:19 +02:00
} ;
typedef nodetool : : node_server < cryptonote : : t_cryptonote_protocol_handler < test_core > > Server ;
2017-06-19 13:26:02 +03:00
static bool is_blocked ( Server & server , const epee : : net_utils : : network_address & address , time_t * t = NULL )
2015-12-24 18:05:19 +02:00
{
2019-09-16 22:20:23 +03:00
std : : map < std : : string , time_t > hosts = server . get_blocked_hosts ( ) ;
2019-04-17 01:23:15 +03:00
for ( auto rec : hosts )
{
2019-09-16 22:20:23 +03:00
if ( rec . first = = address . host_str ( ) )
2019-04-17 01:23:15 +03:00
{
if ( t )
* t = rec . second ;
return true ;
}
}
return false ;
2015-12-24 18:05:19 +02:00
}
TEST ( ban , add )
{
test_core pr_core ;
cryptonote : : t_cryptonote_protocol_handler < test_core > cprotocol ( pr_core , NULL ) ;
Server server ( cprotocol ) ;
cprotocol . set_p2p_endpoint ( & server ) ;
// starts empty
2017-06-19 13:26:02 +03:00
ASSERT_TRUE ( server . get_blocked_hosts ( ) . empty ( ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
// add an IP
2017-06-19 13:26:02 +03:00
ASSERT_TRUE ( server . block_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 1 ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
// add the same, should not change
2017-06-19 13:26:02 +03:00
ASSERT_TRUE ( server . block_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 1 ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
// remove an unblocked IP, should not change
2017-06-19 13:26:02 +03:00
ASSERT_FALSE ( server . unblock_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 1 ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
// remove the IP, ends up empty
2017-06-19 13:26:02 +03:00
ASSERT_TRUE ( server . unblock_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 0 ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
// remove the IP from an empty list, still empty
2017-06-19 13:26:02 +03:00
ASSERT_FALSE ( server . unblock_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 0 ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
// add two for known amounts of time, they're both blocked
2017-06-19 13:26:02 +03:00
ASSERT_TRUE ( server . block_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) , 1 ) ) ;
ASSERT_TRUE ( server . block_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) , 3 ) ) ;
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 2 ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
ASSERT_TRUE ( server . unblock_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_TRUE ( server . unblock_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
// these tests would need to call is_remote_ip_allowed, which is private
#if 0
// after two seconds, the first IP is unblocked, but not the second yet
sleep ( 2 ) ;
2017-06-19 13:26:02 +03:00
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 1 ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
// after two more seconds, the second IP is also unblocked
sleep ( 2 ) ;
2017-06-19 13:26:02 +03:00
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 0 ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
# endif
// add an IP again, then re-ban for longer, then shorter
time_t t ;
2017-06-19 13:26:02 +03:00
ASSERT_TRUE ( server . block_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) , 2 ) ) ;
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 1 ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) , & t ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
ASSERT_TRUE ( t > = 1 ) ;
2017-06-19 13:26:02 +03:00
ASSERT_TRUE ( server . block_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) , 9 ) ) ;
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 1 ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) , & t ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
ASSERT_TRUE ( t > = 8 ) ;
2017-06-19 13:26:02 +03:00
ASSERT_TRUE ( server . block_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) , 5 ) ) ;
ASSERT_TRUE ( server . get_blocked_hosts ( ) . size ( ) = = 1 ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) , & t ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 5 ) ) ) ;
2015-12-24 18:05:19 +02:00
ASSERT_TRUE ( t > = 4 ) ;
}
2019-04-12 00:57:51 +03:00
TEST ( ban , limit )
{
test_core pr_core ;
cryptonote : : t_cryptonote_protocol_handler < test_core > cprotocol ( pr_core , NULL ) ;
Server server ( cprotocol ) ;
cprotocol . set_p2p_endpoint ( & server ) ;
// starts empty
ASSERT_TRUE ( server . get_blocked_hosts ( ) . empty ( ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_TRUE ( server . block_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) , std : : numeric_limits < time_t > : : max ( ) - 1 ) ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
ASSERT_TRUE ( server . block_host ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) , 1 ) ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) ) ) ;
}
2019-03-29 12:47:53 +02:00
TEST ( ban , subnet )
{
time_t seconds ;
test_core pr_core ;
cryptonote : : t_cryptonote_protocol_handler < test_core > cprotocol ( pr_core , NULL ) ;
Server server ( cprotocol ) ;
cprotocol . set_p2p_endpoint ( & server ) ;
ASSERT_TRUE ( server . block_subnet ( MAKE_IPV4_SUBNET ( 1 , 2 , 3 , 4 , 24 ) , 10 ) ) ;
ASSERT_TRUE ( server . get_blocked_subnets ( ) . size ( ) = = 1 ) ;
ASSERT_TRUE ( server . is_host_blocked ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 4 ) , & seconds ) ) ;
ASSERT_TRUE ( seconds > = 9 ) ;
ASSERT_TRUE ( server . is_host_blocked ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 255 ) , & seconds ) ) ;
ASSERT_TRUE ( server . is_host_blocked ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 0 ) , & seconds ) ) ;
ASSERT_FALSE ( server . is_host_blocked ( MAKE_IPV4_ADDRESS ( 1 , 2 , 4 , 0 ) , & seconds ) ) ;
ASSERT_FALSE ( server . is_host_blocked ( MAKE_IPV4_ADDRESS ( 1 , 2 , 2 , 0 ) , & seconds ) ) ;
ASSERT_TRUE ( server . unblock_subnet ( MAKE_IPV4_SUBNET ( 1 , 2 , 3 , 8 , 24 ) ) ) ;
ASSERT_TRUE ( server . get_blocked_subnets ( ) . size ( ) = = 0 ) ;
ASSERT_FALSE ( server . is_host_blocked ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 255 ) , & seconds ) ) ;
ASSERT_FALSE ( server . is_host_blocked ( MAKE_IPV4_ADDRESS ( 1 , 2 , 3 , 0 ) , & seconds ) ) ;
ASSERT_TRUE ( server . block_subnet ( MAKE_IPV4_SUBNET ( 1 , 2 , 3 , 4 , 8 ) , 10 ) ) ;
ASSERT_TRUE ( server . get_blocked_subnets ( ) . size ( ) = = 1 ) ;
ASSERT_TRUE ( server . is_host_blocked ( MAKE_IPV4_ADDRESS ( 1 , 255 , 3 , 255 ) , & seconds ) ) ;
ASSERT_TRUE ( server . is_host_blocked ( MAKE_IPV4_ADDRESS ( 1 , 0 , 3 , 255 ) , & seconds ) ) ;
ASSERT_FALSE ( server . unblock_subnet ( MAKE_IPV4_SUBNET ( 1 , 2 , 3 , 8 , 24 ) ) ) ;
ASSERT_TRUE ( server . get_blocked_subnets ( ) . size ( ) = = 1 ) ;
ASSERT_TRUE ( server . block_subnet ( MAKE_IPV4_SUBNET ( 1 , 2 , 3 , 4 , 8 ) , 10 ) ) ;
ASSERT_TRUE ( server . get_blocked_subnets ( ) . size ( ) = = 1 ) ;
ASSERT_TRUE ( server . unblock_subnet ( MAKE_IPV4_SUBNET ( 1 , 255 , 0 , 0 , 8 ) ) ) ;
ASSERT_TRUE ( server . get_blocked_subnets ( ) . size ( ) = = 0 ) ;
}
2019-09-16 22:20:23 +03:00
TEST ( ban , ignores_port )
{
time_t seconds ;
test_core pr_core ;
cryptonote : : t_cryptonote_protocol_handler < test_core > cprotocol ( pr_core , NULL ) ;
Server server ( cprotocol ) ;
cprotocol . set_p2p_endpoint ( & server ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS_PORT ( 1 , 2 , 3 , 4 , 5 ) ) ) ;
ASSERT_TRUE ( server . block_host ( MAKE_IPV4_ADDRESS_PORT ( 1 , 2 , 3 , 4 , 5 ) , std : : numeric_limits < time_t > : : max ( ) - 1 ) ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS_PORT ( 1 , 2 , 3 , 4 , 5 ) ) ) ;
ASSERT_TRUE ( is_blocked ( server , MAKE_IPV4_ADDRESS_PORT ( 1 , 2 , 3 , 4 , 6 ) ) ) ;
ASSERT_TRUE ( server . unblock_host ( MAKE_IPV4_ADDRESS_PORT ( 1 , 2 , 3 , 4 , 5 ) ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS_PORT ( 1 , 2 , 3 , 4 , 5 ) ) ) ;
ASSERT_FALSE ( is_blocked ( server , MAKE_IPV4_ADDRESS_PORT ( 1 , 2 , 3 , 4 , 6 ) ) ) ;
}
2019-10-13 16:27:46 +03:00
TEST ( node_server , bind_same_p2p_port )
{
2019-10-30 20:39:48 +02:00
struct test_data_t
{
2019-10-13 16:27:46 +03:00
test_core pr_core ;
2019-10-30 20:39:48 +02:00
cryptonote : : t_cryptonote_protocol_handler < test_core > cprotocol ;
std : : unique_ptr < Server > server ;
test_data_t ( ) : cprotocol ( pr_core , NULL )
{
server . reset ( new Server ( cprotocol ) ) ;
cprotocol . set_p2p_endpoint ( server . get ( ) ) ;
}
} ;
2019-10-13 16:27:46 +03:00
2019-10-30 20:39:48 +02:00
const auto new_node = [ ] ( ) - > std : : unique_ptr < test_data_t > {
test_data_t * d = new test_data_t ;
return std : : unique_ptr < test_data_t > ( d ) ;
2019-10-13 16:27:46 +03:00
} ;
2019-10-30 20:39:48 +02:00
const auto init = [ ] ( const std : : unique_ptr < test_data_t > & server , const char * port ) - > bool {
2019-10-13 16:27:46 +03:00
boost : : program_options : : options_description desc_options ( " Command line options " ) ;
cryptonote : : core : : init_options ( desc_options ) ;
Server : : init_options ( desc_options ) ;
const char * argv [ 2 ] = { nullptr , nullptr } ;
boost : : program_options : : variables_map vm ;
boost : : program_options : : store ( boost : : program_options : : parse_command_line ( 1 , argv , desc_options ) , vm ) ;
vm . find ( nodetool : : arg_p2p_bind_port . name ) - > second = boost : : program_options : : variable_value ( std : : string ( port ) , false ) ;
boost : : program_options : : notify ( vm ) ;
2019-10-30 20:39:48 +02:00
return server - > server - > init ( vm ) ;
2019-10-13 16:27:46 +03:00
} ;
constexpr char port [ ] = " 48080 " ;
constexpr char port_another [ ] = " 58080 " ;
const auto node = new_node ( ) ;
EXPECT_TRUE ( init ( node , port ) ) ;
EXPECT_FALSE ( init ( new_node ( ) , port ) ) ;
EXPECT_TRUE ( init ( new_node ( ) , port_another ) ) ;
}
2017-11-30 00:53:58 +02:00
namespace nodetool { template class node_server < cryptonote : : t_cryptonote_protocol_handler < test_core > > ; }
namespace cryptonote { template class t_cryptonote_protocol_handler < test_core > ; }