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