Search code examples
javajnaevosuite

Allow Evosuite to write files during test generation


Im currently trying to get Evosuite to work with JNA. Consider the following basic example:

import com.sun.jna.ptr.IntByReference;

public class Example {
    public static int foo(int x) {
        IntByReference c = new IntByReference(x);

        if (c.getValue() == 100) {
            return 100;
        } else {
            return 0;
        }
    }
}

Im running Evosuite from the command line with these options:

java32 -jar evosuite.jar -projectCP "src;E:\evosuite\test\lib\jna-5.2.0.jar" -class Example -criterion branch

Evosuite wont reach 100% branch coverage (only the trivial 33%), but notifies me with this message after the timeout:

* Permissions denied during test execution:
  - java.io.FilePermission:
         write C:\Users\PC\AppData\Local\Temp\jna--2025216854: 1

I found out that JNA needs to write some temp files in order to work, but Evosuite will block any atempt of file writing during test generation. I understand that this is a reasonable policy in most cases because you dont want Evosuite to write random files to your disk while generating tests for a saveFile() function, but in my case this shouldn't be a problem.

Is there a way to tell Evosuite to allow file writing during test generation or a different approach to generate tests for java programms using the JNA library?


Solution

  • I figured out how to run JNA without the need of writing temporary files thanks to cubrr.

    • copy the system specific jnidispatch.[dll, ...] file to a folder
    • add -Djna.boot.library.path=folder -Djna.nounpack=true to the command

    Note: the jna.boot.library.path should only point to the containig folder, do not write folder/jnidispatch.


    Solution to the initial question:

    Setting the evosuite option -Dsandbox=false will remove most restrictions for test generation and finally allowed me to generate my tests!