View Javadoc
1   /*
2    * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.transport;
44  
45  import static org.junit.Assert.assertEquals;
46  
47  import java.io.File;
48  import java.nio.file.Files;
49  import java.util.Arrays;
50  import java.util.concurrent.TimeUnit;
51  
52  import org.eclipse.jgit.junit.MockSystemReader;
53  import org.eclipse.jgit.util.FS;
54  import org.eclipse.jgit.util.SystemReader;
55  import org.junit.After;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  import com.jcraft.jsch.Session;
60  
61  /**
62   * Tests for correctly interpreting ssh config values when Jsch sessions are
63   * used.
64   */
65  public class JschConfigSessionFactoryTest {
66  
67  	File tmpConfigFile;
68  
69  	OpenSshConfig tmpConfig;
70  
71  	DefaultSshSessionFactory factory = new DefaultSshSessionFactory();
72  
73  	@Before
74  	public void setup() {
75  		SystemReader.setInstance(new MockSystemReader());
76  	}
77  
78  	@After
79  	public void removeTmpConfig() {
80  		SystemReader.setInstance(null);
81  		if (tmpConfigFile == null) {
82  			return;
83  		}
84  		if (tmpConfigFile.exists() && !tmpConfigFile.delete()) {
85  			tmpConfigFile.deleteOnExit();
86  		}
87  		tmpConfigFile = null;
88  	}
89  
90  	@Test
91  	public void testNoConfigEntry() throws Exception {
92  		tmpConfigFile = File.createTempFile("jsch", "test");
93  		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
94  				tmpConfigFile);
95  		factory.setConfig(tmpConfig);
96  		Session session = createSession("ssh://egit/egit/egit");
97  		assertEquals("egit", session.getHost());
98  		// No user in URI, none in ssh config: default is OS user name
99  		assertEquals(SystemReader.getInstance().getProperty("user.name"),
100 				session.getUserName());
101 		assertEquals(22, session.getPort());
102 	}
103 
104 	@Test
105 	public void testAlias() throws Exception {
106 		tmpConfigFile = createConfig("Host egit", "Hostname git.eclipse.org",
107 				"User foo", "Port 29418");
108 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
109 				tmpConfigFile);
110 		factory.setConfig(tmpConfig);
111 		Session session = createSession("ssh://egit/egit/egit");
112 		assertEquals("git.eclipse.org", session.getHost());
113 		assertEquals("foo", session.getUserName());
114 		assertEquals(29418, session.getPort());
115 	}
116 
117 	@Test
118 	public void testAliasWithUser() throws Exception {
119 		tmpConfigFile = createConfig("Host egit", "Hostname git.eclipse.org",
120 				"User foo", "Port 29418");
121 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
122 				tmpConfigFile);
123 		factory.setConfig(tmpConfig);
124 		Session session = createSession("ssh://bar@egit/egit/egit");
125 		assertEquals("git.eclipse.org", session.getHost());
126 		assertEquals("bar", session.getUserName());
127 		assertEquals(29418, session.getPort());
128 	}
129 
130 	@Test
131 	public void testAliasWithPort() throws Exception {
132 		tmpConfigFile = createConfig("Host egit", "Hostname git.eclipse.org",
133 				"User foo", "Port 29418");
134 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
135 				tmpConfigFile);
136 		factory.setConfig(tmpConfig);
137 		Session session = createSession("ssh://bar@egit:22/egit/egit");
138 		assertEquals("git.eclipse.org", session.getHost());
139 		assertEquals("bar", session.getUserName());
140 		assertEquals(22, session.getPort());
141 	}
142 
143 	@Test
144 	public void testAliasIdentical() throws Exception {
145 		tmpConfigFile = createConfig("Host git.eclipse.org",
146 				"Hostname git.eclipse.org", "User foo", "Port 29418");
147 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
148 				tmpConfigFile);
149 		factory.setConfig(tmpConfig);
150 		Session session = createSession("ssh://git.eclipse.org/egit/egit");
151 		assertEquals("git.eclipse.org", session.getHost());
152 		assertEquals("foo", session.getUserName());
153 		assertEquals(29418, session.getPort());
154 	}
155 
156 	@Test
157 	public void testAliasIdenticalWithUser() throws Exception {
158 		tmpConfigFile = createConfig("Host git.eclipse.org",
159 				"Hostname git.eclipse.org", "User foo", "Port 29418");
160 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
161 				tmpConfigFile);
162 		factory.setConfig(tmpConfig);
163 		Session session = createSession("ssh://bar@git.eclipse.org/egit/egit");
164 		assertEquals("git.eclipse.org", session.getHost());
165 		assertEquals("bar", session.getUserName());
166 		assertEquals(29418, session.getPort());
167 	}
168 
169 	@Test
170 	public void testAliasIdenticalWithPort() throws Exception {
171 		tmpConfigFile = createConfig("Host git.eclipse.org",
172 				"Hostname git.eclipse.org", "User foo", "Port 29418");
173 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
174 				tmpConfigFile);
175 		factory.setConfig(tmpConfig);
176 		Session session = createSession(
177 				"ssh://bar@git.eclipse.org:300/egit/egit");
178 		assertEquals("git.eclipse.org", session.getHost());
179 		assertEquals("bar", session.getUserName());
180 		assertEquals(300, session.getPort());
181 	}
182 
183 	@Test
184 	public void testConnectTimout() throws Exception {
185 		tmpConfigFile = createConfig("Host git.eclipse.org",
186 				"Hostname git.eclipse.org", "User foo", "Port 29418",
187 				"ConnectTimeout 10");
188 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
189 				tmpConfigFile);
190 		factory.setConfig(tmpConfig);
191 		Session session = createSession("ssh://git.eclipse.org/something");
192 		assertEquals("git.eclipse.org", session.getHost());
193 		assertEquals("foo", session.getUserName());
194 		assertEquals(29418, session.getPort());
195 		assertEquals(TimeUnit.SECONDS.toMillis(10), session.getTimeout());
196 	}
197 
198 	@Test
199 	public void testAliasCaseDifferenceUpcase() throws Exception {
200 		tmpConfigFile = createConfig("Host Bitbucket.org",
201 				"Hostname bitbucket.org", "User foo", "Port 29418",
202 				"ConnectTimeout 10", //
203 				"Host bitbucket.org", "Hostname bitbucket.org", "User bar",
204 				"Port 22", "ConnectTimeout 5");
205 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
206 				tmpConfigFile);
207 		factory.setConfig(tmpConfig);
208 		Session session = createSession("ssh://Bitbucket.org/something");
209 		assertEquals("bitbucket.org", session.getHost());
210 		assertEquals("foo", session.getUserName());
211 		assertEquals(29418, session.getPort());
212 		assertEquals(TimeUnit.SECONDS.toMillis(10), session.getTimeout());
213 	}
214 
215 	@Test
216 	public void testAliasCaseDifferenceLowcase() throws Exception {
217 		tmpConfigFile = createConfig("Host Bitbucket.org",
218 				"Hostname bitbucket.org", "User foo", "Port 29418",
219 				"ConnectTimeout 10", //
220 				"Host bitbucket.org", "Hostname bitbucket.org", "User bar",
221 				"Port 22", "ConnectTimeout 5");
222 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
223 				tmpConfigFile);
224 		factory.setConfig(tmpConfig);
225 		Session session = createSession("ssh://bitbucket.org/something");
226 		assertEquals("bitbucket.org", session.getHost());
227 		assertEquals("bar", session.getUserName());
228 		assertEquals(22, session.getPort());
229 		assertEquals(TimeUnit.SECONDS.toMillis(5), session.getTimeout());
230 	}
231 
232 	@Test
233 	public void testAliasCaseDifferenceUpcaseInverted() throws Exception {
234 		tmpConfigFile = createConfig("Host bitbucket.org",
235 				"Hostname bitbucket.org", "User bar", "Port 22",
236 				"ConnectTimeout 5", //
237 				"Host Bitbucket.org", "Hostname bitbucket.org", "User foo",
238 				"Port 29418", "ConnectTimeout 10");
239 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
240 				tmpConfigFile);
241 		factory.setConfig(tmpConfig);
242 		Session session = createSession("ssh://Bitbucket.org/something");
243 		assertEquals("bitbucket.org", session.getHost());
244 		assertEquals("foo", session.getUserName());
245 		assertEquals(29418, session.getPort());
246 		assertEquals(TimeUnit.SECONDS.toMillis(10), session.getTimeout());
247 	}
248 
249 	@Test
250 	public void testAliasCaseDifferenceLowcaseInverted() throws Exception {
251 		tmpConfigFile = createConfig("Host bitbucket.org",
252 				"Hostname bitbucket.org", "User bar", "Port 22",
253 				"ConnectTimeout 5", //
254 				"Host Bitbucket.org", "Hostname bitbucket.org", "User foo",
255 				"Port 29418", "ConnectTimeout 10");
256 		tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
257 				tmpConfigFile);
258 		factory.setConfig(tmpConfig);
259 		Session session = createSession("ssh://bitbucket.org/something");
260 		assertEquals("bitbucket.org", session.getHost());
261 		assertEquals("bar", session.getUserName());
262 		assertEquals(22, session.getPort());
263 		assertEquals(TimeUnit.SECONDS.toMillis(5), session.getTimeout());
264 	}
265 
266 	private File createConfig(String... lines) throws Exception {
267 		File f = File.createTempFile("jsch", "test");
268 		Files.write(f.toPath(), Arrays.asList(lines));
269 		return f;
270 	}
271 
272 	private Session createSession(String uriText) throws Exception {
273 		// For this test to make sense, these few lines must correspond to the
274 		// code in JschConfigSessionFactory.getSession(). Because of
275 		// side-effects we cannot encapsulate that there properly and so we have
276 		// to duplicate this bit here. We also can't test getSession() itself
277 		// since it would try to actually connect to a server.
278 		URIish uri = new URIish(uriText);
279 		String host = uri.getHost();
280 		String user = uri.getUser();
281 		String password = uri.getPass();
282 		int port = uri.getPort();
283 		OpenSshConfig.Host hostConfig = tmpConfig.lookup(host);
284 		if (port <= 0) {
285 			port = hostConfig.getPort();
286 		}
287 		if (user == null) {
288 			user = hostConfig.getUser();
289 		}
290 		return factory.createSession(null, FS.DETECTED, user, password, host,
291 				port, hostConfig);
292 	}
293 }