mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +01:00
Cleaned up serial and protocol packages in preparation for user-defined types.
This commit is contained in:
@@ -36,6 +36,7 @@ import net.sf.briar.api.protocol.MessageParser;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.Raw;
|
||||
import net.sf.briar.api.serial.RawByteArray;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
import net.sf.briar.api.serial.ReaderFactory;
|
||||
import net.sf.briar.api.serial.WriterFactory;
|
||||
import net.sf.briar.serial.SerialModule;
|
||||
@@ -125,7 +126,8 @@ public class BundleReadWriteTest extends TestCase {
|
||||
MessageParser messageParser =
|
||||
new MessageParserImpl(keyParser, sig, dig, rf);
|
||||
FileInputStream in = new FileInputStream(bundle);
|
||||
BundleReader r = new BundleReaderImpl(in, rf, keyPair.getPublic(), sig,
|
||||
Reader reader = rf.createReader(in);
|
||||
BundleReader r = new BundleReaderImpl(reader, keyPair.getPublic(), sig,
|
||||
dig, messageParser, new HeaderFactoryImpl(),
|
||||
new BatchFactoryImpl());
|
||||
|
||||
@@ -164,7 +166,8 @@ public class BundleReadWriteTest extends TestCase {
|
||||
MessageParser messageParser =
|
||||
new MessageParserImpl(keyParser, sig, dig, rf);
|
||||
FileInputStream in = new FileInputStream(bundle);
|
||||
BundleReader r = new BundleReaderImpl(in, rf, keyPair.getPublic(), sig,
|
||||
Reader reader = rf.createReader(in);
|
||||
BundleReader r = new BundleReaderImpl(reader, keyPair.getPublic(), sig,
|
||||
dig, messageParser, new HeaderFactoryImpl(),
|
||||
new BatchFactoryImpl());
|
||||
|
||||
|
||||
73
test/net/sf/briar/protocol/ConsumersTest.java
Normal file
73
test/net/sf/briar/protocol/ConsumersTest.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.Signature;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.api.serial.FormatException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConsumersTest extends TestCase {
|
||||
|
||||
private static final String SIGNATURE_ALGO = "SHA256withRSA";
|
||||
private static final String KEY_PAIR_ALGO = "RSA";
|
||||
private static final String DIGEST_ALGO = "SHA-256";
|
||||
|
||||
@Test
|
||||
public void testSigningConsumer() throws Exception {
|
||||
Signature s = Signature.getInstance(SIGNATURE_ALGO);
|
||||
KeyPair k = KeyPairGenerator.getInstance(KEY_PAIR_ALGO).genKeyPair();
|
||||
byte[] data = new byte[1234];
|
||||
// Generate some random data and sign it
|
||||
new Random().nextBytes(data);
|
||||
s.initSign(k.getPrivate());
|
||||
s.update(data);
|
||||
byte[] sig = s.sign();
|
||||
// Check that feeding a SigningConsumer generates the same signature
|
||||
s.initSign(k.getPrivate());
|
||||
SigningConsumer sc = new SigningConsumer(s);
|
||||
sc.write(data[0]);
|
||||
sc.write(data, 1, data.length - 2);
|
||||
sc.write(data[data.length - 1]);
|
||||
byte[] sig1 = s.sign();
|
||||
assertTrue(Arrays.equals(sig, sig1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigestingConsumer() throws Exception {
|
||||
MessageDigest m = MessageDigest.getInstance(DIGEST_ALGO);
|
||||
byte[] data = new byte[1234];
|
||||
// Generate some random data and digest it
|
||||
new Random().nextBytes(data);
|
||||
m.reset();
|
||||
m.update(data);
|
||||
byte[] dig = m.digest();
|
||||
// Check that feeding a DigestingConsumer generates the same digest
|
||||
m.reset();
|
||||
DigestingConsumer dc = new DigestingConsumer(m);
|
||||
dc.write(data[0]);
|
||||
dc.write(data, 1, data.length - 2);
|
||||
dc.write(data[data.length - 1]);
|
||||
byte[] dig1 = m.digest();
|
||||
assertTrue(Arrays.equals(dig, dig1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCountingConsumer() throws Exception {
|
||||
byte[] data = new byte[1234];
|
||||
CountingConsumer cc = new CountingConsumer(data.length);
|
||||
cc.write(data[0]);
|
||||
cc.write(data, 1, data.length - 2);
|
||||
cc.write(data[data.length - 1]);
|
||||
assertEquals(data.length, cc.getCount());
|
||||
try {
|
||||
cc.write((byte) 0);
|
||||
assertTrue(false);
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.Signature;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class SigningStreamTest extends TestCase {
|
||||
|
||||
private static final String SIGNATURE_ALGO = "SHA256withRSA";
|
||||
private static final String KEY_PAIR_ALGO = "RSA";
|
||||
private static final String DIGEST_ALGO = "SHA-256";
|
||||
|
||||
private final KeyPair keyPair;
|
||||
private final Signature sig;
|
||||
private final MessageDigest dig;
|
||||
private final Random random;
|
||||
|
||||
public SigningStreamTest() throws Exception {
|
||||
super();
|
||||
keyPair = KeyPairGenerator.getInstance(KEY_PAIR_ALGO).generateKeyPair();
|
||||
sig = Signature.getInstance(SIGNATURE_ALGO);
|
||||
dig = MessageDigest.getInstance(DIGEST_ALGO);
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOutputStreamOutputMatchesInput() throws Exception {
|
||||
byte[] input = new byte[1000];
|
||||
random.nextBytes(input);
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
SigningDigestingOutputStream signOut =
|
||||
new SigningDigestingOutputStream(out, sig, dig);
|
||||
sig.initSign(keyPair.getPrivate());
|
||||
|
||||
signOut.setSigning(true);
|
||||
signOut.write(input, 0, 500);
|
||||
signOut.setSigning(false);
|
||||
signOut.write(input, 500, 250);
|
||||
signOut.setSigning(true);
|
||||
signOut.write(input, 750, 250);
|
||||
|
||||
byte[] output = out.toByteArray();
|
||||
assertTrue(Arrays.equals(input, output));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputStreamOutputMatchesInput() throws Exception {
|
||||
byte[] input = new byte[1000];
|
||||
random.nextBytes(input);
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(input);
|
||||
SigningDigestingInputStream signIn =
|
||||
new SigningDigestingInputStream(in, sig, dig);
|
||||
sig.initVerify(keyPair.getPublic());
|
||||
|
||||
byte[] output = new byte[1000];
|
||||
signIn.setSigning(true);
|
||||
assertEquals(500, signIn.read(output, 0, 500));
|
||||
signIn.setSigning(false);
|
||||
assertEquals(250, signIn.read(output, 500, 250));
|
||||
signIn.setSigning(true);
|
||||
assertEquals(250, signIn.read(output, 750, 250));
|
||||
|
||||
assertTrue(Arrays.equals(input, output));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVerificationLagsByOneByte() throws Exception {
|
||||
byte[] input = new byte[1000];
|
||||
random.nextBytes(input);
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
SigningDigestingOutputStream signOut =
|
||||
new SigningDigestingOutputStream(out, sig, dig);
|
||||
sig.initSign(keyPair.getPrivate());
|
||||
|
||||
// Sign bytes 0-499, skip bytes 500-749, sign bytes 750-999
|
||||
signOut.setSigning(true);
|
||||
signOut.write(input, 0, 500);
|
||||
signOut.setSigning(false);
|
||||
signOut.write(input, 500, 250);
|
||||
signOut.setSigning(true);
|
||||
signOut.write(input, 750, 250);
|
||||
|
||||
byte[] signature = sig.sign();
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(input);
|
||||
SigningDigestingInputStream signIn =
|
||||
new SigningDigestingInputStream(in, sig, dig);
|
||||
sig.initVerify(keyPair.getPublic());
|
||||
|
||||
byte[] output = new byte[1000];
|
||||
// Consume a lookahead byte
|
||||
assertEquals(1, signIn.read(output, 0, 1));
|
||||
// All the offsets are increased by 1 because of the lookahead byte
|
||||
signIn.setSigning(true);
|
||||
assertEquals(500, signIn.read(output, 1, 500));
|
||||
signIn.setSigning(false);
|
||||
assertEquals(250, signIn.read(output, 501, 250));
|
||||
signIn.setSigning(true);
|
||||
assertEquals(249, signIn.read(output, 751, 249));
|
||||
// Have to reach EOF for the lookahead byte to be processed
|
||||
assertEquals(-1, signIn.read());
|
||||
|
||||
assertTrue(Arrays.equals(input, output));
|
||||
assertTrue(sig.verify(signature));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigestionLagsByOneByte() throws Exception {
|
||||
byte[] input = new byte[1000];
|
||||
random.nextBytes(input);
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
SigningDigestingOutputStream signOut =
|
||||
new SigningDigestingOutputStream(out, sig, dig);
|
||||
dig.reset();
|
||||
|
||||
// Digest bytes 0-499, skip bytes 500-749, digest bytes 750-999
|
||||
signOut.setDigesting(true);
|
||||
signOut.write(input, 0, 500);
|
||||
signOut.setDigesting(false);
|
||||
signOut.write(input, 500, 250);
|
||||
signOut.setDigesting(true);
|
||||
signOut.write(input, 750, 250);
|
||||
|
||||
byte[] hash = dig.digest();
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(input);
|
||||
SigningDigestingInputStream signIn =
|
||||
new SigningDigestingInputStream(in, sig, dig);
|
||||
dig.reset();
|
||||
|
||||
byte[] output = new byte[1000];
|
||||
// Consume a lookahead byte
|
||||
assertEquals(1, signIn.read(output, 0, 1));
|
||||
// All the offsets are increased by 1 because of the lookahead byte
|
||||
signIn.setDigesting(true);
|
||||
assertEquals(500, signIn.read(output, 1, 500));
|
||||
signIn.setDigesting(false);
|
||||
assertEquals(250, signIn.read(output, 501, 250));
|
||||
signIn.setDigesting(true);
|
||||
assertEquals(249, signIn.read(output, 751, 249));
|
||||
// Have to reach EOF for the lookahead byte to be processed
|
||||
assertEquals(-1, signIn.read());
|
||||
|
||||
assertTrue(Arrays.equals(input, output));
|
||||
assertTrue(Arrays.equals(hash, dig.digest()));
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,11 @@ package net.sf.briar.serial;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.api.serial.FormatException;
|
||||
import net.sf.briar.api.serial.Raw;
|
||||
import net.sf.briar.api.serial.RawByteArray;
|
||||
import net.sf.briar.util.StringUtils;
|
||||
@@ -121,38 +119,11 @@ public class ReaderImplTest extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUtf8() throws IOException {
|
||||
public void testReadString() throws IOException {
|
||||
setContents("F703666F6F" + "F703666F6F" + "F700");
|
||||
assertEquals("foo", r.readUtf8());
|
||||
assertEquals("foo", r.readUtf8());
|
||||
assertEquals("", r.readUtf8());
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUtf8LimitNotExceeded() throws IOException {
|
||||
setContents("F703666F6F");
|
||||
r.setReadLimit(3);
|
||||
assertEquals("foo", r.readUtf8());
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUtf8LimitExceeded() throws IOException {
|
||||
setContents("F703666F6F");
|
||||
r.setReadLimit(2);
|
||||
try {
|
||||
r.readUtf8();
|
||||
assertTrue(false);
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUtf8LimitReset() throws IOException {
|
||||
setContents("F703666F6F");
|
||||
r.setReadLimit(2);
|
||||
r.resetReadLimit();
|
||||
assertEquals("foo", r.readUtf8());
|
||||
assertEquals("foo", r.readString());
|
||||
assertEquals("foo", r.readString());
|
||||
assertEquals("", r.readString());
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@@ -165,33 +136,6 @@ public class ReaderImplTest extends TestCase {
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadRawLimitNotExceeded() throws IOException {
|
||||
setContents("F603010203");
|
||||
r.setReadLimit(3);
|
||||
assertTrue(Arrays.equals(new byte[] {1, 2, 3}, r.readRaw()));
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadRawMaxLengthExceeded() throws IOException {
|
||||
setContents("F603010203");
|
||||
r.setReadLimit(2);
|
||||
try {
|
||||
r.readRaw();
|
||||
assertTrue(false);
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadRawLimitReset() throws IOException {
|
||||
setContents("F603010203");
|
||||
r.setReadLimit(2);
|
||||
r.resetReadLimit();
|
||||
assertTrue(Arrays.equals(new byte[] {1, 2, 3}, r.readRaw()));
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadDefiniteList() throws IOException {
|
||||
setContents("F5" + "03" + "01" + "F703666F6F" + "FC0080");
|
||||
@@ -261,7 +205,7 @@ public class ReaderImplTest extends TestCase {
|
||||
assertFalse(r.hasListEnd());
|
||||
assertEquals((byte) 1, r.readIntAny());
|
||||
assertFalse(r.hasListEnd());
|
||||
assertEquals("foo", r.readUtf8());
|
||||
assertEquals("foo", r.readString());
|
||||
assertFalse(r.hasListEnd());
|
||||
assertEquals((short) 128, r.readIntAny());
|
||||
assertTrue(r.hasListEnd());
|
||||
@@ -300,7 +244,7 @@ public class ReaderImplTest extends TestCase {
|
||||
assertTrue(r.hasMapStart());
|
||||
r.readMapStart();
|
||||
assertFalse(r.hasMapEnd());
|
||||
assertEquals("foo", r.readUtf8());
|
||||
assertEquals("foo", r.readString());
|
||||
assertFalse(r.hasMapEnd());
|
||||
assertEquals((byte) 123, r.readIntAny());
|
||||
assertFalse(r.hasMapEnd());
|
||||
@@ -345,25 +289,10 @@ public class ReaderImplTest extends TestCase {
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRawBytesRead() throws IOException {
|
||||
setContents("F4" + "00" + "F4" + "00");
|
||||
assertEquals(0L, r.getRawBytesRead());
|
||||
Map<Object, Object> m = r.readMap(Object.class, Object.class);
|
||||
assertEquals(2L, r.getRawBytesRead());
|
||||
assertEquals(Collections.emptyMap(), m);
|
||||
m = r.readMap(Object.class, Object.class);
|
||||
assertEquals(4L, r.getRawBytesRead());
|
||||
assertEquals(Collections.emptyMap(), m);
|
||||
assertTrue(r.eof());
|
||||
assertEquals(4L, r.getRawBytesRead());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadEmptyInput() throws IOException {
|
||||
setContents("");
|
||||
assertTrue(r.eof());
|
||||
assertEquals(0L, r.getRawBytesRead());
|
||||
}
|
||||
|
||||
private void setContents(String hex) {
|
||||
|
||||
@@ -129,7 +129,7 @@ public class WriterImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testWriteUtf8() throws IOException {
|
||||
w.writeUtf8("foo");
|
||||
w.writeString("foo");
|
||||
// UTF-8 tag, length as uint7, UTF-8 bytes
|
||||
checkContents("F7" + "03" + "666F6F");
|
||||
}
|
||||
@@ -170,7 +170,7 @@ public class WriterImplTest extends TestCase {
|
||||
public void testWriteIndefiniteList() throws IOException {
|
||||
w.writeListStart();
|
||||
w.writeIntAny((byte) 1); // Written as uint7
|
||||
w.writeUtf8("foo");
|
||||
w.writeString("foo");
|
||||
w.writeIntAny(128L); // Written as an int16
|
||||
w.writeListEnd();
|
||||
checkContents("F3" + "01" + "F703666F6F" + "FC0080" + "F1");
|
||||
@@ -179,7 +179,7 @@ public class WriterImplTest extends TestCase {
|
||||
@Test
|
||||
public void testWriteIndefiniteMap() throws IOException {
|
||||
w.writeMapStart();
|
||||
w.writeUtf8("foo");
|
||||
w.writeString("foo");
|
||||
w.writeIntAny(123); // Written as a uint7
|
||||
w.writeRaw(new byte[] {});
|
||||
w.writeNull();
|
||||
@@ -212,6 +212,6 @@ public class WriterImplTest extends TestCase {
|
||||
byte[] expected = StringUtils.fromHexString(hex);
|
||||
assertTrue(StringUtils.toHexString(out.toByteArray()),
|
||||
Arrays.equals(expected, out.toByteArray()));
|
||||
assertEquals(expected.length, w.getRawBytesWritten());
|
||||
assertEquals(expected.length, w.getBytesWritten());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user