Search in sources :

Example 1 with FieldTypeResult

use of com.ge.research.osate.verdict.dsl.ThreatModelUtil.FieldTypeResult in project VERDICT by ge-high-assurance.

the class VerdictJavaValidator method checkThreatEqualContains.

/**
 * Check that RHS and LHS in equality are of the same type.
 * Check that LHS type is list of RHS type in contains.
 *
 * @param equalContains
 */
@Check(CheckType.FAST)
public void checkThreatEqualContains(ThreatEqualContains equalContains) {
    FieldTypeResult left = ThreatModelUtil.getVarType(equalContains.getLeft(), indexProvider);
    FieldTypeResult right = ThreatModelUtil.getVarType(equalContains.getRight(), indexProvider);
    if (equalContains.isEqual()) {
        if (left.type.isPresent() && right.type.isPresent()) {
            // Both are typed
            if (!left.type.get().equals(right.type.get())) {
                error("Incompatible types: " + left.type.get().getShortName() + " and " + right.type.get().getShortName());
            }
        } else if (left.type.isPresent()) {
            // Left is typed
            if (equalContains.getRight().getIds() != null && equalContains.getRight().getIds().isEmpty()) {
                // Possible value
                if (!left.type.get().isValue(right.varName)) {
                    error("Invalid value " + right.varName + " for type " + left.type.get().getShortName());
                }
            }
        } else if (right.type.isPresent()) {
            // Right is typed
            if (equalContains.getLeft().getIds() != null && equalContains.getLeft().getIds().isEmpty()) {
                // Possible value
                if (!right.type.get().isValue(left.varName)) {
                    error("Invalid value " + left.varName + " for type " + right.type.get().getShortName());
                }
            }
        } else {
            // Neither is typed
            if (equalContains.getLeft().getIds().isEmpty() && equalContains.getRight().getIds().isEmpty()) {
                // Both are values
                error("Undefined var: " + left.varName, VerdictPackage.Literals.THREAT_EQUAL_CONTAINS__LEFT);
                error("Undefined var: " + right.varName, VerdictPackage.Literals.THREAT_EQUAL_CONTAINS__RIGHT);
            }
        }
    } else if (equalContains.isContains()) {
        if (left.type.isPresent() && right.type.isPresent()) {
            // Check list-of relation
            if (!left.type.get().isListOf(right.type.get())) {
                error("Incompatible types " + left.type.get().getShortName() + " and " + right.type.get().getShortName() + ", left must be list of right ");
            }
        } else {
            /**
             * Make sure vars are defined.
             *
             * This check can only happen here because standalone vars
             * might be bare value constants.
             */
            if (!left.type.isPresent() && equalContains.getLeft().getIds() != null && equalContains.getLeft().getIds().isEmpty()) {
                error("Undefined var: " + left.varName, VerdictPackage.Literals.THREAT_EQUAL_CONTAINS__LEFT);
            }
            if (!right.type.isPresent() && equalContains.getRight().getIds() != null && equalContains.getRight().getIds().isEmpty()) {
                error("Undefined var: " + right.varName, VerdictPackage.Literals.THREAT_EQUAL_CONTAINS__RIGHT);
            }
        }
    }
}
Also used : FieldTypeResult(com.ge.research.osate.verdict.dsl.ThreatModelUtil.FieldTypeResult) Check(org.eclipse.xtext.validation.Check)

Aggregations

FieldTypeResult (com.ge.research.osate.verdict.dsl.ThreatModelUtil.FieldTypeResult)1 Check (org.eclipse.xtext.validation.Check)1