use of de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression in project cpg by Fraunhofer-AISEC.
the class FunctionPointerTest method test.
void test(String language) throws Exception {
List<TranslationUnitDeclaration> result = analyze(language);
List<FunctionDeclaration> functions = TestUtils.subnodesOfType(result, FunctionDeclaration.class);
FunctionDeclaration main = TestUtils.findByUniqueName(functions, "main");
List<CallExpression> calls = TestUtils.subnodesOfType(main, CallExpression.class);
FunctionDeclaration noParam = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("target") && f.getParameters().isEmpty());
FunctionDeclaration singleParam = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("target") && f.getParameters().size() == 1);
FunctionDeclaration noParamUnknown = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("fun") && f.getParameters().isEmpty());
FunctionDeclaration singleParamUnknown = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("fun") && f.getParameters().size() == 1);
Pattern pattern = Pattern.compile("\\((?<member>.+)?\\*(?<obj>.+\\.)?(?<func>.+)\\)");
for (CallExpression call : calls) {
if (call instanceof ConstructExpression) {
continue;
}
String func;
if (!call.getName().contains("(")) {
func = call.getName();
assertNotEquals("", func, "Unexpected call " + func);
} else {
Matcher matcher = pattern.matcher(call.getName());
assertTrue(matcher.matches(), "Unexpected call " + call.getName());
func = matcher.group("func");
}
switch(func) {
case "no_param":
case "no_param_uninitialized":
case "no_param_field":
case "no_param_field_uninitialized":
assertEquals(List.of(noParam), call.getInvokes());
break;
case "single_param":
case "single_param_uninitialized":
case "single_param_field":
case "single_param_field_uninitialized":
assertEquals(List.of(singleParam), call.getInvokes());
break;
case "no_param_unknown":
case "no_param_unknown_uninitialized":
case "no_param_unknown_field":
case "no_param_unknown_field_uninitialized":
assertEquals(List.of(noParamUnknown), call.getInvokes());
assertTrue(noParamUnknown.isInferred());
break;
case "single_param_unknown":
case "single_param_unknown_uninitialized":
case "single_param_unknown_field":
case "single_param_unknown_field_uninitialized":
assertEquals(List.of(singleParamUnknown), call.getInvokes());
assertTrue(singleParamUnknown.isInferred());
break;
default:
fail("Unexpected call " + call.getName());
}
List<VariableDeclaration> variables = TestUtils.subnodesOfType(result, VariableDeclaration.class);
for (VariableDeclaration variable : variables) {
switch(variable.getName()) {
case "no_param_unused":
case "no_param_unused_field":
case "no_param_unused_uninitialized":
assertEquals(noParam, getSourceFunction(variable));
break;
case "single_param_unused":
case "single_param_unused_field":
case "single_param_unused_field_uninitialized":
assertEquals(singleParam, getSourceFunction(variable));
break;
}
}
}
}
Aggregations