From 458c0ca28548f89109d71e65dd515f1256b20d86 Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Fri, 4 Jul 2014 12:07:18 +0100 Subject: [PATCH] Don't broadcast MessageAddedEvent if message wasn't added. Fixed a bug in SimplexMessagingIntegrationTest that should've caught this. --- .../briarproject/db/DatabaseComponentImpl.java | 8 +++++--- .../src/org/briarproject/db/JdbcDatabase.java | 4 ++-- .../simplex/SimplexMessagingIntegrationTest.java | 15 +++++++++------ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java index ffc1061c56..79113fbd43 100644 --- a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java +++ b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java @@ -1133,9 +1133,11 @@ DatabaseCleaner.Callback { } finally { lock.writeLock().unlock(); } - // FIXME: MessageAddedEvent should only be broadcast if msg is visible - if(visible) callListeners(new MessageToAckEvent(c)); - if(!duplicate) callListeners(new MessageAddedEvent(m.getGroup(), c)); + if(visible) { + if(!duplicate) + callListeners(new MessageAddedEvent(m.getGroup(), c)); + callListeners(new MessageToAckEvent(c)); + } } public void receiveOffer(ContactId c, Offer o) throws DbException { diff --git a/briar-core/src/org/briarproject/db/JdbcDatabase.java b/briar-core/src/org/briarproject/db/JdbcDatabase.java index b398860563..5d51ad1049 100644 --- a/briar-core/src/org/briarproject/db/JdbcDatabase.java +++ b/briar-core/src/org/briarproject/db/JdbcDatabase.java @@ -1077,8 +1077,8 @@ abstract class JdbcDatabase implements Database<Connection> { } } - public boolean containsVisibleGroup(Connection txn, ContactId c, - GroupId g) throws DbException { + public boolean containsVisibleGroup(Connection txn, ContactId c, GroupId g) + throws DbException { PreparedStatement ps = null; ResultSet rs = null; try { diff --git a/briar-tests/src/org/briarproject/messaging/simplex/SimplexMessagingIntegrationTest.java b/briar-tests/src/org/briarproject/messaging/simplex/SimplexMessagingIntegrationTest.java index a32327b98d..0856bbb38b 100644 --- a/briar-tests/src/org/briarproject/messaging/simplex/SimplexMessagingIntegrationTest.java +++ b/briar-tests/src/org/briarproject/messaging/simplex/SimplexMessagingIntegrationTest.java @@ -25,7 +25,7 @@ import org.briarproject.api.event.Event; import org.briarproject.api.event.EventListener; import org.briarproject.api.event.MessageAddedEvent; import org.briarproject.api.messaging.Group; -import org.briarproject.api.messaging.GroupId; +import org.briarproject.api.messaging.GroupFactory; import org.briarproject.api.messaging.Message; import org.briarproject.api.messaging.MessageFactory; import org.briarproject.api.messaging.MessageVerifier; @@ -59,7 +59,6 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { private final File testDir = TestUtils.getTestDirectory(); private final File aliceDir = new File(testDir, "alice"); private final File bobDir = new File(testDir, "bob"); - private final Group group; private final TransportId transportId; private final byte[] initialSecret; private final long epoch; @@ -67,8 +66,6 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { private Injector alice, bob; public SimplexMessagingIntegrationTest() throws Exception { - GroupId groupId = new GroupId(TestUtils.getRandomId()); - group = new Group(groupId, "Group", new byte[GROUP_SALT_LENGTH]); transportId = new TransportId("id"); // Create matching secrets for Alice and Bob initialSecret = new byte[32]; @@ -77,6 +74,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { epoch = System.currentTimeMillis() - 2 * rotationPeriod; } + @Override @Before public void setUp() { testDir.mkdirs(); @@ -88,7 +86,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { return Guice.createInjector(new TestDatabaseModule(dir), new TestLifecycleModule(), new TestSystemModule(), new CryptoModule(), new DatabaseModule(), new MessagingModule(), - new DuplexMessagingModule(), new SimplexMessagingModule(), + new DuplexMessagingModule(), new SimplexMessagingModule(), new SerialModule(), new TransportModule()); } @@ -122,6 +120,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { new byte[MAX_PUBLIC_KEY_LENGTH]); ContactId contactId = db.addContact(bobAuthor, aliceId); // Add the inbox group + GroupFactory gf = alice.getInstance(GroupFactory.class); + Group group = gf.createGroup("Group", new byte[GROUP_SALT_LENGTH]); db.addGroup(group); db.setInboxGroup(contactId, group); // Add the transport and the endpoint @@ -181,6 +181,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { new byte[MAX_PUBLIC_KEY_LENGTH]); ContactId contactId = db.addContact(aliceAuthor, bobId); // Add the inbox group + GroupFactory gf = bob.getInstance(GroupFactory.class); + Group group = gf.createGroup("Group", new byte[GROUP_SALT_LENGTH]); db.addGroup(group); db.setInboxGroup(contactId, group); // Add the transport and the endpoint @@ -228,6 +230,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { db.close(); } + @Override @After public void tearDown() { TestUtils.deleteTestDirectory(testDir); @@ -235,7 +238,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { private static class MessageListener implements EventListener { - private boolean messageAdded = false; + private volatile boolean messageAdded = false; public void eventOccurred(Event e) { if(e instanceof MessageAddedEvent) messageAdded = true; -- GitLab