mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
Pass segments rather than frames to/from segmented plugins.
This commit is contained in:
@@ -11,7 +11,6 @@ import javax.crypto.spec.IvParameterSpec;
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.api.plugins.FrameSource;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
import org.apache.commons.io.output.ByteArrayOutputStream;
|
||||
@@ -20,14 +19,14 @@ import org.junit.Test;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
public class ConnectionDecrypterImplTest extends BriarTestCase {
|
||||
public class ConnectionDecrypterTest extends BriarTestCase {
|
||||
|
||||
private static final int MAC_LENGTH = 32;
|
||||
|
||||
private final Cipher frameCipher;
|
||||
private final ErasableKey frameKey;
|
||||
|
||||
public ConnectionDecrypterImplTest() {
|
||||
public ConnectionDecrypterTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
@@ -8,7 +8,6 @@ import java.io.ByteArrayInputStream;
|
||||
|
||||
import net.sf.briar.TestUtils;
|
||||
import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.plugins.FrameSource;
|
||||
import net.sf.briar.api.transport.ConnectionReader;
|
||||
|
||||
import org.apache.commons.io.output.ByteArrayOutputStream;
|
||||
|
||||
@@ -15,7 +15,6 @@ import javax.crypto.Mac;
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.api.plugins.FrameSource;
|
||||
import net.sf.briar.api.transport.ConnectionReader;
|
||||
import net.sf.briar.api.transport.ConnectionWriter;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
@@ -8,7 +8,6 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.plugins.FrameSource;
|
||||
|
||||
/** A connection decrypter that performs no decryption. */
|
||||
class NullConnectionDecrypter implements FrameSource {
|
||||
|
||||
@@ -11,7 +11,8 @@ import javax.crypto.spec.IvParameterSpec;
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.api.plugins.FrameSource;
|
||||
import net.sf.briar.api.plugins.Segment;
|
||||
import net.sf.briar.api.plugins.SegmentSource;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -53,7 +54,7 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
|
||||
plaintext1.length);
|
||||
// Use a connection decrypter to decrypt the ciphertext
|
||||
byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
|
||||
FrameSource in = new ByteArrayFrameSource(frames);
|
||||
SegmentSource in = new ByteArraySegmentSource(frames);
|
||||
FrameSource decrypter = new SegmentedConnectionDecrypter(in,
|
||||
frameCipher, frameKey, MAC_LENGTH);
|
||||
// First frame
|
||||
@@ -69,20 +70,24 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
private static class ByteArrayFrameSource implements FrameSource {
|
||||
private static class ByteArraySegmentSource implements SegmentSource {
|
||||
|
||||
private final byte[][] frames;
|
||||
|
||||
private int frame = 0;
|
||||
|
||||
private ByteArrayFrameSource(byte[][] frames) {
|
||||
private ByteArraySegmentSource(byte[][] frames) {
|
||||
this.frames = frames;
|
||||
}
|
||||
|
||||
public int readFrame(byte[] b) throws IOException {
|
||||
byte[] src = frames[frame++];
|
||||
System.arraycopy(src, 0, b, 0, src.length);
|
||||
return src.length;
|
||||
public boolean readSegment(Segment s) throws IOException {
|
||||
if(frame == frames.length) return false;
|
||||
byte[] src = frames[frame];
|
||||
System.arraycopy(src, 0, s.getBuffer(), 0, src.length);
|
||||
s.setLength(src.length);
|
||||
s.setTransmissionNumber(frame);
|
||||
frame++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ import javax.crypto.spec.IvParameterSpec;
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.api.plugins.FrameSink;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import net.sf.briar.api.plugins.Segment;
|
||||
import net.sf.briar.api.plugins.SegmentSink;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -60,13 +60,11 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase {
|
||||
out.write(ciphertext1);
|
||||
byte[] expected = out.toByteArray();
|
||||
// Use a connection encrypter to encrypt the plaintext
|
||||
ByteArrayFrameSink sink = new ByteArrayFrameSink();
|
||||
ByteArraySegmentSink sink = new ByteArraySegmentSink();
|
||||
ConnectionEncrypter e = new SegmentedConnectionEncrypter(sink,
|
||||
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey);
|
||||
// The first frame's buffer must have enough space for the tag
|
||||
byte[] b = new byte[TAG_LENGTH + plaintext.length];
|
||||
System.arraycopy(plaintext, 0, b, 0, plaintext.length);
|
||||
e.writeFrame(b, plaintext.length);
|
||||
e.writeFrame(plaintext, plaintext.length);
|
||||
e.writeFrame(plaintext1, plaintext1.length);
|
||||
byte[] actual = out.toByteArray();
|
||||
// Check that the actual ciphertext matches the expected ciphertext
|
||||
@@ -74,11 +72,11 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase {
|
||||
assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity());
|
||||
}
|
||||
|
||||
private static class ByteArrayFrameSink extends ByteArrayOutputStream
|
||||
implements FrameSink {
|
||||
private static class ByteArraySegmentSink extends ByteArrayOutputStream
|
||||
implements SegmentSink {
|
||||
|
||||
public void writeFrame(byte[] b, int len) throws IOException {
|
||||
write(b, 0, len);
|
||||
public void writeSegment(Segment s) throws IOException {
|
||||
write(s.getBuffer(), 0, s.getLength());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user