Migrate net.i2p.data tests to JUnit 4

This commit is contained in:
str4d
2017-12-02 17:25:09 +00:00
parent c36905a309
commit 60efd0b426
21 changed files with 485 additions and 432 deletions

View File

@ -17,6 +17,9 @@ sourceSets {
test { test {
java { java {
srcDir 'java/test/junit' srcDir 'java/test/junit'
exclude 'net/i2p/AllCoreTests.java'
exclude 'net/i2p/data/DataTestSuite.java'
exclude 'net/i2p/data/i2cp/I2CPTestSuite.java'
} }
resources { resources {
srcDir 'java/test/junit' srcDir 'java/test/junit'

View File

@ -8,23 +8,19 @@ package net.i2p;
* *
*/ */
import junit.framework.Test; import org.junit.runners.Suite;
import junit.framework.TestSuite; import org.junit.runner.RunWith;
/** /**
* @author Comwiz * @author str4d
*/ */
@RunWith(Suite.class)
@Suite.SuiteClasses({
net.i2p.client.I2PClientTestSuite.class,
net.i2p.crypto.CryptoTestSuite.class,
net.i2p.data.DataTestSuite.class,
net.i2p.stat.StatTestSuite.class,
net.i2p.util.UtilTestSuite.class,
})
public class AllCoreTests { public class AllCoreTests {
}
public static Test suite() {
TestSuite suite = new TestSuite("net.i2p.AllCoreTests");
suite.addTest(net.i2p.client.I2PClientTestSuite.suite());
suite.addTest(net.i2p.crypto.CryptoTestSuite.suite());
suite.addTest(net.i2p.data.DataTestSuite.suite());
suite.addTest(net.i2p.stat.StatTestSuite.suite());
suite.addTest(net.i2p.util.UtilTestSuite.suite());
return suite;
}
}

View File

@ -8,16 +8,19 @@ package net.i2p.data;
* *
*/ */
import junit.framework.TestCase; import static org.junit.Assert.*;
import org.junit.Test;
public class Base64Test extends TestCase{ public class Base64Test {
@Test
public void testBase64(){ public void testBase64(){
String orig = "you smell"; String orig = "you smell";
String encoded = Base64.encode(DataHelper.getASCII(orig)); String encoded = Base64.encode(DataHelper.getASCII(orig));
byte decoded[] = Base64.decode(encoded); byte decoded[] = Base64.decode(encoded);
String transformed = new String(decoded); String transformed = new String(decoded);
assertTrue(orig.equals(transformed)); assertTrue(orig.equals(transformed));
byte all[] = new byte[256]; byte all[] = new byte[256];
for (int i = 0; i < all.length; i++) for (int i = 0; i < all.length; i++)
all[i] = (byte) (0xFF & i); all[i] = (byte) (0xFF & i);

View File

@ -8,34 +8,36 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import junit.framework.TestCase; import org.junit.Test;
/** /**
* Test harness for the boolean structure * Test harness for the boolean structure
* *
* @author jrandom * @author jrandom
*/ */
public class BooleanTest extends TestCase{ public class BooleanTest {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Test
public void testBoolean() throws Exception{ public void testBoolean() throws Exception{
byte[] temp = null; byte[] temp = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataHelper.writeBoolean(baos, Boolean.TRUE); DataHelper.writeBoolean(baos, Boolean.TRUE);
temp = baos.toByteArray(); temp = baos.toByteArray();
Boolean b = null; Boolean b = null;
ByteArrayInputStream bais = new ByteArrayInputStream(temp); ByteArrayInputStream bais = new ByteArrayInputStream(temp);
b = DataHelper.readBoolean(bais); b = DataHelper.readBoolean(bais);
assertEquals(Boolean.TRUE, b); assertEquals(Boolean.TRUE, b);
} }
} }

View File

@ -1,5 +1,7 @@
package net.i2p.data; package net.i2p.data;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -8,18 +10,19 @@ import java.util.Date;
import java.util.Random; import java.util.Random;
import java.util.TimeZone; import java.util.TimeZone;
import junit.framework.TestCase; import org.junit.Test;
/** /**
* basic unit tests for the DataHelper * basic unit tests for the DataHelper
* *
*/ */
public class DataHelperTest extends TestCase{ public class DataHelperTest {
/** /**
* Test to/from/read/writeLong with every 1, 2, and 4 byte value, as * Test to/from/read/writeLong with every 1, 2, and 4 byte value, as
* well as some 8 byte values. * well as some 8 byte values.
*/ */
@Test
public void testLong() throws Exception{ public void testLong() throws Exception{
for (int i = 0; i <= 0xFF; i+=4) for (int i = 0; i <= 0xFF; i+=4)
checkLong(1, i); checkLong(1, i);
@ -43,18 +46,19 @@ public class DataHelperTest extends TestCase{
byte extract2[] = new byte[numBytes]; byte extract2[] = new byte[numBytes];
DataHelper.toLong(extract2, 0, numBytes, value); DataHelper.toLong(extract2, 0, numBytes, value);
assertTrue(DataHelper.eq(extract, extract2)); assertTrue(DataHelper.eq(extract, extract2));
long read = DataHelper.fromLong(extract, 0, numBytes); long read = DataHelper.fromLong(extract, 0, numBytes);
assertTrue(read == value); assertTrue(read == value);
ByteArrayInputStream bais = new ByteArrayInputStream(written); ByteArrayInputStream bais = new ByteArrayInputStream(written);
read = DataHelper.readLong(bais, numBytes); read = DataHelper.readLong(bais, numBytes);
assertTrue(read == value); assertTrue(read == value);
read = DataHelper.fromLong(written, 0, numBytes); read = DataHelper.fromLong(written, 0, numBytes);
assertTrue(read == value); assertTrue(read == value);
} }
@Test
public void testDate() throws Exception{ public void testDate() throws Exception{
TimeZone.setDefault(TimeZone.getTimeZone("UTC")); TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
@ -65,10 +69,10 @@ public class DataHelperTest extends TestCase{
cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0); cal.set(Calendar.SECOND, 0);
checkDate(cal.getTime()); checkDate(cal.getTime());
cal.set(Calendar.SECOND, 1); cal.set(Calendar.SECOND, 1);
checkDate(cal.getTime()); checkDate(cal.getTime());
cal.set(Calendar.YEAR, 1999); cal.set(Calendar.YEAR, 1999);
cal.set(Calendar.MONTH, 11); cal.set(Calendar.MONTH, 11);
cal.set(Calendar.DAY_OF_MONTH, 31); cal.set(Calendar.DAY_OF_MONTH, 31);
@ -76,7 +80,7 @@ public class DataHelperTest extends TestCase{
cal.set(Calendar.MINUTE, 59); cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59); cal.set(Calendar.SECOND, 59);
checkDate(cal.getTime()); checkDate(cal.getTime());
cal.set(Calendar.YEAR, 2000); cal.set(Calendar.YEAR, 2000);
cal.set(Calendar.MONTH, 0); cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.DAY_OF_MONTH, 1);
@ -84,10 +88,10 @@ public class DataHelperTest extends TestCase{
cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0); cal.set(Calendar.SECOND, 0);
checkDate(cal.getTime()); checkDate(cal.getTime());
cal.setTimeInMillis(System.currentTimeMillis()); cal.setTimeInMillis(System.currentTimeMillis());
checkDate(cal.getTime()); checkDate(cal.getTime());
cal.set(Calendar.SECOND, cal.get(Calendar.SECOND)+10); cal.set(Calendar.SECOND, cal.get(Calendar.SECOND)+10);
checkDate(cal.getTime()); checkDate(cal.getTime());
@ -105,7 +109,7 @@ public class DataHelperTest extends TestCase{
} }
assertTrue(error); assertTrue(error);
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private void checkDate(Date when) throws Exception{ private void checkDate(Date when) throws Exception{
byte buf[] = new byte[DataHelper.DATE_LENGTH]; byte buf[] = new byte[DataHelper.DATE_LENGTH];
@ -115,7 +119,8 @@ public class DataHelperTest extends TestCase{
Date time = DataHelper.fromDate(buf, 0); Date time = DataHelper.fromDate(buf, 0);
assertEquals(when.getTime(), time.getTime()); assertEquals(when.getTime(), time.getTime());
} }
@Test
public void testCompress() throws Exception{ public void testCompress() throws Exception{
Random r = new Random(); Random r = new Random();
for (int size = 0; size < 32*1024; size+=32){ // Original had size++, changed value because for (int size = 0; size < 32*1024; size+=32){ // Original had size++, changed value because
@ -125,10 +130,10 @@ public class DataHelperTest extends TestCase{
byte compressed[] = DataHelper.compress(data); byte compressed[] = DataHelper.compress(data);
byte decompressed[] = DataHelper.decompress(compressed); byte decompressed[] = DataHelper.decompress(compressed);
assertTrue(DataHelper.eq(data, decompressed)); assertTrue(DataHelper.eq(data, decompressed));
} }
} }
@Test
public void testSkip() throws Exception { public void testSkip() throws Exception {
final int sz = 256; final int sz = 256;
TestInputStream tis = new TestInputStream(sz); TestInputStream tis = new TestInputStream(sz);
@ -137,9 +142,9 @@ public class DataHelperTest extends TestCase{
DataHelper.skip(tis, 1); DataHelper.skip(tis, 1);
fail(); fail();
} catch (IOException ioe) {} } catch (IOException ioe) {}
DataHelper.skip(tis, 0); DataHelper.skip(tis, 0);
try { try {
DataHelper.skip(tis, -1); DataHelper.skip(tis, -1);
fail("skipped negative?"); fail("skipped negative?");

View File

@ -8,65 +8,75 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import junit.framework.TestCase; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/** /**
* @author Comwiz * @author Comwiz
*/ */
public class DataStructureImplTest extends TestCase{ public class DataStructureImplTest {
DataStructure _struct; DataStructure _struct;
protected void setUp(){ @Rule
public ExpectedException exception = ExpectedException.none();
@Before
public void setUp(){
_struct = new DataStructureImpl(){ _struct = new DataStructureImpl(){
private int x = 0; private int x = 0;
public void writeBytes(OutputStream out) throws IOException, DataFormatException{ public void writeBytes(OutputStream out) throws IOException, DataFormatException{
if(x++==0) if(x++==0)
throw new DataFormatException("let it enfold you", new Exception()); throw new DataFormatException("let it enfold you", new Exception());
else else
throw new IOException(); throw new IOException();
} }
public void readBytes(InputStream in) throws IOException{ public void readBytes(InputStream in) throws IOException{
throw new IOException(); throw new IOException();
} }
}; };
} }
public void testNulls() throws Exception{ @Test
public void toBase64ReturnsNull() throws Exception{
assertNull(_struct.toBase64()); 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{ @Test
boolean error = false; public void fromBase64ThrowsOnNull() throws Exception{
try{ exception.expect(DataFormatException.class);
_struct.fromByteArray(DataHelper.getASCII("water is poison")); exception.expectMessage("Null data passed in");
}catch(DataFormatException dfe){ _struct.fromBase64(null);
error = true; }
}
assertTrue(error); @Test
public void calculateHashReturnsNull() throws Exception{
assertNull(_struct.toByteArray()); assertNull(_struct.calculateHash());
}
@Test
public void fromByteArrayThrowsOnNull() throws Exception{
exception.expect(DataFormatException.class);
exception.expectMessage("Null data passed in");
_struct.fromByteArray(null);
}
@Test
public void fromByteArrayThrowsOnError() throws Exception{
exception.expect(DataFormatException.class);
exception.expectMessage("Error reading the byte array");
_struct.fromByteArray(DataHelper.getASCII("water is poison"));
}
@Test
public void toByteArrayReturnsNullOnError() throws Exception{
assertNull(_struct.toByteArray()); assertNull(_struct.toByteArray());
} }
} }

View File

@ -1,36 +1,31 @@
package net.i2p.data; package net.i2p.data;
import junit.framework.Test; import org.junit.runners.Suite;
import junit.framework.TestSuite; import org.junit.runner.RunWith;
@RunWith(Suite.class)
@Suite.SuiteClasses({
Base64Test.class,
BooleanTest.class,
CertificateTest.class,
DataHelperTest.class,
DataStructureImplTest.class,
DateTest.class,
DestinationTest.class,
HashTest.class,
LeaseSetTest.class,
LeaseTest.class,
MappingTest.class,
PayloadTest.class,
PrivateKeyTest.class,
PublicKeyTest.class,
SessionKeyTest.class,
SignatureTest.class,
SigningPrivateKeyTest.class,
SigningPublicKeyTest.class,
StringTest.class,
TunnelIdTest.class,
UnsignedIntegerTest.class,
})
public class DataTestSuite { public class DataTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite("net.i2p.data.DataTestSuite");
suite.addTestSuite(Base64Test.class);
suite.addTestSuite(BooleanTest.class);
suite.addTestSuite(CertificateTest.class);
suite.addTestSuite(DataHelperTest.class);
suite.addTestSuite(DataStructureImplTest.class);
suite.addTestSuite(DateTest.class);
suite.addTestSuite(DestinationTest.class);
suite.addTestSuite(HashTest.class);
suite.addTestSuite(LeaseSetTest.class);
suite.addTestSuite(LeaseTest.class);
suite.addTestSuite(MappingTest.class);
suite.addTestSuite(PayloadTest.class);
suite.addTestSuite(PrivateKeyTest.class);
suite.addTestSuite(PublicKeyTest.class);
suite.addTestSuite(SessionKeyTest.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

@ -8,35 +8,35 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Date; import java.util.Date;
import junit.framework.TestCase; import org.junit.Test;
/** /**
* Test harness for the date structure * Test harness for the date structure
* *
* @author jrandom * @author jrandom
*/ */
public class DateTest extends TestCase{ public class DateTest {
@Test
public void testDate() throws Exception{ public void testDate() throws Exception{
byte[] temp = null; byte[] temp = null;
Date orig = new Date(); Date orig = new Date();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataHelper.writeDate(baos, orig); DataHelper.writeDate(baos, orig);
temp = baos.toByteArray(); temp = baos.toByteArray();
Date d = null; Date d = null;
ByteArrayInputStream bais = new ByteArrayInputStream(temp); ByteArrayInputStream bais = new ByteArrayInputStream(temp);
d = DataHelper.readDate(bais); d = DataHelper.readDate(bais);
assertEquals(orig, d); assertEquals(orig, d);
} }
}
}

View File

@ -10,14 +10,16 @@ package net.i2p.data;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import net.i2p.crypto.EncType; import net.i2p.crypto.EncType;
import net.i2p.crypto.SigType; import net.i2p.crypto.SigType;
import junit.framework.TestCase;
import org.junit.Test;
/** /**
* @author str4d * @author str4d
*/ */
public class KeyCertificateTest extends TestCase { public class KeyCertificateTest {
private static final byte[] P256_PAYLOAD = new byte[] { private static final byte[] P256_PAYLOAD = new byte[] {
0, (byte) (SigType.ECDSA_SHA256_P256.getCode()), 0, (byte) (SigType.ECDSA_SHA256_P256.getCode()),
0, (byte) (EncType.EC_P256.getCode()) 0, (byte) (EncType.EC_P256.getCode())
@ -29,6 +31,7 @@ public class KeyCertificateTest extends TestCase {
0, 0, 0, 0 0, 0, 0, 0
}; };
@Test
public void testFromP256Payload() throws DataFormatException { public void testFromP256Payload() throws DataFormatException {
KeyCertificate cert = new KeyCertificate(P256_PAYLOAD); KeyCertificate cert = new KeyCertificate(P256_PAYLOAD);
assertThat(cert.getSigTypeCode(), is(equalTo(SigType.ECDSA_SHA256_P256.getCode()))); assertThat(cert.getSigTypeCode(), is(equalTo(SigType.ECDSA_SHA256_P256.getCode())));
@ -36,10 +39,11 @@ public class KeyCertificateTest extends TestCase {
assertThat(cert.getExtraSigningKeyData(), is(nullValue())); assertThat(cert.getExtraSigningKeyData(), is(nullValue()));
} }
@Test
public void testFromEd25519Payload() throws DataFormatException { public void testFromEd25519Payload() throws DataFormatException {
KeyCertificate cert = new KeyCertificate(P521_PAYLOAD); KeyCertificate cert = new KeyCertificate(P521_PAYLOAD);
assertThat(cert.getSigTypeCode(), is(equalTo(SigType.ECDSA_SHA512_P521.getCode()))); assertThat(cert.getSigTypeCode(), is(equalTo(SigType.ECDSA_SHA512_P521.getCode())));
assertThat(cert.getCryptoTypeCode(), is(equalTo(EncType.ELGAMAL_2048.getCode()))); assertThat(cert.getCryptoTypeCode(), is(equalTo(EncType.ELGAMAL_2048.getCode())));
assertThat(cert.getExtraSigningKeyData().length, is(4)); assertThat(cert.getExtraSigningKeyData().length, is(4));
} }
} }

View File

@ -8,6 +8,11 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/** /**
* Test harness for loading / storing Lease objects * Test harness for loading / storing Lease objects
@ -15,6 +20,10 @@ package net.i2p.data;
* @author jrandom * @author jrandom
*/ */
public class LeaseSetTest extends StructureTest { public class LeaseSetTest extends StructureTest {
@Rule
public ExpectedException exception = ExpectedException.none();
public DataStructure createDataStructure() throws DataFormatException { public DataStructure createDataStructure() throws DataFormatException {
LeaseSet leaseSet = new LeaseSet(); LeaseSet leaseSet = new LeaseSet();
leaseSet.setDestination((Destination)(new DestinationTest()).createDataStructure()); leaseSet.setDestination((Destination)(new DestinationTest()).createDataStructure());
@ -25,50 +34,48 @@ public class LeaseSetTest extends StructureTest {
return leaseSet; return leaseSet;
} }
public DataStructure createStructureToRead() { return new LeaseSet(); } public DataStructure createStructureToRead() { return new LeaseSet(); }
public void testGetLeaseInvalid() { @Test
public void failsToGetLeaseWhenEmpty() {
// create test subject // create test subject
LeaseSet subj = new LeaseSet(); LeaseSet subj = new LeaseSet();
// should contain no leases now.. // should contain no leases now.
try { exception.expect(IndexOutOfBoundsException.class);
assertNull(subj.getLease(0)); exception.expectMessage("Index: 0, Size: 0");
} catch(RuntimeException exc) { subj.getLease(0);
// all good
}
// this shouldn't work either
try {
assertNull(subj.getLease(-1));
} catch(RuntimeException exc) {
// all good
}
} }
@Test
public void failsToGetInvalidLease() {
// create test subject
LeaseSet subj = new LeaseSet();
// this shouldn't work either
exception.expect(IndexOutOfBoundsException.class);
exception.expectMessage("-1");
subj.getLease(-1);
}
@Test
public void testAddLeaseNull() { public void testAddLeaseNull() {
// create test subject // create test subject
LeaseSet subj = new LeaseSet(); LeaseSet subj = new LeaseSet();
// now add an null lease // now add an null lease
try { exception.expect(IllegalArgumentException.class);
subj.addLease(null); exception.expectMessage("erm, null lease");
fail("Failed at failing."); subj.addLease(null);
} catch(IllegalArgumentException exc) {
// all good
}
} }
@Test
public void testAddLeaseInvalid() { public void testAddLeaseInvalid() {
// create test subject // create test subject
LeaseSet subj = new LeaseSet(); LeaseSet subj = new LeaseSet();
// try to add completely invalid lease(ie. no data) // try to add completely invalid lease(ie. no data)
try { exception.expect(IllegalArgumentException.class);
subj.addLease(new Lease()); exception.expectMessage("erm, lease has no gateway");
fail("Failed at failing."); subj.addLease(new Lease());
} catch(IllegalArgumentException exc) {
// all good
}
} }
} }

View File

@ -8,15 +8,25 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Date; import java.util.Date;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/** /**
* Test harness for loading / storing Lease objects * Test harness for loading / storing Lease objects
* *
* @author jrandom * @author jrandom
*/ */
public class LeaseTest extends StructureTest { public class LeaseTest extends StructureTest {
@Rule
public ExpectedException exception = ExpectedException.none();
public DataStructure createDataStructure() throws DataFormatException { public DataStructure createDataStructure() throws DataFormatException {
Lease lease = new Lease(); Lease lease = new Lease();
lease.setEndDate(new Date(1000*60*2)); lease.setEndDate(new Date(1000*60*2));
@ -24,7 +34,7 @@ public class LeaseTest extends StructureTest {
lease.setGateway(new Hash(h)); lease.setGateway(new Hash(h));
StructureTest tst = new TunnelIdTest(); StructureTest tst = new TunnelIdTest();
lease.setTunnelId((TunnelId)tst.createDataStructure()); lease.setTunnelId((TunnelId)tst.createDataStructure());
return lease; return lease;
} }
public DataStructure createStructureToRead() { return new Lease(); } public DataStructure createStructureToRead() { return new Lease(); }
@ -37,54 +47,54 @@ public class LeaseTest extends StructureTest {
lease.setGateway(new Hash(h)); lease.setGateway(new Hash(h));
StructureTest tst = new TunnelIdTest(); StructureTest tst = new TunnelIdTest();
lease.setTunnelId((TunnelId)tst.createDataStructure()); lease.setTunnelId((TunnelId)tst.createDataStructure());
lease.getNumSuccess(); lease.getNumSuccess();
lease.getNumFailure(); lease.getNumFailure();
} }
*/ */
@Test
public void testExpiration() throws Exception{ public void testExpiration() throws Exception{
Lease lease = new Lease(); Lease lease = new Lease();
assertTrue(lease.isExpired()); assertTrue(lease.isExpired());
lease.setEndDate(new Date(1000*60*2)); lease.setEndDate(new Date(1000*60*2));
byte h[] = new byte[Hash.HASH_LENGTH]; byte h[] = new byte[Hash.HASH_LENGTH];
lease.setGateway(new Hash(h)); lease.setGateway(new Hash(h));
StructureTest tst = new TunnelIdTest(); StructureTest tst = new TunnelIdTest();
lease.setTunnelId((TunnelId)tst.createDataStructure()); lease.setTunnelId((TunnelId)tst.createDataStructure());
assertTrue(lease.isExpired()); assertTrue(lease.isExpired());
} }
public void testNullWrite() throws Exception{ @Test
public void failsWriteWithNullTunnelId() throws Exception{
Lease lease = new Lease(); Lease lease = new Lease();
lease.setEndDate(new Date(1000*60*2)); lease.setEndDate(new Date(1000*60*2));
byte h[] = new byte[Hash.HASH_LENGTH]; byte h[] = new byte[Hash.HASH_LENGTH];
lease.setGateway(new Hash(h)); lease.setGateway(new Hash(h));
lease.setTunnelId(null); lease.setTunnelId(null);
boolean error = false;
try{ exception.expect(DataFormatException.class);
lease.writeBytes(new ByteArrayOutputStream()); exception.expectMessage("Not enough data to write out a Lease");
}catch(DataFormatException dfe){ lease.writeBytes(new ByteArrayOutputStream());
error = true; }
}
assertTrue(error); @Test
public void failsWriteWithNullGateway() throws Exception{
lease = new Lease(); Lease lease = new Lease();
lease.setEndDate(new Date(1000*60*2)); lease.setEndDate(new Date(1000*60*2));
h = new byte[Hash.HASH_LENGTH]; byte h[] = new byte[Hash.HASH_LENGTH];
lease.setGateway(null); lease.setGateway(null);
StructureTest tst = new TunnelIdTest(); StructureTest tst = new TunnelIdTest();
lease.setTunnelId((TunnelId)tst.createDataStructure()); lease.setTunnelId((TunnelId)tst.createDataStructure());
error = false;
try{ exception.expect(DataFormatException.class);
lease.writeBytes(new ByteArrayOutputStream()); exception.expectMessage("Not enough data to write out a Lease");
}catch(DataFormatException dfe){ lease.writeBytes(new ByteArrayOutputStream());
error = true;
}
assertTrue(error);
} }
@Test
public void testNullEquals() throws Exception{ public void testNullEquals() throws Exception{
Lease lease = new Lease(); Lease lease = new Lease();
lease.setEndDate(new Date(1000*60*2)); lease.setEndDate(new Date(1000*60*2));
@ -93,5 +103,4 @@ public class LeaseTest extends StructureTest {
lease.setTunnelId(null); lease.setTunnelId(null);
assertFalse(lease.equals(null)); assertFalse(lease.equals(null));
} }
} }

View File

@ -8,38 +8,38 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Properties; import java.util.Properties;
import junit.framework.TestCase; import org.junit.Test;
/** /**
* Test harness for the date structure * Test harness for the date structure
* *
* @author jrandom * @author jrandom
*/ */
public class MappingTest extends TestCase{ public class MappingTest {
@Test
public void testProperties() throws Exception{ public void testProperties() throws Exception{
byte[] temp = null; byte[] temp = null;
Properties orig = new Properties(); Properties orig = new Properties();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
orig.setProperty("key1", "val1"); orig.setProperty("key1", "val1");
orig.setProperty("key2", "val2"); orig.setProperty("key2", "val2");
orig.setProperty("key3", "val3"); orig.setProperty("key3", "val3");
DataHelper.writeProperties(baos, orig); DataHelper.writeProperties(baos, orig);
temp = baos.toByteArray(); temp = baos.toByteArray();
Properties p = null; Properties p = null;
ByteArrayInputStream bais = new ByteArrayInputStream(temp); ByteArrayInputStream bais = new ByteArrayInputStream(temp);
p = DataHelper.readProperties(bais); p = DataHelper.readProperties(bais);
assertEquals(orig, p); assertEquals(orig, p);
} }
}
}

View File

@ -8,15 +8,25 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/** /**
* Test harness for loading / storing PrivateKey objects * Test harness for loading / storing PrivateKey objects
* *
* @author jrandom * @author jrandom
*/ */
public class PrivateKeyTest extends StructureTest { public class PrivateKeyTest extends StructureTest {
@Rule
public ExpectedException exception = ExpectedException.none();
public DataStructure createDataStructure() throws DataFormatException { public DataStructure createDataStructure() throws DataFormatException {
PrivateKey privateKey = new PrivateKey(); PrivateKey privateKey = new PrivateKey();
byte data[] = new byte[PrivateKey.KEYSIZE_BYTES]; byte data[] = new byte[PrivateKey.KEYSIZE_BYTES];
@ -26,69 +36,59 @@ public class PrivateKeyTest extends StructureTest {
return privateKey; return privateKey;
} }
public DataStructure createStructureToRead() { return new PrivateKey(); } public DataStructure createStructureToRead() { return new PrivateKey(); }
@Test
public void testBase64Constructor() throws Exception{ public void testBase64Constructor() throws Exception{
PrivateKey privateKey = new PrivateKey(); PrivateKey privateKey = new PrivateKey();
byte data[] = new byte[PrivateKey.KEYSIZE_BYTES]; byte data[] = new byte[PrivateKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56); data[i] = (byte)(i%56);
privateKey.setData(data); privateKey.setData(data);
PrivateKey key2 = new PrivateKey(privateKey.toBase64()); PrivateKey key2 = new PrivateKey(privateKey.toBase64());
assertEquals(privateKey, key2); assertEquals(privateKey, key2);
} }
@Test
public void testNullEquals(){ public void testNullEquals(){
PrivateKey privateKey = new PrivateKey(); PrivateKey privateKey = new PrivateKey();
byte data[] = new byte[PrivateKey.KEYSIZE_BYTES]; byte data[] = new byte[PrivateKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56); data[i] = (byte)(i%56);
privateKey.setData(data); privateKey.setData(data);
assertFalse(privateKey.equals(null)); assertFalse(privateKey.equals(null));
} }
@Test
public void testNullData() throws Exception{ public void testNullData() throws Exception{
PrivateKey privateKey = new PrivateKey(); PrivateKey privateKey = new PrivateKey();
privateKey.toString(); privateKey.toString();
boolean error = false; exception.expect(DataFormatException.class);
try{ exception.expectMessage("No data to write out");
privateKey.writeBytes(new ByteArrayOutputStream()); privateKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
} }
@Test
public void testShortData() throws Exception{ public void testShortData() throws Exception{
PrivateKey privateKey = new PrivateKey(); PrivateKey privateKey = new PrivateKey();
byte data[] = new byte[56]; byte data[] = new byte[56];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i); data[i] = (byte)(i);
boolean error = false; exception.expect(IllegalArgumentException.class);
try{ exception.expectMessage("Bad data length: 56; required: " + PrivateKey.KEYSIZE_BYTES);
privateKey.setData(data); privateKey.setData(data);
privateKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}catch(IllegalArgumentException exc) {
error = true;
}
assertTrue(error);
} }
@Test
public void testShortRead() throws Exception{ public void testShortRead() throws Exception{
PrivateKey privateKey = new PrivateKey(); PrivateKey privateKey = new PrivateKey();
ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two")); ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two"));
boolean error = false;
try{ exception.expect(DataFormatException.class);
privateKey.readBytes(in); exception.expectMessage("EOF reading PrivateKey, read: 31, required: " + PrivateKey.KEYSIZE_BYTES);
}catch(DataFormatException dfe){ privateKey.readBytes(in);
error = true;
}
assertTrue(error);
} }
} }

View File

@ -8,15 +8,25 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/** /**
* Test harness for loading / storing PublicKey objects * Test harness for loading / storing PublicKey objects
* *
* @author jrandom * @author jrandom
*/ */
public class PublicKeyTest extends StructureTest { public class PublicKeyTest extends StructureTest {
@Rule
public ExpectedException exception = ExpectedException.none();
public DataStructure createDataStructure() throws DataFormatException { public DataStructure createDataStructure() throws DataFormatException {
PublicKey publicKey = new PublicKey(); PublicKey publicKey = new PublicKey();
byte data[] = new byte[PublicKey.KEYSIZE_BYTES]; byte data[] = new byte[PublicKey.KEYSIZE_BYTES];
@ -26,68 +36,60 @@ public class PublicKeyTest extends StructureTest {
return publicKey; return publicKey;
} }
public DataStructure createStructureToRead() { return new PublicKey(); } public DataStructure createStructureToRead() { return new PublicKey(); }
@Test
public void testBase64Constructor() throws Exception{ public void testBase64Constructor() throws Exception{
PublicKey publicKey = new PublicKey(); PublicKey publicKey = new PublicKey();
byte data[] = new byte[PublicKey.KEYSIZE_BYTES]; byte data[] = new byte[PublicKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56); data[i] = (byte)(i%56);
publicKey.setData(data); publicKey.setData(data);
PublicKey key2 = new PublicKey(publicKey.toBase64()); PublicKey key2 = new PublicKey(publicKey.toBase64());
assertEquals(publicKey, key2); assertEquals(publicKey, key2);
} }
@Test
public void testNullEquals(){ public void testNullEquals(){
PublicKey publicKey = new PublicKey(); PublicKey publicKey = new PublicKey();
byte data[] = new byte[PublicKey.KEYSIZE_BYTES]; byte data[] = new byte[PublicKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56); data[i] = (byte)(i%56);
publicKey.setData(data); publicKey.setData(data);
assertFalse(publicKey.equals(null)); assertFalse(publicKey.equals(null));
} }
@Test
public void testNullData() throws Exception{ public void testNullData() throws Exception{
PublicKey publicKey = new PublicKey(); PublicKey publicKey = new PublicKey();
publicKey.toString(); publicKey.toString();
boolean error = false; exception.expect(DataFormatException.class);
try{ exception.expectMessage("No data to write out");
publicKey.writeBytes(new ByteArrayOutputStream()); publicKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
} }
@Test
public void testShortData() throws Exception{ public void testShortData() throws Exception{
PublicKey publicKey = new PublicKey(); PublicKey publicKey = new PublicKey();
byte data[] = new byte[56]; byte data[] = new byte[56];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i); data[i] = (byte)(i);
boolean error = false; exception.expect(IllegalArgumentException.class);
try{ exception.expectMessage("Bad data length: 56; required: " + PublicKey.KEYSIZE_BYTES);
publicKey.setData(data); publicKey.setData(data);
publicKey.writeBytes(new ByteArrayOutputStream()); publicKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}catch(IllegalArgumentException exc) {
error = true;
}
assertTrue(error);
} }
@Test
public void testShortRead() throws Exception{ public void testShortRead() throws Exception{
PublicKey publicKey = new PublicKey(); PublicKey publicKey = new PublicKey();
ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two")); ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two"));
boolean error = false;
try{ exception.expect(DataFormatException.class);
publicKey.readBytes(in); exception.expectMessage("EOF reading PublicKey, read: 31, required: " + PublicKey.KEYSIZE_BYTES);
}catch(DataFormatException dfe){ publicKey.readBytes(in);
error = true;
}
assertTrue(error);
} }
} }

View File

@ -8,15 +8,25 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/** /**
* Test harness for loading / storing SigningPrivateKey objects * Test harness for loading / storing SigningPrivateKey objects
* *
* @author jrandom * @author jrandom
*/ */
public class SigningPrivateKeyTest extends StructureTest { public class SigningPrivateKeyTest extends StructureTest {
@Rule
public ExpectedException exception = ExpectedException.none();
public DataStructure createDataStructure() throws DataFormatException { public DataStructure createDataStructure() throws DataFormatException {
SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES]; byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES];
@ -26,70 +36,60 @@ public class SigningPrivateKeyTest extends StructureTest {
return signingPrivateKey; return signingPrivateKey;
} }
public DataStructure createStructureToRead() { return new SigningPrivateKey(); } public DataStructure createStructureToRead() { return new SigningPrivateKey(); }
@Test
public void testBase64Constructor() throws Exception{ public void testBase64Constructor() throws Exception{
SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES]; byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56); data[i] = (byte)(i%56);
signingPrivateKey.setData(data); signingPrivateKey.setData(data);
SigningPrivateKey key2 = new SigningPrivateKey(signingPrivateKey.toBase64()); SigningPrivateKey key2 = new SigningPrivateKey(signingPrivateKey.toBase64());
assertEquals(signingPrivateKey, key2); assertEquals(signingPrivateKey, key2);
} }
@Test
public void testNullEquals(){ public void testNullEquals(){
SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES]; byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56); data[i] = (byte)(i%56);
signingPrivateKey.setData(data); signingPrivateKey.setData(data);
assertFalse(signingPrivateKey.equals(null)); assertFalse(signingPrivateKey.equals(null));
} }
@Test
public void testNullData() throws Exception{ public void testNullData() throws Exception{
SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
signingPrivateKey.toString(); signingPrivateKey.toString();
boolean error = false; exception.expect(DataFormatException.class);
try{ exception.expectMessage("No data to write out");
signingPrivateKey.writeBytes(new ByteArrayOutputStream()); signingPrivateKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
} }
@Test
public void testShortData() throws Exception{ public void testShortData() throws Exception{
SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
byte data[] = new byte[56]; byte data[] = new byte[56];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i); data[i] = (byte)(i);
boolean error = false; exception.expect(IllegalArgumentException.class);
try{ exception.expectMessage("Bad data length: 56; required: " + SigningPrivateKey.KEYSIZE_BYTES);
signingPrivateKey.setData(data); signingPrivateKey.setData(data);
signingPrivateKey.writeBytes(new ByteArrayOutputStream()); signingPrivateKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}catch(IllegalArgumentException exc) {
error = true;
}
assertTrue(error);
} }
@Test
public void testShortRead() throws Exception{ public void testShortRead() throws Exception{
SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("short")); ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("short"));
boolean error = false;
try{ exception.expect(DataFormatException.class);
signingPrivateKey.readBytes(in); exception.expectMessage("EOF reading SigningPrivateKey, read: 5, required: " + SigningPrivateKey.KEYSIZE_BYTES);
}catch(DataFormatException dfe){ signingPrivateKey.readBytes(in);
error = true;
}
assertTrue(error);
} }
} }

View File

@ -8,15 +8,25 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/** /**
* Test harness for loading / storing PublicKey objects * Test harness for loading / storing PublicKey objects
* *
* @author jrandom * @author jrandom
*/ */
public class SigningPublicKeyTest extends StructureTest { public class SigningPublicKeyTest extends StructureTest {
@Rule
public ExpectedException exception = ExpectedException.none();
public DataStructure createDataStructure() throws DataFormatException { public DataStructure createDataStructure() throws DataFormatException {
SigningPublicKey publicKey = new SigningPublicKey(); SigningPublicKey publicKey = new SigningPublicKey();
byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES]; byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES];
@ -26,70 +36,60 @@ public class SigningPublicKeyTest extends StructureTest {
return publicKey; return publicKey;
} }
public DataStructure createStructureToRead() { return new SigningPublicKey(); } public DataStructure createStructureToRead() { return new SigningPublicKey(); }
@Test
public void testBase64Constructor() throws Exception{ public void testBase64Constructor() throws Exception{
SigningPublicKey publicKey = new SigningPublicKey(); SigningPublicKey publicKey = new SigningPublicKey();
byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES]; byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56); data[i] = (byte)(i%56);
publicKey.setData(data); publicKey.setData(data);
SigningPublicKey key2 = new SigningPublicKey(publicKey.toBase64()); SigningPublicKey key2 = new SigningPublicKey(publicKey.toBase64());
assertEquals(publicKey, key2); assertEquals(publicKey, key2);
} }
@Test
public void testNullEquals(){ public void testNullEquals(){
SigningPublicKey publicKey = new SigningPublicKey(); SigningPublicKey publicKey = new SigningPublicKey();
byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES]; byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i%56); data[i] = (byte)(i%56);
publicKey.setData(data); publicKey.setData(data);
assertFalse(publicKey.equals(null)); assertFalse(publicKey.equals(null));
} }
@Test
public void testNullData() throws Exception{ public void testNullData() throws Exception{
SigningPublicKey publicKey = new SigningPublicKey(); SigningPublicKey publicKey = new SigningPublicKey();
publicKey.toString(); publicKey.toString();
boolean error = false; exception.expect(DataFormatException.class);
try{ exception.expectMessage("No data to write out");
publicKey.writeBytes(new ByteArrayOutputStream()); publicKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
} }
@Test
public void testShortData() throws Exception{ public void testShortData() throws Exception{
SigningPublicKey publicKey = new SigningPublicKey(); SigningPublicKey publicKey = new SigningPublicKey();
byte data[] = new byte[56]; byte data[] = new byte[56];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
data[i] = (byte)(i); data[i] = (byte)(i);
boolean error = false; exception.expect(IllegalArgumentException.class);
try{ exception.expectMessage("Bad data length: 56; required: " + SigningPublicKey.KEYSIZE_BYTES);
publicKey.setData(data); publicKey.setData(data);
publicKey.writeBytes(new ByteArrayOutputStream()); publicKey.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}catch(IllegalArgumentException exc) {
error = true;
}
assertTrue(error);
} }
@Test
public void testShortRead() throws Exception{ public void testShortRead() throws Exception{
SigningPublicKey publicKey = new SigningPublicKey(); SigningPublicKey publicKey = new SigningPublicKey();
ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two")); ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two"));
boolean error = false;
try{ exception.expect(DataFormatException.class);
publicKey.readBytes(in); exception.expectMessage("EOF reading SigningPublicKey, read: 31, required: " + SigningPublicKey.KEYSIZE_BYTES);
}catch(DataFormatException dfe){ publicKey.readBytes(in);
error = true;
}
assertTrue(error);
} }
} }

View File

@ -8,49 +8,64 @@ package net.i2p.data;
* *
*/ */
import junit.framework.TestCase; import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/** /**
* Test harness for the simple data structure * Test harness for the simple data structure
* *
* @author welterde * @author welterde
*/ */
public class SimpleDataStructureTest extends TestCase { public class SimpleDataStructureTest {
public void testSetDataImmutable() throws Exception { @Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void setDataThrowsOnNullAfterDataSet() throws Exception {
// create new test subject // create new test subject
TestStruct struct = new TestStruct(); TestStruct struct = new TestStruct();
// try to set null object.. should not fail.. // try to set null object.. should not fail..
struct.setData(null); struct.setData(null);
// set data to something // set data to something
struct.setData(new byte[3]); struct.setData(new byte[3]);
// now setting it to null should fail // now setting it to null should fail
try { exception.expect(RuntimeException.class);
struct.setData(null); exception.expectMessage("Data already set");
fail("Should not have allowed us to change this.."); struct.setData(null);
} catch(RuntimeException exc) {
// all good
}
// setting it to something non-null should fail as well.
try {
struct.setData(new byte[3]);
fail("Should not have allowed us to change this..");
} catch(RuntimeException exc) {
// all good
}
} }
@Test
public void setDataThrowsOnDataAfterDataSet() throws Exception {
// create new test subject
TestStruct struct = new TestStruct();
// try to set null object.. should not fail..
struct.setData(null);
// set data to something
struct.setData(new byte[3]);
// setting it to something non-null should fail.
exception.expect(RuntimeException.class);
exception.expectMessage("Data already set");
struct.setData(new byte[3]);
}
@Test
public void testReadBytesImmutable() throws Exception { public void testReadBytesImmutable() throws Exception {
// create new test subject // create new test subject
TestStruct struct = new TestStruct(); TestStruct struct = new TestStruct();
// load some data using setData // load some data using setData
struct.setData(new byte[3]); struct.setData(new byte[3]);
// now try to load via readBytes // now try to load via readBytes
try { try {
struct.readBytes(null); struct.readBytes(null);
@ -59,35 +74,37 @@ public class SimpleDataStructureTest extends TestCase {
// all good // all good
} }
} }
@Test
public void testToBase64Safe() throws Exception { public void testToBase64Safe() throws Exception {
// create new test subject // create new test subject
TestStruct struct = new TestStruct(); TestStruct struct = new TestStruct();
// now try to get the Base64.. should not throw an exception, but should not be an empty string either // now try to get the Base64.. should not throw an exception, but should not be an empty string either
assertNull(struct.toBase64()); assertNull(struct.toBase64());
} }
@Test
public void testCalculateHashSafe() throws Exception { public void testCalculateHashSafe() throws Exception {
// create new test subject // create new test subject
TestStruct struct = new TestStruct(); TestStruct struct = new TestStruct();
// now try to get the hash.. should not throw an exception // now try to get the hash.. should not throw an exception
assertNull(struct.calculateHash()); assertNull(struct.calculateHash());
} }
@Test
public void testHashCodeSafe() throws Exception { public void testHashCodeSafe() throws Exception {
// create new test subject // create new test subject
TestStruct struct = new TestStruct(); TestStruct struct = new TestStruct();
// just make sure it doesn't explode in our face // just make sure it doesn't explode in our face
struct.hashCode(); struct.hashCode();
} }
public class TestStruct extends SimpleDataStructure { public class TestStruct extends SimpleDataStructure {
public int length() { public int length() {
return 3; return 3;
} }
} }
}
}

View File

@ -8,33 +8,35 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import junit.framework.TestCase; import org.junit.Test;
/** /**
* Test harness for the date structure * Test harness for the date structure
* *
* @author jrandom * @author jrandom
*/ */
public class StringTest extends TestCase{ public class StringTest {
@Test
public void testString() throws Exception{ public void testString() throws Exception{
byte[] temp = null; byte[] temp = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataHelper.writeString(baos, "Hello, I2P"); DataHelper.writeString(baos, "Hello, I2P");
temp = baos.toByteArray(); temp = baos.toByteArray();
String s = null; String s = null;
ByteArrayInputStream bais = new ByteArrayInputStream(temp); ByteArrayInputStream bais = new ByteArrayInputStream(temp);
s = DataHelper.readString(bais); s = DataHelper.readString(bais);
assertEquals(s, "Hello, I2P"); assertEquals(s, "Hello, I2P");
} }
} }

View File

@ -8,10 +8,12 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import junit.framework.TestCase; import org.junit.Test;
/** /**
* Utility class for wrapping data structure tests * Utility class for wrapping data structure tests
@ -19,32 +21,33 @@ import junit.framework.TestCase;
* @author jrandom * @author jrandom
*/ */
public abstract class StructureTest extends TestCase{ public abstract class StructureTest {
/** create a populated structure for writing */ /** create a populated structure for writing */
public abstract DataStructure createDataStructure() throws DataFormatException; public abstract DataStructure createDataStructure() throws DataFormatException;
/** create an unpopulated structure for reading */ /** create an unpopulated structure for reading */
public abstract DataStructure createStructureToRead(); public abstract DataStructure createStructureToRead();
@Test
public void testStructure() throws Exception{ public void testStructure() throws Exception{
byte[] temp = null; byte[] temp = null;
DataStructure orig; DataStructure orig;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
orig = createDataStructure(); orig = createDataStructure();
orig.writeBytes(baos); orig.writeBytes(baos);
temp = baos.toByteArray(); temp = baos.toByteArray();
DataStructure ds; DataStructure ds;
ByteArrayInputStream bais = new ByteArrayInputStream(temp); ByteArrayInputStream bais = new ByteArrayInputStream(temp);
ds = createStructureToRead(); ds = createStructureToRead();
ds.readBytes(bais); ds.readBytes(bais);
// I2CP message classes don't implement equals() // I2CP message classes don't implement equals()
if (!getClass().getName().startsWith("net.i2p.data.i2cp.")) if (!getClass().getName().startsWith("net.i2p.data.i2cp."))
@ -56,5 +59,4 @@ public abstract class StructureTest extends TestCase{
byte[] temp2 = baos2.toByteArray(); byte[] temp2 = baos2.toByteArray();
assert(DataHelper.eq(temp, temp2)); assert(DataHelper.eq(temp, temp2));
} }
} }

View File

@ -8,32 +8,33 @@ package net.i2p.data;
* *
*/ */
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import junit.framework.TestCase; import org.junit.Test;
/** /**
* Test harness for the date structure * Test harness for the date structure
* *
* @author jrandom * @author jrandom
*/ */
public class UnsignedIntegerTest extends TestCase{ public class UnsignedIntegerTest {
@Test
public void testLong() throws Exception{ public void testLong() throws Exception{
byte[] temp = null; byte[] temp = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataHelper.writeLong(baos, 4, 42); DataHelper.writeLong(baos, 4, 42);
temp = baos.toByteArray(); temp = baos.toByteArray();
long l; long l;
ByteArrayInputStream bais = new ByteArrayInputStream(temp); ByteArrayInputStream bais = new ByteArrayInputStream(temp);
l = DataHelper.readLong(bais, 4); l = DataHelper.readLong(bais, 4);
assertEquals(42, l); assertEquals(42, l);
} }
} }

View File

@ -1,40 +1,35 @@
package net.i2p.data.i2cp; package net.i2p.data.i2cp;
import junit.framework.Test; import org.junit.runners.Suite;
import junit.framework.TestSuite; import org.junit.runner.RunWith;
@RunWith(Suite.class)
@Suite.SuiteClasses({
AbuseReasonTest.class,
AbuseSeverityTest.class,
BandwidthLimitsMessageTest.class,
CreateLeaseSetMessageTest.class,
CreateSessionMessageTest.class,
DestLookupMessageTest.class,
DestReplyMessageTest.class,
DestroySessionMessageTest.class,
DisconnectMessageTest.class,
GetBandwidthLimitsMessageTest.class,
GetDateMessageTest.class,
MessageIdTest.class,
MessagePayloadMessageTest.class,
MessageStatusMessageTest.class,
ReceiveMessageBeginMessageTest.class,
ReceiveMessageEndMessageTest.class,
ReconfigureSessionMessageTest.class,
ReportAbuseMessageTest.class,
RequestLeaseSetMessageTest.class,
SendMessageExpiresMessageTest.class,
SendMessageMessageTest.class,
SessionConfigTest.class,
SessionIdTest.class,
SessionStatusMessageTest.class,
SetDateMessageTest.class,
})
public class I2CPTestSuite { public class I2CPTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite("net.i2p.data.i2cp.I2CPTestSuite");
suite.addTestSuite(AbuseReasonTest.class);
suite.addTestSuite(AbuseSeverityTest.class);
suite.addTestSuite(BandwidthLimitsMessageTest.class);
suite.addTestSuite(CreateLeaseSetMessageTest.class);
suite.addTestSuite(CreateSessionMessageTest.class);
suite.addTestSuite(DestLookupMessageTest.class);
suite.addTestSuite(DestReplyMessageTest.class);
suite.addTestSuite(DestroySessionMessageTest.class);
suite.addTestSuite(DisconnectMessageTest.class);
suite.addTestSuite(GetBandwidthLimitsMessageTest.class);
suite.addTestSuite(GetDateMessageTest.class);
suite.addTestSuite(MessageIdTest.class);
suite.addTestSuite(MessagePayloadMessageTest.class);
suite.addTestSuite(MessageStatusMessageTest.class);
suite.addTestSuite(ReceiveMessageBeginMessageTest.class);
suite.addTestSuite(ReceiveMessageEndMessageTest.class);
suite.addTestSuite(ReconfigureSessionMessageTest.class);
suite.addTestSuite(ReportAbuseMessageTest.class);
suite.addTestSuite(RequestLeaseSetMessageTest.class);
suite.addTestSuite(SendMessageExpiresMessageTest.class);
suite.addTestSuite(SendMessageMessageTest.class);
suite.addTestSuite(SessionConfigTest.class);
suite.addTestSuite(SessionIdTest.class);
suite.addTestSuite(SessionStatusMessageTest.class);
suite.addTestSuite(SetDateMessageTest.class);
return suite;
}
} }