mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
Each request packet should contain the unique ID of the offer to which
it responds.
This commit is contained in:
@@ -27,6 +27,7 @@ import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageEncoder;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.ProtocolReader;
|
||||
import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
@@ -78,6 +79,7 @@ public class FileReadWriteTest extends TestCase {
|
||||
private final Message message, message1, message2, message3;
|
||||
private final String authorName = "Alice";
|
||||
private final String messageBody = "Hello world";
|
||||
private final OfferId offerId;
|
||||
private final Map<String, Map<String, String>> transports;
|
||||
|
||||
public FileReadWriteTest() throws Exception {
|
||||
@@ -114,6 +116,7 @@ public class FileReadWriteTest extends TestCase {
|
||||
message3 = messageEncoder.encodeMessage(MessageId.NONE, group1,
|
||||
groupKeyPair.getPrivate(), author, authorKeyPair.getPrivate(),
|
||||
messageBody.getBytes("UTF-8"));
|
||||
offerId = new OfferId(TestUtils.getRandomId());
|
||||
transports = Collections.singletonMap("foo",
|
||||
Collections.singletonMap("bar", "baz"));
|
||||
}
|
||||
@@ -155,7 +158,7 @@ public class FileReadWriteTest extends TestCase {
|
||||
BitSet requested = new BitSet(4);
|
||||
requested.set(1);
|
||||
requested.set(3);
|
||||
r.writeBitmap(requested, 4);
|
||||
r.writeRequest(offerId, requested, 4);
|
||||
packetWriter.finishPacket();
|
||||
|
||||
SubscriptionWriter s =
|
||||
@@ -231,6 +234,7 @@ public class FileReadWriteTest extends TestCase {
|
||||
assertTrue(protocolReader.hasRequest());
|
||||
Request r = protocolReader.readRequest();
|
||||
packetReader.finishPacket();
|
||||
assertEquals(offerId, r.getOfferId());
|
||||
BitSet requested = r.getBitmap();
|
||||
assertFalse(requested.get(0));
|
||||
assertTrue(requested.get(1));
|
||||
|
||||
@@ -12,7 +12,7 @@ import org.junit.Test;
|
||||
public class DatabaseCleanerImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testStoppingCleanerWakesItUp() throws DbException {
|
||||
public void testStoppingCleanerWakesItUp() throws Exception {
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
Callback callback = new Callback() {
|
||||
|
||||
@@ -31,11 +31,7 @@ public class DatabaseCleanerImplTest extends TestCase {
|
||||
long start = System.currentTimeMillis();
|
||||
// Start the cleaner and check that shouldCheckFreeSpace() is called
|
||||
cleaner.startCleaning();
|
||||
try {
|
||||
assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||
} catch(InterruptedException e) {
|
||||
fail();
|
||||
}
|
||||
assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||
// Stop the cleaner (it should be waiting between sweeps)
|
||||
cleaner.stopCleaning();
|
||||
long end = System.currentTimeMillis();
|
||||
|
||||
@@ -12,10 +12,10 @@ import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.Rating;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DatabaseListener;
|
||||
import net.sf.briar.api.db.DatabaseListener.Event;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.NoSuchContactException;
|
||||
import net.sf.briar.api.db.Status;
|
||||
import net.sf.briar.api.db.DatabaseListener.Event;
|
||||
import net.sf.briar.api.protocol.Ack;
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.Batch;
|
||||
@@ -25,6 +25,7 @@ import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.protocol.writers.AckWriter;
|
||||
@@ -49,6 +50,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
||||
protected final ContactId contactId;
|
||||
protected final GroupId groupId;
|
||||
protected final MessageId messageId, parentId;
|
||||
protected final OfferId offerId;
|
||||
private final long timestamp;
|
||||
private final int size;
|
||||
private final byte[] raw;
|
||||
@@ -65,6 +67,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
||||
groupId = new GroupId(TestUtils.getRandomId());
|
||||
messageId = new MessageId(TestUtils.getRandomId());
|
||||
parentId = new MessageId(TestUtils.getRandomId());
|
||||
offerId = new OfferId(TestUtils.getRandomId());
|
||||
timestamp = System.currentTimeMillis();
|
||||
size = 1234;
|
||||
raw = new byte[size];
|
||||
@@ -1031,7 +1034,9 @@ public abstract class DatabaseComponentTest extends TestCase {
|
||||
will(returnValue(true)); // Visible - do not request message # 1
|
||||
oneOf(database).setStatusSeenIfVisible(txn, contactId, messageId2);
|
||||
will(returnValue(false)); // Not visible - request message # 2
|
||||
oneOf(requestWriter).writeBitmap(expectedRequest, 3);
|
||||
oneOf(offer).getId();
|
||||
will(returnValue(offerId));
|
||||
oneOf(requestWriter).writeRequest(offerId, expectedRequest, 3);
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageEncoder;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.ProtocolReader;
|
||||
import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
@@ -46,6 +47,7 @@ public class ProtocolReadWriteTest extends TestCase {
|
||||
private final Group group;
|
||||
private final Message message;
|
||||
private final String messageBody = "Hello world";
|
||||
private final OfferId offerId;
|
||||
private final BitSet bitSet;
|
||||
private final Map<Group, Long> subscriptions;
|
||||
private final Map<String, Map<String, String>> transports;
|
||||
@@ -62,6 +64,7 @@ public class ProtocolReadWriteTest extends TestCase {
|
||||
MessageEncoder messageEncoder = i.getInstance(MessageEncoder.class);
|
||||
message = messageEncoder.encodeMessage(MessageId.NONE, group,
|
||||
messageBody.getBytes("UTF-8"));
|
||||
offerId = new OfferId(TestUtils.getRandomId());
|
||||
bitSet = new BitSet();
|
||||
bitSet.set(3);
|
||||
bitSet.set(7);
|
||||
@@ -90,7 +93,7 @@ public class ProtocolReadWriteTest extends TestCase {
|
||||
o.finish();
|
||||
|
||||
RequestWriter r = writerFactory.createRequestWriter(out);
|
||||
r.writeBitmap(bitSet, 10);
|
||||
r.writeRequest(offerId, bitSet, 10);
|
||||
|
||||
SubscriptionWriter s = writerFactory.createSubscriptionWriter(out);
|
||||
s.writeSubscriptions(subscriptions);
|
||||
|
||||
@@ -5,8 +5,10 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.util.BitSet;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.FormatException;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
import net.sf.briar.api.serial.ReaderFactory;
|
||||
@@ -38,7 +40,8 @@ public class RequestReaderTest extends TestCase {
|
||||
@Test
|
||||
public void testFormatExceptionIfRequestIsTooLarge() throws Exception {
|
||||
RequestFactory requestFactory = context.mock(RequestFactory.class);
|
||||
RequestReader requestReader = new RequestReader(requestFactory);
|
||||
RequestReader requestReader =
|
||||
new RequestReader(new OfferIdReader(), requestFactory);
|
||||
|
||||
byte[] b = createRequest(true);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
@@ -56,10 +59,12 @@ public class RequestReaderTest extends TestCase {
|
||||
public void testNoFormatExceptionIfRequestIsMaximumSize() throws Exception {
|
||||
final RequestFactory requestFactory =
|
||||
context.mock(RequestFactory.class);
|
||||
RequestReader requestReader = new RequestReader(requestFactory);
|
||||
RequestReader requestReader =
|
||||
new RequestReader(new OfferIdReader(), requestFactory);
|
||||
final Request request = context.mock(Request.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(requestFactory).createRequest(with(any(BitSet.class)));
|
||||
oneOf(requestFactory).createRequest(with(any(OfferId.class)),
|
||||
with(any(BitSet.class)));
|
||||
will(returnValue(request));
|
||||
}});
|
||||
|
||||
@@ -95,8 +100,8 @@ public class RequestReaderTest extends TestCase {
|
||||
// Deserialise the request
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
RequestReader requestReader =
|
||||
new RequestReader(new RequestFactoryImpl());
|
||||
RequestReader requestReader = new RequestReader(new OfferIdReader(),
|
||||
new RequestFactoryImpl());
|
||||
reader.addObjectReader(Tags.REQUEST, requestReader);
|
||||
Request r = reader.readUserDefined(Tags.REQUEST, Request.class);
|
||||
BitSet decoded = r.getBitmap();
|
||||
@@ -115,10 +120,15 @@ public class RequestReaderTest extends TestCase {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedTag(Tags.REQUEST);
|
||||
// Allow one byte for the REQUEST tag, one byte for the BYTES tag, and
|
||||
// five bytes for the length as an int32
|
||||
if(tooBig) w.writeBytes(new byte[Request.MAX_SIZE - 6]);
|
||||
else w.writeBytes(new byte[Request.MAX_SIZE - 7]);
|
||||
w.writeUserDefinedTag(Tags.OFFER_ID);
|
||||
w.writeBytes(new byte[UniqueId.LENGTH]);
|
||||
// Allow one byte for the REQUEST tag, one byte for the OFFER_ID tag,
|
||||
// one byte for the BYTES tag, one byte for the length as a uint7,
|
||||
// UniqueID.LENGTH bytes for the offer ID, one byte for the BYTES tag,
|
||||
// and five bytes for the length as an int32
|
||||
int overhead = UniqueId.LENGTH + 10;
|
||||
if(tooBig) w.writeBytes(new byte[Request.MAX_SIZE - overhead + 1]);
|
||||
else w.writeBytes(new byte[Request.MAX_SIZE - overhead]);
|
||||
assertEquals(tooBig, out.size() > Request.MAX_SIZE);
|
||||
return out.toByteArray();
|
||||
}
|
||||
@@ -127,6 +137,8 @@ public class RequestReaderTest extends TestCase {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedTag(Tags.REQUEST);
|
||||
w.writeUserDefinedTag(Tags.OFFER_ID);
|
||||
w.writeBytes(new byte[UniqueId.LENGTH]);
|
||||
w.writeBytes(bitmap);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import java.io.IOException;
|
||||
import java.util.BitSet;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.protocol.writers.RequestWriter;
|
||||
import net.sf.briar.api.serial.WriterFactory;
|
||||
import net.sf.briar.serial.SerialModule;
|
||||
@@ -18,11 +20,13 @@ import com.google.inject.Injector;
|
||||
public class RequestWriterImplTest extends TestCase {
|
||||
|
||||
private final WriterFactory writerFactory;
|
||||
private final OfferId offerId;
|
||||
|
||||
public RequestWriterImplTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new SerialModule());
|
||||
writerFactory = i.getInstance(WriterFactory.class);
|
||||
offerId = new OfferId(new byte[UniqueId.LENGTH]);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -41,10 +45,14 @@ public class RequestWriterImplTest extends TestCase {
|
||||
b.set(11);
|
||||
b.set(12);
|
||||
b.set(15);
|
||||
r.writeBitmap(b, 16);
|
||||
// Short user tag 10, short bytes with length 2, 0xD959
|
||||
r.writeRequest(offerId, b, 16);
|
||||
// Short user tag 11, short user tag 10, bytes with length 32 as a
|
||||
// uint7, 32 zero bytes, short bytes with length 2, 0xD959
|
||||
byte[] output = out.toByteArray();
|
||||
assertEquals("CA" + "92" + "D959", StringUtils.toHexString(output));
|
||||
assertEquals("CB" + "CA" + "F6" + "20"
|
||||
+ "00000000000000000000000000000000"
|
||||
+ "00000000000000000000000000000000"
|
||||
+ "92" + "D959", StringUtils.toHexString(output));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -62,9 +70,13 @@ public class RequestWriterImplTest extends TestCase {
|
||||
b.set(9);
|
||||
b.set(11);
|
||||
b.set(12);
|
||||
r.writeBitmap(b, 13);
|
||||
// Short user tag 10, short bytes with length 2, 0x59D8
|
||||
r.writeRequest(offerId, b, 13);
|
||||
// Short user tag 11, short user tag 10, bytes with length 32 as a
|
||||
// uint7, 32 zero bytes, short bytes with length 2, 0x59D8
|
||||
byte[] output = out.toByteArray();
|
||||
assertEquals("CA" + "92" + "59D8", StringUtils.toHexString(output));
|
||||
assertEquals("CB" + "CA" + "F6" + "20"
|
||||
+ "00000000000000000000000000000000"
|
||||
+ "00000000000000000000000000000000"
|
||||
+ "92" + "59D8", StringUtils.toHexString(output));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user