From 4fe19aeec20e3587571640f74b2541c49e6755c9 Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Fri, 22 Nov 2019 10:33:12 -0800 Subject: [PATCH] [DEADCODE] Remove old request handler tests Now that all the implementations are unit tested in P2PDataStorage, the old tests can be removed. --- .../p2p/peers/getdata/RequestDataHandler.java | 4 +- .../getdata/GetDataRequestHandlerTest.java | 199 -------------- .../getdata/GetDataRequestOnMessageTest.java | 160 ----------- .../RequestDataHandlerRequestDataTest.java | 249 ------------------ .../bisq/network/p2p/storage/TestState.java | 5 +- 5 files changed, 2 insertions(+), 615 deletions(-) delete mode 100644 p2p/src/test/java/bisq/network/p2p/peers/getdata/GetDataRequestHandlerTest.java delete mode 100644 p2p/src/test/java/bisq/network/p2p/peers/getdata/GetDataRequestOnMessageTest.java delete mode 100644 p2p/src/test/java/bisq/network/p2p/peers/getdata/RequestDataHandlerRequestDataTest.java diff --git a/p2p/src/main/java/bisq/network/p2p/peers/getdata/RequestDataHandler.java b/p2p/src/main/java/bisq/network/p2p/peers/getdata/RequestDataHandler.java index 1685a96a659..4d1ada32941 100644 --- a/p2p/src/main/java/bisq/network/p2p/peers/getdata/RequestDataHandler.java +++ b/p2p/src/main/java/bisq/network/p2p/peers/getdata/RequestDataHandler.java @@ -35,7 +35,6 @@ import bisq.common.proto.network.NetworkEnvelope; import bisq.common.proto.network.NetworkPayload; -import com.google.common.annotations.VisibleForTesting; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.SettableFuture; @@ -87,8 +86,7 @@ public interface Listener { private final PeerManager peerManager; private final Listener listener; private Timer timeoutTimer; - @VisibleForTesting - final int nonce = new Random().nextInt(); + private final int nonce = new Random().nextInt(); private boolean stopped; diff --git a/p2p/src/test/java/bisq/network/p2p/peers/getdata/GetDataRequestHandlerTest.java b/p2p/src/test/java/bisq/network/p2p/peers/getdata/GetDataRequestHandlerTest.java deleted file mode 100644 index 10a7555e785..00000000000 --- a/p2p/src/test/java/bisq/network/p2p/peers/getdata/GetDataRequestHandlerTest.java +++ /dev/null @@ -1,199 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.network.p2p.peers.getdata; - -import bisq.network.p2p.NodeAddress; -import bisq.network.p2p.TestUtils; -import bisq.network.p2p.network.Connection; -import bisq.network.p2p.network.NetworkNode; -import bisq.network.p2p.peers.getdata.messages.GetDataResponse; -import bisq.network.p2p.peers.getdata.messages.GetUpdatedDataRequest; -import bisq.network.p2p.peers.getdata.messages.PreliminaryGetDataRequest; -import bisq.network.p2p.storage.P2PDataStorage; -import bisq.network.p2p.storage.TestState; -import bisq.network.p2p.storage.mocks.PersistableNetworkPayloadStub; -import bisq.network.p2p.storage.mocks.ProtectedStoragePayloadStub; -import bisq.network.p2p.storage.payload.PersistableNetworkPayload; -import bisq.network.p2p.storage.payload.ProtectedStorageEntry; -import bisq.network.p2p.storage.payload.ProtectedStoragePayload; - -import bisq.common.Proto; -import bisq.common.app.Capabilities; -import bisq.common.app.Capability; - -import com.google.common.util.concurrent.SettableFuture; - -import java.security.KeyPair; -import java.security.NoSuchAlgorithmException; - -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import static org.mockito.Mockito.*; - -public class GetDataRequestHandlerTest { - private TestState testState; - - @Mock - NetworkNode networkNode; - - private NodeAddress localNodeAddress; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - this.testState = new TestState(); - - this.localNodeAddress = new NodeAddress("localhost", 8080); - when(networkNode.getNodeAddress()).thenReturn(this.localNodeAddress); - - // Set up basic capabilities to ensure message contains it. Ensure it is unique from other tests - // so we catch mismatch bugs. - Capabilities.app.addAll(Capability.DAO_FULL_NODE); - } - - /** - * Generates a unique ProtectedStorageEntry that is valid for add. This is used to initialize P2PDataStorage state - * so the tests can validate the correct behavior. Adds of identical payloads with different sequence numbers - * is not supported. - */ - private ProtectedStorageEntry getProtectedStorageEntryForAdd() throws NoSuchAlgorithmException { - KeyPair ownerKeys = TestUtils.generateKeyPair(); - - ProtectedStoragePayload protectedStoragePayload = new ProtectedStoragePayloadStub(ownerKeys.getPublic()); - - ProtectedStorageEntry stub = mock(ProtectedStorageEntry.class); - when(stub.getOwnerPubKey()).thenReturn(ownerKeys.getPublic()); - when(stub.isValidForAddOperation()).thenReturn(true); - when(stub.matchesRelevantPubKey(any(ProtectedStorageEntry.class))).thenReturn(true); - when(stub.getSequenceNumber()).thenReturn(1); - when(stub.getProtectedStoragePayload()).thenReturn(protectedStoragePayload); - - return stub; - } - - // TESTCASE: Construct a request that requires excluding duplicates and adding missing entrys for - // PersistableNetworkPayloads and ProtectedStorageEntrys to verify the correct keys are added to the response. - @Test - public void handle_PreliminaryGetDataRequestTest() throws NoSuchAlgorithmException { - - // Construct a seed node w/ 2 PersistableNetworkPayloads and 2 ProtectedStorageEntrys and a PreliminaryGetDataRequest - // that only contains 1 known PersistableNetworkPayload and 1 known ProtectedStorageEntry as well as 2 unknowns - PersistableNetworkPayload pnp_onSeedNodeNotInRequest = new PersistableNetworkPayloadStub(new byte[] { 1 }); - PersistableNetworkPayload pnp_onSeedNodeAndInRequest = new PersistableNetworkPayloadStub(new byte[] { 2 }); - PersistableNetworkPayload pnp_onlyInRequest = new PersistableNetworkPayloadStub(new byte[] { 3 }); - ProtectedStorageEntry pse_onSeedNodeNotInRequest = getProtectedStorageEntryForAdd(); - ProtectedStorageEntry pse_onSeedNodeAndInRequest = getProtectedStorageEntryForAdd(); - ProtectedStorageEntry pse_onlyInRequest = getProtectedStorageEntryForAdd(); - - this.testState.getMockedStorage().addPersistableNetworkPayload( - pnp_onSeedNodeNotInRequest, this.localNodeAddress, false, false, false, false); - this.testState.getMockedStorage().addPersistableNetworkPayload( - pnp_onSeedNodeAndInRequest, this.localNodeAddress, false, false, false, false); - this.testState.getMockedStorage().addProtectedStorageEntry(pse_onSeedNodeNotInRequest, this.localNodeAddress, null, false); - this.testState.getMockedStorage().addProtectedStorageEntry(pse_onSeedNodeAndInRequest, this.localNodeAddress, null, false); - - SettableFuture sendFuture = mock(SettableFuture.class); - final ArgumentCaptor captor = ArgumentCaptor.forClass(GetDataResponse.class); - when(this.networkNode.sendMessage(any(Connection.class), captor.capture())).thenReturn(sendFuture); - - PreliminaryGetDataRequest getDataRequest = mock(PreliminaryGetDataRequest.class); - HashSet knownKeysSet = new HashSet<>(Arrays.asList( - pnp_onSeedNodeAndInRequest.getHash(), - pnp_onlyInRequest.getHash(), - P2PDataStorage.get32ByteHash(pse_onSeedNodeAndInRequest.getProtectedStoragePayload()), - P2PDataStorage.get32ByteHash(pse_onlyInRequest.getProtectedStoragePayload()))); - when(getDataRequest.getNonce()).thenReturn(1); - when(getDataRequest.getExcludedKeys()).thenReturn(knownKeysSet); - - Connection connection = mock(Connection.class); - when(connection.noCapabilityRequiredOrCapabilityIsSupported(any(Proto.class))).thenReturn(true); - - GetDataRequestHandler handler = - new GetDataRequestHandler(this.networkNode, this.testState.getMockedStorage(), null); - handler.handle(getDataRequest, connection); - - // Verify the request node is sent back only the 2 missing payloads - GetDataResponse getDataResponse = captor.getValue(); - Assert.assertEquals(getDataResponse.getRequestNonce(), getDataRequest.getNonce()); - Assert.assertEquals(getDataResponse.getSupportedCapabilities(), Capabilities.app); - Assert.assertEquals(getDataResponse.getRequestNonce(), getDataRequest.getNonce()); - Assert.assertFalse(getDataResponse.isGetUpdatedDataResponse()); - Assert.assertEquals(getDataResponse.getPersistableNetworkPayloadSet(), new HashSet<>(Collections.singletonList(pnp_onSeedNodeNotInRequest))); - Assert.assertEquals(getDataResponse.getDataSet(), new HashSet<>(Collections.singletonList(pse_onSeedNodeNotInRequest))); - } - - // TESTCASE: Same as above, but with an GetUpdatedDataRequest - @Test - public void handle_GetUpdatedDataRequestTest() throws NoSuchAlgorithmException { - - // Construct a seed node w/ 2 PersistableNetworkPayloads and 2 ProtectedStorageEntrys and a PreliminaryGetDataRequest - // that only contains 1 known PersistableNetworkPayload and 1 known ProtectedStorageEntry as well as 2 unknowns - PersistableNetworkPayload pnp_onSeedNodeNotInRequest = new PersistableNetworkPayloadStub(new byte[] { 1 }); - PersistableNetworkPayload pnp_onSeedNodeAndInRequest = new PersistableNetworkPayloadStub(new byte[] { 2 }); - PersistableNetworkPayload pnp_onlyInRequest = new PersistableNetworkPayloadStub(new byte[] { 3 }); - ProtectedStorageEntry pse_onSeedNodeNotInRequest = getProtectedStorageEntryForAdd(); - ProtectedStorageEntry pse_onSeedNodeAndInRequest = getProtectedStorageEntryForAdd(); - ProtectedStorageEntry pse_onlyInRequest = getProtectedStorageEntryForAdd(); - - this.testState.getMockedStorage().addPersistableNetworkPayload( - pnp_onSeedNodeNotInRequest, this.localNodeAddress, false, false, false, false); - this.testState.getMockedStorage().addPersistableNetworkPayload( - pnp_onSeedNodeAndInRequest, this.localNodeAddress, false, false, false, false); - this.testState.getMockedStorage().addProtectedStorageEntry(pse_onSeedNodeNotInRequest, this.localNodeAddress, null, false); - this.testState.getMockedStorage().addProtectedStorageEntry(pse_onSeedNodeAndInRequest, this.localNodeAddress, null, false); - - SettableFuture sendFuture = mock(SettableFuture.class); - final ArgumentCaptor captor = ArgumentCaptor.forClass(GetDataResponse.class); - when(this.networkNode.sendMessage(any(Connection.class), captor.capture())).thenReturn(sendFuture); - - GetUpdatedDataRequest getDataRequest = mock(GetUpdatedDataRequest.class); - HashSet knownKeysSet = new HashSet<>(Arrays.asList( - pnp_onSeedNodeAndInRequest.getHash(), - pnp_onlyInRequest.getHash(), - P2PDataStorage.get32ByteHash(pse_onSeedNodeAndInRequest.getProtectedStoragePayload()), - P2PDataStorage.get32ByteHash(pse_onlyInRequest.getProtectedStoragePayload()))); - when(getDataRequest.getNonce()).thenReturn(1); - when(getDataRequest.getExcludedKeys()).thenReturn(knownKeysSet); - - Connection connection = mock(Connection.class); - when(connection.noCapabilityRequiredOrCapabilityIsSupported(any(Proto.class))).thenReturn(true); - - GetDataRequestHandler handler = - new GetDataRequestHandler(this.networkNode, this.testState.getMockedStorage(), null); - handler.handle(getDataRequest, connection); - - // Verify the request node is sent back only the 2 missing payloads - GetDataResponse getDataResponse = captor.getValue(); - Assert.assertEquals(getDataResponse.getRequestNonce(), getDataRequest.getNonce()); - Assert.assertEquals(getDataResponse.getSupportedCapabilities(), Capabilities.app); - Assert.assertEquals(getDataResponse.getRequestNonce(), getDataRequest.getNonce()); - Assert.assertTrue(getDataResponse.isGetUpdatedDataResponse()); - Assert.assertEquals(getDataResponse.getPersistableNetworkPayloadSet(), new HashSet<>(Collections.singletonList(pnp_onSeedNodeNotInRequest))); - Assert.assertEquals(getDataResponse.getDataSet(), new HashSet<>(Collections.singletonList(pse_onSeedNodeNotInRequest))); - } -} diff --git a/p2p/src/test/java/bisq/network/p2p/peers/getdata/GetDataRequestOnMessageTest.java b/p2p/src/test/java/bisq/network/p2p/peers/getdata/GetDataRequestOnMessageTest.java deleted file mode 100644 index deaf85a85b1..00000000000 --- a/p2p/src/test/java/bisq/network/p2p/peers/getdata/GetDataRequestOnMessageTest.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.network.p2p.peers.getdata; - -import bisq.network.p2p.NodeAddress; -import bisq.network.p2p.TestUtils; -import bisq.network.p2p.network.Connection; -import bisq.network.p2p.network.NetworkNode; -import bisq.network.p2p.peers.PeerManager; -import bisq.network.p2p.peers.getdata.messages.GetDataRequest; -import bisq.network.p2p.peers.getdata.messages.GetDataResponse; -import bisq.network.p2p.storage.TestState; -import bisq.network.p2p.storage.mocks.PersistableNetworkPayloadStub; -import bisq.network.p2p.storage.mocks.ProtectedStoragePayloadStub; -import bisq.network.p2p.storage.payload.ProcessOncePersistableNetworkPayload; -import bisq.network.p2p.storage.payload.PersistableNetworkPayload; -import bisq.network.p2p.storage.payload.ProtectedStorageEntry; -import bisq.network.p2p.storage.payload.ProtectedStoragePayload; - -import bisq.common.app.Capabilities; -import bisq.common.app.Capability; - -import com.google.common.util.concurrent.SettableFuture; - -import java.security.KeyPair; -import java.security.NoSuchAlgorithmException; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Optional; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - - - -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -public class GetDataRequestOnMessageTest { - private TestState testState; - - @Mock - NetworkNode networkNode; - - private NodeAddress localNodeAddress; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - this.testState = new TestState(); - - this.localNodeAddress = new NodeAddress("localhost", 8080); - when(networkNode.getNodeAddress()).thenReturn(this.localNodeAddress); - - // Set up basic capabilities to ensure message contains it. Ensure it is unique from other tests - // so we catch mismatch bugs. - Capabilities.app.addAll(Capability.DAO_FULL_NODE); - } - - /** - * Generates a unique ProtectedStorageEntry that is valid for add. This is used to initialize P2PDataStorage state - * so the tests can validate the correct behavior. Adds of identical payloads with different sequence numbers - * is not supported. - */ - private ProtectedStorageEntry getProtectedStorageEntryForAdd() throws NoSuchAlgorithmException { - KeyPair ownerKeys = TestUtils.generateKeyPair(); - - ProtectedStoragePayload protectedStoragePayload = new ProtectedStoragePayloadStub(ownerKeys.getPublic()); - - ProtectedStorageEntry stub = mock(ProtectedStorageEntry.class); - when(stub.getOwnerPubKey()).thenReturn(ownerKeys.getPublic()); - when(stub.isValidForAddOperation()).thenReturn(true); - when(stub.matchesRelevantPubKey(any(ProtectedStorageEntry.class))).thenReturn(true); - when(stub.getSequenceNumber()).thenReturn(1); - when(stub.getProtectedStoragePayload()).thenReturn(protectedStoragePayload); - - return stub; - } - - static class LazyPersistableNetworkPayloadStub extends PersistableNetworkPayloadStub - implements ProcessOncePersistableNetworkPayload { - - LazyPersistableNetworkPayloadStub(byte[] hash) { - super(hash); - } - } - - // TESTCASE: GetDataResponse processing. This large tests includes all interesting variations of state - @Test - public void onMessage_GetDataResponseTest() throws NoSuchAlgorithmException { - - PersistableNetworkPayload pnp_onLocalNodeOnly = new PersistableNetworkPayloadStub(new byte[] { 1 }); - PersistableNetworkPayload pnp_inRequestOnly = new LazyPersistableNetworkPayloadStub(new byte[]{2}); - PersistableNetworkPayload pnp_onLocalNodeAndRequest = new PersistableNetworkPayloadStub(new byte[] { 3 }); - ProtectedStorageEntry pse_onLocalNodeOnly = getProtectedStorageEntryForAdd(); - ProtectedStorageEntry pse_inRequestOnly = getProtectedStorageEntryForAdd(); - ProtectedStorageEntry pse_onLocalNodeAndRequest = getProtectedStorageEntryForAdd(); - - this.testState.getMockedStorage().addPersistableNetworkPayload( - pnp_onLocalNodeOnly, this.localNodeAddress, false, false, false, false); - this.testState.getMockedStorage().addPersistableNetworkPayload( - pnp_onLocalNodeAndRequest, this.localNodeAddress, false, false, false, false); - this.testState.getMockedStorage().addProtectedStorageEntry(pse_onLocalNodeOnly, this.localNodeAddress, null, false); - this.testState.getMockedStorage().addProtectedStorageEntry(pse_onLocalNodeAndRequest, this.localNodeAddress, null, false); - - RequestDataHandler handler = new RequestDataHandler( - this.networkNode, - this.testState.getMockedStorage(), - mock(PeerManager.class), - mock(RequestDataHandler.Listener.class) - ); - - GetDataResponse getDataResponse = new GetDataResponse( - new HashSet<>(Arrays.asList(pse_inRequestOnly, pse_onLocalNodeAndRequest)), - new HashSet<>(Arrays.asList(pnp_inRequestOnly, pnp_onLocalNodeAndRequest)), - handler.nonce, false); - - NodeAddress peersNodeAddress = new NodeAddress("peer", 10); - - // Make a request with the sole reason to set the peersNodeAddress - SettableFuture sendFuture = mock(SettableFuture.class); - when(networkNode.sendMessage(any(NodeAddress.class), any(GetDataRequest.class))).thenReturn(sendFuture); - handler.requestData(peersNodeAddress, true); - - Connection connection = mock(Connection.class); - when(connection.getPeersNodeAddressOptional()).thenReturn(Optional.of(peersNodeAddress)); - handler.onMessage(getDataResponse, connection); - - Assert.assertEquals(3, this.testState.getMockedStorage().getMap().size()); - Assert.assertTrue(this.testState.getMockedStorage().getAppendOnlyDataStoreMap().containsValue(pnp_onLocalNodeOnly)); - Assert.assertTrue(this.testState.getMockedStorage().getAppendOnlyDataStoreMap().containsValue(pnp_inRequestOnly)); - Assert.assertTrue(this.testState.getMockedStorage().getAppendOnlyDataStoreMap().containsValue(pnp_onLocalNodeAndRequest)); - - Assert.assertEquals(3, this.testState.getMockedStorage().getMap().size()); - Assert.assertTrue(this.testState.getMockedStorage().getMap().containsValue(pse_onLocalNodeOnly)); - Assert.assertTrue(this.testState.getMockedStorage().getMap().containsValue(pse_inRequestOnly)); - Assert.assertTrue(this.testState.getMockedStorage().getMap().containsValue(pse_onLocalNodeAndRequest)); - } -} diff --git a/p2p/src/test/java/bisq/network/p2p/peers/getdata/RequestDataHandlerRequestDataTest.java b/p2p/src/test/java/bisq/network/p2p/peers/getdata/RequestDataHandlerRequestDataTest.java deleted file mode 100644 index e757ce07f04..00000000000 --- a/p2p/src/test/java/bisq/network/p2p/peers/getdata/RequestDataHandlerRequestDataTest.java +++ /dev/null @@ -1,249 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.network.p2p.peers.getdata; - -import bisq.network.p2p.NodeAddress; -import bisq.network.p2p.TestUtils; -import bisq.network.p2p.network.Connection; -import bisq.network.p2p.network.NetworkNode; -import bisq.network.p2p.peers.PeerManager; -import bisq.network.p2p.peers.getdata.messages.GetDataRequest; -import bisq.network.p2p.peers.getdata.messages.GetUpdatedDataRequest; -import bisq.network.p2p.peers.getdata.messages.PreliminaryGetDataRequest; -import bisq.network.p2p.storage.P2PDataStorage; -import bisq.network.p2p.storage.TestState; -import bisq.network.p2p.storage.mocks.PersistableNetworkPayloadStub; -import bisq.network.p2p.storage.mocks.ProtectedStoragePayloadStub; -import bisq.network.p2p.storage.payload.PersistableNetworkPayload; -import bisq.network.p2p.storage.payload.ProtectedStorageEntry; -import bisq.network.p2p.storage.payload.ProtectedStoragePayload; - -import bisq.common.app.Capabilities; -import bisq.common.app.Capability; - -import com.google.common.util.concurrent.SettableFuture; - -import java.security.KeyPair; -import java.security.NoSuchAlgorithmException; - -import java.util.Set; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import static org.mockito.Mockito.*; - -public class RequestDataHandlerRequestDataTest { - private TestState testState; - - @Mock - NetworkNode networkNode; - - private NodeAddress localNodeAddress; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - this.testState = new TestState(); - - this.localNodeAddress = new NodeAddress("localhost", 8080); - when(networkNode.getNodeAddress()).thenReturn(this.localNodeAddress); - - // Set up basic capabilities to ensure message contains it - Capabilities.app.addAll(Capability.MEDIATION); - } - - /** - * Returns true if the target bytes are found in the container set. - */ - private boolean byteSetContains(Set container, byte[] target) { - // Set.contains() doesn't do a deep compare, so generate a Set so equals() does what - // we want - Set translatedContainer = - P2PDataStorage.ByteArray.convertBytesSetToByteArraySet(container); - - return translatedContainer.contains(new P2PDataStorage.ByteArray(target)); - } - - /** - * Generates a unique ProtectedStorageEntry that is valid for add. This is used to initialize P2PDataStorage state - * so the tests can validate the correct behavior. Adds of identical payloads with different sequence numbers - * is not supported. - */ - private ProtectedStorageEntry getProtectedStorageEntryForAdd() throws NoSuchAlgorithmException { - KeyPair ownerKeys = TestUtils.generateKeyPair(); - - ProtectedStoragePayload protectedStoragePayload = new ProtectedStoragePayloadStub(ownerKeys.getPublic()); - - ProtectedStorageEntry stub = mock(ProtectedStorageEntry.class); - when(stub.getOwnerPubKey()).thenReturn(ownerKeys.getPublic()); - when(stub.isValidForAddOperation()).thenReturn(true); - when(stub.matchesRelevantPubKey(any(ProtectedStorageEntry.class))).thenReturn(true); - when(stub.getSequenceNumber()).thenReturn(1); - when(stub.getProtectedStoragePayload()).thenReturn(protectedStoragePayload); - - return stub; - } - - // TESTCASE: P2PDataStorage with no entries returns an empty PreliminaryGetDataRequest - @Test - public void requestData_EmptyP2PDataStore_PreliminaryGetDataRequest() { - SettableFuture sendFuture = mock(SettableFuture.class); - - final ArgumentCaptor captor = ArgumentCaptor.forClass(PreliminaryGetDataRequest.class); - when(networkNode.sendMessage(any(NodeAddress.class), captor.capture())).thenReturn(sendFuture); - - RequestDataHandler handler = new RequestDataHandler( - this.networkNode, - this.testState.getMockedStorage(), - mock(PeerManager.class), - mock(RequestDataHandler.Listener.class) - ); - - handler.requestData(this.localNodeAddress, true); - - // expect empty message since p2pDataStore is empty - PreliminaryGetDataRequest getDataRequest = captor.getValue(); - - Assert.assertEquals(getDataRequest.getNonce(), handler.nonce); - Assert.assertEquals(getDataRequest.getSupportedCapabilities(), Capabilities.app); - Assert.assertTrue(getDataRequest.getExcludedKeys().isEmpty()); - } - - // TESTCASE: P2PDataStorage with no entries returns an empty PreliminaryGetDataRequest - @Test - public void requestData_EmptyP2PDataStore_GetUpdatedDataRequest() { - SettableFuture sendFuture = mock(SettableFuture.class); - - final ArgumentCaptor captor = ArgumentCaptor.forClass(GetUpdatedDataRequest.class); - when(networkNode.sendMessage(any(NodeAddress.class), captor.capture())).thenReturn(sendFuture); - - RequestDataHandler handler = new RequestDataHandler( - this.networkNode, - this.testState.getMockedStorage(), - mock(PeerManager.class), - mock(RequestDataHandler.Listener.class) - ); - - handler.requestData(this.localNodeAddress, false); - - // expect empty message since p2pDataStore is empty - GetUpdatedDataRequest getDataRequest = captor.getValue(); - - Assert.assertEquals(getDataRequest.getNonce(), handler.nonce); - Assert.assertEquals(getDataRequest.getSenderNodeAddress(), this.localNodeAddress); - Assert.assertTrue(getDataRequest.getExcludedKeys().isEmpty()); - } - - // TESTCASE: P2PDataStorage with PersistableNetworkPayloads and ProtectedStorageEntry generates - // correct GetDataRequestMessage with both sets of keys. - @Test - public void requestData_FilledP2PDataStore_PreliminaryGetDataRequest() throws NoSuchAlgorithmException { - SettableFuture sendFuture = mock(SettableFuture.class); - - PersistableNetworkPayload toAdd1 = new PersistableNetworkPayloadStub(new byte[] { 1 }); - PersistableNetworkPayload toAdd2 = new PersistableNetworkPayloadStub(new byte[] { 2 }); - ProtectedStorageEntry toAdd3 = getProtectedStorageEntryForAdd(); - ProtectedStorageEntry toAdd4 = getProtectedStorageEntryForAdd(); - - this.testState.getMockedStorage().addPersistableNetworkPayload( - toAdd1, this.localNodeAddress, false, false, false, false); - this.testState.getMockedStorage().addPersistableNetworkPayload( - toAdd2, this.localNodeAddress, false, false, false, false); - - this.testState.getMockedStorage().addProtectedStorageEntry(toAdd3, this.localNodeAddress, null, false); - this.testState.getMockedStorage().addProtectedStorageEntry(toAdd4, this.localNodeAddress, null, false); - - final ArgumentCaptor captor = ArgumentCaptor.forClass(PreliminaryGetDataRequest.class); - when(networkNode.sendMessage(any(NodeAddress.class), captor.capture())).thenReturn(sendFuture); - - RequestDataHandler handler = new RequestDataHandler( - this.networkNode, - this.testState.getMockedStorage(), - mock(PeerManager.class), - mock(RequestDataHandler.Listener.class) - ); - - handler.requestData(this.localNodeAddress, true); - - // expect empty message since p2pDataStore is empty - PreliminaryGetDataRequest getDataRequest = captor.getValue(); - - Assert.assertEquals(getDataRequest.getNonce(), handler.nonce); - Assert.assertEquals(getDataRequest.getSupportedCapabilities(), Capabilities.app); - Assert.assertEquals(4, getDataRequest.getExcludedKeys().size()); - Assert.assertTrue(byteSetContains(getDataRequest.getExcludedKeys(), toAdd1.getHash())); - Assert.assertTrue(byteSetContains(getDataRequest.getExcludedKeys(), toAdd2.getHash())); - Assert.assertTrue(byteSetContains(getDataRequest.getExcludedKeys(), - P2PDataStorage.get32ByteHash(toAdd3.getProtectedStoragePayload()))); - Assert.assertTrue(byteSetContains(getDataRequest.getExcludedKeys(), - P2PDataStorage.get32ByteHash(toAdd4.getProtectedStoragePayload()))); - } - - // TESTCASE: P2PDataStorage with PersistableNetworkPayloads and ProtectedStorageEntry generates - // correct GetDataRequestMessage with both sets of keys. - @Test - public void requestData_FilledP2PDataStore_GetUpdatedDataRequest() throws NoSuchAlgorithmException { - SettableFuture sendFuture = mock(SettableFuture.class); - - PersistableNetworkPayload toAdd1 = new PersistableNetworkPayloadStub(new byte[] { 1 }); - PersistableNetworkPayload toAdd2 = new PersistableNetworkPayloadStub(new byte[] { 2 }); - ProtectedStorageEntry toAdd3 = getProtectedStorageEntryForAdd(); - ProtectedStorageEntry toAdd4 = getProtectedStorageEntryForAdd(); - - this.testState.getMockedStorage().addPersistableNetworkPayload( - toAdd1, this.localNodeAddress, false, false, false, false); - this.testState.getMockedStorage().addPersistableNetworkPayload( - toAdd2, this.localNodeAddress, false, false, false, false); - - this.testState.getMockedStorage().addProtectedStorageEntry(toAdd3, this.localNodeAddress, null, false); - this.testState.getMockedStorage().addProtectedStorageEntry(toAdd4, this.localNodeAddress, null, false); - - final ArgumentCaptor captor = ArgumentCaptor.forClass(GetUpdatedDataRequest.class); - when(networkNode.sendMessage(any(NodeAddress.class), captor.capture())).thenReturn(sendFuture); - - RequestDataHandler handler = new RequestDataHandler( - this.networkNode, - this.testState.getMockedStorage(), - mock(PeerManager.class), - mock(RequestDataHandler.Listener.class) - ); - - handler.requestData(this.localNodeAddress, false); - - // expect empty message since p2pDataStore is empty - GetUpdatedDataRequest getDataRequest = captor.getValue(); - - Assert.assertEquals(getDataRequest.getNonce(), handler.nonce); - Assert.assertEquals(getDataRequest.getSenderNodeAddress(), this.localNodeAddress); - Assert.assertEquals(4, getDataRequest.getExcludedKeys().size()); - Assert.assertTrue(byteSetContains(getDataRequest.getExcludedKeys(), toAdd1.getHash())); - Assert.assertTrue(byteSetContains(getDataRequest.getExcludedKeys(), toAdd2.getHash())); - Assert.assertTrue(byteSetContains(getDataRequest.getExcludedKeys(), - P2PDataStorage.get32ByteHash(toAdd3.getProtectedStoragePayload()))); - Assert.assertTrue(byteSetContains(getDataRequest.getExcludedKeys(), - P2PDataStorage.get32ByteHash(toAdd4.getProtectedStoragePayload()))); - } -} diff --git a/p2p/src/test/java/bisq/network/p2p/storage/TestState.java b/p2p/src/test/java/bisq/network/p2p/storage/TestState.java index 15e80a9c6ac..51d831df41a 100644 --- a/p2p/src/test/java/bisq/network/p2p/storage/TestState.java +++ b/p2p/src/test/java/bisq/network/p2p/storage/TestState.java @@ -51,8 +51,6 @@ import java.util.Set; import java.util.concurrent.TimeUnit; -import lombok.Getter; - import org.junit.Assert; import org.mockito.ArgumentCaptor; @@ -67,7 +65,6 @@ public class TestState { static final int MAX_SEQUENCE_NUMBER_MAP_SIZE_BEFORE_PURGE = 5; - @Getter P2PDataStorage mockedStorage; final Broadcaster mockBroadcaster; @@ -77,7 +74,7 @@ public class TestState { private final ProtectedDataStoreService protectedDataStoreService; final ClockFake clockFake; - public TestState() { + TestState() { this.mockBroadcaster = mock(Broadcaster.class); this.mockSeqNrStorage = mock(Storage.class); this.clockFake = new ClockFake();