mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-27 09:00:28 +08:00
Add ServerVariableHolder & PassEncryptUtil Tests
This commit is contained in:
parent
24fa0c8599
commit
0b7b270f78
@ -17,18 +17,18 @@ import java.security.spec.InvalidKeySpecException;
|
|||||||
*/
|
*/
|
||||||
public class PassEncryptUtil {
|
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.
|
// These constants may be changed without breaking existing hashes.
|
||||||
public static final int SALT_BYTE_SIZE = 24;
|
private static final int SALT_BYTE_SIZE = 24;
|
||||||
public static final int HASH_BYTE_SIZE = 18;
|
private static final int HASH_BYTE_SIZE = 18;
|
||||||
public static final int PBKDF2_ITERATIONS = 64000;
|
private static final int PBKDF2_ITERATIONS = 64000;
|
||||||
// These constants define the encoding and may not be changed.
|
// These constants define the encoding and may not be changed.
|
||||||
public static final int HASH_SECTIONS = 5;
|
private static final int HASH_SECTIONS = 5;
|
||||||
public static final int HASH_ALGORITHM_INDEX = 0;
|
private static final int HASH_ALGORITHM_INDEX = 0;
|
||||||
public static final int ITERATION_INDEX = 1;
|
private static final int ITERATION_INDEX = 1;
|
||||||
public static final int HASH_SIZE_INDEX = 2;
|
private static final int HASH_SIZE_INDEX = 2;
|
||||||
public static final int SALT_INDEX = 3;
|
private static final int SALT_INDEX = 3;
|
||||||
public static final int PBKDF2_INDEX = 4;
|
private static final int PBKDF2_INDEX = 4;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor used to hide the public constructor
|
* Constructor used to hide the public constructor
|
||||||
@ -41,7 +41,7 @@ public class PassEncryptUtil {
|
|||||||
return createHash(password.toCharArray());
|
return createHash(password.toCharArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String createHash(char[] password) throws CannotPerformOperationException {
|
private static String createHash(char[] password) throws CannotPerformOperationException {
|
||||||
// Generate a random salt
|
// Generate a random salt
|
||||||
SecureRandom random = new SecureRandom();
|
SecureRandom random = new SecureRandom();
|
||||||
byte[] salt = new byte[SALT_BYTE_SIZE];
|
byte[] salt = new byte[SALT_BYTE_SIZE];
|
||||||
@ -63,7 +63,7 @@ public class PassEncryptUtil {
|
|||||||
return verifyPassword(password.toCharArray(), correctHash);
|
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
|
// Decode the hash into its parameters
|
||||||
String[] params = correctHash.split(":");
|
String[] params = correctHash.split(":");
|
||||||
if (params.length != HASH_SECTIONS) {
|
if (params.length != HASH_SECTIONS) {
|
||||||
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -18,6 +18,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,47 +28,40 @@ import static org.junit.Assert.assertTrue;
|
|||||||
@PrepareForTest(JavaPlugin.class)
|
@PrepareForTest(JavaPlugin.class)
|
||||||
public class SettingsTest {
|
public class SettingsTest {
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public SettingsTest() {
|
public SettingsTest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
TestInit.init();
|
TestInit.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsTrue() {
|
public void testIsTrue() {
|
||||||
assertTrue("Webserver supposed to be enabled by default", Settings.WEBSERVER_ENABLED.isTrue());
|
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
|
@Test
|
||||||
public void testToString() {
|
public void testToString() {
|
||||||
assertEquals("sqlite", Settings.DB_TYPE.toString());
|
assertEquals("sqlite", Settings.DB_TYPE.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetNumber() {
|
public void testGetNumber() {
|
||||||
assertEquals(8804, Settings.WEBSERVER_PORT.getNumber());
|
assertEquals(8804, Settings.WEBSERVER_PORT.getNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetStringList() {
|
public void testGetStringList() {
|
||||||
List<String> exp = new ArrayList<>();
|
List<String> exp = new ArrayList<>();
|
||||||
@ -76,9 +70,6 @@ public class SettingsTest {
|
|||||||
assertEquals(exp, result);
|
assertEquals(exp, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetPath() {
|
public void testGetPath() {
|
||||||
assertEquals("Settings.WebServer.Enabled", Settings.WEBSERVER_ENABLED.getPath());
|
assertEquals("Settings.WebServer.Enabled", Settings.WEBSERVER_ENABLED.getPath());
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,11 +15,16 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
public class RandomData {
|
public class RandomData {
|
||||||
|
|
||||||
private static final Random r = new Random();
|
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() {
|
public static List<UserData> randomUserData() {
|
||||||
List<UserData> test = new ArrayList<>();
|
List<UserData> test = new ArrayList<>();
|
||||||
for (int i = 0; i < 20; i++) {
|
for (int i = 0; i < 20; i++) {
|
||||||
|
Loading…
Reference in New Issue
Block a user