mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Reorganised Guice modules. Contribute entropy to pool on Linux/Android.
This commit is contained in:
@@ -9,9 +9,10 @@
|
||||
<item>org.briarproject.messaging.MessagingModule</item>
|
||||
<item>org.briarproject.messaging.duplex.DuplexMessagingModule</item>
|
||||
<item>org.briarproject.messaging.simplex.SimplexMessagingModule</item>
|
||||
<item>org.briarproject.plugins.AndroidPluginsModule</item>
|
||||
<item>org.briarproject.plugins.PluginsModule</item>
|
||||
<item>org.briarproject.serial.SerialModule</item>
|
||||
<item>org.briarproject.system.SystemModule</item>
|
||||
<item>org.briarproject.system.AndroidSystemModule</item>
|
||||
<item>org.briarproject.transport.TransportModule</item>
|
||||
</string-array>
|
||||
</resources>
|
||||
@@ -4,9 +4,6 @@ import static android.content.Context.MODE_PRIVATE;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -19,23 +16,11 @@ import javax.inject.Singleton;
|
||||
import org.briarproject.api.android.AndroidExecutor;
|
||||
import org.briarproject.api.android.DatabaseUiExecutor;
|
||||
import org.briarproject.api.android.ReferenceManager;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.db.DatabaseConfig;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.lifecycle.ShutdownManager;
|
||||
import org.briarproject.api.plugins.PluginExecutor;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginConfig;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginConfig;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.api.system.FileUtils;
|
||||
import org.briarproject.api.ui.UiCallback;
|
||||
import org.briarproject.plugins.droidtooth.DroidtoothPluginFactory;
|
||||
import org.briarproject.plugins.tcp.DroidLanTcpPluginFactory;
|
||||
import org.briarproject.plugins.tcp.WanTcpPluginFactory;
|
||||
import org.briarproject.plugins.tor.TorPluginFactory;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
@@ -75,7 +60,6 @@ public class AndroidModule extends AbstractModule {
|
||||
bind(AndroidExecutor.class).to(AndroidExecutorImpl.class);
|
||||
bind(ReferenceManager.class).to(
|
||||
ReferenceManagerImpl.class).in(Singleton.class);
|
||||
bind(FileUtils.class).to(AndroidFileUtils.class);
|
||||
bind(UiCallback.class).toInstance(uiCallback);
|
||||
}
|
||||
|
||||
@@ -85,38 +69,6 @@ public class AndroidModule extends AbstractModule {
|
||||
return databaseUiExecutor;
|
||||
}
|
||||
|
||||
@Provides
|
||||
SimplexPluginConfig getSimplexPluginConfig() {
|
||||
return new SimplexPluginConfig() {
|
||||
public Collection<SimplexPluginFactory> getFactories() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Provides
|
||||
DuplexPluginConfig getDuplexPluginConfig(
|
||||
@PluginExecutor Executor pluginExecutor,
|
||||
AndroidExecutor androidExecutor, Context appContext,
|
||||
CryptoComponent crypto, ShutdownManager shutdownManager) {
|
||||
DuplexPluginFactory droidtooth = new DroidtoothPluginFactory(
|
||||
pluginExecutor, androidExecutor, appContext,
|
||||
crypto.getSecureRandom());
|
||||
DuplexPluginFactory tor = new TorPluginFactory(pluginExecutor,
|
||||
appContext, shutdownManager);
|
||||
DuplexPluginFactory lan = new DroidLanTcpPluginFactory(pluginExecutor,
|
||||
appContext);
|
||||
DuplexPluginFactory wan = new WanTcpPluginFactory(pluginExecutor,
|
||||
shutdownManager);
|
||||
final Collection<DuplexPluginFactory> factories =
|
||||
Arrays.asList(droidtooth, tor, lan, wan);
|
||||
return new DuplexPluginConfig() {
|
||||
public Collection<DuplexPluginFactory> getFactories() {
|
||||
return factories;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Provides @Singleton
|
||||
DatabaseConfig getDatabaseConfig(final Application app) {
|
||||
final File dir = app.getApplicationContext().getDir("db", MODE_PRIVATE);
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.briarproject.plugins;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import org.briarproject.api.android.AndroidExecutor;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.lifecycle.ShutdownManager;
|
||||
import org.briarproject.api.plugins.PluginExecutor;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginConfig;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginConfig;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.plugins.droidtooth.DroidtoothPluginFactory;
|
||||
import org.briarproject.plugins.tcp.DroidLanTcpPluginFactory;
|
||||
import org.briarproject.plugins.tcp.WanTcpPluginFactory;
|
||||
import org.briarproject.plugins.tor.TorPluginFactory;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
|
||||
public class AndroidPluginsModule extends AbstractModule {
|
||||
|
||||
protected void configure() {}
|
||||
|
||||
@Provides
|
||||
SimplexPluginConfig getSimplexPluginConfig() {
|
||||
return new SimplexPluginConfig() {
|
||||
public Collection<SimplexPluginFactory> getFactories() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Provides
|
||||
DuplexPluginConfig getDuplexPluginConfig(
|
||||
@PluginExecutor Executor pluginExecutor,
|
||||
AndroidExecutor androidExecutor, Context appContext,
|
||||
CryptoComponent crypto, ShutdownManager shutdownManager) {
|
||||
DuplexPluginFactory droidtooth = new DroidtoothPluginFactory(
|
||||
pluginExecutor, androidExecutor, appContext,
|
||||
crypto.getSecureRandom());
|
||||
DuplexPluginFactory tor = new TorPluginFactory(pluginExecutor,
|
||||
appContext, shutdownManager);
|
||||
DuplexPluginFactory lan = new DroidLanTcpPluginFactory(pluginExecutor,
|
||||
appContext);
|
||||
DuplexPluginFactory wan = new WanTcpPluginFactory(pluginExecutor,
|
||||
shutdownManager);
|
||||
final Collection<DuplexPluginFactory> factories =
|
||||
Arrays.asList(droidtooth, tor, lan, wan);
|
||||
return new DuplexPluginConfig() {
|
||||
public Collection<DuplexPluginFactory> getFactories() {
|
||||
return factories;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,8 @@ import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class DroidtoothPluginFactory implements DuplexPluginFactory {
|
||||
|
||||
@@ -7,7 +7,8 @@ import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class DroidLanTcpPluginFactory implements DuplexPluginFactory {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.briarproject.android;
|
||||
package org.briarproject.system;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.briarproject.system;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import android.os.Build;
|
||||
|
||||
class AndroidSeedProvider extends LinuxSeedProvider {
|
||||
|
||||
@Override
|
||||
void writeToEntropyPool(DataOutputStream out) throws IOException {
|
||||
out.writeInt(android.os.Process.myPid());
|
||||
out.writeInt(android.os.Process.myTid());
|
||||
out.writeInt(android.os.Process.myUid());
|
||||
String fingerprint = Build.FINGERPRINT;
|
||||
if(fingerprint != null) out.writeUTF(fingerprint);
|
||||
if(Build.VERSION.SDK_INT >= 9) {
|
||||
String serial = Build.SERIAL;
|
||||
if(serial != null) out.writeUTF(serial);
|
||||
}
|
||||
super.writeToEntropyPool(out);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,18 @@
|
||||
package org.briarproject.system;
|
||||
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.api.system.SystemTimer;
|
||||
import org.briarproject.api.system.FileUtils;
|
||||
import org.briarproject.api.system.SeedProvider;
|
||||
import org.briarproject.api.system.Timer;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
public class SystemModule extends AbstractModule {
|
||||
public class AndroidSystemModule extends AbstractModule {
|
||||
|
||||
protected void configure() {
|
||||
bind(Clock.class).to(SystemClock.class);
|
||||
bind(Timer.class).to(SystemTimer.class);
|
||||
bind(SeedProvider.class).to(AndroidSeedProvider.class);
|
||||
bind(FileUtils.class).to(AndroidFileUtils.class);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.briarproject.api.crypto;
|
||||
package org.briarproject.api.system;
|
||||
|
||||
/**
|
||||
* Uses a platform-specific source to provide a seed for a pseudo-random
|
||||
@@ -30,8 +30,8 @@ import org.briarproject.api.crypto.PrivateKey;
|
||||
import org.briarproject.api.crypto.PseudoRandom;
|
||||
import org.briarproject.api.crypto.PublicKey;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.crypto.SeedProvider;
|
||||
import org.briarproject.api.crypto.Signature;
|
||||
import org.briarproject.api.system.SeedProvider;
|
||||
import org.briarproject.util.ByteUtils;
|
||||
import org.spongycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.spongycastle.crypto.BlockCipher;
|
||||
|
||||
@@ -14,9 +14,7 @@ import javax.inject.Singleton;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.api.crypto.PasswordStrengthEstimator;
|
||||
import org.briarproject.api.crypto.SeedProvider;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.util.OsUtils;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
@@ -41,9 +39,6 @@ public class CryptoModule extends AbstractModule {
|
||||
}
|
||||
|
||||
protected void configure() {
|
||||
if(OsUtils.isAndroid() || OsUtils.isLinux()) {
|
||||
bind(SeedProvider.class).to(LinuxSeedProvider.class);
|
||||
}
|
||||
bind(CryptoComponent.class).to(
|
||||
CryptoComponentImpl.class).in(Singleton.class);
|
||||
bind(PasswordStrengthEstimator.class).to(
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package org.briarproject.crypto;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.briarproject.api.crypto.SeedProvider;
|
||||
|
||||
class LinuxSeedProvider implements SeedProvider {
|
||||
|
||||
public byte[] getSeed() {
|
||||
byte[] seed = new byte[SEED_BYTES];
|
||||
try {
|
||||
DataInputStream in = new DataInputStream(
|
||||
new FileInputStream("/dev/urandom"));
|
||||
in.readFully(seed);
|
||||
in.close();
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return seed;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.lifecycle.ShutdownManager;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.FileUtils;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
public class LanTcpPluginFactory implements DuplexPluginFactory {
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.briarproject.api.reliability.ReliabilityLayer;
|
||||
import org.briarproject.api.reliability.ReliabilityLayerFactory;
|
||||
import org.briarproject.api.reliability.WriteHandler;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
class ReliabilityLayerFactoryImpl implements ReliabilityLayerFactory {
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.briarproject.system;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.system.SeedProvider;
|
||||
|
||||
class LinuxSeedProvider implements SeedProvider {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(LinuxSeedProvider.class.getName());
|
||||
|
||||
private final String outputFile, inputFile;
|
||||
|
||||
LinuxSeedProvider() {
|
||||
this("/dev/urandom", "/dev/urandom");
|
||||
}
|
||||
|
||||
LinuxSeedProvider(String outputFile, String inputFile) {
|
||||
this.outputFile = outputFile;
|
||||
this.inputFile = inputFile;
|
||||
}
|
||||
|
||||
public byte[] getSeed() {
|
||||
byte[] seed = new byte[SEED_BYTES];
|
||||
// Contribute whatever slightly unpredictable info we have to the pool
|
||||
try {
|
||||
DataOutputStream out = new DataOutputStream(
|
||||
new FileOutputStream(outputFile));
|
||||
writeToEntropyPool(out);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch(IOException e) {
|
||||
// On some devices /dev/urandom isn't writable - this isn't fatal
|
||||
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
// Read the seed from the pool
|
||||
try {
|
||||
DataInputStream in = new DataInputStream(
|
||||
new FileInputStream(inputFile));
|
||||
in.readFully(seed);
|
||||
in.close();
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return seed;
|
||||
}
|
||||
|
||||
void writeToEntropyPool(DataOutputStream out) throws IOException {
|
||||
out.writeLong(System.currentTimeMillis());
|
||||
out.writeLong(System.nanoTime());
|
||||
List<NetworkInterface> ifaces =
|
||||
Collections.list(NetworkInterface.getNetworkInterfaces());
|
||||
for(NetworkInterface i : ifaces) {
|
||||
List<InetAddress> addrs = Collections.list(i.getInetAddresses());
|
||||
for(InetAddress a : addrs) out.write(a.getAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
package org.briarproject.api.system;
|
||||
package org.briarproject.system;
|
||||
|
||||
import org.briarproject.api.system.Clock;
|
||||
|
||||
/** Default clock implementation. */
|
||||
public class SystemClock implements Clock {
|
||||
@@ -1,7 +1,9 @@
|
||||
package org.briarproject.api.system;
|
||||
package org.briarproject.system;
|
||||
|
||||
import java.util.TimerTask;
|
||||
|
||||
import org.briarproject.api.system.Timer;
|
||||
|
||||
/** Default timer implementation. */
|
||||
public class SystemTimer implements Timer {
|
||||
|
||||
@@ -8,7 +8,7 @@ import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
public class BluetoothPluginFactory implements DuplexPluginFactory {
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.util.concurrent.Executor;
|
||||
|
||||
import org.briarproject.api.reliability.ReliabilityLayerFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
class ModemFactoryImpl implements ModemFactory {
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package org.briarproject.system;
|
||||
|
||||
import org.briarproject.api.system.FileUtils;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
public class DesktopOsModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(FileUtils.class).to(FileUtilsImpl.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.briarproject.system;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.FileUtils;
|
||||
import org.briarproject.api.system.SeedProvider;
|
||||
import org.briarproject.api.system.Timer;
|
||||
import org.briarproject.util.OsUtils;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
public class DesktopSystemModule extends AbstractModule {
|
||||
|
||||
protected void configure() {
|
||||
bind(Clock.class).to(SystemClock.class);
|
||||
bind(Timer.class).to(SystemTimer.class);
|
||||
if(OsUtils.isLinux())
|
||||
bind(SeedProvider.class).to(LinuxSeedProvider.class);
|
||||
bind(FileUtils.class).toInstance(new FileUtils() {
|
||||
public long getFreeSpace(File f) throws IOException {
|
||||
return f.getFreeSpace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package org.briarproject.system;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.briarproject.api.system.FileUtils;
|
||||
|
||||
class FileUtilsImpl implements FileUtils {
|
||||
|
||||
public long getFreeSpace(File f) throws IOException {
|
||||
return f.getFreeSpace();
|
||||
}
|
||||
}
|
||||
@@ -123,6 +123,7 @@
|
||||
<test name='org.briarproject.plugins.tcp.LanTcpPluginTest'/>
|
||||
<test name='org.briarproject.serial.ReaderImplTest'/>
|
||||
<test name='org.briarproject.serial.WriterImplTest'/>
|
||||
<test name='org.briarproject.system.LinuxSeedProviderTest'/>
|
||||
<test name='org.briarproject.transport.ConnectionReaderImplTest'/>
|
||||
<test name='org.briarproject.transport.ConnectionRegistryImplTest'/>
|
||||
<test name='org.briarproject.transport.ConnectionWindowTest'/>
|
||||
|
||||
@@ -48,9 +48,7 @@ import org.briarproject.messaging.duplex.DuplexMessagingModule;
|
||||
import org.briarproject.messaging.simplex.SimplexMessagingModule;
|
||||
import org.briarproject.reliability.ReliabilityModule;
|
||||
import org.briarproject.serial.SerialModule;
|
||||
import org.briarproject.system.SystemModule;
|
||||
import org.briarproject.transport.TransportModule;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
@@ -79,8 +77,8 @@ public class ProtocolIntegrationTest extends BriarTestCase {
|
||||
|
||||
public ProtocolIntegrationTest() throws Exception {
|
||||
Injector i = Guice.createInjector(new TestDatabaseModule(),
|
||||
new TestLifecycleModule(), new TestUiModule(),
|
||||
new SystemModule(), new CryptoModule(), new DatabaseModule(),
|
||||
new TestLifecycleModule(), new TestSystemModule(),
|
||||
new TestUiModule(), new CryptoModule(), new DatabaseModule(),
|
||||
new MessagingModule(), new DuplexMessagingModule(),
|
||||
new SimplexMessagingModule(), new ReliabilityModule(),
|
||||
new SerialModule(), new TransportModule());
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.briarproject;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.briarproject.api.crypto.SeedProvider;
|
||||
import org.briarproject.api.system.SeedProvider;
|
||||
|
||||
public class TestSeedProvider implements SeedProvider {
|
||||
|
||||
|
||||
18
briar-tests/src/org/briarproject/TestSystemModule.java
Normal file
18
briar-tests/src/org/briarproject/TestSystemModule.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.briarproject;
|
||||
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SeedProvider;
|
||||
import org.briarproject.api.system.Timer;
|
||||
import org.briarproject.system.SystemClock;
|
||||
import org.briarproject.system.SystemTimer;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
public class TestSystemModule extends AbstractModule {
|
||||
|
||||
protected void configure() {
|
||||
bind(Clock.class).to(SystemClock.class);
|
||||
bind(Timer.class).to(SystemTimer.class);
|
||||
bind(SeedProvider.class).to(TestSeedProvider.class);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,12 @@
|
||||
package org.briarproject;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.briarproject.api.UniqueId;
|
||||
|
||||
public class TestUtils {
|
||||
@@ -42,38 +39,12 @@ public class TestUtils {
|
||||
testDir.getParentFile().delete(); // Delete if empty
|
||||
}
|
||||
|
||||
public static File getBuildDirectory() {
|
||||
File build = new File("build"); // Ant
|
||||
if(build.exists() && build.isDirectory()) return build;
|
||||
File bin = new File("bin"); // Eclipse
|
||||
if(bin.exists() && bin.isDirectory()) return bin;
|
||||
throw new RuntimeException("Could not find build directory");
|
||||
}
|
||||
|
||||
public static File getFontDirectory() {
|
||||
File f = new File("i18n");
|
||||
if(f.exists() && f.isDirectory()) return f;
|
||||
f = new File("../i18n");
|
||||
if(f.exists() && f.isDirectory()) return f;
|
||||
throw new RuntimeException("Could not find font directory");
|
||||
}
|
||||
|
||||
public static byte[] getRandomId() {
|
||||
byte[] b = new byte[UniqueId.LENGTH];
|
||||
random.nextBytes(b);
|
||||
return b;
|
||||
}
|
||||
|
||||
public static void readFully(InputStream in, byte[] b) throws IOException {
|
||||
int offset = 0;
|
||||
while(offset < b.length) {
|
||||
int read = in.read(b, offset, b.length - offset);
|
||||
if(read == -1) break;
|
||||
offset += read;
|
||||
}
|
||||
TestCase.assertEquals(b.length, offset);
|
||||
}
|
||||
|
||||
public static String createRandomString(int length) throws Exception {
|
||||
StringBuilder s = new StringBuilder(length);
|
||||
for(int i = 0; i < length; i++)
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.TestSeedProvider;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyPair;
|
||||
import org.briarproject.api.crypto.SeedProvider;
|
||||
import org.briarproject.api.system.SeedProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
public class KeyAgreementTest extends BriarTestCase {
|
||||
|
||||
@@ -6,9 +6,9 @@ import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.system.SystemTimer;
|
||||
import org.briarproject.api.system.Timer;
|
||||
import org.briarproject.db.DatabaseCleaner.Callback;
|
||||
import org.briarproject.system.SystemTimer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ import java.util.Collections;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.lifecycle.ShutdownManager;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.db.DatabaseCleaner.Callback;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.Mockery;
|
||||
|
||||
@@ -38,9 +38,9 @@ import org.briarproject.api.messaging.GroupId;
|
||||
import org.briarproject.api.messaging.GroupStatus;
|
||||
import org.briarproject.api.messaging.Message;
|
||||
import org.briarproject.api.messaging.MessageId;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.api.transport.Endpoint;
|
||||
import org.briarproject.api.transport.TemporarySecret;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.util.Random;
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.TestDatabaseModule;
|
||||
import org.briarproject.TestLifecycleModule;
|
||||
import org.briarproject.TestSystemModule;
|
||||
import org.briarproject.TestUtils;
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorFactory;
|
||||
@@ -45,9 +46,7 @@ import org.briarproject.db.DatabaseModule;
|
||||
import org.briarproject.messaging.duplex.DuplexMessagingModule;
|
||||
import org.briarproject.messaging.simplex.SimplexMessagingModule;
|
||||
import org.briarproject.serial.SerialModule;
|
||||
import org.briarproject.system.SystemModule;
|
||||
import org.briarproject.transport.TransportModule;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
@@ -63,7 +62,7 @@ public class ConstantsTest extends BriarTestCase {
|
||||
|
||||
public ConstantsTest() throws Exception {
|
||||
Injector i = Guice.createInjector(new TestDatabaseModule(),
|
||||
new TestLifecycleModule(), new SystemModule(),
|
||||
new TestLifecycleModule(), new TestSystemModule(),
|
||||
new CryptoModule(), new DatabaseModule(), new MessagingModule(),
|
||||
new DuplexMessagingModule(), new SimplexMessagingModule(),
|
||||
new SerialModule(), new TransportModule());
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.util.concurrent.Executors;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.TestLifecycleModule;
|
||||
import org.briarproject.TestSystemModule;
|
||||
import org.briarproject.TestUtils;
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.TransportId;
|
||||
@@ -30,9 +31,7 @@ import org.briarproject.crypto.CryptoModule;
|
||||
import org.briarproject.messaging.MessagingModule;
|
||||
import org.briarproject.messaging.duplex.DuplexMessagingModule;
|
||||
import org.briarproject.serial.SerialModule;
|
||||
import org.briarproject.system.SystemModule;
|
||||
import org.briarproject.transport.TransportModule;
|
||||
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.Mockery;
|
||||
import org.junit.Test;
|
||||
@@ -68,7 +67,7 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
|
||||
}
|
||||
};
|
||||
Injector i = Guice.createInjector(testModule,
|
||||
new TestLifecycleModule(), new SystemModule(),
|
||||
new TestLifecycleModule(), new TestSystemModule(),
|
||||
new CryptoModule(), new MessagingModule(),
|
||||
new DuplexMessagingModule(), new SimplexMessagingModule(),
|
||||
new SerialModule(), new TransportModule());
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.util.Random;
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.TestDatabaseModule;
|
||||
import org.briarproject.TestLifecycleModule;
|
||||
import org.briarproject.TestSystemModule;
|
||||
import org.briarproject.TestUtils;
|
||||
import org.briarproject.api.Author;
|
||||
import org.briarproject.api.AuthorId;
|
||||
@@ -42,9 +43,7 @@ import org.briarproject.messaging.MessagingModule;
|
||||
import org.briarproject.messaging.duplex.DuplexMessagingModule;
|
||||
import org.briarproject.plugins.ImmediateExecutor;
|
||||
import org.briarproject.serial.SerialModule;
|
||||
import org.briarproject.system.SystemModule;
|
||||
import org.briarproject.transport.TransportModule;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -87,7 +86,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
|
||||
private Injector createInjector(File dir) {
|
||||
return Guice.createInjector(new TestDatabaseModule(dir),
|
||||
new TestLifecycleModule(), new SystemModule(),
|
||||
new TestLifecycleModule(), new TestSystemModule(),
|
||||
new CryptoModule(), new DatabaseModule(), new MessagingModule(),
|
||||
new DuplexMessagingModule(), new SimplexMessagingModule(),
|
||||
new SerialModule(), new TransportModule());
|
||||
|
||||
@@ -17,9 +17,9 @@ import org.briarproject.api.plugins.simplex.SimplexPluginCallback;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginConfig;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.api.transport.ConnectionDispatcher;
|
||||
import org.briarproject.api.ui.UiCallback;
|
||||
import org.briarproject.system.SystemClock;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.Mockery;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -10,8 +10,8 @@ import java.util.concurrent.Executors;
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.plugins.DuplexClientTest;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
// This is not a JUnit test - it has to be run manually while the server test
|
||||
// is running on another machine
|
||||
|
||||
@@ -8,8 +8,8 @@ import java.util.concurrent.Executors;
|
||||
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.plugins.DuplexServerTest;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
// This is not a JUnit test - it has to be run manually while the client test
|
||||
// is running on another machine
|
||||
|
||||
@@ -10,8 +10,8 @@ import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.plugins.DuplexClientTest;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
// This is not a JUnit test - it has to be run manually while the server test
|
||||
// is running on another machine
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ import java.util.concurrent.Executors;
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportProperties;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.system.SystemClock;
|
||||
import org.briarproject.plugins.DuplexServerTest;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
// This is not a JUnit test - it has to be run manually while the client test
|
||||
// is running on another machine
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package org.briarproject.system;
|
||||
|
||||
import static org.briarproject.api.system.SeedProvider.SEED_BYTES;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.HashSet;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.TestUtils;
|
||||
import org.briarproject.api.Bytes;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class LinuxSeedProviderTest extends BriarTestCase {
|
||||
|
||||
private final File testDir = TestUtils.getTestDirectory();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
testDir.mkdirs();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeedAppearsSane() {
|
||||
Set<Bytes> seeds = new HashSet<Bytes>();
|
||||
LinuxSeedProvider p = new LinuxSeedProvider();
|
||||
for(int i = 0; i < 1000; i++) {
|
||||
byte[] seed = p.getSeed();
|
||||
assertEquals(SEED_BYTES, seed.length);
|
||||
assertTrue(seeds.add(new Bytes(seed)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntropyIsWrittenToPool() throws Exception {
|
||||
// Redirect the provider's entropy to a file
|
||||
File urandom = new File(testDir, "urandom");
|
||||
urandom.delete();
|
||||
assertTrue(urandom.createNewFile());
|
||||
assertEquals(0, urandom.length());
|
||||
String path = urandom.getAbsolutePath();
|
||||
LinuxSeedProvider p = new LinuxSeedProvider(path, "/dev/urandom");
|
||||
p.getSeed();
|
||||
// There should be 16 bytes from the clock, plus network interfaces
|
||||
assertTrue(urandom.length() > 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeedIsReadFromPool() throws Exception {
|
||||
// Generate a seed
|
||||
byte[] seed = new byte[SEED_BYTES];
|
||||
new Random().nextBytes(seed);
|
||||
// Write the seed to a file
|
||||
File urandom = new File(testDir, "urandom");
|
||||
urandom.delete();
|
||||
FileOutputStream out = new FileOutputStream(urandom);
|
||||
out.write(seed);
|
||||
out.flush();
|
||||
out.close();
|
||||
assertTrue(urandom.exists());
|
||||
assertEquals(SEED_BYTES, urandom.length());
|
||||
// Check that the provider reads the seed from the file
|
||||
String path = urandom.getAbsolutePath();
|
||||
LinuxSeedProvider p = new LinuxSeedProvider("/dev/urandom", path);
|
||||
assertArrayEquals(seed, p.getSeed());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
TestUtils.deleteTestDirectory(testDir);
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,12 @@ import java.io.ByteArrayInputStream;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.TestLifecycleModule;
|
||||
import org.briarproject.TestSystemModule;
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.crypto.AuthenticatedCipher;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.crypto.CryptoModule;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
@@ -35,7 +35,7 @@ public class IncomingEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
public IncomingEncryptionLayerTest() {
|
||||
Injector i = Guice.createInjector(new CryptoModule(),
|
||||
new TestLifecycleModule());
|
||||
new TestLifecycleModule(), new TestSystemModule());
|
||||
crypto = i.getInstance(CryptoComponent.class);
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
frameKey = crypto.generateSecretKey();
|
||||
|
||||
@@ -11,11 +11,11 @@ import java.io.ByteArrayOutputStream;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.TestLifecycleModule;
|
||||
import org.briarproject.TestSystemModule;
|
||||
import org.briarproject.api.crypto.AuthenticatedCipher;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.crypto.CryptoModule;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
@@ -35,7 +35,7 @@ public class OutgoingEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
public OutgoingEncryptionLayerTest() {
|
||||
Injector i = Guice.createInjector(new CryptoModule(),
|
||||
new TestLifecycleModule());
|
||||
new TestLifecycleModule(), new TestSystemModule());
|
||||
crypto = i.getInstance(CryptoComponent.class);
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
tag = new byte[TAG_LENGTH];
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.util.Random;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.TestLifecycleModule;
|
||||
import org.briarproject.TestSystemModule;
|
||||
import org.briarproject.TestUtils;
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.TransportId;
|
||||
@@ -23,7 +24,6 @@ import org.briarproject.api.transport.ConnectionContext;
|
||||
import org.briarproject.api.transport.ConnectionWriter;
|
||||
import org.briarproject.api.transport.ConnectionWriterFactory;
|
||||
import org.briarproject.crypto.CryptoModule;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
@@ -52,7 +52,7 @@ public class TransportIntegrationTest extends BriarTestCase {
|
||||
}
|
||||
};
|
||||
Injector i = Guice.createInjector(testModule, new CryptoModule(),
|
||||
new TestLifecycleModule());
|
||||
new TestLifecycleModule(), new TestSystemModule());
|
||||
crypto = i.getInstance(CryptoComponent.class);
|
||||
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);
|
||||
contactId = new ContactId(234);
|
||||
|
||||
Reference in New Issue
Block a user