From cf9443a828ef5cf7238432e1f0bfc772611cc061 Mon Sep 17 00:00:00 2001 From: Eric Brown Date: Mon, 13 Nov 2023 20:04:04 -0800 Subject: [PATCH] Auto generate pickle tests (#158) Signed-off-by: Eric Brown --- .../stdlib/pickle/examples/pickle_load.py | 5 ++ .../stdlib/pickle/examples/pickle_loads.py | 5 ++ .../pickle/examples/pickle_unpickler.py | 5 ++ .../python/stdlib/pickle/test_pickle_load.py | 62 +++++-------------- 4 files changed, 30 insertions(+), 47 deletions(-) diff --git a/tests/unit/rules/python/stdlib/pickle/examples/pickle_load.py b/tests/unit/rules/python/stdlib/pickle/examples/pickle_load.py index 913b5572..52fdf2d2 100644 --- a/tests/unit/rules/python/stdlib/pickle/examples/pickle_load.py +++ b/tests/unit/rules/python/stdlib/pickle/examples/pickle_load.py @@ -1,3 +1,8 @@ +# level: WARNING +# start_line: 11 +# end_line: 11 +# start_column: 10 +# end_column: 21 import pickle diff --git a/tests/unit/rules/python/stdlib/pickle/examples/pickle_loads.py b/tests/unit/rules/python/stdlib/pickle/examples/pickle_loads.py index 391167ae..a678b934 100644 --- a/tests/unit/rules/python/stdlib/pickle/examples/pickle_loads.py +++ b/tests/unit/rules/python/stdlib/pickle/examples/pickle_loads.py @@ -1,3 +1,8 @@ +# level: WARNING +# start_line: 14 +# end_line: 14 +# start_column: 10 +# end_column: 22 import pickle diff --git a/tests/unit/rules/python/stdlib/pickle/examples/pickle_unpickler.py b/tests/unit/rules/python/stdlib/pickle/examples/pickle_unpickler.py index 6bf7e307..d0da9693 100644 --- a/tests/unit/rules/python/stdlib/pickle/examples/pickle_unpickler.py +++ b/tests/unit/rules/python/stdlib/pickle/examples/pickle_unpickler.py @@ -1,3 +1,8 @@ +# level: WARNING +# start_line: 11 +# end_line: 11 +# start_column: 10 +# end_column: 26 import pickle diff --git a/tests/unit/rules/python/stdlib/pickle/test_pickle_load.py b/tests/unit/rules/python/stdlib/pickle/test_pickle_load.py index c337117a..264573dd 100644 --- a/tests/unit/rules/python/stdlib/pickle/test_pickle_load.py +++ b/tests/unit/rules/python/stdlib/pickle/test_pickle_load.py @@ -1,17 +1,17 @@ # Copyright 2023 Secure Saurce LLC import os +from parameterized import parameterized + from precli.core.level import Level from precli.rules import Rule from tests.unit.rules.python import test_case -RULE_ID = "PRE0012" - - class PickleLoadTests(test_case.TestCase): def setUp(self): super().setUp() + self.rule_id = "PRE0012" self.base_path = os.path.join( "tests", "unit", @@ -23,55 +23,23 @@ def setUp(self): ) def test_pickle_load_rule_meta(self): - rule = Rule.get_by_id(RULE_ID) - self.assertEqual(RULE_ID, rule.id) + rule = Rule.get_by_id(self.rule_id) + self.assertEqual(self.rule_id, rule.id) self.assertEqual("deserialization_of_untrusted_data", rule.name) self.assertEqual( - f"https://docs.securesauce.dev/rules/{RULE_ID}", rule.help_url + f"https://docs.securesauce.dev/rules/{self.rule_id}", rule.help_url ) self.assertEqual(True, rule.default_config.enabled) self.assertEqual(Level.WARNING, rule.default_config.level) self.assertEqual(-1.0, rule.default_config.rank) self.assertEqual("502", rule.cwe.cwe_id) - def test_pickle_load(self): - results = self.parser.parse( - os.path.join(self.base_path, "pickle_load.py") - ) - self.assertEqual(1, len(results)) - result = results[0] - self.assertEqual(RULE_ID, result.rule_id) - self.assertEqual(6, result.location.start_line) - self.assertEqual(6, result.location.end_line) - self.assertEqual(10, result.location.start_column) - self.assertEqual(21, result.location.end_column) - self.assertEqual(Level.WARNING, result.level) - self.assertEqual(-1.0, result.rank) - - def test_pickle_loads(self): - results = self.parser.parse( - os.path.join(self.base_path, "pickle_loads.py") - ) - self.assertEqual(1, len(results)) - result = results[0] - self.assertEqual(RULE_ID, result.rule_id) - self.assertEqual(9, result.location.start_line) - self.assertEqual(9, result.location.end_line) - self.assertEqual(10, result.location.start_column) - self.assertEqual(22, result.location.end_column) - self.assertEqual(Level.WARNING, result.level) - self.assertEqual(-1.0, result.rank) - - def test_pickle_loads(self): - results = self.parser.parse( - os.path.join(self.base_path, "pickle_unpickler.py") - ) - self.assertEqual(1, len(results)) - result = results[0] - self.assertEqual(RULE_ID, result.rule_id) - self.assertEqual(6, result.location.start_line) - self.assertEqual(6, result.location.end_line) - self.assertEqual(10, result.location.start_column) - self.assertEqual(26, result.location.end_column) - self.assertEqual(Level.WARNING, result.level) - self.assertEqual(-1.0, result.rank) + @parameterized.expand( + [ + "pickle_load", + "pickle_loads", + "pickle_unpickler", + ] + ) + def test(self, filename): + self.check(filename)