use of de.fraunhofer.aisec.cpg.helpers.Util.Edge.ENTRIES in project cpg by Fraunhofer-AISEC.
the class EOGTest method testIf.
/**
* Tests EOG building in the presence of if/else statements.
*
* @param relPath
* @param refNodeString - Exact string of reference nodes, do not change/insert nodes in the test
* file.
* @throws TranslationException
*/
void testIf(String relPath, String refNodeString) throws Exception {
List<Node> nodes = translateToNodes(relPath);
// All BinaryOperators (including If conditions) have only one successor
List<BinaryOperator> binops = Util.filterCast(nodes, BinaryOperator.class);
for (BinaryOperator binop : binops) {
SubgraphWalker.Border binopEOG = SubgraphWalker.getEOGPathEdges(binop);
assertEquals(1, binopEOG.getExits().size());
}
List<IfStatement> ifs = Util.filterCast(nodes, IfStatement.class);
assertEquals(2, ifs.size());
ifs.forEach(ifnode -> Assertions.assertNotNull(ifnode.getThenStatement()));
assertTrue(ifs.stream().anyMatch(node -> node.getElseStatement() == null) && ifs.stream().anyMatch(node -> node.getElseStatement() != null));
IfStatement ifSimple = ifs.get(0);
IfStatement ifBranched = ifs.get(1);
List<Node> prints = nodes.stream().filter(node -> node.getCode().equals(refNodeString)).collect(Collectors.toList());
SubgraphWalker.Border ifEOG = SubgraphWalker.getEOGPathEdges(ifSimple);
SubgraphWalker.Border conditionEOG = SubgraphWalker.getEOGPathEdges(ifSimple.getCondition());
SubgraphWalker.Border thenEOG = SubgraphWalker.getEOGPathEdges(ifSimple.getThenStatement());
// IfStmt has 2 outgoing EOG edges (for true and false branch)
assertEquals(2, ifEOG.getExits().size());
// Assert: Only single entry and exit NODE per block
assertTrue(conditionEOG.getEntries().size() == 1 && conditionEOG.getExits().size() == 1);
assertTrue(thenEOG.getEntries().size() == 1 && thenEOG.getExits().size() == 1);
// Assert: Condition of simple if is preceded by print
assertTrue(Util.eogConnect(ENTRIES, ifSimple.getCondition(), prints.get(0)));
// Assert: All EOGs going into the then branch (=the 2nd print stmt) come from the IfStatement
assertTrue(Util.eogConnect(ENTRIES, ifSimple.getThenStatement(), NODE, ifSimple));
// Assert: The EOGs going into the second print come either from the then branch or the
// IfStatement
assertTrue(Util.eogConnect(NODE, EXITS, ifSimple, prints.get(1)));
assertTrue(Util.eogConnect(NODE, EXITS, ifSimple, ifSimple.getThenStatement()));
assertTrue(Util.eogConnect(NODE, EXITS, ifSimple.getThenStatement(), prints.get(1)));
conditionEOG = SubgraphWalker.getEOGPathEdges(ifBranched.getCondition());
thenEOG = SubgraphWalker.getEOGPathEdges(ifBranched.getThenStatement());
SubgraphWalker.Border elseEOG = SubgraphWalker.getEOGPathEdges(ifBranched.getElseStatement());
// Assert: Only single entry and exit NODE per block
assertTrue(conditionEOG.getEntries().size() == 1 && conditionEOG.getExits().size() == 1);
assertTrue(thenEOG.getEntries().size() == 1 && thenEOG.getExits().size() == 1);
assertTrue(elseEOG.getEntries().size() == 1 && elseEOG.getExits().size() == 1);
// Assert: Branched if is preceded by the second print
assertTrue(Util.eogConnect(ENTRIES, ifBranched, prints.get(1)));
// IfStatement has exactly 2 outgoing EOGS: true (then) and false (else) branch
assertTrue(Util.eogConnect(NODE, EXITS, ifBranched, ifBranched.getThenStatement()));
assertTrue(Util.eogConnect(NODE, EXITS, ifBranched, ifBranched.getElseStatement()));
SubgraphWalker.Border ifBranchedEOG = SubgraphWalker.getEOGPathEdges(ifBranched);
assertEquals(2, ifBranchedEOG.getExits().size());
// Assert: EOG going into then branch comes from the condition branch
assertTrue(Util.eogConnect(ENTRIES, ifBranched.getThenStatement(), NODE, ifBranched));
// Assert: EOG going into else branch comes from the condition branch
assertTrue(Util.eogConnect(ENTRIES, ifBranched.getElseStatement(), NODE, ifBranched));
// Assert: EOG edges going into the third print either come from the then or else branch
assertTrue(Util.eogConnect(SUBTREE, EXITS, ifBranched, prints.get(2)));
// Assert: EOG edges going into the branch root node either come from the then or else branch
assertTrue(Util.eogConnect(NODE, ENTRIES, ifBranched, ifBranched.getCondition()));
}
Aggregations