Merge branch 'client-helper' into 'master'

Helper class to reduce client boilerplate

* Renamed BdfReader methods for consistency with BdfList/BdfDictionary
* Added readList() and readDictionary() methods to BdfReader
* Added ClientHelper to reduce boilerplate when converting messages and metadata to and from BDF
* Moved PrivateGroupFactory to the same package as ClientHelper


See merge request !114
This commit is contained in:
akwizgran
2016-02-29 14:37:45 +00:00
23 changed files with 552 additions and 126 deletions

View File

@@ -0,0 +1,14 @@
package org.briarproject.clients;
import org.briarproject.BriarTestCase;
import org.junit.Test;
import static org.junit.Assert.fail;
public class ClientHelperImplTest extends BriarTestCase {
@Test
public void testUnitTestsExist() {
fail(); // FIXME: Write tests
}
}

View File

@@ -3,11 +3,14 @@ package org.briarproject.data;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.FormatException;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.util.StringUtils;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import static org.briarproject.api.data.BdfDictionary.NULL_VALUE;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -54,100 +57,102 @@ public class BdfReaderImplTest extends BriarTestCase {
}
@Test
public void testReadInt8() throws Exception {
public void testReadLong8() throws Exception {
setContents("21" + "00" + "21" + "FF"
+ "21" + "7F" + "21" + "80");
assertEquals(0, r.readInteger());
assertEquals(-1, r.readInteger());
assertEquals(Byte.MAX_VALUE, r.readInteger());
assertEquals(Byte.MIN_VALUE, r.readInteger());
assertEquals(0, r.readLong());
assertEquals(-1, r.readLong());
assertEquals(Byte.MAX_VALUE, r.readLong());
assertEquals(Byte.MIN_VALUE, r.readLong());
assertTrue(r.eof());
}
@Test
public void testSkipInt8() throws Exception {
public void testSkipLong8() throws Exception {
setContents("21" + "00");
r.skipInteger();
r.skipLong();
assertTrue(r.eof());
}
@Test
public void testReadInt16() throws Exception {
public void testReadLong16() throws Exception {
setContents("22" + "0080" + "22" + "FF7F"
+ "22" + "7FFF" + "22" + "8000");
assertEquals(Byte.MAX_VALUE + 1, r.readInteger());
assertEquals(Byte.MIN_VALUE - 1, r.readInteger());
assertEquals(Short.MAX_VALUE, r.readInteger());
assertEquals(Short.MIN_VALUE, r.readInteger());
assertEquals(Byte.MAX_VALUE + 1, r.readLong());
assertEquals(Byte.MIN_VALUE - 1, r.readLong());
assertEquals(Short.MAX_VALUE, r.readLong());
assertEquals(Short.MIN_VALUE, r.readLong());
assertTrue(r.eof());
}
@Test
public void testSkipInt16() throws Exception {
public void testSkipLong16() throws Exception {
setContents("22" + "0080");
r.skipInteger();
r.skipLong();
assertTrue(r.eof());
}
@Test
public void testReadInt32() throws Exception {
public void testReadLong32() throws Exception {
setContents("24" + "00008000" + "24" + "FFFF7FFF"
+ "24" + "7FFFFFFF" + "24" + "80000000");
assertEquals(Short.MAX_VALUE + 1, r.readInteger());
assertEquals(Short.MIN_VALUE - 1, r.readInteger());
assertEquals(Integer.MAX_VALUE, r.readInteger());
assertEquals(Integer.MIN_VALUE, r.readInteger());
assertEquals(Short.MAX_VALUE + 1, r.readLong());
assertEquals(Short.MIN_VALUE - 1, r.readLong());
assertEquals(Integer.MAX_VALUE, r.readLong());
assertEquals(Integer.MIN_VALUE, r.readLong());
assertTrue(r.eof());
}
@Test
public void testSkipInt32() throws Exception {
public void testSkipLong32() throws Exception {
setContents("24" + "00008000");
r.skipInteger();
r.skipLong();
assertTrue(r.eof());
}
@Test
public void testReadInt64() throws Exception {
public void testReadLong64() throws Exception {
setContents("28" + "0000000080000000" + "28" + "FFFFFFFF7FFFFFFF"
+ "28" + "7FFFFFFFFFFFFFFF" + "28" + "8000000000000000");
assertEquals(Integer.MAX_VALUE + 1L, r.readInteger());
assertEquals(Integer.MIN_VALUE - 1L, r.readInteger());
assertEquals(Long.MAX_VALUE, r.readInteger());
assertEquals(Long.MIN_VALUE, r.readInteger());
assertEquals(Integer.MAX_VALUE + 1L, r.readLong());
assertEquals(Integer.MIN_VALUE - 1L, r.readLong());
assertEquals(Long.MAX_VALUE, r.readLong());
assertEquals(Long.MIN_VALUE, r.readLong());
assertTrue(r.eof());
}
@Test
public void testSkipInt64() throws Exception {
public void testSkipLong() throws Exception {
setContents("28" + "0000000080000000");
r.skipInteger();
r.skipLong();
assertTrue(r.eof());
}
@Test
public void testReadFloat() throws Exception {
public void testReadDouble() throws Exception {
// http://babbage.cs.qc.edu/IEEE-754/Decimal.html
// http://steve.hollasch.net/cgindex/coding/ieeefloat.html
setContents("38" + "0000000000000000" + "38" + "3FF0000000000000"
+ "38" + "4000000000000000" + "38" + "BFF0000000000000"
+ "38" + "8000000000000000" + "38" + "FFF0000000000000"
+ "38" + "7FF0000000000000" + "38" + "7FF8000000000000");
assertEquals(0, Double.compare(0.0, r.readFloat()));
assertEquals(0, Double.compare(1.0, r.readFloat()));
assertEquals(0, Double.compare(2.0, r.readFloat()));
assertEquals(0, Double.compare(-1.0, r.readFloat()));
assertEquals(0, Double.compare(-0.0, r.readFloat()));
assertEquals(0, Double.compare(Double.NEGATIVE_INFINITY, r.readFloat()));
assertEquals(0, Double.compare(Double.POSITIVE_INFINITY, r.readFloat()));
assertTrue(Double.isNaN(r.readFloat()));
assertEquals(0, Double.compare(0.0, r.readDouble()));
assertEquals(0, Double.compare(1.0, r.readDouble()));
assertEquals(0, Double.compare(2.0, r.readDouble()));
assertEquals(0, Double.compare(-1.0, r.readDouble()));
assertEquals(0, Double.compare(-0.0, r.readDouble()));
assertEquals(0, Double.compare(Double.NEGATIVE_INFINITY,
r.readDouble()));
assertEquals(0, Double.compare(Double.POSITIVE_INFINITY,
r.readDouble()));
assertTrue(Double.isNaN(r.readDouble()));
assertTrue(r.eof());
}
@Test
public void testSkipFloat() throws Exception {
setContents("38" + "0000000000000000");
r.skipFloat();
r.skipDouble();
assertTrue(r.eof());
}
@@ -377,17 +382,31 @@ public class BdfReaderImplTest extends BriarTestCase {
@Test
public void testReadList() throws Exception {
// A list containing 1, "foo", and 128
// A list containing 1, "foo", and null
setContents("60" + "21" + "01" +
"41" + "03" + "666F6F" +
"22" + "0080" + "80");
"00" + "80");
BdfList list = r.readList();
assertEquals(3, list.size());
assertEquals(1L, list.get(0));
assertEquals("foo", list.get(1));
assertEquals(NULL_VALUE, list.get(2));
}
@Test
public void testReadListManually() throws Exception {
// A list containing 1, "foo", and null
setContents("60" + "21" + "01" +
"41" + "03" + "666F6F" +
"00" + "80");
r.readListStart();
assertFalse(r.hasListEnd());
assertEquals(1, r.readInteger());
assertEquals(1, r.readLong());
assertFalse(r.hasListEnd());
assertEquals("foo", r.readString(1000));
assertFalse(r.hasListEnd());
assertEquals(128, r.readInteger());
assertTrue(r.hasNull());
r.readNull();
assertTrue(r.hasListEnd());
r.readListEnd();
assertTrue(r.eof());
@@ -405,6 +424,19 @@ public class BdfReaderImplTest extends BriarTestCase {
@Test
public void testReadDictionary() throws Exception {
// A dictionary containing "foo" -> 123 and "bar" -> null
setContents("70" + "41" + "03" + "666F6F" + "21" + "7B" +
"41" + "03" + "626172" + "00" + "80");
BdfDictionary dictionary = r.readDictionary();
assertEquals(2, dictionary.size());
assertTrue(dictionary.containsKey("foo"));
assertEquals(123L, dictionary.get("foo"));
assertTrue(dictionary.containsKey("bar"));
assertEquals(NULL_VALUE, dictionary.get("bar"));
}
@Test
public void testReadDictionaryManually() throws Exception {
// A dictionary containing "foo" -> 123 and "bar" -> null
setContents("70" + "41" + "03" + "666F6F" + "21" + "7B" +
"41" + "03" + "626172" + "00" + "80");
@@ -412,7 +444,7 @@ public class BdfReaderImplTest extends BriarTestCase {
assertFalse(r.hasDictionaryEnd());
assertEquals("foo", r.readString(1000));
assertFalse(r.hasDictionaryEnd());
assertEquals(123, r.readInteger());
assertEquals(123, r.readLong());
assertFalse(r.hasDictionaryEnd());
assertEquals("bar", r.readString(1000));
assertFalse(r.hasDictionaryEnd());

View File

@@ -13,6 +13,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.briarproject.api.data.BdfDictionary.NULL_VALUE;
import static org.junit.Assert.assertArrayEquals;
public class BdfWriterImplTest extends BriarTestCase {
@@ -41,17 +42,17 @@ public class BdfWriterImplTest extends BriarTestCase {
}
@Test
public void testWriteInteger() throws IOException {
w.writeInteger(0);
w.writeInteger(-1);
w.writeInteger(Byte.MAX_VALUE);
w.writeInteger(Byte.MIN_VALUE);
w.writeInteger(Short.MAX_VALUE);
w.writeInteger(Short.MIN_VALUE);
w.writeInteger(Integer.MAX_VALUE);
w.writeInteger(Integer.MIN_VALUE);
w.writeInteger(Long.MAX_VALUE);
w.writeInteger(Long.MIN_VALUE);
public void testWriteLong() throws IOException {
w.writeLong(0);
w.writeLong(-1);
w.writeLong(Byte.MAX_VALUE);
w.writeLong(Byte.MIN_VALUE);
w.writeLong(Short.MAX_VALUE);
w.writeLong(Short.MIN_VALUE);
w.writeLong(Integer.MAX_VALUE);
w.writeLong(Integer.MIN_VALUE);
w.writeLong(Long.MAX_VALUE);
w.writeLong(Long.MIN_VALUE);
// INTEGER_8 tag, 0, INTEGER_8 tag, -1, etc
checkContents("21" + "00" + "21" + "FF" +
"21" + "7F" + "21" + "80" +
@@ -61,17 +62,17 @@ public class BdfWriterImplTest extends BriarTestCase {
}
@Test
public void testWriteFloat() throws IOException {
public void testWriteDouble() throws IOException {
// http://babbage.cs.qc.edu/IEEE-754/Decimal.html
// 1 bit for sign, 11 for exponent, 52 for significand
w.writeFloat(0.0); // 0 0 0 -> 0x0000000000000000
w.writeFloat(1.0); // 0 1023 1 -> 0x3FF0000000000000
w.writeFloat(2.0); // 0 1024 1 -> 0x4000000000000000
w.writeFloat(-1.0); // 1 1023 1 -> 0xBFF0000000000000
w.writeFloat(-0.0); // 1 0 0 -> 0x8000000000000000
w.writeFloat(Double.NEGATIVE_INFINITY); // 1 2047 0 -> 0xFFF00000...
w.writeFloat(Double.POSITIVE_INFINITY); // 0 2047 0 -> 0x7FF00000...
w.writeFloat(Double.NaN); // 0 2047 1 -> 0x7FF8000000000000
w.writeDouble(0.0); // 0 0 0 -> 0x0000000000000000
w.writeDouble(1.0); // 0 1023 1 -> 0x3FF0000000000000
w.writeDouble(2.0); // 0 1024 1 -> 0x4000000000000000
w.writeDouble(-1.0); // 1 1023 1 -> 0xBFF0000000000000
w.writeDouble(-0.0); // 1 0 0 -> 0x8000000000000000
w.writeDouble(Double.NEGATIVE_INFINITY); // 1 2047 0 -> 0xFFF00000...
w.writeDouble(Double.POSITIVE_INFINITY); // 0 2047 0 -> 0x7FF00000...
w.writeDouble(Double.NaN); // 0 2047 1 -> 0x7FF8000000000000
checkContents("38" + "0000000000000000" + "38" + "3FF0000000000000"
+ "38" + "4000000000000000" + "38" + "BFF0000000000000"
+ "38" + "8000000000000000" + "38" + "FFF0000000000000"
@@ -122,7 +123,7 @@ public class BdfWriterImplTest extends BriarTestCase {
}
@Test
public void testWriteBytes8() throws IOException {
public void testWriteRaw8() throws IOException {
byte[] longest = new byte[Byte.MAX_VALUE];
String longHex = StringUtils.toHexString(longest);
w.writeRaw(new byte[] {1, 2, 3});
@@ -132,7 +133,7 @@ public class BdfWriterImplTest extends BriarTestCase {
}
@Test
public void testWriteBytes16() throws IOException {
public void testWriteRaw16() throws IOException {
byte[] shortest = new byte[Byte.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest);
byte[] longest = new byte[Short.MAX_VALUE];
@@ -144,7 +145,7 @@ public class BdfWriterImplTest extends BriarTestCase {
}
@Test
public void testWriteBytes32() throws IOException {
public void testWriteRaw32() throws IOException {
byte[] shortest = new byte[Short.MAX_VALUE + 1];
String shortHex = StringUtils.toHexString(shortest);
w.writeRaw(shortest);
@@ -166,10 +167,11 @@ public class BdfWriterImplTest extends BriarTestCase {
List<Object> l = new ArrayList<Object>();
l.add(1);
l.add(null);
l.add(NULL_VALUE);
l.add(2);
w.writeList(l);
// LIST tag, 1 as integer, NULL tag, 2 as integer, END tag
checkContents("60" + "21" + "01" + "00" + "21" + "02" + "80");
// LIST tag, 1 as integer, NULL tag, NULL tag, 2 as integer, END tag
checkContents("60" + "21" + "01" + "00" + "00" + "21" + "02" + "80");
}
@Test
@@ -188,9 +190,9 @@ public class BdfWriterImplTest extends BriarTestCase {
@Test
public void testWriteDelimitedList() throws IOException {
w.writeListStart();
w.writeInteger(1);
w.writeLong(1);
w.writeString("foo");
w.writeInteger(128);
w.writeLong(128);
w.writeListEnd();
// LIST tag, 1 as integer, "foo" as string, 128 as integer, END tag
checkContents("60" + "21" + "01" +
@@ -202,7 +204,7 @@ public class BdfWriterImplTest extends BriarTestCase {
public void testWriteDelimitedDictionary() throws IOException {
w.writeDictionaryStart();
w.writeString("foo");
w.writeInteger(123);
w.writeLong(123);
w.writeString("bar");
w.writeNull();
w.writeDictionaryEnd();

View File

@@ -23,6 +23,7 @@ import org.briarproject.api.messaging.PrivateMessage;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import org.briarproject.clients.ClientsModule;
import org.briarproject.contact.ContactModule;
import org.briarproject.crypto.CryptoModule;
import org.briarproject.data.DataModule;
@@ -56,10 +57,10 @@ public class ConstantsTest extends BriarTestCase {
public ConstantsTest() throws Exception {
Injector i = Guice.createInjector(new TestDatabaseModule(),
new TestLifecycleModule(), new TestSystemModule(),
new ContactModule(), new CryptoModule(), new DatabaseModule(),
new DataModule(), new EventModule(), new ForumModule(),
new IdentityModule(), new MessagingModule(), new SyncModule(),
new TransportModule());
new ClientsModule(), new ContactModule(), new CryptoModule(),
new DatabaseModule(), new DataModule(), new EventModule(),
new ForumModule(), new IdentityModule(), new MessagingModule(),
new SyncModule(), new TransportModule());
crypto = i.getInstance(CryptoComponent.class);
authorFactory = i.getInstance(AuthorFactory.class);
privateMessageFactory = i.getInstance(PrivateMessageFactory.class);

View File

@@ -36,6 +36,7 @@ import org.briarproject.api.transport.KeyManager;
import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriterFactory;
import org.briarproject.clients.ClientsModule;
import org.briarproject.contact.ContactModule;
import org.briarproject.crypto.CryptoModule;
import org.briarproject.data.DataModule;
@@ -86,10 +87,11 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
private Injector createInjector(File dir) {
return Guice.createInjector(new TestDatabaseModule(dir),
new TestSystemModule(), new ContactModule(), new CryptoModule(),
new DatabaseModule(), new DataModule(), new EventModule(),
new IdentityModule(), new LifecycleModule(),
new MessagingModule(), new SyncModule(), new TransportModule());
new TestSystemModule(), new ClientsModule(),
new ContactModule(), new CryptoModule(), new DatabaseModule(),
new DataModule(), new EventModule(), new IdentityModule(),
new LifecycleModule(), new MessagingModule(), new SyncModule(),
new TransportModule());
}
@Test