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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2015, christian.Halstrick <christian.halstrick@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.http.test;
  11. import static org.junit.Assert.assertTrue;
  12. import static org.junit.Assert.fail;
  13. import java.util.Collection;
  14. import java.util.Collections;
  15. import javax.servlet.http.HttpServletRequest;
  16. import org.eclipse.jetty.servlet.ServletContextHandler;
  17. import org.eclipse.jetty.servlet.ServletHolder;
  18. import org.eclipse.jgit.errors.CorruptObjectException;
  19. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  20. import org.eclipse.jgit.errors.TooLargePackException;
  21. import org.eclipse.jgit.errors.TransportException;
  22. import org.eclipse.jgit.http.server.GitServlet;
  23. import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
  24. import org.eclipse.jgit.junit.TestRepository;
  25. import org.eclipse.jgit.junit.http.HttpTestCase;
  26. import org.eclipse.jgit.lib.AnyObjectId;
  27. import org.eclipse.jgit.lib.Constants;
  28. import org.eclipse.jgit.lib.NullProgressMonitor;
  29. import org.eclipse.jgit.lib.ObjectChecker;
  30. import org.eclipse.jgit.lib.Repository;
  31. import org.eclipse.jgit.lib.StoredConfig;
  32. import org.eclipse.jgit.revwalk.RevBlob;
  33. import org.eclipse.jgit.revwalk.RevCommit;
  34. import org.eclipse.jgit.transport.PostReceiveHook;
  35. import org.eclipse.jgit.transport.PreReceiveHook;
  36. import org.eclipse.jgit.transport.ReceiveCommand;
  37. import org.eclipse.jgit.transport.ReceivePack;
  38. import org.eclipse.jgit.transport.RemoteRefUpdate;
  39. import org.eclipse.jgit.transport.Transport;
  40. import org.eclipse.jgit.transport.URIish;
  41. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  42. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  43. import org.junit.Before;
  44. import org.junit.Test;
  45. /**
  46. * Tests for correct responses of {@link GitServlet}. Especially error
  47. * situations where the {@link GitServlet} faces exceptions during request
  48. * processing are tested
  49. */
  50. public class GitServletResponseTests extends HttpTestCase {
  51. private Repository srvRepo;
  52. private URIish srvURI;
  53. private GitServlet gs;
  54. private long maxPackSize = 0; // the maximum pack file size used by
  55. // the server
  56. private PostReceiveHook postHook = null;
  57. private PreReceiveHook preHook = null;
  58. private ObjectChecker oc = null;
  59. /**
  60. * Setup a http server using {@link GitServlet}. Tests should be able to
  61. * configure the maximum pack file size, the object checker and custom hooks
  62. * just before they talk to the server.
  63. */
  64. @Override
  65. @Before
  66. public void setUp() throws Exception {
  67. super.setUp();
  68. final TestRepository<Repository> srv = createTestRepository();
  69. final String repoName = srv.getRepository().getDirectory().getName();
  70. ServletContextHandler app = server.addContext("/git");
  71. gs = new GitServlet();
  72. gs.setRepositoryResolver((HttpServletRequest req, String name) -> {
  73. if (!name.equals(repoName)) {
  74. throw new RepositoryNotFoundException(name);
  75. }
  76. final Repository db = srv.getRepository();
  77. db.incrementOpen();
  78. return db;
  79. });
  80. gs.setReceivePackFactory(new DefaultReceivePackFactory() {
  81. @Override
  82. public ReceivePack create(HttpServletRequest req, Repository db)
  83. throws ServiceNotEnabledException,
  84. ServiceNotAuthorizedException {
  85. ReceivePack recv = super.create(req, db);
  86. if (maxPackSize > 0)
  87. recv.setMaxPackSizeLimit(maxPackSize);
  88. if (postHook != null)
  89. recv.setPostReceiveHook(postHook);
  90. if (preHook != null)
  91. recv.setPreReceiveHook(preHook);
  92. if (oc != null)
  93. recv.setObjectChecker(oc);
  94. return recv;
  95. }
  96. });
  97. app.addServlet(new ServletHolder(gs), "/*");
  98. server.setUp();
  99. srvRepo = srv.getRepository();
  100. srvURI = toURIish(app, repoName);
  101. StoredConfig cfg = srvRepo.getConfig();
  102. cfg.setBoolean("http", null, "receivepack", true);
  103. cfg.save();
  104. }
  105. /**
  106. * Configure a {@link GitServlet} that faces a {@link IllegalStateException}
  107. * during executing preReceiveHooks. This used to lead to exceptions with a
  108. * description of "invalid channel 101" on the client side. Make sure
  109. * clients receive the correct response on the correct sideband.
  110. *
  111. * @throws Exception
  112. */
  113. @Test
  114. public void testRuntimeExceptionInPreReceiveHook() throws Exception {
  115. final TestRepository client = createTestRepository();
  116. final RevBlob Q_txt = client
  117. .blob("some blob content to measure pack size");
  118. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  119. final Repository clientRepo = client.getRepository();
  120. final String srvBranchName = Constants.R_HEADS + "new.branch";
  121. maxPackSize = 0;
  122. postHook = null;
  123. preHook = (ReceivePack rp, Collection<ReceiveCommand> commands) -> {
  124. throw new IllegalStateException();
  125. };
  126. try (Transport t = Transport.open(clientRepo, srvURI)) {
  127. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  128. srvBranchName, false, null, null);
  129. try {
  130. t.push(NullProgressMonitor.INSTANCE,
  131. Collections.singleton(update));
  132. fail("should not reach this line");
  133. } catch (Exception e) {
  134. assertTrue(e instanceof TransportException);
  135. }
  136. }
  137. }
  138. /**
  139. * Configure a {@link GitServlet} that faces a {@link IllegalStateException}
  140. * during executing objectChecking.
  141. *
  142. * @throws Exception
  143. */
  144. @Test
  145. public void testObjectCheckerException() throws Exception {
  146. final TestRepository client = createTestRepository();
  147. final RevBlob Q_txt = client
  148. .blob("some blob content to measure pack size");
  149. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  150. final Repository clientRepo = client.getRepository();
  151. final String srvBranchName = Constants.R_HEADS + "new.branch";
  152. maxPackSize = 0;
  153. postHook = null;
  154. preHook = null;
  155. oc = new ObjectChecker() {
  156. @Override
  157. public void checkCommit(AnyObjectId id, byte[] raw)
  158. throws CorruptObjectException {
  159. throw new CorruptObjectException("refusing all commits");
  160. }
  161. };
  162. try (Transport t = Transport.open(clientRepo, srvURI)) {
  163. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  164. srvBranchName, false, null, null);
  165. try {
  166. t.push(NullProgressMonitor.INSTANCE,
  167. Collections.singleton(update));
  168. fail("should not reach this line");
  169. } catch (Exception e) {
  170. assertTrue(e instanceof TransportException);
  171. }
  172. }
  173. }
  174. /**
  175. * Configure a {@link GitServlet} that faces a {@link TooLargePackException}
  176. * during persisting the pack and a {@link IllegalStateException} during
  177. * executing postReceiveHooks. This used to lead to exceptions with a
  178. * description of "invalid channel 101" on the client side. Make sure
  179. * clients receive the correct response about the too large pack on the
  180. * correct sideband.
  181. *
  182. * @throws Exception
  183. */
  184. @Test
  185. public void testUnpackErrorWithSubsequentExceptionInPostReceiveHook()
  186. throws Exception {
  187. final TestRepository client = createTestRepository();
  188. final RevBlob Q_txt = client
  189. .blob("some blob content to measure pack size");
  190. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  191. final Repository clientRepo = client.getRepository();
  192. final String srvBranchName = Constants.R_HEADS + "new.branch";
  193. // this maxPackSize leads to an unPackError
  194. maxPackSize = 100;
  195. // this PostReceiveHook when called after an unsuccesfull unpack will
  196. // lead to an IllegalStateException
  197. postHook = (ReceivePack rp, Collection<ReceiveCommand> commands) -> {
  198. // the maxPackSize setting caused that the packfile couldn't be
  199. // saved to disk. Calling getPackSize() now will lead to a
  200. // IllegalStateException.
  201. rp.getPackSize();
  202. };
  203. try (Transport t = Transport.open(clientRepo, srvURI)) {
  204. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  205. srvBranchName, false, null, null);
  206. try {
  207. t.push(NullProgressMonitor.INSTANCE,
  208. Collections.singleton(update));
  209. fail("should not reach this line");
  210. } catch (Exception e) {
  211. assertTrue(e instanceof TooLargePackException);
  212. }
  213. }
  214. }
  215. }