mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Test and bugfixes for XorErasureEncoder.
This commit is contained in:
@@ -21,8 +21,8 @@ class XorErasureEncoder implements ErasureEncoder {
|
|||||||
byte[] src = f.getBuffer(), parity = set[n - 1].getBuffer();
|
byte[] src = f.getBuffer(), parity = set[n - 1].getBuffer();
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
for(int i = 0; i < n - 1; i++) {
|
for(int i = 0; i < n - 1; i++) {
|
||||||
System.arraycopy(src, 0, set[i].getBuffer(), offset, length);
|
System.arraycopy(src, offset, set[i].getBuffer(), 0, length);
|
||||||
for(int j = 0; j < length; j++) parity[j] ^= src[j];
|
for(int j = 0; j < length; j++) parity[j] ^= src[offset + j];
|
||||||
offset += length;
|
offset += length;
|
||||||
}
|
}
|
||||||
return set;
|
return set;
|
||||||
|
|||||||
@@ -61,6 +61,7 @@
|
|||||||
<test name='net.sf.briar.transport.IncomingSegmentedEncryptionLayerTest'/>
|
<test name='net.sf.briar.transport.IncomingSegmentedEncryptionLayerTest'/>
|
||||||
<test name='net.sf.briar.transport.OutgoingEncryptionLayerImplTest'/>
|
<test name='net.sf.briar.transport.OutgoingEncryptionLayerImplTest'/>
|
||||||
<test name='net.sf.briar.transport.OutgoingSegmentedEncryptionLayerTest'/>
|
<test name='net.sf.briar.transport.OutgoingSegmentedEncryptionLayerTest'/>
|
||||||
|
<test name='net.sf.briar.transport.XorErasureEncoderTest'/>
|
||||||
<test name='net.sf.briar.util.ByteUtilsTest'/>
|
<test name='net.sf.briar.util.ByteUtilsTest'/>
|
||||||
<test name='net.sf.briar.util.FileUtilsTest'/>
|
<test name='net.sf.briar.util.FileUtilsTest'/>
|
||||||
<test name='net.sf.briar.util.StringUtilsTest'/>
|
<test name='net.sf.briar.util.StringUtilsTest'/>
|
||||||
|
|||||||
41
test/net/sf/briar/transport/XorErasureEncoderTest.java
Normal file
41
test/net/sf/briar/transport/XorErasureEncoderTest.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package net.sf.briar.transport;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import net.sf.briar.BriarTestCase;
|
||||||
|
import net.sf.briar.api.transport.Segment;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class XorErasureEncoderTest extends BriarTestCase {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncoding() {
|
||||||
|
// Create a 100-byte frame
|
||||||
|
Frame f = new Frame();
|
||||||
|
f.setLength(100);
|
||||||
|
byte[] b = f.getBuffer();
|
||||||
|
for(int i = 0; i < 100; 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
|
||||||
|
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
|
||||||
|
byte[] b1 = set[0].getBuffer();
|
||||||
|
for(int i = 0; i < 34; i++) assertEquals(i, b1[i]);
|
||||||
|
byte[] b2 = set[1].getBuffer();
|
||||||
|
for(int i = 0; i < 34; i++) assertEquals(i + 34, 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]);
|
||||||
|
// 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++) {
|
||||||
|
expected[i] = (byte) (b1[i] ^ b2[i] ^ b3[i]);
|
||||||
|
}
|
||||||
|
assertArrayEquals(expected, b4);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user