use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class MessageInputStreamTest method testCanAccept_inOrderSmallMsgsDoS.
@Test
public void testCanAccept_inOrderSmallMsgsDoS() {
// Fill the buffer to one message under count that would trip DoS protection
int numMsgs = 4 * _options.getMaxWindowSize();
byte[] orig = new byte[numMsgs];
_context.random().nextBytes(orig);
for (int i = 0; i < numMsgs - 1; i++) {
byte[] msg = new byte[1];
System.arraycopy(orig, i, msg, 0, 1);
in.messageReceived(i, new ByteArray(msg));
}
assertTrue(in.canAccept(numMsgs, 1));
// Trip DoS protection
byte[] msg = new byte[1];
System.arraycopy(orig, (numMsgs - 1), msg, 0, 1);
in.messageReceived(numMsgs - 1, new ByteArray(msg));
assertFalse(in.canAccept(numMsgs, 1));
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class MessageInputStreamTest method testCanAccept_allMaxSize.
@Test
public void testCanAccept_allMaxSize() {
// Fill the buffer to one message under limit with max-size msgs
int numMsgs = _options.getInboundBufferSize() / _options.getMaxMessageSize();
byte[] orig = new byte[_options.getInboundBufferSize()];
_context.random().nextBytes(orig);
for (int i = 0; i < numMsgs - 1; i++) {
byte[] msg = new byte[_options.getMaxMessageSize()];
System.arraycopy(orig, i * _options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize());
in.messageReceived(i, new ByteArray(msg));
}
assertTrue(in.canAccept(numMsgs, 1));
byte[] msg = new byte[_options.getMaxMessageSize()];
System.arraycopy(orig, (numMsgs - 1) * _options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize());
in.messageReceived(numMsgs - 1, new ByteArray(msg));
assertFalse(in.canAccept(numMsgs, 1));
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class MessageInputStreamTest method testGetNacks.
@Test
public void testGetNacks() {
assertThat(in.getNacks(), is(nullValue()));
in.messageReceived(0, new ByteArray());
assertThat(in.getNacks(), is(nullValue()));
in.messageReceived(2, new ByteArray());
assertThat(in.getNacks(), is(equalTo(new long[] { 1 })));
in.messageReceived(4, new ByteArray());
assertThat(in.getNacks(), is(equalTo(new long[] { 1, 3 })));
in.messageReceived(1, new ByteArray());
assertThat(in.getNacks(), is(equalTo(new long[] { 3 })));
in.messageReceived(3, new ByteArray());
assertThat(in.getNacks(), is(nullValue()));
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class MessageInputStreamTest method testInOrder.
@Test
public void testInOrder() throws IOException {
byte[] orig = new byte[256 * 1024];
_context.random().nextBytes(orig);
for (int i = 0; i < orig.length / 1024; i++) {
byte[] msg = new byte[1024];
System.arraycopy(orig, i * 1024, msg, 0, 1024);
in.messageReceived(i, new ByteArray(msg));
}
byte[] read = new byte[orig.length];
int howMany = DataHelper.read(in, read);
if (howMany != orig.length)
fail("not enough bytes read [" + howMany + "]");
if (!DataHelper.eq(orig, read))
fail("data read is not equal");
_log.info("Passed test: in order");
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class MessageInputStreamTest method testStaggered.
@Test
public void testStaggered() throws IOException {
byte[] orig = new byte[256 * 1024];
byte[] read = new byte[orig.length];
_context.random().nextBytes(orig);
ArrayList<Integer> order = new ArrayList<Integer>(32);
for (int i = 0; i < orig.length / 1024; i++) order.add(new Integer(i));
Collections.shuffle(order);
int offset = 0;
for (int i = 0; i < orig.length / 1024; i++) {
byte[] msg = new byte[1024];
Integer cur = (Integer) order.get(i);
System.arraycopy(orig, cur.intValue() * 1024, msg, 0, 1024);
in.messageReceived(cur.intValue(), new ByteArray(msg));
_log.debug("Injecting " + cur);
if (in.available() > 0) {
int curRead = in.read(read, offset, read.length - offset);
_log.debug("read " + curRead);
if (curRead == -1)
fail("EOF with offset " + offset);
else
offset += curRead;
}
}
if (!DataHelper.eq(orig, read))
fail("data read is not equal");
_log.info("Passed test: staggered");
}
Aggregations