Skip to content

Commit

Permalink
Add annotation to disable thread group check
Browse files Browse the repository at this point in the history
  • Loading branch information
bensofficial committed Dec 9, 2024
1 parent 267e744 commit 43baceb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
26 changes: 26 additions & 0 deletions src/main/java/de/tum/in/test/api/DisableThreadGroupCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.tum.in.test.api;

import org.apiguardian.api.API;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Disables the Thread Group Check.
*
* @author Benjamin Schmitz
* @since 1.14.0
* @version 1.0.0
*/
@API(status = API.Status.EXPERIMENTAL)
@Inherited
@Documented
@Retention(RUNTIME)
@Target({ TYPE, METHOD, ANNOTATION_TYPE })
public @interface DisableThreadGroupCheck {
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static AresSecurityConfiguration generateConfiguration(TestContext contex
config.withPackageWhitelist(generatePackageWhiteList(context));
config.withTrustedPackages(getTrustedPackages(context));
config.withThreadTrustScope(getThreadTrustScope(context));
config.withIsThreadGroupCheckDisabled(isThreadGroupCheckDisabled(context));
configureAllowLocalPort(config, context);
return config.build();
}
Expand Down Expand Up @@ -109,4 +110,8 @@ private static TrustScope getThreadTrustScope(TestContext context) {
return TestContextUtils.findAnnotationIn(context, TrustedThreads.class).map(TrustedThreads::value)
.orElse(TrustScope.MINIMAL);
}

public static Boolean isThreadGroupCheckDisabled(TestContext context) {
return TestContextUtils.findAnnotationIn(context, DisableThreadGroupCheck.class).isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public final class AresSecurityConfiguration {
private final Set<PackageRule> whitelistedPackages;
private final Set<PackageRule> trustedPackages;
private final TrustScope threadTrustScope;
private final boolean isThreadGroupCheckDisabled;

AresSecurityConfiguration(Optional<Class<?>> testClass, Optional<Method> testMethod, Path executionPath, // NOSONAR
Collection<String> whitelistedClassNames, Optional<Collection<PathRule>> whitelistedPaths,
Collection<PathRule> blacklistedPaths, Set<Integer> allowedLocalPorts, OptionalInt allowLocalPortsAbove,
Set<Integer> excludedLocalPorts, OptionalInt allowedThreadCount, Set<PackageRule> blacklistedPackages,
Set<PackageRule> whitelistedPackages, Set<PackageRule> trustedPackages, TrustScope threadTrustScope) {
Set<PackageRule> whitelistedPackages, Set<PackageRule> trustedPackages, TrustScope threadTrustScope,
boolean isThreadGroupCheckDisabled) {
this.testClass = Objects.requireNonNull(testClass);
this.testMethod = Objects.requireNonNull(testMethod);
this.executionPath = executionPath.toAbsolutePath();
Expand All @@ -46,6 +48,7 @@ public final class AresSecurityConfiguration {
this.whitelistedPackages = Set.copyOf(whitelistedPackages);
this.trustedPackages = Set.copyOf(trustedPackages);
this.threadTrustScope = threadTrustScope;
this.isThreadGroupCheckDisabled = isThreadGroupCheckDisabled;
}

public Optional<Class<?>> testClass() {
Expand Down Expand Up @@ -104,6 +107,10 @@ public TrustScope threadTrustScope() {
return threadTrustScope;
}

public boolean isThreadGroupCheckDisabled() {
return isThreadGroupCheckDisabled;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
Expand All @@ -122,24 +129,27 @@ public boolean equals(Object obj) {
&& Objects.equals(blacklistedPaths, other.blacklistedPaths)
&& Objects.equals(blacklistedPackages, other.blacklistedPackages)
&& Objects.equals(whitelistedPackages, other.whitelistedPackages)
&& Objects.equals(threadTrustScope, other.threadTrustScope);
&& Objects.equals(threadTrustScope, other.threadTrustScope)
&& Objects.equals(isThreadGroupCheckDisabled, other.isThreadGroupCheckDisabled);
}

@Override
public int hashCode() {
return Objects.hash(executionPath, testClass, testMethod, whitelistedClassNames, allowedThreadCount,
whitelistedPaths, blacklistedPaths, blacklistedPackages, whitelistedPackages, threadTrustScope);
whitelistedPaths, blacklistedPaths, blacklistedPackages, whitelistedPackages, threadTrustScope,
isThreadGroupCheckDisabled);
}

@Override
public String toString() {
return String.format("AresSecurityConfiguration [whitelistedClassNames=%s, executionPath=%s," //$NON-NLS-1$
+ " testClass=%s, testMethod=%s, whitelistedPaths=%s, blacklistedPaths=%s, allowedLocalPorts=%s," //$NON-NLS-1$
+ " allowLocalPortsAbove=%s, excludedLocalPorts=%s, allowedThreadCount=%s," //$NON-NLS-1$
+ " blacklistedPackages=%s, whitelistedPackages=%s, trustedPackages=%s, threadTrustScope=%s]", //$NON-NLS-1$
+ " blacklistedPackages=%s, whitelistedPackages=%s, trustedPackages=%s, threadTrustScope=%s," //$NON-NLS-1$
+ " isThreadGroupCheckDisabled=%b]", //$NON-NLS-1$
whitelistedClassNames, executionPath, testClass, testMethod, whitelistedPaths, blacklistedPaths,
allowedLocalPorts, allowLocalPortsAbove, excludedLocalPorts, allowedThreadCount, blacklistedPackages,
whitelistedPackages, trustedPackages, threadTrustScope);
whitelistedPackages, trustedPackages, threadTrustScope, isThreadGroupCheckDisabled);
}

public String shortDesc() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public final class AresSecurityConfigurationBuilder {
private OptionalInt allowedThreadCount;
private Set<PackageRule> trustedPackages;
private TrustScope threadTrustScope;
private boolean isThreadGroupCheckDisabled;

private AresSecurityConfigurationBuilder() {
testClass = Optional.empty();
Expand All @@ -74,6 +75,7 @@ private AresSecurityConfigurationBuilder() {
allowedThreadCount = OptionalInt.empty();
trustedPackages = Set.of();
threadTrustScope = TrustScope.MINIMAL;
isThreadGroupCheckDisabled = false;
}

public AresSecurityConfigurationBuilder withPath(Path executionPath) {
Expand Down Expand Up @@ -142,12 +144,17 @@ public AresSecurityConfigurationBuilder withThreadTrustScope(TrustScope threadTr
return this;
}

public AresSecurityConfigurationBuilder withIsThreadGroupCheckDisabled(boolean isThreadGroupCheckDisabled) {
this.isThreadGroupCheckDisabled = isThreadGroupCheckDisabled;
return this;
}

public AresSecurityConfiguration build() {
validate();
return new AresSecurityConfiguration(testClass, testMethod, executionPath, whitelistedClassNames,
Optional.ofNullable(whitelistedPaths), blacklistedPaths, allowedLocalPorts, allowLocalPortsAbove,
excludedLocalPorts, allowedThreadCount, blacklistedPackages, whitelistedPackages, trustedPackages,
threadTrustScope);
threadTrustScope, isThreadGroupCheckDisabled);
}

private void validate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,11 @@ public ThreadGroup getThreadGroup() {
*/
@SuppressWarnings("deprecation")
private Thread[] checkThreadGroup() {
if (configuration.isThreadGroupCheckDisabled()) {
LOG.debug("Thread group check is disabled"); //$NON-NLS-1$
return new Thread[0];
}

blockThreadCreation = true;
int originalCount = testThreadGroup.activeCount();
if (originalCount == 0)
Expand Down

0 comments on commit 43baceb

Please sign in to comment.