Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

OpenSshConfigTest.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. @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. assertEquals(1, h.getConnectionAttempts());
  87. assertNull(h.getIdentityFile());
  88. }
  89. @Test
  90. public void testSeparatorParsing() throws Exception {
  91. config("Host\tfirst\n" +
  92. "\tHostName\tfirst.tld\n" +
  93. "\n" +
  94. "Host second\n" +
  95. " HostName\tsecond.tld\n" +
  96. "Host=third\n" +
  97. "HostName=third.tld\n\n\n" +
  98. "\t Host = fourth\n\n\n" +
  99. " \t HostName\t=fourth.tld\n" +
  100. "Host\t = last\n" +
  101. "HostName \t last.tld");
  102. assertNotNull(osc.lookup("first"));
  103. assertEquals("first.tld", osc.lookup("first").getHostName());
  104. assertNotNull(osc.lookup("second"));
  105. assertEquals("second.tld", osc.lookup("second").getHostName());
  106. assertNotNull(osc.lookup("third"));
  107. assertEquals("third.tld", osc.lookup("third").getHostName());
  108. assertNotNull(osc.lookup("fourth"));
  109. assertEquals("fourth.tld", osc.lookup("fourth").getHostName());
  110. assertNotNull(osc.lookup("last"));
  111. assertEquals("last.tld", osc.lookup("last").getHostName());
  112. }
  113. @Test
  114. public void testQuoteParsing() throws Exception {
  115. config("Host \"good\"\n" +
  116. " HostName=\"good.tld\"\n" +
  117. " Port=\"6007\"\n" +
  118. " User=\"gooduser\"\n" +
  119. "Host multiple unquoted and \"quoted\" \"hosts\"\n" +
  120. " Port=\"2222\"\n" +
  121. "Host \"spaced\"\n" +
  122. "# Bad host name, but testing preservation of spaces\n" +
  123. " HostName=\" spaced\ttld \"\n" +
  124. "# Misbalanced quotes\n" +
  125. "Host \"bad\"\n" +
  126. "# OpenSSH doesn't allow this but ...\n" +
  127. " HostName=bad.tld\"\n");
  128. assertEquals("good.tld", osc.lookup("good").getHostName());
  129. assertEquals("gooduser", osc.lookup("good").getUser());
  130. assertEquals(6007, osc.lookup("good").getPort());
  131. assertEquals(2222, osc.lookup("multiple").getPort());
  132. assertEquals(2222, osc.lookup("quoted").getPort());
  133. assertEquals(2222, osc.lookup("and").getPort());
  134. assertEquals(2222, osc.lookup("unquoted").getPort());
  135. assertEquals(2222, osc.lookup("hosts").getPort());
  136. assertEquals(" spaced\ttld ", osc.lookup("spaced").getHostName());
  137. assertEquals("bad.tld\"", osc.lookup("bad").getHostName());
  138. }
  139. @Test
  140. public void testAlias_DoesNotMatch() throws Exception {
  141. config("Host orcz\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. }
  149. @Test
  150. public void testAlias_OptionsSet() throws Exception {
  151. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\tPort 2222\n"
  152. + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
  153. + "\tForwardX11 no\n");
  154. final Host h = osc.lookup("orcz");
  155. assertNotNull(h);
  156. assertEquals("repo.or.cz", h.getHostName());
  157. assertEquals("jex", h.getUser());
  158. assertEquals(2222, h.getPort());
  159. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  160. }
  161. @Test
  162. public void testAlias_OptionsKeywordCaseInsensitive() throws Exception {
  163. config("hOsT orcz\n" + "\thOsTnAmE repo.or.cz\n" + "\tPORT 2222\n"
  164. + "\tuser jex\n" + "\tidentityfile .ssh/id_jex\n"
  165. + "\tForwardX11 no\n");
  166. final Host h = osc.lookup("orcz");
  167. assertNotNull(h);
  168. assertEquals("repo.or.cz", h.getHostName());
  169. assertEquals("jex", h.getUser());
  170. assertEquals(2222, h.getPort());
  171. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  172. }
  173. @Test
  174. public void testAlias_OptionsInherit() throws Exception {
  175. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  176. + "\tHostName not.a.host.example.com\n" + "\tPort 2222\n"
  177. + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
  178. + "\tForwardX11 no\n");
  179. final Host h = osc.lookup("orcz");
  180. assertNotNull(h);
  181. assertEquals("repo.or.cz", h.getHostName());
  182. assertEquals("jex", h.getUser());
  183. assertEquals(2222, h.getPort());
  184. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  185. }
  186. @Test
  187. public void testAlias_PreferredAuthenticationsDefault() throws Exception {
  188. final Host h = osc.lookup("orcz");
  189. assertNotNull(h);
  190. assertNull(h.getPreferredAuthentications());
  191. }
  192. @Test
  193. public void testAlias_PreferredAuthentications() throws Exception {
  194. config("Host orcz\n" + "\tPreferredAuthentications publickey\n");
  195. final Host h = osc.lookup("orcz");
  196. assertNotNull(h);
  197. assertEquals("publickey", h.getPreferredAuthentications());
  198. }
  199. @Test
  200. public void testAlias_InheritPreferredAuthentications() throws Exception {
  201. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  202. + "\tPreferredAuthentications publickey, hostbased\n");
  203. final Host h = osc.lookup("orcz");
  204. assertNotNull(h);
  205. assertEquals("publickey,hostbased", h.getPreferredAuthentications());
  206. }
  207. @Test
  208. public void testAlias_BatchModeDefault() throws Exception {
  209. final Host h = osc.lookup("orcz");
  210. assertNotNull(h);
  211. assertFalse(h.isBatchMode());
  212. }
  213. @Test
  214. public void testAlias_BatchModeYes() throws Exception {
  215. config("Host orcz\n" + "\tBatchMode yes\n");
  216. final Host h = osc.lookup("orcz");
  217. assertNotNull(h);
  218. assertTrue(h.isBatchMode());
  219. }
  220. @Test
  221. public void testAlias_InheritBatchMode() throws Exception {
  222. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  223. + "\tBatchMode yes\n");
  224. final Host h = osc.lookup("orcz");
  225. assertNotNull(h);
  226. assertTrue(h.isBatchMode());
  227. }
  228. @Test
  229. public void testAlias_ConnectionAttemptsDefault() throws Exception {
  230. final Host h = osc.lookup("orcz");
  231. assertNotNull(h);
  232. assertEquals(1, h.getConnectionAttempts());
  233. }
  234. @Test
  235. public void testAlias_ConnectionAttempts() throws Exception {
  236. config("Host orcz\n" + "\tConnectionAttempts 5\n");
  237. final Host h = osc.lookup("orcz");
  238. assertNotNull(h);
  239. assertEquals(5, h.getConnectionAttempts());
  240. }
  241. @Test
  242. public void testAlias_invalidConnectionAttempts() throws Exception {
  243. config("Host orcz\n" + "\tConnectionAttempts -1\n");
  244. final Host h = osc.lookup("orcz");
  245. assertNotNull(h);
  246. assertEquals(1, h.getConnectionAttempts());
  247. }
  248. @Test
  249. public void testAlias_badConnectionAttempts() throws Exception {
  250. config("Host orcz\n" + "\tConnectionAttempts xxx\n");
  251. final Host h = osc.lookup("orcz");
  252. assertNotNull(h);
  253. assertEquals(1, h.getConnectionAttempts());
  254. }
  255. }