4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Test running bitcoind with the -rpcbind and -rpcallowip options."""
6
6
7
- import socket
8
7
import sys
9
8
10
9
from test_framework .test_framework import BitcoinTestFramework , SkipTest
@@ -20,6 +19,11 @@ def set_test_params(self):
20
19
def setup_network (self ):
21
20
self .add_nodes (self .num_nodes , None )
22
21
22
+ def add_options (self , parser ):
23
+ parser .add_option ("--ipv4" , action = 'store_true' , dest = "run_ipv4" , help = "Run ipv4 tests only" , default = False )
24
+ parser .add_option ("--ipv6" , action = 'store_true' , dest = "run_ipv6" , help = "Run ipv6 tests only" , default = False )
25
+ parser .add_option ("--nonloopback" , action = 'store_true' , dest = "run_nonloopback" , help = "Run non-loopback tests only" , default = False )
26
+
23
27
def run_bind_test (self , allow_ips , connect_to , addresses , expected ):
24
28
'''
25
29
Start a node with requested rpcallowip and rpcbind parameters,
@@ -54,55 +58,69 @@ def run_allowip_test(self, allow_ips, rpchost, rpcport):
54
58
55
59
def run_test (self ):
56
60
# due to OS-specific network stats queries, this test works only on Linux
61
+ if sum ([self .options .run_ipv4 , self .options .run_ipv6 , self .options .run_nonloopback ]) > 1 :
62
+ raise AssertionError ("Only one of --ipv4, --ipv6 and --nonloopback can be set" )
63
+
64
+ self .log .info ("Check for linux" )
57
65
if not sys .platform .startswith ('linux' ):
58
- raise SkipTest ("This test can only be run on Linux." )
59
- # find the first non-loopback interface for testing
60
- non_loopback_ip = None
66
+ raise SkipTest ("This test can only be run on linux." )
67
+
68
+ self .log .info ("Check for ipv6" )
69
+ have_ipv6 = test_ipv6_local ()
70
+ if not have_ipv6 and not self .options .run_ipv4 :
71
+ raise SkipTest ("This test requires ipv6 support." )
72
+
73
+ self .log .info ("Check for non-loopback interface" )
74
+ self .non_loopback_ip = None
61
75
for name ,ip in all_interfaces ():
62
76
if ip != '127.0.0.1' :
63
- non_loopback_ip = ip
77
+ self . non_loopback_ip = ip
64
78
break
65
- if non_loopback_ip is None :
66
- raise SkipTest ("This test requires at least one non-loopback IPv4 interface." )
67
- try :
68
- s = socket .socket (socket .AF_INET6 , socket .SOCK_DGRAM )
69
- s .connect (("::1" ,1 ))
70
- s .close
71
- except OSError :
72
- raise SkipTest ("This test requires IPv6 support." )
73
-
74
- self .log .info ("Using interface %s for testing" % non_loopback_ip )
75
-
76
- defaultport = rpc_port (0 )
77
-
78
- # check default without rpcallowip (IPv4 and IPv6 localhost)
79
- self .run_bind_test (None , '127.0.0.1' , [],
80
- [('127.0.0.1' , defaultport ), ('::1' , defaultport )])
81
- # check default with rpcallowip (IPv6 any)
82
- self .run_bind_test (['127.0.0.1' ], '127.0.0.1' , [],
83
- [('::0' , defaultport )])
84
- # check only IPv4 localhost (explicit)
85
- self .run_bind_test (['127.0.0.1' ], '127.0.0.1' , ['127.0.0.1' ],
86
- [('127.0.0.1' , defaultport )])
87
- # check only IPv4 localhost (explicit) with alternative port
88
- self .run_bind_test (['127.0.0.1' ], '127.0.0.1:32171' , ['127.0.0.1:32171' ],
89
- [('127.0.0.1' , 32171 )])
90
- # check only IPv4 localhost (explicit) with multiple alternative ports on same host
91
- self .run_bind_test (['127.0.0.1' ], '127.0.0.1:32171' , ['127.0.0.1:32171' , '127.0.0.1:32172' ],
92
- [('127.0.0.1' , 32171 ), ('127.0.0.1' , 32172 )])
93
- # check only IPv6 localhost (explicit)
94
- self .run_bind_test (['[::1]' ], '[::1]' , ['[::1]' ],
95
- [('::1' , defaultport )])
96
- # check both IPv4 and IPv6 localhost (explicit)
97
- self .run_bind_test (['127.0.0.1' ], '127.0.0.1' , ['127.0.0.1' , '[::1]' ],
98
- [('127.0.0.1' , defaultport ), ('::1' , defaultport )])
79
+ if self .non_loopback_ip is None and self .options .run_nonloopback :
80
+ raise SkipTest ("This test requires a non-loopback ip address." )
81
+
82
+ self .defaultport = rpc_port (0 )
83
+
84
+ if not self .options .run_nonloopback :
85
+ self ._run_loopback_tests ()
86
+ if not self .options .run_ipv4 and not self .options .run_ipv6 :
87
+ self ._run_nonloopback_tests ()
88
+
89
+ def _run_loopback_tests (self ):
90
+ if self .options .run_ipv4 :
91
+ # check only IPv4 localhost (explicit)
92
+ self .run_bind_test (['127.0.0.1' ], '127.0.0.1' , ['127.0.0.1' ],
93
+ [('127.0.0.1' , self .defaultport )])
94
+ # check only IPv4 localhost (explicit) with alternative port
95
+ self .run_bind_test (['127.0.0.1' ], '127.0.0.1:32171' , ['127.0.0.1:32171' ],
96
+ [('127.0.0.1' , 32171 )])
97
+ # check only IPv4 localhost (explicit) with multiple alternative ports on same host
98
+ self .run_bind_test (['127.0.0.1' ], '127.0.0.1:32171' , ['127.0.0.1:32171' , '127.0.0.1:32172' ],
99
+ [('127.0.0.1' , 32171 ), ('127.0.0.1' , 32172 )])
100
+ else :
101
+ # check default without rpcallowip (IPv4 and IPv6 localhost)
102
+ self .run_bind_test (None , '127.0.0.1' , [],
103
+ [('127.0.0.1' , self .defaultport ), ('::1' , self .defaultport )])
104
+ # check default with rpcallowip (IPv6 any)
105
+ self .run_bind_test (['127.0.0.1' ], '127.0.0.1' , [],
106
+ [('::0' , self .defaultport )])
107
+ # check only IPv6 localhost (explicit)
108
+ self .run_bind_test (['[::1]' ], '[::1]' , ['[::1]' ],
109
+ [('::1' , self .defaultport )])
110
+ # check both IPv4 and IPv6 localhost (explicit)
111
+ self .run_bind_test (['127.0.0.1' ], '127.0.0.1' , ['127.0.0.1' , '[::1]' ],
112
+ [('127.0.0.1' , self .defaultport ), ('::1' , self .defaultport )])
113
+
114
+ def _run_nonloopback_tests (self ):
115
+ self .log .info ("Using interface %s for testing" % self .non_loopback_ip )
116
+
99
117
# check only non-loopback interface
100
- self .run_bind_test ([non_loopback_ip ], non_loopback_ip , [non_loopback_ip ],
101
- [(non_loopback_ip , defaultport )])
118
+ self .run_bind_test ([self . non_loopback_ip ], self . non_loopback_ip , [self . non_loopback_ip ],
119
+ [(self . non_loopback_ip , self . defaultport )])
102
120
103
121
# Check that with invalid rpcallowip, we are denied
104
- self .run_allowip_test ([non_loopback_ip ], non_loopback_ip , defaultport )
105
- assert_raises_rpc_error (- 342 , "non-JSON HTTP response with '403 Forbidden' from server" , self .run_allowip_test , ['1.1.1.1' ], non_loopback_ip , defaultport )
122
+ self .run_allowip_test ([self . non_loopback_ip ], self . non_loopback_ip , self . defaultport )
123
+ assert_raises_rpc_error (- 342 , "non-JSON HTTP response with '403 Forbidden' from server" , self .run_allowip_test , ['1.1.1.1' ], self . non_loopback_ip , self . defaultport )
106
124
107
125
if __name__ == '__main__' :
108
126
RPCBindTest ().main ()
0 commit comments