mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
ProtocolIntegrationTest (formerly FileReadWriteTest) doesn't need to
use a file.
This commit is contained in:
@@ -13,8 +13,8 @@
|
|||||||
<path refid='test-classes'/>
|
<path refid='test-classes'/>
|
||||||
<path refid='util-classes'/>
|
<path refid='util-classes'/>
|
||||||
</classpath>
|
</classpath>
|
||||||
<test name='net.sf.briar.FileReadWriteTest'/>
|
|
||||||
<test name='net.sf.briar.LockFairnessTest'/>
|
<test name='net.sf.briar.LockFairnessTest'/>
|
||||||
|
<test name='net.sf.briar.ProtocolIntegrationTest'/>
|
||||||
<test name='net.sf.briar.crypto.CounterModeTest'/>
|
<test name='net.sf.briar.crypto.CounterModeTest'/>
|
||||||
<test name='net.sf.briar.crypto.CryptoComponentTest'/>
|
<test name='net.sf.briar.crypto.CryptoComponentTest'/>
|
||||||
<test name='net.sf.briar.crypto.SharedSecretTest'/>
|
<test name='net.sf.briar.crypto.SharedSecretTest'/>
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package net.sf.briar;
|
package net.sf.briar;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.security.KeyPair;
|
import java.security.KeyPair;
|
||||||
@@ -51,17 +50,12 @@ import net.sf.briar.protocol.writers.ProtocolWritersModule;
|
|||||||
import net.sf.briar.serial.SerialModule;
|
import net.sf.briar.serial.SerialModule;
|
||||||
import net.sf.briar.transport.TransportModule;
|
import net.sf.briar.transport.TransportModule;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.google.inject.Guice;
|
import com.google.inject.Guice;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
|
|
||||||
public class FileReadWriteTest extends TestCase {
|
public class ProtocolIntegrationTest extends TestCase {
|
||||||
|
|
||||||
private final File testDir = TestUtils.getTestDirectory();
|
|
||||||
private final File file = new File(testDir, "foo");
|
|
||||||
|
|
||||||
private final BatchId ack = new BatchId(TestUtils.getRandomId());
|
private final BatchId ack = new BatchId(TestUtils.getRandomId());
|
||||||
private final long timestamp = System.currentTimeMillis();
|
private final long timestamp = System.currentTimeMillis();
|
||||||
@@ -81,12 +75,12 @@ public class FileReadWriteTest extends TestCase {
|
|||||||
private final String messageBody = "Hello world";
|
private final String messageBody = "Hello world";
|
||||||
private final Map<String, Map<String, String>> transports;
|
private final Map<String, Map<String, String>> transports;
|
||||||
|
|
||||||
public FileReadWriteTest() throws Exception {
|
public ProtocolIntegrationTest() throws Exception {
|
||||||
super();
|
super();
|
||||||
Injector i = Guice.createInjector(new CryptoModule(),
|
Injector i = Guice.createInjector(new CryptoModule(),
|
||||||
new DatabaseModule(), new ProtocolModule(),
|
new DatabaseModule(), new ProtocolModule(),
|
||||||
new ProtocolWritersModule(), new SerialModule(),
|
new ProtocolWritersModule(), new SerialModule(),
|
||||||
new TestDatabaseModule(testDir), new TransportModule());
|
new TestDatabaseModule(), new TransportModule());
|
||||||
connectionReaderFactory = i.getInstance(ConnectionReaderFactory.class);
|
connectionReaderFactory = i.getInstance(ConnectionReaderFactory.class);
|
||||||
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);
|
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);
|
||||||
protocolReaderFactory = i.getInstance(ProtocolReaderFactory.class);
|
protocolReaderFactory = i.getInstance(ProtocolReaderFactory.class);
|
||||||
@@ -124,67 +118,59 @@ public class FileReadWriteTest extends TestCase {
|
|||||||
Collections.singletonMap("bar", "baz"));
|
Collections.singletonMap("bar", "baz"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
testDir.mkdirs();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWriteAndRead() throws Exception {
|
public void testWriteAndRead() throws Exception {
|
||||||
write();
|
read(write());
|
||||||
read();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void write() throws Exception {
|
private byte[] write() throws Exception {
|
||||||
OutputStream out = new FileOutputStream(file);
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
// Use Alice's secret for writing
|
// Use Alice's secret for writing
|
||||||
ConnectionWriter w = connectionWriterFactory.createConnectionWriter(out,
|
ConnectionWriter w = connectionWriterFactory.createConnectionWriter(out,
|
||||||
Long.MAX_VALUE, true, transportId, connection, aliceSecret);
|
Long.MAX_VALUE, true, transportId, connection, aliceSecret);
|
||||||
out = w.getOutputStream();
|
OutputStream out1 = w.getOutputStream();
|
||||||
|
|
||||||
AckWriter a = protocolWriterFactory.createAckWriter(out);
|
AckWriter a = protocolWriterFactory.createAckWriter(out1);
|
||||||
assertTrue(a.writeBatchId(ack));
|
assertTrue(a.writeBatchId(ack));
|
||||||
a.finish();
|
a.finish();
|
||||||
|
|
||||||
BatchWriter b = protocolWriterFactory.createBatchWriter(out);
|
BatchWriter b = protocolWriterFactory.createBatchWriter(out1);
|
||||||
assertTrue(b.writeMessage(message.getBytes()));
|
assertTrue(b.writeMessage(message.getBytes()));
|
||||||
assertTrue(b.writeMessage(message1.getBytes()));
|
assertTrue(b.writeMessage(message1.getBytes()));
|
||||||
assertTrue(b.writeMessage(message2.getBytes()));
|
assertTrue(b.writeMessage(message2.getBytes()));
|
||||||
assertTrue(b.writeMessage(message3.getBytes()));
|
assertTrue(b.writeMessage(message3.getBytes()));
|
||||||
b.finish();
|
b.finish();
|
||||||
|
|
||||||
OfferWriter o = protocolWriterFactory.createOfferWriter(out);
|
OfferWriter o = protocolWriterFactory.createOfferWriter(out1);
|
||||||
assertTrue(o.writeMessageId(message.getId()));
|
assertTrue(o.writeMessageId(message.getId()));
|
||||||
assertTrue(o.writeMessageId(message1.getId()));
|
assertTrue(o.writeMessageId(message1.getId()));
|
||||||
assertTrue(o.writeMessageId(message2.getId()));
|
assertTrue(o.writeMessageId(message2.getId()));
|
||||||
assertTrue(o.writeMessageId(message3.getId()));
|
assertTrue(o.writeMessageId(message3.getId()));
|
||||||
o.finish();
|
o.finish();
|
||||||
|
|
||||||
RequestWriter r = protocolWriterFactory.createRequestWriter(out);
|
RequestWriter r = protocolWriterFactory.createRequestWriter(out1);
|
||||||
BitSet requested = new BitSet(4);
|
BitSet requested = new BitSet(4);
|
||||||
requested.set(1);
|
requested.set(1);
|
||||||
requested.set(3);
|
requested.set(3);
|
||||||
r.writeRequest(requested, 4);
|
r.writeRequest(requested, 4);
|
||||||
|
|
||||||
SubscriptionWriter s =
|
SubscriptionWriter s =
|
||||||
protocolWriterFactory.createSubscriptionWriter(out);
|
protocolWriterFactory.createSubscriptionWriter(out1);
|
||||||
// Use a LinkedHashMap for predictable iteration order
|
// Use a LinkedHashMap for predictable iteration order
|
||||||
Map<Group, Long> subs = new LinkedHashMap<Group, Long>();
|
Map<Group, Long> subs = new LinkedHashMap<Group, Long>();
|
||||||
subs.put(group, 0L);
|
subs.put(group, 0L);
|
||||||
subs.put(group1, 0L);
|
subs.put(group1, 0L);
|
||||||
s.writeSubscriptions(subs, timestamp);
|
s.writeSubscriptions(subs, timestamp);
|
||||||
|
|
||||||
TransportWriter t = protocolWriterFactory.createTransportWriter(out);
|
TransportWriter t = protocolWriterFactory.createTransportWriter(out1);
|
||||||
t.writeTransports(transports, timestamp);
|
t.writeTransports(transports, timestamp);
|
||||||
|
|
||||||
out.close();
|
out1.close();
|
||||||
assertTrue(file.exists());
|
return out.toByteArray();
|
||||||
assertTrue(file.length() > message.getSize());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void read() throws Exception {
|
private void read(byte[] connection) throws Exception {
|
||||||
|
InputStream in = new ByteArrayInputStream(connection);
|
||||||
InputStream in = new FileInputStream(file);
|
|
||||||
byte[] iv = new byte[16];
|
byte[] iv = new byte[16];
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
while(offset < 16) {
|
while(offset < 16) {
|
||||||
@@ -256,11 +242,6 @@ public class FileReadWriteTest extends TestCase {
|
|||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
TestUtils.deleteTestDirectory(testDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkMessageEquality(Message m1, Message m2) {
|
private void checkMessageEquality(Message m1, Message m2) {
|
||||||
assertEquals(m1.getId(), m2.getId());
|
assertEquals(m1.getId(), m2.getId());
|
||||||
assertEquals(m1.getParent(), m2.getParent());
|
assertEquals(m1.getParent(), m2.getParent());
|
||||||
@@ -14,6 +14,10 @@ public class TestDatabaseModule extends AbstractModule {
|
|||||||
private final File dir;
|
private final File dir;
|
||||||
private final Password password;
|
private final Password password;
|
||||||
|
|
||||||
|
public TestDatabaseModule() {
|
||||||
|
this(new File("."));
|
||||||
|
}
|
||||||
|
|
||||||
public TestDatabaseModule(File dir) {
|
public TestDatabaseModule(File dir) {
|
||||||
this.dir = dir;
|
this.dir = dir;
|
||||||
this.password = new Password() {
|
this.password = new Password() {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
|
|||||||
import static net.sf.briar.api.transport.TransportConstants.MIN_CONNECTION_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.MIN_CONNECTION_LENGTH;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import net.sf.briar.TestDatabaseModule;
|
import net.sf.briar.TestDatabaseModule;
|
||||||
@@ -31,7 +30,7 @@ public class ConnectionWriterTest extends TestCase {
|
|||||||
super();
|
super();
|
||||||
Injector i = Guice.createInjector(new CryptoModule(),
|
Injector i = Guice.createInjector(new CryptoModule(),
|
||||||
new DatabaseModule(), new ProtocolModule(), new SerialModule(),
|
new DatabaseModule(), new ProtocolModule(), new SerialModule(),
|
||||||
new TestDatabaseModule(new File(".")), new TransportModule());
|
new TestDatabaseModule(), new TransportModule());
|
||||||
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);
|
connectionWriterFactory = i.getInstance(ConnectionWriterFactory.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user