01: package org.bouncycastle.asn1.test;
02:
03: import org.bouncycastle.asn1.ASN1InputStream;
04: import org.bouncycastle.asn1.ASN1Sequence;
05: import org.bouncycastle.asn1.DERSequence;
06: import org.bouncycastle.asn1.DERString;
07: import org.bouncycastle.asn1.x500.DirectoryString;
08: import org.bouncycastle.asn1.x509.sigi.NameOrPseudonym;
09:
10: import java.io.IOException;
11:
12: public class NameOrPseudonymUnitTest extends ASN1UnitTest {
13: public String getName() {
14: return "NameOrPseudonym";
15: }
16:
17: public void performTest() throws Exception {
18: String pseudonym = "pseudonym";
19: DirectoryString surname = new DirectoryString("surname");
20: ASN1Sequence givenName = new DERSequence(new DirectoryString(
21: "givenName"));
22:
23: NameOrPseudonym id = new NameOrPseudonym(pseudonym);
24:
25: checkConstruction(id, pseudonym, null, null);
26:
27: id = new NameOrPseudonym(surname, givenName);
28:
29: checkConstruction(id, null, surname, givenName);
30:
31: id = NameOrPseudonym.getInstance(null);
32:
33: if (id != null) {
34: fail("null getInstance() failed.");
35: }
36:
37: try {
38: NameOrPseudonym.getInstance(new Object());
39:
40: fail("getInstance() failed to detect bad object.");
41: } catch (IllegalArgumentException e) {
42: // expected
43: }
44: }
45:
46: private void checkConstruction(NameOrPseudonym id,
47: String pseudonym, DirectoryString surname,
48: ASN1Sequence givenName) throws IOException {
49: checkValues(id, pseudonym, surname, givenName);
50:
51: id = NameOrPseudonym.getInstance(id);
52:
53: checkValues(id, pseudonym, surname, givenName);
54:
55: ASN1InputStream aIn = new ASN1InputStream(id.toASN1Object()
56: .getEncoded());
57:
58: if (surname != null) {
59: ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
60:
61: id = NameOrPseudonym.getInstance(seq);
62: } else {
63: DERString s = (DERString) aIn.readObject();
64:
65: id = NameOrPseudonym.getInstance(s);
66: }
67:
68: checkValues(id, pseudonym, surname, givenName);
69: }
70:
71: private void checkValues(NameOrPseudonym id, String pseudonym,
72: DirectoryString surname, ASN1Sequence givenName) {
73:
74: if (surname != null) {
75: checkMandatoryField("surname", surname, id.getSurname());
76: checkMandatoryField("givenName", givenName,
77: new DERSequence(id.getGivenName()[0]));
78: } else {
79: checkOptionalField("pseudonym", new DirectoryString(
80: pseudonym), id.getPseudonym());
81: }
82: }
83:
84: public static void main(String[] args) {
85: runTest(new NameOrPseudonymUnitTest());
86: }
87: }
|