1 13 14 package se.anatom.ejbca.ca.publisher; 15 16 import java.io.BufferedReader ; 17 import java.io.IOException ; 18 import java.io.InputStreamReader ; 19 import java.security.cert.X509Certificate ; 20 import java.util.ArrayList ; 21 import java.util.Properties ; 22 23 import javax.naming.Context ; 24 import javax.naming.NamingException ; 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 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 + "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 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 ctx; 101 private static IPublisherSessionRemote pub; 102 103 private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER); 104 105 private String externalCommand = "ls"; 106 private String externalCommand2 = "cmd.exe /c dir"; 107 private String invalidOption = " --------------:"; 108 private String invalidOption2 = " /parameterthatdoesnotexist"; 109 110 115 public TestPublisher(String name) { 116 super(name); 117 } 118 119 protected void setUp() throws Exception { 120 log.debug(">setUp()"); 121 ctx = getInitialContext(); 122 123 Object 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 { 135 } 136 137 private Context getInitialContext() throws NamingException { 138 log.debug(">getInitialContext"); 139 Context ctx = new javax.naming.InitialContext (); 140 log.debug("<getInitialContext"); 141 142 return ctx; 143 } 144 145 150 public void test01AddLDAPPublisher() throws Exception { 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 171 public void test02AddADPublisher() throws Exception { 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 192 public void test03AddCustomPublisher() throws Exception { 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 214 public void test04RenamePublisher() throws Exception { 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 234 public void test05ClonePublisher() throws Exception { 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 251 public void test06EditPublisher() throws Exception { 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 272 public void test07StoreCertToDummy() throws Exception { 273 log.debug(">test07StoreCertToDummy()"); 274 X509Certificate cert = CertTools.getCertfromByteArray(testcert); 275 ArrayList publishers = new ArrayList (); 276 publishers.add(new Integer (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 288 public void test08storeCRLToDummy() throws Exception { 289 log.debug(">test08storeCRLToDummy()"); 290 291 ArrayList publishers = new ArrayList (); 292 publishers.add(new Integer (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 305 public void test09removePublishers() throws Exception { 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 pee) { 315 } 316 assertTrue("Removing Publisher failed", ret); 317 318 log.debug("<test09removePublishers()"); 319 } 320 321 326 public void test10GenPurpCustPubl() throws Exception { 327 log.debug(">test10GenPurpCustPubl()"); 328 329 GeneralPurposeCustomPublisher gpcPublisher = null; 330 Properties props = new Properties (); 331 332 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 gpcPublisher = new GeneralPurposeCustomPublisher(); 344 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 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 } 366 372 public void test11GenPurpCustPublErrorCode() throws Exception { 373 log.debug(">test11GenPurpCustPublErrorCode()"); 374 375 GeneralPurposeCustomPublisher gpcPublisher = null; 376 Properties props = new Properties (); 377 378 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 gpcPublisher = new GeneralPurposeCustomPublisher(); 390 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 } 424 430 public void test12GenPurpCustPublStandardError() throws Exception { 431 log.debug(">test12GenPurpCustPublStandardError()"); 432 433 GeneralPurposeCustomPublisher gpcPublisher = null; 434 Properties props = new Properties (); 435 436 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 gpcPublisher = new GeneralPurposeCustomPublisher(); 448 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 } 482 487 public void test13GenPurpCustPublConnection() throws Exception { 488 log.debug(">test13GenPurpCustPublConnection()"); 489 GeneralPurposeCustomPublisher gpcPublisher = null; 490 Properties props = new Properties (); 491 gpcPublisher = new GeneralPurposeCustomPublisher(); 493 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 } 524 530 private boolean isValidCommand(String externalCommandToTest) { 531 boolean ret = false; 532 try { 533 String [] cmdarray = externalCommandToTest.split("\\s"); 534 Process externalProcess = Runtime.getRuntime().exec( cmdarray, null, null ); 535 BufferedReader br = new BufferedReader ( new InputStreamReader ( externalProcess.getInputStream() ) ); 536 while (br.readLine() != null) { } 537 if ( externalProcess.waitFor() == 0 ) { 538 ret = true; 539 } 540 } catch (IOException e) { 541 } catch (InterruptedException e) { 542 } 543 return ret; 544 } } | Popular Tags |