From 4b0cf771d3dae9520df7b7d412f7be2346aa07ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=BCrten?= <sebastian@mobanisto.de> Date: Thu, 4 Nov 2021 16:38:48 +0100 Subject: [PATCH] Make multi-account tests customizable Also, add another test that automatically connects both started app instances. --- ...estWithTwoConnectedTemporaryAccountsKt.xml | 10 +++ .../TestWithTwoTemporaryAccountsKt.xml | 10 +++ .../briar/desktop/BriarDesktopTestApp.kt | 3 + .../RunWithMultipleTemporaryAccounts.kt | 87 +++++++++++++++++++ .../TestWithTwoConnectedTemporaryAccounts.kt | 11 +++ .../desktop/TestWithTwoTemporaryAccounts.kt | 85 +----------------- 6 files changed, 125 insertions(+), 81 deletions(-) create mode 100644 .idea/runConfigurations/TestWithTwoConnectedTemporaryAccountsKt.xml create mode 100644 .idea/runConfigurations/TestWithTwoTemporaryAccountsKt.xml create mode 100644 src/test/kotlin/org/briarproject/briar/desktop/RunWithMultipleTemporaryAccounts.kt create mode 100644 src/test/kotlin/org/briarproject/briar/desktop/TestWithTwoConnectedTemporaryAccounts.kt diff --git a/.idea/runConfigurations/TestWithTwoConnectedTemporaryAccountsKt.xml b/.idea/runConfigurations/TestWithTwoConnectedTemporaryAccountsKt.xml new file mode 100644 index 0000000000..7e3e061296 --- /dev/null +++ b/.idea/runConfigurations/TestWithTwoConnectedTemporaryAccountsKt.xml @@ -0,0 +1,10 @@ +<component name="ProjectRunConfigurationManager"> + <configuration default="false" name="TestWithTwoConnectedTemporaryAccountsKt" type="JetRunConfigurationType" nameIsGenerated="true"> + <option name="MAIN_CLASS_NAME" value="org.briarproject.briar.desktop.TestWithTwoConnectedTemporaryAccountsKt" /> + <module name="briar-desktop.test" /> + <shortenClasspath name="NONE" /> + <method v="2"> + <option name="Make" enabled="true" /> + </method> + </configuration> +</component> \ No newline at end of file diff --git a/.idea/runConfigurations/TestWithTwoTemporaryAccountsKt.xml b/.idea/runConfigurations/TestWithTwoTemporaryAccountsKt.xml new file mode 100644 index 0000000000..297d2ae45b --- /dev/null +++ b/.idea/runConfigurations/TestWithTwoTemporaryAccountsKt.xml @@ -0,0 +1,10 @@ +<component name="ProjectRunConfigurationManager"> + <configuration default="false" name="TestWithTwoTemporaryAccountsKt" type="JetRunConfigurationType" nameIsGenerated="true"> + <option name="MAIN_CLASS_NAME" value="org.briarproject.briar.desktop.TestWithTwoTemporaryAccountsKt" /> + <module name="briar-desktop.test" /> + <shortenClasspath name="NONE" /> + <method v="2"> + <option name="Make" enabled="true" /> + </method> + </configuration> +</component> \ No newline at end of file diff --git a/src/test/kotlin/org/briarproject/briar/desktop/BriarDesktopTestApp.kt b/src/test/kotlin/org/briarproject/briar/desktop/BriarDesktopTestApp.kt index 04210dd0cc..775f69eaed 100644 --- a/src/test/kotlin/org/briarproject/briar/desktop/BriarDesktopTestApp.kt +++ b/src/test/kotlin/org/briarproject/briar/desktop/BriarDesktopTestApp.kt @@ -4,6 +4,7 @@ import dagger.Component import org.briarproject.bramble.BrambleCoreEagerSingletons import org.briarproject.bramble.BrambleCoreModule import org.briarproject.bramble.api.account.AccountManager +import org.briarproject.bramble.api.contact.ContactManager import org.briarproject.bramble.api.lifecycle.LifecycleManager import org.briarproject.bramble.api.lifecycle.ShutdownManager import org.briarproject.briar.BriarCoreEagerSingletons @@ -26,6 +27,8 @@ internal interface BriarDesktopTestApp : BrambleCoreEagerSingletons, BriarCoreEa fun getBriarUi(): BriarUi + fun getContactManager(): ContactManager + fun getSecureRandom(): SecureRandom fun getLifecycleManager(): LifecycleManager diff --git a/src/test/kotlin/org/briarproject/briar/desktop/RunWithMultipleTemporaryAccounts.kt b/src/test/kotlin/org/briarproject/briar/desktop/RunWithMultipleTemporaryAccounts.kt new file mode 100644 index 0000000000..2747c4277f --- /dev/null +++ b/src/test/kotlin/org/briarproject/briar/desktop/RunWithMultipleTemporaryAccounts.kt @@ -0,0 +1,87 @@ +package org.briarproject.briar.desktop + +import androidx.compose.runtime.Composable +import androidx.compose.ui.ExperimentalComposeUiApi +import androidx.compose.ui.window.ApplicationScope +import androidx.compose.ui.window.application +import org.briarproject.bramble.BrambleCoreEagerSingletons +import org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_CONTROL_PORT +import org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_SOCKS_PORT +import org.briarproject.briar.BriarCoreEagerSingletons +import org.briarproject.briar.desktop.TestUtils.getDataDir +import java.util.logging.Level.INFO +import java.util.logging.LogManager +import java.util.logging.Logger + +internal class RunWithMultipleTemporaryAccounts( + private val names: List<String>, + val customization: List<BriarDesktopTestApp>.() -> Unit +) { + + companion object { + private val LOG = Logger.getLogger(RunWithMultipleTemporaryAccounts::class.java.name) + } + + private val apps = mutableListOf<BriarDesktopTestApp>() + + @OptIn(ExperimentalComposeUiApi::class) + fun run() { + LogManager.getLogManager().getLogger("").level = INFO + + for (i in names.indices) { + val name = names[i] + val app = app(name, DEFAULT_SOCKS_PORT + 2 * i, DEFAULT_CONTROL_PORT + 2 * i) + apps.add(app) + } + + customization(apps) + + application { + for (app in apps) { + start(app, this) + } + } + } + + private fun app(name: String, socksPort: Int, controlPort: Int): BriarDesktopTestApp { + val dataDir = getDataDir() + LOG.info("Using data directory '$dataDir'") + + val app = + DaggerBriarDesktopTestApp.builder().desktopTestModule( + DesktopTestModule(dataDir, socksPort, controlPort) + ).build() + + app.getShutdownManager().addShutdownHook { + LOG.info("deleting temporary account at $dataDir") + org.apache.commons.io.FileUtils.deleteDirectory(dataDir.toFile()) + } + + // We need to load the eager singletons directly after making the + // dependency graphs + BrambleCoreEagerSingletons.Helper.injectEagerSingletons(app) + BriarCoreEagerSingletons.Helper.injectEagerSingletons(app) + + val lifecycleManager = app.getLifecycleManager() + val accountManager = app.getAccountManager() + + val password = "verySecret123!" + accountManager.createAccount(name, password) + + val dbKey = accountManager.databaseKey ?: throw AssertionError() + lifecycleManager.startServices(dbKey) + lifecycleManager.waitForStartup() + + return app + } + + @Composable + fun start(app: BriarDesktopTestApp, applicationScope: ApplicationScope) { + app.getBriarUi().start { + apps.forEach { + it.getBriarUi().stop() + } + applicationScope.exitApplication() + } + } +} diff --git a/src/test/kotlin/org/briarproject/briar/desktop/TestWithTwoConnectedTemporaryAccounts.kt b/src/test/kotlin/org/briarproject/briar/desktop/TestWithTwoConnectedTemporaryAccounts.kt new file mode 100644 index 0000000000..9c34fbbca3 --- /dev/null +++ b/src/test/kotlin/org/briarproject/briar/desktop/TestWithTwoConnectedTemporaryAccounts.kt @@ -0,0 +1,11 @@ +package org.briarproject.briar.desktop + +fun main(args: Array<String>) = RunWithMultipleTemporaryAccounts(listOf("alice", "bob")) { + forEach { + it.getDeterministicTestDataCreator().createTestData(5, 20, 50) + } + val app1 = get(0) + val app2 = get(1) + app1.getContactManager().addPendingContact(app2.getContactManager().handshakeLink, "bob") + app2.getContactManager().addPendingContact(app1.getContactManager().handshakeLink, "alice") +}.run() diff --git a/src/test/kotlin/org/briarproject/briar/desktop/TestWithTwoTemporaryAccounts.kt b/src/test/kotlin/org/briarproject/briar/desktop/TestWithTwoTemporaryAccounts.kt index 72c6db6c96..2cb86294a4 100644 --- a/src/test/kotlin/org/briarproject/briar/desktop/TestWithTwoTemporaryAccounts.kt +++ b/src/test/kotlin/org/briarproject/briar/desktop/TestWithTwoTemporaryAccounts.kt @@ -1,84 +1,7 @@ package org.briarproject.briar.desktop -import androidx.compose.runtime.Composable -import androidx.compose.ui.ExperimentalComposeUiApi -import androidx.compose.ui.window.ApplicationScope -import androidx.compose.ui.window.application -import org.briarproject.bramble.BrambleCoreEagerSingletons -import org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_CONTROL_PORT -import org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_SOCKS_PORT -import org.briarproject.briar.BriarCoreEagerSingletons -import org.briarproject.briar.desktop.TestUtils.getDataDir -import java.util.logging.Level.INFO -import java.util.logging.LogManager -import java.util.logging.Logger - -fun main(args: Array<String>) = TestWithTwoTemporaryAccounts().run() - -internal class TestWithTwoTemporaryAccounts() { - - companion object { - private val LOG = Logger.getLogger(TestWithTwoTemporaryAccounts::class.java.name) - } - - private val apps = mutableListOf<BriarDesktopTestApp>() - - @OptIn(ExperimentalComposeUiApi::class) - fun run() { - LogManager.getLogManager().getLogger("").level = INFO - - val app1 = app("alice", DEFAULT_SOCKS_PORT, DEFAULT_CONTROL_PORT) - val app2 = app("bob", DEFAULT_SOCKS_PORT + 2, DEFAULT_CONTROL_PORT + 2) - apps.add(app1) - apps.add(app2) - app1.getDeterministicTestDataCreator().createTestData(5, 20, 50) - app2.getDeterministicTestDataCreator().createTestData(5, 20, 50) - - application { - start(app1, this) - start(app2, this) - } - } - - private fun app(name: String, socksPort: Int, controlPort: Int): BriarDesktopTestApp { - val dataDir = getDataDir() - LOG.info("Using data directory '$dataDir'") - - val app = - DaggerBriarDesktopTestApp.builder().desktopTestModule( - DesktopTestModule(dataDir, socksPort, controlPort) - ).build() - - app.getShutdownManager().addShutdownHook { - LOG.info("deleting temporary account at $dataDir") - org.apache.commons.io.FileUtils.deleteDirectory(dataDir.toFile()) - } - - // We need to load the eager singletons directly after making the - // dependency graphs - BrambleCoreEagerSingletons.Helper.injectEagerSingletons(app) - BriarCoreEagerSingletons.Helper.injectEagerSingletons(app) - - val lifecycleManager = app.getLifecycleManager() - val accountManager = app.getAccountManager() - - val password = "verySecret123!" - accountManager.createAccount(name, password) - - val dbKey = accountManager.databaseKey ?: throw AssertionError() - lifecycleManager.startServices(dbKey) - lifecycleManager.waitForStartup() - - return app - } - - @Composable - fun start(app: BriarDesktopTestApp, applicationScope: ApplicationScope) { - app.getBriarUi().start { - apps.forEach { - it.getBriarUi().stop() - } - applicationScope.exitApplication() - } +fun main(args: Array<String>) = RunWithMultipleTemporaryAccounts(listOf("alice", "bob")) { + forEach { + it.getDeterministicTestDataCreator().createTestData(5, 20, 50) } -} +}.run() -- GitLab