KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > ca > publisher > TestPublisher


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package se.anatom.ejbca.ca.publisher;
15
16 import java.io.BufferedReader JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStreamReader JavaDoc;
19 import java.security.cert.X509Certificate JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import javax.naming.Context JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.apache.log4j.Logger;
29 import org.ejbca.core.ejb.ca.publisher.IPublisherSessionHome;
30 import org.ejbca.core.ejb.ca.publisher.IPublisherSessionRemote;
31 import org.ejbca.core.ejb.ca.store.CertificateDataBean;
32 import org.ejbca.core.model.ca.crl.RevokedCertInfo;
33 import org.ejbca.core.model.ca.publisher.ActiveDirectoryPublisher;
34 import org.ejbca.core.model.ca.publisher.BasePublisher;
35 import org.ejbca.core.model.ca.publisher.CustomPublisherContainer;
36 import org.ejbca.core.model.ca.publisher.GeneralPurposeCustomPublisher;
37 import org.ejbca.core.model.ca.publisher.LdapPublisher;
38 import org.ejbca.core.model.ca.publisher.PublisherConnectionException;
39 import org.ejbca.core.model.ca.publisher.PublisherException;
40 import org.ejbca.core.model.ca.publisher.PublisherExistsException;
41 import org.ejbca.core.model.log.Admin;
42 import org.ejbca.util.Base64;
43 import org.ejbca.util.CertTools;
44
45
46
47 /**
48  * Tests Publishers.
49  *
50  * @version $Id: TestPublisher.java,v 1.6.6.3 2007/04/10 10:42:21 jeklund Exp $
51  */

52 public class TestPublisher extends TestCase {
53     
54     static byte[] testcert = Base64.decode(("MIICWzCCAcSgAwIBAgIIJND6Haa3NoAwDQYJKoZIhvcNAQEFBQAwLzEPMA0GA1UE"
55             + "AxMGVGVzdENBMQ8wDQYDVQQKEwZBbmFUb20xCzAJBgNVBAYTAlNFMB4XDTAyMDEw"
56             + "ODA5MTE1MloXDTA0MDEwODA5MjE1MlowLzEPMA0GA1UEAxMGMjUxMzQ3MQ8wDQYD"
57             + "VQQKEwZBbmFUb20xCzAJBgNVBAYTAlNFMIGdMA0GCSqGSIb3DQEBAQUAA4GLADCB"
58             + "hwKBgQCQ3UA+nIHECJ79S5VwI8WFLJbAByAnn1k/JEX2/a0nsc2/K3GYzHFItPjy"
59             + "Bv5zUccPLbRmkdMlCD1rOcgcR9mmmjMQrbWbWp+iRg0WyCktWb/wUS8uNNuGQYQe"
60             + "ACl11SAHFX+u9JUUfSppg7SpqFhSgMlvyU/FiGLVEHDchJEdGQIBEaOBgTB/MA8G"
61             + "A1UdEwEB/wQFMAMBAQAwDwYDVR0PAQH/BAUDAwegADAdBgNVHQ4EFgQUyxKILxFM"
62             + "MNujjNnbeFpnPgB76UYwHwYDVR0jBBgwFoAUy5k/bKQ6TtpTWhsPWFzafOFgLmsw"
63             + "GwYDVR0RBBQwEoEQMjUxMzQ3QGFuYXRvbS5zZTANBgkqhkiG9w0BAQUFAAOBgQAS"
64             + "5wSOJhoVJSaEGHMPw6t3e+CbnEL9Yh5GlgxVAJCmIqhoScTMiov3QpDRHOZlZ15c"
65             + "UlqugRBtORuA9xnLkrdxYNCHmX6aJTfjdIW61+o/ovP0yz6ulBkqcKzopAZLirX+"
66             + "XSWf2uI9miNtxYMVnbQ1KPdEAt7Za3OQR6zcS0lGKg==").getBytes());
67     
68     static byte[] testcacert = Base64.decode(("MIICLDCCAZWgAwIBAgIISDzEq64yCAcwDQYJKoZIhvcNAQEFBQAwLzEPMA0GA1UE"
69             + "AxMGVGVzdENBMQ8wDQYDVQQKEwZBbmFUb20xCzAJBgNVBAYTAlNFMB4XDTAxMTIw"
70             + "NDA5MzI1N1oXDTAzMTIwNDA5NDI1N1owLzEPMA0GA1UEAxMGVGVzdENBMQ8wDQYD"
71             + "VQQKEwZBbmFUb20xCzAJBgNVBAYTAlNFMIGdMA0GCSqGSIb3DQEBAQUAA4GLADCB"
72             + "hwKBgQCnhOvkaj+9Qmt9ZseVn8Jhl6ewTrAOK3c9usxBhiGs+TalGjuAK37bbnbZ"
73             + "rlzCZpEsjSZYgXS++3NttiDbPzATkV/c33uIzBHjyk8/paOmTrkIux8hbIYMce+/"
74             + "WTYnAM3J41mSuDMy2yZxZ72Yntzqg4UUXiW+JQDkhGx8ZtcSSwIBEaNTMFEwDwYD"
75             + "VR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUy5k/bKQ6TtpTWhsPWFzafOFgLmswHwYD"
76             + "VR0jBBgwFoAUy5k/bKQ6TtpTWhsPWFzafOFgLmswDQYJKoZIhvcNAQEFBQADgYEA"
77             + "gHzQLoqLobU43lKvQCiZbYWEXHTf3AdzUd6aMOYOM80iKS9kgrMsnKjp61IFCZwr"
78             + "OcY1lOkpjADUTSqfVJWuF1z5k9c1bXnh5zu48LA2r2dlbHqG8twMQ+tPh1MYa3lV"
79             + "ugWhKqArGEawICRPUZJrLy/eDbCgVB4QT3rC7rOJOH0=").getBytes());
80     
81     static byte[] testcrl = Base64.decode(("MIIDEzCCAnwCAQEwDQYJKoZIhvcNAQEFBQAwLzEPMA0GA1UEAxMGVGVzdENBMQ8w"
82             + "DQYDVQQKEwZBbmFUb20xCzAJBgNVBAYTAlNFFw0wMjAxMDMxMjExMTFaFw0wMjAx"
83             + "MDIxMjExMTFaMIIB5jAZAggfi2rKt4IrZhcNMDIwMTAzMTIxMDUxWjAZAghAxdYk"
84             + "7mJxkxcNMDIwMTAzMTIxMDUxWjAZAgg+lCCL+jumXxcNMDIwMTAzMTIxMDUyWjAZ"
85             + "Agh4AAPpzSk/+hcNMDIwMTAzMTIxMDUyWjAZAghkhx9SFvxAgxcNMDIwMTAzMTIx"
86             + "MDUyWjAZAggj4g5SUqaGvBcNMDIwMTAzMTIxMDUyWjAZAghT+nqB0c6vghcNMDIw"
87             + "MTAzMTE1MzMzWjAZAghsBWMAA55+7BcNMDIwMTAzMTE1MzMzWjAZAgg8h0t6rKQY"
88             + "ZhcNMDIwMTAzMTE1MzMzWjAZAgh7KFsd40ICwhcNMDIwMTAzMTE1MzM0WjAZAggA"
89             + "kFlDNU8ubxcNMDIwMTAzMTE1MzM0WjAZAghyQfo1XNl0EBcNMDIwMTAzMTE1MzM0"
90             + "WjAZAggC5Pz7wI/29hcNMDIwMTAyMTY1NDMzWjAZAggEWvzRRpFGoRcNMDIwMTAy"
91             + "MTY1NDMzWjAZAggC7Q2W0iXswRcNMDIwMTAyMTY1NDMzWjAZAghrfwG3t6vCiBcN"
92             + "MDIwMTAyMTY1NDMzWjAZAgg5C+4zxDGEjhcNMDIwMTAyMTY1NDMzWjAZAggX/olM"
93             + "45KxnxcNMDIwMTAyMTY1NDMzWqAvMC0wHwYDVR0jBBgwFoAUy5k/bKQ6TtpTWhsP"
94             + "WFzafOFgLmswCgYDVR0UBAMCAQQwDQYJKoZIhvcNAQEFBQADgYEAPvYDZofCOopw"
95             + "OCKVGaK1aPpHkJmu5Xi1XtRGO9DhmnSZ28hrNu1A5R8OQI43Z7xFx8YK3S56GRuY"
96             + "0EGU/RgM3AWhyTAps66tdyipRavKmH6MMrN4ypW/qbhsd4o8JE9pxxn9zsQaNxYZ"
97             + "SNbXM2/YxkdoRSjkrbb9DUdCmCR/kEA=").getBytes());
98     
99     private static Logger log = Logger.getLogger(TestPublisher.class);
100     private static Context JavaDoc ctx;
101     private static IPublisherSessionRemote pub;
102
103     private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
104     
105     private String JavaDoc externalCommand = "ls";
106     private String JavaDoc externalCommand2 = "cmd.exe /c dir";
107     private String JavaDoc invalidOption = " --------------:";
108     private String JavaDoc invalidOption2 = " /parameterthatdoesnotexist";
109
110     /**
111      * Creates a new TestPublisher object.
112      *
113      * @param name name
114      */

115     public TestPublisher(String JavaDoc name) {
116         super(name);
117     }
118     
119     protected void setUp() throws Exception JavaDoc {
120         log.debug(">setUp()");
121         ctx = getInitialContext();
122         
123         Object JavaDoc obj = ctx.lookup("PublisherSession");
124         IPublisherSessionHome home = (IPublisherSessionHome) javax.rmi.PortableRemoteObject.narrow(obj,
125                 IPublisherSessionHome.class);
126         pub = home.create();
127         
128         CertTools.installBCProvider();
129         
130         log.debug("<setUp()");
131         
132     }
133     
134     protected void tearDown() throws Exception JavaDoc {
135     }
136     
137     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
138         log.debug(">getInitialContext");
139         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
140         log.debug("<getInitialContext");
141         
142         return ctx;
143     }
144     
145     /**
146      * adds ldap publisher
147      *
148      * @throws Exception error
149      */

150     public void test01AddLDAPPublisher() throws Exception JavaDoc {
151         log.debug(">test01AddLDAPPublisher()");
152         boolean ret = false;
153         try {
154             LdapPublisher publisher = new LdapPublisher();
155             publisher.setHostname("localhost");
156             publisher.setDescription("Used in Junit Test, Remove this one");
157             pub.addPublisher(admin, "TESTLDAP", publisher);
158             ret = true;
159         } catch (PublisherExistsException pee) {
160         }
161         
162         assertTrue("Creating LDAP Publisher failed", ret);
163         log.debug("<test01AddLDAPPublisher()");
164     }
165     
166     /**
167      * adds ad publisher
168      *
169      * @throws Exception error
170      */

171     public void test02AddADPublisher() throws Exception JavaDoc {
172         log.debug(">test02AddADPublisher() ");
173         boolean ret = false;
174         try {
175             ActiveDirectoryPublisher publisher = new ActiveDirectoryPublisher();
176             publisher.setHostname("localhost");
177             publisher.setDescription("Used in Junit Test, Remove this one");
178             pub.addPublisher(admin, "TESTAD", publisher);
179             ret = true;
180         } catch (PublisherExistsException pee) {
181         }
182         
183         assertTrue("Creating AD Publisher failed", ret);
184         log.debug("<test02AddADPublisher() ");
185     }
186     
187     /**
188      * adds custom publisher
189      *
190      * @throws Exception error
191      */

192     public void test03AddCustomPublisher() throws Exception JavaDoc {
193         log.debug(">test03AddCustomPublisher()");
194         boolean ret = false;
195         try {
196             CustomPublisherContainer publisher = new CustomPublisherContainer();
197             publisher.setClassPath("org.ejbca.core.model.ca.publisher.DummyCustomPublisher");
198             publisher.setDescription("Used in Junit Test, Remove this one");
199             pub.addPublisher(admin, "TESTDUMMYCUSTOM", publisher);
200             ret = true;
201         } catch (PublisherExistsException pee) {
202         }
203         
204         assertTrue("Creating Custom Publisher failed", ret);
205         
206         log.debug("<test03AddCustomPublisher()");
207     }
208     
209     /**
210      * renames publisher
211      *
212      * @throws Exception error
213      */

214     public void test04RenamePublisher() throws Exception JavaDoc {
215         log.debug(">test04RenamePublisher()");
216         
217         boolean ret = false;
218         try {
219             pub.renamePublisher(admin, "TESTDUMMYCUSTOM", "TESTNEWDUMMYCUSTOM");
220             ret = true;
221         } catch (PublisherExistsException pee) {
222         }
223         assertTrue("Renaming Custom Publisher failed", ret);
224         
225         
226         log.debug("<test04RenamePublisher()");
227     }
228     
229     /**
230      * clones publisher
231      *
232      * @throws Exception error
233      */

234     public void test05ClonePublisher() throws Exception JavaDoc {
235         log.debug(">test05ClonePublisher()");
236         
237         boolean ret = false;
238         pub.clonePublisher(admin, "TESTNEWDUMMYCUSTOM", "TESTCLONEDUMMYCUSTOM");
239         ret = true;
240         assertTrue("Cloning Custom Publisher failed", ret);
241         
242         log.debug("<test05ClonePublisher()");
243     }
244     
245     
246     /**
247      * edits publisher
248      *
249      * @throws Exception error
250      */

251     public void test06EditPublisher() throws Exception JavaDoc {
252         log.debug(">test06EditPublisher()");
253         
254         boolean ret = false;
255         
256         BasePublisher publisher = pub.getPublisher(admin, "TESTCLONEDUMMYCUSTOM");
257         publisher.setDescription(publisher.getDescription().toUpperCase());
258         pub.changePublisher(admin, "TESTCLONEDUMMYCUSTOM", publisher);
259         ret = true;
260         
261         assertTrue("Editing Custom Publisher failed", ret);
262         
263         
264         log.debug("<test06EditPublisher()");
265     }
266     
267     /**
268      * stores a cert to the dummy publisher
269      *
270      * @throws Exception error
271      */

272     public void test07StoreCertToDummy() throws Exception JavaDoc {
273         log.debug(">test07StoreCertToDummy()");
274         X509Certificate JavaDoc cert = CertTools.getCertfromByteArray(testcert);
275         ArrayList JavaDoc publishers = new ArrayList JavaDoc();
276         publishers.add(new Integer JavaDoc(pub.getPublisherId(admin, "TESTNEWDUMMYCUSTOM")));
277         
278         boolean ret = pub.storeCertificate(new Admin(Admin.TYPE_INTERNALUSER), publishers, cert, "test05", "foo123", null, CertificateDataBean.CERT_ACTIVE, CertificateDataBean.CERTTYPE_ENDENTITY, -1, RevokedCertInfo.NOT_REVOKED, null);
279         assertTrue("Storing certificate to dummy publisher failed", ret);
280         log.debug("<test07StoreCertToDummyr()");
281     }
282     
283     /**
284      * stores a cert to the dummy publisher
285      *
286      * @throws Exception error
287      */

288     public void test08storeCRLToDummy() throws Exception JavaDoc {
289         log.debug(">test08storeCRLToDummy()");
290         
291         ArrayList JavaDoc publishers = new ArrayList JavaDoc();
292         publishers.add(new Integer JavaDoc(pub.getPublisherId(admin, "TESTNEWDUMMYCUSTOM")));
293         boolean ret = pub.storeCRL(admin, publishers, testcrl, null, 1);
294         assertTrue("Storing CRL to dummy publisher failed", ret);
295         
296         log.debug("<test08storeCRLToDummy()");
297     }
298     
299     
300     /**
301      * removes all publishers
302      *
303      * @throws Exception error
304      */

305     public void test09removePublishers() throws Exception JavaDoc {
306         log.debug(">test09removePublishers()");
307         boolean ret = false;
308         try {
309             pub.removePublisher(admin, "TESTLDAP");
310             pub.removePublisher(admin, "TESTAD");
311             pub.removePublisher(admin, "TESTNEWDUMMYCUSTOM");
312             pub.removePublisher(admin, "TESTCLONEDUMMYCUSTOM");
313             ret = true;
314         } catch (Exception JavaDoc pee) {
315         }
316         assertTrue("Removing Publisher failed", ret);
317         
318         log.debug("<test09removePublishers()");
319     }
320
321     /**
322      * Test normal operation of GeneralPurposeCustomPublisher.
323      *
324      * @throws Exception error
325      */

326     public void test10GenPurpCustPubl() throws Exception JavaDoc {
327         log.debug(">test10GenPurpCustPubl()");
328         
329         GeneralPurposeCustomPublisher gpcPublisher = null;
330         Properties JavaDoc props = new Properties JavaDoc();
331     
332         //Make sure an external command exists for testing purposes
333
boolean ret = true;
334         if ( isValidCommand(externalCommand) ) {
335             ret = false;
336         } else if ( isValidCommand(externalCommand2) ) {
337             externalCommand = externalCommand2;
338             invalidOption = invalidOption2;
339             ret = false;
340         }
341         assertFalse("This test requires \"" + externalCommand + "\" or \"" + externalCommand2 + "\"to be available.", ret);
342         // Create
343
gpcPublisher = new GeneralPurposeCustomPublisher();
344         // Make sure it fails without a given external command
345
ret = false;
346         try {
347             props.setProperty(GeneralPurposeCustomPublisher.crlExternalCommandPropertyName, "");
348             gpcPublisher.init(props);
349             ret = gpcPublisher.storeCRL(admin, testcrl, null, 1);
350         } catch (PublisherException e) {
351         }
352         assertFalse("Store CRL with GeneralPurposeCustomPublisher did not failed with invalid properties.", ret);
353         // Test function by calling a command that is available on most platforms
354
ret = false;
355         try {
356             props.setProperty(GeneralPurposeCustomPublisher.crlExternalCommandPropertyName, externalCommand);
357             gpcPublisher.init(props);
358             ret = gpcPublisher.storeCRL(admin, testcrl, null, 1);
359         } catch (PublisherException e) {
360             e.printStackTrace();
361         }
362         assertTrue("Store CRL with GeneralPurposeCustomPublisher failed.", ret);
363         log.debug("<test10GenPurpCustPubl()");
364     } // test10GenPurpCustPubl
365

366     /**
367      * Verify that GeneralPurposeCustomPublisher will fail on an error code from
368      * an external application.
369      *
370      * @throws Exception error
371      */

372     public void test11GenPurpCustPublErrorCode() throws Exception JavaDoc {
373         log.debug(">test11GenPurpCustPublErrorCode()");
374         
375         GeneralPurposeCustomPublisher gpcPublisher = null;
376         Properties JavaDoc props = new Properties JavaDoc();
377     
378         //Make sure an external command exists for testing purposes
379
boolean ret = true;
380         if ( isValidCommand(externalCommand) ) {
381             ret = false;
382         } else if ( isValidCommand(externalCommand2) ) {
383             externalCommand = externalCommand2;
384             invalidOption = invalidOption2;
385             ret = false;
386         }
387         assertFalse("This test requires \"" + externalCommand + "\" or \"" + externalCommand2 + "\"to be available.", ret);
388         // Create
389
gpcPublisher = new GeneralPurposeCustomPublisher();
390         // Test function by calling a command that is available on most platforms with invalid option
391
ret = false;
392         try {
393             props.setProperty(GeneralPurposeCustomPublisher.crlExternalCommandPropertyName, externalCommand + invalidOption);
394             props.setProperty(GeneralPurposeCustomPublisher.crlFailOnErrorCodePropertyName, "true");
395             props.setProperty(GeneralPurposeCustomPublisher.crlFailOnStandardErrorPropertyName, "false");
396             gpcPublisher.init(props);
397             ret = gpcPublisher.storeCRL(admin, testcrl, null, 1);
398         } catch (PublisherException e) {
399         }
400         assertFalse("Store CRL with GeneralPurposeCustomPublisher did not fail on errorcode.", ret);
401         ret = false;
402         try {
403             props.setProperty(GeneralPurposeCustomPublisher.certExternalCommandPropertyName, externalCommand + invalidOption);
404             props.setProperty(GeneralPurposeCustomPublisher.certFailOnErrorCodePropertyName, "true");
405             props.setProperty(GeneralPurposeCustomPublisher.certFailOnStandardErrorPropertyName, "false");
406             gpcPublisher.init(props);
407             ret = gpcPublisher.storeCRL(admin, testcrl, null, 1);
408         } catch (PublisherException e) {
409         }
410         assertFalse("Store cert with GeneralPurposeCustomPublisher did not fail on errorcode.", ret);
411         ret = false;
412         try {
413             props.setProperty(GeneralPurposeCustomPublisher.revokeExternalCommandPropertyName, externalCommand + invalidOption);
414             props.setProperty(GeneralPurposeCustomPublisher.revokeFailOnErrorCodePropertyName, "true");
415             props.setProperty(GeneralPurposeCustomPublisher.revokeFailOnStandardErrorPropertyName, "false");
416             gpcPublisher.init(props);
417             ret = gpcPublisher.storeCRL(admin, testcrl, null, 1);
418         } catch (PublisherException e) {
419         }
420         assertFalse("Revoke cert with GeneralPurposeCustomPublisher did not fail on errorcode.", ret);
421         log.debug("<test11GenPurpCustPublErrorCode()");
422     } // test11GenPurpCustPublErrorCode
423

424     /**
425      * Verify that GeneralPurposeCustomPublisher will fail on output to standard
426      * error from an external application.
427      *
428      * @throws Exception error
429      */

430     public void test12GenPurpCustPublStandardError() throws Exception JavaDoc {
431         log.debug(">test12GenPurpCustPublStandardError()");
432         
433         GeneralPurposeCustomPublisher gpcPublisher = null;
434         Properties JavaDoc props = new Properties JavaDoc();
435     
436         //Make sure an external command exists for testing purposes
437
boolean ret = true;
438         if ( isValidCommand(externalCommand) ) {
439             ret = false;
440         } else if ( isValidCommand(externalCommand2) ) {
441             externalCommand = externalCommand2;
442             invalidOption = invalidOption2;
443             ret = false;
444         }
445         assertFalse("This test requires \"" + externalCommand + "\" or \"" + externalCommand2 + "\"to be available.", ret);
446         // Create
447
gpcPublisher = new GeneralPurposeCustomPublisher();
448         // Test function by calling a command that is available on most platforms with invalid option
449
ret = false;
450         try {
451             props.setProperty(GeneralPurposeCustomPublisher.crlExternalCommandPropertyName, externalCommand + invalidOption);
452             props.setProperty(GeneralPurposeCustomPublisher.crlFailOnErrorCodePropertyName, "false");
453             props.setProperty(GeneralPurposeCustomPublisher.crlFailOnStandardErrorPropertyName, "true");
454             gpcPublisher.init(props);
455             ret = gpcPublisher.storeCRL(admin, testcrl, null, 1);
456         } catch (PublisherException e) {
457         }
458         assertFalse("Store CRL with GeneralPurposeCustomPublisher did not fail on standard error.", ret);
459         ret = false;
460         try {
461             props.setProperty(GeneralPurposeCustomPublisher.certExternalCommandPropertyName, externalCommand + invalidOption);
462             props.setProperty(GeneralPurposeCustomPublisher.certFailOnErrorCodePropertyName, "false");
463             props.setProperty(GeneralPurposeCustomPublisher.certFailOnStandardErrorPropertyName, "true");
464             gpcPublisher.init(props);
465             ret = gpcPublisher.storeCRL(admin, testcrl, null, 1);
466         } catch (PublisherException e) {
467         }
468         assertFalse("Store cert with GeneralPurposeCustomPublisher did not fail on standard error.", ret);
469         ret = false;
470         try {
471             props.setProperty(GeneralPurposeCustomPublisher.revokeExternalCommandPropertyName, externalCommand + invalidOption);
472             props.setProperty(GeneralPurposeCustomPublisher.revokeFailOnErrorCodePropertyName, "false");
473             props.setProperty(GeneralPurposeCustomPublisher.revokeFailOnStandardErrorPropertyName, "true");
474             gpcPublisher.init(props);
475             ret = gpcPublisher.storeCRL(admin, testcrl, null, 1);
476         } catch (PublisherException e) {
477         }
478         assertFalse("Revoke cert with GeneralPurposeCustomPublisher did not fail on standard error.", ret);
479         log.debug("<test12GenPurpCustPublStandardError()");
480     } // test12GenPurpCustPublStandardError
481

482     /**
483      * Test that the GeneralPurposeCustomPublisher fails when the external executable file does not exist.
484      *
485      * @throws Exception
486      */

487     public void test13GenPurpCustPublConnection() throws Exception JavaDoc {
488         log.debug(">test13GenPurpCustPublConnection()");
489         GeneralPurposeCustomPublisher gpcPublisher = null;
490         Properties JavaDoc props = new Properties JavaDoc();
491         // Create
492
gpcPublisher = new GeneralPurposeCustomPublisher();
493         // Test connection separatly for all publishers with invalid filename
494
boolean ret = false;
495         try {
496             props.setProperty(GeneralPurposeCustomPublisher.crlExternalCommandPropertyName, "randomfilenamethatdoesnotexistandneverwill8998752");
497             gpcPublisher.init(props);
498             gpcPublisher.testConnection(admin);
499             ret = true;
500         } catch (PublisherConnectionException e) {
501         }
502         assertFalse("testConnection reported all ok, but commandfile does not exist!", ret);
503         ret = false;
504         try {
505             props.setProperty(GeneralPurposeCustomPublisher.certExternalCommandPropertyName, "randomfilenamethatdoesnotexistandneverwill8998752");
506             gpcPublisher.init(props);
507             gpcPublisher.testConnection(admin);
508             ret = true;
509         } catch (PublisherConnectionException e) {
510         }
511         assertFalse("testConnection reported all ok, but commandfile does not exist!", ret);
512         ret = false;
513         try {
514             props.setProperty(GeneralPurposeCustomPublisher.revokeExternalCommandPropertyName, "randomfilenamethatdoesnotexistandneverwill8998752");
515             gpcPublisher.init(props);
516             gpcPublisher.testConnection(admin);
517             ret = true;
518         } catch (PublisherConnectionException e) {
519         }
520         assertFalse("testConnection reported all ok, but commandfile does not exist!", ret);
521         log.debug("<test12GenPurpCustPublStandardError()");
522     } // test13GenPurpCustPublConnection
523

524     /**
525      * Tries to execute the argument and return true if no exception was thrown and the command returned 0.
526      *
527      * @param externalCommandToTest The String to run.
528      * @return Returns false on error.
529      */

530     private boolean isValidCommand(String JavaDoc externalCommandToTest) {
531         boolean ret = false;
532         try {
533             String JavaDoc[] cmdarray = externalCommandToTest.split("\\s");
534             Process JavaDoc externalProcess = Runtime.getRuntime().exec( cmdarray, null, null );
535             BufferedReader JavaDoc br = new BufferedReader JavaDoc( new InputStreamReader JavaDoc( externalProcess.getInputStream() ) );
536             while (br.readLine() != null) { }
537             if ( externalProcess.waitFor() == 0 ) {
538                 ret = true;
539             }
540         } catch (IOException JavaDoc e) {
541         } catch (InterruptedException JavaDoc e) {
542         }
543         return ret;
544     } // isValidCommand
545
} // TestPublisher
546
Popular Tags