PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/depend/minasshd/sshd-core/src/test/java/org/apache/sshd/CipherTest.java

http://github.com/gaffo/scumd
Java | 185 lines | 145 code | 17 blank | 23 comment | 4 complexity | 5cb72baccc453fbf04e67e583a18149d MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF 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.apache.sshd;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.net.ServerSocket;
  23. import java.util.Arrays;
  24. import com.jcraft.jsch.JSch;
  25. import com.jcraft.jsch.Logger;
  26. import com.jcraft.jsch.UserInfo;
  27. import org.apache.sshd.common.Cipher;
  28. import org.apache.sshd.common.NamedFactory;
  29. import org.apache.sshd.common.Random;
  30. import org.apache.sshd.common.cipher.AES128CBC;
  31. import org.apache.sshd.common.cipher.AES192CBC;
  32. import org.apache.sshd.common.cipher.AES256CBC;
  33. import org.apache.sshd.common.cipher.BlowfishCBC;
  34. import org.apache.sshd.common.cipher.TripleDESCBC;
  35. import org.apache.sshd.common.cipher.CipherNone;
  36. import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
  37. import org.apache.sshd.common.random.BouncyCastleRandom;
  38. import org.apache.sshd.util.BogusPasswordAuthenticator;
  39. import org.apache.sshd.util.EchoShellFactory;
  40. import org.junit.After;
  41. import static org.junit.Assert.assertEquals;
  42. import org.junit.Ignore;
  43. import org.junit.Test;
  44. /**
  45. * Test Cipher algorithms.
  46. *
  47. * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  48. */
  49. public class CipherTest {
  50. private SshServer sshd;
  51. private int port;
  52. @Test
  53. public void testAES128CBC() throws Exception {
  54. setUp(new AES128CBC.Factory());
  55. runTest();
  56. }
  57. @Test
  58. @Ignore("AES192CBC is not always available by default")
  59. public void testAES192CBC() throws Exception {
  60. setUp(new AES192CBC.Factory());
  61. runTest();
  62. }
  63. @Test
  64. @Ignore("AES256CBC is not always available by default")
  65. public void testAES256CBC() throws Exception {
  66. setUp(new AES256CBC.Factory());
  67. runTest();
  68. }
  69. @Test
  70. public void testBlowfishCBC() throws Exception {
  71. setUp(new BlowfishCBC.Factory());
  72. runTest();
  73. }
  74. @Test
  75. public void testTripleDESCBC() throws Exception {
  76. setUp(new TripleDESCBC.Factory());
  77. runTest();
  78. }
  79. @Test
  80. public void loadTest() throws Exception {
  81. Random random = new BouncyCastleRandom();
  82. loadTest(new AES128CBC.Factory(), random);
  83. loadTest(new BlowfishCBC.Factory(), random);
  84. loadTest(new TripleDESCBC.Factory(), random);
  85. }
  86. protected void loadTest(NamedFactory<Cipher> factory, Random random) throws Exception {
  87. Cipher cipher = factory.create();
  88. byte[] key = new byte[cipher.getBlockSize()];
  89. byte[] iv = new byte[cipher.getIVSize()];
  90. random.fill(key, 0, key.length);
  91. random.fill(iv, 0, iv.length);
  92. cipher.init(Cipher.Mode.Encrypt, key, iv);
  93. byte[] input = new byte[cipher.getBlockSize()];
  94. random.fill(input, 0, input.length);
  95. long t0 = System.currentTimeMillis();
  96. for (int i = 0; i < 100000; i++) {
  97. cipher.update(input, 0, input.length);
  98. }
  99. long t1 = System.currentTimeMillis();
  100. System.err.println(factory.getName() + ": " + (t1 - t0) + " ms");
  101. }
  102. protected void setUp(NamedFactory<org.apache.sshd.common.Cipher> cipher) throws Exception {
  103. ServerSocket s = new ServerSocket(0);
  104. port = s.getLocalPort();
  105. s.close();
  106. sshd = SshServer.setUpDefaultServer();
  107. sshd.setPort(port);
  108. sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/hostkey.pem" }));
  109. sshd.setCipherFactories(Arrays.<NamedFactory<org.apache.sshd.common.Cipher>>asList(cipher));
  110. sshd.setShellFactory(new EchoShellFactory());
  111. sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator());
  112. sshd.start();
  113. }
  114. @After
  115. public void tearDown() throws Exception {
  116. if (sshd != null) {
  117. sshd.stop();
  118. }
  119. }
  120. protected void runTest() throws Exception {
  121. JSch sch = new JSch();
  122. JSch.setConfig("cipher.s2c", "aes128-cbc,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,none");
  123. JSch.setConfig("cipher.c2s", "aes128-cbc,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,none");
  124. sch.setLogger(new Logger() {
  125. public boolean isEnabled(int i) {
  126. return true;
  127. }
  128. public void log(int i, String s) {
  129. System.out.println("Log(jsch," + i + "): " + s);
  130. }
  131. });
  132. com.jcraft.jsch.Session s = sch.getSession("smx", "localhost", port);
  133. s.setUserInfo(new UserInfo() {
  134. public String getPassphrase() {
  135. return null;
  136. }
  137. public String getPassword() {
  138. return "smx";
  139. }
  140. public boolean promptPassword(String message) {
  141. return true;
  142. }
  143. public boolean promptPassphrase(String message) {
  144. return false;
  145. }
  146. public boolean promptYesNo(String message) {
  147. return true;
  148. }
  149. public void showMessage(String message) {
  150. }
  151. });
  152. s.connect();
  153. com.jcraft.jsch.Channel c = s.openChannel("shell");
  154. c.connect();
  155. OutputStream os = c.getOutputStream();
  156. InputStream is = c.getInputStream();
  157. for (int i = 0; i < 10; i++) {
  158. os.write("this is my command\n".getBytes());
  159. os.flush();
  160. byte[] data = new byte[512];
  161. int len = is.read(data);
  162. String str = new String(data, 0, len);
  163. assertEquals("this is my command\n", str);
  164. }
  165. c.disconnect();
  166. s.disconnect();
  167. }
  168. }