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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
#
# Copyright 2014 Hewlett-Packard Development Company, L.P.
# Copyright 2015 Nebula, Inc.
#
# SPDX-License-Identifier: Apache-2.0
import ast
import os
import shutil
import sys
import tempfile
import testtools
from bandit.core import utils as b_utils
def _touch(path):
"""Create an empty file at ``path``."""
open(path, "w").close()
class UtilTests(testtools.TestCase):
"""This set of tests exercises bandit.core.util functions."""
def setUp(self):
super().setUp()
self._setup_get_module_qualname_from_path()
def _setup_get_module_qualname_from_path(self):
"""Setup a fake directory for testing get_module_qualname_from_path().
Create temporary directory and then create fake .py files
within directory structure. We setup test cases for
a typical module, a path misssing a middle __init__.py,
no __init__.py anywhere in path, symlinking .py files.
"""
self.tempdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tempdir)
self.reltempdir = os.path.relpath(self.tempdir)
# good/a/b/c/test_typical.py
os.makedirs(os.path.join(self.tempdir, "good", "a", "b", "c"), 0o755)
_touch(os.path.join(self.tempdir, "good", "__init__.py"))
_touch(os.path.join(self.tempdir, "good", "a", "__init__.py"))
_touch(os.path.join(self.tempdir, "good", "a", "b", "__init__.py"))
_touch(
os.path.join(self.tempdir, "good", "a", "b", "c", "__init__.py")
)
_touch(
os.path.join(
self.tempdir, "good", "a", "b", "c", "test_typical.py"
)
)
# missingmid/a/b/c/test_missingmid.py
os.makedirs(
os.path.join(self.tempdir, "missingmid", "a", "b", "c"), 0o755
)
_touch(os.path.join(self.tempdir, "missingmid", "__init__.py"))
# no missingmid/a/__init__.py
_touch(
os.path.join(self.tempdir, "missingmid", "a", "b", "__init__.py")
)
_touch(
os.path.join(
self.tempdir, "missingmid", "a", "b", "c", "__init__.py"
)
)
_touch(
os.path.join(
self.tempdir, "missingmid", "a", "b", "c", "test_missingmid.py"
)
)
# missingend/a/b/c/test_missingend.py
os.makedirs(
os.path.join(self.tempdir, "missingend", "a", "b", "c"), 0o755
)
_touch(os.path.join(self.tempdir, "missingend", "__init__.py"))
_touch(
os.path.join(self.tempdir, "missingend", "a", "b", "__init__.py")
)
# no missingend/a/b/c/__init__.py
_touch(
os.path.join(
self.tempdir, "missingend", "a", "b", "c", "test_missingend.py"
)
)
# syms/a/bsym/c/test_typical.py
os.makedirs(os.path.join(self.tempdir, "syms", "a"), 0o755)
_touch(os.path.join(self.tempdir, "syms", "__init__.py"))
_touch(os.path.join(self.tempdir, "syms", "a", "__init__.py"))
os.symlink(
os.path.join(self.tempdir, "good", "a", "b"),
os.path.join(self.tempdir, "syms", "a", "bsym"),
)
def test_get_module_qualname_from_path_abs_typical(self):
"""Test get_module_qualname_from_path with typical absolute paths."""
name = b_utils.get_module_qualname_from_path(
os.path.join(
self.tempdir, "good", "a", "b", "c", "test_typical.py"
)
)
self.assertEqual("good.a.b.c.test_typical", name)
def test_get_module_qualname_from_path_with_dot(self):
"""Test get_module_qualname_from_path with a "." ."""
name = b_utils.get_module_qualname_from_path(
os.path.join(".", "__init__.py")
)
self.assertEqual("__init__", name)
def test_get_module_qualname_from_path_abs_missingmid(self):
# Test get_module_qualname_from_path with missing module
# __init__.py
name = b_utils.get_module_qualname_from_path(
os.path.join(
self.tempdir, "missingmid", "a", "b", "c", "test_missingmid.py"
)
)
self.assertEqual("b.c.test_missingmid", name)
def test_get_module_qualname_from_path_abs_missingend(self):
# Test get_module_qualname_from_path with no __init__.py
# last dir'''
name = b_utils.get_module_qualname_from_path(
os.path.join(
self.tempdir, "missingend", "a", "b", "c", "test_missingend.py"
)
)
self.assertEqual("test_missingend", name)
def test_get_module_qualname_from_path_abs_syms(self):
"""Test get_module_qualname_from_path with symlink in path."""
name = b_utils.get_module_qualname_from_path(
os.path.join(
self.tempdir, "syms", "a", "bsym", "c", "test_typical.py"
)
)
self.assertEqual("syms.a.bsym.c.test_typical", name)
def test_get_module_qualname_from_path_rel_typical(self):
"""Test get_module_qualname_from_path with typical relative paths."""
name = b_utils.get_module_qualname_from_path(
os.path.join(
self.reltempdir, "good", "a", "b", "c", "test_typical.py"
)
)
self.assertEqual("good.a.b.c.test_typical", name)
def test_get_module_qualname_from_path_rel_missingmid(self):
# Test get_module_qualname_from_path with module __init__.py
# missing and relative paths
name = b_utils.get_module_qualname_from_path(
os.path.join(
self.reltempdir,
"missingmid",
"a",
"b",
"c",
"test_missingmid.py",
)
)
self.assertEqual("b.c.test_missingmid", name)
def test_get_module_qualname_from_path_rel_missingend(self):
# Test get_module_qualname_from_path with __init__.py missing from
# last dir and using relative paths
name = b_utils.get_module_qualname_from_path(
os.path.join(
self.reltempdir,
"missingend",
"a",
"b",
"c",
"test_missingend.py",
)
)
self.assertEqual("test_missingend", name)
def test_get_module_qualname_from_path_rel_syms(self):
"""Test get_module_qualname_from_path with symbolic relative paths."""
name = b_utils.get_module_qualname_from_path(
os.path.join(
self.reltempdir, "syms", "a", "bsym", "c", "test_typical.py"
)
)
self.assertEqual("syms.a.bsym.c.test_typical", name)
def test_get_module_qualname_from_path_sys(self):
"""Test get_module_qualname_from_path with system module paths."""
name = b_utils.get_module_qualname_from_path(os.__file__)
self.assertEqual("os", name)
# This will fail because of magic for os.path. Not sure how to fix.
# name = b_utils.get_module_qualname_from_path(os.path.__file__)
# self.assertEqual(name, 'os.path')
def test_get_module_qualname_from_path_invalid_path(self):
"""Test get_module_qualname_from_path with invalid path."""
name = b_utils.get_module_qualname_from_path("/a/b/c/d/e.py")
self.assertEqual("e", name)
def test_get_module_qualname_from_path_dir(self):
"""Test get_module_qualname_from_path with dir path."""
self.assertRaises(
b_utils.InvalidModulePath,
b_utils.get_module_qualname_from_path,
"/tmp/",
)
def test_namespace_path_join(self):
p = b_utils.namespace_path_join("base1.base2", "name")
self.assertEqual("base1.base2.name", p)
def test_namespace_path_split(self):
(head, tail) = b_utils.namespace_path_split("base1.base2.name")
self.assertEqual("base1.base2", head)
self.assertEqual("name", tail)
def test_get_call_name1(self):
"""Gets a qualified call name."""
tree = ast.parse("a.b.c.d(x,y)").body[0].value
name = b_utils.get_call_name(tree, {})
self.assertEqual("a.b.c.d", name)
def test_get_call_name2(self):
"""Gets qualified call name and resolves aliases."""
tree = ast.parse("a.b.c.d(x,y)").body[0].value
name = b_utils.get_call_name(tree, {"a": "alias.x.y"})
self.assertEqual("alias.x.y.b.c.d", name)
name = b_utils.get_call_name(tree, {"a.b": "alias.x.y"})
self.assertEqual("alias.x.y.c.d", name)
name = b_utils.get_call_name(tree, {"a.b.c.d": "alias.x.y"})
self.assertEqual("alias.x.y", name)
def test_get_call_name3(self):
"""Getting name for a complex call."""
tree = ast.parse("a.list[0](x,y)").body[0].value
name = b_utils._get_attr_qual_name(tree, {})
self.assertEqual("", name)
# TODO(ljfisher) At best we might be able to get:
# self.assertEqual(name, 'a.list[0]')
def test_linerange(self):
with open("./examples/jinja2_templating.py") as test_file:
tree = ast.parse(test_file.read())
# Check linerange returns corrent number of lines
line = tree.body[8]
lrange = b_utils.linerange(line)
# line 9 should be three lines long
self.assertEqual(3, len(lrange))
# the range should be the correct line numbers
self.assertEqual([11, 12, 13], list(lrange))
def test_path_for_function(self):
path = b_utils.get_path_for_function(b_utils.get_path_for_function)
self.assertEqual(path, b_utils.__file__)
def test_path_for_function_no_file(self):
self.assertIsNone(b_utils.get_path_for_function(sys.settrace))
def test_path_for_function_no_module(self):
self.assertIsNone(b_utils.get_path_for_function(1))
def test_escaped_representation_simple(self):
res = b_utils.escaped_bytes_representation(b"ascii")
self.assertEqual(res, b"ascii")
def test_escaped_representation_valid_not_printable(self):
res = b_utils.escaped_bytes_representation(b"\\u0000")
self.assertEqual(res, b"\\x00")
def test_escaped_representation_invalid(self):
res = b_utils.escaped_bytes_representation(b"\\uffff")
self.assertEqual(res, b"\\uffff")
def test_escaped_representation_mixed(self):
res = b_utils.escaped_bytes_representation(b"ascii\\u0000\\uffff")
self.assertEqual(res, b"ascii\\x00\\uffff")
def test_deepgetattr(self):
a = type("", (), {})
a.b = type("", (), {})
a.b.c = type("", (), {})
a.b.c.d = "deep value"
a.b.c.d2 = "deep value 2"
a.b.c.e = "a.b.c"
self.assertEqual("deep value", b_utils.deepgetattr(a.b.c, "d"))
self.assertEqual("deep value 2", b_utils.deepgetattr(a.b.c, "d2"))
self.assertEqual("a.b.c", b_utils.deepgetattr(a.b.c, "e"))
self.assertEqual("deep value", b_utils.deepgetattr(a, "b.c.d"))
self.assertEqual("deep value 2", b_utils.deepgetattr(a, "b.c.d2"))
self.assertRaises(AttributeError, b_utils.deepgetattr, a.b, "z")
def test_parse_ini_file(self):
tests = [
{
"content": "[bandit]\nexclude=/abc,/def",
"expected": {"exclude": "/abc,/def"},
},
{"content": "[Blabla]\nsomething=something", "expected": None},
]
with tempfile.NamedTemporaryFile("r+") as t:
for test in tests:
with open(t.name, "w") as f:
f.write(test["content"])
self.assertEqual(
b_utils.parse_ini_file(t.name), test["expected"]
)
def test_check_ast_node_good(self):
node = b_utils.check_ast_node("Call")
self.assertEqual("Call", node)
def test_check_ast_node_bad_node(self):
self.assertRaises(TypeError, b_utils.check_ast_node, "Derp")
def test_check_ast_node_bad_type(self):
self.assertRaises(TypeError, b_utils.check_ast_node, "walk")
|