Fix merge problems in TransportPropertyValidatorTest.

This commit is contained in:
akwizgran
2016-03-16 15:37:54 +00:00
parent 89ca0e5a26
commit f20a1e8dfe

View File

@@ -1,86 +1,70 @@
package org.briarproject.properties; package org.briarproject.properties;
import org.briarproject.BriarTestCase; import org.briarproject.BriarTestCase;
import org.junit.Test; import org.briarproject.TestUtils;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.briarproject.api.data.BdfWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import org.briarproject.properties.TransportPropertyValidator;
import org.briarproject.api.properties.TransportProperties;
import org.briarproject.api.TransportId;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.UniqueId;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.DeviceId;
import org.briarproject.api.data.BdfWriter;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.BdfReader;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfEntry;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.system.Clock;
import org.briarproject.api.DeviceId; import org.briarproject.api.DeviceId;
import org.briarproject.api.FormatException; import org.briarproject.api.FormatException;
import org.briarproject.api.TransportId;
import org.briarproject.api.UniqueId;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message; import org.briarproject.api.sync.Message;
import org.briarproject.api.properties.TransportPropertyManager; import org.briarproject.api.sync.MessageId;
import org.briarproject.TestUtils; import org.briarproject.api.system.Clock;
import org.briarproject.util.StringUtils; import org.jmock.Mockery;
import org.junit.Test;
import java.io.IOException;
import static org.briarproject.api.TransportId.MAX_TRANSPORT_ID_LENGTH; import static org.briarproject.api.TransportId.MAX_TRANSPORT_ID_LENGTH;
import static org.briarproject.api.properties.TransportPropertyConstants.MAX_PROPERTIES_PER_TRANSPORT; import static org.briarproject.api.properties.TransportPropertyConstants.MAX_PROPERTIES_PER_TRANSPORT;
import static org.junit.Assert.assertEquals;
import org.jmock.Expectations;
import org.jmock.Mockery;
public class TransportPropertyValidatorTest extends BriarTestCase { public class TransportPropertyValidatorTest extends BriarTestCase {
private TransportId transportId; private final TransportId transportId;
private MessageId messageId; private final DeviceId deviceId;
private GroupId groupId; private final BdfDictionary bdfDictionary;
private ClientId clientId; private final Group group;
private DeviceId deviceId; private final Message message;
private final ClientHelper h;
private final MetadataEncoder e;
private final Clock c;
private final Group g;
private final TransportPropertyValidator tpv; private final TransportPropertyValidator tpv;
private final BdfDictionary d;
public TransportPropertyValidatorTest () { public TransportPropertyValidatorTest() {
transportId = new TransportId("test"); transportId = new TransportId("test");
messageId = new MessageId(TestUtils.getRandomId()); deviceId = new DeviceId(TestUtils.getRandomId());
groupId = new GroupId(TestUtils.getRandomId()); bdfDictionary = new BdfDictionary();
clientId = new ClientId(TestUtils.getRandomId());
deviceId = new DeviceId(TestUtils.getRandomId());
Mockery context = new Mockery(); GroupId groupId = new GroupId(TestUtils.getRandomId());
h = context.mock(ClientHelper.class); ClientId clientId = new ClientId(TestUtils.getRandomId());
e = context.mock(MetadataEncoder.class); byte[] descriptor = TestUtils.getRandomBytes(12);
c = context.mock(Clock.class); group = new Group(groupId, clientId, descriptor);
g = new Group(groupId, clientId, TestUtils.getRandomBytes(12));
d = new BdfDictionary(); MessageId messageId = new MessageId(TestUtils.getRandomId());
tpv = new TransportPropertyValidator( long timestamp = System.currentTimeMillis();
h, e, c); byte[] body = TestUtils.getRandomBytes(123);
message = new Message(messageId, groupId, timestamp, body);
Mockery context = new Mockery();
ClientHelper clientHelper = context.mock(ClientHelper.class);
MetadataEncoder metadataEncoder = context.mock(MetadataEncoder.class);
Clock clock = context.mock(Clock.class);
tpv = new TransportPropertyValidator(clientHelper, metadataEncoder,
clock);
} }
@Test @Test
public void testValidateProperMessage() throws IOException { public void testValidateProperMessage() throws IOException {
BdfDictionary result; BdfList body = BdfList.of(deviceId, transportId.getString(), 4,
final BdfList m = BdfList.of(deviceId, transportId.getString(), 4, d); bdfDictionary);
result = tpv.validateMessage(m, g, 1L); BdfDictionary result = tpv.validateMessage(message, group, body);
assertEquals("test", result.getString("transportId")); assertEquals("test", result.getString("transportId"));
assertEquals(result.getLong("version").longValue(), 4); assertEquals(result.getLong("version").longValue(), 4);
@@ -90,63 +74,64 @@ public class TransportPropertyValidatorTest extends BriarTestCase {
public void testValidateWrongVersionValue() throws IOException { public void testValidateWrongVersionValue() throws IOException {
/* Will create a negative version number */ /* Will create a negative version number */
BdfList m = BdfList.of(deviceId, transportId.getString(), -1, d); BdfList body = BdfList.of(deviceId, transportId.getString(), -1,
tpv.validateMessage(m, g, 1L); bdfDictionary);
tpv.validateMessage(message, group, body);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
public void testValidateWrongVersionType() throws IOException { public void testValidateWrongVersionType() throws IOException {
/* Instead of sending a version number I'm sending a dict */ /* Instead of sending a version number I'm sending a dict */
BdfList m = BdfList.of(deviceId, transportId.getString(), d, d); BdfList body = BdfList.of(deviceId, transportId.getString(),
tpv.validateMessage(m, g, 1L); bdfDictionary, bdfDictionary);
tpv.validateMessage(message, group, body);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
public void testValidateShortDeviceId() throws IOException { public void testValidateShortDeviceId() throws IOException {
/* Will create a Device Id with a short length, getRaw should work */ /* Will create a Device Id with a short length, getRaw should work */
BdfList m = BdfList.of(new byte[UniqueId.LENGTH-1], BdfList body = BdfList.of(new byte[UniqueId.LENGTH - 1],
transportId.getString(), 1, d); transportId.getString(), 1, bdfDictionary);
tpv.validateMessage(m, g, 1L); tpv.validateMessage(message, group, body);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
public void testValidateLongDeviceId() throws IOException { public void testValidateLongDeviceId() throws IOException {
BdfList m = BdfList.of(new byte[UniqueId.LENGTH+1], BdfList body = BdfList.of(new byte[UniqueId.LENGTH + 1],
transportId.getString(), 1, d); transportId.getString(), 1, bdfDictionary);
tpv.validateMessage(m, g, 1L); tpv.validateMessage(message, group, body);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
public void testValidateWrongDeviceId() throws IOException { public void testValidateWrongDeviceId() throws IOException {
BdfList m = BdfList.of(d, transportId.getString(), 1, d); BdfList body = BdfList.of(bdfDictionary, transportId.getString(), 1,
tpv.validateMessage(m, g, 1L); bdfDictionary);
tpv.validateMessage(message, group, body);
} }
@Test(expected =FormatException.class) @Test(expected = FormatException.class)
public void testValidateLongTransportId() throws IOException { public void testValidateLongTransportId() throws IOException {
/* Generate a string or arbitrary length for the transport id*/ /* Generate a string or arbitrary length for the transport id*/
String wrongTransportIdString = TestUtils.createRandomString(MAX_TRANSPORT_ID_LENGTH + 1); String wrongTransportIdString =
BdfList m = BdfList.of(deviceId, wrongTransportIdString, 4, d); TestUtils.createRandomString(MAX_TRANSPORT_ID_LENGTH + 1);
BdfList body = BdfList.of(deviceId, wrongTransportIdString, 4,
tpv.validateMessage(m, g, 1L); bdfDictionary);
tpv.validateMessage(message, group, body);
} }
@Test(expected = FormatException.class) @Test(expected = FormatException.class)
public void testValidateTooManyProperties() throws IOException { public void testValidateTooManyProperties() throws IOException {
/* Generate a big map for the BdfDictionary*/ /* Generate a big map for the BdfDictionary*/
HashMap map = new HashMap(); BdfDictionary d = new BdfDictionary();
for (int i = 0; i < MAX_PROPERTIES_PER_TRANSPORT + 1; i++) for (int i = 0; i < MAX_PROPERTIES_PER_TRANSPORT + 1; i++)
map.put("" + i, "" + i); d.put(String.valueOf(i), i);
BdfDictionary d = new BdfDictionary(map); BdfList body = BdfList.of(deviceId, transportId.getString(), 4, d);
tpv.validateMessage(message, group, body);
BdfList m = BdfList.of(deviceId, transportId.getString(), 4, d);
tpv.validateMessage(m, g, 1L);
} }
} }