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