great renaming (cont.)

This commit is contained in:
jrandom
2004-04-08 04:54:58 +00:00
committed by zzz
parent e40b94c875
commit 2e3fd0abf2
48 changed files with 2864 additions and 0 deletions

View File

@ -0,0 +1,30 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.AbuseReason;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class AbuseReasonTest extends StructureTest {
static {
TestData.registerTest(new AbuseReasonTest(), "AbuseReason");
}
public DataStructure createDataStructure() throws DataFormatException {
AbuseReason res = new AbuseReason();
res.setReason("Because they're mean");
return res;
}
public DataStructure createStructureToRead() { return new AbuseReason(); }
}

View File

@ -0,0 +1,30 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.AbuseSeverity;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class AbuseSeverityTest extends StructureTest {
static {
TestData.registerTest(new AbuseSeverityTest(), "AbuseSeverity");
}
public DataStructure createDataStructure() throws DataFormatException {
AbuseSeverity sev = new AbuseSeverity();
sev.setSeverity(64);
return sev;
}
public DataStructure createStructureToRead() { return new AbuseSeverity(); }
}

View File

@ -0,0 +1,59 @@
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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.util.Log;
/**
* 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 byte[] getData() {
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;
}
}
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

@ -0,0 +1,34 @@
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 net.i2p.data.Certificate;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class CertificateTest extends StructureTest {
static {
TestData.registerTest(new CertificateTest(), "Certificate");
}
public DataStructure createDataStructure() throws DataFormatException {
Certificate cert = new Certificate();
byte data[] = new byte[32];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%16);
cert.setPayload(data);
cert.setCertificateType(Certificate.CERTIFICATE_TYPE_NULL);
return cert;
}
public DataStructure createStructureToRead() { return new Certificate(); }
}

View File

@ -0,0 +1,37 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.LeaseSet;
import net.i2p.data.PrivateKey;
import net.i2p.data.SigningPrivateKey;
import net.i2p.data.i2cp.CreateLeaseSetMessage;
import net.i2p.data.i2cp.SessionId;
/**
* Test harness for loading / storing CreateLeaseSetMessage objects
*
* @author jrandom
*/
class CreateLeaseSetMessageTest extends StructureTest {
static {
TestData.registerTest(new CreateLeaseSetMessageTest(), "CreateLeaseSetMessage");
}
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.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
return msg;
}
public DataStructure createStructureToRead() { return new CreateLeaseSetMessage(); }
}

View File

@ -0,0 +1,31 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.CreateSessionMessage;
import net.i2p.data.i2cp.SessionConfig;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class CreateSessionMessageTest extends StructureTest {
static {
TestData.registerTest(new CreateSessionMessageTest(), "CreateSessionMessage");
}
public DataStructure createDataStructure() throws DataFormatException {
CreateSessionMessage msg = new CreateSessionMessage();
msg.setSessionConfig((SessionConfig)(new SessionConfigTest()).createDataStructure());
return msg;
}
public DataStructure createStructureToRead() { return new CreateSessionMessage(); }
}

View File

@ -0,0 +1,60 @@
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.ByteArrayOutputStream;
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;
/**
* Test harness for the boolean 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 byte[] getData() {
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;
}
}
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

@ -0,0 +1,38 @@
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 net.i2p.data.Certificate;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.Destination;
import net.i2p.data.PublicKey;
import net.i2p.data.SigningPublicKey;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class DestinationTest extends StructureTest {
static {
TestData.registerTest(new DestinationTest(), "Destination");
}
public DataStructure createDataStructure() throws DataFormatException {
Destination dest = new Destination();
StructureTest tst = new CertificateTest();
dest.setCertificate((Certificate)tst.createDataStructure());
tst = new PublicKeyTest();
dest.setPublicKey((PublicKey)tst.createDataStructure());
tst = new SigningPublicKeyTest();
dest.setSigningPublicKey((SigningPublicKey)tst.createDataStructure());
return dest;
}
public DataStructure createStructureToRead() { return new Destination(); }
}

View File

@ -0,0 +1,31 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.DestroySessionMessage;
import net.i2p.data.i2cp.SessionId;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class DestroySessionMessageTest extends StructureTest {
static {
TestData.registerTest(new DestroySessionMessageTest(), "DestroySessionMessage");
}
public DataStructure createDataStructure() throws DataFormatException {
DestroySessionMessage msg = new DestroySessionMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
return msg;
}
public DataStructure createStructureToRead() { return new DestroySessionMessage(); }
}

View File

@ -0,0 +1,30 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.DisconnectMessage;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class DisconnectMessageTest extends StructureTest {
static {
TestData.registerTest(new DisconnectMessageTest(), "DisconnectMessage");
}
public DataStructure createDataStructure() throws DataFormatException {
DisconnectMessage msg = new DisconnectMessage();
msg.setReason("Because I say so");
return msg;
}
public DataStructure createStructureToRead() { return new DisconnectMessage(); }
}

View File

@ -0,0 +1,33 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.Hash;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class HashTest extends StructureTest {
static {
TestData.registerTest(new HashTest(), "Hash");
}
public DataStructure createDataStructure() throws DataFormatException {
Hash hash = new Hash();
byte data[] = new byte[32];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%16);
hash.setData(data);
return hash;
}
public DataStructure createStructureToRead() { return new Hash(); }
}

View File

@ -0,0 +1,38 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.Destination;
import net.i2p.data.LeaseSet;
import net.i2p.data.PublicKey;
import net.i2p.data.Signature;
import net.i2p.data.SigningPublicKey;
/**
* Test harness for loading / storing Lease objects
*
* @author jrandom
*/
class LeaseSetTest extends StructureTest {
static {
TestData.registerTest(new LeaseSetTest(), "LeaseSet");
}
public DataStructure createDataStructure() throws DataFormatException {
LeaseSet leaseSet = new LeaseSet();
leaseSet.setDestination((Destination)(new DestinationTest()).createDataStructure());
leaseSet.setEncryptionKey((PublicKey)(new PublicKeyTest()).createDataStructure());
leaseSet.setSignature((Signature)(new SignatureTest()).createDataStructure());
leaseSet.setSigningKey((SigningPublicKey)(new SigningPublicKeyTest()).createDataStructure());
//leaseSet.setVersion(42l);
return leaseSet;
}
public DataStructure createStructureToRead() { return new LeaseSet(); }
}

View File

@ -0,0 +1,41 @@
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.Date;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.Lease;
import net.i2p.data.RouterIdentity;
import net.i2p.data.TunnelId;
/**
* Test harness for loading / storing Lease objects
*
* @author jrandom
*/
class LeaseTest extends StructureTest {
static {
TestData.registerTest(new LeaseTest(), "Lease");
}
public DataStructure createDataStructure() throws DataFormatException {
Lease lease = new Lease();
StructureTest tst = new DestinationTest();
lease.setEndDate(new Date(1000*60*2));
//lease.setStartDate(new Date(1000*60));
tst = new RouterIdentityTest();
lease.setRouterIdentity((RouterIdentity)tst.createDataStructure());
tst = new TunnelIdTest();
lease.setTunnelId((TunnelId)tst.createDataStructure());
return lease;
}
public DataStructure createStructureToRead() { return new Lease(); }
}

View File

@ -0,0 +1,64 @@
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.ByteArrayOutputStream;
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;
/**
* Test harness for the mapping structure (in java, a Properties map)
*
* @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 byte[] getData() {
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;
}
}
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

@ -0,0 +1,30 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.MessageId;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class MessageIdTest extends StructureTest {
static {
TestData.registerTest(new MessageIdTest(), "MessageId");
}
public DataStructure createDataStructure() throws DataFormatException {
MessageId id = new MessageId();
id.setMessageId(101);
return id;
}
public DataStructure createStructureToRead() { return new MessageId(); }
}

View File

@ -0,0 +1,56 @@
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.IOException;
import java.io.InputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.Payload;
import net.i2p.data.i2cp.MessageId;
import net.i2p.data.i2cp.MessagePayloadMessage;
import net.i2p.data.i2cp.SessionId;
import net.i2p.util.Log;
/**
* Test harness for loading / storing SendMessageMessage objects
*
* @author jrandom
*/
class MessagePayloadMessageTest extends StructureTest {
private final static Log _log = new Log(MessagePayloadMessage.class);
static {
TestData.registerTest(new MessagePayloadMessageTest(), "MessagePayloadMessage");
}
public DataStructure createDataStructure() throws DataFormatException {
MessagePayloadMessage msg = new MessagePayloadMessage();
msg.setMessageId((MessageId)(new MessageIdTest()).createDataStructure());
msg.setPayload((Payload)(new PayloadTest()).createDataStructure());
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;
}
}
}

View File

@ -0,0 +1,35 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.MessageId;
import net.i2p.data.i2cp.MessageStatusMessage;
import net.i2p.data.i2cp.SessionId;
/**
* Test harness for loading / storing MessageStatusMessage objects
*
* @author jrandom
*/
class MessageStatusMessageTest extends StructureTest {
static {
TestData.registerTest(new MessageStatusMessageTest(), "MessageStatusMessage");
}
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);
return msg;
}
public DataStructure createStructureToRead() { return new MessageStatusMessage(); }
}

View File

@ -0,0 +1,65 @@
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.IOException;
import java.io.InputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.Destination;
import net.i2p.data.Hash;
import net.i2p.data.Payload;
import net.i2p.data.SessionKey;
import net.i2p.util.Log;
/**
* Test harness for loading / storing Payload objects
*
* @author jrandom
*/
class PayloadTest extends StructureTest {
private final static Log _log = new Log(PayloadTest.class);
static {
TestData.registerTest(new PayloadTest(), "Payload");
}
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);
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;
}
}
}

View File

@ -0,0 +1,33 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.PrivateKey;
/**
* Test harness for loading / storing PrivateKey objects
*
* @author jrandom
*/
class PrivateKeyTest extends StructureTest {
static {
TestData.registerTest(new PrivateKeyTest(), "PrivateKey");
}
public DataStructure createDataStructure() throws DataFormatException {
PrivateKey privateKey = new PrivateKey();
byte data[] = new byte[PrivateKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%16);
privateKey.setData(data);
return privateKey;
}
public DataStructure createStructureToRead() { return new PrivateKey(); }
}

View File

@ -0,0 +1,33 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.PublicKey;
/**
* Test harness for loading / storing PublicKey objects
*
* @author jrandom
*/
class PublicKeyTest extends StructureTest {
static {
TestData.registerTest(new PublicKeyTest(), "PublicKey");
}
public DataStructure createDataStructure() throws DataFormatException {
PublicKey publicKey = new PublicKey();
byte data[] = new byte[PublicKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%16);
publicKey.setData(data);
return publicKey;
}
public DataStructure createStructureToRead() { return new PublicKey(); }
}

View File

@ -0,0 +1,33 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.MessageId;
import net.i2p.data.i2cp.ReceiveMessageBeginMessage;
import net.i2p.data.i2cp.SessionId;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class ReceiveMessageBeginMessageTest extends StructureTest {
static {
TestData.registerTest(new ReceiveMessageBeginMessageTest(), "ReceiveMessageBeginMessage");
}
public DataStructure createDataStructure() throws DataFormatException {
ReceiveMessageBeginMessage msg = new ReceiveMessageBeginMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
msg.setMessageId((MessageId)(new MessageIdTest()).createDataStructure());
return msg;
}
public DataStructure createStructureToRead() { return new ReceiveMessageBeginMessage(); }
}

View File

@ -0,0 +1,33 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.MessageId;
import net.i2p.data.i2cp.ReceiveMessageEndMessage;
import net.i2p.data.i2cp.SessionId;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class ReceiveMessageEndMessageTest extends StructureTest {
static {
TestData.registerTest(new ReceiveMessageEndMessageTest(), "ReceiveMessageEndMessage");
}
public DataStructure createDataStructure() throws DataFormatException {
ReceiveMessageEndMessage msg = new ReceiveMessageEndMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
msg.setMessageId((MessageId)(new MessageIdTest()).createDataStructure());
return msg;
}
public DataStructure createStructureToRead() { return new ReceiveMessageEndMessage(); }
}

View File

@ -0,0 +1,37 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.AbuseReason;
import net.i2p.data.i2cp.AbuseSeverity;
import net.i2p.data.i2cp.MessageId;
import net.i2p.data.i2cp.ReportAbuseMessage;
import net.i2p.data.i2cp.SessionId;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class ReportAbuseMessageTest extends StructureTest {
static {
TestData.registerTest(new ReportAbuseMessageTest(), "ReportAbuseMessage");
}
public DataStructure createDataStructure() throws DataFormatException {
ReportAbuseMessage msg = new ReportAbuseMessage();
msg.setMessageId((MessageId)(new MessageIdTest()).createDataStructure());
msg.setReason((AbuseReason)(new AbuseReasonTest()).createDataStructure());
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
msg.setSeverity((AbuseSeverity)(new AbuseSeverityTest()).createDataStructure());
return msg;
}
public DataStructure createStructureToRead() { return new ReportAbuseMessage(); }
}

View File

@ -0,0 +1,38 @@
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.Date;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.RouterIdentity;
import net.i2p.data.TunnelId;
import net.i2p.data.i2cp.RequestLeaseSetMessage;
import net.i2p.data.i2cp.SessionId;
/**
* Test harness for loading / storing RequestLeaseSetMessage objects
*
* @author jrandom
*/
class RequestLeaseSetMessageTest extends StructureTest {
static {
TestData.registerTest(new RequestLeaseSetMessageTest(), "RequestLeaseSetMessage");
}
public DataStructure createDataStructure() throws DataFormatException {
RequestLeaseSetMessage msg = new RequestLeaseSetMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
msg.setEndDate(new Date(1000*60*60*12));
msg.addEndpoint((RouterIdentity)(new RouterIdentityTest()).createDataStructure(),
(TunnelId)(new TunnelIdTest()).createDataStructure());
return msg;
}
public DataStructure createStructureToRead() { return new RequestLeaseSetMessage(); }
}

View File

@ -0,0 +1,42 @@
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.Date;
import java.util.Properties;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.RouterAddress;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class RouterAddressTest extends StructureTest {
static {
TestData.registerTest(new RouterAddressTest(), "RouterAddress");
}
public DataStructure createDataStructure() throws DataFormatException {
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");
return addr;
}
public DataStructure createStructureToRead() { return new RouterAddress(); }
}

View File

@ -0,0 +1,38 @@
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 net.i2p.data.Certificate;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.PublicKey;
import net.i2p.data.RouterIdentity;
import net.i2p.data.SigningPublicKey;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class RouterIdentityTest extends StructureTest {
static {
TestData.registerTest(new RouterIdentityTest(), "RouterIdentity");
}
public DataStructure createDataStructure() throws DataFormatException {
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);
return ident;
}
public DataStructure createStructureToRead() { return new RouterIdentity(); }
}

View File

@ -0,0 +1,85 @@
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.HashSet;
import java.util.Properties;
import net.i2p.crypto.KeyGenerator;
import net.i2p.data.Certificate;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.PrivateKey;
import net.i2p.data.PublicKey;
import net.i2p.data.RouterIdentity;
import net.i2p.data.RouterInfo;
import net.i2p.data.SigningPrivateKey;
import net.i2p.data.SigningPublicKey;
import net.i2p.util.Log;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
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();
DataStructure structure = (new RouterAddressTest()).createDataStructure();
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);
info.setIdentity(ident);
Properties options = new Properties();
for (int i = 0; i < 16; i++) {
options.setProperty("option." + i, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890$:." + i);
}
options.setProperty("netConnectionSpeed", "OC12");
info.setOptions(options);
HashSet peers = new HashSet();
structure = (new HashTest()).createDataStructure();
peers.add(structure);
info.setPeers(peers);
info.setPublished(System.currentTimeMillis());
//info.setVersion(69);
info.sign(signingPrivKey);
return info;
}
public DataStructure createStructureToRead() { return new RouterInfo(); }
}

View File

@ -0,0 +1,56 @@
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.IOException;
import java.io.InputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.Destination;
import net.i2p.data.Payload;
import net.i2p.data.i2cp.SendMessageMessage;
import net.i2p.data.i2cp.SessionId;
import net.i2p.util.Log;
/**
* Test harness for loading / storing SendMessageMessage objects
*
* @author jrandom
*/
class SendMessageMessageTest extends StructureTest {
private final static Log _log = new Log(SendMessageMessageTest.class);
static {
TestData.registerTest(new SendMessageMessageTest(), "SendMessageMessage");
}
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());
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;
}
}
}

View File

@ -0,0 +1,42 @@
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.Properties;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.Destination;
import net.i2p.data.Signature;
import net.i2p.data.SigningPrivateKey;
import net.i2p.data.i2cp.SessionConfig;
/**
* Test harness for loading / storing Hash objects
*
* @author jrandom
*/
class SessionConfigTest extends StructureTest {
static {
TestData.registerTest(new SessionConfigTest(), "SessionConfig");
}
public DataStructure createDataStructure() throws DataFormatException {
SessionConfig cfg = new SessionConfig();
cfg.setDestination((Destination)(new DestinationTest()).createDataStructure());
cfg.setSignature((Signature)(new SignatureTest()).createDataStructure());
Properties options = new Properties();
options.setProperty("routerHost", "localhost");
options.setProperty("routerPort", "54321");
options.setProperty("routerSecret", "blah");
cfg.setOptions(options);
cfg.signSessionConfig((SigningPrivateKey)(new SigningPrivateKeyTest()).createDataStructure());
return cfg;
}
public DataStructure createStructureToRead() { return new SessionConfig(); }
}

View File

@ -0,0 +1,30 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.SessionId;
/**
* Test harness for loading / storing SessionId objects
*
* @author jrandom
*/
class SessionIdTest extends StructureTest {
static {
TestData.registerTest(new SessionIdTest(), "SessionId");
}
public DataStructure createDataStructure() throws DataFormatException {
SessionId id = new SessionId();
id.setSessionId(7);
return id;
}
public DataStructure createStructureToRead() { return new SessionId(); }
}

View File

@ -0,0 +1,33 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.SessionKey;
/**
* Test harness for loading / storing SessionKey objects
*
* @author jrandom
*/
class SessionKeyTest extends StructureTest {
static {
TestData.registerTest(new SessionKeyTest(), "SessionKey");
}
public DataStructure createDataStructure() throws DataFormatException {
SessionKey key = new SessionKey();
byte data[] = new byte[SessionKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%16);
key.setData(data);
return key;
}
public DataStructure createStructureToRead() { return new SessionKey(); }
}

View File

@ -0,0 +1,32 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.i2cp.SessionId;
import net.i2p.data.i2cp.SessionStatusMessage;
/**
* Test harness for loading / storing SessionStatusMessage objects
*
* @author jrandom
*/
class SessionStatusMessageTest extends StructureTest {
static {
TestData.registerTest(new SessionStatusMessageTest(), "SessionStatusMessage");
}
public DataStructure createDataStructure() throws DataFormatException {
SessionStatusMessage msg = new SessionStatusMessage();
msg.setSessionId((SessionId)(new SessionIdTest()).createDataStructure());
msg.setStatus(SessionStatusMessage.STATUS_CREATED);
return msg;
}
public DataStructure createStructureToRead() { return new SessionStatusMessage(); }
}

View File

@ -0,0 +1,33 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.Signature;
/**
* Test harness for loading / storing Signature objects
*
* @author jrandom
*/
class SignatureTest extends StructureTest {
static {
TestData.registerTest(new SignatureTest(), "Signature");
}
public DataStructure createDataStructure() throws DataFormatException {
Signature sig = new Signature();
byte data[] = new byte[Signature.SIGNATURE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%16);
sig.setData(data);
return sig;
}
public DataStructure createStructureToRead() { return new Signature(); }
}

View File

@ -0,0 +1,33 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.SigningPrivateKey;
/**
* Test harness for loading / storing SigningPrivateKey objects
*
* @author jrandom
*/
class SigningPrivateKeyTest extends StructureTest {
static {
TestData.registerTest(new SigningPrivateKeyTest(), "SigningPrivateKey");
}
public DataStructure createDataStructure() throws DataFormatException {
SigningPrivateKey privateKey = 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;
}
public DataStructure createStructureToRead() { return new SigningPrivateKey(); }
}

View File

@ -0,0 +1,33 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.SigningPublicKey;
/**
* Test harness for loading / storing PublicKey objects
*
* @author jrandom
*/
class SigningPublicKeyTest extends StructureTest {
static {
TestData.registerTest(new SigningPublicKeyTest(), "SigningPublicKey");
}
public DataStructure createDataStructure() throws DataFormatException {
SigningPublicKey publicKey = new SigningPublicKey();
byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%16);
publicKey.setData(data);
return publicKey;
}
public DataStructure createStructureToRead() { return new SigningPublicKey(); }
}

View File

@ -0,0 +1,59 @@
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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.util.Log;
/**
* Test harness for the boolean 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 byte[] getData() {
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;
}
}
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

@ -0,0 +1,60 @@
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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.util.Log;
/**
* Utility class for wrapping data structure tests
*
* @author jrandom
*/
abstract class StructureTest implements TestDataGenerator, TestDataPrinter {
private static final Log _log = new Log(StructureTest.class);
public abstract DataStructure createDataStructure() throws DataFormatException;
public abstract DataStructure createStructureToRead();
public byte[] getData() {
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();
}
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

@ -0,0 +1,150 @@
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"; } });
}
static void registerTest(StructureTest test, String name) {
registerGenerator(test, name);
registerPrinter(test, name);
}
static void registerGenerator(TestDataGenerator test, String name) {
_generators.put(name, test);
}
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

@ -0,0 +1,16 @@
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

@ -0,0 +1,18 @@
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

@ -0,0 +1,30 @@
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 net.i2p.data.DataFormatException;
import net.i2p.data.DataStructure;
import net.i2p.data.TunnelId;
/**
* Test harness for loading / storing TunnelId objects
*
* @author jrandom
*/
class TunnelIdTest extends StructureTest {
static {
TestData.registerTest(new TunnelIdTest(), "TunnelId");
}
public DataStructure createDataStructure() throws DataFormatException {
TunnelId id = new TunnelId();
id.setTunnelId(42);
return id;
}
public DataStructure createStructureToRead() { return new TunnelId(); }
}

View File

@ -0,0 +1,59 @@
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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.util.Log;
/**
* Test harness for the numerical structure (in java, an UnsignedInteger)
*
* @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 byte[] getData() {
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;
}
}
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;
}
}
}