View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
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  
44  package org.eclipse.jgit.http.test;
45  
46  import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT;
47  import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_TYPE;
48  import static org.eclipse.jgit.util.HttpSupport.HDR_PRAGMA;
49  import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
50  import static org.junit.Assert.assertEquals;
51  import static org.junit.Assert.assertFalse;
52  import static org.junit.Assert.assertNotNull;
53  import static org.junit.Assert.assertNull;
54  import static org.junit.Assert.assertTrue;
55  import static org.junit.Assert.fail;
56  
57  import java.io.IOException;
58  import java.util.Arrays;
59  import java.util.Collection;
60  import java.util.List;
61  import java.util.Map;
62  
63  import javax.servlet.http.HttpServletRequest;
64  
65  import org.eclipse.jetty.servlet.ServletContextHandler;
66  import org.eclipse.jetty.servlet.ServletHolder;
67  import org.eclipse.jgit.errors.NotSupportedException;
68  import org.eclipse.jgit.errors.RepositoryNotFoundException;
69  import org.eclipse.jgit.http.server.GitServlet;
70  import org.eclipse.jgit.junit.TestRepository;
71  import org.eclipse.jgit.junit.http.AccessEvent;
72  import org.eclipse.jgit.junit.http.HttpTestCase;
73  import org.eclipse.jgit.lib.Constants;
74  import org.eclipse.jgit.lib.NullProgressMonitor;
75  import org.eclipse.jgit.lib.Ref;
76  import org.eclipse.jgit.lib.Repository;
77  import org.eclipse.jgit.revwalk.RevBlob;
78  import org.eclipse.jgit.revwalk.RevCommit;
79  import org.eclipse.jgit.transport.FetchConnection;
80  import org.eclipse.jgit.transport.HttpTransport;
81  import org.eclipse.jgit.transport.Transport;
82  import org.eclipse.jgit.transport.TransportHttp;
83  import org.eclipse.jgit.transport.URIish;
84  import org.eclipse.jgit.transport.http.HttpConnectionFactory;
85  import org.eclipse.jgit.transport.http.JDKHttpConnectionFactory;
86  import org.eclipse.jgit.transport.http.apache.HttpClientConnectionFactory;
87  import org.eclipse.jgit.transport.resolver.RepositoryResolver;
88  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
89  import org.junit.Before;
90  import org.junit.Test;
91  import org.junit.runner.RunWith;
92  import org.junit.runners.Parameterized;
93  import org.junit.runners.Parameterized.Parameters;
94  
95  @RunWith(Parameterized.class)
96  public class DumbClientSmartServerTest extends HttpTestCase {
97  	private Repository remoteRepository;
98  
99  	private URIish remoteURI;
100 
101 	private RevBlob A_txt;
102 
103 	private RevCommit A, B;
104 
105 	@Parameters
106 	public static Collection<Object[]> data() {
107 		// run all tests with both connection factories we have
108 		return Arrays.asList(new Object[][] {
109 				{ new JDKHttpConnectionFactory() },
110 				{ new HttpClientConnectionFactory() } });
111 	}
112 
113 	public DumbClientSmartServerTest(HttpConnectionFactory cf) {
114 		HttpTransport.setConnectionFactory(cf);
115 	}
116 
117 	@Before
118 	public void setUp() throws Exception {
119 		super.setUp();
120 
121 		final TestRepository<Repository> src = createTestRepository();
122 		final String srcName = src.getRepository().getDirectory().getName();
123 
124 		ServletContextHandler app = server.addContext("/git");
125 		GitServlet gs = new GitServlet();
126 		gs.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
127 			public Repository open(HttpServletRequest req, String name)
128 					throws RepositoryNotFoundException,
129 					ServiceNotEnabledException {
130 				if (!name.equals(srcName))
131 					throw new RepositoryNotFoundException(name);
132 
133 				final Repository db = src.getRepository();
134 				db.incrementOpen();
135 				return db;
136 			}
137 		});
138 		app.addServlet(new ServletHolder(gs), "/*");
139 
140 		server.setUp();
141 
142 		remoteRepository = src.getRepository();
143 		remoteURI = toURIish(app, srcName);
144 
145 		A_txt = src.blob("A");
146 		A = src.commit().add("A_txt", A_txt).create();
147 		B = src.commit().parent(A).add("A_txt", "C").add("B", "B").create();
148 		src.update(master, B);
149 	}
150 
151 	@Test
152 	public void testListRemote() throws IOException {
153 		Repository dst = createBareRepository();
154 
155 		assertEquals("http", remoteURI.getScheme());
156 
157 		Map<String, Ref> map;
158 		Transport t = Transport.open(dst, remoteURI);
159 		((TransportHttp) t).setUseSmartHttp(false);
160 		try {
161 			// I didn't make up these public interface names, I just
162 			// approved them for inclusion into the code base. Sorry.
163 			// --spearce
164 			//
165 			assertTrue("isa TransportHttp", t instanceof TransportHttp);
166 			assertTrue("isa HttpTransport", t instanceof HttpTransport);
167 
168 			FetchConnection c = t.openFetch();
169 			try {
170 				map = c.getRefsMap();
171 			} finally {
172 				c.close();
173 			}
174 		} finally {
175 			t.close();
176 		}
177 
178 		assertNotNull("have map of refs", map);
179 		assertEquals(2, map.size());
180 
181 		assertNotNull("has " + master, map.get(master));
182 		assertEquals(B, map.get(master).getObjectId());
183 
184 		assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
185 		assertEquals(B, map.get(Constants.HEAD).getObjectId());
186 
187 		List<AccessEvent> requests = getRequests();
188 		assertEquals(2, requests.size());
189 		assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());
190 
191 		AccessEvent info = requests.get(0);
192 		assertEquals("GET", info.getMethod());
193 		assertEquals(join(remoteURI, "info/refs"), info.getPath());
194 		assertEquals(0, info.getParameters().size());
195 		assertNull("no service parameter", info.getParameter("service"));
196 		assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
197 		assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
198 		assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT)
199 				.startsWith("JGit/"));
200 		assertEquals("*/*", info.getRequestHeader(HDR_ACCEPT));
201 		assertEquals(200, info.getStatus());
202 		assertEquals("text/plain; charset=UTF-8",
203 				info
204 				.getResponseHeader(HDR_CONTENT_TYPE));
205 
206 		AccessEvent head = requests.get(1);
207 		assertEquals("GET", head.getMethod());
208 		assertEquals(join(remoteURI, "HEAD"), head.getPath());
209 		assertEquals(0, head.getParameters().size());
210 		assertEquals(200, head.getStatus());
211 		assertEquals("text/plain", head.getResponseHeader(HDR_CONTENT_TYPE));
212 	}
213 
214 	@Test
215 	public void testInitialClone_Small() throws Exception {
216 		Repository dst = createBareRepository();
217 		assertFalse(dst.hasObject(A_txt));
218 
219 		Transport t = Transport.open(dst, remoteURI);
220 		((TransportHttp) t).setUseSmartHttp(false);
221 		try {
222 			t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
223 		} finally {
224 			t.close();
225 		}
226 
227 		assertTrue(dst.hasObject(A_txt));
228 		assertEquals(B, dst.exactRef(master).getObjectId());
229 		fsck(dst, B);
230 
231 		List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
232 		assertEquals(1, loose.size());
233 		assertEquals("GET", loose.get(0).getMethod());
234 		assertEquals(0, loose.get(0).getParameters().size());
235 		assertEquals(200, loose.get(0).getStatus());
236 		assertEquals("application/x-git-loose-object", loose.get(0)
237 				.getResponseHeader(HDR_CONTENT_TYPE));
238 	}
239 
240 	@Test
241 	public void testInitialClone_Packed() throws Exception {
242 		new TestRepository<Repository>(remoteRepository).packAndPrune();
243 
244 		Repository dst = createBareRepository();
245 		assertFalse(dst.hasObject(A_txt));
246 
247 		Transport t = Transport.open(dst, remoteURI);
248 		((TransportHttp) t).setUseSmartHttp(false);
249 		try {
250 			t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
251 		} finally {
252 			t.close();
253 		}
254 
255 		assertTrue(dst.hasObject(A_txt));
256 		assertEquals(B, dst.exactRef(master).getObjectId());
257 		fsck(dst, B);
258 
259 		List<AccessEvent> req;
260 
261 		req = getRequests(loose(remoteURI, B));
262 		assertEquals(1, req.size());
263 		assertEquals("GET", req.get(0).getMethod());
264 		assertEquals(0, req.get(0).getParameters().size());
265 		assertEquals(404, req.get(0).getStatus());
266 
267 		req = getRequests(join(remoteURI, "objects/info/packs"));
268 		assertEquals(1, req.size());
269 		assertEquals("GET", req.get(0).getMethod());
270 		assertEquals(0, req.get(0).getParameters().size());
271 		assertEquals(200, req.get(0).getStatus());
272 		assertEquals("text/plain; charset=UTF-8",
273 				req.get(0).getResponseHeader(
274 				HDR_CONTENT_TYPE));
275 	}
276 
277 	@Test
278 	public void testPushNotSupported() throws Exception {
279 		final TestRepository src = createTestRepository();
280 		final RevCommit Q = src.commit().create();
281 		final Repository db = src.getRepository();
282 
283 		Transport t = Transport.open(db, remoteURI);
284 		((TransportHttp) t).setUseSmartHttp(false);
285 		try {
286 			try {
287 				t.push(NullProgressMonitor.INSTANCE, push(src, Q));
288 				fail("push incorrectly completed against a smart server");
289 			} catch (NotSupportedException nse) {
290 				String exp = "smart HTTP push disabled";
291 				assertEquals(exp, nse.getMessage());
292 			}
293 		} finally {
294 			t.close();
295 		}
296 	}
297 }