/domain-management/src/test/java/org/jboss/as/domain/management/security/state/WeakCheckStateTestCase.java

https://github.com/maschmid/jboss-as · Java · 224 lines · 147 code · 51 blank · 26 comment · 1 complexity · 44a8fb88572e0eae4adf4ac37b49cea5 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2012, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.domain.management.security.state;
  23. import org.jboss.as.domain.management.security.AssertConsoleBuilder;
  24. import org.jboss.msc.service.StartException;
  25. import org.junit.Test;
  26. import java.io.IOException;
  27. import static org.jboss.as.domain.management.DomainManagementMessages.MESSAGES;
  28. import static org.junit.Assert.assertTrue;
  29. /**
  30. * Test the password weakness
  31. *
  32. * @author <a href="mailto:flemming.harms@gmail.com">Flemming Harms</a>
  33. */
  34. public class WeakCheckStateTestCase extends PropertyTestHelper {
  35. @Test
  36. public void testState() throws IOException, StartException {
  37. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  38. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder();
  39. consoleMock.setResponses(consoleBuilder);
  40. State duplicateUserCheckState = weakCheckState.execute();
  41. assertTrue("Expected the next state to be DuplicateUserCheckState", duplicateUserCheckState instanceof DuplicateUserCheckState);
  42. consoleBuilder.validate();
  43. }
  44. @Test
  45. public void testWrongPassword() {
  46. values.setUserName("thesame");
  47. values.setPassword("thesame".toCharArray());
  48. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  49. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().expectedErrorMessage(MESSAGES.usernamePasswordMatch());
  50. consoleMock.setResponses(consoleBuilder);
  51. State errorState = weakCheckState.execute();
  52. assertTrue("Expected the next state to be ErrorState", errorState instanceof ErrorState);
  53. State promptNewUserState = errorState.execute();
  54. assertTrue("Expected the next state to be PromptNewUserState", promptNewUserState instanceof PromptNewUserState);
  55. consoleBuilder.validate();
  56. }
  57. @Test
  58. public void testForbiddenPassword() {
  59. values.setUserName("willFail");
  60. values.setPassword("administrator".toCharArray());
  61. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  62. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().expectedErrorMessage(MESSAGES.passwordMustNotBeEqual("administrator"));
  63. consoleMock.setResponses(consoleBuilder);
  64. State errorState = weakCheckState.execute();
  65. assertTrue("Expected the next state to be ErrorState", errorState instanceof ErrorState);
  66. State promptNewUserState = errorState.execute();
  67. assertTrue("Expected the next state to be PromptNewUserState", promptNewUserState instanceof PromptNewUserState);
  68. consoleBuilder.validate();
  69. }
  70. @Test
  71. public void testWeakPassword() {
  72. values.setUserName("willFail");
  73. values.setPassword("zxcvbnm1@".toCharArray());
  74. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  75. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().expectedErrorMessage(MESSAGES.passwordNotStrongEnough("MODERATE", "MEDIUM"));
  76. consoleMock.setResponses(consoleBuilder);
  77. State errorState = weakCheckState.execute();
  78. assertTrue("Expected the next state to be ErrorState", errorState instanceof ErrorState);
  79. State promptNewUserState = errorState.execute();
  80. assertTrue("Expected the next state to be PromptNewUserState", promptNewUserState instanceof PromptNewUserState);
  81. consoleBuilder.validate();
  82. }
  83. @Test
  84. public void testTooShortPassword() {
  85. values.setUserName("willFail");
  86. values.setPassword("1QwD%rf".toCharArray());
  87. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  88. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().expectedErrorMessage(MESSAGES.passwordNotLongEnough(8));
  89. consoleMock.setResponses(consoleBuilder);
  90. State errorState = weakCheckState.execute();
  91. assertTrue("Expected the next state to be ErrorState", errorState instanceof ErrorState);
  92. State promptNewUserState = errorState.execute();
  93. assertTrue("Expected the next state to be PromptNewUserState", promptNewUserState instanceof PromptNewUserState);
  94. consoleBuilder.validate();
  95. }
  96. @Test
  97. public void testNoDigitInPassword() {
  98. values.setUserName("willFail");
  99. values.setPassword("!QwD%rGf".toCharArray());
  100. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  101. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().expectedErrorMessage(MESSAGES.passwordMustHaveDigit());
  102. consoleMock.setResponses(consoleBuilder);
  103. State errorState = weakCheckState.execute();
  104. assertTrue("Expected the next state to be ErrorState", errorState instanceof ErrorState);
  105. State promptNewUserState = errorState.execute();
  106. assertTrue("Expected the next state to be PromptNewUserState", promptNewUserState instanceof PromptNewUserState);
  107. consoleBuilder.validate();
  108. }
  109. @Test
  110. public void testNoSymbolInPassword() {
  111. values.setUserName("willFail");
  112. values.setPassword("1QwD5rGf".toCharArray());
  113. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  114. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().expectedErrorMessage(MESSAGES.passwordMustHaveSymbol());
  115. consoleMock.setResponses(consoleBuilder);
  116. State errorState = weakCheckState.execute();
  117. assertTrue("Expected the next state to be ErrorState", errorState instanceof ErrorState);
  118. State promptNewUserState = errorState.execute();
  119. assertTrue("Expected the next state to be PromptNewUserState", promptNewUserState instanceof PromptNewUserState);
  120. consoleBuilder.validate();
  121. }
  122. @Test
  123. public void testNoAlphaInPassword() {
  124. values.setUserName("willFail");
  125. values.setPassword("1$*>5&#}".toCharArray());
  126. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  127. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().expectedErrorMessage(MESSAGES.passwordMustHaveAlpha());
  128. consoleMock.setResponses(consoleBuilder);
  129. State errorState = weakCheckState.execute();
  130. assertTrue("Expected the next state to be ErrorState", errorState instanceof ErrorState);
  131. State promptNewUserState = errorState.execute();
  132. assertTrue("Expected the next state to be PromptNewUserState", promptNewUserState instanceof PromptNewUserState);
  133. consoleBuilder.validate();
  134. }
  135. @Test
  136. public void testUsernameNotAlphaNumeric() {
  137. values.setUserName("username&");
  138. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  139. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().expectedErrorMessage(MESSAGES.usernameNotAlphaNumeric());
  140. consoleMock.setResponses(consoleBuilder);
  141. State errorState = weakCheckState.execute();
  142. assertTrue("Expected the next state to be ErrorState", errorState instanceof ErrorState);
  143. State promptNewUserState = errorState.execute();
  144. assertTrue("Expected the next state to be PromptNewUserState", promptNewUserState instanceof PromptNewUserState);
  145. consoleBuilder.validate();
  146. }
  147. @Test
  148. public void testBadUsername() {
  149. String[] BAD_USER_NAMES = {"admin", "administrator", "root"};
  150. for (String userName : BAD_USER_NAMES) {
  151. values.setUserName(userName);
  152. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  153. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder().
  154. expectedConfirmMessage(MESSAGES.usernameEasyToGuess(userName), MESSAGES.sureToAddUser(userName), "n");
  155. consoleMock.setResponses(consoleBuilder);
  156. State confirmationChoice = weakCheckState.execute();
  157. assertTrue("Expected the next state to be ConfirmationChoice", confirmationChoice instanceof ConfirmationChoice);
  158. State promptNewUserState = confirmationChoice.execute();
  159. assertTrue("Expected the next state to be PromptNewUserState", promptNewUserState instanceof PromptNewUserState);
  160. consoleBuilder.validate();
  161. }
  162. }
  163. @Test
  164. public void testUsernameWithValidPunctuation() {
  165. values.setUserName("username.@\\=,/");
  166. WeakCheckState weakCheckState = new WeakCheckState(consoleMock, values);
  167. AssertConsoleBuilder consoleBuilder = new AssertConsoleBuilder();
  168. consoleMock.setResponses(consoleBuilder);
  169. State duplicateUserCheckState = weakCheckState.execute();
  170. assertTrue("Expected the next state to be DuplicateUserCheckState", duplicateUserCheckState instanceof DuplicateUserCheckState);
  171. consoleBuilder.validate();
  172. }
  173. }