mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-26 03:40:56 +01:00
Implement send support for resumable uploads behind a flag.
This commit is contained in:
committed by
Greyson Parrelli
parent
7c442865c5
commit
2afb939ee6
@@ -203,7 +203,7 @@ public class AttachmentCipherTest extends TestCase {
|
||||
|
||||
private static EncryptResult encryptData(byte[] data, byte[] keyMaterial) throws IOException {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
AttachmentCipherOutputStream encryptStream = new AttachmentCipherOutputStream(keyMaterial, outputStream);
|
||||
AttachmentCipherOutputStream encryptStream = new AttachmentCipherOutputStream(keyMaterial, null, outputStream);
|
||||
|
||||
encryptStream.write(data);
|
||||
encryptStream.flush();
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package org.whispersystems.signalservice.api.crypto;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SkippingOutputStreamTest {
|
||||
|
||||
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
|
||||
@Test
|
||||
public void givenZeroToSkip_whenIWriteInt_thenIGetIntInOutput() throws Exception {
|
||||
// GIVEN
|
||||
SkippingOutputStream testSubject = new SkippingOutputStream(0, outputStream);
|
||||
|
||||
// WHEN
|
||||
testSubject.write(0);
|
||||
|
||||
// THEN
|
||||
assertEquals(1, outputStream.toByteArray().length);
|
||||
assertEquals(0, outputStream.toByteArray()[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneToSkip_whenIWriteIntTwice_thenIGetSecondIntInOutput() throws Exception {
|
||||
// GIVEN
|
||||
SkippingOutputStream testSubject = new SkippingOutputStream(1, outputStream);
|
||||
|
||||
// WHEN
|
||||
testSubject.write(0);
|
||||
testSubject.write(1);
|
||||
|
||||
// THEN
|
||||
assertEquals(1, outputStream.toByteArray().length);
|
||||
assertEquals(1, outputStream.toByteArray()[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZeroToSkip_whenIWriteArray_thenIGetArrayInOutput() throws Exception {
|
||||
// GIVEN
|
||||
byte[] expected = new byte[]{1, 2, 3, 4, 5};
|
||||
SkippingOutputStream testSubject = new SkippingOutputStream(0, outputStream);
|
||||
|
||||
// WHEN
|
||||
testSubject.write(expected);
|
||||
|
||||
// THEN
|
||||
assertEquals(expected.length, outputStream.toByteArray().length);
|
||||
assertArrayEquals(expected, outputStream.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonZeroToSkip_whenIWriteArray_thenIGetEndOfArrayInOutput() throws Exception {
|
||||
// GIVEN
|
||||
byte[] expected = new byte[]{1, 2, 3, 4, 5};
|
||||
SkippingOutputStream testSubject = new SkippingOutputStream(3, outputStream);
|
||||
|
||||
// WHEN
|
||||
testSubject.write(expected);
|
||||
|
||||
// THEN
|
||||
assertEquals(2, outputStream.toByteArray().length);
|
||||
assertArrayEquals(new byte[]{4, 5}, outputStream.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSkipGreaterThanByteArray_whenIWriteArray_thenIGetNoOutput() throws Exception {
|
||||
// GIVEN
|
||||
byte[] array = new byte[]{1, 2, 3, 4, 5};
|
||||
SkippingOutputStream testSubject = new SkippingOutputStream(10, outputStream);
|
||||
|
||||
// WHEN
|
||||
testSubject.write(array);
|
||||
|
||||
// THEN
|
||||
assertEquals(0, outputStream.toByteArray().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZeroToSkip_whenIWriteArrayRange_thenIGetArrayRangeInOutput() throws Exception {
|
||||
// GIVEN
|
||||
byte[] expected = new byte[]{1, 2, 3, 4, 5};
|
||||
SkippingOutputStream testSubject = new SkippingOutputStream(0, outputStream);
|
||||
|
||||
// WHEN
|
||||
testSubject.write(expected, 1, 3);
|
||||
|
||||
// THEN
|
||||
assertEquals(3, outputStream.toByteArray().length);
|
||||
assertArrayEquals(new byte[]{2, 3, 4}, outputStream.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonZeroToSkip_whenIWriteArrayRange_thenIGetEndOfArrayRangeInOutput() throws Exception {
|
||||
// GIVEN
|
||||
byte[] expected = new byte[]{1, 2, 3, 4, 5};
|
||||
SkippingOutputStream testSubject = new SkippingOutputStream(1, outputStream);
|
||||
|
||||
// WHEN
|
||||
testSubject.write(expected, 3, 2);
|
||||
|
||||
// THEN
|
||||
assertEquals(1, outputStream.toByteArray().length);
|
||||
assertArrayEquals(new byte[]{5}, outputStream.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSkipGreaterThanByteArrayRange_whenIWriteArrayRange_thenIGetNoOutput() throws Exception {
|
||||
// GIVEN
|
||||
byte[] array = new byte[]{1, 2, 3, 4, 5};
|
||||
SkippingOutputStream testSubject = new SkippingOutputStream(10, outputStream);
|
||||
|
||||
// WHEN
|
||||
testSubject.write(array, 3, 2);
|
||||
|
||||
// THEN
|
||||
assertEquals(0, outputStream.toByteArray().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSkipGreaterThanByteArrayRange_whenIWriteArrayRangeTwice_thenIGetExpectedOutput() throws Exception {
|
||||
// GIVEN
|
||||
byte[] array = new byte[]{1, 2, 3, 4, 5};
|
||||
SkippingOutputStream testSubject = new SkippingOutputStream(3, outputStream);
|
||||
|
||||
// WHEN
|
||||
testSubject.write(array, 3, 2);
|
||||
testSubject.write(array, 3, 2);
|
||||
|
||||
// THEN
|
||||
assertEquals(1, outputStream.toByteArray().length);
|
||||
assertArrayEquals(new byte[]{5}, outputStream.toByteArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package org.whispersystems.signalservice.internal.push.http;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.whispersystems.signalservice.api.crypto.AttachmentCipherOutputStream;
|
||||
import org.whispersystems.signalservice.internal.util.Util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import okio.Buffer;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class DigestingRequestBodyTest {
|
||||
|
||||
private static int CONTENT_LENGTH = 70000;
|
||||
private static int TOTAL_LENGTH = (int) AttachmentCipherOutputStream.getCiphertextLength(CONTENT_LENGTH);
|
||||
|
||||
private final byte[] attachmentKey = Util.getSecretBytes(64);
|
||||
private final byte[] attachmentIV = Util.getSecretBytes(16);
|
||||
private final byte[] input = Util.getSecretBytes(CONTENT_LENGTH);
|
||||
|
||||
private final OutputStreamFactory outputStreamFactory = new AttachmentCipherOutputStreamFactory(attachmentKey, attachmentIV);
|
||||
|
||||
@Test
|
||||
public void givenSameKeyAndIV_whenIWriteToBuffer_thenIExpectSameTransmittedDigest() throws Exception {
|
||||
DigestingRequestBody fromStart = getBody(0);
|
||||
DigestingRequestBody fromMiddle = getBody(CONTENT_LENGTH / 2);
|
||||
|
||||
try (Buffer buffer = new Buffer()) {
|
||||
fromStart.writeTo(buffer);
|
||||
}
|
||||
|
||||
try (Buffer buffer = new Buffer()) {
|
||||
fromMiddle.writeTo(buffer);
|
||||
}
|
||||
|
||||
assertArrayEquals(fromStart.getTransmittedDigest(), fromMiddle.getTransmittedDigest());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameKeyAndIV_whenIWriteToBuffer_thenIExpectSameContents() throws Exception {
|
||||
DigestingRequestBody fromStart = getBody(0);
|
||||
DigestingRequestBody fromMiddle = getBody(CONTENT_LENGTH / 2);
|
||||
|
||||
byte[] cipher1;
|
||||
|
||||
try (Buffer buffer = new Buffer()) {
|
||||
fromStart.writeTo(buffer);
|
||||
|
||||
cipher1 = buffer.readByteArray();
|
||||
}
|
||||
|
||||
byte[] cipher2;
|
||||
|
||||
try (Buffer buffer = new Buffer()) {
|
||||
fromMiddle.writeTo(buffer);
|
||||
|
||||
cipher2 = buffer.readByteArray();
|
||||
}
|
||||
|
||||
assertEquals(cipher1.length, TOTAL_LENGTH);
|
||||
assertEquals(cipher2.length, TOTAL_LENGTH - (CONTENT_LENGTH / 2));
|
||||
|
||||
for (int i = 0; i < cipher2.length; i++) {
|
||||
assertEquals(cipher2[i], cipher1[i + (CONTENT_LENGTH / 2)]);
|
||||
}
|
||||
}
|
||||
|
||||
private DigestingRequestBody getBody(long contentStart) {
|
||||
return new DigestingRequestBody(new ByteArrayInputStream(input), outputStreamFactory, "application/octet", CONTENT_LENGTH, (a, b) -> {}, () -> false, contentStart);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user