Index: modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/PKIStatusInfoTest.java =================================================================== --- modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/PKIStatusInfoTest.java (revision 0) +++ modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/PKIStatusInfoTest.java (revision 0) @@ -0,0 +1,37 @@ +package org.apache.harmony.security.tests.x509.tsp; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.harmony.security.x509.tsp.PKIFailureInfo; +import org.apache.harmony.security.x509.tsp.PKIStatus; +import org.apache.harmony.security.x509.tsp.PKIStatusInfo; + +import junit.framework.TestCase; + +public class PKIStatusInfoTest extends TestCase { + + /** + * @throws IOException + * @tests 'org.apache.harmony.security.x509.tsp.PKIStatusInfo.getEncoded()' + */ + public void testGetEncoded() throws IOException { + ArrayList statusStr = new ArrayList(2); + statusStr.add("one"); + statusStr.add("two"); + PKIStatusInfo info = new PKIStatusInfo(PKIStatus.REJECTION, statusStr, + PKIFailureInfo.BAD_DATA_FORMAT); + byte [] encoding = PKIStatusInfo.ASN1.encode(info); + PKIStatusInfo decoded = (PKIStatusInfo) PKIStatusInfo.ASN1 + .decode(encoding); + + assertEquals(info.getStatus(), decoded.getStatus()); + List decodedStString = decoded.getStatusString(); + assertNotNull(decodedStString); + assertEquals("one", decodedStString.get(0)); + assertEquals("two", decodedStString.get(1)); + assertEquals(info.getFailInfo(), decoded.getFailInfo()); + } +} + Index: modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/TSTInfoTest.java =================================================================== --- modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/TSTInfoTest.java (revision 0) +++ modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/TSTInfoTest.java (revision 0) @@ -0,0 +1,69 @@ +package org.apache.harmony.security.tests.x509.tsp; + +import java.io.IOException; +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Date; + +import junit.framework.TestCase; + +import org.apache.harmony.security.x501.Name; +import org.apache.harmony.security.x509.AlgorithmIdentifier; +import org.apache.harmony.security.x509.Extension; +import org.apache.harmony.security.x509.Extensions; +import org.apache.harmony.security.x509.GeneralName; +import org.apache.harmony.security.x509.tsp.MessageImprint; +import org.apache.harmony.security.x509.tsp.TSTInfo; + +public class TSTInfoTest extends TestCase { + + /** + * @throws IOException + * @tests 'org.apache.harmony.security.x509.tsp.TSTInfo.getEncoded()' + */ + public void testGetEncoded() throws IOException { + // Unizeto CETRUM policy + String policy = "1.2.3.4.5"; + // SHA1 OID as defined in RFC 3161 + String sha1 = "1.3.14.3.2.26"; + MessageImprint msgImprint = new MessageImprint(new AlgorithmIdentifier( + sha1), new byte[20]); + Date genTime = new Date(); + BigInteger nonce = BigInteger.valueOf(1234567890L); + int[] accuracy = new int[] { 1, 0, 0 }; + GeneralName tsa = new GeneralName(new Name("CN=AnAuthority")); + Extensions exts = new Extensions(); + // Time-Stamping extension OID: as defined in RFC 3161 + int[] timeStampingExtOID = new int[] { 1, 3, 6, 1, 5, 5, 7, 3, 8 }; + byte[] timeStampingExtValue = new byte[] { (byte) 1, (byte) 2, (byte) 3 }; + Extension ext = new Extension(timeStampingExtOID, true, + timeStampingExtValue); + exts.addExtension(ext); + + TSTInfo info = new TSTInfo(1, policy, msgImprint, BigInteger.TEN, + genTime, accuracy, Boolean.FALSE, nonce, tsa, exts); + + byte[] encoding = TSTInfo.ASN1.encode(info); + TSTInfo decoded = (TSTInfo) TSTInfo.ASN1.decode(encoding); + + assertEquals("Decoded version is incorrect", info.getVersion(), decoded + .getVersion()); + assertEquals("Decoded policy is incorrect", policy, decoded.getPolicy()); + assertTrue("Decoded messageImprint is incorrect", Arrays.equals( + MessageImprint.ASN1.encode(msgImprint), MessageImprint.ASN1 + .encode(decoded.getMessageImprint()))); + assertEquals("Decoded serialNumber is incorrect", BigInteger.TEN, + decoded.getSerialNumber()); + assertEquals("Decoded genTime is incorrect", genTime, decoded + .getGenTime()); + assertTrue("Decoded accuracy is incorrect", Arrays.equals(accuracy, + decoded.getAccuracy())); + assertFalse("Decoded ordering is incorrect", decoded.getOrdering() + .booleanValue()); + assertEquals("Decoded nonce is incorrect", nonce, decoded.getNonce()); + assertEquals("Decoded tsa is incorrect", tsa, decoded.getTsa()); + assertEquals("Decoded extensions is incorrect", exts, decoded + .getExtensions()); + } +} + Index: modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/TimeStampReqTest.java =================================================================== --- modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/TimeStampReqTest.java (revision 0) +++ modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/TimeStampReqTest.java (revision 0) @@ -0,0 +1,52 @@ +package org.apache.harmony.security.tests.x509.tsp; + +import java.io.IOException; +import java.math.BigInteger; +import java.util.Arrays; + +import junit.framework.TestCase; + +import org.apache.harmony.security.x509.AlgorithmIdentifier; +import org.apache.harmony.security.x509.Extension; +import org.apache.harmony.security.x509.Extensions; +import org.apache.harmony.security.x509.tsp.MessageImprint; +import org.apache.harmony.security.x509.tsp.TimeStampReq; + +public class TimeStampReqTest extends TestCase { + + /** + * @throws IOException + * @tests 'org.apache.harmony.security.x509.tsp.TimeStampReq.getEncoded()' + */ + public void testTimeStampReq() throws IOException { + // SHA1 OID + MessageImprint msgImprint = new MessageImprint(new AlgorithmIdentifier( + "1.3.14.3.2.26"), new byte[20]); + String reqPolicy = "1.2.3.4.5"; + BigInteger nonce = BigInteger.valueOf(1234567890L); + Extensions exts = new Extensions(); + int[] extOID = new int[] { 1, 2, 3, 2, 1 }; + byte[] extValue = new byte[] { (byte) 1, (byte) 2, (byte) 3 }; + Extension ext = new Extension(extOID, false, extValue); + exts.addExtension(ext); + + TimeStampReq req = new TimeStampReq(1, msgImprint, reqPolicy, + nonce, Boolean.FALSE, exts); + byte[] encoding = req.getEncoded(); + TimeStampReq decoded = (TimeStampReq) TimeStampReq.ASN1 + .decode(encoding); + assertEquals("Decoded version is incorrect", req.getVersion(), decoded + .getVersion()); + assertTrue("Decoded messageImprint is incorrect", Arrays.equals( + MessageImprint.ASN1.encode(msgImprint), MessageImprint.ASN1 + .encode(decoded.getMessageImprint()))); + assertEquals("Decoded reqPolicy is incorrect", reqPolicy, decoded + .getReqPolicy()); + assertEquals("Decoded nonce is incorrect", nonce, decoded.getNonce()); + assertFalse("Decoded certReq is incorrect", decoded.getCertReq() + .booleanValue()); + assertEquals("Decoded extensions is incorrect", exts, decoded + .getExtensions()); + } +} + Index: modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/TimeStampRespTest.java =================================================================== --- modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/TimeStampRespTest.java (revision 0) +++ modules/security/src/test/impl/java/org/apache/harmony/security/tests/x509/tsp/TimeStampRespTest.java (revision 0) @@ -0,0 +1,95 @@ +package org.apache.harmony.security.tests.x509.tsp; + +import java.io.IOException; +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; + +import junit.framework.TestCase; + +import org.apache.harmony.security.asn1.ASN1Integer; +import org.apache.harmony.security.asn1.ASN1OctetString; +import org.apache.harmony.security.pkcs7.ContentInfo; +import org.apache.harmony.security.pkcs7.SignedData; +import org.apache.harmony.security.pkcs7.SignerInfo; +import org.apache.harmony.security.x501.Name; +import org.apache.harmony.security.x509.AlgorithmIdentifier; +import org.apache.harmony.security.x509.Extension; +import org.apache.harmony.security.x509.Extensions; +import org.apache.harmony.security.x509.GeneralName; +import org.apache.harmony.security.x509.tsp.MessageImprint; +import org.apache.harmony.security.x509.tsp.PKIFailureInfo; +import org.apache.harmony.security.x509.tsp.PKIStatus; +import org.apache.harmony.security.x509.tsp.PKIStatusInfo; +import org.apache.harmony.security.x509.tsp.TSTInfo; +import org.apache.harmony.security.x509.tsp.TimeStampResp; + +public class TimeStampRespTest extends TestCase { + + /** + * @throws IOException + * @tests 'org.apache.harmony.security.x509.tsp.TimeStampResp.getEncoded()' + */ + public void testGetEncoded() throws IOException { + String statusString = "statusString"; + PKIStatusInfo status = new PKIStatusInfo(PKIStatus.REJECTION, + Collections.singletonList(statusString), + PKIFailureInfo.BAD_REQUEST); + + + // creating TimeStampToken + String policy = "1.2.3.4.5"; + String sha1 = "1.3.14.3.2.26"; + MessageImprint msgImprint = new MessageImprint(new AlgorithmIdentifier( + sha1), new byte[20]); + Date genTime = new Date(); + BigInteger nonce = BigInteger.valueOf(1234567890L); + // accuracy is 1 second + int[] accuracy = new int[] { 1, 0, 0 }; + GeneralName tsa = new GeneralName(new Name("CN=AnAuthority")); + Extensions exts = new Extensions(); + // Time-Stamping extension OID: as defined in RFC 3161 + int[] timeStampingExtOID = new int[] { 1, 3, 6, 1, 5, 5, 7, 3, 8 }; + byte[] timeStampingExtValue = new byte[] { (byte) 1, (byte) 2, (byte) 3 }; + Extension ext = new Extension(timeStampingExtOID, true, + timeStampingExtValue); + exts.addExtension(ext); + + TSTInfo tSTInfo = new TSTInfo(1, policy, msgImprint, BigInteger.TEN, + genTime, accuracy, Boolean.FALSE, nonce, tsa, exts); + + Object[] issuerAndSerialNumber = new Object[] { new Name("CN=issuer"), + ASN1Integer.fromIntValue(12345) }; + // SHA1withDSA OID + String sha1dsa = "1.2.840.10040.4.3"; + SignerInfo sigInfo = new SignerInfo(1, issuerAndSerialNumber, + new AlgorithmIdentifier(sha1), null, new AlgorithmIdentifier( + sha1dsa), new byte[20], null); + // TSTInfo OID according to RFC 3161 + int[] tSTInfoOid = new int[] { 1, 2, 840, 113549, 1, 9, 16, 1, 4 }; + ContentInfo tSTInfoEncoded = new ContentInfo(tSTInfoOid, + ASN1OctetString.getInstance().encode( + TSTInfo.ASN1.encode(tSTInfo))); + SignedData tokenContent = new SignedData(1, Collections + .singletonList(new AlgorithmIdentifier(sha1)), tSTInfoEncoded, + null, null, Collections.singletonList(sigInfo)); + ContentInfo timeStampToken = new ContentInfo(ContentInfo.SIGNED_DATA, + tokenContent); + + TimeStampResp response = new TimeStampResp(status, timeStampToken); + + byte [] encoding = TimeStampResp.ASN1.encode(response); + TimeStampResp decoded = (TimeStampResp) TimeStampResp.ASN1 + .decode(encoding); + + // deeper checks are performed in the corresponding unit tests + assertTrue("Decoded status is incorrect", Arrays.equals( + PKIStatusInfo.ASN1.encode(status), PKIStatusInfo.ASN1 + .encode(decoded.getStatus()))); + assertTrue("Decoded timeStampToken is incorrect", Arrays.equals( + timeStampToken.getEncoded(), decoded.getTimeStampToken() + .getEncoded())); + } +} +