Retrieve messages from the database in raw form to avoid creating unnecessary short-lived objects. Added timestamps to headers.

This commit is contained in:
akwizgran
2011-07-14 12:01:35 +01:00
parent d4382fd232
commit 836d30f6df
32 changed files with 202 additions and 197 deletions

View File

@@ -4,6 +4,8 @@ import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Map;
import net.sf.briar.api.serial.Raw;
/**
* An interface for writing a bundle of acknowledgements, subscriptions,
* transport details and batches.
@@ -18,8 +20,8 @@ public interface BundleWriter {
Map<String, String> transports) throws IOException,
GeneralSecurityException;
/** Adds a batch to the bundle and returns its identifier. */
BatchId addBatch(Iterable<Message> messages) throws IOException,
/** Adds a batch of messages to the bundle and returns its identifier. */
BatchId addBatch(Iterable<Raw> messages) throws IOException,
GeneralSecurityException;
/** Finishes writing the bundle. */

View File

@@ -16,4 +16,7 @@ public interface Header {
/** Returns the transport details contained in the header. */
Map<String, String> getTransports();
/** Returns the header's timestamp. */
long getTimestamp();
}

View File

@@ -1,7 +0,0 @@
package net.sf.briar.api.protocol;
public interface MessageFactory {
Message createMessage(MessageId id, MessageId parent, GroupId group,
AuthorId author, long timestamp, byte[] raw);
}

View File

@@ -1,14 +1,14 @@
package net.sf.briar.serial;
package net.sf.briar.api.serial;
import java.util.Arrays;
import net.sf.briar.api.serial.Raw;
class RawImpl implements Raw {
/** A byte array wrapped in the Raw interface. */
public class RawByteArray implements Raw {
private final byte[] bytes;
RawImpl(byte[] bytes) {
public RawByteArray(byte[] bytes) {
this.bytes = bytes;
}

View File

@@ -158,11 +158,11 @@ interface Database<T> {
Set<BatchId> getLostBatches(T txn, ContactId c) throws DbException;
/**
* Returns the message identified by the given ID.
* Returns the message identified by the given ID, in raw format.
* <p>
* Locking: messages read.
*/
Message getMessage(T txn, MessageId m) throws DbException;
byte[] getMessage(T txn, MessageId m) throws DbException;
/**
* Returns the IDs of all messages signed by the given author.

View File

@@ -4,7 +4,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import com.google.inject.Inject;
public class DatabaseCleanerImpl implements DatabaseCleaner, Runnable {
class DatabaseCleanerImpl implements DatabaseCleaner, Runnable {
private final Callback db;
private final int msBetweenSweeps;

View File

@@ -12,7 +12,6 @@ import java.util.logging.Logger;
import net.sf.briar.api.crypto.Password;
import net.sf.briar.api.db.DatabasePassword;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.protocol.MessageFactory;
import com.google.inject.Inject;
@@ -28,9 +27,8 @@ class H2Database extends JdbcDatabase {
private final long maxSize;
@Inject
H2Database(File dir, MessageFactory messageFactory,
@DatabasePassword Password password, long maxSize) {
super(messageFactory, "BINARY(32)", "BIGINT");
H2Database(File dir, @DatabasePassword Password password, long maxSize) {
super("BINARY(32)", "BIGINT");
home = new File(dir, "db");
this.password = password;
url = "jdbc:h2:split:" + home.getPath()

View File

@@ -27,7 +27,6 @@ import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageFactory;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.util.FileUtils;
@@ -157,7 +156,6 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final Logger LOG =
Logger.getLogger(JdbcDatabase.class.getName());
private final MessageFactory messageFactory;
// Different database libraries use different names for certain types
private final String hashType, bigIntType;
private final LinkedList<Connection> connections =
@@ -168,9 +166,7 @@ abstract class JdbcDatabase implements Database<Connection> {
protected abstract Connection createConnection() throws SQLException;
JdbcDatabase(MessageFactory messageFactory, String hashType,
String bigIntType) {
this.messageFactory = messageFactory;
JdbcDatabase(String hashType, String bigIntType) {
this.hashType = hashType;
this.bigIntType = bigIntType;
}
@@ -683,32 +679,25 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Message getMessage(Connection txn, MessageId m) throws DbException {
public byte[] getMessage(Connection txn, MessageId m) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql =
"SELECT parentId, groupId, authorId, timestamp, size, raw"
+ " FROM messages WHERE messageId = ?";
String sql = "SELECT size, raw FROM messages WHERE messageId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes());
rs = ps.executeQuery();
boolean found = rs.next();
assert found;
MessageId parent = new MessageId(rs.getBytes(1));
GroupId group = new GroupId(rs.getBytes(2));
AuthorId author = new AuthorId(rs.getBytes(3));
long timestamp = rs.getLong(4);
int size = rs.getInt(5);
Blob b = rs.getBlob(6);
int size = rs.getInt(1);
Blob b = rs.getBlob(2);
byte[] raw = b.getBytes(1, size);
assert raw.length == size;
boolean more = rs.next();
assert !more;
rs.close();
ps.close();
return messageFactory.createMessage(m, parent, group, author,
timestamp, raw);
return raw;
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);

View File

@@ -25,6 +25,8 @@ import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Header;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.RawByteArray;
import com.google.inject.Inject;
@@ -282,8 +284,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
try {
Txn txn = db.startTransaction();
try {
long capacity = Math.min(b.getRemainingCapacity(),
Batch.MAX_SIZE);
long capacity =
Math.min(b.getRemainingCapacity(), Batch.MAX_SIZE);
Iterator<MessageId> it =
db.getSendableMessages(txn, c, capacity).iterator();
if(!it.hasNext()) {
@@ -291,12 +293,12 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
return false; // No more messages to send
}
sent = new HashSet<MessageId>();
List<Message> messages = new ArrayList<Message>();
List<Raw> messages = new ArrayList<Raw>();
while(it.hasNext()) {
MessageId m = it.next();
Message message = db.getMessage(txn, m);
bytesSent += message.getSize();
messages.add(message);
byte[] message = db.getMessage(txn, m);
bytesSent += message.length;
messages.add(new RawByteArray(message));
sent.add(m);
}
batchId = b.addBatch(messages);

View File

@@ -25,6 +25,8 @@ import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Header;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.RawByteArray;
import com.google.inject.Inject;
@@ -212,8 +214,8 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
synchronized(messageStatusLock) {
Txn txn = db.startTransaction();
try {
long capacity = Math.min(b.getRemainingCapacity(),
Batch.MAX_SIZE);
long capacity =
Math.min(b.getRemainingCapacity(), Batch.MAX_SIZE);
Iterator<MessageId> it =
db.getSendableMessages(txn, c, capacity).iterator();
if(!it.hasNext()) {
@@ -221,13 +223,13 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
return false; // No more messages to send
}
Set<MessageId> sent = new HashSet<MessageId>();
List<Message> messages = new ArrayList<Message>();
List<Raw> messages = new ArrayList<Raw>();
int bytesSent = 0;
while(it.hasNext()) {
MessageId m = it.next();
Message message = db.getMessage(txn, m);
bytesSent += message.getSize();
messages.add(message);
byte[] message = db.getMessage(txn, m);
bytesSent += message.length;
messages.add(new RawByteArray(message));
sent.add(m);
}
BatchId batchId = b.addBatch(messages);

View File

@@ -20,6 +20,7 @@ import javax.swing.UIManager;
import net.sf.briar.api.i18n.FontManager;
import net.sf.briar.util.FileUtils;
// Needs to be public for installer
public class FontManagerImpl implements FontManager {
private static final Logger LOG =

View File

@@ -21,6 +21,7 @@ import net.sf.briar.api.i18n.FontManager;
import net.sf.briar.api.i18n.I18n;
import net.sf.briar.util.FileUtils;
// Needs to be public for installer
public class I18nImpl implements I18n {
/**

View File

@@ -6,7 +6,7 @@ import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Message;
public class BatchFactoryImpl implements BatchFactory {
class BatchFactoryImpl implements BatchFactory {
public Batch createBatch(BatchId id, List<Message> messages) {
return new BatchImpl(id, messages);

View File

@@ -81,12 +81,13 @@ class BundleReaderImpl implements BundleReader {
}
Map<String, String> transports =
r.readMap(String.class, String.class);
long timestamp = r.readInt64();
in.setSigning(false);
// Read and verify the signature
byte[] sig = r.readRaw();
if(!signature.verify(sig)) throw new SignatureException();
// Build and return the header
return headerFactory.createHeader(acks, subs, transports);
return headerFactory.createHeader(acks, subs, transports, timestamp);
}
public Batch getNextBatch() throws IOException, GeneralSecurityException {

View File

@@ -11,7 +11,7 @@ import java.util.Map;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.BundleWriter;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.Writer;
import net.sf.briar.api.serial.WriterFactory;
@@ -59,6 +59,7 @@ class BundleWriterImpl implements BundleWriter {
for(GroupId sub : subs) w.writeRaw(sub);
w.writeListEnd();
w.writeMap(transports);
w.writeInt64(System.currentTimeMillis());
out.setSigning(false);
// Create and write the signature
byte[] sig = signature.sign();
@@ -67,7 +68,7 @@ class BundleWriterImpl implements BundleWriter {
state = State.FIRST_BATCH;
}
public BatchId addBatch(Iterable<Message> messages) throws IOException,
public BatchId addBatch(Iterable<Raw> messages) throws IOException,
GeneralSecurityException {
if(state == State.FIRST_BATCH) {
w.writeListStart();
@@ -81,7 +82,7 @@ class BundleWriterImpl implements BundleWriter {
out.setDigesting(true);
out.setSigning(true);
w.writeListStart();
for(Message m : messages) w.writeRaw(m);
for(Raw message : messages) w.writeRaw(message);
w.writeListEnd();
out.setSigning(false);
// Create and write the signature

View File

@@ -10,5 +10,5 @@ import net.sf.briar.api.protocol.Header;
interface HeaderFactory {
Header createHeader(Set<BatchId> acks, Set<GroupId> subs,
Map<String, String> transports);
Map<String, String> transports, long timestamp);
}

View File

@@ -10,7 +10,7 @@ import net.sf.briar.api.protocol.Header;
class HeaderFactoryImpl implements HeaderFactory {
public Header createHeader(Set<BatchId> acks, Set<GroupId> subs,
Map<String, String> transports) {
return new HeaderImpl(acks, subs, transports);
Map<String, String> transports, long timestamp) {
return new HeaderImpl(acks, subs, transports, timestamp);
}
}

View File

@@ -13,12 +13,14 @@ class HeaderImpl implements Header {
private final Set<BatchId> acks;
private final Set<GroupId> subs;
private final Map<String, String> transports;
private final long timestamp;
HeaderImpl(Set<BatchId> acks, Set<GroupId> subs,
Map<String, String> transports) {
Map<String, String> transports, long timestamp) {
this.acks = acks;
this.subs = subs;
this.transports = transports;
this.timestamp = timestamp;
}
public Set<BatchId> getAcks() {
@@ -32,4 +34,8 @@ class HeaderImpl implements Header {
public Map<String, String> getTransports() {
return transports;
}
public long getTimestamp() {
return timestamp;
}
}

View File

@@ -6,7 +6,7 @@ import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
/** A simple in-memory implementation of a message. */
public class MessageImpl implements Message {
class MessageImpl implements Message {
private final MessageId id, parent;
private final GroupId group;

View File

@@ -5,7 +5,7 @@ import java.io.InputStream;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.ReaderFactory;
public class ReaderFactoryImpl implements ReaderFactory {
class ReaderFactoryImpl implements ReaderFactory {
public Reader createReader(InputStream in) {
return new ReaderImpl(in);

View File

@@ -8,6 +8,7 @@ import java.util.List;
import java.util.Map;
import net.sf.briar.api.serial.FormatException;
import net.sf.briar.api.serial.RawByteArray;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.Tag;
@@ -308,7 +309,7 @@ class ReaderImpl implements Reader {
if(hasFloat32()) return Float.valueOf(readFloat32());
if(hasFloat64()) return Double.valueOf(readFloat64());
if(hasUtf8()) return readUtf8();
if(hasRaw()) return new RawImpl(readRaw());
if(hasRaw()) return new RawByteArray(readRaw());
if(hasList()) return readList();
if(hasMap()) return readMap();
if(hasNull()) {

View File

@@ -5,7 +5,7 @@ import java.io.OutputStream;
import net.sf.briar.api.serial.Writer;
import net.sf.briar.api.serial.WriterFactory;
public class WriterFactoryImpl implements WriterFactory {
class WriterFactoryImpl implements WriterFactory {
public Writer createWriter(OutputStream out) {
return new WriterImpl(out);

View File

@@ -9,6 +9,7 @@ import net.sf.briar.api.setup.SetupParameters;
import net.sf.briar.api.setup.SetupWorkerFactory;
import net.sf.briar.util.FileUtils;
// Needs to be public for installer
public class SetupWorkerFactoryImpl implements SetupWorkerFactory {
private final I18n i18n;

View File

@@ -21,7 +21,8 @@ import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Header;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.protocol.MessageImpl;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.RawByteArray;
import org.jmock.Expectations;
import org.jmock.Mockery;
@@ -37,7 +38,7 @@ public abstract class DatabaseComponentTest extends TestCase {
protected final MessageId messageId, parentId;
private final long timestamp;
private final int size;
private final byte[] body;
private final byte[] raw;
private final Message message;
private final Set<ContactId> contacts;
private final Set<BatchId> acks;
@@ -55,9 +56,9 @@ public abstract class DatabaseComponentTest extends TestCase {
parentId = new MessageId(TestUtils.getRandomId());
timestamp = System.currentTimeMillis();
size = 1234;
body = new byte[size];
message = new MessageImpl(messageId, MessageId.NONE, groupId, authorId,
timestamp, body);
raw = new byte[size];
message = new TestMessage(messageId, MessageId.NONE, groupId, authorId,
timestamp, raw);
contacts = Collections.singleton(contactId);
acks = Collections.singleton(batchId);
subs = Collections.singleton(groupId);
@@ -453,6 +454,7 @@ public abstract class DatabaseComponentTest extends TestCase {
@Test
public void testGenerateBundle() throws Exception {
final long headerSize = 1234L;
final Raw messageRaw = new RawByteArray(raw);
Mockery context = new Mockery();
@SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class);
@@ -482,9 +484,9 @@ public abstract class DatabaseComponentTest extends TestCase {
Batch.MAX_SIZE - headerSize);
will(returnValue(messages));
oneOf(database).getMessage(txn, messageId);
will(returnValue(message));
will(returnValue(raw));
// Add the batch to the bundle
oneOf(bundleWriter).addBatch(Collections.singletonList(message));
oneOf(bundleWriter).addBatch(Collections.singletonList(messageRaw));
will(returnValue(batchId));
// Record the outstanding batch
oneOf(database).addOutstandingBatch(

View File

@@ -23,9 +23,7 @@ import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageFactory;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.protocol.MessageImpl;
import org.jmock.Expectations;
import org.jmock.Mockery;
@@ -49,7 +47,7 @@ public class H2DatabaseTest extends TestCase {
private final MessageId messageId;
private final long timestamp;
private final int size;
private final byte[] body;
private final byte[] raw;
private final Message message;
public H2DatabaseTest() {
@@ -61,10 +59,10 @@ public class H2DatabaseTest extends TestCase {
messageId = new MessageId(TestUtils.getRandomId());
timestamp = System.currentTimeMillis();
size = 1234;
body = new byte[size];
random.nextBytes(body);
message = new MessageImpl(messageId, MessageId.NONE, groupId, authorId,
timestamp, body);
raw = new byte[size];
random.nextBytes(raw);
message = new TestMessage(messageId, MessageId.NONE, groupId, authorId,
timestamp, raw);
}
@Before
@@ -74,10 +72,8 @@ public class H2DatabaseTest extends TestCase {
@Test
public void testPersistence() throws DbException {
MessageFactory messageFactory = new TestMessageFactory();
Database<Connection> db = open(false);
// Create a new database
Database<Connection> db = open(false, messageFactory);
// Store some records
Connection txn = db.startTransaction();
assertFalse(db.containsContact(txn, contactId));
@@ -94,7 +90,7 @@ public class H2DatabaseTest extends TestCase {
db.close();
// Reopen the database
db = open(true, messageFactory);
db = open(true);
// Check that the records are still there
txn = db.startTransaction();
assertTrue(db.containsContact(txn, contactId));
@@ -102,14 +98,8 @@ public class H2DatabaseTest extends TestCase {
assertEquals(Collections.singletonMap("foo", "bar"), transports);
assertTrue(db.containsSubscription(txn, groupId));
assertTrue(db.containsMessage(txn, messageId));
Message m1 = db.getMessage(txn, messageId);
assertEquals(messageId, m1.getId());
assertEquals(MessageId.NONE, m1.getParent());
assertEquals(groupId, m1.getGroup());
assertEquals(authorId, m1.getAuthor());
assertEquals(timestamp, m1.getTimestamp());
assertEquals(size, m1.getSize());
assertTrue(Arrays.equals(body, m1.getBytes()));
byte[] raw1 = db.getMessage(txn, messageId);
assertTrue(Arrays.equals(raw, raw1));
// Delete the records
db.removeContact(txn, contactId);
db.removeMessage(txn, messageId);
@@ -118,7 +108,7 @@ public class H2DatabaseTest extends TestCase {
db.close();
// Repoen the database
db = open(true, messageFactory);
db = open(true);
// Check that the records are gone
txn = db.startTransaction();
assertFalse(db.containsContact(txn, contactId));
@@ -134,8 +124,7 @@ public class H2DatabaseTest extends TestCase {
ContactId contactId1 = new ContactId(2);
ContactId contactId2 = new ContactId(3);
ContactId contactId3 = new ContactId(4);
MessageFactory messageFactory = new TestMessageFactory();
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Create three contacts
Connection txn = db.startTransaction();
@@ -156,14 +145,14 @@ public class H2DatabaseTest extends TestCase {
assertEquals(contactId3, db.addContact(txn, null));
assertTrue(db.containsContact(txn, contactId3));
db.commitTransaction(txn);
db.close();
}
@Test
public void testRatings() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Unknown authors should be unrated
assertEquals(Rating.UNRATED, db.getRating(txn, authorId));
@@ -174,15 +163,13 @@ public class H2DatabaseTest extends TestCase {
txn = db.startTransaction();
assertEquals(Rating.GOOD, db.getRating(txn, authorId));
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testUnsubscribingRemovesMessage() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Subscribe to a group and store a message
Connection txn = db.startTransaction();
@@ -198,14 +185,11 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testSendableMessagesMustBeSendable() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Add a contact, subscribe to a group and store a message
Connection txn = db.startTransaction();
@@ -240,14 +224,11 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testSendableMessagesMustBeNew() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Add a contact, subscribe to a group and store a message
Connection txn = db.startTransaction();
@@ -288,14 +269,11 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testSendableMessagesMustBeSubscribed() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Add a contact, subscribe to a group and store a message
Connection txn = db.startTransaction();
@@ -329,14 +307,11 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testSendableMessagesMustFitCapacity() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Add a contact, subscribe to a group and store a message
Connection txn = db.startTransaction();
@@ -363,15 +338,12 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testBatchesToAck() throws DbException {
BatchId batchId1 = new BatchId(TestUtils.getRandomId());
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Add a contact and some batches to ack
Connection txn = db.startTransaction();
@@ -395,14 +367,11 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testRemoveAckedBatch() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Add a contact, subscribe to a group and store a message
Connection txn = db.startTransaction();
@@ -438,14 +407,11 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testRemoveLostBatch() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Add a contact, subscribe to a group and store a message
Connection txn = db.startTransaction();
@@ -483,7 +449,6 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
@@ -493,9 +458,7 @@ public class H2DatabaseTest extends TestCase {
ids[i] = new BatchId(TestUtils.getRandomId());
}
Set<MessageId> empty = Collections.emptySet();
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Add a contact
Connection txn = db.startTransaction();
@@ -525,7 +488,6 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
@@ -535,9 +497,7 @@ public class H2DatabaseTest extends TestCase {
ids[i] = new BatchId(TestUtils.getRandomId());
}
Set<MessageId> empty = Collections.emptySet();
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Add a contact
Connection txn = db.startTransaction();
@@ -559,18 +519,15 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testGetMessagesByAuthor() throws DbException {
AuthorId authorId1 = new AuthorId(TestUtils.getRandomId());
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
Message message1 = new MessageImpl(messageId1, MessageId.NONE, groupId,
authorId1, timestamp, body);
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Message message1 = new TestMessage(messageId1, MessageId.NONE, groupId,
authorId1, timestamp, raw);
Database<Connection> db = open(false);
// Subscribe to a group and store two messages
Connection txn = db.startTransaction();
@@ -593,7 +550,6 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
@@ -602,16 +558,14 @@ public class H2DatabaseTest extends TestCase {
MessageId childId2 = new MessageId(TestUtils.getRandomId());
MessageId childId3 = new MessageId(TestUtils.getRandomId());
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
Message child1 = new MessageImpl(childId1, messageId, groupId,
authorId, timestamp, body);
Message child2 = new MessageImpl(childId2, messageId, groupId,
authorId, timestamp, body);
Message child1 = new TestMessage(childId1, messageId, groupId,
authorId, timestamp, raw);
Message child2 = new TestMessage(childId2, messageId, groupId,
authorId, timestamp, raw);
// The third child is in a different group
Message child3 = new MessageImpl(childId3, messageId, groupId1,
authorId, timestamp, body);
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Message child3 = new TestMessage(childId3, messageId, groupId1,
authorId, timestamp, raw);
Database<Connection> db = open(false);
// Subscribe to the groups and store the messages
Connection txn = db.startTransaction();
@@ -637,17 +591,14 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testGetOldMessages() throws DbException {
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
Message message1 = new MessageImpl(messageId1, MessageId.NONE, groupId,
authorId, timestamp + 1000, body);
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Message message1 = new TestMessage(messageId1, MessageId.NONE, groupId,
authorId, timestamp + 1000, raw);
Database<Connection> db = open(false);
// Subscribe to a group and store two messages
Connection txn = db.startTransaction();
@@ -674,18 +625,15 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
@Test
public void testGetFreeSpace() throws DbException {
byte[] largeBody = new byte[ONE_MEGABYTE];
for(int i = 0; i < largeBody.length; i++) largeBody[i] = (byte) i;
Message message1 = new MessageImpl(messageId, MessageId.NONE, groupId,
Message message1 = new TestMessage(messageId, MessageId.NONE, groupId,
authorId, timestamp, largeBody);
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Sanity check: there should be enough space on disk for this test
assertTrue(testDir.getFreeSpace() > MAX_SIZE);
@@ -701,17 +649,14 @@ public class H2DatabaseTest extends TestCase {
assertTrue(db.getFreeSpace() < free);
db.close();
context.assertIsSatisfied();
}
@Test
public void testCloseWaitsForCommit() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
final AtomicBoolean transactionFinished = new AtomicBoolean(false);
final AtomicBoolean closed = new AtomicBoolean(false);
final AtomicBoolean error = new AtomicBoolean(false);
final Database<Connection> db = open(false, messageFactory);
final Database<Connection> db = open(false);
// Start a transaction
Connection txn = db.startTransaction();
@@ -746,12 +691,10 @@ public class H2DatabaseTest extends TestCase {
@Test
public void testCloseWaitsForAbort() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
final AtomicBoolean transactionFinished = new AtomicBoolean(false);
final AtomicBoolean closed = new AtomicBoolean(false);
final AtomicBoolean error = new AtomicBoolean(false);
final Database<Connection> db = open(false, messageFactory);
final Database<Connection> db = open(false);
// Start a transaction
Connection txn = db.startTransaction();
@@ -786,9 +729,7 @@ public class H2DatabaseTest extends TestCase {
@Test
public void testUpdateTransports() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
Database<Connection> db = open(false, messageFactory);
Database<Connection> db = open(false);
// Add a contact with some transport details
Connection txn = db.startTransaction();
@@ -813,11 +754,9 @@ public class H2DatabaseTest extends TestCase {
db.commitTransaction(txn);
db.close();
context.assertIsSatisfied();
}
private Database<Connection> open(boolean resume,
MessageFactory messageFactory) throws DbException {
private Database<Connection> open(boolean resume) throws DbException {
final char[] passwordArray = passwordString.toCharArray();
Mockery context = new Mockery();
final Password password = context.mock(Password.class);
@@ -825,8 +764,7 @@ public class H2DatabaseTest extends TestCase {
oneOf(password).getPassword();
will(returnValue(passwordArray));
}});
Database<Connection> db =
new H2Database(testDir, messageFactory, password, MAX_SIZE);
Database<Connection> db = new H2Database(testDir, password, MAX_SIZE);
db.open(resume);
context.assertIsSatisfied();
// The password array should be cleared after use
@@ -839,12 +777,4 @@ public class H2DatabaseTest extends TestCase {
public void tearDown() {
TestUtils.deleteTestDirectory(testDir);
}
private static class TestMessageFactory implements MessageFactory {
public Message createMessage(MessageId id, MessageId parent,
GroupId group, AuthorId author, long timestamp, byte[] raw) {
return new MessageImpl(id, parent, group, author, timestamp, raw);
}
}
}

View File

@@ -0,0 +1,63 @@
package net.sf.briar.db;
import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
class TestMessage implements Message {
private final MessageId id, parent;
private final GroupId group;
private final AuthorId author;
private final long timestamp;
private final byte[] raw;
public TestMessage(MessageId id, MessageId parent, GroupId group,
AuthorId author, long timestamp, byte[] raw) {
this.id = id;
this.parent = parent;
this.group = group;
this.author = author;
this.timestamp = timestamp;
this.raw = raw;
}
public MessageId getId() {
return id;
}
public MessageId getParent() {
return parent;
}
public GroupId getGroup() {
return group;
}
public AuthorId getAuthor() {
return author;
}
public long getTimestamp() {
return timestamp;
}
public int getSize() {
return raw.length;
}
public byte[] getBytes() {
return raw;
}
@Override
public boolean equals(Object o) {
return o instanceof Message && id.equals(((Message)o).getId());
}
@Override
public int hashCode() {
return id.hashCode();
}
}

View File

@@ -34,15 +34,19 @@ import net.sf.briar.api.protocol.MessageEncoder;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.MessageParser;
import net.sf.briar.api.protocol.UniqueId;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.RawByteArray;
import net.sf.briar.api.serial.ReaderFactory;
import net.sf.briar.api.serial.WriterFactory;
import net.sf.briar.serial.ReaderFactoryImpl;
import net.sf.briar.serial.WriterFactoryImpl;
import net.sf.briar.serial.SerialModule;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class BundleReadWriteTest extends TestCase {
private static final String SIGNATURE_ALGO = "SHA256withRSA";
@@ -62,9 +66,8 @@ public class BundleReadWriteTest extends TestCase {
private final String nick = "Foo Bar";
private final String messageBody = "This is the message body! Wooooooo!";
// FIXME: This test should not depend on impls in another component
private final ReaderFactory rf = new ReaderFactoryImpl();
private final WriterFactory wf = new WriterFactoryImpl();
private final ReaderFactory rf;
private final WriterFactory wf;
private final KeyPair keyPair;
private final Signature sig;
@@ -74,6 +77,9 @@ public class BundleReadWriteTest extends TestCase {
public BundleReadWriteTest() throws Exception {
super();
Injector i = Guice.createInjector(new SerialModule());
rf = i.getInstance(ReaderFactory.class);
wf = i.getInstance(WriterFactory.class);
keyPair = KeyPairGenerator.getInstance(KEY_PAIR_ALGO).generateKeyPair();
sig = Signature.getInstance(SIGNATURE_ALGO);
dig = MessageDigest.getInstance(DIGEST_ALGO);
@@ -101,9 +107,10 @@ public class BundleReadWriteTest extends TestCase {
FileOutputStream out = new FileOutputStream(bundle);
BundleWriter w = new BundleWriterImpl(out, wf, keyPair.getPrivate(),
sig, dig, capacity);
Raw messageRaw = new RawByteArray(message.getBytes());
w.addHeader(acks, subs, transports);
w.addBatch(Collections.singleton(message));
w.addBatch(Collections.singleton(messageRaw));
w.finish();
assertTrue(bundle.exists());

View File

@@ -11,6 +11,7 @@ import java.util.Map.Entry;
import junit.framework.TestCase;
import net.sf.briar.api.serial.FormatException;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.RawByteArray;
import net.sf.briar.util.StringUtils;
import org.junit.Test;
@@ -222,7 +223,7 @@ public class ReaderImplTest extends TestCase {
assertNotNull(m);
assertEquals(2, m.size());
assertEquals((byte) 123, m.get("foo"));
Raw raw = new RawImpl(new byte[] {});
Raw raw = new RawByteArray(new byte[] {});
assertTrue(m.containsKey(raw));
assertNull(m.get(raw));
assertTrue(r.eof());
@@ -287,7 +288,7 @@ public class ReaderImplTest extends TestCase {
assertNotNull(m);
assertEquals(2, m.size());
assertEquals((byte) 123, m.get("foo"));
Raw raw = new RawImpl(new byte[] {});
Raw raw = new RawByteArray(new byte[] {});
assertTrue(m.containsKey(raw));
assertNull(m.get(raw));
assertTrue(r.eof());

View File

@@ -9,6 +9,7 @@ import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import net.sf.briar.api.serial.RawByteArray;
import net.sf.briar.util.StringUtils;
import org.junit.Before;
@@ -141,7 +142,7 @@ public class WriterImplTest extends TestCase {
@Test
public void testWriteRawObject() throws IOException {
w.writeRaw(new RawImpl(new byte[] {0, 1, -1, 127, -128}));
w.writeRaw(new RawByteArray(new byte[] {0, 1, -1, 127, -128}));
checkContents("F6" + "05" + "0001FF7F80");
}
@@ -160,7 +161,7 @@ public class WriterImplTest extends TestCase {
// Use LinkedHashMap to get predictable iteration order
Map<Object, Object> m = new LinkedHashMap<Object, Object>();
m.put("foo", Integer.valueOf(123)); // Written as a uint7
m.put(new RawImpl(new byte[] {}), null); // Empty array != null
m.put(new RawByteArray(new byte[] {}), null); // Empty array != null
w.writeMap(m);
checkContents("F4" + "02" + "F703666F6F" + "7B" + "F600" + "F0");
}

View File

@@ -17,7 +17,7 @@ import net.sf.briar.api.i18n.Stri18ng;
import net.sf.briar.ui.wizard.Wizard;
import net.sf.briar.ui.wizard.WizardPanel;
public class PasswordPanel extends WizardPanel {
class PasswordPanel extends WizardPanel {
private static final long serialVersionUID = -1012132977732308293L;

View File

@@ -4,7 +4,7 @@ import net.sf.briar.api.i18n.I18n;
import net.sf.briar.api.i18n.Stri18ng;
import net.sf.briar.ui.wizard.DirectoryChooserPanel;
public class LocationPanel extends DirectoryChooserPanel {
class LocationPanel extends DirectoryChooserPanel {
private static final long serialVersionUID = -8831098591612528860L;

View File

@@ -4,7 +4,7 @@ import net.sf.briar.api.i18n.I18n;
import net.sf.briar.api.i18n.Stri18ng;
import net.sf.briar.ui.wizard.Wizard;
public class SetupWizard extends Wizard {
class SetupWizard extends Wizard {
private static int WIDTH = 400, HEIGHT = 300;