summaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authorDavid Ostrovsky <david@ostrovsky.org>2014-03-16 22:55:30 +0100
committerJames Moger <james.moger@gitblit.com>2014-04-10 18:58:08 -0400
commit5d58a05a9843ec90d06ca42061ff638418f73687 (patch)
tree1d6e8b7cdd2d5f24659cb1f0766719a3ba62a79a /src/test
parent75ebd391b88884581b1139c87c98bb687941a8fe (diff)
downloadgitblit-5d58a05a9843ec90d06ca42061ff638418f73687.tar.gz
gitblit-5d58a05a9843ec90d06ca42061ff638418f73687.zip
Add SSH daemon test
Diffstat (limited to 'src/test')
-rw-r--r--src/test/config/test-gitblit.properties2
-rw-r--r--src/test/java/com/gitblit/tests/BogusPublicKeyAuthenticator.java39
-rw-r--r--src/test/java/com/gitblit/tests/GitBlitSuite.java14
-rw-r--r--src/test/java/com/gitblit/tests/SshDaemonTest.java90
-rw-r--r--src/test/java/com/gitblit/tests/SshUtils.java74
5 files changed, 218 insertions, 1 deletions
diff --git a/src/test/config/test-gitblit.properties b/src/test/config/test-gitblit.properties
index e636469e..7d8e9a79 100644
--- a/src/test/config/test-gitblit.properties
+++ b/src/test/config/test-gitblit.properties
@@ -7,6 +7,8 @@ git.repositoriesFolder = ${baseFolder}/git
git.searchRepositoriesSubfolders = true
git.enableGitServlet = true
git.daemonPort = 8300
+git.sshPort = 29418
+git.sshPublicKeyAuthenticator = com.gitblit.tests.BogusPublicKeyAuthenticator
groovy.scriptsFolder = src/main/distrib/data/groovy
groovy.preReceiveScripts = blockpush
groovy.postReceiveScripts = sendmail
diff --git a/src/test/java/com/gitblit/tests/BogusPublicKeyAuthenticator.java b/src/test/java/com/gitblit/tests/BogusPublicKeyAuthenticator.java
new file mode 100644
index 00000000..80be1a01
--- /dev/null
+++ b/src/test/java/com/gitblit/tests/BogusPublicKeyAuthenticator.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gitblit.tests;
+
+import java.security.PublicKey;
+
+import org.apache.sshd.server.session.ServerSession;
+
+import com.gitblit.manager.IAuthenticationManager;
+import com.gitblit.transport.ssh.CachingPublicKeyAuthenticator;
+import com.gitblit.transport.ssh.IKeyManager;
+
+public class BogusPublicKeyAuthenticator extends CachingPublicKeyAuthenticator {
+
+ public BogusPublicKeyAuthenticator(IKeyManager keyManager,
+ IAuthenticationManager authManager) {
+ super(keyManager, authManager);
+ }
+
+ @Override
+ protected boolean doAuthenticate(String username, PublicKey suppliedKey,
+ ServerSession session) {
+ // TODO(davido): put authenticated user in session
+ return true;
+ }
+}
diff --git a/src/test/java/com/gitblit/tests/GitBlitSuite.java b/src/test/java/com/gitblit/tests/GitBlitSuite.java
index c015c847..17d609e7 100644
--- a/src/test/java/com/gitblit/tests/GitBlitSuite.java
+++ b/src/test/java/com/gitblit/tests/GitBlitSuite.java
@@ -61,7 +61,7 @@ import com.gitblit.utils.JGitUtils;
MarkdownUtilsTest.class, JGitUtilsTest.class, SyndicationUtilsTest.class,
DiffUtilsTest.class, MetricUtilsTest.class, X509UtilsTest.class,
GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class, GitDaemonTest.class,
- GroovyScriptTest.class, LuceneExecutorTest.class, RepositoryModelTest.class,
+ GroovyScriptTest.class, LuceneExecutorTest.class, RepositoryModelTest.class, SshDaemonTest.class,
FanoutServiceTest.class, Issue0259Test.class, Issue0271Test.class, HtpasswdAuthenticationTest.class,
ModelUtilsTest.class, JnaUtilsTest.class, LdapSyncServiceTest.class, FileTicketServiceTest.class,
BranchTicketServiceTest.class, RedisTicketServiceTest.class, AuthenticationManagerTest.class })
@@ -78,6 +78,16 @@ public class GitBlitSuite {
static int port = 8280;
static int gitPort = 8300;
static int shutdownPort = 8281;
+ static int sshPort = 29418;
+
+// Overriding of keys doesn't seem to work
+// static {
+// try {
+// sshPort = SshUtils.getFreePort();
+// } catch (Exception e) {
+// e.printStackTrace();
+// }
+// }
public static String url = "http://localhost:" + port;
public static String gitServletUrl = "http://localhost:" + port + "/git";
@@ -140,6 +150,8 @@ public class GitBlitSuite {
"\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
GitBlitSuite.USERSCONF.getAbsolutePath(), "--settings", GitBlitSuite.SETTINGS.getAbsolutePath(),
"--baseFolder", "data");
+ // doesn't work
+ //, "--sshPort", "" + sshPort);
}
});
diff --git a/src/test/java/com/gitblit/tests/SshDaemonTest.java b/src/test/java/com/gitblit/tests/SshDaemonTest.java
new file mode 100644
index 00000000..5294f691
--- /dev/null
+++ b/src/test/java/com/gitblit/tests/SshDaemonTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gitblit.tests;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.security.KeyPair;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.sshd.ClientChannel;
+import org.apache.sshd.ClientSession;
+import org.apache.sshd.SshClient;
+import org.apache.sshd.common.KeyPairProvider;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.gitblit.Constants;
+
+public class SshDaemonTest extends GitblitUnitTest {
+
+ private static final AtomicBoolean started = new AtomicBoolean(false);
+ private static KeyPair pair;
+
+ @BeforeClass
+ public static void startGitblit() throws Exception {
+ started.set(GitBlitSuite.startGitblit());
+ pair = SshUtils.createTestHostKeyProvider().loadKey(KeyPairProvider.SSH_RSA);
+ }
+
+ @AfterClass
+ public static void stopGitblit() throws Exception {
+ if (started.get()) {
+ GitBlitSuite.stopGitblit();
+ }
+ }
+
+ @Test
+ public void testPublicKeyAuthentication() throws Exception {
+ SshClient client = SshClient.setUpDefaultClient();
+ client.start();
+ ClientSession session = client.connect("localhost", GitBlitSuite.sshPort).await().getSession();
+ pair.getPublic().getEncoded();
+ assertTrue(session.authPublicKey("admin", pair).await().isSuccess());
+ }
+
+ @Test
+ public void testVersionCommand() throws Exception {
+ SshClient client = SshClient.setUpDefaultClient();
+ client.start();
+ ClientSession session = client.connect("localhost", GitBlitSuite.sshPort).await().getSession();
+ pair.getPublic().getEncoded();
+ assertTrue(session.authPublicKey("admin", pair).await().isSuccess());
+
+ ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_EXEC, "gitblit version");
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ Writer w = new OutputStreamWriter(baos);
+ w.close();
+ channel.setIn(new ByteArrayInputStream(baos.toByteArray()));
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ByteArrayOutputStream err = new ByteArrayOutputStream();
+ channel.setOut(out);
+ channel.setErr(err);
+ channel.open();
+
+ channel.waitFor(ClientChannel.CLOSED, 0);
+
+ String result = out.toString().trim();
+ channel.close(false);
+ client.stop();
+
+ assertEquals(Constants.getGitBlitVersion(), result);
+ }
+}
diff --git a/src/test/java/com/gitblit/tests/SshUtils.java b/src/test/java/com/gitblit/tests/SshUtils.java
new file mode 100644
index 00000000..9760f755
--- /dev/null
+++ b/src/test/java/com/gitblit/tests/SshUtils.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.gitblit.tests;
+
+import java.io.File;
+import java.net.ServerSocket;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import org.apache.sshd.common.KeyPairProvider;
+import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
+import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
+
+public class SshUtils {
+
+ public static KeyPairProvider createTestHostKeyProvider() {
+ return new SimpleGeneratorHostKeyProvider("target/hostkey.rsa", "RSA");
+ }
+
+ public static FileKeyPairProvider createTestKeyPairProvider(String resource) {
+ return new FileKeyPairProvider(new String[] { getFile(resource) });
+ }
+
+ public static int getFreePort() throws Exception {
+ ServerSocket s = new ServerSocket(0);
+ try {
+ return s.getLocalPort();
+ } finally {
+ s.close();
+ }
+ }
+
+ private static String getFile(String resource) {
+ URL url = SshUtils.class.getClassLoader().getResource(resource);
+ File f;
+ try {
+ f = new File(url.toURI());
+ } catch(URISyntaxException e) {
+ f = new File(url.getPath());
+ }
+ return f.toString();
+ }
+
+ public static void deleteRecursive(File file) {
+ if (file != null) {
+ if (file.isDirectory()) {
+ File[] children = file.listFiles();
+ if (children != null) {
+ for (File child : children) {
+ deleteRecursive(child);
+ }
+ }
+ }
+ file.delete();
+ }
+ }
+
+}