2 * Copyright (C) 2008, Google Inc.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.transport
;
41 import java
.io
.FileOutputStream
;
42 import java
.io
.IOException
;
43 import java
.io
.OutputStreamWriter
;
45 import org
.spearce
.jgit
.lib
.RepositoryTestCase
;
46 import org
.spearce
.jgit
.transport
.OpenSshConfig
.Host
;
48 public class OpenSshConfigTest
extends RepositoryTestCase
{
51 private File configFile
;
53 private OpenSshConfig osc
;
55 public void setUp() throws Exception
{
58 home
= new File(trash
, "home");
61 configFile
= new File(new File(home
, ".ssh"), "config");
62 configFile
.getParentFile().mkdir();
64 System
.setProperty("user.name", "jex_junit");
65 osc
= new OpenSshConfig(home
, configFile
);
68 private void config(final String data
) throws IOException
{
69 final OutputStreamWriter fw
= new OutputStreamWriter(
70 new FileOutputStream(configFile
), "UTF-8");
75 public void testNoConfig() {
76 final Host h
= osc
.lookup("repo.or.cz");
78 assertEquals("repo.or.cz", h
.getHostName());
79 assertEquals("jex_junit", h
.getUser());
80 assertEquals(22, h
.getPort());
81 assertNull(h
.getIdentityFile());
84 public void testSeparatorParsing() throws Exception
{
85 config("Host\tfirst\n" +
86 "\tHostName\tfirst.tld\n" +
89 " HostName\tsecond.tld\n" +
91 "HostName=third.tld\n\n\n" +
92 "\t Host = fourth\n\n\n" +
93 " \t HostName\t=fourth.tld\n" +
95 "HostName \t last.tld");
96 assertNotNull(osc
.lookup("first"));
97 assertEquals("first.tld", osc
.lookup("first").getHostName());
98 assertNotNull(osc
.lookup("second"));
99 assertEquals("second.tld", osc
.lookup("second").getHostName());
100 assertNotNull(osc
.lookup("third"));
101 assertEquals("third.tld", osc
.lookup("third").getHostName());
102 assertNotNull(osc
.lookup("last"));
103 assertEquals("last.tld", osc
.lookup("last").getHostName());
106 public void testAlias_DoesNotMatch() throws Exception
{
107 config("Host orcz\n" + "\tHostName repo.or.cz\n");
108 final Host h
= osc
.lookup("repo.or.cz");
110 assertEquals("repo.or.cz", h
.getHostName());
111 assertEquals("jex_junit", h
.getUser());
112 assertEquals(22, h
.getPort());
113 assertNull(h
.getIdentityFile());
116 public void testAlias_OptionsSet() throws Exception
{
117 config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\tPort 2222\n"
118 + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
119 + "\tForwardX11 no\n");
120 final Host h
= osc
.lookup("orcz");
122 assertEquals("repo.or.cz", h
.getHostName());
123 assertEquals("jex", h
.getUser());
124 assertEquals(2222, h
.getPort());
125 assertEquals(new File(home
, ".ssh/id_jex"), h
.getIdentityFile());
128 public void testAlias_OptionsKeywordCaseInsensitive() throws Exception
{
129 config("hOsT orcz\n" + "\thOsTnAmE repo.or.cz\n" + "\tPORT 2222\n"
130 + "\tuser jex\n" + "\tidentityfile .ssh/id_jex\n"
131 + "\tForwardX11 no\n");
132 final Host h
= osc
.lookup("orcz");
134 assertEquals("repo.or.cz", h
.getHostName());
135 assertEquals("jex", h
.getUser());
136 assertEquals(2222, h
.getPort());
137 assertEquals(new File(home
, ".ssh/id_jex"), h
.getIdentityFile());
140 public void testAlias_OptionsInherit() throws Exception
{
141 config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
142 + "\tHostName not.a.host.example.com\n" + "\tPort 2222\n"
143 + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
144 + "\tForwardX11 no\n");
145 final Host h
= osc
.lookup("orcz");
147 assertEquals("repo.or.cz", h
.getHostName());
148 assertEquals("jex", h
.getUser());
149 assertEquals(2222, h
.getPort());
150 assertEquals(new File(home
, ".ssh/id_jex"), h
.getIdentityFile());
153 public void testAlias_PreferredAuthenticationsDefault() throws Exception
{
154 final Host h
= osc
.lookup("orcz");
156 assertNull(h
.getPreferredAuthentications());
159 public void testAlias_PreferredAuthentications() throws Exception
{
160 config("Host orcz\n" + "\tPreferredAuthentications publickey\n");
161 final Host h
= osc
.lookup("orcz");
163 assertEquals("publickey", h
.getPreferredAuthentications());
166 public void testAlias_InheritPreferredAuthentications() throws Exception
{
167 config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
168 + "\tPreferredAuthentications publickey, hostbased\n");
169 final Host h
= osc
.lookup("orcz");
171 assertEquals("publickey,hostbased", h
.getPreferredAuthentications());
174 public void testAlias_BatchModeDefault() throws Exception
{
175 final Host h
= osc
.lookup("orcz");
177 assertEquals(false, h
.isBatchMode());
180 public void testAlias_BatchModeYes() throws Exception
{
181 config("Host orcz\n" + "\tBatchMode yes\n");
182 final Host h
= osc
.lookup("orcz");
184 assertEquals(true, h
.isBatchMode());
187 public void testAlias_InheritBatchMode() throws Exception
{
188 config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
189 + "\tBatchMode yes\n");
190 final Host h
= osc
.lookup("orcz");
192 assertEquals(true, h
.isBatchMode());