Commit d1f75f28 authored by Paff's avatar Paff
Browse files

test coverage analysis > 90

bug fixes
parent 9e30723e
package secarc.ets.analysis.checker;
import interfaces2.resolvers.AmbigousException;
import secarc._ast.ASTSecArcRefRole;
import secarc._ast.ASTSecArcRole;
import secarc.ets.entries.RoleEntry;
import secarc.ets.graph.ArchitectureGraph;
......@@ -30,4 +31,16 @@ public interface ISecAnalysisRoleChecker {
*/
int check(ASTSecArcRole node, RoleEntry entry, ArchitectureGraph graph, boolean advanced) throws AmbigousException;
/**
* Analysis for referenced roles
*
* @param node ast node to check
* @param entry entry of ast node
* @param graph graph with connectors as edges
* @param advanced output for advanced users
* @return
* @throws AmbigousException
*/
int check(ASTSecArcRefRole node, RoleEntry entry, ArchitectureGraph graph, boolean advanced) throws AmbigousException;
}
......@@ -43,7 +43,7 @@ public class AvoidInputString extends Analysis implements
@Override
public int check(ASTArcPort node, SecPortEntry entry,
ArchitectureGraph graph, boolean advanced) throws AmbigousException {
if(AnalysisHelper.isPortBenningOfPath(entry, graph) != null
if(AnalysisHelper.isPortBenningOfPath(entry, graph) == null
&& entry.getTypeReference().getExtendedName().equals("java.lang.String")) {
addReport("The input port " + entry.getName() + " has the type " + entry.getTypeReference() + ". This type is not specific enough.", node.get_SourcePositionStart());
return factor;
......
......@@ -157,7 +157,7 @@ public class EncryptedPathEndInLowTrustlevel extends DerivedTrustlevel implement
//Trustlevel of supercomponent
superComponentEntry = getDerivedTrustlevel((ASTArcComponent) lastComponent.getBestKnownVersion().getNode());
//If no trustlevel in supercomponent exists, take the default trustlevel of the environment
if(superComponentEntry.getTrustlevel().get() == null) {
if(superComponentEntry == null || !superComponentEntry.getTrustlevel().isPresent()) {
trustlevelSuperComponentCompare = -1;
} else {
trustlevelSuperComponentCompare = CoCoHelper.getTrustlevelAsInteger(superComponentEntry);
......
......@@ -6,6 +6,7 @@ import mc.umlp.arcd._ast.ASTArcComponent;
import mc.umlp.arcd._ast.ASTArcConnector;
import mc.umlp.arcd._ast.ASTArcSimpleConnector;
import mc.umlp.arcd.ets.entries.ComponentEntry;
import mc.umlp.arcd.ets.entries.SubComponentEntry;
import mc.umlp.common._ast.UMLPNode;
import secarc.error.MontiSecArcAnalysisErrorCodes;
import secarc.ets.analysis.checker.ISecAnalysisConnectorChecker;
......@@ -75,14 +76,22 @@ public class TrustlevelConnectorHigherThanEnvironment extends DerivedTrustlevel
protected int innerCheck(UMLPNode node, SecConnectorEntry entry, ASTArcComponent component) throws AmbigousException {
String refTarget = entry.getTarget();
String refSource = entry.getSource();
SubComponentEntry subComponentSource = null;
SecComponentEntry componentSource = null;
SubComponentEntry subComponentTarget = null;
SecComponentEntry componentTarget = null;
SecComponentEntry componentParent = null;
if(refSource.contains(".")) {
//Is in another namespace
componentSource = (SecComponentEntry) resolver.resolve(refSource.substring(0, refSource.indexOf(".")), ComponentEntry.KIND, getNameSpaceFor(component));
subComponentSource = (SubComponentEntry) resolver.resolve(refSource.substring(0, refSource.indexOf(".")), SubComponentEntry.KIND, getNameSpaceFor(component));
//Checked in coco
if(subComponentSource == null) {
return 0;
} else {
componentSource = (SecComponentEntry) subComponentSource.getComponentType();
}
} else {
//Is in the same namespace
componentSource = (SecComponentEntry) resolver.resolve(component.getName(), ComponentEntry.KIND, getNameSpaceFor(node));
......@@ -90,9 +99,13 @@ public class TrustlevelConnectorHigherThanEnvironment extends DerivedTrustlevel
if(refTarget.contains(".")) {
//Is in another namespace
refTarget = refTarget.substring(0, refTarget.indexOf("."));
refTarget = refTarget.replaceFirst(refTarget.substring(0, 1), refTarget.substring(0, 1).toUpperCase());
componentTarget = (SecComponentEntry) resolver.resolve(refTarget, ComponentEntry.KIND, getNameSpaceFor(component));
subComponentTarget = (SubComponentEntry) resolver.resolve(refTarget.substring(0, refTarget.indexOf(".")), SubComponentEntry.KIND, getNameSpaceFor(component));
//Checked in CoCo
if(subComponentTarget == null) {
return 0;
} else {
componentTarget = (SecComponentEntry) subComponentTarget.getComponentType();
}
} else {
//Is in the same namespace
componentTarget = (SecComponentEntry) resolver.resolve(component.getName(), ComponentEntry.KIND, getNameSpaceFor(node));
......@@ -104,10 +117,10 @@ public class TrustlevelConnectorHigherThanEnvironment extends DerivedTrustlevel
} else {
//In a higher namespace
componentParent = (SecComponentEntry) resolver.resolve(component.getMainParent().getName(), ComponentEntry.KIND, getNameSpaceFor(component.getMainParent()));
}
}
//Is checked in another coco
if(componentSource == null || componentTarget == null || componentParent == null) {
if(componentSource == null || componentTarget == null || componentParent == null) {
return 0;
}
......
......@@ -7,6 +7,7 @@ import interfaces2.resolvers.AmbigousException;
import mc.IErrorCode;
import mc.helper.NameHelper;
import mc.umlp.arcd._ast.ASTArcComponent;
import mc.umlp.arcd.ets.entries.PortEntry;
import mc.umlp.arcd.ets.entries.SubComponentEntry;
import secarc.error.MontiSecArcAnalysisErrorCodes;
import secarc.ets.analysis.checker.Analysis;
......@@ -14,6 +15,7 @@ import secarc.ets.analysis.checker.ISecAnalysisComponentChecker;
import secarc.ets.check.MontiSecArcAnalysisConstants;
import secarc.ets.entries.RoleEntry;
import secarc.ets.entries.SecComponentEntry;
import secarc.ets.entries.SecPortEntry;
/**
* D01: Lists all roles for a component
......@@ -39,7 +41,7 @@ public class DerivedRolesComponent extends Analysis implements
/**
* Roles of component
*/
List<RoleEntry> roleEntries;
List<String> roleEntries;
public DerivedRolesComponent(int factor) {
super(MontiSecArcAnalysisConstants.DERIVED_ROLES_COMPONENT);
......@@ -55,17 +57,19 @@ public class DerivedRolesComponent extends Analysis implements
return 0;
}
roleEntries = new ArrayList<RoleEntry>();
roleEntries = new ArrayList<String>();
addSubcomponentRoles(entry);
addPortRoles(entry);
if(roleEntries.size() > 0) {
StringBuilder sBuilder = new StringBuilder("The component " + NameHelper.getSimplenameFromComplexname(entry.getName()) + " has the following roles: " + roleEntries.get(0).getName());
StringBuilder sBuilder = new StringBuilder("The component " + NameHelper.getSimplenameFromComplexname(entry.getName()) + " has the following roles: " + roleEntries.get(0));
roleEntries.remove(0);
for(RoleEntry role : roleEntries) {
for(String name : roleEntries) {
sBuilder.append(", ");
sBuilder.append(role.getName());
sBuilder.append(name);
}
if(advanced) {
addReport(sBuilder.toString(), node.get_SourcePositionStart());
......@@ -79,13 +83,33 @@ public class DerivedRolesComponent extends Analysis implements
public void addSubcomponentRoles(SecComponentEntry entry) {
if(entry.getRoles() != null) {
roleEntries.addAll(entry.getRoles());
for(RoleEntry role : entry.getRoles()) {
if(!roleEntries.contains(role.getName())) {
roleEntries.add(role.getName());
}
}
}
for(SubComponentEntry subComponentEntry : entry.getSubComponents()) {
addSubcomponentRoles((SecComponentEntry) subComponentEntry.getComponentType().getBestKnownVersion());
}
}
private void addPortRoles(SecComponentEntry entry) {
List<PortEntry> ports = entry.getAllPorts(loader, deserializers);
if(ports != null) {
for(PortEntry port : ports) {
if(((SecPortEntry) port).getRoles() != null) {
for(RoleEntry role : ((SecPortEntry) port).getRoles()) {
if(!roleEntries.contains(role.getName())) {
roleEntries.add(role.getName());
}
}
}
}
}
}
@Override
public IErrorCode getErrorCode() {
......
......@@ -8,6 +8,7 @@ import mc.IErrorCode;
import mc.umlp.arcd._ast.ASTArcPort;
import mc.umlp.arcd.ets.entries.ComponentEntry;
import mc.umlp.arcd.ets.entries.PortEntry;
import mc.umlp.arcd.ets.entries.SubComponentEntry;
import secarc.error.MontiSecArcAnalysisErrorCodes;
import secarc.ets.analysis.checker.Analysis;
import secarc.ets.analysis.checker.ISecAnalysisPortChecker;
......@@ -38,6 +39,11 @@ public class DerivedRolesPort extends Analysis implements
*/
private int factor = 1;
/**
* Roles of component
*/
List<String> roleEntries;
public DerivedRolesPort(int factor) {
super(MontiSecArcAnalysisConstants.DERIVE_ROLES_PORT);
this.factor = factor;
......@@ -46,34 +52,34 @@ public class DerivedRolesPort extends Analysis implements
@Override
public int check(ASTArcPort node, SecPortEntry entry,
ArchitectureGraph graph, boolean advanced) throws AmbigousException {
List<RoleEntry> roles = new ArrayList<RoleEntry>();
roleEntries = new ArrayList<String>();
SecComponentEntry componentEntry = (SecComponentEntry) resolver.resolve(node.getMainParent().getName(), ComponentEntry.KIND, getNameSpaceFor(node));
//Add all roles of the component
roles.addAll(componentEntry.getRoles());
//Delete all roles of other ports
List<PortEntry> ports = componentEntry.getAllPorts(loader, deserializers);
for(RoleEntry role : ((SecPortEntry) entry).getRoles()) {
roleEntries.add(role.getName());
}
for(PortEntry port : ports) {
if(!port.equals(entry)) {
roles.removeAll(((SecPortEntry) port).getRoles());
}
//If nothing is specified for the port, roles of the the component are used
if(roleEntries.isEmpty()) {
addSubcomponentRoles(componentEntry);
addPortRoles(componentEntry);
}
StringBuilder sBuilder = new StringBuilder("The port " + entry.getName() + " has the following roles: ");
if(roles.size() > 0) {
sBuilder.append(roles.get(0).getName());
roles.remove(0);
if(roleEntries.size() > 0) {
sBuilder.append(roleEntries.get(0));
roleEntries.remove(0);
} else {
return 0;
}
for(RoleEntry role : roles) {
for(String name : roleEntries) {
sBuilder.append(", ");
sBuilder.append(role.getName());
sBuilder.append(name);
}
addReport(sBuilder.toString(), node.get_SourcePositionStart());
......@@ -81,6 +87,36 @@ public class DerivedRolesPort extends Analysis implements
return factor;
}
public void addSubcomponentRoles(SecComponentEntry entry) {
if(entry.getRoles() != null) {
for(RoleEntry role : entry.getRoles()) {
if(!roleEntries.contains(role.getName())) {
roleEntries.add(role.getName());
}
}
}
for(SubComponentEntry subComponentEntry : entry.getSubComponents()) {
addSubcomponentRoles((SecComponentEntry) subComponentEntry.getComponentType().getBestKnownVersion());
}
}
private void addPortRoles(SecComponentEntry entry) {
List<PortEntry> ports = entry.getAllPorts(loader, deserializers);
if(ports != null) {
for(PortEntry port : ports) {
if(((SecPortEntry) port).getRoles() != null) {
for(RoleEntry role : ((SecPortEntry) port).getRoles()) {
if(!roleEntries.contains(role.getName())) {
roleEntries.add(role.getName());
}
}
}
}
}
}
@Override
public IErrorCode getErrorCode() {
......
......@@ -7,6 +7,7 @@ import interfaces2.resolvers.AmbigousException;
import mc.IErrorCode;
import mc.helper.NameHelper;
import mc.umlp.arcd._ast.ASTArcComponent;
import mc.umlp.arcd.ets.entries.PortEntry;
import mc.umlp.arcd.ets.entries.SubComponentEntry;
import secarc.error.MontiSecArcAnalysisErrorCodes;
import secarc.ets.analysis.checker.Analysis;
......@@ -14,6 +15,7 @@ import secarc.ets.analysis.checker.ISecAnalysisComponentChecker;
import secarc.ets.check.MontiSecArcAnalysisConstants;
import secarc.ets.entries.RoleEntry;
import secarc.ets.entries.SecComponentEntry;
import secarc.ets.entries.SecPortEntry;
/**
* D03: Lists all roles for a third party component
......@@ -39,7 +41,7 @@ public class DerivedRolesThirdParty extends Analysis implements
/**
* Roles of component
*/
List<RoleEntry> roleEntries;
List<String> roleEntries;
public DerivedRolesThirdParty(int factor) {
super(MontiSecArcAnalysisConstants.DERIVED_ROLES_THIRD_PARTY);
......@@ -56,25 +58,27 @@ public class DerivedRolesThirdParty extends Analysis implements
//Check if component is a 3rd party component
if(entry.getConfiguration() == null || entry.getCPE() == null) {
return 0;
return 0;
}
roleEntries = new ArrayList<RoleEntry>();
roleEntries = new ArrayList<String>();
addSubcomponentRoles(entry);
addPortRoles(entry);
StringBuilder sBuilder = new StringBuilder("The third party component " + NameHelper.getSimplenameFromComplexname(entry.getName()) + " has the following roles: ");
if(roleEntries.size()>0) {
sBuilder.append(roleEntries.get(0).getName());
sBuilder.append(roleEntries.get(0));
roleEntries.remove(0);
} else {
return 0;
}
for(RoleEntry role : roleEntries) {
for(String name : roleEntries) {
sBuilder.append(", ");
sBuilder.append(role.getName());
sBuilder.append(name);
}
addReport(sBuilder.toString(), node.get_SourcePositionStart());
......@@ -82,9 +86,29 @@ public class DerivedRolesThirdParty extends Analysis implements
return factor;
}
public void addSubcomponentRoles(SecComponentEntry entry) {
private void addPortRoles(SecComponentEntry entry) {
List<PortEntry> ports = entry.getAllPorts(loader, deserializers);
if(ports != null) {
for(PortEntry port : ports) {
if(((SecPortEntry) port).getRoles() != null) {
for(RoleEntry role : ((SecPortEntry) port).getRoles()) {
if(!roleEntries.contains(role.getName())) {
roleEntries.add(role.getName());
}
}
}
}
}
}
private void addSubcomponentRoles(SecComponentEntry entry) {
if(entry.getRoles() != null) {
roleEntries.addAll(entry.getRoles());
for(RoleEntry role : entry.getRoles()) {
if(!roleEntries.contains(role.getName())) {
roleEntries.add(role.getName());
}
}
}
for(SubComponentEntry subComponentEntry : entry.getSubComponents()) {
......
......@@ -10,6 +10,8 @@ import org.jgrapht.alg.DirectedNeighborIndex;
import interfaces2.STEntry;
import interfaces2.resolvers.AmbigousException;
import mc.IErrorCode;
import mc.ast.ASTNode;
import secarc._ast.ASTSecArcRefRole;
import secarc._ast.ASTSecArcRole;
import secarc.error.MontiSecArcAnalysisErrorCodes;
import secarc.ets.analysis.checker.Analysis;
......@@ -52,46 +54,60 @@ public class RoleAccess extends Analysis implements ISecAnalysisRoleChecker {
@Override
public int check(ASTSecArcRole node, RoleEntry entry, ArchitectureGraph graph, boolean advanced)
throws AmbigousException {
return innerCheck(node, graph, entry);
}
/*
* (non-Javadoc)
* @see secarc.ets.analysis.checker.ISecAnalysisRoleChecker#check(secarc._ast.ASTSecArcRefRole, secarc.ets.entries.RoleEntry, secarc.ets.graph.ArchitectureGraph, boolean)
*/
@Override
public int check(ASTSecArcRefRole node, RoleEntry entry,
ArchitectureGraph graph, boolean advanced) throws AmbigousException {
return innerCheck(node, graph, entry);
}
protected int innerCheck(ASTNode node, ArchitectureGraph graph, RoleEntry entry) {
//Get all roles with the same name
Set<Vertex<? extends STEntry>> vertexSet = graph.getRawGraph().vertexSet();
Iterator<Vertex<? extends STEntry>> it = vertexSet.iterator();
STEntry element = null;
//Result set of role with the given name of the entry
List<RoleEntry> resultSet = new ArrayList<RoleEntry>();
while(it.hasNext()) {
element = it.next().getArchitectureElement();
if(element instanceof RoleEntry && element.getName().equals(entry.getName())) {
if(element instanceof RoleEntry && element.getName().equals(entry.getName())) {
resultSet.add((RoleEntry) element);
}
}
DirectedNeighborIndex<Vertex<? extends STEntry>, Edge> directedNeighborIndex = new DirectedNeighborIndex<Vertex<? extends STEntry>, Edge>(graph.getRawGraph());
List<Vertex<? extends STEntry>> allPredescessor = new ArrayList<Vertex<? extends STEntry>>();
for(RoleEntry role : resultSet) {
allPredescessor.addAll(directedNeighborIndex.predecessorListOf(Vertex.of(entry)));
allPredescessor.addAll(directedNeighborIndex.predecessorListOf(Vertex.of(role)));
}
StringBuilder sBuilder = new StringBuilder("The role " + entry.getName() + " has access to the following ports and components: ");
if(!allPredescessor.isEmpty()) {
sBuilder.append(allPredescessor.get(0).getArchitectureElement().getName());
allPredescessor.remove(0);
}
for(Vertex<? extends STEntry> vertex : allPredescessor) {
sBuilder.append(", ");
sBuilder.append(vertex.getArchitectureElement().getName());
}
addReport(sBuilder.toString(), node.get_SourcePositionStart());
return factor;
}
......
......@@ -5,7 +5,6 @@ import java.util.List;
import com.google.common.base.Optional;
import mc.IErrorCode;
import mc.helper.NameHelper;
import mc.umlp.arcd._ast.ASTArcComponent;
import mc.umlp.arcd.ets.entries.ComponentEntry;
import secarc.error.MontiSecArcAnalysisErrorCodes;
......
......@@ -24,6 +24,7 @@ import mc.umlp.arcd.ets.entries.PortEntry;
import secarc._ast.ASTSecArcConfiguration;
import secarc._ast.ASTSecArcFilter;
import secarc._ast.ASTSecArcIdentity;
import secarc._ast.ASTSecArcRefRole;
import secarc._ast.ASTSecArcRole;
import secarc._ast.ASTSecArcTrustlevelRelation;
import secarc.ets.analysis.checker.ISecAnalysisComponentChecker;
......@@ -407,6 +408,25 @@ public class MontiSecArcAnalysisVisitor extends CheckWorkflowClient {
}
}
/**
* Visits referenced roles
*/
public void visit(ASTSecArcRefRole node) {
for (String name : node.getRoles()) {
try {
RoleEntry entry = (RoleEntry) resolver.resolve(name, RoleEntry.KIND, getNameSpaceFor(node));
if (entry != null) {
for (ISecAnalysisRoleChecker cc : analysisRoleChecker) {
metricSum += cc.check(node, entry, graphConnectorEdges, advanced);
}
}
} catch (AmbigousException e) {
// not handled here
MCG.getLogger().info(e.getMessage());
}
}
}
/**
* Visits trustlevel relation
*/
......
......@@ -14,6 +14,7 @@ import mc.ProblemReport;
import org.junit.Test;
import secarc.error.MontiSecArcAnalysisErrorCodes;
import secarc.ets.check.MontiSecArcContextConditionConstants;
/**
......@@ -115,7 +116,7 @@ public class MontiSecArcAnalysisTest extends TestWithSymtabAnalysis<MontiSecArcA
assertTrue(errorCodes.contains(error.getErrorcode()));
}
//Fourth file
//Second file
handler.getWarnings().clear();
errorCodes.clear();
System.out.println("FilterPort.secarc");
......@@ -127,6 +128,32 @@ public class MontiSecArcAnalysisTest extends TestWithSymtabAnalysis<MontiSecArcA
for(ProblemReport error : handler.getErrors()) {
assertTrue(errorCodes.contains(error.getErrorcode()));
}
//Third file
handler.getWarnings().clear();
errorCodes.clear();
System.out.println("FilterWithLowerTrustlevel.secarc");
tool = createTestToolWithoutJava(new String[] { "src/test/resources/secarc/analysis/filter/FilterWithLowerTrustlevel.secarc" });
tool.init();
assertTrue(tool.run());
assertEquals(5, handler.getWarnings().size());
for(ProblemReport error : handler.getErrors()) {
assertTrue(errorCodes.contains(error.getErrorcode()));
}
//Forth file
handler.getWarnings().clear();
errorCodes.clear();
System.out.println("InputString.secarc");
tool = createTestToolWithoutJava(new String[] { "src/test/resources/secarc/analysis/filter/InputString.secarc" });
tool.init();
assertTrue(tool.run());
assertEquals(3, handler.getWarnings().size());
for(ProblemReport error : handler.getErrors()) {
assertTrue(errorCodes.contains(error.getErrorcode()));
}
}
......@@ -185,7 +212,20 @@ public class MontiSecArcAnalysisTest extends TestWithSymtabAnalysis<MontiSecArcA
errorCodes.add(MontiSecArcAnalysisErrorCodes.RoleAccess);
errorCodes.add(MontiSecArcAnalysisErrorCodes.ListThirdPartyComponents);