summaryrefslogtreecommitdiffstats
path: root/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/com/gitblit/tests/GitBlitSuite.java18
-rw-r--r--src/test/java/com/gitblit/tests/SshDaemonTest.java134
-rw-r--r--src/test/java/com/gitblit/tests/SshKeysDispatcherTest.java115
-rw-r--r--src/test/java/com/gitblit/tests/SshUnitTest.java141
-rw-r--r--src/test/java/com/gitblit/tests/SshUtils.java74
5 files changed, 322 insertions, 160 deletions
diff --git a/src/test/java/com/gitblit/tests/GitBlitSuite.java b/src/test/java/com/gitblit/tests/GitBlitSuite.java
index b8d3b181..5a7dcea1 100644
--- a/src/test/java/com/gitblit/tests/GitBlitSuite.java
+++ b/src/test/java/com/gitblit/tests/GitBlitSuite.java
@@ -64,7 +64,8 @@ import com.gitblit.utils.JGitUtils;
SshDaemonTest.class, GroovyScriptTest.class, LuceneExecutorTest.class, RepositoryModelTest.class,
FanoutServiceTest.class, Issue0259Test.class, Issue0271Test.class, HtpasswdAuthenticationTest.class,
ModelUtilsTest.class, JnaUtilsTest.class, LdapSyncServiceTest.class, FileTicketServiceTest.class,
- BranchTicketServiceTest.class, RedisTicketServiceTest.class, AuthenticationManagerTest.class })
+ BranchTicketServiceTest.class, RedisTicketServiceTest.class, AuthenticationManagerTest.class,
+ SshKeysDispatcherTest.class })
public class GitBlitSuite {
public static final File BASEFOLDER = new File("data");
@@ -137,11 +138,16 @@ public class GitBlitSuite {
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
- GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
- "" + shutdownPort, "--gitPort", "" + gitPort, "--repositoriesFolder",
- "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
- GitBlitSuite.USERSCONF.getAbsolutePath(), "--settings", GitBlitSuite.SETTINGS.getAbsolutePath(),
- "--baseFolder", "data", "--sshPort", "" + sshPort);
+ GitBlitServer.main(
+ "--httpPort", "" + port,
+ "--httpsPort", "0",
+ "--shutdownPort", "" + shutdownPort,
+ "--gitPort", "" + gitPort,
+ "--sshPort", "" + sshPort,
+ "--repositoriesFolder", GitBlitSuite.REPOSITORIES.getAbsolutePath(),
+ "--userService", GitBlitSuite.USERSCONF.getAbsolutePath(),
+ "--settings", GitBlitSuite.SETTINGS.getAbsolutePath(),
+ "--baseFolder", "data");
}
});
diff --git a/src/test/java/com/gitblit/tests/SshDaemonTest.java b/src/test/java/com/gitblit/tests/SshDaemonTest.java
index 620190ef..dcaeaff8 100644
--- a/src/test/java/com/gitblit/tests/SshDaemonTest.java
+++ b/src/test/java/com/gitblit/tests/SshDaemonTest.java
@@ -15,102 +15,76 @@
*/
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 java.io.File;
+import java.text.MessageFormat;
+import java.util.List;
-import org.apache.sshd.ClientChannel;
import org.apache.sshd.ClientSession;
import org.apache.sshd.SshClient;
-import org.apache.sshd.common.KeyPairProvider;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
+import org.eclipse.jgit.api.CloneCommand;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.transport.SshSessionFactory;
+import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
+import org.eclipse.jgit.util.FileUtils;
import org.junit.Test;
import com.gitblit.Constants;
-import com.gitblit.transport.ssh.IPublicKeyManager;
-import com.gitblit.transport.ssh.MemoryKeyManager;
-import com.gitblit.transport.ssh.SshKey;
+import com.gitblit.Constants.AccessRestrictionType;
+import com.gitblit.Constants.AuthorizationControl;
+import com.gitblit.models.RepositoryModel;
+import com.gitblit.utils.JGitUtils;
-public class SshDaemonTest extends GitblitUnitTest {
+public class SshDaemonTest extends SshUnitTest {
- private static final AtomicBoolean started = new AtomicBoolean(false);
- private static KeyPair pair;
+ static File ticgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit");
- @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();
- }
- }
-
- protected MemoryKeyManager getKeyManager() {
- IPublicKeyManager mgr = gitblit().getPublicKeyManager();
- if (mgr instanceof MemoryKeyManager) {
- return (MemoryKeyManager) gitblit().getPublicKeyManager();
- } else {
- throw new RuntimeException("unexpected key manager type " + mgr.getClass().getName());
- }
- }
-
- @Before
- public void prepare() {
- MemoryKeyManager keyMgr = getKeyManager();
- keyMgr.addKey("admin", new SshKey(pair.getPublic()));
- }
-
- @After
- public void tearDown() {
- MemoryKeyManager keyMgr = getKeyManager();
- keyMgr.removeAllKeys("admin");
- }
+ String url = GitBlitSuite.sshDaemonUrl;
@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());
+ SshClient client = getClient();
+ ClientSession session = client.connect(username, "localhost", GitBlitSuite.sshPort).await().getSession();
+ session.addPublicKeyIdentity(rwKeyPair);
+ assertTrue(session.auth().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, "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 = testSshCommand("version");
+ assertEquals(Constants.getGitBlitVersion(), result);
+ }
- String result = out.toString().trim();
- channel.close(false);
- client.stop();
+ @Test
+ public void testCloneCommand() throws Exception {
+ if (ticgitFolder.exists()) {
+ GitBlitSuite.close(ticgitFolder);
+ FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE);
+ }
- assertEquals(Constants.getGitBlitVersion(), result);
- }
+ // set clone restriction
+ RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
+ model.accessRestriction = AccessRestrictionType.CLONE;
+ model.authorizationControl = AuthorizationControl.NAMED;
+ repositories().updateRepositoryModel(model.name, model, false);
+
+ JschConfigTestSessionFactory sessionFactory = new JschConfigTestSessionFactory(roKeyPair);
+ SshSessionFactory.setInstance(sessionFactory);
+
+ CloneCommand clone = Git.cloneRepository();
+ clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
+ clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
+ clone.setDirectory(ticgitFolder);
+ clone.setBare(false);
+ clone.setCloneAllBranches(true);
+ Git git = clone.call();
+ List<RevCommit> commits = JGitUtils.getRevLog(git.getRepository(), 10);
+ GitBlitSuite.close(git);
+ assertEquals(10, commits.size());
+
+ // restore anonymous repository access
+ model.accessRestriction = AccessRestrictionType.NONE;
+ model.authorizationControl = AuthorizationControl.NAMED;
+ repositories().updateRepositoryModel(model.name, model, false);
+ }
}
diff --git a/src/test/java/com/gitblit/tests/SshKeysDispatcherTest.java b/src/test/java/com/gitblit/tests/SshKeysDispatcherTest.java
new file mode 100644
index 00000000..8ccdc5bf
--- /dev/null
+++ b/src/test/java/com/gitblit/tests/SshKeysDispatcherTest.java
@@ -0,0 +1,115 @@
+/*
+ * 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.KeyPair;
+import java.util.List;
+
+import org.junit.Test;
+import org.parboiled.common.StringUtils;
+
+import com.gitblit.Constants.AccessPermission;
+import com.gitblit.transport.ssh.SshKey;
+
+/**
+ * Tests the Keys Dispatcher and it's commands.
+ *
+ * @author James Moger
+ *
+ */
+public class SshKeysDispatcherTest extends SshUnitTest {
+
+ @Test
+ public void testKeysListCommand() throws Exception {
+ String result = testSshCommand("keys ls -L");
+ List<SshKey> keys = getKeyManager().getKeys(username);
+ assertEquals(String.format("There are %d keys!", keys.size()), 2, keys.size());
+ assertEquals(keys.get(0).getRawData() + "\n" + keys.get(1).getRawData(), result);
+ }
+
+ @Test
+ public void testKeysWhichCommand() throws Exception {
+ String result = testSshCommand("keys which -L");
+ List<SshKey> keys = getKeyManager().getKeys(username);
+ assertEquals(String.format("There are %d keys!", keys.size()), 2, keys.size());
+ assertEquals(keys.get(0).getRawData(), result);
+ }
+
+ @Test
+ public void testKeysRmCommand() throws Exception {
+ testSshCommand("keys rm 2");
+ String result = testSshCommand("keys ls -L");
+ List<SshKey> keys = getKeyManager().getKeys(username);
+ assertEquals(String.format("There are %d keys!", keys.size()), 1, keys.size());
+ assertEquals(keys.get(0).getRawData(), result);
+ }
+
+ @Test
+ public void testKeysRmAllByIndexCommand() throws Exception {
+ testSshCommand("keys rm 1 2");
+ List<SshKey> keys = getKeyManager().getKeys(username);
+ assertEquals(String.format("There are %d keys!", keys.size()), 0, keys.size());
+ try {
+ testSshCommand("keys ls -L");
+ assertTrue("Authentication worked without a public key?!", false);
+ } catch (AssertionError e) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testKeysRmAllCommand() throws Exception {
+ testSshCommand("keys rm ALL");
+ List<SshKey> keys = getKeyManager().getKeys(username);
+ assertEquals(String.format("There are %d keys!", keys.size()), 0, keys.size());
+ try {
+ testSshCommand("keys ls -L");
+ assertTrue("Authentication worked without a public key?!", false);
+ } catch (AssertionError e) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testKeysAddCommand() throws Exception {
+ KeyPair kp = generator.generateKeyPair();
+ SshKey key = new SshKey(kp.getPublic());
+ testSshCommand("keys add --permission R", key.getRawData());
+ List<SshKey> keys = getKeyManager().getKeys(username);
+ assertEquals(String.format("There are %d keys!", keys.size()), 3, keys.size());
+ assertEquals(AccessPermission.CLONE, keys.get(2).getPermission());
+
+ String result = testSshCommand("keys ls -L");
+ StringBuilder sb = new StringBuilder();
+ for (SshKey sk : keys) {
+ sb.append(sk.getRawData());
+ sb.append('\n');
+ }
+ sb.setLength(sb.length() - 1);
+ assertEquals(sb.toString(), result);
+ }
+
+ @Test
+ public void testKeysCommentCommand() throws Exception {
+ List<SshKey> keys = getKeyManager().getKeys(username);
+ assertTrue(StringUtils.isEmpty(keys.get(0).getComment()));
+ String comment = "this is my comment";
+ testSshCommand(String.format("keys comment 1 %s", comment));
+
+ keys = getKeyManager().getKeys(username);
+ assertEquals(comment, keys.get(0).getComment());
+ }
+}
diff --git a/src/test/java/com/gitblit/tests/SshUnitTest.java b/src/test/java/com/gitblit/tests/SshUnitTest.java
new file mode 100644
index 00000000..43b51b74
--- /dev/null
+++ b/src/test/java/com/gitblit/tests/SshUnitTest.java
@@ -0,0 +1,141 @@
+/*
+ * 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.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.net.SocketAddress;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.PublicKey;
+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.client.ServerKeyVerifier;
+import org.apache.sshd.common.util.SecurityUtils;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+
+import com.gitblit.Constants.AccessPermission;
+import com.gitblit.transport.ssh.IPublicKeyManager;
+import com.gitblit.transport.ssh.MemoryKeyManager;
+import com.gitblit.transport.ssh.SshKey;
+
+/**
+ * Base class for SSH unit tests.
+ */
+public abstract class SshUnitTest extends GitblitUnitTest {
+
+ protected static final AtomicBoolean started = new AtomicBoolean(false);
+ protected static KeyPairGenerator generator;
+ protected KeyPair rwKeyPair;
+ protected KeyPair roKeyPair;
+ protected String username = "admin";
+ protected String password = "admin";
+
+ @BeforeClass
+ public static void startGitblit() throws Exception {
+ generator = SecurityUtils.getKeyPairGenerator("RSA");
+ started.set(GitBlitSuite.startGitblit());
+ }
+
+ @AfterClass
+ public static void stopGitblit() throws Exception {
+ if (started.get()) {
+ GitBlitSuite.stopGitblit();
+ }
+ }
+
+ protected MemoryKeyManager getKeyManager() {
+ IPublicKeyManager mgr = gitblit().getPublicKeyManager();
+ if (mgr instanceof MemoryKeyManager) {
+ return (MemoryKeyManager) gitblit().getPublicKeyManager();
+ } else {
+ throw new RuntimeException("unexpected key manager type " + mgr.getClass().getName());
+ }
+ }
+
+ @Before
+ public void prepare() {
+ rwKeyPair = generator.generateKeyPair();
+
+ MemoryKeyManager keyMgr = getKeyManager();
+ keyMgr.addKey(username, new SshKey(rwKeyPair.getPublic()));
+
+ roKeyPair = generator.generateKeyPair();
+ SshKey sshKey = new SshKey(roKeyPair.getPublic());
+ sshKey.setPermission(AccessPermission.CLONE);
+ keyMgr.addKey(username, sshKey);
+ }
+
+ @After
+ public void tearDown() {
+ MemoryKeyManager keyMgr = getKeyManager();
+ keyMgr.removeAllKeys(username);
+ }
+
+ protected SshClient getClient() {
+ SshClient client = SshClient.setUpDefaultClient();
+ client.setServerKeyVerifier(new ServerKeyVerifier() {
+ @Override
+ public boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) {
+ return true;
+ }
+ });
+ client.start();
+ return client;
+ }
+
+ protected String testSshCommand(String cmd) throws IOException, InterruptedException {
+ return testSshCommand(cmd, null);
+ }
+
+ protected String testSshCommand(String cmd, String stdin) throws IOException, InterruptedException {
+ SshClient client = getClient();
+ ClientSession session = client.connect(username, "localhost", GitBlitSuite.sshPort).await().getSession();
+ session.addPublicKeyIdentity(rwKeyPair);
+ assertTrue(session.auth().await().isSuccess());
+
+ ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_EXEC, cmd);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ if (stdin != null) {
+ Writer w = new OutputStreamWriter(baos);
+ w.write(stdin);
+ 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();
+ return result;
+ }
+}
diff --git a/src/test/java/com/gitblit/tests/SshUtils.java b/src/test/java/com/gitblit/tests/SshUtils.java
deleted file mode 100644
index 9760f755..00000000
--- a/src/test/java/com/gitblit/tests/SshUtils.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * 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();
- }
- }
-
-}