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;
|
6adf56
|
24 |
import com.gitblit.Constants.AuthorizationControl;
|
7e8873
|
25 |
import com.gitblit.GitBlit;
|
JM |
26 |
import com.gitblit.models.RepositoryModel;
|
6adf56
|
27 |
import com.gitblit.models.UserModel;
|
375caa
|
28 |
|
29692a
|
29 |
public class GitServletTest {
|
375caa
|
30 |
|
ee0b1f
|
31 |
static File ticgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit");
|
cb285c
|
32 |
|
ee0b1f
|
33 |
static File ticgit2Folder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit2");
|
JM |
34 |
|
|
35 |
static File jgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/jgit");
|
b74031
|
36 |
|
JM |
37 |
static File jgit2Folder = new File(GitBlitSuite.REPOSITORIES, "working/jgit2");
|
375caa
|
38 |
|
7e8873
|
39 |
String url = GitBlitSuite.url;
|
JM |
40 |
String account = GitBlitSuite.account;
|
|
41 |
String password = GitBlitSuite.password;
|
375caa
|
42 |
|
7e8873
|
43 |
private static final AtomicBoolean started = new AtomicBoolean(false);
|
375caa
|
44 |
|
29692a
|
45 |
@BeforeClass
|
JM |
46 |
public static void startGitblit() throws Exception {
|
7e8873
|
47 |
started.set(GitBlitSuite.startGitblit());
|
375caa
|
48 |
}
|
JM |
49 |
|
29692a
|
50 |
@AfterClass
|
JM |
51 |
public static void stopGitblit() throws Exception {
|
7e8873
|
52 |
if (started.get()) {
|
JM |
53 |
GitBlitSuite.stopGitblit();
|
f3ce6e
|
54 |
deleteWorkingFolders();
|
7e8873
|
55 |
}
|
ee0b1f
|
56 |
}
|
JM |
57 |
|
f3ce6e
|
58 |
public static void deleteWorkingFolders() throws Exception {
|
ee0b1f
|
59 |
if (ticgitFolder.exists()) {
|
JM |
60 |
FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE);
|
|
61 |
}
|
|
62 |
if (ticgit2Folder.exists()) {
|
|
63 |
FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
|
|
64 |
}
|
|
65 |
if (jgitFolder.exists()) {
|
|
66 |
FileUtils.delete(jgitFolder, FileUtils.RECURSIVE);
|
b74031
|
67 |
}
|
JM |
68 |
if (jgit2Folder.exists()) {
|
|
69 |
FileUtils.delete(jgit2Folder, FileUtils.RECURSIVE);
|
ee0b1f
|
70 |
}
|
375caa
|
71 |
}
|
JM |
72 |
|
29692a
|
73 |
@Test
|
375caa
|
74 |
public void testClone() throws Exception {
|
JM |
75 |
CloneCommand clone = Git.cloneRepository();
|
7e8873
|
76 |
clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
|
ee0b1f
|
77 |
clone.setDirectory(ticgitFolder);
|
375caa
|
78 |
clone.setBare(false);
|
JM |
79 |
clone.setCloneAllBranches(true);
|
7e8873
|
80 |
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
|
ee0b1f
|
81 |
close(clone.call());
|
7e8873
|
82 |
assertTrue(true);
|
JM |
83 |
}
|
|
84 |
|
|
85 |
@Test
|
|
86 |
public void testBogusLoginClone() throws Exception {
|
|
87 |
// restrict repository access
|
|
88 |
RepositoryModel model = GitBlit.self().getRepositoryModel("ticgit.git");
|
|
89 |
model.accessRestriction = AccessRestrictionType.CLONE;
|
|
90 |
GitBlit.self().updateRepositoryModel(model.name, model, false);
|
|
91 |
|
ee0b1f
|
92 |
// delete any existing working folder
|
7e8873
|
93 |
boolean cloned = false;
|
JM |
94 |
try {
|
|
95 |
CloneCommand clone = Git.cloneRepository();
|
|
96 |
clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
|
ee0b1f
|
97 |
clone.setDirectory(ticgit2Folder);
|
7e8873
|
98 |
clone.setBare(false);
|
JM |
99 |
clone.setCloneAllBranches(true);
|
|
100 |
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider("bogus", "bogus"));
|
ee0b1f
|
101 |
close(clone.call());
|
7e8873
|
102 |
cloned = true;
|
JM |
103 |
} catch (Exception e) {
|
dcf575
|
104 |
// swallow the exception which we expect
|
7e8873
|
105 |
}
|
JM |
106 |
|
|
107 |
// restore anonymous repository access
|
|
108 |
model.accessRestriction = AccessRestrictionType.NONE;
|
|
109 |
GitBlit.self().updateRepositoryModel(model.name, model, false);
|
|
110 |
|
|
111 |
assertFalse("Bogus login cloned a repository?!", cloned);
|
375caa
|
112 |
}
|
6adf56
|
113 |
|
JM |
114 |
@Test
|
|
115 |
public void testUnauthorizedLoginClone() throws Exception {
|
|
116 |
// restrict repository access
|
|
117 |
RepositoryModel model = GitBlit.self().getRepositoryModel("ticgit.git");
|
|
118 |
model.accessRestriction = AccessRestrictionType.CLONE;
|
|
119 |
model.authorizationControl = AuthorizationControl.NAMED;
|
|
120 |
UserModel user = new UserModel("james");
|
|
121 |
user.password = "james";
|
|
122 |
GitBlit.self().updateUserModel(user.username, user, true);
|
|
123 |
GitBlit.self().updateRepositoryModel(model.name, model, false);
|
|
124 |
|
|
125 |
FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
|
|
126 |
|
|
127 |
// delete any existing working folder
|
|
128 |
boolean cloned = false;
|
|
129 |
try {
|
|
130 |
CloneCommand clone = Git.cloneRepository();
|
|
131 |
clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
|
|
132 |
clone.setDirectory(ticgit2Folder);
|
|
133 |
clone.setBare(false);
|
|
134 |
clone.setCloneAllBranches(true);
|
|
135 |
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(user.username, user.password));
|
|
136 |
close(clone.call());
|
|
137 |
cloned = true;
|
|
138 |
} catch (Exception e) {
|
|
139 |
// swallow the exception which we expect
|
|
140 |
}
|
|
141 |
|
|
142 |
assertFalse("Unauthorized login cloned a repository?!", cloned);
|
|
143 |
|
|
144 |
FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
|
|
145 |
|
|
146 |
// switch to authenticated
|
|
147 |
model.authorizationControl = AuthorizationControl.AUTHENTICATED;
|
|
148 |
GitBlit.self().updateRepositoryModel(model.name, model, false);
|
|
149 |
|
|
150 |
// try clone again
|
|
151 |
cloned = false;
|
|
152 |
CloneCommand clone = Git.cloneRepository();
|
|
153 |
clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
|
|
154 |
clone.setDirectory(ticgit2Folder);
|
|
155 |
clone.setBare(false);
|
|
156 |
clone.setCloneAllBranches(true);
|
|
157 |
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(user.username, user.password));
|
|
158 |
close(clone.call());
|
|
159 |
cloned = true;
|
|
160 |
|
|
161 |
assertTrue("Authenticated login could not clone!", cloned);
|
|
162 |
|
|
163 |
FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
|
|
164 |
|
|
165 |
// restore anonymous repository access
|
|
166 |
model.accessRestriction = AccessRestrictionType.NONE;
|
|
167 |
model.authorizationControl = AuthorizationControl.NAMED;
|
|
168 |
GitBlit.self().updateRepositoryModel(model.name, model, false);
|
|
169 |
GitBlit.self().deleteUser(user.username);
|
|
170 |
}
|
375caa
|
171 |
|
29692a
|
172 |
@Test
|
cb285c
|
173 |
public void testAnonymousPush() throws Exception {
|
ee0b1f
|
174 |
Git git = Git.open(ticgitFolder);
|
JM |
175 |
File file = new File(ticgitFolder, "TODO");
|
e5cb55
|
176 |
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
|
ee0b1f
|
177 |
BufferedWriter w = new BufferedWriter(os);
|
e5cb55
|
178 |
w.write("// hellol中文 " + new Date().toString() + "\n");
|
ee0b1f
|
179 |
w.close();
|
JM |
180 |
git.add().addFilepattern(file.getName()).call();
|
|
181 |
git.commit().setMessage("test commit").call();
|
375caa
|
182 |
git.push().setPushAll().call();
|
ee0b1f
|
183 |
close(git);
|
d40adc
|
184 |
}
|
ee0b1f
|
185 |
|
cb285c
|
186 |
@Test
|
JM |
187 |
public void testSubfolderPush() throws Exception {
|
|
188 |
CloneCommand clone = Git.cloneRepository();
|
|
189 |
clone.setURI(MessageFormat.format("{0}/git/test/jgit.git", url));
|
|
190 |
clone.setDirectory(jgitFolder);
|
|
191 |
clone.setBare(false);
|
|
192 |
clone.setCloneAllBranches(true);
|
|
193 |
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
|
ee0b1f
|
194 |
close(clone.call());
|
cb285c
|
195 |
assertTrue(true);
|
ee0b1f
|
196 |
|
cb285c
|
197 |
Git git = Git.open(jgitFolder);
|
JM |
198 |
File file = new File(jgitFolder, "TODO");
|
e5cb55
|
199 |
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
|
ee0b1f
|
200 |
BufferedWriter w = new BufferedWriter(os);
|
JM |
201 |
w.write("// " + new Date().toString() + "\n");
|
|
202 |
w.close();
|
|
203 |
git.add().addFilepattern(file.getName()).call();
|
|
204 |
git.commit().setMessage("test commit").call();
|
cb285c
|
205 |
git.push().setPushAll().call();
|
ee0b1f
|
206 |
close(git);
|
JM |
207 |
}
|
|
208 |
|
b74031
|
209 |
@Test
|
JM |
210 |
public void testPushToNonBareRepository() throws Exception {
|
|
211 |
CloneCommand clone = Git.cloneRepository();
|
|
212 |
clone.setURI(MessageFormat.format("{0}/git/working/jgit", url));
|
|
213 |
clone.setDirectory(jgit2Folder);
|
|
214 |
clone.setBare(false);
|
|
215 |
clone.setCloneAllBranches(true);
|
|
216 |
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
|
|
217 |
close(clone.call());
|
|
218 |
assertTrue(true);
|
|
219 |
|
|
220 |
Git git = Git.open(jgit2Folder);
|
|
221 |
File file = new File(jgit2Folder, "NONBARE");
|
e5cb55
|
222 |
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
|
b74031
|
223 |
BufferedWriter w = new BufferedWriter(os);
|
JM |
224 |
w.write("// " + new Date().toString() + "\n");
|
|
225 |
w.close();
|
|
226 |
git.add().addFilepattern(file.getName()).call();
|
|
227 |
git.commit().setMessage("test commit followed by push to non-bare repository").call();
|
|
228 |
try {
|
|
229 |
git.push().setPushAll().call();
|
|
230 |
assertTrue(false);
|
|
231 |
} catch (Exception e) {
|
|
232 |
assertTrue(e.getCause().getMessage().contains("git-receive-pack not permitted"));
|
|
233 |
}
|
|
234 |
close(git);
|
|
235 |
}
|
|
236 |
|
ee0b1f
|
237 |
private void close(Git git) {
|
JM |
238 |
// really close the repository
|
|
239 |
// decrement the use counter to 0
|
|
240 |
for (int i = 0; i < 2; i++) {
|
|
241 |
git.getRepository().close();
|
|
242 |
}
|
cb285c
|
243 |
}
|
375caa
|
244 |
}
|