use of de.fraunhofer.aisec.cpg.helpers.NodeComparator in project cpg by Fraunhofer-AISEC.
the class EOGTest method translateToNodes.
/**
* Translates the given file into CPG and returns the graph. Extracted to reduce code duplicates
*
* @param path - path for the file to test.
*/
private List<Node> translateToNodes(String path) throws Exception {
File toTranslate = new File(path);
Path topLevel = toTranslate.getParentFile().toPath();
TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(toTranslate), topLevel, true);
List<Node> nodes = SubgraphWalker.flattenAST(tu);
// Todo until explicitly added Return Statements are either removed again or code and region set
// properly
nodes = nodes.stream().filter(node -> node.getCode() != null).collect(Collectors.toList());
nodes.sort(new NodeComparator());
return nodes;
}
use of de.fraunhofer.aisec.cpg.helpers.NodeComparator in project cpg by Fraunhofer-AISEC.
the class DFGTest method testSensitivityWithLabels.
@Test
void testSensitivityWithLabels() throws Exception {
Path topLevel = Path.of("src", "test", "resources", "dfg");
TranslationUnitDeclaration result = TestUtils.analyze(List.of(topLevel.resolve("LoopDFGs.java").toFile()), topLevel, true).get(0);
MethodDeclaration looping = TestUtils.getSubnodeOfTypeWithName(result, MethodDeclaration.class, "labeledBreakContinue");
List<Node> methodNodes = SubgraphWalker.flattenAST(looping);
Literal l0 = getLiteral(methodNodes, 0);
Literal l1 = getLiteral(methodNodes, 1);
Literal l2 = getLiteral(methodNodes, 2);
Literal l3 = getLiteral(methodNodes, 3);
Literal l4 = getLiteral(methodNodes, 4);
List<Node> calls = TestUtils.findByPredicate(SubgraphWalker.flattenAST(looping), n -> n instanceof CallExpression && n.getName().equals("println"));
calls.sort(new NodeComparator());
Set<Node> dfgNodesA0 = flattenDFGGraph(TestUtils.getSubnodeOfTypeWithName(calls.get(0), DeclaredReferenceExpression.class, "a"), false);
Set<Node> dfgNodesA1 = flattenDFGGraph(TestUtils.getSubnodeOfTypeWithName(calls.get(1), DeclaredReferenceExpression.class, "a"), false);
Set<Node> dfgNodesA2 = flattenDFGGraph(TestUtils.getSubnodeOfTypeWithName(calls.get(2), DeclaredReferenceExpression.class, "a"), false);
assertTrue(dfgNodesA0.contains(l0));
assertTrue(dfgNodesA0.contains(l1));
assertTrue(dfgNodesA0.contains(l3));
assertFalse(dfgNodesA0.contains(l4));
assertTrue(dfgNodesA1.contains(l0));
assertTrue(dfgNodesA1.contains(l1));
assertTrue(dfgNodesA1.contains(l3));
assertFalse(dfgNodesA1.contains(l4));
assertTrue(dfgNodesA2.contains(l0));
assertTrue(dfgNodesA2.contains(l1));
assertTrue(dfgNodesA2.contains(l2));
assertTrue(dfgNodesA2.contains(l3));
assertFalse(dfgNodesA2.contains(l4));
}
use of de.fraunhofer.aisec.cpg.helpers.NodeComparator 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);
}
}
use of de.fraunhofer.aisec.cpg.helpers.NodeComparator in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testSwitch.
@Test
void testSwitch() throws Exception {
File file = new File("src/test/resources/cfg/switch.cpp");
TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
List<Node> graphNodes = SubgraphWalker.flattenAST(declaration);
graphNodes.sort(new NodeComparator());
assertTrue(graphNodes.size() != 0);
List<SwitchStatement> switchStatements = Util.filterCast(graphNodes, SwitchStatement.class);
assertTrue(switchStatements.size() == 3);
SwitchStatement switchStatement = switchStatements.get(0);
assertTrue(((CompoundStatement) switchStatement.getStatement()).getStatements().size() == 11);
List<CaseStatement> caseStatements = Util.filterCast(SubgraphWalker.flattenAST(switchStatement), CaseStatement.class);
assertTrue(caseStatements.size() == 4);
List<DefaultStatement> defaultStatements = Util.filterCast(SubgraphWalker.flattenAST(switchStatement), DefaultStatement.class);
assertTrue(defaultStatements.size() == 1);
}
use of de.fraunhofer.aisec.cpg.helpers.NodeComparator in project cpg by Fraunhofer-AISEC.
the class VariableResolverCppTest method initTests.
@BeforeAll
void initTests() throws ExecutionException, InterruptedException {
final String topLevelPath = "src/test/resources/variables_extended/cpp/";
List<String> fileNames = Arrays.asList("scope_variables.cpp", "external_class.cpp");
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().debugParser(true).defaultLanguages().failOnError(true).loadIncludes(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, "ExternalClass");
externVarName = TestUtils.getSubnodeOfTypeWithName(externalClass, FieldDeclaration.class, "varName");
externStaticVarName = TestUtils.getSubnodeOfTypeWithName(externalClass, FieldDeclaration.class, "staticVarName");
outerClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "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();
List<RecordDeclaration> classes = Util.filterCast(nodes, RecordDeclaration.class);
// Inner class and its fields
innerClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "ScopeVariables::InnerClass");
innerVarName = innerClass.getFields().stream().filter(n -> n.getName().equals("varName")).findFirst().get();
innerStaticVarName = innerClass.getFields().stream().filter(n -> n.getName().equals("staticVarName")).findFirst().get();
innerImpThis = innerClass.getFields().stream().filter(n -> n.getName().equals("this")).findFirst().get();
main = TestUtils.getOfTypeWithName(nodes, FunctionDeclaration.class, "main");
// Functions in the outer and inner object
outerFunction1 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function1")).collect(Collectors.toList()).get(0);
forStatements = Util.filterCast(SubgraphWalker.flattenAST(outerFunction1), ForStatement.class);
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);
outerFunction5 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function5")).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);
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);
}
}
Aggregations