1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.transport;
11
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.UncheckedIOException;
17 import java.nio.file.Files;
18 import java.util.Arrays;
19
20 import org.eclipse.jgit.errors.TransportException;
21 import org.eclipse.jgit.lib.Constants;
22 import org.eclipse.jgit.transport.OpenSshConfig.Host;
23 import org.eclipse.jgit.transport.ssh.SshTestBase;
24 import org.eclipse.jgit.util.FS;
25 import org.junit.experimental.theories.Theories;
26 import org.junit.runner.RunWith;
27
28 import com.jcraft.jsch.JSch;
29 import com.jcraft.jsch.JSchException;
30 import com.jcraft.jsch.Session;
31
32 @RunWith(Theories.class)
33 public class JSchSshTest extends SshTestBase {
34
35 private class TestSshSessionFactory extends JschConfigSessionFactory {
36
37 @Override
38 protected void configure(Host hc, Session session) {
39
40 }
41
42 @Override
43 public synchronized RemoteSession getSession(URIish uri,
44 CredentialsProvider credentialsProvider, FS fs, int tms)
45 throws TransportException {
46 return super.getSession(uri, credentialsProvider, fs, tms);
47 }
48
49 @Override
50 protected JSch createDefaultJSch(FS fs) throws JSchException {
51 JSch defaultJSch = super.createDefaultJSch(fs);
52 if (knownHosts.exists()) {
53 defaultJSch.setKnownHosts(knownHosts.getAbsolutePath());
54 }
55 return defaultJSch;
56 }
57 }
58
59 @Override
60 protected SshSessionFactory createSessionFactory() {
61 return new TestSshSessionFactory();
62 }
63
64 @Override
65 protected void installConfig(String... config) {
66 SshSessionFactory factory = getSessionFactory();
67 assertTrue(factory instanceof JschConfigSessionFactory);
68 JschConfigSessionFactory j = (JschConfigSessionFactory) factory;
69 try {
70 j.setConfig(createConfig(config));
71 } catch (IOException e) {
72 throw new UncheckedIOException(e);
73 }
74 }
75
76 private OpenSshConfig createConfig(String... content) throws IOException {
77 File configFile = new File(sshDir, Constants.CONFIG);
78 if (content != null) {
79 Files.write(configFile.toPath(), Arrays.asList(content));
80 }
81 return new OpenSshConfig(getTemporaryDirectory(), configFile);
82 }
83
84 }