Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

OpenSshConfigTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (C) 2008, 2017 Google Inc.
  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 java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.junit.Assert.assertArrayEquals;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertNotNull;
  49. import static org.junit.Assert.assertNotSame;
  50. import static org.junit.Assert.assertNull;
  51. import static org.junit.Assert.assertSame;
  52. import static org.junit.Assert.assertTrue;
  53. import java.io.File;
  54. import java.io.FileOutputStream;
  55. import java.io.IOException;
  56. import java.io.OutputStreamWriter;
  57. import org.eclipse.jgit.junit.RepositoryTestCase;
  58. import org.eclipse.jgit.lib.Constants;
  59. import org.eclipse.jgit.transport.OpenSshConfig.Host;
  60. import org.eclipse.jgit.util.FileUtils;
  61. import org.eclipse.jgit.util.SystemReader;
  62. import org.junit.Before;
  63. import org.junit.Test;
  64. import com.jcraft.jsch.ConfigRepository;
  65. import com.jcraft.jsch.ConfigRepository.Config;
  66. public class OpenSshConfigTest extends RepositoryTestCase {
  67. private File home;
  68. private File configFile;
  69. private OpenSshConfig osc;
  70. @Override
  71. @Before
  72. public void setUp() throws Exception {
  73. super.setUp();
  74. home = new File(trash, "home");
  75. FileUtils.mkdir(home);
  76. configFile = new File(new File(home, ".ssh"), Constants.CONFIG);
  77. FileUtils.mkdir(configFile.getParentFile());
  78. mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "jex_junit");
  79. osc = new OpenSshConfig(home, configFile);
  80. }
  81. private void config(String data) throws IOException {
  82. long lastMtime = configFile.lastModified();
  83. do {
  84. try (final OutputStreamWriter fw = new OutputStreamWriter(
  85. new FileOutputStream(configFile), UTF_8)) {
  86. fw.write(data);
  87. }
  88. } while (lastMtime == configFile.lastModified());
  89. }
  90. @Test
  91. public void testNoConfig() {
  92. final Host h = osc.lookup("repo.or.cz");
  93. assertNotNull(h);
  94. assertEquals("repo.or.cz", h.getHostName());
  95. assertEquals("jex_junit", h.getUser());
  96. assertEquals(22, h.getPort());
  97. assertEquals(1, h.getConnectionAttempts());
  98. assertNull(h.getIdentityFile());
  99. }
  100. @Test
  101. public void testSeparatorParsing() throws Exception {
  102. config("Host\tfirst\n" +
  103. "\tHostName\tfirst.tld\n" +
  104. "\n" +
  105. "Host second\n" +
  106. " HostName\tsecond.tld\n" +
  107. "Host=third\n" +
  108. "HostName=third.tld\n\n\n" +
  109. "\t Host = fourth\n\n\n" +
  110. " \t HostName\t=fourth.tld\n" +
  111. "Host\t = last\n" +
  112. "HostName \t last.tld");
  113. assertNotNull(osc.lookup("first"));
  114. assertEquals("first.tld", osc.lookup("first").getHostName());
  115. assertNotNull(osc.lookup("second"));
  116. assertEquals("second.tld", osc.lookup("second").getHostName());
  117. assertNotNull(osc.lookup("third"));
  118. assertEquals("third.tld", osc.lookup("third").getHostName());
  119. assertNotNull(osc.lookup("fourth"));
  120. assertEquals("fourth.tld", osc.lookup("fourth").getHostName());
  121. assertNotNull(osc.lookup("last"));
  122. assertEquals("last.tld", osc.lookup("last").getHostName());
  123. }
  124. @Test
  125. public void testQuoteParsing() throws Exception {
  126. config("Host \"good\"\n" +
  127. " HostName=\"good.tld\"\n" +
  128. " Port=\"6007\"\n" +
  129. " User=\"gooduser\"\n" +
  130. "Host multiple unquoted and \"quoted\" \"hosts\"\n" +
  131. " Port=\"2222\"\n" +
  132. "Host \"spaced\"\n" +
  133. "# Bad host name, but testing preservation of spaces\n" +
  134. " HostName=\" spaced\ttld \"\n" +
  135. "# Misbalanced quotes\n" +
  136. "Host \"bad\"\n" +
  137. "# OpenSSH doesn't allow this but ...\n" +
  138. " HostName=bad.tld\"\n");
  139. assertEquals("good.tld", osc.lookup("good").getHostName());
  140. assertEquals("gooduser", osc.lookup("good").getUser());
  141. assertEquals(6007, osc.lookup("good").getPort());
  142. assertEquals(2222, osc.lookup("multiple").getPort());
  143. assertEquals(2222, osc.lookup("quoted").getPort());
  144. assertEquals(2222, osc.lookup("and").getPort());
  145. assertEquals(2222, osc.lookup("unquoted").getPort());
  146. assertEquals(2222, osc.lookup("hosts").getPort());
  147. assertEquals(" spaced\ttld ", osc.lookup("spaced").getHostName());
  148. assertEquals("bad.tld\"", osc.lookup("bad").getHostName());
  149. }
  150. @Test
  151. public void testCaseInsensitiveKeyLookup() throws Exception {
  152. config("Host orcz\n" + "Port 29418\n"
  153. + "\tHostName repo.or.cz\nStrictHostKeyChecking yes\n");
  154. final Host h = osc.lookup("orcz");
  155. Config c = h.getConfig();
  156. String exactCase = c.getValue("StrictHostKeyChecking");
  157. assertEquals("yes", exactCase);
  158. assertEquals(exactCase, c.getValue("stricthostkeychecking"));
  159. assertEquals(exactCase, c.getValue("STRICTHOSTKEYCHECKING"));
  160. assertEquals(exactCase, c.getValue("sTrIcThostKEYcheckING"));
  161. assertNull(c.getValue("sTrIcThostKEYcheckIN"));
  162. }
  163. @Test
  164. public void testAlias_DoesNotMatch() throws Exception {
  165. config("Host orcz\n" + "Port 29418\n" + "\tHostName repo.or.cz\n");
  166. final Host h = osc.lookup("repo.or.cz");
  167. assertNotNull(h);
  168. assertEquals("repo.or.cz", h.getHostName());
  169. assertEquals("jex_junit", h.getUser());
  170. assertEquals(22, h.getPort());
  171. assertNull(h.getIdentityFile());
  172. final Host h2 = osc.lookup("orcz");
  173. assertEquals("repo.or.cz", h.getHostName());
  174. assertEquals("jex_junit", h.getUser());
  175. assertEquals(29418, h2.getPort());
  176. assertNull(h.getIdentityFile());
  177. }
  178. @Test
  179. public void testAlias_OptionsSet() throws Exception {
  180. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\tPort 2222\n"
  181. + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
  182. + "\tForwardX11 no\n");
  183. final Host h = osc.lookup("orcz");
  184. assertNotNull(h);
  185. assertEquals("repo.or.cz", h.getHostName());
  186. assertEquals("jex", h.getUser());
  187. assertEquals(2222, h.getPort());
  188. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  189. }
  190. @Test
  191. public void testAlias_OptionsKeywordCaseInsensitive() throws Exception {
  192. config("hOsT orcz\n" + "\thOsTnAmE repo.or.cz\n" + "\tPORT 2222\n"
  193. + "\tuser jex\n" + "\tidentityfile .ssh/id_jex\n"
  194. + "\tForwardX11 no\n");
  195. final Host h = osc.lookup("orcz");
  196. assertNotNull(h);
  197. assertEquals("repo.or.cz", h.getHostName());
  198. assertEquals("jex", h.getUser());
  199. assertEquals(2222, h.getPort());
  200. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  201. }
  202. @Test
  203. public void testAlias_OptionsInherit() throws Exception {
  204. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  205. + "\tHostName not.a.host.example.com\n" + "\tPort 2222\n"
  206. + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
  207. + "\tForwardX11 no\n");
  208. final Host h = osc.lookup("orcz");
  209. assertNotNull(h);
  210. assertEquals("repo.or.cz", h.getHostName());
  211. assertEquals("jex", h.getUser());
  212. assertEquals(2222, h.getPort());
  213. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  214. }
  215. @Test
  216. public void testAlias_PreferredAuthenticationsDefault() throws Exception {
  217. final Host h = osc.lookup("orcz");
  218. assertNotNull(h);
  219. assertNull(h.getPreferredAuthentications());
  220. }
  221. @Test
  222. public void testAlias_PreferredAuthentications() throws Exception {
  223. config("Host orcz\n" + "\tPreferredAuthentications publickey\n");
  224. final Host h = osc.lookup("orcz");
  225. assertNotNull(h);
  226. assertEquals("publickey", h.getPreferredAuthentications());
  227. }
  228. @Test
  229. public void testAlias_InheritPreferredAuthentications() throws Exception {
  230. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  231. + "\tPreferredAuthentications publickey, hostbased\n");
  232. final Host h = osc.lookup("orcz");
  233. assertNotNull(h);
  234. assertEquals("publickey,hostbased", h.getPreferredAuthentications());
  235. }
  236. @Test
  237. public void testAlias_BatchModeDefault() throws Exception {
  238. final Host h = osc.lookup("orcz");
  239. assertNotNull(h);
  240. assertFalse(h.isBatchMode());
  241. }
  242. @Test
  243. public void testAlias_BatchModeYes() throws Exception {
  244. config("Host orcz\n" + "\tBatchMode yes\n");
  245. final Host h = osc.lookup("orcz");
  246. assertNotNull(h);
  247. assertTrue(h.isBatchMode());
  248. }
  249. @Test
  250. public void testAlias_InheritBatchMode() throws Exception {
  251. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  252. + "\tBatchMode yes\n");
  253. final Host h = osc.lookup("orcz");
  254. assertNotNull(h);
  255. assertTrue(h.isBatchMode());
  256. }
  257. @Test
  258. public void testAlias_ConnectionAttemptsDefault() throws Exception {
  259. final Host h = osc.lookup("orcz");
  260. assertNotNull(h);
  261. assertEquals(1, h.getConnectionAttempts());
  262. }
  263. @Test
  264. public void testAlias_ConnectionAttempts() throws Exception {
  265. config("Host orcz\n" + "\tConnectionAttempts 5\n");
  266. final Host h = osc.lookup("orcz");
  267. assertNotNull(h);
  268. assertEquals(5, h.getConnectionAttempts());
  269. }
  270. @Test
  271. public void testAlias_invalidConnectionAttempts() throws Exception {
  272. config("Host orcz\n" + "\tConnectionAttempts -1\n");
  273. final Host h = osc.lookup("orcz");
  274. assertNotNull(h);
  275. assertEquals(1, h.getConnectionAttempts());
  276. }
  277. @Test
  278. public void testAlias_badConnectionAttempts() throws Exception {
  279. config("Host orcz\n" + "\tConnectionAttempts xxx\n");
  280. final Host h = osc.lookup("orcz");
  281. assertNotNull(h);
  282. assertEquals(1, h.getConnectionAttempts());
  283. }
  284. @Test
  285. public void testDefaultBlock() throws Exception {
  286. config("ConnectionAttempts 5\n\nHost orcz\nConnectionAttempts 3\n");
  287. final Host h = osc.lookup("orcz");
  288. assertNotNull(h);
  289. assertEquals(5, h.getConnectionAttempts());
  290. }
  291. @Test
  292. public void testHostCaseInsensitive() throws Exception {
  293. config("hOsT orcz\nConnectionAttempts 3\n");
  294. final Host h = osc.lookup("orcz");
  295. assertNotNull(h);
  296. assertEquals(3, h.getConnectionAttempts());
  297. }
  298. @Test
  299. public void testListValueSingle() throws Exception {
  300. config("Host orcz\nUserKnownHostsFile /foo/bar\n");
  301. final ConfigRepository.Config c = osc.getConfig("orcz");
  302. assertNotNull(c);
  303. assertEquals("/foo/bar", c.getValue("UserKnownHostsFile"));
  304. }
  305. @Test
  306. public void testListValueMultiple() throws Exception {
  307. // Tilde expansion occurs within the parser
  308. config("Host orcz\nUserKnownHostsFile \"~/foo/ba z\" /foo/bar \n");
  309. final ConfigRepository.Config c = osc.getConfig("orcz");
  310. assertNotNull(c);
  311. assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
  312. "/foo/bar" },
  313. c.getValues("UserKnownHostsFile"));
  314. }
  315. @Test
  316. public void testRepeatedLookups() throws Exception {
  317. config("Host orcz\n" + "\tConnectionAttempts 5\n");
  318. final Host h1 = osc.lookup("orcz");
  319. final Host h2 = osc.lookup("orcz");
  320. assertNotNull(h1);
  321. assertSame(h1, h2);
  322. assertEquals(5, h1.getConnectionAttempts());
  323. assertEquals(h1.getConnectionAttempts(), h2.getConnectionAttempts());
  324. final ConfigRepository.Config c = osc.getConfig("orcz");
  325. assertNotNull(c);
  326. assertSame(c, h1.getConfig());
  327. assertSame(c, h2.getConfig());
  328. }
  329. @Test
  330. public void testRepeatedLookupsWithModification() throws Exception {
  331. config("Host orcz\n" + "\tConnectionAttempts -1\n");
  332. final Host h1 = osc.lookup("orcz");
  333. assertNotNull(h1);
  334. assertEquals(1, h1.getConnectionAttempts());
  335. config("Host orcz\n" + "\tConnectionAttempts 5\n");
  336. final Host h2 = osc.lookup("orcz");
  337. assertNotNull(h2);
  338. assertNotSame(h1, h2);
  339. assertEquals(5, h2.getConnectionAttempts());
  340. assertEquals(1, h1.getConnectionAttempts());
  341. assertNotSame(h1.getConfig(), h2.getConfig());
  342. }
  343. @Test
  344. public void testIdentityFile() throws Exception {
  345. config("Host orcz\nIdentityFile \"~/foo/ba z\"\nIdentityFile /foo/bar");
  346. final Host h = osc.lookup("orcz");
  347. assertNotNull(h);
  348. File f = h.getIdentityFile();
  349. assertNotNull(f);
  350. // Host does tilde replacement
  351. assertEquals(new File(home, "foo/ba z"), f);
  352. final ConfigRepository.Config c = h.getConfig();
  353. // Config does tilde replacement, too
  354. assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
  355. "/foo/bar" },
  356. c.getValues("IdentityFile"));
  357. }
  358. @Test
  359. public void testMultiIdentityFile() throws Exception {
  360. config("IdentityFile \"~/foo/ba z\"\nHost orcz\nIdentityFile /foo/bar\nHOST *\nIdentityFile /foo/baz");
  361. final Host h = osc.lookup("orcz");
  362. assertNotNull(h);
  363. File f = h.getIdentityFile();
  364. assertNotNull(f);
  365. // Host does tilde replacement
  366. assertEquals(new File(home, "foo/ba z"), f);
  367. final ConfigRepository.Config c = h.getConfig();
  368. // Config does tilde replacement, too
  369. assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
  370. "/foo/bar", "/foo/baz" },
  371. c.getValues("IdentityFile"));
  372. }
  373. @Test
  374. public void testNegatedPattern() throws Exception {
  375. config("Host repo.or.cz\nIdentityFile ~/foo/bar\nHOST !*.or.cz\nIdentityFile /foo/baz");
  376. final Host h = osc.lookup("repo.or.cz");
  377. assertNotNull(h);
  378. assertEquals(new File(home, "foo/bar"), h.getIdentityFile());
  379. assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath() },
  380. h.getConfig().getValues("IdentityFile"));
  381. }
  382. @Test
  383. public void testPattern() throws Exception {
  384. config("Host repo.or.cz\nIdentityFile ~/foo/bar\nHOST *.or.cz\nIdentityFile /foo/baz");
  385. final Host h = osc.lookup("repo.or.cz");
  386. assertNotNull(h);
  387. assertEquals(new File(home, "foo/bar"), h.getIdentityFile());
  388. assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath(),
  389. "/foo/baz" },
  390. h.getConfig().getValues("IdentityFile"));
  391. }
  392. @Test
  393. public void testMultiHost() throws Exception {
  394. config("Host orcz *.or.cz\nIdentityFile ~/foo/bar\nHOST *.or.cz\nIdentityFile /foo/baz");
  395. final Host h1 = osc.lookup("repo.or.cz");
  396. assertNotNull(h1);
  397. assertEquals(new File(home, "foo/bar"), h1.getIdentityFile());
  398. assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath(),
  399. "/foo/baz" },
  400. h1.getConfig().getValues("IdentityFile"));
  401. final Host h2 = osc.lookup("orcz");
  402. assertNotNull(h2);
  403. assertEquals(new File(home, "foo/bar"), h2.getIdentityFile());
  404. assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath() },
  405. h2.getConfig().getValues("IdentityFile"));
  406. }
  407. @Test
  408. public void testEqualsSign() throws Exception {
  409. config("Host=orcz\n\tConnectionAttempts = 5\n\tUser=\t foobar\t\n");
  410. final Host h = osc.lookup("orcz");
  411. assertNotNull(h);
  412. assertEquals(5, h.getConnectionAttempts());
  413. assertEquals("foobar", h.getUser());
  414. }
  415. @Test
  416. public void testMissingArgument() throws Exception {
  417. config("Host=orcz\n\tSendEnv\nIdentityFile\t\nForwardX11\n\tUser=\t foobar\t\n");
  418. final Host h = osc.lookup("orcz");
  419. assertNotNull(h);
  420. assertEquals("foobar", h.getUser());
  421. assertArrayEquals(new String[0], h.getConfig().getValues("SendEnv"));
  422. assertNull(h.getIdentityFile());
  423. assertNull(h.getConfig().getValue("ForwardX11"));
  424. }
  425. @Test
  426. public void testHomeDirUserReplacement() throws Exception {
  427. config("Host=orcz\n\tIdentityFile %d/.ssh/%u_id_dsa");
  428. final Host h = osc.lookup("orcz");
  429. assertNotNull(h);
  430. assertEquals(new File(new File(home, ".ssh"), "jex_junit_id_dsa"),
  431. h.getIdentityFile());
  432. }
  433. @Test
  434. public void testHostnameReplacement() throws Exception {
  435. config("Host=orcz\nHost *.*\n\tHostname %h\nHost *\n\tHostname %h.example.org");
  436. final Host h = osc.lookup("orcz");
  437. assertNotNull(h);
  438. assertEquals("orcz.example.org", h.getHostName());
  439. }
  440. @Test
  441. public void testRemoteUserReplacement() throws Exception {
  442. config("Host=orcz\n\tUser foo\n" + "Host *.*\n\tHostname %h\n"
  443. + "Host *\n\tHostname %h.ex%%20ample.org\n\tIdentityFile ~/.ssh/%h_%r_id_dsa");
  444. final Host h = osc.lookup("orcz");
  445. assertNotNull(h);
  446. assertEquals(
  447. new File(new File(home, ".ssh"),
  448. "orcz.ex%20ample.org_foo_id_dsa"),
  449. h.getIdentityFile());
  450. }
  451. @Test
  452. public void testLocalhostFQDNReplacement() throws Exception {
  453. String localhost = SystemReader.getInstance().getHostname();
  454. config("Host=orcz\n\tIdentityFile ~/.ssh/%l_id_dsa");
  455. final Host h = osc.lookup("orcz");
  456. assertNotNull(h);
  457. assertEquals(
  458. new File(new File(home, ".ssh"), localhost + "_id_dsa"),
  459. h.getIdentityFile());
  460. }
  461. }