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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. @Before
  99. public void setUp() throws Exception {
  100. super.setUp();
  101. final TestRepository<Repository> srv = createTestRepository();
  102. final String repoName = srv.getRepository().getDirectory().getName();
  103. ServletContextHandler app = server.addContext("/git");
  104. gs = new GitServlet();
  105. gs.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
  106. public Repository open(HttpServletRequest req, String name)
  107. throws RepositoryNotFoundException,
  108. ServiceNotEnabledException {
  109. if (!name.equals(repoName))
  110. throw new RepositoryNotFoundException(name);
  111. final Repository db = srv.getRepository();
  112. db.incrementOpen();
  113. return db;
  114. }
  115. });
  116. gs.setReceivePackFactory(new DefaultReceivePackFactory() {
  117. public ReceivePack create(HttpServletRequest req, Repository db)
  118. throws ServiceNotEnabledException,
  119. ServiceNotAuthorizedException {
  120. ReceivePack recv = super.create(req, db);
  121. if (maxPackSize > 0)
  122. recv.setMaxPackSizeLimit(maxPackSize);
  123. if (postHook != null)
  124. recv.setPostReceiveHook(postHook);
  125. if (preHook != null)
  126. recv.setPreReceiveHook(preHook);
  127. if (oc != null)
  128. recv.setObjectChecker(oc);
  129. return recv;
  130. }
  131. });
  132. app.addServlet(new ServletHolder(gs), "/*");
  133. server.setUp();
  134. srvRepo = srv.getRepository();
  135. srvURI = toURIish(app, repoName);
  136. StoredConfig cfg = srvRepo.getConfig();
  137. cfg.setBoolean("http", null, "receivepack", true);
  138. cfg.save();
  139. }
  140. /**
  141. * Configure a {@link GitServlet} that faces a {@link IllegalStateException}
  142. * during executing preReceiveHooks. This used to lead to exceptions with a
  143. * description of "invalid channel 101" on the client side. Make sure
  144. * clients receive the correct response on the correct sideband.
  145. *
  146. * @throws Exception
  147. */
  148. @Test
  149. public void testRuntimeExceptionInPreReceiveHook() throws Exception {
  150. final TestRepository client = createTestRepository();
  151. final RevBlob Q_txt = client
  152. .blob("some blob content to measure pack size");
  153. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  154. final Repository clientRepo = client.getRepository();
  155. final String srvBranchName = Constants.R_HEADS + "new.branch";
  156. Transport t;
  157. maxPackSize = 0;
  158. postHook = null;
  159. preHook = new PreReceiveHook() {
  160. @Override
  161. public void onPreReceive(ReceivePack rp,
  162. Collection<ReceiveCommand> commands) {
  163. throw new IllegalStateException();
  164. }
  165. };
  166. t = Transport.open(clientRepo, srvURI);
  167. try {
  168. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  169. srvBranchName, false, null, null);
  170. try {
  171. t.push(NullProgressMonitor.INSTANCE,
  172. Collections.singleton(update));
  173. fail("should not reach this line");
  174. } catch (Exception e) {
  175. assertTrue(e instanceof TransportException);
  176. }
  177. } finally {
  178. t.close();
  179. }
  180. }
  181. /**
  182. * Configure a {@link GitServlet} that faces a {@link IllegalStateException}
  183. * during executing objectChecking.
  184. *
  185. * @throws Exception
  186. */
  187. @Test
  188. public void testObjectCheckerException() throws Exception {
  189. final TestRepository client = createTestRepository();
  190. final RevBlob Q_txt = client
  191. .blob("some blob content to measure pack size");
  192. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  193. final Repository clientRepo = client.getRepository();
  194. final String srvBranchName = Constants.R_HEADS + "new.branch";
  195. Transport t;
  196. maxPackSize = 0;
  197. postHook = null;
  198. preHook = null;
  199. oc = new ObjectChecker() {
  200. @Override
  201. public void checkCommit(AnyObjectId id, byte[] raw)
  202. throws CorruptObjectException {
  203. throw new CorruptObjectException("refusing all commits");
  204. }
  205. };
  206. t = Transport.open(clientRepo, srvURI);
  207. try {
  208. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  209. srvBranchName, false, null, null);
  210. try {
  211. t.push(NullProgressMonitor.INSTANCE,
  212. Collections.singleton(update));
  213. fail("should not reach this line");
  214. } catch (Exception e) {
  215. assertTrue(e instanceof TransportException);
  216. }
  217. } finally {
  218. t.close();
  219. }
  220. }
  221. /**
  222. * Configure a {@link GitServlet} that faces a {@link TooLargePackException}
  223. * during persisting the pack and a {@link IllegalStateException} during
  224. * executing postReceiveHooks. This used to lead to exceptions with a
  225. * description of "invalid channel 101" on the client side. Make sure
  226. * clients receive the correct response about the too large pack on the
  227. * correct sideband.
  228. *
  229. * @throws Exception
  230. */
  231. @Test
  232. public void testUnpackErrorWithSubsequentExceptionInPostReceiveHook()
  233. throws Exception {
  234. final TestRepository client = createTestRepository();
  235. final RevBlob Q_txt = client
  236. .blob("some blob content to measure pack size");
  237. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  238. final Repository clientRepo = client.getRepository();
  239. final String srvBranchName = Constants.R_HEADS + "new.branch";
  240. Transport t;
  241. // this maxPackSize leads to an unPackError
  242. maxPackSize = 400;
  243. // this PostReceiveHook when called after an unsuccesfull unpack will
  244. // lead to an IllegalStateException
  245. postHook = new PostReceiveHook() {
  246. public void onPostReceive(ReceivePack rp,
  247. Collection<ReceiveCommand> commands) {
  248. // the maxPackSize setting caused that the packfile couldn't be
  249. // saved to disk. Calling getPackSize() now will lead to a
  250. // IllegalStateException.
  251. rp.getPackSize();
  252. }
  253. };
  254. t = Transport.open(clientRepo, srvURI);
  255. try {
  256. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  257. srvBranchName, false, null, null);
  258. try {
  259. t.push(NullProgressMonitor.INSTANCE,
  260. Collections.singleton(update));
  261. fail("should not reach this line");
  262. } catch (Exception e) {
  263. assertTrue(e instanceof TooLargePackException);
  264. }
  265. } finally {
  266. t.close();
  267. }
  268. }
  269. }