Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
testratelimiter.cpp
Go to the documentation of this file.
1 #include "commonincludes.hpp"
2 #include "unittest.h"
3 #include "ratelimiter.h"
4 #include "testratelimiter.h"
5 
6 
8 {
9 public:
10  time_t _time;
11 
12  RateLimiterMockTime(size_t tablesize) : RateLimiter(tablesize, false), _time(0)
13  {
14  }
15 
16  void set_time(time_t t)
17  {
18  _time = t;
19  }
20 
21  virtual time_t get_time()
22  {
23  return _time;
24  }
25 
26 };
27 
29 {
30  HRESULT hr = S_OK;
31 
32  hr = Test1();
33  if (SUCCEEDED(hr))
34  {
35  hr = Test2();
36  }
37  return hr;
38 }
39 
40 
41 
43 {
44  // simulate 60 packets within one minute
45  CSocketAddress sockaddr(0x12341234,1234);
46  CSocketAddress sockaddr_goodguy(0x67896789,6789);
47  RateLimiterMockTime ratelimiter(20000);
48  HRESULT hr = S_OK;
49  bool result = false;
50 
51  for (int x = 0; x < 59; x++)
52  {
53  ratelimiter.set_time(x / 2);
54  result = ratelimiter.RateCheck(sockaddr);
55  ChkIf(result == false, E_FAIL);
56 
57  if ((x % 4) == 0)
58  {
59  result = ratelimiter.RateCheck(sockaddr_goodguy);
60  ChkIf(result == false, E_FAIL);
61  }
62  }
63 
64  // 60th packet should fail for bad guy
65  result = ratelimiter.RateCheck(sockaddr);
66  ChkIf(result, E_FAIL);
67 
68 
69  // should not impact good guy
70  result = ratelimiter.RateCheck(sockaddr_goodguy);
71  ChkIf(result == false, E_FAIL);
72 
73  // at the one hour mark, he should still be in the penalty box
75  result = ratelimiter.RateCheck(sockaddr);
76  ChkIf(result, E_FAIL);
77 
78  // but at the 30 second mark he should be out
80  result = ratelimiter.RateCheck(sockaddr);
81  ChkIf(result==false, E_FAIL);
82 
83 Cleanup:
84  return hr;
85 }
86 
88 {
89  // simulate 20000 ip addresses getting in
90  RateLimiterMockTime ratelimiter(20000);
91  CSocketAddress badguy_addr(10000, 9999);
92  CSocketAddress goodguy_addr(40000, 9999);
93  bool result;
94  HRESULT hr = S_OK;
95  uint32_t ip = 0;
96 
97  for (int x = 0; x < 20000; x++)
98  {
99  ip = (uint32_t)x;
100  CSocketAddress addr(ip, 9999);
101  result = ratelimiter.RateCheck(addr);
102  ChkIf(result == false, E_FAIL);
103  }
104 
105  // force an entry in the table to be in the penalty boxy
106  for (int x = 0; x < 60; x++)
107  {
108  ratelimiter.RateCheck(badguy_addr);
109  }
110  result = ratelimiter.RateCheck(badguy_addr);
111  ChkIf(result, E_FAIL);
112 
113  // force another entry that should flush the table
114  result = ratelimiter.RateCheck(goodguy_addr);
115  ChkIf(result==false, E_FAIL);
116 
117  // bad guy will no longer be in table
118  result = ratelimiter.RateCheck(badguy_addr);
119  ChkIf(result==false, E_FAIL);
120 
121 Cleanup:
122  return hr;
123 }
124 
#define S_OK
Definition: hresult.h:46
#define ChkIf(expr, hrerror)
Definition: chkmacros.h:63
#define SUCCEEDED(hr)
Definition: hresult.h:28
int32_t HRESULT
Definition: hresult.h:22
virtual time_t get_time()
static const time_t PENALTY_TIME_SECONDS
Definition: ratelimiter.h:61
#define E_FAIL
Definition: hresult.h:56
RateLimiterMockTime(size_t tablesize)
void set_time(time_t t)
bool RateCheck(const CSocketAddress &addr)
Definition: ratelimiter.cpp:44