1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
# Copyright (c) 2015 VMware, Inc.
#
# SPDX-License-Identifier: Apache-2.0
import os
import subprocess
import testtools
class RuntimeTests(testtools.TestCase):
def _test_runtime(self, cmdlist, infile=None):
process = subprocess.Popen(
cmdlist,
stdin=infile if infile else subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
close_fds=True,
)
stdout, stderr = process.communicate()
retcode = process.poll()
return (retcode, stdout.decode("utf-8"))
def _test_example(self, cmdlist, targets):
for t in targets:
cmdlist.append(os.path.join(os.getcwd(), "examples", t))
return self._test_runtime(cmdlist)
def test_no_arguments(self):
(retcode, output) = self._test_runtime(
[
"bandit",
]
)
self.assertEqual(2, retcode)
self.assertIn("usage: bandit [-h]", output)
def test_piped_input(self):
with open("examples/imports.py") as infile:
(retcode, output) = self._test_runtime(["bandit", "-"], infile)
self.assertEqual(1, retcode)
self.assertIn("Total lines of code: 4", output)
self.assertIn("Low: 2", output)
self.assertIn("High: 2", output)
self.assertIn("Files skipped (0):", output)
self.assertIn("Issue: [B403:blacklist] Consider possible", output)
self.assertIn("<stdin>:2", output)
self.assertIn("<stdin>:4", output)
def test_nonexistent_config(self):
(retcode, output) = self._test_runtime(
["bandit", "-c", "nonexistent.yml", "xx.py"]
)
self.assertEqual(2, retcode)
self.assertIn("nonexistent.yml : Could not read config file.", output)
def test_help_arg(self):
(retcode, output) = self._test_runtime(["bandit", "-h"])
self.assertEqual(0, retcode)
self.assertIn(
"Bandit - a Python source code security analyzer", output
)
self.assertIn("usage: bandit [-h]", output)
self.assertIn("positional arguments:", output)
self.assertIn("tests were discovered and loaded:", output)
# test examples (use _test_example() to wrap in config location argument
def test_example_nonexistent(self):
(retcode, output) = self._test_example(
[
"bandit",
],
[
"nonexistent.py",
],
)
self.assertEqual(0, retcode)
self.assertIn("Files skipped (1):", output)
self.assertIn("nonexistent.py (No such file or directory", output)
def test_example_okay(self):
(retcode, output) = self._test_example(
[
"bandit",
],
[
"okay.py",
],
)
self.assertEqual(0, retcode)
self.assertIn("Total lines of code: 1", output)
self.assertIn("Files skipped (0):", output)
self.assertIn("No issues identified.", output)
def test_example_nonsense(self):
(retcode, output) = self._test_example(
[
"bandit",
],
[
"nonsense.py",
],
)
self.assertEqual(0, retcode)
self.assertIn("Files skipped (1):", output)
self.assertIn("nonsense.py (syntax error while parsing AST", output)
def test_example_nonsense2(self):
(retcode, output) = self._test_example(
[
"bandit",
],
[
"nonsense2.py",
],
)
self.assertEqual(0, retcode)
self.assertIn("Files skipped (1):", output)
self.assertIn("nonsense2.py (syntax error while parsing AST", output)
def test_example_imports(self):
(retcode, output) = self._test_example(
[
"bandit",
],
[
"imports.py",
],
)
self.assertEqual(1, retcode)
self.assertIn("Total lines of code: 4", output)
self.assertIn("Low: 2", output)
self.assertIn("High: 2", output)
self.assertIn("Files skipped (0):", output)
self.assertIn("Issue: [B403:blacklist] Consider possible", output)
self.assertIn("imports.py:2", output)
self.assertIn("imports.py:4", output)
|