You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
255 lines
8.1 KiB
255 lines
8.1 KiB
import org.junit.jupiter.api.BeforeAll;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
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 {
|
|
|
|
static Vault vlt;
|
|
|
|
@BeforeAll
|
|
static void init() {
|
|
vlt = new Vault();
|
|
}
|
|
|
|
@BeforeEach
|
|
void reset() {
|
|
vlt.outputS = System.out;
|
|
vlt.inputS = getEmptyStringInputStream();
|
|
}
|
|
|
|
@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("Add"));
|
|
}*/
|
|
|
|
/*@Test
|
|
void openShowCredentialMenuItem() {
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
vlt.inputS = new ByteArrayInputStream("c\n1".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\n1\nu\npeter".getBytes(StandardCharsets.UTF_8));
|
|
vlt.outputS = outputStream;
|
|
vlt.credentialMenu();
|
|
assertTrue(outputStream.toString().contains("Type"));
|
|
assertEquals(1,vlt.newID);
|
|
assertTrue(vlt.decision);
|
|
assertEquals("peter",vlt.newString);
|
|
}*/
|
|
|
|
@Test
|
|
void editCredentialTest() {
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
vlt.outputS = outputStream;
|
|
vlt.inputS = new ByteArrayInputStream("1\np\npassword".getBytes(StandardCharsets.UTF_8));
|
|
vlt.editCredential();
|
|
|
|
assertEquals(1, vlt.newID);
|
|
assertEquals("password", vlt.newString);
|
|
|
|
|
|
vlt.inputS = new ByteArrayInputStream("1\nu\npeter".getBytes(StandardCharsets.UTF_8));
|
|
vlt.editCredential();
|
|
|
|
assertEquals(1, vlt.newID);
|
|
assertEquals("peter", vlt.newString);
|
|
|
|
}
|
|
|
|
@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("ID"));
|
|
|
|
vlt.inputS = new ByteArrayInputStream("peter".getBytes(StandardCharsets.UTF_8));
|
|
vlt.credentialMenu();
|
|
|
|
assertTrue(outputStream.toString().contains("name"));
|
|
}
|
|
|
|
@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("anymore"));
|
|
}
|
|
|
|
@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("anymore"));
|
|
}
|
|
|
|
@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("anymore"));
|
|
}
|
|
|
|
private ByteArrayInputStream getEmptyStringInputStream() {
|
|
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
}
|