2005-09-12 comwiz
* Migrated the router tests to junit
This commit is contained in:
@ -10,10 +10,10 @@ package net.i2p.data.i2np;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.DataFormatException;
|
||||
import net.i2p.data.DataStructure;
|
||||
import net.i2p.data.StructureTest;
|
||||
import net.i2p.data.TestData;
|
||||
import net.i2p.data.RouterInfo;
|
||||
import net.i2p.data.RouterInfoTest;
|
||||
import net.i2p.data.i2np.DatabaseStoreMessage;
|
||||
@ -24,20 +24,18 @@ import net.i2p.util.Clock;
|
||||
*
|
||||
* @author jrandom
|
||||
*/
|
||||
class DatabaseStoreMessageTest extends StructureTest {
|
||||
static {
|
||||
TestData.registerTest(new DatabaseStoreMessageTest(), "DatabaseStoreMessage");
|
||||
}
|
||||
public class DatabaseStoreMessageTest extends StructureTest {
|
||||
public DataStructure createDataStructure() throws DataFormatException {
|
||||
DatabaseStoreMessage msg = new DatabaseStoreMessage(_context);
|
||||
DatabaseStoreMessage msg = new DatabaseStoreMessage(I2PAppContext.getGlobalContext());
|
||||
RouterInfo info = (RouterInfo)new RouterInfoTest().createDataStructure();
|
||||
msg.setKey(info.getIdentity().getHash());
|
||||
msg.setMessageExpiration(Clock.getInstance().now());
|
||||
msg.setUniqueId(42);
|
||||
msg.setUniqueId(666);
|
||||
msg.setRouterInfo(info);
|
||||
return msg;
|
||||
}
|
||||
public DataStructure createStructureToRead() { return new DatabaseStoreMessage(_context); }
|
||||
|
||||
public static void main(String args[]) { TestData.main(new String[] { "test", "i2np.DatabaseStoreMessage", "foo.dat" }); }
|
||||
public DataStructure createStructureToRead() {
|
||||
return new DatabaseStoreMessage(I2PAppContext.getGlobalContext());
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,6 @@ import net.i2p.data.DataStructure;
|
||||
import net.i2p.data.Hash;
|
||||
import net.i2p.data.SessionKey;
|
||||
import net.i2p.data.StructureTest;
|
||||
import net.i2p.data.TestData;
|
||||
import net.i2p.data.TunnelId;
|
||||
import net.i2p.data.i2np.DeliveryInstructions;
|
||||
import net.i2p.util.Log;
|
||||
@ -23,34 +22,30 @@ import net.i2p.util.Log;
|
||||
*
|
||||
* @author jrandom
|
||||
*/
|
||||
class DeliveryInstructionsTest extends StructureTest {
|
||||
private final static Log _log = new Log(DeliveryInstructionsTest.class);
|
||||
static {
|
||||
TestData.registerTest(new DeliveryInstructionsTest(), "DeliveryInstructions");
|
||||
}
|
||||
public class DeliveryInstructionsTest extends StructureTest {
|
||||
public DataStructure createDataStructure() throws DataFormatException {
|
||||
DeliveryInstructions instructions = new DeliveryInstructions();
|
||||
instructions.setDelayRequested(true);
|
||||
instructions.setDelaySeconds(42);
|
||||
instructions.setDeliveryMode(DeliveryInstructions.DELIVERY_MODE_TUNNEL);
|
||||
instructions.setEncrypted(true);
|
||||
SessionKey key = new SessionKey();
|
||||
byte keyData[] = new byte[SessionKey.KEYSIZE_BYTES];
|
||||
for (int i = 0; i < keyData.length; i++)
|
||||
keyData[i] = (byte)i;
|
||||
key.setData(keyData);
|
||||
instructions.setEncryptionKey(key);
|
||||
Hash hash = new Hash();
|
||||
byte hashData[] = new byte[32];
|
||||
for (int i = 0; i < hashData.length; i++)
|
||||
hashData[i] = (byte)(i%32);
|
||||
hash.setData(hashData);
|
||||
instructions.setRouter(hash);
|
||||
TunnelId id = new TunnelId();
|
||||
id.setTunnelId(666);
|
||||
instructions.setTunnelId(id);
|
||||
_log.debug("Instructions created: " + instructions + "\nBase 64: " + instructions.toBase64());
|
||||
return instructions;
|
||||
instructions.setDelayRequested(true);
|
||||
instructions.setDelaySeconds(42);
|
||||
instructions.setDeliveryMode(DeliveryInstructions.DELIVERY_MODE_TUNNEL);
|
||||
instructions.setEncrypted(true);
|
||||
SessionKey key = new SessionKey();
|
||||
byte keyData[] = new byte[SessionKey.KEYSIZE_BYTES];
|
||||
for (int i = 0; i < keyData.length; i++)
|
||||
keyData[i] = (byte)i;
|
||||
key.setData(keyData);
|
||||
instructions.setEncryptionKey(key);
|
||||
Hash hash = new Hash();
|
||||
byte hashData[] = new byte[32];
|
||||
for (int i = 0; i < hashData.length; i++)
|
||||
hashData[i] = (byte)(i%32);
|
||||
hash.setData(hashData);
|
||||
instructions.setRouter(hash);
|
||||
TunnelId id = new TunnelId();
|
||||
id.setTunnelId(666);
|
||||
instructions.setTunnelId(id);
|
||||
|
||||
return instructions;
|
||||
}
|
||||
public DataStructure createStructureToRead() { return new DeliveryInstructions(); }
|
||||
}
|
||||
|
@ -17,64 +17,46 @@ import net.i2p.data.DataFormatException;
|
||||
import net.i2p.data.i2np.DatabaseStoreMessage;
|
||||
import net.i2p.data.i2np.I2NPMessage;
|
||||
import net.i2p.data.i2np.I2NPMessageReader;
|
||||
import net.i2p.util.Log;
|
||||
import net.i2p.router.RouterContext;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Test harness for loading / storing I2NP DatabaseStore message objects
|
||||
*
|
||||
* @author jrandom
|
||||
*/
|
||||
class I2NPMessageReaderTest implements I2NPMessageReader.I2NPMessageEventListener {
|
||||
private final static Log _log = new Log(I2NPMessageReaderTest.class);
|
||||
private static RouterContext _context = new RouterContext(null);
|
||||
public class I2NPMessageReaderTest extends TestCase implements I2NPMessageReader.I2NPMessageEventListener{
|
||||
|
||||
public static void main(String args[]) {
|
||||
I2NPMessageReaderTest test = new I2NPMessageReaderTest();
|
||||
test.runTest();
|
||||
try { Thread.sleep(30*1000); } catch (InterruptedException ie) {}
|
||||
}
|
||||
public void setUp(){}
|
||||
|
||||
public void runTest() {
|
||||
public void testI2NPMessageReader() throws IOException, DataFormatException{
|
||||
InputStream data = getData();
|
||||
test(data);
|
||||
}
|
||||
|
||||
private InputStream getData() {
|
||||
private InputStream getData() throws IOException, DataFormatException{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
DatabaseStoreMessage msg = (DatabaseStoreMessage)new DatabaseStoreMessageTest().createDataStructure();
|
||||
msg.writeBytes(baos);
|
||||
msg.writeBytes(baos);
|
||||
msg.writeBytes(baos);
|
||||
_log.debug("DB Store message in tunnel contains: " + msg);
|
||||
msg.writeBytes(baos);
|
||||
} catch (DataFormatException dfe) {
|
||||
_log.error("Error building data", dfe);
|
||||
} catch (IOException ioe) {
|
||||
_log.error("Error writing stream", ioe);
|
||||
}
|
||||
DatabaseStoreMessage msg = (DatabaseStoreMessage)new DatabaseStoreMessageTest().createDataStructure();
|
||||
msg.writeBytes(baos);
|
||||
msg.writeBytes(baos);
|
||||
msg.writeBytes(baos);
|
||||
msg.writeBytes(baos);
|
||||
return new ByteArrayInputStream(baos.toByteArray());
|
||||
}
|
||||
|
||||
private void test(InputStream in) {
|
||||
_log.debug("Testing the input stream");
|
||||
I2NPMessageReader reader = new I2NPMessageReader(_context, in, this);
|
||||
_log.debug("Created, beginning reading");
|
||||
I2NPMessageReader reader = new I2NPMessageReader(new RouterContext(null), in, this);
|
||||
reader.startReading();
|
||||
_log.debug("Reading commenced");
|
||||
}
|
||||
|
||||
public void disconnected(I2NPMessageReader reader) {
|
||||
_log.debug("Disconnected");
|
||||
}
|
||||
|
||||
public void messageReceived(I2NPMessageReader reader, I2NPMessage message, long msToRead, int size) {
|
||||
_log.debug("Message received: " + message);
|
||||
}
|
||||
|
||||
public void readError(I2NPMessageReader reader, Exception error) {
|
||||
_log.debug("Read error: " + error.getMessage(), error);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user