Electroneum
ban.cpp File Reference
#include "gtest/gtest.h"
#include "cryptonote_core/cryptonote_core.h"
#include "p2p/net_node.h"
#include "p2p/net_node.inl"
#include "cryptonote_protocol/cryptonote_protocol_handler.h"
#include "cryptonote_protocol/cryptonote_protocol_handler.inl"
Include dependency graph for ban.cpp:

Go to the source code of this file.

Classes

class  test_core
 

Namespaces

 cryptonote
 Holds cryptonote related classes and helpers.
 
 nodetool
 

Macros

#define MAKE_IPV4_ADDRESS(a, b, c, d)   epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),0}
 

Typedefs

typedef nodetool::node_server< cryptonote::t_cryptonote_protocol_handler< test_core > > Server
 

Functions

 TEST (ban, add)
 
 TEST (ban, limit)
 

Macro Definition Documentation

◆ MAKE_IPV4_ADDRESS

#define MAKE_IPV4_ADDRESS (   a,
  b,
  c,
 
)    epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),0}

Definition at line 38 of file ban.cpp.

Typedef Documentation

◆ Server

Function Documentation

◆ TEST() [1/2]

TEST ( ban  ,
add   
)

Definition at line 114 of file ban.cpp.

115 {
116  test_core pr_core;
118  Server server(cprotocol);
119  cprotocol.set_p2p_endpoint(&server);
120 
121  // starts empty
122  ASSERT_TRUE(server.get_blocked_hosts().empty());
123  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
124  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
125 
126  // add an IP
127  ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
128  ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
129  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
130  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
131 
132  // add the same, should not change
133  ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
134  ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
135  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
136  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
137 
138  // remove an unblocked IP, should not change
139  ASSERT_FALSE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,5)));
140  ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
141  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
142  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
143 
144  // remove the IP, ends up empty
145  ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
146  ASSERT_TRUE(server.get_blocked_hosts().size() == 0);
147  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
148  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
149 
150  // remove the IP from an empty list, still empty
151  ASSERT_FALSE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
152  ASSERT_TRUE(server.get_blocked_hosts().size() == 0);
153  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
154  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
155 
156  // add two for known amounts of time, they're both blocked
157  ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 1));
158  ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,5), 3));
159  ASSERT_TRUE(server.get_blocked_hosts().size() == 2);
160  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
161  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
162  ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
163  ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,5)));
164 
165  // these tests would need to call is_remote_ip_allowed, which is private
166 #if 0
167  // after two seconds, the first IP is unblocked, but not the second yet
168  sleep(2);
169  ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
170  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
171  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
172 
173  // after two more seconds, the second IP is also unblocked
174  sleep(2);
175  ASSERT_TRUE(server.get_blocked_hosts().size() == 0);
176  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
177  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
178 #endif
179 
180  // add an IP again, then re-ban for longer, then shorter
181  time_t t;
182  ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 2));
183  ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
184  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t));
185  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
186  ASSERT_TRUE(t >= 1);
187  ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 9));
188  ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
189  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t));
190  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
191  ASSERT_TRUE(t >= 8);
192  ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 5));
193  ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
194  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t));
195  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
196  ASSERT_TRUE(t >= 4);
197 }
#define MAKE_IPV4_ADDRESS(a, b, c, d)
Definition: ban.cpp:38
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [2/2]

TEST ( ban  ,
limit   
)

Definition at line 199 of file ban.cpp.

200 {
201  test_core pr_core;
203  Server server(cprotocol);
204  cprotocol.set_p2p_endpoint(&server);
205 
206  // starts empty
207  ASSERT_TRUE(server.get_blocked_hosts().empty());
208  ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
209  ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), std::numeric_limits<time_t>::max() - 1));
210  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
211  ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 1));
212  ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
213 }
#define MAKE_IPV4_ADDRESS(a, b, c, d)
Definition: ban.cpp:38
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function: