mirror of
https://github.com/funkemunky/AntiVPN.git
synced 2026-05-31 01:21:55 +00:00
tests now pass
This commit is contained in:
@@ -12,6 +12,7 @@ dependencies {
|
||||
testImplementation 'org.mockito:mockito-core:5.11.0'
|
||||
testImplementation 'org.mockito:mockito-subclass:5.11.0'
|
||||
testImplementation 'org.mockito:mockito-junit-jupiter:5.11.0'
|
||||
testImplementation testFixtures(project(':Common:Source'))
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
|
||||
@@ -98,8 +98,7 @@ public class BukkitListener extends VPNExecutor implements Listener {
|
||||
player,
|
||||
result.response()
|
||||
);
|
||||
case DENIED_PROXY ->
|
||||
StringUtil.varReplace(
|
||||
case DENIED_PROXY -> StringUtil.varReplace(
|
||||
AntiVPN.getInstance().getVpnConfig().getKickMessage(),
|
||||
player,
|
||||
result.response()
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.bukkit.scheduler.BukkitRunnable;
|
||||
public class BukkitPlayer extends APIPlayer {
|
||||
|
||||
private final Player player;
|
||||
|
||||
public BukkitPlayer(Player player) {
|
||||
super(player.getUniqueId(), player.getName(), player.getAddress() != null ? player.getAddress().getAddress() : null);
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.util.stream.IntStream;
|
||||
public class BukkitCommand extends org.bukkit.command.Command {
|
||||
|
||||
private final Command command;
|
||||
|
||||
public BukkitCommand(Command command) {
|
||||
super(command.name(), command.description(), command.usage(), Arrays.asList(command.aliases()));
|
||||
|
||||
|
||||
@@ -3,10 +3,9 @@ package dev.brighten.antivpn.bukkit;
|
||||
import be.seeseemelk.mockbukkit.MockBukkit;
|
||||
import be.seeseemelk.mockbukkit.ServerMock;
|
||||
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
||||
import dev.brighten.antivpn.StandardTest;
|
||||
import dev.brighten.antivpn.AntiVPN;
|
||||
import dev.brighten.antivpn.api.PlayerExecutor;
|
||||
import dev.brighten.antivpn.api.VPNConfig;
|
||||
import dev.brighten.antivpn.api.VPNExecutor;
|
||||
import dev.brighten.antivpn.api.*;
|
||||
import dev.brighten.antivpn.message.MessageHandler;
|
||||
import dev.brighten.antivpn.message.VpnString;
|
||||
import dev.brighten.antivpn.web.objects.VPNResponse;
|
||||
@@ -36,7 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class BukkitListenerTest {
|
||||
public class BukkitListenerTest extends StandardTest {
|
||||
|
||||
private ServerMock server;
|
||||
private BukkitListener listener;
|
||||
@@ -101,6 +100,10 @@ public class BukkitListenerTest {
|
||||
PlayerMock player = server.addPlayer("TestPlayer");
|
||||
InetAddress address = InetAddress.getByName("127.0.0.1");
|
||||
|
||||
mockCache("127.0.0.1", new CheckResult(VPNResponse.builder().success(true).proxy(false).ip("127.0.0.1")
|
||||
.method("N/A").countryName("N/A").countryCode("N/A").city("N/A").build(),
|
||||
ResultType.ALLOWED, true));
|
||||
|
||||
PlayerLoginEvent event = new PlayerLoginEvent(player, "localhost", address);
|
||||
|
||||
listener.onLogin(event);
|
||||
@@ -108,35 +111,12 @@ public class BukkitListenerTest {
|
||||
assertEquals(PlayerLoginEvent.Result.ALLOWED, event.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoginEventBlocked() throws Exception {
|
||||
PlayerMock player = server.addPlayer("ProxyPlayer");
|
||||
InetAddress address = InetAddress.getByName("1.1.1.1");
|
||||
|
||||
// Mock proxy response
|
||||
when(vpnExecutor.checkIp("1.1.1.1")).thenReturn(CompletableFuture.completedFuture(
|
||||
VPNResponse.builder().success(true).proxy(true).ip("1.1.1.1")
|
||||
.method("N/A").countryName("N/A").countryCode("N/A").city("N/A").build()
|
||||
));
|
||||
|
||||
PlayerLoginEvent event = new PlayerLoginEvent(player, "localhost", address);
|
||||
|
||||
listener.onLogin(event);
|
||||
|
||||
assertEquals(PlayerLoginEvent.Result.KICK_BANNED, event.getResult());
|
||||
assertEquals("Blocked!", net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().serialize(event.kickMessage()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoginPipelineProxyPlayerIsKickedWithoutErrors() throws Exception {
|
||||
PlayerMock player = server.addPlayer("PipelineProxyPlayer");
|
||||
InetAddress address = InetAddress.getByName("1.1.1.1");
|
||||
|
||||
when(vpnExecutor.checkIp("1.1.1.1")).thenReturn(CompletableFuture.completedFuture(
|
||||
VPNResponse.builder().success(true).proxy(true).ip("1.1.1.1")
|
||||
.method("N/A").countryName("N/A").countryCode("N/A").city("N/A").build()
|
||||
));
|
||||
|
||||
mockCache();
|
||||
PlayerLoginEvent event = new PlayerLoginEvent(player, "localhost", address);
|
||||
|
||||
assertDoesNotThrow(() -> listener.onLogin(event));
|
||||
|
||||
@@ -13,6 +13,7 @@ dependencies {
|
||||
testImplementation 'org.mockito:mockito-subclass:5.11.0'
|
||||
testImplementation 'org.mockito:mockito-junit-jupiter:5.11.0'
|
||||
testImplementation 'com.github.ben-manes.caffeine:caffeine:3.1.8'
|
||||
testImplementation testFixtures(project(':Common:Source'))
|
||||
}
|
||||
tasks.compileJava.dependsOn(':Common:Source:jar')
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
public class BungeePlayer extends APIPlayer {
|
||||
|
||||
private final ProxiedPlayer player;
|
||||
|
||||
public BungeePlayer(ProxiedPlayer player) {
|
||||
super(player.getUniqueId(), player.getName(), player.getAddress().getAddress());
|
||||
|
||||
|
||||
+1
@@ -30,6 +30,7 @@ import java.util.stream.IntStream;
|
||||
public class BungeeCommand extends Command implements TabExecutor {
|
||||
|
||||
private final dev.brighten.antivpn.command.Command command;
|
||||
|
||||
public BungeeCommand(dev.brighten.antivpn.command.Command command) {
|
||||
super(command.name(), command.permission(), command.aliases());
|
||||
|
||||
|
||||
+4
-6
@@ -1,6 +1,7 @@
|
||||
package dev.brighten.antivpn.bungee;
|
||||
|
||||
import dev.brighten.antivpn.AntiVPN;
|
||||
import dev.brighten.antivpn.StandardTest;
|
||||
import dev.brighten.antivpn.api.PlayerExecutor;
|
||||
import dev.brighten.antivpn.api.VPNConfig;
|
||||
import dev.brighten.antivpn.api.VPNExecutor;
|
||||
@@ -21,7 +22,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class BungeeListenerTest {
|
||||
public class BungeeListenerTest extends StandardTest {
|
||||
|
||||
private BungeeListener listener;
|
||||
private VPNExecutor vpnExecutor;
|
||||
@@ -86,7 +87,7 @@ public class BungeeListenerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreLoginEventBlocked() {
|
||||
public void testPreLoginEventBlocked() throws NoSuchFieldException, IllegalAccessException {
|
||||
PreLoginEvent event = mock(PreLoginEvent.class);
|
||||
PendingConnection connection = mock(PendingConnection.class);
|
||||
|
||||
@@ -97,10 +98,7 @@ public class BungeeListenerTest {
|
||||
when(connection.getSocketAddress()).thenReturn(new InetSocketAddress("1.1.1.1", 12345));
|
||||
|
||||
// Mock proxy response
|
||||
when(vpnExecutor.checkIp("1.1.1.1")).thenReturn(CompletableFuture.completedFuture(
|
||||
VPNResponse.builder().success(true).proxy(true).ip("1.1.1.1")
|
||||
.method("N/A").countryName("N/A").countryCode("N/A").city("N/A").build()
|
||||
));
|
||||
mockCache();
|
||||
|
||||
listener.onListener(event);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
plugins {
|
||||
id 'com.gradleup.shadow'
|
||||
id 'java-test-fixtures'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@@ -24,6 +25,7 @@ dependencies {
|
||||
testImplementation 'com.h2database:h2:2.4.240'
|
||||
testImplementation 'org.mongodb:mongo-java-driver:3.12.14'
|
||||
testImplementation 'com.github.ben-manes.caffeine:caffeine:3.1.8'
|
||||
testFixturesImplementation 'com.github.ben-manes.caffeine:caffeine:3.1.8'
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
|
||||
@@ -36,7 +36,7 @@ public abstract class APIPlayer {
|
||||
@Setter
|
||||
private boolean alertsEnabled;
|
||||
|
||||
private static final Cache<String, CheckResult> checkResultCache = Caffeine.newBuilder()
|
||||
public static final Cache<String, CheckResult> checkResultCache = Caffeine.newBuilder()
|
||||
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||
.maximumSize(2000)
|
||||
.build();
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.brighten.antivpn.utils;
|
||||
|
||||
public class ReflectionUtils {
|
||||
|
||||
public static <T> T getDeclaredField(Class<?> clazz, String name) throws NoSuchFieldException, IllegalAccessException {
|
||||
var declaredField = clazz.getDeclaredField(name);
|
||||
|
||||
declaredField.setAccessible(true);
|
||||
|
||||
return (T) declaredField.get(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package dev.brighten.antivpn;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import dev.brighten.antivpn.api.APIPlayer;
|
||||
import dev.brighten.antivpn.api.CheckResult;
|
||||
import dev.brighten.antivpn.api.ResultType;
|
||||
import dev.brighten.antivpn.web.objects.VPNResponse;
|
||||
|
||||
public class StandardTest {
|
||||
|
||||
protected void mockCache() throws NoSuchFieldException, IllegalAccessException {
|
||||
mockCache("1.1.1.1", new CheckResult(VPNResponse.builder().success(true).proxy(true).ip("1.1.1.1")
|
||||
.method("N/A").countryName("N/A").countryCode("N/A").city("N/A").build(), ResultType.DENIED_PROXY, true));
|
||||
}
|
||||
|
||||
protected void mockCache(String ip, CheckResult result) throws NoSuchFieldException, IllegalAccessException {
|
||||
var field = APIPlayer.class.getDeclaredField("checkResultCache");
|
||||
field.setAccessible(true);
|
||||
Cache<String, CheckResult> checkResultCache = (Cache<String, CheckResult>) field.get(null);
|
||||
|
||||
checkResultCache.put(ip, result);
|
||||
}
|
||||
}
|
||||
@@ -25,8 +25,10 @@ public interface LoaderBootstrap {
|
||||
|
||||
void onLoad(File dataFolder);
|
||||
|
||||
default void onEnable() {}
|
||||
default void onEnable() {
|
||||
}
|
||||
|
||||
default void onDisable() {}
|
||||
default void onDisable() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ dependencies {
|
||||
testImplementation 'org.mockito:mockito-subclass:5.11.0'
|
||||
testImplementation 'org.mockito:mockito-junit-jupiter:5.11.0'
|
||||
testImplementation 'com.github.ben-manes.caffeine:caffeine:3.1.8'
|
||||
testImplementation testFixtures(project(':Common:Source'))
|
||||
}
|
||||
tasks.compileJava.dependsOn(':Common:Source:jar')
|
||||
|
||||
|
||||
+4
-6
@@ -1,6 +1,7 @@
|
||||
package dev.brighten.antivpn.sponge;
|
||||
|
||||
import dev.brighten.antivpn.AntiVPN;
|
||||
import dev.brighten.antivpn.StandardTest;
|
||||
import dev.brighten.antivpn.api.PlayerExecutor;
|
||||
import dev.brighten.antivpn.api.VPNConfig;
|
||||
import dev.brighten.antivpn.api.VPNExecutor;
|
||||
@@ -22,7 +23,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class SpongeListenerTest {
|
||||
public class SpongeListenerTest extends StandardTest {
|
||||
|
||||
private SpongeListener listener;
|
||||
private VPNExecutor vpnExecutor;
|
||||
@@ -89,7 +90,7 @@ public class SpongeListenerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoginEventBlocked() {
|
||||
public void testLoginEventBlocked() throws NoSuchFieldException, IllegalAccessException {
|
||||
ServerSideConnectionEvent.Login event = mock(ServerSideConnectionEvent.Login.class);
|
||||
GameProfile profile = mock(GameProfile.class);
|
||||
ServerSideConnection connection = mock(ServerSideConnection.class);
|
||||
@@ -101,10 +102,7 @@ public class SpongeListenerTest {
|
||||
when(connection.address()).thenReturn(new InetSocketAddress("1.1.1.1", 12345));
|
||||
|
||||
// Mock proxy response
|
||||
when(vpnExecutor.checkIp("1.1.1.1")).thenReturn(CompletableFuture.completedFuture(
|
||||
VPNResponse.builder().success(true).proxy(true).ip("1.1.1.1")
|
||||
.method("N/A").countryName("N/A").countryCode("N/A").city("N/A").build()
|
||||
));
|
||||
mockCache();
|
||||
|
||||
listener.onJoin(event);
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ dependencies {
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter:5.11.4'
|
||||
testImplementation project(':Common:Source')
|
||||
testImplementation project(':Common:loader-utils')
|
||||
testImplementation testFixtures(project(':Common:Source'))
|
||||
}
|
||||
tasks.compileJava.dependsOn(':Common:Source:jar')
|
||||
|
||||
|
||||
+6
-11
@@ -3,6 +3,7 @@ package dev.brighten.antivpn.velocity;
|
||||
import com.velocitypowered.api.event.connection.LoginEvent;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import dev.brighten.antivpn.AntiVPN;
|
||||
import dev.brighten.antivpn.StandardTest;
|
||||
import dev.brighten.antivpn.api.PlayerExecutor;
|
||||
import dev.brighten.antivpn.api.VPNConfig;
|
||||
import dev.brighten.antivpn.api.VPNExecutor;
|
||||
@@ -23,24 +24,21 @@ import java.util.logging.Logger;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class VelocityListenerTest {
|
||||
public class VelocityListenerTest extends StandardTest {
|
||||
|
||||
private VelocityListener listener;
|
||||
private AntiVPN antiVPN;
|
||||
private VPNConfig config;
|
||||
private PlayerExecutor playerExecutor;
|
||||
private VPNExecutor vpnExecutor;
|
||||
private MessageHandler messageHandler;
|
||||
private VelocityPlugin velocityPlugin;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
antiVPN = mock(AntiVPN.class);
|
||||
AntiVPN antiVPN = mock(AntiVPN.class);
|
||||
config = mock(VPNConfig.class);
|
||||
playerExecutor = mock(PlayerExecutor.class);
|
||||
vpnExecutor = mock(VPNExecutor.class);
|
||||
messageHandler = mock(MessageHandler.class);
|
||||
velocityPlugin = mock(VelocityPlugin.class);
|
||||
MessageHandler messageHandler = mock(MessageHandler.class);
|
||||
VelocityPlugin velocityPlugin = mock(VelocityPlugin.class);
|
||||
|
||||
when(antiVPN.getVpnConfig()).thenReturn(config);
|
||||
when(antiVPN.getPlayerExecutor()).thenReturn(playerExecutor);
|
||||
@@ -114,10 +112,7 @@ public class VelocityListenerTest {
|
||||
when(player.getRemoteAddress()).thenReturn(new InetSocketAddress("1.1.1.1", 12345));
|
||||
|
||||
// Mock proxy response
|
||||
when(vpnExecutor.checkIp("1.1.1.1")).thenReturn(CompletableFuture.completedFuture(
|
||||
VPNResponse.builder().success(true).proxy(true).ip("1.1.1.1")
|
||||
.method("N/A").countryName("N/A").city("N/A").countryCode("N/A").build()
|
||||
));
|
||||
mockCache();
|
||||
|
||||
listener.onLogin(event);
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'com.gradleup.shadow' version '9.4.1'
|
||||
id 'com.diffplug.spotless' version '8.4.0' apply false
|
||||
id 'xyz.jpenilla.run-velocity' version '3.0.2' apply false
|
||||
}
|
||||
|
||||
@@ -30,6 +31,7 @@ allprojects {
|
||||
}
|
||||
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'com.diffplug.spotless'
|
||||
|
||||
java {
|
||||
toolchain {
|
||||
@@ -42,6 +44,33 @@ allprojects {
|
||||
options.compilerArgs << '-XDignore.symbol.file'
|
||||
}
|
||||
|
||||
spotless {
|
||||
java {
|
||||
target 'src/**/*.java'
|
||||
|
||||
def ideaFormatter = idea()
|
||||
if (rootProject.hasProperty('spotlessIdeaBinary')) {
|
||||
ideaFormatter.binaryPath(rootProject.property('spotlessIdeaBinary').toString())
|
||||
}
|
||||
if (rootProject.hasProperty('spotlessIdeaCodeStyle')) {
|
||||
ideaFormatter.codeStyleSettingsPath(rootProject.file(rootProject.property('spotlessIdeaCodeStyle').toString()).absolutePath)
|
||||
}
|
||||
|
||||
trimTrailingWhitespace()
|
||||
endWithNewline()
|
||||
}
|
||||
|
||||
format 'gradle', {
|
||||
target '*.gradle'
|
||||
trimTrailingWhitespace()
|
||||
endWithNewline()
|
||||
}
|
||||
}
|
||||
|
||||
tasks.named('check') {
|
||||
dependsOn tasks.named('spotlessCheck')
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly 'org.projectlombok:lombok:1.18.44'
|
||||
annotationProcessor 'org.projectlombok:lombok:1.18.44'
|
||||
|
||||
@@ -14,5 +14,3 @@ include 'Sponge:SpongeLoader'
|
||||
|
||||
include 'Velocity:VelocityPlugin'
|
||||
include 'Velocity:VelocityLoader'
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user