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.

HttpClientTests.java 12KB

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