blob: bd73242940243cdcea551c4f3be890afd9f1ee87 [file] [log] [blame]
Dave Borowitzd0b7e182013-01-11 15:55:09 -08001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
Dave Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
Dave Borowitz1d94e652014-07-30 12:45:09 -070018import static com.google.gitiles.TestGitilesUrls.URLS;
Dave Borowitzd0b7e182013-01-11 15:55:09 -080019
Dave Borowitz32ec5b92014-07-30 07:43:28 -070020import com.google.common.collect.ImmutableList;
Dave Borowitz1d94e652014-07-30 12:45:09 -070021import com.google.common.collect.ImmutableMap;
Dave Borowitz32ec5b92014-07-30 07:43:28 -070022import com.google.gitiles.RefServlet.RefJsonData;
Dave Borowitz32ec5b92014-07-30 07:43:28 -070023import com.google.gson.reflect.TypeToken;
24
Dave Borowitzd0b7e182013-01-11 15:55:09 -080025import org.eclipse.jgit.lib.ObjectId;
26import org.eclipse.jgit.lib.Repository;
27import org.eclipse.jgit.revwalk.RevCommit;
28import org.eclipse.jgit.revwalk.RevTag;
Dave Borowitz1d94e652014-07-30 12:45:09 -070029import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070030import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080031import org.junit.runner.RunWith;
32import org.junit.runners.JUnit4;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070033
34import java.io.IOException;
Dave Borowitz32ec5b92014-07-30 07:43:28 -070035import java.util.List;
36import java.util.Map;
Dave Borowitzd0b7e182013-01-11 15:55:09 -080037
Dave Borowitz1d94e652014-07-30 12:45:09 -070038import javax.servlet.http.HttpServletRequest;
39
Nodir Turakulov08cdc232015-08-28 12:02:16 -070040/** Tests for {@link RefServlet}. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080041@RunWith(JUnit4.class)
Nodir Turakulov4bc26002015-08-18 18:24:37 -070042public class RefServletTest extends ServletTest {
Dave Borowitz1d94e652014-07-30 12:45:09 -070043 private void setUpSimpleRefs() throws Exception {
Dave Borowitzd0b7e182013-01-11 15:55:09 -080044 RevCommit commit = repo.branch("refs/heads/master").commit().create();
45 repo.update("refs/heads/branch", commit);
46 repo.update("refs/tags/ctag", commit);
47 RevTag tag = repo.tag("atag", commit);
48 repo.update("refs/tags/atag", tag);
Dave Borowitz1d94e652014-07-30 12:45:09 -070049 repo.getRepository().updateRef("HEAD").link("refs/heads/master");
Dave Borowitzd0b7e182013-01-11 15:55:09 -080050 }
51
Dave Borowitzd40bdf12014-04-19 19:33:56 -070052 @Test
53 public void evilRefName() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -070054 setUpSimpleRefs();
Dave Borowitzd0b7e182013-01-11 15:55:09 -080055 String evilRefName = "refs/evil/<script>window.close();</script>/&foo";
Dave Borowitzfde41fd2015-09-16 15:14:38 -040056 assertThat(Repository.isValidRefName(evilRefName)).isTrue();
Dave Borowitzd0b7e182013-01-11 15:55:09 -080057 repo.branch(evilRefName).commit().create();
58
Nodir Turakulov4bc26002015-08-18 18:24:37 -070059 FakeHttpServletResponse res = buildText("/repo/+refs/evil");
Dave Borowitzcf38c032016-05-02 11:06:23 -040060 assertThat(res.getActualBodyString())
61 .isEqualTo(
62 id(evilRefName) + " refs/evil/&lt;script&gt;window.close();&lt;/script&gt;/&amp;foo\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -080063 }
64
Dave Borowitzd40bdf12014-04-19 19:33:56 -070065 @Test
66 public void getRefsTextAll() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -070067 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -070068 FakeHttpServletResponse res = buildText("/repo/+refs");
Dave Borowitzd0b7e182013-01-11 15:55:09 -080069
Dave Borowitzcf38c032016-05-02 11:06:23 -040070 assertThat(res.getActualBodyString())
71 .isEqualTo(
72 id("HEAD")
73 + " HEAD\n"
74 + id("refs/heads/branch")
75 + " refs/heads/branch\n"
76 + id("refs/heads/master")
77 + " refs/heads/master\n"
78 + id("refs/tags/atag")
79 + " refs/tags/atag\n"
80 + peeled("refs/tags/atag")
81 + " refs/tags/atag^{}\n"
82 + id("refs/tags/ctag")
83 + " refs/tags/ctag\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -080084 }
85
Dave Borowitzd40bdf12014-04-19 19:33:56 -070086 @Test
87 public void getRefsTextAllTrailingSlash() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -070088 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -070089 FakeHttpServletResponse res = buildText("/repo/+refs/");
Dave Borowitzd0b7e182013-01-11 15:55:09 -080090
Dave Borowitzcf38c032016-05-02 11:06:23 -040091 assertThat(res.getActualBodyString())
92 .isEqualTo(
93 id("HEAD")
94 + " HEAD\n"
95 + id("refs/heads/branch")
96 + " refs/heads/branch\n"
97 + id("refs/heads/master")
98 + " refs/heads/master\n"
99 + id("refs/tags/atag")
100 + " refs/tags/atag\n"
101 + peeled("refs/tags/atag")
102 + " refs/tags/atag^{}\n"
103 + id("refs/tags/ctag")
104 + " refs/tags/ctag\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800105 }
106
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700107 @Test
108 public void getRefsHeadsText() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700109 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700110 FakeHttpServletResponse res = buildText("/repo/+refs/heads");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800111
Dave Borowitzcf38c032016-05-02 11:06:23 -0400112 assertThat(res.getActualBodyString())
113 .isEqualTo(
114 id("refs/heads/branch")
115 + " refs/heads/branch\n"
116 + id("refs/heads/master")
117 + " refs/heads/master\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800118 }
119
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700120 @Test
121 public void getRefsHeadsTextTrailingSlash() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700122 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700123 FakeHttpServletResponse res = buildText("/repo/+refs/heads/");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800124
Dave Borowitzcf38c032016-05-02 11:06:23 -0400125 assertThat(res.getActualBodyString())
126 .isEqualTo(
127 id("refs/heads/branch")
128 + " refs/heads/branch\n"
129 + id("refs/heads/master")
130 + " refs/heads/master\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800131 }
132
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700133 @Test
134 public void noHeadText() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700135 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700136 FakeHttpServletResponse res = buildText("/repo/+refs/HEAD");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800137
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800138 // /+refs/foo means refs/foo(/*), so this is empty.
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400139 assertThat(res.getActualBodyString()).isEqualTo("");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800140 }
141
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700142 @Test
143 public void singleHeadText() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700144 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700145 FakeHttpServletResponse res = buildText("/repo/+refs/heads/master");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800146
Dave Borowitzcf38c032016-05-02 11:06:23 -0400147 assertThat(res.getActualBodyString())
148 .isEqualTo(id("refs/heads/master") + " refs/heads/master\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800149 }
150
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700151 @Test
152 public void singlePeeledTagText() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700153 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700154 FakeHttpServletResponse res = buildText("/repo/+refs/tags/atag");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800155
Dave Borowitzcf38c032016-05-02 11:06:23 -0400156 assertThat(res.getActualBodyString())
157 .isEqualTo(
158 id("refs/tags/atag")
159 + " refs/tags/atag\n"
160 + peeled("refs/tags/atag")
161 + " refs/tags/atag^{}\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800162 }
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700163
164 @Test
165 public void getRefsJsonAll() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700166 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700167 Map<String, RefJsonData> result = buildRefJson("/repo/+refs");
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700168 List<String> keys = ImmutableList.copyOf(result.keySet());
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400169 assertThat(keys)
170 .containsExactly(
Dave Borowitzcf38c032016-05-02 11:06:23 -0400171 "HEAD", "refs/heads/branch", "refs/heads/master", "refs/tags/atag", "refs/tags/ctag")
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400172 .inOrder();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700173
174 RefJsonData head = result.get(keys.get(0));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400175 assertThat(head.value).isEqualTo(id("HEAD"));
176 assertThat(head.peeled).isNull();
177 assertThat(head.target).isEqualTo("refs/heads/master");
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700178
179 RefJsonData branch = result.get(keys.get(1));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400180 assertThat(branch.value).isEqualTo(id("refs/heads/branch"));
181 assertThat(branch.peeled).isNull();
182 assertThat(branch.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700183
184 RefJsonData master = result.get(keys.get(2));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400185 assertThat(master.value).isEqualTo(id("refs/heads/master"));
186 assertThat(master.peeled).isNull();
187 assertThat(master.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700188
189 RefJsonData atag = result.get(keys.get(3));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400190 assertThat(atag.value).isEqualTo(id("refs/tags/atag"));
191 assertThat(atag.peeled).isEqualTo(peeled("refs/tags/atag"));
192 assertThat(atag.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700193
194 RefJsonData ctag = result.get(keys.get(4));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400195 assertThat(ctag.value).isEqualTo(id("refs/tags/ctag"));
196 assertThat(ctag.peeled).isNull();
197 assertThat(ctag.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700198 }
199
200 @Test
201 public void getRefsHeadsJson() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700202 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700203 Map<String, RefJsonData> result = buildRefJson("/repo/+refs/heads");
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700204 List<String> keys = ImmutableList.copyOf(result.keySet());
Dave Borowitzcf38c032016-05-02 11:06:23 -0400205 assertThat(keys).containsExactly("branch", "master").inOrder();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700206
207 RefJsonData branch = result.get(keys.get(0));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400208 assertThat(branch.value).isEqualTo(id("refs/heads/branch"));
209 assertThat(branch.peeled).isNull();
210 assertThat(branch.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700211
212 RefJsonData master = result.get(keys.get(1));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400213 assertThat(master.value).isEqualTo(id("refs/heads/master"));
214 assertThat(master.peeled).isNull();
215 assertThat(master.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700216 }
217
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700218 private Map<String, RefJsonData> buildRefJson(String path) throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400219 return buildJson(new TypeToken<Map<String, RefJsonData>>() {}, path);
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700220 }
Dave Borowitz1d94e652014-07-30 12:45:09 -0700221
222 @Test
223 public void emptySoy() throws Exception {
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400224 assertThat(buildBranchesSoyData()).isEmpty();
225 assertThat(buildTagsSoyData()).isEmpty();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700226 }
227
228 @Test
229 public void branchesAndTagsSoy() throws Exception {
230 repo.branch("refs/heads/foo").commit().create();
231 repo.branch("refs/heads/bar").commit().create();
232 repo.branch("refs/tags/baz").commit().create();
233 repo.branch("refs/nope/quux").commit().create();
234
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400235 assertThat(buildBranchesSoyData())
Dave Borowitzcf38c032016-05-02 11:06:23 -0400236 .containsExactly(ref("/b/test/+/bar", "bar"), ref("/b/test/+/foo", "foo"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400237 .inOrder();
Dave Borowitzcf38c032016-05-02 11:06:23 -0400238 assertThat(buildTagsSoyData()).containsExactly(ref("/b/test/+/baz", "baz")).inOrder();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700239 }
240
241 @Test
242 public void ambiguousBranchSoy() throws Exception {
243 repo.branch("refs/heads/foo").commit().create();
244 repo.branch("refs/heads/bar").commit().create();
245 repo.branch("refs/tags/foo").commit().create();
246
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400247 assertThat(buildBranchesSoyData())
Dave Borowitzcf38c032016-05-02 11:06:23 -0400248 .containsExactly(ref("/b/test/+/bar", "bar"), ref("/b/test/+/refs/heads/foo", "foo"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400249 .inOrder();
250 assertThat(buildTagsSoyData())
251 .containsExactly(
Dave Borowitz1d94e652014-07-30 12:45:09 -0700252 // refs/tags/ is searched before refs/heads/, so this does not
253 // appear ambiguous.
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400254 ref("/b/test/+/foo", "foo"))
255 .inOrder();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700256 }
257
258 @Test
259 public void ambiguousRelativeToNonBranchOrTagSoy() throws Exception {
260 repo.branch("refs/foo").commit().create();
261 repo.branch("refs/heads/foo").commit().create();
262 repo.branch("refs/tags/foo").commit().create();
263
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400264 assertThat(buildBranchesSoyData())
Dave Borowitzcf38c032016-05-02 11:06:23 -0400265 .containsExactly(ref("/b/test/+/refs/heads/foo", "foo"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400266 .inOrder();
Dave Borowitzcf38c032016-05-02 11:06:23 -0400267 assertThat(buildTagsSoyData()).containsExactly(ref("/b/test/+/refs/tags/foo", "foo")).inOrder();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700268 }
269
270 @Test
271 public void refsHeadsSoy() throws Exception {
272 repo.branch("refs/heads/foo").commit().create();
273 repo.branch("refs/heads/refs/heads/foo").commit().create();
274
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400275 assertThat(buildBranchesSoyData())
276 .containsExactly(
Dave Borowitz1d94e652014-07-30 12:45:09 -0700277 ref("/b/test/+/foo", "foo"),
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400278 ref("/b/test/+/refs/heads/refs/heads/foo", "refs/heads/foo"))
279 .inOrder();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700280 }
281
282 private HttpServletRequest buildSoyRequest() {
283 HttpServletRequest req = FakeHttpServletRequest.newRequest(repo.getRepository());
Dave Borowitzcf38c032016-05-02 11:06:23 -0400284 ViewFilter.setView(
285 req,
286 GitilesView.repositoryIndex()
287 .setHostName(URLS.getHostName(req))
288 .setServletPath(req.getServletPath())
289 .setRepositoryName("test")
290 .build());
Dave Borowitz1d94e652014-07-30 12:45:09 -0700291 return req;
292 }
293
294 private List<?> buildBranchesSoyData() throws Exception {
295 return RefServlet.getBranchesSoyData(buildSoyRequest(), Integer.MAX_VALUE);
296 }
297
298 private List<?> buildTagsSoyData() throws Exception {
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700299 try (RevWalk rw = new RevWalk(repo.getRepository())) {
Dave Borowitzcf38c032016-05-02 11:06:23 -0400300 return RefServlet.getTagsSoyData(
301 buildSoyRequest(), new TimeCache(TimeCache.defaultBuilder()), rw, Integer.MAX_VALUE);
Dave Borowitz1d94e652014-07-30 12:45:09 -0700302 }
303 }
304
305 private String id(String refName) throws IOException {
306 return ObjectId.toString(repo.getRepository().getRef(refName).getObjectId());
307 }
308
309 private String peeled(String refName) throws IOException {
Dave Borowitzcf38c032016-05-02 11:06:23 -0400310 return ObjectId.toString(
311 repo.getRepository().peel(repo.getRepository().getRef(refName)).getPeeledObjectId());
Dave Borowitz1d94e652014-07-30 12:45:09 -0700312 }
313
314 private Map<String, String> ref(String url, String name) {
315 return ImmutableMap.of("url", url, "name", name);
316 }
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800317}