Clean up tests and examples for Python 3.4

This addresses some of the feedback in
I71c0bb61c9ee0bf1b751a719a4eb95bf7a0b4943

Change-Id: Ie916ebda1ddff6de528a495bf249d40e7b1e7fc5
This commit is contained in:
Ian Cordasco 2015-06-03 16:45:52 -05:00
parent 6279a2f984
commit 22e48756a6
6 changed files with 51 additions and 15 deletions

View File

@ -16,6 +16,8 @@
import _ast
import six
from bandit.core import utils
@ -184,10 +186,6 @@ class Context():
elif isinstance(literal, _ast.Str):
return literal.s
# Python 3 only
# elif isinstance(literal, _ast.Bytes):
# return literal.s
elif isinstance(literal, _ast.List):
return_list = list()
for li in literal.elts:
@ -213,16 +211,19 @@ class Context():
# what do we want to do with this?
pass
# Python 3 only
# elif isinstance(literal, _ast.NameConstant):
# return literal.value
elif isinstance(literal, _ast.Name):
return literal.id
elif hasattr(literal, 'value'):
# NOTE(sigmavirus24): NameConstants are only part of the AST in Python
# 3. NameConstants tend to refer to things like True and False. This
# prevents them from being re-assigned in Python 3.
elif six.PY3 and isinstance(literal, _ast.NameConstant):
return str(literal.value)
# NOTE(sigmavirus24): Bytes are only part of the AST in Python 3
elif six.PY3 and isinstance(literal, _ast.Bytes):
return literal.s
else:
return None

1
examples/exec-py3.py Normal file
View File

@ -0,0 +1 @@
exec("do evil")

17
examples/os-chmod-py3.py Normal file
View File

@ -0,0 +1,17 @@
import os
import stat
keyfile = 'foo'
os.chmod('/etc/passwd', 0o227)
os.chmod('/etc/passwd', 0o7)
os.chmod('/etc/passwd', 0o664)
os.chmod('/etc/passwd', 0o777)
os.chmod('/etc/passwd', 0o770)
os.chmod('/etc/passwd', 0o776)
os.chmod('/etc/passwd', 0o760)
os.chmod('~/.bashrc', 511)
os.chmod('/etc/hosts', 0o777)
os.chmod('/tmp/oh_hai', 0x1ff)
os.chmod('/etc/passwd', stat.S_IRWXU)
os.chmod(key_file, 0o777)

View File

@ -19,6 +19,8 @@ import os
import unittest
import inspect
import six
from bandit.core import constants as C
from bandit.core import manager as b_manager
from bandit.core import test_set as b_test_set
@ -100,8 +102,14 @@ class FunctionalTests(unittest.TestCase):
def test_exec(self):
'''Test the `exec` example.'''
expect = {'SEVERITY': {'MEDIUM': 2}, 'CONFIDENCE': {'HIGH': 2}}
self.check_example('exec.py', expect)
filename = 'exec-{}.py'
if six.PY2:
filename = filename.format('py2')
expect = {'SEVERITY': {'MEDIUM': 2}, 'CONFIDENCE': {'HIGH': 2}}
else:
filename = filename.format('py3')
expect = {'SEVERITY': {'MEDIUM': 1}, 'CONFIDENCE': {'HIGH': 1}}
self.check_example(filename, expect)
def test_exec_as_root(self):
'''Test for the `run_as_root=True` keyword argument.'''
@ -173,10 +181,19 @@ class FunctionalTests(unittest.TestCase):
def test_os_chmod(self):
'''Test setting file permissions.'''
expect = {
'SEVERITY': {'MEDIUM': 2, 'HIGH': 9},
'CONFIDENCE': {'HIGH': 10, 'MEDIUM': 1}
}
filename = 'os-chmod-{}.py'
if six.PY2:
filename = filename.format('py2')
expect = {
'SEVERITY': {'MEDIUM': 2, 'HIGH': 9},
'CONFIDENCE': {'HIGH': 10, 'MEDIUM': 1}
}
else:
filename = filename.format('py3')
expect = {
'SEVERITY': {'MEDIUM': 2, 'HIGH': 9},
'CONFIDENCE': {'HIGH': 10, 'MEDIUM': 1}
}
self.check_example('os-chmod.py', expect)
def test_os_exec(self):