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 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C) 2008, 2014 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 org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import java.io.File;
  50. import java.io.FileOutputStream;
  51. import java.io.IOException;
  52. import java.io.OutputStreamWriter;
  53. import org.eclipse.jgit.junit.RepositoryTestCase;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.transport.OpenSshConfig.Host;
  56. import org.eclipse.jgit.util.FileUtils;
  57. import org.junit.Before;
  58. import org.junit.Test;
  59. public class OpenSshConfigTest extends RepositoryTestCase {
  60. private File home;
  61. private File configFile;
  62. private OpenSshConfig osc;
  63. @Override
  64. @Before
  65. public void setUp() throws Exception {
  66. super.setUp();
  67. home = new File(trash, "home");
  68. FileUtils.mkdir(home);
  69. configFile = new File(new File(home, ".ssh"), Constants.CONFIG);
  70. FileUtils.mkdir(configFile.getParentFile());
  71. System.setProperty("user.name", "jex_junit");
  72. osc = new OpenSshConfig(home, configFile);
  73. }
  74. private void config(final String data) throws IOException {
  75. final OutputStreamWriter fw = new OutputStreamWriter(
  76. new FileOutputStream(configFile), "UTF-8");
  77. fw.write(data);
  78. fw.close();
  79. }
  80. @Test
  81. public void testNoConfig() {
  82. final Host h = osc.lookup("repo.or.cz");
  83. assertNotNull(h);
  84. assertEquals("repo.or.cz", h.getHostName());
  85. assertEquals("jex_junit", h.getUser());
  86. assertEquals(22, h.getPort());
  87. assertEquals(1, h.getConnectionAttempts());
  88. assertNull(h.getIdentityFile());
  89. }
  90. @Test
  91. public void testSeparatorParsing() throws Exception {
  92. config("Host\tfirst\n" +
  93. "\tHostName\tfirst.tld\n" +
  94. "\n" +
  95. "Host second\n" +
  96. " HostName\tsecond.tld\n" +
  97. "Host=third\n" +
  98. "HostName=third.tld\n\n\n" +
  99. "\t Host = fourth\n\n\n" +
  100. " \t HostName\t=fourth.tld\n" +
  101. "Host\t = last\n" +
  102. "HostName \t last.tld");
  103. assertNotNull(osc.lookup("first"));
  104. assertEquals("first.tld", osc.lookup("first").getHostName());
  105. assertNotNull(osc.lookup("second"));
  106. assertEquals("second.tld", osc.lookup("second").getHostName());
  107. assertNotNull(osc.lookup("third"));
  108. assertEquals("third.tld", osc.lookup("third").getHostName());
  109. assertNotNull(osc.lookup("fourth"));
  110. assertEquals("fourth.tld", osc.lookup("fourth").getHostName());
  111. assertNotNull(osc.lookup("last"));
  112. assertEquals("last.tld", osc.lookup("last").getHostName());
  113. }
  114. @Test
  115. public void testQuoteParsing() throws Exception {
  116. config("Host \"good\"\n" +
  117. " HostName=\"good.tld\"\n" +
  118. " Port=\"6007\"\n" +
  119. " User=\"gooduser\"\n" +
  120. "Host multiple unquoted and \"quoted\" \"hosts\"\n" +
  121. " Port=\"2222\"\n" +
  122. "Host \"spaced\"\n" +
  123. "# Bad host name, but testing preservation of spaces\n" +
  124. " HostName=\" spaced\ttld \"\n" +
  125. "# Misbalanced quotes\n" +
  126. "Host \"bad\"\n" +
  127. "# OpenSSH doesn't allow this but ...\n" +
  128. " HostName=bad.tld\"\n");
  129. assertEquals("good.tld", osc.lookup("good").getHostName());
  130. assertEquals("gooduser", osc.lookup("good").getUser());
  131. assertEquals(6007, osc.lookup("good").getPort());
  132. assertEquals(2222, osc.lookup("multiple").getPort());
  133. assertEquals(2222, osc.lookup("quoted").getPort());
  134. assertEquals(2222, osc.lookup("and").getPort());
  135. assertEquals(2222, osc.lookup("unquoted").getPort());
  136. assertEquals(2222, osc.lookup("hosts").getPort());
  137. assertEquals(" spaced\ttld ", osc.lookup("spaced").getHostName());
  138. assertEquals("bad.tld\"", osc.lookup("bad").getHostName());
  139. }
  140. @Test
  141. public void testAlias_DoesNotMatch() throws Exception {
  142. config("Host orcz\n" + "\tHostName repo.or.cz\n");
  143. final Host h = osc.lookup("repo.or.cz");
  144. assertNotNull(h);
  145. assertEquals("repo.or.cz", h.getHostName());
  146. assertEquals("jex_junit", h.getUser());
  147. assertEquals(22, h.getPort());
  148. assertNull(h.getIdentityFile());
  149. }
  150. @Test
  151. public void testAlias_OptionsSet() throws Exception {
  152. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\tPort 2222\n"
  153. + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
  154. + "\tForwardX11 no\n");
  155. final Host h = osc.lookup("orcz");
  156. assertNotNull(h);
  157. assertEquals("repo.or.cz", h.getHostName());
  158. assertEquals("jex", h.getUser());
  159. assertEquals(2222, h.getPort());
  160. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  161. }
  162. @Test
  163. public void testAlias_OptionsKeywordCaseInsensitive() throws Exception {
  164. config("hOsT orcz\n" + "\thOsTnAmE repo.or.cz\n" + "\tPORT 2222\n"
  165. + "\tuser jex\n" + "\tidentityfile .ssh/id_jex\n"
  166. + "\tForwardX11 no\n");
  167. final Host h = osc.lookup("orcz");
  168. assertNotNull(h);
  169. assertEquals("repo.or.cz", h.getHostName());
  170. assertEquals("jex", h.getUser());
  171. assertEquals(2222, h.getPort());
  172. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  173. }
  174. @Test
  175. public void testAlias_OptionsInherit() throws Exception {
  176. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  177. + "\tHostName not.a.host.example.com\n" + "\tPort 2222\n"
  178. + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
  179. + "\tForwardX11 no\n");
  180. final Host h = osc.lookup("orcz");
  181. assertNotNull(h);
  182. assertEquals("repo.or.cz", h.getHostName());
  183. assertEquals("jex", h.getUser());
  184. assertEquals(2222, h.getPort());
  185. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  186. }
  187. @Test
  188. public void testAlias_PreferredAuthenticationsDefault() throws Exception {
  189. final Host h = osc.lookup("orcz");
  190. assertNotNull(h);
  191. assertNull(h.getPreferredAuthentications());
  192. }
  193. @Test
  194. public void testAlias_PreferredAuthentications() throws Exception {
  195. config("Host orcz\n" + "\tPreferredAuthentications publickey\n");
  196. final Host h = osc.lookup("orcz");
  197. assertNotNull(h);
  198. assertEquals("publickey", h.getPreferredAuthentications());
  199. }
  200. @Test
  201. public void testAlias_InheritPreferredAuthentications() throws Exception {
  202. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  203. + "\tPreferredAuthentications publickey, hostbased\n");
  204. final Host h = osc.lookup("orcz");
  205. assertNotNull(h);
  206. assertEquals("publickey,hostbased", h.getPreferredAuthentications());
  207. }
  208. @Test
  209. public void testAlias_BatchModeDefault() throws Exception {
  210. final Host h = osc.lookup("orcz");
  211. assertNotNull(h);
  212. assertFalse(h.isBatchMode());
  213. }
  214. @Test
  215. public void testAlias_BatchModeYes() throws Exception {
  216. config("Host orcz\n" + "\tBatchMode yes\n");
  217. final Host h = osc.lookup("orcz");
  218. assertNotNull(h);
  219. assertTrue(h.isBatchMode());
  220. }
  221. @Test
  222. public void testAlias_InheritBatchMode() throws Exception {
  223. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  224. + "\tBatchMode yes\n");
  225. final Host h = osc.lookup("orcz");
  226. assertNotNull(h);
  227. assertTrue(h.isBatchMode());
  228. }
  229. @Test
  230. public void testAlias_ConnectionAttemptsDefault() throws Exception {
  231. final Host h = osc.lookup("orcz");
  232. assertNotNull(h);
  233. assertEquals(1, h.getConnectionAttempts());
  234. }
  235. @Test
  236. public void testAlias_ConnectionAttempts() throws Exception {
  237. config("Host orcz\n" + "\tConnectionAttempts 5\n");
  238. final Host h = osc.lookup("orcz");
  239. assertNotNull(h);
  240. assertEquals(5, h.getConnectionAttempts());
  241. }
  242. @Test
  243. public void testAlias_invalidConnectionAttempts() throws Exception {
  244. config("Host orcz\n" + "\tConnectionAttempts -1\n");
  245. final Host h = osc.lookup("orcz");
  246. assertNotNull(h);
  247. assertEquals(1, h.getConnectionAttempts());
  248. }
  249. @Test
  250. public void testAlias_badConnectionAttempts() throws Exception {
  251. config("Host orcz\n" + "\tConnectionAttempts xxx\n");
  252. final Host h = osc.lookup("orcz");
  253. assertNotNull(h);
  254. assertEquals(1, h.getConnectionAttempts());
  255. }
  256. }