PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/compute/src/test/java/org/jclouds/ssh/config/RsaSshKeyPairGeneratorTest.java

https://github.com/richardcloudsoft/legacy-jclouds
Java | 98 lines | 61 code | 16 blank | 21 comment | 0 complexity | 35689037d52278d9c45733be50daf328 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.ssh.config;
  20. import static org.easymock.EasyMock.createMock;
  21. import static org.easymock.EasyMock.expect;
  22. import static org.easymock.EasyMock.replay;
  23. import static org.easymock.EasyMock.verify;
  24. import static org.jclouds.crypto.PemsTest.PRIVATE_KEY;
  25. import static org.jclouds.crypto.PemsTest.PUBLIC_KEY;
  26. import static org.testng.Assert.assertEquals;
  27. import java.io.IOException;
  28. import java.security.KeyFactory;
  29. import java.security.KeyPair;
  30. import java.security.KeyPairGenerator;
  31. import java.security.NoSuchAlgorithmException;
  32. import java.security.PrivateKey;
  33. import java.security.PublicKey;
  34. import java.security.SecureRandom;
  35. import java.security.interfaces.RSAPublicKey;
  36. import java.security.spec.InvalidKeySpecException;
  37. import org.jclouds.crypto.Crypto;
  38. import org.jclouds.crypto.Pems;
  39. import org.jclouds.io.Payloads;
  40. import org.jclouds.ssh.SshKeys;
  41. import org.jclouds.ssh.internal.RsaSshKeyPairGenerator;
  42. import org.testng.annotations.BeforeClass;
  43. import org.testng.annotations.Test;
  44. import com.google.common.collect.ImmutableMap;
  45. import com.google.inject.AbstractModule;
  46. import com.google.inject.Guice;
  47. /**
  48. * @author Adrian Cole
  49. */
  50. @Test(groups = "unit", testName = "RsaSshKeyPairGeneratorTest")
  51. public class RsaSshKeyPairGeneratorTest {
  52. private static final String lineSeparator = System.getProperty("line.separator");
  53. private KeyPair keyPair;
  54. private String openSshKey;
  55. @BeforeClass
  56. public void setup() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException {
  57. KeyFactory keyfactory = KeyFactory.getInstance("RSA");
  58. PrivateKey privateKey = keyfactory.generatePrivate(Pems.privateKeySpec(Payloads.newStringPayload(PRIVATE_KEY)));
  59. PublicKey publicKey = keyfactory.generatePublic(Pems.publicKeySpec(Payloads.newStringPayload(PUBLIC_KEY)));
  60. keyPair = new KeyPair(publicKey, privateKey);
  61. openSshKey = SshKeys.encodeAsOpenSSH(RSAPublicKey.class.cast(publicKey));
  62. }
  63. @Test
  64. public void testApply() {
  65. final Crypto crypto = createMock(Crypto.class);
  66. KeyPairGenerator rsaKeyPairGenerator = createMock(KeyPairGenerator.class);
  67. final SecureRandom secureRandom = createMock(SecureRandom.class);
  68. expect(crypto.rsaKeyPairGenerator()).andReturn(rsaKeyPairGenerator);
  69. rsaKeyPairGenerator.initialize(2048, secureRandom);
  70. expect(rsaKeyPairGenerator.genKeyPair()).andReturn(keyPair);
  71. replay(crypto, rsaKeyPairGenerator, secureRandom);
  72. RsaSshKeyPairGenerator supplier = Guice.createInjector(new AbstractModule(){
  73. protected void configure() {
  74. bind(Crypto.class).toInstance(crypto);
  75. bind(SecureRandom.class).toInstance(secureRandom);
  76. }
  77. }).getInstance(RsaSshKeyPairGenerator.class);
  78. assertEquals(supplier.get(),
  79. ImmutableMap.of("public", openSshKey, "private", PRIVATE_KEY.replaceAll("\n", lineSeparator)));
  80. verify(crypto, rsaKeyPairGenerator, secureRandom);
  81. }
  82. }