shadowsocks/test.py

60 lines
1.6 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-18 07:50:05 +00:00
from shadowsocks import encrypt_salsa20
2013-07-04 15:16:10 +00:00
2014-06-01 09:35:32 +00:00
encrypt_salsa20.test()
2013-07-04 15:16:10 +00:00
print 'encryption test passed'
2014-06-18 07:50:05 +00:00
p1 = Popen(['python', 'shadowsocks/server.py', '-c', sys.argv[-1]], shell=False,
bufsize=0, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
p2 = Popen(['python', 'shadowsocks/local.py', '-c', sys.argv[-1]], shell=False,
bufsize=0, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
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-01 09:27:36 +00:00
'--socks5-hostname', '127.0.0.1:1081'], shell=False,
2014-06-18 07:50:05 +00:00
bufsize=0, close_fds=True)
2013-07-04 15:16:10 +00:00
break
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)