mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Remove RemovableDrivePlugin, refactor plugin interface.
This commit is contained in:
@@ -10,20 +10,20 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.bramble.api.reliability.ReliabilityLayerFactory;
|
||||
import org.briarproject.bramble.plugin.bluetooth.JavaBluetoothPluginFactory;
|
||||
import org.briarproject.bramble.plugin.file.RemovableDrivePluginFactory;
|
||||
import org.briarproject.bramble.plugin.modem.ModemPluginFactory;
|
||||
import org.briarproject.bramble.plugin.tcp.LanTcpPluginFactory;
|
||||
import org.briarproject.bramble.plugin.tcp.WanTcpPluginFactory;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyList;
|
||||
|
||||
@Module
|
||||
public class DesktopPluginModule extends PluginModule {
|
||||
|
||||
@@ -41,12 +41,8 @@ public class DesktopPluginModule extends PluginModule {
|
||||
backoffFactory);
|
||||
DuplexPluginFactory wan = new WanTcpPluginFactory(ioExecutor,
|
||||
backoffFactory, shutdownManager);
|
||||
SimplexPluginFactory removable =
|
||||
new RemovableDrivePluginFactory(ioExecutor);
|
||||
Collection<SimplexPluginFactory> simplex =
|
||||
Collections.singletonList(removable);
|
||||
Collection<DuplexPluginFactory> duplex =
|
||||
Arrays.asList(bluetooth, modem, lan, wan);
|
||||
asList(bluetooth, modem, lan, wan);
|
||||
@NotNullByDefault
|
||||
PluginConfig pluginConfig = new PluginConfig() {
|
||||
|
||||
@@ -57,7 +53,7 @@ public class DesktopPluginModule extends PluginModule {
|
||||
|
||||
@Override
|
||||
public Collection<SimplexPluginFactory> getSimplexFactories() {
|
||||
return simplex;
|
||||
return emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
class LinuxRemovableDriveFinder extends UnixRemovableDriveFinder {
|
||||
|
||||
@Override
|
||||
protected String getMountCommand() {
|
||||
return "/bin/mount";
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected String parseMountPoint(String line) {
|
||||
// The format is "/dev/foo on /bar/baz type bam (opt1,opt2)"
|
||||
String pattern = "^/dev/[^ ]+ on (.*) type [^ ]+ \\([^)]+\\)$";
|
||||
String path = line.replaceFirst(pattern, "$1");
|
||||
return path.equals(line) ? null : path;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isRemovableDriveMountPoint(String path) {
|
||||
return path.startsWith("/mnt/") || path.startsWith("/media/");
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
class LinuxRemovableDriveMonitor extends UnixRemovableDriveMonitor {
|
||||
|
||||
@Override
|
||||
protected String[] getPathsToWatch() {
|
||||
return new String[] {"/mnt", "/media"};
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
class MacRemovableDriveFinder extends UnixRemovableDriveFinder {
|
||||
|
||||
@Override
|
||||
protected String getMountCommand() {
|
||||
return "/sbin/mount";
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected String parseMountPoint(String line) {
|
||||
// The format is "/dev/foo on /bar/baz (opt1, opt2)"
|
||||
String pattern = "^/dev/[^ ]+ on (.*) \\([^)]+\\)$";
|
||||
String path = line.replaceFirst(pattern, "$1");
|
||||
return path.equals(line) ? null : path;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isRemovableDriveMountPoint(String path) {
|
||||
return path.startsWith("/Volumes/");
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
class MacRemovableDriveMonitor extends UnixRemovableDriveMonitor {
|
||||
|
||||
@Override
|
||||
protected String[] getPathsToWatch() {
|
||||
return new String[] {"/Volumes"};
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(PollingRemovableDriveMonitor.class.getName());
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final RemovableDriveFinder finder;
|
||||
private final int pollingInterval;
|
||||
|
||||
private final Lock pollingLock = new ReentrantLock();
|
||||
private final Condition stopPolling = pollingLock.newCondition();
|
||||
|
||||
private volatile boolean running = false;
|
||||
private volatile Callback callback = null;
|
||||
|
||||
PollingRemovableDriveMonitor(Executor ioExecutor,
|
||||
RemovableDriveFinder finder, int pollingInterval) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.finder = finder;
|
||||
this.pollingInterval = pollingInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Callback callback) throws IOException {
|
||||
this.callback = callback;
|
||||
running = true;
|
||||
ioExecutor.execute(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws IOException {
|
||||
running = false;
|
||||
pollingLock.lock();
|
||||
try {
|
||||
stopPolling.signalAll();
|
||||
} finally {
|
||||
pollingLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Collection<File> drives = finder.findRemovableDrives();
|
||||
while (running) {
|
||||
pollingLock.lock();
|
||||
try {
|
||||
stopPolling.await(pollingInterval, MILLISECONDS);
|
||||
} finally {
|
||||
pollingLock.unlock();
|
||||
}
|
||||
if (!running) return;
|
||||
Collection<File> newDrives = finder.findRemovableDrives();
|
||||
for (File f : newDrives) {
|
||||
if (!drives.contains(f)) callback.driveInserted(f);
|
||||
}
|
||||
drives = newDrives;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
LOG.warning("Interrupted while waiting to poll");
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IOException e) {
|
||||
callback.exceptionThrown(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
@NotNullByDefault
|
||||
interface RemovableDriveFinder {
|
||||
|
||||
Collection<File> findRemovableDrives() throws IOException;
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@NotNullByDefault
|
||||
interface RemovableDriveMonitor {
|
||||
|
||||
void start(Callback c) throws IOException;
|
||||
|
||||
void stop() throws IOException;
|
||||
|
||||
interface Callback {
|
||||
|
||||
void driveInserted(File root);
|
||||
|
||||
void exceptionThrown(IOException e);
|
||||
}
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.PluginException;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginCallback;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
@NotNullByDefault
|
||||
class RemovableDrivePlugin extends FilePlugin
|
||||
implements RemovableDriveMonitor.Callback {
|
||||
|
||||
static final TransportId ID =
|
||||
new TransportId("org.briarproject.bramble.file");
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(RemovableDrivePlugin.class.getName());
|
||||
|
||||
private final RemovableDriveFinder finder;
|
||||
private final RemovableDriveMonitor monitor;
|
||||
|
||||
RemovableDrivePlugin(Executor ioExecutor, SimplexPluginCallback callback,
|
||||
RemovableDriveFinder finder, RemovableDriveMonitor monitor,
|
||||
int maxLatency) {
|
||||
super(ioExecutor, callback, maxLatency);
|
||||
this.finder = finder;
|
||||
this.monitor = monitor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportId getId() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws PluginException {
|
||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||
running = true;
|
||||
try {
|
||||
monitor.start(this);
|
||||
} catch (IOException e) {
|
||||
throw new PluginException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws PluginException {
|
||||
running = false;
|
||||
try {
|
||||
monitor.stop();
|
||||
} catch (IOException e) {
|
||||
throw new PluginException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldPoll() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPollingInterval() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void poll(Collection<ContactId> connected) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected File chooseOutputDirectory() {
|
||||
try {
|
||||
List<File> drives = new ArrayList<>(finder.findRemovableDrives());
|
||||
if (drives.isEmpty()) return null;
|
||||
String[] paths = new String[drives.size()];
|
||||
for (int i = 0; i < paths.length; i++) {
|
||||
paths[i] = drives.get(i).getPath();
|
||||
}
|
||||
int i = callback.showChoice(paths, "REMOVABLE_DRIVE_CHOOSE_DRIVE");
|
||||
if (i == -1) return null;
|
||||
return drives.get(i);
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void readerFinished(File f) {
|
||||
callback.showMessage("REMOVABLE_DRIVE_READ_FINISHED");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writerFinished(File f) {
|
||||
callback.showMessage("REMOVABLE_DRIVE_WRITE_FINISHED");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<File> findFilesByName(String filename) {
|
||||
List<File> matches = new ArrayList<>();
|
||||
try {
|
||||
for (File drive : finder.findRemovableDrives()) {
|
||||
File[] files = drive.listFiles();
|
||||
if (files != null) {
|
||||
for (File f : files) {
|
||||
if (f.isFile() && filename.equals(f.getName()))
|
||||
matches.add(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void driveInserted(File root) {
|
||||
File[] files = root.listFiles();
|
||||
if (files != null) {
|
||||
for (File f : files) if (f.isFile()) createReaderFromFile(f);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionThrown(IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.bramble.util.OsUtils;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class RemovableDrivePluginFactory implements SimplexPluginFactory {
|
||||
|
||||
// Maximum latency 14 days (Royal Mail or lackadaisical carrier pigeon)
|
||||
private static final int MAX_LATENCY = 14 * 24 * 60 * 60 * 1000;
|
||||
private static final int POLLING_INTERVAL = 10 * 1000; // 10 seconds
|
||||
|
||||
private final Executor ioExecutor;
|
||||
|
||||
public RemovableDrivePluginFactory(Executor ioExecutor) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportId getId() {
|
||||
return RemovableDrivePlugin.ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxLatency() {
|
||||
return MAX_LATENCY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SimplexPlugin createPlugin(SimplexPluginCallback callback) {
|
||||
RemovableDriveFinder finder;
|
||||
RemovableDriveMonitor monitor;
|
||||
if (OsUtils.isLinux()) {
|
||||
finder = new LinuxRemovableDriveFinder();
|
||||
monitor = new LinuxRemovableDriveMonitor();
|
||||
} else if (OsUtils.isMacLeopardOrNewer()) {
|
||||
finder = new MacRemovableDriveFinder();
|
||||
monitor = new MacRemovableDriveMonitor();
|
||||
} else if (OsUtils.isMac()) {
|
||||
// JNotify requires OS X 10.5 or newer, so we have to poll
|
||||
finder = new MacRemovableDriveFinder();
|
||||
monitor = new PollingRemovableDriveMonitor(ioExecutor, finder,
|
||||
POLLING_INTERVAL);
|
||||
} else if (OsUtils.isWindows()) {
|
||||
finder = new WindowsRemovableDriveFinder();
|
||||
monitor = new PollingRemovableDriveMonitor(ioExecutor, finder,
|
||||
POLLING_INTERVAL);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return new RemovableDrivePlugin(ioExecutor, callback, finder, monitor,
|
||||
MAX_LATENCY);
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
abstract class UnixRemovableDriveFinder implements RemovableDriveFinder {
|
||||
|
||||
protected abstract String getMountCommand();
|
||||
|
||||
@Nullable
|
||||
protected abstract String parseMountPoint(String line);
|
||||
|
||||
protected abstract boolean isRemovableDriveMountPoint(String path);
|
||||
|
||||
@Override
|
||||
public List<File> findRemovableDrives() throws IOException {
|
||||
List<File> drives = new ArrayList<>();
|
||||
Process p = new ProcessBuilder(getMountCommand()).start();
|
||||
Scanner s = new Scanner(p.getInputStream(), "UTF-8");
|
||||
try {
|
||||
while (s.hasNextLine()) {
|
||||
String line = s.nextLine();
|
||||
String[] tokens = line.split(" ");
|
||||
if (tokens.length < 3) continue;
|
||||
// The general format is "/dev/foo on /bar/baz ..."
|
||||
if (tokens[0].startsWith("/dev/") && tokens[1].equals("on")) {
|
||||
// The path may contain spaces so we can't use tokens[2]
|
||||
String path = parseMountPoint(line);
|
||||
if (path != null && isRemovableDriveMountPoint(path)) {
|
||||
File f = new File(path);
|
||||
if (f.exists() && f.isDirectory()) drives.add(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
s.close();
|
||||
}
|
||||
return drives;
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import net.contentobjects.jnotify.JNotify;
|
||||
import net.contentobjects.jnotify.JNotifyListener;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
abstract class UnixRemovableDriveMonitor implements RemovableDriveMonitor,
|
||||
JNotifyListener {
|
||||
|
||||
//TODO: rationalise this in a further refactor
|
||||
private static final Lock staticLock = new ReentrantLock();
|
||||
|
||||
// The following are locking: staticLock
|
||||
private static boolean triedLoad = false;
|
||||
private static Throwable loadError = null;
|
||||
|
||||
private final Lock lock = new ReentrantLock();
|
||||
|
||||
// The following are locking: lock
|
||||
private final List<Integer> watches = new ArrayList<>();
|
||||
private boolean started = false;
|
||||
private Callback callback = null;
|
||||
|
||||
protected abstract String[] getPathsToWatch();
|
||||
|
||||
@Nullable
|
||||
private static Throwable tryLoad() {
|
||||
try {
|
||||
Class.forName("net.contentobjects.jnotify.JNotify");
|
||||
return null;
|
||||
} catch (UnsatisfiedLinkError | ClassNotFoundException e) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkEnabled() throws IOException {
|
||||
staticLock.lock();
|
||||
try {
|
||||
if (!triedLoad) {
|
||||
loadError = tryLoad();
|
||||
triedLoad = true;
|
||||
}
|
||||
if (loadError != null) throw new IOException(loadError.toString());
|
||||
} finally {
|
||||
staticLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Callback callback) throws IOException {
|
||||
checkEnabled();
|
||||
List<Integer> watches = new ArrayList<>();
|
||||
int mask = JNotify.FILE_CREATED;
|
||||
for (String path : getPathsToWatch()) {
|
||||
if (new File(path).exists())
|
||||
watches.add(JNotify.addWatch(path, mask, false, this));
|
||||
}
|
||||
lock.lock();
|
||||
try {
|
||||
if (started) throw new AssertionError();
|
||||
if (this.callback != null) throw new AssertionError();
|
||||
started = true;
|
||||
this.callback = callback;
|
||||
this.watches.addAll(watches);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws IOException {
|
||||
checkEnabled();
|
||||
List<Integer> watches;
|
||||
lock.lock();
|
||||
try {
|
||||
if (!started) throw new AssertionError();
|
||||
if (callback == null) throw new AssertionError();
|
||||
started = false;
|
||||
callback = null;
|
||||
watches = new ArrayList<>(this.watches);
|
||||
this.watches.clear();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
for (Integer w : watches) JNotify.removeWatch(w);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fileCreated(int wd, String rootPath, String name) {
|
||||
Callback callback;
|
||||
lock.lock();
|
||||
try {
|
||||
callback = this.callback;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
if (callback != null)
|
||||
callback.driveInserted(new File(rootPath + "/" + name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fileDeleted(int wd, String rootPath, String name) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fileModified(int wd, String rootPath, String name) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fileRenamed(int wd, String rootPath, String oldName,
|
||||
String newName) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import com.sun.jna.platform.win32.Kernel32;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@NotNullByDefault
|
||||
class WindowsRemovableDriveFinder implements RemovableDriveFinder {
|
||||
|
||||
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364939.aspx
|
||||
private static final int DRIVE_REMOVABLE = 2;
|
||||
|
||||
@Override
|
||||
public Collection<File> findRemovableDrives() throws IOException {
|
||||
File[] roots = File.listRoots();
|
||||
if (roots == null) throw new IOException();
|
||||
List<File> drives = new ArrayList<>();
|
||||
for (File root : roots) {
|
||||
try {
|
||||
int type = Kernel32.INSTANCE.GetDriveType(root.getPath());
|
||||
if (type == DRIVE_REMOVABLE) drives.add(root);
|
||||
} catch (RuntimeException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
return drives;
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import org.briarproject.bramble.util.StringUtils;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -115,7 +115,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void poll(Collection<ContactId> connected) {
|
||||
public void poll(Map<ContactId, TransportProperties> contacts) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@@ -139,17 +139,16 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DuplexTransportConnection createConnection(ContactId c) {
|
||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||
if (!running) return null;
|
||||
// Get the ISO 3166 code for the caller's country
|
||||
String fromIso = callback.getLocalProperties().get("iso3166");
|
||||
if (StringUtils.isNullOrEmpty(fromIso)) return null;
|
||||
// Get the ISO 3166 code for the callee's country
|
||||
TransportProperties properties = callback.getRemoteProperties(c);
|
||||
String toIso = properties.get("iso3166");
|
||||
String toIso = p.get("iso3166");
|
||||
if (StringUtils.isNullOrEmpty(toIso)) return null;
|
||||
// Get the callee's phone number
|
||||
String number = properties.get("number");
|
||||
String number = p.get("number");
|
||||
if (StringUtils.isNullOrEmpty(number)) return null;
|
||||
// Convert the number into direct dialling form
|
||||
number = CountryCodes.translate(number, fromIso, toIso);
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.test.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));
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.test.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));
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.plugin.file.RemovableDriveMonitor.Callback;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
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
|
||||
File file1 = new File("foo");
|
||||
File file2 = new File("bar");
|
||||
@NotNullByDefault
|
||||
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
|
||||
CountDownLatch latch = new CountDownLatch(2);
|
||||
List<File> detected = new ArrayList<>();
|
||||
@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
|
||||
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
|
||||
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
|
||||
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
|
||||
RemovableDriveMonitor monitor = new PollingRemovableDriveMonitor(
|
||||
Executors.newCachedThreadPool(), finder, 1);
|
||||
monitor.start(callback);
|
||||
assertTrue(latch.await(10, SECONDS));
|
||||
monitor.stop();
|
||||
}
|
||||
}
|
||||
@@ -1,378 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
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.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.ImmediateExecutor;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
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 {
|
||||
List<File> drives = Collections.emptyList();
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
Executor executor = context.mock(Executor.class);
|
||||
SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
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 {
|
||||
File drive1 = new File(testDir, "1");
|
||||
File drive2 = new File(testDir, "2");
|
||||
List<File> drives = new ArrayList<>();
|
||||
drives.add(drive1);
|
||||
drives.add(drive2);
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
Executor executor = context.mock(Executor.class);
|
||||
SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
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 {
|
||||
File drive1 = new File(testDir, "1");
|
||||
File drive2 = new File(testDir, "2");
|
||||
List<File> drives = new ArrayList<>();
|
||||
drives.add(drive1);
|
||||
drives.add(drive2);
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
Executor executor = context.mock(Executor.class);
|
||||
SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
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 {
|
||||
File drive1 = new File(testDir, "1");
|
||||
File drive2 = new File(testDir, "2");
|
||||
List<File> drives = new ArrayList<>();
|
||||
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());
|
||||
}};
|
||||
Executor executor = context.mock(Executor.class);
|
||||
SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
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 {
|
||||
File drive1 = new File(testDir, "1");
|
||||
File drive2 = new File(testDir, "2");
|
||||
List<File> drives = new ArrayList<>();
|
||||
drives.add(drive1);
|
||||
drives.add(drive2);
|
||||
// Create drive1 as a directory
|
||||
assertTrue(drive1.mkdir());
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
Executor executor = context.mock(Executor.class);
|
||||
SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
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 {
|
||||
File drive1 = new File(testDir, "1");
|
||||
File drive2 = new File(testDir, "2");
|
||||
List<File> drives = new ArrayList<>();
|
||||
drives.add(drive1);
|
||||
drives.add(drive2);
|
||||
// Create drive1 as a directory
|
||||
assertTrue(drive1.mkdir());
|
||||
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
Executor executor = context.mock(Executor.class);
|
||||
SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
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());
|
||||
}};
|
||||
Executor executor = context.mock(Executor.class);
|
||||
SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
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());
|
||||
}};
|
||||
Executor executor = context.mock(Executor.class);
|
||||
SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
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());
|
||||
}};
|
||||
SimplexPluginCallback callback =
|
||||
context.mock(SimplexPluginCallback.class);
|
||||
RemovableDriveFinder finder =
|
||||
context.mock(RemovableDriveFinder.class);
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package org.briarproject.bramble.plugin.file;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.plugin.file.RemovableDriveMonitor.Callback;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
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
|
||||
List<File> detected = new ArrayList<>();
|
||||
CountDownLatch latch = new CountDownLatch(2);
|
||||
@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
|
||||
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(File dir) {
|
||||
@NotNullByDefault
|
||||
RemovableDriveMonitor monitor = new UnixRemovableDriveMonitor() {
|
||||
@Override
|
||||
protected String[] getPathsToWatch() {
|
||||
return new String[] {dir.getPath()};
|
||||
}
|
||||
};
|
||||
return monitor;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.bramble.plugin.modem;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
@@ -66,7 +65,6 @@ public class ModemPluginTest extends BrambleTestCase {
|
||||
TransportProperties remote = new TransportProperties();
|
||||
remote.put("iso3166", ISO_1336);
|
||||
remote.put("number", NUMBER);
|
||||
ContactId contactId = new ContactId(234);
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
@@ -78,14 +76,12 @@ public class ModemPluginTest extends BrambleTestCase {
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
oneOf(callback).getRemoteProperties(contactId);
|
||||
will(returnValue(remote));
|
||||
oneOf(modem).dial(NUMBER);
|
||||
will(returnValue(true));
|
||||
}});
|
||||
plugin.start();
|
||||
// A connection should be returned
|
||||
assertNotNull(plugin.createConnection(contactId));
|
||||
assertNotNull(plugin.createConnection(remote));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -105,7 +101,6 @@ public class ModemPluginTest extends BrambleTestCase {
|
||||
TransportProperties remote = new TransportProperties();
|
||||
remote.put("iso3166", ISO_1336);
|
||||
remote.put("number", NUMBER);
|
||||
ContactId contactId = new ContactId(234);
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
@@ -117,14 +112,12 @@ public class ModemPluginTest extends BrambleTestCase {
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
oneOf(callback).getRemoteProperties(contactId);
|
||||
will(returnValue(remote));
|
||||
oneOf(modem).dial(NUMBER);
|
||||
will(returnValue(false));
|
||||
}});
|
||||
plugin.start();
|
||||
// No connection should be returned
|
||||
assertNull(plugin.createConnection(contactId));
|
||||
assertNull(plugin.createConnection(remote));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -144,7 +137,6 @@ public class ModemPluginTest extends BrambleTestCase {
|
||||
TransportProperties remote = new TransportProperties();
|
||||
remote.put("iso3166", ISO_1336);
|
||||
remote.put("number", NUMBER);
|
||||
ContactId contactId = new ContactId(234);
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(serialPortList).getPortNames();
|
||||
@@ -156,8 +148,6 @@ public class ModemPluginTest extends BrambleTestCase {
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
oneOf(callback).getRemoteProperties(contactId);
|
||||
will(returnValue(remote));
|
||||
oneOf(modem).dial(NUMBER);
|
||||
will(throwException(new IOException()));
|
||||
// resetModem()
|
||||
@@ -170,7 +160,7 @@ public class ModemPluginTest extends BrambleTestCase {
|
||||
}});
|
||||
plugin.start();
|
||||
// No connection should be returned
|
||||
assertNull(plugin.createConnection(contactId));
|
||||
assertNull(plugin.createConnection(remote));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user