KickJava   Java API By Example, From Geeks To Geeks.

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


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;
23
24 import java.io.Serializable JavaDoc;
25 import java.math.BigInteger JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27 import java.security.KeyException JavaDoc;
28 import java.security.MessageDigest JavaDoc;
29
30 import org.jboss.logging.Logger;
31 import org.apache.log4j.NDC;
32 import org.apache.log4j.PatternLayout;
33 import org.apache.log4j.WriterAppender;
34
35 import org.jboss.logging.XLevel;
36 import org.jboss.security.Util;
37 import org.jboss.security.srp.SRPConf;
38 import org.jboss.security.srp.SRPServerInterface;
39 import org.jboss.security.srp.SRPClientSession;
40 import org.jboss.security.srp.SRPParameters;
41 import org.jboss.security.srp.SRPServerSession;
42
43 /** Test of the SRP protocol msg exchange sequence.
44
45 @author Scott.Stark@jboss.org
46 @version $Revision: 58118 $
47 */

48 public class SRPProtocolTestCase extends junit.framework.TestCase
49 {
50    String JavaDoc username = "stark";
51    char[] password = "scott".toCharArray();
52    SRPServerInterface server;
53
54    public SRPProtocolTestCase(String JavaDoc name)
55    {
56        super(name);
57    }
58    public SRPProtocolTestCase(String JavaDoc name, String JavaDoc username, char[] password)
59    {
60       super(name);
61       this.username = username;
62       this.password = password;
63    }
64
65    protected void setUp() throws Exception JavaDoc
66    {
67       // Set up a simple configuration that logs on the console.
68
Logger root = Logger.getRoot();
69        root.setLevel(XLevel.TRACE);
70        root.addAppender(new WriterAppender(new PatternLayout("%x%m%n"), System.out));
71        Util.init();
72        NDC.push("S,");
73        server = new SimpleSRPServer(password, "123456");
74        NDC.pop();
75        NDC.remove();
76    }
77
78     public void testProtocol() throws Exception JavaDoc
79     {
80         SRPParameters params = server.getSRPParameters(username);
81         NDC.push("C,");
82         SRPClientSession client = new SRPClientSession(username, password, params);
83         byte[] A = client.exponential();
84         NDC.pop();
85         NDC.push("S,");
86         byte[] B = server.init(username, A);
87         NDC.pop();
88         NDC.push("C,");
89         byte[] M1 = client.response(B);
90         NDC.pop();
91         NDC.push("S,");
92         byte[] M2 = server.verify(username, M1);
93         NDC.pop();
94         NDC.push("C,");
95         if( client.verify(M2) == false )
96             throw new SecurityException JavaDoc("Failed to validate server reply");
97         NDC.pop();
98         NDC.remove();
99     }
100
101     /**
102     * @param args the command line arguments
103     */

104     public static void main(String JavaDoc args[])
105     {
106         long start = System.currentTimeMillis();
107         try
108         {
109             SRPProtocolTestCase tst = null;
110             if( args.length == 0 )
111                tst = new SRPProtocolTestCase("main");
112             else
113                tst = new SRPProtocolTestCase("main", args[0], args[1].toCharArray());
114
115             tst.setUp();
116             tst.testProtocol();
117         }
118         catch(Exception JavaDoc e)
119         {
120             e.printStackTrace(System.out);
121         }
122         finally
123         {
124             long end = System.currentTimeMillis();
125             System.out.println("Elapsed time = "+(end - start));
126         }
127     }
128
129 }
130
Popular Tags