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.

148 lines
4.7 KiB

  1. import org.junit.jupiter.api.BeforeAll;
  2. import org.junit.jupiter.api.BeforeEach;
  3. import org.junit.jupiter.api.Test;
  4. import org.mockito.Mockito;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.ByteArrayOutputStream;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.NoSuchElementException;
  9. import static org.junit.jupiter.api.Assertions.*;
  10. public class VaultTest {
  11. //Vault vlt = new Vault();
  12. static Vault vlt;
  13. @BeforeAll
  14. static void init() {
  15. vlt = new Vault();
  16. }
  17. @BeforeEach
  18. void reset() {
  19. vlt.outputS = System.out;
  20. vlt.inputS = System.in;
  21. }
  22. @Test
  23. void addCredential() {vlt.addCredential();}
  24. @Test
  25. void showCredential() {vlt.showCredential();}
  26. @Test
  27. void getAsJson() {vlt.getAsJson();}
  28. @Test
  29. void loadFromJson() {vlt.loadFromJson();}
  30. @Test
  31. void openConfigureMenu() {
  32. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  33. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  34. vlt.outputS = outputStream;
  35. vlt.configure();
  36. assertTrue(outputStream.toString().startsWith("Configure:"));
  37. }
  38. @Test
  39. void exitConfigurationMenu(){
  40. vlt.inputS = new ByteArrayInputStream("e".getBytes(StandardCharsets.UTF_8));
  41. vlt.configure();
  42. assertFalse(vlt.config);
  43. }
  44. @Test
  45. void doNotExitConfigAfterWrongInput() {
  46. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  47. vlt.configure();
  48. assertTrue(vlt.config);
  49. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  50. vlt.inputS = getEmptyStringInputStream();
  51. vlt.outputS = outputStream;
  52. assertThrowsExactly(NoSuchElementException.class, () -> vlt.configure());
  53. assertTrue(outputStream.toString().startsWith("Configure:"));
  54. }
  55. @Test
  56. void openSetPWLength() {
  57. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  58. vlt.inputS = new ByteArrayInputStream("12".getBytes(StandardCharsets.UTF_8));
  59. vlt.outputS = outputStream;
  60. vlt.setPWLength();
  61. assertTrue(outputStream.toString().startsWith("Set"));
  62. }
  63. @Test
  64. void setPWLength() {
  65. vlt.inputS = new ByteArrayInputStream("7\n".getBytes(StandardCharsets.UTF_8));
  66. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  67. vlt.outputS = outputStream;
  68. vlt.setPWLength();
  69. assertEquals(7, vlt.PWLength);
  70. assertTrue(outputStream.toString().contains("now:"));
  71. }
  72. @Test
  73. void setPWLengthWrongInput() {
  74. vlt.inputS = new ByteArrayInputStream("T\n".getBytes(StandardCharsets.UTF_8));
  75. vlt.setPWLength();
  76. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  77. vlt.inputS = getEmptyStringInputStream();
  78. vlt.outputS = outputStream;
  79. assertThrowsExactly(NoSuchElementException.class, () -> vlt.setPWLength());
  80. assertTrue(outputStream.toString().startsWith("Set"));
  81. }
  82. @Test
  83. void setCapital() {
  84. vlt.inputS = new ByteArrayInputStream("yes\n".getBytes(StandardCharsets.UTF_8));
  85. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  86. vlt.outputS = outputStream;
  87. vlt.setCapital();
  88. assertTrue(outputStream.toString().startsWith("Should"));
  89. assertTrue(vlt.haveCapitals = true);
  90. assertTrue(outputStream.toString().contains("contain"));
  91. vlt.inputS = new ByteArrayInputStream("no\n".getBytes(StandardCharsets.UTF_8));
  92. vlt.outputS = outputStream;
  93. vlt.setCapital();
  94. assertTrue(outputStream.toString().startsWith("Should"));
  95. assertFalse(vlt.haveCapitals = false);
  96. assertTrue(outputStream.toString().contains("don´t"));
  97. }
  98. @Test
  99. void setSpecialCharTest() {
  100. vlt.inputS = new ByteArrayInputStream("yes\n".getBytes(StandardCharsets.UTF_8));
  101. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  102. vlt.outputS = outputStream;
  103. vlt.setSpecialChar();
  104. assertTrue(outputStream.toString().startsWith("Should"));
  105. assertTrue(vlt.hasSpecialChars = true);
  106. assertTrue(outputStream.toString().contains("contain"));
  107. vlt.inputS = new ByteArrayInputStream("no\n".getBytes(StandardCharsets.UTF_8));
  108. vlt.outputS = outputStream;
  109. vlt.setSpecialChar();
  110. assertTrue(outputStream.toString().startsWith("Should"));
  111. assertFalse(vlt.hasSpecialChars = false);
  112. assertTrue(outputStream.toString().contains("don´t"));
  113. }
  114. private ByteArrayInputStream getEmptyStringInputStream() {
  115. return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
  116. }
  117. }