forked from pixee/codemodder-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_codeql.py
More file actions
89 lines (84 loc) · 3.24 KB
/
Copy pathtest_codeql.py
File metadata and controls
89 lines (84 loc) · 3.24 KB
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
import json
from pathlib import Path
from unittest import TestCase, mock
from codemodder.codeql import CodeQLResultSet
class TestCodeQLResultSet(TestCase):
def test_from_sarif(self):
# Given a SARIF file with known content
sarif_content = {
"runs": [
{
"tool": {
"driver": {"name": "CodeQL"},
"extensions": [{"rules": [{"id": "python/sql-injection"}]}],
},
"results": [
{
"ruleId": "python/sql-injection",
"message": {"text": "Possible SQL injection"},
"locations": [
{
"physicalLocation": {
"artifactLocation": {"uri": "example.py"},
"region": {
"startLine": 10,
"startColumn": 5,
"endLine": 10,
"endColumn": 20,
},
}
}
],
"rule": {
"toolComponent": {"index": 0},
"index": 0,
},
}
],
}
]
}
sarif_file = Path("/path/to/sarif/file.sarif")
with mock.patch(
"builtins.open", mock.mock_open(read_data=json.dumps(sarif_content))
):
# When parsing the SARIF file
result_set = CodeQLResultSet.from_sarif(sarif_file)
# Then the result set should contain the expected results
self.assertEqual(len(result_set), 1)
self.assertIn("python/sql-injection", result_set)
self.assertEqual(len(result_set["python/sql-injection"]), 1)
self.assertEqual(
result_set["python/sql-injection"][Path("example.py")][0].rule_id,
"python/sql-injection",
)
self.assertEqual(
result_set["python/sql-injection"][Path("example.py")][0]
.locations[0]
.file,
Path("example.py"),
)
self.assertEqual(
result_set["python/sql-injection"][Path("example.py")][0]
.locations[0]
.start.line,
10,
)
self.assertEqual(
result_set["python/sql-injection"][Path("example.py")][0]
.locations[0]
.start.column,
5,
)
self.assertEqual(
result_set["python/sql-injection"][Path("example.py")][0]
.locations[0]
.end.line,
10,
)
self.assertEqual(
result_set["python/sql-injection"][Path("example.py")][0]
.locations[0]
.end.column,
20,
)