1 /* 2 * Copyright �, Aegeus Technology Limited. 3 * All rights reserved. 4 */ 5 package jsdsi; 6 7 import java.io.BufferedInputStream; 8 import java.io.BufferedOutputStream; 9 import java.io.ByteArrayInputStream; 10 import java.io.ByteArrayOutputStream; 11 import java.io.IOException; 12 import java.net.URL; 13 import java.security.KeyPair; 14 15 import jsdsi.sexp.ObjInputStream; 16 import jsdsi.sexp.ObjOutputStream; 17 import jsdsi.sexp.SexpException; 18 import jsdsi.sexp.SexpParseException; 19 import jsdsi.util.DateUtil; 20 import jsdsi.util.DigestAlgoEnum; 21 import jsdsi.util.KeyEnum; 22 import jsdsi.util.KeyPairFactory; 23 import junit.framework.Test; 24 import junit.framework.TestCase; 25 import junit.framework.TestSuite; 26 27 /*** 28 * @author Sean Radford 29 * @version $Revision: 1.4.2.1 $ $Date: 2004/11/16 12:22:53 $ 30 */ 31 public class SexpMarshallingTest extends TestCase { 32 33 static { 34 jsdsi.Provider.install(); 35 } 36 37 static Obj[] objArray = null; 38 static { 39 try { 40 objArray = new Obj[] { 41 (Obj) RSAPublicKey.create().getPublic(), 42 new RSAPublicKey((java.security.interfaces.RSAPublicKey) RSAPublicKey.create().getPublic(), 43 new URL[] { new URL("http://localhost/")}), 44 createAuthCertificate().toSequence(), 45 createNameCertificate().toSequence() 46 }; 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 } 51 52 static Certificate createAuthCertificate() throws Exception { 53 KeyPair issuerKP = KeyPairFactory.create(KeyEnum.RSA, 512); 54 KeyPair subjectKP = KeyPairFactory.create(KeyEnum.RSA, 512); 55 Validity validity = new Validity(DateUtil.newDate(), 56 DateUtil.newDate(1)); 57 Tag tag = new StringTag("test tag"); 58 boolean propagate = true; 59 AuthCert cert = new AuthCert((Principal) issuerKP.getPublic(), 60 (Principal) subjectKP.getPublic(), 61 validity, 62 "test diplay hint", 63 "a test certificate", tag, propagate); 64 Signature sig = Signature.create(issuerKP, cert, DigestAlgoEnum.MD5); 65 return new Certificate(cert, sig); 66 } 67 68 static Certificate createNameCertificate() throws Exception { 69 KeyPair issuerKP = KeyPairFactory.create(KeyEnum.RSA, 512); 70 KeyPair subjectKP = KeyPairFactory.create(KeyEnum.RSA, 512); 71 Validity validity = new Validity(DateUtil.newDate(), 72 DateUtil.newDate(1)); 73 74 NameCert cert = new NameCert((Principal) issuerKP.getPublic(), 75 (Subject)subjectKP.getPublic(), 76 validity, 77 "test diplay hint", 78 "a test certificate", "alice"); 79 Signature sig = Signature.create(issuerKP, cert, DigestAlgoEnum.MD5); 80 return new Certificate(cert, sig); 81 } 82 83 /*** 84 * @see TestCase#setUp() 85 */ 86 protected void setUp() throws Exception { 87 super.setUp(); 88 } 89 90 /*** 91 * @see TestCase#tearDown() 92 */ 93 protected void tearDown() throws Exception { 94 super.tearDown(); 95 } 96 97 static abstract class AbstractMarshallingTest extends TestCase { 98 99 static final int OFFSET = 2; 100 101 static final int WIDTH = 80; 102 103 static final int LAST = 2; 104 105 Obj obj; 106 107 public AbstractMarshallingTest(String type, Obj obj) { 108 super(type); 109 this.obj = obj; 110 } 111 112 public void runTest() throws Exception { 113 byte[] bytes = toByteArray(obj); 114 Obj out = fromByteArray(bytes); 115 assertEquals(this.getName(), obj, out); 116 } 117 118 protected abstract byte[] toByteArray(Obj obj) throws IOException; 119 120 protected Obj fromByteArray(byte[] bytes) throws SexpParseException, 121 SexpException, IOException { 122 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 123 BufferedInputStream bis = new BufferedInputStream(bais); 124 ObjInputStream ois = new ObjInputStream(bis); 125 return ois.readObj(); 126 } 127 } 128 129 static class CanonicalMarshallingTest extends AbstractMarshallingTest { 130 131 public CanonicalMarshallingTest(Obj obj) { 132 super("Canonical", obj); 133 } 134 135 protected byte[] toByteArray(Obj obj) throws IOException { 136 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 137 BufferedOutputStream bos = new BufferedOutputStream(baos); 138 ObjOutputStream oos = new ObjOutputStream(bos); 139 oos.writeCanonical(obj); 140 oos.close(); 141 return baos.toByteArray(); 142 } 143 } 144 145 static class ReadableMarshallingTest extends AbstractMarshallingTest { 146 147 static final int OFFSET = 2; 148 149 static final int WIDTH = 80; 150 151 static final int LAST = 2; 152 153 public ReadableMarshallingTest(Obj obj) { 154 super("Readable", obj); 155 } 156 157 protected byte[] toByteArray(Obj obj) throws IOException { 158 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 159 BufferedOutputStream bos = new BufferedOutputStream(baos); 160 ObjOutputStream oos = new ObjOutputStream(bos); 161 oos.writeReadable(obj, OFFSET, WIDTH, LAST); 162 oos.close(); 163 return baos.toByteArray(); 164 } 165 } 166 167 static class TransportMarshallingTest extends AbstractMarshallingTest { 168 169 public TransportMarshallingTest(Obj obj) { 170 super("Transport", obj); 171 } 172 173 protected byte[] toByteArray(Obj obj) throws IOException { 174 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 175 BufferedOutputStream bos = new BufferedOutputStream(baos); 176 ObjOutputStream oos = new ObjOutputStream(bos); 177 oos.writeTransport(obj); 178 oos.close(); 179 return baos.toByteArray(); 180 } 181 } 182 183 public static Test suite() { 184 // just check objArray was initialised correctly 185 if (objArray == null) { throw new IllegalStateException("jsdsi.Obj[] is null"); } 186 TestSuite s = new NamedTestSuite("SexpMarshallingTest"); 187 // check tag self-equality, intersection, implication 188 for (int i = 0; i < objArray.length; i++) { 189 s.addTest(new CanonicalMarshallingTest(objArray[i])); 190 s.addTest(new ReadableMarshallingTest(objArray[i])); 191 s.addTest(new TransportMarshallingTest(objArray[i])); 192 } 193 return s; 194 } 195 196 }

This page was automatically generated by Maven