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

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