James Moger
2012-06-15 01774948d84794d1d9c216f9a6859d7f150815d5
commit | author | age
375caa 1 package com.gitblit.tests;
JM 2
7e8873 3 import static org.junit.Assert.assertFalse;
JM 4 import static org.junit.Assert.assertTrue;
5
375caa 6 import java.io.BufferedWriter;
JM 7 import java.io.File;
8 import java.io.FileOutputStream;
9 import java.io.OutputStreamWriter;
10 import java.text.MessageFormat;
11 import java.util.Date;
7e8873 12 import java.util.concurrent.atomic.AtomicBoolean;
375caa 13
JM 14 import org.eclipse.jgit.api.CloneCommand;
15 import org.eclipse.jgit.api.Git;
e5cb55 16 import org.eclipse.jgit.lib.Constants;
d40adc 17 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
375caa 18 import org.eclipse.jgit.util.FileUtils;
29692a 19 import org.junit.AfterClass;
JM 20 import org.junit.BeforeClass;
21 import org.junit.Test;
375caa 22
7e8873 23 import com.gitblit.Constants.AccessRestrictionType;
JM 24 import com.gitblit.GitBlit;
25 import com.gitblit.models.RepositoryModel;
375caa 26
29692a 27 public class GitServletTest {
375caa 28
ee0b1f 29     static File ticgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit");
cb285c 30     
ee0b1f 31     static File ticgit2Folder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit2");
JM 32
33     static File jgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/jgit");
b74031 34     
JM 35     static File jgit2Folder = new File(GitBlitSuite.REPOSITORIES, "working/jgit2");
375caa 36
7e8873 37     String url = GitBlitSuite.url;
JM 38     String account = GitBlitSuite.account;
39     String password = GitBlitSuite.password;
375caa 40
7e8873 41     private static final AtomicBoolean started = new AtomicBoolean(false);
375caa 42
29692a 43     @BeforeClass
JM 44     public static void startGitblit() throws Exception {
ee0b1f 45         deleteWorkingFolders();
7e8873 46         started.set(GitBlitSuite.startGitblit());
375caa 47     }
JM 48
29692a 49     @AfterClass
JM 50     public static void stopGitblit() throws Exception {
7e8873 51         if (started.get()) {
JM 52             GitBlitSuite.stopGitblit();
53         }
ee0b1f 54         deleteWorkingFolders();
JM 55     }
56     
57     private static void deleteWorkingFolders() throws Exception {
58         if (ticgitFolder.exists()) {
59             FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE);
60         }
61         if (ticgit2Folder.exists()) {
62             FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
63         }
64         if (jgitFolder.exists()) {
65             FileUtils.delete(jgitFolder, FileUtils.RECURSIVE);
b74031 66         }
JM 67         if (jgit2Folder.exists()) {
68             FileUtils.delete(jgit2Folder, FileUtils.RECURSIVE);
ee0b1f 69         }
375caa 70     }
JM 71
29692a 72     @Test
375caa 73     public void testClone() throws Exception {
JM 74         CloneCommand clone = Git.cloneRepository();
7e8873 75         clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
ee0b1f 76         clone.setDirectory(ticgitFolder);
375caa 77         clone.setBare(false);
JM 78         clone.setCloneAllBranches(true);
7e8873 79         clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
ee0b1f 80         close(clone.call());        
7e8873 81         assertTrue(true);
JM 82     }
83
84     @Test
85     public void testBogusLoginClone() throws Exception {
86         // restrict repository access
87         RepositoryModel model = GitBlit.self().getRepositoryModel("ticgit.git");
88         model.accessRestriction = AccessRestrictionType.CLONE;
89         GitBlit.self().updateRepositoryModel(model.name, model, false);
90
ee0b1f 91         // delete any existing working folder        
7e8873 92         boolean cloned = false;
JM 93         try {
94             CloneCommand clone = Git.cloneRepository();
95             clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
ee0b1f 96             clone.setDirectory(ticgit2Folder);
7e8873 97             clone.setBare(false);
JM 98             clone.setCloneAllBranches(true);
99             clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider("bogus", "bogus"));
ee0b1f 100             close(clone.call());
7e8873 101             cloned = true;
JM 102         } catch (Exception e) {
dcf575 103             // swallow the exception which we expect
7e8873 104         }
JM 105
106         // restore anonymous repository access
107         model.accessRestriction = AccessRestrictionType.NONE;
108         GitBlit.self().updateRepositoryModel(model.name, model, false);
109
110         assertFalse("Bogus login cloned a repository?!", cloned);
375caa 111     }
JM 112
29692a 113     @Test
cb285c 114     public void testAnonymousPush() throws Exception {
ee0b1f 115         Git git = Git.open(ticgitFolder);
JM 116         File file = new File(ticgitFolder, "TODO");
e5cb55 117         OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
ee0b1f 118         BufferedWriter w = new BufferedWriter(os);
e5cb55 119         w.write("// hellol中文 " + new Date().toString() + "\n");
ee0b1f 120         w.close();
JM 121         git.add().addFilepattern(file.getName()).call();
122         git.commit().setMessage("test commit").call();
375caa 123         git.push().setPushAll().call();
ee0b1f 124         close(git);
d40adc 125     }
ee0b1f 126
cb285c 127     @Test
JM 128     public void testSubfolderPush() throws Exception {
129         CloneCommand clone = Git.cloneRepository();
130         clone.setURI(MessageFormat.format("{0}/git/test/jgit.git", url));
131         clone.setDirectory(jgitFolder);
132         clone.setBare(false);
133         clone.setCloneAllBranches(true);
134         clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
ee0b1f 135         close(clone.call());
cb285c 136         assertTrue(true);
ee0b1f 137
cb285c 138         Git git = Git.open(jgitFolder);
JM 139         File file = new File(jgitFolder, "TODO");
e5cb55 140         OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
ee0b1f 141         BufferedWriter w = new BufferedWriter(os);
JM 142         w.write("// " + new Date().toString() + "\n");
143         w.close();
144         git.add().addFilepattern(file.getName()).call();
145         git.commit().setMessage("test commit").call();
cb285c 146         git.push().setPushAll().call();
ee0b1f 147         close(git);
JM 148     }
149     
b74031 150     @Test
JM 151     public void testPushToNonBareRepository() throws Exception {
152         CloneCommand clone = Git.cloneRepository();
153         clone.setURI(MessageFormat.format("{0}/git/working/jgit", url));
154         clone.setDirectory(jgit2Folder);
155         clone.setBare(false);
156         clone.setCloneAllBranches(true);
157         clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
158         close(clone.call());
159         assertTrue(true);
160
161         Git git = Git.open(jgit2Folder);
162         File file = new File(jgit2Folder, "NONBARE");
e5cb55 163         OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
b74031 164         BufferedWriter w = new BufferedWriter(os);
JM 165         w.write("// " + new Date().toString() + "\n");
166         w.close();
167         git.add().addFilepattern(file.getName()).call();
168         git.commit().setMessage("test commit followed by push to non-bare repository").call();
169         try {
170             git.push().setPushAll().call();
171             assertTrue(false);
172         } catch (Exception e) {
173             assertTrue(e.getCause().getMessage().contains("git-receive-pack not permitted"));
174         }
175         close(git);
176     }
177     
ee0b1f 178     private void close(Git git) {
JM 179         // really close the repository
180         // decrement the use counter to 0
181         for (int i = 0; i < 2; i++) {
182             git.getRepository().close();
183         }
cb285c 184     }
375caa 185 }