2005-03-01 jrandom
* Really disable the streaming lib packet caching * Synchronized a message handling point in the SDK (even though its use is already essentially single threaded, its better to play it safe) * Don't add new RepublishLeaseSetJobs on failure, just requeue up the existing one (duh) * Throttle the number of concurrent pending tunnel builds across all pools, in addition to simply throttling the number of new requests per minute for each pool individually. This should avoid the cascading failure when tunnel builds take too long, as no new builds will be created until the previous ones are handled. * Factored out and extended the DataHelper's unit tests for dealing with long and date formatting. * Explicitly specify the HTTP auth realm as "i2prouter", though this alone doesn't address the bug where jetty asks for authentication too much. (thanks orion!) * Updated the StreamSinkServer to ignore all read bytes, rather than write them to the filesystem.
This commit is contained in:
@ -78,7 +78,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
|
||||
/** class that generates new messages */
|
||||
protected I2CPMessageProducer _producer;
|
||||
/** map of integer --> MessagePayloadMessage */
|
||||
Map _availableMessages;
|
||||
private Map _availableMessages;
|
||||
|
||||
protected I2PClientMessageHandlerMap _handlerMap;
|
||||
|
||||
@ -290,7 +290,10 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
|
||||
*
|
||||
*/
|
||||
public byte[] receiveMessage(int msgId) throws I2PSessionException {
|
||||
MessagePayloadMessage msg = (MessagePayloadMessage) _availableMessages.remove(new Integer(msgId));
|
||||
MessagePayloadMessage msg = null;
|
||||
synchronized (_availableMessages) {
|
||||
msg = (MessagePayloadMessage) _availableMessages.remove(new Integer(msgId));
|
||||
}
|
||||
if (msg == null) return null;
|
||||
return msg.getPayload().getUnencryptedData();
|
||||
}
|
||||
@ -339,7 +342,9 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
|
||||
* Recieve a payload message and let the app know its available
|
||||
*/
|
||||
public void addNewMessage(MessagePayloadMessage msg) {
|
||||
_availableMessages.put(new Integer(msg.getMessageId().getMessageId()), msg);
|
||||
synchronized (_availableMessages) {
|
||||
_availableMessages.put(new Integer(msg.getMessageId().getMessageId()), msg);
|
||||
}
|
||||
int id = msg.getMessageId().getMessageId();
|
||||
byte data[] = msg.getPayload().getUnencryptedData();
|
||||
if ((data == null) || (data.length <= 0)) {
|
||||
|
@ -361,7 +361,7 @@ public class DataHelper {
|
||||
*/
|
||||
public static void writeLong(OutputStream rawStream, int numBytes, long value)
|
||||
throws DataFormatException, IOException {
|
||||
|
||||
if (value < 0) throw new DataFormatException("Value is negative (" + value + ")");
|
||||
for (int i = numBytes - 1; i >= 0; i--) {
|
||||
byte cur = (byte)( (value >>> (i*8) ) & 0xFF);
|
||||
rawStream.write(cur);
|
||||
@ -369,6 +369,7 @@ public class DataHelper {
|
||||
}
|
||||
|
||||
public static byte[] toLong(int numBytes, long value) throws IllegalArgumentException {
|
||||
if (value < 0) throw new IllegalArgumentException("Negative value not allowed");
|
||||
byte val[] = new byte[numBytes];
|
||||
toLong(val, 0, numBytes, value);
|
||||
return val;
|
||||
@ -397,43 +398,6 @@ public class DataHelper {
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
for (int i = 0; i <= 0xFF; i++)
|
||||
testLong(1, i);
|
||||
System.out.println("Test 1byte passed");
|
||||
for (long i = 0; i <= 0xFFFF; i++)
|
||||
testLong(2, i);
|
||||
System.out.println("Test 2byte passed");
|
||||
for (long i = 0; i <= 0xFFFFFF; i ++)
|
||||
testLong(3, i);
|
||||
System.out.println("Test 3byte passed");
|
||||
for (long i = 0; i <= 0xFFFFFFFF; i++)
|
||||
testLong(4, i);
|
||||
System.out.println("Test 4byte passed");
|
||||
for (long i = 0; i <= 0xFFFFFFFF; i++)
|
||||
testLong(8, i);
|
||||
System.out.println("Test 8byte passed");
|
||||
}
|
||||
private static void testLong(int numBytes, long value) {
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(numBytes);
|
||||
writeLong(baos, numBytes, value);
|
||||
byte written[] = baos.toByteArray();
|
||||
byte extract[] = toLong(numBytes, value);
|
||||
if (!eq(written, extract))
|
||||
throw new RuntimeException("testLong("+numBytes+","+value+") FAILED");
|
||||
|
||||
long read = fromLong(extract, 0, extract.length);
|
||||
if (read != value)
|
||||
throw new RuntimeException("testLong("+numBytes+","+value+") FAILED on read (" + read + ")");
|
||||
read = readLong(new ByteArrayInputStream(written), numBytes);
|
||||
if (read != value)
|
||||
throw new RuntimeException("testLong("+numBytes+","+value+") FAILED on readLong (" + read + ")");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** Read in a date from the stream as specified by the I2P data structure spec.
|
||||
* A date is an 8 byte unsigned integer in network byte order specifying the number of
|
||||
* milliseconds since midnight on January 1, 1970 in the GMT timezone. If the number is
|
||||
|
160
core/java/test/net/i2p/data/DataHelperTest.java
Normal file
160
core/java/test/net/i2p/data/DataHelperTest.java
Normal file
@ -0,0 +1,160 @@
|
||||
package net.i2p.data;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
/**
|
||||
* basic unit tests for the DataHelper
|
||||
*
|
||||
*/
|
||||
public class DataHelperTest {
|
||||
private I2PAppContext _context;
|
||||
private Log _log;
|
||||
|
||||
public DataHelperTest(I2PAppContext ctx) {
|
||||
_context = ctx;
|
||||
_log = ctx.logManager().getLog(DataHelperTest.class);
|
||||
}
|
||||
|
||||
public void runTests() {
|
||||
// long (read/write/to/from)
|
||||
testLong();
|
||||
// date (read/write/to/from)
|
||||
testDate();
|
||||
// string
|
||||
// properties
|
||||
// boolean
|
||||
// readline
|
||||
// compress
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to/from/read/writeLong with every 1, 2, and 4 byte value, as
|
||||
* well as some 8 byte values.
|
||||
*/
|
||||
public void testLong() {
|
||||
for (int i = 0; i <= 0xFF; i++)
|
||||
testLong(1, i);
|
||||
System.out.println("Test 1byte passed");
|
||||
for (long i = 0; i <= 0xFFFF; i++)
|
||||
testLong(2, i);
|
||||
System.out.println("Test 2byte passed");
|
||||
for (long i = 0; i <= 0xFFFFFF; i ++)
|
||||
testLong(3, i);
|
||||
System.out.println("Test 3byte passed");
|
||||
for (long i = 0; i <= 0xFFFFFFFF; i++)
|
||||
testLong(4, i);
|
||||
System.out.println("Test 4byte passed");
|
||||
// i know, doesnt test (2^63)-(2^64-1)
|
||||
for (long i = Long.MAX_VALUE - 0xFFFFFFl; i < Long.MAX_VALUE; i++)
|
||||
testLong(8, i);
|
||||
System.out.println("Test 8byte passed");
|
||||
}
|
||||
private static void testLong(int numBytes, long value) {
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(numBytes);
|
||||
DataHelper.writeLong(baos, numBytes, value);
|
||||
byte written[] = baos.toByteArray();
|
||||
byte extract[] = DataHelper.toLong(numBytes, value);
|
||||
if (extract.length != numBytes)
|
||||
throw new RuntimeException("testLong("+numBytes+","+value+") FAILED (len="+extract.length+")");
|
||||
if (!DataHelper.eq(written, extract))
|
||||
throw new RuntimeException("testLong("+numBytes+","+value+") FAILED");
|
||||
byte extract2[] = new byte[numBytes];
|
||||
DataHelper.toLong(extract2, 0, numBytes, value);
|
||||
if (!DataHelper.eq(extract, extract2))
|
||||
throw new RuntimeException("testLong("+numBytes+","+value+") FAILED on toLong");
|
||||
|
||||
long read = DataHelper.fromLong(extract, 0, numBytes);
|
||||
if (read != value)
|
||||
throw new RuntimeException("testLong("+numBytes+","+value+") FAILED on read (" + read + ")");
|
||||
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(written);
|
||||
read = DataHelper.readLong(bais, numBytes);
|
||||
if (read != value)
|
||||
throw new RuntimeException("testLong("+numBytes+","+value+") FAILED on readLong (" + read + ")");
|
||||
read = DataHelper.fromLong(written, 0, numBytes);
|
||||
if (read != value)
|
||||
throw new RuntimeException("testLong("+numBytes+","+value+") FAILED on fromLong (" + read + ")");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("test(" + numBytes +","+ value +"): " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void testDate() {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
||||
cal.set(Calendar.YEAR, 1970);
|
||||
cal.set(Calendar.MONTH, 0);
|
||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 0);
|
||||
cal.set(Calendar.MINUTE, 0);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
testDate(cal.getTime());
|
||||
|
||||
cal.set(Calendar.SECOND, 1);
|
||||
testDate(cal.getTime());
|
||||
|
||||
cal.set(Calendar.YEAR, 1999);
|
||||
cal.set(Calendar.MONTH, 11);
|
||||
cal.set(Calendar.DAY_OF_MONTH, 31);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 23);
|
||||
cal.set(Calendar.MINUTE, 59);
|
||||
cal.set(Calendar.SECOND, 59);
|
||||
testDate(cal.getTime());
|
||||
|
||||
cal.set(Calendar.YEAR, 2000);
|
||||
cal.set(Calendar.MONTH, 0);
|
||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 0);
|
||||
cal.set(Calendar.MINUTE, 0);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
testDate(cal.getTime());
|
||||
|
||||
cal.setTimeInMillis(System.currentTimeMillis());
|
||||
testDate(cal.getTime());
|
||||
|
||||
cal.set(Calendar.SECOND, cal.get(Calendar.SECOND)+10);
|
||||
testDate(cal.getTime());
|
||||
|
||||
try {
|
||||
cal.set(Calendar.YEAR, 1969);
|
||||
cal.set(Calendar.MONTH, 11);
|
||||
cal.set(Calendar.DAY_OF_MONTH, 31);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 23);
|
||||
cal.set(Calendar.MINUTE, 59);
|
||||
cal.set(Calendar.SECOND, 59);
|
||||
testDate(cal.getTime());
|
||||
System.err.println("foo! this should fail");
|
||||
} catch (RuntimeException re) {
|
||||
// should fail on dates prior to the epoch
|
||||
}
|
||||
}
|
||||
|
||||
private void testDate(Date when) {
|
||||
try {
|
||||
byte buf[] = new byte[DataHelper.DATE_LENGTH];
|
||||
DataHelper.toDate(buf, 0, when.getTime());
|
||||
byte tbuf[] = DataHelper.toDate(when);
|
||||
if (!DataHelper.eq(tbuf, buf))
|
||||
throw new RuntimeException("testDate("+when.toString()+") failed on toDate");
|
||||
Date time = DataHelper.fromDate(buf, 0);
|
||||
if (when.getTime() != time.getTime())
|
||||
throw new RuntimeException("testDate("+when.toString()+") failed (" + time.toString() + ")");
|
||||
System.out.println("eq: " + time);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
DataHelperTest test = new DataHelperTest(I2PAppContext.getGlobalContext());
|
||||
test.runTests();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user