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.security.test;
023:
024: import org.apache.log4j.Category;
025: import org.apache.log4j.NDC;
026: import org.apache.log4j.PatternLayout;
027: import org.apache.log4j.WriterAppender;
028:
029: import org.jboss.logging.XLevel;
030: import org.jboss.security.Util;
031: import org.jboss.security.srp.SRPServerInterface;
032: import org.jboss.security.srp.SRPClientSession;
033: import org.jboss.security.srp.SRPParameters;
034:
035: /** Test of the SRP protocol msg exchange sequence.
036:
037: @author Scott.Stark@jboss.org
038: @version $Revision: 57210 $
039: */
040: public class SRPProtocolTestCase extends junit.framework.TestCase {
041: String username = "stark";
042: char[] password = "scott".toCharArray();
043: SRPServerInterface server;
044:
045: public SRPProtocolTestCase(String name) {
046: super (name);
047: }
048:
049: public SRPProtocolTestCase(String name, String username,
050: char[] password) {
051: super (name);
052: this .username = username;
053: this .password = password;
054: }
055:
056: protected void setUp() throws Exception {
057: // Set up a simple configuration that logs on the console.
058: Category root = Category.getRoot();
059: root.setLevel(XLevel.TRACE);
060: root.addAppender(new WriterAppender(
061: new PatternLayout("%x%m%n"), System.out));
062: Util.init();
063: NDC.push("S,");
064: server = new SimpleSRPServer(password, "123456");
065: NDC.pop();
066: NDC.remove();
067: }
068:
069: public void testProtocol() throws Exception {
070: SRPParameters params = server.getSRPParameters(username);
071: NDC.push("C,");
072: SRPClientSession client = new SRPClientSession(username,
073: password, params);
074: byte[] A = client.exponential();
075: NDC.pop();
076: NDC.push("S,");
077: byte[] B = server.init(username, A);
078: NDC.pop();
079: NDC.push("C,");
080: byte[] M1 = client.response(B);
081: NDC.pop();
082: NDC.push("S,");
083: byte[] M2 = server.verify(username, M1);
084: NDC.pop();
085: NDC.push("C,");
086: if (client.verify(M2) == false)
087: throw new SecurityException(
088: "Failed to validate server reply");
089: NDC.pop();
090: NDC.remove();
091: }
092:
093: /**
094: * @param args the command line arguments
095: */
096: public static void main(String args[]) {
097: long start = System.currentTimeMillis();
098: try {
099: SRPProtocolTestCase tst = null;
100: if (args.length == 0)
101: tst = new SRPProtocolTestCase("main");
102: else
103: tst = new SRPProtocolTestCase("main", args[0], args[1]
104: .toCharArray());
105:
106: tst.setUp();
107: tst.testProtocol();
108: } catch (Exception e) {
109: e.printStackTrace(System.out);
110: } finally {
111: long end = System.currentTimeMillis();
112: System.out.println("Elapsed time = " + (end - start));
113: }
114: }
115:
116: }
|