|
@@ -1,10 +1,7 @@
|
|
|
package org.cryptomator.webdav.jackrabbit;
|
|
|
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
import java.net.URI;
|
|
|
import java.net.URISyntaxException;
|
|
|
import java.net.URL;
|
|
@@ -15,6 +12,13 @@ import java.util.Collection;
|
|
|
import java.util.Random;
|
|
|
import java.util.concurrent.ForkJoinTask;
|
|
|
|
|
|
+import org.apache.commons.httpclient.HttpClient;
|
|
|
+import org.apache.commons.httpclient.HttpMethod;
|
|
|
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
|
|
+import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
|
|
|
+import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
|
|
|
+import org.apache.commons.httpclient.methods.GetMethod;
|
|
|
+import org.apache.commons.httpclient.methods.PutMethod;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
import org.cryptomator.crypto.aes256.Aes256Cryptor;
|
|
@@ -31,27 +35,34 @@ public class RangeRequestTest {
|
|
|
|
|
|
private static final Aes256Cryptor CRYPTOR = new Aes256Cryptor();
|
|
|
private static final WebDavServer SERVER = new WebDavServer();
|
|
|
+ private static final File TMP_VAULT = Files.createTempDir();
|
|
|
+ private static ServletLifeCycleAdapter SERVLET;
|
|
|
+ private static URI VAULT_BASE_URI;
|
|
|
|
|
|
@BeforeClass
|
|
|
- public static void startServer() {
|
|
|
+ public static void startServer() throws URISyntaxException {
|
|
|
SERVER.start();
|
|
|
+ SERVLET = SERVER.createServlet(TMP_VAULT.toPath(), CRYPTOR, new ArrayList<String>(), new ArrayList<String>(), "JUnitTestVault");
|
|
|
+ SERVLET.start();
|
|
|
+ VAULT_BASE_URI = new URI("http", SERVLET.getServletUri().getSchemeSpecificPart() + "/", null);
|
|
|
+ Assert.assertTrue(SERVLET.isRunning());
|
|
|
+ Assert.assertNotNull(VAULT_BASE_URI);
|
|
|
}
|
|
|
|
|
|
@AfterClass
|
|
|
public static void stopServer() {
|
|
|
+ SERVLET.stop();
|
|
|
SERVER.stop();
|
|
|
+ FileUtils.deleteQuietly(TMP_VAULT);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testAsyncRangeRequests() throws IOException, URISyntaxException {
|
|
|
- final File tmpVault = Files.createTempDir();
|
|
|
- final ServletLifeCycleAdapter servlet = SERVER.createServlet(tmpVault.toPath(), CRYPTOR, new ArrayList<String>(), new ArrayList<String>(), "JUnitTestVault");
|
|
|
- final boolean started = servlet.start();
|
|
|
- final URI vaultBaseUri = new URI("http", servlet.getServletUri().getSchemeSpecificPart() + "/", null);
|
|
|
- final URL testResourceUrl = new URL(vaultBaseUri.toURL(), "testfile.txt");
|
|
|
+ final URL testResourceUrl = new URL(VAULT_BASE_URI.toURL(), "asyncRangeRequestTestFile.txt");
|
|
|
|
|
|
- Assert.assertTrue(started);
|
|
|
- Assert.assertNotNull(vaultBaseUri);
|
|
|
+ final MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
|
|
|
+ cm.getParams().setDefaultMaxConnectionsPerHost(50);
|
|
|
+ final HttpClient client = new HttpClient(cm);
|
|
|
|
|
|
// prepare 8MiB test data:
|
|
|
final byte[] plaintextData = new byte[2097152 * Integer.BYTES];
|
|
@@ -59,37 +70,32 @@ public class RangeRequestTest {
|
|
|
for (int i = 0; i < 2097152; i++) {
|
|
|
bbIn.putInt(i);
|
|
|
}
|
|
|
- final InputStream plaintextIn = new ByteArrayInputStream(plaintextData);
|
|
|
|
|
|
// put request:
|
|
|
- final HttpURLConnection putConn = (HttpURLConnection) testResourceUrl.openConnection();
|
|
|
- putConn.setDoOutput(true);
|
|
|
- putConn.setRequestMethod("PUT");
|
|
|
- IOUtils.copy(plaintextIn, putConn.getOutputStream());
|
|
|
- putConn.getOutputStream().close();
|
|
|
- final int putResponse = putConn.getResponseCode();
|
|
|
- putConn.disconnect();
|
|
|
+ final EntityEnclosingMethod putMethod = new PutMethod(testResourceUrl.toString());
|
|
|
+ putMethod.setRequestEntity(new ByteArrayRequestEntity(plaintextData));
|
|
|
+ final int putResponse = client.executeMethod(putMethod);
|
|
|
+ putMethod.releaseConnection();
|
|
|
Assert.assertEquals(201, putResponse);
|
|
|
|
|
|
// multiple async range requests:
|
|
|
final Collection<ForkJoinTask<?>> tasks = new ArrayList<>();
|
|
|
final Random generator = new Random(System.currentTimeMillis());
|
|
|
- for (int i = 0; i < 100; i++) {
|
|
|
+ for (int i = 0; i < 200; i++) {
|
|
|
final int pos1 = generator.nextInt(plaintextData.length);
|
|
|
final int pos2 = generator.nextInt(plaintextData.length);
|
|
|
final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
|
|
|
try {
|
|
|
- final HttpURLConnection conn = (HttpURLConnection) testResourceUrl.openConnection();
|
|
|
final int lower = Math.min(pos1, pos2);
|
|
|
final int upper = Math.max(pos1, pos2);
|
|
|
- conn.setRequestMethod("GET");
|
|
|
- conn.addRequestProperty("Range", "bytes=" + lower + "-" + upper);
|
|
|
- final int rangeResponse = conn.getResponseCode();
|
|
|
- final byte[] buffer = new byte[upper - lower + 1];
|
|
|
- final int bytesReceived = IOUtils.read(conn.getInputStream(), buffer);
|
|
|
- Assert.assertEquals(206, rangeResponse);
|
|
|
- Assert.assertEquals(buffer.length, bytesReceived);
|
|
|
- Assert.assertArrayEquals(Arrays.copyOfRange(plaintextData, lower, upper + 1), buffer);
|
|
|
+ final HttpMethod getMethod = new GetMethod(testResourceUrl.toString());
|
|
|
+ getMethod.addRequestHeader("Range", "bytes=" + lower + "-" + upper);
|
|
|
+ final int statusCode = client.executeMethod(getMethod);
|
|
|
+ final byte[] responseBody = new byte[upper - lower + 1];
|
|
|
+ IOUtils.read(getMethod.getResponseBodyAsStream(), responseBody);
|
|
|
+ getMethod.releaseConnection();
|
|
|
+ Assert.assertEquals(206, statusCode);
|
|
|
+ Assert.assertArrayEquals(Arrays.copyOfRange(plaintextData, lower, upper + 1), responseBody);
|
|
|
} catch (IOException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
@@ -101,9 +107,32 @@ public class RangeRequestTest {
|
|
|
task.join();
|
|
|
}
|
|
|
|
|
|
- servlet.stop();
|
|
|
+ cm.shutdown();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUnsatisfiableRangeRequest() throws IOException, URISyntaxException {
|
|
|
+ final URL testResourceUrl = new URL(VAULT_BASE_URI.toURL(), "unsatisfiableRangeRequestTestFile.txt");
|
|
|
+ final HttpClient client = new HttpClient();
|
|
|
+
|
|
|
+ // prepare file content:
|
|
|
+ final byte[] fileContent = "This is some test file content.".getBytes();
|
|
|
+
|
|
|
+ // put request:
|
|
|
+ final EntityEnclosingMethod putMethod = new PutMethod(testResourceUrl.toString());
|
|
|
+ putMethod.setRequestEntity(new ByteArrayRequestEntity(fileContent));
|
|
|
+ final int putResponse = client.executeMethod(putMethod);
|
|
|
+ putMethod.releaseConnection();
|
|
|
+ Assert.assertEquals(201, putResponse);
|
|
|
|
|
|
- FileUtils.deleteQuietly(tmpVault);
|
|
|
+ // get request:
|
|
|
+ final HttpMethod getMethod = new GetMethod(testResourceUrl.toString());
|
|
|
+ getMethod.addRequestHeader("Range", "chunks=1-2");
|
|
|
+ final int getResponse = client.executeMethod(getMethod);
|
|
|
+ final byte[] response = getMethod.getResponseBody();
|
|
|
+ getMethod.releaseConnection();
|
|
|
+ Assert.assertEquals(416, getResponse);
|
|
|
+ Assert.assertArrayEquals(fileContent, response);
|
|
|
}
|
|
|
|
|
|
}
|