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;
023:
024: import java.security.MessageDigest;
025: import java.security.Security;
026:
027: import junit.extensions.TestSetup;
028: import junit.framework.Test;
029: import junit.framework.TestCase;
030: import junit.framework.TestSuite;
031:
032: import org.jboss.crypto.JBossSXProvider;
033: import org.jboss.security.Util;
034:
035: /** Tests of the org.jboss.crypto.* Java Cryptography Architecture plugin
036: classes
037:
038: @author Scott.Stark@jboss.org
039: @version $Revision: 37390 $
040: */
041: public class SecurityProviderlTestCase extends TestCase {
042: public SecurityProviderlTestCase(String name) {
043: super (name);
044: }
045:
046: /** Compare Util.sessionKeyHash against the SHA-SRP MessageDigest. This
047: will not match the Util.sessionKeyHash as the algorithm described in
048: RFC2945 does not reverse the odd and even byte arrays as is done in
049: Util.sessionKeyHash.
050: */
051: public void testSHAInterleave() throws Exception {
052: System.out.println("testSHAInterleave");
053: MessageDigest md = MessageDigest.getInstance("SHA-SRP");
054: byte[] test = "session_key".getBytes();
055:
056: byte[] hash1 = Util.sessionKeyHash(test);
057: String hash1b64 = Util.encodeBase64(hash1);
058: System.out.println("hash1 = " + hash1b64);
059: byte[] hash2 = md.digest(test);
060: String hash2b64 = Util.encodeBase64(hash2);
061: System.out.println("hash2 = " + hash2b64);
062: super .assertTrue(hash1b64.equals(hash2b64) == false);
063: }
064:
065: /** This should match the Util.sessionKeyHash
066: */
067: public void testSHAReverseInterleave() throws Exception {
068: System.out.println("testSHAReverseInterleave");
069: MessageDigest md = MessageDigest.getInstance("SHA-SRP-Reverse");
070: byte[] test = "session_key".getBytes();
071:
072: byte[] hash1 = Util.sessionKeyHash(test);
073: String hash1b64 = Util.encodeBase64(hash1);
074: System.out.println("hash1 = " + hash1b64);
075: byte[] hash2 = md.digest(test);
076: String hash2b64 = Util.encodeBase64(hash2);
077: System.out.println("hash2 = " + hash2b64);
078: super .assertEquals(hash1b64, hash2b64);
079: }
080:
081: public static Test suite() {
082: TestSuite suite = new TestSuite(SecurityProviderlTestCase.class);
083:
084: // Create an initializer for the test suite
085: TestSetup wrapper = new TestSetup(suite) {
086: protected void setUp() throws Exception {
087: Util.init();
088: JBossSXProvider provider = new JBossSXProvider();
089: Security.addProvider(provider);
090: }
091:
092: protected void tearDown() throws Exception {
093: Security.removeProvider(JBossSXProvider.PROVIDER_NAME);
094: }
095: };
096: return wrapper;
097: }
098:
099: public static void main(java.lang.String[] args) {
100: System.setErr(System.out);
101: Test suite = suite();
102: junit.textui.TestRunner.run(suite);
103: }
104: }
|