1 22 package org.jboss.test; 23 24 import java.io.Serializable ; 25 import java.math.BigInteger ; 26 import java.rmi.RemoteException ; 27 import java.security.KeyException ; 28 import java.security.MessageDigest ; 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 48 public class SRPProtocolTestCase extends junit.framework.TestCase 49 { 50 String username = "stark"; 51 char[] password = "scott".toCharArray(); 52 SRPServerInterface server; 53 54 public SRPProtocolTestCase(String name) 55 { 56 super(name); 57 } 58 public SRPProtocolTestCase(String name, String username, char[] password) 59 { 60 super(name); 61 this.username = username; 62 this.password = password; 63 } 64 65 protected void setUp() throws Exception 66 { 67 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 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 ("Failed to validate server reply"); 97 NDC.pop(); 98 NDC.remove(); 99 } 100 101 104 public static void main(String 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 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 |