1

I want to pass a security manager to be used in Junit tests by gradle. I tried jvmargs(not working) and now I'm using this:

apply plugin: 'java'

test {
  systemProperties << ['java.security.policy': file("$projectDir/security.policy").absolutePath]
}

...

Java class

package hello;

import java.io.*;
import java.lang.*; 

public class MyClass {
  public void method() {
    try {
        File file = new File("/etc/passwd");
        FileReader fr = new FileReader(file);
        char [] a = new char[50];
        fr.read(a);
        for(char c : a)
           System.out.print(c);
        fr.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
  }
}

My policy is:

grant {
    permission java.io.FilePermission "/home/-", "read";
};

So I expect to get security exception when trying to read /etc/passwd but I don't.

Issam Zoli
  • 2,724
  • 1
  • 21
  • 35

1 Answers1

1

The Gradle User Guide section 46.2.3 has an example on setting a system property for a test which differs from yours:

test {
    systemProperties 'property': 'value'
}

Perhaps you can try with that syntax, which would be

test {
    systemProperties 'java.security.policy': file("$projectDir/security.policy").absolutePath
}
Per Huss
  • 4,755
  • 12
  • 29