Use strings rather than hashes to identify transports. Dev task #64.

This commit is contained in:
akwizgran
2014-01-24 10:39:34 +00:00
parent 468db2a97b
commit 822392f9e7
26 changed files with 115 additions and 172 deletions

View File

@@ -47,11 +47,7 @@ import android.content.IntentFilter;
class DroidtoothPlugin implements DuplexPlugin {
// Share an ID with the J2SE Bluetooth plugin
static final byte[] TRANSPORT_ID =
StringUtils.fromHexString("d99c9313c04417dcf22fc60d12a187ea"
+ "00a539fd260f08a13a0d8a900cde5e49"
+ "1b4df2ffd42e40c408f2db7868f518aa");
static final TransportId ID = new TransportId(TRANSPORT_ID);
static final TransportId ID = new TransportId("bt");
private static final Logger LOG =
Logger.getLogger(DroidtoothPlugin.class.getName());
@@ -94,11 +90,6 @@ class DroidtoothPlugin implements DuplexPlugin {
return ID;
}
public String getName() {
// Share a name with the J2SE Bluetooth plugin
return "BLUETOOTH_PLUGIN_NAME";
}
public int getMaxFrameLength() {
return maxFrameLength;
}

View File

@@ -53,11 +53,7 @@ import android.os.FileObserver;
class TorPlugin implements DuplexPlugin, EventHandler {
static final byte[] TRANSPORT_ID =
StringUtils.fromHexString("fa866296495c73a52e6a82fd12db6f15"
+ "47753b5e636bb8b24975780d7d2e3fc2"
+ "d32a4c480c74de2dc6e3157a632a0287");
static final TransportId ID = new TransportId(TRANSPORT_ID);
static final TransportId ID = new TransportId("tor");
private static final int SOCKS_PORT = 59050, CONTROL_PORT = 59051;
private static final int COOKIE_TIMEOUT = 3000; // Milliseconds
@@ -108,10 +104,6 @@ class TorPlugin implements DuplexPlugin, EventHandler {
return ID;
}
public String getName() {
return "TOR_PLUGIN_NAME";
}
public int getMaxFrameLength() {
return maxFrameLength;
}

View File

@@ -1,21 +1,32 @@
package org.briarproject.api;
import java.util.Arrays;
import static org.briarproject.api.TransportPropertyConstants.MAX_TRANSPORT_ID_LENGTH;
/**
* Type-safe wrapper for a byte array that uniquely identifies a transport
* plugin.
* Type-safe wrapper for a string that uniquely identifies a transport plugin.
*/
public class TransportId extends UniqueId {
public class TransportId {
public TransportId(byte[] id) {
super(id);
private final String id;
public TransportId(String id) {
if(id.length() > MAX_TRANSPORT_ID_LENGTH || id.equals(""))
throw new IllegalArgumentException();
this.id = id;
}
public String getString() {
return id;
}
@Override
public boolean equals(Object o) {
if(o instanceof TransportId)
return Arrays.equals(id, ((TransportId) o).id);
if(o instanceof TransportId) return id.equals(((TransportId) o).id);
return false;
}
@Override
public int hashCode() {
return id.hashCode();
}
}

View File

@@ -2,6 +2,12 @@ package org.briarproject.api;
public interface TransportPropertyConstants {
/**
* The maximum length of a string that uniquely identifies a transport
* plugin.
*/
int MAX_TRANSPORT_ID_LENGTH = 10;
/** The maximum number of properties per transport. */
int MAX_PROPERTIES_PER_TRANSPORT = 100;

View File

@@ -11,9 +11,6 @@ public interface Plugin {
/** Returns the plugin's transport identifier. */
TransportId getId();
/** Returns a label for looking up the plugin's translated name. */
String getName();
/** Returns the transport's maximum frame length in bytes. */
int getMaxFrameLength();

View File

@@ -57,7 +57,7 @@ import org.briarproject.api.transport.TemporarySecret;
*/
abstract class JdbcDatabase implements Database<Connection> {
private static final int SCHEMA_VERSION = 1;
private static final int SCHEMA_VERSION = 2;
private static final String CREATE_SETTINGS =
"CREATE TABLE settings"
@@ -219,14 +219,14 @@ abstract class JdbcDatabase implements Database<Connection> {
// Dependents: window
private static final String CREATE_TRANSPORTS =
"CREATE TABLE transports"
+ " (transportId HASH NOT NULL,"
+ " (transportId VARCHAR NOT NULL,"
+ " maxLatency BIGINT NOT NULL,"
+ " PRIMARY KEY (transportId))";
// Locking: transport
private static final String CREATE_TRANSPORT_CONFIGS =
"CREATE TABLE transportConfigs"
+ " (transportId HASH NOT NULL,"
+ " (transportId VARCHAR NOT NULL,"
+ " key VARCHAR NOT NULL,"
+ " value VARCHAR NOT NULL,"
+ " PRIMARY KEY (transportId, key),"
@@ -237,7 +237,7 @@ abstract class JdbcDatabase implements Database<Connection> {
// Locking: transport
private static final String CREATE_TRANSPORT_PROPS =
"CREATE TABLE transportProperties"
+ " (transportId HASH NOT NULL,"
+ " (transportId VARCHAR NOT NULL,"
+ " key VARCHAR NOT NULL,"
+ " value VARCHAR NOT NULL,"
+ " PRIMARY KEY (transportId, key),"
@@ -249,7 +249,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_TRANSPORT_VERSIONS =
"CREATE TABLE transportVersions"
+ " (contactId INT NOT NULL,"
+ " transportId HASH NOT NULL,"
+ " transportId VARCHAR NOT NULL,"
+ " localVersion BIGINT NOT NULL,"
+ " localAcked BIGINT NOT NULL,"
+ " expiry BIGINT NOT NULL,"
@@ -266,7 +266,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_CONTACT_TRANSPORT_PROPS =
"CREATE TABLE contactTransportProperties"
+ " (contactId INT NOT NULL,"
+ " transportId HASH NOT NULL," // Not a foreign key
+ " transportId VARCHAR NOT NULL," // Not a foreign key
+ " key VARCHAR NOT NULL,"
+ " value VARCHAR NOT NULL,"
+ " PRIMARY KEY (contactId, transportId, key),"
@@ -278,7 +278,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_CONTACT_TRANSPORT_VERSIONS =
"CREATE TABLE contactTransportVersions"
+ " (contactId INT NOT NULL,"
+ " transportId HASH NOT NULL," // Not a foreign key
+ " transportId VARCHAR NOT NULL," // Not a foreign key
+ " remoteVersion BIGINT NOT NULL,"
+ " remoteAcked BOOLEAN NOT NULL,"
+ " PRIMARY KEY (contactId, transportId),"
@@ -290,7 +290,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_ENDPOINTS =
"CREATE TABLE endpoints"
+ " (contactId INT NOT NULL,"
+ " transportId HASH NOT NULL,"
+ " transportId VARCHAR NOT NULL,"
+ " epoch BIGINT NOT NULL,"
+ " alice BOOLEAN NOT NULL,"
+ " PRIMARY KEY (contactId, transportId),"
@@ -305,7 +305,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_SECRETS =
"CREATE TABLE secrets"
+ " (contactId INT NOT NULL,"
+ " transportId HASH NOT NULL,"
+ " transportId VARCHAR NOT NULL,"
+ " period BIGINT NOT NULL,"
+ " secret SECRET NOT NULL,"
+ " outgoing BIGINT NOT NULL,"
@@ -676,8 +676,8 @@ abstract class JdbcDatabase implements Database<Connection> {
sql = "SELECT transportId FROM transports";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
Collection<byte[]> transports = new ArrayList<byte[]>();
while(rs.next()) transports.add(rs.getBytes(1));
Collection<String> transports = new ArrayList<String>();
while(rs.next()) transports.add(rs.getString(1));
rs.close();
ps.close();
if(transports.isEmpty()) return c;
@@ -686,8 +686,8 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " VALUES (?, ?, 1, 0, 0, 0)";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
for(byte[] t : transports) {
ps.setBytes(2, t);
for(String t : transports) {
ps.setString(2, t);
ps.addBatch();
}
int[] batchAffected = ps.executeBatch();
@@ -713,7 +713,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " VALUES (?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setInt(1, ep.getContactId().getInt());
ps.setBytes(2, ep.getTransportId().getBytes());
ps.setString(2, ep.getTransportId().getString());
ps.setLong(3, ep.getEpoch());
ps.setBoolean(4, ep.getAlice());
int affected = ps.executeUpdate();
@@ -860,7 +860,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql);
for(TemporarySecret s : secrets) {
ps.setInt(1, s.getContactId().getInt());
ps.setBytes(2, s.getTransportId().getBytes());
ps.setString(2, s.getTransportId().getString());
ps.setLong(3, s.getPeriod());
ps.setBytes(4, s.getSecret());
ps.setLong(5, s.getOutgoingConnectionCounter());
@@ -881,7 +881,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql);
for(TemporarySecret s : secrets) {
ps.setInt(1, s.getContactId().getInt());
ps.setBytes(2, s.getTransportId().getBytes());
ps.setString(2, s.getTransportId().getString());
ps.setLong(3, s.getPeriod() - 2);
ps.addBatch();
}
@@ -924,7 +924,7 @@ abstract class JdbcDatabase implements Database<Connection> {
// Return false if the transport is already in the database
String sql = "SELECT NULL FROM transports WHERE transportId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, t.getBytes());
ps.setString(1, t.getString());
rs = ps.executeQuery();
boolean found = rs.next();
if(rs.next()) throw new DbStateException();
@@ -935,7 +935,7 @@ abstract class JdbcDatabase implements Database<Connection> {
sql = "INSERT INTO transports (transportId, maxLatency)"
+ " VALUES (?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, t.getBytes());
ps.setString(1, t.getString());
ps.setLong(2, maxLatency);
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
@@ -953,7 +953,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " localVersion, localAcked, expiry, txCount)"
+ " VALUES (?, ?, 1, 0, 0, 0)";
ps = txn.prepareStatement(sql);
ps.setBytes(2, t.getBytes());
ps.setString(2, t.getString());
for(Integer c : contacts) {
ps.setInt(1, c);
ps.addBatch();
@@ -1113,7 +1113,7 @@ abstract class JdbcDatabase implements Database<Connection> {
try {
String sql = "SELECT NULL FROM transports WHERE transportId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, t.getBytes());
ps.setString(1, t.getString());
rs = ps.executeQuery();
boolean found = rs.next();
if(rs.next()) throw new DbStateException();
@@ -1256,7 +1256,7 @@ abstract class JdbcDatabase implements Database<Connection> {
String sql = "SELECT key, value FROM transportConfigs"
+ " WHERE transportId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, t.getBytes());
ps.setString(1, t.getString());
rs = ps.executeQuery();
TransportConfig c = new TransportConfig();
while(rs.next()) c.put(rs.getString(1), rs.getString(2));
@@ -1380,7 +1380,7 @@ abstract class JdbcDatabase implements Database<Connection> {
List<Endpoint> endpoints = new ArrayList<Endpoint>();
while(rs.next()) {
ContactId contactId = new ContactId(rs.getInt(1));
TransportId transportId = new TransportId(rs.getBytes(2));
TransportId transportId = new TransportId(rs.getString(2));
long epoch = rs.getLong(3);
boolean alice = rs.getBoolean(4);
endpoints.add(new Endpoint(contactId, transportId, epoch,
@@ -1617,7 +1617,7 @@ abstract class JdbcDatabase implements Database<Connection> {
TransportId lastId = null;
TransportProperties p = null;
while(rs.next()) {
TransportId id = new TransportId(rs.getBytes(1));
TransportId id = new TransportId(rs.getString(1));
String key = rs.getString(2), value = rs.getString(3);
if(!id.equals(lastId)) {
p = new TransportProperties();
@@ -1644,7 +1644,7 @@ abstract class JdbcDatabase implements Database<Connection> {
String sql = "SELECT key, value FROM transportProperties"
+ " WHERE transportId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, t.getBytes());
ps.setString(1, t.getString());
rs = ps.executeQuery();
TransportProperties p = new TransportProperties();
while(rs.next()) p.put(rs.getString(1), rs.getString(2));
@@ -1967,7 +1967,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " WHERE transportId = ?"
+ " ORDER BY contactId";
ps = txn.prepareStatement(sql);
ps.setBytes(1, t.getBytes());
ps.setString(1, t.getString());
rs = ps.executeQuery();
Map<ContactId, TransportProperties> properties =
new HashMap<ContactId, TransportProperties>();
@@ -2141,7 +2141,7 @@ abstract class JdbcDatabase implements Database<Connection> {
List<TemporarySecret> secrets = new ArrayList<TemporarySecret>();
while(rs.next()) {
ContactId contactId = new ContactId(rs.getInt(1));
TransportId transportId = new TransportId(rs.getBytes(2));
TransportId transportId = new TransportId(rs.getString(2));
long epoch = rs.getLong(3);
boolean alice = rs.getBoolean(4);
long period = rs.getLong(5);
@@ -2260,7 +2260,7 @@ abstract class JdbcDatabase implements Database<Connection> {
rs = ps.executeQuery();
List<TransportAck> acks = new ArrayList<TransportAck>();
while(rs.next()) {
TransportId id = new TransportId(rs.getBytes(1));
TransportId id = new TransportId(rs.getString(1));
acks.add(new TransportAck(id, rs.getLong(2)));
}
rs.close();
@@ -2271,7 +2271,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
for(TransportAck a : acks) {
ps.setBytes(2, a.getId().getBytes());
ps.setString(2, a.getId().getString());
ps.addBatch();
}
int[] batchAffected = ps.executeBatch();
@@ -2299,7 +2299,7 @@ abstract class JdbcDatabase implements Database<Connection> {
rs = ps.executeQuery();
Map<TransportId, Long> latencies = new HashMap<TransportId, Long>();
while(rs.next()){
TransportId id = new TransportId(rs.getBytes(1));
TransportId id = new TransportId(rs.getString(1));
latencies.put(id, rs.getLong(2));
}
rs.close();
@@ -2336,7 +2336,7 @@ abstract class JdbcDatabase implements Database<Connection> {
TransportProperties p = null;
List<Integer> txCounts = new ArrayList<Integer>();
while(rs.next()) {
TransportId id = new TransportId(rs.getBytes(1));
TransportId id = new TransportId(rs.getString(1));
String key = rs.getString(2), value = rs.getString(3);
long version = rs.getLong(4);
int txCount = rs.getInt(5);
@@ -2360,7 +2360,7 @@ abstract class JdbcDatabase implements Database<Connection> {
for(TransportUpdate u : updates) {
int txCount = txCounts.get(i++);
ps.setLong(1, calculateExpiry(now, maxLatency, txCount));
ps.setBytes(3, u.getId().getBytes());
ps.setString(3, u.getId().getString());
ps.addBatch();
}
int [] batchAffected = ps.executeBatch();
@@ -2436,7 +2436,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " WHERE contactId = ? AND transportId = ? AND period = ?";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
ps.setBytes(2, t.getBytes());
ps.setString(2, t.getString());
ps.setLong(3, period);
rs = ps.executeQuery();
if(!rs.next()) {
@@ -2453,7 +2453,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " WHERE contactId = ? AND transportId = ? AND period = ?";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
ps.setBytes(2, t.getBytes());
ps.setString(2, t.getString());
ps.setLong(3, period);
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
@@ -2548,7 +2548,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " SET localVersion = localVersion + 1, expiry = 0"
+ " WHERE transportId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, t.getBytes());
ps.setString(1, t.getString());
ps.executeUpdate();
ps.close();
} catch(SQLException e) {
@@ -2565,7 +2565,7 @@ abstract class JdbcDatabase implements Database<Connection> {
String sql = "UPDATE " + tableName + " SET value = ?"
+ " WHERE transportId = ? AND key = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(2, t.getBytes());
ps.setString(2, t.getString());
for(Entry<String, String> e : m.entrySet()) {
ps.setString(1, e.getValue());
ps.setString(3, e.getKey());
@@ -2581,7 +2581,7 @@ abstract class JdbcDatabase implements Database<Connection> {
sql = "INSERT INTO " + tableName + " (transportId, key, value)"
+ " VALUES (?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, t.getBytes());
ps.setString(1, t.getString());
int updateIndex = 0, inserted = 0;
for(Entry<String, String> e : m.entrySet()) {
if(batchAffected[updateIndex] == 0) {
@@ -2804,7 +2804,7 @@ abstract class JdbcDatabase implements Database<Connection> {
try {
String sql = "DELETE FROM transports WHERE transportId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, t.getBytes());
ps.setString(1, t.getString());
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
ps.close();
@@ -2869,7 +2869,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.setLong(1, centre);
ps.setBytes(2, bitmap);
ps.setInt(3, c.getInt());
ps.setBytes(4, t.getBytes());
ps.setString(4, t.getString());
ps.setLong(5, period);
int affected = ps.executeUpdate();
if(affected < 0 || affected > 1) throw new DbStateException();
@@ -3027,7 +3027,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.setInt(1, c.getInt());
int batchSize = 0;
for(Entry<TransportId, TransportProperties> e : p.entrySet()) {
ps.setBytes(2, e.getKey().getBytes());
ps.setString(2, e.getKey().getString());
for(Entry<String, String> e1 : e.getValue().entrySet()) {
ps.setString(3, e1.getKey());
ps.setString(4, e1.getValue());
@@ -3058,7 +3058,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " WHERE contactId = ? AND transportId = ?";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
ps.setBytes(2, t.getBytes());
ps.setString(2, t.getString());
rs = ps.executeQuery();
boolean found = rs.next();
if(rs.next()) throw new DbStateException();
@@ -3074,7 +3074,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql);
ps.setLong(1, version);
ps.setInt(2, c.getInt());
ps.setBytes(3, t.getBytes());
ps.setString(3, t.getString());
ps.setLong(4, version);
int affected = ps.executeUpdate();
if(affected < 0 || affected > 1) throw new DbStateException();
@@ -3088,7 +3088,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " VALUES (?, ?, ?, FALSE)";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
ps.setBytes(2, t.getBytes());
ps.setString(2, t.getString());
ps.setLong(3, version);
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
@@ -3099,7 +3099,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " WHERE contactId = ? AND transportId = ?";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
ps.setBytes(2, t.getBytes());
ps.setString(2, t.getString());
ps.executeUpdate();
ps.close();
// Store the new properties, if any
@@ -3109,7 +3109,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " VALUES (?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
ps.setBytes(2, t.getBytes());
ps.setString(2, t.getString());
for(Entry<String, String> e : p.entrySet()) {
ps.setString(3, e.getKey());
ps.setString(4, e.getValue());
@@ -3203,7 +3203,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql);
ps.setLong(1, version);
ps.setInt(2, c.getInt());
ps.setBytes(3, t.getBytes());
ps.setString(3, t.getString());
ps.setLong(4, version);
ps.setLong(5, version);
int affected = ps.executeUpdate();

View File

@@ -6,6 +6,7 @@ import static org.briarproject.api.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.api.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.api.TransportPropertyConstants.MAX_PROPERTIES_PER_TRANSPORT;
import static org.briarproject.api.TransportPropertyConstants.MAX_PROPERTY_LENGTH;
import static org.briarproject.api.TransportPropertyConstants.MAX_TRANSPORT_ID_LENGTH;
import static org.briarproject.api.invitation.InvitationConstants.CONNECTION_TIMEOUT;
import static org.briarproject.api.invitation.InvitationConstants.HASH_LENGTH;
@@ -29,7 +30,6 @@ import org.briarproject.api.FormatException;
import org.briarproject.api.LocalAuthor;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.UniqueId;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.KeyManager;
import org.briarproject.api.crypto.KeyPair;
@@ -233,7 +233,7 @@ abstract class Connector extends Thread {
protected void sendTransportProperties(Writer w) throws IOException {
w.writeListStart();
for(Entry<TransportId, TransportProperties> e : localProps.entrySet()) {
w.writeBytes(e.getKey().getBytes());
w.writeString(e.getKey().getString());
w.writeMap(e.getValue());
}
w.writeListEnd();
@@ -248,9 +248,9 @@ abstract class Connector extends Thread {
new HashMap<TransportId, TransportProperties>();
r.readListStart();
while(!r.hasListEnd()) {
byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH) throw new FormatException();
TransportId id = new TransportId(b);
String idString = r.readString(MAX_TRANSPORT_ID_LENGTH);
if(idString.equals("")) throw new FormatException();
TransportId id = new TransportId(idString);
Map<String, String> p = new HashMap<String, String>();
r.readMapStart();
for(int i = 0; !r.hasMapEnd(); i++) {
@@ -331,12 +331,8 @@ abstract class Connector extends Thread {
new TransportIdComparator();
public int compare(TransportId t1, TransportId t2) {
byte[] b1 = t1.getBytes(), b2 = t2.getBytes();
for(int i = 0; i < UniqueId.LENGTH; i++) {
if((b1[i] & 0xff) < (b2[i] & 0xff)) return -1;
if((b1[i] & 0xff) > (b2[i] & 0xff)) return 1;
}
return 0;
String s1 = t1.getString(), s2 = t2.getString();
return String.CASE_INSENSITIVE_ORDER.compare(s1, s2);
}
}
}

View File

@@ -2,6 +2,7 @@ package org.briarproject.messaging;
import static org.briarproject.api.TransportPropertyConstants.MAX_PROPERTIES_PER_TRANSPORT;
import static org.briarproject.api.TransportPropertyConstants.MAX_PROPERTY_LENGTH;
import static org.briarproject.api.TransportPropertyConstants.MAX_TRANSPORT_ID_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PACKET_LENGTH;
import static org.briarproject.api.messaging.Types.ACK;
import static org.briarproject.api.messaging.Types.MESSAGE;
@@ -211,12 +212,13 @@ class PacketReaderImpl implements PacketReader {
public TransportAck readTransportAck() throws IOException {
r.readStructStart(TRANSPORT_ACK);
byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length < UniqueId.LENGTH) throw new FormatException();
String idString = r.readString(MAX_TRANSPORT_ID_LENGTH);
if(idString.equals("")) throw new FormatException();
TransportId id = new TransportId(idString);
long version = r.readInteger();
if(version < 0) throw new FormatException();
r.readStructEnd();
return new TransportAck(new TransportId(b), version);
return new TransportAck(id, version);
}
public boolean hasTransportUpdate() throws IOException {
@@ -230,9 +232,9 @@ class PacketReaderImpl implements PacketReader {
// Read the start of the struct
r.readStructStart(TRANSPORT_UPDATE);
// Read the transport ID
byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length < UniqueId.LENGTH) throw new FormatException();
TransportId id = new TransportId(b);
String idString = r.readString(MAX_TRANSPORT_ID_LENGTH);
if(idString.equals("")) throw new FormatException();
TransportId id = new TransportId(idString);
// Read the transport properties
Map<String, String> p = new HashMap<String, String>();
r.readMapStart();

View File

@@ -141,7 +141,7 @@ class PacketWriterImpl implements PacketWriter {
public void writeTransportAck(TransportAck a) throws IOException {
w.writeStructStart(TRANSPORT_ACK);
w.writeBytes(a.getId().getBytes());
w.writeString(a.getId().getString());
w.writeInteger(a.getVersion());
w.writeStructEnd();
if(flush) out.flush();
@@ -149,7 +149,7 @@ class PacketWriterImpl implements PacketWriter {
public void writeTransportUpdate(TransportUpdate u) throws IOException {
w.writeStructStart(TRANSPORT_UPDATE);
w.writeBytes(u.getId().getBytes());
w.writeString(u.getId().getString());
w.writeMap(u.getProperties());
w.writeInteger(u.getVersion());
w.writeStructEnd();

View File

@@ -34,11 +34,7 @@ import org.briarproject.util.StringUtils;
/** A socket plugin that supports exchanging invitations over a LAN. */
class LanTcpPlugin extends TcpPlugin {
static final byte[] TRANSPORT_ID =
StringUtils.fromHexString("0d79357fd7f74d66c2f6f6ad0f7fff81"
+ "d21c53a43b90b0507ed0683872d8e2fc"
+ "5a88e8f953638228dc26669639757bbf");
static final TransportId ID = new TransportId(TRANSPORT_ID);
static final TransportId ID = new TransportId("lan");
private static final Logger LOG =
Logger.getLogger(LanTcpPlugin.class.getName());
@@ -58,10 +54,6 @@ class LanTcpPlugin extends TcpPlugin {
return ID;
}
public String getName() {
return "LAN_TCP_PLUGIN_NAME";
}
@Override
protected List<SocketAddress> getLocalSocketAddresses() {
List<SocketAddress> addrs = new ArrayList<SocketAddress>();

View File

@@ -24,11 +24,7 @@ import org.briarproject.util.StringUtils;
class WanTcpPlugin extends TcpPlugin {
static final byte[] TRANSPORT_ID =
StringUtils.fromHexString("58c66d999e492b85065924acfd739d80"
+ "c65a62f87e5a4fc6c284f95908b9007d"
+ "512a93ebf89bf68f50a29e96eebf97b6");
static final TransportId ID = new TransportId(TRANSPORT_ID);
static final TransportId ID = new TransportId("wan");
private static final Logger LOG =
Logger.getLogger(WanTcpPlugin.class.getName());
@@ -49,10 +45,6 @@ class WanTcpPlugin extends TcpPlugin {
return ID;
}
public String getName() {
return "WAN_TCP_PLUGIN_NAME";
}
@Override
protected List<SocketAddress> getLocalSocketAddresses() {
List<SocketAddress> addrs = new ArrayList<SocketAddress>();

View File

@@ -36,11 +36,7 @@ import org.briarproject.util.StringUtils;
class BluetoothPlugin implements DuplexPlugin {
// Share an ID with the Android Bluetooth plugin
static final byte[] TRANSPORT_ID =
StringUtils.fromHexString("d99c9313c04417dcf22fc60d12a187ea"
+ "00a539fd260f08a13a0d8a900cde5e49"
+ "1b4df2ffd42e40c408f2db7868f518aa");
static final TransportId ID = new TransportId(TRANSPORT_ID);
static final TransportId ID = new TransportId("bt");
private static final Logger LOG =
Logger.getLogger(BluetoothPlugin.class.getName());
@@ -74,10 +70,6 @@ class BluetoothPlugin implements DuplexPlugin {
return ID;
}
public String getName() {
return "BLUETOOTH_PLUGIN_NAME";
}
public int getMaxFrameLength() {
return maxFrameLength;
}

View File

@@ -15,16 +15,11 @@ import org.briarproject.api.ContactId;
import org.briarproject.api.TransportId;
import org.briarproject.api.plugins.simplex.SimplexPluginCallback;
import org.briarproject.api.system.FileUtils;
import org.briarproject.util.StringUtils;
class RemovableDrivePlugin extends FilePlugin
implements RemovableDriveMonitor.Callback {
static final byte[] TRANSPORT_ID =
StringUtils.fromHexString("7c81bf5c9b1cd557685548c85f976bbd"
+ "e633d2418ea2e230e5710fb43c6f8cc0"
+ "68abca3a9d0edb13bcea13b851725c5d");
static final TransportId ID = new TransportId(TRANSPORT_ID);
static final TransportId ID = new TransportId("file");
private static final Logger LOG =
Logger.getLogger(RemovableDrivePlugin.class.getName());
@@ -45,10 +40,6 @@ implements RemovableDriveMonitor.Callback {
return ID;
}
public String getName() {
return "REMOVABLE_DRIVE_PLUGIN_NAME";
}
public boolean start() throws IOException {
running = true;
monitor.start(this);

View File

@@ -27,11 +27,7 @@ import org.briarproject.util.StringUtils;
class ModemPlugin implements DuplexPlugin, Modem.Callback {
static final byte[] TRANSPORT_ID =
StringUtils.fromHexString("8f573867bedf54884b5868ee5d902832" +
"ee5e522da84d0d431712bd672fbd2f79" +
"262d27b93879b94ee9afbb80e7fc87fb");
static final TransportId ID = new TransportId(TRANSPORT_ID);
static final TransportId ID = new TransportId("modem");
private static final Logger LOG =
Logger.getLogger(ModemPlugin.class.getName());
@@ -65,10 +61,6 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
return ID;
}
public String getName() {
return "MODEM_PLUGIN_NAME";
}
public int getMaxFrameLength() {
return maxFrameLength;
}

View File

@@ -109,7 +109,7 @@ public class ProtocolIntegrationTest extends BriarTestCase {
messageBody.getBytes("UTF-8"));
messageIds = Arrays.asList(message.getId(), message1.getId());
// Create some transport properties
transportId = new TransportId(TestUtils.getRandomId());
transportId = new TransportId("id");
transportProperties = new TransportProperties(Collections.singletonMap(
"bar", "baz"));
}

View File

@@ -98,9 +98,9 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
subject, timestamp, raw);
message1 = new TestMessage(messageId1, messageId, group, null,
contentType, subject, timestamp, raw);
transportId = new TransportId(TestUtils.getRandomId());
transportId = new TransportId("id");
transportProperties = new TransportProperties(Collections.singletonMap(
"foo", "bar"));
"bar", "baz"));
contactId = new ContactId(234);
contact = new Contact(contactId, author, localAuthorId);
endpoint = new Endpoint(contactId, transportId, 123, true);

View File

@@ -85,7 +85,7 @@ public class H2DatabaseTest extends BriarTestCase {
random.nextBytes(raw);
message = new TestMessage(messageId, null, group, author, contentType,
subject, timestamp, raw);
transportId = new TransportId(TestUtils.getRandomId());
transportId = new TransportId("id");
contactId = new ContactId(1);
}
@@ -1320,8 +1320,8 @@ public class H2DatabaseTest extends BriarTestCase {
long epoch1 = 123, latency1 = 234;
long epoch2 = 345, latency2 = 456;
boolean alice1 = true, alice2 = false;
TransportId transportId1 = new TransportId(TestUtils.getRandomId());
TransportId transportId2 = new TransportId(TestUtils.getRandomId());
TransportId transportId1 = new TransportId("bar");
TransportId transportId2 = new TransportId("baz");
Endpoint ep1 = new Endpoint(contactId, transportId1, epoch1, alice1);
Endpoint ep2 = new Endpoint(contactId, transportId2, epoch2, alice2);

View File

@@ -5,6 +5,7 @@ import static org.briarproject.api.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.api.TransportPropertyConstants.MAX_PROPERTIES_PER_TRANSPORT;
import static org.briarproject.api.TransportPropertyConstants.MAX_PROPERTY_LENGTH;
import static org.briarproject.api.TransportPropertyConstants.MAX_TRANSPORT_ID_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_BODY_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_CONTENT_TYPE_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_GROUP_NAME_LENGTH;
@@ -174,7 +175,8 @@ public class ConstantsTest extends BriarTestCase {
p.put(key, value);
}
// Create a maximum-length transport update
TransportId id = new TransportId(TestUtils.getRandomId());
String idString = TestUtils.createRandomString(MAX_TRANSPORT_ID_LENGTH);
TransportId id = new TransportId(idString);
TransportUpdate u = new TransportUpdate(id, p, Long.MAX_VALUE);
// Serialise the update
ByteArrayOutputStream out = new ByteArrayOutputStream();

View File

@@ -76,7 +76,7 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
packetWriterFactory = i.getInstance(PacketWriterFactory.class);
contactId = new ContactId(234);
messageId = new MessageId(TestUtils.getRandomId());
transportId = new TransportId(TestUtils.getRandomId());
transportId = new TransportId("id");
secret = new byte[32];
new Random().nextBytes(secret);
}

View File

@@ -69,7 +69,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
public SimplexMessagingIntegrationTest() throws Exception {
GroupId groupId = new GroupId(TestUtils.getRandomId());
group = new Group(groupId, "Group", new byte[GROUP_SALT_LENGTH]);
transportId = new TransportId(TestUtils.getRandomId());
transportId = new TransportId("id");
// Create matching secrets for Alice and Bob
initialSecret = new byte[32];
new Random().nextBytes(initialSecret);

View File

@@ -5,7 +5,6 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.TransportId;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.plugins.duplex.DuplexPlugin;
@@ -44,25 +43,23 @@ public class PluginManagerImplTest extends BriarTestCase {
final SimplexPluginFactory simplexFactory =
context.mock(SimplexPluginFactory.class);
final SimplexPlugin simplexPlugin = context.mock(SimplexPlugin.class);
final TransportId simplexId = new TransportId(TestUtils.getRandomId());
final TransportId simplexId = new TransportId("simplex");
final long simplexLatency = 12345;
final SimplexPluginFactory simplexFailFactory =
context.mock(SimplexPluginFactory.class, "simplexFailFactory");
final SimplexPlugin simplexFailPlugin =
context.mock(SimplexPlugin.class, "simplexFailPlugin");
final TransportId simplexFailId =
new TransportId(TestUtils.getRandomId());
final TransportId simplexFailId = new TransportId("simplex1");
final long simplexFailLatency = 23456;
// Two duplex plugin factories: one creates a plugin, the other fails
final DuplexPluginFactory duplexFactory =
context.mock(DuplexPluginFactory.class);
final DuplexPlugin duplexPlugin = context.mock(DuplexPlugin.class);
final TransportId duplexId = new TransportId(TestUtils.getRandomId());
final TransportId duplexId = new TransportId("duplex");
final long duplexLatency = 34567;
final DuplexPluginFactory duplexFailFactory =
context.mock(DuplexPluginFactory.class, "duplexFailFactory");
final TransportId duplexFailId =
new TransportId(TestUtils.getRandomId());
final TransportId duplexFailId = new TransportId("duplex1");
context.checking(new Expectations() {{
// First simplex plugin
oneOf(simplexPluginConfig).getFactories();

View File

@@ -5,11 +5,9 @@ import java.util.Collection;
import java.util.Collections;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.ContactId;
import org.briarproject.api.TransportId;
import org.briarproject.api.transport.ConnectionRegistry;
import org.junit.Test;
public class ConnectionRegistryImplTest extends BriarTestCase {
@@ -20,8 +18,8 @@ public class ConnectionRegistryImplTest extends BriarTestCase {
public ConnectionRegistryImplTest() {
contactId = new ContactId(1);
contactId1 = new ContactId(2);
transportId = new TransportId(TestUtils.getRandomId());
transportId1 = new TransportId(TestUtils.getRandomId());
transportId = new TransportId("id");
transportId1 = new TransportId("id1");
}
@Test

View File

@@ -7,7 +7,6 @@ import java.util.Arrays;
import java.util.Collections;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.ContactId;
import org.briarproject.api.TransportId;
import org.briarproject.api.crypto.CryptoComponent;
@@ -19,7 +18,6 @@ import org.briarproject.api.transport.ConnectionContext;
import org.briarproject.api.transport.ConnectionRecogniser;
import org.briarproject.api.transport.Endpoint;
import org.briarproject.api.transport.TemporarySecret;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;
@@ -38,7 +36,7 @@ public class KeyManagerImplTest extends BriarTestCase {
public KeyManagerImplTest() {
contactId = new ContactId(234);
transportId = new TransportId(TestUtils.getRandomId());
transportId = new TransportId("id");
secret0 = new byte[32];
secret1 = new byte[32];
secret2 = new byte[32];

View File

@@ -8,7 +8,6 @@ import java.util.Arrays;
import java.util.Collections;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.ContactId;
import org.briarproject.api.TransportId;
import org.briarproject.api.crypto.CryptoComponent;
@@ -22,7 +21,6 @@ import org.briarproject.api.transport.ConnectionRecogniser;
import org.briarproject.api.transport.Endpoint;
import org.briarproject.api.transport.TemporarySecret;
import org.briarproject.util.ByteUtils;
import org.hamcrest.Description;
import org.jmock.Expectations;
import org.jmock.Mockery;
@@ -45,7 +43,7 @@ public class KeyRotationIntegrationTest extends BriarTestCase {
public KeyRotationIntegrationTest() {
contactId = new ContactId(234);
transportId = new TransportId(TestUtils.getRandomId());
transportId = new TransportId("id");
secret0 = new byte[32];
secret1 = new byte[32];
secret2 = new byte[32];

View File

@@ -6,7 +6,6 @@ import static org.junit.Assert.assertArrayEquals;
import java.util.Random;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.ContactId;
import org.briarproject.api.TransportId;
import org.briarproject.api.crypto.CryptoComponent;
@@ -15,7 +14,6 @@ import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.transport.ConnectionContext;
import org.briarproject.api.transport.TemporarySecret;
import org.briarproject.util.ByteUtils;
import org.hamcrest.Description;
import org.jmock.Expectations;
import org.jmock.Mockery;
@@ -26,8 +24,7 @@ import org.junit.Test;
public class TransportConnectionRecogniserTest extends BriarTestCase {
private final ContactId contactId = new ContactId(234);
private final TransportId transportId =
new TransportId(TestUtils.getRandomId());
private final TransportId transportId = new TransportId("id");
@Test
public void testAddAndRemoveSecret() {

View File

@@ -14,7 +14,6 @@ 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;
import org.briarproject.api.crypto.AuthenticatedCipher;
@@ -56,7 +55,7 @@ public class TransportIntegrationTest extends BriarTestCase {
crypto = i.getInstance(CryptoComponent.class);
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);
contactId = new ContactId(234);
transportId = new TransportId(TestUtils.getRandomId());
transportId = new TransportId("id");
frameCipher = crypto.getFrameCipher();
random = new Random();
// Since we're sending frames to ourselves, we only need outgoing keys