-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add main and constant check and tests (#7)
Signed-off-by: romanodanilo <[email protected]> Signed-off-by: romanodanilo <[email protected]>
- Loading branch information
1 parent
cd7db54
commit a5d0757
Showing
16 changed files
with
679 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
qc_otx/checks/core_checker/mandatory_constant_initialization.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import logging, os | ||
|
||
from typing import List | ||
|
||
from lxml import etree | ||
|
||
from qc_baselib import Result, IssueSeverity | ||
|
||
from qc_otx import constants | ||
from qc_otx.checks import models | ||
|
||
from qc_otx.checks.core_checker import core_constants | ||
|
||
|
||
def check_rule(checker_data: models.CheckerData) -> None: | ||
""" | ||
Implements core checker rule Core_Chk009 | ||
Criterion: Constant declarations shall always be initialized. | ||
Severity: Critical | ||
""" | ||
logging.info("Executing mandatory_constant_initialization check") | ||
|
||
issue_severity = IssueSeverity.ERROR | ||
|
||
rule_uid = checker_data.result.register_rule( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=core_constants.CHECKER_ID, | ||
emanating_entity="asam.net", | ||
standard="otx", | ||
definition_setting="1.0.0", | ||
rule_full_name="core.chk_009.mandatory_constant_initialization", | ||
) | ||
|
||
tree = checker_data.input_file_xml_root | ||
root = tree.getroot() | ||
# Use XPath to find all nodes constant | ||
constant_nodes = tree.xpath("//constant") | ||
|
||
for constant_node in constant_nodes: | ||
constant_name = constant_node.get("name") | ||
|
||
# Define the XPath expression for the sequence of children | ||
xpath_expr = ".//realisation/dataType/init" | ||
|
||
# Use XPath to find if the sequence exists | ||
is_valid = constant_node.xpath(xpath_expr) | ||
|
||
if not is_valid: | ||
current_xpath = tree.getpath(constant_node) | ||
issue_id = checker_data.result.register_issue( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=core_constants.CHECKER_ID, | ||
description="Issue flagging when a constant is not initialized", | ||
level=issue_severity, | ||
rule_uid=rule_uid, | ||
) | ||
|
||
checker_data.result.add_xml_location( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=core_constants.CHECKER_ID, | ||
issue_id=issue_id, | ||
xpath=current_xpath, | ||
description=f"Constant {constant_name} at {current_xpath} is not initialized", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging, os | ||
|
||
from typing import List | ||
|
||
from lxml import etree | ||
|
||
from qc_baselib import Result, IssueSeverity | ||
|
||
from qc_otx import constants | ||
from qc_otx.checks import models | ||
|
||
from qc_otx.checks.core_checker import core_constants | ||
|
||
|
||
def check_rule(checker_data: models.CheckerData) -> None: | ||
""" | ||
Implements core checker rule Core_Chk008 | ||
Criterion: The value of <procedure> attribute visibility shall always be "PUBLIC" if the procedure name is "main". | ||
Severity: Critical | ||
""" | ||
logging.info("Executing public_main_procedure check") | ||
|
||
issue_severity = IssueSeverity.ERROR | ||
|
||
rule_uid = checker_data.result.register_rule( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=core_constants.CHECKER_ID, | ||
emanating_entity="asam.net", | ||
standard="otx", | ||
definition_setting="1.0.0", | ||
rule_full_name="core.chk_008.public_main_procedure", | ||
) | ||
|
||
tree = checker_data.input_file_xml_root | ||
root = tree.getroot() | ||
|
||
# Use XPath to find all nodes procedures | ||
procedure_nodes = tree.xpath(f"//procedure") | ||
|
||
for procedure_node in procedure_nodes: | ||
procedure_name = procedure_node.get("name") | ||
procedure_visibility = procedure_node.get("visibility") | ||
|
||
# Visibility defaults to private if not specified | ||
if procedure_visibility is None: | ||
procedure_visibility = "PRIVATE" | ||
|
||
has_issue = procedure_name == "main" and procedure_visibility != "PUBLIC" | ||
|
||
if has_issue: | ||
current_xpath = tree.getpath(procedure_node) | ||
issue_id = checker_data.result.register_issue( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=core_constants.CHECKER_ID, | ||
description="Issue flagging when procedure called main has not PUBLIC visibility", | ||
level=issue_severity, | ||
rule_uid=rule_uid, | ||
) | ||
|
||
checker_data.result.add_xml_location( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=core_constants.CHECKER_ID, | ||
issue_id=issue_id, | ||
xpath=current_xpath, | ||
description=f"Procedure at {current_xpath} is called main but its visibility is not PUBLIC", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx id="2" | ||
name="Core_Chk008_negative" | ||
package="Core_Chk008" | ||
version="1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://iso.org/OTX/1.0.0 Core/otx.xsd" | ||
timestamp="2010-11-11T14:40:10"> | ||
|
||
<imports> | ||
<import package="org.iso.otx.examples" document="Signatures" prefix="sig" /> | ||
<import package="org.iso.otx.examples" document="Validities" prefix="val" /> | ||
</imports> | ||
|
||
<procedures> | ||
<!-- main procedure has not PUBLIC visibility, but PACKAGE --> | ||
<procedure name="main" visibility="PACKAGE" id="7-p1"> | ||
<specification>This is an empty top-level procedure, a test sequence</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
<procedure name="writeDebugMsg" implements="sig:WriteMsg" validFor="val:DebugMode" | ||
visibility="PACKAGE" id="7-p2"> | ||
<specification>An empty procedure implementing a signature, with validity information.</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
</procedures> | ||
|
||
|
||
</otx> |
33 changes: 33 additions & 0 deletions
33
tests/data/Core_Chk008/Core_Chk008_negative_no_visibility.otx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx id="2" | ||
name="Core_Chk008_negative_no_visibility" | ||
package="Core_Chk008" | ||
version="1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://iso.org/OTX/1.0.0 Core/otx.xsd" | ||
timestamp="2010-11-11T14:40:10"> | ||
|
||
<imports> | ||
<import package="org.iso.otx.examples" document="Signatures" prefix="sig" /> | ||
<import package="org.iso.otx.examples" document="Validities" prefix="val" /> | ||
</imports> | ||
|
||
<procedures> | ||
<!-- main procedure has no visibility, defaults to invalid PRIVATE visibility --> | ||
<procedure name="main" id="7-p1"> | ||
<specification>This is an empty top-level procedure, a test sequence</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
<procedure name="writeDebugMsg" implements="sig:WriteMsg" validFor="val:DebugMode" | ||
visibility="PACKAGE" id="7-p2"> | ||
<specification>An empty procedure implementing a signature, with validity information.</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
</procedures> | ||
|
||
|
||
</otx> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx id="2" | ||
name="Core_Chk008_negative_two_mains" | ||
package="Core_Chk008" | ||
version="1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://iso.org/OTX/1.0.0 Core/otx.xsd" | ||
timestamp="2010-11-11T14:40:10"> | ||
|
||
<procedures> | ||
<procedure name="main" visibility="PUBLIC" id="7-p1"> | ||
<specification>This is an empty top-level procedure, a test sequence</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
<!-- main procedure has not PUBLIC visibility, but PRIVATE --> | ||
<procedure name="main" visibility="PRIVATE" id="7-p2"> | ||
<specification>This is a second empty top-level procedure, a test sequence</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
</procedures> | ||
|
||
|
||
</otx> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx id="2" | ||
name="Core_Chk008_positive" | ||
package="Core_Chk008" | ||
version="1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://iso.org/OTX/1.0.0 Core/otx.xsd" | ||
timestamp="2010-11-11T14:40:10"> | ||
|
||
<imports> | ||
<import package="org.iso.otx.examples" document="Signatures" prefix="sig" /> | ||
<import package="org.iso.otx.examples" document="Validities" prefix="val" /> | ||
</imports> | ||
|
||
<procedures> | ||
<procedure name="main" visibility="PUBLIC" id="7-p1"> | ||
<specification>This is an empty top-level procedure, a test sequence</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
<procedure name="writeDebugMsg" implements="sig:WriteMsg" validFor="val:DebugMode" | ||
visibility="PACKAGE" id="7-p2"> | ||
<specification>An empty procedure implementing a signature, with validity information.</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
</procedures> | ||
|
||
|
||
</otx> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx id="2" | ||
name="Core_Chk008_positive_no_main" | ||
package="Core_Chk008" | ||
version="1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://iso.org/OTX/1.0.0 Core/otx.xsd" | ||
timestamp="2010-11-11T14:40:10"> | ||
|
||
<imports> | ||
<import package="org.iso.otx.examples" document="Signatures" prefix="sig" /> | ||
<import package="org.iso.otx.examples" document="Validities" prefix="val" /> | ||
</imports> | ||
|
||
<procedures> | ||
<procedure name="writeDebugMsg" implements="sig:WriteMsg" validFor="val:DebugMode" | ||
visibility="PACKAGE" id="7-p2"> | ||
<specification>An empty procedure implementing a signature, with validity information.</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
</procedures> | ||
|
||
|
||
</otx> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx id="2" | ||
name="Core_Chk008_positive_two_mains" | ||
package="Core_Chk008" | ||
version="1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://iso.org/OTX/1.0.0 Core/otx.xsd" | ||
timestamp="2010-11-11T14:40:10"> | ||
|
||
|
||
<procedures> | ||
<procedure name="main" visibility="PUBLIC" id="7-p1"> | ||
<specification>This is an empty top-level procedure, a test sequence</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
<procedure name="main" visibility="PUBLIC" id="7-p2"> | ||
<specification>This is a second empty top-level procedure, a test sequence</specification> | ||
<realisation> | ||
<flow /> | ||
</realisation> | ||
</procedure> | ||
</procedures> | ||
|
||
|
||
</otx> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx id="2" | ||
name="Core_Chk009_negative" | ||
package="Core_Chk009" | ||
version="1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://iso.org/OTX/1.0.0 Core/otx.xsd" | ||
timestamp="2010-11-11T14:40:10"> | ||
|
||
<declarations> | ||
<!-- constant PI not initialized --> | ||
<constant name="PI" visibility="PUBLIC" id="3-d1"> | ||
<specification>This defines global constant</specification> | ||
<realisation> | ||
<dataType xsi:type="Float"> | ||
</dataType> | ||
</realisation> | ||
</constant> | ||
</declarations> | ||
|
||
</otx> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx id="2" | ||
name="Core_Chk009_negative_multiple" | ||
package="Core_Chk009" | ||
version="1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://iso.org/OTX/1.0.0 Core/otx.xsd" | ||
timestamp="2010-11-11T14:40:10"> | ||
|
||
<declarations> | ||
<constant name="PI" visibility="PUBLIC" id="3-d1"> | ||
<specification>This defines global constant</specification> | ||
<realisation> | ||
<dataType xsi:type="Float"> | ||
<init value="3.14159265" /> | ||
</dataType> | ||
</realisation> | ||
</constant> | ||
<!-- constant 2_PI not initialized --> | ||
<constant name="2_PI" visibility="PRIVATE" id="3-d2"> | ||
<specification>This defines another global constant</specification> | ||
</constant> | ||
</declarations> | ||
|
||
</otx> |
Oops, something went wrong.