Skip to content

Commit

Permalink
Auto generate crypt tests (#152)
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Brown <[email protected]>
  • Loading branch information
ericwb authored Nov 14, 2023
1 parent 573b07c commit ab222d0
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: WARNING
# start_line: 9
# end_line: 9
# start_column: 0
# end_column: 11
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: WARNING
# start_line: 9
# end_line: 9
# start_column: 0
# end_column: 11
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: WARNING
# start_line: 9
# end_line: 9
# start_column: 0
# end_column: 12
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: WARNING
# start_line: 9
# end_line: 9
# start_column: 0
# end_column: 12
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
import crypt


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
import crypt


Expand Down
133 changes: 24 additions & 109 deletions tests/unit/rules/python/stdlib/crypt/test_crypt_weak_hash.py
Original file line number Diff line number Diff line change
@@ -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 = "PRE0002"


class CryptWeakHashTests(test_case.TestCase):
def setUp(self):
super().setUp()
self.rule_id = "PRE0002"
self.base_path = os.path.join(
"tests",
"unit",
Expand All @@ -23,117 +23,32 @@ def setUp(self):
)

def test_crypt_weak_hash_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("reversible_one_way_hash", 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("328", rule.cwe.cwe_id)

def test_crypt_crypt(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_crypt.py")
)
self.assertEqual(0, len(results))

def test_crypt_crypt_method_blowfish(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_crypt_method_blowfish.py")
)
self.assertEqual(0, len(results))

def test_crypt_crypt_method_crypt(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_crypt_method_crypt.py")
)
self.assertEqual(1, len(results))
result = results[0]
self.assertEqual(RULE_ID, result.rule_id)
self.assertEqual(4, result.location.start_line)
self.assertEqual(4, result.location.end_line)
self.assertEqual(0, result.location.start_column)
self.assertEqual(11, result.location.end_column)
self.assertEqual(Level.WARNING, result.level)
self.assertEqual(-1.0, result.rank)

def test_crypt_crypt_method_md5(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_crypt_method_md5.py")
)
self.assertEqual(1, len(results))
result = results[0]
self.assertEqual(RULE_ID, result.rule_id)
self.assertEqual(4, result.location.start_line)
self.assertEqual(4, result.location.end_line)
self.assertEqual(0, result.location.start_column)
self.assertEqual(11, result.location.end_column)
self.assertEqual(Level.WARNING, result.level)
self.assertEqual(-1.0, result.rank)

def test_crypt_crypt_method_sha256(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_crypt_method_sha256.py")
)
self.assertEqual(0, len(results))

def test_crypt_crypt_method_sha512(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_crypt_method_sha512.py")
)
self.assertEqual(0, len(results))

def test_crypt_mksalt(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_mksalt.py")
)
self.assertEqual(0, len(results))

def test_crypt_mksalt_method_blowfish(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_mksalt_method_blowfish.py")
)
self.assertEqual(0, len(results))

def test_crypt_mksalt_method_crypt(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_mksalt_method_crypt.py")
)
self.assertEqual(1, len(results))
result = results[0]
self.assertEqual(RULE_ID, result.rule_id)
self.assertEqual(4, result.location.start_line)
self.assertEqual(4, result.location.end_line)
self.assertEqual(0, result.location.start_column)
self.assertEqual(12, result.location.end_column)
self.assertEqual(Level.WARNING, result.level)
self.assertEqual(-1.0, result.rank)

def test_crypt_mksalt_method_md5(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_mksalt_method_md5.py")
)
self.assertEqual(1, len(results))
result = results[0]
self.assertEqual(RULE_ID, result.rule_id)
self.assertEqual(4, result.location.start_line)
self.assertEqual(4, result.location.end_line)
self.assertEqual(0, result.location.start_column)
self.assertEqual(12, result.location.end_column)
self.assertEqual(Level.WARNING, result.level)
self.assertEqual(-1.0, result.rank)

def test_crypt_mksalt_method_sha256(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_mksalt_method_sha256.py")
)
self.assertEqual(0, len(results))

def test_crypt_mksalt_method_sha512(self):
results = self.parser.parse(
os.path.join(self.base_path, "crypt_mksalt_method_sha512.py")
)
self.assertEqual(0, len(results))
@parameterized.expand(
[
"crypt_crypt",
"crypt_crypt_method_blowfish",
"crypt_crypt_method_crypt",
"crypt_crypt_method_md5",
"crypt_crypt_method_sha256",
"crypt_crypt_method_sha512",
"crypt_mksalt",
"crypt_mksalt_method_blowfish",
"crypt_mksalt_method_crypt",
"crypt_mksalt_method_md5",
"crypt_mksalt_method_sha256",
"crypt_mksalt_method_sha512",
]
)
def test(self, filename):
self.check(filename)

0 comments on commit ab222d0

Please sign in to comment.