Skip to content

Commit

Permalink
Merge pull request #6977 from alvasw/FilterManagerAddFilterToNetworkT…
Browse files Browse the repository at this point in the history
…ests

Add FilterManagerAddFilterToNetworkTests
  • Loading branch information
alejandrogarcia83 authored Dec 31, 2023
2 parents 143628b + b255dad commit 1f072bf
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ dependencies {
testImplementation libs.hamcrest
testImplementation libs.junit.jupiter.api
testImplementation libs.junit.jupiter.params
testImplementation libs.mockito
testImplementation libs.mockito.core
testImplementation libs.mockito.junit.jupiter

testRuntimeOnly libs.junit.jupiter.engine
}
Expand Down
5 changes: 4 additions & 1 deletion common/src/main/java/bisq/common/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import ch.qos.logback.classic.Level;

import lombok.Getter;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -157,7 +159,8 @@ public class Config {

// Options supported only at the command-line interface (cli)
public final boolean helpRequested;
public final File configFile;
@Getter
private final File configFile;

// Options supported on cmd line and in the config file
public final String appName;
Expand Down
16 changes: 8 additions & 8 deletions common/src/test/java/bisq/common/config/ConfigTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void whenNoOptionsAreSet_thenDataDirPropertiesEqualDefaultValues() {
assertThat(config.appName, equalTo(config.defaultAppName));
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
assertThat(config.appDataDir, equalTo(config.defaultAppDataDir));
assertThat(config.configFile, equalTo(config.defaultConfigFile));
assertThat(config.getConfigFile(), equalTo(config.defaultConfigFile));
}

@Test
Expand All @@ -62,7 +62,7 @@ public void whenAppNameOptionIsSet_thenDataDirPropertiesReflectItsValue() {
assertThat(config.appName, equalTo("My-Bisq"));
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
assertThat(config.appDataDir, equalTo(new File(config.userDataDir, "My-Bisq")));
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
assertThat(config.getConfigFile(), equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
}

@Test
Expand All @@ -72,7 +72,7 @@ public void whenAppDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() thr
assertThat(config.appName, equalTo(config.defaultAppName));
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
assertThat(config.appDataDir, equalTo(appDataDir));
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
assertThat(config.getConfigFile(), equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
}

@Test
Expand All @@ -82,7 +82,7 @@ public void whenUserDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() th
assertThat(config.appName, equalTo(config.defaultAppName));
assertThat(config.userDataDir, equalTo(userDataDir));
assertThat(config.appDataDir, equalTo(new File(userDataDir, config.defaultAppName)));
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
assertThat(config.getConfigFile(), equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
}

@Test
Expand All @@ -92,7 +92,7 @@ public void whenAppNameAndAppDataDirOptionsAreSet_thenDataDirPropertiesReflectTh
assertThat(config.appName, equalTo("My-Bisq"));
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
assertThat(config.appDataDir, equalTo(appDataDir));
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
assertThat(config.getConfigFile(), equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
}

@Test
Expand Down Expand Up @@ -166,7 +166,7 @@ public void whenConfigFileOptionIsSetInConfigFile_thenConfigExceptionIsThrown()
public void whenConfigFileOptionIsSetToExistingFile_thenConfigFilePropertyReflectsItsValue() throws IOException {
File configFile = File.createTempFile("bisq", "properties");
Config config = configWithOpts(opt(CONFIG_FILE, configFile.getAbsolutePath()));
assertThat(config.configFile, equalTo(configFile));
assertThat(config.getConfigFile(), equalTo(configFile));
}

@Test
Expand All @@ -175,7 +175,7 @@ public void whenConfigFileOptionIsSetToRelativePath_thenThePathIsPrefixedByAppDa
File appDataDir = configFile.getParentFile();
String relativeConfigFilePath = configFile.getName();
Config config = configWithOpts(opt(APP_DATA_DIR, appDataDir), opt(CONFIG_FILE, relativeConfigFilePath));
assertThat(config.configFile, equalTo(configFile));
assertThat(config.getConfigFile(), equalTo(configFile));
}

@Test
Expand All @@ -188,7 +188,7 @@ public void whenAppNameIsSetInConfigFile_thenDataDirPropertiesReflectItsValue()
assertThat(config.appName, equalTo("My-Bisq"));
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
assertThat(config.appDataDir, equalTo(new File(config.userDataDir, config.appName)));
assertThat(config.configFile, equalTo(configFile));
assertThat(config.getConfigFile(), equalTo(configFile));
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/filter/FilterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public FilterManager(P2PService p2PService,
this.keyRing = keyRing;
this.user = user;
this.preferences = preferences;
this.configFileEditor = new ConfigFileEditor(config.configFile);
this.configFileEditor = new ConfigFileEditor(config.getConfigFile());
this.providersRepository = providersRepository;
this.ignoreDevMsg = ignoreDevMsg;

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 1f072bf

Please sign in to comment.