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 testAlias_DoesNotMatch() throws Exception
{
85 config("Host orcz\n" + "\tHostName repo.or.cz\n");
86 final Host h
= osc
.lookup("repo.or.cz");
88 assertEquals("repo.or.cz", h
.getHostName());
89 assertEquals("jex_junit", h
.getUser());
90 assertEquals(22, h
.getPort());
91 assertNull(h
.getIdentityFile());
94 public void testAlias_OptionsSet() throws Exception
{
95 config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\tPort 2222\n"
96 + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
97 + "\tForwardX11 no\n");
98 final Host h
= osc
.lookup("orcz");
100 assertEquals("repo.or.cz", h
.getHostName());
101 assertEquals("jex", h
.getUser());
102 assertEquals(2222, h
.getPort());
103 assertEquals(new File(home
, ".ssh/id_jex"), h
.getIdentityFile());
106 public void testAlias_OptionsKeywordCaseInsensitive() throws Exception
{
107 config("hOsT orcz\n" + "\thOsTnAmE repo.or.cz\n" + "\tPORT 2222\n"
108 + "\tuser jex\n" + "\tidentityfile .ssh/id_jex\n"
109 + "\tForwardX11 no\n");
110 final Host h
= osc
.lookup("orcz");
112 assertEquals("repo.or.cz", h
.getHostName());
113 assertEquals("jex", h
.getUser());
114 assertEquals(2222, h
.getPort());
115 assertEquals(new File(home
, ".ssh/id_jex"), h
.getIdentityFile());
118 public void testAlias_OptionsInherit() throws Exception
{
119 config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
120 + "\tHostName not.a.host.example.com\n" + "\tPort 2222\n"
121 + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
122 + "\tForwardX11 no\n");
123 final Host h
= osc
.lookup("orcz");
125 assertEquals("repo.or.cz", h
.getHostName());
126 assertEquals("jex", h
.getUser());
127 assertEquals(2222, h
.getPort());
128 assertEquals(new File(home
, ".ssh/id_jex"), h
.getIdentityFile());
131 public void testAlias_PreferredAuthenticationsDefault() throws Exception
{
132 final Host h
= osc
.lookup("orcz");
134 assertNull(h
.getPreferredAuthentications());
137 public void testAlias_PreferredAuthentications() throws Exception
{
138 config("Host orcz\n" + "\tPreferredAuthentications publickey\n");
139 final Host h
= osc
.lookup("orcz");
141 assertEquals("publickey", h
.getPreferredAuthentications());
144 public void testAlias_InheritPreferredAuthentications() throws Exception
{
145 config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
146 + "\tPreferredAuthentications publickey, hostbased\n");
147 final Host h
= osc
.lookup("orcz");
149 assertEquals("publickey,hostbased", h
.getPreferredAuthentications());
152 public void testAlias_BatchModeDefault() throws Exception
{
153 final Host h
= osc
.lookup("orcz");
155 assertEquals(false, h
.isBatchMode());
158 public void testAlias_BatchModeYes() throws Exception
{
159 config("Host orcz\n" + "\tBatchMode yes\n");
160 final Host h
= osc
.lookup("orcz");
162 assertEquals(true, h
.isBatchMode());
165 public void testAlias_InheritBatchMode() throws Exception
{
166 config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
167 + "\tBatchMode yes\n");
168 final Host h
= osc
.lookup("orcz");
170 assertEquals(true, h
.isBatchMode());