Search in sources :

Example 1 with MethodDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration in project cpg by Fraunhofer-AISEC.

the class VisitorTest method testAllEogNodeVisitor.

/**
 * Visits all nodes along EOG.
 */
@Test
void testAllEogNodeVisitor() {
    List<Node> nodeList = new ArrayList<>();
    RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
    MethodDeclaration method = recordDeclaration.getMethods().stream().filter(m -> m.getName().equals("method")).collect(Collectors.toList()).get(0);
    /* TODO A better way to get the "first" statement in a method body is needed.
    This is currently the only (fragile, ugly and unsafe) way to get to the first "real" statement in a method body.
    getNextEOG() and getNextCFG() return empty lists.
    */
    Statement firstStmt = ((CompoundStatement) method.getBody()).getStatements().get(0);
    firstStmt.accept(Strategy::EOG_FORWARD, new IVisitor<Node>() {

        public void visit(Node n) {
            System.out.println(n);
            nodeList.add(n);
        }
    });
    assertEquals(22, nodeList.size());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) CompoundStatement(de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement) Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) ReturnStatement(de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement) Strategy(de.fraunhofer.aisec.cpg.processing.strategy.Strategy) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with MethodDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration in project cpg by Fraunhofer-AISEC.

the class SuperCallTest method testInterfaceCall.

@Test
void testInterfaceCall() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel, true);
    List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
    RecordDeclaration interface1 = TestUtils.findByUniqueName(records, "Interface1");
    List<MethodDeclaration> interface1Methods = TestUtils.subnodesOfType(interface1, MethodDeclaration.class);
    MethodDeclaration interface1Target = TestUtils.findByUniqueName(interface1Methods, "target");
    RecordDeclaration interface2 = TestUtils.findByUniqueName(records, "Interface2");
    List<MethodDeclaration> interface2Methods = TestUtils.subnodesOfType(interface2, MethodDeclaration.class);
    MethodDeclaration interface2Target = TestUtils.findByUniqueName(interface2Methods, "target");
    RecordDeclaration subClass = TestUtils.findByUniqueName(records, "SubClass");
    List<MethodDeclaration> methods = TestUtils.subnodesOfType(subClass, MethodDeclaration.class);
    MethodDeclaration target = TestUtils.findByUniqueName(methods, "target");
    List<CallExpression> calls = TestUtils.subnodesOfType(target, CallExpression.class);
    CallExpression interface1Call = TestUtils.findByUniquePredicate(calls, c -> "Interface1.super.target();".equals(c.getCode()));
    CallExpression interface2Call = TestUtils.findByUniquePredicate(calls, c -> "Interface2.super.target();".equals(c.getCode()));
    assertEquals(List.of(interface1Target), interface1Call.getInvokes());
    assertEquals(List.of(interface2Target), interface2Call.getInvokes());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 3 with MethodDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration in project cpg by Fraunhofer-AISEC.

the class StaticImportsTest method testAsteriskImport.

@Test
void testAsteriskImport() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel.resolve("asterisk"), true);
    List<MethodDeclaration> methods = TestUtils.subnodesOfType(result, MethodDeclaration.class);
    MethodDeclaration main = TestUtils.findByUniqueName(methods, "main");
    List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
    RecordDeclaration A = TestUtils.findByUniqueName(records, "A");
    RecordDeclaration B = TestUtils.findByUniqueName(records, "B");
    for (CallExpression call : TestUtils.subnodesOfType(main, CallExpression.class)) {
        switch(call.getName()) {
            case "a":
                assertEquals(List.of(TestUtils.findByUniqueName(methods, "a")), call.getInvokes());
                assertTrue(((MethodDeclaration) call.getInvokes().get(0)).isStatic());
                break;
            case "b":
                List<MethodDeclaration> bs = methods.stream().filter(m -> m.getName().equals("b") && m.isStatic()).collect(Collectors.toList());
                assertEquals(call.getInvokes(), bs.stream().filter(b -> b.hasSignature(call.getSignature())).collect(Collectors.toList()));
                break;
            case "nonStatic":
                MethodDeclaration nonStatic = TestUtils.findByUniqueName(B.getMethods(), "nonStatic");
                assertTrue(nonStatic.isInferred());
                assertEquals(List.of(nonStatic), call.getInvokes());
        }
    }
    List<FieldDeclaration> testFields = TestUtils.subnodesOfType(A, FieldDeclaration.class);
    FieldDeclaration staticField = TestUtils.findByUniqueName(testFields, "staticField");
    FieldDeclaration nonStaticField = TestUtils.findByUniqueName(testFields, "nonStaticField");
    assertTrue(staticField.getModifiers().contains("static"));
    assertFalse(nonStaticField.getModifiers().contains("static"));
    List<MemberExpression> declaredReferences = TestUtils.subnodesOfType(main, MemberExpression.class);
    MemberExpression usage = TestUtils.findByUniqueName(declaredReferences, "staticField");
    assertEquals(staticField, usage.getRefersTo());
    MemberExpression nonStatic = TestUtils.findByUniqueName(declaredReferences, "nonStaticField");
    assertNotEquals(nonStaticField, nonStatic.getRefersTo());
    assertTrue(nonStatic.getRefersTo().isInferred());
}
Also used : TestUtils(de.fraunhofer.aisec.cpg.TestUtils) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) Assertions(org.junit.jupiter.api.Assertions) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) Path(java.nio.file.Path) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 4 with MethodDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration in project cpg by Fraunhofer-AISEC.

the class StaticImportsTest method testSingleStaticImport.

@Test
void testSingleStaticImport() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel.resolve("single"), true);
    List<MethodDeclaration> methods = TestUtils.subnodesOfType(result, MethodDeclaration.class);
    MethodDeclaration test = TestUtils.findByUniqueName(methods, "test");
    MethodDeclaration main = TestUtils.findByUniqueName(methods, "main");
    CallExpression call = TestUtils.subnodesOfType(main, CallExpression.class).get(0);
    assertEquals(List.of(test), call.getInvokes());
    List<FieldDeclaration> testFields = TestUtils.subnodesOfType(result, FieldDeclaration.class).stream().filter(f -> f.getName().equals("test")).collect(Collectors.toList());
    assertEquals(1, testFields.size());
    FieldDeclaration staticField = testFields.get(0);
    assertTrue(staticField.getModifiers().contains("static"));
    List<MemberExpression> memberExpressions = TestUtils.subnodesOfType(main, MemberExpression.class);
    MemberExpression usage = TestUtils.findByUniqueName(memberExpressions, "test");
    assertEquals(staticField, usage.getRefersTo());
}
Also used : TestUtils(de.fraunhofer.aisec.cpg.TestUtils) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) Assertions(org.junit.jupiter.api.Assertions) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) Path(java.nio.file.Path) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 5 with MethodDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration in project cpg by Fraunhofer-AISEC.

the class DFGTest method testSensitivityWithLabels.

@Test
void testSensitivityWithLabels() throws Exception {
    Path topLevel = Path.of("src", "test", "resources", "dfg");
    TranslationUnitDeclaration result = TestUtils.analyze(List.of(topLevel.resolve("LoopDFGs.java").toFile()), topLevel, true).get(0);
    MethodDeclaration looping = TestUtils.getSubnodeOfTypeWithName(result, MethodDeclaration.class, "labeledBreakContinue");
    List<Node> methodNodes = SubgraphWalker.flattenAST(looping);
    Literal l0 = getLiteral(methodNodes, 0);
    Literal l1 = getLiteral(methodNodes, 1);
    Literal l2 = getLiteral(methodNodes, 2);
    Literal l3 = getLiteral(methodNodes, 3);
    Literal l4 = getLiteral(methodNodes, 4);
    List<Node> calls = TestUtils.findByPredicate(SubgraphWalker.flattenAST(looping), n -> n instanceof CallExpression && n.getName().equals("println"));
    calls.sort(new NodeComparator());
    Set<Node> dfgNodesA0 = flattenDFGGraph(TestUtils.getSubnodeOfTypeWithName(calls.get(0), DeclaredReferenceExpression.class, "a"), false);
    Set<Node> dfgNodesA1 = flattenDFGGraph(TestUtils.getSubnodeOfTypeWithName(calls.get(1), DeclaredReferenceExpression.class, "a"), false);
    Set<Node> dfgNodesA2 = flattenDFGGraph(TestUtils.getSubnodeOfTypeWithName(calls.get(2), DeclaredReferenceExpression.class, "a"), false);
    assertTrue(dfgNodesA0.contains(l0));
    assertTrue(dfgNodesA0.contains(l1));
    assertTrue(dfgNodesA0.contains(l3));
    assertFalse(dfgNodesA0.contains(l4));
    assertTrue(dfgNodesA1.contains(l0));
    assertTrue(dfgNodesA1.contains(l1));
    assertTrue(dfgNodesA1.contains(l3));
    assertFalse(dfgNodesA1.contains(l4));
    assertTrue(dfgNodesA2.contains(l0));
    assertTrue(dfgNodesA2.contains(l1));
    assertTrue(dfgNodesA2.contains(l2));
    assertTrue(dfgNodesA2.contains(l3));
    assertFalse(dfgNodesA2.contains(l4));
}
Also used : Path(java.nio.file.Path) NodeComparator(de.fraunhofer.aisec.cpg.helpers.NodeComparator) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test)

Aggregations

MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)10 Test (org.junit.jupiter.api.Test)10 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)9 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)8 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)8 CallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)5 Path (java.nio.file.Path)4 FieldDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration)3 MemberExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression)3 TestUtils (de.fraunhofer.aisec.cpg.TestUtils)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Assertions (org.junit.jupiter.api.Assertions)2 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)1 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)1 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)1 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)1 DeclaredReferenceExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression)1 NodeComparator (de.fraunhofer.aisec.cpg.helpers.NodeComparator)1 Strategy (de.fraunhofer.aisec.cpg.processing.strategy.Strategy)1