From 4b5ad9ace4a482ddd2dbb2723e0db05654cc198d Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 6 Jun 2019 11:34:00 +0100 Subject: [PATCH] Make test expectations modular. --- .../rendezvous/RendezvousPollerImplTest.java | 150 +++++++----------- 1 file changed, 61 insertions(+), 89 deletions(-) diff --git a/bramble-core/src/test/java/org/briarproject/bramble/rendezvous/RendezvousPollerImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/rendezvous/RendezvousPollerImplTest.java index 92af0f780..4832ff870 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/rendezvous/RendezvousPollerImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/rendezvous/RendezvousPollerImplTest.java @@ -115,20 +115,10 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase { with(MILLISECONDS)); will(new CaptureArgumentAction<>(captureExpiryTask, Runnable.class, 0)); - // Load our handshake key pair - oneOf(db).transactionWithResult(with(true), withDbCallable(txn)); - will(returnValue(handshakeKeyPair)); - // Derive the rendezvous key - oneOf(transportCrypto).deriveStaticMasterKey( - pendingContact.getPublicKey(), handshakeKeyPair); - will(returnValue(staticMasterKey)); - oneOf(rendezvousCrypto).deriveRendezvousKey(staticMasterKey); - will(returnValue(rendezvousKey)); - oneOf(transportCrypto).isAlice(pendingContact.getPublicKey(), - handshakeKeyPair); - will(returnValue(alice)); }}); + expectDeriveRendezvousKey(); + rendezvousPoller.startService(); context.assertIsSatisfied(); @@ -157,45 +147,27 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase { @Test public void testCreatesAndClosesEndpointsWhenPendingContactIsAddedAndRemoved() throws Exception { - Transaction txn = new Transaction(null, true); long now = pendingContact.getTimestamp(); // Enable the transport - no endpoints should be created yet - context.checking(new Expectations() {{ - oneOf(pluginManager).getPlugin(transportId); - will(returnValue(plugin)); - oneOf(plugin).supportsRendezvous(); - will(returnValue(true)); - allowing(plugin).getId(); - will(returnValue(transportId)); - }}); + expectGetPlugin(); rendezvousPoller.eventOccurred(new TransportEnabledEvent(transportId)); context.assertIsSatisfied(); // Add the pending contact - endpoint should be created and polled - context.checking(new DbExpectations() {{ + context.checking(new Expectations() {{ // Add pending contact oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), with(RENDEZVOUS_TIMEOUT_MS), with(MILLISECONDS)); - oneOf(db).transactionWithResult(with(true), withDbCallable(txn)); - will(returnValue(handshakeKeyPair)); - oneOf(transportCrypto).deriveStaticMasterKey( - pendingContact.getPublicKey(), handshakeKeyPair); - will(returnValue(staticMasterKey)); - oneOf(rendezvousCrypto).deriveRendezvousKey(staticMasterKey); - will(returnValue(rendezvousKey)); - oneOf(transportCrypto).isAlice(pendingContact.getPublicKey(), - handshakeKeyPair); - will(returnValue(alice)); - oneOf(rendezvousCrypto).createKeyMaterialSource(rendezvousKey, - transportId); - will(returnValue(keyMaterialSource)); - oneOf(plugin).createRendezvousEndpoint(with(keyMaterialSource), - with(alice), with(any(ConnectionHandler.class))); - will(returnValue(rendezvousEndpoint)); + }}); + + expectDeriveRendezvousKey(); + expectCreateEndpoint(); + + context.checking(new Expectations() {{ // Poll newly added pending contact oneOf(rendezvousEndpoint).getRemoteTransportProperties(); will(returnValue(transportProperties)); @@ -224,25 +196,17 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase { @Test public void testCreatesAndClosesEndpointsWhenPendingContactIsAddedAndExpired() throws Exception { - Transaction txn = new Transaction(null, true); long now = pendingContact.getTimestamp(); AtomicReference captureExpiryTask = new AtomicReference<>(); // Enable the transport - no endpoints should be created yet - context.checking(new Expectations() {{ - oneOf(pluginManager).getPlugin(transportId); - will(returnValue(plugin)); - oneOf(plugin).supportsRendezvous(); - will(returnValue(true)); - allowing(plugin).getId(); - will(returnValue(transportId)); - }}); + expectGetPlugin(); rendezvousPoller.eventOccurred(new TransportEnabledEvent(transportId)); context.assertIsSatisfied(); // Add the pending contact - endpoint should be created and polled - context.checking(new DbExpectations() {{ + context.checking(new Expectations() {{ // Add pending contact oneOf(clock).currentTimeMillis(); will(returnValue(now)); @@ -251,22 +215,12 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase { with(RENDEZVOUS_TIMEOUT_MS), with(MILLISECONDS)); will(new CaptureArgumentAction<>(captureExpiryTask, Runnable.class, 0)); - oneOf(db).transactionWithResult(with(true), withDbCallable(txn)); - will(returnValue(handshakeKeyPair)); - oneOf(transportCrypto).deriveStaticMasterKey( - pendingContact.getPublicKey(), handshakeKeyPair); - will(returnValue(staticMasterKey)); - oneOf(rendezvousCrypto).deriveRendezvousKey(staticMasterKey); - will(returnValue(rendezvousKey)); - oneOf(transportCrypto).isAlice(pendingContact.getPublicKey(), - handshakeKeyPair); - will(returnValue(alice)); - oneOf(rendezvousCrypto).createKeyMaterialSource(rendezvousKey, - transportId); - will(returnValue(keyMaterialSource)); - oneOf(plugin).createRendezvousEndpoint(with(keyMaterialSource), - with(alice), with(any(ConnectionHandler.class))); - will(returnValue(rendezvousEndpoint)); + }}); + + expectDeriveRendezvousKey(); + expectCreateEndpoint(); + + context.checking(new Expectations() {{ // Poll newly added pending contact oneOf(rendezvousEndpoint).getRemoteTransportProperties(); will(returnValue(transportProperties)); @@ -300,7 +254,6 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase { @Test public void testCreatesAndClosesEndpointsWhenTransportIsEnabledAndDisabled() throws Exception { - Transaction txn = new Transaction(null, true); long now = pendingContact.getTimestamp(); // Add the pending contact - no endpoints should be created yet @@ -309,37 +262,17 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase { will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), with(RENDEZVOUS_TIMEOUT_MS), with(MILLISECONDS)); - oneOf(db).transactionWithResult(with(true), withDbCallable(txn)); - will(returnValue(handshakeKeyPair)); - oneOf(transportCrypto).deriveStaticMasterKey( - pendingContact.getPublicKey(), handshakeKeyPair); - will(returnValue(staticMasterKey)); - oneOf(rendezvousCrypto).deriveRendezvousKey(staticMasterKey); - will(returnValue(rendezvousKey)); - oneOf(transportCrypto).isAlice(pendingContact.getPublicKey(), - handshakeKeyPair); - will(returnValue(alice)); }}); + expectDeriveRendezvousKey(); + rendezvousPoller.eventOccurred( new PendingContactAddedEvent(pendingContact)); context.assertIsSatisfied(); // Enable the transport - endpoint should be created - context.checking(new Expectations() {{ - oneOf(pluginManager).getPlugin(transportId); - will(returnValue(plugin)); - oneOf(plugin).supportsRendezvous(); - will(returnValue(true)); - allowing(plugin).getId(); - will(returnValue(transportId)); - oneOf(rendezvousCrypto).createKeyMaterialSource(rendezvousKey, - transportId); - will(returnValue(keyMaterialSource)); - oneOf(plugin).createRendezvousEndpoint(with(keyMaterialSource), - with(alice), with(any(ConnectionHandler.class))); - will(returnValue(rendezvousEndpoint)); - }}); + expectGetPlugin(); + expectCreateEndpoint(); rendezvousPoller.eventOccurred(new TransportEnabledEvent(transportId)); context.assertIsSatisfied(); @@ -356,4 +289,43 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase { rendezvousPoller.eventOccurred( new PendingContactRemovedEvent(pendingContact.getId())); } + + private void expectDeriveRendezvousKey() throws Exception { + Transaction txn = new Transaction(null, true); + + context.checking(new DbExpectations() {{ + oneOf(db).transactionWithResult(with(true), withDbCallable(txn)); + will(returnValue(handshakeKeyPair)); + oneOf(transportCrypto).deriveStaticMasterKey( + pendingContact.getPublicKey(), handshakeKeyPair); + will(returnValue(staticMasterKey)); + oneOf(rendezvousCrypto).deriveRendezvousKey(staticMasterKey); + will(returnValue(rendezvousKey)); + oneOf(transportCrypto).isAlice(pendingContact.getPublicKey(), + handshakeKeyPair); + will(returnValue(alice)); + }}); + } + + private void expectCreateEndpoint() { + context.checking(new Expectations() {{ + oneOf(rendezvousCrypto).createKeyMaterialSource(rendezvousKey, + transportId); + will(returnValue(keyMaterialSource)); + oneOf(plugin).createRendezvousEndpoint(with(keyMaterialSource), + with(alice), with(any(ConnectionHandler.class))); + will(returnValue(rendezvousEndpoint)); + }}); + } + + private void expectGetPlugin() { + context.checking(new Expectations() {{ + oneOf(pluginManager).getPlugin(transportId); + will(returnValue(plugin)); + oneOf(plugin).supportsRendezvous(); + will(returnValue(true)); + allowing(plugin).getId(); + will(returnValue(transportId)); + }}); + } }