use of de.fraunhofer.aisec.cpg.helpers.Util.Connect.NODE in project cpg by Fraunhofer-AISEC.
the class EOGTest method testBranchProperty.
/**
* Tests EOG branch edge property in if/else if/else construct
*
* @throws Exception
*/
@Test
void testBranchProperty() throws Exception {
Path topLevel = Path.of("src", "test", "resources", "eog");
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(topLevel.resolve("EOG.java").toFile()), topLevel, true);
// Test If-Block
IfStatement firstIf = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, IfStatement.class), l -> l.getLocation().getRegion().getStartLine() == 6).get(0);
DeclaredReferenceExpression a = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 8 && l.getName().equals("a")).get(0);
DeclaredReferenceExpression b = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 7 && l.getName().equals("b")).get(0);
List<PropertyEdge<Node>> nextEOG = firstIf.getNextEOGEdges();
assertEquals(2, nextEOG.size());
for (PropertyEdge<Node> edge : nextEOG) {
assertEquals(firstIf, edge.getStart());
if (edge.getEnd().equals(b)) {
assertEquals(true, edge.getProperty(Properties.BRANCH));
assertEquals(0, edge.getProperty(Properties.INDEX));
} else {
assertEquals(a, edge.getEnd());
assertEquals(false, edge.getProperty(Properties.BRANCH));
assertEquals(1, edge.getProperty(Properties.INDEX));
}
}
IfStatement elseIf = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, IfStatement.class), l -> l.getLocation().getRegion().getStartLine() == 8).get(0);
assertEquals(elseIf, firstIf.getElseStatement());
DeclaredReferenceExpression b2 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 9 && l.getName().equals("b")).get(0);
DeclaredReferenceExpression x = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), l -> l.getLocation().getRegion().getStartLine() == 11 && l.getName().equals("x")).get(0);
nextEOG = elseIf.getNextEOGEdges();
assertEquals(2, nextEOG.size());
for (PropertyEdge<Node> edge : nextEOG) {
assertEquals(elseIf, edge.getStart());
if (edge.getEnd().equals(b2)) {
assertEquals(true, edge.getProperty(Properties.BRANCH));
assertEquals(0, edge.getProperty(Properties.INDEX));
} else {
assertEquals(x, edge.getEnd());
assertEquals(false, edge.getProperty(Properties.BRANCH));
assertEquals(1, edge.getProperty(Properties.INDEX));
}
}
}
use of de.fraunhofer.aisec.cpg.helpers.Util.Connect.NODE 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()));
}
use of de.fraunhofer.aisec.cpg.helpers.Util.Connect.NODE in project cpg by Fraunhofer-AISEC.
the class EOGTest method testCPPCallGraph.
/**
* Test function (not method) calls.
*
* @throws TransactionException
*/
@Test
void testCPPCallGraph() throws Exception {
List<Node> nodes = translateToNodes("src/test/resources/cg.cpp");
List<CallExpression> calls = TestUtils.subnodesOfType(nodes, CallExpression.class);
List<FunctionDeclaration> functions = TestUtils.subnodesOfType(nodes, FunctionDeclaration.class);
FunctionDeclaration target;
CallExpression first = TestUtils.findByUniqueName(calls, "first");
target = TestUtils.findByUniqueName(functions, "first");
assertEquals(List.of(target), first.getInvokes());
CallExpression second = TestUtils.findByUniqueName(calls, "second");
target = TestUtils.findByUniqueName(functions, "second");
assertEquals(List.of(target), second.getInvokes());
CallExpression third = TestUtils.findByUniqueName(calls, "third");
target = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("third") && f.getParameters().size() == 2);
assertEquals(List.of(target), third.getInvokes());
CallExpression fourth = TestUtils.findByUniqueName(calls, "fourth");
target = TestUtils.findByUniqueName(functions, "fourth");
assertEquals(List.of(target), fourth.getInvokes());
}
use of de.fraunhofer.aisec.cpg.helpers.Util.Connect.NODE in project cpg by Fraunhofer-AISEC.
the class EOGTest method testConditionShortCircuit.
@Test
void testConditionShortCircuit() throws Exception {
List<Node> nodes = translateToNodes("src/test/resources/cfg/ShortCircuit.java");
List<BinaryOperator> binaryOperators = Util.filterCast(nodes, BinaryOperator.class).stream().filter(bo -> bo.getOperatorCode().equals("&&") || bo.getOperatorCode().equals("||")).collect(Collectors.toList());
for (BinaryOperator bo : binaryOperators) {
assertTrue(Util.eogConnect(ALL, SUBTREE, EXITS, bo.getLhs(), SUBTREE, bo.getRhs()));
assertTrue(Util.eogConnect(ALL, SUBTREE, EXITS, bo.getLhs(), NODE, bo));
assertTrue(bo.getLhs().getNextEOG().size() == 2);
}
}
Aggregations