You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JschConfigSessionFactoryTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. import static org.junit.Assert.assertEquals;
  45. import java.io.File;
  46. import java.nio.file.Files;
  47. import java.util.Arrays;
  48. import java.util.concurrent.TimeUnit;
  49. import org.eclipse.jgit.util.FS;
  50. import org.junit.After;
  51. import org.junit.Test;
  52. import com.jcraft.jsch.Session;
  53. /**
  54. * Tests for correctly interpreting ssh config values when Jsch sessions are
  55. * used.
  56. */
  57. public class JschConfigSessionFactoryTest {
  58. File tmpConfigFile;
  59. OpenSshConfig tmpConfig;
  60. DefaultSshSessionFactory factory = new DefaultSshSessionFactory();
  61. @After
  62. public void removeTmpConfig() {
  63. if (tmpConfigFile == null) {
  64. return;
  65. }
  66. if (tmpConfigFile.exists() && !tmpConfigFile.delete()) {
  67. tmpConfigFile.deleteOnExit();
  68. }
  69. tmpConfigFile = null;
  70. }
  71. @Test
  72. public void testNoConfigEntry() throws Exception {
  73. tmpConfigFile = File.createTempFile("jsch", "test");
  74. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  75. tmpConfigFile);
  76. factory.setConfig(tmpConfig);
  77. Session session = createSession("ssh://egit/egit/egit");
  78. assertEquals("egit", session.getHost());
  79. // No user in URI, none in ssh config: default is OS user name
  80. assertEquals(System.getProperty("user.name"), session.getUserName());
  81. assertEquals(22, session.getPort());
  82. }
  83. @Test
  84. public void testAlias() throws Exception {
  85. tmpConfigFile = createConfig("Host egit", "Hostname git.eclipse.org",
  86. "User foo", "Port 29418");
  87. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  88. tmpConfigFile);
  89. factory.setConfig(tmpConfig);
  90. Session session = createSession("ssh://egit/egit/egit");
  91. assertEquals("git.eclipse.org", session.getHost());
  92. assertEquals("foo", session.getUserName());
  93. assertEquals(29418, session.getPort());
  94. }
  95. @Test
  96. public void testAliasWithUser() throws Exception {
  97. tmpConfigFile = createConfig("Host egit", "Hostname git.eclipse.org",
  98. "User foo", "Port 29418");
  99. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  100. tmpConfigFile);
  101. factory.setConfig(tmpConfig);
  102. Session session = createSession("ssh://bar@egit/egit/egit");
  103. assertEquals("git.eclipse.org", session.getHost());
  104. assertEquals("bar", session.getUserName());
  105. assertEquals(29418, session.getPort());
  106. }
  107. @Test
  108. public void testAliasWithPort() throws Exception {
  109. tmpConfigFile = createConfig("Host egit", "Hostname git.eclipse.org",
  110. "User foo", "Port 29418");
  111. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  112. tmpConfigFile);
  113. factory.setConfig(tmpConfig);
  114. Session session = createSession("ssh://bar@egit:22/egit/egit");
  115. assertEquals("git.eclipse.org", session.getHost());
  116. assertEquals("bar", session.getUserName());
  117. assertEquals(22, session.getPort());
  118. }
  119. @Test
  120. public void testAliasIdentical() throws Exception {
  121. tmpConfigFile = createConfig("Host git.eclipse.org",
  122. "Hostname git.eclipse.org", "User foo", "Port 29418");
  123. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  124. tmpConfigFile);
  125. factory.setConfig(tmpConfig);
  126. Session session = createSession("ssh://git.eclipse.org/egit/egit");
  127. assertEquals("git.eclipse.org", session.getHost());
  128. assertEquals("foo", session.getUserName());
  129. assertEquals(29418, session.getPort());
  130. }
  131. @Test
  132. public void testAliasIdenticalWithUser() throws Exception {
  133. tmpConfigFile = createConfig("Host git.eclipse.org",
  134. "Hostname git.eclipse.org", "User foo", "Port 29418");
  135. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  136. tmpConfigFile);
  137. factory.setConfig(tmpConfig);
  138. Session session = createSession("ssh://bar@git.eclipse.org/egit/egit");
  139. assertEquals("git.eclipse.org", session.getHost());
  140. assertEquals("bar", session.getUserName());
  141. assertEquals(29418, session.getPort());
  142. }
  143. @Test
  144. public void testAliasIdenticalWithPort() 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(
  151. "ssh://bar@git.eclipse.org:300/egit/egit");
  152. assertEquals("git.eclipse.org", session.getHost());
  153. assertEquals("bar", session.getUserName());
  154. assertEquals(300, session.getPort());
  155. }
  156. @Test
  157. public void testConnectTimout() throws Exception {
  158. tmpConfigFile = createConfig("Host git.eclipse.org",
  159. "Hostname git.eclipse.org", "User foo", "Port 29418",
  160. "ConnectTimeout 10");
  161. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  162. tmpConfigFile);
  163. factory.setConfig(tmpConfig);
  164. Session session = createSession("ssh://git.eclipse.org/something");
  165. assertEquals("git.eclipse.org", session.getHost());
  166. assertEquals("foo", session.getUserName());
  167. assertEquals(29418, session.getPort());
  168. assertEquals(TimeUnit.SECONDS.toMillis(10), session.getTimeout());
  169. }
  170. @Test
  171. public void testAliasCaseDifferenceUpcase() throws Exception {
  172. tmpConfigFile = createConfig("Host Bitbucket.org",
  173. "Hostname bitbucket.org", "User foo", "Port 29418",
  174. "ConnectTimeout 10", //
  175. "Host bitbucket.org", "Hostname bitbucket.org", "User bar",
  176. "Port 22", "ConnectTimeout 5");
  177. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  178. tmpConfigFile);
  179. factory.setConfig(tmpConfig);
  180. Session session = createSession("ssh://Bitbucket.org/something");
  181. assertEquals("bitbucket.org", session.getHost());
  182. assertEquals("foo", session.getUserName());
  183. assertEquals(29418, session.getPort());
  184. assertEquals(TimeUnit.SECONDS.toMillis(10), session.getTimeout());
  185. }
  186. @Test
  187. public void testAliasCaseDifferenceLowcase() throws Exception {
  188. tmpConfigFile = createConfig("Host Bitbucket.org",
  189. "Hostname bitbucket.org", "User foo", "Port 29418",
  190. "ConnectTimeout 10", //
  191. "Host bitbucket.org", "Hostname bitbucket.org", "User bar",
  192. "Port 22", "ConnectTimeout 5");
  193. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  194. tmpConfigFile);
  195. factory.setConfig(tmpConfig);
  196. Session session = createSession("ssh://bitbucket.org/something");
  197. assertEquals("bitbucket.org", session.getHost());
  198. assertEquals("bar", session.getUserName());
  199. assertEquals(22, session.getPort());
  200. assertEquals(TimeUnit.SECONDS.toMillis(5), session.getTimeout());
  201. }
  202. @Test
  203. public void testAliasCaseDifferenceUpcaseInverted() throws Exception {
  204. tmpConfigFile = createConfig("Host bitbucket.org",
  205. "Hostname bitbucket.org", "User bar", "Port 22",
  206. "ConnectTimeout 5", //
  207. "Host Bitbucket.org", "Hostname bitbucket.org", "User foo",
  208. "Port 29418", "ConnectTimeout 10");
  209. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  210. tmpConfigFile);
  211. factory.setConfig(tmpConfig);
  212. Session session = createSession("ssh://Bitbucket.org/something");
  213. assertEquals("bitbucket.org", session.getHost());
  214. assertEquals("foo", session.getUserName());
  215. assertEquals(29418, session.getPort());
  216. assertEquals(TimeUnit.SECONDS.toMillis(10), session.getTimeout());
  217. }
  218. @Test
  219. public void testAliasCaseDifferenceLowcaseInverted() throws Exception {
  220. tmpConfigFile = createConfig("Host bitbucket.org",
  221. "Hostname bitbucket.org", "User bar", "Port 22",
  222. "ConnectTimeout 5", //
  223. "Host Bitbucket.org", "Hostname bitbucket.org", "User foo",
  224. "Port 29418", "ConnectTimeout 10");
  225. tmpConfig = new OpenSshConfig(tmpConfigFile.getParentFile(),
  226. tmpConfigFile);
  227. factory.setConfig(tmpConfig);
  228. Session session = createSession("ssh://bitbucket.org/something");
  229. assertEquals("bitbucket.org", session.getHost());
  230. assertEquals("bar", session.getUserName());
  231. assertEquals(22, session.getPort());
  232. assertEquals(TimeUnit.SECONDS.toMillis(5), session.getTimeout());
  233. }
  234. private File createConfig(String... lines) throws Exception {
  235. File f = File.createTempFile("jsch", "test");
  236. Files.write(f.toPath(), Arrays.asList(lines));
  237. return f;
  238. }
  239. private Session createSession(String uriText) throws Exception {
  240. // For this test to make sense, these few lines must correspond to the
  241. // code in JschConfigSessionFactory.getSession(). Because of
  242. // side-effects we cannot encapsulate that there properly and so we have
  243. // to duplicate this bit here. We also can't test getSession() itself
  244. // since it would try to actually connect to a server.
  245. URIish uri = new URIish(uriText);
  246. String host = uri.getHost();
  247. String user = uri.getUser();
  248. String password = uri.getPass();
  249. int port = uri.getPort();
  250. OpenSshConfig.Host hostConfig = tmpConfig.lookup(host);
  251. if (port <= 0) {
  252. port = hostConfig.getPort();
  253. }
  254. if (user == null) {
  255. user = hostConfig.getUser();
  256. }
  257. return factory.createSession(null, FS.DETECTED, user, password, host,
  258. port, hostConfig);
  259. }
  260. }