001: package org.bouncycastle.asn1.test;
002:
003: import org.bouncycastle.asn1.ASN1InputStream;
004: import org.bouncycastle.asn1.ASN1Sequence;
005: import org.bouncycastle.asn1.DERGeneralizedTime;
006: import org.bouncycastle.asn1.x500.DirectoryString;
007: import org.bouncycastle.asn1.x509.sigi.NameOrPseudonym;
008: import org.bouncycastle.asn1.x509.sigi.PersonalData;
009:
010: import java.io.IOException;
011: import java.math.BigInteger;
012:
013: public class PersonalDataUnitTest extends ASN1UnitTest {
014: public String getName() {
015: return "PersonalData";
016: }
017:
018: public void performTest() throws Exception {
019: NameOrPseudonym nameOrPseudonym = new NameOrPseudonym(
020: "pseudonym");
021: BigInteger nameDistinguisher = BigInteger.valueOf(10);
022: DERGeneralizedTime dateOfBirth = new DERGeneralizedTime(
023: "20070315173729Z");
024: DirectoryString placeOfBirth = new DirectoryString(
025: "placeOfBirth");
026: String gender = "M";
027: DirectoryString postalAddress = new DirectoryString("address");
028:
029: PersonalData data = new PersonalData(nameOrPseudonym,
030: nameDistinguisher, dateOfBirth, placeOfBirth, gender,
031: postalAddress);
032:
033: checkConstruction(data, nameOrPseudonym, nameDistinguisher,
034: dateOfBirth, placeOfBirth, gender, postalAddress);
035:
036: data = new PersonalData(nameOrPseudonym, null, dateOfBirth,
037: placeOfBirth, gender, postalAddress);
038:
039: checkConstruction(data, nameOrPseudonym, null, dateOfBirth,
040: placeOfBirth, gender, postalAddress);
041:
042: data = new PersonalData(nameOrPseudonym, nameDistinguisher,
043: null, placeOfBirth, gender, postalAddress);
044:
045: checkConstruction(data, nameOrPseudonym, nameDistinguisher,
046: null, placeOfBirth, gender, postalAddress);
047:
048: data = new PersonalData(nameOrPseudonym, nameDistinguisher,
049: dateOfBirth, null, gender, postalAddress);
050:
051: checkConstruction(data, nameOrPseudonym, nameDistinguisher,
052: dateOfBirth, null, gender, postalAddress);
053:
054: data = new PersonalData(nameOrPseudonym, nameDistinguisher,
055: dateOfBirth, placeOfBirth, null, postalAddress);
056:
057: checkConstruction(data, nameOrPseudonym, nameDistinguisher,
058: dateOfBirth, placeOfBirth, null, postalAddress);
059:
060: data = new PersonalData(nameOrPseudonym, nameDistinguisher,
061: dateOfBirth, placeOfBirth, gender, null);
062:
063: checkConstruction(data, nameOrPseudonym, nameDistinguisher,
064: dateOfBirth, placeOfBirth, gender, null);
065:
066: data = PersonalData.getInstance(null);
067:
068: if (data != null) {
069: fail("null getInstance() failed.");
070: }
071:
072: try {
073: PersonalData.getInstance(new Object());
074:
075: fail("getInstance() failed to detect bad object.");
076: } catch (IllegalArgumentException e) {
077: // expected
078: }
079: }
080:
081: private void checkConstruction(PersonalData data,
082: NameOrPseudonym nameOrPseudonym,
083: BigInteger nameDistinguisher,
084: DERGeneralizedTime dateOfBirth,
085: DirectoryString placeOfBirth, String gender,
086: DirectoryString postalAddress) throws IOException {
087: checkValues(data, nameOrPseudonym, nameDistinguisher,
088: dateOfBirth, placeOfBirth, gender, postalAddress);
089:
090: data = PersonalData.getInstance(data);
091:
092: checkValues(data, nameOrPseudonym, nameDistinguisher,
093: dateOfBirth, placeOfBirth, gender, postalAddress);
094:
095: ASN1InputStream aIn = new ASN1InputStream(data.toASN1Object()
096: .getEncoded());
097:
098: ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
099:
100: data = PersonalData.getInstance(seq);
101:
102: checkValues(data, nameOrPseudonym, nameDistinguisher,
103: dateOfBirth, placeOfBirth, gender, postalAddress);
104: }
105:
106: private void checkValues(PersonalData data,
107: NameOrPseudonym nameOrPseudonym,
108: BigInteger nameDistinguisher,
109: DERGeneralizedTime dateOfBirth,
110: DirectoryString placeOfBirth, String gender,
111: DirectoryString postalAddress) {
112: checkMandatoryField("nameOrPseudonym", nameOrPseudonym, data
113: .getNameOrPseudonym());
114: checkOptionalField("nameDistinguisher", nameDistinguisher, data
115: .getNameDistinguisher());
116: checkOptionalField("dateOfBirth", dateOfBirth, data
117: .getDateOfBirth());
118: checkOptionalField("placeOfBirth", placeOfBirth, data
119: .getPlaceOfBirth());
120: checkOptionalField("gender", gender, data.getGender());
121: checkOptionalField("postalAddress", postalAddress, data
122: .getPostalAddress());
123: }
124:
125: public static void main(String[] args) {
126: runTest(new PersonalDataUnitTest());
127: }
128: }
|