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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.Constants;
  60. import org.eclipse.jgit.lib.NullProgressMonitor;
  61. import org.eclipse.jgit.lib.ObjectChecker;
  62. import org.eclipse.jgit.lib.Repository;
  63. import org.eclipse.jgit.lib.StoredConfig;
  64. import org.eclipse.jgit.revwalk.RevBlob;
  65. import org.eclipse.jgit.revwalk.RevCommit;
  66. import org.eclipse.jgit.transport.PostReceiveHook;
  67. import org.eclipse.jgit.transport.PreReceiveHook;
  68. import org.eclipse.jgit.transport.ReceiveCommand;
  69. import org.eclipse.jgit.transport.ReceivePack;
  70. import org.eclipse.jgit.transport.RemoteRefUpdate;
  71. import org.eclipse.jgit.transport.Transport;
  72. import org.eclipse.jgit.transport.URIish;
  73. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  74. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  75. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  76. import org.junit.Before;
  77. import org.junit.Test;
  78. /**
  79. * Tests for correct responses of {@link GitServlet}. Especially error
  80. * situations where the {@link GitServlet} faces exceptions during request
  81. * processing are tested
  82. */
  83. public class GitServletResponseTests extends HttpTestCase {
  84. private Repository srvRepo;
  85. private URIish srvURI;
  86. private GitServlet gs;
  87. private long maxPackSize = 0; // the maximum pack file size used by
  88. // the server
  89. private PostReceiveHook postHook = null;
  90. private PreReceiveHook preHook = null;
  91. private ObjectChecker oc = null;
  92. /**
  93. * Setup a http server using {@link GitServlet}. Tests should be able to
  94. * configure the maximum pack file size, the object checker and custom hooks
  95. * just before they talk to the server.
  96. */
  97. @Before
  98. public void setUp() throws Exception {
  99. super.setUp();
  100. final TestRepository<Repository> srv = createTestRepository();
  101. final String repoName = srv.getRepository().getDirectory().getName();
  102. ServletContextHandler app = server.addContext("/git");
  103. gs = new GitServlet();
  104. gs.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
  105. public Repository open(HttpServletRequest req, String name)
  106. throws RepositoryNotFoundException,
  107. ServiceNotEnabledException {
  108. if (!name.equals(repoName))
  109. throw new RepositoryNotFoundException(name);
  110. final Repository db = srv.getRepository();
  111. db.incrementOpen();
  112. return db;
  113. }
  114. });
  115. gs.setReceivePackFactory(new DefaultReceivePackFactory() {
  116. public ReceivePack create(HttpServletRequest req, Repository db)
  117. throws ServiceNotEnabledException,
  118. ServiceNotAuthorizedException {
  119. ReceivePack recv = super.create(req, db);
  120. if (maxPackSize > 0)
  121. recv.setMaxPackSizeLimit(maxPackSize);
  122. if (postHook != null)
  123. recv.setPostReceiveHook(postHook);
  124. if (preHook != null)
  125. recv.setPreReceiveHook(preHook);
  126. if (oc != null)
  127. recv.setObjectChecker(oc);
  128. return recv;
  129. }
  130. });
  131. app.addServlet(new ServletHolder(gs), "/*");
  132. server.setUp();
  133. srvRepo = srv.getRepository();
  134. srvURI = toURIish(app, repoName);
  135. StoredConfig cfg = srvRepo.getConfig();
  136. cfg.setBoolean("http", null, "receivepack", true);
  137. cfg.save();
  138. }
  139. /**
  140. * Configure a {@link GitServlet} that faces a {@link IllegalStateException}
  141. * during executing preReceiveHooks. This used to lead to exceptions with a
  142. * description of "invalid channel 101" on the client side. Make sure
  143. * clients receive the correct response on the correct sideband.
  144. *
  145. * @throws Exception
  146. */
  147. @Test
  148. public void testRuntimeExceptionInPreReceiveHook() throws Exception {
  149. final TestRepository client = createTestRepository();
  150. final RevBlob Q_txt = client
  151. .blob("some blob content to measure pack size");
  152. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  153. final Repository clientRepo = client.getRepository();
  154. final String srvBranchName = Constants.R_HEADS + "new.branch";
  155. Transport t;
  156. maxPackSize = 0;
  157. postHook = null;
  158. preHook = new PreReceiveHook() {
  159. @Override
  160. public void onPreReceive(ReceivePack rp,
  161. Collection<ReceiveCommand> commands) {
  162. throw new IllegalStateException();
  163. }
  164. };
  165. t = Transport.open(clientRepo, srvURI);
  166. try {
  167. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  168. srvBranchName, false, null, null);
  169. try {
  170. t.push(NullProgressMonitor.INSTANCE,
  171. Collections.singleton(update));
  172. fail("should not reach this line");
  173. } catch (Exception e) {
  174. assertTrue(e instanceof TransportException);
  175. }
  176. } finally {
  177. t.close();
  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. Transport t;
  195. maxPackSize = 0;
  196. postHook = null;
  197. preHook = null;
  198. oc = new ObjectChecker() {
  199. @Override
  200. public void checkCommit(byte[] raw) throws CorruptObjectException {
  201. throw new IllegalStateException();
  202. }
  203. };
  204. t = Transport.open(clientRepo, srvURI);
  205. try {
  206. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  207. srvBranchName, false, null, null);
  208. try {
  209. t.push(NullProgressMonitor.INSTANCE,
  210. Collections.singleton(update));
  211. fail("should not reach this line");
  212. } catch (Exception e) {
  213. assertTrue(e instanceof TransportException);
  214. }
  215. } finally {
  216. t.close();
  217. }
  218. }
  219. /**
  220. * Configure a {@link GitServlet} that faces a {@link TooLargePackException}
  221. * during persisting the pack and a {@link IllegalStateException} during
  222. * executing postReceiveHooks. This used to lead to exceptions with a
  223. * description of "invalid channel 101" on the client side. Make sure
  224. * clients receive the correct response about the too large pack on the
  225. * correct sideband.
  226. *
  227. * @throws Exception
  228. */
  229. @Test
  230. public void testUnpackErrorWithSubsequentExceptionInPostReceiveHook()
  231. throws Exception {
  232. final TestRepository client = createTestRepository();
  233. final RevBlob Q_txt = client
  234. .blob("some blob content to measure pack size");
  235. final RevCommit Q = client.commit().add("Q", Q_txt).create();
  236. final Repository clientRepo = client.getRepository();
  237. final String srvBranchName = Constants.R_HEADS + "new.branch";
  238. Transport t;
  239. // this maxPackSize leads to an unPackError
  240. maxPackSize = 400;
  241. // this PostReceiveHook when called after an unsuccesfull unpack will
  242. // lead to an IllegalStateException
  243. postHook = new PostReceiveHook() {
  244. public void onPostReceive(ReceivePack rp,
  245. Collection<ReceiveCommand> commands) {
  246. // the maxPackSize setting caused that the packfile couldn't be
  247. // saved to disk. Calling getPackSize() now will lead to a
  248. // IllegalStateException.
  249. rp.getPackSize();
  250. }
  251. };
  252. t = Transport.open(clientRepo, srvURI);
  253. try {
  254. RemoteRefUpdate update = new RemoteRefUpdate(clientRepo, Q.name(),
  255. srvBranchName, false, null, null);
  256. try {
  257. t.push(NullProgressMonitor.INSTANCE,
  258. Collections.singleton(update));
  259. fail("should not reach this line");
  260. } catch (Exception e) {
  261. assertTrue(e instanceof TooLargePackException);
  262. }
  263. } finally {
  264. t.close();
  265. }
  266. }
  267. }