Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JschConfigSessionFactoryTest.java 11KB

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