From ac136d3732159623405b80c7703bb5d16e66e236 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Fri, 13 Jan 2012 15:05:42 +0000
Subject: [PATCH] Pass segments rather than frames to/from segmented plugins.

---
 api/net/sf/briar/api/plugins/Segment.java     | 16 +++++++
 api/net/sf/briar/api/plugins/SegmentSink.java |  9 ++++
 .../sf/briar/api/plugins/SegmentSource.java   | 12 +++++
 .../DuplexSegmentedTransportConnection.java   |  8 ++--
 .../SimplexSegmentedTransportReader.java      |  4 +-
 .../SimplexSegmentedTransportWriter.java      |  4 +-
 .../briar/transport/ConnectionDecrypter.java  |  8 ++--
 .../briar/transport/ConnectionEncrypter.java  |  1 -
 .../transport/ConnectionEncrypterImpl.java    |  2 +-
 .../ConnectionReaderFactoryImpl.java          |  1 -
 .../briar/transport/ConnectionReaderImpl.java |  1 -
 .../net/sf/briar/transport}/FrameSink.java    |  4 +-
 .../net/sf/briar/transport}/FrameSource.java  |  4 +-
 .../net/sf/briar/transport/SegmentImpl.java   | 45 +++++++++++++++++++
 .../SegmentedConnectionDecrypter.java         | 22 ++++++---
 .../SegmentedConnectionEncrypter.java         | 26 ++++++-----
 test/build.xml                                |  2 +-
 ...Test.java => ConnectionDecrypterTest.java} |  5 +--
 .../transport/ConnectionReaderImplTest.java   |  1 -
 .../briar/transport/FrameReadWriteTest.java   |  1 -
 .../transport/NullConnectionDecrypter.java    |  1 -
 .../SegmentedConnectionDecrypterTest.java     | 21 +++++----
 .../SegmentedConnectionEncrypterTest.java     | 18 ++++----
 23 files changed, 155 insertions(+), 61 deletions(-)
 create mode 100644 api/net/sf/briar/api/plugins/Segment.java
 create mode 100644 api/net/sf/briar/api/plugins/SegmentSink.java
 create mode 100644 api/net/sf/briar/api/plugins/SegmentSource.java
 rename {api/net/sf/briar/api/plugins => components/net/sf/briar/transport}/FrameSink.java (65%)
 rename {api/net/sf/briar/api/plugins => components/net/sf/briar/transport}/FrameSource.java (74%)
 create mode 100644 components/net/sf/briar/transport/SegmentImpl.java
 rename test/net/sf/briar/transport/{ConnectionDecrypterImplTest.java => ConnectionDecrypterTest.java} (94%)

diff --git a/api/net/sf/briar/api/plugins/Segment.java b/api/net/sf/briar/api/plugins/Segment.java
new file mode 100644
index 0000000000..47ed614773
--- /dev/null
+++ b/api/net/sf/briar/api/plugins/Segment.java
@@ -0,0 +1,16 @@
+package net.sf.briar.api.plugins;
+
+public interface Segment {
+
+	void clear();
+
+	byte[] getBuffer();
+
+	int getLength();
+
+	long getTransmissionNumber();
+
+	void setLength(int length);
+
+	void setTransmissionNumber(int transmission);
+}
diff --git a/api/net/sf/briar/api/plugins/SegmentSink.java b/api/net/sf/briar/api/plugins/SegmentSink.java
new file mode 100644
index 0000000000..8f017c96e2
--- /dev/null
+++ b/api/net/sf/briar/api/plugins/SegmentSink.java
@@ -0,0 +1,9 @@
+package net.sf.briar.api.plugins;
+
+import java.io.IOException;
+
+public interface SegmentSink {
+
+	/** Writes the given segment. */
+	void writeSegment(Segment s) throws IOException;
+}
diff --git a/api/net/sf/briar/api/plugins/SegmentSource.java b/api/net/sf/briar/api/plugins/SegmentSource.java
new file mode 100644
index 0000000000..bb5a92a18f
--- /dev/null
+++ b/api/net/sf/briar/api/plugins/SegmentSource.java
@@ -0,0 +1,12 @@
+package net.sf.briar.api.plugins;
+
+import java.io.IOException;
+
+public interface SegmentSource {
+
+	/**
+	 * Attempts to read a segment into the given buffer and returns true if a
+	 * segment was read, or false if no more segments can be read.
+	 */
+	boolean readSegment(Segment s) throws IOException;
+}
diff --git a/api/net/sf/briar/api/plugins/duplex/DuplexSegmentedTransportConnection.java b/api/net/sf/briar/api/plugins/duplex/DuplexSegmentedTransportConnection.java
index 728f3e60b4..c9b16844ed 100644
--- a/api/net/sf/briar/api/plugins/duplex/DuplexSegmentedTransportConnection.java
+++ b/api/net/sf/briar/api/plugins/duplex/DuplexSegmentedTransportConnection.java
@@ -1,15 +1,15 @@
 package net.sf.briar.api.plugins.duplex;
 
-import net.sf.briar.api.plugins.FrameSink;
-import net.sf.briar.api.plugins.FrameSource;
+import net.sf.briar.api.plugins.SegmentSink;
+import net.sf.briar.api.plugins.SegmentSource;
 
 /**
  * An interface for reading and writing data over a duplex segmented transport.
  * The connection is not responsible for encrypting/decrypting or authenticating
  * the data.
  */
-public interface DuplexSegmentedTransportConnection extends FrameSource,
-FrameSink {
+public interface DuplexSegmentedTransportConnection extends SegmentSource,
+SegmentSink {
 
 	/** Returns the maximum length of a segment in bytes. */
 	int getMaximumSegmentLength();
diff --git a/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportReader.java b/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportReader.java
index 66ab1a91c4..c34946d5cf 100644
--- a/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportReader.java
+++ b/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportReader.java
@@ -1,13 +1,13 @@
 package net.sf.briar.api.plugins.simplex;
 
-import net.sf.briar.api.plugins.FrameSource;
+import net.sf.briar.api.plugins.SegmentSource;
 
 /**
  * An interface for reading data from a simplex segmented transport. The reader
  * is not responsible for decrypting or authenticating the data before
  * returning it.
  */
-public interface SimplexSegmentedTransportReader extends FrameSource {
+public interface SimplexSegmentedTransportReader extends SegmentSource {
 
 	/**
 	 * Closes the reader and disposes of any associated resources. The first
diff --git a/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportWriter.java b/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportWriter.java
index 2a8cb810bc..364faf6ab3 100644
--- a/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportWriter.java
+++ b/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportWriter.java
@@ -1,12 +1,12 @@
 package net.sf.briar.api.plugins.simplex;
 
-import net.sf.briar.api.plugins.FrameSink;
+import net.sf.briar.api.plugins.SegmentSink;
 
 /**
  * An interface for writing data to a simplex segmented transport. The writer is
  * not responsible for authenticating or encrypting the data before writing it.
  */
-public interface SimplexSegmentedTransportWriter extends FrameSink {
+public interface SimplexSegmentedTransportWriter extends SegmentSink {
 
 	/** Returns the capacity of the transport in bytes. */
 	long getCapacity();
diff --git a/components/net/sf/briar/transport/ConnectionDecrypter.java b/components/net/sf/briar/transport/ConnectionDecrypter.java
index c8facbdc74..227c09bbdf 100644
--- a/components/net/sf/briar/transport/ConnectionDecrypter.java
+++ b/components/net/sf/briar/transport/ConnectionDecrypter.java
@@ -14,7 +14,6 @@ import javax.crypto.spec.IvParameterSpec;
 
 import net.sf.briar.api.FormatException;
 import net.sf.briar.api.crypto.ErasableKey;
-import net.sf.briar.api.plugins.FrameSource;
 
 class ConnectionDecrypter implements FrameSource {
 
@@ -49,6 +48,8 @@ class ConnectionDecrypter implements FrameSource {
 		} catch(GeneralSecurityException badIvOrKey) {
 			throw new RuntimeException(badIvOrKey);
 		}
+		// Clear the buffer before exposing it to the transport plugin
+		for(int i = 0; i < b.length; i++) b[i] = 0;
 		try {
 			// Read the first block
 			int offset = 0;
@@ -64,7 +65,7 @@ class ConnectionDecrypter implements FrameSource {
 			// Decrypt the first block
 			try {
 				int decrypted = frameCipher.update(b, 0, blockSize, b);
-				assert decrypted == blockSize;
+				if(decrypted != blockSize) throw new RuntimeException();
 			} catch(GeneralSecurityException badCipher) {
 				throw new RuntimeException(badCipher);
 			}
@@ -86,7 +87,8 @@ class ConnectionDecrypter implements FrameSource {
 			try {
 				int decrypted = frameCipher.doFinal(b, blockSize,
 						length - blockSize, b, blockSize);
-				assert decrypted == length - blockSize;
+				if(decrypted != length - blockSize)
+					throw new RuntimeException();
 			} catch(GeneralSecurityException badCipher) {
 				throw new RuntimeException(badCipher);
 			}
diff --git a/components/net/sf/briar/transport/ConnectionEncrypter.java b/components/net/sf/briar/transport/ConnectionEncrypter.java
index 930f1ee819..01fcee7708 100644
--- a/components/net/sf/briar/transport/ConnectionEncrypter.java
+++ b/components/net/sf/briar/transport/ConnectionEncrypter.java
@@ -2,7 +2,6 @@ package net.sf.briar.transport;
 
 import java.io.IOException;
 
-import net.sf.briar.api.plugins.FrameSink;
 
 /** Encrypts authenticated data to be sent over a connection. */
 interface ConnectionEncrypter extends FrameSink {
diff --git a/components/net/sf/briar/transport/ConnectionEncrypterImpl.java b/components/net/sf/briar/transport/ConnectionEncrypterImpl.java
index 9370c20f9a..0e83690360 100644
--- a/components/net/sf/briar/transport/ConnectionEncrypterImpl.java
+++ b/components/net/sf/briar/transport/ConnectionEncrypterImpl.java
@@ -50,7 +50,7 @@ class ConnectionEncrypterImpl implements ConnectionEncrypter {
 		try {
 			frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
 			int encrypted = frameCipher.doFinal(b, 0, len, b);
-			assert encrypted == len;
+			if(encrypted != len) throw new RuntimeException();
 		} catch(GeneralSecurityException badCipher) {
 			throw new RuntimeException(badCipher);
 		}
diff --git a/components/net/sf/briar/transport/ConnectionReaderFactoryImpl.java b/components/net/sf/briar/transport/ConnectionReaderFactoryImpl.java
index 91c0929320..36be2d4143 100644
--- a/components/net/sf/briar/transport/ConnectionReaderFactoryImpl.java
+++ b/components/net/sf/briar/transport/ConnectionReaderFactoryImpl.java
@@ -7,7 +7,6 @@ import javax.crypto.Mac;
 
 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.ConnectionReaderFactory;
 import net.sf.briar.util.ByteUtils;
diff --git a/components/net/sf/briar/transport/ConnectionReaderImpl.java b/components/net/sf/briar/transport/ConnectionReaderImpl.java
index 49928cd4e7..1d7b1de19e 100644
--- a/components/net/sf/briar/transport/ConnectionReaderImpl.java
+++ b/components/net/sf/briar/transport/ConnectionReaderImpl.java
@@ -12,7 +12,6 @@ import javax.crypto.Mac;
 
 import net.sf.briar.api.FormatException;
 import net.sf.briar.api.crypto.ErasableKey;
-import net.sf.briar.api.plugins.FrameSource;
 import net.sf.briar.api.transport.ConnectionReader;
 
 class ConnectionReaderImpl extends InputStream implements ConnectionReader {
diff --git a/api/net/sf/briar/api/plugins/FrameSink.java b/components/net/sf/briar/transport/FrameSink.java
similarity index 65%
rename from api/net/sf/briar/api/plugins/FrameSink.java
rename to components/net/sf/briar/transport/FrameSink.java
index 841eb81592..710f60a5e3 100644
--- a/api/net/sf/briar/api/plugins/FrameSink.java
+++ b/components/net/sf/briar/transport/FrameSink.java
@@ -1,8 +1,8 @@
-package net.sf.briar.api.plugins;
+package net.sf.briar.transport;
 
 import java.io.IOException;
 
-public interface FrameSink {
+interface FrameSink {
 
 	/** Writes the given frame. */
 	void writeFrame(byte[] b, int len) throws IOException;
diff --git a/api/net/sf/briar/api/plugins/FrameSource.java b/components/net/sf/briar/transport/FrameSource.java
similarity index 74%
rename from api/net/sf/briar/api/plugins/FrameSource.java
rename to components/net/sf/briar/transport/FrameSource.java
index 5947282140..7347b6ccba 100644
--- a/api/net/sf/briar/api/plugins/FrameSource.java
+++ b/components/net/sf/briar/transport/FrameSource.java
@@ -1,8 +1,8 @@
-package net.sf.briar.api.plugins;
+package net.sf.briar.transport;
 
 import java.io.IOException;
 
-public interface FrameSource {
+interface FrameSource {
 
 	/**
 	 * Reads a frame into the given buffer and returns its length, or -1 if no
diff --git a/components/net/sf/briar/transport/SegmentImpl.java b/components/net/sf/briar/transport/SegmentImpl.java
new file mode 100644
index 0000000000..b7cb6398e4
--- /dev/null
+++ b/components/net/sf/briar/transport/SegmentImpl.java
@@ -0,0 +1,45 @@
+package net.sf.briar.transport;
+
+import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
+import net.sf.briar.api.plugins.Segment;
+import net.sf.briar.util.ByteUtils;
+
+class SegmentImpl implements Segment {
+
+	private final byte[] buf = new byte[MAX_FRAME_LENGTH];
+
+	private int length = -1;
+	private long transmission = -1;
+
+	public void clear() {
+		for(int i = 0; i < buf.length; i++) buf[i] = 0;
+		length = -1;
+		transmission = -1;
+	}
+
+	public byte[] getBuffer() {
+		return buf;
+	}
+
+	public int getLength() {
+		if(length == -1) throw new IllegalStateException();
+		return length;
+	}
+
+	public long getTransmissionNumber() {
+		if(transmission == -1) throw new IllegalStateException();
+		return transmission;
+	}
+
+	public void setLength(int length) {
+		if(length < 0 || length > buf.length)
+			throw new IllegalArgumentException();
+		this.length = length;
+	}
+
+	public void setTransmissionNumber(int transmission) {
+		if(transmission < 0 || transmission > ByteUtils.MAX_32_BIT_UNSIGNED)
+			throw new IllegalArgumentException();
+		this.transmission = transmission;
+	}
+}
diff --git a/components/net/sf/briar/transport/SegmentedConnectionDecrypter.java b/components/net/sf/briar/transport/SegmentedConnectionDecrypter.java
index 385d967120..47bc7b1423 100644
--- a/components/net/sf/briar/transport/SegmentedConnectionDecrypter.java
+++ b/components/net/sf/briar/transport/SegmentedConnectionDecrypter.java
@@ -12,19 +12,21 @@ import javax.crypto.spec.IvParameterSpec;
 
 import net.sf.briar.api.FormatException;
 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;
 
 class SegmentedConnectionDecrypter implements FrameSource {
 
-	private final FrameSource in;
+	private final SegmentSource in;
 	private final Cipher frameCipher;
 	private final ErasableKey frameKey;
 	private final int macLength, blockSize;
 	private final byte[] iv;
+	private final Segment segment;
 
 	private long frame = 0L;
 
-	SegmentedConnectionDecrypter(FrameSource in, Cipher frameCipher,
+	SegmentedConnectionDecrypter(SegmentSource in, Cipher frameCipher,
 			ErasableKey frameKey, int macLength) {
 		this.in = in;
 		this.frameCipher = frameCipher;
@@ -34,6 +36,7 @@ class SegmentedConnectionDecrypter implements FrameSource {
 		if(blockSize < FRAME_HEADER_LENGTH)
 			throw new IllegalArgumentException();
 		iv = IvEncoder.encodeIv(0, blockSize);
+		segment = new SegmentImpl();
 	}
 
 	public int readFrame(byte[] b) throws IOException {
@@ -47,17 +50,22 @@ class SegmentedConnectionDecrypter implements FrameSource {
 		} catch(GeneralSecurityException badIvOrKey) {
 			throw new RuntimeException(badIvOrKey);
 		}
+		// Clear the buffer before exposing it to the transport plugin
+		segment.clear();
 		try {
 			// Read the frame
-			int length = in.readFrame(b);
-			if(length == -1) return -1;
+			if(!in.readSegment(segment)) return -1;
+			if(segment.getTransmissionNumber() != frame)
+				throw new FormatException();
+			int length = segment.getLength();
 			if(length > MAX_FRAME_LENGTH) throw new FormatException();
 			if(length < FRAME_HEADER_LENGTH + macLength)
 				throw new FormatException();
 			// Decrypt the frame
 			try {
-				int decrypted = frameCipher.doFinal(b, 0, length, b);
-				assert decrypted == length;
+				int decrypted = frameCipher.doFinal(segment.getBuffer(), 0,
+						length, b);
+				if(decrypted != length) throw new RuntimeException();
 			} catch(GeneralSecurityException badCipher) {
 				throw new RuntimeException(badCipher);
 			}
diff --git a/components/net/sf/briar/transport/SegmentedConnectionEncrypter.java b/components/net/sf/briar/transport/SegmentedConnectionEncrypter.java
index f67d9b9d91..60f905120c 100644
--- a/components/net/sf/briar/transport/SegmentedConnectionEncrypter.java
+++ b/components/net/sf/briar/transport/SegmentedConnectionEncrypter.java
@@ -1,6 +1,7 @@
 package net.sf.briar.transport;
 
 import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
+import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
 
 import java.io.IOException;
 import java.security.GeneralSecurityException;
@@ -9,20 +10,23 @@ import javax.crypto.Cipher;
 import javax.crypto.spec.IvParameterSpec;
 
 import net.sf.briar.api.crypto.ErasableKey;
-import net.sf.briar.api.plugins.FrameSink;
+import net.sf.briar.api.plugins.Segment;
+import net.sf.briar.api.plugins.SegmentSink;
 
 class SegmentedConnectionEncrypter implements ConnectionEncrypter {
 
-	private final FrameSink out;
+	private final SegmentSink out;
 	private final Cipher frameCipher;
 	private final ErasableKey frameKey;
 	private final byte[] iv, tag;
+	private final Segment segment;
 
 	private long capacity, frame = 0L;
 	private boolean tagWritten = false;
 
-	SegmentedConnectionEncrypter(FrameSink out, long capacity, Cipher tagCipher,
-			Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey) {
+	SegmentedConnectionEncrypter(SegmentSink out, long capacity,
+			Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey,
+			ErasableKey frameKey) {
 		this.out = out;
 		this.capacity = capacity;
 		this.frameCipher = frameCipher;
@@ -31,16 +35,16 @@ class SegmentedConnectionEncrypter implements ConnectionEncrypter {
 		// Encrypt the tag
 		tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
 		tagKey.erase();
+		segment = new SegmentImpl();
 	}
 
 	public void writeFrame(byte[] b, int len) throws IOException {
 		if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
 		int offset = 0;
 		if(!tagWritten) {
-			if(tag.length + len > b.length)
+			if(tag.length + len > MAX_FRAME_LENGTH)
 				throw new IllegalArgumentException();
-			System.arraycopy(b, 0, b, tag.length, len);
-			System.arraycopy(tag, 0, b, 0, tag.length);
+			System.arraycopy(tag, 0, segment.getBuffer(), 0, tag.length);
 			capacity -= tag.length;
 			tagWritten = true;
 			offset = tag.length;
@@ -49,13 +53,15 @@ class SegmentedConnectionEncrypter implements ConnectionEncrypter {
 		IvParameterSpec ivSpec = new IvParameterSpec(iv);
 		try {
 			frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
-			int encrypted = frameCipher.doFinal(b, offset, len, b, offset);
-			assert encrypted == len;
+			int encrypted = frameCipher.doFinal(b, 0, len, segment.getBuffer(),
+					offset);
+			if(encrypted != len) throw new RuntimeException();
 		} catch(GeneralSecurityException badCipher) {
 			throw new RuntimeException(badCipher);
 		}
+		segment.setLength(offset + len);
 		try {
-			out.writeFrame(b, offset + len);
+			out.writeSegment(segment);
 		} catch(IOException e) {
 			frameKey.erase();
 			throw e;
diff --git a/test/build.xml b/test/build.xml
index 8c643549de..f0d6316227 100644
--- a/test/build.xml
+++ b/test/build.xml
@@ -49,7 +49,7 @@
 			<test name='net.sf.briar.serial.ReaderImplTest'/>
 			<test name='net.sf.briar.serial.WriterImplTest'/>
 			<test name='net.sf.briar.setup.SetupWorkerTest'/>
-			<test name='net.sf.briar.transport.ConnectionDecrypterImplTest'/>
+			<test name='net.sf.briar.transport.ConnectionDecrypterTest'/>
 			<test name='net.sf.briar.transport.ConnectionEncrypterImplTest'/>
 			<test name='net.sf.briar.transport.ConnectionReaderImplTest'/>
 			<test name='net.sf.briar.transport.ConnectionRecogniserImplTest'/>
diff --git a/test/net/sf/briar/transport/ConnectionDecrypterImplTest.java b/test/net/sf/briar/transport/ConnectionDecrypterTest.java
similarity index 94%
rename from test/net/sf/briar/transport/ConnectionDecrypterImplTest.java
rename to test/net/sf/briar/transport/ConnectionDecrypterTest.java
index f8d09593a4..829c73335a 100644
--- a/test/net/sf/briar/transport/ConnectionDecrypterImplTest.java
+++ b/test/net/sf/briar/transport/ConnectionDecrypterTest.java
@@ -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);
diff --git a/test/net/sf/briar/transport/ConnectionReaderImplTest.java b/test/net/sf/briar/transport/ConnectionReaderImplTest.java
index 7a6f8000ad..798f8e4572 100644
--- a/test/net/sf/briar/transport/ConnectionReaderImplTest.java
+++ b/test/net/sf/briar/transport/ConnectionReaderImplTest.java
@@ -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;
diff --git a/test/net/sf/briar/transport/FrameReadWriteTest.java b/test/net/sf/briar/transport/FrameReadWriteTest.java
index ac51080048..b763682a4b 100644
--- a/test/net/sf/briar/transport/FrameReadWriteTest.java
+++ b/test/net/sf/briar/transport/FrameReadWriteTest.java
@@ -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;
diff --git a/test/net/sf/briar/transport/NullConnectionDecrypter.java b/test/net/sf/briar/transport/NullConnectionDecrypter.java
index 319df394cc..32b5a21649 100644
--- a/test/net/sf/briar/transport/NullConnectionDecrypter.java
+++ b/test/net/sf/briar/transport/NullConnectionDecrypter.java
@@ -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 {
diff --git a/test/net/sf/briar/transport/SegmentedConnectionDecrypterTest.java b/test/net/sf/briar/transport/SegmentedConnectionDecrypterTest.java
index a987ede8f8..20727365e8 100644
--- a/test/net/sf/briar/transport/SegmentedConnectionDecrypterTest.java
+++ b/test/net/sf/briar/transport/SegmentedConnectionDecrypterTest.java
@@ -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;
 		}
 	}
 }
diff --git a/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java b/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java
index 9c4cf62dc6..638d9ac31d 100644
--- a/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java
+++ b/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java
@@ -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());
 		}
 	}
 }
-- 
GitLab