shadowsocks/test.py

59 lines
1.5 KiB
Python
Raw Normal View History

2013-07-04 15:16:10 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
2014-06-01 09:35:32 +00:00
import sys
2013-07-04 15:16:10 +00:00
import os
import signal
import select
2014-05-17 07:00:12 +00:00
import time
2013-07-04 15:16:10 +00:00
from subprocess import Popen, PIPE
2014-06-20 14:58:01 +00:00
if 'salsa20' in sys.argv[-1]:
from shadowsocks import encrypt_salsa20
encrypt_salsa20.test()
print 'encryption test passed'
2013-07-04 15:16:10 +00:00
2014-06-20 14:32:10 +00:00
p1 = Popen(['python', 'shadowsocks/server.py', '-c', sys.argv[-1]], stdin=PIPE,
stdout=PIPE, stderr=PIPE, close_fds=True)
p2 = Popen(['python', 'shadowsocks/local.py', '-c', sys.argv[-1]], stdin=PIPE,
stdout=PIPE, stderr=PIPE, close_fds=True)
2014-06-18 07:50:05 +00:00
p3 = None
2013-07-04 15:16:10 +00:00
try:
2014-05-22 04:09:43 +00:00
local_ready = False
server_ready = False
2013-07-04 15:16:10 +00:00
fdset = [p1.stdout, p2.stdout, p1.stderr, p2.stderr]
while True:
r, w, e = select.select(fdset, [], fdset)
if e:
break
for fd in r:
line = fd.readline()
sys.stdout.write(line)
2014-05-22 04:09:43 +00:00
if line.find('starting local') >= 0:
local_ready = True
if line.find('starting server') >= 0:
server_ready = True
if local_ready and server_ready and p3 is None:
time.sleep(1)
2013-07-04 15:16:10 +00:00
p3 = Popen(['curl', 'http://www.example.com/', '-v', '-L',
2014-06-20 14:32:10 +00:00
'--socks5-hostname', '127.0.0.1:1081'], close_fds=True)
2013-07-04 15:16:10 +00:00
break
2014-06-20 14:32:10 +00:00
2013-07-04 15:16:10 +00:00
if p3 is not None:
r = p3.wait()
if r == 0:
print 'test passed'
sys.exit(r)
2014-06-01 09:35:32 +00:00
2013-07-04 15:16:10 +00:00
finally:
for p in [p1, p2]:
try:
os.kill(p.pid, signal.SIGTERM)
except OSError:
pass
sys.exit(-1)