PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/domain-management/src/test/java/org/jboss/as/domain/management/security/adduser/PropertyTestHelper.java

https://github.com/dbuos/wildfly
Java | 299 lines | 160 code | 33 blank | 106 comment | 13 complexity | 4f34b59eb724b67fcefb73872535d8ec MD5 | raw file
Possible License(s): Apache-2.0
  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.adduser;
  23. import org.jboss.as.domain.management.security.PropertiesFileLoader;
  24. import org.jboss.msc.service.StartException;
  25. import org.junit.Before;
  26. import java.io.BufferedReader;
  27. import java.io.Closeable;
  28. import java.io.File;
  29. import java.io.FileReader;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.Properties;
  34. import java.util.regex.Matcher;
  35. import static org.junit.Assert.assertEquals;
  36. import static org.junit.Assert.assertNotNull;
  37. import static org.junit.Assert.assertTrue;
  38. /**
  39. * Helper for setting up a test case with ConsoleMock, StateValues and
  40. * property files for user and roles
  41. *
  42. * @author <a href="mailto:flemming.harms@gmail.com">Flemming Harms</a>
  43. */
  44. public class PropertyTestHelper {
  45. protected static final String USER_NAME = "Aldo.Raine";
  46. protected static final String ROLES = "admin, jms";
  47. protected ConsoleMock consoleMock;
  48. protected StateValues values;
  49. @Before
  50. public void setUp() throws IOException {
  51. ArrayList<File> usersPropertyFileList = new ArrayList<File>();
  52. ArrayList<File> rolesPropertyFileList = new ArrayList<File>();
  53. File usersPropertyFile = File.createTempFile("UpdateUser", null);
  54. usersPropertyFile.deleteOnExit();
  55. File rolesPropertyFile = File.createTempFile("UpdateRoles", null);
  56. rolesPropertyFile.deleteOnExit();
  57. usersPropertyFileList.add(usersPropertyFile);
  58. rolesPropertyFileList.add(rolesPropertyFile);
  59. values = new StateValues();
  60. values.setUserFiles(usersPropertyFileList);
  61. values.setGroupFiles(rolesPropertyFileList);
  62. values.setUserName(USER_NAME);
  63. values.setPassword("1sT%l<[pzD");
  64. values.setRealm("Management");
  65. consoleMock = new ConsoleMock();
  66. }
  67. protected Properties loadProperties(String filePath) throws StartException, IOException {
  68. PropertiesFileLoader propertiesLoad = new PropertiesFileLoader(filePath);
  69. propertiesLoad.start(null);
  70. Properties properties = (Properties) propertiesLoad.getProperties().clone();
  71. propertiesLoad.stop(null);
  72. return properties;
  73. }
  74. protected void assertUserPropertyFile(String userName) throws StartException, IOException {
  75. assertUserPropertyFile(userName, null);
  76. }
  77. protected void assertUserPropertyFile(String userName, String expectedPassword) throws StartException, IOException {
  78. assertUserPropertyFile(userName, expectedPassword, values.getOptions().isDisable());
  79. }
  80. private void assertUserPropertyFile(String userName, String expectedPassword, boolean disable) throws StartException, IOException {
  81. final String password;
  82. password = getPassword(userName, disable);
  83. assertNotNull(password);
  84. if (expectedPassword != null) {
  85. assertEquals(expectedPassword, password);
  86. }
  87. }
  88. protected String getPassword(String userName) throws IOException, StartException {
  89. return getValueFromFile(userName, values.getUserFiles().get(0).getAbsolutePath(), false);
  90. }
  91. private String getPassword(String userName, boolean disable) throws IOException, StartException {
  92. String password;
  93. if (disable) {
  94. // Read the file line by line and return the password of the user
  95. password = getDisabledUserPassword(userName);
  96. } else {
  97. // Load the properties and return the password of the user
  98. password = getEnabledUserPassword(userName);
  99. }
  100. return password;
  101. }
  102. protected void assertRolePropertyFile(String userName) throws StartException, IOException {
  103. assertRolePropertyFile(userName, values.getGroups());
  104. }
  105. protected void assertRolePropertyFile(String userName, String expectedRoles) throws StartException, IOException {
  106. assertRolePropertyFile(userName, expectedRoles, values.getOptions().isDisable());
  107. }
  108. private void assertRolePropertyFile(String userName, String expectedRoles, boolean disable) throws StartException, IOException {
  109. final String roles;
  110. roles = getRoles(userName, disable);
  111. assertEquals(expectedRoles, roles);
  112. }
  113. protected String getRoles(String userName) throws IOException, StartException {
  114. return getValueFromFile(userName, values.getGroupFiles().get(0).getAbsolutePath(), false);
  115. }
  116. private String getRoles(String userName, boolean disable) throws IOException, StartException {
  117. String roles;
  118. if (disable) {
  119. // Read the file line by line and return the roles of the user
  120. roles = getDisabledUserRoles(userName);
  121. } else {
  122. // Load the properties and return the roles of the user
  123. roles = getEnabledUserRoles(userName);
  124. }
  125. return roles;
  126. }
  127. /**
  128. * Get the password of the enabled user.<br/>
  129. * Read the properties users file and return the password of the user
  130. *
  131. * @param userName The name of the user
  132. * @return The password of the user
  133. * @throws StartException
  134. * @throws IOException
  135. */
  136. protected String getEnabledUserPassword(String userName) throws StartException, IOException {
  137. return getValueFromProperty(userName, values.getUserFiles().get(0).getAbsolutePath());
  138. }
  139. /**
  140. * Get the roles of the enabled user.<br/>
  141. * Read the properties roles file and return the roles of the user
  142. *
  143. * @param userName The name of the user
  144. * @return The roles of the user
  145. * @throws StartException
  146. * @throws IOException
  147. */
  148. protected String getEnabledUserRoles(String userName) throws StartException, IOException {
  149. return getValueFromProperty(userName, values.getGroupFiles().get(0).getAbsolutePath());
  150. }
  151. /**
  152. * Load properties from the file and return the value of the username property
  153. *
  154. * @param userName The name of the user
  155. * @param filePath The file path
  156. * @return The value of the username property
  157. * @throws StartException
  158. * @throws IOException
  159. */
  160. private String getValueFromProperty(String userName, String filePath) throws StartException, IOException {
  161. Properties properties = loadProperties(filePath);
  162. return (String) properties.get(userName);
  163. }
  164. /**
  165. * Get the password of the disabled user.<br/>
  166. * Read the users file line by line and return the password of the user.
  167. *
  168. * @param userName The name of the user
  169. * @return The password of the user
  170. * @throws IOException
  171. * @see #getDisabledValueFromFile(String, String)
  172. */
  173. private String getDisabledUserPassword(String userName) throws IOException {
  174. return getDisabledValueFromFile(userName, values.getUserFiles().get(0).getAbsolutePath());
  175. }
  176. /**
  177. * Get the roles of the disabled user.<br/>
  178. * Read the roles file line by line and return the roles of the user.
  179. *
  180. * @param userName The name of the user
  181. * @return The roles of the user
  182. * @throws IOException
  183. */
  184. private String getDisabledUserRoles(String userName) throws IOException {
  185. return getDisabledValueFromFile(userName, values.getGroupFiles().get(0).getAbsolutePath());
  186. }
  187. /**
  188. * Count the number of lines in the users file.
  189. *
  190. * @return The number of lines in the file
  191. * @throws IOException
  192. */
  193. protected int countLineNumberUserFile() throws IOException {
  194. return readContent(values.getUserFiles().get(0).getAbsolutePath()).size();
  195. }
  196. /**
  197. * Count the number of lines in the roles file
  198. *
  199. * @return The number of lines in the file
  200. * @throws IOException
  201. */
  202. protected int countLineNumberRoleFile() throws IOException {
  203. return readContent(values.getGroupFiles().get(0).getAbsolutePath()).size();
  204. }
  205. /**
  206. * Get the value of the disabled username line.<br/>
  207. * Read the file line by line and return the disabled value of the username line.
  208. *
  209. * @param userName The name of the user
  210. * @param filePath The file path
  211. * @return The disabled value of the username line
  212. * @throws IOException
  213. */
  214. private String getDisabledValueFromFile(String userName, String filePath) throws IOException {
  215. return getValueFromFile(userName, filePath, true);
  216. }
  217. /**
  218. * Get the value of the username line.<br/>
  219. * Read the file line by line and return the value of the username line.
  220. *
  221. * @param userName The name of the user
  222. * @param filePath The file path
  223. * @param onlyDisabledLine return only disabled line
  224. * @return The value of the username line
  225. * @throws IOException
  226. */
  227. private String getValueFromFile(String userName, String filePath, boolean onlyDisabledLine) throws IOException {
  228. List<String> content = readContent(filePath);
  229. boolean found = false;
  230. String value = null;
  231. for (String line : content) {
  232. String trimmed = line.trim();
  233. Matcher matcher = PropertiesFileLoader.PROPERTY_PATTERN.matcher(trimmed);
  234. if (!onlyDisabledLine || trimmed.startsWith("#")) {
  235. if (matcher.matches() && userName.equals(matcher.group(1))) {
  236. found = true;
  237. value = matcher.group(2);
  238. break;
  239. }
  240. }
  241. }
  242. assertTrue(found);
  243. return value;
  244. }
  245. private List<String> readContent(String filePath) throws IOException {
  246. List<String> content = new ArrayList<String>();
  247. FileReader fileReader = new FileReader(filePath);
  248. BufferedReader bufferedFileReader = new BufferedReader(fileReader);
  249. try {
  250. String line;
  251. while ((line = bufferedFileReader.readLine()) != null) {
  252. content.add(line);
  253. }
  254. } finally {
  255. safeClose(bufferedFileReader);
  256. safeClose(fileReader);
  257. }
  258. return content;
  259. }
  260. private void safeClose(final Closeable c) {
  261. try {
  262. c.close();
  263. } catch (IOException ignored) {
  264. }
  265. }
  266. }