mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Move all unit tests to their modules and remove briar-tests
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package org.briarproject.bramble.lifecycle;
|
||||
|
||||
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class WindowsShutdownManagerImplTest extends ShutdownManagerImplTest {
|
||||
|
||||
@Override
|
||||
protected ShutdownManager createShutdownManager() {
|
||||
return new WindowsShutdownManagerImpl();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManagerWaitsForHooksToRun() {
|
||||
WindowsShutdownManagerImpl s = new WindowsShutdownManagerImpl();
|
||||
SlowHook[] hooks = new SlowHook[10];
|
||||
for (int i = 0; i < hooks.length; i++) {
|
||||
hooks[i] = new SlowHook();
|
||||
s.addShutdownHook(hooks[i]);
|
||||
}
|
||||
s.runShutdownHooks();
|
||||
for (SlowHook hook : hooks) assertTrue(hook.finished);
|
||||
}
|
||||
|
||||
private static class SlowHook implements Runnable {
|
||||
|
||||
private volatile boolean finished = false;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
finished = true;
|
||||
} catch (InterruptedException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.BrambleTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class LinuxRemovableDriveFinderTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testParseMountPoint() {
|
||||
LinuxRemovableDriveFinder f = new LinuxRemovableDriveFinder();
|
||||
String line = "/dev/sda3 on / type ext3"
|
||||
+ " (rw,errors=remount-ro,commit=0)";
|
||||
assertEquals("/", f.parseMountPoint(line));
|
||||
line = "gvfs-fuse-daemon on /home/alice/.gvfs"
|
||||
+ " type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=alice)";
|
||||
assertEquals(null, f.parseMountPoint(line)); // Can't be parsed
|
||||
line = "fusectl on /sys/fs/fuse/connections type fusectl (rw)";
|
||||
assertEquals(null, f.parseMountPoint(line)); // Can't be parsed
|
||||
line = "/dev/sdd1 on /media/HAZ SPACE(!) type vfat"
|
||||
+ " (rw,nosuid,nodev,uhelper=udisks,uid=1000,gid=1000,"
|
||||
+ "shortname=mixed,dmask=0077,utf8=1,showexec,flush)";
|
||||
assertEquals("/media/HAZ SPACE(!)", f.parseMountPoint(line));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.BrambleTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MacRemovableDriveFinderTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testParseMountPoint() {
|
||||
MacRemovableDriveFinder f = new MacRemovableDriveFinder();
|
||||
String line = "/dev/disk0s3 on / (local, journaled)";
|
||||
assertEquals("/", f.parseMountPoint(line));
|
||||
line = "devfs on /dev (local)";
|
||||
assertEquals(null, f.parseMountPoint(line)); // Can't be parsed
|
||||
line = "<volfs> on /.vol";
|
||||
assertEquals(null, f.parseMountPoint(line)); // Can't be parsed
|
||||
line = "automount -nsl [117] on /Network (automounted)";
|
||||
assertEquals(null, f.parseMountPoint(line)); // Can't be parsed
|
||||
line = "/dev/disk1s1 on /Volumes/HAZ SPACE(!) (local, nodev, nosuid)";
|
||||
assertEquals("/Volumes/HAZ SPACE(!)", f.parseMountPoint(line));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.BrambleTestCase;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.plugin.file.RemovableDriveMonitor.Callback;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class PollingRemovableDriveMonitorTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testOneCallbackPerFile() throws Exception {
|
||||
// Create a finder that returns no files the first time, then two files
|
||||
final File file1 = new File("foo");
|
||||
final File file2 = new File("bar");
|
||||
@NotNullByDefault
|
||||
final RemovableDriveFinder finder = new RemovableDriveFinder() {
|
||||
|
||||
private AtomicBoolean firstCall = new AtomicBoolean(true);
|
||||
|
||||
@Override
|
||||
public Collection<File> findRemovableDrives() throws IOException {
|
||||
if (firstCall.getAndSet(false)) return Collections.emptyList();
|
||||
else return Arrays.asList(file1, file2);
|
||||
}
|
||||
};
|
||||
// Create a callback that waits for two files
|
||||
final CountDownLatch latch = new CountDownLatch(2);
|
||||
final List<File> detected = new ArrayList<File>();
|
||||
@NotNullByDefault
|
||||
Callback callback = new Callback() {
|
||||
|
||||
@Override
|
||||
public void driveInserted(File f) {
|
||||
detected.add(f);
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionThrown(IOException e) {
|
||||
fail();
|
||||
}
|
||||
};
|
||||
// Create the monitor and start it
|
||||
final RemovableDriveMonitor monitor = new PollingRemovableDriveMonitor(
|
||||
Executors.newCachedThreadPool(), finder, 1);
|
||||
monitor.start(callback);
|
||||
// Wait for the monitor to detect the files
|
||||
assertTrue(latch.await(10, SECONDS));
|
||||
monitor.stop();
|
||||
// Check that both files were detected
|
||||
assertEquals(2, detected.size());
|
||||
assertTrue(detected.contains(file1));
|
||||
assertTrue(detected.contains(file2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExceptionCallback() throws Exception {
|
||||
// Create a finder that throws an exception the second time it's polled
|
||||
final RemovableDriveFinder finder = new RemovableDriveFinder() {
|
||||
|
||||
private AtomicBoolean firstCall = new AtomicBoolean(true);
|
||||
|
||||
@Override
|
||||
public Collection<File> findRemovableDrives() throws IOException {
|
||||
if (firstCall.getAndSet(false)) return Collections.emptyList();
|
||||
else throw new IOException();
|
||||
}
|
||||
};
|
||||
// Create a callback that waits for an exception
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
@NotNullByDefault
|
||||
Callback callback = new Callback() {
|
||||
|
||||
@Override
|
||||
public void driveInserted(File root) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionThrown(IOException e) {
|
||||
latch.countDown();
|
||||
}
|
||||
};
|
||||
// Create the monitor and start it
|
||||
final RemovableDriveMonitor monitor = new PollingRemovableDriveMonitor(
|
||||
Executors.newCachedThreadPool(), finder, 1);
|
||||
monitor.start(callback);
|
||||
assertTrue(latch.await(10, SECONDS));
|
||||
monitor.stop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.BrambleTestCase;
|
||||
import org.briarproject.bramble.ImmediateExecutor;
|
||||
import org.briarproject.bramble.TestUtils;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginCallback;
|
||||
import org.briarproject.bramble.plugin.file.RemovableDriveMonitor.Callback;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.Mockery;
|
||||
import org.jmock.lib.concurrent.Synchroniser;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.MIN_STREAM_LENGTH;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class RemovableDrivePluginTest extends BrambleTestCase {
|
||||
|
||||
private final File testDir = TestUtils.getTestDirectory();
|
||||
private final ContactId contactId = new ContactId(234);
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
testDir.mkdirs();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriterIsNullIfNoDrivesAreFound() throws Exception {
|
||||
final List<File> drives = Collections.emptyList();
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
final Executor executor = context.mock(Executor.class);
|
||||
final SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
final RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
final RemovableDriveMonitor monitor =
|
||||
context.mock(RemovableDriveMonitor.class);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(monitor).start(with(any(Callback.class)));
|
||||
oneOf(finder).findRemovableDrives();
|
||||
will(returnValue(drives));
|
||||
}});
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor, 0);
|
||||
plugin.start();
|
||||
|
||||
assertNull(plugin.createWriter(contactId));
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriterIsNullIfNoDriveIsChosen() throws Exception {
|
||||
final File drive1 = new File(testDir, "1");
|
||||
final File drive2 = new File(testDir, "2");
|
||||
final List<File> drives = new ArrayList<File>();
|
||||
drives.add(drive1);
|
||||
drives.add(drive2);
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
final Executor executor = context.mock(Executor.class);
|
||||
final SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
final RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
final RemovableDriveMonitor monitor =
|
||||
context.mock(RemovableDriveMonitor.class);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(monitor).start(with(any(Callback.class)));
|
||||
oneOf(finder).findRemovableDrives();
|
||||
will(returnValue(drives));
|
||||
oneOf(callback).showChoice(with(any(String[].class)),
|
||||
with(any(String[].class)));
|
||||
will(returnValue(-1)); // The user cancelled the choice
|
||||
}});
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor, 0);
|
||||
plugin.start();
|
||||
|
||||
assertNull(plugin.createWriter(contactId));
|
||||
File[] files = drive1.listFiles();
|
||||
assertTrue(files == null || files.length == 0);
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriterIsNullIfOutputDirDoesNotExist() throws Exception {
|
||||
final File drive1 = new File(testDir, "1");
|
||||
final File drive2 = new File(testDir, "2");
|
||||
final List<File> drives = new ArrayList<File>();
|
||||
drives.add(drive1);
|
||||
drives.add(drive2);
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
final Executor executor = context.mock(Executor.class);
|
||||
final SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
final RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
final RemovableDriveMonitor monitor =
|
||||
context.mock(RemovableDriveMonitor.class);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(monitor).start(with(any(Callback.class)));
|
||||
oneOf(finder).findRemovableDrives();
|
||||
will(returnValue(drives));
|
||||
oneOf(callback).showChoice(with(any(String[].class)),
|
||||
with(any(String[].class)));
|
||||
will(returnValue(0)); // The user chose drive1 but it doesn't exist
|
||||
}});
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor, 0);
|
||||
plugin.start();
|
||||
|
||||
assertNull(plugin.createWriter(contactId));
|
||||
File[] files = drive1.listFiles();
|
||||
assertTrue(files == null || files.length == 0);
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriterIsNullIfOutputDirIsAFile() throws Exception {
|
||||
final File drive1 = new File(testDir, "1");
|
||||
final File drive2 = new File(testDir, "2");
|
||||
final List<File> drives = new ArrayList<File>();
|
||||
drives.add(drive1);
|
||||
drives.add(drive2);
|
||||
// Create drive1 as a file rather than a directory
|
||||
assertTrue(drive1.createNewFile());
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
final Executor executor = context.mock(Executor.class);
|
||||
final SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
final RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
final RemovableDriveMonitor monitor =
|
||||
context.mock(RemovableDriveMonitor.class);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(monitor).start(with(any(Callback.class)));
|
||||
oneOf(finder).findRemovableDrives();
|
||||
will(returnValue(drives));
|
||||
oneOf(callback).showChoice(with(any(String[].class)),
|
||||
with(any(String[].class)));
|
||||
will(returnValue(0)); // The user chose drive1 but it's not a dir
|
||||
}});
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor, 0);
|
||||
plugin.start();
|
||||
|
||||
assertNull(plugin.createWriter(contactId));
|
||||
File[] files = drive1.listFiles();
|
||||
assertTrue(files == null || files.length == 0);
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriterIsNotNullIfOutputDirIsADir() throws Exception {
|
||||
final File drive1 = new File(testDir, "1");
|
||||
final File drive2 = new File(testDir, "2");
|
||||
final List<File> drives = new ArrayList<File>();
|
||||
drives.add(drive1);
|
||||
drives.add(drive2);
|
||||
// Create drive1 as a directory
|
||||
assertTrue(drive1.mkdir());
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
final Executor executor = context.mock(Executor.class);
|
||||
final SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
final RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
final RemovableDriveMonitor monitor =
|
||||
context.mock(RemovableDriveMonitor.class);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(monitor).start(with(any(Callback.class)));
|
||||
oneOf(finder).findRemovableDrives();
|
||||
will(returnValue(drives));
|
||||
oneOf(callback).showChoice(with(any(String[].class)),
|
||||
with(any(String[].class)));
|
||||
will(returnValue(0)); // The user chose drive1
|
||||
}});
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor, 0);
|
||||
plugin.start();
|
||||
|
||||
assertNotNull(plugin.createWriter(contactId));
|
||||
// The output file should exist and should be empty
|
||||
File[] files = drive1.listFiles();
|
||||
assertNotNull(files);
|
||||
assertEquals(1, files.length);
|
||||
assertEquals(0, files[0].length());
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWritingToWriter() throws Exception {
|
||||
final File drive1 = new File(testDir, "1");
|
||||
final File drive2 = new File(testDir, "2");
|
||||
final List<File> drives = new ArrayList<File>();
|
||||
drives.add(drive1);
|
||||
drives.add(drive2);
|
||||
// Create drive1 as a directory
|
||||
assertTrue(drive1.mkdir());
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
final Executor executor = context.mock(Executor.class);
|
||||
final SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
final RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
final RemovableDriveMonitor monitor =
|
||||
context.mock(RemovableDriveMonitor.class);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(monitor).start(with(any(Callback.class)));
|
||||
oneOf(finder).findRemovableDrives();
|
||||
will(returnValue(drives));
|
||||
oneOf(callback).showChoice(with(any(String[].class)),
|
||||
with(any(String[].class)));
|
||||
will(returnValue(0)); // The user chose drive1
|
||||
oneOf(callback).showMessage(with(any(String[].class)));
|
||||
}});
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor, 0);
|
||||
plugin.start();
|
||||
|
||||
TransportConnectionWriter writer = plugin.createWriter(contactId);
|
||||
assertNotNull(writer);
|
||||
// The output file should exist and should be empty
|
||||
File[] files = drive1.listFiles();
|
||||
assertNotNull(files);
|
||||
assertEquals(1, files.length);
|
||||
assertEquals(0, files[0].length());
|
||||
// Writing to the output stream should increase the size of the file
|
||||
OutputStream out = writer.getOutputStream();
|
||||
out.write(new byte[1234]);
|
||||
out.flush();
|
||||
out.close();
|
||||
// Disposing of the writer should not delete the file
|
||||
writer.dispose(false);
|
||||
assertTrue(files[0].exists());
|
||||
assertEquals(1234, files[0].length());
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyDriveIsIgnored() throws Exception {
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
final Executor executor = context.mock(Executor.class);
|
||||
final SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
final RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
final RemovableDriveMonitor monitor =
|
||||
context.mock(RemovableDriveMonitor.class);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(monitor).start(with(any(Callback.class)));
|
||||
}});
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor, 0);
|
||||
plugin.start();
|
||||
|
||||
plugin.driveInserted(testDir);
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilenames() {
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
final Executor executor = context.mock(Executor.class);
|
||||
final SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
final RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
final RemovableDriveMonitor monitor =
|
||||
context.mock(RemovableDriveMonitor.class);
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor, 0);
|
||||
|
||||
assertFalse(plugin.isPossibleConnectionFilename("abcdefg.dat"));
|
||||
assertFalse(plugin.isPossibleConnectionFilename("abcdefghi.dat"));
|
||||
assertFalse(plugin.isPossibleConnectionFilename("abcdefgh_dat"));
|
||||
assertFalse(plugin.isPossibleConnectionFilename("abcdefgh.rat"));
|
||||
assertTrue(plugin.isPossibleConnectionFilename("abcdefgh.dat"));
|
||||
assertTrue(plugin.isPossibleConnectionFilename("ABCDEFGH.DAT"));
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReaderIsCreated() throws Exception {
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
final SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
final RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
final RemovableDriveMonitor monitor =
|
||||
context.mock(RemovableDriveMonitor.class);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(monitor).start(with(any(Callback.class)));
|
||||
oneOf(callback).readerCreated(with(any(FileTransportReader.class)));
|
||||
}});
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
|
||||
new ImmediateExecutor(), callback, finder, monitor, 0);
|
||||
plugin.start();
|
||||
|
||||
File f = new File(testDir, "abcdefgh.dat");
|
||||
OutputStream out = new FileOutputStream(f);
|
||||
out.write(new byte[MIN_STREAM_LENGTH]);
|
||||
out.flush();
|
||||
out.close();
|
||||
assertEquals(MIN_STREAM_LENGTH, f.length());
|
||||
plugin.driveInserted(testDir);
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
TestUtils.deleteTestDirectory(testDir);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.BrambleTestCase;
|
||||
import org.briarproject.bramble.TestUtils;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.plugin.file.RemovableDriveMonitor.Callback;
|
||||
import org.briarproject.bramble.util.OsUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class UnixRemovableDriveMonitorTest extends BrambleTestCase {
|
||||
|
||||
private final File testDir = TestUtils.getTestDirectory();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
testDir.mkdirs();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonexistentDir() throws Exception {
|
||||
if (!(OsUtils.isLinux() || OsUtils.isMacLeopardOrNewer())) {
|
||||
System.err.println("WARNING: Skipping test, can't run on this OS");
|
||||
return;
|
||||
}
|
||||
File doesNotExist = new File(testDir, "doesNotExist");
|
||||
RemovableDriveMonitor monitor = createMonitor(doesNotExist);
|
||||
@NotNullByDefault
|
||||
Callback callback = new Callback() {
|
||||
|
||||
@Override
|
||||
public void driveInserted(File root) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionThrown(IOException e) {
|
||||
fail();
|
||||
}
|
||||
};
|
||||
monitor.start(callback);
|
||||
monitor.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneCallbackPerFile() throws Exception {
|
||||
if (!(OsUtils.isLinux() || OsUtils.isMacLeopardOrNewer())) {
|
||||
System.err.println("WARNING: Skipping test, can't run on this OS");
|
||||
return;
|
||||
}
|
||||
// Create a callback that will wait for two files before stopping
|
||||
final List<File> detected = new ArrayList<File>();
|
||||
final CountDownLatch latch = new CountDownLatch(2);
|
||||
@NotNullByDefault
|
||||
final Callback callback = new Callback() {
|
||||
|
||||
@Override
|
||||
public void driveInserted(File f) {
|
||||
detected.add(f);
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionThrown(IOException e) {
|
||||
fail();
|
||||
}
|
||||
};
|
||||
// Create the monitor and start it
|
||||
RemovableDriveMonitor monitor = createMonitor(testDir);
|
||||
monitor.start(callback);
|
||||
// Create two files in the test directory
|
||||
File file1 = new File(testDir, "1");
|
||||
File file2 = new File(testDir, "2");
|
||||
assertTrue(file1.createNewFile());
|
||||
assertTrue(file2.createNewFile());
|
||||
// Wait for the monitor to detect the files
|
||||
assertTrue(latch.await(5, SECONDS));
|
||||
monitor.stop();
|
||||
// Check that both files were detected
|
||||
assertEquals(2, detected.size());
|
||||
assertTrue(detected.contains(file1));
|
||||
assertTrue(detected.contains(file2));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
TestUtils.deleteTestDirectory(testDir);
|
||||
}
|
||||
|
||||
private RemovableDriveMonitor createMonitor(final File dir) {
|
||||
@NotNullByDefault
|
||||
RemovableDriveMonitor monitor = new UnixRemovableDriveMonitor() {
|
||||
@Override
|
||||
protected String[] getPathsToWatch() {
|
||||
return new String[] {dir.getPath()};
|
||||
}
|
||||
};
|
||||
return monitor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.briarproject.bramble.plugin.modem;
|
||||
|
||||
import org.briarproject.bramble.BrambleTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class CountryCodesTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testTranslation() {
|
||||
// Unrecognised country for caller
|
||||
assertNull(CountryCodes.translate("02012345678", "ZZ", "GB"));
|
||||
// Unrecognised country for callee
|
||||
assertNull(CountryCodes.translate("02012345678", "GB", "ZZ"));
|
||||
|
||||
// GB to GB, callee has not included a prefix
|
||||
assertEquals("02012345678",
|
||||
CountryCodes.translate("2012345678", "GB", "GB"));
|
||||
// GB to GB, callee has included NDD prefix
|
||||
assertEquals("02012345678",
|
||||
CountryCodes.translate("02012345678", "GB", "GB"));
|
||||
// GB to GB, callee has included plus sign and country code
|
||||
assertEquals("02012345678",
|
||||
CountryCodes.translate("+442012345678", "GB", "GB"));
|
||||
// GB to GB, callee has included IDD prefix and country code
|
||||
assertEquals("02012345678",
|
||||
CountryCodes.translate("00442012345678", "GB", "GB"));
|
||||
|
||||
// Russia to GB, callee has not included a prefix
|
||||
assertEquals("8**10442012345678",
|
||||
CountryCodes.translate("2012345678", "RU", "GB"));
|
||||
// Russia to GB, callee has included NDD prefix
|
||||
assertEquals("8**10442012345678",
|
||||
CountryCodes.translate("02012345678", "RU", "GB"));
|
||||
// Russia to GB, callee has included plus sign and country code
|
||||
assertEquals("8**10442012345678",
|
||||
CountryCodes.translate("+442012345678", "RU", "GB"));
|
||||
// Russia to GB, callee has included IDD prefix and country code
|
||||
assertEquals("8**10442012345678",
|
||||
CountryCodes.translate("00442012345678", "RU", "GB"));
|
||||
|
||||
// Andorra to Andorra (no NDD), callee has not included a prefix
|
||||
assertEquals("765432", CountryCodes.translate("765432", "AD", "AD"));
|
||||
// Andorra to Andorra, callee has included plus sign and country code
|
||||
assertEquals("765432",
|
||||
CountryCodes.translate("+376765432", "AD", "AD"));
|
||||
// Andorra to Andorra, callee has included IDD and country code
|
||||
assertEquals("765432",
|
||||
CountryCodes.translate("00376765432", "AD", "AD"));
|
||||
|
||||
// GB to Andorra (no NDD), callee has not included a prefix
|
||||
assertEquals("00376765432",
|
||||
CountryCodes.translate("765432", "GB", "AD"));
|
||||
// GB to Andorra, callee has included plus sign and country code
|
||||
assertEquals("00376765432",
|
||||
CountryCodes.translate("+376765432", "GB", "AD"));
|
||||
// GB to Andorra, callee has included IDD and country code
|
||||
assertEquals("00376765432",
|
||||
CountryCodes.translate("00376765432", "GB", "AD"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package org.briarproject.bramble.plugin.modem;
|
||||
|
||||
import org.briarproject.bramble.BrambleTestCase;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.Mockery;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ModemPluginTest extends BrambleTestCase {
|
||||
|
||||
private static final String ISO_1336 = "GB";
|
||||
private static final String NUMBER = "0123456789";
|
||||
|
||||
@Test
|
||||
public void testModemCreation() throws Exception {
|
||||
Mockery context = new Mockery();
|
||||
final ModemFactory modemFactory = context.mock(ModemFactory.class);
|
||||
final SerialPortList serialPortList =
|
||||
context.mock(SerialPortList.class);
|
||||
final ModemPlugin plugin = new ModemPlugin(modemFactory,
|
||||
serialPortList, null, 0);
|
||||
final Modem modem = context.mock(Modem.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] { "foo", "bar", "baz" }));
|
||||
// First call to createModem() returns false
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(false));
|
||||
// Second call to createModem() throws an exception
|
||||
oneOf(modemFactory).createModem(plugin, "bar");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(throwException(new IOException()));
|
||||
// Third call to createModem() returns true
|
||||
oneOf(modemFactory).createModem(plugin, "baz");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
}});
|
||||
assertTrue(plugin.start());
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateConnection() throws Exception {
|
||||
Mockery context = new Mockery();
|
||||
final ModemFactory modemFactory = context.mock(ModemFactory.class);
|
||||
final SerialPortList serialPortList =
|
||||
context.mock(SerialPortList.class);
|
||||
final DuplexPluginCallback callback =
|
||||
context.mock(DuplexPluginCallback.class);
|
||||
final ModemPlugin plugin = new ModemPlugin(modemFactory,
|
||||
serialPortList, callback, 0);
|
||||
final Modem modem = context.mock(Modem.class);
|
||||
final TransportProperties local = new TransportProperties();
|
||||
local.put("iso3166", ISO_1336);
|
||||
TransportProperties p = new TransportProperties();
|
||||
p.put("iso3166", ISO_1336);
|
||||
p.put("number", NUMBER);
|
||||
ContactId contactId = new ContactId(234);
|
||||
final Map<ContactId, TransportProperties> remote =
|
||||
Collections.singletonMap(contactId, p);
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] { "foo" }));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
oneOf(callback).getRemoteProperties();
|
||||
will(returnValue(remote));
|
||||
oneOf(modem).dial(NUMBER);
|
||||
will(returnValue(true));
|
||||
}});
|
||||
assertTrue(plugin.start());
|
||||
// A connection should be returned
|
||||
assertNotNull(plugin.createConnection(contactId));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateConnectionWhenDialReturnsFalse() throws Exception {
|
||||
Mockery context = new Mockery();
|
||||
final ModemFactory modemFactory = context.mock(ModemFactory.class);
|
||||
final SerialPortList serialPortList =
|
||||
context.mock(SerialPortList.class);
|
||||
final DuplexPluginCallback callback =
|
||||
context.mock(DuplexPluginCallback.class);
|
||||
final ModemPlugin plugin = new ModemPlugin(modemFactory,
|
||||
serialPortList, callback, 0);
|
||||
final Modem modem = context.mock(Modem.class);
|
||||
final TransportProperties local = new TransportProperties();
|
||||
local.put("iso3166", ISO_1336);
|
||||
TransportProperties p = new TransportProperties();
|
||||
p.put("iso3166", ISO_1336);
|
||||
p.put("number", NUMBER);
|
||||
ContactId contactId = new ContactId(234);
|
||||
final Map<ContactId, TransportProperties> remote =
|
||||
Collections.singletonMap(contactId, p);
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] { "foo" }));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
oneOf(callback).getRemoteProperties();
|
||||
will(returnValue(remote));
|
||||
oneOf(modem).dial(NUMBER);
|
||||
will(returnValue(false));
|
||||
}});
|
||||
assertTrue(plugin.start());
|
||||
// No connection should be returned
|
||||
assertNull(plugin.createConnection(contactId));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateConnectionWhenDialThrowsException() throws Exception {
|
||||
Mockery context = new Mockery();
|
||||
final ModemFactory modemFactory = context.mock(ModemFactory.class);
|
||||
final SerialPortList serialPortList =
|
||||
context.mock(SerialPortList.class);
|
||||
final DuplexPluginCallback callback =
|
||||
context.mock(DuplexPluginCallback.class);
|
||||
final ModemPlugin plugin = new ModemPlugin(modemFactory,
|
||||
serialPortList, callback, 0);
|
||||
final Modem modem = context.mock(Modem.class);
|
||||
final TransportProperties local = new TransportProperties();
|
||||
local.put("iso3166", ISO_1336);
|
||||
TransportProperties p = new TransportProperties();
|
||||
p.put("iso3166", ISO_1336);
|
||||
p.put("number", NUMBER);
|
||||
ContactId contactId = new ContactId(234);
|
||||
final Map<ContactId, TransportProperties> remote =
|
||||
Collections.singletonMap(contactId, p);
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] { "foo" }));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
oneOf(callback).getRemoteProperties();
|
||||
will(returnValue(remote));
|
||||
oneOf(modem).dial(NUMBER);
|
||||
will(throwException(new IOException()));
|
||||
// resetModem()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] { "foo" }));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
}});
|
||||
assertTrue(plugin.start());
|
||||
// No connection should be returned
|
||||
assertNull(plugin.createConnection(contactId));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user