Add support for ~/.ssh/config BatchMode
[egit/charleso.git] / org.spearce.jgit.test / tst / org / spearce / jgit / transport / OpenSshConfigTest.java
blob959b6b76b3d9d0dc451a901849877305cd4d551c
1 /*
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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;
40 import java.io.File;
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 {
49 private File home;
51 private File configFile;
53 private OpenSshConfig osc;
55 public void setUp() throws Exception {
56 super.setUp();
58 home = new File(trash, "home");
59 home.mkdir();
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");
71 fw.write(data);
72 fw.close();
75 public void testNoConfig() {
76 final Host h = osc.lookup("repo.or.cz");
77 assertNotNull(h);
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");
87 assertNotNull(h);
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");
99 assertNotNull(h);
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");
111 assertNotNull(h);
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");
124 assertNotNull(h);
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");
133 assertNotNull(h);
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");
140 assertNotNull(h);
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");
148 assertNotNull(h);
149 assertEquals("publickey,hostbased", h.getPreferredAuthentications());
152 public void testAlias_BatchModeDefault() throws Exception {
153 final Host h = osc.lookup("orcz");
154 assertNotNull(h);
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");
161 assertNotNull(h);
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");
169 assertNotNull(h);
170 assertEquals(true, h.isBatchMode());