#1016 Update hash algorithms list, add test that Deprecated annotation is in sync between enum and hash impl. class
This commit is contained in:
@@ -11,7 +11,6 @@ import fr.xephi.authme.output.LogLevel;
|
||||
import fr.xephi.authme.process.register.RegisterSecondaryArgument;
|
||||
import fr.xephi.authme.process.register.RegistrationType;
|
||||
import fr.xephi.authme.security.HashAlgorithm;
|
||||
import fr.xephi.authme.security.crypts.EncryptionMethod;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
@@ -301,8 +300,8 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
HashAlgorithm currentHash = SecuritySettings.PASSWORD_HASH.getValue(resource);
|
||||
// Skip CUSTOM (has no class) and PLAINTEXT (is force-migrated later on in the startup process)
|
||||
if (currentHash != HashAlgorithm.CUSTOM && currentHash != HashAlgorithm.PLAINTEXT) {
|
||||
Class<? extends EncryptionMethod> clazz = currentHash.getClazz();
|
||||
if (clazz.isAnnotationPresent(Deprecated.class)) {
|
||||
Class<?> encryptionClass = currentHash.getClazz();
|
||||
if (encryptionClass.isAnnotationPresent(Deprecated.class)) {
|
||||
resource.setValue(SecuritySettings.PASSWORD_HASH.getPath(), HashAlgorithm.SHA256);
|
||||
Set<HashAlgorithm> legacyHashes = SecuritySettings.LEGACY_HASHES.getValue(resource);
|
||||
legacyHashes.add(currentHash);
|
||||
|
||||
@@ -4,6 +4,8 @@ import ch.jalu.injector.Injector;
|
||||
import ch.jalu.injector.InjectorBuilder;
|
||||
import fr.xephi.authme.security.crypts.EncryptionMethod;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.security.crypts.description.Recommendation;
|
||||
import fr.xephi.authme.security.crypts.description.Usage;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
@@ -12,6 +14,8 @@ import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@@ -70,4 +74,29 @@ public class HashAlgorithmIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldBeDeprecatedIfEncryptionClassIsDeprecated() throws NoSuchFieldException {
|
||||
// given
|
||||
List<String> failedEntries = new LinkedList<>();
|
||||
|
||||
// when
|
||||
for (HashAlgorithm hashAlgorithm : HashAlgorithm.values()) {
|
||||
if (hashAlgorithm != HashAlgorithm.CUSTOM) {
|
||||
boolean isEnumDeprecated = HashAlgorithm.class.getDeclaredField(hashAlgorithm.name())
|
||||
.isAnnotationPresent(Deprecated.class);
|
||||
boolean isDeprecatedClass = hashAlgorithm.getClazz().isAnnotationPresent(Deprecated.class);
|
||||
Recommendation recommendation = hashAlgorithm.getClazz().getAnnotation(Recommendation.class);
|
||||
boolean hasDeprecatedUsage = recommendation != null && recommendation.value() == Usage.DEPRECATED;
|
||||
if (isEnumDeprecated != isDeprecatedClass || isEnumDeprecated != hasDeprecatedUsage) {
|
||||
failedEntries.add(hashAlgorithm + ": enum @Deprecated = " + isEnumDeprecated
|
||||
+ ", @Deprecated class = " + isDeprecatedClass + ", usage Deprecated = " + hasDeprecatedUsage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// then
|
||||
if (!failedEntries.isEmpty()) {
|
||||
fail("Found inconsistencies:\n" + String.join("\n", failedEntries));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user