Removed restricted groups (may be restored after beta testing).

This commit is contained in:
akwizgran
2013-09-27 15:11:04 +01:00
parent 1e5e067df7
commit b94954544d
31 changed files with 219 additions and 630 deletions

View File

@@ -68,8 +68,8 @@ public class ProtocolIntegrationTest extends BriarTestCase {
private final ContactId contactId;
private final byte[] secret;
private final Author author;
private final Group group, group1;
private final Message message, message1, message2, message3;
private final Group group;
private final Message message, message1;
private final String authorName = "Alice";
private final String contentType = "text/plain";
private final String messageBody = "Hello world";
@@ -93,33 +93,23 @@ public class ProtocolIntegrationTest extends BriarTestCase {
// Create a shared secret
secret = new byte[32];
new Random().nextBytes(secret);
// Create two groups: one restricted, one unrestricted
// Create a group
GroupFactory groupFactory = i.getInstance(GroupFactory.class);
group = groupFactory.createGroup("Unrestricted group");
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
KeyPair groupKeyPair = crypto.generateSignatureKeyPair();
group1 = groupFactory.createGroup("Restricted group",
groupKeyPair.getPublic().getEncoded());
group = groupFactory.createGroup("Group");
// Create an author
AuthorFactory authorFactory = i.getInstance(AuthorFactory.class);
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
KeyPair authorKeyPair = crypto.generateSignatureKeyPair();
author = authorFactory.createAuthor(authorName,
authorKeyPair.getPublic().getEncoded());
// Create two messages to each group: one anonymous, one pseudonymous
// Create two messages to the group: one anonymous, one pseudonymous
MessageFactory messageFactory = i.getInstance(MessageFactory.class);
message = messageFactory.createAnonymousMessage(null, group,
contentType, messageBody.getBytes("UTF-8"));
message1 = messageFactory.createAnonymousMessage(null, group1,
groupKeyPair.getPrivate(), contentType,
messageBody.getBytes("UTF-8"));
message2 = messageFactory.createPseudonymousMessage(null, group,
message1 = messageFactory.createPseudonymousMessage(null, group,
author, authorKeyPair.getPrivate(), contentType,
messageBody.getBytes("UTF-8"));
message3 = messageFactory.createPseudonymousMessage(null, group1,
groupKeyPair.getPrivate(), author, authorKeyPair.getPrivate(),
contentType, messageBody.getBytes("UTF-8"));
messageIds = Arrays.asList(message.getId(), message1.getId(),
message2.getId(), message3.getId());
messageIds = Arrays.asList(message.getId(), message1.getId());
// Create some transport properties
transportId = new TransportId(TestUtils.getRandomId());
transportProperties = new TransportProperties(Collections.singletonMap(
@@ -145,18 +135,14 @@ public class ProtocolIntegrationTest extends BriarTestCase {
writer.writeMessage(message.getSerialised());
writer.writeMessage(message1.getSerialised());
writer.writeMessage(message2.getSerialised());
writer.writeMessage(message3.getSerialised());
writer.writeOffer(new Offer(messageIds));
BitSet requested = new BitSet(4);
BitSet requested = new BitSet(2);
requested.set(1);
requested.set(3);
writer.writeRequest(new Request(requested, 4));
writer.writeRequest(new Request(requested, 2));
SubscriptionUpdate su = new SubscriptionUpdate(
Arrays.asList(group, group1), 1);
SubscriptionUpdate su = new SubscriptionUpdate(Arrays.asList(group), 1);
writer.writeSubscriptionUpdate(su);
TransportUpdate tu = new TransportUpdate(transportId,
@@ -191,12 +177,7 @@ public class ProtocolIntegrationTest extends BriarTestCase {
assertTrue(reader.hasMessage());
m = reader.readMessage();
checkMessageEquality(message1, messageVerifier.verifyMessage(m));
assertTrue(reader.hasMessage());
m = reader.readMessage();
checkMessageEquality(message2, messageVerifier.verifyMessage(m));
assertTrue(reader.hasMessage());
m = reader.readMessage();
checkMessageEquality(message3, messageVerifier.verifyMessage(m));
assertFalse(reader.hasMessage());
// Read the offer
assertTrue(reader.hasOffer());
@@ -209,15 +190,13 @@ public class ProtocolIntegrationTest extends BriarTestCase {
BitSet requested = req.getBitmap();
assertFalse(requested.get(0));
assertTrue(requested.get(1));
assertFalse(requested.get(2));
assertTrue(requested.get(3));
// If there are any padding bits, they should all be zero
assertEquals(2, requested.cardinality());
assertEquals(1, requested.cardinality());
// Read the subscription update
assertTrue(reader.hasSubscriptionUpdate());
SubscriptionUpdate su = reader.readSubscriptionUpdate();
assertEquals(Arrays.asList(group, group1), su.getGroups());
assertEquals(Arrays.asList(group), su.getGroups());
assertEquals(1, su.getVersion());
// Read the transport update

View File

@@ -15,7 +15,7 @@ public class PasswordBasedKdfTest extends BriarTestCase {
public void testEncryptionAndDecryption() {
CryptoComponent crypto = new CryptoComponentImpl();
Random random = new Random();
byte[] input = new byte[123];
byte[] input = new byte[1234];
random.nextBytes(input);
char[] password = "password".toCharArray();
byte[] ciphertext = crypto.encryptWithPassword(input, password);
@@ -27,7 +27,7 @@ public class PasswordBasedKdfTest extends BriarTestCase {
public void testInvalidCiphertextReturnsNull() {
CryptoComponent crypto = new CryptoComponentImpl();
Random random = new Random();
byte[] input = new byte[123];
byte[] input = new byte[1234];
random.nextBytes(input);
char[] password = "password".toCharArray();
byte[] ciphertext = crypto.encryptWithPassword(input, password);

View File

@@ -1,5 +1,7 @@
package net.sf.briar.db;
import static net.sf.briar.api.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static net.sf.briar.api.messaging.MessagingConstants.GROUP_SALT_LENGTH;
import static net.sf.briar.api.messaging.Rating.GOOD;
import static net.sf.briar.api.messaging.Rating.UNRATED;
@@ -59,8 +61,8 @@ import org.junit.Test;
public abstract class DatabaseComponentTest extends BriarTestCase {
protected final Object txn = new Object();
protected final GroupId groupId, restrictedGroupId;
protected final Group group, restrictedGroup;
protected final GroupId groupId;
protected final Group group;
protected final AuthorId authorId;
protected final Author author;
protected final AuthorId localAuthorId;
@@ -80,15 +82,12 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
public DatabaseComponentTest() {
groupId = new GroupId(TestUtils.getRandomId());
restrictedGroupId = new GroupId(TestUtils.getRandomId());
group = new Group(groupId, "Group name", null);
restrictedGroup = new Group(restrictedGroupId, "Restricted group name",
new byte[60]);
group = new Group(groupId, "Group", new byte[GROUP_SALT_LENGTH]);
authorId = new AuthorId(TestUtils.getRandomId());
author = new Author(authorId, "Alice", new byte[60]);
author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
localAuthorId = new AuthorId(TestUtils.getRandomId());
localAuthor = new LocalAuthor(localAuthorId, "Bob", new byte[60],
new byte[60]);
localAuthor = new LocalAuthor(localAuthorId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
messageId = new MessageId(TestUtils.getRandomId());
messageId1 = new MessageId(TestUtils.getRandomId());
privateMessageId = new MessageId(TestUtils.getRandomId());
@@ -142,7 +141,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
// setRating(authorId, GOOD)
oneOf(database).setRating(txn, authorId, GOOD);
will(returnValue(UNRATED));
oneOf(database).getUnrestrictedGroupMessages(txn, authorId);
oneOf(database).getGroupMessages(txn, authorId);
will(returnValue(Collections.emptyList()));
oneOf(listener).eventOccurred(with(any(RatingChangedEvent.class)));
// setRating(authorId, GOOD) again
@@ -186,8 +185,6 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
oneOf(database).getVisibility(txn, groupId);
will(returnValue(Collections.emptyList()));
oneOf(database).removeSubscription(txn, groupId);
oneOf(database).containsLocalGroup(txn, groupId);
will(returnValue(false));
oneOf(listener).eventOccurred(with(any(
SubscriptionRemovedEvent.class)));
oneOf(listener).eventOccurred(with(any(
@@ -228,59 +225,6 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
context.assertIsSatisfied();
}
@Test
public void testRestrictedGroupMessagesAreAlwaysSendable()
throws Exception {
final Message groupMessage = new TestMessage(messageId, null,
restrictedGroup, author, contentType, subject, timestamp, raw);
final Message groupMessage1 = new TestMessage(messageId1, null,
restrictedGroup, null, contentType, subject, timestamp, raw);
Mockery context = new Mockery();
@SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
context.checking(new Expectations() {{
// addLocalGroupMessage(groupMessage)
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).containsSubscription(txn, restrictedGroupId);
will(returnValue(true));
oneOf(database).addGroupMessage(txn, groupMessage, false);
will(returnValue(true));
oneOf(database).setReadFlag(txn, messageId, true);
oneOf(database).getContactIds(txn);
will(returnValue(Arrays.asList(contactId)));
oneOf(database).addStatus(txn, contactId, messageId, false);
oneOf(database).setSendability(txn, messageId, 1);
oneOf(database).commitTransaction(txn);
// receiveMessage(groupMessage1)
oneOf(database).startTransaction();
will(returnValue(txn));
oneOf(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).containsVisibleSubscription(txn, contactId,
restrictedGroupId);
will(returnValue(true));
oneOf(database).addGroupMessage(txn, groupMessage1, true);
will(returnValue(true));
oneOf(database).addStatus(txn, contactId, messageId1, true);
oneOf(database).getContactIds(txn);
will(returnValue(Arrays.asList(contactId)));
oneOf(database).setSendability(txn, messageId1, 1);
oneOf(database).addMessageToAck(txn, contactId, messageId1);
oneOf(database).commitTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown);
db.addLocalGroupMessage(groupMessage);
db.receiveMessage(contactId, groupMessage1);
context.assertIsSatisfied();
}
@Test
public void testNullParentStopsBackwardInclusion() throws Exception {
Mockery context = new Mockery();
@@ -295,7 +239,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
oneOf(database).setRating(txn, authorId, GOOD);
will(returnValue(UNRATED));
// The sendability of the author's messages should be incremented
oneOf(database).getUnrestrictedGroupMessages(txn, authorId);
oneOf(database).getGroupMessages(txn, authorId);
will(returnValue(Arrays.asList(messageId)));
oneOf(database).getSendability(txn, messageId);
will(returnValue(0));
@@ -327,7 +271,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
oneOf(database).setRating(txn, authorId, GOOD);
will(returnValue(UNRATED));
// The sendability of the author's messages should be incremented
oneOf(database).getUnrestrictedGroupMessages(txn, authorId);
oneOf(database).getGroupMessages(txn, authorId);
will(returnValue(Arrays.asList(messageId)));
oneOf(database).getSendability(txn, messageId);
will(returnValue(0));
@@ -364,7 +308,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
oneOf(database).setRating(txn, authorId, GOOD);
will(returnValue(UNRATED));
// The sendability of the author's messages should be incremented
oneOf(database).getUnrestrictedGroupMessages(txn, authorId);
oneOf(database).getGroupMessages(txn, authorId);
will(returnValue(Arrays.asList(messageId)));
oneOf(database).getSendability(txn, messageId);
will(returnValue(0));

View File

@@ -1,6 +1,8 @@
package net.sf.briar.db;
import static java.util.concurrent.TimeUnit.SECONDS;
import static net.sf.briar.api.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static net.sf.briar.api.messaging.MessagingConstants.GROUP_SALT_LENGTH;
import static net.sf.briar.api.messaging.Rating.GOOD;
import static net.sf.briar.api.messaging.Rating.UNRATED;
import static org.junit.Assert.assertArrayEquals;
@@ -71,12 +73,12 @@ public class H2DatabaseTest extends BriarTestCase {
public H2DatabaseTest() throws Exception {
groupId = new GroupId(TestUtils.getRandomId());
group = new Group(groupId, "Group name", null);
group = new Group(groupId, "Group", new byte[GROUP_SALT_LENGTH]);
authorId = new AuthorId(TestUtils.getRandomId());
author = new Author(authorId, "Alice", new byte[60]);
author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]);
localAuthorId = new AuthorId(TestUtils.getRandomId());
localAuthor = new LocalAuthor(localAuthorId, "Bob", new byte[60],
new byte[60]);
localAuthor = new LocalAuthor(localAuthorId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
messageId = new MessageId(TestUtils.getRandomId());
messageId1 = new MessageId(TestUtils.getRandomId());
contentType = "text/plain";
@@ -535,38 +537,28 @@ public class H2DatabaseTest extends BriarTestCase {
}
@Test
public void testGetUnrestrictedGroupMessages() throws Exception {
public void testGetGroupMessages() throws Exception {
AuthorId authorId1 = new AuthorId(TestUtils.getRandomId());
Author author1 = new Author(authorId1, "Bob", new byte[60]);
Author author1 = new Author(authorId1, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH]);
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
Message message1 = new TestMessage(messageId1, null, group, author1,
contentType, subject, timestamp, raw);
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
Group group1 = new Group(groupId1, "Restricted group name",
new byte[60]);
MessageId messageId2 = new MessageId(TestUtils.getRandomId());
Message message2 = new TestMessage(messageId2, null, group1, author,
contentType, subject, timestamp, raw);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Subscribe to an unrestricted group and store two messages
// Subscribe to a group and store two messages
db.addSubscription(txn, group);
db.addGroupMessage(txn, message, false);
db.addGroupMessage(txn, message1, false);
// Subscribe to a restricted group and store a message
db.addSubscription(txn, group1);
db.addGroupMessage(txn, message2, false);
// Check that only the messages in the unrestricted group are retrieved
Collection<MessageId> ids = db.getUnrestrictedGroupMessages(txn,
authorId);
// Check that both messages are retrievable by their authors
Collection<MessageId> ids = db.getGroupMessages(txn, authorId);
Iterator<MessageId> it = ids.iterator();
assertTrue(it.hasNext());
assertEquals(messageId, it.next());
assertFalse(it.hasNext());
ids = db.getUnrestrictedGroupMessages(txn, authorId1);
ids = db.getGroupMessages(txn, authorId1);
it = ids.iterator();
assertTrue(it.hasNext());
assertEquals(messageId1, it.next());
@@ -582,7 +574,8 @@ public class H2DatabaseTest extends BriarTestCase {
MessageId childId2 = new MessageId(TestUtils.getRandomId());
MessageId childId3 = new MessageId(TestUtils.getRandomId());
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
Group group1 = new Group(groupId1, "Group name", null);
Group group1 = new Group(groupId1, "Another group",
new byte[GROUP_SALT_LENGTH]);
Message child1 = new TestMessage(childId1, messageId, group, author,
contentType, subject, timestamp, raw);
Message child2 = new TestMessage(childId2, messageId, group, author,
@@ -1193,7 +1186,8 @@ public class H2DatabaseTest extends BriarTestCase {
public void testGetGroupMessageParentWithParentInAnotherGroup()
throws Exception {
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
Group group1 = new Group(groupId1, "Group name", null);
Group group1 = new Group(groupId1, "Another group",
new byte[GROUP_SALT_LENGTH]);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
@@ -1446,7 +1440,8 @@ public class H2DatabaseTest extends BriarTestCase {
// Subscribe to a couple of groups
db.addSubscription(txn, group);
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
Group group1 = new Group(groupId1, "Group name", null);
Group group1 = new Group(groupId1, "Another group",
new byte[GROUP_SALT_LENGTH]);
db.addSubscription(txn, group1);
// Store two messages in the first group
@@ -1499,7 +1494,8 @@ public class H2DatabaseTest extends BriarTestCase {
List<Group> groups = new ArrayList<Group>();
for(int i = 0; i < 100; i++) {
GroupId id = new GroupId(TestUtils.getRandomId());
groups.add(new Group(id, "Group name", null));
String name = "Group " + i;
groups.add(new Group(id, name, new byte[GROUP_SALT_LENGTH]));
}
Database<Connection> db = open(false);
@@ -1834,7 +1830,8 @@ public class H2DatabaseTest extends BriarTestCase {
public void testGetAvailableGroups() throws Exception {
ContactId contactId1 = new ContactId(2);
AuthorId authorId1 = new AuthorId(TestUtils.getRandomId());
Author author1 = new Author(authorId1, "Carol", new byte[60]);
Author author1 = new Author(authorId1, "Carol",
new byte[MAX_PUBLIC_KEY_LENGTH]);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();

View File

@@ -96,7 +96,7 @@ public class ConstantsTest extends BriarTestCase {
byte[] publicKey = keyPair.getPublic().getEncoded();
assertTrue(publicKey.length <= MAX_PUBLIC_KEY_LENGTH);
// Sign some random data and check the length of the signature
byte[] toBeSigned = new byte[1000];
byte[] toBeSigned = new byte[1234];
random.nextBytes(toBeSigned);
sig.initSign(keyPair.getPrivate());
sig.update(toBeSigned);
@@ -120,23 +120,19 @@ public class ConstantsTest extends BriarTestCase {
MessageId parent = new MessageId(TestUtils.getRandomId());
// Create a maximum-length group
String groupName = TestUtils.createRandomString(MAX_GROUP_NAME_LENGTH);
byte[] groupPublic = new byte[MAX_PUBLIC_KEY_LENGTH];
Group group = groupFactory.createGroup(groupName, groupPublic);
Group group = groupFactory.createGroup(groupName);
// Create a maximum-length author
String authorName =
TestUtils.createRandomString(MAX_AUTHOR_NAME_LENGTH);
byte[] authorPublic = new byte[MAX_PUBLIC_KEY_LENGTH];
Author author = authorFactory.createAuthor(authorName, authorPublic);
// Create a maximum-length message
PrivateKey groupPrivate =
crypto.generateSignatureKeyPair().getPrivate();
PrivateKey authorPrivate =
crypto.generateSignatureKeyPair().getPrivate();
PrivateKey privateKey = crypto.generateSignatureKeyPair().getPrivate();
String contentType =
TestUtils.createRandomString(MAX_CONTENT_TYPE_LENGTH);
byte[] body = new byte[MAX_BODY_LENGTH];
Message message = messageFactory.createPseudonymousMessage(parent,
group, groupPrivate, author, authorPrivate, contentType, body);
group, author, privateKey, contentType, body);
// Check the size of the serialised message
int length = message.getSerialised().length;
assertTrue(length > UniqueId.LENGTH + MAX_GROUP_NAME_LENGTH
@@ -181,10 +177,8 @@ public class ConstantsTest extends BriarTestCase {
// Create the maximum number of maximum-length groups
Collection<Group> subs = new ArrayList<Group>();
for(int i = 0; i < MAX_SUBSCRIPTIONS; i++) {
String groupName =
TestUtils.createRandomString(MAX_GROUP_NAME_LENGTH);
byte[] groupPublic = new byte[MAX_PUBLIC_KEY_LENGTH];
subs.add(groupFactory.createGroup(groupName, groupPublic));
String name = TestUtils.createRandomString(MAX_GROUP_NAME_LENGTH);
subs.add(groupFactory.createGroup(name));
}
// Create a maximum-length subscription update
SubscriptionUpdate u = new SubscriptionUpdate(subs, Long.MAX_VALUE);

View File

@@ -1,5 +1,6 @@
package net.sf.briar.messaging.simplex;
import static net.sf.briar.api.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayInputStream;
@@ -108,11 +109,12 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
// Add a local pseudonym for Alice
AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice",
new byte[60], new byte[60]);
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
db.addLocalAuthor(aliceAuthor);
// Add Bob as a contact
AuthorId bobId = new AuthorId(TestUtils.getRandomId());
Author bobAuthor = new Author(bobId, "Bob", new byte[60]);
Author bobAuthor = new Author(bobId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH]);
ContactId contactId = db.addContact(bobAuthor, aliceId);
// Add the transport and the endpoint
db.addTransport(transportId, LATENCY);
@@ -161,12 +163,13 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
km.start();
// Add a local pseudonym for Bob
AuthorId bobId = new AuthorId(TestUtils.getRandomId());
LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob", new byte[60],
new byte[60]);
LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100]);
db.addLocalAuthor(bobAuthor);
// Add Alice as a contact
AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
Author aliceAuthor = new Author(aliceId, "Alice", new byte[60]);
Author aliceAuthor = new Author(aliceId, "Alice",
new byte[MAX_PUBLIC_KEY_LENGTH]);
ContactId contactId = db.addContact(aliceAuthor, bobId);
// Add the transport and the endpoint
db.addTransport(transportId, LATENCY);

View File

@@ -263,13 +263,13 @@ public class RemovableDrivePluginTest extends BriarTestCase {
assertEquals(0, files[0].length());
// Writing to the output stream should increase the size of the file
OutputStream out = writer.getOutputStream();
out.write(new byte[123]);
out.write(new byte[1234]);
out.flush();
out.close();
// Disposing of the writer should not delete the file
writer.dispose(false);
assertTrue(files[0].exists());
assertEquals(123, files[0].length());
assertEquals(1234, files[0].length());
context.assertIsSatisfied();
}