Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:param checktype: The type of checks to run
:param nosec_lines: Lines which should be skipped because of nosec
:return: a score based on the number and type of test results
'''
scores = {
'SEVERITY': [0] * len(constants.RANKING),
'CONFIDENCE': [0] * len(constants.RANKING)
}
tests = self.testset.get_tests(checktype)
for test in tests:
name = test.__name__
# execute test with the an instance of the context class
temp_context = copy.copy(raw_context)
context = b_context.Context(temp_context)
try:
if hasattr(test, '_config'):
result = test(context, test._config)
else:
result = test(context)
# if we have a result, record it and update scores
if (result is not None and
result.lineno not in self.nosec_lines and
temp_context['lineno'] not in self.nosec_lines):
if isinstance(temp_context['filename'], bytes):
result.fname = temp_context['filename'].decode('utf-8')
else:
result.fname = temp_context['filename']
def test_call_function_name(self):
expected_string = 'spam'
ref_context = dict(name=expected_string)
new_context = context.Context(context_object=ref_context)
self.assertEqual(expected_string, new_context.call_function_name)
new_context = context.Context()
self.assertIsNone(new_context.call_function_name)
def test_string_val(self):
expected_string = 'spam'
ref_context = dict(str=expected_string)
new_context = context.Context(context_object=ref_context)
self.assertEqual(expected_string, new_context.string_val)
new_context = context.Context()
self.assertIsNone(new_context.string_val)
def test_is_module_being_imported(self):
ref_context = dict(module='spam')
new_context = context.Context(context_object=ref_context)
self.assertTrue(new_context.is_module_being_imported('spam'))
self.assertFalse(new_context.is_module_being_imported('eggs'))
new_context = context.Context()
self.assertFalse(new_context.is_module_being_imported('spam'))
def test_statement(self):
expected_string = 'spam'
ref_context = dict(statement=expected_string)
new_context = context.Context(context_object=ref_context)
self.assertEqual(expected_string, new_context.statement)
new_context = context.Context()
self.assertIsNone(new_context.statement)
def test_call_function_name_qual(self):
expected_string = 'spam'
ref_context = dict(qualname=expected_string)
new_context = context.Context(context_object=ref_context)
self.assertEqual(expected_string, new_context.call_function_name_qual)
new_context = context.Context()
self.assertIsNone(new_context.call_function_name_qual)
def test_check_call_arg_value(self, call_keywords):
new_context = context.Context()
call_keywords.return_value = dict(spam='eggs')
self.assertTrue(new_context.check_call_arg_value('spam', 'eggs'))
self.assertTrue(new_context.check_call_arg_value('spam',
['spam', 'eggs']))
self.assertFalse(new_context.check_call_arg_value('spam', 'spam'))
self.assertFalse(new_context.check_call_arg_value('spam'))
self.assertFalse(new_context.check_call_arg_value('eggs'))
new_context = context.Context()
self.assertIsNone(new_context.check_call_arg_value(None))
def test_function_def_defaults_qual(self, get_qual_attr):
get_qual_attr.return_value = 'spam'
ref_node = mock.Mock(args=mock.Mock(defaults=['spam']))
ref_context = dict(node=ref_node, import_aliases=None)
new_context = context.Context(context_object=ref_context)
self.assertListEqual(['spam'], new_context.function_def_defaults_qual)
ref_node = mock.Mock(args=mock.Mock(defaults=[]))
ref_context = dict(node=ref_node, import_aliases=None)
new_context = context.Context(context_object=ref_context)
self.assertListEqual([], new_context.function_def_defaults_qual)
new_context = context.Context()
self.assertListEqual([], new_context.function_def_defaults_qual)
def test_function_def_defaults_qual(self, get_qual_attr):
get_qual_attr.return_value = 'spam'
ref_node = mock.Mock(args=mock.Mock(defaults=['spam']))
ref_context = dict(node=ref_node, import_aliases=None)
new_context = context.Context(context_object=ref_context)
self.assertListEqual(['spam'], new_context.function_def_defaults_qual)
ref_node = mock.Mock(args=mock.Mock(defaults=[]))
ref_context = dict(node=ref_node, import_aliases=None)
new_context = context.Context(context_object=ref_context)
self.assertListEqual([], new_context.function_def_defaults_qual)
new_context = context.Context()
self.assertListEqual([], new_context.function_def_defaults_qual)
def test_call_args_count(self):
ref_call = mock.Mock()
ref_call.args = ['spam', 'eggs']
ref_context = dict(call=ref_call)
new_context = context.Context(context_object=ref_context)
self.assertEqual(len(ref_call.args), new_context.call_args_count)
ref_context = dict(call={})
new_context = context.Context(context_object=ref_context)
self.assertIsNone(new_context.call_args_count)
new_context = context.Context()
self.assertIsNone(new_context.call_args_count)