001: package org.bouncycastle.asn1.test;
002:
003: import java.io.IOException;
004:
005: import org.bouncycastle.asn1.ASN1EncodableVector;
006: import org.bouncycastle.asn1.ASN1InputStream;
007: import org.bouncycastle.asn1.ASN1Sequence;
008: import org.bouncycastle.asn1.DERSequence;
009: import org.bouncycastle.asn1.DERTaggedObject;
010: import org.bouncycastle.asn1.DERUTF8String;
011: import org.bouncycastle.asn1.esf.SignerLocation;
012: import org.bouncycastle.util.test.SimpleTest;
013:
014: public class SignerLocationUnitTest extends SimpleTest {
015: public String getName() {
016: return "SignerLocation";
017: }
018:
019: public void performTest() throws Exception {
020: DERUTF8String countryName = new DERUTF8String("Australia");
021:
022: SignerLocation sl = new SignerLocation(countryName, null, null);
023:
024: checkConstruction(sl, countryName, null, null);
025:
026: DERUTF8String localityName = new DERUTF8String("Melbourne");
027:
028: sl = new SignerLocation(null, localityName, null);
029:
030: checkConstruction(sl, null, localityName, null);
031:
032: sl = new SignerLocation(countryName, localityName, null);
033:
034: checkConstruction(sl, countryName, localityName, null);
035:
036: ASN1EncodableVector v = new ASN1EncodableVector();
037:
038: v.add(new DERUTF8String("line 1"));
039: v.add(new DERUTF8String("line 2"));
040:
041: ASN1Sequence postalAddress = new DERSequence(v);
042:
043: sl = new SignerLocation(null, null, postalAddress);
044:
045: checkConstruction(sl, null, null, postalAddress);
046:
047: sl = new SignerLocation(countryName, null, postalAddress);
048:
049: checkConstruction(sl, countryName, null, postalAddress);
050:
051: sl = new SignerLocation(countryName, localityName,
052: postalAddress);
053:
054: checkConstruction(sl, countryName, localityName, postalAddress);
055:
056: sl = SignerLocation.getInstance(null);
057:
058: if (sl != null) {
059: fail("null getInstance() failed.");
060: }
061:
062: try {
063: SignerLocation.getInstance(new Object());
064:
065: fail("getInstance() failed to detect bad object.");
066: } catch (IllegalArgumentException e) {
067: // expected
068: }
069:
070: //
071: // out of range postal address
072: //
073: v = new ASN1EncodableVector();
074:
075: v.add(new DERUTF8String("line 1"));
076: v.add(new DERUTF8String("line 2"));
077: v.add(new DERUTF8String("line 3"));
078: v.add(new DERUTF8String("line 4"));
079: v.add(new DERUTF8String("line 5"));
080: v.add(new DERUTF8String("line 6"));
081: v.add(new DERUTF8String("line 7"));
082:
083: postalAddress = new DERSequence(v);
084:
085: try {
086: new SignerLocation(null, null, postalAddress);
087:
088: fail("constructor failed to detect bad postalAddress.");
089: } catch (IllegalArgumentException e) {
090: // expected
091: }
092:
093: try {
094: new SignerLocation(new DERSequence(new DERTaggedObject(2,
095: postalAddress)));
096:
097: fail("sequence constructor failed to detect bad postalAddress.");
098: } catch (IllegalArgumentException e) {
099: // expected
100: }
101:
102: try {
103: new SignerLocation(new DERSequence(new DERTaggedObject(5,
104: postalAddress)));
105:
106: fail("sequence constructor failed to detect bad tag.");
107: } catch (IllegalArgumentException e) {
108: // expected
109: }
110: }
111:
112: private void checkConstruction(SignerLocation sl,
113: DERUTF8String countryName, DERUTF8String localityName,
114: ASN1Sequence postalAddress) throws IOException {
115: checkValues(sl, countryName, localityName, postalAddress);
116:
117: sl = SignerLocation.getInstance(sl);
118:
119: checkValues(sl, countryName, localityName, postalAddress);
120:
121: ASN1InputStream aIn = new ASN1InputStream(sl.toASN1Object()
122: .getEncoded());
123:
124: ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
125:
126: sl = SignerLocation.getInstance(seq);
127:
128: checkValues(sl, countryName, localityName, postalAddress);
129: }
130:
131: private void checkValues(SignerLocation sl,
132: DERUTF8String countryName, DERUTF8String localityName,
133: ASN1Sequence postalAddress) {
134: if (countryName != null) {
135: if (!countryName.equals(sl.getCountryName())) {
136: fail("countryNames don't match.");
137: }
138: } else if (sl.getCountryName() != null) {
139: fail("countryName found when none expected.");
140: }
141:
142: if (localityName != null) {
143: if (!localityName.equals(sl.getLocalityName())) {
144: fail("localityNames don't match.");
145: }
146: } else if (sl.getLocalityName() != null) {
147: fail("localityName found when none expected.");
148: }
149:
150: if (postalAddress != null) {
151: if (!postalAddress.equals(sl.getPostalAddress())) {
152: fail("postalAddresses don't match.");
153: }
154: } else if (sl.getPostalAddress() != null) {
155: fail("postalAddress found when none expected.");
156: }
157: }
158:
159: public static void main(String[] args) {
160: runTest(new SignerLocationUnitTest());
161: }
162: }
|