001: package org.bouncycastle.asn1.test;
002:
003: import org.bouncycastle.asn1.ASN1EncodableVector;
004: import org.bouncycastle.asn1.ASN1Sequence;
005: import org.bouncycastle.asn1.ASN1InputStream;
006: import org.bouncycastle.asn1.DERObjectIdentifier;
007: import org.bouncycastle.asn1.DERSequence;
008: import org.bouncycastle.asn1.x509.GeneralName;
009: import org.bouncycastle.asn1.x509.X509Name;
010: import org.bouncycastle.asn1.x509.qualified.SemanticsInformation;
011: import org.bouncycastle.util.test.SimpleTest;
012:
013: public class SemanticsInformationUnitTest extends SimpleTest {
014: public String getName() {
015: return "SemanticsInformation";
016: }
017:
018: public void performTest() throws Exception {
019: DERObjectIdentifier statementId = new DERObjectIdentifier("1.1");
020: SemanticsInformation mv = new SemanticsInformation(statementId);
021:
022: checkConstruction(mv, statementId, null);
023:
024: GeneralName[] names = new GeneralName[2];
025:
026: names[0] = new GeneralName(GeneralName.rfc822Name,
027: "test@test.org");
028: names[1] = new GeneralName(new X509Name("cn=test"));
029:
030: mv = new SemanticsInformation(statementId, names);
031:
032: checkConstruction(mv, statementId, names);
033:
034: mv = new SemanticsInformation(names);
035:
036: checkConstruction(mv, null, names);
037:
038: mv = SemanticsInformation.getInstance(null);
039:
040: if (mv != null) {
041: fail("null getInstance() failed.");
042: }
043:
044: try {
045: SemanticsInformation.getInstance(new Object());
046:
047: fail("getInstance() failed to detect bad object.");
048: } catch (IllegalArgumentException e) {
049: // expected
050: }
051:
052: try {
053: ASN1EncodableVector v = new ASN1EncodableVector();
054:
055: new SemanticsInformation(new DERSequence(v));
056:
057: fail("constructor failed to detect empty sequence.");
058: } catch (IllegalArgumentException e) {
059: // expected
060: }
061: }
062:
063: private void checkConstruction(SemanticsInformation mv,
064: DERObjectIdentifier semanticsIdentifier, GeneralName[] names)
065: throws Exception {
066: checkStatement(mv, semanticsIdentifier, names);
067:
068: mv = SemanticsInformation.getInstance(mv);
069:
070: checkStatement(mv, semanticsIdentifier, names);
071:
072: ASN1InputStream aIn = new ASN1InputStream(mv.toASN1Object()
073: .getEncoded());
074:
075: ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
076:
077: mv = SemanticsInformation.getInstance(seq);
078:
079: checkStatement(mv, semanticsIdentifier, names);
080: }
081:
082: private void checkStatement(SemanticsInformation si,
083: DERObjectIdentifier id, GeneralName[] names) {
084: if (id != null) {
085: if (!si.getSemanticsIdentifier().equals(id)) {
086: fail("ids don't match.");
087: }
088: } else if (si.getSemanticsIdentifier() != null) {
089: fail("statementId found when none expected.");
090: }
091:
092: if (names != null) {
093: GeneralName[] siNames = si.getNameRegistrationAuthorities();
094:
095: for (int i = 0; i != siNames.length; i++) {
096: if (!names[i].equals(siNames[i])) {
097: fail("name registration authorities don't match.");
098: }
099: }
100: } else if (si.getNameRegistrationAuthorities() != null) {
101: fail("name registration authorities found when none expected.");
102: }
103: }
104:
105: public static void main(String[] args) {
106: runTest(new SemanticsInformationUnitTest());
107: }
108: }
|