Search in sources :

Example 1 with VariableDeclaration

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

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

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

the class CXXLiteralTest method testLargeNegativeNumber.

@Test
void testLargeNegativeNumber() throws Exception {
    File file = new File("src/test/resources/largenegativenumber.cpp");
    TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
    assertFalse(main.isEmpty());
    FunctionDeclaration funcDecl = main.iterator().next();
    VariableDeclaration a = funcDecl.getVariableDeclarationByName("a").orElse(null);
    assertNotNull(a);
    assertEquals(1, ((Literal) Objects.requireNonNull(a.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
    // there are no negative literals, so the construct "-2147483648" is
    // a unary expression and the literal "2147483648". Since "2147483648" is too large to fit
    // in an integer, it should be automatically converted to a long. The resulting value
    // -2147483648 however is small enough to fit into an int, so it is ok for the variable a to
    // have an int type
    VariableDeclaration b = funcDecl.getVariableDeclarationByName("b").orElse(null);
    assertNotNull(b);
    assertEquals(2147483648L, ((Literal) Objects.requireNonNull(b.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
    VariableDeclaration c = funcDecl.getVariableDeclarationByName("c").orElse(null);
    assertNotNull(c);
    assertEquals(2147483649L, ((Literal) Objects.requireNonNull(c.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
    VariableDeclaration d = funcDecl.getVariableDeclarationByName("d").orElse(null);
    assertNotNull(d);
    assertEquals(new BigInteger("9223372036854775808"), ((Literal) Objects.requireNonNull(d.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) BigInteger(java.math.BigInteger) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with VariableDeclaration

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

the class DFGTest method testControlSensitiveDFGPassSwitch.

@Test
void testControlSensitiveDFGPassSwitch() throws Exception {
    Path topLevel = Path.of("src", "test", "resources", "dfg");
    List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(topLevel.resolve("ControlFlowSensitiveDFGSwitch.java").toFile()), topLevel, true);
    VariableDeclaration a = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, VariableDeclaration.class), v -> v.getName().equals("a")).get(0);
    VariableDeclaration b = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, VariableDeclaration.class), v -> v.getName().equals("b")).get(0);
    DeclaredReferenceExpression ab = (DeclaredReferenceExpression) b.getPrevEOG().get(0);
    DeclaredReferenceExpression a10 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), dre -> TestUtils.compareLineFromLocationIfExists(dre, true, 8)).get(0);
    DeclaredReferenceExpression a11 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), dre -> TestUtils.compareLineFromLocationIfExists(dre, true, 11)).get(0);
    DeclaredReferenceExpression a12 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), dre -> TestUtils.compareLineFromLocationIfExists(dre, true, 14)).get(0);
    Literal<?> literal0 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(0)).get(0);
    Literal<?> literal10 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(10)).get(0);
    Literal<?> literal11 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(11)).get(0);
    Literal<?> literal12 = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, Literal.class), l -> l.getValue().equals(12)).get(0);
    assertEquals(3, literal10.getNextDFG().size());
    assertTrue(literal10.getNextDFG().contains(a10));
    assertEquals(3, literal11.getNextDFG().size());
    assertTrue(literal11.getNextDFG().contains(a11));
    assertEquals(4, literal12.getNextDFG().size());
    assertTrue(literal12.getNextDFG().contains(a12));
    assertEquals(4, a.getPrevDFG().size());
    assertTrue(a.getPrevDFG().contains(literal0));
    assertTrue(a.getPrevDFG().contains(a10));
    assertTrue(a.getPrevDFG().contains(a11));
    assertTrue(a.getPrevDFG().contains(a12));
    assertTrue(ab.getPrevDFG().contains(literal0));
    assertTrue(ab.getPrevDFG().contains(literal10));
    assertTrue(ab.getPrevDFG().contains(literal11));
    assertTrue(ab.getPrevDFG().contains(literal12));
    assertEquals(1, ab.getNextDFG().size());
    assertTrue(ab.getNextDFG().contains(b));
    // Fallthrough test
    CallExpression println = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, CallExpression.class), c -> c.getName().equals("println")).get(0);
    DeclaredReferenceExpression aPrintln = TestUtils.findByPredicate(TestUtils.subnodesOfType(result, DeclaredReferenceExpression.class), e -> e.getNextEOG().contains(println)).get(0);
    assertEquals(2, aPrintln.getPrevDFG().size());
    assertTrue(aPrintln.getPrevDFG().contains(literal0));
    assertTrue(aPrintln.getPrevDFG().contains(literal12));
}
Also used : Path(java.nio.file.Path) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) TestUtils(de.fraunhofer.aisec.cpg.TestUtils) Set(java.util.Set) de.fraunhofer.aisec.cpg.graph(de.fraunhofer.aisec.cpg.graph) NodeComparator(de.fraunhofer.aisec.cpg.helpers.NodeComparator) de.fraunhofer.aisec.cpg.graph.statements.expressions(de.fraunhofer.aisec.cpg.graph.statements.expressions) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SubgraphWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test)

Example 5 with VariableDeclaration

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

the class VariableResolverCppTest method testMemberAccessedOverInstance.

@Test
void testMemberAccessedOverInstance() {
    VariableDeclaration declaration = TestUtils.getSubnodeOfTypeWithName(outerFunction2, VariableDeclaration.class, "scopeVariables");
    VRUtil.assertUsageOfMemberAndBase(callParamMap.get("func2_instance_varName"), declaration, outerVarName);
}
Also used : ParamVariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)50 Test (org.junit.jupiter.api.Test)45 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)40 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)25 ParamVariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ParamVariableDeclaration)21 File (java.io.File)14 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)13 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)8 DeclarationStatement (de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement)7 de.fraunhofer.aisec.cpg.graph (de.fraunhofer.aisec.cpg.graph)6 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)6 IfStatement (de.fraunhofer.aisec.cpg.graph.statements.IfStatement)6 TryStatement (de.fraunhofer.aisec.cpg.graph.statements.TryStatement)6 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)5 CaseStatement (de.fraunhofer.aisec.cpg.graph.statements.CaseStatement)5 DefaultStatement (de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement)5 ForEachStatement (de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement)5 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)5 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)5 SwitchStatement (de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement)5