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.

LfsServerTest.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2015, Matthias Sohn <matthias.sohnk@sap.com>
  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.lfs.server.fs;
  44. import static org.junit.Assert.assertEquals;
  45. import java.io.BufferedInputStream;
  46. import java.io.FileNotFoundException;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.nio.ByteBuffer;
  50. import java.nio.channels.Channels;
  51. import java.nio.channels.FileChannel;
  52. import java.nio.channels.ReadableByteChannel;
  53. import java.nio.file.Files;
  54. import java.nio.file.Path;
  55. import java.nio.file.Paths;
  56. import java.nio.file.StandardOpenOption;
  57. import java.security.DigestInputStream;
  58. import java.security.SecureRandom;
  59. import org.apache.http.HttpEntity;
  60. import org.apache.http.HttpResponse;
  61. import org.apache.http.StatusLine;
  62. import org.apache.http.client.ClientProtocolException;
  63. import org.apache.http.client.methods.CloseableHttpResponse;
  64. import org.apache.http.client.methods.HttpGet;
  65. import org.apache.http.client.methods.HttpPut;
  66. import org.apache.http.entity.ContentType;
  67. import org.apache.http.entity.InputStreamEntity;
  68. import org.apache.http.entity.StringEntity;
  69. import org.apache.http.impl.client.CloseableHttpClient;
  70. import org.apache.http.impl.client.HttpClientBuilder;
  71. import org.eclipse.jetty.servlet.ServletContextHandler;
  72. import org.eclipse.jetty.servlet.ServletHolder;
  73. import org.eclipse.jgit.junit.http.AppServer;
  74. import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
  75. import org.eclipse.jgit.lfs.lib.Constants;
  76. import org.eclipse.jgit.lfs.lib.LongObjectId;
  77. import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
  78. import org.eclipse.jgit.util.FileUtils;
  79. import org.junit.After;
  80. import org.junit.Before;
  81. @SuppressWarnings("restriction")
  82. public abstract class LfsServerTest {
  83. private static final long timeout = /* 10 sec */ 10 * 1000;
  84. protected static final int MiB = 1024 * 1024;
  85. /** In-memory application server; subclass must start. */
  86. protected AppServer server;
  87. private Path tmp;
  88. private Path dir;
  89. protected FileLfsRepository repository;
  90. protected FileLfsServlet servlet;
  91. public LfsServerTest() {
  92. super();
  93. }
  94. public Path getTempDirectory() {
  95. return tmp;
  96. }
  97. public Path getDir() {
  98. return dir;
  99. }
  100. @Before
  101. public void setup() throws Exception {
  102. tmp = Files.createTempDirectory("jgit_test_");
  103. server = new AppServer();
  104. ServletContextHandler app = server.addContext("/lfs");
  105. dir = Paths.get(tmp.toString(), "lfs");
  106. this.repository = new FileLfsRepository(null, dir);
  107. servlet = new FileLfsServlet(repository, timeout);
  108. app.addServlet(new ServletHolder(servlet), "/objects/*");
  109. server.setUp();
  110. }
  111. @After
  112. public void tearDown() throws Exception {
  113. server.tearDown();
  114. FileUtils.delete(tmp.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
  115. }
  116. protected AnyLongObjectId putContent(String s)
  117. throws IOException, ClientProtocolException {
  118. AnyLongObjectId id = LongObjectIdTestUtils.hash(s);
  119. return putContent(id, s);
  120. }
  121. protected AnyLongObjectId putContent(AnyLongObjectId id, String s)
  122. throws ClientProtocolException, IOException {
  123. try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
  124. HttpEntity entity = new StringEntity(s,
  125. ContentType.APPLICATION_OCTET_STREAM);
  126. String hexId = id.name();
  127. HttpPut request = new HttpPut(
  128. server.getURI() + "/lfs/objects/" + hexId);
  129. request.setEntity(entity);
  130. try (CloseableHttpResponse response = client.execute(request)) {
  131. StatusLine statusLine = response.getStatusLine();
  132. int status = statusLine.getStatusCode();
  133. if (status >= 400) {
  134. throw new RuntimeException("Status: " + status + ". "
  135. + statusLine.getReasonPhrase());
  136. }
  137. }
  138. return id;
  139. }
  140. }
  141. protected LongObjectId putContent(Path f)
  142. throws FileNotFoundException, IOException {
  143. try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
  144. LongObjectId id1, id2;
  145. String hexId1, hexId2;
  146. try (DigestInputStream in = new DigestInputStream(
  147. new BufferedInputStream(Files.newInputStream(f)),
  148. Constants.newMessageDigest())) {
  149. InputStreamEntity entity = new InputStreamEntity(in,
  150. Files.size(f), ContentType.APPLICATION_OCTET_STREAM);
  151. id1 = LongObjectIdTestUtils.hash(f);
  152. hexId1 = id1.name();
  153. HttpPut request = new HttpPut(
  154. server.getURI() + "/lfs/objects/" + hexId1);
  155. request.setEntity(entity);
  156. HttpResponse response = client.execute(request);
  157. checkResponseStatus(response);
  158. id2 = LongObjectId.fromRaw(in.getMessageDigest().digest());
  159. hexId2 = id2.name();
  160. assertEquals(hexId1, hexId2);
  161. }
  162. return id1;
  163. }
  164. }
  165. private void checkResponseStatus(HttpResponse response) {
  166. StatusLine statusLine = response.getStatusLine();
  167. int status = statusLine.getStatusCode();
  168. if (statusLine.getStatusCode() >= 400) {
  169. throw new RuntimeException("Status: " + status + " "
  170. + statusLine.getReasonPhrase());
  171. }
  172. assertEquals(200, status);
  173. }
  174. protected long getContent(AnyLongObjectId id, Path f) throws IOException {
  175. String hexId = id.name();
  176. return getContent(hexId, f);
  177. }
  178. protected long getContent(String hexId, Path f) throws IOException {
  179. try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
  180. HttpGet request = new HttpGet(
  181. server.getURI() + "/lfs/objects/" + hexId);
  182. HttpResponse response = client.execute(request);
  183. checkResponseStatus(response);
  184. HttpEntity entity = response.getEntity();
  185. long pos = 0;
  186. try (InputStream in = entity.getContent();
  187. ReadableByteChannel inChannel = Channels.newChannel(in);
  188. FileChannel outChannel = FileChannel.open(f,
  189. StandardOpenOption.CREATE_NEW,
  190. StandardOpenOption.WRITE)) {
  191. long transferred;
  192. do {
  193. transferred = outChannel.transferFrom(inChannel, pos, MiB);
  194. pos += transferred;
  195. } while (transferred > 0);
  196. }
  197. return pos;
  198. }
  199. }
  200. /**
  201. * Creates a file with random content, repeatedly writing a random string of
  202. * 4k length to the file until the file has at least the specified length.
  203. *
  204. * @param f
  205. * file to fill
  206. * @param size
  207. * size of the file to generate
  208. * @return length of the generated file in bytes
  209. * @throws IOException
  210. */
  211. protected long createPseudoRandomContentFile(Path f, long size)
  212. throws IOException {
  213. SecureRandom rnd = new SecureRandom();
  214. byte[] buf = new byte[4096];
  215. rnd.nextBytes(buf);
  216. ByteBuffer bytebuf = ByteBuffer.wrap(buf);
  217. try (FileChannel outChannel = FileChannel.open(f,
  218. StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
  219. long len = 0;
  220. do {
  221. len += outChannel.write(bytebuf);
  222. if (bytebuf.position() == 4096) {
  223. bytebuf.rewind();
  224. }
  225. } while (len < size);
  226. }
  227. return Files.size(f);
  228. }
  229. }