1# Future imports for Python 2.7, mandatory in 3.0
2from __future__ import division
3from __future__ import print_function
4from __future__ import unicode_literals
5
6import errno
7import logging
8import os
9import random
10import socket
11import subprocess
12import sys
13import time
14import re
15
16CONTROL_SOCK_TIMEOUT = 10.0
17LOG_TIMEOUT = 60.0
18LOG_WAIT = 0.1
19
20def fail(msg):
21    logging.error('FAIL')
22    sys.exit(msg)
23
24def skip(msg):
25    logging.warning('SKIP: {}'.format(msg))
26    sys.exit(77)
27
28def wait_for_log(s):
29    cutoff = time.time() + LOG_TIMEOUT
30    while time.time() < cutoff:
31        l = tor_process.stdout.readline()
32        l = l.decode('utf8', 'backslashreplace')
33        if s in l:
34            logging.info('Tor logged: "{}"'.format(l.strip()))
35            return
36        # readline() returns a blank string when there is no output
37        # avoid busy-waiting
38        if len(l) == 0:
39            logging.debug('Tor has not logged anything, waiting for "{}"'.format(s))
40            time.sleep(LOG_WAIT)
41        else:
42            logging.info('Tor logged: "{}", waiting for "{}"'.format(l.strip(), s))
43    fail('Could not find "{}" in logs after {} seconds'.format(s, LOG_TIMEOUT))
44
45def pick_random_port():
46    port = 0
47    random.seed()
48
49    for i in range(8):
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:
53            s.close()
54        else:
55            break
56
57    if port == 0:
58        fail('Could not find a random free port between 10000 and 60000')
59
60    return port
61
62def check_control_list(control_out_file, expected, value_name):
63    received_count = 0
64    for e in expected:
65        received = control_out_file.readline().strip()
66        received_count += 1
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 '):
71            break
72
73    if received_count != len(expected):
74        fail('Expected response with {} lines but received {} lines'.format(len(expected), received_count))
75
76
77logging.basicConfig(level=logging.DEBUG,
78                    format='%(asctime)s.%(msecs)03d %(message)s',
79                    datefmt='%Y-%m-%d %H:%M:%S')
80
81if sys.hexversion < 0x02070000:
82    fail("ERROR: unsupported Python version (should be >= 2.7)")
83
84if sys.hexversion > 0x03000000 and sys.hexversion < 0x03010000:
85    fail("ERROR: unsupported Python3 version (should be >= 3.1)")
86
87if 'TOR_SKIP_TEST_INCLUDE' in os.environ:
88    skip('$TOR_SKIP_TEST_INCLUDE is set')
89
90control_port = pick_random_port()
91
92assert control_port != 0
93
94if len(sys.argv) < 4:
95     fail('Usage: %s <path-to-tor> <data-dir> <torrc>' % sys.argv[0])
96
97if not os.path.exists(sys.argv[1]):
98    fail('ERROR: cannot find tor at %s' % sys.argv[1])
99if not os.path.exists(sys.argv[2]):
100    fail('ERROR: cannot find datadir at %s' % sys.argv[2])
101if not os.path.exists(sys.argv[3]):
102    fail('ERROR: cannot find torrcdir at %s' % sys.argv[3])
103
104tor_path = sys.argv[1]
105data_dir = sys.argv[2]
106torrc_dir = sys.argv[3]
107
108empty_torrc_path = os.path.join(data_dir, 'empty_torrc')
109open(empty_torrc_path, 'w').close()
110empty_defaults_torrc_path = os.path.join(data_dir, 'empty_defaults_torrc')
111open(empty_defaults_torrc_path, 'w').close()
112torrc = os.path.join(torrc_dir, 'torrc')
113
114tor_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',
121                               '-f', torrc,
122                               '--defaults-torrc', empty_defaults_torrc_path,
123                               ],
124                               stdout=subprocess.PIPE,
125                               stderr=subprocess.PIPE)
126
127if tor_process == None:
128    fail('ERROR: running tor failed')
129
130wait_for_log('Opened Control listener')
131
132control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
133if control_socket.connect_ex(('127.0.0.1', control_port)):
134    tor_process.terminate()
135    fail('Cannot connect to ControlPort')
136control_socket.settimeout(CONTROL_SOCK_TIMEOUT)
137control_out_file = control_socket.makefile('r')
138
139control_socket.sendall('AUTHENTICATE \r\n'.encode('ascii'))
140res = control_out_file.readline().strip()
141if res != '250 OK':
142    tor_process.terminate()
143    fail('Cannot authenticate. Response was: {}'.format(res))
144
145# test configuration file values and order
146control_socket.sendall('GETCONF NodeFamily\r\n'.encode('ascii'))
147check_control_list(control_out_file, ['1', '2', '3', '4', '5', '6', '4' , '5'], 'NodeFamily')
148
149# test reloading the configuration file with seccomp sandbox enabled
150foo_path = os.path.join(torrc_dir, 'torrc.d', 'foo')
151with open(foo_path, 'a') as foo:
152    foo.write('NodeFamily 7')
153
154control_socket.sendall('SIGNAL RELOAD\r\n'.encode('ascii'))
155wait_for_log('Reloading config and resetting internal state.')
156res = control_out_file.readline().strip()
157if res != '250 OK':
158    tor_process.terminate()
159    fail('Cannot reload configuration. Response was: {}'.format(res))
160
161
162control_socket.sendall('GETCONF NodeFamily\r\n'.encode('ascii'))
163check_control_list(control_out_file, ['1', '2', '3', '4', '5', '6', '7', '4' , '5'], 'NodeFamily')
164
165# test that config-can-saveconf is 0 because we have a %include
166control_socket.sendall('getinfo config-can-saveconf\r\n'.encode('ascii'))
167res = control_out_file.readline().strip()
168if res != '250-config-can-saveconf=0':
169    tor_process.terminate()
170    fail('getinfo config-can-saveconf returned wrong response: {}'.format(res))
171else:
172    res = control_out_file.readline().strip()
173    if res != '250 OK':
174        tor_process.terminate()
175        fail('getinfo failed. Response was: {}'.format(res))
176
177# test that saveconf returns error because we have a %include
178control_socket.sendall('SAVECONF\r\n'.encode('ascii'))
179res = control_out_file.readline().strip()
180if res != '551 Unable to write configuration to disk.':
181    tor_process.terminate()
182    fail('SAVECONF returned wrong response. Response was: {}'.format(res))
183
184control_socket.sendall('SIGNAL HALT\r\n'.encode('ascii'))
185
186wait_for_log('exiting cleanly')
187logging.info('OK')
188
189try:
190    tor_process.terminate()
191except OSError as e:
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")
195    else:
196        raise
197