選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

JSchSshTest.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. //TODO(ms): move to org.eclipse.jgit.ssh.jsch in 6.0
  11. package org.eclipse.jgit.transport;
  12. import static org.junit.Assert.assertTrue;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.io.UncheckedIOException;
  16. import java.nio.file.Files;
  17. import java.util.Arrays;
  18. import org.eclipse.jgit.errors.TransportException;
  19. import org.eclipse.jgit.junit.ssh.SshTestBase;
  20. import org.eclipse.jgit.lib.Constants;
  21. import org.eclipse.jgit.transport.OpenSshConfig.Host;
  22. import org.eclipse.jgit.util.FS;
  23. import org.junit.experimental.theories.Theories;
  24. import org.junit.runner.RunWith;
  25. import com.jcraft.jsch.JSch;
  26. import com.jcraft.jsch.JSchException;
  27. import com.jcraft.jsch.Session;
  28. @RunWith(Theories.class)
  29. public class JSchSshTest extends SshTestBase {
  30. private class TestSshSessionFactory extends JschConfigSessionFactory {
  31. @Override
  32. protected void configure(Host hc, Session session) {
  33. // Nothing
  34. }
  35. @Override
  36. public synchronized RemoteSession getSession(URIish uri,
  37. CredentialsProvider credentialsProvider, FS fs, int tms)
  38. throws TransportException {
  39. return super.getSession(uri, credentialsProvider, fs, tms);
  40. }
  41. @Override
  42. protected JSch createDefaultJSch(FS fs) throws JSchException {
  43. JSch defaultJSch = super.createDefaultJSch(fs);
  44. if (knownHosts.exists()) {
  45. defaultJSch.setKnownHosts(knownHosts.getAbsolutePath());
  46. }
  47. return defaultJSch;
  48. }
  49. }
  50. @Override
  51. protected SshSessionFactory createSessionFactory() {
  52. return new TestSshSessionFactory();
  53. }
  54. @Override
  55. protected void installConfig(String... config) {
  56. SshSessionFactory factory = getSessionFactory();
  57. assertTrue(factory instanceof JschConfigSessionFactory);
  58. JschConfigSessionFactory j = (JschConfigSessionFactory) factory;
  59. try {
  60. j.setConfig(createConfig(config));
  61. } catch (IOException e) {
  62. throw new UncheckedIOException(e);
  63. }
  64. }
  65. private OpenSshConfig createConfig(String... content) throws IOException {
  66. File configFile = new File(sshDir, Constants.CONFIG);
  67. if (content != null) {
  68. Files.write(configFile.toPath(), Arrays.asList(content));
  69. }
  70. return new OpenSshConfig(getTemporaryDirectory(), configFile);
  71. }
  72. }