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;
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.OpenSshConfig.Host;
28  import org.eclipse.jgit.util.FS;
29  
30  import com.jcraft.jsch.JSch;
31  import com.jcraft.jsch.JSchException;
32  import com.jcraft.jsch.Session;
33  
34  public class JSchSshProtocol2Test extends SshBasicTestBase {
35  
36  	private class TestSshSessionFactory extends JschConfigSessionFactory {
37  
38  		@Override
39  		protected void configure(Host hc, Session session) {
40  			// Nothing
41  		}
42  
43  		@Override
44  		public synchronized RemoteSession getSession(URIish uri,
45  				CredentialsProvider credentialsProvider, FS fs, int tms)
46  				throws TransportException {
47  			return super.getSession(uri, credentialsProvider, fs, tms);
48  		}
49  
50  		@Override
51  		protected JSch createDefaultJSch(FS fs) throws JSchException {
52  			JSch defaultJSch = super.createDefaultJSch(fs);
53  			if (knownHosts.exists()) {
54  				defaultJSch.setKnownHosts(knownHosts.getAbsolutePath());
55  			}
56  			return defaultJSch;
57  		}
58  	}
59  
60  	@Override
61  	protected SshSessionFactory createSessionFactory() {
62  		return new TestSshSessionFactory();
63  	}
64  
65  	@Override
66  	protected void installConfig(String... config) {
67  		SshSessionFactory factory = getSessionFactory();
68  		assertTrue(factory instanceof JschConfigSessionFactory);
69  		JschConfigSessionFactory j = (JschConfigSessionFactory) factory;
70  		try {
71  			j.setConfig(createConfig(config));
72  		} catch (IOException e) {
73  			throw new UncheckedIOException(e);
74  		}
75  	}
76  
77  	private OpenSshConfig createConfig(String... content) throws IOException {
78  		File configFile = new File(sshDir, Constants.CONFIG);
79  		if (content != null) {
80  			Files.write(configFile.toPath(), Arrays.asList(content));
81  		}
82  		return new OpenSshConfig(getTemporaryDirectory(), configFile);
83  	}
84  
85  	@Override
86  	public void setUp() throws Exception {
87  		super.setUp();
88  		StoredConfig config = ((Repository) db).getConfig();
89  		config.setInt("protocol", null, "version", 2);
90  		config.save();
91  	}
92  }