mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Use a versioned format for encoding authors.
This commit is contained in:
@@ -15,6 +15,8 @@ import org.briarproject.bramble.api.data.MetadataParser;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.bramble.api.sync.MessageFactory;
|
||||
@@ -31,9 +33,14 @@ import java.security.GeneralSecurityException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
@@ -54,7 +61,8 @@ public class ClientHelperImplTest extends BrambleTestCase {
|
||||
context.mock(MetadataEncoder.class);
|
||||
private final CryptoComponent cryptoComponent =
|
||||
context.mock(CryptoComponent.class);
|
||||
private final ClientHelper clientHelper;
|
||||
private final AuthorFactory authorFactory =
|
||||
context.mock(AuthorFactory.class);
|
||||
|
||||
private final GroupId groupId = new GroupId(getRandomId());
|
||||
private final BdfDictionary dictionary = new BdfDictionary();
|
||||
@@ -66,17 +74,15 @@ public class ClientHelperImplTest extends BrambleTestCase {
|
||||
private final Metadata metadata = new Metadata();
|
||||
private final BdfList list = BdfList.of("Sign this!", getRandomBytes(42));
|
||||
private final String label = StringUtils.getRandomString(5);
|
||||
private final Author author = getAuthor();
|
||||
|
||||
public ClientHelperImplTest() {
|
||||
clientHelper =
|
||||
new ClientHelperImpl(db, messageFactory, bdfReaderFactory,
|
||||
bdfWriterFactory, metadataParser, metadataEncoder,
|
||||
cryptoComponent);
|
||||
}
|
||||
private final ClientHelper clientHelper = new ClientHelperImpl(db,
|
||||
messageFactory, bdfReaderFactory, bdfWriterFactory, metadataParser,
|
||||
metadataEncoder, cryptoComponent, authorFactory);
|
||||
|
||||
@Test
|
||||
public void testAddLocalMessage() throws Exception {
|
||||
boolean shared = true;
|
||||
boolean shared = new Random().nextBoolean();
|
||||
Transaction txn = new Transaction(null, false);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
@@ -180,8 +186,7 @@ public class ClientHelperImplTest extends BrambleTestCase {
|
||||
oneOf(db).endTransaction(txn);
|
||||
}});
|
||||
|
||||
assertEquals(map,
|
||||
clientHelper.getMessageMetadataAsDictionary(groupId));
|
||||
assertEquals(map, clientHelper.getMessageMetadataAsDictionary(groupId));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -318,8 +323,7 @@ public class ClientHelperImplTest extends BrambleTestCase {
|
||||
}});
|
||||
|
||||
try {
|
||||
clientHelper
|
||||
.verifySignature(label, rawMessage, publicKey, list);
|
||||
clientHelper.verifySignature(label, rawMessage, publicKey, list);
|
||||
fail();
|
||||
} catch (GeneralSecurityException e) {
|
||||
// expected
|
||||
@@ -327,6 +331,154 @@ public class ClientHelperImplTest extends BrambleTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptsValidAuthor() throws Exception {
|
||||
BdfList authorList = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
author.getName(),
|
||||
author.getPublicKey()
|
||||
);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(authorFactory).createAuthor(author.getFormatVersion(),
|
||||
author.getName(), author.getPublicKey());
|
||||
will(returnValue(author));
|
||||
}});
|
||||
|
||||
assertEquals(author, clientHelper.parseAndValidateAuthor(authorList));
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooShortAuthor() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
author.getName()
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsTooLongAuthor() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
author.getName(),
|
||||
author.getPublicKey(),
|
||||
"foo"
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithNullFormatVersion() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
null,
|
||||
author.getName(),
|
||||
author.getPublicKey()
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithNonIntegerFormatVersion()
|
||||
throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
"foo",
|
||||
author.getName(),
|
||||
author.getPublicKey()
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithUnknownFormatVersion() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion() + 1,
|
||||
author.getName(),
|
||||
author.getPublicKey()
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithTooShortName() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
"",
|
||||
author.getPublicKey()
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithTooLongName() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
getRandomString(MAX_AUTHOR_NAME_LENGTH + 1),
|
||||
author.getPublicKey()
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithNullName() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
null,
|
||||
author.getPublicKey()
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithNonStringName() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
getRandomBytes(5),
|
||||
author.getPublicKey()
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithTooShortPublicKey() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
author.getName(),
|
||||
new byte[0]
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithTooLongPublicKey() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
author.getName(),
|
||||
getRandomBytes(MAX_PUBLIC_KEY_LENGTH + 1)
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithNullPublicKey() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
author.getName(),
|
||||
null
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testRejectsAuthorWithNonRawPublicKey() throws Exception {
|
||||
BdfList invalidAuthor = BdfList.of(
|
||||
author.getFormatVersion(),
|
||||
author.getName(),
|
||||
"foo"
|
||||
);
|
||||
clientHelper.parseAndValidateAuthor(invalidAuthor);
|
||||
}
|
||||
|
||||
private byte[] expectToByteArray(BdfList list) throws Exception {
|
||||
BdfWriter bdfWriter = context.mock(BdfWriter.class);
|
||||
|
||||
@@ -352,5 +504,4 @@ public class ClientHelperImplTest extends BrambleTestCase {
|
||||
will(returnValue(eof));
|
||||
}});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,8 +18,9 @@ import org.junit.Test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -32,9 +33,7 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
|
||||
private final KeyManager keyManager = context.mock(KeyManager.class);
|
||||
private final ContactManager contactManager;
|
||||
private final ContactId contactId = new ContactId(42);
|
||||
private final Author remote =
|
||||
new Author(new AuthorId(getRandomId()), "remote",
|
||||
getRandomBytes(42));
|
||||
private final Author remote = getAuthor();
|
||||
private final AuthorId local = new AuthorId(getRandomId());
|
||||
private final boolean verified = false, active = true;
|
||||
private final Contact contact =
|
||||
@@ -47,8 +46,8 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
|
||||
@Test
|
||||
public void testAddContact() throws Exception {
|
||||
SecretKey master = getSecretKey();
|
||||
long timestamp = 42;
|
||||
boolean alice = true;
|
||||
long timestamp = System.currentTimeMillis();
|
||||
boolean alice = new Random().nextBoolean();
|
||||
Transaction txn = new Transaction(null, false);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
|
||||
@@ -17,7 +17,6 @@ import org.briarproject.bramble.api.db.NoSuchTransportException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.identity.event.LocalAuthorAddedEvent;
|
||||
import org.briarproject.bramble.api.identity.event.LocalAuthorRemovedEvent;
|
||||
@@ -49,7 +48,6 @@ import org.briarproject.bramble.api.transport.OutgoingKeys;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -59,7 +57,6 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE;
|
||||
@@ -68,6 +65,9 @@ import static org.briarproject.bramble.api.sync.ValidationManager.State.DELIVERE
|
||||
import static org.briarproject.bramble.api.sync.ValidationManager.State.UNKNOWN;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.REORDERING_WINDOW_SIZE;
|
||||
import static org.briarproject.bramble.db.DatabaseConstants.MAX_OFFERED_MESSAGES;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
@@ -85,9 +85,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
private final ClientId clientId;
|
||||
private final GroupId groupId;
|
||||
private final Group group;
|
||||
private final AuthorId authorId;
|
||||
private final Author author;
|
||||
private final AuthorId localAuthorId;
|
||||
private final LocalAuthor localAuthor;
|
||||
private final MessageId messageId, messageId1;
|
||||
private final int size;
|
||||
@@ -100,18 +98,15 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
private final Contact contact;
|
||||
|
||||
public DatabaseComponentImplTest() {
|
||||
clientId = new ClientId(StringUtils.getRandomString(5));
|
||||
clientId = new ClientId(getRandomString(123));
|
||||
groupId = new GroupId(TestUtils.getRandomId());
|
||||
byte[] descriptor = new byte[MAX_GROUP_DESCRIPTOR_LENGTH];
|
||||
group = new Group(groupId, clientId, descriptor);
|
||||
authorId = new AuthorId(TestUtils.getRandomId());
|
||||
author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
|
||||
localAuthorId = new AuthorId(TestUtils.getRandomId());
|
||||
long timestamp = System.currentTimeMillis();
|
||||
localAuthor = new LocalAuthor(localAuthorId, "Bob",
|
||||
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp);
|
||||
author = getAuthor();
|
||||
localAuthor = getLocalAuthor();
|
||||
messageId = new MessageId(TestUtils.getRandomId());
|
||||
messageId1 = new MessageId(TestUtils.getRandomId());
|
||||
long timestamp = System.currentTimeMillis();
|
||||
size = 1234;
|
||||
raw = new byte[size];
|
||||
message = new Message(messageId, groupId, timestamp, raw);
|
||||
@@ -120,7 +115,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
transportId = new TransportId("id");
|
||||
maxLatency = Integer.MAX_VALUE;
|
||||
contactId = new ContactId(234);
|
||||
contact = new Contact(contactId, author, localAuthorId, true, true);
|
||||
contact = new Contact(contactId, author, localAuthor.getId(),
|
||||
true, true);
|
||||
}
|
||||
|
||||
private DatabaseComponent createDatabaseComponent(Database<Object> database,
|
||||
@@ -142,18 +138,20 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
// registerLocalAuthor()
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
will(returnValue(false));
|
||||
oneOf(database).addLocalAuthor(txn, localAuthor);
|
||||
oneOf(eventBus).broadcast(with(any(LocalAuthorAddedEvent.class)));
|
||||
// addContact()
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsLocalAuthor(txn, authorId);
|
||||
oneOf(database).containsLocalAuthor(txn, author.getId());
|
||||
will(returnValue(false));
|
||||
oneOf(database).containsContact(txn, authorId, localAuthorId);
|
||||
oneOf(database).containsContact(txn, author.getId(),
|
||||
localAuthor.getId());
|
||||
will(returnValue(false));
|
||||
oneOf(database).addContact(txn, author, localAuthorId, true, true);
|
||||
oneOf(database).addContact(txn, author, localAuthor.getId(),
|
||||
true, true);
|
||||
will(returnValue(contactId));
|
||||
oneOf(eventBus).broadcast(with(any(ContactAddedEvent.class)));
|
||||
oneOf(eventBus).broadcast(with(any(
|
||||
@@ -187,9 +185,9 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
oneOf(database).removeContact(txn, contactId);
|
||||
oneOf(eventBus).broadcast(with(any(ContactRemovedEvent.class)));
|
||||
// removeLocalAuthor()
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).removeLocalAuthor(txn, localAuthorId);
|
||||
oneOf(database).removeLocalAuthor(txn, localAuthor.getId());
|
||||
oneOf(eventBus).broadcast(with(any(LocalAuthorRemovedEvent.class)));
|
||||
// endTransaction()
|
||||
oneOf(database).commitTransaction(txn);
|
||||
@@ -203,9 +201,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
Transaction transaction = db.startTransaction(false);
|
||||
try {
|
||||
db.addLocalAuthor(transaction, localAuthor);
|
||||
assertEquals(contactId,
|
||||
db.addContact(transaction, author, localAuthorId, true,
|
||||
true));
|
||||
assertEquals(contactId, db.addContact(transaction, author,
|
||||
localAuthor.getId(), true, true));
|
||||
assertEquals(Collections.singletonList(contact),
|
||||
db.getContacts(transaction));
|
||||
db.addGroup(transaction, group); // First time - listeners called
|
||||
@@ -214,7 +211,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
db.getGroups(transaction, clientId));
|
||||
db.removeGroup(transaction, group);
|
||||
db.removeContact(transaction, contactId);
|
||||
db.removeLocalAuthor(transaction, localAuthorId);
|
||||
db.removeLocalAuthor(transaction, localAuthor.getId());
|
||||
db.commitTransaction(transaction);
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
@@ -487,7 +484,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
// Check whether the pseudonym is in the DB (which it's not)
|
||||
exactly(3).of(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
exactly(3).of(database).containsLocalAuthor(txn, localAuthorId);
|
||||
exactly(3).of(database).containsLocalAuthor(txn,
|
||||
localAuthor.getId());
|
||||
will(returnValue(false));
|
||||
exactly(3).of(database).abortTransaction(txn);
|
||||
}});
|
||||
@@ -496,7 +494,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
Transaction transaction = db.startTransaction(false);
|
||||
try {
|
||||
db.addContact(transaction, author, localAuthorId, true, true);
|
||||
db.addContact(transaction, author, localAuthor.getId(), true, true);
|
||||
fail();
|
||||
} catch (NoSuchLocalAuthorException expected) {
|
||||
// Expected
|
||||
@@ -506,7 +504,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
transaction = db.startTransaction(false);
|
||||
try {
|
||||
db.getLocalAuthor(transaction, localAuthorId);
|
||||
db.getLocalAuthor(transaction, localAuthor.getId());
|
||||
fail();
|
||||
} catch (NoSuchLocalAuthorException expected) {
|
||||
// Expected
|
||||
@@ -516,7 +514,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
transaction = db.startTransaction(false);
|
||||
try {
|
||||
db.removeLocalAuthor(transaction, localAuthorId);
|
||||
db.removeLocalAuthor(transaction, localAuthor.getId());
|
||||
fail();
|
||||
} catch (NoSuchLocalAuthorException expected) {
|
||||
// Expected
|
||||
@@ -759,18 +757,20 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
// registerLocalAuthor()
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
will(returnValue(false));
|
||||
oneOf(database).addLocalAuthor(txn, localAuthor);
|
||||
oneOf(eventBus).broadcast(with(any(LocalAuthorAddedEvent.class)));
|
||||
// addContact()
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsLocalAuthor(txn, authorId);
|
||||
oneOf(database).containsLocalAuthor(txn, author.getId());
|
||||
will(returnValue(false));
|
||||
oneOf(database).containsContact(txn, authorId, localAuthorId);
|
||||
oneOf(database).containsContact(txn, author.getId(),
|
||||
localAuthor.getId());
|
||||
will(returnValue(false));
|
||||
oneOf(database).addContact(txn, author, localAuthorId, true, true);
|
||||
oneOf(database).addContact(txn, author, localAuthor.getId(),
|
||||
true, true);
|
||||
will(returnValue(contactId));
|
||||
oneOf(eventBus).broadcast(with(any(ContactAddedEvent.class)));
|
||||
oneOf(eventBus).broadcast(with(any(
|
||||
@@ -792,9 +792,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
Transaction transaction = db.startTransaction(false);
|
||||
try {
|
||||
db.addLocalAuthor(transaction, localAuthor);
|
||||
assertEquals(contactId,
|
||||
db.addContact(transaction, author, localAuthorId, true,
|
||||
true));
|
||||
assertEquals(contactId, db.addContact(transaction, author,
|
||||
localAuthor.getId(), true, true));
|
||||
db.commitTransaction(transaction);
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
@@ -1406,10 +1405,10 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
// Contact is a local identity
|
||||
oneOf(database).containsLocalAuthor(txn, authorId);
|
||||
oneOf(database).containsLocalAuthor(txn, author.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).abortTransaction(txn);
|
||||
}});
|
||||
@@ -1419,7 +1418,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
Transaction transaction = db.startTransaction(false);
|
||||
try {
|
||||
db.addContact(transaction, author, localAuthorId, true, true);
|
||||
db.addContact(transaction, author, localAuthor.getId(), true, true);
|
||||
fail();
|
||||
} catch (ContactExistsException expected) {
|
||||
// Expected
|
||||
@@ -1433,12 +1432,13 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsLocalAuthor(txn, authorId);
|
||||
oneOf(database).containsLocalAuthor(txn, author.getId());
|
||||
will(returnValue(false));
|
||||
// Contact already exists for this local identity
|
||||
oneOf(database).containsContact(txn, authorId, localAuthorId);
|
||||
oneOf(database).containsContact(txn, author.getId(),
|
||||
localAuthor.getId());
|
||||
will(returnValue(true));
|
||||
oneOf(database).abortTransaction(txn);
|
||||
}});
|
||||
@@ -1448,7 +1448,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
Transaction transaction = db.startTransaction(false);
|
||||
try {
|
||||
db.addContact(transaction, author, localAuthorId, true, true);
|
||||
db.addContact(transaction, author, localAuthor.getId(), true, true);
|
||||
fail();
|
||||
} catch (ContactExistsException expected) {
|
||||
// Expected
|
||||
|
||||
@@ -7,7 +7,6 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
@@ -26,7 +25,6 @@ import org.briarproject.bramble.system.SystemClock;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestDatabaseConfig;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -46,7 +44,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static org.briarproject.bramble.api.db.Metadata.REMOVE;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE;
|
||||
@@ -56,6 +53,11 @@ import static org.briarproject.bramble.api.sync.ValidationManager.State.DELIVERE
|
||||
import static org.briarproject.bramble.api.sync.ValidationManager.State.INVALID;
|
||||
import static org.briarproject.bramble.api.sync.ValidationManager.State.PENDING;
|
||||
import static org.briarproject.bramble.api.sync.ValidationManager.State.UNKNOWN;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -75,7 +77,6 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
private final ClientId clientId;
|
||||
private final Group group;
|
||||
private final Author author;
|
||||
private final AuthorId localAuthorId;
|
||||
private final LocalAuthor localAuthor;
|
||||
private final MessageId messageId;
|
||||
private final long timestamp;
|
||||
@@ -86,19 +87,16 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
private final ContactId contactId;
|
||||
|
||||
JdbcDatabaseTest() throws Exception {
|
||||
groupId = new GroupId(TestUtils.getRandomId());
|
||||
clientId = new ClientId(StringUtils.getRandomString(5));
|
||||
groupId = new GroupId(getRandomId());
|
||||
clientId = new ClientId(getRandomString(123));
|
||||
byte[] descriptor = new byte[MAX_GROUP_DESCRIPTOR_LENGTH];
|
||||
group = new Group(groupId, clientId, descriptor);
|
||||
AuthorId authorId = new AuthorId(TestUtils.getRandomId());
|
||||
author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
|
||||
localAuthorId = new AuthorId(TestUtils.getRandomId());
|
||||
author = getAuthor();
|
||||
localAuthor = getLocalAuthor();
|
||||
messageId = new MessageId(getRandomId());
|
||||
timestamp = System.currentTimeMillis();
|
||||
localAuthor = new LocalAuthor(localAuthorId, "Bob",
|
||||
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp);
|
||||
messageId = new MessageId(TestUtils.getRandomId());
|
||||
size = 1234;
|
||||
raw = TestUtils.getRandomBytes(size);
|
||||
raw = getRandomBytes(size);
|
||||
message = new Message(messageId, groupId, timestamp, raw);
|
||||
transportId = new TransportId("id");
|
||||
contactId = new ContactId(1);
|
||||
@@ -119,7 +117,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
Connection txn = db.startTransaction();
|
||||
assertFalse(db.containsContact(txn, contactId));
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
assertTrue(db.containsContact(txn, contactId));
|
||||
assertFalse(db.containsGroup(txn, groupId));
|
||||
@@ -182,7 +180,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
@@ -220,7 +218,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact, a shared group and a shared but unvalidated message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
@@ -266,7 +264,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact, an invisible group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, true);
|
||||
@@ -318,7 +316,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact, a shared group and an unshared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
@@ -350,7 +348,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
@@ -377,13 +375,13 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact and a visible group
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, false);
|
||||
|
||||
// Add some messages to ack
|
||||
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId messageId1 = new MessageId(getRandomId());
|
||||
Message message1 = new Message(messageId1, groupId, timestamp, raw);
|
||||
db.addMessage(txn, message, DELIVERED, true);
|
||||
db.addStatus(txn, contactId, messageId, false, true);
|
||||
@@ -414,7 +412,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
@@ -572,7 +570,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact and a shared group
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
@@ -592,7 +590,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
|
||||
// The group is not in the database
|
||||
@@ -610,7 +608,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact, a group and a message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, true);
|
||||
@@ -630,7 +628,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact and a group
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
|
||||
@@ -680,7 +678,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add the contact, the transport and the transport keys
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
db.addTransportKeys(txn, contactId, keys);
|
||||
@@ -742,7 +740,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add the contact, transport and transport keys
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
db.updateTransportKeys(txn, Collections.singletonMap(contactId, keys));
|
||||
@@ -778,7 +776,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add the contact, transport and transport keys
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addTransport(txn, transportId, 123);
|
||||
db.updateTransportKeys(txn, Collections.singletonMap(contactId, keys));
|
||||
@@ -813,7 +811,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
|
||||
// Add a contact associated with the local author
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
|
||||
// Ensure contact is returned from database by Author ID
|
||||
@@ -838,18 +836,19 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a local author - no contacts should be associated
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
Collection<ContactId> contacts = db.getContacts(txn, localAuthorId);
|
||||
Collection<ContactId> contacts =
|
||||
db.getContacts(txn, localAuthor.getId());
|
||||
assertEquals(Collections.emptyList(), contacts);
|
||||
|
||||
// Add a contact associated with the local author
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
contacts = db.getContacts(txn, localAuthorId);
|
||||
contacts = db.getContacts(txn, localAuthor.getId());
|
||||
assertEquals(Collections.singletonList(contactId), contacts);
|
||||
|
||||
// Remove the local author - the contact should be removed
|
||||
db.removeLocalAuthor(txn, localAuthorId);
|
||||
contacts = db.getContacts(txn, localAuthorId);
|
||||
db.removeLocalAuthor(txn, localAuthor.getId());
|
||||
contacts = db.getContacts(txn, localAuthor.getId());
|
||||
assertEquals(Collections.emptyList(), contacts);
|
||||
assertFalse(db.containsContact(txn, contactId));
|
||||
|
||||
@@ -864,14 +863,14 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact - initially there should be no offered messages
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
assertEquals(0, db.countOfferedMessages(txn, contactId));
|
||||
|
||||
// Add some offered messages and count them
|
||||
List<MessageId> ids = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
MessageId m = new MessageId(TestUtils.getRandomId());
|
||||
MessageId m = new MessageId(getRandomId());
|
||||
db.addOfferedMessage(txn, contactId, m);
|
||||
ids.add(m);
|
||||
}
|
||||
@@ -1056,7 +1055,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testMetadataQueries() throws Exception {
|
||||
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId messageId1 = new MessageId(getRandomId());
|
||||
Message message1 = new Message(messageId1, groupId, timestamp, raw);
|
||||
|
||||
Database<Connection> db = open(false);
|
||||
@@ -1160,7 +1159,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testMetadataQueriesOnlyForDeliveredMessages() throws Exception {
|
||||
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId messageId1 = new MessageId(getRandomId());
|
||||
Message message1 = new Message(messageId1, groupId, timestamp, raw);
|
||||
|
||||
Database<Connection> db = open(false);
|
||||
@@ -1231,10 +1230,10 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testMessageDependencies() throws Exception {
|
||||
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId messageId2 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId messageId3 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId messageId4 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId messageId1 = new MessageId(getRandomId());
|
||||
MessageId messageId2 = new MessageId(getRandomId());
|
||||
MessageId messageId3 = new MessageId(getRandomId());
|
||||
MessageId messageId4 = new MessageId(getRandomId());
|
||||
Message message1 = new Message(messageId1, groupId, timestamp, raw);
|
||||
Message message2 = new Message(messageId2, groupId, timestamp, raw);
|
||||
|
||||
@@ -1315,21 +1314,21 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addMessage(txn, message, PENDING, true);
|
||||
|
||||
// Add a second group
|
||||
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
|
||||
GroupId groupId1 = new GroupId(getRandomId());
|
||||
Group group1 = new Group(groupId1, clientId,
|
||||
TestUtils.getRandomBytes(MAX_GROUP_DESCRIPTOR_LENGTH));
|
||||
getRandomBytes(MAX_GROUP_DESCRIPTOR_LENGTH));
|
||||
db.addGroup(txn, group1);
|
||||
|
||||
// Add a message to the second group
|
||||
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId messageId1 = new MessageId(getRandomId());
|
||||
Message message1 = new Message(messageId1, groupId1, timestamp, raw);
|
||||
db.addMessage(txn, message1, DELIVERED, true);
|
||||
|
||||
// Create an ID for a missing message
|
||||
MessageId messageId2 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId messageId2 = new MessageId(getRandomId());
|
||||
|
||||
// Add another message to the first group
|
||||
MessageId messageId3 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId messageId3 = new MessageId(getRandomId());
|
||||
Message message3 = new Message(messageId3, groupId, timestamp, raw);
|
||||
db.addMessage(txn, message3, DELIVERED, true);
|
||||
|
||||
@@ -1364,10 +1363,10 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testGetPendingMessagesForDelivery() throws Exception {
|
||||
MessageId mId1 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId mId2 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId mId3 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId mId4 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId mId1 = new MessageId(getRandomId());
|
||||
MessageId mId2 = new MessageId(getRandomId());
|
||||
MessageId mId3 = new MessageId(getRandomId());
|
||||
MessageId mId4 = new MessageId(getRandomId());
|
||||
Message m1 = new Message(mId1, groupId, timestamp, raw);
|
||||
Message m2 = new Message(mId2, groupId, timestamp, raw);
|
||||
Message m3 = new Message(mId3, groupId, timestamp, raw);
|
||||
@@ -1401,10 +1400,10 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testGetMessagesToShare() throws Exception {
|
||||
MessageId mId1 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId mId2 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId mId3 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId mId4 = new MessageId(TestUtils.getRandomId());
|
||||
MessageId mId1 = new MessageId(getRandomId());
|
||||
MessageId mId2 = new MessageId(getRandomId());
|
||||
MessageId mId3 = new MessageId(getRandomId());
|
||||
MessageId mId4 = new MessageId(getRandomId());
|
||||
Message m1 = new Message(mId1, groupId, timestamp, raw);
|
||||
Message m2 = new Message(mId2, groupId, timestamp, raw);
|
||||
Message m3 = new Message(mId3, groupId, timestamp, raw);
|
||||
@@ -1443,7 +1442,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
@@ -1512,9 +1511,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testDifferentLocalAuthorsCanHaveTheSameContact()
|
||||
throws Exception {
|
||||
AuthorId localAuthorId1 = new AuthorId(TestUtils.getRandomId());
|
||||
LocalAuthor localAuthor1 = new LocalAuthor(localAuthorId1, "Carol",
|
||||
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp);
|
||||
LocalAuthor localAuthor1 = getLocalAuthor();
|
||||
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
@@ -1525,15 +1522,15 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add the same contact for each local author
|
||||
ContactId contactId =
|
||||
db.addContact(txn, author, localAuthorId, true, true);
|
||||
db.addContact(txn, author, localAuthor.getId(), true, true);
|
||||
ContactId contactId1 =
|
||||
db.addContact(txn, author, localAuthorId1, true, true);
|
||||
db.addContact(txn, author, localAuthor1.getId(), true, true);
|
||||
|
||||
// The contacts should be distinct
|
||||
assertNotEquals(contactId, contactId1);
|
||||
assertEquals(2, db.getContacts(txn).size());
|
||||
assertEquals(1, db.getContacts(txn, localAuthorId).size());
|
||||
assertEquals(1, db.getContacts(txn, localAuthorId1).size());
|
||||
assertEquals(1, db.getContacts(txn, localAuthor.getId()).size());
|
||||
assertEquals(1, db.getContacts(txn, localAuthor1.getId()).size());
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
@@ -1546,7 +1543,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact, a shared group and a shared message
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
@@ -1592,7 +1589,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a contact
|
||||
db.addLocalAuthor(txn, localAuthor);
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthorId,
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
true, true));
|
||||
|
||||
// The contact should be active
|
||||
|
||||
@@ -10,8 +10,6 @@ import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -23,6 +21,8 @@ import static org.briarproject.bramble.api.identity.Author.Status.OURSELVES;
|
||||
import static org.briarproject.bramble.api.identity.Author.Status.UNKNOWN;
|
||||
import static org.briarproject.bramble.api.identity.Author.Status.UNVERIFIED;
|
||||
import static org.briarproject.bramble.api.identity.Author.Status.VERIFIED;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
@@ -30,11 +30,7 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
private final IdentityManager identityManager;
|
||||
private final DatabaseComponent db = context.mock(DatabaseComponent.class);
|
||||
private final Transaction txn = new Transaction(null, false);
|
||||
private final LocalAuthor localAuthor =
|
||||
new LocalAuthor(new AuthorId(TestUtils.getRandomId()),
|
||||
StringUtils.getRandomString(8),
|
||||
TestUtils.getRandomBytes(42), TestUtils.getRandomBytes(42),
|
||||
0);
|
||||
private final LocalAuthor localAuthor = getLocalAuthor();
|
||||
private final Collection<LocalAuthor> localAuthors =
|
||||
Collections.singletonList(localAuthor);
|
||||
|
||||
@@ -80,7 +76,8 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
@Test
|
||||
public void testGetAuthorStatus() throws DbException {
|
||||
AuthorId authorId = new AuthorId(TestUtils.getRandomId());
|
||||
Author author = getAuthor();
|
||||
AuthorId authorId = author.getId();
|
||||
Collection<Contact> contacts = new ArrayList<>();
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
@@ -95,20 +92,16 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
|
||||
assertEquals(UNKNOWN, identityManager.getAuthorStatus(authorId));
|
||||
|
||||
// add one unverified contact
|
||||
Author author = new Author(authorId, StringUtils.getRandomString(8),
|
||||
TestUtils.getRandomBytes(42));
|
||||
Contact contact =
|
||||
new Contact(new ContactId(1), author, localAuthor.getId(),
|
||||
false, true);
|
||||
Contact contact = new Contact(new ContactId(1), author,
|
||||
localAuthor.getId(), false, true);
|
||||
contacts.add(contact);
|
||||
|
||||
checkAuthorStatusContext(authorId, contacts);
|
||||
assertEquals(UNVERIFIED, identityManager.getAuthorStatus(authorId));
|
||||
|
||||
// add one verified contact
|
||||
Contact contact2 =
|
||||
new Contact(new ContactId(1), author, localAuthor.getId(),
|
||||
true, true);
|
||||
Contact contact2 = new Contact(new ContactId(1), author,
|
||||
localAuthor.getId(), true, true);
|
||||
contacts.add(contact2);
|
||||
|
||||
checkAuthorStatusContext(authorId, contacts);
|
||||
|
||||
@@ -11,8 +11,6 @@ import org.briarproject.bramble.api.data.MetadataParser;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
@@ -31,16 +29,15 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_ID;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_VERSION;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
@@ -609,22 +606,10 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
return new Group(g, CLIENT_ID, descriptor);
|
||||
}
|
||||
|
||||
private LocalAuthor getLocalAuthor() {
|
||||
AuthorId id = new AuthorId(getRandomId());
|
||||
String name = getRandomString(MAX_AUTHOR_NAME_LENGTH);
|
||||
byte[] publicKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||
byte[] privateKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||
long created = System.currentTimeMillis();
|
||||
return new LocalAuthor(id, name, publicKey, privateKey, created);
|
||||
}
|
||||
|
||||
private Contact getContact(boolean active) {
|
||||
ContactId c = new ContactId(nextContactId++);
|
||||
AuthorId a = new AuthorId(getRandomId());
|
||||
String name = getRandomString(MAX_AUTHOR_NAME_LENGTH);
|
||||
byte[] publicKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||
return new Contact(c, new Author(a, name, publicKey),
|
||||
localAuthor.getId(), true, active);
|
||||
return new Contact(c, getAuthor(), localAuthor.getId(),
|
||||
true, active);
|
||||
}
|
||||
|
||||
private Message getMessage(GroupId g, long timestamp) {
|
||||
|
||||
@@ -9,7 +9,10 @@ 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;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
|
||||
public abstract class ValidatorTestCase extends BrambleMockTestCase {
|
||||
|
||||
@@ -21,16 +24,14 @@ public abstract class ValidatorTestCase extends BrambleMockTestCase {
|
||||
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 MessageId messageId = new MessageId(getRandomId());
|
||||
protected final GroupId groupId = new GroupId(getRandomId());
|
||||
protected final long timestamp = 1234567890 * 1000L;
|
||||
protected final byte[] raw = TestUtils.getRandomBytes(123);
|
||||
protected final byte[] raw = getRandomBytes(123);
|
||||
protected final Message message =
|
||||
new Message(messageId, groupId, timestamp, raw);
|
||||
protected final ClientId clientId =
|
||||
new ClientId(StringUtils.getRandomString(123));
|
||||
protected final byte[] descriptor = TestUtils.getRandomBytes(123);
|
||||
protected final ClientId clientId = new ClientId(getRandomString(123));
|
||||
protected final byte[] descriptor = getRandomBytes(123);
|
||||
protected final Group group = new Group(groupId, clientId, descriptor);
|
||||
|
||||
}
|
||||
|
||||
@@ -23,8 +23,10 @@ import org.junit.Test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
@@ -59,9 +61,7 @@ public class KeyManagerImplTest extends BrambleTestCase {
|
||||
@Before
|
||||
public void testStartService() throws Exception {
|
||||
Transaction txn = new Transaction(null, false);
|
||||
AuthorId remoteAuthorId = new AuthorId(getRandomId());
|
||||
Author remoteAuthor = new Author(remoteAuthorId, "author",
|
||||
getRandomBytes(42));
|
||||
Author remoteAuthor = getAuthor();
|
||||
AuthorId localAuthorId = new AuthorId(getRandomId());
|
||||
Collection<Contact> contacts = new ArrayList<>();
|
||||
contacts.add(new Contact(contactId, remoteAuthor, localAuthorId, true,
|
||||
@@ -101,12 +101,12 @@ public class KeyManagerImplTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testAddContact() throws Exception {
|
||||
SecretKey secretKey = getSecretKey();
|
||||
long timestamp = 42L;
|
||||
boolean alice = true;
|
||||
long timestamp = System.currentTimeMillis();
|
||||
boolean alice = new Random().nextBoolean();
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(transportKeyManager)
|
||||
.addContact(txn, contactId, secretKey, timestamp, alice);
|
||||
oneOf(transportKeyManager).addContact(txn, contactId, secretKey,
|
||||
timestamp, alice);
|
||||
}});
|
||||
|
||||
keyManager.addContact(txn, contactId, secretKey, timestamp, alice);
|
||||
@@ -121,8 +121,8 @@ public class KeyManagerImplTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testGetStreamContextForUnknownTransport() throws Exception {
|
||||
assertEquals(null, keyManager
|
||||
.getStreamContext(contactId, unknownTransportId));
|
||||
assertEquals(null,
|
||||
keyManager.getStreamContext(contactId, unknownTransportId));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user