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.8KB

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