运行与安全管理器相关的EvoSuite测试时发生异常

时间:2018-07-09 12:21:47

标签: java evosuite java-security-manager

我正在尝试与此tutorial一起运行EvoSuite测试。

我能够生成测试类并使用Java 8对其进行编译。 但是,当尝试使用以下命令运行它时,将引发异常。

java  -cp ".\log4j.jar;.\ojdbc6.jar;.\commons-codec-1.3.jar;.\slf4j-log4j12-1.7.16.jar;.\slf4j-api-1.7.16.jar;D:\proj\target\classes;.\evosuite-tests;.\evosuite-standalone-runtime-1.0.6.jar;.\junit-4.12.jar;.\hamcrest-core-1.3.jar;"   org.junit.runner.JUnitCore  com.u.g.KeyClient_ESTest

抛出异常:

 java.lang.SecurityException: Security manager blocks ("java.sql.SQLPermission" "deregisterDriver")
    java.lang.Thread.getStackTrace(Thread.java:1556)
    org.evosuite.runtime.sandbox.MSecurityManager.checkPermission(MSecurityManager.java:434)
    java.sql.DriverManager.deregisterDriver(DriverManager.java:402)
    oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:467)
    java.sql.DriverManager.getConnection(DriverManager.java:664)
    java.sql.DriverManager.getConnection(DriverManager.java:247)
    com.u.g.KeyClient.getFromJDBC(KeyClient.java:164)
    com.u.g.KeyClient.fetchKey(KeyClient.java:95)
    com.u.g.KeyClient.<init>(KeyClient.java:40)
    com.u.g.KeyClient.getInstance(KeyClient.java:48)
    com.u.g.KeyClient_ESTest.test1(KeyClient_ESTest.java:36)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
    org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
    java.util.concurrent.FutureTask.run(FutureTask.java:266)
    java.lang.Thread.run(Thread.java:745)

    at org.evosuite.runtime.sandbox.MSecurityManager.checkPermission(MSecurityManager.java:452)
    at java.sql.DriverManager.deregisterDriver(DriverManager.java:402)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:467)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.u.g.KeyClient.getFromJDBC(KeyClient.java:164)
    at com.u.g.KeyClient.fetchKey(KeyClient.java:95)
    at com.u.g.KeyClient.<init>(KeyClient.java:40)
    at com.u.g.KeyClient.getInstance(KeyClient.java:48)
    at com.u.g.KeyClient_ESTest.test1(KeyClient_ESTest.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
    at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.lang.Thread.run(Thread.java:745)

    FAILURES!!!
    Tests run: 2,  Failures: 1

有人可以帮助/指导我解决此问题吗? 预先感谢。

其他信息:

测试类是单例,它使用JDBC API连接到数据库以检索某些值。

1 个答案:

答案 0 :(得分:0)

这是对我有用的解决方案。 解决方案是使用以下cmd选项生成测试分类

  

-Dsandbox_mode = OFF

生成测试类的完整命令是

C:\java8\jdk1.8.0_102\bin\java  -jar evosuite-master-1.0.6.jar -Dsandbox_mode=OFF -class com.u.g.KeyClient -projectCP ".\log4j.jar;.\ojdbc6.jar;.\commons-codec-1.3.jar;.\target\classes;" 

相应的KeyClient_ESTest_scaffolding类将具有RuntimeSetting来关闭沙箱

@BeforeClass 
public static void initEvoSuiteFramework() { 
org.evosuite.runtime.RuntimeSettings.className = "com.ultimatix.gdpr.KeyClient"; 
org.evosuite.runtime.GuiSupport.initialize(); 
org.evosuite.runtime.RuntimeSettings.maxNumberOfThreads = 100; 
org.evosuite.runtime.RuntimeSettings.maxNumberOfIterationsPerLoop = 10000; 
org.evosuite.runtime.RuntimeSettings.mockSystemIn = true; 
org.evosuite.runtime.RuntimeSettings.sandboxMode = org.evosuite.runtime.sandbox.Sandbox.SandboxMode.OFF; 
org.evosuite.runtime.sandbox.Sandbox.initializeSecurityManagerForSUT(); 
org.evosuite.runtime.classhandling.JDKClassResetter.init();
setSystemProperties();
initializeClasses();
org.evosuite.runtime.Runtime.getInstance().resetRuntime(); 

}

希望它可以帮助面临类似问题的人。