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 	@Before
113 	public void setUp() throws Exception {
114 		super.setUp();
115 
116 		final TestRepository<Repository> src = createTestRepository();
117 		final File srcGit = src.getRepository().getDirectory();
118 		final URI base = srcGit.getParentFile().toURI();
119 
120 		ServletContextHandler app = server.addContext("/git");
121 		app.setResourceBase(base.toString());
122 		ServletHolder holder = app.addServlet(DefaultServlet.class, "/");
123 		// The tmp directory is symlinked on OS X
124 		holder.setInitParameter("aliases", "true");
125 		server.setUp();
126 
127 		remoteRepository = src.getRepository();
128 		remoteURI = toURIish(app, srcGit.getName());
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 			// I didn't make up these public interface names, I just
145 			// approved them for inclusion into the code base. Sorry.
146 			// --spearce
147 			//
148 			assertTrue("isa TransportHttp", t instanceof TransportHttp);
149 			assertTrue("isa HttpTransport", t instanceof HttpTransport);
150 
151 			try (FetchConnection c = t.openFetch()) {
152 				map = c.getRefsMap();
153 			}
154 		}
155 
156 		assertNotNull("have map of refs", map);
157 		assertEquals(2, map.size());
158 
159 		assertNotNull("has " + master, map.get(master));
160 		assertEquals(B, map.get(master).getObjectId());
161 
162 		assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
163 		assertEquals(B, map.get(Constants.HEAD).getObjectId());
164 
165 		List<AccessEvent> requests = getRequests();
166 		assertEquals(2, requests.size());
167 		assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());
168 
169 		AccessEvent info = requests.get(0);
170 		assertEquals("GET", info.getMethod());
171 		assertEquals(join(remoteURI, "info/refs"), info.getPath());
172 		assertEquals(1, info.getParameters().size());
173 		assertEquals("git-upload-pack", info.getParameter("service"));
174 		assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
175 		assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
176 		assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT)
177 				.startsWith("JGit/"));
178 		assertEquals("application/x-git-upload-pack-advertisement, */*", info
179 				.getRequestHeader(HDR_ACCEPT));
180 		assertEquals(200, info.getStatus());
181 
182 		AccessEvent head = requests.get(1);
183 		assertEquals("GET", head.getMethod());
184 		assertEquals(join(remoteURI, "HEAD"), head.getPath());
185 		assertEquals(0, head.getParameters().size());
186 		assertEquals("no-cache", head.getRequestHeader(HDR_PRAGMA));
187 		assertNotNull("has user-agent", head.getRequestHeader(HDR_USER_AGENT));
188 		assertTrue("is jgit agent", head.getRequestHeader(HDR_USER_AGENT)
189 				.startsWith("JGit/"));
190 		assertEquals(200, head.getStatus());
191 	}
192 
193 	@Test
194 	public void testInitialClone_Loose() throws Exception {
195 		Repository dst = createBareRepository();
196 		assertFalse(dst.hasObject(A_txt));
197 
198 		try (Transport t = Transport.open(dst, remoteURI)) {
199 			t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
200 		}
201 
202 		assertTrue(dst.hasObject(A_txt));
203 		assertEquals(B, dst.exactRef(master).getObjectId());
204 		fsck(dst, B);
205 
206 		List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
207 		assertEquals(1, loose.size());
208 		assertEquals("GET", loose.get(0).getMethod());
209 		assertEquals(0, loose.get(0).getParameters().size());
210 		assertEquals(200, loose.get(0).getStatus());
211 	}
212 
213 	@Test
214 	public void testInitialClone_Packed() throws Exception {
215 		new TestRepository<Repository>(remoteRepository).packAndPrune();
216 
217 		Repository dst = createBareRepository();
218 		assertFalse(dst.hasObject(A_txt));
219 
220 		try (Transport t = Transport.open(dst, remoteURI)) {
221 			t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
222 		}
223 
224 		assertTrue(dst.hasObject(A_txt));
225 		assertEquals(B, dst.exactRef(master).getObjectId());
226 		fsck(dst, B);
227 
228 		List<AccessEvent> req;
229 		AccessEvent event;
230 
231 		req = getRequests(loose(remoteURI, B));
232 		assertEquals(1, req.size());
233 		event = req.get(0);
234 		assertEquals("GET", event.getMethod());
235 		assertEquals(0, event.getParameters().size());
236 		assertEquals(404, event.getStatus());
237 
238 		req = getRequests(join(remoteURI, "objects/info/packs"));
239 		assertEquals(1, req.size());
240 		event = req.get(0);
241 		assertEquals("GET", event.getMethod());
242 		assertEquals(0, event.getParameters().size());
243 		assertEquals("no-cache", event.getRequestHeader(HDR_PRAGMA));
244 		assertNotNull("has user-agent", event.getRequestHeader(HDR_USER_AGENT));
245 		assertTrue("is jgit agent", event.getRequestHeader(HDR_USER_AGENT)
246 				.startsWith("JGit/"));
247 		assertEquals(200, event.getStatus());
248 	}
249 
250 	@Test
251 	public void testPushNotSupported() throws Exception {
252 		final TestRepository src = createTestRepository();
253 		final RevCommit Q = src.commit().create();
254 		final Repository db = src.getRepository();
255 
256 		try (Transport t = Transport.open(db, remoteURI)) {
257 			try {
258 				t.push(NullProgressMonitor.INSTANCE, push(src, Q));
259 				fail("push incorrectly completed against a dumb server");
260 			} catch (NotSupportedException nse) {
261 				String exp = "remote does not support smart HTTP push";
262 				assertEquals(exp, nse.getMessage());
263 			}
264 		}
265 	}
266 }