1 # Future imports for Python 2.7, mandatory in 3.0
2 from __future__
import division
3 from __future__
import print_function
4 from __future__
import unicode_literals
16 CONTROL_SOCK_TIMEOUT
= 10.0
25 logging
.warning('SKIP: {}'.format(msg
))
29 cutoff
= time
.time() + LOG_TIMEOUT
30 while time
.time() < cutoff
:
31 l
= tor_process
.stdout
.readline()
32 l
= l
.decode('utf8', 'backslashreplace')
34 logging
.info('Tor logged: "{}"'.format(l
.strip()))
36 # readline() returns a blank string when there is no output
39 logging
.debug('Tor has not logged anything, waiting for "{}"'.format(s
))
42 logging
.info('Tor logged: "{}", waiting for "{}"'.format(l
.strip(), s
))
43 fail('Could not find "{}" in logs after {} seconds'.format(s
, LOG_TIMEOUT
))
45 def pick_random_port():
50 port
= random
.randint(10000, 60000)
51 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
52 if s
.connect_ex(('127.0.0.1', port
)) == 0:
58 fail('Could not find a random free port between 10000 and 60000')
62 def check_control_list(control_out_file
, expected
, value_name
):
65 received
= control_out_file
.readline().strip()
67 parts
= re
.split('[ =-]', received
.strip())
68 if len(parts
) != 3 or parts
[0] != '250' or parts
[1] != value_name
or parts
[2] != e
:
69 fail('Unexpected value in response line "{}". Expected {} for value {}'.format(received
, e
, value_name
))
70 if received
.startswith('250 '):
73 if received_count
!= len(expected
):
74 fail('Expected response with {} lines but received {} lines'.format(len(expected
), received_count
))
77 logging
.basicConfig(level
=logging
.DEBUG
,
78 format
='%(asctime)s.%(msecs)03d %(message)s',
79 datefmt
='%Y-%m-%d %H:%M:%S')
81 if sys
.hexversion
< 0x02070000:
82 fail("ERROR: unsupported Python version (should be >= 2.7)")
84 if sys
.hexversion
> 0x03000000 and sys
.hexversion
< 0x03010000:
85 fail("ERROR: unsupported Python3 version (should be >= 3.1)")
87 if 'TOR_SKIP_TEST_INCLUDE' in os
.environ
:
88 skip('$TOR_SKIP_TEST_INCLUDE is set')
90 control_port
= pick_random_port()
92 assert control_port
!= 0
95 fail('Usage: %s <path-to-tor> <data-dir> <torrc>' % sys
.argv
[0])
97 if not os
.path
.exists(sys
.argv
[1]):
98 fail('ERROR: cannot find tor at %s' % sys
.argv
[1])
99 if not os
.path
.exists(sys
.argv
[2]):
100 fail('ERROR: cannot find datadir at %s' % sys
.argv
[2])
101 if not os
.path
.exists(sys
.argv
[3]):
102 fail('ERROR: cannot find torrcdir at %s' % sys
.argv
[3])
104 tor_path
= sys
.argv
[1]
105 data_dir
= sys
.argv
[2]
106 torrc_dir
= sys
.argv
[3]
108 empty_torrc_path
= os
.path
.join(data_dir
, 'empty_torrc')
109 open(empty_torrc_path
, 'w').close()
110 empty_defaults_torrc_path
= os
.path
.join(data_dir
, 'empty_defaults_torrc')
111 open(empty_defaults_torrc_path
, 'w').close()
112 torrc
= os
.path
.join(torrc_dir
, 'torrc')
114 tor_process
= subprocess
.Popen([tor_path
,
115 '-DataDirectory', data_dir
,
116 '-ControlPort', '127.0.0.1:{}'.format(control_port
),
117 '-Log', 'info stdout',
118 '-LogTimeGranularity', '1',
119 '-FetchServerDescriptors', '0',
120 '-DisableNetwork', '1',
122 '--defaults-torrc', empty_defaults_torrc_path
,
124 stdout
=subprocess
.PIPE
,
125 stderr
=subprocess
.PIPE
)
127 if tor_process
== None:
128 fail('ERROR: running tor failed')
130 wait_for_log('Opened Control listener')
132 control_socket
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
133 if control_socket
.connect_ex(('127.0.0.1', control_port
)):
134 tor_process
.terminate()
135 fail('Cannot connect to ControlPort')
136 control_socket
.settimeout(CONTROL_SOCK_TIMEOUT
)
137 control_out_file
= control_socket
.makefile('r')
139 control_socket
.sendall('AUTHENTICATE \r\n'.encode('ascii'))
140 res
= control_out_file
.readline().strip()
142 tor_process
.terminate()
143 fail('Cannot authenticate. Response was: {}'.format(res
))
145 # test configuration file values and order
146 control_socket
.sendall('GETCONF NodeFamily\r\n'.encode('ascii'))
147 check_control_list(control_out_file
, ['1', '2', '3', '4', '5', '6', '4' , '5'], 'NodeFamily')
149 # test reloading the configuration file with seccomp sandbox enabled
150 foo_path
= os
.path
.join(torrc_dir
, 'torrc.d', 'foo')
151 with
open(foo_path
, 'a') as foo
:
152 foo
.write('NodeFamily 7')
154 control_socket
.sendall('SIGNAL RELOAD\r\n'.encode('ascii'))
155 wait_for_log('Reloading config and resetting internal state.')
156 res
= control_out_file
.readline().strip()
158 tor_process
.terminate()
159 fail('Cannot reload configuration. Response was: {}'.format(res
))
162 control_socket
.sendall('GETCONF NodeFamily\r\n'.encode('ascii'))
163 check_control_list(control_out_file
, ['1', '2', '3', '4', '5', '6', '7', '4' , '5'], 'NodeFamily')
165 # test that config-can-saveconf is 0 because we have a %include
166 control_socket
.sendall('getinfo config-can-saveconf\r\n'.encode('ascii'))
167 res
= control_out_file
.readline().strip()
168 if res
!= '250-config-can-saveconf=0':
169 tor_process
.terminate()
170 fail('getinfo config-can-saveconf returned wrong response: {}'.format(res
))
172 res
= control_out_file
.readline().strip()
174 tor_process
.terminate()
175 fail('getinfo failed. Response was: {}'.format(res
))
177 # test that saveconf returns error because we have a %include
178 control_socket
.sendall('SAVECONF\r\n'.encode('ascii'))
179 res
= control_out_file
.readline().strip()
180 if res
!= '551 Unable to write configuration to disk.':
181 tor_process
.terminate()
182 fail('SAVECONF returned wrong response. Response was: {}'.format(res
))
184 control_socket
.sendall('SIGNAL HALT\r\n'.encode('ascii'))
186 wait_for_log('exiting cleanly')
190 tor_process
.terminate()
192 if e
.errno
== errno
.ESRCH
: # errno 3: No such process
193 # assume tor has already exited due to SIGNAL HALT
194 logging
.warn("Tor has already exited")