1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 }