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_PRAGMA;
48  import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
49  import static org.junit.Assert.assertEquals;
50  import static org.junit.Assert.assertFalse;
51  import static org.junit.Assert.assertNotNull;
52  import static org.junit.Assert.assertTrue;
53  import static org.junit.Assert.fail;
54  
55  import java.io.File;
56  import java.io.IOException;
57  import java.net.URI;
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.DefaultServlet;
64  import org.eclipse.jetty.servlet.ServletContextHandler;
65  import org.eclipse.jetty.servlet.ServletHolder;
66  import org.eclipse.jgit.errors.NotSupportedException;
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 DumbClientDumbServerTest 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 DumbClientDumbServerTest(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 File srcGit = src.getRepository().getDirectory();
119 		final URI base = srcGit.getParentFile().toURI();
120 
121 		ServletContextHandler app = server.addContext("/git");
122 		app.setResourceBase(base.toString());
123 		ServletHolder holder = app.addServlet(DefaultServlet.class, "/");
124 		// The tmp directory is symlinked on OS X
125 		holder.setInitParameter("aliases", "true");
126 		server.setUp();
127 
128 		remoteRepository = src.getRepository();
129 		remoteURI = toURIish(app, srcGit.getName());
130 
131 		A_txt = src.blob("A");
132 		A = src.commit().add("A_txt", A_txt).create();
133 		B = src.commit().parent(A).add("A_txt", "C").add("B", "B").create();
134 		src.update(master, B);
135 	}
136 
137 	@Test
138 	public void testListRemote() throws IOException {
139 		Repository dst = createBareRepository();
140 
141 		assertEquals("http", remoteURI.getScheme());
142 
143 		Map<String, Ref> map;
144 		try (Transport t = Transport.open(dst, remoteURI)) {
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(1, info.getParameters().size());
174 		assertEquals("git-upload-pack", 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("application/x-git-upload-pack-advertisement, */*", info
180 				.getRequestHeader(HDR_ACCEPT));
181 		assertEquals(200, info.getStatus());
182 
183 		AccessEvent head = requests.get(1);
184 		assertEquals("GET", head.getMethod());
185 		assertEquals(join(remoteURI, "HEAD"), head.getPath());
186 		assertEquals(0, head.getParameters().size());
187 		assertEquals("no-cache", head.getRequestHeader(HDR_PRAGMA));
188 		assertNotNull("has user-agent", head.getRequestHeader(HDR_USER_AGENT));
189 		assertTrue("is jgit agent", head.getRequestHeader(HDR_USER_AGENT)
190 				.startsWith("JGit/"));
191 		assertEquals(200, head.getStatus());
192 	}
193 
194 	@Test
195 	public void testInitialClone_Loose() throws Exception {
196 		Repository dst = createBareRepository();
197 		assertFalse(dst.hasObject(A_txt));
198 
199 		try (Transport t = Transport.open(dst, remoteURI)) {
200 			t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
201 		}
202 
203 		assertTrue(dst.hasObject(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 	}
213 
214 	@Test
215 	public void testInitialClone_Packed() throws Exception {
216 		new TestRepository<>(remoteRepository).packAndPrune();
217 
218 		Repository dst = createBareRepository();
219 		assertFalse(dst.hasObject(A_txt));
220 
221 		try (Transport t = Transport.open(dst, remoteURI)) {
222 			t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
223 		}
224 
225 		assertTrue(dst.hasObject(A_txt));
226 		assertEquals(B, dst.exactRef(master).getObjectId());
227 		fsck(dst, B);
228 
229 		List<AccessEvent> req;
230 		AccessEvent event;
231 
232 		req = getRequests(loose(remoteURI, B));
233 		assertEquals(1, req.size());
234 		event = req.get(0);
235 		assertEquals("GET", event.getMethod());
236 		assertEquals(0, event.getParameters().size());
237 		assertEquals(404, event.getStatus());
238 
239 		req = getRequests(join(remoteURI, "objects/info/packs"));
240 		assertEquals(1, req.size());
241 		event = req.get(0);
242 		assertEquals("GET", event.getMethod());
243 		assertEquals(0, event.getParameters().size());
244 		assertEquals("no-cache", event.getRequestHeader(HDR_PRAGMA));
245 		assertNotNull("has user-agent", event.getRequestHeader(HDR_USER_AGENT));
246 		assertTrue("is jgit agent", event.getRequestHeader(HDR_USER_AGENT)
247 				.startsWith("JGit/"));
248 		assertEquals(200, event.getStatus());
249 	}
250 
251 	@Test
252 	public void testPushNotSupported() throws Exception {
253 		final TestRepository src = createTestRepository();
254 		final RevCommit Q = src.commit().create();
255 		final Repository db = src.getRepository();
256 
257 		try (Transport t = Transport.open(db, remoteURI)) {
258 			try {
259 				t.push(NullProgressMonitor.INSTANCE, push(src, Q));
260 				fail("push incorrectly completed against a dumb server");
261 			} catch (NotSupportedException nse) {
262 				String exp = "remote does not support smart HTTP push";
263 				assertEquals(exp, nse.getMessage());
264 			}
265 		}
266 	}
267 }