#2661 Remove mocks of InetAddress (which is a sealed class in JDK 19)
- Remove mocks of InetAddress in favor of using real InetAddress instances. This fixes test issues under JDK 19, where InetAddress has been changed to a sealed class
This commit is contained in:
parent
a14df80a87
commit
779e62674e
@ -1,6 +1,7 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import ch.jalu.configme.properties.Property;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
@ -23,6 +24,7 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@ -156,7 +158,7 @@ public class ValidationService implements Reloadable {
|
||||
}
|
||||
|
||||
String ip = PlayerUtils.getPlayerIp(player);
|
||||
String domain = player.getAddress().getHostName();
|
||||
String domain = getHostName(player.getAddress());
|
||||
for (String restriction : restrictions) {
|
||||
if (restriction.startsWith("regex:")) {
|
||||
restriction = restriction.replace("regex:", "");
|
||||
@ -173,6 +175,11 @@ public class ValidationService implements Reloadable {
|
||||
return false;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected String getHostName(InetSocketAddress inetSocketAddr) {
|
||||
return inetSocketAddr.getHostName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies whether the given value is allowed according to the given whitelist and blacklist settings.
|
||||
* Whitelist has precedence over blacklist: if a whitelist is set, the value is rejected if not present
|
||||
|
||||
@ -11,13 +11,13 @@ import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* AuthMe test utilities.
|
||||
@ -98,11 +98,14 @@ public final class TestHelper {
|
||||
* @param player the player mock
|
||||
* @param ip the ip address it should return
|
||||
*/
|
||||
public static void mockPlayerIp(Player player, String ip) {
|
||||
InetAddress inetAddress = mock(InetAddress.class);
|
||||
given(inetAddress.getHostAddress()).willReturn(ip);
|
||||
InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, 8093);
|
||||
given(player.getAddress()).willReturn(inetSocketAddress);
|
||||
public static void mockIpAddressToPlayer(Player player, String ip) {
|
||||
try {
|
||||
InetAddress inetAddress = InetAddress.getByName(ip);
|
||||
InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, 8093);
|
||||
given(player.getAddress()).willReturn(inetSocketAddress);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new IllegalStateException("Invalid IP address: " + ip, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -100,7 +100,7 @@ public class GetIpCommandTest {
|
||||
private static Player mockPlayer(String name, String ip) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
@ -147,7 +147,7 @@ public class TempbanManagerTest {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
String ip = "123.45.67.89";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
String banReason = "IP ban too many logins";
|
||||
given(messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
Settings settings = mockSettings(2, 100, "");
|
||||
@ -175,7 +175,7 @@ public class TempbanManagerTest {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn("Bob");
|
||||
String ip = "143.45.77.89";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
String banCommand = "banip %ip% 15d IP ban too many logins";
|
||||
Settings settings = mockSettings(2, 100, banCommand);
|
||||
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
|
||||
@ -193,7 +193,7 @@ public class TempbanManagerTest {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
String ip = "22.44.66.88";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
String banReason = "kick msg";
|
||||
given(messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
Settings settings = mockSettings(10, 60, "");
|
||||
|
||||
@ -125,7 +125,7 @@ public class AsynchronousLoginTest {
|
||||
String name = "oscar";
|
||||
String ip = "1.1.1.245";
|
||||
Player player = mockPlayer(name);
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
PlayerAuth auth = PlayerAuth.builder().name(name).build();
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
@ -148,7 +148,7 @@ public class AsynchronousLoginTest {
|
||||
String name = "oscar";
|
||||
String ip = "1.1.1.245";
|
||||
Player player = mockPlayer(name);
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
PlayerAuth auth = PlayerAuth.builder().name(name).build();
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
@ -243,26 +243,26 @@ public class AsynchronousLoginTest {
|
||||
private void mockOnlinePlayersInBukkitService() {
|
||||
// 1.1.1.1: albania (online), brazil (offline)
|
||||
Player playerA = mockPlayer("albania");
|
||||
TestHelper.mockPlayerIp(playerA, "1.1.1.1");
|
||||
TestHelper.mockIpAddressToPlayer(playerA, "1.1.1.1");
|
||||
given(dataSource.isLogged(playerA.getName())).willReturn(true);
|
||||
Player playerB = mockPlayer("brazil");
|
||||
TestHelper.mockPlayerIp(playerB, "1.1.1.1");
|
||||
TestHelper.mockIpAddressToPlayer(playerB, "1.1.1.1");
|
||||
given(dataSource.isLogged(playerB.getName())).willReturn(false);
|
||||
|
||||
// 2.2.2.2: congo (online), denmark (offline), ecuador (online)
|
||||
Player playerC = mockPlayer("congo");
|
||||
TestHelper.mockPlayerIp(playerC, "2.2.2.2");
|
||||
TestHelper.mockIpAddressToPlayer(playerC, "2.2.2.2");
|
||||
given(dataSource.isLogged(playerC.getName())).willReturn(true);
|
||||
Player playerD = mockPlayer("denmark");
|
||||
TestHelper.mockPlayerIp(playerD, "2.2.2.2");
|
||||
TestHelper.mockIpAddressToPlayer(playerD, "2.2.2.2");
|
||||
given(dataSource.isLogged(playerD.getName())).willReturn(false);
|
||||
Player playerE = mockPlayer("ecuador");
|
||||
TestHelper.mockPlayerIp(playerE, "2.2.2.2");
|
||||
TestHelper.mockIpAddressToPlayer(playerE, "2.2.2.2");
|
||||
given(dataSource.isLogged(playerE.getName())).willReturn(true);
|
||||
|
||||
// 3.3.3.3: france (offline)
|
||||
Player playerF = mockPlayer("france");
|
||||
TestHelper.mockPlayerIp(playerF, "3.3.3.3");
|
||||
TestHelper.mockIpAddressToPlayer(playerF, "3.3.3.3");
|
||||
|
||||
List<Player> onlinePlayers = Arrays.asList(playerA, playerB, playerC, playerD, playerE, playerF);
|
||||
given(bukkitService.getOnlinePlayers()).willReturn(onlinePlayers);
|
||||
|
||||
@ -111,7 +111,7 @@ public class AsyncRegisterTest {
|
||||
// given
|
||||
String name = "edbert";
|
||||
Player player = mockPlayerWithName(name);
|
||||
TestHelper.mockPlayerIp(player, "33.44.55.66");
|
||||
TestHelper.mockIpAddressToPlayer(player, "33.44.55.66");
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
given(commonService.getProperty(RegistrationSettings.IS_ENABLED)).willReturn(true);
|
||||
given(dataSource.isAuthAvailable(name)).willReturn(false);
|
||||
@ -136,7 +136,7 @@ public class AsyncRegisterTest {
|
||||
// given
|
||||
String name = "edbert";
|
||||
Player player = mockPlayerWithName(name);
|
||||
TestHelper.mockPlayerIp(player, "33.44.55.66");
|
||||
TestHelper.mockIpAddressToPlayer(player, "33.44.55.66");
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
given(commonService.getProperty(RegistrationSettings.IS_ENABLED)).willReturn(true);
|
||||
given(commonService.getProperty(RestrictionSettings.MAX_REGISTRATION_PER_IP)).willReturn(0);
|
||||
|
||||
@ -110,7 +110,7 @@ public class EmailRegisterExecutorTest {
|
||||
given(passwordSecurity.computeHash(anyString(), anyString())).willAnswer(
|
||||
invocation -> new HashedPassword(invocation.getArgument(0)));
|
||||
Player player = mock(Player.class);
|
||||
TestHelper.mockPlayerIp(player, "123.45.67.89");
|
||||
TestHelper.mockIpAddressToPlayer(player, "123.45.67.89");
|
||||
given(player.getName()).willReturn("Veronica");
|
||||
EmailRegisterParams params = EmailRegisterParams.of(player, "test@example.com");
|
||||
|
||||
|
||||
@ -97,7 +97,7 @@ public class PasswordRegisterExecutorTest {
|
||||
given(passwordSecurity.computeHash(anyString(), anyString())).willAnswer(
|
||||
invocation -> new HashedPassword(invocation.getArgument(0)));
|
||||
Player player = mockPlayerWithName("S1m0N");
|
||||
TestHelper.mockPlayerIp(player, "123.45.67.89");
|
||||
TestHelper.mockIpAddressToPlayer(player, "123.45.67.89");
|
||||
PasswordRegisterParams params = PasswordRegisterParams.of(player, "pass", "mail@example.org");
|
||||
|
||||
// when
|
||||
|
||||
@ -25,7 +25,7 @@ public class PlayerAuthBuilderHelperTest {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn("Noah");
|
||||
String ip = "192.168.34.47";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
HashedPassword hashedPassword = new HashedPassword("myHash0001");
|
||||
String email = "test@example.org";
|
||||
|
||||
|
||||
@ -78,15 +78,15 @@ public class PasswordRecoveryServiceTest {
|
||||
public void shouldKeepTrackOfSuccessfulRecoversByIp() {
|
||||
// given
|
||||
Player bobby = mock(Player.class);
|
||||
TestHelper.mockPlayerIp(bobby, "192.168.8.8");
|
||||
TestHelper.mockIpAddressToPlayer(bobby, "192.168.8.8");
|
||||
given(bobby.getName()).willReturn("bobby");
|
||||
|
||||
Player bobby2 = mock(Player.class);
|
||||
TestHelper.mockPlayerIp(bobby2, "127.0.0.1");
|
||||
TestHelper.mockIpAddressToPlayer(bobby2, "127.0.0.1");
|
||||
given(bobby2.getName()).willReturn("bobby");
|
||||
|
||||
Player other = mock(Player.class);
|
||||
TestHelper.mockPlayerIp(other, "192.168.8.8");
|
||||
TestHelper.mockIpAddressToPlayer(other, "192.168.8.8");
|
||||
given(other.getName()).willReturn("other");
|
||||
|
||||
// when
|
||||
@ -102,12 +102,12 @@ public class PasswordRecoveryServiceTest {
|
||||
public void shouldRemovePlayerFromSuccessfulRecovers() {
|
||||
// given
|
||||
Player bobby = mock(Player.class);
|
||||
TestHelper.mockPlayerIp(bobby, "192.168.8.8");
|
||||
TestHelper.mockIpAddressToPlayer(bobby, "192.168.8.8");
|
||||
given(bobby.getName()).willReturn("bobby");
|
||||
recoveryService.addSuccessfulRecovery(bobby);
|
||||
|
||||
Player other = mock(Player.class);
|
||||
TestHelper.mockPlayerIp(other, "8.8.8.8");
|
||||
TestHelper.mockIpAddressToPlayer(other, "8.8.8.8");
|
||||
given(other.getName()).willReturn("other");
|
||||
recoveryService.addSuccessfulRecovery(other);
|
||||
|
||||
|
||||
@ -240,7 +240,7 @@ public class SessionServiceTest {
|
||||
private static Player mockPlayerWithNameAndIp(String name, String ip) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,7 +21,9 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Collections;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -30,7 +32,9 @@ import static java.util.Arrays.asList;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.willReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
@ -358,18 +362,23 @@ public class ValidationServiceTest {
|
||||
Player gabriel2 = mockPlayer("Gabriel", "93.23.33.34");
|
||||
Player emanuel = mockPlayer("emanuel", "94.65.24.10");
|
||||
Player emanuel2 = mockPlayer("emanuel", "94.65.60.10");
|
||||
Player imyourisp = mockPlayer("imyourisp", "bazinga.yourisp.net");
|
||||
Player imYourIsp = mockPlayer("imyourisp", "65.65.65.65");
|
||||
Player notRestricted = mockPlayer("notRestricted", "0.0.0.0");
|
||||
|
||||
ValidationService validationServiceSpy = Mockito.spy(validationService);
|
||||
willReturn("bogus.tld").given(validationServiceSpy).getHostName(any(InetSocketAddress.class));
|
||||
InetSocketAddress imYourIspSocketAddr = imYourIsp.getAddress();
|
||||
willReturn("bazinga.yourisp.net").given(validationServiceSpy).getHostName(imYourIspSocketAddr);
|
||||
|
||||
// when
|
||||
boolean isBobbyAdmitted = validationService.fulfillsNameRestrictions(bobby);
|
||||
boolean isTamaraAdmitted = validationService.fulfillsNameRestrictions(tamara);
|
||||
boolean isGabrielAdmitted = validationService.fulfillsNameRestrictions(gabriel);
|
||||
boolean isGabriel2Admitted = validationService.fulfillsNameRestrictions(gabriel2);
|
||||
boolean isEmanuelAdmitted = validationService.fulfillsNameRestrictions(emanuel);
|
||||
boolean isEmanuel2Admitted = validationService.fulfillsNameRestrictions(emanuel2);
|
||||
boolean isImyourispAdmitted = validationService.fulfillsNameRestrictions(imyourisp);
|
||||
boolean isNotRestrictedAdmitted = validationService.fulfillsNameRestrictions(notRestricted);
|
||||
boolean isBobbyAdmitted = validationServiceSpy.fulfillsNameRestrictions(bobby);
|
||||
boolean isTamaraAdmitted = validationServiceSpy.fulfillsNameRestrictions(tamara);
|
||||
boolean isGabrielAdmitted = validationServiceSpy.fulfillsNameRestrictions(gabriel);
|
||||
boolean isGabriel2Admitted = validationServiceSpy.fulfillsNameRestrictions(gabriel2);
|
||||
boolean isEmanuelAdmitted = validationServiceSpy.fulfillsNameRestrictions(emanuel);
|
||||
boolean isEmanuel2Admitted = validationServiceSpy.fulfillsNameRestrictions(emanuel2);
|
||||
boolean isImYourIspAdmitted = validationServiceSpy.fulfillsNameRestrictions(imYourIsp);
|
||||
boolean isNotRestrictedAdmitted = validationServiceSpy.fulfillsNameRestrictions(notRestricted);
|
||||
|
||||
// then
|
||||
assertThat(isBobbyAdmitted, equalTo(true));
|
||||
@ -378,7 +387,7 @@ public class ValidationServiceTest {
|
||||
assertThat(isGabriel2Admitted, equalTo(false));
|
||||
assertThat(isEmanuelAdmitted, equalTo(true));
|
||||
assertThat(isEmanuel2Admitted, equalTo(false));
|
||||
assertThat(isImyourispAdmitted, equalTo(true));
|
||||
assertThat(isImYourIspAdmitted, equalTo(true));
|
||||
assertThat(isNotRestrictedAdmitted, equalTo(true));
|
||||
}
|
||||
|
||||
@ -402,8 +411,7 @@ public class ValidationServiceTest {
|
||||
private static Player mockPlayer(String name, String ip) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
given(player.getAddress().getHostName()).willReturn("--");
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
return player;
|
||||
}
|
||||
|
||||
|
||||
@ -92,7 +92,7 @@ public class WelcomeMessageConfigurationTest {
|
||||
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn("Bobby");
|
||||
TestHelper.mockPlayerIp(player, "123.45.66.77");
|
||||
TestHelper.mockIpAddressToPlayer(player, "123.45.66.77");
|
||||
given(geoIpService.getCountryName("123.45.66.77")).willReturn("Syldavia");
|
||||
given(service.getProperty(PluginSettings.SERVER_NAME)).willReturn("CrazyServer");
|
||||
|
||||
|
||||
@ -313,7 +313,7 @@ public class CommandManagerTest {
|
||||
given(player.getName()).willReturn("Bobby");
|
||||
given(player.getDisplayName()).willReturn("bob");
|
||||
String ip = "127.0.0.3";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
given(geoIpService.getCountryName(ip)).willReturn("Syldavia");
|
||||
return player;
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ public class PlayerUtilsTest {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
String ip = "124.86.248.62";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
TestHelper.mockIpAddressToPlayer(player, ip);
|
||||
|
||||
// when
|
||||
String result = PlayerUtils.getPlayerIp(player);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user