From 707e7b06df57f4a37f6e97f7bb38bbff758527fd Mon Sep 17 00:00:00 2001 From: akwizgran Date: Fri, 4 Dec 2020 12:10:43 +0000 Subject: [PATCH] Add integration tests for timer mirroring. --- .../blog/BlogManagerIntegrationTest.java | 2 - .../IntroductionIntegrationTest.java | 4 +- .../messaging/AutoDeleteIntegrationTest.java | 240 ++++++++++++++++++ .../GroupInvitationIntegrationTest.java | 8 +- .../sharing/BlogSharingIntegrationTest.java | 4 +- .../sharing/ForumSharingIntegrationTest.java | 4 +- .../briar/test/BriarIntegrationTest.java | 21 +- 7 files changed, 263 insertions(+), 20 deletions(-) create mode 100644 briar-core/src/test/java/org/briarproject/briar/messaging/AutoDeleteIntegrationTest.java diff --git a/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerIntegrationTest.java index 94fad0819..84d104e74 100644 --- a/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/blog/BlogManagerIntegrationTest.java @@ -46,8 +46,6 @@ public class BlogManagerIntegrationTest public void setUp() throws Exception { super.setUp(); - author0 = identityManager0.getLocalAuthor(); - author1 = identityManager1.getLocalAuthor(); String rssTitle = getRandomString(MAX_AUTHOR_NAME_LENGTH); rssAuthor = c0.getAuthorFactory().createLocalAuthor(rssTitle); diff --git a/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java index 2c2eacf21..7ffdc7ccb 100644 --- a/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java @@ -263,8 +263,8 @@ public class IntroductionIntegrationTest addListeners(true, true); // 0 and 1 set an auto-delete timer for their conversation - setAutoDeleteTimer(c0, contactId1From0); - setAutoDeleteTimer(c1, contactId0From1); + setAutoDeleteTimer(c0, contactId1From0, MIN_AUTO_DELETE_TIMER_MS); + setAutoDeleteTimer(c1, contactId0From1, MIN_AUTO_DELETE_TIMER_MS); // Make introduction introductionManager0 diff --git a/briar-core/src/test/java/org/briarproject/briar/messaging/AutoDeleteIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/messaging/AutoDeleteIntegrationTest.java new file mode 100644 index 000000000..1778b7115 --- /dev/null +++ b/briar-core/src/test/java/org/briarproject/briar/messaging/AutoDeleteIntegrationTest.java @@ -0,0 +1,240 @@ +package org.briarproject.briar.messaging; + +import org.briarproject.bramble.api.contact.ContactId; +import org.briarproject.bramble.api.db.DatabaseComponent; +import org.briarproject.bramble.api.db.DbException; +import org.briarproject.bramble.api.sync.GroupId; +import org.briarproject.bramble.api.sync.MessageId; +import org.briarproject.bramble.test.TestDatabaseConfigModule; +import org.briarproject.briar.api.autodelete.AutoDeleteManager; +import org.briarproject.briar.api.conversation.ConversationManager; +import org.briarproject.briar.api.conversation.ConversationMessageHeader; +import org.briarproject.briar.api.messaging.MessagingManager; +import org.briarproject.briar.api.messaging.PrivateMessage; +import org.briarproject.briar.api.messaging.PrivateMessageFactory; +import org.briarproject.briar.test.BriarIntegrationTest; +import org.briarproject.briar.test.BriarIntegrationTestComponent; +import org.briarproject.briar.test.DaggerBriarIntegrationTestComponent; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static java.util.Collections.emptyList; +import static org.briarproject.briar.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS; +import static org.briarproject.briar.api.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER; +import static org.junit.Assert.assertEquals; + +public class AutoDeleteIntegrationTest + extends BriarIntegrationTest { + + @Override + protected void createComponents() { + BriarIntegrationTestComponent component = + DaggerBriarIntegrationTestComponent.builder().build(); + BriarIntegrationTestComponent.Helper.injectEagerSingletons(component); + component.inject(this); + + c0 = DaggerBriarIntegrationTestComponent.builder() + .testDatabaseConfigModule(new TestDatabaseConfigModule(t0Dir)) + .build(); + BriarIntegrationTestComponent.Helper.injectEagerSingletons(c0); + + c1 = DaggerBriarIntegrationTestComponent.builder() + .testDatabaseConfigModule(new TestDatabaseConfigModule(t1Dir)) + .build(); + BriarIntegrationTestComponent.Helper.injectEagerSingletons(c1); + + c2 = DaggerBriarIntegrationTestComponent.builder() + .testDatabaseConfigModule(new TestDatabaseConfigModule(t2Dir)) + .build(); + BriarIntegrationTestComponent.Helper.injectEagerSingletons(c2); + } + + @Test + public void testMessageWithoutTimer() throws Exception { + // 0 creates a message without a timer + MessageId messageId = createMessageWithoutTimer(c0, contactId1From0); + // The message should have been added to 0's view of the conversation + List headers0 = + getMessageHeaders(c0, contactId1From0); + assertEquals(1, headers0.size()); + ConversationMessageHeader h0 = headers0.get(0); + assertEquals(messageId, h0.getId()); + // The message should not have a timer + assertEquals(NO_AUTO_DELETE_TIMER, h0.getAutoDeleteTimer()); + // Sync the message to 1 + sync0To1(1, true); + // The message should have been added to 1's view of the conversation + List headers1 = + getMessageHeaders(c1, contactId0From1); + assertEquals(1, headers1.size()); + ConversationMessageHeader h1 = headers1.get(0); + assertEquals(messageId, h1.getId()); + // The message should not have a timer + assertEquals(NO_AUTO_DELETE_TIMER, h1.getAutoDeleteTimer()); + } + + @Test + public void testDefaultTimer() throws Exception { + // 0 creates a message with the default timer + MessageId messageId = createMessageWithTimer(c0, contactId1From0); + // The message should have been added to 0's view of the conversation + List headers0 = + getMessageHeaders(c0, contactId1From0); + assertEquals(1, headers0.size()); + ConversationMessageHeader h0 = headers0.get(0); + assertEquals(messageId, h0.getId()); + // The message should have the default timer (none) + assertEquals(NO_AUTO_DELETE_TIMER, h0.getAutoDeleteTimer()); + // Sync the message to 1 + sync0To1(1, true); + // The message should have been added to 1's view of the conversation + List headers1 = + getMessageHeaders(c1, contactId0From1); + assertEquals(1, headers1.size()); + ConversationMessageHeader h1 = headers1.get(0); + assertEquals(messageId, h1.getId()); + // The message should have the default timer (none) + assertEquals(NO_AUTO_DELETE_TIMER, h1.getAutoDeleteTimer()); + // Both peers should still be using the default timer + assertEquals(NO_AUTO_DELETE_TIMER, + getAutoDeleteTimer(c0, contactId1From0)); + assertEquals(NO_AUTO_DELETE_TIMER, + getAutoDeleteTimer(c1, contactId0From1)); + } + + @Test + public void testTimerIsMirrored() throws Exception { + // Set 0's timer + setAutoDeleteTimer(c0, contactId1From0, MIN_AUTO_DELETE_TIMER_MS); + // 0 should be using the new timer + assertEquals(MIN_AUTO_DELETE_TIMER_MS, + getAutoDeleteTimer(c0, contactId1From0)); + // 1 should still be using the default timer + assertEquals(NO_AUTO_DELETE_TIMER, + getAutoDeleteTimer(c1, contactId0From1)); + // 0 creates a message with the new timer + MessageId messageId0 = createMessageWithTimer(c0, contactId1From0); + // The message should have been added to 0's view of the conversation + List headers0 = + getMessageHeaders(c0, contactId1From0); + assertEquals(1, headers0.size()); + ConversationMessageHeader h0 = headers0.get(0); + assertEquals(messageId0, h0.getId()); + // The message should have the new timer + assertEquals(MIN_AUTO_DELETE_TIMER_MS, h0.getAutoDeleteTimer()); + // Sync the message to 1 + sync0To1(1, true); + // The message should have been added to 1's view of the conversation + List headers1 = + getMessageHeaders(c1, contactId0From1); + assertEquals(1, headers1.size()); + ConversationMessageHeader h1 = headers1.get(0); + assertEquals(messageId0, h1.getId()); + // The message should have the new timer + assertEquals(MIN_AUTO_DELETE_TIMER_MS, h1.getAutoDeleteTimer()); + // 0 and 1 should both be using the new timer + assertEquals(MIN_AUTO_DELETE_TIMER_MS, + getAutoDeleteTimer(c0, contactId1From0)); + assertEquals(MIN_AUTO_DELETE_TIMER_MS, + getAutoDeleteTimer(c1, contactId0From1)); + // 1 creates a message + MessageId messageId1 = createMessageWithTimer(c1, contactId0From1); + // The message should have been added to 1's view of the conversation + headers1 = getMessageHeaders(c1, contactId0From1); + assertEquals(2, headers1.size()); + assertEquals(messageId0, headers1.get(0).getId()); + assertEquals(messageId1, headers1.get(1).getId()); + // The message should have the new timer + assertEquals(MIN_AUTO_DELETE_TIMER_MS, + headers1.get(1).getAutoDeleteTimer()); + // Sync the message to 0 + sync1To0(1, true); + // The message should have been added to 0's view of the conversation + headers0 = getMessageHeaders(c0, contactId1From0); + assertEquals(2, headers0.size()); + assertEquals(messageId0, headers0.get(0).getId()); + assertEquals(messageId1, headers0.get(1).getId()); + // The message should have the new timer + assertEquals(MIN_AUTO_DELETE_TIMER_MS, + headers0.get(1).getAutoDeleteTimer()); + // 0 and 1 should both be using the new timer + assertEquals(MIN_AUTO_DELETE_TIMER_MS, + getAutoDeleteTimer(c0, contactId1From0)); + assertEquals(MIN_AUTO_DELETE_TIMER_MS, + getAutoDeleteTimer(c1, contactId0From1)); + } + + private MessageId createMessageWithoutTimer( + BriarIntegrationTestComponent component, ContactId contactId) + throws Exception { + DatabaseComponent db = component.getDatabaseComponent(); + ConversationManager conversationManager = + component.getConversationManager(); + MessagingManager messagingManager = component.getMessagingManager(); + PrivateMessageFactory factory = component.getPrivateMessageFactory(); + + GroupId groupId = messagingManager.getConversationId(contactId); + return db.transactionWithResult(false, txn -> { + long timestamp = conversationManager + .getTimestampForOutgoingMessage(txn, contactId); + PrivateMessage m = factory.createPrivateMessage(groupId, timestamp, + "Hi!", emptyList()); + messagingManager.addLocalMessage(txn, m); + return m.getMessage().getId(); + }); + } + + private MessageId createMessageWithTimer( + BriarIntegrationTestComponent component, ContactId contactId) + throws Exception { + DatabaseComponent db = component.getDatabaseComponent(); + ConversationManager conversationManager = + component.getConversationManager(); + AutoDeleteManager autoDeleteManager = component.getAutoDeleteManager(); + MessagingManager messagingManager = component.getMessagingManager(); + PrivateMessageFactory factory = component.getPrivateMessageFactory(); + + GroupId groupId = messagingManager.getConversationId(contactId); + return db.transactionWithResult(false, txn -> { + long timestamp = conversationManager + .getTimestampForOutgoingMessage(txn, contactId); + long timer = autoDeleteManager + .getAutoDeleteTimer(txn, contactId, timestamp); + PrivateMessage m = factory.createPrivateMessage(groupId, timestamp, + "Hi!", emptyList(), timer); + messagingManager.addLocalMessage(txn, m); + return m.getMessage().getId(); + }); + } + + private List getMessageHeaders( + BriarIntegrationTestComponent component, ContactId contactId) + throws Exception { + DatabaseComponent db = component.getDatabaseComponent(); + MessagingManager messagingManager = component.getMessagingManager(); + + return sortHeaders(db.transactionWithResult(true, txn -> + messagingManager.getMessageHeaders(txn, contactId))); + } + + private long getAutoDeleteTimer(BriarIntegrationTestComponent component, + ContactId contactId) throws DbException { + DatabaseComponent db = component.getDatabaseComponent(); + AutoDeleteManager autoDeleteManager = component.getAutoDeleteManager(); + + return db.transactionWithResult(false, + txn -> autoDeleteManager.getAutoDeleteTimer(txn, contactId)); + } + + private List sortHeaders( + Collection in) { + List out = new ArrayList<>(in); + //noinspection UseCompareMethod + out.sort((a, b) -> + Long.valueOf(a.getTimestamp()).compareTo(b.getTimestamp())); + return out; + } +} diff --git a/briar-core/src/test/java/org/briarproject/briar/privategroup/invitation/GroupInvitationIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/privategroup/invitation/GroupInvitationIntegrationTest.java index e2c49bb24..f4cbd6c7c 100644 --- a/briar-core/src/test/java/org/briarproject/briar/privategroup/invitation/GroupInvitationIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/privategroup/invitation/GroupInvitationIntegrationTest.java @@ -168,8 +168,8 @@ public class GroupInvitationIntegrationTest @Test public void testInvitationDeclineWithAutoDelete() throws Exception { // 0 and 1 set an auto-delete timer for their conversation - setAutoDeleteTimer(c0, contactId1From0); - setAutoDeleteTimer(c1, contactId0From1); + setAutoDeleteTimer(c0, contactId1From0, MIN_AUTO_DELETE_TIMER_MS); + setAutoDeleteTimer(c1, contactId0From1, MIN_AUTO_DELETE_TIMER_MS); // Send invitation sendInvitation(clock.currentTimeMillis(), null); @@ -253,8 +253,8 @@ public class GroupInvitationIntegrationTest @Test public void testInvitationAcceptWithAutoDelete() throws Exception { // 0 and 1 set an auto-delete timer for their conversation - setAutoDeleteTimer(c0, contactId1From0); - setAutoDeleteTimer(c1, contactId0From1); + setAutoDeleteTimer(c0, contactId1From0, MIN_AUTO_DELETE_TIMER_MS); + setAutoDeleteTimer(c1, contactId0From1, MIN_AUTO_DELETE_TIMER_MS); // Send invitation sendInvitation(clock.currentTimeMillis(), null); diff --git a/briar-core/src/test/java/org/briarproject/briar/sharing/BlogSharingIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/sharing/BlogSharingIntegrationTest.java index b8454c72d..173d3bebf 100644 --- a/briar-core/src/test/java/org/briarproject/briar/sharing/BlogSharingIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/sharing/BlogSharingIntegrationTest.java @@ -210,8 +210,8 @@ public class BlogSharingIntegrationTest listenToEvents(true); // Set an auto-delete timer for the conversation - setAutoDeleteTimer(c0, contactId1From0); - setAutoDeleteTimer(c1, contactId0From1); + setAutoDeleteTimer(c0, contactId1From0, MIN_AUTO_DELETE_TIMER_MS); + setAutoDeleteTimer(c1, contactId0From1, MIN_AUTO_DELETE_TIMER_MS); // Send invitation blogSharingManager0 diff --git a/briar-core/src/test/java/org/briarproject/briar/sharing/ForumSharingIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/sharing/ForumSharingIntegrationTest.java index 999e9c022..f46b494aa 100644 --- a/briar-core/src/test/java/org/briarproject/briar/sharing/ForumSharingIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/sharing/ForumSharingIntegrationTest.java @@ -191,8 +191,8 @@ public class ForumSharingIntegrationTest @Test public void testSuccessfulSharingWithAutoDelete() throws Exception { // Set an auto-delete timer for the conversation - setAutoDeleteTimer(c0, contactId1From0); - setAutoDeleteTimer(c1, contactId0From1); + setAutoDeleteTimer(c0, contactId1From0, MIN_AUTO_DELETE_TIMER_MS); + setAutoDeleteTimer(c1, contactId0From1, MIN_AUTO_DELETE_TIMER_MS); // Send invitation forumSharingManager0 diff --git a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java index 6711de50d..c2b353811 100644 --- a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java @@ -30,6 +30,7 @@ import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.test.TestTransportConnectionReader; import org.briarproject.bramble.test.TestTransportConnectionWriter; import org.briarproject.bramble.test.TestUtils; +import org.briarproject.briar.api.autodelete.AutoDeleteManager; import org.briarproject.briar.api.blog.BlogFactory; import org.briarproject.briar.api.blog.BlogPostFactory; import org.briarproject.briar.api.client.MessageTracker; @@ -63,7 +64,6 @@ import static org.briarproject.bramble.api.sync.validation.MessageState.PENDING; import static org.briarproject.bramble.test.TestPluginConfigModule.SIMPLEX_TRANSPORT_ID; import static org.briarproject.bramble.test.TestUtils.getSecretKey; import static org.briarproject.bramble.util.LogUtils.logException; -import static org.briarproject.briar.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -453,16 +453,21 @@ public abstract class BriarIntegrationTest - component.getAutoDeleteManager().setAutoDeleteTimer(txn, - contactId, MIN_AUTO_DELETE_TIMER_MS)); + ContactId contactId, long timer) throws DbException { + DatabaseComponent db = component.getDatabaseComponent(); + AutoDeleteManager autoDeleteManager = component.getAutoDeleteManager(); + + db.transaction(false, txn -> + autoDeleteManager.setAutoDeleteTimer(txn, contactId, timer)); } protected long getAutoDeleteTimer(BriarIntegrationTestComponent component, ContactId contactId, long timestamp) throws DbException { - return component.getDatabaseComponent().transactionWithResult(false, - txn -> component.getAutoDeleteManager().getAutoDeleteTimer(txn, - contactId, timestamp)); + DatabaseComponent db = component.getDatabaseComponent(); + AutoDeleteManager autoDeleteManager = component.getAutoDeleteManager(); + + return db.transactionWithResult(false, + txn -> autoDeleteManager.getAutoDeleteTimer(txn, contactId, + timestamp)); } }