KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > SecurityProviderlTestCase


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test;
8
9 import java.security.MessageDigest JavaDoc;
10 import java.security.Security JavaDoc;
11
12 import junit.extensions.TestSetup;
13 import junit.framework.Test;
14 import junit.framework.TestCase;
15 import junit.framework.TestSuite;
16
17 import org.jboss.crypto.JBossSXProvider;
18 import org.jboss.security.Util;
19
20 /** Tests of the org.jboss.crypto.* Java Cryptography Architecture plugin
21  classes
22  
23  @author Scott.Stark@jboss.org
24  @version $Revision: 1.1.26.1 $
25  */

26 public class SecurityProviderlTestCase extends TestCase
27 {
28    public SecurityProviderlTestCase(String JavaDoc name)
29    {
30       super(name);
31    }
32    
33    /** Compare Util.sessionKeyHash against the SHA-SRP MessageDigest. This
34     will not match the Util.sessionKeyHash as the algorithm described in
35     RFC2945 does not reverse the odd and even byte arrays as is done in
36     Util.sessionKeyHash.
37     */

38    public void testSHAInterleave() throws Exception JavaDoc
39    {
40       System.out.println("testSHAInterleave");
41       MessageDigest JavaDoc md = MessageDigest.getInstance("SHA-SRP");
42       byte[] test = "session_key".getBytes();
43
44       byte[] hash1 = Util.sessionKeyHash(test);
45       String JavaDoc hash1b64 = Util.encodeBase64(hash1);
46       System.out.println("hash1 = "+hash1b64);
47       byte[] hash2 = md.digest(test);
48       String JavaDoc hash2b64 = Util.encodeBase64(hash2);
49       System.out.println("hash2 = "+hash2b64);
50       super.assertTrue(hash1b64.equals(hash2b64) == false);
51    }
52    /** This should match the Util.sessionKeyHash
53     */

54    public void testSHAReverseInterleave() throws Exception JavaDoc
55    {
56       System.out.println("testSHAReverseInterleave");
57       MessageDigest JavaDoc md = MessageDigest.getInstance("SHA-SRP-Reverse");
58       byte[] test = "session_key".getBytes();
59
60       byte[] hash1 = Util.sessionKeyHash(test);
61       String JavaDoc hash1b64 = Util.encodeBase64(hash1);
62       System.out.println("hash1 = "+hash1b64);
63       byte[] hash2 = md.digest(test);
64       String JavaDoc hash2b64 = Util.encodeBase64(hash2);
65       System.out.println("hash2 = "+hash2b64);
66       super.assertEquals(hash1b64, hash2b64);
67    }
68
69    public static Test suite()
70    {
71       TestSuite suite = new TestSuite(SecurityProviderlTestCase.class);
72
73       // Create an initializer for the test suite
74
TestSetup wrapper = new TestSetup(suite)
75       {
76          protected void setUp() throws Exception JavaDoc
77          {
78             Util.init();
79             JBossSXProvider provider = new JBossSXProvider();
80             Security.addProvider(provider);
81          }
82          protected void tearDown() throws Exception JavaDoc
83          {
84             Security.removeProvider(JBossSXProvider.PROVIDER_NAME);
85          }
86       };
87       return wrapper;
88    }
89
90    public static void main(java.lang.String JavaDoc[] args)
91    {
92       System.setErr(System.out);
93       Test suite = suite();
94       junit.textui.TestRunner.run(suite);
95    }
96 }
97
Popular Tags