Login performance (#1331)

* Ignore intellij tmp files

* Enhance onLogin performance
This commit is contained in:
Gabriele C 2017-09-17 09:05:13 +02:00 committed by ljacqu
parent 3d67305e9e
commit 6c6fbaf12d
6 changed files with 102 additions and 76 deletions

1
.gitignore vendored
View File

@ -17,6 +17,7 @@ hs_err_pid*
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
# Ignore project files # Ignore project files
*.iml *.iml
*.java___jb_tmp___
# Ignore IDEA directory # Ignore IDEA directory
.idea/* .idea/*

View File

@ -64,16 +64,16 @@ public class OnJoinVerifier implements Reloadable {
/** /**
* Checks if Antibot is enabled. * Checks if Antibot is enabled.
* *
* @param player the player * @param name the player name
* @param isAuthAvailable whether or not the player is registered * @param isAuthAvailable whether or not the player is registered
* @throws FailedVerificationException if the verification fails * @throws FailedVerificationException if the verification fails
*/ */
public void checkAntibot(Player player, boolean isAuthAvailable) throws FailedVerificationException { public void checkAntibot(String name, boolean isAuthAvailable) throws FailedVerificationException {
if (isAuthAvailable || permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)) { if (isAuthAvailable || permissionsManager.hasPermissionOffline(name, PlayerStatePermission.BYPASS_ANTIBOT)) {
return; return;
} }
if (antiBotService.shouldKick()) { if (antiBotService.shouldKick()) {
antiBotService.addPlayerKick(player.getName()); antiBotService.addPlayerKick(name);
throw new FailedVerificationException(MessageKey.KICK_ANTIBOT); throw new FailedVerificationException(MessageKey.KICK_ANTIBOT);
} }
} }
@ -148,14 +148,13 @@ public class OnJoinVerifier implements Reloadable {
/** /**
* Checks that the casing in the username corresponds to the one in the database, if so configured. * Checks that the casing in the username corresponds to the one in the database, if so configured.
* *
* @param player the player to verify * @param connectingName the player name to verify
* @param auth the auth object associated with the player * @param auth the auth object associated with the player
* @throws FailedVerificationException if the verification fails * @throws FailedVerificationException if the verification fails
*/ */
public void checkNameCasing(Player player, PlayerAuth auth) throws FailedVerificationException { public void checkNameCasing(String connectingName, PlayerAuth auth) throws FailedVerificationException {
if (auth != null && settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)) { if (auth != null && settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)) {
String realName = auth.getRealName(); // might be null or "Player" String realName = auth.getRealName(); // might be null or "Player"
String connectingName = player.getName();
if (StringUtils.isEmpty(realName) || "Player".equals(realName)) { if (StringUtils.isEmpty(realName) || "Player".equals(realName)) {
dataSource.updateRealName(connectingName.toLowerCase(), connectingName); dataSource.updateRealName(connectingName.toLowerCase(), connectingName);
@ -168,15 +167,15 @@ public class OnJoinVerifier implements Reloadable {
/** /**
* Checks that the player's country is admitted. * Checks that the player's country is admitted.
* *
* @param player the player * @param name the player name
* @param address the player address * @param address the player address
* @param isAuthAvailable whether or not the user is registered * @param isAuthAvailable whether or not the user is registered
* @throws FailedVerificationException if the verification fails * @throws FailedVerificationException if the verification fails
*/ */
public void checkPlayerCountry(Player player, String address, boolean isAuthAvailable) throws FailedVerificationException { public void checkPlayerCountry(String name, String address, boolean isAuthAvailable) throws FailedVerificationException {
if ((!isAuthAvailable || settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)) if ((!isAuthAvailable || settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED))
&& settings.getProperty(ProtectionSettings.ENABLE_PROTECTION) && settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)
&& !permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_COUNTRY_CHECK) && !permissionsManager.hasPermissionOffline(name, PlayerStatePermission.BYPASS_COUNTRY_CHECK)
&& !validationService.isCountryAdmitted(address)) { && !validationService.isCountryAdmitted(address)) {
throw new FailedVerificationException(MessageKey.COUNTRY_BANNED_ERROR); throw new FailedVerificationException(MessageKey.COUNTRY_BANNED_ERROR);
} }

View File

@ -12,6 +12,7 @@ import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
@ -26,6 +27,7 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerBedEnterEvent; import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
@ -77,6 +79,8 @@ public class PlayerListener implements Listener {
@Inject @Inject
private JoinMessageService joinMessageService; private JoinMessageService joinMessageService;
private boolean isAsyncPlayerPreLoginEventCalled = false;
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
String cmd = event.getMessage().split(" ")[0].toLowerCase(); String cmd = event.getMessage().split(" ")[0].toLowerCase();
@ -190,27 +194,7 @@ public class PlayerListener implements Listener {
management.performJoin(player); management.performJoin(player);
} }
// Note #831: AsyncPlayerPreLoginEvent is not fired by all servers in offline mode private void runOnJoinChecks(String name, String ip) throws FailedVerificationException {
// e.g. CraftBukkit does not fire it. So we need to run crucial things with PlayerLoginEvent.
// Single session feature can be implemented for Spigot and CraftBukkit by canceling a kick
// event caused by "logged in from another location". The nicer way, but only for Spigot, would be
// to check in the AsyncPlayerPreLoginEvent. To support all servers, we use the less nice way.
@EventHandler(priority = EventPriority.LOW)
public void onPlayerLogin(PlayerLoginEvent event) {
final Player player = event.getPlayer();
final String name = player.getName();
if (validationService.isUnrestricted(name)) {
return;
}
if (onJoinVerifier.refusePlayerForFullServer(event)) {
return;
}
if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) {
return;
}
try {
// Fast stuff // Fast stuff
onJoinVerifier.checkSingleSession(name); onJoinVerifier.checkSingleSession(name);
onJoinVerifier.checkIsValidName(name); onJoinVerifier.checkIsValidName(name);
@ -220,16 +204,61 @@ public class PlayerListener implements Listener {
final PlayerAuth auth = dataSource.getAuth(name); final PlayerAuth auth = dataSource.getAuth(name);
final boolean isAuthAvailable = auth != null; final boolean isAuthAvailable = auth != null;
onJoinVerifier.checkKickNonRegistered(isAuthAvailable); onJoinVerifier.checkKickNonRegistered(isAuthAvailable);
onJoinVerifier.checkAntibot(player, isAuthAvailable); onJoinVerifier.checkAntibot(name, isAuthAvailable);
onJoinVerifier.checkNameCasing(player, auth); onJoinVerifier.checkNameCasing(name, auth);
onJoinVerifier.checkPlayerCountry(player, event.getAddress().getHostAddress(), isAuthAvailable); onJoinVerifier.checkPlayerCountry(name, ip, isAuthAvailable);
}
// Note #831: AsyncPlayerPreLoginEvent is not fired by all servers in offline mode
// e.g. CraftBukkit does not fire it. So we need to run crucial things with PlayerLoginEvent.
// Single session feature can be implemented for Spigot and CraftBukkit by canceling a kick
// event caused by "logged in from another location". The nicer way, but only for Spigot, would be
// to check in the AsyncPlayerPreLoginEvent. To support all servers, we use the less nice way.
@EventHandler(priority = EventPriority.HIGHEST)
public void onAsyncPlayerPreLoginEvent(AsyncPlayerPreLoginEvent event) {
isAsyncPlayerPreLoginEventCalled = true;
final String name = event.getName();
if (validationService.isUnrestricted(name)) {
return;
}
try {
runOnJoinChecks(name, event.getAddress().getHostAddress());
} catch (FailedVerificationException e) { } catch (FailedVerificationException e) {
event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs())); event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs()));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER); event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerLogin(PlayerLoginEvent event) {
final Player player = event.getPlayer();
final String name = player.getName();
if (validationService.isUnrestricted(name)) {
return;
}
if (onJoinVerifier.refusePlayerForFullServer(event)) {
return;
}
if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) {
return; return;
} }
teleportationService.teleportOnJoin(player); teleportationService.teleportOnJoin(player);
if(!isAsyncPlayerPreLoginEventCalled) {
try {
runOnJoinChecks(name, event.getAddress().getHostAddress());
} catch (FailedVerificationException e) {
event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs()));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
}
}
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)

View File

@ -31,7 +31,7 @@ public final class ListenerConsistencyTest {
"PlayerListener#onPlayerJoin", "PlayerListener#onPlayerLogin", "PlayerListener#onPlayerJoin", "PlayerListener#onPlayerLogin",
"PlayerListener#onPlayerQuit", "ServerListener#onPluginDisable", "PlayerListener#onPlayerQuit", "ServerListener#onPluginDisable",
"ServerListener#onServerPing", "ServerListener#onPluginEnable", "ServerListener#onServerPing", "ServerListener#onPluginEnable",
"PlayerListener#onJoinMessage"); "PlayerListener#onJoinMessage", "PlayerListener#onAsyncPlayerPreLoginEvent");
@BeforeClass @BeforeClass
public static void collectListenerClasses() { public static void collectListenerClasses() {

View File

@ -248,12 +248,12 @@ public class OnJoinVerifierTest {
@Test @Test
public void shouldAllowProperlyCasedName() throws FailedVerificationException { public void shouldAllowProperlyCasedName() throws FailedVerificationException {
// given // given
Player player = newPlayerWithName("Bobby"); String name = "Bobby";
PlayerAuth auth = PlayerAuth.builder().name("bobby").realName("Bobby").build(); PlayerAuth auth = PlayerAuth.builder().name("bobby").realName("Bobby").build();
given(settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)).willReturn(true); given(settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)).willReturn(true);
// when // when
onJoinVerifier.checkNameCasing(player, auth); onJoinVerifier.checkNameCasing(name, auth);
// then // then
verifyZeroInteractions(dataSource); verifyZeroInteractions(dataSource);
@ -262,7 +262,7 @@ public class OnJoinVerifierTest {
@Test @Test
public void shouldRejectNameWithWrongCasing() throws FailedVerificationException { public void shouldRejectNameWithWrongCasing() throws FailedVerificationException {
// given // given
Player player = newPlayerWithName("Tester"); String name = "Tester";
PlayerAuth auth = PlayerAuth.builder().name("tester").realName("testeR").build(); PlayerAuth auth = PlayerAuth.builder().name("tester").realName("testeR").build();
given(settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)).willReturn(true); given(settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)).willReturn(true);
@ -270,19 +270,19 @@ public class OnJoinVerifierTest {
expectValidationExceptionWith(MessageKey.INVALID_NAME_CASE, "testeR", "Tester"); expectValidationExceptionWith(MessageKey.INVALID_NAME_CASE, "testeR", "Tester");
// when / then // when / then
onJoinVerifier.checkNameCasing(player, auth); onJoinVerifier.checkNameCasing(name, auth);
verifyZeroInteractions(dataSource); verifyZeroInteractions(dataSource);
} }
@Test @Test
public void shouldUpdateMissingRealName() throws FailedVerificationException { public void shouldUpdateMissingRealName() throws FailedVerificationException {
// given // given
Player player = newPlayerWithName("Authme"); String name = "Authme";
PlayerAuth auth = PlayerAuth.builder().name("authme").realName("").build(); PlayerAuth auth = PlayerAuth.builder().name("authme").realName("").build();
given(settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)).willReturn(true); given(settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)).willReturn(true);
// when // when
onJoinVerifier.checkNameCasing(player, auth); onJoinVerifier.checkNameCasing(name, auth);
// then // then
verify(dataSource).updateRealName("authme", "Authme"); verify(dataSource).updateRealName("authme", "Authme");
@ -291,12 +291,12 @@ public class OnJoinVerifierTest {
@Test @Test
public void shouldUpdateDefaultRealName() throws FailedVerificationException { public void shouldUpdateDefaultRealName() throws FailedVerificationException {
// given // given
Player player = newPlayerWithName("SOMEONE"); String name = "SOMEONE";
PlayerAuth auth = PlayerAuth.builder().name("someone").realName("Player").build(); PlayerAuth auth = PlayerAuth.builder().name("someone").realName("Player").build();
given(settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)).willReturn(true); given(settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)).willReturn(true);
// when // when
onJoinVerifier.checkNameCasing(player, auth); onJoinVerifier.checkNameCasing(name, auth);
// then // then
verify(dataSource).updateRealName("someone", "SOMEONE"); verify(dataSource).updateRealName("someone", "SOMEONE");
@ -305,12 +305,12 @@ public class OnJoinVerifierTest {
@Test @Test
public void shouldAcceptCasingMismatchForDisabledSetting() throws FailedVerificationException { public void shouldAcceptCasingMismatchForDisabledSetting() throws FailedVerificationException {
// given // given
Player player = newPlayerWithName("Test"); String name = "Test";
PlayerAuth auth = PlayerAuth.builder().name("test").realName("TEST").build(); PlayerAuth auth = PlayerAuth.builder().name("test").realName("TEST").build();
given(settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)).willReturn(false); given(settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)).willReturn(false);
// when // when
onJoinVerifier.checkNameCasing(player, auth); onJoinVerifier.checkNameCasing(name, auth);
// then // then
verifyZeroInteractions(dataSource); verifyZeroInteractions(dataSource);
@ -319,11 +319,11 @@ public class OnJoinVerifierTest {
@Test @Test
public void shouldAcceptNameForUnregisteredAccount() throws FailedVerificationException { public void shouldAcceptNameForUnregisteredAccount() throws FailedVerificationException {
// given // given
Player player = newPlayerWithName("MyPlayer"); String name = "MyPlayer";
PlayerAuth auth = null; PlayerAuth auth = null;
// when // when
onJoinVerifier.checkNameCasing(player, auth); onJoinVerifier.checkNameCasing(name, auth);
// then // then
verifyZeroInteractions(dataSource); verifyZeroInteractions(dataSource);
@ -347,7 +347,9 @@ public class OnJoinVerifierTest {
public void shouldRejectNameAlreadyOnline() throws FailedVerificationException { public void shouldRejectNameAlreadyOnline() throws FailedVerificationException {
// given // given
String name = "Charlie"; String name = "Charlie";
Player onlinePlayer = newPlayerWithName("charlie");
Player onlinePlayer = mock(Player.class);
given(bukkitService.getPlayerExact("Charlie")).willReturn(onlinePlayer); given(bukkitService.getPlayerExact("Charlie")).willReturn(onlinePlayer);
given(settings.getProperty(RestrictionSettings.FORCE_SINGLE_SESSION)).willReturn(true); given(settings.getProperty(RestrictionSettings.FORCE_SINGLE_SESSION)).willReturn(true);
@ -374,27 +376,27 @@ public class OnJoinVerifierTest {
@Test @Test
public void shouldAllowUser() throws FailedVerificationException { public void shouldAllowUser() throws FailedVerificationException {
// given // given
Player player = newPlayerWithName("Bobby"); String name = "Bobby";
boolean isAuthAvailable = false; boolean isAuthAvailable = false;
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false); given(permissionsManager.hasPermissionOffline(name, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false);
given(antiBotService.shouldKick()).willReturn(false); given(antiBotService.shouldKick()).willReturn(false);
// when // when
onJoinVerifier.checkAntibot(player, isAuthAvailable); onJoinVerifier.checkAntibot(name, isAuthAvailable);
// then // then
verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT); verify(permissionsManager).hasPermissionOffline(name, PlayerStatePermission.BYPASS_ANTIBOT);
verify(antiBotService).shouldKick(); verify(antiBotService).shouldKick();
} }
@Test @Test
public void shouldAllowUserWithAuth() throws FailedVerificationException { public void shouldAllowUserWithAuth() throws FailedVerificationException {
// given // given
Player player = newPlayerWithName("Lacey"); String name = "Lacey";
boolean isAuthAvailable = true; boolean isAuthAvailable = true;
// when // when
onJoinVerifier.checkAntibot(player, isAuthAvailable); onJoinVerifier.checkAntibot(name, isAuthAvailable);
// then // then
verifyZeroInteractions(permissionsManager, antiBotService); verifyZeroInteractions(permissionsManager, antiBotService);
@ -403,32 +405,32 @@ public class OnJoinVerifierTest {
@Test @Test
public void shouldAllowUserWithBypassPermission() throws FailedVerificationException { public void shouldAllowUserWithBypassPermission() throws FailedVerificationException {
// given // given
Player player = newPlayerWithName("Steward"); String name = "Steward";
boolean isAuthAvailable = false; boolean isAuthAvailable = false;
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(true); given(permissionsManager.hasPermissionOffline(name, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(true);
// when // when
onJoinVerifier.checkAntibot(player, isAuthAvailable); onJoinVerifier.checkAntibot(name, isAuthAvailable);
// then // then
verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT); verify(permissionsManager).hasPermissionOffline(name, PlayerStatePermission.BYPASS_ANTIBOT);
verifyZeroInteractions(antiBotService); verifyZeroInteractions(antiBotService);
} }
@Test @Test
public void shouldKickUserForFailedAntibotCheck() throws FailedVerificationException { public void shouldKickUserForFailedAntibotCheck() throws FailedVerificationException {
// given // given
Player player = newPlayerWithName("D3"); String name = "D3";
boolean isAuthAvailable = false; boolean isAuthAvailable = false;
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false); given(permissionsManager.hasPermissionOffline(name, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false);
given(antiBotService.shouldKick()).willReturn(true); given(antiBotService.shouldKick()).willReturn(true);
// when / then // when / then
try { try {
onJoinVerifier.checkAntibot(player, isAuthAvailable); onJoinVerifier.checkAntibot(name, isAuthAvailable);
fail("Expected exception to be thrown"); fail("Expected exception to be thrown");
} catch (FailedVerificationException e) { } catch (FailedVerificationException e) {
verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT); verify(permissionsManager).hasPermissionOffline(name, PlayerStatePermission.BYPASS_ANTIBOT);
verify(antiBotService).shouldKick(); verify(antiBotService).shouldKick();
} }
@ -439,18 +441,18 @@ public class OnJoinVerifierTest {
*/ */
@Test @Test
public void shouldNotCheckCountry() throws FailedVerificationException { public void shouldNotCheckCountry() throws FailedVerificationException {
Player player = newPlayerWithName("david"); String name = "david";
String ip = "127.0.0.1"; String ip = "127.0.0.1";
// protection setting disabled // protection setting disabled
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(false); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(false);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true);
onJoinVerifier.checkPlayerCountry(player, ip, false); onJoinVerifier.checkPlayerCountry(name, ip, false);
verifyZeroInteractions(validationService); verifyZeroInteractions(validationService);
// protection for registered players disabled // protection for registered players disabled
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(false); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(false);
onJoinVerifier.checkPlayerCountry(player, ip, true); onJoinVerifier.checkPlayerCountry(name, ip, true);
verifyZeroInteractions(validationService); verifyZeroInteractions(validationService);
} }
@ -458,12 +460,12 @@ public class OnJoinVerifierTest {
public void shouldCheckAndAcceptUnregisteredPlayerCountry() throws FailedVerificationException { public void shouldCheckAndAcceptUnregisteredPlayerCountry() throws FailedVerificationException {
// given // given
String ip = "192.168.0.1"; String ip = "192.168.0.1";
Player player = newPlayerWithName("lucas"); String name = "lucas";
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true);
given(validationService.isCountryAdmitted(ip)).willReturn(true); given(validationService.isCountryAdmitted(ip)).willReturn(true);
// when // when
onJoinVerifier.checkPlayerCountry(player, ip, false); onJoinVerifier.checkPlayerCountry(name, ip, false);
// then // then
verify(validationService).isCountryAdmitted(ip); verify(validationService).isCountryAdmitted(ip);
@ -473,13 +475,13 @@ public class OnJoinVerifierTest {
public void shouldCheckAndAcceptRegisteredPlayerCountry() throws FailedVerificationException { public void shouldCheckAndAcceptRegisteredPlayerCountry() throws FailedVerificationException {
// given // given
String ip = "192.168.10.24"; String ip = "192.168.10.24";
Player player = newPlayerWithName("gabriel"); String name = "gabriel";
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true);
given(validationService.isCountryAdmitted(ip)).willReturn(true); given(validationService.isCountryAdmitted(ip)).willReturn(true);
// when // when
onJoinVerifier.checkPlayerCountry(player, ip, true); onJoinVerifier.checkPlayerCountry(name, ip, true);
// then // then
verify(validationService).isCountryAdmitted(ip); verify(validationService).isCountryAdmitted(ip);
@ -489,7 +491,7 @@ public class OnJoinVerifierTest {
public void shouldThrowForBannedCountry() throws FailedVerificationException { public void shouldThrowForBannedCountry() throws FailedVerificationException {
// given // given
String ip = "192.168.40.0"; String ip = "192.168.40.0";
Player player = newPlayerWithName("bob"); String name = "bob";
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true); given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true);
given(validationService.isCountryAdmitted(ip)).willReturn(false); given(validationService.isCountryAdmitted(ip)).willReturn(false);
@ -497,13 +499,7 @@ public class OnJoinVerifierTest {
expectValidationExceptionWith(MessageKey.COUNTRY_BANNED_ERROR); expectValidationExceptionWith(MessageKey.COUNTRY_BANNED_ERROR);
// when // when
onJoinVerifier.checkPlayerCountry(player, ip, false); onJoinVerifier.checkPlayerCountry(name, ip, false);
}
private static Player newPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })

View File

@ -575,10 +575,10 @@ public class PlayerListenerTest {
verify(onJoinVerifier).refusePlayerForFullServer(event); verify(onJoinVerifier).refusePlayerForFullServer(event);
verify(onJoinVerifier).checkSingleSession(name); verify(onJoinVerifier).checkSingleSession(name);
verify(onJoinVerifier).checkIsValidName(name); verify(onJoinVerifier).checkIsValidName(name);
verify(onJoinVerifier).checkAntibot(player, true); verify(onJoinVerifier).checkAntibot(name, true);
verify(onJoinVerifier).checkKickNonRegistered(true); verify(onJoinVerifier).checkKickNonRegistered(true);
verify(onJoinVerifier).checkNameCasing(player, auth); verify(onJoinVerifier).checkNameCasing(name, auth);
verify(onJoinVerifier).checkPlayerCountry(player, ip, true); verify(onJoinVerifier).checkPlayerCountry(name, ip, true);
verify(teleportationService).teleportOnJoin(player); verify(teleportationService).teleportOnJoin(player);
verifyNoModifyingCalls(event); verifyNoModifyingCalls(event);
} }
@ -588,7 +588,8 @@ public class PlayerListenerTest {
// given // given
String name = "inval!dName"; String name = "inval!dName";
Player player = mockPlayerWithName(name); Player player = mockPlayerWithName(name);
PlayerLoginEvent event = spy(new PlayerLoginEvent(player, "", null)); TestHelper.mockPlayerIp(player, "33.32.33.33");
PlayerLoginEvent event = spy(new PlayerLoginEvent(player, "", player.getAddress().getAddress()));
given(validationService.isUnrestricted(name)).willReturn(false); given(validationService.isUnrestricted(name)).willReturn(false);
given(onJoinVerifier.refusePlayerForFullServer(event)).willReturn(false); given(onJoinVerifier.refusePlayerForFullServer(event)).willReturn(false);
FailedVerificationException exception = new FailedVerificationException( FailedVerificationException exception = new FailedVerificationException(