View Javadoc
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  
11  //TODO(ms): move to org.eclipse.jgit.ssh.jsch in 6.0
12  package org.eclipse.jgit.transport.ssh.jsch;
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.SshBasicTestBase;
24  import org.eclipse.jgit.lib.Constants;
25  import org.eclipse.jgit.lib.Repository;
26  import org.eclipse.jgit.lib.StoredConfig;
27  import org.eclipse.jgit.transport.CredentialsProvider;
28  import org.eclipse.jgit.transport.RemoteSession;
29  import org.eclipse.jgit.transport.SshSessionFactory;
30  import org.eclipse.jgit.transport.URIish;
31  import org.eclipse.jgit.transport.ssh.jsch.OpenSshConfig.Host;
32  import org.eclipse.jgit.util.FS;
33  
34  import com.jcraft.jsch.JSch;
35  import com.jcraft.jsch.JSchException;
36  import com.jcraft.jsch.Session;
37  
38  public class JSchSshProtocol2Test extends SshBasicTestBase {
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  	@Override
90  	public void setUp() throws Exception {
91  		super.setUp();
92  		StoredConfig config = ((Repository) db).getConfig();
93  		config.setInt("protocol", null, "version", 2);
94  		config.save();
95  	}
96  }