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 	@Override
118 	@Before
119 	public void setUp() throws Exception {
120 		super.setUp();
121 
122 		final TestRepository<Repository> src = createTestRepository();
123 		final String srcName = src.getRepository().getDirectory().getName();
124 
125 		ServletContextHandler app = server.addContext("/git");
126 		GitServlet gs = new GitServlet();
127 		gs.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
128 			@Override
129 			public Repository open(HttpServletRequest req, String name)
130 					throws RepositoryNotFoundException,
131 					ServiceNotEnabledException {
132 				if (!name.equals(srcName))
133 					throw new RepositoryNotFoundException(name);
134 
135 				final Repository db = src.getRepository();
136 				db.incrementOpen();
137 				return db;
138 			}
139 		});
140 		app.addServlet(new ServletHolder(gs), "/*");
141 
142 		server.setUp();
143 
144 		remoteRepository = src.getRepository();
145 		remoteURI = toURIish(app, srcName);
146 
147 		A_txt = src.blob("A");
148 		A = src.commit().add("A_txt", A_txt).create();
149 		B = src.commit().parent(A).add("A_txt", "C").add("B", "B").create();
150 		src.update(master, B);
151 	}
152 
153 	@Test
154 	public void testListRemote() throws IOException {
155 		Repository dst = createBareRepository();
156 
157 		assertEquals("http", remoteURI.getScheme());
158 
159 		Map<String, Ref> map;
160 		Transport t = Transport.open(dst, remoteURI);
161 		((TransportHttp) t).setUseSmartHttp(false);
162 		try {
163 			// I didn't make up these public interface names, I just
164 			// approved them for inclusion into the code base. Sorry.
165 			// --spearce
166 			//
167 			assertTrue("isa TransportHttp", t instanceof TransportHttp);
168 			assertTrue("isa HttpTransport", t instanceof HttpTransport);
169 
170 			FetchConnection c = t.openFetch();
171 			try {
172 				map = c.getRefsMap();
173 			} finally {
174 				c.close();
175 			}
176 		} finally {
177 			t.close();
178 		}
179 
180 		assertNotNull("have map of refs", map);
181 		assertEquals(2, map.size());
182 
183 		assertNotNull("has " + master, map.get(master));
184 		assertEquals(B, map.get(master).getObjectId());
185 
186 		assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
187 		assertEquals(B, map.get(Constants.HEAD).getObjectId());
188 
189 		List<AccessEvent> requests = getRequests();
190 		assertEquals(2, requests.size());
191 		assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());
192 
193 		AccessEvent info = requests.get(0);
194 		assertEquals("GET", info.getMethod());
195 		assertEquals(join(remoteURI, "info/refs"), info.getPath());
196 		assertEquals(0, info.getParameters().size());
197 		assertNull("no service parameter", info.getParameter("service"));
198 		assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
199 		assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
200 		assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT)
201 				.startsWith("JGit/"));
202 		assertEquals("*/*", info.getRequestHeader(HDR_ACCEPT));
203 		assertEquals(200, info.getStatus());
204 		assertEquals("text/plain;charset=utf-8",
205 				info
206 				.getResponseHeader(HDR_CONTENT_TYPE));
207 
208 		AccessEvent head = requests.get(1);
209 		assertEquals("GET", head.getMethod());
210 		assertEquals(join(remoteURI, "HEAD"), head.getPath());
211 		assertEquals(0, head.getParameters().size());
212 		assertEquals(200, head.getStatus());
213 		assertEquals("text/plain", head.getResponseHeader(HDR_CONTENT_TYPE));
214 	}
215 
216 	@Test
217 	public void testInitialClone_Small() throws Exception {
218 		Repository dst = createBareRepository();
219 		assertFalse(dst.hasObject(A_txt));
220 
221 		Transport t = Transport.open(dst, remoteURI);
222 		((TransportHttp) t).setUseSmartHttp(false);
223 		try {
224 			t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
225 		} finally {
226 			t.close();
227 		}
228 
229 		assertTrue(dst.hasObject(A_txt));
230 		assertEquals(B, dst.exactRef(master).getObjectId());
231 		fsck(dst, B);
232 
233 		List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
234 		assertEquals(1, loose.size());
235 		assertEquals("GET", loose.get(0).getMethod());
236 		assertEquals(0, loose.get(0).getParameters().size());
237 		assertEquals(200, loose.get(0).getStatus());
238 		assertEquals("application/x-git-loose-object", loose.get(0)
239 				.getResponseHeader(HDR_CONTENT_TYPE));
240 	}
241 
242 	@Test
243 	public void testInitialClone_Packed() throws Exception {
244 		new TestRepository<>(remoteRepository).packAndPrune();
245 
246 		Repository dst = createBareRepository();
247 		assertFalse(dst.hasObject(A_txt));
248 
249 		Transport t = Transport.open(dst, remoteURI);
250 		((TransportHttp) t).setUseSmartHttp(false);
251 		try {
252 			t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
253 		} finally {
254 			t.close();
255 		}
256 
257 		assertTrue(dst.hasObject(A_txt));
258 		assertEquals(B, dst.exactRef(master).getObjectId());
259 		fsck(dst, B);
260 
261 		List<AccessEvent> req;
262 
263 		req = getRequests(loose(remoteURI, B));
264 		assertEquals(1, req.size());
265 		assertEquals("GET", req.get(0).getMethod());
266 		assertEquals(0, req.get(0).getParameters().size());
267 		assertEquals(404, req.get(0).getStatus());
268 
269 		req = getRequests(join(remoteURI, "objects/info/packs"));
270 		assertEquals(1, req.size());
271 		assertEquals("GET", req.get(0).getMethod());
272 		assertEquals(0, req.get(0).getParameters().size());
273 		assertEquals(200, req.get(0).getStatus());
274 		assertEquals("text/plain;charset=utf-8",
275 				req.get(0).getResponseHeader(
276 				HDR_CONTENT_TYPE));
277 	}
278 
279 	@Test
280 	public void testPushNotSupported() throws Exception {
281 		final TestRepository src = createTestRepository();
282 		final RevCommit Q = src.commit().create();
283 		final Repository db = src.getRepository();
284 
285 		Transport t = Transport.open(db, remoteURI);
286 		((TransportHttp) t).setUseSmartHttp(false);
287 		try {
288 			try {
289 				t.push(NullProgressMonitor.INSTANCE, push(src, Q));
290 				fail("push incorrectly completed against a smart server");
291 			} catch (NotSupportedException nse) {
292 				String exp = "smart HTTP push disabled";
293 				assertEquals(exp, nse.getMessage());
294 			}
295 		} finally {
296 			t.close();
297 		}
298 	}
299 }