2005-03-23 Comwiz

* Phase 1 of the unit test bounty completed. (The router build script was modified not to build the router
 tests because of a broken dependancy on the core tests. This should be fixed in
 phase 3 of the unit test bounty.)
This commit is contained in:
comwiz
2005-06-23 02:11:04 +00:00
committed by zzz
parent adeb09576a
commit 440cf2c983
81 changed files with 3306 additions and 1429 deletions

View File

@@ -0,0 +1,37 @@
package net.i2p.client;
/*
* free (adj.): unencumbered; not under the control of others
* Written by jrandom in 2003 and released into the public domain
* with no warranty of any kind, either expressed or implied.
* It probably won't make your computer catch on fire, or eat
* your children, but it might. Use at your own risk.
*
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import net.i2p.client.I2PClientFactory;
import net.i2p.client.I2PClient;
import junit.framework.TestCase;
/**
*
* @author Comwiz
*
*/
public class I2PClientTest extends TestCase {
private I2PClient _client;
public void setUp(){
_client = I2PClientFactory.createClient();
}
public void testI2PClient() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
_client.createDestination(out);
_client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
}
}

View File

@@ -0,0 +1,30 @@
package net.i2p.client;
/*
* free (adj.): unencumbered; not under the control of others
* Written by jrandom in 2003 and released into the public domain
* with no warranty of any kind, either expressed or implied.
* It probably won't make your computer catch on fire, or eat
* your children, but it might. Use at your own risk.
*
*/
import net.i2p.client.datagram.DatagramTest;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* @author Comwiz
*/
public class I2PClientTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite("net.i2p.client.I2PClientTestSuite");
suite.addTestSuite(I2PClientTest.class);
suite.addTestSuite(I2PSessionTest.class);
suite.addTestSuite(DatagramTest.class);
return suite;
}
}

View File

@@ -0,0 +1,98 @@
package net.i2p.client;
/*
* free (adj.): unencumbered; not under the control of others
* Written by jrandom in 2003 and released into the public domain
* with no warranty of any kind, either expressed or implied.
* It probably won't make your computer catch on fire, or eat
* your children, but it might. Use at your own risk.
*
*/
import java.util.HashSet;
import java.util.Set;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import net.i2p.client.I2PSession;
import net.i2p.client.I2PSessionImpl;
import net.i2p.client.I2PSessionImpl2;
import net.i2p.client.I2PSessionException;
import net.i2p.client.I2PClient;
import net.i2p.client.I2PClientFactory;
import net.i2p.client.I2PSessionListener;
import net.i2p.data.Destination;
import net.i2p.I2PAppContext;
import junit.framework.TestCase;
/**
*
* @author Comwiz
*
*/
public class I2PSessionTest extends TestCase implements I2PSessionListener{
private Set _s;
public void setUp(){
}
protected void tearDown() {
System.gc();
}
public void testSendClosedMessage() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
Destination d = I2PClientFactory.createClient().createDestination(out);
I2PSession session = new I2PSessionImpl2(I2PAppContext.getGlobalContext(), new ByteArrayInputStream(out.toByteArray()), null);
boolean error = false;
try{
session.sendMessage(d, out.toByteArray());
}catch(I2PSessionException i2pse){
error = true;
}
assertTrue(error);
}
public void testSendAndRecieve() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
Destination d = I2PClientFactory.createClient().createDestination(out);
I2PSession session = new I2PSessionImpl2(I2PAppContext.getGlobalContext(), new ByteArrayInputStream(out.toByteArray()), null);
session.connect();
session.setSessionListener(this);
_s = new HashSet();
_s.add("a");
_s.add("b");
_s.add("c");
_s.add("d");
session.sendMessage(d, "a".getBytes());
session.sendMessage(d, "b".getBytes());
session.sendMessage(d, "c".getBytes());
session.sendMessage(d, "d".getBytes());
for(int i = 0; (i < 20)&&(!_s.isEmpty()); i++){
Thread.sleep(1000);
}
assertTrue(_s.isEmpty());
}
public void disconnected(I2PSession session){}
public void errorOccurred(I2PSession session, java.lang.String message, java.lang.Throwable error){}
public void messageAvailable(I2PSession session, int msgId, long size){
try{
String x = new String(session.receiveMessage(msgId));
if(_s.contains(x))
_s.remove(x);
}catch(Exception e){
fail();
}
}
public void reportAbuse(I2PSession session, int severity){}
}

View File

@@ -0,0 +1,118 @@
package net.i2p.client.datagram;
/*
* free (adj.): unencumbered; not under the control of others
* Written by jrandom in 2003 and released into the public domain
* with no warranty of any kind, either expressed or implied.
* It probably won't make your computer catch on fire, or eat
* your children, but it might. Use at your own risk.
*
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import net.i2p.client.datagram.I2PDatagramMaker;
import net.i2p.client.datagram.I2PDatagramDissector;
import net.i2p.client.datagram.I2PInvalidDatagramException;
import net.i2p.client.I2PClientFactory;
import net.i2p.client.I2PClient;
import net.i2p.client.I2PSession;
import net.i2p.data.DataHelper;
import net.i2p.data.Hash;
import net.i2p.data.Destination;
import net.i2p.data.DataFormatException;
import net.i2p.crypto.DSAEngine;
import junit.framework.TestCase;
/**
*
* @author Comwiz
*
*/
public class DatagramTest extends TestCase {
private I2PClient _client;
public void setUp(){
}
protected void tearDown() {
System.gc();
}
public void testDatagram() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
I2PClient client = I2PClientFactory.createClient();
Destination d = client.createDestination(out);
I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
I2PDatagramMaker dm = new I2PDatagramMaker(session);
byte[] dg = dm.makeI2PDatagram("What's the deal with 42?".getBytes());
I2PDatagramDissector dd = new I2PDatagramDissector();
dd.loadI2PDatagram(dg);
byte[] x = dd.getPayload();
assertTrue(DataHelper.eq(x, "What's the deal with 42?".getBytes()));
x = dd.extractPayload();
assertTrue(DataHelper.eq(x, "What's the deal with 42?".getBytes()));
assertEquals(d, dd.getSender());
assertEquals(d, dd.extractSender());
}
/*public void testMakeNullDatagram() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
I2PClient client = I2PClientFactory.createClient();
Destination d = client.createDestination(out);
I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
I2PDatagramMaker dm = new I2PDatagramMaker(session);
byte[] dg = dm.makeI2PDatagram(null);
assertNull(dg);
}*/
/*public void testExtractNullDatagram() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
I2PClient client = I2PClientFactory.createClient();
Destination d = client.createDestination(out);
I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
I2PDatagramDissector dd = new I2PDatagramDissector();
dd.loadI2PDatagram(null);
}*/
public void testBadagram() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
I2PClient client = I2PClientFactory.createClient();
Destination d = client.createDestination(out);
I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
DSAEngine dsaEng = DSAEngine.getInstance();
ByteArrayOutputStream dout = new ByteArrayOutputStream();
d.writeBytes(dout);
dsaEng.sign(Hash.FAKE_HASH.toByteArray(), session.getPrivateKey()).writeBytes(dout);
dout.write("blah".getBytes());
byte[] data = dout.toByteArray();
I2PDatagramDissector dd = new I2PDatagramDissector();
dd.loadI2PDatagram(data);
boolean error = false;
try{
dd.getPayload();
}catch(I2PInvalidDatagramException i2pide){
error = true;
}
error = false;
try{
dd.getSender();
}catch(I2PInvalidDatagramException i2pide){
error = true;
}
}
}