diff options
Diffstat (limited to 'org.eclipse.jgit.junit')
-rw-r--r-- | org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/ssh/SshTestGitServer.java | 45 |
1 files changed, 38 insertions, 7 deletions
diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/ssh/SshTestGitServer.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/ssh/SshTestGitServer.java index 675a11589d..8d3207c43e 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/ssh/SshTestGitServer.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/ssh/SshTestGitServer.java @@ -53,8 +53,9 @@ import java.util.Collections; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import org.apache.sshd.common.config.keys.IdentityUtils; +import org.apache.sshd.common.config.keys.AuthorizedKeyEntry; import org.apache.sshd.common.config.keys.KeyUtils; +import org.apache.sshd.common.config.keys.PublicKeyEntryResolver; import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory; import org.apache.sshd.common.keyprovider.KeyPairProvider; import org.apache.sshd.common.session.Session; @@ -65,13 +66,15 @@ import org.apache.sshd.server.shell.UnknownCommand; import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory; import org.eclipse.jgit.annotations.NonNull; import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.ReceivePack; +import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.UploadPack; /** * A simple ssh/sftp git <em>test</em> server based on Apache MINA sshd. * <p> * Supports only a single repository. Authenticates only the given test user - * against his given test public key. ssh is limited to fetching (upload-pack). + * against his given test public key. Supports fetch and push. * </p> * * @since 5.2 @@ -112,9 +115,7 @@ public class SshTestGitServer { @NonNull Repository repository, @NonNull byte[] hostKey) throws IOException, GeneralSecurityException { this.testUser = testUser; - this.testKey = IdentityUtils - .loadIdentities(Collections.singletonMap("A", testKey), null) - .get("A").getPublic(); + setTestUserPublicKey(testKey); this.repository = repository; server = SshServer.setUpDefaultServer(); // Set host key @@ -156,9 +157,10 @@ public class SshTestGitServer { .compareKeys(SshTestGitServer.this.testKey, publicKey); }); server.setCommandFactory(command -> { - if (command.startsWith("git-upload-pack") - || command.startsWith("git upload-pack")) { + if (command.startsWith(RemoteConfig.DEFAULT_UPLOAD_PACK)) { return new GitUploadPackCommand(command, executorService); + } else if (command.startsWith(RemoteConfig.DEFAULT_RECEIVE_PACK)) { + return new GitReceivePackCommand(command, executorService); } return new UnknownCommand(command); }); @@ -186,6 +188,12 @@ public class SshTestGitServer { server.stop(true); } + public void setTestUserPublicKey(Path key) + throws IOException, GeneralSecurityException { + this.testKey = AuthorizedKeyEntry.readAuthorizedKeys(key).get(0) + .resolvePublicKey(PublicKeyEntryResolver.IGNORING); + } + private class GitUploadPackCommand extends AbstractCommandSupport { protected GitUploadPackCommand(String command, @@ -214,4 +222,27 @@ public class SshTestGitServer { } } + + private class GitReceivePackCommand extends AbstractCommandSupport { + + protected GitReceivePackCommand(String command, + ExecutorService executorService) { + super(command, executorService, false); + } + + @Override + public void run() { + try { + new ReceivePack(repository).receive(getInputStream(), + getOutputStream(), getErrorStream()); + onExit(0); + } catch (IOException e) { + log.warn( + MessageFormat.format("Could not run {0}", getCommand()), + e); + onExit(-1, e.toString()); + } + } + + } } |