mirror of
https://github.com/funkemunky/AntiVPN.git
synced 2026-05-31 09:31:54 +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 {
|
||||
|
||||
@@ -44,9 +44,9 @@ public class BukkitCommandExecutor implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public Optional<APIPlayer> getPlayer() {
|
||||
if(!isPlayer()) return Optional.empty();
|
||||
if (!isPlayer()) return Optional.empty();
|
||||
|
||||
return AntiVPN.getInstance().getPlayerExecutor().getPlayer(((Player)sender).getUniqueId());
|
||||
return AntiVPN.getInstance().getPlayerExecutor().getPlayer(((Player) sender).getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -85,9 +85,9 @@ public class BukkitListener extends VPNExecutor implements Listener {
|
||||
|
||||
CheckResult result = player.checkPlayer();
|
||||
|
||||
if(!result.resultType().isShouldBlock()) return;
|
||||
if (!result.resultType().isShouldBlock()) return;
|
||||
|
||||
if(!AntiVPN.getInstance().getVpnConfig().isKickPlayers()) {
|
||||
if (!AntiVPN.getInstance().getVpnConfig().isKickPlayers()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -98,12 +98,11 @@ public class BukkitListener extends VPNExecutor implements Listener {
|
||||
player,
|
||||
result.response()
|
||||
);
|
||||
case DENIED_PROXY ->
|
||||
StringUtil.varReplace(
|
||||
AntiVPN.getInstance().getVpnConfig().getKickMessage(),
|
||||
player,
|
||||
result.response()
|
||||
);
|
||||
case DENIED_PROXY -> StringUtil.varReplace(
|
||||
AntiVPN.getInstance().getVpnConfig().getKickMessage(),
|
||||
player,
|
||||
result.response()
|
||||
);
|
||||
default -> "You were kicked by KauriVPN for an unknown reason!";
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ public class BukkitPlayerExecutor implements PlayerExecutor {
|
||||
public Optional<APIPlayer> getPlayer(String name) {
|
||||
final Player player = Bukkit.getPlayer(name);
|
||||
|
||||
if(player == null) {
|
||||
if (player == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class BukkitPlayerExecutor implements PlayerExecutor {
|
||||
public Optional<APIPlayer> getPlayer(UUID uuid) {
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
|
||||
if(player == null) {
|
||||
if (player == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ public class BukkitPlugin implements LoaderBootstrap {
|
||||
playerCommandRunner.start();
|
||||
|
||||
// Loading our bStats metrics to be pushed to https://bstats.org
|
||||
if(AntiVPN.getInstance().getVpnConfig().metrics()) {
|
||||
if (AntiVPN.getInstance().getVpnConfig().metrics()) {
|
||||
Bukkit.getLogger().info("Starting bStats metrics...");
|
||||
Metrics metrics = new Metrics(plugin, 12615);
|
||||
metrics.addCustomChart(new SimplePie("database_used", this::getDatabaseType));
|
||||
@@ -129,7 +129,7 @@ public class BukkitPlugin implements LoaderBootstrap {
|
||||
Field field = SimpleCommandMap.class.getDeclaredField("knownCommands");
|
||||
field.setAccessible(true);
|
||||
|
||||
if(field.get(commandMap) instanceof Map<?, ?> knownCommands) {
|
||||
if (field.get(commandMap) instanceof Map<?, ?> knownCommands) {
|
||||
Map<String, org.bukkit.command.Command> casted = (Map<String, org.bukkit.command.Command>) knownCommands;
|
||||
casted.values().removeAll(registeredCommands);
|
||||
registeredCommands.clear();
|
||||
@@ -149,11 +149,11 @@ public class BukkitPlugin implements LoaderBootstrap {
|
||||
private String getDatabaseType() {
|
||||
VPNDatabase database = AntiVPN.getInstance().getDatabase();
|
||||
|
||||
if(database instanceof MySqlVPN) {
|
||||
if (database instanceof MySqlVPN) {
|
||||
return "MySQL";
|
||||
} else if(database instanceof H2VPN) {
|
||||
} else if (database instanceof H2VPN) {
|
||||
return "H2";
|
||||
} else if(database instanceof MongoVPN) {
|
||||
} else if (database instanceof MongoVPN) {
|
||||
return "MongoDB";
|
||||
} else {
|
||||
return "No-Database";
|
||||
|
||||
@@ -41,12 +41,12 @@ public class PlayerCommandRunner {
|
||||
void start() {
|
||||
executorService.scheduleAtFixedRate(() -> {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
while(!playerActions.isEmpty()) {
|
||||
while (!playerActions.isEmpty()) {
|
||||
PlayerAction action = playerActions.peek();
|
||||
|
||||
if(action == null) continue;
|
||||
if (action == null) continue;
|
||||
|
||||
if(currentTime - action.start > 2000L || Bukkit.getPlayer(action.getUuid()) != null) {
|
||||
if (currentTime - action.start > 2000L || Bukkit.getPlayer(action.getUuid()) != null) {
|
||||
new BukkitRunnable() {
|
||||
public void run() {
|
||||
action.getAction().run();
|
||||
|
||||
@@ -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()));
|
||||
|
||||
@@ -41,9 +42,9 @@ public class BukkitCommand extends org.bukkit.command.Command {
|
||||
throws IllegalArgumentException {
|
||||
val children = command.children();
|
||||
|
||||
if(children.length > 0 && args.length > 0) {
|
||||
if (children.length > 0 && args.length > 0) {
|
||||
for (Command child : children) {
|
||||
if(child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases())
|
||||
if (child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases())
|
||||
.anyMatch(alias2 -> alias2.equalsIgnoreCase(args[0]))) {
|
||||
return child.tabComplete(new BukkitCommandExecutor(sender), alias, IntStream
|
||||
.range(0, args.length - 1)
|
||||
@@ -56,7 +57,7 @@ public class BukkitCommand extends org.bukkit.command.Command {
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String s, String[] args) {
|
||||
if(!sender.hasPermission("antivpn.command.*")
|
||||
if (!sender.hasPermission("antivpn.command.*")
|
||||
&& !sender.hasPermission(command.permission())) {
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
|
||||
AntiVPN.getInstance().getMessageHandler().getString("no-permission").getMessage()));
|
||||
@@ -65,11 +66,11 @@ public class BukkitCommand extends org.bukkit.command.Command {
|
||||
|
||||
val children = command.children();
|
||||
|
||||
if(children.length > 0 && args.length > 0) {
|
||||
if (children.length > 0 && args.length > 0) {
|
||||
for (Command child : children) {
|
||||
if(child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases())
|
||||
if (child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases())
|
||||
.anyMatch(alias -> alias.equalsIgnoreCase(args[0]))) {
|
||||
if(!sender.hasPermission("antivpn.command.*")
|
||||
if (!sender.hasPermission("antivpn.command.*")
|
||||
&& !sender.hasPermission(child.permission())) {
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
|
||||
AntiVPN.getInstance().getMessageHandler().getString("no-permission").getMessage()));
|
||||
|
||||
@@ -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;
|
||||
@@ -56,32 +55,32 @@ public class BukkitListenerTest {
|
||||
PlayerExecutor playerExecutor = mock(PlayerExecutor.class);
|
||||
vpnExecutor = mock(VPNExecutor.class);
|
||||
MessageHandler messageHandler = mock(MessageHandler.class);
|
||||
|
||||
|
||||
when(antiVPN.getVpnConfig()).thenReturn(config);
|
||||
when(antiVPN.getPlayerExecutor()).thenReturn(playerExecutor);
|
||||
when(antiVPN.getExecutor()).thenReturn(vpnExecutor);
|
||||
when(antiVPN.getMessageHandler()).thenReturn(messageHandler);
|
||||
|
||||
|
||||
when(playerExecutor.getPlayer(any(UUID.class))).thenReturn(Optional.empty());
|
||||
when(config.getPrefixWhitelists()).thenReturn(java.util.Collections.emptyList());
|
||||
when(config.getCountryList()).thenReturn(java.util.Collections.emptyList());
|
||||
when(config.isKickPlayers()).thenReturn(true);
|
||||
when(config.getKickMessage()).thenReturn("Blocked!");
|
||||
|
||||
|
||||
VpnString mockVpnString = mock(VpnString.class);
|
||||
when(mockVpnString.getFormattedMessage(any())).thenReturn("Blocked!");
|
||||
when(messageHandler.getString(anyString())).thenReturn(mockVpnString);
|
||||
|
||||
|
||||
when(vpnExecutor.checkIp(anyString())).thenReturn(CompletableFuture.completedFuture(
|
||||
VPNResponse.builder().success(true).proxy(false).ip("127.0.0.1")
|
||||
.method("N/A").countryName("N/A").city("N/A").build()
|
||||
));
|
||||
|
||||
|
||||
// Use reflection to set the private static INSTANCE field
|
||||
Field instanceField = AntiVPN.class.getDeclaredField("INSTANCE");
|
||||
instanceField.setAccessible(true);
|
||||
instanceField.set(null, antiVPN);
|
||||
|
||||
|
||||
listener = new BukkitListener();
|
||||
}
|
||||
|
||||
@@ -92,7 +91,7 @@ public class BukkitListenerTest {
|
||||
instanceField.setAccessible(true);
|
||||
instanceField.set(null, null);
|
||||
BukkitPlugin.pluginInstance = null;
|
||||
|
||||
|
||||
MockBukkit.unmock();
|
||||
}
|
||||
|
||||
@@ -100,31 +99,16 @@ public class BukkitListenerTest {
|
||||
public void testLoginEventAllowed() throws Exception {
|
||||
PlayerMock player = server.addPlayer("TestPlayer");
|
||||
InetAddress address = InetAddress.getByName("127.0.0.1");
|
||||
|
||||
PlayerLoginEvent event = new PlayerLoginEvent(player, "localhost", address);
|
||||
|
||||
listener.onLogin(event);
|
||||
|
||||
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()
|
||||
));
|
||||
|
||||
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);
|
||||
|
||||
assertEquals(PlayerLoginEvent.Result.KICK_BANNED, event.getResult());
|
||||
assertEquals("Blocked!", net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().serialize(event.kickMessage()));
|
||||
|
||||
assertEquals(PlayerLoginEvent.Result.ALLOWED, event.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -132,11 +116,7 @@ public class BukkitListenerTest {
|
||||
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')
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ public class BungeeListener extends VPNExecutor implements Listener {
|
||||
|
||||
if (!result.resultType().isShouldBlock()) return;
|
||||
|
||||
if(!AntiVPN.getInstance().getVpnConfig().isKickPlayers()) {
|
||||
if (!AntiVPN.getInstance().getVpnConfig().isKickPlayers()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ public class BungeeListener extends VPNExecutor implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onJoin(LoginEvent event) {
|
||||
if(event.isCancelled()) return;
|
||||
if (event.isCancelled()) return;
|
||||
|
||||
// Handling player alerts on join
|
||||
AntiVPN.getInstance().getPlayerExecutor().getPlayer(event.getConnection().getUniqueId())
|
||||
|
||||
@@ -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());
|
||||
|
||||
|
||||
+2
-2
@@ -31,7 +31,7 @@ public class BungeePlayerExecutor implements PlayerExecutor {
|
||||
public Optional<APIPlayer> getPlayer(String name) {
|
||||
ProxiedPlayer player = BungeePlugin.pluginInstance.getProxy().getPlayer(name);
|
||||
|
||||
if(player == null) return Optional.empty();
|
||||
if (player == null) return Optional.empty();
|
||||
|
||||
return Optional.of(cachedPlayers.computeIfAbsent(player.getUniqueId(), key -> new BungeePlayer(player)));
|
||||
}
|
||||
@@ -40,7 +40,7 @@ public class BungeePlayerExecutor implements PlayerExecutor {
|
||||
public Optional<APIPlayer> getPlayer(UUID uuid) {
|
||||
ProxiedPlayer player = BungeePlugin.pluginInstance.getProxy().getPlayer(uuid);
|
||||
|
||||
if(player == null) return Optional.empty();
|
||||
if (player == null) return Optional.empty();
|
||||
|
||||
return Optional.of(cachedPlayers.computeIfAbsent(uuid, key -> new BungeePlayer(player)));
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ import java.util.concurrent.TimeUnit;
|
||||
public class BungeePlugin implements LoaderBootstrap {
|
||||
|
||||
public static BungeePlugin pluginInstance;
|
||||
|
||||
|
||||
@Getter
|
||||
private File dataFolder;
|
||||
|
||||
@@ -64,7 +64,7 @@ public class BungeePlugin implements LoaderBootstrap {
|
||||
ProxyServer.getInstance().getLogger().info("Starting AntiVPN services...");
|
||||
AntiVPN.start(new BungeeListener(), new BungeePlayerExecutor(), getDataFolder());
|
||||
|
||||
if(AntiVPN.getInstance().getVpnConfig().metrics()) {
|
||||
if (AntiVPN.getInstance().getVpnConfig().metrics()) {
|
||||
ProxyServer.getInstance().getLogger().info("Starting bStats metrics...");
|
||||
Metrics metrics = new Metrics(getPlugin(), 12616);
|
||||
metrics.addCustomChart(new SimplePie("database_used", this::getDatabaseType));
|
||||
@@ -85,11 +85,11 @@ public class BungeePlugin implements LoaderBootstrap {
|
||||
|
||||
private String getDatabaseType() {
|
||||
VPNDatabase database = AntiVPN.getInstance().getDatabase();
|
||||
if(database instanceof MySqlVPN) {
|
||||
if (database instanceof MySqlVPN) {
|
||||
return "MySQL";
|
||||
} else if(database instanceof H2VPN) {
|
||||
} else if (database instanceof H2VPN) {
|
||||
return "H2";
|
||||
} else if(database instanceof MongoVPN) {
|
||||
} else if (database instanceof MongoVPN) {
|
||||
return "MongoDB";
|
||||
} else {
|
||||
return "No-Database";
|
||||
|
||||
+8
-7
@@ -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());
|
||||
|
||||
@@ -38,7 +39,7 @@ public class BungeeCommand extends Command implements TabExecutor {
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String[] args) {
|
||||
if(!sender.hasPermission("antivpn.command.*")
|
||||
if (!sender.hasPermission("antivpn.command.*")
|
||||
&& !sender.hasPermission(command.permission())) {
|
||||
sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&',
|
||||
AntiVPN.getInstance().getMessageHandler().getString("no-permission").getMessage())));
|
||||
@@ -47,11 +48,11 @@ public class BungeeCommand extends Command implements TabExecutor {
|
||||
|
||||
val children = command.children();
|
||||
|
||||
if(children.length > 0 && args.length > 0) {
|
||||
if (children.length > 0 && args.length > 0) {
|
||||
for (dev.brighten.antivpn.command.Command child : children) {
|
||||
if(child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases())
|
||||
if (child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases())
|
||||
.anyMatch(alias -> alias.equalsIgnoreCase(args[0]))) {
|
||||
if(!sender.hasPermission("antivpn.command.*")
|
||||
if (!sender.hasPermission("antivpn.command.*")
|
||||
&& !sender.hasPermission(child.permission())) {
|
||||
sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&',
|
||||
AntiVPN.getInstance().getMessageHandler().getString("no-permission").getMessage())));
|
||||
@@ -61,7 +62,7 @@ public class BungeeCommand extends Command implements TabExecutor {
|
||||
sender.sendMessage(TextComponent
|
||||
.fromLegacyText(ChatColor
|
||||
.translateAlternateColorCodes('&',
|
||||
child.execute(new BungeeCommandExecutor(sender), IntStream
|
||||
child.execute(new BungeeCommandExecutor(sender), IntStream
|
||||
.range(0, args.length - 1)
|
||||
.mapToObj(i -> args[i + 1]).toArray(String[]::new)))));
|
||||
return;
|
||||
@@ -80,9 +81,9 @@ public class BungeeCommand extends Command implements TabExecutor {
|
||||
public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
val children = command.children();
|
||||
|
||||
if(children.length > 0 && args.length > 0) {
|
||||
if (children.length > 0 && args.length > 0) {
|
||||
for (dev.brighten.antivpn.command.Command child : children) {
|
||||
if(child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases())
|
||||
if (child.name().equalsIgnoreCase(args[0]) || Arrays.stream(child.aliases())
|
||||
.anyMatch(alias2 -> alias2.equalsIgnoreCase(args[0]))) {
|
||||
return child.tabComplete(new BungeeCommandExecutor(sender), "alias", IntStream
|
||||
.range(0, args.length - 1)
|
||||
|
||||
+1
-1
@@ -45,7 +45,7 @@ public class BungeeCommandExecutor implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public Optional<APIPlayer> getPlayer() {
|
||||
if(!isPlayer()) return Optional.empty();
|
||||
if (!isPlayer()) return Optional.empty();
|
||||
|
||||
return AntiVPN.getInstance().getPlayerExecutor().getPlayer(((ProxiedPlayer) sender).getUniqueId());
|
||||
}
|
||||
|
||||
+5
-7
@@ -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;
|
||||
@@ -71,7 +72,7 @@ public class BungeeListenerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreLoginEventAllowed() {
|
||||
public void testPreLoginEventAllowed() {
|
||||
PreLoginEvent event = mock(PreLoginEvent.class);
|
||||
PendingConnection connection = mock(PendingConnection.class);
|
||||
|
||||
@@ -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 {
|
||||
@@ -7,12 +8,12 @@ dependencies {
|
||||
implementation 'org.ow2.asm:asm-commons:9.8'
|
||||
implementation 'org.yaml:snakeyaml:2.2'
|
||||
implementation 'org.jetbrains:annotations:26.0.2'
|
||||
|
||||
|
||||
compileOnly 'com.mysql:mysql-connector-j:9.3.0'
|
||||
compileOnly 'com.h2database:h2:2.4.240'
|
||||
implementation'com.github.ben-manes.caffeine:caffeine:3.1.8'
|
||||
compileOnly 'org.mongodb:mongo-java-driver:3.12.14'
|
||||
|
||||
|
||||
testImplementation 'org.mockito:mockito-core:5.11.0'
|
||||
testImplementation "org.junit.jupiter:junit-jupiter:5.8.1"
|
||||
testImplementation "org.testcontainers:testcontainers:2.0.4"
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -70,7 +70,7 @@ public class JarInJarClassLoader extends URLClassLoader {
|
||||
* Creates a new jar-in-jar class loader.
|
||||
*
|
||||
* @param loaderClassLoader the loader plugin's classloader (setup and created by the platform)
|
||||
* @param jarResourcePath the path to the jar-in-jar resource within the loader jar
|
||||
* @param jarResourcePath the path to the jar-in-jar resource within the loader jar
|
||||
* @throws LoadingException if something unexpectedly bad happens
|
||||
*/
|
||||
public JarInJarClassLoader(ClassLoader loaderClassLoader, String... jarResourcePath) throws LoadingException {
|
||||
@@ -168,11 +168,11 @@ public class JarInJarClassLoader extends URLClassLoader {
|
||||
/**
|
||||
* Creates a new plugin instance.
|
||||
*
|
||||
* @param bootstrapClass the name of the bootstrap plugin class
|
||||
* @param bootstrapClass the name of the bootstrap plugin class
|
||||
* @param loaderPluginType the type of the loader plugin, the only parameter of the bootstrap
|
||||
* plugin constructor
|
||||
* @param loaderPlugin the loader plugin instance
|
||||
* @param <T> the type of the loader plugin
|
||||
* @param loaderPlugin the loader plugin instance
|
||||
* @param <T> the type of the loader plugin
|
||||
* @return the instantiated bootstrap plugin
|
||||
*/
|
||||
public <T> LoaderBootstrap instantiatePlugin(String bootstrapClass, Class<T> loaderPluginType, T loaderPlugin) throws LoadingException {
|
||||
@@ -202,7 +202,7 @@ public class JarInJarClassLoader extends URLClassLoader {
|
||||
* then returns a URL that can be used by the {@link JarInJarClassLoader}.
|
||||
*
|
||||
* @param loaderClassLoader the classloader for the "host" loader plugin
|
||||
* @param jarResourcePath the inner jar resource path
|
||||
* @param jarResourcePath the inner jar resource path
|
||||
* @return a URL to the extracted file
|
||||
*/
|
||||
private static URL extractJar(ClassLoader loaderClassLoader, String jarResourcePath) throws LoadingException {
|
||||
|
||||
@@ -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