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.List;
59  import java.util.Map;
60  
61  import org.eclipse.jetty.servlet.DefaultServlet;
62  import org.eclipse.jetty.servlet.ServletContextHandler;
63  import org.eclipse.jetty.servlet.ServletHolder;
64  import org.eclipse.jgit.errors.NotSupportedException;
65  import org.eclipse.jgit.junit.TestRepository;
66  import org.eclipse.jgit.junit.http.AccessEvent;
67  import org.eclipse.jgit.lib.Constants;
68  import org.eclipse.jgit.lib.NullProgressMonitor;
69  import org.eclipse.jgit.lib.Ref;
70  import org.eclipse.jgit.lib.Repository;
71  import org.eclipse.jgit.revwalk.RevBlob;
72  import org.eclipse.jgit.revwalk.RevCommit;
73  import org.eclipse.jgit.transport.FetchConnection;
74  import org.eclipse.jgit.transport.HttpTransport;
75  import org.eclipse.jgit.transport.Transport;
76  import org.eclipse.jgit.transport.TransportHttp;
77  import org.eclipse.jgit.transport.URIish;
78  import org.eclipse.jgit.transport.http.HttpConnectionFactory;
79  import org.junit.Before;
80  import org.junit.Test;
81  
82  public class DumbClientDumbServerTest extends AllFactoriesHttpTestCase {
83  	private Repository remoteRepository;
84  
85  	private URIish remoteURI;
86  
87  	private RevBlob A_txt;
88  
89  	private RevCommit A, B;
90  
91  	public DumbClientDumbServerTest(HttpConnectionFactory cf) {
92  		super(cf);
93  	}
94  
95  	@Override
96  	@Before
97  	public void setUp() throws Exception {
98  		super.setUp();
99  
100 		final TestRepository<Repository> src = createTestRepository();
101 		final File srcGit = src.getRepository().getDirectory();
102 		final URI base = srcGit.getParentFile().toURI();
103 
104 		ServletContextHandler app = server.addContext("/git");
105 		app.setResourceBase(base.toString());
106 		ServletHolder holder = app.addServlet(DefaultServlet.class, "/");
107 		// The tmp directory is symlinked on OS X
108 		holder.setInitParameter("aliases", "true");
109 		server.setUp();
110 
111 		remoteRepository = src.getRepository();
112 		remoteURI = toURIish(app, srcGit.getName());
113 
114 		A_txt = src.blob("A");
115 		A = src.commit().add("A_txt", A_txt).create();
116 		B = src.commit().parent(A).add("A_txt", "C").add("B", "B").create();
117 		src.update(master, B);
118 	}
119 
120 	@Test
121 	public void testListRemote() throws IOException {
122 		Repository dst = createBareRepository();
123 
124 		assertEquals("http", remoteURI.getScheme());
125 
126 		Map<String, Ref> map;
127 		try (Transport t = Transport.open(dst, remoteURI)) {
128 			// I didn't make up these public interface names, I just
129 			// approved them for inclusion into the code base. Sorry.
130 			// --spearce
131 			//
132 			assertTrue("isa TransportHttp", t instanceof TransportHttp);
133 			assertTrue("isa HttpTransport", t instanceof HttpTransport);
134 
135 			try (FetchConnection c = t.openFetch()) {
136 				map = c.getRefsMap();
137 			}
138 		}
139 
140 		assertNotNull("have map of refs", map);
141 		assertEquals(2, map.size());
142 
143 		assertNotNull("has " + master, map.get(master));
144 		assertEquals(B, map.get(master).getObjectId());
145 
146 		assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
147 		assertEquals(B, map.get(Constants.HEAD).getObjectId());
148 
149 		List<AccessEvent> requests = getRequests();
150 		assertEquals(2, requests.size());
151 		assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());
152 
153 		AccessEvent info = requests.get(0);
154 		assertEquals("GET", info.getMethod());
155 		assertEquals(join(remoteURI, "info/refs"), info.getPath());
156 		assertEquals(1, info.getParameters().size());
157 		assertEquals("git-upload-pack", info.getParameter("service"));
158 		assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
159 		assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
160 		assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT)
161 				.startsWith("JGit/"));
162 		assertEquals("application/x-git-upload-pack-advertisement, */*", info
163 				.getRequestHeader(HDR_ACCEPT));
164 		assertEquals(200, info.getStatus());
165 
166 		AccessEvent head = requests.get(1);
167 		assertEquals("GET", head.getMethod());
168 		assertEquals(join(remoteURI, "HEAD"), head.getPath());
169 		assertEquals(0, head.getParameters().size());
170 		assertEquals("no-cache", head.getRequestHeader(HDR_PRAGMA));
171 		assertNotNull("has user-agent", head.getRequestHeader(HDR_USER_AGENT));
172 		assertTrue("is jgit agent", head.getRequestHeader(HDR_USER_AGENT)
173 				.startsWith("JGit/"));
174 		assertEquals(200, head.getStatus());
175 	}
176 
177 	@Test
178 	public void testInitialClone_Loose() throws Exception {
179 		Repository dst = createBareRepository();
180 		assertFalse(dst.getObjectDatabase().has(A_txt));
181 
182 		try (Transport t = Transport.open(dst, remoteURI)) {
183 			t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
184 		}
185 
186 		assertTrue(dst.getObjectDatabase().has(A_txt));
187 		assertEquals(B, dst.exactRef(master).getObjectId());
188 		fsck(dst, B);
189 
190 		List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
191 		assertEquals(1, loose.size());
192 		assertEquals("GET", loose.get(0).getMethod());
193 		assertEquals(0, loose.get(0).getParameters().size());
194 		assertEquals(200, loose.get(0).getStatus());
195 	}
196 
197 	@Test
198 	public void testInitialClone_Packed() throws Exception {
199 		try (TestRepository<Repository> tr = new TestRepository<>(
200 				remoteRepository)) {
201 			tr.packAndPrune();
202 		}
203 
204 		Repository dst = createBareRepository();
205 		assertFalse(dst.getObjectDatabase().has(A_txt));
206 
207 		try (Transport t = Transport.open(dst, remoteURI)) {
208 			t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
209 		}
210 
211 		assertTrue(dst.getObjectDatabase().has(A_txt));
212 		assertEquals(B, dst.exactRef(master).getObjectId());
213 		fsck(dst, B);
214 
215 		List<AccessEvent> req;
216 		AccessEvent event;
217 
218 		req = getRequests(loose(remoteURI, B));
219 		assertEquals(1, req.size());
220 		event = req.get(0);
221 		assertEquals("GET", event.getMethod());
222 		assertEquals(0, event.getParameters().size());
223 		assertEquals(404, event.getStatus());
224 
225 		req = getRequests(join(remoteURI, "objects/info/packs"));
226 		assertEquals(1, req.size());
227 		event = req.get(0);
228 		assertEquals("GET", event.getMethod());
229 		assertEquals(0, event.getParameters().size());
230 		assertEquals("no-cache", event.getRequestHeader(HDR_PRAGMA));
231 		assertNotNull("has user-agent", event.getRequestHeader(HDR_USER_AGENT));
232 		assertTrue("is jgit agent", event.getRequestHeader(HDR_USER_AGENT)
233 				.startsWith("JGit/"));
234 		assertEquals(200, event.getStatus());
235 	}
236 
237 	@Test
238 	public void testPushNotSupported() throws Exception {
239 		final TestRepository src = createTestRepository();
240 		final RevCommit Q = src.commit().create();
241 		final Repository db = src.getRepository();
242 
243 		try (Transport t = Transport.open(db, remoteURI)) {
244 			try {
245 				t.push(NullProgressMonitor.INSTANCE, push(src, Q));
246 				fail("push incorrectly completed against a dumb server");
247 			} catch (NotSupportedException nse) {
248 				String exp = "remote does not support smart HTTP push";
249 				assertEquals(exp, nse.getMessage());
250 			}
251 		}
252 	}
253 }