Browse Source

Merge remote-tracking branch 'origin/feature-credential' into feature-credential

# Conflicts:
#	src/main/java/Vault.java
feature-credential
fdai6352 3 years ago
parent
commit
5949c7178c
  1. 30
      README.md
  2. 27
      pom.xml
  3. 28
      src/main/java/HttpApi.java
  4. 119
      src/main/java/PasswordGenerator.java
  5. 63
      src/main/java/PasswordManager.java
  6. 166
      src/main/java/PasswordValidator.java
  7. 37
      src/main/java/ResourceApi.java
  8. 50
      src/main/java/Storage.java
  9. 4
      src/main/java/StorageInterface.java
  10. 2
      src/main/java/Vault.java
  11. 137465
      src/main/resources/german_wordlist.txt
  12. 19
      src/test/java/HttpApiTest.java
  13. 72
      src/test/java/PasswordGeneratorTest.java
  14. 55
      src/test/java/PasswordManagerTest.java
  15. 130
      src/test/java/PasswordValidatorTest.java
  16. 48
      src/test/java/ResourceApiTest.java
  17. 62
      src/test/java/StorageTest.java
  18. 13
      src/test/java/VaultTest.java

30
README.md

@ -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.

27
pom.xml

@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.ciip-gruppe8-password-manager</groupId>
<artifactId>ciip-gruppe8-password-manager</artifactId>
<groupId>org.ciip-grp8-password-manager</groupId>
<artifactId>ciip-grp8-password-manager</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
@ -70,9 +70,26 @@
<version>5.7.0</version>
</dependency>
</dependencies>
<!-- <configuration>
<argLine>${argLine}</argLine>
</configuration> -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>PasswordManager</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

28
src/main/java/HttpApi.java

@ -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;
}
}

119
src/main/java/PasswordGenerator.java

@ -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;
}
}

63
src/main/java/PasswordManager.java

@ -1,6 +1,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
@ -12,6 +13,9 @@ public class PasswordManager implements PasswordManagerInterface {
boolean running = true;
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ArrayList<Vault> vaults = new ArrayList<>();
int selectedVaultId = -1;
ArrayList<String> vaultOperationOptions = new ArrayList<>();
public static void main(String[] args) {
PasswordManager pm = new PasswordManager();
@ -26,11 +30,6 @@ public class PasswordManager implements PasswordManagerInterface {
println("Hello World");
}
@Override
public void listVaults() {
println("Vaults:");
}
private void println(String output) {
try {
outputStream.write((output + "\n").getBytes(StandardCharsets.UTF_8));
@ -39,6 +38,18 @@ public class PasswordManager implements PasswordManagerInterface {
}
}
@Override
public void listVaults() {
println("Vaults:");
for (int i = 0; i < vaults.size(); i++) {
println("- vault id: " + i);
}
}
public void createNewVault() {
vaults.add(new Vault());
}
@Override
public void openVault() {
@ -53,8 +64,10 @@ public class PasswordManager implements PasswordManagerInterface {
sb.append("\nciip Gruppe 8 - Password Manager\n\n");
sb.append("Menu:\n");
sb.append("- list vaults: l:\n");
sb.append("- select vault: v x (replace x with vault id)\n");
sb.append("- list vaults: l\n");
sb.append("- create new vault: v\n");
sb.append("- select vault and show operations: v x (replace x with vault id)\n");
sb.append("- select vault operation: o x (replace o with a operation id for the selected vault)\n");
sb.append("- exit: e\n");
println(sb.toString());
@ -65,7 +78,9 @@ public class PasswordManager implements PasswordManagerInterface {
running = false;
} else if (input.equals("l")) {
listVaults();
} else if (input.startsWith("v")) {
} else if (input.equals("v")) {
createNewVault();
} else if (input.startsWith("v")) {
String vaultAction = (input.replace('v', ' ')).trim();
int vaultId = -1;
@ -76,14 +91,16 @@ public class PasswordManager implements PasswordManagerInterface {
}
if (vaultId >= 0) {
selectedVaultId = vaultId;
println("\nVault " + vaultId + " selected.");
try {
int internalOptionCounter = 0;
ArrayList<String> options = new ArrayList<>();
println("\nAvailable vault operations:");
for (Method m : Class.forName("Vault").getDeclaredMethods()) { // returns all declared methods including private or protected
try {
if (Modifier.isPublic(this.getClass().getDeclaredMethod(m.getName()).getModifiers())) { // filter out methods that are not public (using Modifier) or have arguments (using getDeclaredMethod)
options.add(m.getName());
if (Modifier.isPublic(Class.forName("Vault").getDeclaredMethod(m.getName()).getModifiers())) { // filter out methods that are not public (using Modifier) or have arguments (using getDeclaredMethod)
vaultOperationOptions.add(m.getName());
println(internalOptionCounter + " - " + m.getName());
internalOptionCounter++;
}
@ -95,6 +112,30 @@ public class PasswordManager implements PasswordManagerInterface {
println("-- vaults not implemented yet");
}
}
} else if (input.startsWith("o")) {
String operation = (input.replace('o', ' ')).trim();
int operationId = -1;
try {
operationId = Integer.parseInt(operation);
} catch (NumberFormatException e) {
println("-- please enter a valid operation id");
}
if (selectedVaultId < 0) {
println("-- a vault needs to be selected");
} else if (operationId >= 0 && operationId < vaultOperationOptions.size()) {
Vault vault = vaults.get(selectedVaultId);
try {
println("Run selected operation '" + vaultOperationOptions.get(operationId) + "':");
vault.getClass().getDeclaredMethod(vaultOperationOptions.get(operationId)).invoke(vault);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
println("-- selected operation failed");
}
} else {
println("-- please enter a valid operation id");
}
}
}
}

166
src/main/java/PasswordValidator.java

@ -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;
}
}

37
src/main/java/ResourceApi.java

@ -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();
}
}

50
src/main/java/Storage.java

@ -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;
}
}

4
src/main/java/StorageInterface.java

@ -0,0 +1,4 @@
public interface StorageInterface {
public void export();
public void load();
}

2
src/main/java/Vault.java

@ -19,6 +19,7 @@ public class Vault implements VaultInterface {
public boolean decision;
InputStream inputS = System.in;
OutputStream outputS = System.out;
private CredentialRepository credentialRepository = new CredentialRepository();
/*public static void main (String args[]) {
@ -80,7 +81,6 @@ public class Vault implements VaultInterface {
setNumbers(inputN);
break;
}
}
public void setPWLength(String pwLength) {

137465
src/main/resources/german_wordlist.txt
File diff suppressed because it is too large
View File

19
src/test/java/HttpApiTest.java

@ -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();
}
}
}

72
src/test/java/PasswordGeneratorTest.java

@ -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());
}
}

55
src/test/java/PasswordManagerTest.java

@ -46,7 +46,18 @@ class PasswordManagerTest {
pm.inputStream = new ByteArrayInputStream("l\n".getBytes(StandardCharsets.UTF_8));
pm.outputStream = outputStream;
pm.showMenu();
assertTrue(outputStream.toString().endsWith("Vaults:\n"));
pm.createNewVault();
assertTrue(outputStream.toString().contains("Vaults:\n- vault id: 0\n"));
}
@Test
void menuNavigationCreateNewVault() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
pm.inputStream = new ByteArrayInputStream("v\n".getBytes(StandardCharsets.UTF_8));
pm.outputStream = outputStream;
int vaultCount = pm.vaults.size();
pm.showMenu();
assertEquals(vaultCount + 1, pm.vaults.size());
}
@Test
@ -58,6 +69,41 @@ class PasswordManagerTest {
assertFalse(outputStream.toString().endsWith("-- vaults not implemented yet\n"));
}
@Test
void menuNavigationSelectVaultOperation() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
pm.inputStream = new ByteArrayInputStream("v\n".getBytes(StandardCharsets.UTF_8));
pm.outputStream = new ByteArrayOutputStream();
pm.showMenu();
pm.inputStream = new ByteArrayInputStream("v 0\n".getBytes(StandardCharsets.UTF_8));
pm.outputStream = new ByteArrayOutputStream();
pm.showMenu();
System.out.println(pm.outputStream.toString());
pm.inputStream = new ByteArrayInputStream("o 0\n".getBytes(StandardCharsets.UTF_8));
pm.outputStream = outputStream;
pm.vaults.get(0).inputS = new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8));
pm.showMenu();
assertTrue(outputStream.toString().contains("Run selected operation"));
outputStream = new ByteArrayOutputStream();
pm.inputStream = new ByteArrayInputStream("o foobert\n".getBytes(StandardCharsets.UTF_8));
pm.outputStream = outputStream;
pm.showMenu();
assertTrue(outputStream.toString().endsWith("-- please enter a valid operation id\n"));
outputStream = new ByteArrayOutputStream();
pm.inputStream = new ByteArrayInputStream("o 1000\n".getBytes(StandardCharsets.UTF_8));
pm.outputStream = outputStream;
pm.showMenu();
System.out.println(outputStream);
assertTrue(outputStream.toString().endsWith("-- please enter a valid operation id\n"));
}
@Test
void showMenu() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@ -88,6 +134,13 @@ class PasswordManagerTest {
assertTrue(outputStream.toString().startsWith("\nciip Gruppe 8"));
}
@Test
void createNewVault() {
int vaultCount = pm.vaults.size();
pm.createNewVault();
assertEquals(vaultCount + 1, pm.vaults.size());
}
private ByteArrayInputStream getEmptyStringInputStream() {
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
}

130
src/test/java/PasswordValidatorTest.java

@ -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*_.+"));
}
}

48
src/test/java/ResourceApiTest.java

@ -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"));
}
}

62
src/test/java/StorageTest.java

@ -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));
}
}

13
src/test/java/VaultTest.java

@ -119,14 +119,13 @@ public class VaultTest {
assertTrue(outputStream.toString().contains("name"));
}
@Test
void getAsJson() {vlt.getAsJson();}
@Test
void loadFromJson() {vlt.loadFromJson();}
@Test
@Test
void openConfigureMenu() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
@ -142,7 +141,7 @@ public class VaultTest {
assertFalse(vlt.config);
}
@Test
@Test
void doNotExitConfigAfterWrongInput() {
vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
vlt.configure();
@ -156,7 +155,7 @@ public class VaultTest {
assertTrue(outputStream.toString().startsWith("Configure:"));
}
@Test
@Test
void openSetPWLengthMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("l\n7".getBytes(StandardCharsets.UTF_8));
@ -198,7 +197,7 @@ public class VaultTest {
vlt.setCapital("no");
assertFalse(vlt.haveCapitals = false);
assertTrue(outputStream.toString().contains("don´t"));
assertTrue(outputStream.toString().contains("don't"));
}
@Test
@ -222,7 +221,7 @@ public class VaultTest {
vlt.setSpecialChar("no");
assertFalse(vlt.hasSpecialChars = false);
assertTrue(outputStream.toString().contains("don´t"));
assertTrue(outputStream.toString().contains("don't"));
}
@Test
@ -247,7 +246,7 @@ public class VaultTest {
vlt.setNumbers("no");
assertFalse(vlt.hasNumbers = false);
assertTrue(outputStream.toString().contains("don´t"));
assertTrue(outputStream.toString().contains("don't"));
}
private ByteArrayInputStream getEmptyStringInputStream() {

Loading…
Cancel
Save