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

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