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