KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > JaasSecurityDomainUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * 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.test.security.test;
23
24 import java.util.Arrays JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.crypto.spec.PBEParameterSpec;
29 import javax.crypto.spec.PBEKeySpec;
30 import javax.crypto.SecretKeyFactory;
31 import javax.crypto.SecretKey;
32 import javax.crypto.Cipher;
33
34 import org.jboss.test.JBossTestCase;
35 import org.jboss.test.JBossTestSetup;
36 import org.jboss.security.plugins.FilePassword;
37
38 import junit.extensions.TestSetup;
39 import junit.framework.Test;
40 import junit.framework.TestSuite;
41
42
43 /** Tests of the JaasSecurityDomain service.
44  
45  @author Scott.Stark@jboss.org
46  @version $Revision: 41577 $
47  */

48 public class JaasSecurityDomainUnitTestCase
49    extends JBossTestCase
50 {
51    public JaasSecurityDomainUnitTestCase(String JavaDoc name)
52    {
53       super(name);
54    }
55
56    public void testTmpFilePassword() throws Exception JavaDoc
57    {
58       ObjectName JavaDoc name = new ObjectName JavaDoc("jboss.security:service=JaasSecurityDomain,domain=testTmpFilePassword");
59       byte[] secret = "secret".getBytes();
60       Object JavaDoc[] args = {secret};
61       String JavaDoc[] sig = {secret.getClass().getName()};
62       byte[] encode = (byte[]) super.invoke(name, "encode", args, sig);
63       assertTrue("secret != encode", Arrays.equals(secret, encode) == false);
64
65       PBEParameterSpec cipherSpec = new PBEParameterSpec("abcdefgh".getBytes(), 13);
66       PBEKeySpec keySpec = new PBEKeySpec("password1".toCharArray());
67       SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEwithMD5andDES");
68       SecretKey cipherKey = factory.generateSecret(keySpec);
69       Cipher cipher = Cipher.getInstance("PBEwithMD5andDES");
70       cipher.init(Cipher.DECRYPT_MODE, cipherKey, cipherSpec);
71       byte[] decode = cipher.doFinal(encode);
72       assertTrue("secret == decode", Arrays.equals(secret, decode));
73    }
74
75    public void testFilePassword() throws Exception JavaDoc
76    {
77       ObjectName JavaDoc name = new ObjectName JavaDoc("jboss.security:service=JaasSecurityDomain,domain=testFilePassword");
78       byte[] secret = "secret".getBytes();
79       Object JavaDoc[] args = {secret};
80       String JavaDoc[] sig = {secret.getClass().getName()};
81       byte[] encode = (byte[]) super.invoke(name, "encode", args, sig);
82       assertTrue("secret != encode", Arrays.equals(secret, encode) == false);
83
84       PBEParameterSpec cipherSpec = new PBEParameterSpec("abcdefgh".getBytes(), 13);
85       PBEKeySpec keySpec = new PBEKeySpec("password2".toCharArray());
86       SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEwithMD5andDES");
87       SecretKey cipherKey = factory.generateSecret(keySpec);
88       Cipher cipher = Cipher.getInstance("PBEwithMD5andDES");
89       cipher.init(Cipher.DECRYPT_MODE, cipherKey, cipherSpec);
90       byte[] decode = cipher.doFinal(encode);
91       assertTrue("secret == decode", Arrays.equals(secret, decode));
92    }
93
94    public void testEncodeDecode() throws Exception JavaDoc
95    {
96       ObjectName JavaDoc name = new ObjectName JavaDoc("jboss.security:service=JaasSecurityDomain,domain=encode-decode");
97       byte[] secret = "secret".getBytes();
98       Object JavaDoc[] args = {secret};
99       String JavaDoc[] sig = {secret.getClass().getName()};
100       byte[] encode = (byte[]) super.invoke(name, "encode", args, sig);
101       assertTrue("secret != encode", Arrays.equals(secret, encode) == false);
102       args = new Object JavaDoc[]{encode};
103       byte[] decode = (byte[]) super.invoke(name, "decode", args, sig);
104       assertTrue("secret == decode", Arrays.equals(secret, decode));
105    }
106    public void testEncodeDecode64() throws Exception JavaDoc
107    {
108       ObjectName JavaDoc name = new ObjectName JavaDoc("jboss.security:service=JaasSecurityDomain,domain=encode-decode");
109       byte[] secret = "secret".getBytes();
110       Object JavaDoc[] args = {secret};
111       String JavaDoc[] sig = {secret.getClass().getName()};
112       String JavaDoc encode = (String JavaDoc) super.invoke(name, "encode64", args, sig);
113       Object JavaDoc[] args2 = {encode};
114       String JavaDoc[] sig2 = {"java.lang.String"};
115       byte[] decode = (byte[]) super.invoke(name, "decode64", args2, sig2);
116       assertTrue("secret == decode", Arrays.equals(secret, decode));
117    }
118
119    /**
120     * Setup the test suite.
121     */

122    public static Test suite() throws Exception JavaDoc
123    {
124       TestSuite suite = new TestSuite();
125       suite.addTest(new TestSuite(JaasSecurityDomainUnitTestCase.class));
126
127       // Create an initializer for the test suite
128
TestSetup wrapper = new JBossTestSetup(suite)
129       {
130          File JavaDoc tmpPassword;
131          File JavaDoc password;
132          protected void setUp() throws Exception JavaDoc
133          {
134             super.setUp();
135             // Create a tmp password file for testTmpFilePassword
136
tmpPassword = new File JavaDoc(System.getProperty("java.io.tmpdir"), "tmp.password");
137             FileWriter JavaDoc writer = new FileWriter JavaDoc(tmpPassword);
138             writer.write("password1");
139             writer.close();
140
141             // Create the opaque password file for testFilePassword
142
password = new File JavaDoc(System.getProperty("java.io.tmpdir")+ "/tst.password");
143             String JavaDoc[] args2 = {
144                "12345678", // salt
145
"17", // count
146
"password2", // password
147
password.getAbsolutePath() // password-file
148
};
149             FilePassword.main(args2);
150             getLog().info("Created password file: "+args2[2]);
151
152             String JavaDoc url = getResourceURL("security/jaassecdomain-tests-service.xml");
153             deploy(url);
154             flushAuthCache("unit-tests");
155          }
156          protected void tearDown() throws Exception JavaDoc
157          {
158             tmpPassword.delete();
159             password.delete();
160             String JavaDoc url = getResourceURL("security/jaassecdomain-tests-service.xml");
161             undeploy(url);
162             super.tearDown();
163          
164          }
165       };
166       return wrapper;
167    }
168
169 }
170
Popular Tags