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

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