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.

242 lines
7.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 openCredentialMenu() {
  24. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  25. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  26. vlt.outputS = outputStream;
  27. vlt.credentialMenu();
  28. assertTrue(vlt.credentialM);
  29. assertTrue(outputStream.toString().startsWith("Configure"));
  30. }
  31. @Test
  32. void exitConfigureMenu(){
  33. vlt.inputS = new ByteArrayInputStream("e".getBytes(StandardCharsets.UTF_8));
  34. vlt.credentialMenu();
  35. assertFalse(vlt.credentialM);
  36. }
  37. @Test
  38. void openAddCredentialMenuItem() {
  39. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  40. vlt.inputS = new ByteArrayInputStream("a\npeter\npassword".getBytes(StandardCharsets.UTF_8));
  41. vlt.outputS = outputStream;
  42. vlt.credentialMenu();
  43. assertTrue(outputStream.toString().contains("username"));
  44. assertEquals("peter", vlt.userName);
  45. assertTrue(outputStream.toString().contains("password"));
  46. assertEquals("password",vlt.password);
  47. }
  48. /*@Test
  49. void openShowCredentialMenuItem() {
  50. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  51. vlt.inputS = new ByteArrayInputStream("c".getBytes(StandardCharsets.UTF_8));
  52. vlt.outputS = outputStream;
  53. vlt.credentialMenu();
  54. assertTrue(outputStream.toString().contains("Type"));
  55. }
  56. @Test
  57. void openEditCredentialMenuItem() {
  58. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  59. vlt.inputS = new ByteArrayInputStream("l".getBytes(StandardCharsets.UTF_8));
  60. vlt.outputS = outputStream;
  61. vlt.credentialMenu();
  62. assertTrue(outputStream.toString().contains("Type"));
  63. }
  64. @Test
  65. void addCredentialTest() {
  66. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  67. vlt.outputS = outputStream;
  68. vlt.inputS = new ByteArrayInputStream("peter\npassword".getBytes(StandardCharsets.UTF_8));
  69. vlt.addCredential();
  70. assertTrue(outputStream.toString().contains("username"));
  71. assertEquals("peter", vlt.userName);
  72. assertTrue(outputStream.toString().contains("password"));
  73. assertEquals("password",vlt.password);
  74. }*/
  75. @Test
  76. void showCredentialTest() {
  77. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  78. vlt.outputS = outputStream;
  79. vlt.inputS = new ByteArrayInputStream("1".getBytes(StandardCharsets.UTF_8));
  80. vlt.showCredential();
  81. assertTrue(outputStream.toString().contains("Type"));
  82. assertTrue(vlt.isInt);
  83. vlt.outputS = outputStream;
  84. vlt.inputS = new ByteArrayInputStream("peter".getBytes(StandardCharsets.UTF_8));
  85. vlt.showCredential();
  86. assertTrue(outputStream.toString().contains("Type"));
  87. assertFalse(vlt.isInt);
  88. }
  89. @Test
  90. void getAsJson() {vlt.getAsJson();}
  91. @Test
  92. void loadFromJson() {vlt.loadFromJson();}
  93. @Test
  94. void openConfigureMenu() {
  95. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  96. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  97. vlt.outputS = outputStream;
  98. vlt.configure();
  99. assertTrue(outputStream.toString().startsWith("Configure:"));
  100. }
  101. @Test
  102. void exitConfigurationMenu(){
  103. vlt.inputS = new ByteArrayInputStream("e".getBytes(StandardCharsets.UTF_8));
  104. vlt.configure();
  105. assertFalse(vlt.config);
  106. }
  107. @Test
  108. void doNotExitConfigAfterWrongInput() {
  109. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  110. vlt.configure();
  111. assertTrue(vlt.config);
  112. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  113. vlt.inputS = getEmptyStringInputStream();
  114. vlt.outputS = outputStream;
  115. assertThrowsExactly(NoSuchElementException.class, () -> vlt.configure());
  116. assertTrue(outputStream.toString().startsWith("Configure:"));
  117. }
  118. @Test
  119. void openSetPWLengthMenuItem() {
  120. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  121. vlt.inputS = new ByteArrayInputStream("l\n7".getBytes(StandardCharsets.UTF_8));
  122. vlt.outputS = outputStream;
  123. vlt.configure();
  124. assertEquals(7, vlt.PWLength);
  125. assertTrue(outputStream.toString().contains("Set"));
  126. }
  127. @Test
  128. void setPWLength() {
  129. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  130. vlt.outputS = outputStream;
  131. vlt.setPWLength("7");
  132. assertEquals(7, vlt.PWLength);
  133. assertTrue(outputStream.toString().contains("now:"));
  134. }
  135. @Test
  136. void openSetCapitalsMenuItem() {
  137. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  138. vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
  139. vlt.outputS = outputStream;
  140. vlt.configure();
  141. assertTrue(vlt.haveCapitals = true);
  142. }
  143. @Test
  144. void setCapitals() {
  145. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  146. vlt.outputS = outputStream;
  147. vlt.setCapital("yes");
  148. assertTrue(vlt.haveCapitals = true);
  149. assertTrue(outputStream.toString().contains("contain"));
  150. vlt.outputS = outputStream;
  151. vlt.setCapital("no");
  152. assertFalse(vlt.haveCapitals = false);
  153. assertTrue(outputStream.toString().contains("don´t"));
  154. }
  155. @Test
  156. void openSetSpecialCharsMenuItem() {
  157. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  158. vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
  159. vlt.outputS = outputStream;
  160. vlt.configure();
  161. assertTrue(vlt.hasSpecialChars = true);
  162. }
  163. @Test
  164. void setSpecialCharTest() {
  165. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  166. vlt.outputS = outputStream;
  167. vlt.setSpecialChar("yes");
  168. assertTrue(vlt.hasSpecialChars = true);
  169. vlt.outputS = outputStream;
  170. vlt.setSpecialChar("no");
  171. assertFalse(vlt.hasSpecialChars = false);
  172. assertTrue(outputStream.toString().contains("don´t"));
  173. }
  174. @Test
  175. void openSetNumbersMenuItem() {
  176. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  177. vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
  178. vlt.outputS = outputStream;
  179. vlt.configure();
  180. assertTrue(vlt.hasNumbers = true);
  181. }
  182. @Test
  183. void setNumbersTest() {
  184. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  185. vlt.outputS = outputStream;
  186. vlt.setNumbers("yes");
  187. assertTrue(vlt.hasNumbers = true);
  188. assertTrue(outputStream.toString().contains("contain"));
  189. vlt.outputS = outputStream;
  190. vlt.setNumbers("no");
  191. assertFalse(vlt.hasNumbers = false);
  192. assertTrue(outputStream.toString().contains("don´t"));
  193. }
  194. private ByteArrayInputStream getEmptyStringInputStream() {
  195. return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
  196. }
  197. }