Changeset 4b34b49 for apps/streaming
- Timestamp:
- Jul 27, 2015 6:32:53 AM (5 years ago)
- Branches:
- master
- Children:
- db9555d
- Parents:
- 1652bb3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
apps/streaming/java/test/junit/net/i2p/client/streaming/impl/MessageInputStreamTest.java
r1652bb3 r4b34b49 1 1 package net.i2p.client.streaming.impl; 2 3 import static org.hamcrest.Matchers.*; 4 import static org.junit.Assert.*; 5 import static org.mockito.Mockito.*; 2 6 3 7 import java.io.IOException; … … 7 11 import org.junit.After; 8 12 import org.junit.Before; 13 import org.junit.Rule; 9 14 import org.junit.Test; 10 11 import junit.framework.TestCase; 15 import org.mockito.Mock; 16 import org.mockito.junit.MockitoJUnit; 17 import org.mockito.junit.MockitoRule; 18 12 19 import net.i2p.I2PAppContext; 13 20 import net.i2p.data.ByteArray; … … 18 25 * Stress test the MessageInputStream 19 26 */ 20 public class MessageInputStreamTest extends TestCase { 27 public class MessageInputStreamTest { 28 29 @Rule 30 public MockitoRule rule = MockitoJUnit.rule(); 31 21 32 private I2PAppContext _context; 22 33 private Log _log; 23 34 private ConnectionOptions _options; 24 35 private MessageInputStream in; 36 37 @Mock private PacketLocal packetLocal; 25 38 26 39 @Before … … 37 50 public void tearDown() { 38 51 in.close(); 52 } 53 54 @Test 55 public void testGetHighestReadyBlockId() { 56 assertThat(in.getHighestReadyBlockId(), is((long) -1)); 57 in.messageReceived(0, new ByteArray()); 58 assertThat(in.getHighestReadyBlockId(), is((long) 0)); 59 in.messageReceived(2, new ByteArray()); 60 assertThat(in.getHighestReadyBlockId(), is((long) 0)); 61 in.messageReceived(1, new ByteArray()); 62 assertThat(in.getHighestReadyBlockId(), is((long) 2)); 63 } 64 65 @Test 66 public void testGetHighestBlockId() { 67 assertThat(in.getHighestBlockId(), is((long) -1)); 68 in.messageReceived(0, new ByteArray()); 69 assertThat(in.getHighestBlockId(), is((long) 0)); 70 in.messageReceived(2, new ByteArray()); 71 assertThat(in.getHighestBlockId(), is((long) 2)); 72 in.messageReceived(1, new ByteArray()); 73 assertThat(in.getHighestBlockId(), is((long) 2)); 74 } 75 76 @Test 77 public void testCanAccept() { 78 // Can always accept packets with no data 79 assertTrue(in.canAccept(0, 0)); 80 assertTrue(in.canAccept(0, -1)); 81 82 // Can always accept packets with message ID < MIN_READY_BUFFERS (= 16) 83 assertTrue(in.canAccept(0, 1024)); 84 assertTrue(in.canAccept(1, 1024)); 85 assertTrue(in.canAccept(15, 1024)); 86 } 87 88 @Test 89 public void testCanAccept_locallyClosed() { 90 // Fill the buffer 91 int numMsgs = _options.getInboundBufferSize() / _options.getMaxMessageSize(); 92 byte orig[] = new byte[_options.getInboundBufferSize()]; 93 _context.random().nextBytes(orig); 94 for (int i = 0; i < numMsgs; i++) { 95 byte msg[] = new byte[_options.getMaxMessageSize()]; 96 System.arraycopy(orig, i*_options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize()); 97 in.messageReceived(i, new ByteArray(msg)); 98 } 99 // Check that new messages won't be accepted 100 assertFalse(in.canAccept(numMsgs, 1)); 101 // Close 102 in.close(); 103 // Check that new messages will now be accepted 104 assertTrue(in.canAccept(numMsgs, 1)); 105 } 106 107 @Test 108 public void testCanAccept_allMaxSize() { 109 // Fill the buffer to one message under limit with max-size msgs 110 int numMsgs = _options.getInboundBufferSize() / _options.getMaxMessageSize(); 111 byte orig[] = new byte[_options.getInboundBufferSize()]; 112 _context.random().nextBytes(orig); 113 for (int i = 0; i < numMsgs - 1; i++) { 114 byte msg[] = new byte[_options.getMaxMessageSize()]; 115 System.arraycopy(orig, i*_options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize()); 116 in.messageReceived(i, new ByteArray(msg)); 117 } 118 assertTrue(in.canAccept(numMsgs, 1)); 119 byte msg[] = new byte[_options.getMaxMessageSize()]; 120 System.arraycopy(orig, (numMsgs-1)*_options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize()); 121 in.messageReceived(numMsgs - 1, new ByteArray(msg)); 122 assertFalse(in.canAccept(numMsgs, 1)); 123 } 124 125 @Test 126 public void testCanAccept_smallerMsgsWithMaxSizeCount() { 127 // Fill the buffer to one message under count that would reach limit with max-size msgs 128 int numMsgs = _options.getInboundBufferSize() / _options.getMaxMessageSize(); 129 byte orig[] = new byte[numMsgs*1024]; 130 _context.random().nextBytes(orig); 131 for (int i = 0; i < numMsgs - 1; i++) { 132 byte msg[] = new byte[1024]; 133 System.arraycopy(orig, i*1024, msg, 0, 1024); 134 in.messageReceived(i, new ByteArray(msg)); 135 } 136 assertTrue(in.canAccept(numMsgs, 1)); 137 byte msg[] = new byte[1024]; 138 System.arraycopy(orig, (numMsgs-1)*1024, msg, 0, 1024); 139 in.messageReceived(numMsgs - 1, new ByteArray(msg)); 140 assertTrue(in.canAccept(numMsgs, 1)); 141 } 142 143 @Test 144 public void testCanAccept_readyDup() { 145 // Fill the buffer 146 int numMsgs = _options.getInboundBufferSize() / _options.getMaxMessageSize(); 147 byte orig[] = new byte[_options.getInboundBufferSize()]; 148 _context.random().nextBytes(orig); 149 for (int i = 0; i < numMsgs; i++) { 150 byte msg[] = new byte[_options.getMaxMessageSize()]; 151 System.arraycopy(orig, i*_options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize()); 152 in.messageReceived(i, new ByteArray(msg)); 153 } 154 // Check that new messages won't be accepted 155 assertFalse(in.canAccept(numMsgs, 1)); 156 // Check that duplicate messages will be accepted 157 assertTrue(in.canAccept(numMsgs-1, 1)); 158 } 159 160 @Test 161 public void testCanAccept_notReadyDup() { 162 // Fill the buffer 163 int numMsgs = _options.getInboundBufferSize() / _options.getMaxMessageSize(); 164 byte orig[] = new byte[_options.getInboundBufferSize()]; 165 _context.random().nextBytes(orig); 166 for (int i = 0; i < numMsgs; i++) { 167 byte msg[] = new byte[_options.getMaxMessageSize()]; 168 System.arraycopy(orig, i*_options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize()); 169 if (i == numMsgs-1) 170 in.messageReceived(numMsgs, new ByteArray(msg)); 171 else 172 in.messageReceived(i, new ByteArray(msg)); 173 } 174 // Check that duplicate notReady messages will be accepted 175 assertTrue(in.canAccept(numMsgs, 1)); 176 } 177 178 @Test 179 public void testCanAccept_msgIdExceedsBuffer() { 180 // Fill the buffer to one message under limit with max-size msgs 181 int numMsgs = _options.getInboundBufferSize() / _options.getMaxMessageSize(); 182 byte orig[] = new byte[_options.getInboundBufferSize()]; 183 _context.random().nextBytes(orig); 184 for (int i = 0; i < numMsgs - 1; i++) { 185 byte msg[] = new byte[_options.getMaxMessageSize()]; 186 System.arraycopy(orig, i*_options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize()); 187 in.messageReceived(i, new ByteArray(msg)); 188 } 189 // Add one half-size msg 190 byte msg[] = new byte[_options.getMaxMessageSize()/2]; 191 System.arraycopy(orig, (numMsgs-1)*_options.getMaxMessageSize(), msg, 0, _options.getMaxMessageSize()/2); 192 in.messageReceived(numMsgs - 1, new ByteArray(msg)); 193 // Check that it predicts only one more msgId will be accepted 194 assertTrue(in.canAccept(numMsgs, 1)); 195 assertFalse(in.canAccept(numMsgs+1, 1)); 196 } 197 198 @Test 199 public void testCanAccept_inOrderSmallMsgsDoS() { 200 // Fill the buffer to one message under count that would trip DoS protection 201 int numMsgs = 4 * _options.getMaxWindowSize(); 202 byte orig[] = new byte[numMsgs]; 203 _context.random().nextBytes(orig); 204 for (int i = 0; i < numMsgs - 1; i++) { 205 byte msg[] = new byte[1]; 206 System.arraycopy(orig, i, msg, 0, 1); 207 in.messageReceived(i, new ByteArray(msg)); 208 } 209 assertTrue(in.canAccept(numMsgs, 1)); 210 // Trip DoS protection 211 byte msg[] = new byte[1]; 212 System.arraycopy(orig, (numMsgs-1), msg, 0, 1); 213 in.messageReceived(numMsgs - 1, new ByteArray(msg)); 214 assertFalse(in.canAccept(numMsgs, 1)); 215 } 216 217 @Test 218 public void testGetNacks() { 219 assertThat(in.getNacks(), is(nullValue())); 220 in.messageReceived(0, new ByteArray()); 221 assertThat(in.getNacks(), is(nullValue())); 222 in.messageReceived(2, new ByteArray()); 223 assertThat(in.getNacks(), is(equalTo(new long[] {1}))); 224 in.messageReceived(4, new ByteArray()); 225 assertThat(in.getNacks(), is(equalTo(new long[] {1, 3}))); 226 in.messageReceived(1, new ByteArray()); 227 assertThat(in.getNacks(), is(equalTo(new long[] {3}))); 228 in.messageReceived(3, new ByteArray()); 229 assertThat(in.getNacks(), is(nullValue())); 230 } 231 232 @Test 233 public void testUpdateAcks_noMsgs() { 234 in.updateAcks(packetLocal); 235 verify(packetLocal).setAckThrough(-1); 236 verify(packetLocal).setNacks(null); 237 } 238 239 @Test 240 public void testUpdateAcks_inOrderMsgs() { 241 in.messageReceived(0, new ByteArray()); 242 in.messageReceived(1, new ByteArray()); 243 in.messageReceived(2, new ByteArray()); 244 in.updateAcks(packetLocal); 245 verify(packetLocal).setAckThrough(2); 246 verify(packetLocal).setNacks(null); 247 } 248 249 @Test 250 public void testUpdateAcks_missingMsgs() { 251 in.messageReceived(0, new ByteArray()); 252 in.messageReceived(2, new ByteArray()); 253 in.updateAcks(packetLocal); 254 verify(packetLocal).setAckThrough(2); 255 verify(packetLocal).setNacks(new long[] {1}); 256 } 257 258 @Test 259 public void testReadTimeout() { 260 assertThat(in.getReadTimeout(), is(-1)); 261 in.setReadTimeout(100); 262 assertThat(in.getReadTimeout(), is(100)); 39 263 } 40 264
Note: See TracChangeset
for help on using the changeset viewer.