001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.security.test;
023:
024: import java.io.File;
025: import java.io.FileWriter;
026: import java.util.Arrays;
027:
028: import org.jboss.security.Util;
029: import org.jboss.security.plugins.FilePassword;
030: import org.jboss.test.BaseTestCase;
031: import org.jboss.util.StringPropertyReplacer;
032:
033: /**
034: org.jboss.security.Util tests
035:
036: @author Scott.Stark@jboss.org
037: @version $Revision: 57211 $
038: */
039: public class SecurityUtilUnitTestCase extends BaseTestCase {
040: private File tmpPassword;
041: private File password;
042:
043: public SecurityUtilUnitTestCase(String name) {
044: super (name);
045: }
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049: // Create a tmp password file for testTmpFilePassword
050: tmpPassword = new File(System.getProperty("java.io.tmpdir"),
051: "tmp.password");
052: FileWriter writer = new FileWriter(tmpPassword);
053: writer.write("password1");
054: writer.close();
055:
056: // Create the opaque password file for testFilePassword
057: password = new File(System.getProperty("java.io.tmpdir")
058: + "/tst.password");
059: String[] args2 = { "12345678", // salt
060: "17", // count
061: "password2", // password
062: password.getAbsolutePath() // password-file
063: };
064: FilePassword.main(args2);
065: getLog().info("Created password file: " + args2[2]);
066: }
067:
068: protected void tearDown() throws Exception {
069: tmpPassword.delete();
070: password.delete();
071: super .tearDown();
072: }
073:
074: /**
075: * Test {CLASS}org.jboss.security.plugins.TmpFilePassword
076: * @throws Exception
077: */
078: public void testTmpFilePassword() throws Exception {
079: String passwordCmd = "{CLASS}org.jboss.security.plugins.TmpFilePassword:${java.io.tmpdir}/tmp.password,5000";
080: passwordCmd = StringPropertyReplacer
081: .replaceProperties(passwordCmd);
082: char[] password = Util.loadPassword(passwordCmd);
083: assertTrue("password1", Arrays.equals(password, "password1"
084: .toCharArray()));
085: }
086:
087: /**
088: * Test {CLASS}org.jboss.security.plugins.FilePassword
089: * @throws Exception
090: */
091: public void testFilePassword() throws Exception {
092: String passwordCmd = "{CLASS}org.jboss.security.plugins.FilePassword:${java.io.tmpdir}/tst.password";
093: passwordCmd = StringPropertyReplacer
094: .replaceProperties(passwordCmd);
095: char[] password = Util.loadPassword(passwordCmd);
096: assertTrue("password2", Arrays.equals(password, "password2"
097: .toCharArray()));
098: }
099:
100: /**
101: * Test {EXT}org.jboss.test.security.test.ExecPasswordCmd
102: * @throws Exception
103: */
104: public void testExtPassword() throws Exception {
105: // First check for java.exe or java as the binary
106: File java = new File(System.getProperty("java.home"),
107: "/bin/java");
108: File javaExe = new File(System.getProperty("java.home"),
109: "/bin/java.exe");
110: String jre;
111: if (java.exists())
112: jre = java.getAbsolutePath();
113: else
114: jre = javaExe.getAbsolutePath();
115: // Build the command to run this jre
116: String cmd = jre + " -cp "
117: + System.getProperty("java.class.path")
118: + " org.jboss.test.security.test.ExecPasswordCmd";
119: String passwordCmd = "{EXT}" + cmd;
120: char[] password = Util.loadPassword(passwordCmd);
121: assertTrue("password3", Arrays.equals(password, "password3"
122: .toCharArray()));
123: }
124:
125: }
|