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