| 1 |
from sarif import * |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
@pytest.fixture(scope='function', autouse=True) |
| 6 |
def sarif(): |
| 7 |
return sarif_from_env() |
| 8 |
|
| 9 |
final_line_num = 34 |
| 10 |
|
| 11 |
line_num_call_to_PyList_New = final_line_num - 7; |
| 12 |
line_num_for_loop = final_line_num - 5; |
| 13 |
line_num_call_to_PyList_Append = final_line_num - 3; |
| 14 |
|
| 15 |
expected_file_name = 'test-warning-with-path.c' |
| 16 |
|
| 17 |
def test_sarif_output_for_warning_with_path(sarif): |
| 18 |
schema = sarif['$schema'] |
| 19 |
assert schema == 'https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json' |
| 20 |
|
| 21 |
version = sarif['version'] |
| 22 |
assert version == '2.1.0' |
| 23 |
|
| 24 |
runs = sarif['runs'] |
| 25 |
run = runs[0] |
| 26 |
|
| 27 |
tool = run['tool'] |
| 28 |
assert tool['driver']['name'] == expected_file_name + '.exe' |
| 29 |
|
| 30 |
results = run['results'] |
| 31 |
assert len(results) == 1 |
| 32 |
|
| 33 |
result = results[0] |
| 34 |
assert result['ruleId'] == 'warning' |
| 35 |
assert result['level'] == 'warning' |
| 36 |
assert result['message']['text'] \ |
| 37 |
== "passing NULL as argument 1 to 'PyList_Append' which requires a non-NULL parameter" |
| 38 |
assert len(result['locations']) == 1 |
| 39 |
location = result['locations'][0] |
| 40 |
|
| 41 |
phys_loc = location['physicalLocation'] |
| 42 |
assert phys_loc['artifactLocation']['uri'].endswith(expected_file_name) |
| 43 |
assert phys_loc['region']['startLine'] \ |
| 44 |
== line_num_call_to_PyList_Append |
| 45 |
assert phys_loc['region']['startColumn'] == 5 |
| 46 |
assert phys_loc['region']['endColumn'] == 30 |
| 47 |
assert phys_loc['contextRegion']['startLine'] \ |
| 48 |
== line_num_call_to_PyList_Append |
| 49 |
assert phys_loc['contextRegion']['snippet']['text'] \ |
| 50 |
== ' PyList_Append(list, item);\n' |
| 51 |
|
| 52 |
assert len(location['logicalLocations']) == 1 |
| 53 |
logical_loc = location['logicalLocations'][0] |
| 54 |
assert logical_loc['name'] == 'make_a_list_of_random_ints_badly' |
| 55 |
assert logical_loc['fullyQualifiedName'] == 'make_a_list_of_random_ints_badly' |
| 56 |
assert logical_loc['decoratedName'] == 'make_a_list_of_random_ints_badly' |
| 57 |
assert logical_loc['kind'] == 'function' |
| 58 |
|
| 59 |
assert len(result['codeFlows']) == 1 |
| 60 |
assert len(result['codeFlows'][0]['threadFlows']) == 1 |
| 61 |
thread_flow = result['codeFlows'][0]['threadFlows'][0] |
| 62 |
|
| 63 |
assert len(thread_flow['locations']) == 3 |
| 64 |
|
| 65 |
tfl_0 = thread_flow['locations'][0] |
| 66 |
tfl_0_loc = tfl_0['location'] |
| 67 |
assert get_location_artifact_uri(tfl_0_loc).endswith(expected_file_name) |
| 68 |
assert get_location_physical_region(tfl_0_loc)['startLine'] \ |
| 69 |
== line_num_call_to_PyList_New |
| 70 |
assert get_location_physical_region(tfl_0_loc)['startColumn'] == 10 |
| 71 |
assert get_location_physical_region(tfl_0_loc)['endColumn'] == 23 |
| 72 |
assert get_location_snippet_text(tfl_0_loc) \ |
| 73 |
== ' list = PyList_New(0);\n' |
| 74 |
assert tfl_0_loc['logicalLocations'] == location['logicalLocations'] |
| 75 |
assert tfl_0_loc['message']['text'] \ |
| 76 |
== "when 'PyList_New' fails, returning NULL" |
| 77 |
assert tfl_0['nestingLevel'] == 0 |
| 78 |
assert tfl_0['executionOrder'] == 1 |
| 79 |
|
| 80 |
tfl_1 = thread_flow['locations'][1] |
| 81 |
tfl_1_loc = tfl_1['location'] |
| 82 |
assert get_location_artifact_uri(tfl_1_loc).endswith(expected_file_name) |
| 83 |
assert get_location_physical_region(tfl_1_loc)['startLine'] \ |
| 84 |
== line_num_for_loop |
| 85 |
assert get_location_physical_region(tfl_1_loc)['startColumn'] == 15 |
| 86 |
assert get_location_physical_region(tfl_1_loc)['endColumn'] == 24 |
| 87 |
assert get_location_snippet_text(tfl_1_loc) \ |
| 88 |
== ' for (i = 0; i < count; i++) {\n' |
| 89 |
assert tfl_1_loc['logicalLocations'] == location['logicalLocations'] |
| 90 |
assert tfl_1_loc['message']['text'] \ |
| 91 |
== "when 'i < count'" |
| 92 |
assert tfl_1['nestingLevel'] == 0 |
| 93 |
assert tfl_1['executionOrder'] == 2 |
| 94 |
|
| 95 |
tfl_2 = thread_flow['locations'][2] |
| 96 |
tfl_2_loc = tfl_2['location'] |
| 97 |
assert get_location_artifact_uri(tfl_2_loc).endswith(expected_file_name) |
| 98 |
assert get_location_physical_region(tfl_2_loc)['startLine'] \ |
| 99 |
== line_num_call_to_PyList_Append |
| 100 |
assert get_location_physical_region(tfl_2_loc)['startColumn'] == 5 |
| 101 |
assert get_location_physical_region(tfl_2_loc)['endColumn'] == 30 |
| 102 |
assert get_location_snippet_text(tfl_2_loc) \ |
| 103 |
== ' PyList_Append(list, item);\n' |
| 104 |
assert tfl_2_loc['logicalLocations'] == location['logicalLocations'] |
| 105 |
assert tfl_2_loc['message']['text'] \ |
| 106 |
== "when calling 'PyList_Append', passing NULL from (1) as argument 1" |
| 107 |
assert tfl_2['nestingLevel'] == 0 |
| 108 |
assert tfl_2['executionOrder'] == 3 |