Search in sources :

Example 1 with PhysicalLocation

use of de.fraunhofer.aisec.cpg.sarif.PhysicalLocation in project cpg by Fraunhofer-AISEC.

the class CXXIncludeTest method testCodeAndRegionInInclude.

@Test
void testCodeAndRegionInInclude() throws Exception {
    // checks, whether code and region for nodes in includes are properly set
    File file = new File("src/test/resources/include.cpp");
    TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    Set<RecordDeclaration> someClass = tu.getDeclarationsByName("SomeClass", RecordDeclaration.class);
    assertFalse(someClass.isEmpty());
    ConstructorDeclaration decl = someClass.iterator().next().getConstructors().get(0);
    assertEquals("SomeClass();", decl.getCode());
    PhysicalLocation location = decl.getLocation();
    assertNotNull(location);
    assertEquals(new Region(16, 3, 16, 15), location.getRegion());
}
Also used : Region(de.fraunhofer.aisec.cpg.sarif.Region) File(java.io.File) PhysicalLocation(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with PhysicalLocation

use of de.fraunhofer.aisec.cpg.sarif.PhysicalLocation in project cpg by Fraunhofer-AISEC.

the class LocationConverterTest method toEntityAttributeWithInteger.

@Test
void toEntityAttributeWithInteger() {
    // arrange
    final CompositeAttributeConverter<PhysicalLocation> sut = getSut();
    final Map<String, Object> value = new HashMap<>();
    final int startLineValue = 1;
    // autoboxing to Integer
    value.put(LocationConverter.START_LINE, startLineValue);
    final int endLineValue = 2;
    value.put(LocationConverter.END_LINE, endLineValue);
    final int startColumnValue = 3;
    value.put(LocationConverter.START_COLUMN, startColumnValue);
    final int endColumnValue = 4;
    value.put(LocationConverter.END_COLUMN, endColumnValue);
    value.put(LocationConverter.ARTIFACT, URI_STRING);
    final Region region = new Region(startLineValue, startColumnValue, endLineValue, endColumnValue);
    final PhysicalLocation want = new PhysicalLocation(URI_TO_TEST, region);
    // act
    final PhysicalLocation have = sut.toEntityAttribute(value);
    // assert
    Assertions.assertEquals(want, have);
}
Also used : HashMap(java.util.HashMap) Region(de.fraunhofer.aisec.cpg.sarif.Region) PhysicalLocation(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation) Test(org.junit.jupiter.api.Test)

Example 3 with PhysicalLocation

use of de.fraunhofer.aisec.cpg.sarif.PhysicalLocation in project cpg by Fraunhofer-AISEC.

the class LocationConverterTest method toEntityAttributeWithNullStartLine.

@Test
void toEntityAttributeWithNullStartLine() {
    // arrange
    final CompositeAttributeConverter<PhysicalLocation> sut = getSut();
    final Map<String, ?> value = new HashMap<>();
    value.put(LocationConverter.START_LINE, null);
    // act
    final PhysicalLocation have = sut.toEntityAttribute(value);
    // assert
    Assertions.assertNull(have);
}
Also used : HashMap(java.util.HashMap) PhysicalLocation(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation) Test(org.junit.jupiter.api.Test)

Example 4 with PhysicalLocation

use of de.fraunhofer.aisec.cpg.sarif.PhysicalLocation in project cpg by Fraunhofer-AISEC.

the class LocationConverterTest method toEntityAttributeWithMixedTypes.

@Test
void toEntityAttributeWithMixedTypes() {
    // arrange
    final CompositeAttributeConverter<PhysicalLocation> sut = getSut();
    final Map<String, Object> value = new HashMap<>();
    final Object startLineValue = 1;
    value.put(LocationConverter.START_LINE, startLineValue);
    final Object endLineValue = (long) 2;
    value.put(LocationConverter.END_LINE, endLineValue);
    final Object startColumnValue = "3";
    value.put(LocationConverter.START_COLUMN, startColumnValue);
    final Object endColumnValue = new CustomNumber(4);
    value.put(LocationConverter.END_COLUMN, endColumnValue);
    value.put(LocationConverter.ARTIFACT, URI_STRING);
    final Region region = new Region(Integer.parseInt(startLineValue.toString()), Integer.parseInt(startColumnValue.toString()), Integer.parseInt(endLineValue.toString()), Integer.parseInt(endColumnValue.toString()));
    final PhysicalLocation want = new PhysicalLocation(URI_TO_TEST, region);
    // act
    final PhysicalLocation have = sut.toEntityAttribute(value);
    // assert
    Assertions.assertEquals(want, have);
}
Also used : HashMap(java.util.HashMap) Region(de.fraunhofer.aisec.cpg.sarif.Region) PhysicalLocation(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation) Test(org.junit.jupiter.api.Test)

Example 5 with PhysicalLocation

use of de.fraunhofer.aisec.cpg.sarif.PhysicalLocation in project cpg by Fraunhofer-AISEC.

the class LocationConverterTest method toEntityAttributeWithLong.

@Test
void toEntityAttributeWithLong() {
    // arrange
    final CompositeAttributeConverter<PhysicalLocation> sut = getSut();
    final Map<String, Object> value = new HashMap<>();
    final long startLineValue = 1;
    // autoboxing to Long
    value.put(LocationConverter.START_LINE, startLineValue);
    final long endLineValue = 2;
    value.put(LocationConverter.END_LINE, endLineValue);
    final long startColumnValue = 3;
    value.put(LocationConverter.START_COLUMN, startColumnValue);
    final long endColumnValue = 4;
    value.put(LocationConverter.END_COLUMN, endColumnValue);
    value.put(LocationConverter.ARTIFACT, URI_STRING);
    final Region region = new Region((int) startLineValue, (int) startColumnValue, (int) endLineValue, (int) endColumnValue);
    final PhysicalLocation want = new PhysicalLocation(URI_TO_TEST, region);
    // act
    final PhysicalLocation have = sut.toEntityAttribute(value);
    // assert
    Assertions.assertEquals(want, have);
}
Also used : HashMap(java.util.HashMap) Region(de.fraunhofer.aisec.cpg.sarif.Region) PhysicalLocation(de.fraunhofer.aisec.cpg.sarif.PhysicalLocation) Test(org.junit.jupiter.api.Test)

Aggregations

PhysicalLocation (de.fraunhofer.aisec.cpg.sarif.PhysicalLocation)16 Region (de.fraunhofer.aisec.cpg.sarif.Region)10 Test (org.junit.jupiter.api.Test)10 HashMap (java.util.HashMap)7 TokenRange (com.github.javaparser.TokenRange)3 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)2 NodeList (com.github.javaparser.ast.NodeList)2 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)2 AnnotationMemberDeclaration (com.github.javaparser.ast.body.AnnotationMemberDeclaration)2 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)2 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)2 InitializerDeclaration (com.github.javaparser.ast.body.InitializerDeclaration)2 Parameter (com.github.javaparser.ast.body.Parameter)2 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)2 Statement (com.github.javaparser.ast.stmt.Statement)2 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)2 ResolvedConstructorDeclaration (com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration)2 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)2