Search in sources :

Example 1 with RecordDeclaration

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

the class TypedefTest method testMemberTypeDef.

@Test
void testMemberTypeDef() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze("cpp", topLevel, true);
    List<ValueDeclaration> variables = TestUtils.subnodesOfType(result, ValueDeclaration.class);
    List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
    RecordDeclaration addConst = TestUtils.findByUniqueName(records, "add_const");
    ValueDeclaration typeMember1 = TestUtils.findByUniqueName(addConst.getFields(), "typeMember1");
    ValueDeclaration typeMember2 = TestUtils.findByUniqueName(addConst.getFields(), "typeMember2");
    assertEquals(typeMember1.getType(), typeMember2.getType());
    ValueDeclaration typeMemberOutside = TestUtils.findByUniqueName(variables, "typeMemberOutside");
    assertNotEquals(typeMemberOutside.getType(), typeMember2.getType());
    ValueDeclaration cptr1 = TestUtils.findByUniqueName(variables, "cptr1");
    ValueDeclaration cptr2 = TestUtils.findByUniqueName(variables, "cptr2");
    assertEquals(cptr1.getType(), cptr2.getType());
    assertNotEquals(typeMemberOutside.getType(), cptr2.getType());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) ValueDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ValueDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Example 2 with RecordDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration 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 3 with RecordDeclaration

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

the class VisitorTest method testReturnStmtVisitor.

/**
 * Visits only ReturnStatement nodes.
 */
@Test
void testReturnStmtVisitor() {
    List<ReturnStatement> returnStmts = new ArrayList<>();
    RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
    recordDeclaration.accept(Strategy::AST_FORWARD, new IVisitor<Node>() {

        public void visit(ReturnStatement r) {
            returnStmts.add(r);
        }
    });
    assertEquals(2, returnStmts.size());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) 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 4 with RecordDeclaration

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

the class SuperCallTest method testNoExcessFields.

@Test
void testNoExcessFields() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel, true);
    List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
    RecordDeclaration superClass = TestUtils.findByUniqueName(records, "SuperClass");
    assertEquals(2, superClass.getFields().size());
    assertEquals(Set.of("this", "field"), superClass.getFields().stream().map(Node::getName).collect(Collectors.toSet()));
    RecordDeclaration subClass = TestUtils.findByUniqueName(records, "SubClass");
    assertEquals(2, subClass.getFields().size());
    assertEquals(Set.of("this", "field"), subClass.getFields().stream().map(Node::getName).collect(Collectors.toSet()));
    RecordDeclaration inner = TestUtils.findByUniqueName(records, "SubClass.Inner");
    assertEquals(1, inner.getFields().size());
    assertEquals(Set.of("this"), inner.getFields().stream().map(Node::getName).collect(Collectors.toSet()));
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 5 with RecordDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration 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)

Aggregations

RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)21 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)18 Test (org.junit.jupiter.api.Test)18 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)15 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)10 File (java.io.File)8 FieldDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration)6 CallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)6 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)4 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)4 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)4 MemberExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression)4 List (java.util.List)4 TestUtils (de.fraunhofer.aisec.cpg.TestUtils)3 NamespaceDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration)3 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)3 DeclaredReferenceExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression)3 Strategy (de.fraunhofer.aisec.cpg.processing.strategy.Strategy)3 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)2 TranslationConfiguration (de.fraunhofer.aisec.cpg.TranslationConfiguration)2