Search in sources :

Example 1 with CallExpression

use of de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression 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 2 with CallExpression

use of de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression 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 3 with CallExpression

use of de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression 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 4 with CallExpression

use of de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression in project cpg by Fraunhofer-AISEC.

the class VariableResolverJavaTest method initTests.

@BeforeAll
void initTests() throws ExecutionException, InterruptedException {
    final String topLevelPath = "src/test/resources/variables_extended/java/";
    List<String> fileNames = Arrays.asList("ScopeVariables.java", "ExternalClass.java");
    List<File> fileLocations = fileNames.stream().map(fileName -> new File(topLevelPath + fileName)).collect(Collectors.toList());
    TranslationConfiguration config = TranslationConfiguration.builder().sourceLocations(fileLocations.toArray(new File[fileNames.size()])).topLevel(new File(topLevelPath)).defaultPasses().defaultLanguages().debugParser(true).failOnError(true).build();
    TranslationManager analyzer = TranslationManager.builder().config(config).build();
    List<TranslationUnitDeclaration> tu = analyzer.analyze().get().getTranslationUnits();
    List<Node> nodes = tu.stream().flatMap(tUnit -> SubgraphWalker.flattenAST(tUnit).stream()).collect(Collectors.toList());
    List<CallExpression> calls = TestUtils.findByName(Util.filterCast(nodes, CallExpression.class), "printLog");
    calls.sort(new NodeComparator());
    List<RecordDeclaration> records = Util.filterCast(nodes, RecordDeclaration.class);
    // Extract all Variable declarations and field declarations for matching
    externalClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "variables_extended.ExternalClass");
    externVarName = TestUtils.getSubnodeOfTypeWithName(externalClass, FieldDeclaration.class, "varName");
    externStaticVarName = TestUtils.getSubnodeOfTypeWithName(externalClass, FieldDeclaration.class, "staticVarName");
    outerClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "variables_extended.ScopeVariables");
    outerVarName = outerClass.getFields().stream().filter(n -> n.getName().equals("varName")).findFirst().get();
    outerStaticVarName = outerClass.getFields().stream().filter(n -> n.getName().equals("staticVarName")).findFirst().get();
    outerImpThis = outerClass.getFields().stream().filter(n -> n.getName().equals("this")).findFirst().get();
    // Inner class and its fields
    innerClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "variables_extended.ScopeVariables.InnerClass");
    innerVarName = innerClass.getFields().stream().filter(n -> n.getName().equals("varName")).findFirst().get();
    innerStaticVarName = TestUtils.getSubnodeOfTypeWithName(innerClass, FieldDeclaration.class, "staticVarName");
    innerImpThis = TestUtils.getSubnodeOfTypeWithName(innerClass, FieldDeclaration.class, "this");
    innerImpOuter = TestUtils.getSubnodeOfTypeWithName(innerClass, FieldDeclaration.class, "ScopeVariables.this");
    main = TestUtils.getSubnodeOfTypeWithName(outerClass, MethodDeclaration.class, "main");
    outerFunction1 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function1")).collect(Collectors.toList()).get(0);
    forStatements = Util.filterCast(SubgraphWalker.flattenAST(outerFunction1), ForStatement.class);
    // Functions i nthe outer and inner object
    outerFunction2 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function2")).collect(Collectors.toList()).get(0);
    outerFunction3 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function3")).collect(Collectors.toList()).get(0);
    outerFunction4 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function4")).collect(Collectors.toList()).get(0);
    innerFunction1 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function1")).collect(Collectors.toList()).get(0);
    innerFunction2 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function2")).collect(Collectors.toList()).get(0);
    innerFunction3 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function3")).collect(Collectors.toList()).get(0);
    for (CallExpression call : calls) {
        Expression first = call.getArguments().get(0);
        String logId = ((Literal) first).getValue().toString();
        Expression second = call.getArguments().get(1);
        callParamMap.put(logId, second);
    }
}
Also used : Arrays(java.util.Arrays) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) HashMap(java.util.HashMap) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) NodeComparator(de.fraunhofer.aisec.cpg.helpers.NodeComparator) Literal(de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal) ForStatement(de.fraunhofer.aisec.cpg.graph.statements.ForStatement) TestInstance(org.junit.jupiter.api.TestInstance) BeforeAll(org.junit.jupiter.api.BeforeAll) Util(de.fraunhofer.aisec.cpg.helpers.Util) Map(java.util.Map) SubgraphWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker) Node(de.fraunhofer.aisec.cpg.graph.Node) ParamVariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) TestUtils(de.fraunhofer.aisec.cpg.TestUtils) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) Collectors(java.util.stream.Collectors) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) Expression(de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression) ValueDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ValueDeclaration) List(java.util.List) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) TranslationConfiguration(de.fraunhofer.aisec.cpg.TranslationConfiguration) TranslationManager(de.fraunhofer.aisec.cpg.TranslationManager) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) NodeComparator(de.fraunhofer.aisec.cpg.helpers.NodeComparator) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) Node(de.fraunhofer.aisec.cpg.graph.Node) TranslationManager(de.fraunhofer.aisec.cpg.TranslationManager) TranslationConfiguration(de.fraunhofer.aisec.cpg.TranslationConfiguration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) Expression(de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) ForStatement(de.fraunhofer.aisec.cpg.graph.statements.ForStatement) File(java.io.File) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 5 with CallExpression

use of de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression in project cpg by Fraunhofer-AISEC.

the class CallResolverTest method testImplicitCastMethodCallResolution.

@Test
void testImplicitCastMethodCallResolution() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "implicitcast", "implicitCastInMethod.cpp").toFile()), topLevel, true);
    List<FunctionDeclaration> functionDeclarations = TestUtils.subnodesOfType(result, FunctionDeclaration.class);
    List<CallExpression> callExpressions = TestUtils.subnodesOfType(result, CallExpression.class);
    // Check resolution of calc
    CallExpression calc = TestUtils.findByUniqueName(callExpressions, "calc");
    FunctionDeclaration calcFunctionDeclaration = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("calc") && !f.isInferred());
    assertEquals(1, calc.getInvokes().size());
    assertEquals(calcFunctionDeclaration, calc.getInvokes().get(0));
    assertTrue(calc.getArguments().get(0) instanceof CastExpression);
    assertEquals(2.0, ((Literal) ((CastExpression) calc.getArguments().get(0)).getExpression()).getValue());
    assertEquals("int", ((CastExpression) calc.getArguments().get(0)).getCastType().getName());
    // Check resolution of doSmth
    CallExpression doSmth = TestUtils.findByUniqueName(callExpressions, "doSmth");
    FunctionDeclaration doSmthFunctionDeclaration = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("doSmth") && !f.isInferred());
    assertEquals(1, doSmth.getInvokes().size());
    assertEquals(doSmthFunctionDeclaration, doSmth.getInvokes().get(0));
    assertTrue(doSmth.getArguments().get(0) instanceof CastExpression);
    assertEquals(10.0, ((Literal) ((CastExpression) doSmth.getArguments().get(0)).getExpression()).getValue());
    assertEquals("int", ((CastExpression) doSmth.getArguments().get(0)).getCastType().getName());
}
Also used : CastExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CastExpression) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

CallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)26 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)19 Test (org.junit.jupiter.api.Test)19 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)10 Node (de.fraunhofer.aisec.cpg.graph.Node)9 Literal (de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal)8 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)7 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)7 List (java.util.List)7 TestUtils (de.fraunhofer.aisec.cpg.TestUtils)6 DeclaredReferenceExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression)6 Collectors (java.util.stream.Collectors)6 CastExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CastExpression)5 File (java.io.File)5 FieldDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration)4 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)4 Path (java.nio.file.Path)4 Assertions (org.junit.jupiter.api.Assertions)4 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)3 MemberExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression)3