diff options
author | rl1987 <rl1987@sdf.lonestar.org> | 2018-07-21 12:13:58 +0300 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-09-12 09:06:16 -0400 |
commit | 4811869d7a603dfb6cc9881bb5aae9815ceab4e5 (patch) | |
tree | ed233c1f13ece5fc1278419888efd1d75ac03081 /src | |
parent | d8157097b42b6377d0cd83c758efce5825df83f2 (diff) | |
download | tor-4811869d7a603dfb6cc9881bb5aae9815ceab4e5.tar.gz tor-4811869d7a603dfb6cc9881bb5aae9815ceab4e5.zip |
Pick random ports in test_rebind.py
Diffstat (limited to 'src')
-rw-r--r-- | src/test/test_rebind.py | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/src/test/test_rebind.py b/src/test/test_rebind.py index 4a132411b9..9472d5cef5 100644 --- a/src/test/test_rebind.py +++ b/src/test/test_rebind.py @@ -5,10 +5,11 @@ import subprocess import socket import os import time +import random def try_connecting_to_socksport(): socks_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - if socks_socket.connect_ex(('127.0.0.1', 9052)): + if socks_socket.connect_ex(('127.0.0.1', socks_port)): tor_process.terminate() print('FAIL') sys.exit('Cannot connect to SOCKSPort') @@ -20,14 +21,34 @@ def wait_for_log(s): if s in l: return +def pick_random_port(): + port = 0 + random.seed() + + for i in xrange(8): + port = random.randint(10000, 60000) + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + if s.connect_ex(('127.0.0.1', port)) == 0: + s.close() + else: + break + + return port + +control_port = pick_random_port() +socks_port = pick_random_port() + +assert control_port != 0 +assert socks_port != 0 + if not os.path.exists(sys.argv[1]): sys.exit('ERROR: cannot find tor at %s' % sys.argv[1]) tor_path = sys.argv[1] tor_process = subprocess.Popen([tor_path, - '-ControlPort', '127.0.0.1:9053', - '-SOCKSPort', '127.0.0.1:9052', + '-ControlPort', '127.0.0.1:{}'.format(control_port), + '-SOCKSPort', '127.0.0.1:{}'.format(socks_port), '-FetchServerDescriptors', '0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -43,18 +64,18 @@ wait_for_log('Opened Control listener on') try_connecting_to_socksport() control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -if control_socket.connect_ex(('127.0.0.1', 9053)): +if control_socket.connect_ex(('127.0.0.1', control_port)): tor_process.terminate() print('FAIL') sys.exit('Cannot connect to ControlPort') control_socket.sendall('AUTHENTICATE \r\n') -control_socket.sendall('SETCONF SOCKSPort=0.0.0.0:9052\r\n') +control_socket.sendall('SETCONF SOCKSPort=0.0.0.0:{}\r\n'.format(socks_port)) wait_for_log('Opened Socks listener') try_connecting_to_socksport() -control_socket.sendall('SETCONF SOCKSPort=127.0.0.1:9052\r\n') +control_socket.sendall('SETCONF SOCKSPort=127.0.0.1:{}\r\n'.format(socks_port)) wait_for_log('Opened Socks listener') try_connecting_to_socksport() |