Skip to content

Commit

Permalink
Use better test generation for cryptography (#150)
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 8f2b14a commit a4e781d
Show file tree
Hide file tree
Showing 69 changed files with 279 additions and 661 deletions.
46 changes: 46 additions & 0 deletions tests/unit/rules/python/test_case.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
# Copyright 2023 Secure Saurce LLC
import os

import testtools

from precli.core.level import Level
from precli.parsers import python


class TestCase(testtools.TestCase):
def setUp(self):
super().setUp()
self.parser = python.Python()

def expected(self, filename):
with open(os.path.join(self.base_path, f"{filename}.py")) as f:
level = f.readline().strip()
level = level.removeprefix("# level: ")
level = getattr(Level, level)
if level != Level.NONE:
start_line = f.readline().strip()
start_line = int(start_line.removeprefix("# start_line: "))
end_line = f.readline().strip()
end_line = int(end_line.removeprefix("# end_line: "))
start_col = f.readline().strip()
start_col = int(start_col.removeprefix("# start_column: "))
end_col = f.readline().strip()
end_col = int(end_col.removeprefix("# end_column: "))
else:
start_line = end_line = start_col = end_col = -1

return (level, start_line, end_line, start_col, end_col)

def check(self, filename):
(
level,
start_line,
end_line,
start_column,
end_column,
) = self.expected(filename)
results = self.parser.parse(
os.path.join(self.base_path, f"{filename}.py")
)
if level == Level.NONE:
self.assertEqual(0, len(results))
else:
self.assertEqual(1, len(results))
result = results[0]
self.assertEqual(self.rule_id, result.rule_id)
self.assertEqual(start_line, result.location.start_line)
self.assertEqual(end_line, result.location.end_line)
self.assertEqual(start_column, result.location.start_column)
self.assertEqual(end_column, result.location.end_column)
self.assertEqual(level, result.level)
self.assertEqual(-1.0, result.rank)
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: ERROR
# start_line: 9
# end_line: 9
# start_column: 24
# end_column: 28
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: ERROR
# start_line: 9
# end_line: 9
# start_column: 33
# end_column: 37
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: ERROR
# start_line: 10
# end_line: 10
# start_column: 24
# end_column: 31
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: ERROR
# start_line: 9
# end_line: 9
# start_column: 25
# end_column: 29
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: ERROR
# start_line: 9
# end_line: 9
# start_column: 34
# end_column: 38
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: ERROR
# start_line: 10
# end_line: 10
# start_column: 25
# end_column: 32
from cryptography.hazmat.primitives.asymmetric import dsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: WARNING
# start_line: 11
# end_line: 11
# start_column: 37
# end_column: 42
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: WARNING
# start_line: 11
# end_line: 11
# start_column: 37
# end_column: 42
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: WARNING
# start_line: 11
# end_line: 11
# start_column: 37
# end_column: 42
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


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: 27
# end_column: 36
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


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: 27
# end_column: 36
from cryptography.hazmat.primitives.asymmetric import ec


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: 27
# end_column: 36
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import ec


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: ERROR
# start_line: 9
# end_line: 9
# start_column: 32
# end_column: 36
from cryptography.hazmat.primitives.asymmetric import rsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import rsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import rsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# level: ERROR
# start_line: 9
# end_line: 9
# start_column: 57
# end_column: 61
from cryptography.hazmat.primitives.asymmetric import rsa


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# level: NONE
from cryptography.hazmat.primitives.asymmetric import rsa


Expand Down
Loading

0 comments on commit a4e781d

Please sign in to comment.