mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 09:52:23 +01:00
Write FilterManagerAddFilterToNetworkTests
This commit is contained in:
parent
bf98531c46
commit
b255dad5c3
@ -0,0 +1,244 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.filter;
|
||||||
|
|
||||||
|
import bisq.core.provider.ProvidersRepository;
|
||||||
|
import bisq.core.user.Preferences;
|
||||||
|
import bisq.core.user.User;
|
||||||
|
|
||||||
|
import bisq.network.p2p.P2PService;
|
||||||
|
import bisq.network.p2p.network.BanFilter;
|
||||||
|
import bisq.network.p2p.storage.P2PDataStorage;
|
||||||
|
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
|
||||||
|
|
||||||
|
import bisq.common.app.DevEnv;
|
||||||
|
import bisq.common.config.Config;
|
||||||
|
import bisq.common.crypto.KeyRing;
|
||||||
|
import bisq.common.crypto.Sig;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.ECKey;
|
||||||
|
|
||||||
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
|
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.NoSuchProviderException;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
import java.security.Security;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
import static org.bitcoinj.core.Utils.HEX;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class FilterManagerAddFilterToNetworkTests {
|
||||||
|
static {
|
||||||
|
Security.addProvider(new BouncyCastleProvider());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<P2PDataStorage.ByteArray, ProtectedStorageEntry> p2pStorageMap;
|
||||||
|
private FilterManager filterManager;
|
||||||
|
|
||||||
|
private final PublicKey ownerPublicKey;
|
||||||
|
private final String privilegedDevPubKeyHex;
|
||||||
|
private final ECKey privilegedDevEcKey;
|
||||||
|
|
||||||
|
public FilterManagerAddFilterToNetworkTests() throws NoSuchAlgorithmException, NoSuchProviderException {
|
||||||
|
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(Sig.KEY_ALGO, "BC");
|
||||||
|
KeyPair ownerKeyPair = keyPairGenerator.generateKeyPair();
|
||||||
|
ownerPublicKey = ownerKeyPair.getPublic();
|
||||||
|
|
||||||
|
privilegedDevEcKey = ECKey.fromPrivate(new BigInteger(1, HEX.decode(DevEnv.DEV_PRIVILEGE_PRIV_KEY)));
|
||||||
|
privilegedDevPubKeyHex = HEX.encode(privilegedDevEcKey.getPubKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void beforeEach(@TempDir Path tmpDir, @Mock P2PService p2PService, @Mock P2PDataStorage p2pDataStorage) {
|
||||||
|
doReturn(p2pDataStorage).when(p2PService).getP2PDataStorage();
|
||||||
|
|
||||||
|
p2pStorageMap = new HashMap<>();
|
||||||
|
doReturn(p2pStorageMap).when(p2pDataStorage).getMap();
|
||||||
|
|
||||||
|
Config config = mock(Config.class);
|
||||||
|
File configFile = tmpDir.resolve("configFile").toFile();
|
||||||
|
doReturn(configFile).when(config).getConfigFile();
|
||||||
|
|
||||||
|
filterManager = new FilterManager(
|
||||||
|
p2PService,
|
||||||
|
mock(KeyRing.class),
|
||||||
|
mock(User.class),
|
||||||
|
mock(Preferences.class),
|
||||||
|
config,
|
||||||
|
mock(ProvidersRepository.class),
|
||||||
|
mock(BanFilter.class),
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addFilterWithInvalidPublicKey() {
|
||||||
|
// There should exist no filter before we add our filter
|
||||||
|
assertNull(filterManager.getFilter());
|
||||||
|
|
||||||
|
Filter filter = TestFilter.createFilter(ownerPublicKey, "invalidPubKeyAsHex");
|
||||||
|
p2pStorageMap.put(
|
||||||
|
new P2PDataStorage.ByteArray(new byte[100]),
|
||||||
|
TestFilter.createProtectedStorageEntryForFilter(filter)
|
||||||
|
);
|
||||||
|
|
||||||
|
filterManager.onAllServicesInitialized();
|
||||||
|
|
||||||
|
// FilterManager didn't add our filter
|
||||||
|
assertNull(filterManager.getFilter());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addFilterWithInvalidSignature() {
|
||||||
|
// No filter before adding our filter
|
||||||
|
assertNull(filterManager.getFilter());
|
||||||
|
|
||||||
|
Filter filter = TestFilter.createFilter(ownerPublicKey, privilegedDevPubKeyHex);
|
||||||
|
p2pStorageMap.put(
|
||||||
|
new P2PDataStorage.ByteArray(new byte[100]),
|
||||||
|
TestFilter.createProtectedStorageEntryForFilter(filter)
|
||||||
|
);
|
||||||
|
|
||||||
|
filterManager.onAllServicesInitialized();
|
||||||
|
|
||||||
|
// FilterManager didn't add our filter
|
||||||
|
assertNull(filterManager.getFilter());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void publishValidFilter() {
|
||||||
|
// No filter before adding our filter
|
||||||
|
assertNull(filterManager.getFilter());
|
||||||
|
|
||||||
|
Filter filterWithSig = TestFilter.createSignedFilter(ownerPublicKey, privilegedDevEcKey);
|
||||||
|
p2pStorageMap.put(
|
||||||
|
new P2PDataStorage.ByteArray(new byte[100]),
|
||||||
|
TestFilter.createProtectedStorageEntryForFilter(filterWithSig)
|
||||||
|
);
|
||||||
|
|
||||||
|
filterManager.onAllServicesInitialized();
|
||||||
|
|
||||||
|
// Our filter got set
|
||||||
|
Filter currentFilter = filterManager.getFilter();
|
||||||
|
assertNotNull(currentFilter);
|
||||||
|
assertEquals(filterWithSig, currentFilter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addTooOldFilter() {
|
||||||
|
// No filter before adding our filter
|
||||||
|
assertNull(filterManager.getFilter());
|
||||||
|
|
||||||
|
long creationTime = System.currentTimeMillis();
|
||||||
|
Filter firstFilterWithSig = TestFilter.createSignedFilter(ownerPublicKey, privilegedDevEcKey, creationTime);
|
||||||
|
Filter secondFilterWithSig = TestFilter.createSignedFilter(ownerPublicKey, privilegedDevEcKey,
|
||||||
|
creationTime + 100);
|
||||||
|
|
||||||
|
assertNotEquals(firstFilterWithSig, secondFilterWithSig);
|
||||||
|
|
||||||
|
p2pStorageMap.put(
|
||||||
|
new P2PDataStorage.ByteArray(new byte[100]),
|
||||||
|
TestFilter.createProtectedStorageEntryForFilter(secondFilterWithSig)
|
||||||
|
);
|
||||||
|
|
||||||
|
filterManager.onAllServicesInitialized();
|
||||||
|
|
||||||
|
// Our filter got set
|
||||||
|
Filter currentFilter = filterManager.getFilter();
|
||||||
|
assertNotNull(currentFilter);
|
||||||
|
assertEquals(secondFilterWithSig, currentFilter);
|
||||||
|
|
||||||
|
p2pStorageMap.clear();
|
||||||
|
p2pStorageMap.put(
|
||||||
|
new P2PDataStorage.ByteArray(new byte[100]),
|
||||||
|
TestFilter.createProtectedStorageEntryForFilter(firstFilterWithSig)
|
||||||
|
);
|
||||||
|
|
||||||
|
filterManager.onAllServicesInitialized();
|
||||||
|
|
||||||
|
// Our filter got set
|
||||||
|
currentFilter = filterManager.getFilter();
|
||||||
|
assertNotNull(currentFilter);
|
||||||
|
assertEquals(secondFilterWithSig, currentFilter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addNewerFilter() {
|
||||||
|
// No filter before adding our filter
|
||||||
|
assertNull(filterManager.getFilter());
|
||||||
|
|
||||||
|
long creationTime = System.currentTimeMillis();
|
||||||
|
Filter firstFilterWithSig = TestFilter.createSignedFilter(ownerPublicKey, privilegedDevEcKey, creationTime);
|
||||||
|
Filter secondFilterWithSig = TestFilter.createSignedFilter(ownerPublicKey, privilegedDevEcKey,
|
||||||
|
creationTime + 100);
|
||||||
|
|
||||||
|
assertNotEquals(firstFilterWithSig, secondFilterWithSig);
|
||||||
|
|
||||||
|
p2pStorageMap.put(
|
||||||
|
new P2PDataStorage.ByteArray(new byte[100]),
|
||||||
|
TestFilter.createProtectedStorageEntryForFilter(firstFilterWithSig)
|
||||||
|
);
|
||||||
|
|
||||||
|
filterManager.onAllServicesInitialized();
|
||||||
|
|
||||||
|
// Our filter got set
|
||||||
|
Filter currentFilter = filterManager.getFilter();
|
||||||
|
assertNotNull(currentFilter);
|
||||||
|
assertEquals(firstFilterWithSig, currentFilter);
|
||||||
|
|
||||||
|
p2pStorageMap.clear();
|
||||||
|
p2pStorageMap.put(
|
||||||
|
new P2PDataStorage.ByteArray(new byte[100]),
|
||||||
|
TestFilter.createProtectedStorageEntryForFilter(secondFilterWithSig)
|
||||||
|
);
|
||||||
|
|
||||||
|
filterManager.onAllServicesInitialized();
|
||||||
|
|
||||||
|
// Our filter got set
|
||||||
|
currentFilter = filterManager.getFilter();
|
||||||
|
assertNotNull(currentFilter);
|
||||||
|
assertEquals(secondFilterWithSig, currentFilter);
|
||||||
|
}
|
||||||
|
}
|
113
core/src/test/java/bisq/core/filter/TestFilter.java
Normal file
113
core/src/test/java/bisq/core/filter/TestFilter.java
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.filter;
|
||||||
|
|
||||||
|
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.ECKey;
|
||||||
|
import org.bitcoinj.core.Sha256Hash;
|
||||||
|
|
||||||
|
import org.bouncycastle.util.encoders.Base64;
|
||||||
|
|
||||||
|
import java.security.PublicKey;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import static org.bitcoinj.core.Utils.HEX;
|
||||||
|
|
||||||
|
public class TestFilter {
|
||||||
|
|
||||||
|
public static ProtectedStorageEntry createProtectedStorageEntryForFilter(Filter filter) {
|
||||||
|
return new ProtectedStorageEntry(
|
||||||
|
filter,
|
||||||
|
filter.getOwnerPubKey(),
|
||||||
|
1000,
|
||||||
|
new byte[100],
|
||||||
|
Clock.systemDefaultZone()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Filter createSignedFilter(PublicKey ownerPublicKey, ECKey signerKey) {
|
||||||
|
return createSignedFilter(ownerPublicKey, signerKey, System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Filter createSignedFilter(PublicKey ownerPublicKey, ECKey signerKey, long creationDate) {
|
||||||
|
Filter unsignedFilter = createFilter(ownerPublicKey, HEX.encode(signerKey.getPubKey()), creationDate);
|
||||||
|
return signFilter(unsignedFilter, signerKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Filter createFilter(PublicKey ownerPublicKey, String signerPubKeyAsHex) {
|
||||||
|
return createFilter(ownerPublicKey, signerPubKeyAsHex, System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Filter createFilter(PublicKey ownerPublicKey, String signerPubKeyAsHex, long creationDate) {
|
||||||
|
return new Filter(
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
false,
|
||||||
|
Collections.emptyList(),
|
||||||
|
false,
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptyList(),
|
||||||
|
ownerPublicKey.getEncoded(),
|
||||||
|
creationDate,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
signerPubKeyAsHex,
|
||||||
|
Collections.emptyList(),
|
||||||
|
false,
|
||||||
|
Collections.emptyList(),
|
||||||
|
Collections.emptySet(),
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
1,
|
||||||
|
Collections.emptyList(),
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
Collections.emptyList()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Filter signFilter(Filter unsignedFilter, ECKey signerKey) {
|
||||||
|
byte[] filterData = unsignedFilter.toProtoMessage().toByteArray();
|
||||||
|
Sha256Hash hash = Sha256Hash.of(filterData);
|
||||||
|
|
||||||
|
ECKey.ECDSASignature ecdsaSignature = signerKey.sign(hash);
|
||||||
|
byte[] encodeToDER = ecdsaSignature.encodeToDER();
|
||||||
|
|
||||||
|
String signatureAsBase64 = new String(Base64.encode(encodeToDER), StandardCharsets.UTF_8);
|
||||||
|
return Filter.cloneWithSig(unsignedFilter, signatureAsBase64);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user