mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Store each connection window slot as a database row.
This is less memory-efficient but necessary for the coming forward secrecy changes.
This commit is contained in:
@@ -60,7 +60,7 @@ public class BasicH2Test extends TestCase {
|
||||
// Check that the name can be retrieved using the unique ID
|
||||
assertEquals("foo", getName(uniqueId));
|
||||
}
|
||||
|
||||
|
||||
private void addRow(byte[] uniqueId, String name) throws SQLException {
|
||||
String sql = "INSERT INTO foo (uniqueId, name) VALUES (?, ?)";
|
||||
PreparedStatement ps = null;
|
||||
|
||||
@@ -33,6 +33,7 @@ import net.sf.briar.api.protocol.GroupFactory;
|
||||
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.ProtocolConstants;
|
||||
import net.sf.briar.api.protocol.Transport;
|
||||
import net.sf.briar.api.protocol.TransportId;
|
||||
import net.sf.briar.api.protocol.TransportIndex;
|
||||
@@ -1446,8 +1447,8 @@ public class H2DatabaseTest extends TestCase {
|
||||
remoteIndex);
|
||||
// The connection window should exist and be in the initial state
|
||||
assertNotNull(w);
|
||||
assertEquals(0L, w.getCentre());
|
||||
assertEquals(0, w.getBitmap());
|
||||
long top = ProtocolConstants.CONNECTION_WINDOW_SIZE / 2 - 1;
|
||||
for(long l = 0; l <= top; l++) assertFalse(w.isSeen(l));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
@@ -1465,17 +1466,21 @@ public class H2DatabaseTest extends TestCase {
|
||||
remoteIndex);
|
||||
// The connection window should exist and be in the initial state
|
||||
assertNotNull(w);
|
||||
assertEquals(0L, w.getCentre());
|
||||
assertEquals(0, w.getBitmap());
|
||||
Collection<Long> unseen = w.getUnseen();
|
||||
long top = ProtocolConstants.CONNECTION_WINDOW_SIZE / 2 - 1;
|
||||
assertEquals(top + 1, unseen.size());
|
||||
for(long l = 0; l <= top; l++) {
|
||||
assertFalse(w.isSeen(l));
|
||||
assertTrue(unseen.contains(l));
|
||||
}
|
||||
// Update the connection window and store it
|
||||
w.setSeen(5L);
|
||||
w.setSeen(5);
|
||||
db.setConnectionWindow(txn, contactId, remoteIndex, w);
|
||||
// Check that the connection window was stored
|
||||
w = db.getConnectionWindow(txn, contactId, remoteIndex);
|
||||
assertNotNull(w);
|
||||
assertEquals(6L, w.getCentre());
|
||||
assertTrue(w.isSeen(5L));
|
||||
assertEquals(0x00010000, w.getBitmap());
|
||||
top += 5;
|
||||
for(long l = 0; l <= top; l++) assertEquals(l == 5, w.isSeen(l));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
|
||||
@@ -28,7 +28,7 @@ public class ConsumersTest extends TestCase {
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
crypto = i.getInstance(CryptoComponent.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDigestingConsumer() throws Exception {
|
||||
MessageDigest messageDigest = crypto.getMessageDigest();
|
||||
|
||||
@@ -101,7 +101,7 @@ public class WriterImplTest extends TestCase {
|
||||
@Test
|
||||
public void testWriteFloat32() throws IOException {
|
||||
// http://babbage.cs.qc.edu/IEEE-754/Decimal.html
|
||||
// 1 bit for sign, 8 for exponent, 23 for significand
|
||||
// 1 bit for sign, 8 for exponent, 23 for significand
|
||||
w.writeFloat32(0F); // 0 0 0 -> 0x00000000
|
||||
w.writeFloat32(1F); // 0 127 1 -> 0x3F800000
|
||||
w.writeFloat32(2F); // 0 128 1 -> 0x40000000
|
||||
@@ -118,7 +118,7 @@ public class WriterImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testWriteFloat64() throws IOException {
|
||||
// 1 bit for sign, 11 for exponent, 52 for significand
|
||||
// 1 bit for sign, 11 for exponent, 52 for significand
|
||||
w.writeFloat64(0.0); // 0 0 0 -> 0x0000000000000000
|
||||
w.writeFloat64(1.0); // 0 1023 1 -> 0x3FF0000000000000
|
||||
w.writeFloat64(2.0); // 0 1024 1 -> 0x4000000000000000
|
||||
|
||||
@@ -84,7 +84,7 @@ public class ConnectionDecrypterImplTest extends TestCase {
|
||||
out.write(ciphertextMac);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use a ConnectionDecrypter to decrypt the ciphertext
|
||||
ConnectionDecrypter d = new ConnectionDecrypterImpl(in,
|
||||
ConnectionDecrypter d = new ConnectionDecrypterImpl(in,
|
||||
IvEncoder.encodeIv(initiator, transportIndex, connection),
|
||||
frameCipher, frameKey);
|
||||
// First frame
|
||||
|
||||
@@ -49,7 +49,7 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Transport transport = new Transport(transportId, localIndex,
|
||||
Collections.singletonMap("foo", "bar"));
|
||||
transports = Collections.singletonList(transport);
|
||||
connectionWindow = new ConnectionWindowImpl(0L, 0);
|
||||
connectionWindow = new ConnectionWindowImpl();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -120,8 +120,7 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
// Second time - the IV should no longer be expected
|
||||
assertNull(c.acceptConnection(encryptedIv));
|
||||
// The window should have advanced
|
||||
assertEquals(4L, connectionWindow.getCentre());
|
||||
Collection<Long> unseen = connectionWindow.getUnseenConnectionNumbers();
|
||||
Collection<Long> unseen = connectionWindow.getUnseen();
|
||||
assertEquals(19, unseen.size());
|
||||
for(int i = 0; i < 19; i++) {
|
||||
if(i == 3) continue;
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.util.ByteUtils;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConnectionWindowImplTest extends TestCase {
|
||||
|
||||
private static final long MAX_32_BIT_UNSIGNED = 4294967295L; // 2^32 - 1
|
||||
|
||||
@Test
|
||||
public void testWindowSliding() {
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl(0L, 0);
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl();
|
||||
for(int i = 0; i < 100; i++) {
|
||||
assertFalse(w.isSeen(i));
|
||||
w.setSeen(i);
|
||||
@@ -22,7 +22,7 @@ public class ConnectionWindowImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testWindowJumping() {
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl(0L, 0);
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl();
|
||||
for(int i = 0; i < 100; i += 13) {
|
||||
assertFalse(w.isSeen(i));
|
||||
w.setSeen(i);
|
||||
@@ -32,7 +32,7 @@ public class ConnectionWindowImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testWindowUpperLimit() {
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl(0L, 0);
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl();
|
||||
// Centre is 0, highest value in window is 15
|
||||
w.setSeen(15);
|
||||
// Centre is 16, highest value in window is 31
|
||||
@@ -42,18 +42,22 @@ public class ConnectionWindowImplTest extends TestCase {
|
||||
w.setSeen(48);
|
||||
fail();
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
w = new ConnectionWindowImpl(MAX_32_BIT_UNSIGNED - 1, 0);
|
||||
// Values greater than 2^31 - 1 should never be allowed
|
||||
w.setSeen(MAX_32_BIT_UNSIGNED);
|
||||
// Values greater than 2^32 - 1 should never be allowed
|
||||
Collection<Long> unseen = new ArrayList<Long>();
|
||||
for(int i = 0; i < 32; i++) {
|
||||
unseen.add(ByteUtils.MAX_32_BIT_UNSIGNED - i);
|
||||
}
|
||||
w = new ConnectionWindowImpl(unseen);
|
||||
w.setSeen(ByteUtils.MAX_32_BIT_UNSIGNED);
|
||||
try {
|
||||
w.setSeen(MAX_32_BIT_UNSIGNED + 1);
|
||||
w.setSeen(ByteUtils.MAX_32_BIT_UNSIGNED + 1);
|
||||
fail();
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWindowLowerLimit() {
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl(0L, 0);
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl();
|
||||
// Centre is 0, negative values should never be allowed
|
||||
try {
|
||||
w.setSeen(-1);
|
||||
@@ -83,7 +87,7 @@ public class ConnectionWindowImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testCannotSetSeenTwice() {
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl(0L, 0);
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl();
|
||||
w.setSeen(15);
|
||||
try {
|
||||
w.setSeen(15);
|
||||
@@ -93,9 +97,9 @@ public class ConnectionWindowImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testGetUnseenConnectionNumbers() {
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl(0L, 0);
|
||||
ConnectionWindowImpl w = new ConnectionWindowImpl();
|
||||
// Centre is 0; window should cover 0 to 15, inclusive, with none seen
|
||||
Collection<Long> unseen = w.getUnseenConnectionNumbers();
|
||||
Collection<Long> unseen = w.getUnseen();
|
||||
assertEquals(16, unseen.size());
|
||||
for(int i = 0; i < 16; i++) {
|
||||
assertTrue(unseen.contains(Long.valueOf(i)));
|
||||
@@ -104,7 +108,7 @@ public class ConnectionWindowImplTest extends TestCase {
|
||||
w.setSeen(3);
|
||||
w.setSeen(4);
|
||||
// Centre is 5; window should cover 0 to 20, inclusive, with two seen
|
||||
unseen = w.getUnseenConnectionNumbers();
|
||||
unseen = w.getUnseen();
|
||||
assertEquals(19, unseen.size());
|
||||
for(int i = 0; i < 21; i++) {
|
||||
if(i == 3 || i == 4) {
|
||||
@@ -117,7 +121,7 @@ public class ConnectionWindowImplTest extends TestCase {
|
||||
}
|
||||
w.setSeen(19);
|
||||
// Centre is 20; window should cover 4 to 35, inclusive, with two seen
|
||||
unseen = w.getUnseenConnectionNumbers();
|
||||
unseen = w.getUnseen();
|
||||
assertEquals(30, unseen.size());
|
||||
for(int i = 4; i < 36; i++) {
|
||||
if(i == 4 || i == 19) {
|
||||
|
||||
Reference in New Issue
Block a user