Name subject = new Name("O=subject");
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(
new AlgorithmIdentifier("1.2.840.113549.1.1.2"), new byte[4]);
List attributes = new ArrayList();
// 1.2.840.113549.1.9.1 is OID of EMAILADDRESS
attributes.add(new AttributeTypeAndValue("1.2.840.113549.1.9.1",
new AttributeValue("a@b.com", false)));
CertificationRequestInfo certReqInfo = new CertificationRequestInfo(
version, subject, spki, attributes);
// check what we have constructed
assertEquals(version, certReqInfo.getVersion());
assertEquals(subject.getName(X500Principal.RFC1779), certReqInfo
.getSubject().getName(X500Principal.RFC1779));
assertTrue(Arrays.equals(spki.getEncoded(), certReqInfo
.getSubjectPublicKeyInfo().getEncoded()));
assertEquals(attributes, certReqInfo.getAttributes());
// decode the encoded CertificationRequestInfo
byte[] encoding = certReqInfo.getEncoded();
CertificationRequestInfo decoded =
(CertificationRequestInfo) CertificationRequestInfo.ASN1
.decode(encoding);
// check what was decoded
assertEquals(certReqInfo.getVersion(), decoded.getVersion());
assertEquals(certReqInfo.getSubject().getName(X500Principal.CANONICAL),
decoded.getSubject().getName(X500Principal.CANONICAL));
assertTrue(Arrays.equals(certReqInfo.getSubjectPublicKeyInfo()
.getEncoded(), decoded.getSubjectPublicKeyInfo().getEncoded()));
AttributeTypeAndValue certReqInfoATaV = (AttributeTypeAndValue) certReqInfo
.getAttributes().get(0);
AttributeTypeAndValue decodedATaV = (AttributeTypeAndValue) decoded
.getAttributes().get(0);
assertEquals(certReqInfoATaV.getType(), decodedATaV.getType());
}