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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 java.io.File;
  45. import java.io.FileOutputStream;
  46. import java.io.IOException;
  47. import java.io.OutputStreamWriter;
  48. import org.eclipse.jgit.lib.RepositoryTestCase;
  49. import org.eclipse.jgit.transport.OpenSshConfig.Host;
  50. public class OpenSshConfigTest extends RepositoryTestCase {
  51. private File home;
  52. private File configFile;
  53. private OpenSshConfig osc;
  54. public void setUp() throws Exception {
  55. super.setUp();
  56. home = new File(trash, "home");
  57. home.mkdir();
  58. configFile = new File(new File(home, ".ssh"), "config");
  59. configFile.getParentFile().mkdir();
  60. System.setProperty("user.name", "jex_junit");
  61. osc = new OpenSshConfig(home, configFile);
  62. }
  63. private void config(final String data) throws IOException {
  64. final OutputStreamWriter fw = new OutputStreamWriter(
  65. new FileOutputStream(configFile), "UTF-8");
  66. fw.write(data);
  67. fw.close();
  68. }
  69. public void testNoConfig() {
  70. final Host h = osc.lookup("repo.or.cz");
  71. assertNotNull(h);
  72. assertEquals("repo.or.cz", h.getHostName());
  73. assertEquals("jex_junit", h.getUser());
  74. assertEquals(22, h.getPort());
  75. assertNull(h.getIdentityFile());
  76. }
  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. public void testQuoteParsing() throws Exception {
  101. config("Host \"good\"\n" +
  102. " HostName=\"good.tld\"\n" +
  103. " Port=\"6007\"\n" +
  104. " User=\"gooduser\"\n" +
  105. "Host multiple unquoted and \"quoted\" \"hosts\"\n" +
  106. " Port=\"2222\"\n" +
  107. "Host \"spaced\"\n" +
  108. "# Bad host name, but testing preservation of spaces\n" +
  109. " HostName=\" spaced\ttld \"\n" +
  110. "# Misbalanced quotes\n" +
  111. "Host \"bad\"\n" +
  112. "# OpenSSH doesn't allow this but ...\n" +
  113. " HostName=bad.tld\"\n");
  114. assertEquals("good.tld", osc.lookup("good").getHostName());
  115. assertEquals("gooduser", osc.lookup("good").getUser());
  116. assertEquals(6007, osc.lookup("good").getPort());
  117. assertEquals(2222, osc.lookup("multiple").getPort());
  118. assertEquals(2222, osc.lookup("quoted").getPort());
  119. assertEquals(2222, osc.lookup("and").getPort());
  120. assertEquals(2222, osc.lookup("unquoted").getPort());
  121. assertEquals(2222, osc.lookup("hosts").getPort());
  122. assertEquals(" spaced\ttld ", osc.lookup("spaced").getHostName());
  123. assertEquals("bad.tld\"", osc.lookup("bad").getHostName());
  124. }
  125. public void testAlias_DoesNotMatch() throws Exception {
  126. config("Host orcz\n" + "\tHostName repo.or.cz\n");
  127. final Host h = osc.lookup("repo.or.cz");
  128. assertNotNull(h);
  129. assertEquals("repo.or.cz", h.getHostName());
  130. assertEquals("jex_junit", h.getUser());
  131. assertEquals(22, h.getPort());
  132. assertNull(h.getIdentityFile());
  133. }
  134. public void testAlias_OptionsSet() throws Exception {
  135. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\tPort 2222\n"
  136. + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
  137. + "\tForwardX11 no\n");
  138. final Host h = osc.lookup("orcz");
  139. assertNotNull(h);
  140. assertEquals("repo.or.cz", h.getHostName());
  141. assertEquals("jex", h.getUser());
  142. assertEquals(2222, h.getPort());
  143. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  144. }
  145. public void testAlias_OptionsKeywordCaseInsensitive() throws Exception {
  146. config("hOsT orcz\n" + "\thOsTnAmE repo.or.cz\n" + "\tPORT 2222\n"
  147. + "\tuser jex\n" + "\tidentityfile .ssh/id_jex\n"
  148. + "\tForwardX11 no\n");
  149. final Host h = osc.lookup("orcz");
  150. assertNotNull(h);
  151. assertEquals("repo.or.cz", h.getHostName());
  152. assertEquals("jex", h.getUser());
  153. assertEquals(2222, h.getPort());
  154. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  155. }
  156. public void testAlias_OptionsInherit() throws Exception {
  157. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  158. + "\tHostName not.a.host.example.com\n" + "\tPort 2222\n"
  159. + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
  160. + "\tForwardX11 no\n");
  161. final Host h = osc.lookup("orcz");
  162. assertNotNull(h);
  163. assertEquals("repo.or.cz", h.getHostName());
  164. assertEquals("jex", h.getUser());
  165. assertEquals(2222, h.getPort());
  166. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  167. }
  168. public void testAlias_PreferredAuthenticationsDefault() throws Exception {
  169. final Host h = osc.lookup("orcz");
  170. assertNotNull(h);
  171. assertNull(h.getPreferredAuthentications());
  172. }
  173. public void testAlias_PreferredAuthentications() throws Exception {
  174. config("Host orcz\n" + "\tPreferredAuthentications publickey\n");
  175. final Host h = osc.lookup("orcz");
  176. assertNotNull(h);
  177. assertEquals("publickey", h.getPreferredAuthentications());
  178. }
  179. public void testAlias_InheritPreferredAuthentications() throws Exception {
  180. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  181. + "\tPreferredAuthentications publickey, hostbased\n");
  182. final Host h = osc.lookup("orcz");
  183. assertNotNull(h);
  184. assertEquals("publickey,hostbased", h.getPreferredAuthentications());
  185. }
  186. public void testAlias_BatchModeDefault() throws Exception {
  187. final Host h = osc.lookup("orcz");
  188. assertNotNull(h);
  189. assertEquals(false, h.isBatchMode());
  190. }
  191. public void testAlias_BatchModeYes() throws Exception {
  192. config("Host orcz\n" + "\tBatchMode yes\n");
  193. final Host h = osc.lookup("orcz");
  194. assertNotNull(h);
  195. assertEquals(true, h.isBatchMode());
  196. }
  197. public void testAlias_InheritBatchMode() throws Exception {
  198. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  199. + "\tBatchMode yes\n");
  200. final Host h = osc.lookup("orcz");
  201. assertNotNull(h);
  202. assertEquals(true, h.isBatchMode());
  203. }
  204. }