You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JSchSshProtocol2Test.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) 2020 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.SshBasicTestBase;
  20. import org.eclipse.jgit.lib.Constants;
  21. import org.eclipse.jgit.lib.Repository;
  22. import org.eclipse.jgit.lib.StoredConfig;
  23. import org.eclipse.jgit.transport.OpenSshConfig.Host;
  24. import org.eclipse.jgit.util.FS;
  25. import com.jcraft.jsch.JSch;
  26. import com.jcraft.jsch.JSchException;
  27. import com.jcraft.jsch.Session;
  28. public class JSchSshProtocol2Test extends SshBasicTestBase {
  29. private class TestSshSessionFactory extends JschConfigSessionFactory {
  30. @Override
  31. protected void configure(Host hc, Session session) {
  32. // Nothing
  33. }
  34. @Override
  35. public synchronized RemoteSession getSession(URIish uri,
  36. CredentialsProvider credentialsProvider, FS fs, int tms)
  37. throws TransportException {
  38. return super.getSession(uri, credentialsProvider, fs, tms);
  39. }
  40. @Override
  41. protected JSch createDefaultJSch(FS fs) throws JSchException {
  42. JSch defaultJSch = super.createDefaultJSch(fs);
  43. if (knownHosts.exists()) {
  44. defaultJSch.setKnownHosts(knownHosts.getAbsolutePath());
  45. }
  46. return defaultJSch;
  47. }
  48. }
  49. @Override
  50. protected SshSessionFactory createSessionFactory() {
  51. return new TestSshSessionFactory();
  52. }
  53. @Override
  54. protected void installConfig(String... config) {
  55. SshSessionFactory factory = getSessionFactory();
  56. assertTrue(factory instanceof JschConfigSessionFactory);
  57. JschConfigSessionFactory j = (JschConfigSessionFactory) factory;
  58. try {
  59. j.setConfig(createConfig(config));
  60. } catch (IOException e) {
  61. throw new UncheckedIOException(e);
  62. }
  63. }
  64. private OpenSshConfig createConfig(String... content) throws IOException {
  65. File configFile = new File(sshDir, Constants.CONFIG);
  66. if (content != null) {
  67. Files.write(configFile.toPath(), Arrays.asList(content));
  68. }
  69. return new OpenSshConfig(getTemporaryDirectory(), configFile);
  70. }
  71. @Override
  72. public void setUp() throws Exception {
  73. super.setUp();
  74. StoredConfig config = ((Repository) db).getConfig();
  75. config.setInt("protocol", null, "version", 2);
  76. config.save();
  77. }
  78. }