Skip to content
Snippets Groups Projects
SimplexOutgoingSessionTest.java 5.54 KiB
Newer Older
package org.briarproject.messaging;

import java.io.ByteArrayOutputStream;
import java.util.Random;
import java.util.concurrent.Executor;

import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.ContactId;
import org.briarproject.api.TransportId;
import org.briarproject.api.UniqueId;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.messaging.Ack;
import org.briarproject.api.messaging.MessageId;
import org.briarproject.api.messaging.PacketWriterFactory;
import org.briarproject.plugins.ImmediateExecutor;
import org.briarproject.serial.SerialModule;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

public class SimplexOutgoingSessionTest extends BriarTestCase {

	// FIXME: This is an integration test, not a unit test

	private final Mockery context;
	private final DatabaseComponent db;
	private final Executor dbExecutor;
	private final EventBus eventBus;
	private final PacketWriterFactory packetWriterFactory;
	private final TransportId transportId;
	private final MessageId messageId;
	public SimplexOutgoingSessionTest() {
		context = new Mockery();
		db = context.mock(DatabaseComponent.class);
		dbExecutor = new ImmediateExecutor();
		Module testModule = new AbstractModule() {
			@Override
				bind(PacketWriterFactory.class).to(
						PacketWriterFactoryImpl.class);
		Injector i = Guice.createInjector(testModule, new SerialModule());
		eventBus = context.mock(EventBus.class);
		packetWriterFactory = i.getInstance(PacketWriterFactory.class);
		transportId = new TransportId("id");
		messageId = new MessageId(TestUtils.getRandomId());
		new Random().nextBytes(secret);
	}

	@Test
	public void testNothingToSend() throws Exception {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		final SimplexOutgoingSession session = new SimplexOutgoingSession(db,
				dbExecutor, eventBus, packetWriterFactory, contactId,
				transportId, Long.MAX_VALUE, out);
		context.checking(new Expectations() {{
			// Add listener
			oneOf(eventBus).addListener(session);
			// No transport acks to send
			oneOf(db).generateTransportAcks(contactId);
			will(returnValue(null));
			// No transport updates to send
			oneOf(db).generateTransportUpdates(with(contactId),
					with(any(long.class)));
			// No subscription ack to send
			oneOf(db).generateSubscriptionAck(contactId);
			will(returnValue(null));
			// No subscription update to send
			oneOf(db).generateSubscriptionUpdate(with(contactId),
					with(any(long.class)));
			// No retention ack to send
			oneOf(db).generateRetentionAck(contactId);
			will(returnValue(null));
			// No retention update to send
			oneOf(db).generateRetentionUpdate(with(contactId),
					with(any(long.class)));
			// No acks to send
			oneOf(db).generateAck(with(contactId), with(any(int.class)));
			will(returnValue(null));
			oneOf(db).generateBatch(with(contactId), with(any(int.class)),
					with(any(long.class)));
			// Remove listener
			oneOf(eventBus).removeListener(session);
		// Nothing should have been written
		assertEquals(0, out.size());
		context.assertIsSatisfied();
	}

	@Test
	public void testSomethingToSend() throws Exception {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		final SimplexOutgoingSession session = new SimplexOutgoingSession(db,
				dbExecutor, eventBus, packetWriterFactory, contactId,
				transportId, Long.MAX_VALUE, out);
		final byte[] raw = new byte[1234];
		context.checking(new Expectations() {{
			// Add listener
			oneOf(eventBus).addListener(session);
			// No transport acks to send
			oneOf(db).generateTransportAcks(contactId);
			will(returnValue(null));
			// No transport updates to send
			oneOf(db).generateTransportUpdates(with(contactId),
					with(any(long.class)));
			// No subscription ack to send
			oneOf(db).generateSubscriptionAck(contactId);
			will(returnValue(null));
			// No subscription update to send
			oneOf(db).generateSubscriptionUpdate(with(contactId),
					with(any(long.class)));
			// No retention ack to send
			oneOf(db).generateRetentionAck(contactId);
			will(returnValue(null));
			// No retention update to send
			oneOf(db).generateRetentionUpdate(with(contactId),
					with(any(long.class)));
			// One ack to send
			oneOf(db).generateAck(with(contactId), with(any(int.class)));
			will(returnValue(new Ack(Arrays.asList(messageId))));
			// No more acks
			oneOf(db).generateAck(with(contactId), with(any(int.class)));
			will(returnValue(null));
			oneOf(db).generateBatch(with(contactId), with(any(int.class)),
					with(any(long.class)));
			will(returnValue(Arrays.asList(raw)));
			oneOf(db).generateBatch(with(contactId), with(any(int.class)),
					with(any(long.class)));
			// Remove listener
			oneOf(eventBus).removeListener(session);
		// Something should have been written
		assertTrue(out.size() > UniqueId.LENGTH + raw.length);