Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(1/8) [TESTS] Add tests for P2PDataStorage in order to safely refactor #3554

Merged
merged 5 commits into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ private void broadcast(BroadcastMessage message, @Nullable NodeAddress sender,
broadcaster.broadcast(message, sender, listener, isDataOwner);
}

private ByteArray get32ByteHashAsByteArray(NetworkPayload data) {
public static ByteArray get32ByteHashAsByteArray(NetworkPayload data) {
return new ByteArray(P2PDataStorage.get32ByteHash(data));
}

Expand Down
1,518 changes: 1,508 additions & 10 deletions p2p/src/test/java/bisq/network/p2p/storage/P2PDataStorageTest.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

package bisq.network.p2p.storage.mocks;

import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
import bisq.network.p2p.storage.persistence.AppendOnlyDataStoreService;

import java.util.HashMap;
import java.util.Map;

public class AppendOnlyDataStoreServiceFake extends AppendOnlyDataStoreService {
private final Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> map;

public AppendOnlyDataStoreServiceFake() {
super(null);
map = new HashMap<>();
}

public Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> getMap() {
return map;
}

public void put(P2PDataStorage.ByteArray hashAsByteArray, PersistableNetworkPayload payload) {
map.put(hashAsByteArray, payload);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

package bisq.network.p2p.storage.mocks;

import bisq.network.p2p.storage.payload.DateTolerantPayload;

import java.time.Clock;

public class DateTolerantPayloadStub implements DateTolerantPayload {
private final boolean dateInTolerance;

public DateTolerantPayloadStub(boolean dateInTolerance) {
this.dateInTolerance = dateInTolerance;
}

@Override
public boolean isDateInTolerance(Clock clock) {
return this.dateInTolerance;
}

@Override
public protobuf.PersistableNetworkPayload toProtoMessage() {
throw new UnsupportedOperationException("Stub does not support protobuf");
}

@Override
public byte[] getHash() {
return new byte[] { 1 };
}

@Override
public boolean verifyHashSize() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

package bisq.network.p2p.storage.mocks;

import bisq.network.p2p.storage.payload.PersistableNetworkPayload;

public class PersistableNetworkPayloadStub implements PersistableNetworkPayload {
private final boolean hashSizeValid;

public PersistableNetworkPayloadStub(boolean hashSizeValid) {
this.hashSizeValid = hashSizeValid;
}

@Override
public protobuf.PersistableNetworkPayload toProtoMessage() {
throw new UnsupportedOperationException("Stub does not support protobuf");
}

@Override
public byte[] getHash() {
return new byte[] { 1 };
}

@Override
public boolean verifyHashSize() {
return this.hashSizeValid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

package bisq.network.p2p.storage.mocks;

import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
import bisq.network.p2p.storage.persistence.ProtectedDataStoreService;

import java.util.HashMap;
import java.util.Map;

public class ProtectedDataStoreServiceFake extends ProtectedDataStoreService {
private final Map<P2PDataStorage.ByteArray, ProtectedStorageEntry> map;

public ProtectedDataStoreServiceFake() {
super();
map = new HashMap<>();
}

public Map<P2PDataStorage.ByteArray, ProtectedStorageEntry> getMap() {
return map;
}

public void put(P2PDataStorage.ByteArray hashAsByteArray, ProtectedStorageEntry entry) {
map.put(hashAsByteArray, entry);
}
public ProtectedStorageEntry remove(P2PDataStorage.ByteArray hash, ProtectedStorageEntry protectedStorageEntry) {
return map.remove(hash);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

package bisq.network.p2p.storage.mocks;

import bisq.network.p2p.storage.payload.ProtectedStoragePayload;

import bisq.common.crypto.Sig;

import com.google.protobuf.Message;

import java.security.PublicKey;

import java.util.Map;

import lombok.Getter;

import javax.annotation.Nullable;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/*
* Stub ProtectedStoragePayload whose hash is equal to the ownerPubKey
*/
public class ProtectedStoragePayloadStub implements ProtectedStoragePayload {
@Getter
private PublicKey ownerPubKey;

private Message messageMock;

public ProtectedStoragePayloadStub(PublicKey ownerPubKey) {
this.ownerPubKey = ownerPubKey;

// Need to be able to take the hash which leverages protobuf Messages
this.messageMock = mock(protobuf.StoragePayload.class);
when(this.messageMock.toByteArray()).thenReturn(Sig.getPublicKeyBytes(ownerPubKey));
}

@Nullable
@Override
public Map<String, String> getExtraDataMap() {
return null;
}

@Override
public Message toProtoMessage() {
return this.messageMock;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline # allow mocking of final classes in tests