use of org.evosuite.runtime.testdata.EvoSuiteFile in project evosuite by EvoSuite.
the class FileSystemHandlingTest method createNewFileByAddingData.
@Test
public void createNewFileByAddingData() throws IOException {
RuntimeSettings.useVFS = true;
Runtime.getInstance().resetRuntime();
byte[] data = new byte[] { 42, 66 };
EvoSuiteFile file = new EvoSuiteFile("foo");
MockFile mf = new MockFile(file.getPath());
Assert.assertFalse(mf.exists());
FileSystemHandling.appendDataToFile(file, data);
Assert.assertTrue(mf.exists());
MockFileInputStream in = new MockFileInputStream(file.getPath());
byte[] buffer = new byte[4];
int count = in.read(buffer);
in.close();
Assert.assertEquals(data.length, count);
Assert.assertEquals(data[0], buffer[0]);
Assert.assertEquals(data[1], buffer[1]);
Assert.assertEquals(0, buffer[2]);
Assert.assertEquals(0, buffer[3]);
}
use of org.evosuite.runtime.testdata.EvoSuiteFile in project evosuite by EvoSuite.
the class ConcolicExecutionEnvironmentTest method buildTestCaseWithFile.
private static DefaultTestCase buildTestCaseWithFile() throws SecurityException, NoSuchMethodException {
TestCaseBuilder tc = new TestCaseBuilder();
// int int0 = 10;
VariableReference int0 = tc.appendIntPrimitive(10);
// MockFile mockFile0 = (MockFile)MockFile.createTempFile("tmp",
// "foo.txt");
VariableReference prefixVarRef = tc.appendStringPrimitive("temp");
VariableReference sufixVarRef = tc.appendStringPrimitive(".txt");
Method createTempFileMethod = MockFile.class.getMethod("createTempFile", String.class, String.class);
VariableReference mockFileVarRef = tc.appendMethod(null, createTempFileMethod, prefixVarRef, sufixVarRef);
// String path = mockFile0.getPath;
Method getPathMethod = MockFile.class.getMethod("getPath");
VariableReference pathVarRef = tc.appendMethod(mockFileVarRef, getPathMethod);
// EvoSuiteFile evosuiteFile = new EvoSuiteFile();
Constructor<EvoSuiteFile> evoSuiteFileCtor = EvoSuiteFile.class.getConstructor(String.class);
VariableReference evosuiteFile = tc.appendConstructor(evoSuiteFileCtor, pathVarRef);
// String string0 = "<<FILE CONTENT>>"
VariableReference fileContentVarRef = tc.appendStringPrimitive("<<FILE CONTENT>>");
// FileSystemHandling.appendStringToFile(evosuiteFile, string0)
Method appendStringToFileMethod = FileSystemHandling.class.getMethod("appendStringToFile", EvoSuiteFile.class, String.class);
tc.appendMethod(null, appendStringToFileMethod, evosuiteFile, fileContentVarRef);
// DseWithFile dseWithFile0 = new DseWithFile();
Constructor<TestCaseWithFile> ctor = TestCaseWithFile.class.getConstructor();
VariableReference dseWithFileVarRef = tc.appendConstructor(ctor);
// String ret_val = dseWithFile0.test((File) mockFile0);
Method testMethod = TestCaseWithFile.class.getMethod("test", File.class);
tc.appendMethod(dseWithFileVarRef, testMethod, mockFileVarRef);
// DseWithFile.isiZero(int0)
Method isZeroMethod = TestCaseWithFile.class.getMethod("isZero", int.class);
tc.appendMethod(null, isZeroMethod, int0);
return tc.getDefaultTestCase();
}
use of org.evosuite.runtime.testdata.EvoSuiteFile in project evosuite by EvoSuite.
the class FileNamePrimitiveStatement method randomize.
/* (non-Javadoc)
* @see org.evosuite.testcase.PrimitiveStatement#randomize()
*/
/**
* {@inheritDoc}
*/
@Override
public void randomize() {
String path = Randomness.choice(tc.getAccessedEnvironment().getViewOfAccessedFiles());
if (path != null) {
setValue(new EvoSuiteFile(path));
} else {
// FIXME find out why this case can actually happen! (I don't think we want this?)
setValue(null);
}
logger.debug("Randomized filename: " + value);
}
use of org.evosuite.runtime.testdata.EvoSuiteFile in project evosuite by EvoSuite.
the class SymbolicObserver method after.
private void after(FileNamePrimitiveStatement statement, Scope scope) {
EvoSuiteFile conc_evosuite_file = statement.getValue();
VariableReference varRef = statement.getReturnValue();
String varRefName = varRef.getName();
ReferenceExpression fileRef;
if (conc_evosuite_file == null) {
fileRef = env.heap.getReference(null);
if (fileRef.getConcreteValue() != null) {
throw new IllegalStateException("Expected null concrete object");
}
} else {
fileRef = (ReferenceConstant) env.heap.getReference(conc_evosuite_file);
if (fileRef.getConcreteValue() == null) {
throw new IllegalStateException("Expected non-null concrete object");
}
}
symb_references.put(varRefName, fileRef);
}
use of org.evosuite.runtime.testdata.EvoSuiteFile in project evosuite by EvoSuite.
the class EnvironmentDataSystemTest method testOnSpecificTest.
@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(DseFoo.class.getCanonicalName());
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
// String string0 = "baz5";
VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
// DseFoo dseFoo0 = new DseFoo();
GenericConstructor fooConstructor = new GenericConstructor(fooClass.getConstructors()[0], fooClass);
ConstructorStatement fooConstructorStatement = new ConstructorStatement(test, fooConstructor, Arrays.asList(new VariableReference[] {}));
VariableReference fooVar = test.addStatement(fooConstructorStatement);
// String fileName = new String("/home/galeotti/README.txt")
String path = "/home/galeotti/README.txt";
EvoSuiteFile evosuiteFile = new EvoSuiteFile(path);
FileNamePrimitiveStatement fileNameStmt = new FileNamePrimitiveStatement(test, evosuiteFile);
test.addStatement(fileNameStmt);
Method fooIncMethod = fooClass.getMethod("inc", new Class<?>[] {});
GenericMethod incMethod = new GenericMethod(fooIncMethod, fooClass);
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
// DseBar dseBar0 = new DseBar(string0);
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
ConstructorStatement constructorStatement = new ConstructorStatement(test, gc, Arrays.asList(new VariableReference[] { stringVar }));
VariableReference callee = test.addStatement(constructorStatement);
// dseBar0.coverMe(dseFoo0);
Method m = clazz.getRawClass().getMethod("coverMe", new Class<?>[] { fooClass });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { fooVar }));
test.addStatement(ms);
System.out.println(test);
TestSuiteChromosome suite = new TestSuiteChromosome();
BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
BranchCoverageMap.getInstance().searchStarted(null);
assertEquals(4.0, fitness.getFitness(suite), 0.1F);
suite.addTest(test);
assertEquals(1.0, fitness.getFitness(suite), 0.1F);
System.out.println("Test suite: " + suite);
Properties.CONCOLIC_TIMEOUT = Integer.MAX_VALUE;
TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
localObjective.addFitnessFunction(fitness);
localSearch.doSearch(suite, localObjective);
System.out.println("Fitness: " + fitness.getFitness(suite));
System.out.println("Test suite: " + suite);
assertEquals("Local search failed to cover class", 0.0, fitness.getFitness(suite), 0.1F);
BranchCoverageMap.getInstance().searchFinished(null);
}
Aggregations