Merge branch 'master' of https://github.com/AuthMe-Team/AuthMeReloaded into 432-dependency-injection

This commit is contained in:
ljacqu
2016-04-30 12:17:33 +02:00
11 changed files with 330 additions and 75 deletions
@@ -6,6 +6,9 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -84,9 +87,42 @@ public final class TestHelper {
runnable.run();
}
/**
* Assign the necessary fields on ConsoleLogger with mocks.
*
* @return The logger mock used
*/
public static Logger setupLogger() {
Logger logger = Mockito.mock(Logger.class);
ConsoleLogger.setLogger(logger);
return logger;
}
/**
* Check that a class only has a hidden, zero-argument constructor, preventing the
* instantiation of such classes (utility classes). Invokes the hidden constructor
* as to register the code coverage.
*
* @param clazz The class to validate
*/
public static void validateHasOnlyPrivateEmptyConstructor(Class<?> clazz) {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
if (constructors.length > 1) {
throw new IllegalStateException("Class " + clazz.getSimpleName() + " has more than one constructor");
} else if (constructors[0].getParameterTypes().length != 0) {
throw new IllegalStateException("Constructor of " + clazz + " does not have empty parameter list");
} else if (!Modifier.isPrivate(constructors[0].getModifiers())) {
throw new IllegalStateException("Constructor of " + clazz + " is not private");
}
// Ugly hack to get coverage on the private constructors
// http://stackoverflow.com/questions/14077842/how-to-test-a-private-constructor-in-java-application
try {
constructors[0].setAccessible(true);
constructors[0].newInstance();
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new UnsupportedOperationException(e);
}
}
}
@@ -0,0 +1,94 @@
package fr.xephi.authme.events;
import org.apache.commons.lang.reflect.MethodUtils;
import org.bukkit.event.Event;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
/**
* Checks the consistency of the AuthMe event classes.
*/
public class EventsConsistencyTest {
private static final String SRC_FOLDER = "src/main/java/";
private static final String EVENTS_FOLDER = SRC_FOLDER + "/fr/xephi/authme/events/";
private static List<Class<? extends Event>> classes;
@BeforeClass
public static void scanEventClasses() {
File eventsFolder = new File(EVENTS_FOLDER);
File[] filesInFolder = eventsFolder.listFiles();
if (filesInFolder == null || filesInFolder.length == 0) {
throw new IllegalStateException("Could not read folder '" + EVENTS_FOLDER + "'. Is it correct?");
}
classes = new ArrayList<>();
for (File file : filesInFolder) {
Class<? extends Event> clazz = getEventClassFromFile(file);
if (clazz != null) {
classes.add(clazz);
}
}
if (classes.isEmpty()) {
throw new IllegalStateException("Did not find any AuthMe event classes. Is the folder correct?");
}
}
@Test
public void shouldExtendFromCustomEvent() {
for (Class<?> clazz : classes) {
assertThat("Class " + clazz.getSimpleName() + " is subtype of CustomEvent",
CustomEvent.class.isAssignableFrom(clazz), equalTo(true));
}
}
/**
* Bukkit requires a static getHandlerList() method on all event classes, see {@link Event}.
* This test checks that such a method is present, and that it is <i>absent</i> if the class
* is not instantiable (abstract class).
*/
@Test
public void shouldHaveStaticEventHandlerMethod() {
for (Class<?> clazz : classes) {
Method handlerListMethod = MethodUtils.getAccessibleMethod(clazz, "getHandlerList", new Class<?>[]{});
if (canBeInstantiated(clazz)) {
assertThat("Class " + clazz.getSimpleName() + " has static method getHandlerList()",
handlerListMethod != null && Modifier.isStatic(handlerListMethod.getModifiers()), equalTo(true));
} else {
assertThat("Non-instantiable class " + clazz.getSimpleName() + " does not have static getHandlerList()",
handlerListMethod, nullValue());
}
}
}
private static boolean canBeInstantiated(Class<?> clazz) {
return !clazz.isInterface() && !clazz.isEnum() && !Modifier.isAbstract(clazz.getModifiers());
}
private static Class<? extends Event> getEventClassFromFile(File file) {
String fileName = file.getPath();
String className = fileName
.substring(SRC_FOLDER.length(), fileName.length() - ".java".length())
.replace(File.separator, ".");
try {
Class<?> clazz = EventsConsistencyTest.class.getClassLoader().loadClass(className);
if (Event.class.isAssignableFrom(clazz)) {
return (Class<? extends Event>) clazz;
}
return null;
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Could not load class '" + className + "'", e);
}
}
}
@@ -1,22 +1,20 @@
package fr.xephi.authme.settings.properties;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.domain.SettingsClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@@ -86,24 +84,9 @@ public class SettingsClassConsistencyTest {
}
@Test
public void shouldHaveHiddenDefaultConstructorOnly() {
public void shouldHaveHiddenEmptyConstructorOnly() {
for (Class<?> clazz : classes) {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
assertThat(clazz + " should only have one constructor",
constructors, arrayWithSize(1));
assertThat("Constructor of " + clazz + " is private",
Modifier.isPrivate(constructors[0].getModifiers()), equalTo(true));
// Ugly hack to get coverage on the private constructors
// http://stackoverflow.com/questions/14077842/how-to-test-a-private-constructor-in-java-application
try {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
constructor.newInstance();
} catch (NoSuchMethodException | InstantiationException
| IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
TestHelper.validateHasOnlyPrivateEmptyConstructor(clazz);
}
}
@@ -0,0 +1,134 @@
package fr.xephi.authme.util;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.security.crypts.SHA256;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import java.util.Arrays;
import static fr.xephi.authme.AuthMeMatchers.equalToHash;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Test for {@link MigrationService}.
*/
@RunWith(MockitoJUnitRunner.class)
public class MigrationServiceTest {
@Mock
private NewSetting settings;
@Mock
private DataSource dataSource;
@Mock
private SHA256 sha256;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldMigratePlaintextHashes() {
// given
PlayerAuth auth1 = authWithNickAndHash("bobby", "test");
PlayerAuth auth2 = authWithNickAndHash("user", "myPassword");
PlayerAuth auth3 = authWithNickAndHash("Tester12", "$tester12_pw");
given(dataSource.getAllAuths()).willReturn(Arrays.asList(auth1, auth2, auth3));
setSha256MockToUppercase(sha256);
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.PLAINTEXT);
// when
MigrationService.changePlainTextToSha256(settings, dataSource, sha256);
// then
verify(sha256, times(3)).computeHash(anyString(), anyString());
verify(dataSource).getAllAuths(); // need to verify this because we use verifyNoMoreInteractions() after
verify(dataSource).updatePassword(auth1);
assertThat(auth1.getPassword(), equalToHash("TEST"));
verify(dataSource).updatePassword(auth2);
assertThat(auth2.getPassword(), equalToHash("MYPASSWORD"));
verify(dataSource).updatePassword(auth3);
assertThat(auth3.getPassword(), equalToHash("$TESTER12_PW"));
verifyNoMoreInteractions(dataSource);
verify(settings).setProperty(SecuritySettings.PASSWORD_HASH, HashAlgorithm.SHA256);
}
@Test
public void shouldNotMigrateShaHashes() {
// given
PlayerAuth auth1 = authWithNickAndHash("testUser", "abc1234");
PlayerAuth auth2 = authWithNickAndHash("minecraft", "$SHA$f28930ae09823eba4cd98a3");
given(dataSource.getAllAuths()).willReturn(Arrays.asList(auth1, auth2));
setSha256MockToUppercase(sha256);
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.PLAINTEXT);
// when
MigrationService.changePlainTextToSha256(settings, dataSource, sha256);
// then
verify(sha256).computeHash(eq("abc1234"), argThat(equalToIgnoringCase("testUser")));
verifyNoMoreInteractions(sha256);
verify(dataSource).getAllAuths(); // need to verify this because we use verifyNoMoreInteractions() after
verify(dataSource).updatePassword(auth1);
assertThat(auth1.getPassword(), equalToHash("ABC1234"));
verifyNoMoreInteractions(dataSource);
verify(settings).setProperty(SecuritySettings.PASSWORD_HASH, HashAlgorithm.SHA256);
}
@Test
public void shouldNotMigrateForHashOtherThanPlaintext() {
// given
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.BCRYPT);
// when
MigrationService.changePlainTextToSha256(settings, dataSource, sha256);
// then
verify(settings).getProperty(SecuritySettings.PASSWORD_HASH);
verifyNoMoreInteractions(settings, dataSource, sha256);
}
@Test
public void shouldHaveHiddenEmptyConstructorOnly() {
TestHelper.validateHasOnlyPrivateEmptyConstructor(MigrationService.class);
}
private static PlayerAuth authWithNickAndHash(String nick, String hash) {
return PlayerAuth.builder()
.name(nick)
.password(hash, null)
.build();
}
private static void setSha256MockToUppercase(SHA256 sha256) {
given(sha256.computeHash(anyString(), anyString())).willAnswer(new Answer<HashedPassword>() {
@Override
public HashedPassword answer(InvocationOnMock invocation) {
String plainPassword = (String) invocation.getArguments()[0];
return new HashedPassword(plainPassword.toUpperCase(), null);
}
});
}
}