Search in sources :

Example 16 with ByteArray

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));
}
Also used : ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 17 with ByteArray

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));
}
Also used : ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 18 with ByteArray

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()));
}
Also used : ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 19 with ByteArray

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");
}
Also used : ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 20 with ByteArray

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");
}
Also used : ArrayList(java.util.ArrayList) ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Aggregations

ByteArray (net.i2p.data.ByteArray)53 Test (org.junit.Test)14 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)3 Destination (net.i2p.data.Destination)3 InterruptedIOException (java.io.InterruptedIOException)2 SessionKey (net.i2p.data.SessionKey)2 I2NPMessage (net.i2p.data.i2np.I2NPMessage)2 I2NPMessageException (net.i2p.data.i2np.I2NPMessageException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1 CancelledKeyException (java.nio.channels.CancelledKeyException)1 NotYetConnectedException (java.nio.channels.NotYetConnectedException)1 SelectionKey (java.nio.channels.SelectionKey)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 MessageDigest (java.security.MessageDigest)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 I2PException (net.i2p.I2PException)1