diff --git a/.run/BridgeTest.run.xml b/.run/BridgeTest.run.xml new file mode 100644 index 0000000000000000000000000000000000000000..5baa242d1575ce9ff6d88e83586bd11aead074c8 --- /dev/null +++ b/.run/BridgeTest.run.xml @@ -0,0 +1,31 @@ +<component name="ProjectRunConfigurationManager"> + <configuration default="false" name="BridgeTest" type="GradleRunConfiguration" + factoryName="Gradle"> + <ExternalSystemSettings> + <option name="env"> + <map> + <entry key="OPTIONAL_TESTS" value="org.briarproject.onionwrapper.BridgeTest" /> + </map> + </option> + <option name="executionName" /> + <option name="externalProjectPath" value="$PROJECT_DIR$" /> + <option name="externalSystemIdString" value="GRADLE" /> + <option name="scriptParameters" value="" /> + <option name="taskDescriptions"> + <list /> + </option> + <option name="taskNames"> + <list> + <option value=":onionwrapper-java:test" /> + <option value="--tests" /> + <option value=""org.briarproject.onionwrapper.BridgeTest"" /> + </list> + </option> + <option name="vmOptions" /> + </ExternalSystemSettings> + <ExternalSystemDebugServerProcess>false</ExternalSystemDebugServerProcess> + <ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess> + <DebugAllEnabled>false</DebugAllEnabled> + <method v="2" /> + </configuration> +</component> \ No newline at end of file diff --git a/onionwrapper-core/src/test/java/org/briarproject/onionwrapper/TestUtils.java b/onionwrapper-core/src/test/java/org/briarproject/onionwrapper/TestUtils.java index 028916edd4bf8af6da7e65bc477d24e027d9b53a..d7b8afa57cdc85d007dbc284295c0bf48d194cff 100644 --- a/onionwrapper-core/src/test/java/org/briarproject/onionwrapper/TestUtils.java +++ b/onionwrapper-core/src/test/java/org/briarproject/onionwrapper/TestUtils.java @@ -9,6 +9,7 @@ import java.util.logging.Logger; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; +import static java.util.Arrays.asList; import static java.util.logging.Level.WARNING; import static java.util.logging.Logger.getLogger; @@ -67,4 +68,10 @@ public class TestUtils { else if (arch.equals("arm")) return "armhf"; return null; } + + public static boolean isOptionalTestEnabled(Class<?> testClass) { + String optionalTests = System.getenv("OPTIONAL_TESTS"); + return optionalTests != null && + asList(optionalTests.split(",")).contains(testClass.getName()); + } } diff --git a/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BootstrapTest.java b/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BootstrapTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6681b05abfe62b7e232cc248ba4725bf4f2fa6dd --- /dev/null +++ b/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BootstrapTest.java @@ -0,0 +1,71 @@ +package org.briarproject.onionwrapper; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.util.concurrent.ExecutorService; +import java.util.logging.Logger; + +import static java.util.concurrent.Executors.newCachedThreadPool; +import static java.util.concurrent.TimeUnit.MINUTES; +import static java.util.logging.Logger.getLogger; +import static org.briarproject.nullsafety.NullSafety.requireNonNull; +import static org.briarproject.onionwrapper.TestUtils.deleteTestDirectory; +import static org.briarproject.onionwrapper.TestUtils.getArchitectureForTorBinary; +import static org.briarproject.onionwrapper.TestUtils.getTestDirectory; +import static org.briarproject.onionwrapper.TestUtils.isLinux; +import static org.briarproject.onionwrapper.TorWrapper.TorState.CONNECTED; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeNotNull; +import static org.junit.Assume.assumeTrue; + +public class BootstrapTest extends BaseTest { + + private final static Logger LOG = getLogger(BootstrapTest.class.getName()); + + private static final int SOCKS_PORT = 59060; + private static final int CONTROL_PORT = 59061; + private final static long TIMEOUT = MINUTES.toMillis(2); + + private final ExecutorService executor = newCachedThreadPool(); + private final File torDir = getTestDirectory(); + + @Before + public void setUp() { + assumeTrue(isLinux()); + assumeNotNull(getArchitectureForTorBinary()); + } + + @After + public void tearDown() { + deleteTestDirectory(torDir); + executor.shutdown(); + } + + @Test + public void testBootstrapping() throws Exception { + String architecture = requireNonNull(getArchitectureForTorBinary()); + TorWrapper tor = new UnixTorWrapper(executor, executor, architecture, torDir, + CONTROL_PORT, SOCKS_PORT); + + boolean connected; + try { + tor.start(); + tor.enableNetwork(true); + long start = System.currentTimeMillis(); + while (System.currentTimeMillis() - start < TIMEOUT) { + if (tor.getTorState() == CONNECTED) break; + //noinspection BusyWait + Thread.sleep(500); + } + connected = tor.getTorState() == CONNECTED; + if (connected) LOG.info("Connected to Tor"); + else LOG.warning("Could not connect to Tor within timeout"); + } finally { + tor.stop(); + } + assertTrue(connected); + } +} diff --git a/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BridgeTest.java b/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BridgeTest.java index d49f4ff541f0bb1cfd09b61b9433c1280c2e45b4..1c822f53e62ae7853ae2860b682deaaa03c99000 100644 --- a/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BridgeTest.java +++ b/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BridgeTest.java @@ -33,6 +33,7 @@ import static org.briarproject.onionwrapper.TestUtils.deleteTestDirectory; import static org.briarproject.onionwrapper.TestUtils.getArchitectureForTorBinary; import static org.briarproject.onionwrapper.TestUtils.getTestDirectory; import static org.briarproject.onionwrapper.TestUtils.isLinux; +import static org.briarproject.onionwrapper.TestUtils.isOptionalTestEnabled; import static org.briarproject.onionwrapper.TorWrapper.TorState.CONNECTED; import static org.junit.Assert.fail; import static org.junit.Assume.assumeNotNull; @@ -92,6 +93,7 @@ public class BridgeTest extends BaseTest { @Before public void setUp() { + assumeTrue(isOptionalTestEnabled(BridgeTest.class)); assumeTrue(isLinux()); assumeNotNull(getArchitectureForTorBinary()); }