/labs/fgcp/src/test/java/org/jclouds/fujitsu/fgcp/compute/FGCPRestClientModuleTest.java

https://github.com/richardcloudsoft/legacy-jclouds · Java · 95 lines · 43 code · 13 blank · 39 comment · 0 complexity · 27d3bf63663aaa1412f5d3d181d432b8 MD5 · raw file

  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.fujitsu.fgcp.compute;
  20. import static org.testng.Assert.assertNotNull;
  21. import java.io.IOException;
  22. import java.net.URL;
  23. import java.security.KeyStore;
  24. import java.security.KeyStoreException;
  25. import java.security.NoSuchAlgorithmException;
  26. import java.security.cert.CertificateException;
  27. import java.security.spec.InvalidKeySpecException;
  28. import org.jclouds.crypto.Crypto;
  29. import org.jclouds.domain.Credentials;
  30. import org.jclouds.fujitsu.fgcp.suppliers.KeyStoreSupplier;
  31. import org.testng.annotations.BeforeTest;
  32. import org.testng.annotations.Test;
  33. import com.google.common.base.Suppliers;
  34. import com.google.inject.Guice;
  35. import com.google.inject.Injector;
  36. /**
  37. * @author Dies Koper
  38. */
  39. @Test(groups = "unit", testName = "FGCPRestClientModuleTest")
  40. public class FGCPRestClientModuleTest {
  41. protected FGCPRestClientModule module;
  42. protected Crypto crypto;
  43. @BeforeTest
  44. protected void createCrypto() {
  45. Injector i = Guice.createInjector();
  46. crypto = i.getInstance(Crypto.class);
  47. }
  48. @BeforeTest
  49. protected void createRestClientModule() {
  50. Injector i = Guice.createInjector();
  51. module = i.getInstance(FGCPRestClientModule.class);
  52. }
  53. public void testKeyStoreAsPkcs12() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException,
  54. KeyStoreException, CertificateException {
  55. assertNotNull(crypto);
  56. assertNotNull(module);
  57. // self-signed dummy cert:
  58. // keytool -genkey -alias test-fgcp -keyalg RSA -keysize 1024 -validity 5475 -dname "CN=localhost" -keystore
  59. // jclouds-test-fgcp.p12 -storepass jcloudsjclouds -storetype pkcs12
  60. String cert = "/certs/jclouds-test-fgcp.p12";
  61. URL url = this.getClass().getResource(cert);
  62. String certPath = url.getFile();
  63. KeyStore ks = new KeyStoreSupplier(crypto, Suppliers.ofInstance(new Credentials(certPath, "jcloudsjclouds")))
  64. .get();
  65. assertNotNull(ks.getCertificate("test-fgcp"), "cert with alias");
  66. }
  67. /*
  68. * public void testKeyStoreAsPEM() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException,
  69. * KeyStoreException, CertificateException { assertNotNull(crypto); assertNotNull(module);
  70. *
  71. * //openssl pkcs12 -nodes -in jclouds-test-fgcp.p12 -out jclouds-test-fgcp.pem // String privKeyFilename =
  72. * "D:\\UserCert.pem.pkcs12-nodes";//_nobags"; String cert = "/certs/jclouds-test-fgcp.pem"; String keyPassword =
  73. * "jcloudsjclouds";
  74. *
  75. * URL url = this.getClass().getResource(cert); String certPath = url.getFile(); Scanner scanner = new Scanner(new
  76. * File(certPath)); String content = scanner.useDelimiter("\\A").next();
  77. *
  78. * KeyStore ks = module.provideKeyStore(crypto, content, keyPassword);
  79. *
  80. * assertNotNull(ks.getCertificate("test-fgcp"), "cert with alias"); }
  81. */
  82. }