Fix null address

This commit is contained in:
Gabriele C 2017-09-06 00:56:27 +02:00
parent 8ae8ccf5cf
commit 08182e41f6
4 changed files with 20 additions and 14 deletions

View File

@ -169,14 +169,15 @@ public class OnJoinVerifier implements Reloadable {
* Checks that the player's country is admitted.
*
* @param player the player
* @param address the player address
* @param isAuthAvailable whether or not the user is registered
* @throws FailedVerificationException if the verification fails
*/
public void checkPlayerCountry(Player player, boolean isAuthAvailable) throws FailedVerificationException {
public void checkPlayerCountry(Player player, String address, boolean isAuthAvailable) throws FailedVerificationException {
if ((!isAuthAvailable || settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED))
&& settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)
&& !permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_COUNTRY_CHECK)
&& !validationService.isCountryAdmitted(player.getAddress().getAddress().getHostAddress())) {
&& !validationService.isCountryAdmitted(address)) {
throw new FailedVerificationException(MessageKey.COUNTRY_BANNED_ERROR);
}
}

View File

@ -222,7 +222,7 @@ public class PlayerListener implements Listener {
onJoinVerifier.checkKickNonRegistered(isAuthAvailable);
onJoinVerifier.checkAntibot(player, isAuthAvailable);
onJoinVerifier.checkNameCasing(player, auth);
onJoinVerifier.checkPlayerCountry(player, isAuthAvailable);
onJoinVerifier.checkPlayerCountry(player, event.getAddress().getHostAddress(), isAuthAvailable);
} catch (FailedVerificationException e) {
event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs()));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);

View File

@ -440,17 +440,17 @@ public class OnJoinVerifierTest {
@Test
public void shouldNotCheckCountry() throws FailedVerificationException {
Player player = newPlayerWithName("david");
TestHelper.mockPlayerIp(player, "127.0.0.1");
String ip = "127.0.0.1";
// protection setting disabled
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(false);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true);
onJoinVerifier.checkPlayerCountry(player, false);
onJoinVerifier.checkPlayerCountry(player, ip, false);
verifyZeroInteractions(validationService);
// protection for registered players disabled
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(false);
onJoinVerifier.checkPlayerCountry(player, true);
onJoinVerifier.checkPlayerCountry(player, ip, true);
verifyZeroInteractions(validationService);
}
@ -459,12 +459,11 @@ public class OnJoinVerifierTest {
// given
String ip = "192.168.0.1";
Player player = newPlayerWithName("lucas");
TestHelper.mockPlayerIp(player, ip);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true);
given(validationService.isCountryAdmitted(ip)).willReturn(true);
// when
onJoinVerifier.checkPlayerCountry(player, false);
onJoinVerifier.checkPlayerCountry(player, ip, false);
// then
verify(validationService).isCountryAdmitted(ip);
@ -475,13 +474,12 @@ public class OnJoinVerifierTest {
// given
String ip = "192.168.10.24";
Player player = newPlayerWithName("gabriel");
TestHelper.mockPlayerIp(player, ip);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION_REGISTERED)).willReturn(true);
given(validationService.isCountryAdmitted(ip)).willReturn(true);
// when
onJoinVerifier.checkPlayerCountry(player, true);
onJoinVerifier.checkPlayerCountry(player, ip, true);
// then
verify(validationService).isCountryAdmitted(ip);
@ -492,7 +490,6 @@ public class OnJoinVerifierTest {
// given
String ip = "192.168.40.0";
Player player = newPlayerWithName("bob");
TestHelper.mockPlayerIp(player, ip);
given(settings.getProperty(ProtectionSettings.ENABLE_PROTECTION)).willReturn(true);
given(validationService.isCountryAdmitted(ip)).willReturn(false);
@ -500,7 +497,7 @@ public class OnJoinVerifierTest {
expectValidationExceptionWith(MessageKey.COUNTRY_BANNED_ERROR);
// when
onJoinVerifier.checkPlayerCountry(player, false);
onJoinVerifier.checkPlayerCountry(player, ip, false);
}
private static Player newPlayerWithName(String name) {

View File

@ -47,6 +47,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
@ -558,8 +559,9 @@ public class PlayerListenerTest {
// given
String name = "someone";
Player player = mockPlayerWithName(name);
String ip = "12.34.56.78";
PlayerLoginEvent event = spy(new PlayerLoginEvent(player, "", null));
PlayerLoginEvent event = spy(new PlayerLoginEvent(player, "", mockAddrWithIp(ip)));
given(validationService.isUnrestricted(name)).willReturn(false);
given(onJoinVerifier.refusePlayerForFullServer(event)).willReturn(false);
PlayerAuth auth = PlayerAuth.builder().name(name).build();
@ -576,7 +578,7 @@ public class PlayerListenerTest {
verify(onJoinVerifier).checkAntibot(player, true);
verify(onJoinVerifier).checkKickNonRegistered(true);
verify(onJoinVerifier).checkNameCasing(player, auth);
verify(onJoinVerifier).checkPlayerCountry(player, true);
verify(onJoinVerifier).checkPlayerCountry(player, "", true);
verify(teleportationService).teleportOnJoin(player);
verifyNoModifyingCalls(event);
}
@ -883,4 +885,10 @@ public class PlayerListenerTest {
verify(event, atLeast(0)).getAddress();
verifyNoMoreInteractions(event);
}
private static InetAddress mockAddrWithIp(String ip) {
InetAddress addr = mock(InetAddress.class);
given(addr.getHostAddress()).willReturn(ip);
return addr;
}
}