Search in sources :

Example 1 with Node

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

the class ConstructorsTest method testCPPFullDefault.

@Test
void testCPPFullDefault() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "defaultarg", "constructorDefault.cpp").toFile()), topLevel, true);
    List<ConstructorDeclaration> constructors = TestUtils.subnodesOfType(result, ConstructorDeclaration.class);
    List<VariableDeclaration> variables = TestUtils.subnodesOfType(result, VariableDeclaration.class);
    ConstructorDeclaration twoDefaultArg = TestUtils.findByUniquePredicate(constructors, c -> c.getDefaultParameters().size() == 2 && c.getName().equals("D"));
    Literal<?> literal0 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(0));
    Literal<?> literal1 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(1));
    VariableDeclaration d1 = TestUtils.findByUniqueName(variables, "d1");
    assertTrue(d1.getInitializer() instanceof ConstructExpression);
    ConstructExpression d1Initializer = (ConstructExpression) d1.getInitializer();
    assertEquals(twoDefaultArg, d1Initializer.getConstructor());
    assertEquals(0, d1Initializer.getArguments().size());
    assertTrue(twoDefaultArg.getNextEOG().contains(literal0));
    assertTrue(twoDefaultArg.getNextEOG().contains(literal1));
    assertTrue(literal0.getNextEOG().contains(literal1));
    for (Node node : twoDefaultArg.getNextEOG()) {
        if (!(node.equals(literal0) || node.equals(literal1))) {
            assertTrue(literal1.getNextEOG().contains(node));
        }
    }
    VariableDeclaration d2 = TestUtils.findByUniqueName(variables, "d2");
    assertTrue(d2.getInitializer() instanceof ConstructExpression);
    ConstructExpression d2Initializer = (ConstructExpression) d2.getInitializer();
    assertEquals(twoDefaultArg, d2Initializer.getConstructor());
    assertEquals(1, d2Initializer.getArguments().size());
    assertEquals(2, ((Literal) d2Initializer.getArguments().get(0)).getValue());
    VariableDeclaration d3 = TestUtils.findByUniqueName(variables, "d3");
    assertTrue(d3.getInitializer() instanceof ConstructExpression);
    ConstructExpression d3Initializer = (ConstructExpression) d3.getInitializer();
    assertEquals(twoDefaultArg, d3Initializer.getConstructor());
    assertEquals(2, d3Initializer.getArguments().size());
    assertEquals(3, ((Literal) d3Initializer.getArguments().get(0)).getValue());
    assertEquals(4, ((Literal) d3Initializer.getArguments().get(1)).getValue());
}
Also used : ConstructorDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration) Node(de.fraunhofer.aisec.cpg.graph.Node) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Example 2 with Node

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

the class ConstructorsTest method testCPPImplicitCast.

@Test
void testCPPImplicitCast() throws Exception {
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "implicitcastarg", "constructorImplicit.cpp").toFile()), topLevel, true);
    List<ConstructorDeclaration> constructors = TestUtils.subnodesOfType(result, ConstructorDeclaration.class);
    List<VariableDeclaration> variables = TestUtils.subnodesOfType(result, VariableDeclaration.class);
    ConstructorDeclaration implicitConstructor = TestUtils.findByUniquePredicate(constructors, c -> c.getName().equals("I"));
    Literal<?> literal10 = TestUtils.findByUniquePredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(10));
    VariableDeclaration i1 = TestUtils.findByUniqueName(variables, "i1");
    assertTrue(i1.getInitializer() instanceof ConstructExpression);
    ConstructExpression i1Constructor = (ConstructExpression) i1.getInitializer();
    assertFalse(i1Constructor.isImplicit());
    assertEquals(implicitConstructor, i1Constructor.getConstructor());
    assertEquals(1, i1Constructor.getArguments().size());
    assertTrue(i1Constructor.getArguments().get(0) instanceof CastExpression);
    CastExpression i1ConstructorArgument = (CastExpression) i1Constructor.getArguments().get(0);
    assertEquals("int", i1ConstructorArgument.getCastType().getName());
    assertEquals("1.0", i1ConstructorArgument.getExpression().getCode());
    assertEquals("double", i1ConstructorArgument.getExpression().getType().getName());
    ConstructorDeclaration implicitConstructorWithDefault = TestUtils.findByUniquePredicate(constructors, c -> c.getName().equals("H"));
    VariableDeclaration h1 = TestUtils.findByUniqueName(variables, "h1");
    assertTrue(h1.getInitializer() instanceof ConstructExpression);
    ConstructExpression h1Constructor = (ConstructExpression) h1.getInitializer();
    assertFalse(h1Constructor.isImplicit());
    assertEquals(implicitConstructorWithDefault, h1Constructor.getConstructor());
    assertEquals(1, h1Constructor.getArguments().size());
    assertTrue(h1Constructor.getArguments().get(0) instanceof CastExpression);
    CastExpression h1ConstructorArgument1 = (CastExpression) h1Constructor.getArguments().get(0);
    assertEquals("int", h1ConstructorArgument1.getCastType().getName());
    assertEquals("2.0", h1ConstructorArgument1.getExpression().getCode());
    assertEquals("double", h1ConstructorArgument1.getExpression().getType().getName());
    assertTrue(implicitConstructorWithDefault.getNextEOG().contains(literal10));
    for (Node node : implicitConstructorWithDefault.getNextEOG()) {
        if (!node.equals(literal10)) {
            assertTrue(literal10.getNextEOG().contains(node));
        }
    }
}
Also used : ConstructorDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration) Node(de.fraunhofer.aisec.cpg.graph.Node) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Example 3 with Node

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

the class DemoTests method testPartial.

@Test
void testPartial() throws Exception {
    Path topLevel = Paths.get("src/test/resources/partial");
    File[] files = Files.walk(topLevel, Integer.MAX_VALUE).map(Path::toFile).filter(File::isFile).filter(f -> f.getName().endsWith(".java")).toArray(File[]::new);
    TranslationConfiguration config = TranslationConfiguration.builder().sourceLocations(files).topLevel(topLevel.toFile()).defaultPasses().defaultLanguages().debugParser(true).failOnError(true).build();
    TranslationManager analyzer = TranslationManager.builder().config(config).build();
    TranslationResult result = analyzer.analyze().get();
    for (Node node : result.getTranslationUnits()) {
        assertNotNull(node);
    }
}
Also used : Path(java.nio.file.Path) Test(org.junit.jupiter.api.Test) TranslationConfiguration(de.fraunhofer.aisec.cpg.TranslationConfiguration) Files(java.nio.file.Files) Paths(java.nio.file.Paths) Assertions(org.junit.jupiter.api.Assertions) TranslationManager(de.fraunhofer.aisec.cpg.TranslationManager) Node(de.fraunhofer.aisec.cpg.graph.Node) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Path(java.nio.file.Path) TranslationResult(de.fraunhofer.aisec.cpg.TranslationResult) File(java.io.File) Node(de.fraunhofer.aisec.cpg.graph.Node) TranslationManager(de.fraunhofer.aisec.cpg.TranslationManager) TranslationResult(de.fraunhofer.aisec.cpg.TranslationResult) File(java.io.File) TranslationConfiguration(de.fraunhofer.aisec.cpg.TranslationConfiguration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Example 4 with Node

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

the class VRUtil method assertUsageOfMemberAndBase.

/**
 * Asserts that {@code usingNode} uses/references the provided {@code usedBase} and {@code
 * usedMember}. If {@link VRUtil#ENFORCE_MEMBER_EXPRESSION} is true, {@code usingNode} must be a
 * {@link MemberExpression} where {@link MemberExpression#base} uses {@code usedBase} and {@link
 * MemberExpression#refersTo} uses {@code usedMember}. Using is checked as preformed per {@link
 * VRUtil#assertUsageOf(Node,Node)}
 *
 * @param usingNode - Node that uses some member
 * @param usedBase - The expected base that is used
 * @param usedMember - THe expected member that is used
 */
public static void assertUsageOfMemberAndBase(Node usingNode, Node usedBase, Node usedMember) {
    assertNotNull(usingNode);
    if (!(usingNode instanceof MemberExpression) && !ENFORCE_MEMBER_EXPRESSION) {
        // Assumtion here is that the target of the member portion of the expression and not the base
        // is resolved
        assertUsageOf(usingNode, usedMember);
    } else {
        assertTrue(usingNode instanceof MemberExpression);
        MemberExpression memberExpression = (MemberExpression) usingNode;
        Node base = memberExpression.getBase();
        assertUsageOf(base, usedBase);
        assertUsageOf(memberExpression.getRefersTo(), usedMember);
    }
}
Also used : MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) Node(de.fraunhofer.aisec.cpg.graph.Node)

Example 5 with Node

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

Aggregations

Node (de.fraunhofer.aisec.cpg.graph.Node)17 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)11 Test (org.junit.jupiter.api.Test)11 CallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)8 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)7 Literal (de.fraunhofer.aisec.cpg.graph.statements.expressions.Literal)6 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)5 File (java.io.File)5 TranslationConfiguration (de.fraunhofer.aisec.cpg.TranslationConfiguration)3 TranslationManager (de.fraunhofer.aisec.cpg.TranslationManager)3 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)3 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)3 MemberExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression)3 NodeComparator (de.fraunhofer.aisec.cpg.helpers.NodeComparator)3 Collectors (java.util.stream.Collectors)3 TestUtils (de.fraunhofer.aisec.cpg.TestUtils)2 SubGraph (de.fraunhofer.aisec.cpg.graph.SubGraph)2 FieldDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration)2 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)2 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)2