You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (C) 2009-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. package org.eclipse.jgit.http.test;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import java.io.File;
  51. import java.net.URI;
  52. import java.util.List;
  53. import javax.servlet.http.HttpServletRequest;
  54. import org.eclipse.jetty.servlet.DefaultServlet;
  55. import org.eclipse.jetty.servlet.ServletContextHandler;
  56. import org.eclipse.jetty.servlet.ServletHolder;
  57. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  58. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  59. import org.eclipse.jgit.errors.TransportException;
  60. import org.eclipse.jgit.http.server.GitServlet;
  61. import org.eclipse.jgit.internal.JGitText;
  62. import org.eclipse.jgit.junit.TestRepository;
  63. import org.eclipse.jgit.junit.http.AccessEvent;
  64. import org.eclipse.jgit.junit.http.AppServer;
  65. import org.eclipse.jgit.junit.http.HttpTestCase;
  66. import org.eclipse.jgit.lib.Constants;
  67. import org.eclipse.jgit.lib.Ref;
  68. import org.eclipse.jgit.lib.RefUpdate;
  69. import org.eclipse.jgit.lib.Repository;
  70. import org.eclipse.jgit.revwalk.RevCommit;
  71. import org.eclipse.jgit.storage.file.FileBasedConfig;
  72. import org.eclipse.jgit.storage.file.FileRepository;
  73. import org.eclipse.jgit.transport.FetchConnection;
  74. import org.eclipse.jgit.transport.Transport;
  75. import org.eclipse.jgit.transport.URIish;
  76. import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
  77. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  78. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  79. import org.junit.Before;
  80. import org.junit.Test;
  81. public class HttpClientTests extends HttpTestCase {
  82. private TestRepository<FileRepository> remoteRepository;
  83. private URIish dumbAuthNoneURI;
  84. private URIish dumbAuthBasicURI;
  85. private URIish smartAuthNoneURI;
  86. private URIish smartAuthBasicURI;
  87. @Before
  88. public void setUp() throws Exception {
  89. super.setUp();
  90. remoteRepository = createTestRepository();
  91. remoteRepository.update(master, remoteRepository.commit().create());
  92. ServletContextHandler dNone = dumb("/dnone");
  93. ServletContextHandler dBasic = server.authBasic(dumb("/dbasic"));
  94. ServletContextHandler sNone = smart("/snone");
  95. ServletContextHandler sBasic = server.authBasic(smart("/sbasic"));
  96. server.setUp();
  97. final String srcName = nameOf(remoteRepository.getRepository());
  98. dumbAuthNoneURI = toURIish(dNone, srcName);
  99. dumbAuthBasicURI = toURIish(dBasic, srcName);
  100. smartAuthNoneURI = toURIish(sNone, srcName);
  101. smartAuthBasicURI = toURIish(sBasic, srcName);
  102. }
  103. private ServletContextHandler dumb(final String path) {
  104. final File srcGit = remoteRepository.getRepository().getDirectory();
  105. final URI base = srcGit.getParentFile().toURI();
  106. ServletContextHandler ctx = server.addContext(path);
  107. ctx.setResourceBase(base.toString());
  108. ctx.addServlet(DefaultServlet.class, "/");
  109. return ctx;
  110. }
  111. private ServletContextHandler smart(final String path) {
  112. GitServlet gs = new GitServlet();
  113. gs.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
  114. public Repository open(HttpServletRequest req, String name)
  115. throws RepositoryNotFoundException,
  116. ServiceNotEnabledException {
  117. final FileRepository db = remoteRepository.getRepository();
  118. if (!name.equals(nameOf(db)))
  119. throw new RepositoryNotFoundException(name);
  120. db.incrementOpen();
  121. return db;
  122. }
  123. });
  124. ServletContextHandler ctx = server.addContext(path);
  125. ctx.addServlet(new ServletHolder(gs), "/*");
  126. return ctx;
  127. }
  128. private static String nameOf(final FileRepository db) {
  129. return db.getDirectory().getName();
  130. }
  131. @Test
  132. public void testRepositoryNotFound_Dumb() throws Exception {
  133. URIish uri = toURIish("/dumb.none/not-found");
  134. Repository dst = createBareRepository();
  135. Transport t = Transport.open(dst, uri);
  136. try {
  137. try {
  138. t.openFetch();
  139. fail("connection opened to not found repository");
  140. } catch (NoRemoteRepositoryException err) {
  141. String exp = uri + ": " + uri
  142. + "/info/refs?service=git-upload-pack not found";
  143. assertEquals(exp, err.getMessage());
  144. }
  145. } finally {
  146. t.close();
  147. }
  148. }
  149. @Test
  150. public void testRepositoryNotFound_Smart() throws Exception {
  151. URIish uri = toURIish("/smart.none/not-found");
  152. Repository dst = createBareRepository();
  153. Transport t = Transport.open(dst, uri);
  154. try {
  155. try {
  156. t.openFetch();
  157. fail("connection opened to not found repository");
  158. } catch (NoRemoteRepositoryException err) {
  159. String exp = uri + ": " + uri
  160. + "/info/refs?service=git-upload-pack not found";
  161. assertEquals(exp, err.getMessage());
  162. }
  163. } finally {
  164. t.close();
  165. }
  166. }
  167. @Test
  168. public void testListRemote_Dumb_DetachedHEAD() throws Exception {
  169. Repository src = remoteRepository.getRepository();
  170. RefUpdate u = src.updateRef(Constants.HEAD, true);
  171. RevCommit Q = remoteRepository.commit().message("Q").create();
  172. u.setNewObjectId(Q);
  173. assertEquals(RefUpdate.Result.FORCED, u.forceUpdate());
  174. Repository dst = createBareRepository();
  175. Ref head;
  176. Transport t = Transport.open(dst, dumbAuthNoneURI);
  177. try {
  178. FetchConnection c = t.openFetch();
  179. try {
  180. head = c.getRef(Constants.HEAD);
  181. } finally {
  182. c.close();
  183. }
  184. } finally {
  185. t.close();
  186. }
  187. assertNotNull("has " + Constants.HEAD, head);
  188. assertEquals(Q, head.getObjectId());
  189. }
  190. @Test
  191. public void testListRemote_Dumb_NoHEAD() throws Exception {
  192. FileRepository src = remoteRepository.getRepository();
  193. File headref = new File(src.getDirectory(), Constants.HEAD);
  194. assertTrue("HEAD used to be present", headref.delete());
  195. assertFalse("HEAD is gone", headref.exists());
  196. Repository dst = createBareRepository();
  197. Ref head;
  198. Transport t = Transport.open(dst, dumbAuthNoneURI);
  199. try {
  200. FetchConnection c = t.openFetch();
  201. try {
  202. head = c.getRef(Constants.HEAD);
  203. } finally {
  204. c.close();
  205. }
  206. } finally {
  207. t.close();
  208. }
  209. assertNull("has no " + Constants.HEAD, head);
  210. }
  211. @Test
  212. public void testListRemote_Smart_DetachedHEAD() throws Exception {
  213. Repository src = remoteRepository.getRepository();
  214. RefUpdate u = src.updateRef(Constants.HEAD, true);
  215. RevCommit Q = remoteRepository.commit().message("Q").create();
  216. u.setNewObjectId(Q);
  217. assertEquals(RefUpdate.Result.FORCED, u.forceUpdate());
  218. Repository dst = createBareRepository();
  219. Ref head;
  220. Transport t = Transport.open(dst, smartAuthNoneURI);
  221. try {
  222. FetchConnection c = t.openFetch();
  223. try {
  224. head = c.getRef(Constants.HEAD);
  225. } finally {
  226. c.close();
  227. }
  228. } finally {
  229. t.close();
  230. }
  231. assertNotNull("has " + Constants.HEAD, head);
  232. assertEquals(Q, head.getObjectId());
  233. }
  234. @Test
  235. public void testListRemote_Smart_WithQueryParameters() throws Exception {
  236. URIish myURI = toURIish("/snone/do?r=1&p=test.git");
  237. Repository dst = createBareRepository();
  238. Transport t = Transport.open(dst, myURI);
  239. try {
  240. try {
  241. t.openFetch();
  242. fail("test did not fail to find repository as expected");
  243. } catch (NoRemoteRepositoryException err) {
  244. // expected
  245. }
  246. } finally {
  247. t.close();
  248. }
  249. List<AccessEvent> requests = getRequests();
  250. assertEquals(1, requests.size());
  251. AccessEvent info = requests.get(0);
  252. assertEquals("GET", info.getMethod());
  253. assertEquals("/snone/do", info.getPath());
  254. assertEquals(3, info.getParameters().size());
  255. assertEquals("1", info.getParameter("r"));
  256. assertEquals("test.git/info/refs", info.getParameter("p"));
  257. assertEquals("git-upload-pack", info.getParameter("service"));
  258. assertEquals(404, info.getStatus());
  259. }
  260. @Test
  261. public void testListRemote_Dumb_NeedsAuth() throws Exception {
  262. Repository dst = createBareRepository();
  263. Transport t = Transport.open(dst, dumbAuthBasicURI);
  264. try {
  265. try {
  266. t.openFetch();
  267. fail("connection opened even info/refs needs auth basic");
  268. } catch (TransportException err) {
  269. String exp = dumbAuthBasicURI + ": "
  270. + JGitText.get().notAuthorized;
  271. assertEquals(exp, err.getMessage());
  272. }
  273. } finally {
  274. t.close();
  275. }
  276. }
  277. @Test
  278. public void testListRemote_Dumb_Auth() throws Exception {
  279. Repository dst = createBareRepository();
  280. Transport t = Transport.open(dst, dumbAuthBasicURI);
  281. t.setCredentialsProvider(new UsernamePasswordCredentialsProvider(
  282. AppServer.username, AppServer.password));
  283. try {
  284. t.openFetch();
  285. } finally {
  286. t.close();
  287. }
  288. t = Transport.open(dst, dumbAuthBasicURI);
  289. t.setCredentialsProvider(new UsernamePasswordCredentialsProvider(
  290. AppServer.username, ""));
  291. try {
  292. t.openFetch();
  293. fail("connection opened even info/refs needs auth basic and we provide wrong password");
  294. } catch (TransportException err) {
  295. String exp = dumbAuthBasicURI + ": "
  296. + JGitText.get().notAuthorized;
  297. assertEquals(exp, err.getMessage());
  298. } finally {
  299. t.close();
  300. }
  301. }
  302. @Test
  303. public void testListRemote_Smart_UploadPackNeedsAuth() throws Exception {
  304. Repository dst = createBareRepository();
  305. Transport t = Transport.open(dst, smartAuthBasicURI);
  306. try {
  307. try {
  308. t.openFetch();
  309. fail("connection opened even though service disabled");
  310. } catch (TransportException err) {
  311. String exp = smartAuthBasicURI + ": "
  312. + JGitText.get().notAuthorized;
  313. assertEquals(exp, err.getMessage());
  314. }
  315. } finally {
  316. t.close();
  317. }
  318. }
  319. @Test
  320. public void testListRemote_Smart_UploadPackDisabled() throws Exception {
  321. FileRepository src = remoteRepository.getRepository();
  322. final FileBasedConfig cfg = src.getConfig();
  323. cfg.setBoolean("http", null, "uploadpack", false);
  324. cfg.save();
  325. Repository dst = createBareRepository();
  326. Transport t = Transport.open(dst, smartAuthNoneURI);
  327. try {
  328. try {
  329. t.openFetch();
  330. fail("connection opened even though service disabled");
  331. } catch (TransportException err) {
  332. String exp = smartAuthNoneURI + ": Git access forbidden";
  333. assertEquals(exp, err.getMessage());
  334. }
  335. } finally {
  336. t.close();
  337. }
  338. }
  339. }