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