From 0d5f09a9fa2dda522cba5503fcdc856bc729d78e Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Wed, 6 Mar 2019 16:29:48 +0200 Subject: [PATCH] Diagnostic output --- .../server/system/SystemSqlTestSuite.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/application/src/test/java/org/thingsboard/server/system/SystemSqlTestSuite.java b/application/src/test/java/org/thingsboard/server/system/SystemSqlTestSuite.java index 76145c954d..ae45e35fa9 100644 --- a/application/src/test/java/org/thingsboard/server/system/SystemSqlTestSuite.java +++ b/application/src/test/java/org/thingsboard/server/system/SystemSqlTestSuite.java @@ -15,11 +15,21 @@ */ package org.thingsboard.server.system; +import net.bytebuddy.asm.AsmVisitorWrapper; +import net.bytebuddy.asm.MemberSubstitution; +import net.bytebuddy.matcher.ElementMatchers; +import org.apache.cassandra.cql3.functions.ThreadAwareSecurityManager; +import org.hibernate.HibernateException; +import org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState; +import org.hibernate.bytecode.internal.bytebuddy.HibernateMethodLookupDispatcher; import org.junit.ClassRule; import org.junit.extensions.cpsuite.ClasspathSuite; import org.junit.runner.RunWith; import org.thingsboard.server.dao.CustomSqlUnit; +import java.lang.reflect.Method; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.Arrays; /** @@ -30,11 +40,74 @@ import java.util.Arrays; public class SystemSqlTestSuite { static { + //ThreadAwareSecurityManager.install(); SecurityManager appsm = System.getSecurityManager(); System.out.println("SECURITY MANAGER = " + appsm); if (appsm != null) { System.out.println("SECURITY MANAGER CLASS = " + appsm.getClass()); } + + AsmVisitorWrapper.ForDeclaredMethods getDeclaredMethodMemberSubstitution; + AsmVisitorWrapper.ForDeclaredMethods getMethodMemberSubstitution; + + //if ( System.getSecurityManager() != null ) { + getDeclaredMethodMemberSubstitution = getDeclaredMethodMemberSubstitution(); + getMethodMemberSubstitution = getMethodMemberSubstitution(); + //} + //else { + // getDeclaredMethodMemberSubstitution = null; + // getMethodMemberSubstitution = null; + //} + + System.out.println("getDeclaredMethodMemberSubstitution = " + getDeclaredMethodMemberSubstitution); + System.out.println("getMethodMemberSubstitution = " + getMethodMemberSubstitution); + } + + private static class GetDeclaredMethodAction implements PrivilegedAction { + private final Class clazz; + private final String methodName; + private final Class[] parameterTypes; + + private GetDeclaredMethodAction(Class clazz, String methodName, Class... parameterTypes) { + this.clazz = clazz; + this.methodName = methodName; + this.parameterTypes = parameterTypes; + } + + @Override + public Method run() { + try { + Method method = clazz.getDeclaredMethod( methodName, parameterTypes ); + + return method; + } + catch (NoSuchMethodException e) { + throw new HibernateException( "Unable to prepare getDeclaredMethod()/getMethod() substitution", e ); + } + } + } + + + private static AsmVisitorWrapper.ForDeclaredMethods getDeclaredMethodMemberSubstitution() { + // this should only be called if the security manager is enabled, thus the privileged calls + return MemberSubstitution.relaxed() + .method( ElementMatchers.is( AccessController.doPrivileged( new SystemSqlTestSuite.GetDeclaredMethodAction( Class.class, + "getDeclaredMethod", String.class, Class[].class ) ) ) ) + .replaceWith( + AccessController.doPrivileged( new SystemSqlTestSuite.GetDeclaredMethodAction( HibernateMethodLookupDispatcher.class, + "getDeclaredMethod", Class.class, String.class, Class[].class ) ) ) + .on( ElementMatchers.isTypeInitializer() ); + } + + private static AsmVisitorWrapper.ForDeclaredMethods getMethodMemberSubstitution() { + // this should only be called if the security manager is enabled, thus the privileged calls + return MemberSubstitution.relaxed() + .method( ElementMatchers.is( AccessController.doPrivileged( new SystemSqlTestSuite.GetDeclaredMethodAction( Class.class, + "getMethod", String.class, Class[].class ) ) ) ) + .replaceWith( + AccessController.doPrivileged( new SystemSqlTestSuite.GetDeclaredMethodAction( HibernateMethodLookupDispatcher.class, + "getMethod", Class.class, String.class, Class[].class ) ) ) + .on( ElementMatchers.isTypeInitializer() ); } @ClassRule