Empty transport configs/properties should be treated the same as nulls.

This commit is contained in:
akwizgran
2012-12-15 16:02:35 +00:00
parent 28af51b156
commit 3b3ab6fd1a
11 changed files with 62 additions and 49 deletions

View File

@@ -178,18 +178,18 @@ class BluetoothPlugin implements DuplexPlugin {
final ContactId c = e.getKey(); final ContactId c = e.getKey();
if(connected.contains(c)) continue; if(connected.contains(c)) continue;
final String address = e.getValue().get("address"); final String address = e.getValue().get("address");
if(StringUtils.isNullOrEmpty(address)) continue;
final String uuid = e.getValue().get("uuid"); final String uuid = e.getValue().get("uuid");
if(address != null && uuid != null) { if(StringUtils.isNullOrEmpty(uuid)) continue;
pluginExecutor.execute(new Runnable() { pluginExecutor.execute(new Runnable() {
public void run() { public void run() {
if(!running) return; if(!running) return;
String url = makeUrl(address, uuid); String url = makeUrl(address, uuid);
DuplexTransportConnection conn = connect(url); DuplexTransportConnection conn = connect(url);
if(conn != null) if(conn != null)
callback.outgoingConnectionCreated(c, conn); callback.outgoingConnectionCreated(c, conn);
} }
}); });
}
} }
} }
@@ -208,8 +208,9 @@ class BluetoothPlugin implements DuplexPlugin {
TransportProperties p = callback.getRemoteProperties().get(c); TransportProperties p = callback.getRemoteProperties().get(c);
if(p == null) return null; if(p == null) return null;
String address = p.get("address"); String address = p.get("address");
if(StringUtils.isNullOrEmpty(address)) return null;
String uuid = p.get("uuid"); String uuid = p.get("uuid");
if(address == null || uuid == null) return null; if(StringUtils.isNullOrEmpty(uuid)) return null;
String url = makeUrl(address, uuid); String url = makeUrl(address, uuid);
return connect(url); return connect(url);
} }

View File

@@ -210,17 +210,17 @@ class DroidtoothPlugin implements DuplexPlugin {
final ContactId c = e.getKey(); final ContactId c = e.getKey();
if(connected.contains(c)) continue; if(connected.contains(c)) continue;
final String address = e.getValue().get("address"); final String address = e.getValue().get("address");
if(StringUtils.isNullOrEmpty(address)) continue;
final String uuid = e.getValue().get("uuid"); final String uuid = e.getValue().get("uuid");
if(address != null && uuid != null) { if(StringUtils.isNullOrEmpty(uuid)) continue;
pluginExecutor.execute(new Runnable() { pluginExecutor.execute(new Runnable() {
public void run() { public void run() {
if(!running) return; if(!running) return;
DuplexTransportConnection conn = connect(address, uuid); DuplexTransportConnection conn = connect(address, uuid);
if(conn != null) if(conn != null)
callback.outgoingConnectionCreated(c, conn); callback.outgoingConnectionCreated(c, conn);
} }
}); });
}
} }
} }
@@ -256,8 +256,9 @@ class DroidtoothPlugin implements DuplexPlugin {
TransportProperties p = callback.getRemoteProperties().get(c); TransportProperties p = callback.getRemoteProperties().get(c);
if(p == null) return null; if(p == null) return null;
String address = p.get("address"); String address = p.get("address");
if(StringUtils.isNullOrEmpty(address)) return null;
String uuid = p.get("uuid"); String uuid = p.get("uuid");
if(address == null || uuid == null) return null; if(StringUtils.isNullOrEmpty(uuid)) return null;
return connect(address, uuid); return connect(address, uuid);
} }

View File

@@ -19,6 +19,7 @@ import java.util.logging.Logger;
import jssc.SerialPortList; import jssc.SerialPortList;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties; import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.crypto.PseudoRandom; import net.sf.briar.api.crypto.PseudoRandom;
import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.PluginExecutor;
@@ -145,7 +146,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
while(it.hasNext() && running) { while(it.hasNext() && running) {
ContactId c = it.next(); ContactId c = it.next();
String number = remote.get(c).get("number"); String number = remote.get(c).get("number");
if(number == null) continue; if(StringUtils.isNullOrEmpty(number)) continue;
try { try {
if(!modem.dial(number)) continue; if(!modem.dial(number)) continue;
} catch(IOException e) { } catch(IOException e) {
@@ -170,10 +171,13 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
public DuplexTransportConnection createConnection(ContactId c) { public DuplexTransportConnection createConnection(ContactId c) {
if(!running) return null; if(!running) return null;
final Map<ContactId, TransportProperties> remote = TransportConfig config = callback.getConfig();
String fromIso = config.get("iso3166");
if(StringUtils.isNullOrEmpty(fromIso)) return null;
Map<ContactId, TransportProperties> remote =
callback.getRemoteProperties(); callback.getRemoteProperties();
String number = remote.get(c).get("number"); String number = remote.get(c).get("number");
if(number == null) return null; if(StringUtils.isNullOrEmpty(number)) return null;
try { try {
if(!modem.dial(number)) return null; if(!modem.dial(number)) return null;
} catch(IOException e) { } catch(IOException e) {

View File

@@ -8,8 +8,7 @@ import net.sf.briar.api.plugins.duplex.DuplexPluginCallback;
import net.sf.briar.api.plugins.duplex.DuplexPluginFactory; import net.sf.briar.api.plugins.duplex.DuplexPluginFactory;
import net.sf.briar.api.protocol.TransportId; import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.reliability.ReliabilityLayerFactory; import net.sf.briar.api.reliability.ReliabilityLayerFactory;
import net.sf.briar.util.StringUtils;
import org.h2.util.StringUtils;
public class ModemPluginFactory implements DuplexPluginFactory { public class ModemPluginFactory implements DuplexPluginFactory {

View File

@@ -67,7 +67,8 @@ class LanTcpPlugin extends TcpPlugin {
String addrString = p.get("address"); String addrString = p.get("address");
String portString = p.get("port"); String portString = p.get("port");
InetAddress addr = null; InetAddress addr = null;
if(addrString != null && portString != null) { if(!StringUtils.isNullOrEmpty(addrString) &&
!StringUtils.isNullOrEmpty(portString)) {
try { try {
addr = InetAddress.getByName(addrString); addr = InetAddress.getByName(addrString);
int port = Integer.valueOf(portString); int port = Integer.valueOf(portString);

View File

@@ -22,6 +22,7 @@ import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.duplex.DuplexPlugin; import net.sf.briar.api.plugins.duplex.DuplexPlugin;
import net.sf.briar.api.plugins.duplex.DuplexPluginCallback; import net.sf.briar.api.plugins.duplex.DuplexPluginCallback;
import net.sf.briar.api.plugins.duplex.DuplexTransportConnection; import net.sf.briar.api.plugins.duplex.DuplexTransportConnection;
import net.sf.briar.util.StringUtils;
abstract class TcpPlugin implements DuplexPlugin { abstract class TcpPlugin implements DuplexPlugin {
@@ -165,8 +166,8 @@ abstract class TcpPlugin implements DuplexPlugin {
public DuplexTransportConnection createConnection(ContactId c) { public DuplexTransportConnection createConnection(ContactId c) {
if(!running) return null; if(!running) return null;
SocketAddress addr = getRemoteSocketAddress(c); SocketAddress addr = getRemoteSocketAddress(c);
if(addr == null) return null;
Socket s = new Socket(); Socket s = new Socket();
if(addr == null || s == null) return null;
try { try {
s.setSoTimeout(0); s.setSoTimeout(0);
s.connect(addr); s.connect(addr);
@@ -181,18 +182,19 @@ abstract class TcpPlugin implements DuplexPlugin {
TransportProperties p = callback.getRemoteProperties().get(c); TransportProperties p = callback.getRemoteProperties().get(c);
if(p == null) return null; if(p == null) return null;
String addrString = p.get("address"); String addrString = p.get("address");
if(StringUtils.isNullOrEmpty(addrString)) return null;
String portString = p.get("port"); String portString = p.get("port");
if(addrString != null && portString != null) { if(StringUtils.isNullOrEmpty(portString)) return null;
try { try {
InetAddress addr = InetAddress.getByName(addrString); InetAddress addr = InetAddress.getByName(addrString);
int port = Integer.valueOf(portString); int port = Integer.valueOf(portString);
return new InetSocketAddress(addr, port); return new InetSocketAddress(addr, port);
} catch(NumberFormatException e) { } catch(NumberFormatException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch(UnknownHostException e) { return null;
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); } catch(UnknownHostException e) {
} if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
return null;
} }
return null;
} }
} }

View File

@@ -61,7 +61,8 @@ class WanTcpPlugin extends TcpPlugin {
String portString = p.get("port"); String portString = p.get("port");
InetAddress addr = null; InetAddress addr = null;
int port = 0; int port = 0;
if(addrString != null && portString != null) { if(!StringUtils.isNullOrEmpty(addrString) &&
!StringUtils.isNullOrEmpty(portString)) {
try { try {
addr = InetAddress.getByName(addrString); addr = InetAddress.getByName(addrString);
port = Integer.valueOf(portString); port = Integer.valueOf(portString);

View File

@@ -95,9 +95,10 @@ class TorPlugin implements DuplexPlugin {
connected = true; connected = true;
notifyAll(); notifyAll();
} }
// If we're configure not to create a hidden service, return // If we're configured not to create a hidden service, return
TransportConfig c = callback.getConfig(); TransportConfig c = callback.getConfig();
if(c.containsKey("noHiddenService")) { String noHiddenService = c.get("noHiddenService");
if(!StringUtils.isNullOrEmpty(noHiddenService)) {
if(LOG.isLoggable(INFO)) LOG.info("Not creating hidden service"); if(LOG.isLoggable(INFO)) LOG.info("Not creating hidden service");
TransportProperties p = new TransportProperties(); TransportProperties p = new TransportProperties();
p.put("onion", null); p.put("onion", null);
@@ -108,7 +109,7 @@ class TorPlugin implements DuplexPlugin {
TorHiddenServicePrivateNetAddress addr; TorHiddenServicePrivateNetAddress addr;
TorNetLayerUtil util = TorNetLayerUtil.getInstance(); TorNetLayerUtil util = TorNetLayerUtil.getInstance();
String privateKey = c.get("privateKey"); String privateKey = c.get("privateKey");
if(privateKey == null) { if(StringUtils.isNullOrEmpty(privateKey)) {
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Creating hidden service address"); LOG.info("Creating hidden service address");
addr = createHiddenServiceAddress(util); addr = createHiddenServiceAddress(util);
@@ -264,7 +265,7 @@ class TorPlugin implements DuplexPlugin {
TransportProperties p = callback.getRemoteProperties().get(c); TransportProperties p = callback.getRemoteProperties().get(c);
if(p == null) return null; if(p == null) return null;
String onion = p.get("onion"); String onion = p.get("onion");
if(onion == null) return null; if(StringUtils.isNullOrEmpty(onion)) return null;
NetAddress addr = new TcpipNetAddress(onion, 80); NetAddress addr = new TcpipNetAddress(onion, 80);
try { try {
if(LOG.isLoggable(INFO)) LOG.info("Connecting to hidden service"); if(LOG.isLoggable(INFO)) LOG.info("Connecting to hidden service");

View File

@@ -7,8 +7,7 @@ import net.sf.briar.api.plugins.duplex.DuplexPlugin;
import net.sf.briar.api.plugins.duplex.DuplexPluginCallback; import net.sf.briar.api.plugins.duplex.DuplexPluginCallback;
import net.sf.briar.api.plugins.duplex.DuplexPluginFactory; import net.sf.briar.api.plugins.duplex.DuplexPluginFactory;
import net.sf.briar.api.protocol.TransportId; import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.util.StringUtils;
import org.h2.util.StringUtils;
public class TorPluginFactory implements DuplexPluginFactory { public class TorPluginFactory implements DuplexPluginFactory {

View File

@@ -7,6 +7,10 @@ public class StringUtils {
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
}; };
public static boolean isNullOrEmpty(String s) {
return s == null || s.isEmpty();
}
/** /**
* Trims the given string to the given length, returning the head and * Trims the given string to the given length, returning the head and
* appending "..." if the string was trimmed. * appending "..." if the string was trimmed.

View File

@@ -42,7 +42,7 @@ public class TorPluginTest extends BriarTestCase {
assertTrue(onion.endsWith(".onion")); assertTrue(onion.endsWith(".onion"));
// Create another plugin instance for the client // Create another plugin instance for the client
Callback clientCallback = new Callback(); Callback clientCallback = new Callback();
clientCallback.config.put("noHiddenService", ""); clientCallback.config.put("noHiddenService", "true");
TransportProperties p = new TransportProperties(); TransportProperties p = new TransportProperties();
p.put("onion", onion); p.put("onion", onion);
clientCallback.remote.put(contactId, p); clientCallback.remote.put(contactId, p);