001: package org.bouncycastle.asn1.test;
002:
003: import org.bouncycastle.asn1.ASN1InputStream;
004: import org.bouncycastle.asn1.ASN1Sequence;
005: import org.bouncycastle.asn1.DERObjectIdentifier;
006: import org.bouncycastle.asn1.isismtt.x509.NamingAuthority;
007: import org.bouncycastle.asn1.x500.DirectoryString;
008:
009: import java.io.IOException;
010:
011: public class NamingAuthorityUnitTest extends ASN1UnitTest {
012: public String getName() {
013: return "NamingAuthority";
014: }
015:
016: public void performTest() throws Exception {
017: DERObjectIdentifier namingAuthorityID = new DERObjectIdentifier(
018: "1.2.3");
019: String namingAuthorityURL = "url";
020: DirectoryString namingAuthorityText = new DirectoryString(
021: "text");
022:
023: NamingAuthority auth = new NamingAuthority(namingAuthorityID,
024: namingAuthorityURL, namingAuthorityText);
025:
026: checkConstruction(auth, namingAuthorityID, namingAuthorityURL,
027: namingAuthorityText);
028:
029: auth = new NamingAuthority(null, namingAuthorityURL,
030: namingAuthorityText);
031:
032: checkConstruction(auth, null, namingAuthorityURL,
033: namingAuthorityText);
034:
035: auth = new NamingAuthority(namingAuthorityID, null,
036: namingAuthorityText);
037:
038: checkConstruction(auth, namingAuthorityID, null,
039: namingAuthorityText);
040:
041: auth = new NamingAuthority(namingAuthorityID,
042: namingAuthorityURL, null);
043:
044: checkConstruction(auth, namingAuthorityID, namingAuthorityURL,
045: null);
046:
047: auth = NamingAuthority.getInstance(null);
048:
049: if (auth != null) {
050: fail("null getInstance() failed.");
051: }
052:
053: try {
054: NamingAuthority.getInstance(new Object());
055:
056: fail("getInstance() failed to detect bad object.");
057: } catch (IllegalArgumentException e) {
058: // expected
059: }
060: }
061:
062: private void checkConstruction(NamingAuthority auth,
063: DERObjectIdentifier namingAuthorityID,
064: String namingAuthorityURL,
065: DirectoryString namingAuthorityText) throws IOException {
066: checkValues(auth, namingAuthorityID, namingAuthorityURL,
067: namingAuthorityText);
068:
069: auth = NamingAuthority.getInstance(auth);
070:
071: checkValues(auth, namingAuthorityID, namingAuthorityURL,
072: namingAuthorityText);
073:
074: ASN1InputStream aIn = new ASN1InputStream(auth.toASN1Object()
075: .getEncoded());
076:
077: ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
078:
079: auth = NamingAuthority.getInstance(seq);
080:
081: checkValues(auth, namingAuthorityID, namingAuthorityURL,
082: namingAuthorityText);
083: }
084:
085: private void checkValues(NamingAuthority auth,
086: DERObjectIdentifier namingAuthorityId,
087: String namingAuthorityURL,
088: DirectoryString namingAuthorityText) {
089: checkOptionalField("namingAuthorityId", namingAuthorityId, auth
090: .getNamingAuthorityId());
091: checkOptionalField("namingAuthorityURL", namingAuthorityURL,
092: auth.getNamingAuthorityUrl());
093: checkOptionalField("namingAuthorityText", namingAuthorityText,
094: auth.getNamingAuthorityText());
095: }
096:
097: public static void main(String[] args) {
098: runTest(new NamingAuthorityUnitTest());
099: }
100: }
|