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.

OpenSshConfigTest.java 16KB

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