mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
Allow the transport to specify the maximum segment length.
This commit is contained in:
@@ -160,5 +160,9 @@ public class IncomingErrorCorrectionLayerImplTest extends BriarTestCase {
|
||||
s.setLength(length);
|
||||
return true;
|
||||
}
|
||||
|
||||
public int getMaxSegmentLength() {
|
||||
return length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_SEGMENT_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -149,5 +150,9 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
s.setLength(segment.length);
|
||||
return true;
|
||||
}
|
||||
|
||||
public int getMaxSegmentLength() {
|
||||
return MAX_SEGMENT_LENGTH;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package net.sf.briar.transport;
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_SEGMENT_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
@@ -49,4 +51,8 @@ class NullIncomingEncryptionLayer implements IncomingEncryptionLayer {
|
||||
s.setSegmentNumber(segmentNumber++);
|
||||
return true;
|
||||
}
|
||||
|
||||
public int getMaxSegmentLength() {
|
||||
return MAX_SEGMENT_LENGTH - TAG_LENGTH;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_SEGMENT_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
@@ -34,4 +37,8 @@ class NullOutgoingEncryptionLayer implements OutgoingEncryptionLayer {
|
||||
public long getRemainingCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public int getMaxSegmentLength() {
|
||||
return MAX_SEGMENT_LENGTH - TAG_LENGTH;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_SEGMENT_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
@@ -136,5 +137,9 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
public void writeSegment(Segment s) throws IOException {
|
||||
write(s.getBuffer(), 0, s.getLength());
|
||||
}
|
||||
|
||||
public int getMaxSegmentLength() {
|
||||
return MAX_SEGMENT_LENGTH;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.transport.Segment;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
public class XorErasureCodeTest extends BriarTestCase {
|
||||
|
||||
@@ -3,11 +3,11 @@ package net.sf.briar.transport;
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.transport.Segment;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
public class XorErasureDecoderTest extends BriarTestCase {
|
||||
|
||||
@@ -10,30 +10,29 @@ public class XorErasureEncoderTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testEncoding() {
|
||||
// Create a 100-byte frame
|
||||
// Create a frame
|
||||
Frame f = new Frame();
|
||||
f.setLength(100);
|
||||
f.setLength(200);
|
||||
byte[] b = f.getBuffer();
|
||||
for(int i = 0; i < 100; i++) b[i] = (byte) i;
|
||||
for(int i = 0; i < 200; i++) b[i] = (byte) i;
|
||||
// Encode the frame
|
||||
XorErasureEncoder e = new XorErasureEncoder(4);
|
||||
Segment[] set = e.encodeFrame(f);
|
||||
// There should be four pieces of 34 bytes each
|
||||
// There should be four pieces of 67 bytes each
|
||||
assertEquals(4, set.length);
|
||||
for(int i = 0; i < 4; i++) assertEquals(34, set[i].getLength());
|
||||
// The first three pieces should contain the data, plus two zero bytes
|
||||
for(int i = 0; i < 4; i++) assertEquals(67, set[i].getLength());
|
||||
// The first three pieces should contain the data plus on padding byte
|
||||
byte[] b1 = set[0].getBuffer();
|
||||
for(int i = 0; i < 34; i++) assertEquals(i, b1[i]);
|
||||
for(int i = 0; i < 67; i++) assertEquals((byte) i, b1[i]);
|
||||
byte[] b2 = set[1].getBuffer();
|
||||
for(int i = 0; i < 34; i++) assertEquals(i + 34, b2[i]);
|
||||
for(int i = 0; i < 67; i++) assertEquals((byte) (i + 67), b2[i]);
|
||||
byte[] b3 = set[2].getBuffer();
|
||||
for(int i = 0; i < 32; i++) assertEquals(i + 68, b3[i]);
|
||||
assertEquals(0, b3[32]);
|
||||
assertEquals(0, b3[33]);
|
||||
for(int i = 0; i < 66; i++) assertEquals((byte) (i + 134), b3[i]);
|
||||
assertEquals(0, b3[66]);
|
||||
// The fourth piece should be the XOR of the other three
|
||||
byte[] b4 = set[3].getBuffer();
|
||||
byte[] expected = new byte[34];
|
||||
for(int i = 0; i < 34; i++) {
|
||||
byte[] expected = new byte[67];
|
||||
for(int i = 0; i < 67; i++) {
|
||||
expected[i] = (byte) (b1[i] ^ b2[i] ^ b3[i]);
|
||||
}
|
||||
assertArrayEquals(expected, b4);
|
||||
|
||||
Reference in New Issue
Block a user