unit_tests: fix build failures after network_address changes

This commit is contained in:
moneromooo-monero 2017-06-19 11:26:02 +01:00
parent ae8841f2ab
commit f4e3dca113
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
2 changed files with 68 additions and 64 deletions

View file

@ -33,6 +33,8 @@
#include "p2p/net_node.h" #include "p2p/net_node.h"
#include "cryptonote_protocol/cryptonote_protocol_handler.h" #include "cryptonote_protocol/cryptonote_protocol_handler.h"
#define MAKE_IPV4_ADDRESS(a,b,c,d) new epee::net_utils::ipv4_network_address(MAKE_IP(a,b,c,d),0)
namespace cryptonote { namespace cryptonote {
class blockchain_storage; class blockchain_storage;
} }
@ -74,12 +76,13 @@ public:
typedef nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<test_core>> Server; typedef nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<test_core>> Server;
static bool is_blocked(Server &server, uint32_t ip, time_t *t = NULL) static bool is_blocked(Server &server, const epee::net_utils::network_address &address, time_t *t = NULL)
{ {
std::map<uint32_t, time_t> ips = server.get_blocked_ips(); const std::string host = address.host_str();
for (auto rec: ips) std::map<std::string, time_t> hosts = server.get_blocked_hosts();
for (auto rec: hosts)
{ {
if (rec.first == ip) if (rec.first == host)
{ {
if (t) if (t)
*t = rec.second; *t = rec.second;
@ -97,80 +100,80 @@ TEST(ban, add)
cprotocol.set_p2p_endpoint(&server); cprotocol.set_p2p_endpoint(&server);
// starts empty // starts empty
ASSERT_TRUE(server.get_blocked_ips().empty()); ASSERT_TRUE(server.get_blocked_hosts().empty());
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
// add an IP // add an IP
ASSERT_TRUE(server.block_ip(MAKE_IP(1,2,3,4))); ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_TRUE(server.get_blocked_ips().size() == 1); ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
ASSERT_TRUE(is_blocked(server,MAKE_IP(1,2,3,4))); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
// add the same, should not change // add the same, should not change
ASSERT_TRUE(server.block_ip(MAKE_IP(1,2,3,4))); ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_TRUE(server.get_blocked_ips().size() == 1); ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
ASSERT_TRUE(is_blocked(server,MAKE_IP(1,2,3,4))); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
// remove an unblocked IP, should not change // remove an unblocked IP, should not change
ASSERT_FALSE(server.unblock_ip(MAKE_IP(1,2,3,5))); ASSERT_FALSE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,5)));
ASSERT_TRUE(server.get_blocked_ips().size() == 1); ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
ASSERT_TRUE(is_blocked(server,MAKE_IP(1,2,3,4))); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
// remove the IP, ends up empty // remove the IP, ends up empty
ASSERT_TRUE(server.unblock_ip(MAKE_IP(1,2,3,4))); ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_TRUE(server.get_blocked_ips().size() == 0); ASSERT_TRUE(server.get_blocked_hosts().size() == 0);
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
// remove the IP from an empty list, still empty // remove the IP from an empty list, still empty
ASSERT_FALSE(server.unblock_ip(MAKE_IP(1,2,3,4))); ASSERT_FALSE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_TRUE(server.get_blocked_ips().size() == 0); ASSERT_TRUE(server.get_blocked_hosts().size() == 0);
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
// add two for known amounts of time, they're both blocked // add two for known amounts of time, they're both blocked
ASSERT_TRUE(server.block_ip(MAKE_IP(1,2,3,4), 1)); ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 1));
ASSERT_TRUE(server.block_ip(MAKE_IP(1,2,3,5), 3)); ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,5), 3));
ASSERT_TRUE(server.get_blocked_ips().size() == 2); ASSERT_TRUE(server.get_blocked_hosts().size() == 2);
ASSERT_TRUE(is_blocked(server,MAKE_IP(1,2,3,4))); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_TRUE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
ASSERT_TRUE(server.unblock_ip(MAKE_IP(1,2,3,4))); ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_TRUE(server.unblock_ip(MAKE_IP(1,2,3,5))); ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,5)));
// these tests would need to call is_remote_ip_allowed, which is private // these tests would need to call is_remote_ip_allowed, which is private
#if 0 #if 0
// after two seconds, the first IP is unblocked, but not the second yet // after two seconds, the first IP is unblocked, but not the second yet
sleep(2); sleep(2);
ASSERT_TRUE(server.get_blocked_ips().size() == 1); ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_TRUE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
// after two more seconds, the second IP is also unblocked // after two more seconds, the second IP is also unblocked
sleep(2); sleep(2);
ASSERT_TRUE(server.get_blocked_ips().size() == 0); ASSERT_TRUE(server.get_blocked_hosts().size() == 0);
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,4))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
#endif #endif
// add an IP again, then re-ban for longer, then shorter // add an IP again, then re-ban for longer, then shorter
time_t t; time_t t;
ASSERT_TRUE(server.block_ip(MAKE_IP(1,2,3,4), 2)); ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 2));
ASSERT_TRUE(server.get_blocked_ips().size() == 1); ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
ASSERT_TRUE(is_blocked(server,MAKE_IP(1,2,3,4), &t)); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t));
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
ASSERT_TRUE(t >= 1); ASSERT_TRUE(t >= 1);
ASSERT_TRUE(server.block_ip(MAKE_IP(1,2,3,4), 9)); ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 9));
ASSERT_TRUE(server.get_blocked_ips().size() == 1); ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
ASSERT_TRUE(is_blocked(server,MAKE_IP(1,2,3,4), &t)); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t));
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
ASSERT_TRUE(t >= 8); ASSERT_TRUE(t >= 8);
ASSERT_TRUE(server.block_ip(MAKE_IP(1,2,3,4), 5)); ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 5));
ASSERT_TRUE(server.get_blocked_ips().size() == 1); ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
ASSERT_TRUE(is_blocked(server,MAKE_IP(1,2,3,4), &t)); ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t));
ASSERT_FALSE(is_blocked(server,MAKE_IP(1,2,3,5))); ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
ASSERT_TRUE(t >= 4); ASSERT_TRUE(t >= 4);
} }

View file

@ -38,21 +38,22 @@ TEST(peer_list, peer_list_general)
{ {
nodetool::peerlist_manager plm; nodetool::peerlist_manager plm;
plm.init(false); plm.init(false);
#define ADD_GRAY_NODE(ip_, port_, id_, last_seen_) { nodetool::peerlist_entry ple; ple.last_seen=last_seen_;ple.adr.ip = ip_; ple.adr.port = port_; ple.id = id_;plm.append_with_peer_gray(ple);} #define MAKE_IPV4_ADDRESS(a,b,c,d,e) new epee::net_utils::ipv4_network_address(MAKE_IP(a,b,c,d),e)
#define ADD_WHITE_NODE(ip_, port_, id_, last_seen_) { nodetool::peerlist_entry ple;ple.last_seen=last_seen_; ple.adr.ip = ip_; ple.adr.port = port_; ple.id = id_;plm.append_with_peer_white(ple);} #define ADD_GRAY_NODE(addr_, id_, last_seen_) { nodetool::peerlist_entry ple; ple.last_seen=last_seen_;ple.adr = addr_; ple.id = id_;plm.append_with_peer_gray(ple);}
#define ADD_WHITE_NODE(addr_, id_, last_seen_) { nodetool::peerlist_entry ple;ple.last_seen=last_seen_; ple.adr = addr_; ple.id = id_;plm.append_with_peer_white(ple);}
#define PRINT_HEAD(step) {std::list<nodetool::peerlist_entry> bs_head; bool r = plm.get_peerlist_head(bs_head, 100);std::cout << "step " << step << ": " << bs_head.size() << std::endl;} #define PRINT_HEAD(step) {std::list<nodetool::peerlist_entry> bs_head; bool r = plm.get_peerlist_head(bs_head, 100);std::cout << "step " << step << ": " << bs_head.size() << std::endl;}
ADD_GRAY_NODE(MAKE_IP(123,43,12,1), 8080, 121241, 34345); ADD_GRAY_NODE(MAKE_IPV4_ADDRESS(123,43,12,1, 8080), 121241, 34345);
ADD_GRAY_NODE(MAKE_IP(123,43,12,2), 8080, 121241, 34345); ADD_GRAY_NODE(MAKE_IPV4_ADDRESS(123,43,12,2, 8080), 121241, 34345);
ADD_GRAY_NODE(MAKE_IP(123,43,12,3), 8080, 121241, 34345); ADD_GRAY_NODE(MAKE_IPV4_ADDRESS(123,43,12,3, 8080), 121241, 34345);
ADD_GRAY_NODE(MAKE_IP(123,43,12,4), 8080, 121241, 34345); ADD_GRAY_NODE(MAKE_IPV4_ADDRESS(123,43,12,4, 8080), 121241, 34345);
ADD_GRAY_NODE(MAKE_IP(123,43,12,5), 8080, 121241, 34345); ADD_GRAY_NODE(MAKE_IPV4_ADDRESS(123,43,12,5, 8080), 121241, 34345);
ADD_WHITE_NODE(MAKE_IP(123,43,12,1), 8080, 121241, 34345); ADD_WHITE_NODE(MAKE_IPV4_ADDRESS(123,43,12,1, 8080), 121241, 34345);
ADD_WHITE_NODE(MAKE_IP(123,43,12,2), 8080, 121241, 34345); ADD_WHITE_NODE(MAKE_IPV4_ADDRESS(123,43,12,2, 8080), 121241, 34345);
ADD_WHITE_NODE(MAKE_IP(123,43,12,3), 8080, 121241, 34345); ADD_WHITE_NODE(MAKE_IPV4_ADDRESS(123,43,12,3, 8080), 121241, 34345);
ADD_WHITE_NODE(MAKE_IP(123,43,12,4), 8080, 121241, 34345); ADD_WHITE_NODE(MAKE_IPV4_ADDRESS(123,43,12,4, 8080), 121241, 34345);
size_t gray_list_size = plm.get_gray_peers_count(); size_t gray_list_size = plm.get_gray_peers_count();
ASSERT_EQ(gray_list_size, 1); ASSERT_EQ(gray_list_size, 1);
@ -65,7 +66,7 @@ TEST(peer_list, peer_list_general)
ASSERT_EQ(bs_head.size(), 4); ASSERT_EQ(bs_head.size(), 4);
ADD_GRAY_NODE(MAKE_IP(123,43,12,5), 8080, 121241, 34345); ADD_GRAY_NODE(MAKE_IPV4_ADDRESS(123,43,12,5, 8080), 121241, 34345);
ASSERT_EQ(plm.get_gray_peers_count(), 1); ASSERT_EQ(plm.get_gray_peers_count(), 1);
ASSERT_EQ(plm.get_white_peers_count(), 4); ASSERT_EQ(plm.get_white_peers_count(), 4);
} }