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

@@ -17,10 +17,7 @@ import net.i2p.data.i2cp.AbuseReason;
*
* @author jrandom
*/
class AbuseReasonTest extends StructureTest {
static {
TestData.registerTest(new AbuseReasonTest(), "AbuseReason");
}
public class AbuseReasonTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
AbuseReason res = new AbuseReason();
res.setReason("Because they're mean");

View File

@@ -17,10 +17,7 @@ import net.i2p.data.i2cp.AbuseSeverity;
*
* @author jrandom
*/
class AbuseSeverityTest extends StructureTest {
static {
TestData.registerTest(new AbuseSeverityTest(), "AbuseSeverity");
}
public class AbuseSeverityTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
AbuseSeverity sev = new AbuseSeverity();
sev.setSeverity(64);

View File

@@ -0,0 +1,28 @@
package net.i2p.data;
/*
* 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 junit.framework.TestCase;
public class Base64Test extends TestCase{
public void testBase64(){
String orig = "you smell";
String encoded = Base64.encode(orig.getBytes());
byte decoded[] = Base64.decode(encoded);
String transformed = new String(decoded);
assertTrue(orig.equals(transformed));
byte all[] = new byte[256];
for (int i = 0; i < all.length; i++)
all[i] = (byte) (0xFF & i);
encoded = Base64.encode(all);
decoded = Base64.decode(encoded);
assertTrue(DataHelper.eq(decoded, all));
}
}

View File

@@ -9,6 +9,7 @@ package net.i2p.data;
*/
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -16,44 +17,30 @@ import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.util.Log;
import junit.framework.TestCase;
/**
* Test harness for the boolean structure
*
* @author jrandom
*/
class BooleanTest implements TestDataGenerator, TestDataPrinter {
static {
TestData.registerGenerator(new BooleanTest(), "Boolean");
TestData.registerPrinter(new BooleanTest(), "Boolean");
}
private static final Log _log = new Log(BooleanTest.class);
public class BooleanTest extends TestCase{
public byte[] getData() {
public void testBoolean() throws Exception{
byte[] temp = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
DataHelper.writeBoolean(baos, Boolean.TRUE);
return baos.toByteArray();
} catch (DataFormatException dfe) {
_log.error("Error writing the boolean", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error writing the boolean", ioe);
return null;
}
DataHelper.writeBoolean(baos, Boolean.TRUE);
temp = baos.toByteArray();
Boolean b = null;
ByteArrayInputStream bais = new ByteArrayInputStream(temp);
b = DataHelper.readBoolean(bais);
assertEquals(Boolean.TRUE, b);
}
public String testData(InputStream inputStream) {
try {
Boolean b = DataHelper.readBoolean(inputStream);
return ""+b;
} catch (DataFormatException dfe) {
_log.error("Error reading the boolean", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error reading the boolean", ioe);
return null;
}
}
}
}

View File

@@ -17,10 +17,7 @@ import net.i2p.data.DataStructure;
*
* @author jrandom
*/
class CertificateTest extends StructureTest {
static {
TestData.registerTest(new CertificateTest(), "Certificate");
}
public class CertificateTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
Certificate cert = new Certificate();
byte data[] = new byte[32];

View File

@@ -21,15 +21,12 @@ import net.i2p.data.i2cp.SessionId;
*
* @author jrandom
*/
class CreateLeaseSetMessageTest extends StructureTest {
static {
TestData.registerTest(new CreateLeaseSetMessageTest(), "CreateLeaseSetMessage");
}
public class CreateLeaseSetMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
CreateLeaseSetMessage msg = new CreateLeaseSetMessage();
msg.setPrivateKey((PrivateKey)(new PrivateKeyTest()).createDataStructure());
msg.setSigningPrivateKey((SigningPrivateKey)(new SigningPrivateKeyTest()).createDataStructure());
msg.setLeaseSet((LeaseSet)(new LeaseSetTest()).createDataStructure());
msg.setPrivateKey((PrivateKey)(new PrivateKeyTest()).createDataStructure());
msg.setSigningPrivateKey((SigningPrivateKey)(new SigningPrivateKeyTest()).createDataStructure());
msg.setLeaseSet((LeaseSet)(new LeaseSetTest()).createDataStructure());
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
return msg;
}

View File

@@ -18,10 +18,7 @@ import net.i2p.data.i2cp.SessionConfig;
*
* @author jrandom
*/
class CreateSessionMessageTest extends StructureTest {
static {
TestData.registerTest(new CreateSessionMessageTest(), "CreateSessionMessage");
}
public class CreateSessionMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
CreateSessionMessage msg = new CreateSessionMessage();
msg.setSessionConfig((SessionConfig)(new SessionConfigTest()).createDataStructure());

View File

@@ -7,88 +7,60 @@ import java.util.Date;
import java.util.TimeZone;
import net.i2p.I2PAppContext;
import net.i2p.util.Log;
import junit.framework.TestCase;
/**
* basic unit tests for the DataHelper
*
*/
public class DataHelperTest {
public class DataHelperTest extends TestCase{
private I2PAppContext _context;
private Log _log;
public DataHelperTest(I2PAppContext ctx) {
_context = ctx;
_log = ctx.logManager().getLog(DataHelperTest.class);
}
public void runTests() {
// compress
testCompress();
// long (read/write/to/from)
testLong();
// date (read/write/to/from)
testDate();
// string
// properties
// boolean
// readline
protected void setUp() {
_context = I2PAppContext.getGlobalContext();
}
/**
* 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");
public void testLong() throws Exception{
for (int i = 0; i <= 0xFF; i+=4)
checkLong(1, i);
for (long i = 0; i <= 0xFFFF; i+=16)
checkLong(2, i);
for (long i = 0; i <= 0xFFFFFF; i +=128)
checkLong(3, i);
for (long i = 0; i <= 0xFFFFFFFFl; i+=512)
checkLong(4, i);
// 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");
checkLong(8, i);
}
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 static void checkLong(int numBytes, long value) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream(numBytes);
DataHelper.writeLong(baos, numBytes, value);
byte written[] = baos.toByteArray();
byte extract[] = DataHelper.toLong(numBytes, value);
assertTrue(extract.length == numBytes);
assertTrue(DataHelper.eq(written, extract));
byte extract2[] = new byte[numBytes];
DataHelper.toLong(extract2, 0, numBytes, value);
assertTrue(DataHelper.eq(extract, extract2));
long read = DataHelper.fromLong(extract, 0, numBytes);
assertTrue(read == value);
ByteArrayInputStream bais = new ByteArrayInputStream(written);
read = DataHelper.readLong(bais, numBytes);
assertTrue(read == value);
read = DataHelper.fromLong(written, 0, numBytes);
assertTrue(read == value);
}
private void testDate() {
public void testDate() throws Exception{
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.set(Calendar.YEAR, 1970);
@@ -97,10 +69,10 @@ public class DataHelperTest {
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
testDate(cal.getTime());
checkDate(cal.getTime());
cal.set(Calendar.SECOND, 1);
testDate(cal.getTime());
checkDate(cal.getTime());
cal.set(Calendar.YEAR, 1999);
cal.set(Calendar.MONTH, 11);
@@ -108,7 +80,7 @@ public class DataHelperTest {
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
testDate(cal.getTime());
checkDate(cal.getTime());
cal.set(Calendar.YEAR, 2000);
cal.set(Calendar.MONTH, 0);
@@ -116,68 +88,47 @@ public class DataHelperTest {
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
testDate(cal.getTime());
checkDate(cal.getTime());
cal.setTimeInMillis(System.currentTimeMillis());
testDate(cal.getTime());
checkDate(cal.getTime());
cal.set(Calendar.SECOND, cal.get(Calendar.SECOND)+10);
testDate(cal.getTime());
checkDate(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
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);
boolean error = false;
try{
checkDate(cal.getTime());
}catch(Exception e){
error = true;
}
assertTrue(error);
}
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());
}
private void checkDate(Date when) throws Exception{
byte buf[] = new byte[DataHelper.DATE_LENGTH];
DataHelper.toDate(buf, 0, when.getTime());
byte tbuf[] = DataHelper.toDate(when);
assertTrue(DataHelper.eq(tbuf, buf));
Date time = DataHelper.fromDate(buf, 0);
assertEquals(when.getTime(), time.getTime());
}
private void testCompress() {
for (int i = 0; i < 32*1024; i++)
testCompress(i);
}
private void testCompress(int size) {
byte data[] = new byte[size];
_context.random().nextBytes(data);
byte compressed[] = DataHelper.compress(data);
try {
public void testCompress() throws Exception{
for (int size = 0; size < 32*1024; size+=32){ // Original had size++, changed value because
// speed was a problem. -Comwiz
byte data[] = new byte[size];
_context.random().nextBytes(data);
byte compressed[] = DataHelper.compress(data);
byte decompressed[] = DataHelper.decompress(compressed);
boolean ok = DataHelper.eq(data, decompressed);
if (!ok)
throw new RuntimeException("failed match at size=" + size);
else
System.out.println("Match at size=" + size);
} catch (java.io.IOException ioe) {
ioe.printStackTrace();
throw new RuntimeException("Error at size=" + size +":" + ioe.getMessage());
assertTrue(DataHelper.eq(data, decompressed));
}
}
public static void main(String args[]) {
DataHelperTest test = new DataHelperTest(I2PAppContext.getGlobalContext());
test.runTests();
}
}

View File

@@ -0,0 +1,76 @@
package net.i2p.data;
/*
* 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.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.DataStructureImpl;
import junit.framework.TestCase;
/**
* @author Comwiz
*/
public class DataStructureImplTest extends TestCase{
DataStructure _struct;
protected void setUp(){
_struct = new DataStructureImpl(){
private int x = 0;
public void writeBytes(OutputStream out) throws IOException, DataFormatException{
if(x++==0)
throw new DataFormatException("let it enfold you", new Exception());
else
throw new IOException();
}
public void readBytes(InputStream in) throws IOException{
throw new IOException();
}
};
}
public void testNulls() throws Exception{
assertNull(_struct.toBase64());
boolean error = false;
try{
_struct.fromBase64(null);
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
assertNull(_struct.calculateHash());
error = false;
try{
_struct.fromByteArray(null);
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testErrors() throws Exception{
boolean error = false;
try{
_struct.fromByteArray("water is poison".getBytes());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
assertNull(_struct.toByteArray());
assertNull(_struct.toByteArray());
}
}

View File

@@ -0,0 +1,56 @@
package net.i2p.data;
import junit.framework.Test;
import junit.framework.TestSuite;
public class DataTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite("net.i2p.data.DataTestSuite");
suite.addTestSuite(AbuseReasonTest.class);
suite.addTestSuite(AbuseSeverityTest.class);
suite.addTestSuite(Base64Test.class);
suite.addTestSuite(BooleanTest.class);
suite.addTestSuite(CertificateTest.class);
suite.addTestSuite(CreateLeaseSetMessageTest.class);
suite.addTestSuite(CreateSessionMessageTest.class);
suite.addTestSuite(DataHelperTest.class);
suite.addTestSuite(DataStructureImplTest.class);
suite.addTestSuite(DateTest.class);
suite.addTestSuite(DestinationTest.class);
suite.addTestSuite(DestroySessionMessageTest.class);
suite.addTestSuite(DisconnectMessageTest.class);
suite.addTestSuite(HashTest.class);
suite.addTestSuite(LeaseSetTest.class);
suite.addTestSuite(LeaseTest.class);
suite.addTestSuite(MappingTest.class);
suite.addTestSuite(MessageIdTest.class);
suite.addTestSuite(MessagePayloadMessageTest.class);
suite.addTestSuite(MessageStatusMessageTest.class);
suite.addTestSuite(PayloadTest.class);
suite.addTestSuite(PrivateKeyTest.class);
suite.addTestSuite(PublicKeyTest.class);
suite.addTestSuite(ReceiveMessageBeginMessageTest.class);
suite.addTestSuite(ReceiveMessageEndMessageTest.class);
suite.addTestSuite(ReportAbuseMessageTest.class);
suite.addTestSuite(RequestLeaseSetMessageTest.class);
suite.addTestSuite(RouterAddressTest.class);
suite.addTestSuite(RouterIdentityTest.class);
suite.addTestSuite(RouterInfoTest.class);
suite.addTestSuite(SendMessageMessageTest.class);
suite.addTestSuite(SessionConfigTest.class);
suite.addTestSuite(SessionIdTest.class);
suite.addTestSuite(SessionKeyTest.class);
suite.addTestSuite(SessionStatusMessageTest.class);
suite.addTestSuite(SignatureTest.class);
suite.addTestSuite(SigningPrivateKeyTest.class);
suite.addTestSuite(SigningPublicKeyTest.class);
suite.addTestSuite(StringTest.class);
suite.addTestSuite(TunnelIdTest.class);
suite.addTestSuite(UnsignedIntegerTest.class);
return suite;
}
}

View File

@@ -9,6 +9,7 @@ package net.i2p.data;
*/
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
@@ -17,44 +18,31 @@ import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.util.Log;
import junit.framework.TestCase;
/**
* Test harness for the boolean structure
* Test harness for the date structure
*
* @author jrandom
*/
class DateTest implements TestDataGenerator, TestDataPrinter {
static {
TestData.registerGenerator(new DateTest(), "Date");
TestData.registerPrinter(new DateTest(), "Date");
}
private static final Log _log = new Log(DateTest.class);
public class DateTest extends TestCase{
public byte[] getData() {
public void testDate() throws Exception{
byte[] temp = null;
Date orig = new Date();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
DataHelper.writeDate(baos, new Date());
return baos.toByteArray();
} catch (DataFormatException dfe) {
_log.error("Error writing the date", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error writing the date", ioe);
return null;
}
DataHelper.writeDate(baos, orig);
temp = baos.toByteArray();
Date d = null;
ByteArrayInputStream bais = new ByteArrayInputStream(temp);
d = DataHelper.readDate(bais);
assertEquals(orig, d);
}
public String testData(InputStream inputStream) {
try {
Date d = DataHelper.readDate(inputStream);
return ""+d;
} catch (DataFormatException dfe) {
_log.error("Error reading the date", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error reading the date", ioe);
return null;
}
}
}
}

View File

@@ -20,10 +20,7 @@ import net.i2p.data.SigningPublicKey;
*
* @author jrandom
*/
class DestinationTest extends StructureTest {
static {
TestData.registerTest(new DestinationTest(), "Destination");
}
public class DestinationTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
Destination dest = new Destination();
StructureTest tst = new CertificateTest();

View File

@@ -18,10 +18,7 @@ import net.i2p.data.i2cp.SessionId;
*
* @author jrandom
*/
class DestroySessionMessageTest extends StructureTest {
static {
TestData.registerTest(new DestroySessionMessageTest(), "DestroySessionMessage");
}
public class DestroySessionMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
DestroySessionMessage msg = new DestroySessionMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());

View File

@@ -17,10 +17,7 @@ import net.i2p.data.i2cp.DisconnectMessage;
*
* @author jrandom
*/
class DisconnectMessageTest extends StructureTest {
static {
TestData.registerTest(new DisconnectMessageTest(), "DisconnectMessage");
}
public class DisconnectMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
DisconnectMessage msg = new DisconnectMessage();
msg.setReason("Because I say so");

View File

@@ -17,10 +17,7 @@ import net.i2p.data.Hash;
*
* @author jrandom
*/
class HashTest extends StructureTest {
static {
TestData.registerTest(new HashTest(), "Hash");
}
public class HashTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
Hash hash = new Hash();
byte data[] = new byte[32];

View File

@@ -21,10 +21,7 @@ import net.i2p.data.SigningPublicKey;
*
* @author jrandom
*/
class LeaseSetTest extends StructureTest {
static {
TestData.registerTest(new LeaseSetTest(), "LeaseSet");
}
public class LeaseSetTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
LeaseSet leaseSet = new LeaseSet();
leaseSet.setDestination((Destination)(new DestinationTest()).createDataStructure());

View File

@@ -9,6 +9,7 @@ package net.i2p.data;
*/
import java.util.Date;
import java.io.ByteArrayOutputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
@@ -21,14 +22,10 @@ import net.i2p.data.TunnelId;
*
* @author jrandom
*/
class LeaseTest extends StructureTest {
static {
TestData.registerTest(new LeaseTest(), "Lease");
}
public class LeaseTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
Lease lease = new Lease();
lease.setEndDate(new Date(1000*60*2));
//lease.setStartDate(new Date(1000*60));
byte h[] = new byte[Hash.HASH_LENGTH];
lease.setGateway(new Hash(h));
StructureTest tst = new TunnelIdTest();
@@ -37,4 +34,68 @@ class LeaseTest extends StructureTest {
return lease;
}
public DataStructure createStructureToRead() { return new Lease(); }
public void testNumSuccessFail() throws Exception{
Lease lease = new Lease();
lease.setEndDate(new Date(1000*60*2));
byte h[] = new byte[Hash.HASH_LENGTH];
lease.setGateway(new Hash(h));
StructureTest tst = new TunnelIdTest();
lease.setTunnelId((TunnelId)tst.createDataStructure());
lease.getNumSuccess();
lease.getNumFailure();
}
public void testExpiration() throws Exception{
Lease lease = new Lease();
assertTrue(lease.isExpired());
lease.setEndDate(new Date(1000*60*2));
byte h[] = new byte[Hash.HASH_LENGTH];
lease.setGateway(new Hash(h));
StructureTest tst = new TunnelIdTest();
lease.setTunnelId((TunnelId)tst.createDataStructure());
assertTrue(lease.isExpired());
}
public void testNullWrite() throws Exception{
Lease lease = new Lease();
lease.setEndDate(new Date(1000*60*2));
byte h[] = new byte[Hash.HASH_LENGTH];
lease.setGateway(new Hash(h));
lease.setTunnelId(null);
boolean error = false;
try{
lease.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
lease = new Lease();
lease.setEndDate(new Date(1000*60*2));
h = new byte[Hash.HASH_LENGTH];
lease.setGateway(null);
StructureTest tst = new TunnelIdTest();
lease.setTunnelId((TunnelId)tst.createDataStructure());
error = false;
try{
lease.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testNullEquals() throws Exception{
Lease lease = new Lease();
lease.setEndDate(new Date(1000*60*2));
byte h[] = new byte[Hash.HASH_LENGTH];
lease.setGateway(new Hash(h));
lease.setTunnelId(null);
assertFalse(lease.equals(null));
}
}

View File

@@ -9,56 +9,42 @@ package net.i2p.data;
*/
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.util.Log;
import junit.framework.TestCase;
/**
* Test harness for the mapping structure (in java, a Properties map)
* Test harness for the date structure
*
* @author jrandom
*/
class MappingTest implements TestDataGenerator, TestDataPrinter {
static {
TestData.registerGenerator(new MappingTest(), "Mapping");
TestData.registerPrinter(new MappingTest(), "Mapping");
}
private static final Log _log = new Log(MappingTest.class);
public class MappingTest extends TestCase{
public byte[] getData() {
public void testProperties() throws Exception{
byte[] temp = null;
Properties orig = new Properties();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Properties options = new Properties();
options.setProperty("key1", "val1");
options.setProperty("key2", "val2");
options.setProperty("key3", "val3");
try {
DataHelper.writeProperties(baos, options);
return baos.toByteArray();
} catch (DataFormatException dfe) {
_log.error("Error writing the mapping", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error writing the mapping", ioe);
return null;
}
orig.setProperty("key1", "val1");
orig.setProperty("key2", "val2");
orig.setProperty("key3", "val3");
DataHelper.writeProperties(baos, orig);
temp = baos.toByteArray();
Properties p = null;
ByteArrayInputStream bais = new ByteArrayInputStream(temp);
p = DataHelper.readProperties(bais);
assertEquals(orig, p);
}
public String testData(InputStream inputStream) {
try {
Properties options = DataHelper.readProperties(inputStream);
return DataHelper.toString(options);
} catch (DataFormatException dfe) {
_log.error("Error reading the mapping", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error reading the mapping", ioe);
return null;
}
}
}
}

View File

@@ -17,10 +17,7 @@ import net.i2p.data.i2cp.MessageId;
*
* @author jrandom
*/
class MessageIdTest extends StructureTest {
static {
TestData.registerTest(new MessageIdTest(), "MessageId");
}
public class MessageIdTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
MessageId id = new MessageId();
id.setMessageId(101);

View File

@@ -10,6 +10,8 @@ package net.i2p.data;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
@@ -24,12 +26,8 @@ import net.i2p.util.Log;
*
* @author jrandom
*/
class MessagePayloadMessageTest extends StructureTest {
private final static Log _log = new Log(MessagePayloadMessage.class);
static {
TestData.registerTest(new MessagePayloadMessageTest(), "MessagePayloadMessage");
}
public class MessagePayloadMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
MessagePayloadMessage msg = new MessagePayloadMessage();
msg.setMessageId((MessageId)(new MessageIdTest()).createDataStructure());
@@ -37,20 +35,25 @@ class MessagePayloadMessageTest extends StructureTest {
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
return msg;
}
public DataStructure createStructureToRead() { return new MessagePayloadMessage(); }
public String testData(InputStream inputStream) {
try {
DataStructure structure = createStructureToRead();
structure.readBytes(inputStream);
((MessagePayloadMessage)structure).getPayload().setUnencryptedData(((MessagePayloadMessage)structure).getPayload().getEncryptedData());
return structure.toString();
} catch (DataFormatException dfe) {
_log.error("Error reading the data structure", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error reading the data structure", ioe);
return null;
}
public DataStructure createStructureToRead() { return new MessagePayloadMessage(); }
public void testStructure() throws Exception{
byte[] temp = null;
DataStructure orig;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
orig = createDataStructure();
orig.writeBytes(baos);
temp = baos.toByteArray();
DataStructure ds;
ByteArrayInputStream bais = new ByteArrayInputStream(temp);
ds = createStructureToRead();
ds.readBytes(bais);
((MessagePayloadMessage)ds).getPayload().setUnencryptedData(((MessagePayloadMessage)ds).getPayload().getEncryptedData());
assertEquals(orig, ds);
}
}
}

View File

@@ -19,16 +19,14 @@ import net.i2p.data.i2cp.SessionId;
*
* @author jrandom
*/
class MessageStatusMessageTest extends StructureTest {
static {
TestData.registerTest(new MessageStatusMessageTest(), "MessageStatusMessage");
}
public class MessageStatusMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
MessageStatusMessage msg = new MessageStatusMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
msg.setMessageId((MessageId)(new MessageIdTest()).createDataStructure());
msg.setSize(1024*1024*42L);
msg.setStatus(MessageStatusMessage.STATUS_AVAILABLE);
msg.setNonce(1);
return msg;
}
public DataStructure createStructureToRead() { return new MessageStatusMessage(); }

View File

@@ -10,6 +10,8 @@ package net.i2p.data;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
@@ -24,42 +26,45 @@ import net.i2p.util.Log;
*
* @author jrandom
*/
class PayloadTest extends StructureTest {
private final static Log _log = new Log(PayloadTest.class);
static {
TestData.registerTest(new PayloadTest(), "Payload");
}
public class PayloadTest extends StructureTest{
public DataStructure createDataStructure() throws DataFormatException {
Payload payload = new Payload();
SessionKey key = (SessionKey)(new SessionKeyTest()).createDataStructure();
//payload.setEncryptionKey(key);
byte data[] = "Hello, I2P".getBytes();
payload.setUnencryptedData(data);
Hash hash = (Hash)(new HashTest()).createDataStructure();
//payload.setHash(hash);
Destination target = (Destination)(new DestinationTest()).createDataStructure();
payload.setEncryptedData(data);
//payload.encryptPayload(target, 128);
payload.setEncryptedData(data);
return payload;
}
public DataStructure createStructureToRead() { return new Payload(); }
public String testData(InputStream inputStream) {
try {
DataStructure structure = createStructureToRead();
structure.readBytes(inputStream);
Payload payload = (Payload)structure;
payload.setUnencryptedData(payload.getEncryptedData());
//((Payload)structure).decryptPayload((PrivateKey)(new PrivateKeyTest()).createDataStructure());
return structure.toString();
} catch (DataFormatException dfe) {
_log.error("Error reading the data structure", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error reading the data structure", ioe);
return null;
}
public void testStructure() throws Exception{
byte[] temp = null;
DataStructure orig;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
orig = createDataStructure();
orig.writeBytes(baos);
temp = baos.toByteArray();
DataStructure ds;
ByteArrayInputStream bais = new ByteArrayInputStream(temp);
ds = createStructureToRead();
ds.readBytes(bais);
Payload payload = (Payload)ds;
payload.setUnencryptedData(payload.getEncryptedData());
assertEquals(orig, ds);
}
}
}

View File

@@ -1,13 +1,17 @@
package net.i2p.data;
/*
* free (adj.): unencumbered; not under the control of others
* Written by jrandom in 2003 and released into the public domain
* Written by jrandom in 2003 and released into the Private 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.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import net.i2p.I2PAppContext;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.PrivateKey;
@@ -17,10 +21,7 @@ import net.i2p.data.PrivateKey;
*
* @author jrandom
*/
class PrivateKeyTest extends StructureTest {
static {
TestData.registerTest(new PrivateKeyTest(), "PrivateKey");
}
public class PrivateKeyTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
PrivateKey privateKey = new PrivateKey();
byte data[] = new byte[PrivateKey.KEYSIZE_BYTES];
@@ -30,4 +31,67 @@ class PrivateKeyTest extends StructureTest {
return privateKey;
}
public DataStructure createStructureToRead() { return new PrivateKey(); }
public void testBase64Constructor() throws Exception{
PrivateKey privateKey = new PrivateKey();
byte data[] = new byte[PrivateKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56);
privateKey.setData(data);
PrivateKey key2 = new PrivateKey(privateKey.toBase64());
assertEquals(privateKey, key2);
}
public void testNullEquals(){
PrivateKey privateKey = new PrivateKey();
byte data[] = new byte[PrivateKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56);
privateKey.setData(data);
assertFalse(privateKey.equals(null));
}
public void testNullData() throws Exception{
PrivateKey privateKey = new PrivateKey();
privateKey.toString();
boolean error = false;
try{
privateKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testShortData() throws Exception{
PrivateKey privateKey = new PrivateKey();
byte data[] = new byte[56];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i);
privateKey.setData(data);
boolean error = false;
try{
privateKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testShortRead() throws Exception{
PrivateKey privateKey = new PrivateKey();
ByteArrayInputStream in = new ByteArrayInputStream("six times nine equals forty-two".getBytes());
boolean error = false;
try{
privateKey.readBytes(in);
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
}

View File

@@ -8,6 +8,9 @@ package net.i2p.data;
*
*/
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.PublicKey;
@@ -17,10 +20,7 @@ import net.i2p.data.PublicKey;
*
* @author jrandom
*/
class PublicKeyTest extends StructureTest {
static {
TestData.registerTest(new PublicKeyTest(), "PublicKey");
}
public class PublicKeyTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
PublicKey publicKey = new PublicKey();
byte data[] = new byte[PublicKey.KEYSIZE_BYTES];
@@ -30,4 +30,66 @@ class PublicKeyTest extends StructureTest {
return publicKey;
}
public DataStructure createStructureToRead() { return new PublicKey(); }
public void testBase64Constructor() throws Exception{
PublicKey publicKey = new PublicKey();
byte data[] = new byte[PublicKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56);
publicKey.setData(data);
PublicKey key2 = new PublicKey(publicKey.toBase64());
assertEquals(publicKey, key2);
}
public void testNullEquals(){
PublicKey publicKey = new PublicKey();
byte data[] = new byte[PublicKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56);
publicKey.setData(data);
assertFalse(publicKey.equals(null));
}
public void testNullData() throws Exception{
PublicKey publicKey = new PublicKey();
publicKey.toString();
boolean error = false;
try{
publicKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testShortData() throws Exception{
PublicKey publicKey = new PublicKey();
byte data[] = new byte[56];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i);
publicKey.setData(data);
boolean error = false;
try{
publicKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testShortRead() throws Exception{
PublicKey publicKey = new PublicKey();
ByteArrayInputStream in = new ByteArrayInputStream("six times nine equals forty-two".getBytes());
boolean error = false;
try{
publicKey.readBytes(in);
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
}

View File

@@ -19,10 +19,7 @@ import net.i2p.data.i2cp.SessionId;
*
* @author jrandom
*/
class ReceiveMessageBeginMessageTest extends StructureTest {
static {
TestData.registerTest(new ReceiveMessageBeginMessageTest(), "ReceiveMessageBeginMessage");
}
public class ReceiveMessageBeginMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
ReceiveMessageBeginMessage msg = new ReceiveMessageBeginMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());

View File

@@ -19,10 +19,7 @@ import net.i2p.data.i2cp.SessionId;
*
* @author jrandom
*/
class ReceiveMessageEndMessageTest extends StructureTest {
static {
TestData.registerTest(new ReceiveMessageEndMessageTest(), "ReceiveMessageEndMessage");
}
public class ReceiveMessageEndMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
ReceiveMessageEndMessage msg = new ReceiveMessageEndMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());

View File

@@ -21,10 +21,7 @@ import net.i2p.data.i2cp.SessionId;
*
* @author jrandom
*/
class ReportAbuseMessageTest extends StructureTest {
static {
TestData.registerTest(new ReportAbuseMessageTest(), "ReportAbuseMessage");
}
public class ReportAbuseMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
ReportAbuseMessage msg = new ReportAbuseMessage();
msg.setMessageId((MessageId)(new MessageIdTest()).createDataStructure());

View File

@@ -22,10 +22,7 @@ import net.i2p.data.i2cp.SessionId;
*
* @author jrandom
*/
class RequestLeaseSetMessageTest extends StructureTest {
static {
TestData.registerTest(new RequestLeaseSetMessageTest(), "RequestLeaseSetMessage");
}
public class RequestLeaseSetMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
RequestLeaseSetMessage msg = new RequestLeaseSetMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());

View File

@@ -7,7 +7,8 @@ package net.i2p.data;
* your children, but it might. Use at your own risk.
*
*/
import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.Properties;
@@ -20,10 +21,7 @@ import net.i2p.data.RouterAddress;
*
* @author jrandom
*/
class RouterAddressTest extends StructureTest {
static {
TestData.registerTest(new RouterAddressTest(), "RouterAddress");
}
public class RouterAddressTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
RouterAddress addr = new RouterAddress();
byte data[] = new byte[32];
@@ -39,4 +37,45 @@ class RouterAddressTest extends StructureTest {
return addr;
}
public DataStructure createStructureToRead() { return new RouterAddress(); }
public void testBadWrite() throws Exception{
RouterAddress addr = new RouterAddress();
boolean error = true;
try{
addr.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testNullEquals(){
RouterAddress addr = new RouterAddress();
byte data[] = new byte[32];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%16);
addr.setCost(42);
addr.setExpiration(new Date(1000*60*60*24)); // jan 2 1970
Properties options = new Properties();
options.setProperty("hostname", "localhost");
options.setProperty("portnum", "1234");
addr.setOptions(options);
addr.setTransportStyle("Blah");
assertFalse(addr.equals(null));
}
public void testToString(){
RouterAddress addr = new RouterAddress();
byte data[] = new byte[32];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%16);
addr.setCost(42);
addr.setExpiration(new Date(1000*60*60*24)); // jan 2 1970
Properties options = new Properties();
options.setProperty("hostname", "localhost");
options.setProperty("portnum", "1234");
addr.setOptions(options);
addr.setTransportStyle("Blah");
addr.toString();
}
}

View File

@@ -8,6 +8,8 @@ package net.i2p.data;
*
*/
import java.io.ByteArrayOutputStream;
import net.i2p.data.Certificate;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
@@ -20,10 +22,7 @@ import net.i2p.data.SigningPublicKey;
*
* @author jrandom
*/
class RouterIdentityTest extends StructureTest {
static {
TestData.registerTest(new RouterIdentityTest(), "RouterIdentity");
}
public class RouterIdentityTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
RouterIdentity ident = new RouterIdentity();
Certificate cert = (Certificate)(new CertificateTest()).createDataStructure();
@@ -35,4 +34,84 @@ class RouterIdentityTest extends StructureTest {
return ident;
}
public DataStructure createStructureToRead() { return new RouterIdentity(); }
public void testNullCert() throws Exception{
RouterIdentity ident = new RouterIdentity();
ident.setCertificate(null);
PublicKey pk = (PublicKey)(new PublicKeyTest()).createDataStructure();
ident.setPublicKey(pk);
SigningPublicKey k = (SigningPublicKey)(new SigningPublicKeyTest()).createDataStructure();
ident.setSigningPublicKey(k);
boolean error = false;
try{
ident.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testNullPublicKey() throws Exception{
RouterIdentity ident = new RouterIdentity();
Certificate cert = (Certificate)(new CertificateTest()).createDataStructure();
ident.setCertificate(cert);
ident.setPublicKey(null);
SigningPublicKey k = (SigningPublicKey)(new SigningPublicKeyTest()).createDataStructure();
ident.setSigningPublicKey(k);
boolean error = false;
try{
ident.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testNullSigningKey() throws Exception{
RouterIdentity ident = new RouterIdentity();
Certificate cert = (Certificate)(new CertificateTest()).createDataStructure();
ident.setCertificate(cert);
PublicKey pk = (PublicKey)(new PublicKeyTest()).createDataStructure();
ident.setPublicKey(pk);
ident.setSigningPublicKey(null);
boolean error = false;
try{
ident.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testNullEquals() throws Exception{
RouterIdentity ident = new RouterIdentity();
assertFalse(ident.equals(null));
}
public void testCalculatedHash() throws Exception{
RouterIdentity ident = new RouterIdentity();
Certificate cert = (Certificate)(new CertificateTest()).createDataStructure();
ident.setCertificate(cert);
PublicKey pk = (PublicKey)(new PublicKeyTest()).createDataStructure();
ident.setPublicKey(pk);
SigningPublicKey k = (SigningPublicKey)(new SigningPublicKeyTest()).createDataStructure();
ident.setSigningPublicKey(k);
ident.calculateHash();
ident.calculateHash();
ident.calculateHash();
ident.calculateHash();
ident.calculateHash();
}
public void testBadHash() throws Exception{
RouterIdentity ident = new RouterIdentity();
ident.getHash();
}
}

View File

@@ -30,9 +30,6 @@ import net.i2p.util.Log;
*/
public class RouterInfoTest extends StructureTest {
private final static Log _log = new Log(RouterInfoTest.class);
static {
TestData.registerTest(new RouterInfoTest(), "RouterInfo");
}
public DataStructure createDataStructure() throws DataFormatException {
RouterInfo info = new RouterInfo();
HashSet addresses = new HashSet();
@@ -40,32 +37,32 @@ public class RouterInfoTest extends StructureTest {
addresses.add(structure);
info.setAddresses(addresses);
PublicKey pubKey = null;
SigningPublicKey signingPubKey = null;
PrivateKey privKey = null;
SigningPrivateKey signingPrivKey = null;
Object obj[] = KeyGenerator.getInstance().generatePKIKeypair();
pubKey = (PublicKey)obj[0];
privKey = (PrivateKey)obj[1];
obj = KeyGenerator.getInstance().generateSigningKeypair();
signingPubKey = (SigningPublicKey)obj[0];
signingPrivKey = (SigningPrivateKey)obj[1];
_log.debug("SigningPublicKey: " + signingPubKey);
_log.debug("SigningPrivateKey: " + signingPrivKey);
RouterIdentity ident = new RouterIdentity();
ident.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
ident.setPublicKey(pubKey);
ident.setSigningPublicKey(signingPubKey);
PublicKey pubKey = null;
SigningPublicKey signingPubKey = null;
PrivateKey privKey = null;
SigningPrivateKey signingPrivKey = null;
Object obj[] = KeyGenerator.getInstance().generatePKIKeypair();
pubKey = (PublicKey)obj[0];
privKey = (PrivateKey)obj[1];
obj = KeyGenerator.getInstance().generateSigningKeypair();
signingPubKey = (SigningPublicKey)obj[0];
signingPrivKey = (SigningPrivateKey)obj[1];
_log.debug("SigningPublicKey: " + signingPubKey);
_log.debug("SigningPrivateKey: " + signingPrivKey);
RouterIdentity ident = new RouterIdentity();
ident.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
ident.setPublicKey(pubKey);
ident.setSigningPublicKey(signingPubKey);
info.setIdentity(ident);
Properties options = new Properties();
for (int i = 0; i < 16; i++) {
options.setProperty("option." + i, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890$:." + i);
}
for (int i = 0; i < 16; i++) {
options.setProperty("option." + i, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890$:." + i);
}
options.setProperty("netConnectionSpeed", "OC12");
info.setOptions(options);
@@ -73,12 +70,12 @@ public class RouterInfoTest extends StructureTest {
structure = (new HashTest()).createDataStructure();
peers.add(structure);
info.setPeers(peers);
info.setPublished(System.currentTimeMillis());
//info.setVersion(69);
info.sign(signingPrivKey);
info.setPublished(System.currentTimeMillis());
//info.setVersion(69);
info.sign(signingPrivKey);
return info;
}
public DataStructure createStructureToRead() { return new RouterInfo(); }

View File

@@ -10,6 +10,8 @@ package net.i2p.data;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
@@ -24,33 +26,39 @@ import net.i2p.util.Log;
*
* @author jrandom
*/
class SendMessageMessageTest extends StructureTest {
private final static Log _log = new Log(SendMessageMessageTest.class);
static {
TestData.registerTest(new SendMessageMessageTest(), "SendMessageMessage");
}
public class SendMessageMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
SendMessageMessage msg = new SendMessageMessage();
msg.setDestination((Destination)(new DestinationTest()).createDataStructure());
msg.setPayload((Payload)(new PayloadTest()).createDataStructure());
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
msg.setNonce(1);
return msg;
}
public DataStructure createStructureToRead() { return new SendMessageMessage(); }
public String testData(InputStream inputStream) {
try {
DataStructure structure = createStructureToRead();
structure.readBytes(inputStream);
((SendMessageMessage)structure).getPayload().setUnencryptedData(((SendMessageMessage)structure).getPayload().getEncryptedData());
return structure.toString();
} catch (DataFormatException dfe) {
_log.error("Error reading the data structure", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error reading the data structure", ioe);
return null;
}
public DataStructure createStructureToRead() { return new SendMessageMessage(); }
public void testStructure() throws Exception{
byte[] temp = null;
DataStructure orig;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
orig = createDataStructure();
orig.writeBytes(baos);
temp = baos.toByteArray();
DataStructure ds;
ByteArrayInputStream bais = new ByteArrayInputStream(temp);
ds = createStructureToRead();
ds.readBytes(bais);
((SendMessageMessage)ds).getPayload().setUnencryptedData(((SendMessageMessage)ds).getPayload().getEncryptedData());
assertEquals(orig, ds);
}
}
}

View File

@@ -22,10 +22,7 @@ import net.i2p.data.i2cp.SessionConfig;
*
* @author jrandom
*/
class SessionConfigTest extends StructureTest {
static {
TestData.registerTest(new SessionConfigTest(), "SessionConfig");
}
public class SessionConfigTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
SessionConfig cfg = new SessionConfig((Destination)(new DestinationTest()).createDataStructure());
cfg.setSignature((Signature)(new SignatureTest()).createDataStructure());

View File

@@ -17,10 +17,7 @@ import net.i2p.data.i2cp.SessionId;
*
* @author jrandom
*/
class SessionIdTest extends StructureTest {
static {
TestData.registerTest(new SessionIdTest(), "SessionId");
}
public class SessionIdTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
SessionId id = new SessionId();
id.setSessionId(7);

View File

@@ -17,10 +17,7 @@ import net.i2p.data.SessionKey;
*
* @author jrandom
*/
class SessionKeyTest extends StructureTest {
static {
TestData.registerTest(new SessionKeyTest(), "SessionKey");
}
public class SessionKeyTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
SessionKey key = new SessionKey();
byte data[] = new byte[SessionKey.KEYSIZE_BYTES];

View File

@@ -18,10 +18,7 @@ import net.i2p.data.i2cp.SessionStatusMessage;
*
* @author jrandom
*/
class SessionStatusMessageTest extends StructureTest {
static {
TestData.registerTest(new SessionStatusMessageTest(), "SessionStatusMessage");
}
public class SessionStatusMessageTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
SessionStatusMessage msg = new SessionStatusMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());

View File

@@ -17,10 +17,7 @@ import net.i2p.data.Signature;
*
* @author jrandom
*/
class SignatureTest extends StructureTest {
static {
TestData.registerTest(new SignatureTest(), "Signature");
}
public class SignatureTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
Signature sig = new Signature();
byte data[] = new byte[Signature.SIGNATURE_BYTES];

View File

@@ -7,7 +7,11 @@ package net.i2p.data;
* your children, but it might. Use at your own risk.
*
*/
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import net.i2p.I2PAppContext;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.SigningPrivateKey;
@@ -17,17 +21,78 @@ import net.i2p.data.SigningPrivateKey;
*
* @author jrandom
*/
class SigningPrivateKeyTest extends StructureTest {
static {
TestData.registerTest(new SigningPrivateKeyTest(), "SigningPrivateKey");
}
public class SigningPrivateKeyTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
SigningPrivateKey privateKey = new SigningPrivateKey();
SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%16);
privateKey.setData(data);
return privateKey;
signingPrivateKey.setData(data);
return signingPrivateKey;
}
public DataStructure createStructureToRead() { return new SigningPrivateKey(); }
public void testBase64Constructor() throws Exception{
SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56);
signingPrivateKey.setData(data);
SigningPrivateKey key2 = new SigningPrivateKey(signingPrivateKey.toBase64());
assertEquals(signingPrivateKey, key2);
}
public void testNullEquals(){
SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56);
signingPrivateKey.setData(data);
assertFalse(signingPrivateKey.equals(null));
}
public void testNullData() throws Exception{
SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
signingPrivateKey.toString();
boolean error = false;
try{
signingPrivateKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testShortData() throws Exception{
SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
byte data[] = new byte[56];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i);
signingPrivateKey.setData(data);
boolean error = false;
try{
signingPrivateKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testShortRead() throws Exception{
SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
ByteArrayInputStream in = new ByteArrayInputStream("short".getBytes());
boolean error = false;
try{
signingPrivateKey.readBytes(in);
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
}

View File

@@ -8,6 +8,9 @@ package net.i2p.data;
*
*/
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.SigningPublicKey;
@@ -17,10 +20,7 @@ import net.i2p.data.SigningPublicKey;
*
* @author jrandom
*/
class SigningPublicKeyTest extends StructureTest {
static {
TestData.registerTest(new SigningPublicKeyTest(), "SigningPublicKey");
}
public class SigningPublicKeyTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
SigningPublicKey publicKey = new SigningPublicKey();
byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES];
@@ -30,4 +30,67 @@ class SigningPublicKeyTest extends StructureTest {
return publicKey;
}
public DataStructure createStructureToRead() { return new SigningPublicKey(); }
public void testBase64Constructor() throws Exception{
SigningPublicKey publicKey = new SigningPublicKey();
byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56);
publicKey.setData(data);
SigningPublicKey key2 = new SigningPublicKey(publicKey.toBase64());
assertEquals(publicKey, key2);
}
public void testNullEquals(){
SigningPublicKey publicKey = new SigningPublicKey();
byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56);
publicKey.setData(data);
assertFalse(publicKey.equals(null));
}
public void testNullData() throws Exception{
SigningPublicKey publicKey = new SigningPublicKey();
publicKey.toString();
boolean error = false;
try{
publicKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testShortData() throws Exception{
SigningPublicKey publicKey = new SigningPublicKey();
byte data[] = new byte[56];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i);
publicKey.setData(data);
boolean error = false;
try{
publicKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
public void testShortRead() throws Exception{
SigningPublicKey publicKey = new SigningPublicKey();
ByteArrayInputStream in = new ByteArrayInputStream("six times nine equals forty-two".getBytes());
boolean error = false;
try{
publicKey.readBytes(in);
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
}

View File

@@ -9,6 +9,7 @@ package net.i2p.data;
*/
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -16,44 +17,30 @@ import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.util.Log;
import junit.framework.TestCase;
/**
* Test harness for the boolean structure
* Test harness for the date structure
*
* @author jrandom
*/
class StringTest implements TestDataGenerator, TestDataPrinter {
static {
TestData.registerGenerator(new StringTest(), "String");
TestData.registerPrinter(new StringTest(), "String");
}
private static final Log _log = new Log(StringTest.class);
public class StringTest extends TestCase{
public byte[] getData() {
public void testString() throws Exception{
byte[] temp = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
DataHelper.writeString(baos, "Hello, I2P");
return baos.toByteArray();
} catch (DataFormatException dfe) {
_log.error("Error writing the string", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error writing the string", ioe);
return null;
}
DataHelper.writeString(baos, "Hello, I2P");
temp = baos.toByteArray();
String s = null;
ByteArrayInputStream bais = new ByteArrayInputStream(temp);
s = DataHelper.readString(bais);
assertEquals(s, "Hello, I2P");
}
public String testData(InputStream inputStream) {
try {
String str = DataHelper.readString(inputStream);
return ""+str;
} catch (DataFormatException dfe) {
_log.error("Error reading the string", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error reading the string", ioe);
return null;
}
}
}
}

View File

@@ -9,6 +9,7 @@ package net.i2p.data;
*/
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -17,46 +18,38 @@ import net.i2p.data.DataStructure;
import net.i2p.util.Log;
import net.i2p.I2PAppContext;
import junit.framework.TestCase;
/**
* Utility class for wrapping data structure tests
*
* @author jrandom
*/
public abstract class StructureTest implements TestDataGenerator, TestDataPrinter {
private static final Log _log = new Log(StructureTest.class);
protected static I2PAppContext _context = I2PAppContext.getGlobalContext();
public abstract class StructureTest extends TestCase{
public abstract DataStructure createDataStructure() throws DataFormatException;
public abstract DataStructure createStructureToRead();
public byte[] getData() {
public void testStructure() throws Exception{
byte[] temp = null;
DataStructure orig;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
DataStructure structure = createDataStructure();
structure.writeBytes(baos);
} catch (DataFormatException dfe) {
_log.error("Error writing the data structure", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error writing the data structure", ioe);
return null;
}
return baos.toByteArray();
orig = createDataStructure();
orig.writeBytes(baos);
temp = baos.toByteArray();
DataStructure ds;
ByteArrayInputStream bais = new ByteArrayInputStream(temp);
ds = createStructureToRead();
ds.readBytes(bais);
assertEquals(orig, ds);
}
public String testData(InputStream inputStream) {
try {
DataStructure structure = createStructureToRead();
structure.readBytes(inputStream);
return structure.toString() + "\n\nIn base 64: " + structure.toBase64();
} catch (DataFormatException dfe) {
_log.error("Error reading the data structure", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error reading the data structure", ioe);
return null;
}
}
}
}

View File

@@ -1,150 +0,0 @@
package net.i2p.data;
/*
* 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.HashMap;
import java.util.Iterator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import net.i2p.util.Log;
/**
* Test harness for loading / storing data structures
*
* @author jrandom
*/
public class TestData {
private final static Log _log = new Log(TestData.class);
private final static String HELP = "\nUsage: TestData generate objectType outFile\n" +
" TestData display objectType inFile\n" +
" TestData test objectType tempFile\n" +
"Known types: ";
private final static String OP_GENERATE = "generate";
private final static String OP_DISPLAY = "display";
private final static String OP_TEST = "test";
private final static HashMap _generators;
private final static HashMap _printers;
static {
_generators = new HashMap();
_generators.put("NullType", new TestDataGenerator() { public byte[] getData() { return new byte[1]; } });
_printers = new HashMap();
_printers.put("NullType", new TestDataPrinter() { public String testData(InputStream in) { return "Null data read successfully"; } });
}
public static void registerTest(StructureTest test, String name) {
registerGenerator(test, name);
registerPrinter(test, name);
}
public static void registerGenerator(TestDataGenerator test, String name) {
_generators.put(name, test);
}
public static void registerPrinter(TestDataPrinter test, String name) {
_printers.put(name, test);
}
public static void main(String args[]) {
if (args.length < 1) {
showHelp();
return;
}
if (OP_GENERATE.equalsIgnoreCase(args[0])) {
validateTest(args[1]);
if (args.length != 3) {
showHelp();
return;
}
generate(args[1], args[2]);
return;
} else if (OP_DISPLAY.equalsIgnoreCase(args[0])) {
validateTest(args[1]);
if (args.length != 3) {
showHelp();
return;
}
display(args[1], args[2]);
} else if (OP_TEST.equalsIgnoreCase(args[0])) {
validateTest(args[1]);
if (args.length != 3) {
showHelp();
return;
}
generate(args[1], args[2]);
display(args[1], args[2]);
} else {
showHelp();
}
try { Thread.sleep(2000); } catch (InterruptedException ie) {}
}
private static void validateTest(String objectType) {
try {
String clsName = TestData.class.getPackage().getName() + "." + objectType + "Test";
Class.forName(clsName);
} catch (Throwable t) {
_log.error("Error validating the object type", t);
}
}
public static void generate(String objectType, String outFile) {
TestDataGenerator gen = (TestDataGenerator)_generators.get(objectType);
byte[] data = gen.getData();
if (data == null) {
_log.error("Error generating the data. fail");
return;
}
try {
File f = new File(outFile);
FileOutputStream out = new FileOutputStream(f);
out.write(data);
out.flush();
out.close();
_log.debug("Wrote the file out to " + f.getAbsolutePath());
} catch (IOException ioe) {
_log.error("Error writing out the object", ioe);
}
}
public static void display(String type, String inFile) {
try {
File f = new File(inFile);
FileInputStream in = new FileInputStream(f);
TestDataPrinter printer = (TestDataPrinter)_printers.get(type);
String display = printer.testData(in);
in.close();
_log.info("Displaying " + inFile + " of type: " + type);
_log.info(display);
} catch (IOException ioe) {
_log.error("Error reading the file to display", ioe);
}
}
private static String listTypes() {
StringBuffer buf = new StringBuffer();
for (Iterator iter = _generators.keySet().iterator(); iter.hasNext(); ) {
String type = (String)iter.next();
buf.append(type);
if (iter.hasNext())
buf.append(", ");
}
return buf.toString();
}
public static void showHelp() {
_log.info(HELP+listTypes());
}
}

View File

@@ -1,16 +0,0 @@
package net.i2p.data;
/*
* 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.
*
*/
/**
* Defines a method to create the serialization of an object
*/
public interface TestDataGenerator {
public byte[] getData();
}

View File

@@ -1,18 +0,0 @@
package net.i2p.data;
/*
* 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.InputStream;
/**
* Defines a method to read the serialization of an object and display it
*/
public interface TestDataPrinter {
public String testData(InputStream stream);
}

View File

@@ -17,10 +17,7 @@ import net.i2p.data.TunnelId;
*
* @author jrandom
*/
class TunnelIdTest extends StructureTest {
static {
TestData.registerTest(new TunnelIdTest(), "TunnelId");
}
public class TunnelIdTest extends StructureTest {
public DataStructure createDataStructure() throws DataFormatException {
TunnelId id = new TunnelId();
id.setTunnelId(42);

View File

@@ -9,51 +9,38 @@ package net.i2p.data;
*/
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.util.Log;
import junit.framework.TestCase;
/**
* Test harness for the numerical structure (in java, an UnsignedInteger)
* Test harness for the date structure
*
* @author jrandom
*/
class UnsignedIntegerTest implements TestDataGenerator, TestDataPrinter {
static {
TestData.registerGenerator(new UnsignedIntegerTest(), "UnsignedInteger");
TestData.registerPrinter(new UnsignedIntegerTest(), "UnsignedInteger");
}
private static final Log _log = new Log(UnsignedIntegerTest.class);
public class UnsignedIntegerTest extends TestCase{
public byte[] getData() {
public void testLong() throws Exception{
byte[] temp = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
DataHelper.writeLong(baos, 4, 42);
return baos.toByteArray();
} catch (DataFormatException dfe) {
_log.error("Error writing the integer", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error writing the integer", ioe);
return null;
}
DataHelper.writeLong(baos, 4, 42);
temp = baos.toByteArray();
long l;
ByteArrayInputStream bais = new ByteArrayInputStream(temp);
l = DataHelper.readLong(bais, 4);
assertEquals(42, l);
}
public String testData(InputStream inputStream) {
try {
long val = DataHelper.readLong(inputStream, 4);
return ""+val;
} catch (DataFormatException dfe) {
_log.error("Error reading the integer", dfe);
return null;
} catch (IOException ioe) {
_log.error("Error reading the integer", ioe);
return null;
}
}
}
}