[Python-checkins] r55669 - in python/branches/bcannon-objcap: BRANCH_NOTES run_security_tests.py tests/succeed/whitelist_fallthrough tests/succeed/whitelist_fallthrough/__init__.py tests/succeed/whitelist_fallthrough/prep.py tests/succeed/whitelist_fallthrough/test.py
brett.cannon
python-checkins at python.org
Wed May 30 06:16:15 CEST 2007
Author: brett.cannon
Date: Wed May 30 06:16:13 2007
New Revision: 55669
Added:
python/branches/bcannon-objcap/tests/succeed/whitelist_fallthrough/
python/branches/bcannon-objcap/tests/succeed/whitelist_fallthrough/__init__.py (contents, props changed)
python/branches/bcannon-objcap/tests/succeed/whitelist_fallthrough/prep.py (contents, props changed)
python/branches/bcannon-objcap/tests/succeed/whitelist_fallthrough/test.py (contents, props changed)
Modified:
python/branches/bcannon-objcap/BRANCH_NOTES
python/branches/bcannon-objcap/run_security_tests.py
Log:
Write test that verifies module import fall-through works. Led to coming up
with a system for set up and teardown for tests that need that kind of thing.
Modified: python/branches/bcannon-objcap/BRANCH_NOTES
==============================================================================
--- python/branches/bcannon-objcap/BRANCH_NOTES (original)
+++ python/branches/bcannon-objcap/BRANCH_NOTES Wed May 30 06:16:13 2007
@@ -33,10 +33,6 @@
To Do
=====
* Deal with exit()/SystemExit.
-* Write tests.
- - Import
- + Whitelisting works.
- * Name fall-through to alternate implementation.
==========
Modified: python/branches/bcannon-objcap/run_security_tests.py
==============================================================================
--- python/branches/bcannon-objcap/run_security_tests.py (original)
+++ python/branches/bcannon-objcap/run_security_tests.py Wed May 30 06:16:13 2007
@@ -2,24 +2,48 @@
import os
import re
+def exec_test(file_path):
+ exec_ = './secure_python.exe ' + file_path
+ proc = subprocess.Popen(exec_, stderr=subprocess.PIPE, shell=True,
+ universal_newlines=True)
+ proc.wait()
+ stderr_output = proc.stderr.read()
+ return stderr_output
+
+
def run_tests(type_, test_verifier):
failures = []
print "Running '%s' tests ..." % type_
- for file_name in (x for x in os.listdir(os.path.join('tests', type_))
- if x.endswith('.py') and not x.startswith('_')):
- test_name = file_name[:-3]
- print '\t%s ...' % test_name,
-
- exec_ = './secure_python.exe ' + os.path.join('tests', type_, file_name)
- proc = subprocess.Popen(exec_, stderr=subprocess.PIPE, shell=True,
- universal_newlines=True)
- proc.wait()
- stderr_output = proc.stderr.read()
- if not test_verifier(test_name, stderr_output):
- print 'failed'
- failures.append(test_name)
- else:
- print 'passed'
+ for path_name in (x for x in os.listdir(os.path.join('tests', type_))
+ if not x.startswith('_') and not x.startswith('.')):
+ path = os.path.join('tests', type_, path_name)
+ if os.path.isfile(path):
+ if not path_name.endswith('.py'):
+ continue
+ test_name = path_name[:-3]
+ print '\t%s ...' % test_name,
+ stderr_output = exec_test(path)
+ if not test_verifier(test_name, stderr_output):
+ print 'failed'
+ failures.append(test_name)
+ else:
+ print 'passed'
+ elif os.path.isdir(path):
+ print '\t%s ...' % path_name,
+ module_name = 'tests.%s.%s.prep' % (type_, path_name)
+ module = __import__(module_name, fromlist=['set_up', 'tear_down'],
+ level=0)
+ module.set_up()
+ try:
+ stderr_output = exec_test(os.path.join(path, 'test.py'))
+ if not test_verifier(test_name, stderr_output):
+ print 'failed'
+ else:
+ print 'passed'
+ finally:
+ module.tear_down()
+
+
return failures
Added: python/branches/bcannon-objcap/tests/succeed/whitelist_fallthrough/__init__.py
==============================================================================
Added: python/branches/bcannon-objcap/tests/succeed/whitelist_fallthrough/prep.py
==============================================================================
--- (empty file)
+++ python/branches/bcannon-objcap/tests/succeed/whitelist_fallthrough/prep.py Wed May 30 06:16:13 2007
@@ -0,0 +1,12 @@
+from __future__ import with_statement
+
+import os
+
+file_name = os.path.join('Lib', 'sys.py')
+
+def set_up():
+ with open(file_name, 'w') as py_file:
+ py_file.write('test_attr = 42')
+
+def tear_down():
+ os.unlink(file_name)
Added: python/branches/bcannon-objcap/tests/succeed/whitelist_fallthrough/test.py
==============================================================================
--- (empty file)
+++ python/branches/bcannon-objcap/tests/succeed/whitelist_fallthrough/test.py Wed May 30 06:16:13 2007
@@ -0,0 +1,4 @@
+"""Importing a module that has the same name as one that is supposed to be
+blocked (e.g., sys) should be okay."""
+import sys
+sys.test_attr
More information about the Python-checkins
mailing list