Move all unit tests to their modules and remove briar-tests

This commit is contained in:
Torsten Grote
2016-12-12 17:06:30 -02:00
parent 32be148c7a
commit 1081a08ea9
130 changed files with 441 additions and 433 deletions

View File

@@ -0,0 +1,15 @@
package org.briarproject.bramble;
import org.jmock.Mockery;
import org.junit.After;
public abstract class BrambleMockTestCase extends
BrambleTestCase {
protected final Mockery context = new Mockery();
@After
public void checkExpectations() {
context.assertIsSatisfied();
}
}

View File

@@ -0,0 +1,20 @@
package org.briarproject.bramble;
import java.lang.Thread.UncaughtExceptionHandler;
import static org.junit.Assert.fail;
public abstract class BrambleTestCase {
public BrambleTestCase() {
// Ensure exceptions thrown on worker threads cause tests to fail
UncaughtExceptionHandler fail = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
throwable.printStackTrace();
fail();
}
};
Thread.setDefaultUncaughtExceptionHandler(fail);
}
}

View File

@@ -0,0 +1,14 @@
package org.briarproject.bramble;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.concurrent.Executor;
@NotNullByDefault
public class ImmediateExecutor implements Executor {
@Override
public void execute(Runnable r) {
r.run();
}
}

View File

@@ -0,0 +1,57 @@
package org.briarproject.bramble;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.File;
@NotNullByDefault
public class TestDatabaseConfig implements DatabaseConfig {
private final File dir;
private final long maxSize;
private volatile SecretKey key = new SecretKey(new byte[SecretKey.LENGTH]);
public TestDatabaseConfig(File dir, long maxSize) {
this.dir = dir;
this.maxSize = maxSize;
}
@Override
public boolean databaseExists() {
if (!dir.isDirectory()) return false;
File[] files = dir.listFiles();
return files != null && files.length > 0;
}
@Override
public File getDatabaseDirectory() {
return dir;
}
@Override
public void setEncryptionKey(SecretKey key) {
this.key = key;
}
@Override
public SecretKey getEncryptionKey() {
return key;
}
@Override
public void setLocalAuthorName(String nickname) {
}
@Override
public String getLocalAuthorName() {
return null;
}
@Override
public long getMaxSize() {
return maxSize;
}
}

View File

@@ -0,0 +1,38 @@
package org.briarproject.bramble;
import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.db.DatabaseExecutor;
import java.io.File;
import java.util.concurrent.Executor;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class TestDatabaseModule {
private final DatabaseConfig config;
public TestDatabaseModule() {
this(new File("."));
}
public TestDatabaseModule(File dir) {
config = new TestDatabaseConfig(dir, Long.MAX_VALUE);
}
@Provides
DatabaseConfig provideDatabaseConfig() {
return config;
}
@Provides
@Singleton
@DatabaseExecutor
Executor provideDatabaseExecutor() {
return new ImmediateExecutor();
}
}

View File

@@ -0,0 +1,13 @@
package org.briarproject.bramble;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.system.SeedProvider;
@NotNullByDefault
public class TestSeedProvider implements SeedProvider {
@Override
public byte[] getSeed() {
return TestUtils.getRandomBytes(32);
}
}

View File

@@ -0,0 +1,18 @@
package org.briarproject.bramble;
import org.briarproject.bramble.api.system.SeedProvider;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class TestSeedProviderModule {
@Provides
@Singleton
SeedProvider provideSeedProvider() {
return new TestSeedProvider();
}
}

View File

@@ -0,0 +1,48 @@
package org.briarproject.bramble;
import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.util.IoUtils;
import java.io.File;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
public class TestUtils {
private static final AtomicInteger nextTestDir =
new AtomicInteger((int) (Math.random() * 1000 * 1000));
private static final Random random = new Random();
public static File getTestDirectory() {
int name = nextTestDir.getAndIncrement();
return new File("test.tmp/" + name);
}
public static void deleteTestDirectory(File testDir) {
IoUtils.deleteFileOrDir(testDir);
testDir.getParentFile().delete(); // Delete if empty
}
public static byte[] getRandomBytes(int length) {
byte[] b = new byte[length];
random.nextBytes(b);
return b;
}
public static byte[] getRandomId() {
return getRandomBytes(UniqueId.LENGTH);
}
public static String getRandomString(int length) {
char[] c = new char[length];
for (int i = 0; i < length; i++)
c[i] = (char) ('a' + random.nextInt(26));
return new String(c);
}
public static SecretKey getSecretKey() {
return new SecretKey(getRandomBytes(SecretKey.LENGTH));
}
}

View File

@@ -0,0 +1,35 @@
package org.briarproject.bramble;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.data.MetadataEncoder;
import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.sync.ClientId;
import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.system.Clock;
public abstract class ValidatorTestCase extends BrambleMockTestCase {
protected final ClientHelper clientHelper =
context.mock(ClientHelper.class);
protected final MetadataEncoder metadataEncoder =
context.mock(MetadataEncoder.class);
protected final Clock clock = context.mock(Clock.class);
protected final AuthorFactory authorFactory =
context.mock(AuthorFactory.class);
protected final MessageId messageId =
new MessageId(TestUtils.getRandomId());
protected final GroupId groupId = new GroupId(TestUtils.getRandomId());
protected final long timestamp = 1234567890 * 1000L;
protected final byte[] raw = TestUtils.getRandomBytes(123);
protected final Message message =
new Message(messageId, groupId, timestamp, raw);
protected final ClientId clientId =
new ClientId(TestUtils.getRandomString(123));
protected final byte[] descriptor = TestUtils.getRandomBytes(123);
protected final Group group = new Group(groupId, clientId, descriptor);
}

View File

@@ -0,0 +1,133 @@
package org.briarproject.bramble.api.data;
import org.briarproject.bramble.BrambleTestCase;
import org.briarproject.bramble.api.Bytes;
import org.junit.Test;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map.Entry;
import static org.briarproject.bramble.api.data.BdfDictionary.NULL_VALUE;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class BdfDictionaryTest extends BrambleTestCase {
@Test
public void testConstructors() {
assertEquals(Collections.<String, Object>emptyMap(),
new BdfDictionary());
assertEquals(Collections.singletonMap("foo", NULL_VALUE),
new BdfDictionary(Collections.singletonMap("foo", NULL_VALUE)));
}
@Test
public void testFactoryMethod() {
assertEquals(Collections.<String, Object>emptyMap(),
BdfDictionary.of());
assertEquals(Collections.singletonMap("foo", NULL_VALUE),
BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)));
}
@Test
public void testIntegerPromotion() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("foo", (byte) 1);
d.put("bar", (short) 2);
d.put("baz", 3);
d.put("bam", 4L);
assertEquals(Long.valueOf(1), d.getLong("foo"));
assertEquals(Long.valueOf(2), d.getLong("bar"));
assertEquals(Long.valueOf(3), d.getLong("baz"));
assertEquals(Long.valueOf(4), d.getLong("bam"));
}
@Test
public void testFloatPromotion() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("foo", 1F);
d.put("bar", 2D);
assertEquals(Double.valueOf(1), d.getDouble("foo"));
assertEquals(Double.valueOf(2), d.getDouble("bar"));
}
@Test
public void testByteArrayUnwrapping() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("foo", new byte[123]);
d.put("bar", new Bytes(new byte[123]));
byte[] foo = d.getRaw("foo");
assertEquals(123, foo.length);
assertArrayEquals(new byte[123], foo);
byte[] bar = d.getRaw("bar");
assertEquals(123, bar.length);
assertArrayEquals(new byte[123], bar);
}
@Test
public void testKeySetIteratorIsOrderedByKeys() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("a", 1);
d.put("d", 4);
d.put("b", 2);
d.put("c", 3);
// Keys should be returned in their natural order
Iterator<String> it = d.keySet().iterator();
assertTrue(it.hasNext());
assertEquals("a", it.next());
assertTrue(it.hasNext());
assertEquals("b", it.next());
assertTrue(it.hasNext());
assertEquals("c", it.next());
assertTrue(it.hasNext());
assertEquals("d", it.next());
}
@Test
public void testValuesIteratorIsOrderedByKeys() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("a", 1);
d.put("d", 4);
d.put("b", 2);
d.put("c", 3);
// Values should be returned in the natural order of their keys
Iterator<Object> it = d.values().iterator();
assertTrue(it.hasNext());
assertEquals(1, it.next());
assertTrue(it.hasNext());
assertEquals(2, it.next());
assertTrue(it.hasNext());
assertEquals(3, it.next());
assertTrue(it.hasNext());
assertEquals(4, it.next());
}
@Test
public void testEntrySetIteratorIsOrderedByKeys() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("a", 1);
d.put("d", 4);
d.put("b", 2);
d.put("c", 3);
// Entries should be returned in the natural order of their keys
Iterator<Entry<String, Object>> it = d.entrySet().iterator();
assertTrue(it.hasNext());
Entry<String, Object> e = it.next();
assertEquals("a", e.getKey());
assertEquals(1, e.getValue());
assertTrue(it.hasNext());
e = it.next();
assertEquals("b", e.getKey());
assertEquals(2, e.getValue());
assertTrue(it.hasNext());
e = it.next();
assertEquals("c", e.getKey());
assertEquals(3, e.getValue());
assertTrue(it.hasNext());
e = it.next();
assertEquals("d", e.getKey());
assertEquals(4, e.getValue());
}
}

View File

@@ -0,0 +1,369 @@
package org.briarproject.bramble.api.data;
import org.briarproject.bramble.BrambleTestCase;
import org.briarproject.bramble.api.Bytes;
import org.briarproject.bramble.api.FormatException;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import static org.briarproject.bramble.api.data.BdfDictionary.NULL_VALUE;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class BdfListTest extends BrambleTestCase {
@Test
public void testConstructors() {
assertEquals(Collections.emptyList(), new BdfList());
assertEquals(Arrays.asList(1, 2, NULL_VALUE),
new BdfList(Arrays.asList(1, 2, NULL_VALUE)));
}
@Test
public void testFactoryMethod() {
assertEquals(Collections.emptyList(), BdfList.of());
assertEquals(Arrays.asList(1, 2, NULL_VALUE),
BdfList.of(1, 2, NULL_VALUE));
}
@Test
public void testIntegerPromotion() throws Exception {
BdfList list = new BdfList();
list.add((byte) 1);
list.add((short) 2);
list.add(3);
list.add(4L);
assertEquals(Long.valueOf(1), list.getLong(0));
assertEquals(Long.valueOf(2), list.getLong(1));
assertEquals(Long.valueOf(3), list.getLong(2));
assertEquals(Long.valueOf(4), list.getLong(3));
}
@Test
public void testFloatPromotion() throws Exception {
BdfList list = new BdfList();
list.add(1F);
list.add(2D);
assertEquals(Double.valueOf(1), list.getDouble(0));
assertEquals(Double.valueOf(2), list.getDouble(1));
}
@Test
public void testByteArrayUnwrapping() throws Exception {
BdfList list = new BdfList();
list.add(new byte[123]);
list.add(new Bytes(new byte[123]));
byte[] first = list.getRaw(0);
assertEquals(123, first.length);
assertArrayEquals(new byte[123], first);
byte[] second = list.getRaw(1);
assertEquals(123, second.length);
assertArrayEquals(new byte[123], second);
}
@Test
@SuppressWarnings("ConstantConditions")
public void testIndexOutOfBoundsReturnsDefaultValue() throws Exception {
BdfList list = BdfList.of(1, 2, 3);
boolean defaultBoolean = true;
assertEquals(defaultBoolean, list.getBoolean(-1, defaultBoolean));
assertEquals(defaultBoolean, list.getBoolean(3, defaultBoolean));
Long defaultLong = 123L;
assertEquals(defaultLong, list.getLong(-1, defaultLong));
assertEquals(defaultLong, list.getLong(3, defaultLong));
Double defaultDouble = 1.23;
assertEquals(defaultDouble, list.getDouble(-1, defaultDouble));
assertEquals(defaultDouble, list.getDouble(3, defaultDouble));
String defaultString = "123";
assertEquals(defaultString, list.getString(-1, defaultString));
assertEquals(defaultString, list.getString(3, defaultString));
byte[] defaultBytes = new byte[] {1, 2, 3};
assertArrayEquals(defaultBytes, list.getRaw(-1, defaultBytes));
assertArrayEquals(defaultBytes, list.getRaw(3, defaultBytes));
BdfList defaultList = BdfList.of(1, 2, 3);
assertEquals(defaultList, list.getList(-1, defaultList));
assertEquals(defaultList, list.getList(3, defaultList));
BdfDictionary defaultDict = BdfDictionary.of(
new BdfEntry("1", 1),
new BdfEntry("2", 2),
new BdfEntry("3", 3)
);
assertEquals(defaultDict, list.getDictionary(-1, defaultDict));
assertEquals(defaultDict, list.getDictionary(3, defaultDict));
}
@Test
@SuppressWarnings("ConstantConditions")
public void testWrongTypeReturnsDefaultValue() throws Exception {
BdfList list = BdfList.of(1, 2, 3, true);
boolean defaultBoolean = true;
assertEquals(defaultBoolean, list.getBoolean(0, defaultBoolean));
Long defaultLong = 123L;
assertEquals(defaultLong, list.getLong(3, defaultLong));
Double defaultDouble = 1.23;
assertEquals(defaultDouble, list.getDouble(0, defaultDouble));
String defaultString = "123";
assertEquals(defaultString, list.getString(0, defaultString));
byte[] defaultBytes = new byte[] {1, 2, 3};
assertArrayEquals(defaultBytes, list.getRaw(0, defaultBytes));
BdfList defaultList = BdfList.of(1, 2, 3);
assertEquals(defaultList, list.getList(0, defaultList));
BdfDictionary defaultDict = BdfDictionary.of(
new BdfEntry("1", 1),
new BdfEntry("2", 2),
new BdfEntry("3", 3)
);
assertEquals(defaultDict, list.getDictionary(0, defaultDict));
}
@Test(expected = FormatException.class)
public void testNegativeIndexForBooleanThrowsFormatException()
throws Exception {
new BdfList().getBoolean(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForOptionalBooleanThrowsFormatException()
throws Exception {
new BdfList().getOptionalBoolean(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForLongThrowsFormatException()
throws Exception {
new BdfList().getLong(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForOptionalLongThrowsFormatException()
throws Exception {
new BdfList().getOptionalLong(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForDoubleThrowsFormatException()
throws Exception {
new BdfList().getDouble(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForOptionalDoubleThrowsFormatException()
throws Exception {
new BdfList().getOptionalDouble(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForStringThrowsFormatException()
throws Exception {
new BdfList().getString(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForOptionalStringThrowsFormatException()
throws Exception {
new BdfList().getOptionalString(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForRawThrowsFormatException()
throws Exception {
new BdfList().getRaw(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForOptionalRawThrowsFormatException()
throws Exception {
new BdfList().getOptionalRaw(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForListThrowsFormatException()
throws Exception {
new BdfList().getList(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForOptionalListThrowsFormatException()
throws Exception {
new BdfList().getOptionalList(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForDictionaryThrowsFormatException()
throws Exception {
new BdfList().getDictionary(-1);
}
@Test(expected = FormatException.class)
public void testNegativeIndexForOptionalDictionaryThrowsFormatException()
throws Exception {
new BdfList().getOptionalDictionary(-1);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForBooleanThrowsFormatException()
throws Exception {
new BdfList().getBoolean(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForOptionalBooleanThrowsFormatException()
throws Exception {
new BdfList().getOptionalBoolean(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForLongThrowsFormatException()
throws Exception {
new BdfList().getLong(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForOptionalLongThrowsFormatException()
throws Exception {
new BdfList().getOptionalLong(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForDoubleThrowsFormatException()
throws Exception {
new BdfList().getDouble(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForOptionalDoubleThrowsFormatException()
throws Exception {
new BdfList().getOptionalDouble(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForStringThrowsFormatException()
throws Exception {
new BdfList().getString(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForOptionalStringThrowsFormatException()
throws Exception {
new BdfList().getOptionalString(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForRawThrowsFormatException()
throws Exception {
new BdfList().getRaw(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForOptionalRawThrowsFormatException()
throws Exception {
new BdfList().getOptionalRaw(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForListThrowsFormatException()
throws Exception {
new BdfList().getList(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForOptionalListThrowsFormatException()
throws Exception {
new BdfList().getOptionalList(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForDictionaryThrowsFormatException()
throws Exception {
new BdfList().getDictionary(0);
}
@Test(expected = FormatException.class)
public void testTooLargeIndexForOptionalDictionaryThrowsFormatException()
throws Exception {
new BdfList().getOptionalDictionary(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForBooleanThrowsFormatException()
throws Exception {
BdfList.of(123).getBoolean(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForOptionalBooleanThrowsFormatException()
throws Exception {
BdfList.of(123).getOptionalBoolean(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForLongThrowsFormatException() throws Exception {
BdfList.of(1.23).getLong(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForOptionalLongThrowsFormatException()
throws Exception {
BdfList.of(1.23).getOptionalLong(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForDoubleThrowsFormatException() throws Exception {
BdfList.of(123).getDouble(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForOptionalDoubleThrowsFormatException()
throws Exception {
BdfList.of(123).getOptionalDouble(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForStringThrowsFormatException() throws Exception {
BdfList.of(123).getString(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForOptionalStringThrowsFormatException()
throws Exception {
BdfList.of(123).getOptionalString(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForRawThrowsFormatException() throws Exception {
BdfList.of(123).getRaw(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForOptionalRawThrowsFormatException()
throws Exception {
BdfList.of(123).getOptionalRaw(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForListThrowsFormatException() throws Exception {
BdfList.of(123).getList(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForOptionalListThrowsFormatException()
throws Exception {
BdfList.of(123).getOptionalList(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForDictionaryThrowsFormatException()
throws Exception {
BdfList.of(123).getDictionary(0);
}
@Test(expected = FormatException.class)
public void testWrongTypeForOptionalDictionaryThrowsFormatException()
throws Exception {
BdfList.of(123).getOptionalDictionary(0);
}
}