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