Search in sources :

Example 1 with SUBTREE

use of de.fraunhofer.aisec.cpg.helpers.Util.Connect.SUBTREE 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()));
}
Also used : EvaluationOrderGraphPass(de.fraunhofer.aisec.cpg.passes.EvaluationOrderGraphPass) Properties(de.fraunhofer.aisec.cpg.graph.edge.Properties) SUBTREE(de.fraunhofer.aisec.cpg.helpers.Util.Connect.SUBTREE) de.fraunhofer.aisec.cpg.graph(de.fraunhofer.aisec.cpg.graph) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) NodeComparator(de.fraunhofer.aisec.cpg.helpers.NodeComparator) ConstructorDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration) Util(de.fraunhofer.aisec.cpg.helpers.Util) SubgraphWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge) Path(java.nio.file.Path) EXITS(de.fraunhofer.aisec.cpg.helpers.Util.Edge.EXITS) PhysicalLocation.locationLink(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation.locationLink) NODE(de.fraunhofer.aisec.cpg.helpers.Util.Connect.NODE) ALL(de.fraunhofer.aisec.cpg.helpers.Util.Quantifier.ALL) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy) TestUtils(de.fraunhofer.aisec.cpg.TestUtils) TransactionException(org.neo4j.ogm.exception.TransactionException) ANY(de.fraunhofer.aisec.cpg.helpers.Util.Quantifier.ANY) Collectors(java.util.stream.Collectors) File(java.io.File) BinaryOperator(de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) ENTRIES(de.fraunhofer.aisec.cpg.helpers.Util.Edge.ENTRIES) TranslationException(de.fraunhofer.aisec.cpg.frontends.TranslationException) List(java.util.List) IVisitor(de.fraunhofer.aisec.cpg.processing.IVisitor) Stream(java.util.stream.Stream) de.fraunhofer.aisec.cpg.graph.statements(de.fraunhofer.aisec.cpg.graph.statements) Assertions(org.junit.jupiter.api.Assertions) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) SubgraphWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker) BinaryOperator(de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator)

Example 2 with SUBTREE

use of de.fraunhofer.aisec.cpg.helpers.Util.Connect.SUBTREE 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);
    }
}
Also used : EvaluationOrderGraphPass(de.fraunhofer.aisec.cpg.passes.EvaluationOrderGraphPass) Properties(de.fraunhofer.aisec.cpg.graph.edge.Properties) SUBTREE(de.fraunhofer.aisec.cpg.helpers.Util.Connect.SUBTREE) de.fraunhofer.aisec.cpg.graph(de.fraunhofer.aisec.cpg.graph) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) NodeComparator(de.fraunhofer.aisec.cpg.helpers.NodeComparator) ConstructorDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration) Util(de.fraunhofer.aisec.cpg.helpers.Util) SubgraphWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge) Path(java.nio.file.Path) EXITS(de.fraunhofer.aisec.cpg.helpers.Util.Edge.EXITS) PhysicalLocation.locationLink(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation.locationLink) NODE(de.fraunhofer.aisec.cpg.helpers.Util.Connect.NODE) ALL(de.fraunhofer.aisec.cpg.helpers.Util.Quantifier.ALL) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy) TestUtils(de.fraunhofer.aisec.cpg.TestUtils) TransactionException(org.neo4j.ogm.exception.TransactionException) ANY(de.fraunhofer.aisec.cpg.helpers.Util.Quantifier.ANY) Collectors(java.util.stream.Collectors) File(java.io.File) BinaryOperator(de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) ENTRIES(de.fraunhofer.aisec.cpg.helpers.Util.Edge.ENTRIES) TranslationException(de.fraunhofer.aisec.cpg.frontends.TranslationException) List(java.util.List) IVisitor(de.fraunhofer.aisec.cpg.processing.IVisitor) Stream(java.util.stream.Stream) de.fraunhofer.aisec.cpg.graph.statements(de.fraunhofer.aisec.cpg.graph.statements) Assertions(org.junit.jupiter.api.Assertions) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BinaryOperator(de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

BaseTest (de.fraunhofer.aisec.cpg.BaseTest)2 TestUtils (de.fraunhofer.aisec.cpg.TestUtils)2 TranslationException (de.fraunhofer.aisec.cpg.frontends.TranslationException)2 de.fraunhofer.aisec.cpg.graph (de.fraunhofer.aisec.cpg.graph)2 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)2 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)2 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)2 Properties (de.fraunhofer.aisec.cpg.graph.edge.Properties)2 PropertyEdge (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)2 de.fraunhofer.aisec.cpg.graph.statements (de.fraunhofer.aisec.cpg.graph.statements)2 BinaryOperator (de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator)2 CallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)2 DeclaredReferenceExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression)2 NodeComparator (de.fraunhofer.aisec.cpg.helpers.NodeComparator)2 SubgraphWalker (de.fraunhofer.aisec.cpg.helpers.SubgraphWalker)2 Util (de.fraunhofer.aisec.cpg.helpers.Util)2 NODE (de.fraunhofer.aisec.cpg.helpers.Util.Connect.NODE)2 SUBTREE (de.fraunhofer.aisec.cpg.helpers.Util.Connect.SUBTREE)2 ENTRIES (de.fraunhofer.aisec.cpg.helpers.Util.Edge.ENTRIES)2 EXITS (de.fraunhofer.aisec.cpg.helpers.Util.Edge.EXITS)2