Skip to content
Snippets Groups Projects
Commit 4e1444e2 authored by Guillaume Blanc's avatar Guillaume Blanc Committed by Guillaume Blanc
Browse files

Factorize code in AbstractTest

parent 6d1fc666
No related branches found
No related tags found
1 merge request!485Avatar security tests
......@@ -3,6 +3,7 @@ package test;
import myutil.Conversion;
import myutil.FileException;
import myutil.FileUtils;
import myutil.TraceManager;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
......@@ -12,6 +13,8 @@ import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;
......@@ -19,6 +22,7 @@ import static java.io.File.pathSeparator;
import static java.lang.System.getenv;
import static java.nio.file.Files.isExecutable;
import static java.util.regex.Pattern.quote;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public abstract class AbstractTest {
......@@ -29,12 +33,21 @@ public abstract class AbstractTest {
protected static String INPUT_DIR;
protected static String EXPECTED_CODE_DIR;
protected static String ACTUAL_CODE_DIR;
protected static final String PROVERIF_QUERY_PREFIX = "Query";
protected static final String PROVERIF_SUMMARY = "Verification summary:";
protected static String getBaseResourcesDir() {
final String systemPropResDir = System.getProperty("resources_dir");
if (systemPropResDir == null) {
return "resources/test/";
String isGradle = System.getProperty("org.gradle.test.worker");
if (isGradle == null) {
return "test/resources/";
}
else {
return "resources/test/";
}
}
return systemPropResDir;
......@@ -199,4 +212,63 @@ public abstract class AbstractTest {
}
public boolean executeAndVerifyPvspec (String pvspecPath, String goldenPath){
String cmd = "proverif -in pitype " + pvspecPath;
Process process;
BufferedReader cmdOutput;
try {
process = Runtime.getRuntime().exec(cmd);
cmdOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
List<String> golden = generateGoldenList(goldenPath);
String outputLine;
int checked = 0;
boolean summaryFound = false;
while ((outputLine = cmdOutput.readLine()) != null){
if (summaryFound){
if(outputLine.startsWith(PROVERIF_QUERY_PREFIX)){
if (golden.contains(outputLine)) {
checked += 1;
} else {
checked = -1;
break;
}
}
} else {
if (outputLine.contains(PROVERIF_SUMMARY)){
summaryFound = true;
}
}
}
return golden.size() == checked;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private List<String> generateGoldenList(String goldenPath) throws IOException {
BufferedReader goldenFile = null;
try {
goldenFile = new BufferedReader(new FileReader(goldenPath));
} catch (FileNotFoundException e) {
TraceManager.addDev("File " + goldenPath + " not found");
throw new RuntimeException(e);
}
List<String> goldenList = new ArrayList<>();
String line;
while ((line = goldenFile.readLine()) != null){
if (line.startsWith(PROVERIF_QUERY_PREFIX))
goldenList.add(line);
}
return goldenList;
}
}
......@@ -25,8 +25,6 @@ public class AvatarSecurityTests extends AbstractUITest {
final static String OUTPUT_FOLDER = "output/";
final static String PVSPEC_SUFFIX = "_pvspec";
static String OUTPUT_DIR;
static final String PROVERIF_SUMMARY = "Verification summary:";
static final String PROVERIF_QUERY_PREFIX = "Query";
final static String[] MODELS = {
......@@ -35,11 +33,8 @@ public class AvatarSecurityTests extends AbstractUITest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
String test = System.getProperty("org.gradle.test.worker");
String baseResourcesDir = getBaseResourcesDir();
if (test == null){
baseResourcesDir = "test/resources/";
}
RESOURCES_DIR = baseResourcesDir + RES_FOLDER;
INPUT_DIR = RESOURCES_DIR + INPUT_FOLDER;
......@@ -73,62 +68,9 @@ public class AvatarSecurityTests extends AbstractUITest {
boolean pvspecGenerated = mainGUI.gtm.generateProVerifFromAVATAR(pvspecPath, 0, true, true);
assertTrue(pvspecGenerated);
String cmd = "proverif -in pitype " + pvspecPath;
String goldenPath = INPUT_DIR + model + GOLDEN_SUFFIX;
Process process;
BufferedReader cmdOutput;
try {
process = Runtime.getRuntime().exec(cmd);
cmdOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
List<String> golden = generateGoldenList(goldenPath);
String outputLine;
int checked = 0;
boolean summaryFound = false;
while ((outputLine = cmdOutput.readLine()) != null){
if (summaryFound){
if(outputLine.startsWith(PROVERIF_QUERY_PREFIX)){
if (golden.contains(outputLine)) {
checked += 1;
} else {
checked = -1;
break;
}
}
} else {
if (outputLine.contains(PROVERIF_SUMMARY)){
summaryFound = true;
}
}
}
assertEquals(golden.size(), checked);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private List<String> generateGoldenList(String goldenPath) throws IOException {
BufferedReader goldenFile = null;
try {
goldenFile = new BufferedReader(new FileReader(goldenPath));
} catch (FileNotFoundException e) {
TraceManager.addDev("File " + goldenPath + " not found");
throw new RuntimeException(e);
}
List<String> goldenList = new ArrayList<>();
String line;
while ((line = goldenFile.readLine()) != null){
if (line.startsWith(PROVERIF_QUERY_PREFIX))
goldenList.add(line);
}
return goldenList;
assertTrue(executeAndVerifyPvspec(pvspecPath, goldenPath));
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment