blob: daaa52ee19b5c8e3be0a36fa8f3a7c0595cf14cb [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;
Dave Borowitz3b744b12016-08-19 16:11:10 -040024import java.io.IOException;
25import java.util.List;
26import java.util.Map;
27import javax.servlet.http.HttpServletRequest;
Dave Borowitzd0b7e182013-01-11 15:55:09 -080028import org.eclipse.jgit.lib.ObjectId;
29import org.eclipse.jgit.lib.Repository;
30import org.eclipse.jgit.revwalk.RevCommit;
31import org.eclipse.jgit.revwalk.RevTag;
Dave Borowitz1d94e652014-07-30 12:45:09 -070032import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070033import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080034import org.junit.runner.RunWith;
35import org.junit.runners.JUnit4;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070036
Nodir Turakulov08cdc232015-08-28 12:02:16 -070037/** Tests for {@link RefServlet}. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080038@RunWith(JUnit4.class)
Nodir Turakulov4bc26002015-08-18 18:24:37 -070039public class RefServletTest extends ServletTest {
Dave Borowitz1d94e652014-07-30 12:45:09 -070040 private void setUpSimpleRefs() throws Exception {
Dave Borowitzd0b7e182013-01-11 15:55:09 -080041 RevCommit commit = repo.branch("refs/heads/master").commit().create();
42 repo.update("refs/heads/branch", commit);
43 repo.update("refs/tags/ctag", commit);
44 RevTag tag = repo.tag("atag", commit);
45 repo.update("refs/tags/atag", tag);
Dave Borowitz1d94e652014-07-30 12:45:09 -070046 repo.getRepository().updateRef("HEAD").link("refs/heads/master");
Dave Borowitzd0b7e182013-01-11 15:55:09 -080047 }
48
Dave Borowitzd40bdf12014-04-19 19:33:56 -070049 @Test
50 public void evilRefName() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -070051 setUpSimpleRefs();
Dave Borowitzd0b7e182013-01-11 15:55:09 -080052 String evilRefName = "refs/evil/<script>window.close();</script>/&foo";
Dave Borowitzfde41fd2015-09-16 15:14:38 -040053 assertThat(Repository.isValidRefName(evilRefName)).isTrue();
Dave Borowitzd0b7e182013-01-11 15:55:09 -080054 repo.branch(evilRefName).commit().create();
55
Nodir Turakulov4bc26002015-08-18 18:24:37 -070056 FakeHttpServletResponse res = buildText("/repo/+refs/evil");
Dave Borowitzcf38c032016-05-02 11:06:23 -040057 assertThat(res.getActualBodyString())
58 .isEqualTo(
59 id(evilRefName) + " refs/evil/&lt;script&gt;window.close();&lt;/script&gt;/&amp;foo\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -080060 }
61
Dave Borowitzd40bdf12014-04-19 19:33:56 -070062 @Test
63 public void getRefsTextAll() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -070064 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -070065 FakeHttpServletResponse res = buildText("/repo/+refs");
Dave Borowitzd0b7e182013-01-11 15:55:09 -080066
Dave Borowitzcf38c032016-05-02 11:06:23 -040067 assertThat(res.getActualBodyString())
68 .isEqualTo(
69 id("HEAD")
70 + " HEAD\n"
71 + id("refs/heads/branch")
72 + " refs/heads/branch\n"
73 + id("refs/heads/master")
74 + " refs/heads/master\n"
75 + id("refs/tags/atag")
76 + " refs/tags/atag\n"
77 + peeled("refs/tags/atag")
78 + " refs/tags/atag^{}\n"
79 + id("refs/tags/ctag")
80 + " refs/tags/ctag\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -080081 }
82
Dave Borowitzd40bdf12014-04-19 19:33:56 -070083 @Test
84 public void getRefsTextAllTrailingSlash() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -070085 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -070086 FakeHttpServletResponse res = buildText("/repo/+refs/");
Dave Borowitzd0b7e182013-01-11 15:55:09 -080087
Dave Borowitzcf38c032016-05-02 11:06:23 -040088 assertThat(res.getActualBodyString())
89 .isEqualTo(
90 id("HEAD")
91 + " HEAD\n"
92 + id("refs/heads/branch")
93 + " refs/heads/branch\n"
94 + id("refs/heads/master")
95 + " refs/heads/master\n"
96 + id("refs/tags/atag")
97 + " refs/tags/atag\n"
98 + peeled("refs/tags/atag")
99 + " refs/tags/atag^{}\n"
100 + id("refs/tags/ctag")
101 + " refs/tags/ctag\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800102 }
103
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700104 @Test
105 public void getRefsHeadsText() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700106 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700107 FakeHttpServletResponse res = buildText("/repo/+refs/heads");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800108
Dave Borowitzcf38c032016-05-02 11:06:23 -0400109 assertThat(res.getActualBodyString())
110 .isEqualTo(
111 id("refs/heads/branch")
112 + " refs/heads/branch\n"
113 + id("refs/heads/master")
114 + " refs/heads/master\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800115 }
116
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700117 @Test
118 public void getRefsHeadsTextTrailingSlash() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700119 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700120 FakeHttpServletResponse res = buildText("/repo/+refs/heads/");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800121
Dave Borowitzcf38c032016-05-02 11:06:23 -0400122 assertThat(res.getActualBodyString())
123 .isEqualTo(
124 id("refs/heads/branch")
125 + " refs/heads/branch\n"
126 + id("refs/heads/master")
127 + " refs/heads/master\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800128 }
129
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700130 @Test
131 public void noHeadText() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700132 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700133 FakeHttpServletResponse res = buildText("/repo/+refs/HEAD");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800134
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800135 // /+refs/foo means refs/foo(/*), so this is empty.
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400136 assertThat(res.getActualBodyString()).isEqualTo("");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800137 }
138
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700139 @Test
140 public void singleHeadText() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700141 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700142 FakeHttpServletResponse res = buildText("/repo/+refs/heads/master");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800143
Dave Borowitzcf38c032016-05-02 11:06:23 -0400144 assertThat(res.getActualBodyString())
145 .isEqualTo(id("refs/heads/master") + " refs/heads/master\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800146 }
147
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700148 @Test
149 public void singlePeeledTagText() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700150 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700151 FakeHttpServletResponse res = buildText("/repo/+refs/tags/atag");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800152
Dave Borowitzcf38c032016-05-02 11:06:23 -0400153 assertThat(res.getActualBodyString())
154 .isEqualTo(
155 id("refs/tags/atag")
156 + " refs/tags/atag\n"
157 + peeled("refs/tags/atag")
158 + " refs/tags/atag^{}\n");
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800159 }
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700160
161 @Test
162 public void getRefsJsonAll() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700163 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700164 Map<String, RefJsonData> result = buildRefJson("/repo/+refs");
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700165 List<String> keys = ImmutableList.copyOf(result.keySet());
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400166 assertThat(keys)
167 .containsExactly(
Dave Borowitzcf38c032016-05-02 11:06:23 -0400168 "HEAD", "refs/heads/branch", "refs/heads/master", "refs/tags/atag", "refs/tags/ctag")
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400169 .inOrder();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700170
171 RefJsonData head = result.get(keys.get(0));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400172 assertThat(head.value).isEqualTo(id("HEAD"));
173 assertThat(head.peeled).isNull();
174 assertThat(head.target).isEqualTo("refs/heads/master");
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700175
176 RefJsonData branch = result.get(keys.get(1));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400177 assertThat(branch.value).isEqualTo(id("refs/heads/branch"));
178 assertThat(branch.peeled).isNull();
179 assertThat(branch.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700180
181 RefJsonData master = result.get(keys.get(2));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400182 assertThat(master.value).isEqualTo(id("refs/heads/master"));
183 assertThat(master.peeled).isNull();
184 assertThat(master.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700185
186 RefJsonData atag = result.get(keys.get(3));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400187 assertThat(atag.value).isEqualTo(id("refs/tags/atag"));
188 assertThat(atag.peeled).isEqualTo(peeled("refs/tags/atag"));
189 assertThat(atag.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700190
191 RefJsonData ctag = result.get(keys.get(4));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400192 assertThat(ctag.value).isEqualTo(id("refs/tags/ctag"));
193 assertThat(ctag.peeled).isNull();
194 assertThat(ctag.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700195 }
196
197 @Test
198 public void getRefsHeadsJson() throws Exception {
Dave Borowitz1d94e652014-07-30 12:45:09 -0700199 setUpSimpleRefs();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700200 Map<String, RefJsonData> result = buildRefJson("/repo/+refs/heads");
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700201 List<String> keys = ImmutableList.copyOf(result.keySet());
Dave Borowitzcf38c032016-05-02 11:06:23 -0400202 assertThat(keys).containsExactly("branch", "master").inOrder();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700203
204 RefJsonData branch = result.get(keys.get(0));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400205 assertThat(branch.value).isEqualTo(id("refs/heads/branch"));
206 assertThat(branch.peeled).isNull();
207 assertThat(branch.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700208
209 RefJsonData master = result.get(keys.get(1));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400210 assertThat(master.value).isEqualTo(id("refs/heads/master"));
211 assertThat(master.peeled).isNull();
212 assertThat(master.target).isNull();
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700213 }
214
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700215 private Map<String, RefJsonData> buildRefJson(String path) throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400216 return buildJson(new TypeToken<Map<String, RefJsonData>>() {}, path);
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700217 }
Dave Borowitz1d94e652014-07-30 12:45:09 -0700218
219 @Test
220 public void emptySoy() throws Exception {
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400221 assertThat(buildBranchesSoyData()).isEmpty();
222 assertThat(buildTagsSoyData()).isEmpty();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700223 }
224
225 @Test
226 public void branchesAndTagsSoy() throws Exception {
227 repo.branch("refs/heads/foo").commit().create();
228 repo.branch("refs/heads/bar").commit().create();
229 repo.branch("refs/tags/baz").commit().create();
230 repo.branch("refs/nope/quux").commit().create();
231
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400232 assertThat(buildBranchesSoyData())
Dave Borowitzcf38c032016-05-02 11:06:23 -0400233 .containsExactly(ref("/b/test/+/bar", "bar"), ref("/b/test/+/foo", "foo"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400234 .inOrder();
Dave Borowitzcf38c032016-05-02 11:06:23 -0400235 assertThat(buildTagsSoyData()).containsExactly(ref("/b/test/+/baz", "baz")).inOrder();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700236 }
237
238 @Test
239 public void ambiguousBranchSoy() throws Exception {
240 repo.branch("refs/heads/foo").commit().create();
241 repo.branch("refs/heads/bar").commit().create();
242 repo.branch("refs/tags/foo").commit().create();
243
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400244 assertThat(buildBranchesSoyData())
Dave Borowitzcf38c032016-05-02 11:06:23 -0400245 .containsExactly(ref("/b/test/+/bar", "bar"), ref("/b/test/+/refs/heads/foo", "foo"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400246 .inOrder();
247 assertThat(buildTagsSoyData())
248 .containsExactly(
Dave Borowitz1d94e652014-07-30 12:45:09 -0700249 // refs/tags/ is searched before refs/heads/, so this does not
250 // appear ambiguous.
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400251 ref("/b/test/+/foo", "foo"))
252 .inOrder();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700253 }
254
255 @Test
256 public void ambiguousRelativeToNonBranchOrTagSoy() throws Exception {
257 repo.branch("refs/foo").commit().create();
258 repo.branch("refs/heads/foo").commit().create();
259 repo.branch("refs/tags/foo").commit().create();
260
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400261 assertThat(buildBranchesSoyData())
Dave Borowitzcf38c032016-05-02 11:06:23 -0400262 .containsExactly(ref("/b/test/+/refs/heads/foo", "foo"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400263 .inOrder();
Dave Borowitzcf38c032016-05-02 11:06:23 -0400264 assertThat(buildTagsSoyData()).containsExactly(ref("/b/test/+/refs/tags/foo", "foo")).inOrder();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700265 }
266
267 @Test
268 public void refsHeadsSoy() throws Exception {
269 repo.branch("refs/heads/foo").commit().create();
270 repo.branch("refs/heads/refs/heads/foo").commit().create();
271
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400272 assertThat(buildBranchesSoyData())
273 .containsExactly(
Dave Borowitz1d94e652014-07-30 12:45:09 -0700274 ref("/b/test/+/foo", "foo"),
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400275 ref("/b/test/+/refs/heads/refs/heads/foo", "refs/heads/foo"))
276 .inOrder();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700277 }
278
279 private HttpServletRequest buildSoyRequest() {
280 HttpServletRequest req = FakeHttpServletRequest.newRequest(repo.getRepository());
Dave Borowitzcf38c032016-05-02 11:06:23 -0400281 ViewFilter.setView(
282 req,
283 GitilesView.repositoryIndex()
284 .setHostName(URLS.getHostName(req))
285 .setServletPath(req.getServletPath())
286 .setRepositoryName("test")
287 .build());
Dave Borowitz1d94e652014-07-30 12:45:09 -0700288 return req;
289 }
290
291 private List<?> buildBranchesSoyData() throws Exception {
292 return RefServlet.getBranchesSoyData(buildSoyRequest(), Integer.MAX_VALUE);
293 }
294
295 private List<?> buildTagsSoyData() throws Exception {
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700296 try (RevWalk rw = new RevWalk(repo.getRepository())) {
Dave Borowitzcf38c032016-05-02 11:06:23 -0400297 return RefServlet.getTagsSoyData(
298 buildSoyRequest(), new TimeCache(TimeCache.defaultBuilder()), rw, Integer.MAX_VALUE);
Dave Borowitz1d94e652014-07-30 12:45:09 -0700299 }
300 }
301
302 private String id(String refName) throws IOException {
Dave Borowitz14cad732016-05-26 17:34:19 -0400303 return ObjectId.toString(repo.getRepository().exactRef(refName).getObjectId());
Dave Borowitz1d94e652014-07-30 12:45:09 -0700304 }
305
306 private String peeled(String refName) throws IOException {
Dave Borowitzcf38c032016-05-02 11:06:23 -0400307 return ObjectId.toString(
David Pursehousedcb3d052018-07-03 14:04:17 +0900308 repo.getRepository()
309 .getRefDatabase()
310 .peel(repo.getRepository().exactRef(refName))
311 .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}