| 1 |
# Copyright (C) 2007 Manu Garg. |
| 2 |
# Author: Manu Garg <[email protected]> |
| 3 |
# |
| 4 |
# pacparser is a library that provides methods to parse proxy auto-config |
| 5 |
# (PAC) files. Please read README file included with this package for more |
| 6 |
# information about this library. |
| 7 |
# |
| 8 |
# pacparser is free software; you can redistribute it and/or |
| 9 |
# modify it under the terms of the GNU Lesser General Public |
| 10 |
# License as published by the Free Software Foundation; either |
| 11 |
# version 2.1 of the License, or (at your option) any later version. |
| 12 |
|
| 13 |
# pacparser is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
# Lesser General Public License for more details. |
| 17 |
|
| 18 |
# You should have received a copy of the GNU Lesser General Public |
| 19 |
# License along with this library; if not, write to the Free Software |
| 20 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 21 |
# USA. |
| 22 |
|
| 23 |
import getopt |
| 24 |
import glob |
| 25 |
import os |
| 26 |
import sys |
| 27 |
import tempfile |
| 28 |
|
| 29 |
def module_path(tests_dir): |
| 30 |
py_ver = '*'.join([str(x) for x in sys.version_info[0:2]]) |
| 31 |
|
| 32 |
builddir = os.path.join(tests_dir, '..', 'src', 'pymod', 'build') |
| 33 |
print('Build dir: %s', builddir) |
| 34 |
print(os.listdir(builddir)) |
| 35 |
|
| 36 |
return glob.glob(os.path.join(builddir, 'lib*%s' % py_ver))[0] |
| 37 |
|
| 38 |
def runtests(pacfile, testdata, tests_dir): |
| 39 |
try: |
| 40 |
pacparser_module_path = module_path(tests_dir) |
| 41 |
except Exception: |
| 42 |
raise Exception('Tests failed. Could not determine pacparser path.') |
| 43 |
if 'DEBUG' in os.environ: print('Pacparser module path: %s' % |
| 44 |
pacparser_module_path) |
| 45 |
sys.path.insert(0, pacparser_module_path) |
| 46 |
|
| 47 |
try: |
| 48 |
import pacparser |
| 49 |
except ImportError: |
| 50 |
raise Exception('Tests failed. Could not import pacparser.') |
| 51 |
|
| 52 |
if 'DEBUG' in os.environ: print('Imported pacparser module: %s' % |
| 53 |
sys.modules['pacparser']) |
| 54 |
|
| 55 |
f = open(testdata) |
| 56 |
for line in f: |
| 57 |
comment = '' |
| 58 |
if '#' in line: |
| 59 |
comment = line.split('#', 1)[1] |
| 60 |
line = line.split('#', 1)[0].strip() |
| 61 |
if not line: |
| 62 |
continue |
| 63 |
if ('NO_INTERNET' in os.environ and os.environ['NO_INTERNET'] and |
| 64 |
'INTERNET_REQUIRED' in comment): |
| 65 |
continue |
| 66 |
if 'DEBUG' in os.environ: print(line) |
| 67 |
(params, expected_result) = line.strip().split('|') |
| 68 |
args = dict(getopt.getopt(params.split(), 'eu:c:')[0]) |
| 69 |
if '-e' in args: |
| 70 |
pacparser.enable_microsoft_extensions() |
| 71 |
if '-c' in args: |
| 72 |
pacparser.setmyip(args['-c']) |
| 73 |
pacparser.init() |
| 74 |
pacparser.parse_pac_file(pacfile) |
| 75 |
result = pacparser.find_proxy(args['-u']) |
| 76 |
pacparser.cleanup() |
| 77 |
if result != expected_result: |
| 78 |
raise Exception('Tests failed. Got "%s", expected "%s"' % (result, expected_result)) |
| 79 |
|
| 80 |
# Logging test: alert() / console.log() should emit to the C library's |
| 81 |
# stderr. The extension writes directly via vfprintf, so redirect at the |
| 82 |
# file-descriptor level rather than via sys.stderr. |
| 83 |
logging_pac = os.path.join(tests_dir, 'logging.pac') |
| 84 |
expected_stderr = ('ALERT: checking example.com\n' |
| 85 |
'LOG: url: http://example.com/\n' |
| 86 |
'LOG: single arg\n') |
| 87 |
with tempfile.TemporaryFile(mode='w+') as tmp: |
| 88 |
saved_fd = os.dup(2) |
| 89 |
os.dup2(tmp.fileno(), 2) |
| 90 |
try: |
| 91 |
pacparser.init() |
| 92 |
pacparser.parse_pac_file(logging_pac) |
| 93 |
proxy = pacparser.find_proxy('http://example.com/', 'example.com') |
| 94 |
pacparser.cleanup() |
| 95 |
finally: |
| 96 |
sys.stderr.flush() |
| 97 |
os.dup2(saved_fd, 2) |
| 98 |
os.close(saved_fd) |
| 99 |
tmp.seek(0) |
| 100 |
actual_stderr = tmp.read() |
| 101 |
if proxy != 'DIRECT': |
| 102 |
raise Exception('Logging test failed: proxy was "%s", expected "DIRECT"' % proxy) |
| 103 |
if actual_stderr != expected_stderr: |
| 104 |
raise Exception('Logging test failed: stderr mismatch\nExpected:\n%s\nGot:\n%s' % |
| 105 |
(expected_stderr, actual_stderr)) |
| 106 |
|
| 107 |
print('All tests were successful.') |
| 108 |
|
| 109 |
|
| 110 |
def main(): |
| 111 |
tests_dir = os.path.dirname(os.path.join(os.getcwd(), sys.argv[0])) |
| 112 |
pacfile = os.path.join(tests_dir, 'proxy.pac') |
| 113 |
testdata = os.path.join(tests_dir, 'testdata') |
| 114 |
runtests(pacfile, testdata, tests_dir) |
| 115 |
|
| 116 |
if __name__ == '__main__': |
| 117 |
main() |