shadowsocks/tests/test.py

161 lines
5.3 KiB
Python
Raw Normal View History

2013-07-04 15:16:10 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
2014-11-01 04:25:44 +00:00
# Copyright (c) 2014 clowwindy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2014-10-31 10:28:22 +00:00
from __future__ import absolute_import, division, print_function, \
with_statement
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
2014-11-06 04:56:00 +00:00
import argparse
2013-07-04 15:16:10 +00:00
from subprocess import Popen, PIPE
2014-12-11 04:43:16 +00:00
python = ['python']
2014-11-06 04:56:00 +00:00
parser = argparse.ArgumentParser(description='test Shadowsocks')
parser.add_argument('-c', '--client-conf', type=str, default=None)
parser.add_argument('-s', '--server-conf', type=str, default=None)
parser.add_argument('-a', '--client-args', type=str, default=None)
parser.add_argument('-b', '--server-args', type=str, default=None)
2014-12-11 04:43:16 +00:00
parser.add_argument('--with-coverage', action='store_true', default=None)
parser.add_argument('--should-fail', action='store_true', default=None)
parser.add_argument('--tcp-only', action='store_true', default=None)
parser.add_argument('--url', type=str, default='http://www.example.com/')
parser.add_argument('--dns', type=str, default='8.8.8.8')
2014-11-06 04:56:00 +00:00
config = parser.parse_args()
2014-12-11 04:43:16 +00:00
if config.with_coverage:
2014-12-11 06:41:53 +00:00
python = ['coverage', 'run', '-p', '-a']
2014-12-11 04:43:16 +00:00
client_args = python + ['shadowsocks/local.py', '-v']
server_args = python + ['shadowsocks/server.py', '-v']
2014-11-06 04:56:00 +00:00
if config.client_conf:
client_args.extend(['-c', config.client_conf])
if config.server_conf:
server_args.extend(['-c', config.server_conf])
else:
server_args.extend(['-c', config.client_conf])
if config.client_args:
client_args.extend(config.client_args.split())
if config.server_args:
server_args.extend(config.server_args.split())
else:
server_args.extend(config.client_args.split())
p1 = Popen(server_args, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
p2 = Popen(client_args, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
2014-06-18 07:50:05 +00:00
p3 = None
p4 = None
p3_fin = False
p4_fin = False
# 1 shadowsocks started
# 2 curl started
# 3 curl finished
# 4 dig started
# 5 dig finished
stage = 1
2014-06-18 07:50:05 +00:00
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
2014-08-26 16:17:26 +00:00
2013-07-04 15:16:10 +00:00
for fd in r:
line = fd.readline()
if not line:
if stage == 2 and fd == p3.stdout:
stage = 3
if stage == 4 and fd == p4.stdout:
stage = 5
2014-10-31 10:39:39 +00:00
if bytes != str:
line = str(line, 'utf8')
2015-01-12 15:19:24 +00:00
sys.stderr.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
2014-11-01 17:25:02 +00:00
if stage == 1:
time.sleep(2)
2014-07-11 09:35:47 +00:00
p3 = Popen(['curl', config.url, '-v', '-L',
'--socks5-hostname', '127.0.0.1:1081',
'-m', '15', '--connect-timeout', '10'],
stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
if p3 is not None:
fdset.append(p3.stdout)
fdset.append(p3.stderr)
stage = 2
else:
sys.exit(1)
2014-07-11 09:35:47 +00:00
if stage == 3 and p3 is not None:
fdset.remove(p3.stdout)
fdset.remove(p3.stderr)
r = p3.wait()
if config.should_fail:
if r == 0:
sys.exit(1)
else:
if r != 0:
sys.exit(1)
if config.tcp_only:
break
2015-01-12 15:05:19 +00:00
p4 = Popen(['socksify', 'dig', '@%s' % config.dns,
'www.google.com'],
stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
if p4 is not None:
fdset.append(p4.stdout)
fdset.append(p4.stderr)
stage = 4
else:
sys.exit(1)
2014-06-01 09:35:32 +00:00
if stage == 5:
r = p4.wait()
if config.should_fail:
if r == 0:
sys.exit(1)
print('test passed (expecting failure)')
else:
if r != 0:
sys.exit(1)
print('test passed')
break
2013-07-04 15:16:10 +00:00
finally:
for p in [p1, p2]:
try:
os.kill(p.pid, signal.SIGINT)
2014-12-11 04:50:25 +00:00
os.waitpid(p.pid, 0)
2013-07-04 15:16:10 +00:00
except OSError:
pass