Skip to content

Commit 7539f1a

Browse files
committed
tests: Make proxy_test work on travis servers without IPv6
1 parent b49a623 commit 7539f1a

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

qa/rpc-tests/proxy_test.py

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from test_framework.socks5 import Socks5Configuration, Socks5Command, Socks5Server, AddressType
88
from test_framework.test_framework import BitcoinTestFramework
99
from test_framework.util import *
10+
from test_framework.netutil import test_ipv6_local
1011
'''
1112
Test plan:
1213
- Start bitcoind's with different proxy configurations
@@ -34,6 +35,7 @@
3435

3536
class ProxyTest(BitcoinTestFramework):
3637
def __init__(self):
38+
self.have_ipv6 = test_ipv6_local()
3739
# Create two proxies on different ports
3840
# ... one unauthenticated
3941
self.conf1 = Socks5Configuration()
@@ -45,29 +47,36 @@ def __init__(self):
4547
self.conf2.addr = ('127.0.0.1', 14000 + (os.getpid() % 1000))
4648
self.conf2.unauth = True
4749
self.conf2.auth = True
48-
# ... one on IPv6 with similar configuration
49-
self.conf3 = Socks5Configuration()
50-
self.conf3.af = socket.AF_INET6
51-
self.conf3.addr = ('::1', 15000 + (os.getpid() % 1000))
52-
self.conf3.unauth = True
53-
self.conf3.auth = True
50+
if self.have_ipv6:
51+
# ... one on IPv6 with similar configuration
52+
self.conf3 = Socks5Configuration()
53+
self.conf3.af = socket.AF_INET6
54+
self.conf3.addr = ('::1', 15000 + (os.getpid() % 1000))
55+
self.conf3.unauth = True
56+
self.conf3.auth = True
57+
else:
58+
print "Warning: testing without local IPv6 support"
5459

5560
self.serv1 = Socks5Server(self.conf1)
5661
self.serv1.start()
5762
self.serv2 = Socks5Server(self.conf2)
5863
self.serv2.start()
59-
self.serv3 = Socks5Server(self.conf3)
60-
self.serv3.start()
64+
if self.have_ipv6:
65+
self.serv3 = Socks5Server(self.conf3)
66+
self.serv3.start()
6167

6268
def setup_nodes(self):
6369
# Note: proxies are not used to connect to local nodes
6470
# this is because the proxy to use is based on CService.GetNetwork(), which return NET_UNROUTABLE for localhost
65-
return start_nodes(4, self.options.tmpdir, extra_args=[
71+
args = [
6672
['-listen', '-debug=net', '-debug=proxy', '-proxy=%s:%i' % (self.conf1.addr),'-proxyrandomize=1'],
6773
['-listen', '-debug=net', '-debug=proxy', '-proxy=%s:%i' % (self.conf1.addr),'-onion=%s:%i' % (self.conf2.addr),'-proxyrandomize=0'],
6874
['-listen', '-debug=net', '-debug=proxy', '-proxy=%s:%i' % (self.conf2.addr),'-proxyrandomize=1'],
69-
['-listen', '-debug=net', '-debug=proxy', '-proxy=[%s]:%i' % (self.conf3.addr),'-proxyrandomize=0', '-noonion']
70-
])
75+
[]
76+
]
77+
if self.have_ipv6:
78+
args[3] = ['-listen', '-debug=net', '-debug=proxy', '-proxy=[%s]:%i' % (self.conf3.addr),'-proxyrandomize=0', '-noonion']
79+
return start_nodes(4, self.options.tmpdir, extra_args=args)
7180

7281
def node_test(self, node, proxies, auth, test_onion=True):
7382
rv = []
@@ -84,18 +93,19 @@ def node_test(self, node, proxies, auth, test_onion=True):
8493
assert_equal(cmd.password, None)
8594
rv.append(cmd)
8695

87-
# Test: outgoing IPv6 connection through node
88-
node.addnode("[1233:3432:2434:2343:3234:2345:6546:4534]:5443", "onetry")
89-
cmd = proxies[1].queue.get()
90-
assert(isinstance(cmd, Socks5Command))
91-
# Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6
92-
assert_equal(cmd.atyp, AddressType.DOMAINNAME)
93-
assert_equal(cmd.addr, "1233:3432:2434:2343:3234:2345:6546:4534")
94-
assert_equal(cmd.port, 5443)
95-
if not auth:
96-
assert_equal(cmd.username, None)
97-
assert_equal(cmd.password, None)
98-
rv.append(cmd)
96+
if self.have_ipv6:
97+
# Test: outgoing IPv6 connection through node
98+
node.addnode("[1233:3432:2434:2343:3234:2345:6546:4534]:5443", "onetry")
99+
cmd = proxies[1].queue.get()
100+
assert(isinstance(cmd, Socks5Command))
101+
# Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6
102+
assert_equal(cmd.atyp, AddressType.DOMAINNAME)
103+
assert_equal(cmd.addr, "1233:3432:2434:2343:3234:2345:6546:4534")
104+
assert_equal(cmd.port, 5443)
105+
if not auth:
106+
assert_equal(cmd.username, None)
107+
assert_equal(cmd.password, None)
108+
rv.append(cmd)
99109

100110
if test_onion:
101111
# Test: outgoing onion connection through node
@@ -135,10 +145,11 @@ def run_test(self):
135145
rv = self.node_test(self.nodes[2], [self.serv2, self.serv2, self.serv2, self.serv2], True)
136146
# Check that credentials as used for -proxyrandomize connections are unique
137147
credentials = set((x.username,x.password) for x in rv)
138-
assert_equal(len(credentials), 4)
148+
assert_equal(len(credentials), len(rv))
139149

140-
# proxy on IPv6 localhost
141-
self.node_test(self.nodes[3], [self.serv3, self.serv3, self.serv3, self.serv3], False, False)
150+
if self.have_ipv6:
151+
# proxy on IPv6 localhost
152+
self.node_test(self.nodes[3], [self.serv3, self.serv3, self.serv3, self.serv3], False, False)
142153

143154
def networks_dict(d):
144155
r = {}
@@ -167,11 +178,12 @@ def networks_dict(d):
167178
assert_equal(n2[net]['proxy_randomize_credentials'], True)
168179
assert_equal(n2['onion']['reachable'], True)
169180

170-
n3 = networks_dict(self.nodes[3].getnetworkinfo())
171-
for net in ['ipv4','ipv6']:
172-
assert_equal(n3[net]['proxy'], '[%s]:%i' % (self.conf3.addr))
173-
assert_equal(n3[net]['proxy_randomize_credentials'], False)
174-
assert_equal(n3['onion']['reachable'], False)
181+
if self.have_ipv6:
182+
n3 = networks_dict(self.nodes[3].getnetworkinfo())
183+
for net in ['ipv4','ipv6']:
184+
assert_equal(n3[net]['proxy'], '[%s]:%i' % (self.conf3.addr))
185+
assert_equal(n3[net]['proxy_randomize_credentials'], False)
186+
assert_equal(n3['onion']['reachable'], False)
175187

176188
if __name__ == '__main__':
177189
ProxyTest().main()

qa/rpc-tests/test_framework/netutil.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,18 @@ def addr_to_hex(addr):
137137
else:
138138
raise ValueError('Could not parse address %s' % addr)
139139
return binascii.hexlify(bytearray(addr))
140+
141+
def test_ipv6_local():
142+
'''
143+
Check for (local) IPv6 support.
144+
'''
145+
import socket
146+
# By using SOCK_DGRAM this will not actually make a connection, but it will
147+
# fail if there is no route to IPv6 localhost.
148+
have_ipv6 = True
149+
try:
150+
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
151+
s.connect(('::1', 0))
152+
except socket.error:
153+
have_ipv6 = False
154+
return have_ipv6

0 commit comments

Comments
 (0)