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