Add ServerVariableHolder & PassEncryptUtil Tests

This commit is contained in:
Fuzzlemann 2017-08-15 21:26:36 +02:00
parent 24fa0c8599
commit 0b7b270f78
5 changed files with 119 additions and 33 deletions

View File

@ -17,18 +17,18 @@ import java.security.spec.InvalidKeySpecException;
*/
public class PassEncryptUtil {
public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";
private static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";
// These constants may be changed without breaking existing hashes.
public static final int SALT_BYTE_SIZE = 24;
public static final int HASH_BYTE_SIZE = 18;
public static final int PBKDF2_ITERATIONS = 64000;
private static final int SALT_BYTE_SIZE = 24;
private static final int HASH_BYTE_SIZE = 18;
private static final int PBKDF2_ITERATIONS = 64000;
// These constants define the encoding and may not be changed.
public static final int HASH_SECTIONS = 5;
public static final int HASH_ALGORITHM_INDEX = 0;
public static final int ITERATION_INDEX = 1;
public static final int HASH_SIZE_INDEX = 2;
public static final int SALT_INDEX = 3;
public static final int PBKDF2_INDEX = 4;
private static final int HASH_SECTIONS = 5;
private static final int HASH_ALGORITHM_INDEX = 0;
private static final int ITERATION_INDEX = 1;
private static final int HASH_SIZE_INDEX = 2;
private static final int SALT_INDEX = 3;
private static final int PBKDF2_INDEX = 4;
/**
* Constructor used to hide the public constructor
@ -41,7 +41,7 @@ public class PassEncryptUtil {
return createHash(password.toCharArray());
}
public static String createHash(char[] password) throws CannotPerformOperationException {
private static String createHash(char[] password) throws CannotPerformOperationException {
// Generate a random salt
SecureRandom random = new SecureRandom();
byte[] salt = new byte[SALT_BYTE_SIZE];
@ -63,7 +63,7 @@ public class PassEncryptUtil {
return verifyPassword(password.toCharArray(), correctHash);
}
public static boolean verifyPassword(char[] password, String correctHash) throws CannotPerformOperationException, InvalidHashException {
private static boolean verifyPassword(char[] password, String correctHash) throws CannotPerformOperationException, InvalidHashException {
// Decode the hash into its parameters
String[] params = correctHash.split(":");
if (params.length != HASH_SECTIONS) {

View File

@ -0,0 +1,31 @@
package test.java.main.java.com.djrapitops.plan;
import main.java.com.djrapitops.plan.Plan;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.TestInit;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
/**
* @author Fuzzlemann
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class ServerVariableHolderTest {
@Test
public void testServerVariable() throws Exception {
TestInit.init();
boolean usingPaper = Plan.getInstance().getVariable().isUsingPaper();
assertFalse(usingPaper);
String ip = Plan.getInstance().getVariable().getIp();
assertEquals(ip, "0.0.0.0");
}
}

View File

@ -18,6 +18,7 @@ import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
@ -27,47 +28,40 @@ import static org.junit.Assert.assertTrue;
@PrepareForTest(JavaPlugin.class)
public class SettingsTest {
/**
*
*/
public SettingsTest() {
}
/**
*
*/
@Before
public void setUp() throws Exception {
TestInit.init();
}
/**
*
*/
@Test
public void testIsTrue() {
assertTrue("Webserver supposed to be enabled by default", Settings.WEBSERVER_ENABLED.isTrue());
}
/**
*
*/
@Test
public void testIsTrue2() {
Settings gatherCommands = Settings.GATHERCOMMANDS;
gatherCommands.setValue(false);
assertFalse(gatherCommands.isTrue());
gatherCommands.setValue(true);
assertTrue(gatherCommands.isTrue());
}
@Test
public void testToString() {
assertEquals("sqlite", Settings.DB_TYPE.toString());
}
/**
*
*/
@Test
public void testGetNumber() {
assertEquals(8804, Settings.WEBSERVER_PORT.getNumber());
}
/**
*
*/
@Test
public void testGetStringList() {
List<String> exp = new ArrayList<>();
@ -76,9 +70,6 @@ public class SettingsTest {
assertEquals(exp, result);
}
/**
*
*/
@Test
public void testGetPath() {
assertEquals("Settings.WebServer.Enabled", Settings.WEBSERVER_ENABLED.getPath());

View File

@ -0,0 +1,59 @@
package test.java.main.java.com.djrapitops.plan.utilities;
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
import org.junit.Before;
import org.junit.Test;
import test.java.utils.RandomData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Fuzzlemann
*/
public class PassEncryptTest {
private final List<String> passwords = new ArrayList<>();
private final Map<String, String> passwordMap = new HashMap<>();
@Before
public void setUp() {
IntStream.range(0, 50).forEach(i -> passwords.add(RandomData.randomString(RandomData.randomInt(1, 100))));
}
@Before
@Test
public void testHashing() throws Exception {
for (String password : passwords) {
passwordMap.put(password, PassEncryptUtil.createHash(password));
}
}
@Test
public void testConsistency() throws Exception {
for (Map.Entry<String, String> entry : passwordMap.entrySet()) {
String password = entry.getKey();
String expHash = entry.getValue();
String secondHashRun = PassEncryptUtil.createHash(password);
assertEquals(expHash, secondHashRun);
}
}
@Test
public void testVerification() throws Exception {
for (Map.Entry<String, String> entry : passwordMap.entrySet()) {
String password = entry.getKey();
String hash = entry.getValue();
assertTrue(PassEncryptUtil.verifyPassword(password, hash));
}
}
}

View File

@ -15,11 +15,16 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
public class RandomData {
private static final Random r = new Random();
public static int randomInt(int rangeStart, int rangeEnd) {
return ThreadLocalRandom.current().nextInt(rangeStart, rangeEnd);
}
public static List<UserData> randomUserData() {
List<UserData> test = new ArrayList<>();
for (int i = 0; i < 20; i++) {