From 05708a256cb82f5d6a1fa7394c1688d4021d5aa8 Mon Sep 17 00:00:00 2001 From: games647 Date: Mon, 25 Jul 2022 13:02:30 +0200 Subject: [PATCH] Perform check for available reflection during compile time --- bukkit/pom.xml | 14 +++++ .../fastlogin/bukkit/hook/CrazyLoginHook.java | 2 +- .../bukkit/task/DelayedAuthHook.java | 6 +- .../fastlogin/bukkit/ReflectionTest.java | 42 +++++++++++++ .../bukkit/hook/CrazyLoginHookTest.java | 43 +++++++++++++ .../protocollib/VerifyResponseTaskTest.java | 63 +++++++++++++++++++ .../bukkit/task/DelayedAuthHookTest.java | 61 ++++++++++++++++++ bungee/pom.xml | 12 ---- .../bungee/listener/ConnectListener.java | 6 +- .../bungee/listener/ConnectListenerTest.java | 58 +++++++++++++++++ .../games647/fastlogin/core/CommonUtil.java | 14 +++-- .../fastlogin/core/CommonUtilTest.java | 38 +++++++++++ pom.xml | 15 +++-- 13 files changed, 346 insertions(+), 28 deletions(-) create mode 100644 bukkit/src/test/java/com/github/games647/fastlogin/bukkit/ReflectionTest.java create mode 100644 bukkit/src/test/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHookTest.java create mode 100644 bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTaskTest.java create mode 100644 bukkit/src/test/java/com/github/games647/fastlogin/bukkit/task/DelayedAuthHookTest.java create mode 100644 bungee/src/test/java/com/github/games647/fastlogin/bungee/listener/ConnectListenerTest.java create mode 100644 core/src/test/java/com/github/games647/fastlogin/core/CommonUtilTest.java diff --git a/bukkit/pom.xml b/bukkit/pom.xml index ae285d64..6def4b2d 100644 --- a/bukkit/pom.xml +++ b/bukkit/pom.xml @@ -368,5 +368,19 @@ 1.71 test + + + io.netty + netty-transport + 4.1.77.Final + test + + + + io.netty + netty-codec + 4.1.77.Final + test + diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHook.java index f0680293..5ab3bce6 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHook.java @@ -134,7 +134,7 @@ public class CrazyLoginHook implements AuthPlugin { return false; } - private PlayerListener getListener() { + protected PlayerListener getListener() { PlayerListener listener; try { listener = (PlayerListener) FieldUtils.readField(crazyLoginPlugin, "playerListener", true); diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/task/DelayedAuthHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/task/DelayedAuthHook.java index b21b9c35..23762932 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/task/DelayedAuthHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/task/DelayedAuthHook.java @@ -94,8 +94,8 @@ public class DelayedAuthHook implements Runnable { private AuthPlugin getAuthHook() { try { List>> hooks = Arrays.asList(AuthMeHook.class, - CrazyLoginHook.class, LogItHook.class, LoginSecurityHook.class, UltraAuthHook.class, - XAuthHook.class); + CrazyLoginHook.class, LogItHook.class, LoginSecurityHook.class, UltraAuthHook.class, + XAuthHook.class); for (Class> clazz : hooks) { String pluginName = clazz.getSimpleName(); @@ -113,7 +113,7 @@ public class DelayedAuthHook implements Runnable { return null; } - private AuthPlugin newInstance(Class> clazz) + protected AuthPlugin newInstance(Class> clazz) throws ReflectiveOperationException { try { Constructor> cons = clazz.getDeclaredConstructor(FastLoginBukkit.class); diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/ReflectionTest.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/ReflectionTest.java new file mode 100644 index 00000000..dae5c519 --- /dev/null +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/ReflectionTest.java @@ -0,0 +1,42 @@ +/* + * SPDX-License-Identifier: MIT + * + * The MIT License (MIT) + * + * Copyright (c) 2015-2022 games647 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.github.games647.fastlogin.bukkit; + +import com.comphenix.protocol.reflect.FieldUtils; + +import fr.xephi.authme.api.v3.AuthMeApi; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class ReflectionTest { + + @Test + void testAuthMeManagementField() { + assertNotNull(FieldUtils.getField(AuthMeApi.class, "management", true)); + } +} diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHookTest.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHookTest.java new file mode 100644 index 00000000..ae53c1f6 --- /dev/null +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHookTest.java @@ -0,0 +1,43 @@ +/* + * SPDX-License-Identifier: MIT + * + * The MIT License (MIT) + * + * Copyright (c) 2015-2022 games647 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.github.games647.fastlogin.bukkit.hook; + +import com.comphenix.protocol.reflect.FieldUtils; + +import de.st_ddt.crazylogin.CrazyLogin; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class CrazyLoginHookTest { + + @Test + void testPlayerListener() { + assertNotNull(FieldUtils.getField(CrazyLogin.class, "playerListener", true)); + } + +} diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTaskTest.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTaskTest.java new file mode 100644 index 00000000..f751c5c0 --- /dev/null +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTaskTest.java @@ -0,0 +1,63 @@ +/* + * SPDX-License-Identifier: MIT + * + * The MIT License (MIT) + * + * Copyright (c) 2015-2022 games647 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.github.games647.fastlogin.bukkit.listener.protocollib; + +import com.comphenix.protocol.injector.packet.PacketRegistry; +import com.comphenix.protocol.reflect.FieldUtils; +import com.comphenix.protocol.utility.MinecraftReflection; + +import java.util.Optional; + +import org.bukkit.Bukkit; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +import static org.mockito.ArgumentMatchers.any; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.mockStatic; + +class VerifyResponseTaskTest { + + @Test + void getNetworkManagerReflection() throws ClassNotFoundException { + try ( + MockedStatic bukkitMock = mockStatic(Bukkit.class); + MockedStatic reflectionMock = mockStatic(MinecraftReflection.class); + MockedStatic registryMock = mockStatic(PacketRegistry.class) + ) { + bukkitMock.when(Bukkit::getVersion).thenReturn("git-Bukkit-18fbb24 (MC: 1.17)"); + reflectionMock.when(MinecraftReflection::getMinecraftPackage).thenReturn("xyz"); + reflectionMock.when(MinecraftReflection::getEnumProtocolClass).thenReturn(Object.class); + + registryMock.when(() -> PacketRegistry.tryGetPacketClass(any())).thenReturn(Optional.empty()); + + + Class injectorClass = Class.forName("com.comphenix.protocol.injector.netty.channel.NettyChannelInjector"); + assertNotNull(FieldUtils.getField(injectorClass, "networkManager", true)); + } + } +} diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/task/DelayedAuthHookTest.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/task/DelayedAuthHookTest.java new file mode 100644 index 00000000..5e925bf1 --- /dev/null +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/task/DelayedAuthHookTest.java @@ -0,0 +1,61 @@ +/* + * SPDX-License-Identifier: MIT + * + * The MIT License (MIT) + * + * Copyright (c) 2015-2022 games647 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.github.games647.fastlogin.bukkit.task; + +import com.github.games647.fastlogin.core.hooks.AuthPlugin; + +import lombok.val; +import org.bukkit.entity.Player; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class DelayedAuthHookTest { + + @Test + void createNewReflectiveInstance() throws ReflectiveOperationException { + val authHook = new DelayedAuthHook(null); + assertNotNull(authHook.newInstance(DummyHook.class)); + } + + public static class DummyHook implements AuthPlugin { + + @Override + public boolean forceLogin(Player player) { + return false; + } + + @Override + public boolean forceRegister(Player player, String password) { + return false; + } + + @Override + public boolean isRegistered(String playerName) throws Exception { + return false; + } + } +} diff --git a/bungee/pom.xml b/bungee/pom.xml index 2d5eb928..15daff91 100644 --- a/bungee/pom.xml +++ b/bungee/pom.xml @@ -139,22 +139,10 @@ org.slf4j slf4j-api - - io.netty - * - - - net.sf.jopt-simple - * - mysql * - - net.md-5 - bungeecord-log - net.md-5 bungeecord-native diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java index 73ddcfbc..0229715b 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java @@ -71,7 +71,7 @@ import org.slf4j.LoggerFactory; public class ConnectListener implements Listener { private static final String UUID_FIELD_NAME = "uniqueId"; - private static final MethodHandle UNIQUE_ID_SETTER; + protected static final MethodHandle UNIQUE_ID_SETTER; static { MethodHandle setHandle = null; @@ -87,7 +87,7 @@ public class ConnectListener implements Listener { Logger logger = LoggerFactory.getLogger(ConnectListener.class); logger.error( "Cannot find Bungee initial handler; Disabling premium UUID and skin won't work.", - reflectiveOperationException + reflectiveOperationException ); } @@ -171,7 +171,7 @@ public class ConnectListener implements Listener { } } - private void setOfflineId(InitialHandler connection, String username) { + protected void setOfflineId(InitialHandler connection, String username) { try { UUID oldPremiumId = connection.getUniqueId(); UUID offlineUUID = UUIDAdapter.generateOfflineId(username); diff --git a/bungee/src/test/java/com/github/games647/fastlogin/bungee/listener/ConnectListenerTest.java b/bungee/src/test/java/com/github/games647/fastlogin/bungee/listener/ConnectListenerTest.java new file mode 100644 index 00000000..ed2459ca --- /dev/null +++ b/bungee/src/test/java/com/github/games647/fastlogin/bungee/listener/ConnectListenerTest.java @@ -0,0 +1,58 @@ +/* + * SPDX-License-Identifier: MIT + * + * The MIT License (MIT) + * + * Copyright (c) 2015-2022 games647 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.github.games647.fastlogin.bungee.listener; + +import java.lang.reflect.Field; +import java.util.UUID; + +import net.md_5.bungee.BungeeCord; +import net.md_5.bungee.conf.Configuration; +import net.md_5.bungee.connection.InitialHandler; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + +class ConnectListenerTest { + + @Test + void testUUIDSetter() throws Throwable { + BungeeCord proxyMock = mock(BungeeCord.class); + BungeeCord.setInstance(proxyMock); + + Configuration configMock = mock(Configuration.class); + Field configField = proxyMock.getClass().getField("config"); + configField.setAccessible(true); + configField.set(proxyMock, configMock); + + InitialHandler handler = new InitialHandler(proxyMock, null); + + UUID expectedUUID = UUID.randomUUID(); + ConnectListener.UNIQUE_ID_SETTER.invokeExact(handler, expectedUUID); + assertEquals(expectedUUID, handler.getUniqueId()); + } +} diff --git a/core/src/main/java/com/github/games647/fastlogin/core/CommonUtil.java b/core/src/main/java/com/github/games647/fastlogin/core/CommonUtil.java index b76399bf..881cf1df 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/CommonUtil.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/CommonUtil.java @@ -91,11 +91,7 @@ public final class CommonUtil { LoggerFactory.getLogger(parent.getName()).info("Initialize logging service"); try { parent.setLevel(Level.ALL); - - Class adapterClass = JDK14LoggerAdapter.class; - Constructor cons = adapterClass.getDeclaredConstructor(java.util.logging.Logger.class); - cons.setAccessible(true); - return cons.newInstance(parent); + return createJDKLogger(parent); } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException reflectEx) { parent.log(Level.WARNING, "Cannot create slf4j logging adapter", reflectEx); @@ -107,6 +103,14 @@ public final class CommonUtil { } } + protected static JDK14LoggerAdapter createJDKLogger(java.util.logging.Logger parent) + throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { + Class adapterClass = JDK14LoggerAdapter.class; + Constructor cons = adapterClass.getDeclaredConstructor(java.util.logging.Logger.class); + cons.setAccessible(true); + return cons.newInstance(parent); + } + private CommonUtil() { throw new RuntimeException("No instantiation of utility classes allowed"); } diff --git a/core/src/test/java/com/github/games647/fastlogin/core/CommonUtilTest.java b/core/src/test/java/com/github/games647/fastlogin/core/CommonUtilTest.java new file mode 100644 index 00000000..f8ea9e43 --- /dev/null +++ b/core/src/test/java/com/github/games647/fastlogin/core/CommonUtilTest.java @@ -0,0 +1,38 @@ +/* + * SPDX-License-Identifier: MIT + * + * The MIT License (MIT) + * + * Copyright (c) 2015-2022 games647 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.github.games647.fastlogin.core; + +import java.util.logging.Logger; + +import org.junit.jupiter.api.Test; + +class CommonUtilTest { + + @Test + void createJDKLogger() throws Exception { + CommonUtil.createJDKLogger(Logger.getAnonymousLogger()); + } +} diff --git a/pom.xml b/pom.xml index dc29b4b8..18c03e51 100644 --- a/pom.xml +++ b/pom.xml @@ -196,6 +196,13 @@ + + org.projectlombok + lombok + 1.18.24 + provided + + org.junit.jupiter junit-jupiter @@ -204,10 +211,10 @@ - org.projectlombok - lombok - 1.18.24 - provided + org.mockito + mockito-inline + 4.6.1 + test