[test] test OpenSshConfigFile directly, not via the JSch config
This is a prerequisite for removing the JSch support bundle; otherwise OpenSshConfigFile would be left without tests. Copy OpenSshConfigTest from the JSch support bundle and adapt all tests to perform the equivalent checks on OpenSshConfigFile directly. Add a new lookupDefault() method to the SshConfigStore interface and implement it so that it behaves the same and the tests work identically. Change-Id: I046abd9197a8484003e77005024e5d973456f1a3
This commit is contained in:
parent
634302d2da
commit
c6d48ab2f8
|
@ -43,6 +43,7 @@ Import-Package: com.googlecode.javaewah;version="[1.1.6,2.0.0)",
|
|||
org.eclipse.jgit.internal.transport.connectivity;version="[6.0.0,6.1.0)",
|
||||
org.eclipse.jgit.internal.transport.http;version="[6.0.0,6.1.0)",
|
||||
org.eclipse.jgit.internal.transport.parser;version="[6.0.0,6.1.0)",
|
||||
org.eclipse.jgit.internal.transport.ssh;version="[6.0.0,6.1.0)",
|
||||
org.eclipse.jgit.junit;version="[6.0.0,6.1.0)",
|
||||
org.eclipse.jgit.junit.time;version="[6.0.0,6.1.0)",
|
||||
org.eclipse.jgit.lfs;version="[6.0.0,6.1.0)",
|
||||
|
|
|
@ -0,0 +1,605 @@
|
|||
/*
|
||||
* Copyright (C) 2008, 2021 Google Inc. and others
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Distribution License v. 1.0 which is available at
|
||||
* https://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
package org.eclipse.jgit.internal.transport.ssh;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.eclipse.jgit.junit.RepositoryTestCase;
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.transport.SshConfigStore.HostConfig;
|
||||
import org.eclipse.jgit.transport.SshConstants;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
import org.eclipse.jgit.util.FileUtils;
|
||||
import org.eclipse.jgit.util.SystemReader;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class OpenSshConfigFileTest extends RepositoryTestCase {
|
||||
|
||||
private File home;
|
||||
|
||||
private File configFile;
|
||||
|
||||
private OpenSshConfigFile osc;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
home = new File(trash, "home");
|
||||
FileUtils.mkdir(home);
|
||||
|
||||
configFile = new File(new File(home, ".ssh"), Constants.CONFIG);
|
||||
FileUtils.mkdir(configFile.getParentFile());
|
||||
|
||||
mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "jex_junit");
|
||||
mockSystemReader.setProperty("TST_VAR", "TEST");
|
||||
osc = new OpenSshConfigFile(home, configFile, "jex_junit");
|
||||
}
|
||||
|
||||
private void config(String data) throws IOException {
|
||||
FS fs = FS.DETECTED;
|
||||
long resolution = FS.getFileStoreAttributes(configFile.toPath())
|
||||
.getFsTimestampResolution().toNanos();
|
||||
Instant lastMtime = fs.lastModifiedInstant(configFile);
|
||||
do {
|
||||
try (final OutputStreamWriter fw = new OutputStreamWriter(
|
||||
new FileOutputStream(configFile), UTF_8)) {
|
||||
fw.write(data);
|
||||
TimeUnit.NANOSECONDS.sleep(resolution);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.interrupted();
|
||||
}
|
||||
} while (lastMtime.equals(fs.lastModifiedInstant(configFile)));
|
||||
}
|
||||
|
||||
private HostConfig lookup(String hostname) {
|
||||
return osc.lookupDefault(hostname, 0, null);
|
||||
}
|
||||
|
||||
private void assertHost(String expected, HostConfig h) {
|
||||
assertEquals(expected, h.getValue(SshConstants.HOST_NAME));
|
||||
}
|
||||
|
||||
private void assertUser(String expected, HostConfig h) {
|
||||
assertEquals(expected, h.getValue(SshConstants.USER));
|
||||
}
|
||||
|
||||
private void assertPort(int expected, HostConfig h) {
|
||||
assertEquals(expected,
|
||||
OpenSshConfigFile.positive(h.getValue(SshConstants.PORT)));
|
||||
}
|
||||
|
||||
private void assertIdentity(File expected, HostConfig h) {
|
||||
String actual = h.getValue(SshConstants.IDENTITY_FILE);
|
||||
if (expected == null) {
|
||||
assertNull(actual);
|
||||
} else {
|
||||
assertEquals(expected, new File(actual));
|
||||
}
|
||||
}
|
||||
|
||||
private void assertAttempts(int expected, HostConfig h) {
|
||||
assertEquals(expected, OpenSshConfigFile
|
||||
.positive(h.getValue(SshConstants.CONNECTION_ATTEMPTS)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoConfig() {
|
||||
final HostConfig h = lookup("repo.or.cz");
|
||||
assertNotNull(h);
|
||||
assertHost("repo.or.cz", h);
|
||||
assertUser("jex_junit", h);
|
||||
assertPort(22, h);
|
||||
assertAttempts(1, h);
|
||||
assertIdentity(null, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeparatorParsing() throws Exception {
|
||||
config("Host\tfirst\n" +
|
||||
"\tHostName\tfirst.tld\n" +
|
||||
"\n" +
|
||||
"Host second\n" +
|
||||
" HostName\tsecond.tld\n" +
|
||||
"Host=third\n" +
|
||||
"HostName=third.tld\n\n\n" +
|
||||
"\t Host = fourth\n\n\n" +
|
||||
" \t HostName\t=fourth.tld\n" +
|
||||
"Host\t = last\n" +
|
||||
"HostName \t last.tld");
|
||||
assertNotNull(lookup("first"));
|
||||
assertHost("first.tld", lookup("first"));
|
||||
assertNotNull(lookup("second"));
|
||||
assertHost("second.tld", lookup("second"));
|
||||
assertNotNull(lookup("third"));
|
||||
assertHost("third.tld", lookup("third"));
|
||||
assertNotNull(lookup("fourth"));
|
||||
assertHost("fourth.tld", lookup("fourth"));
|
||||
assertNotNull(lookup("last"));
|
||||
assertHost("last.tld", lookup("last"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuoteParsing() throws Exception {
|
||||
config("Host \"good\"\n" +
|
||||
" HostName=\"good.tld\"\n" +
|
||||
" Port=\"6007\"\n" +
|
||||
" User=\"gooduser\"\n" +
|
||||
"Host multiple unquoted and \"quoted\" \"hosts\"\n" +
|
||||
" Port=\"2222\"\n" +
|
||||
"Host \"spaced\"\n" +
|
||||
"# Bad host name, but testing preservation of spaces\n" +
|
||||
" HostName=\" spaced\ttld \"\n" +
|
||||
"# Misbalanced quotes\n" +
|
||||
"Host \"bad\"\n" +
|
||||
"# OpenSSH doesn't allow this but ...\n" +
|
||||
" HostName=bad.tld\"\n");
|
||||
assertHost("good.tld", lookup("good"));
|
||||
assertUser("gooduser", lookup("good"));
|
||||
assertPort(6007, lookup("good"));
|
||||
assertPort(2222, lookup("multiple"));
|
||||
assertPort(2222, lookup("quoted"));
|
||||
assertPort(2222, lookup("and"));
|
||||
assertPort(2222, lookup("unquoted"));
|
||||
assertPort(2222, lookup("hosts"));
|
||||
assertHost(" spaced\ttld ", lookup("spaced"));
|
||||
assertHost("bad.tld\"", lookup("bad"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCaseInsensitiveKeyLookup() throws Exception {
|
||||
config("Host orcz\n" + "Port 29418\n"
|
||||
+ "\tHostName repo.or.cz\nStrictHostKeyChecking yes\n");
|
||||
final HostConfig c = lookup("orcz");
|
||||
String exactCase = c.getValue("StrictHostKeyChecking");
|
||||
assertEquals("yes", exactCase);
|
||||
assertEquals(exactCase, c.getValue("stricthostkeychecking"));
|
||||
assertEquals(exactCase, c.getValue("STRICTHOSTKEYCHECKING"));
|
||||
assertEquals(exactCase, c.getValue("sTrIcThostKEYcheckING"));
|
||||
assertNull(c.getValue("sTrIcThostKEYcheckIN"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_DoesNotMatch() throws Exception {
|
||||
config("Host orcz\n" + "Port 29418\n"
|
||||
+ "\tHostName repo.or.cz\n");
|
||||
final HostConfig h = lookup("repo.or.cz");
|
||||
assertNotNull(h);
|
||||
assertHost("repo.or.cz", h);
|
||||
assertUser("jex_junit", h);
|
||||
assertPort(22, h);
|
||||
assertIdentity(null, h);
|
||||
final HostConfig h2 = lookup("orcz");
|
||||
assertHost("repo.or.cz", h);
|
||||
assertUser("jex_junit", h);
|
||||
assertPort(29418, h2);
|
||||
assertIdentity(null, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_OptionsSet() throws Exception {
|
||||
config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\tPort 2222\n"
|
||||
+ "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
|
||||
+ "\tForwardX11 no\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertHost("repo.or.cz", h);
|
||||
assertUser("jex", h);
|
||||
assertPort(2222, h);
|
||||
assertIdentity(new File(home, ".ssh/id_jex"), h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_OptionsKeywordCaseInsensitive() throws Exception {
|
||||
config("hOsT orcz\n" + "\thOsTnAmE repo.or.cz\n" + "\tPORT 2222\n"
|
||||
+ "\tuser jex\n" + "\tidentityfile .ssh/id_jex\n"
|
||||
+ "\tForwardX11 no\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertHost("repo.or.cz", h);
|
||||
assertUser("jex", h);
|
||||
assertPort(2222, h);
|
||||
assertIdentity(new File(home, ".ssh/id_jex"), h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_OptionsInherit() throws Exception {
|
||||
config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
|
||||
+ "\tHostName not.a.host.example.com\n" + "\tPort 2222\n"
|
||||
+ "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
|
||||
+ "\tForwardX11 no\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertHost("repo.or.cz", h);
|
||||
assertUser("jex", h);
|
||||
assertPort(2222, h);
|
||||
assertIdentity(new File(home, ".ssh/id_jex"), h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_PreferredAuthenticationsDefault() throws Exception {
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertNull(h.getValue(SshConstants.PREFERRED_AUTHENTICATIONS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_PreferredAuthentications() throws Exception {
|
||||
config("Host orcz\n" + "\tPreferredAuthentications publickey\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertEquals("publickey",
|
||||
h.getValue(SshConstants.PREFERRED_AUTHENTICATIONS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_InheritPreferredAuthentications() throws Exception {
|
||||
config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
|
||||
+ "\tPreferredAuthentications publickey, hostbased\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertEquals("publickey,hostbased",
|
||||
h.getValue(SshConstants.PREFERRED_AUTHENTICATIONS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_BatchModeDefault() throws Exception {
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertNull(h.getValue(SshConstants.BATCH_MODE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_BatchModeYes() throws Exception {
|
||||
config("Host orcz\n" + "\tBatchMode yes\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertTrue(OpenSshConfigFile.flag(h.getValue(SshConstants.BATCH_MODE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_InheritBatchMode() throws Exception {
|
||||
config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
|
||||
+ "\tBatchMode yes\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertTrue(OpenSshConfigFile.flag(h.getValue(SshConstants.BATCH_MODE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_ConnectionAttemptsDefault() throws Exception {
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertAttempts(1, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_ConnectionAttempts() throws Exception {
|
||||
config("Host orcz\n" + "\tConnectionAttempts 5\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertAttempts(5, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_invalidConnectionAttempts() throws Exception {
|
||||
config("Host orcz\n" + "\tConnectionAttempts -1\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertAttempts(1, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlias_badConnectionAttempts() throws Exception {
|
||||
config("Host orcz\n" + "\tConnectionAttempts xxx\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertAttempts(1, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultBlock() throws Exception {
|
||||
config("ConnectionAttempts 5\n\nHost orcz\nConnectionAttempts 3\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertAttempts(5, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHostCaseInsensitive() throws Exception {
|
||||
config("hOsT orcz\nConnectionAttempts 3\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertAttempts(3, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListValueSingle() throws Exception {
|
||||
config("Host orcz\nUserKnownHostsFile /foo/bar\n");
|
||||
final HostConfig c = lookup("orcz");
|
||||
assertNotNull(c);
|
||||
assertEquals("/foo/bar", c.getValue("UserKnownHostsFile"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListValueMultiple() throws Exception {
|
||||
// Tilde expansion occurs within the parser
|
||||
config("Host orcz\nUserKnownHostsFile \"~/foo/ba z\" /foo/bar \n");
|
||||
final HostConfig c = lookup("orcz");
|
||||
assertNotNull(c);
|
||||
assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
|
||||
"/foo/bar" },
|
||||
c.getValues("UserKnownHostsFile").toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRepeatedLookupsWithModification() throws Exception {
|
||||
config("Host orcz\n" + "\tConnectionAttempts -1\n");
|
||||
final HostConfig h1 = lookup("orcz");
|
||||
assertNotNull(h1);
|
||||
assertAttempts(1, h1);
|
||||
config("Host orcz\n" + "\tConnectionAttempts 5\n");
|
||||
final HostConfig h2 = lookup("orcz");
|
||||
assertNotNull(h2);
|
||||
assertNotSame(h1, h2);
|
||||
assertAttempts(5, h2);
|
||||
assertAttempts(1, h1);
|
||||
assertNotSame(h1, h2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIdentityFile() throws Exception {
|
||||
config("Host orcz\nIdentityFile \"~/foo/ba z\"\nIdentityFile /foo/bar");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
// Does tilde replacement
|
||||
assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
|
||||
"/foo/bar" },
|
||||
h.getValues(SshConstants.IDENTITY_FILE).toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiIdentityFile() throws Exception {
|
||||
config("IdentityFile \"~/foo/ba z\"\nHost orcz\nIdentityFile /foo/bar\nHOST *\nIdentityFile /foo/baz");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
|
||||
"/foo/bar", "/foo/baz" },
|
||||
h.getValues(SshConstants.IDENTITY_FILE).toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegatedPattern() throws Exception {
|
||||
config("Host repo.or.cz\nIdentityFile ~/foo/bar\nHOST !*.or.cz\nIdentityFile /foo/baz");
|
||||
final HostConfig h = lookup("repo.or.cz");
|
||||
assertNotNull(h);
|
||||
assertIdentity(new File(home, "foo/bar"), h);
|
||||
assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath() },
|
||||
h.getValues(SshConstants.IDENTITY_FILE).toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPattern() throws Exception {
|
||||
config("Host repo.or.cz\nIdentityFile ~/foo/bar\nHOST *.or.cz\nIdentityFile /foo/baz");
|
||||
final HostConfig h = lookup("repo.or.cz");
|
||||
assertNotNull(h);
|
||||
assertIdentity(new File(home, "foo/bar"), h);
|
||||
assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath(),
|
||||
"/foo/baz" },
|
||||
h.getValues(SshConstants.IDENTITY_FILE).toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiHost() throws Exception {
|
||||
config("Host orcz *.or.cz\nIdentityFile ~/foo/bar\nHOST *.or.cz\nIdentityFile /foo/baz");
|
||||
final HostConfig h1 = lookup("repo.or.cz");
|
||||
assertNotNull(h1);
|
||||
assertIdentity(new File(home, "foo/bar"), h1);
|
||||
assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath(),
|
||||
"/foo/baz" },
|
||||
h1.getValues(SshConstants.IDENTITY_FILE).toArray());
|
||||
final HostConfig h2 = lookup("orcz");
|
||||
assertNotNull(h2);
|
||||
assertIdentity(new File(home, "foo/bar"), h2);
|
||||
assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath() },
|
||||
h2.getValues(SshConstants.IDENTITY_FILE).toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsSign() throws Exception {
|
||||
config("Host=orcz\n\tConnectionAttempts = 5\n\tUser=\t foobar\t\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertAttempts(5, h);
|
||||
assertUser("foobar", h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingArgument() throws Exception {
|
||||
config("Host=orcz\n\tSendEnv\nIdentityFile\t\nForwardX11\n\tUser=\t foobar\t\n");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertUser("foobar", h);
|
||||
assertEquals("[]", h.getValues("SendEnv").toString());
|
||||
assertIdentity(null, h);
|
||||
assertNull(h.getValue("ForwardX11"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHomeDirUserReplacement() throws Exception {
|
||||
config("Host=orcz\n\tIdentityFile %d/.ssh/%u_id_dsa");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertIdentity(new File(new File(home, ".ssh"), "jex_junit_id_dsa"), h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHostnameReplacement() throws Exception {
|
||||
config("Host=orcz\nHost *.*\n\tHostname %h\nHost *\n\tHostname %h.example.org");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertHost("orcz.example.org", h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoteUserReplacement() throws Exception {
|
||||
config("Host=orcz\n\tUser foo\n" + "Host *.*\n\tHostname %h\n"
|
||||
+ "Host *\n\tHostname %h.ex%%20ample.org\n\tIdentityFile ~/.ssh/%h_%r_id_dsa");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertIdentity(
|
||||
new File(new File(home, ".ssh"),
|
||||
"orcz.ex%20ample.org_foo_id_dsa"),
|
||||
h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalhostFQDNReplacement() throws Exception {
|
||||
String localhost = SystemReader.getInstance().getHostname();
|
||||
config("Host=orcz\n\tIdentityFile ~/.ssh/%l_id_dsa");
|
||||
final HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertIdentity(
|
||||
new File(new File(home, ".ssh"), localhost + "_id_dsa"),
|
||||
h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPubKeyAcceptedAlgorithms() throws Exception {
|
||||
config("Host=orcz\n\tPubkeyAcceptedAlgorithms ^ssh-rsa");
|
||||
HostConfig h = lookup("orcz");
|
||||
assertEquals("^ssh-rsa",
|
||||
h.getValue(SshConstants.PUBKEY_ACCEPTED_ALGORITHMS));
|
||||
assertEquals("^ssh-rsa", h.getValue("PubkeyAcceptedKeyTypes"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPubKeyAcceptedKeyTypes() throws Exception {
|
||||
config("Host=orcz\n\tPubkeyAcceptedKeyTypes ^ssh-rsa");
|
||||
HostConfig h = lookup("orcz");
|
||||
assertEquals("^ssh-rsa",
|
||||
h.getValue(SshConstants.PUBKEY_ACCEPTED_ALGORITHMS));
|
||||
assertEquals("^ssh-rsa", h.getValue("PubkeyAcceptedKeyTypes"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEolComments() throws Exception {
|
||||
config("#Comment\nHost=orcz #Comment\n\tPubkeyAcceptedAlgorithms ^ssh-rsa # Comment\n#Comment");
|
||||
HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertEquals("^ssh-rsa",
|
||||
h.getValue(SshConstants.PUBKEY_ACCEPTED_ALGORITHMS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnVarSubstitution() throws Exception {
|
||||
config("Host orcz\nIdentityFile /tmp/${TST_VAR}\n"
|
||||
+ "CertificateFile /tmp/${}/foo\nUser ${TST_VAR}\nIdentityAgent /tmp/${TST_VAR/bar");
|
||||
HostConfig h = lookup("orcz");
|
||||
assertNotNull(h);
|
||||
assertEquals("/tmp/TEST",
|
||||
h.getValue(SshConstants.IDENTITY_FILE));
|
||||
// No variable name
|
||||
assertEquals("/tmp/${}/foo", h.getValue(SshConstants.CERTIFICATE_FILE));
|
||||
// User doesn't get env var substitution:
|
||||
assertUser("${TST_VAR}", h);
|
||||
// Unterminated:
|
||||
assertEquals("/tmp/${TST_VAR/bar",
|
||||
h.getValue(SshConstants.IDENTITY_AGENT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeMatch() throws Exception {
|
||||
config("Host foo.bar !foobar.baz *.baz\n" + "Port 29418\n");
|
||||
HostConfig h = lookup("foo.bar");
|
||||
assertNotNull(h);
|
||||
assertPort(29418, h);
|
||||
h = lookup("foobar.baz");
|
||||
assertNotNull(h);
|
||||
assertPort(22, h);
|
||||
h = lookup("foo.baz");
|
||||
assertNotNull(h);
|
||||
assertPort(29418, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeMatch2() throws Exception {
|
||||
// Negative match after the positive match.
|
||||
config("Host foo.bar *.baz !foobar.baz\n" + "Port 29418\n");
|
||||
HostConfig h = lookup("foo.bar");
|
||||
assertNotNull(h);
|
||||
assertPort(29418, h);
|
||||
h = lookup("foobar.baz");
|
||||
assertNotNull(h);
|
||||
assertPort(22, h);
|
||||
h = lookup("foo.baz");
|
||||
assertNotNull(h);
|
||||
assertPort(29418, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoMatch() throws Exception {
|
||||
config("Host !host1 !host2\n" + "Port 29418\n");
|
||||
HostConfig h = lookup("host1");
|
||||
assertNotNull(h);
|
||||
assertPort(22, h);
|
||||
h = lookup("host2");
|
||||
assertNotNull(h);
|
||||
assertPort(22, h);
|
||||
h = lookup("host3");
|
||||
assertNotNull(h);
|
||||
assertPort(22, h);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleMatch() throws Exception {
|
||||
config("Host foo.bar\nPort 29418\nIdentityFile /foo\n\n"
|
||||
+ "Host *.bar\nPort 22\nIdentityFile /bar\n"
|
||||
+ "Host foo.bar\nPort 47\nIdentityFile /baz\n");
|
||||
HostConfig h = lookup("foo.bar");
|
||||
assertNotNull(h);
|
||||
assertPort(29418, h);
|
||||
assertArrayEquals(new Object[] { "/foo", "/bar", "/baz" },
|
||||
h.getValues(SshConstants.IDENTITY_FILE).toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhitespace() throws Exception {
|
||||
config("Host foo \tbar baz\nPort 29418\n");
|
||||
HostConfig h = lookup("foo");
|
||||
assertNotNull(h);
|
||||
assertPort(29418, h);
|
||||
h = lookup("bar");
|
||||
assertNotNull(h);
|
||||
assertPort(29418, h);
|
||||
h = lookup("baz");
|
||||
assertNotNull(h);
|
||||
assertPort(29418, h);
|
||||
h = lookup("\tbar");
|
||||
assertNotNull(h);
|
||||
assertPort(22, h);
|
||||
}
|
||||
}
|
|
@ -111,7 +111,8 @@ Export-Package: org.eclipse.jgit.annotations;version="6.0.0",
|
|||
org.eclipse.jgit.test",
|
||||
org.eclipse.jgit.internal.transport.ssh;version="6.0.0";
|
||||
x-friends:="org.eclipse.jgit.ssh.apache,
|
||||
org.eclipse.jgit.ssh.jsch",
|
||||
org.eclipse.jgit.ssh.jsch,
|
||||
org.eclipse.jgit.test",
|
||||
org.eclipse.jgit.lib;version="6.0.0";
|
||||
uses:="org.eclipse.jgit.transport,
|
||||
org.eclipse.jgit.util.sha1,
|
||||
|
|
|
@ -151,6 +151,18 @@ public OpenSshConfigFile(@NonNull File home, @NonNull File config,
|
|||
@NonNull
|
||||
public HostEntry lookup(@NonNull String hostName, int port,
|
||||
String userName) {
|
||||
return lookup(hostName, port, userName, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public HostEntry lookupDefault(@NonNull String hostName, int port,
|
||||
String userName) {
|
||||
return lookup(hostName, port, userName, true);
|
||||
}
|
||||
|
||||
private HostEntry lookup(@NonNull String hostName, int port,
|
||||
String userName, boolean fillDefaults) {
|
||||
final State cache = refresh();
|
||||
String cacheKey = toCacheKey(hostName, port, userName);
|
||||
HostEntry h = cache.hosts.get(cacheKey);
|
||||
|
@ -169,7 +181,8 @@ public HostEntry lookup(@NonNull String hostName, int port,
|
|||
}
|
||||
});
|
||||
}
|
||||
fullConfig.substitute(hostName, port, userName, localUserName, home);
|
||||
fullConfig.substitute(hostName, port, userName, localUserName, home,
|
||||
fillDefaults);
|
||||
cache.hosts.put(cacheKey, fullConfig);
|
||||
return fullConfig;
|
||||
}
|
||||
|
@ -725,12 +738,12 @@ private List<String> replaceTilde(List<String> values, File home) {
|
|||
}
|
||||
|
||||
void substitute(String originalHostName, int port, String userName,
|
||||
String localUserName, File home) {
|
||||
int p = port >= 0 ? port : positive(getValue(SshConstants.PORT));
|
||||
if (p < 0) {
|
||||
String localUserName, File home, boolean fillDefaults) {
|
||||
int p = port > 0 ? port : positive(getValue(SshConstants.PORT));
|
||||
if (p <= 0) {
|
||||
p = SshConstants.SSH_DEFAULT_PORT;
|
||||
}
|
||||
String u = userName != null && !userName.isEmpty() ? userName
|
||||
String u = !StringUtils.isEmptyOrNull(userName) ? userName
|
||||
: getValue(SshConstants.USER);
|
||||
if (u == null || u.isEmpty()) {
|
||||
u = localUserName;
|
||||
|
@ -747,6 +760,8 @@ void substitute(String originalHostName, int port, String userName,
|
|||
options.put(SshConstants.HOST_NAME, hostName);
|
||||
r.update('h', hostName);
|
||||
}
|
||||
} else if (fillDefaults) {
|
||||
setValue(SshConstants.HOST_NAME, originalHostName);
|
||||
}
|
||||
if (multiOptions != null) {
|
||||
List<String> values = multiOptions
|
||||
|
@ -803,6 +818,19 @@ void substitute(String originalHostName, int port, String userName,
|
|||
}
|
||||
// Match is not implemented and would need to be done elsewhere
|
||||
// anyway.
|
||||
if (fillDefaults) {
|
||||
String s = options.get(SshConstants.USER);
|
||||
if (StringUtils.isEmptyOrNull(s)) {
|
||||
options.put(SshConstants.USER, u);
|
||||
}
|
||||
if (positive(options.get(SshConstants.PORT)) <= 0) {
|
||||
options.put(SshConstants.PORT, Integer.toString(p));
|
||||
}
|
||||
if (positive(
|
||||
options.get(SshConstants.CONNECTION_ATTEMPTS)) <= 0) {
|
||||
options.put(SshConstants.CONNECTION_ATTEMPTS, "1"); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -36,6 +36,48 @@ public interface SshConfigStore {
|
|||
@NonNull
|
||||
HostConfig lookup(@NonNull String hostName, int port, String userName);
|
||||
|
||||
/**
|
||||
* Locate the configuration for a specific host request and if the
|
||||
* configuration has no values for {@link SshConstants#HOST_NAME},
|
||||
* {@link SshConstants#PORT}, {@link SshConstants#USER}, or
|
||||
* {@link SshConstants#CONNECTION_ATTEMPTS}, fill those values with defaults
|
||||
* from the arguments:
|
||||
* <table>
|
||||
* <tr>
|
||||
* <th>ssh config key</th>
|
||||
* <th>value from argument</th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code HostName}</td>
|
||||
* <td>{@code hostName}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code Port}</td>
|
||||
* <td>{@code port > 0 ? port : 22}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code User}</td>
|
||||
* <td>{@code userName}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code ConnectionAttempts}</td>
|
||||
* <td>{@code 1}</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
* @param hostName
|
||||
* host name to look up
|
||||
* @param port
|
||||
* port number; <= 0 if none
|
||||
* @param userName
|
||||
* the user name, may be {@code null} or empty if none given
|
||||
* @return the configuration for the requested name.
|
||||
* @since 6.0
|
||||
*/
|
||||
@NonNull
|
||||
HostConfig lookupDefault(@NonNull String hostName, int port,
|
||||
String userName);
|
||||
|
||||
/**
|
||||
* A host entry from the ssh config. Any merging of global values and of
|
||||
* several matching host entries, %-substitutions, and ~ replacement have
|
||||
|
|
Loading…
Reference in New Issue