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,9 +8,12 @@ package net.i2p.data;
* *
*/ */
import junit.framework.TestCase; import static org.junit.Assert.*;
public class Base64Test extends TestCase{ import org.junit.Test;
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));

View File

@ -8,19 +8,21 @@ 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;

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);
@ -55,6 +58,7 @@ public class DataHelperTest extends TestCase{
} }
@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"));
@ -116,6 +120,7 @@ public class DataHelperTest extends TestCase{
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);

View File

@ -8,19 +8,28 @@ 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{
@ -35,38 +44,39 @@ public class DataStructureImplTest extends TestCase{
}; };
} }
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);
assertNull(_struct.toByteArray()); @Test
public void calculateHashReturnsNull() throws Exception{
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,19 +8,21 @@ 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;
@ -30,7 +32,6 @@ public class DateTest extends TestCase{
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);
@ -38,5 +39,4 @@ public class DateTest extends TestCase{
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,6 +39,7 @@ 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())));

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());
@ -26,49 +35,47 @@ public class LeaseSetTest extends StructureTest {
} }
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));
@ -43,6 +53,7 @@ public class LeaseTest extends StructureTest {
} }
*/ */
@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());
@ -56,35 +67,34 @@ public class LeaseTest extends StructureTest {
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{
lease.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
lease = new Lease(); exception.expect(DataFormatException.class);
exception.expectMessage("Not enough data to write out a Lease");
lease.writeBytes(new ByteArrayOutputStream());
}
@Test
public void failsWriteWithNullGateway() throws Exception{
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,19 +8,21 @@ 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;
@ -33,7 +35,6 @@ public class MappingTest extends TestCase{
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);
@ -41,5 +42,4 @@ public class MappingTest extends TestCase{
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];
@ -27,6 +37,7 @@ public class PrivateKeyTest extends StructureTest {
} }
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];
@ -38,6 +49,7 @@ public class PrivateKeyTest extends StructureTest {
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];
@ -48,47 +60,35 @@ public class PrivateKeyTest extends StructureTest {
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{
privateKey.readBytes(in);
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
exception.expect(DataFormatException.class);
exception.expectMessage("EOF reading PrivateKey, read: 31, required: " + PrivateKey.KEYSIZE_BYTES);
privateKey.readBytes(in);
}
} }

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];
@ -27,6 +37,7 @@ public class PublicKeyTest extends StructureTest {
} }
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];
@ -38,6 +49,7 @@ public class PublicKeyTest extends StructureTest {
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];
@ -48,46 +60,36 @@ public class PublicKeyTest extends StructureTest {
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];
@ -27,6 +37,7 @@ public class SigningPrivateKeyTest extends StructureTest {
} }
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];
@ -38,6 +49,7 @@ public class SigningPrivateKeyTest extends StructureTest {
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];
@ -48,48 +60,36 @@ public class SigningPrivateKeyTest extends StructureTest {
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];
@ -27,6 +37,7 @@ public class SigningPublicKeyTest extends StructureTest {
} }
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];
@ -38,6 +49,7 @@ public class SigningPublicKeyTest extends StructureTest {
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];
@ -48,48 +60,36 @@ public class SigningPublicKeyTest extends StructureTest {
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{
publicKey.readBytes(in);
}catch(DataFormatException dfe){
error = true;
}
assertTrue(error);
}
exception.expect(DataFormatException.class);
exception.expectMessage("EOF reading SigningPublicKey, read: 31, required: " + SigningPublicKey.KEYSIZE_BYTES);
publicKey.readBytes(in);
}
} }

View File

@ -8,16 +8,24 @@ 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();
@ -28,22 +36,29 @@ public class SimpleDataStructureTest extends TestCase {
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();
@ -60,6 +75,7 @@ public class SimpleDataStructureTest extends TestCase {
} }
} }
@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();
@ -68,6 +84,7 @@ public class SimpleDataStructureTest extends TestCase {
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();
@ -76,6 +93,7 @@ public class SimpleDataStructureTest extends TestCase {
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();
@ -89,5 +107,4 @@ public class SimpleDataStructureTest extends TestCase {
return 3; return 3;
} }
} }
} }

View File

@ -8,18 +8,20 @@ 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;

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,7 +21,7 @@ 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;
@ -27,6 +29,7 @@ public abstract class StructureTest extends TestCase{
/** 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;
@ -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,18 +8,20 @@ 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;
@ -28,7 +30,6 @@ public class UnsignedIntegerTest extends TestCase{
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);

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;
}
} }