Skip to content

Commit

Permalink
Add main and constant check and tests (#7)
Browse files Browse the repository at this point in the history
Signed-off-by: romanodanilo <[email protected]>

Signed-off-by: romanodanilo <[email protected]>
  • Loading branch information
romanodanilo authored Jul 2, 2024
1 parent cd7db54 commit a5d0757
Show file tree
Hide file tree
Showing 16 changed files with 679 additions and 0 deletions.
2 changes: 2 additions & 0 deletions qc_otx/checks/core_checker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
from . import (
have_specification_if_no_realisation_exists as have_specification_if_no_realisation_exists,
)
from . import public_main_procedure as public_main_procedure
from . import mandatory_constant_initialization as mandatory_constant_initialization
4 changes: 4 additions & 0 deletions qc_otx/checks/core_checker/core_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
no_dead_import_links,
no_unused_imports,
have_specification_if_no_realisation_exists,
public_main_procedure,
mandatory_constant_initialization,
)


Expand All @@ -33,6 +35,8 @@ def run_checks(checker_data: models.CheckerData) -> None:
no_dead_import_links.check_rule, # Chk003
no_unused_imports.check_rule, # Chk004
have_specification_if_no_realisation_exists.check_rule, # Chk007
public_main_procedure.check_rule, # Chk008
mandatory_constant_initialization.check_rule, # Chk009
]

for rule in rule_list:
Expand Down
64 changes: 64 additions & 0 deletions qc_otx/checks/core_checker/mandatory_constant_initialization.py
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",
)
66 changes: 66 additions & 0 deletions qc_otx/checks/core_checker/public_main_procedure.py
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",
)
33 changes: 33 additions & 0 deletions tests/data/Core_Chk008/Core_Chk008_negative.otx
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 tests/data/Core_Chk008/Core_Chk008_negative_no_visibility.otx
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>
27 changes: 27 additions & 0 deletions tests/data/Core_Chk008/Core_Chk008_negative_two_mains.otx
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>
32 changes: 32 additions & 0 deletions tests/data/Core_Chk008/Core_Chk008_positive.otx
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>
26 changes: 26 additions & 0 deletions tests/data/Core_Chk008/Core_Chk008_positive_no_main.otx
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>
27 changes: 27 additions & 0 deletions tests/data/Core_Chk008/Core_Chk008_positive_two_mains.otx
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>
21 changes: 21 additions & 0 deletions tests/data/Core_Chk009/Core_Chk009_negative.otx
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>
25 changes: 25 additions & 0 deletions tests/data/Core_Chk009/Core_Chk009_negative_multiple.otx
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>
Loading

0 comments on commit a5d0757

Please sign in to comment.