Debian Codegraph @

tests/functional_tests/bans.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2019-2022, The Monero Project
4 #
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without modification, are
8 # permitted provided that the following conditions are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright notice, this list of
11 # conditions and the following disclaimer.
12 #
13 # 2. Redistributions in binary form must reproduce the above copyright notice, this list
14 # of conditions and the following disclaimer in the documentation and/or other
15 # materials provided with the distribution.
16 #
17 # 3. Neither the name of the copyright holder nor the names of its contributors may be
18 # used to endorse or promote products derived from this software without specific
19 # prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
22 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 from __future__ import print_function
32 import time
33
34 """Test peer baning RPC calls
35
36 Test the following RPCs:
37 - set_bans
38 - get_bans
39
40 """
41
42 from framework.daemon import Daemon
43
44 class BanTest():
45 def run_test(self):
46 print('Testing bans')
47
48 daemon = Daemon()
49 res = daemon.get_bans()
50 assert 'bans' not in res or len(res.bans) == 0
51
52 daemon.set_bans([{'host': '1.2.3.4', 'ban': True, 'seconds': 100}])
53 res = daemon.get_bans()
54 assert len(res.bans) == 1
55 assert res.bans[0].host == '1.2.3.4'
56 assert res.bans[0].seconds >= 98 and res.bans[0].seconds <= 100 # allow for slow RPC
57
58 daemon.set_bans([{'host': '5.6.7.8', 'ban': True, 'seconds': 100}])
59 res = daemon.get_bans()
60 assert len(res.bans) == 2
61 for i in range(2):
62 assert res.bans[i].host == '1.2.3.4' or res.bans[i].host == '5.6.7.8'
63 assert res.bans[i].seconds >= 7 and res.bans[0].seconds <= 100 # allow for slow RPC
64
65 daemon.set_bans([{'host': '1.2.3.4', 'ban': False}])
66 res = daemon.get_bans()
67 assert len(res.bans) == 1
68 assert res.bans[0].host == '5.6.7.8'
69 assert res.bans[0].seconds >= 98 and res.bans[0].seconds <= 100 # allow for slow RPC
70
71 time.sleep(2)
72
73 res = daemon.get_bans()
74 assert len(res.bans) == 1
75 assert res.bans[0].host == '5.6.7.8'
76 assert res.bans[0].seconds >= 96 and res.bans[0].seconds <= 98 # allow for slow RPC
77
78 daemon.set_bans([{'host': '3.4.5.6', 'ban': False}])
79 res = daemon.get_bans()
80 assert len(res.bans) == 1
81 assert res.bans[0].host == '5.6.7.8'
82 assert res.bans[0].seconds >= 96 and res.bans[0].seconds <= 98 # allow for slow RPC
83
84 daemon.set_bans([{'host': '3.4.5.6', 'ban': True, 'seconds': 2}])
85 res = daemon.get_bans()
86 assert len(res.bans) == 2
87 for i in range(2):
88 assert res.bans[i].host == '5.6.7.8' or res.bans[i].host == '3.4.5.6'
89 if res.bans[i].host == '5.6.7.8':
90 assert res.bans[i].seconds >= 96 and res.bans[0].seconds <= 98 # allow for slow RPC
91 else:
92 assert res.bans[i].seconds >= 1 and res.bans[0].seconds <= 2 # allow for slow RPC
93
94 time.sleep(2)
95 res = daemon.get_bans()
96 assert len(res.bans) == 1
97 assert res.bans[0].host == '5.6.7.8'
98 assert res.bans[0].seconds >= 94 and res.bans[0].seconds <= 96 # allow for slow RPC
99
100 daemon.set_bans([{'host': '5.6.7.8', 'ban': True, 'seconds': 20}])
101 res = daemon.get_bans()
102 assert len(res.bans) == 1
103 assert res.bans[0].host == '5.6.7.8'
104 assert res.bans[0].seconds >= 18 and res.bans[0].seconds <= 20 # allow for slow RPC
105
106 daemon.set_bans([{'host': '5.6.7.8', 'ban': True, 'seconds': 200}])
107 res = daemon.get_bans()
108 assert len(res.bans) == 1
109 assert res.bans[0].host == '5.6.7.8'
110 assert res.bans[0].seconds >= 198 and res.bans[0].seconds <= 200 # allow for slow RPC
111
112 daemon.set_bans([{'host': '5.6.7.8', 'ban': False}])
113 res = daemon.get_bans()
114 assert 'bans' not in res or len(res.bans) == 0
115
116
117 if __name__ == '__main__':
118 BanTest().run_test()