From c25ca04aa1c07d08dd55cf237a8f657eed65af48 Mon Sep 17 00:00:00 2001 From: Yury Kamenev Date: Tue, 3 Jan 2023 14:03:42 +0800 Subject: [PATCH] Added wrappers for AccessController, AutoCloseable and Throwable --- .../engine/overrides/AccessController.java | 103 ++++++++++++++++++ .../utbot/engine/overrides/AutoCloseable.java | 10 ++ .../org/utbot/engine/overrides/Throwable.java | 31 ++++++ .../org/utbot/framework/util/SootUtils.kt | 3 + 4 files changed, 147 insertions(+) create mode 100644 utbot-framework/src/main/java/org/utbot/engine/overrides/AccessController.java create mode 100644 utbot-framework/src/main/java/org/utbot/engine/overrides/AutoCloseable.java create mode 100644 utbot-framework/src/main/java/org/utbot/engine/overrides/Throwable.java diff --git a/utbot-framework/src/main/java/org/utbot/engine/overrides/AccessController.java b/utbot-framework/src/main/java/org/utbot/engine/overrides/AccessController.java new file mode 100644 index 0000000000..f2cb72f38f --- /dev/null +++ b/utbot-framework/src/main/java/org/utbot/engine/overrides/AccessController.java @@ -0,0 +1,103 @@ +package org.utbot.engine.overrides; + +import org.utbot.api.annotation.UtClassMock; + +import java.security.AccessControlContext; +import java.security.Permission; +import java.security.PrivilegedAction; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; + +@UtClassMock(target = java.security.AccessController.class, internalUsage = true) +public class AccessController { + public static T doPrivileged(PrivilegedAction action) { + return action.run(); + } + + public static T doPrivilegedWithCombiner(PrivilegedAction action) { + return action.run(); + } + + public static T doPrivileged(PrivilegedAction action, + AccessControlContext ignoredContext) { + return action.run(); + } + + public static T doPrivileged(PrivilegedAction action, + AccessControlContext ignoredContext, Permission... perms) { + if (perms == null) { + throw new NullPointerException(); + } + + for (Permission permission : perms) { + if (permission == null) { + throw new NullPointerException(); + } + } + + return action.run(); + } + + public static T doPrivilegedWithCombiner(PrivilegedAction action, + AccessControlContext context, Permission... perms) { + return doPrivileged(action, context, perms); + } + + public static T doPrivileged(PrivilegedExceptionAction action) throws PrivilegedActionException { + try { + return action.run(); + } catch (RuntimeException e) { + // If the action's run method throws an unchecked exception, it will propagate through this method. + throw e; + } catch (Exception e) { + // Exception is wrapped with the PrivilegedActionException ONLY if this exception is CHECKED + throw new PrivilegedActionException(e); + } + } + + public static T doPrivilegedWithCombiner(PrivilegedExceptionAction action) throws PrivilegedActionException { + return doPrivileged(action); + } + + public static T doPrivileged( + PrivilegedExceptionAction action, + AccessControlContext ignoredContext + ) throws PrivilegedActionException { + return doPrivileged(action); + } + + public static T doPrivileged( + PrivilegedExceptionAction action, + AccessControlContext ignoredContext, + Permission... perms + ) throws PrivilegedActionException { + if (perms == null) { + throw new NullPointerException(); + } + + for (Permission permission : perms) { + if (permission == null) { + throw new NullPointerException(); + } + } + + return doPrivileged(action); + } + + public static T doPrivilegedWithCombiner( + PrivilegedExceptionAction action, + AccessControlContext context, + Permission... perms + ) throws PrivilegedActionException { + return doPrivileged(action, context, perms); + } + + public static void checkPermission(Permission perm) { + if (perm == null) { + throw new NullPointerException(); + } + + // We cannot check real permission do determines whether we should throw an AccessControlException, + // so do nothing more. + } +} diff --git a/utbot-framework/src/main/java/org/utbot/engine/overrides/AutoCloseable.java b/utbot-framework/src/main/java/org/utbot/engine/overrides/AutoCloseable.java new file mode 100644 index 0000000000..6ba7913699 --- /dev/null +++ b/utbot-framework/src/main/java/org/utbot/engine/overrides/AutoCloseable.java @@ -0,0 +1,10 @@ +package org.utbot.engine.overrides; + +import org.utbot.api.annotation.UtClassMock; + +@UtClassMock(target = java.lang.AutoCloseable.class, internalUsage = true) +public interface AutoCloseable { + default void close() throws Exception { + // Do nothing + } +} diff --git a/utbot-framework/src/main/java/org/utbot/engine/overrides/Throwable.java b/utbot-framework/src/main/java/org/utbot/engine/overrides/Throwable.java new file mode 100644 index 0000000000..9c51eade7f --- /dev/null +++ b/utbot-framework/src/main/java/org/utbot/engine/overrides/Throwable.java @@ -0,0 +1,31 @@ +package org.utbot.engine.overrides; + +import org.utbot.api.annotation.UtClassMock; +import org.utbot.api.mock.UtMock; + +@UtClassMock(target = java.lang.Throwable.class, internalUsage = true) +public class Throwable { + public void printStackTrace() { + // Do nothing + } + + public synchronized java.lang.Throwable fillInStackTrace() { + return UtMock.makeSymbolic(); + } + + public StackTraceElement[] getStackTrace() { + return UtMock.makeSymbolic(); + } + + public void setStackTrace(StackTraceElement[] stackTrace) { + // Do nothing + } + + public final synchronized void addSuppressed(java.lang.Throwable exception) { + // Do nothing + } + + public final synchronized java.lang.Throwable[] getSuppressed() { + return UtMock.makeSymbolic(); + } +} diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt index 0779f6de85..371cf2fdc1 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt @@ -151,6 +151,9 @@ private val classesToLoad = arrayOf( org.utbot.engine.overrides.Long::class, org.utbot.engine.overrides.Short::class, org.utbot.engine.overrides.System::class, + org.utbot.engine.overrides.Throwable::class, + org.utbot.engine.overrides.AutoCloseable::class, + org.utbot.engine.overrides.AccessController::class, org.utbot.engine.overrides.collections.UtOptional::class, org.utbot.engine.overrides.collections.UtOptionalInt::class, org.utbot.engine.overrides.collections.UtOptionalLong::class,