Browse Source
Merge remote-tracking branch 'origin/feature-credential' into feature-credential
Merge remote-tracking branch 'origin/feature-credential' into feature-credential
# Conflicts: # src/main/java/Vault.javafeature-credential
fdai6352
3 years ago
18 changed files with 138364 additions and 26 deletions
-
30README.md
-
27pom.xml
-
28src/main/java/HttpApi.java
-
119src/main/java/PasswordGenerator.java
-
63src/main/java/PasswordManager.java
-
166src/main/java/PasswordValidator.java
-
37src/main/java/ResourceApi.java
-
50src/main/java/Storage.java
-
4src/main/java/StorageInterface.java
-
2src/main/java/Vault.java
-
137465src/main/resources/german_wordlist.txt
-
19src/test/java/HttpApiTest.java
-
72src/test/java/PasswordGeneratorTest.java
-
55src/test/java/PasswordManagerTest.java
-
130src/test/java/PasswordValidatorTest.java
-
48src/test/java/ResourceApiTest.java
-
62src/test/java/StorageTest.java
-
13src/test/java/VaultTest.java
@ -1,2 +1,30 @@ |
|||
# ciip-gruppe8-password-manager |
|||
# ciip-grp8-password-manager |
|||
|
|||
## Dependencies |
|||
- productive |
|||
- java 11 |
|||
- development |
|||
- java 11 |
|||
- maven |
|||
|
|||
## Build jar file artifact |
|||
- requires development dependencies |
|||
|
|||
`mvn package` |
|||
|
|||
## Run jar file artifact |
|||
- requires productive dependencies |
|||
- requires a built jar file to run |
|||
|
|||
`java -jar target/ciip-grp8-password-manager-1.0-SNAPSHOT.jar` |
|||
|
|||
## Contributors |
|||
- binsky - Timo Triebensky |
|||
- fdai6352 - Pascal Schubert |
|||
- fdai5728 - Claudia Metzler |
|||
|
|||
## Beim Testen beachten! |
|||
- zwischen den Commits `d1652f044327f4845e11db0cce6ef6c4fb392113` und `a81194074a49eabe723e7ad599411d45545a5b1d` werden die Maven Tests auf Grund einer fehlerhaften Änderung in der pom.xml nicht ausgeführt. Die JUnit Tests sollten aber dennoch funktionieren. Maven sagt lediglich, dass alles in Ordnung ist, auch wenn keine Tests ausgeführt werden. Daher ist das Problem erst etwas später aufgefallen. |
|||
- ab dem Commit `a81194074a49eabe723e7ad599411d45545a5b1d` kann auf die oben beschriebene Weise ein Artefakt erfolgreich gebaut und ausgeführt werden. Bauen hat auch vorher schon funktioniert, aber die jar Datei konnte nicht ausgeführt werden. |
|||
- für die Tests ist eine Netzwerkverbindung, sowie funktionierendes DNS nötig, da eine Verbindung zu api.pwnedpasswords.com aufgebaut wird. |
|||
- da für ein erstes Release noch nicht genug Funktionalität implementiert ist, gab es bisher noch keinen Merge/Pull Request auf den main Branch. |
@ -0,0 +1,28 @@ |
|||
import java.io.BufferedReader; |
|||
import java.io.IOException; |
|||
import java.io.InputStreamReader; |
|||
import java.net.HttpURLConnection; |
|||
import java.net.URL; |
|||
|
|||
public class HttpApi { |
|||
public static String sendHttpGETRequest(String url) throws IOException { |
|||
URL obj = new URL(url); |
|||
HttpURLConnection httpURLConnection = (HttpURLConnection) obj.openConnection(); |
|||
httpURLConnection.setRequestMethod("GET"); |
|||
int responseCode = httpURLConnection.getResponseCode(); |
|||
|
|||
if (responseCode == HttpURLConnection.HTTP_OK) { |
|||
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); |
|||
String inputLine; |
|||
StringBuffer response = new StringBuffer(); |
|||
|
|||
while ((inputLine = in.readLine()) != null) { |
|||
response.append(inputLine + "\n"); |
|||
} |
|||
in.close(); |
|||
|
|||
return response.toString(); |
|||
} |
|||
return null; |
|||
} |
|||
} |
@ -0,0 +1,119 @@ |
|||
import java.security.SecureRandom; |
|||
|
|||
public class PasswordGenerator { |
|||
|
|||
final String lowercaseCharacters = "abcdefghjkmnpqrstuvwxyz"; |
|||
final String uppercaseCharacters = "ABCDEFGHJKMNPQRSTUVWXYZ"; |
|||
final String digits = "0123456789"; |
|||
final String specialCharacters = ".!?=@#$()%^&/*_-+"; |
|||
final SecureRandom rand = new SecureRandom(); |
|||
|
|||
private int length = 12; |
|||
private boolean useUppercase = true; |
|||
private boolean useLowercase = true; |
|||
private boolean useDigits = true; |
|||
private boolean useSpecialCharacters = true; |
|||
private boolean requireEveryConfiguredCharacterType = true; |
|||
|
|||
|
|||
public String generateRandomPassword() { |
|||
StringBuilder generatedPassword = new StringBuilder(); |
|||
String characterPool = ""; |
|||
|
|||
if (isUseLowercase()) { |
|||
characterPool += lowercaseCharacters; |
|||
} |
|||
if (isUseUppercase()) { |
|||
characterPool += uppercaseCharacters; |
|||
} |
|||
if (isUseDigits()) { |
|||
characterPool += digits; |
|||
} |
|||
if (isUseSpecialCharacters()) { |
|||
characterPool += specialCharacters; |
|||
} |
|||
|
|||
for (int generatorPosition = 0; generatorPosition < getLength(); generatorPosition++) { |
|||
String customCharacterPool = characterPool; |
|||
|
|||
if (isRequireEveryConfiguredCharacterType()) { |
|||
if (generatorPosition == 0 && isUseLowercase()) { |
|||
customCharacterPool = lowercaseCharacters; |
|||
} else if (generatorPosition == 1 && isUseUppercase()) { |
|||
customCharacterPool = uppercaseCharacters; |
|||
} else if (generatorPosition == 2 && isUseDigits()) { |
|||
customCharacterPool = digits; |
|||
} else if (generatorPosition == 3 && isUseSpecialCharacters()) { |
|||
customCharacterPool = specialCharacters; |
|||
} else { |
|||
customCharacterPool = characterPool; |
|||
} |
|||
} |
|||
|
|||
generatedPassword.append(customCharacterPool.charAt(rand.nextInt(customCharacterPool.length()))); |
|||
} |
|||
|
|||
return shufflePassword(generatedPassword.toString()); |
|||
} |
|||
|
|||
public String shufflePassword(String password) { |
|||
StringBuilder shuffledPassword = new StringBuilder(); |
|||
StringBuilder passwordCopy = new StringBuilder(password); |
|||
|
|||
while (passwordCopy.length() != 0) { |
|||
int index = rand.nextInt(passwordCopy.length()); |
|||
char c = passwordCopy.charAt(index); |
|||
shuffledPassword.append(c); |
|||
passwordCopy.deleteCharAt(index); |
|||
} |
|||
return shuffledPassword.toString(); |
|||
} |
|||
|
|||
public boolean isUseUppercase() { |
|||
return useUppercase; |
|||
} |
|||
|
|||
public void setUseUppercase(boolean useUppercase) { |
|||
this.useUppercase = useUppercase; |
|||
} |
|||
|
|||
public boolean isUseLowercase() { |
|||
return useLowercase; |
|||
} |
|||
|
|||
public void setUseLowercase(boolean useLowercase) { |
|||
this.useLowercase = useLowercase; |
|||
} |
|||
|
|||
public boolean isUseDigits() { |
|||
return useDigits; |
|||
} |
|||
|
|||
public void setUseDigits(boolean useDigits) { |
|||
this.useDigits = useDigits; |
|||
} |
|||
|
|||
public boolean isUseSpecialCharacters() { |
|||
return useSpecialCharacters; |
|||
} |
|||
|
|||
public void setUseSpecialCharacters(boolean useSpecialCharacters) { |
|||
this.useSpecialCharacters = useSpecialCharacters; |
|||
} |
|||
|
|||
public boolean isRequireEveryConfiguredCharacterType() { |
|||
return requireEveryConfiguredCharacterType; |
|||
} |
|||
|
|||
public void setRequireEveryConfiguredCharacterType(boolean requireEveryConfiguredCharacterType) { |
|||
this.requireEveryConfiguredCharacterType = requireEveryConfiguredCharacterType; |
|||
} |
|||
|
|||
public int getLength() { |
|||
return length; |
|||
} |
|||
|
|||
public void setLength(int length) { |
|||
this.length = length; |
|||
} |
|||
} |
@ -0,0 +1,166 @@ |
|||
import java.io.BufferedReader; |
|||
import java.io.IOException; |
|||
import java.io.StringReader; |
|||
import java.math.BigInteger; |
|||
import java.security.MessageDigest; |
|||
import java.security.NoSuchAlgorithmException; |
|||
import java.util.Locale; |
|||
import java.util.regex.Pattern; |
|||
|
|||
public class PasswordValidator { |
|||
int minLength = 6; |
|||
boolean requireUppercase = true; |
|||
boolean requireLowercase = true; |
|||
boolean requireDigit = true; |
|||
boolean requireSpecialChar = true; |
|||
boolean checkPwned = true; |
|||
boolean checkWordlist = false; |
|||
|
|||
private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$"); |
|||
private final Pattern lowercasePattern = Pattern.compile("^(?=.*[a-z]).+$"); |
|||
private final Pattern digitPattern = Pattern.compile("^(?=.*\\d).+$"); |
|||
private final Pattern specialCharPattern = Pattern.compile("^(?=.*[.!?=@#$()%^&/*_\\-+]).+$"); |
|||
private static final String pwnedPasswordsApiUrl = "https://api.pwnedpasswords.com/range/"; |
|||
|
|||
public boolean validate(String password) { |
|||
if (password.length() < minLength) { |
|||
return false; |
|||
} else if (requireUppercase && !uppercasePattern.matcher(password).matches()) { |
|||
return false; |
|||
} else if (requireLowercase && !lowercasePattern.matcher(password).matches()) { |
|||
return false; |
|||
} else if (requireDigit && !digitPattern.matcher(password).matches()) { |
|||
return false; |
|||
} else if (requireSpecialChar && !specialCharPattern.matcher(password).matches()) { |
|||
return false; |
|||
} else if (checkWordlist && isInWordlist(password)) { |
|||
return false; |
|||
} else if (checkPwned && isPwned(password)) { |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
public int getMinLength() { |
|||
return minLength; |
|||
} |
|||
|
|||
public void setMinLength(int minLength) { |
|||
this.minLength = minLength; |
|||
} |
|||
|
|||
public boolean isRequireUppercase() { |
|||
return requireUppercase; |
|||
} |
|||
|
|||
public void setRequireUppercase(boolean requireUppercase) { |
|||
this.requireUppercase = requireUppercase; |
|||
} |
|||
|
|||
public boolean isRequireLowercase() { |
|||
return requireLowercase; |
|||
} |
|||
|
|||
public void setRequireLowercase(boolean requireLowercase) { |
|||
this.requireLowercase = requireLowercase; |
|||
} |
|||
|
|||
public boolean isRequireDigit() { |
|||
return requireDigit; |
|||
} |
|||
|
|||
public void setRequireDigit(boolean requireDigit) { |
|||
this.requireDigit = requireDigit; |
|||
} |
|||
|
|||
public boolean isRequireSpecialChar() { |
|||
return requireSpecialChar; |
|||
} |
|||
|
|||
public void setRequireSpecialChar(boolean requireSpecialChar) { |
|||
this.requireSpecialChar = requireSpecialChar; |
|||
} |
|||
|
|||
public boolean isCheckPwned() { |
|||
return checkPwned; |
|||
} |
|||
|
|||
public void setCheckPwned(boolean checkPwned) { |
|||
this.checkPwned = checkPwned; |
|||
} |
|||
|
|||
public boolean isCheckWordlist() { |
|||
return checkWordlist; |
|||
} |
|||
|
|||
public void setCheckWordlist(boolean checkWordlist) { |
|||
this.checkWordlist = checkWordlist; |
|||
} |
|||
|
|||
public static String getSHA1Hash(String input) { |
|||
if (input.length() > 0) { |
|||
try { |
|||
MessageDigest md = MessageDigest.getInstance("SHA-1"); |
|||
byte[] messageDigest = md.digest(input.getBytes()); |
|||
|
|||
// Convert byte array into signum representation |
|||
BigInteger no = new BigInteger(1, messageDigest); |
|||
|
|||
// Convert message digest into hex value |
|||
StringBuilder hashtext = new StringBuilder(); |
|||
hashtext.append(no.toString(16)); |
|||
|
|||
// Add preceding 0s to make it 32 bit |
|||
while (hashtext.length() < 32) { |
|||
hashtext.insert(0, "0"); |
|||
} |
|||
return hashtext.toString(); |
|||
} catch (NoSuchAlgorithmException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
public static boolean isPwned(String password) { |
|||
String sha1 = PasswordValidator.getSHA1Hash(password); |
|||
if (sha1 != null) { |
|||
String url = pwnedPasswordsApiUrl + sha1.substring(0, 5); |
|||
try { |
|||
String result = HttpApi.sendHttpGETRequest(url); |
|||
BufferedReader bufReader = new BufferedReader(new StringReader(result)); |
|||
String line = null; |
|||
while ((line = bufReader.readLine()) != null) { |
|||
String[] lineSplit = line.split(":"); |
|||
if (lineSplit.length > 0 && sha1.toUpperCase().endsWith(lineSplit[0])) { |
|||
return true; |
|||
} |
|||
} |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public static boolean isInWordlist(String password) { |
|||
try { |
|||
String lowerPassword = password.toLowerCase(); |
|||
ResourceApi resourceApi = new ResourceApi(); |
|||
String germanWordlist = resourceApi.getFileFromResourceAsString("german_wordlist.txt"); |
|||
BufferedReader bufReader = new BufferedReader(new StringReader(germanWordlist)); |
|||
String line = null; |
|||
while ((line = bufReader.readLine()) != null) { |
|||
if (lowerPassword.contains(line.trim().toLowerCase())) { |
|||
return true; |
|||
} |
|||
} |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
} |
@ -0,0 +1,37 @@ |
|||
import java.io.BufferedReader; |
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.io.InputStreamReader; |
|||
|
|||
public class ResourceApi { |
|||
|
|||
public String getFileFromResourceAsString(String fileName) throws IOException { |
|||
InputStream inputStream = getFileFromResourceAsStream(fileName); |
|||
return getStringFromInputStream(inputStream); |
|||
} |
|||
|
|||
public InputStream getFileFromResourceAsStream(String fileName) { |
|||
//ClassLoader classLoader = getClass().getClassLoader(); |
|||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
|||
InputStream inputStream = classLoader.getResourceAsStream(fileName); |
|||
|
|||
if (inputStream == null) { |
|||
throw new IllegalArgumentException("file not found! " + fileName); |
|||
} else { |
|||
return inputStream; |
|||
} |
|||
} |
|||
|
|||
public String getStringFromInputStream(InputStream inputStream) throws IOException { |
|||
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); |
|||
String inputLine; |
|||
StringBuffer response = new StringBuffer(); |
|||
|
|||
while ((inputLine = in.readLine()) != null) { |
|||
response.append(inputLine + "\n"); |
|||
} |
|||
in.close(); |
|||
|
|||
return response.toString(); |
|||
} |
|||
} |
@ -0,0 +1,50 @@ |
|||
import java.io.*; |
|||
import java.util.Scanner; |
|||
|
|||
public class Storage implements StorageInterface { |
|||
@Override |
|||
public void export() { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void load() { |
|||
|
|||
} |
|||
|
|||
public boolean writeFile(String path, String content) { |
|||
if (!path.isEmpty() && !content.isEmpty()) { |
|||
try { |
|||
BufferedWriter writer = new BufferedWriter(new FileWriter(path, false)); |
|||
writer.append(content); |
|||
writer.close(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
return false; |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
public String readFile(String path) { |
|||
if (!path.isEmpty()) { |
|||
StringBuilder data = new StringBuilder(); |
|||
try { |
|||
File f = new File(path); |
|||
Scanner myReader = new Scanner(f); |
|||
while (myReader.hasNextLine()) { |
|||
data.append(myReader.nextLine()); |
|||
} |
|||
myReader.close(); |
|||
} catch (FileNotFoundException e) { |
|||
e.printStackTrace(); |
|||
return null; |
|||
} |
|||
|
|||
return data.toString(); |
|||
} |
|||
return null; |
|||
} |
|||
} |
@ -0,0 +1,4 @@ |
|||
public interface StorageInterface { |
|||
public void export(); |
|||
public void load(); |
|||
} |
137465
src/main/resources/german_wordlist.txt
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,19 @@ |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.Objects; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class HttpApiTest { |
|||
|
|||
@Test |
|||
void sendHttpGETRequest() { |
|||
assertDoesNotThrow(() -> HttpApi.sendHttpGETRequest("https://httpbin.org/get")); |
|||
try { |
|||
assertTrue(Objects.requireNonNull(HttpApi.sendHttpGETRequest("https://httpbin.org/get")).contains("args")); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,72 @@ |
|||
import org.junit.jupiter.api.BeforeAll; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.util.regex.Pattern; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class PasswordGeneratorTest { |
|||
|
|||
static PasswordGenerator passwordGenerator; |
|||
|
|||
@BeforeAll |
|||
static void init() { |
|||
passwordGenerator = new PasswordGenerator(); |
|||
} |
|||
|
|||
@Test |
|||
void testGeneratedPasswordLength() { |
|||
assertNotSame("", passwordGenerator.generateRandomPassword()); |
|||
assertEquals(passwordGenerator.generateRandomPassword().length(), passwordGenerator.getLength()); |
|||
|
|||
passwordGenerator.setLength(33); |
|||
assertEquals(passwordGenerator.getLength(), 33); |
|||
assertEquals(passwordGenerator.generateRandomPassword().length(), passwordGenerator.getLength()); |
|||
} |
|||
|
|||
@Test |
|||
void testPasswordLowercaseRequirement() { |
|||
passwordGenerator.setUseLowercase(false); |
|||
assertFalse(Pattern.compile("^(?=.*[a-z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); |
|||
passwordGenerator.setUseLowercase(true); |
|||
assertTrue(Pattern.compile("^(?=.*[a-z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); |
|||
} |
|||
|
|||
@Test |
|||
void testPasswordUppercaseRequirement() { |
|||
passwordGenerator.setUseUppercase(false); |
|||
assertFalse(Pattern.compile("^(?=.*[A-Z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); |
|||
passwordGenerator.setUseUppercase(true); |
|||
assertTrue(Pattern.compile("^(?=.*[A-Z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); |
|||
} |
|||
|
|||
@Test |
|||
void testPasswordDigitsRequirement() { |
|||
passwordGenerator.setUseDigits(false); |
|||
assertFalse(Pattern.compile("^(?=.*\\d).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); |
|||
passwordGenerator.setUseDigits(true); |
|||
assertTrue(Pattern.compile("^(?=.*\\d).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); |
|||
} |
|||
|
|||
@Test |
|||
void testPasswordSpecialCharactersRequirement() { |
|||
passwordGenerator.setUseSpecialCharacters(false); |
|||
assertFalse(Pattern.compile("^(?=.*[.!?=@#$()%^&/*_\\-+]).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); |
|||
passwordGenerator.setUseSpecialCharacters(true); |
|||
assertTrue(Pattern.compile("^(?=.*[.!?=@#$()%^&/*_\\-+]).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); |
|||
} |
|||
|
|||
@Test |
|||
void testPasswordForEveryRequiredCharacter() { |
|||
passwordGenerator.setRequireEveryConfiguredCharacterType(true); |
|||
PasswordValidator passwordValidator = new PasswordValidator(); |
|||
assertTrue(passwordValidator.validate(passwordGenerator.generateRandomPassword())); |
|||
} |
|||
|
|||
@Test |
|||
void testShufflePassword() { |
|||
String testInput = "ABcdefgh123"; |
|||
assertNotEquals(passwordGenerator.shufflePassword(testInput), testInput); |
|||
assertEquals(passwordGenerator.shufflePassword(testInput).length(), testInput.length()); |
|||
} |
|||
} |
@ -0,0 +1,130 @@ |
|||
import org.junit.jupiter.api.BeforeAll; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class PasswordValidatorTest { |
|||
|
|||
static PasswordValidator passwordValidator = new PasswordValidator(); |
|||
|
|||
@BeforeAll |
|||
static void init() { |
|||
passwordValidator = new PasswordValidator(); |
|||
} |
|||
|
|||
@Test |
|||
void testMinimumPasswordLength() { |
|||
assertFalse(passwordValidator.validate("")); |
|||
|
|||
passwordValidator.setMinLength(6); |
|||
passwordValidator.setRequireUppercase(false); |
|||
passwordValidator.setRequireLowercase(false); |
|||
passwordValidator.setRequireDigit(false); |
|||
passwordValidator.setCheckPwned(false); |
|||
passwordValidator.setRequireSpecialChar(false); |
|||
assertFalse(passwordValidator.validate("abcde")); |
|||
assertTrue(passwordValidator.validate("abcdef")); |
|||
assertTrue(passwordValidator.validate("abcdefg")); |
|||
} |
|||
|
|||
@Test |
|||
void testUppercasePasswordRequirement() { |
|||
passwordValidator.setRequireUppercase(true); |
|||
passwordValidator.setRequireLowercase(false); |
|||
passwordValidator.setRequireDigit(false); |
|||
passwordValidator.setCheckPwned(false); |
|||
passwordValidator.setRequireSpecialChar(false); |
|||
assertFalse(passwordValidator.validate("abcdef")); |
|||
assertTrue(passwordValidator.validate("abCdef")); |
|||
assertTrue(passwordValidator.validate("ABCDEF")); |
|||
} |
|||
|
|||
@Test |
|||
void testLowercasePasswordRequirement() { |
|||
passwordValidator.setRequireUppercase(true); |
|||
passwordValidator.setRequireLowercase(true); |
|||
passwordValidator.setRequireDigit(false); |
|||
passwordValidator.setCheckPwned(false); |
|||
passwordValidator.setRequireSpecialChar(false); |
|||
assertFalse(passwordValidator.validate("abcdef")); |
|||
assertTrue(passwordValidator.validate("abCdef")); |
|||
assertFalse(passwordValidator.validate("ABCDEF")); |
|||
} |
|||
|
|||
@Test |
|||
void testDigitsPasswordRequirement() { |
|||
passwordValidator.setRequireUppercase(true); |
|||
passwordValidator.setRequireLowercase(true); |
|||
passwordValidator.setRequireDigit(true); |
|||
passwordValidator.setCheckPwned(false); |
|||
passwordValidator.setRequireSpecialChar(false); |
|||
assertFalse(passwordValidator.validate("8")); |
|||
assertFalse(passwordValidator.validate("12345678")); |
|||
assertFalse(passwordValidator.validate("abcdef")); |
|||
assertFalse(passwordValidator.validate("abcdef8")); |
|||
assertFalse(passwordValidator.validate("abCdef")); |
|||
assertFalse(passwordValidator.validate("ABCDEF")); |
|||
assertFalse(passwordValidator.validate("ABCDEF8")); |
|||
assertTrue(passwordValidator.validate("abCDE8F")); |
|||
} |
|||
|
|||
@Test |
|||
void testSpecialCharsPasswordRequirement() { |
|||
passwordValidator.setRequireUppercase(true); |
|||
passwordValidator.setRequireLowercase(true); |
|||
passwordValidator.setRequireDigit(true); |
|||
passwordValidator.setCheckPwned(false); |
|||
passwordValidator.setRequireSpecialChar(true); |
|||
assertFalse(passwordValidator.validate("*")); |
|||
assertFalse(passwordValidator.validate("abCDE8F")); |
|||
assertTrue(passwordValidator.validate("abCDE8_F")); |
|||
assertTrue(passwordValidator.validate("abCDE*/8_F")); |
|||
} |
|||
|
|||
/** |
|||
* Requires a network connection and DNS to be set up. |
|||
*/ |
|||
@Test |
|||
void testPasswordPwnedCheck() { |
|||
passwordValidator.setRequireUppercase(true); |
|||
passwordValidator.setRequireLowercase(true); |
|||
passwordValidator.setRequireDigit(true); |
|||
passwordValidator.setCheckPwned(true); |
|||
assertFalse(passwordValidator.validate("8")); |
|||
assertFalse(passwordValidator.validate("asdf12")); |
|||
assertTrue(passwordValidator.validate("=phan0johB4aisae6Mie0jeip9Saejahc0iuvuth7ahv9uoni6o*_.+")); |
|||
} |
|||
|
|||
@Test |
|||
void testPasswordWordlistCheck() { |
|||
passwordValidator.setRequireUppercase(true); |
|||
passwordValidator.setRequireLowercase(true); |
|||
passwordValidator.setRequireDigit(true); |
|||
passwordValidator.setCheckPwned(true); |
|||
passwordValidator.setCheckWordlist(true); |
|||
assertFalse(passwordValidator.validate("8")); |
|||
assertFalse(passwordValidator.validate("_Sonnenblume123")); |
|||
assertTrue(passwordValidator.validate("=phan0johB4aisae6Mie0jeip9Saejahc0iuvuth7ahv9uoni6o*_.+")); |
|||
} |
|||
|
|||
@Test |
|||
void getSHA1Hash() { |
|||
assertEquals("356A192B7913B04C54574D18C28D46E6395428AB".toLowerCase(), PasswordValidator.getSHA1Hash("1")); |
|||
assertEquals("A233F0E898ED0661D6D47ED0958F16B52E537231".toLowerCase(), PasswordValidator.getSHA1Hash("asdf12")); |
|||
assertNull(PasswordValidator.getSHA1Hash("")); |
|||
} |
|||
|
|||
@Test |
|||
void isPwned() { |
|||
assertTrue(PasswordValidator.isPwned("asdf12")); |
|||
assertFalse(PasswordValidator.isPwned("=phan0johB4aisae6Mie0jeip9Saejahc0iuvuth7ahv9uoni6o*_.+")); |
|||
assertFalse(PasswordValidator.isPwned("")); |
|||
} |
|||
|
|||
@Test |
|||
void isInWordlist() { |
|||
assertTrue(PasswordValidator.isInWordlist("Sonnenblume")); |
|||
assertTrue(PasswordValidator.isInWordlist("_Sonnenblume123")); |
|||
assertFalse(PasswordValidator.isInWordlist("=phan0johB4aisae6Mie0jeip9Saejahc0iuvuth7ahv9uoni6o*_.+")); |
|||
} |
|||
} |
@ -0,0 +1,48 @@ |
|||
import org.junit.jupiter.api.BeforeAll; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.io.*; |
|||
import java.nio.charset.StandardCharsets; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class ResourceApiTest { |
|||
|
|||
private static ResourceApi resourceApi; |
|||
|
|||
@BeforeAll |
|||
static void init() { |
|||
resourceApi = new ResourceApi(); |
|||
} |
|||
|
|||
@Test |
|||
void getFileFromResourceAsStream() throws IOException { |
|||
assertThrowsExactly(IllegalArgumentException.class, |
|||
() -> resourceApi.getFileFromResourceAsStream("does_not_exist")); |
|||
|
|||
InputStream is = resourceApi.getFileFromResourceAsStream("german_wordlist.txt"); |
|||
|
|||
BufferedReader in = new BufferedReader(new InputStreamReader(is)); |
|||
String inputLine; |
|||
StringBuffer response = new StringBuffer(); |
|||
|
|||
while ((inputLine = in.readLine()) != null) { |
|||
response.append(inputLine + "\n"); |
|||
} |
|||
in.close(); |
|||
|
|||
assertTrue(response.toString().contains("Alleinherrschaft")); |
|||
} |
|||
|
|||
@Test |
|||
void getStringFromInputStream() throws IOException { |
|||
String testString = "I am a test string!\nAnother test line.\n"; |
|||
InputStream is = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8)); |
|||
assertEquals(resourceApi.getStringFromInputStream(is), testString); |
|||
} |
|||
|
|||
@Test |
|||
void getFileFromResourceAsString() throws IOException { |
|||
assertTrue(resourceApi.getFileFromResourceAsString("german_wordlist.txt").contains("Alleinherrschaft")); |
|||
} |
|||
} |
@ -0,0 +1,62 @@ |
|||
import org.junit.jupiter.api.AfterAll; |
|||
import org.junit.jupiter.api.BeforeAll; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.io.File; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class StorageTest { |
|||
|
|||
static Storage storage; |
|||
static String testFilePath = "/tmp/test.txt"; |
|||
|
|||
@BeforeAll |
|||
static void init() { |
|||
storage = new Storage(); |
|||
|
|||
if (System.getProperty("os.name").toLowerCase().contains("win")) { |
|||
testFilePath = "C:\\test.txt"; |
|||
} |
|||
} |
|||
|
|||
@AfterAll |
|||
static void clear() { |
|||
File myObj = new File(testFilePath); |
|||
myObj.delete(); |
|||
} |
|||
|
|||
@Test |
|||
void constructor() { |
|||
assertInstanceOf(Storage.class, storage); |
|||
} |
|||
|
|||
@Test |
|||
void export() { |
|||
} |
|||
|
|||
@Test |
|||
void load() { |
|||
} |
|||
|
|||
@Test |
|||
void writeFile() { |
|||
String content = "test"; |
|||
assertTrue(storage.writeFile(testFilePath, content)); |
|||
|
|||
File f = new File(testFilePath); |
|||
assertTrue(f.isFile()); |
|||
} |
|||
|
|||
@Test |
|||
void readFile() { |
|||
String content = "test"; |
|||
|
|||
File f = new File(testFilePath); |
|||
if (!f.isFile()) { |
|||
assertTrue(storage.writeFile(testFilePath, content)); |
|||
} |
|||
|
|||
assertEquals(content, storage.readFile(testFilePath)); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue