James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
commit | author | age
5d58a0 1 /*
DO 2  * Copyright 2014 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.tests;
17
521cb6 18 import java.io.File;
JM 19 import java.text.MessageFormat;
20 import java.util.List;
5d58a0 21
d41034 22 import org.apache.sshd.client.SshClient;
JM 23 import org.apache.sshd.client.session.ClientSession;
521cb6 24 import org.eclipse.jgit.api.CloneCommand;
JM 25 import org.eclipse.jgit.api.Git;
26 import org.eclipse.jgit.revwalk.RevCommit;
27 import org.eclipse.jgit.transport.SshSessionFactory;
28 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
29 import org.eclipse.jgit.util.FileUtils;
5d58a0 30 import org.junit.Test;
DO 31
32 import com.gitblit.Constants;
521cb6 33 import com.gitblit.Constants.AccessRestrictionType;
JM 34 import com.gitblit.Constants.AuthorizationControl;
35 import com.gitblit.models.RepositoryModel;
36 import com.gitblit.utils.JGitUtils;
5d58a0 37
521cb6 38 public class SshDaemonTest extends SshUnitTest {
5d58a0 39
521cb6 40     static File ticgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit");
5d58a0 41
521cb6 42     String url = GitBlitSuite.sshDaemonUrl;
245836 43
5d58a0 44     @Test
DO 45     public void testPublicKeyAuthentication() throws Exception {
521cb6 46         SshClient client = getClient();
JM 47         ClientSession session = client.connect(username, "localhost", GitBlitSuite.sshPort).await().getSession();
48         session.addPublicKeyIdentity(rwKeyPair);
49         assertTrue(session.auth().await().isSuccess());
5d58a0 50     }
DO 51
52     @Test
53     public void testVersionCommand() throws Exception {
521cb6 54         String result = testSshCommand("version");
JM 55         assertEquals(Constants.getGitBlitVersion(), result);
56     }
5d58a0 57
521cb6 58     @Test
JM 59     public void testCloneCommand() throws Exception {
60         if (ticgitFolder.exists()) {
61             GitBlitSuite.close(ticgitFolder);
62             FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE);
63         }
5d58a0 64
521cb6 65         // set clone restriction
JM 66         RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
67         model.accessRestriction = AccessRestrictionType.CLONE;
68         model.authorizationControl = AuthorizationControl.NAMED;
69         repositories().updateRepositoryModel(model.name, model, false);
5d58a0 70
521cb6 71         JschConfigTestSessionFactory sessionFactory = new JschConfigTestSessionFactory(roKeyPair);
JM 72         SshSessionFactory.setInstance(sessionFactory);
5d58a0 73
521cb6 74         CloneCommand clone = Git.cloneRepository();
JM 75         clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
76         clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
77         clone.setDirectory(ticgitFolder);
78         clone.setBare(false);
79         clone.setCloneAllBranches(true);
80         Git git = clone.call();
81         List<RevCommit> commits = JGitUtils.getRevLog(git.getRepository(), 10);
82         GitBlitSuite.close(git);
83         assertEquals(10, commits.size());
5d58a0 84
521cb6 85         // restore anonymous repository access
JM 86         model.accessRestriction = AccessRestrictionType.NONE;
87         model.authorizationControl = AuthorizationControl.NAMED;
88         repositories().updateRepositoryModel(model.name, model, false);
89     }
5d58a0 90 }