|
|
@ -0,0 +1,238 @@ |
|
|
|
import org.junit.jupiter.api.BeforeAll; |
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.mockito.Mockito; |
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.NoSuchElementException; |
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
|
|
|
|
|
public class VaultTest { |
|
|
|
|
|
|
|
//Vault vlt = new Vault(); |
|
|
|
|
|
|
|
static Vault vlt; |
|
|
|
|
|
|
|
@BeforeAll |
|
|
|
static void init() { |
|
|
|
vlt = new Vault(); |
|
|
|
} |
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
void reset() { |
|
|
|
vlt.outputS = System.out; |
|
|
|
vlt.inputS = System.in; |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void openCredentialMenu() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.credentialMenu(); |
|
|
|
assertTrue(vlt.credentialM); |
|
|
|
assertTrue(outputStream.toString().startsWith("Configure")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void exitConfigureMenu(){ |
|
|
|
vlt.inputS = new ByteArrayInputStream("e".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.credentialMenu(); |
|
|
|
assertFalse(vlt.credentialM); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void openAddCredentialMenuItem() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.inputS = new ByteArrayInputStream("a".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.credentialMenu(); |
|
|
|
assertTrue(outputStream.toString().contains("Type")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void openShowCredentialMenuItem() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.inputS = new ByteArrayInputStream("c".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.credentialMenu(); |
|
|
|
assertTrue(outputStream.toString().contains("Type")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void openEditCredentialMenuItem() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.inputS = new ByteArrayInputStream("l".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.credentialMenu(); |
|
|
|
assertTrue(outputStream.toString().contains("Type")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void addCredentialTest() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.inputS = new ByteArrayInputStream("peter\npassword".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.addCredential(); |
|
|
|
|
|
|
|
assertTrue(outputStream.toString().contains("username")); |
|
|
|
assertEquals("peter", vlt.userName); |
|
|
|
assertTrue(outputStream.toString().contains("password")); |
|
|
|
assertEquals("password",vlt.password); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void showCredentialTest() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.inputS = new ByteArrayInputStream("1".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.showCredential(); |
|
|
|
|
|
|
|
assertTrue(outputStream.toString().contains("Type")); |
|
|
|
assertTrue(vlt.isInt); |
|
|
|
|
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.inputS = new ByteArrayInputStream("peter".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.showCredential(); |
|
|
|
|
|
|
|
assertTrue(outputStream.toString().contains("Type")); |
|
|
|
assertFalse(vlt.isInt); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void getAsJson() {vlt.getAsJson();} |
|
|
|
|
|
|
|
@Test |
|
|
|
void loadFromJson() {vlt.loadFromJson();} |
|
|
|
|
|
|
|
@Test |
|
|
|
void openConfigureMenu() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.configure(); |
|
|
|
assertTrue(outputStream.toString().startsWith("Configure:")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void exitConfigurationMenu(){ |
|
|
|
vlt.inputS = new ByteArrayInputStream("e".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.configure(); |
|
|
|
assertFalse(vlt.config); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void doNotExitConfigAfterWrongInput() { |
|
|
|
vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.configure(); |
|
|
|
assertTrue(vlt.config); |
|
|
|
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.inputS = getEmptyStringInputStream(); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
|
|
|
|
assertThrowsExactly(NoSuchElementException.class, () -> vlt.configure()); |
|
|
|
assertTrue(outputStream.toString().startsWith("Configure:")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void openSetPWLengthMenuItem() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.inputS = new ByteArrayInputStream("l\n7".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.configure(); |
|
|
|
assertEquals(7, vlt.PWLength); |
|
|
|
assertTrue(outputStream.toString().contains("Set")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void setPWLength() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.setPWLength("7"); |
|
|
|
|
|
|
|
assertEquals(7, vlt.PWLength); |
|
|
|
assertTrue(outputStream.toString().contains("now:")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void openSetCapitalsMenuItem() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.configure(); |
|
|
|
assertTrue(vlt.haveCapitals = true); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void setCapitals() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.setCapital("yes"); |
|
|
|
|
|
|
|
assertTrue(vlt.haveCapitals = true); |
|
|
|
assertTrue(outputStream.toString().contains("contain")); |
|
|
|
|
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.setCapital("no"); |
|
|
|
|
|
|
|
assertFalse(vlt.haveCapitals = false); |
|
|
|
assertTrue(outputStream.toString().contains("don´t")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void openSetSpecialCharsMenuItem() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.configure(); |
|
|
|
assertTrue(vlt.hasSpecialChars = true); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void setSpecialCharTest() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.setSpecialChar("yes"); |
|
|
|
|
|
|
|
assertTrue(vlt.hasSpecialChars = true); |
|
|
|
|
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.setSpecialChar("no"); |
|
|
|
|
|
|
|
assertFalse(vlt.hasSpecialChars = false); |
|
|
|
assertTrue(outputStream.toString().contains("don´t")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void openSetNumbersMenuItem() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8)); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.configure(); |
|
|
|
assertTrue(vlt.hasNumbers = true); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void setNumbersTest() { |
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.setNumbers("yes"); |
|
|
|
|
|
|
|
assertTrue(vlt.hasNumbers = true); |
|
|
|
assertTrue(outputStream.toString().contains("contain")); |
|
|
|
|
|
|
|
vlt.outputS = outputStream; |
|
|
|
vlt.setNumbers("no"); |
|
|
|
|
|
|
|
assertFalse(vlt.hasNumbers = false); |
|
|
|
assertTrue(outputStream.toString().contains("don´t")); |
|
|
|
} |
|
|
|
|
|
|
|
private ByteArrayInputStream getEmptyStringInputStream() { |
|
|
|
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); |
|
|
|
} |
|
|
|
} |