You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitServletResponseTests.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2015, christian.Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.http.test;
  44. import static org.junit.Assert.assertTrue;
  45. import static org.junit.Assert.fail;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import javax.servlet.http.HttpServletRequest;
  49. import org.eclipse.jetty.servlet.ServletContextHandler;
  50. import org.eclipse.jetty.servlet.ServletHolder;
  51. import org.eclipse.jgit.errors.CorruptObjectException;
  52. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  53. import org.eclipse.jgit.errors.TooLargePackException;
  54. import org.eclipse.jgit.errors.TransportException;
  55. import org.eclipse.jgit.http.server.GitServlet;
  56. import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
  57. import org.eclipse.jgit.junit.TestRepository;
  58. import org.eclipse.jgit.junit.http.HttpTestCase;
  59. import org.eclipse.jgit.lib.AnyObjectId;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.NullProgressMonitor;
  62. import org.eclipse.jgit.lib.ObjectChecker;
  63. import org.eclipse.jgit.lib.Repository;
  64. import org.eclipse.jgit.lib.StoredConfig;
  65. import org.eclipse.jgit.revwalk.RevBlob;
  66. import org.eclipse.jgit.revwalk.RevCommit;
  67. import org.eclipse.jgit.transport.PostReceiveHook;
  68. import org.eclipse.jgit.transport.PreReceiveHook;
  69. import org.eclipse.jgit.transport.ReceiveCommand;
  70. import org.eclipse.jgit.transport.ReceivePack;
  71. import org.eclipse.jgit.transport.RemoteRefUpdate;
  72. import org.eclipse.jgit.transport.Transport;
  73. import org.eclipse.jgit.transport.URIish;
  74. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  75. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  76. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  77. import org.junit.Before;
  78. import org.junit.Test;
  79. /**
  80. * Tests for correct responses of {@link GitServlet}. Especially error
  81. * situations where the {@link GitServlet} faces exceptions during request
  82. * processing are tested
  83. */
  84. public class GitServletResponseTests extends HttpTestCase {
  85. private Repository srvRepo;
  86. private URIish srvURI;
  87. private GitServlet gs;
  88. private long maxPackSize = 0; // the maximum pack file size used by
  89. // the server
  90. private PostReceiveHook postHook = null;
  91. private PreReceiveHook preHook = null;
  92. private ObjectChecker oc = null;
  93. /**
  94. * Setup a http server using {@link GitServlet}. Tests should be able to
  95. * configure the maximum pack file size, the object checker and custom hooks
  96. * just before they talk to the server.
  97. */
  98. @Override
  99. @Before
  100. public void setUp() throws Exception {
  101. super.setUp();
  102. final TestRepository<Repository> srv = createTestRepository();
  103. final String repoName = srv.getRepository().getDirectory().getName();
  104. ServletContextHandler app = server.addContext("/git");
  105. gs = new GitServlet();
  106. gs.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
  107. @Override
  108. public Repository open(HttpServletRequest req, String name)
  109. throws RepositoryNotFoundException,
  110. ServiceNotEnabledException {
  111. if (!name.equals(repoName))
  112. throw new RepositoryNotFoundException(name);
  113. final Repository db = srv.getRepository();
  114. db.incrementOpen();
  115. return db;
  116. }
  117. });
  118. gs.setReceivePackFactory(new DefaultReceivePackFactory() {
  119. @Override
  120. public ReceivePack create(HttpServletRequest req, Repository db)
  121. throws ServiceNotEnabledException,
  122. ServiceNotAuthorizedException {
  123. ReceivePack recv = super.create(req, db);
  124. if (maxPackSize > 0)
  125. recv.setMaxPackSizeLimit(maxPackSize);
  126. if (postHook != null)
  127. recv.setPostReceiveHook(postHook);
  128. if (preHook != null)
  129. recv.setPreReceiveHook(preHook);
  130. if (oc != null)
  131. recv.setObjectChecker(oc);
  132. return recv;
  133. }
  134. });
  135. app.addServlet(new ServletHolder(gs), "/*");
  136. server.setUp();
  137. srvRepo = srv.getRepository();
  138. srvURI = toURIish(app, repoName);
  139. StoredConfig cfg = srvRepo.getConfig();
  140. cfg.setBoolean("http", null, "receivepack", true);
  141. cfg.save();
  142. }
  143. /**
  144. * Configure a {@link GitServlet} that faces a {@link IllegalStateException}
  145. * during executing preReceiveHooks. This used to lead to exceptions with a
  146. * description of "invalid channel 101" on the client side. Make sure
  147. * clients receive the correct response on the correct sideband.
  148. *
  149. * @throws Exception
  150. */
  151. @Test
  152. public void testRuntimeExceptionInPreReceiveHook() throws Exception {
  153. final TestRepository client = createTestRepository();
  154. final RevBlob Q_txt = client
  155. .blob("some blob content to measure pack size");
  156. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  157. final Repository clientRepo = client.getRepository();
  158. final String srvBranchName = Constants.R_HEADS + "new.branch";
  159. maxPackSize = 0;
  160. postHook = null;
  161. preHook = new PreReceiveHook() {
  162. @Override
  163. public void onPreReceive(ReceivePack rp,
  164. Collection<ReceiveCommand> commands) {
  165. throw new IllegalStateException();
  166. }
  167. };
  168. try (Transport t = Transport.open(clientRepo, srvURI)) {
  169. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  170. srvBranchName, false, null, null);
  171. try {
  172. t.push(NullProgressMonitor.INSTANCE,
  173. Collections.singleton(update));
  174. fail("should not reach this line");
  175. } catch (Exception e) {
  176. assertTrue(e instanceof TransportException);
  177. }
  178. }
  179. }
  180. /**
  181. * Configure a {@link GitServlet} that faces a {@link IllegalStateException}
  182. * during executing objectChecking.
  183. *
  184. * @throws Exception
  185. */
  186. @Test
  187. public void testObjectCheckerException() throws Exception {
  188. final TestRepository client = createTestRepository();
  189. final RevBlob Q_txt = client
  190. .blob("some blob content to measure pack size");
  191. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  192. final Repository clientRepo = client.getRepository();
  193. final String srvBranchName = Constants.R_HEADS + "new.branch";
  194. maxPackSize = 0;
  195. postHook = null;
  196. preHook = null;
  197. oc = new ObjectChecker() {
  198. @Override
  199. public void checkCommit(AnyObjectId id, byte[] raw)
  200. throws CorruptObjectException {
  201. throw new CorruptObjectException("refusing all commits");
  202. }
  203. };
  204. try (Transport t = Transport.open(clientRepo, srvURI)) {
  205. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  206. srvBranchName, false, null, null);
  207. try {
  208. t.push(NullProgressMonitor.INSTANCE,
  209. Collections.singleton(update));
  210. fail("should not reach this line");
  211. } catch (Exception e) {
  212. assertTrue(e instanceof TransportException);
  213. }
  214. }
  215. }
  216. /**
  217. * Configure a {@link GitServlet} that faces a {@link TooLargePackException}
  218. * during persisting the pack and a {@link IllegalStateException} during
  219. * executing postReceiveHooks. This used to lead to exceptions with a
  220. * description of "invalid channel 101" on the client side. Make sure
  221. * clients receive the correct response about the too large pack on the
  222. * correct sideband.
  223. *
  224. * @throws Exception
  225. */
  226. @Test
  227. public void testUnpackErrorWithSubsequentExceptionInPostReceiveHook()
  228. throws Exception {
  229. final TestRepository client = createTestRepository();
  230. final RevBlob Q_txt = client
  231. .blob("some blob content to measure pack size");
  232. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  233. final Repository clientRepo = client.getRepository();
  234. final String srvBranchName = Constants.R_HEADS + "new.branch";
  235. // this maxPackSize leads to an unPackError
  236. maxPackSize = 100;
  237. // this PostReceiveHook when called after an unsuccesfull unpack will
  238. // lead to an IllegalStateException
  239. postHook = new PostReceiveHook() {
  240. @Override
  241. public void onPostReceive(ReceivePack rp,
  242. Collection<ReceiveCommand> commands) {
  243. // the maxPackSize setting caused that the packfile couldn't be
  244. // saved to disk. Calling getPackSize() now will lead to a
  245. // IllegalStateException.
  246. rp.getPackSize();
  247. }
  248. };
  249. try (Transport t = Transport.open(clientRepo, srvURI)) {
  250. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  251. srvBranchName, false, null, null);
  252. try {
  253. t.push(NullProgressMonitor.INSTANCE,
  254. Collections.singleton(update));
  255. fail("should not reach this line");
  256. } catch (Exception e) {
  257. assertTrue(e instanceof TooLargePackException);
  258. }
  259. }
  260. }
  261. }