diff --git a/core/build.gradle b/core/build.gradle index 53eaf64cdf..369c592559 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -17,6 +17,9 @@ sourceSets { test { java { srcDir 'java/test/junit' + exclude 'net/i2p/AllCoreTests.java' + exclude 'net/i2p/data/DataTestSuite.java' + exclude 'net/i2p/data/i2cp/I2CPTestSuite.java' } resources { srcDir 'java/test/junit' diff --git a/core/java/test/junit/net/i2p/AllCoreTests.java b/core/java/test/junit/net/i2p/AllCoreTests.java index d53278f743..ae7c340cb2 100644 --- a/core/java/test/junit/net/i2p/AllCoreTests.java +++ b/core/java/test/junit/net/i2p/AllCoreTests.java @@ -8,23 +8,19 @@ package net.i2p; * */ -import junit.framework.Test; -import junit.framework.TestSuite; +import org.junit.runners.Suite; +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 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; - } -} \ No newline at end of file +} diff --git a/core/java/test/junit/net/i2p/data/Base64Test.java b/core/java/test/junit/net/i2p/data/Base64Test.java index b8c57db00d..636dea9e84 100644 --- a/core/java/test/junit/net/i2p/data/Base64Test.java +++ b/core/java/test/junit/net/i2p/data/Base64Test.java @@ -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(){ String orig = "you smell"; String encoded = Base64.encode(DataHelper.getASCII(orig)); byte decoded[] = Base64.decode(encoded); String transformed = new String(decoded); assertTrue(orig.equals(transformed)); - + byte all[] = new byte[256]; for (int i = 0; i < all.length; i++) all[i] = (byte) (0xFF & i); diff --git a/core/java/test/junit/net/i2p/data/BooleanTest.java b/core/java/test/junit/net/i2p/data/BooleanTest.java index b0d449e5ea..418be5e81a 100644 --- a/core/java/test/junit/net/i2p/data/BooleanTest.java +++ b/core/java/test/junit/net/i2p/data/BooleanTest.java @@ -8,34 +8,36 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import junit.framework.TestCase; +import org.junit.Test; /** * Test harness for the boolean structure * * @author jrandom */ -public class BooleanTest extends TestCase{ - +public class BooleanTest { @SuppressWarnings("deprecation") + @Test public void testBoolean() throws Exception{ byte[] temp = null; - + ByteArrayOutputStream baos = new ByteArrayOutputStream(); - + DataHelper.writeBoolean(baos, Boolean.TRUE); temp = baos.toByteArray(); - - + + Boolean b = null; ByteArrayInputStream bais = new ByteArrayInputStream(temp); - + b = DataHelper.readBoolean(bais); - + assertEquals(Boolean.TRUE, b); } - + } diff --git a/core/java/test/junit/net/i2p/data/DataHelperTest.java b/core/java/test/junit/net/i2p/data/DataHelperTest.java index ea2d2dd871..29c729527d 100644 --- a/core/java/test/junit/net/i2p/data/DataHelperTest.java +++ b/core/java/test/junit/net/i2p/data/DataHelperTest.java @@ -1,5 +1,7 @@ package net.i2p.data; +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -8,18 +10,19 @@ import java.util.Date; import java.util.Random; import java.util.TimeZone; -import junit.framework.TestCase; +import org.junit.Test; /** * 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 * well as some 8 byte values. */ + @Test public void testLong() throws Exception{ for (int i = 0; i <= 0xFF; i+=4) checkLong(1, i); @@ -43,18 +46,19 @@ public class DataHelperTest extends TestCase{ byte extract2[] = new byte[numBytes]; DataHelper.toLong(extract2, 0, numBytes, value); assertTrue(DataHelper.eq(extract, extract2)); - + long read = DataHelper.fromLong(extract, 0, numBytes); assertTrue(read == value); - + ByteArrayInputStream bais = new ByteArrayInputStream(written); read = DataHelper.readLong(bais, numBytes); assertTrue(read == value); read = DataHelper.fromLong(written, 0, numBytes); assertTrue(read == value); - + } + @Test public void testDate() throws Exception{ TimeZone.setDefault(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.SECOND, 0); checkDate(cal.getTime()); - + cal.set(Calendar.SECOND, 1); checkDate(cal.getTime()); - + cal.set(Calendar.YEAR, 1999); cal.set(Calendar.MONTH, 11); cal.set(Calendar.DAY_OF_MONTH, 31); @@ -76,7 +80,7 @@ public class DataHelperTest extends TestCase{ cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); checkDate(cal.getTime()); - + cal.set(Calendar.YEAR, 2000); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH, 1); @@ -84,10 +88,10 @@ public class DataHelperTest extends TestCase{ cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); checkDate(cal.getTime()); - + cal.setTimeInMillis(System.currentTimeMillis()); checkDate(cal.getTime()); - + cal.set(Calendar.SECOND, cal.get(Calendar.SECOND)+10); checkDate(cal.getTime()); @@ -105,7 +109,7 @@ public class DataHelperTest extends TestCase{ } assertTrue(error); } - + @SuppressWarnings("deprecation") private void checkDate(Date when) throws Exception{ byte buf[] = new byte[DataHelper.DATE_LENGTH]; @@ -115,7 +119,8 @@ public class DataHelperTest extends TestCase{ Date time = DataHelper.fromDate(buf, 0); assertEquals(when.getTime(), time.getTime()); } - + + @Test public void testCompress() throws Exception{ Random r = new Random(); 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 decompressed[] = DataHelper.decompress(compressed); assertTrue(DataHelper.eq(data, decompressed)); - } } + @Test public void testSkip() throws Exception { final int sz = 256; TestInputStream tis = new TestInputStream(sz); @@ -137,9 +142,9 @@ public class DataHelperTest extends TestCase{ DataHelper.skip(tis, 1); fail(); } catch (IOException ioe) {} - + DataHelper.skip(tis, 0); - + try { DataHelper.skip(tis, -1); fail("skipped negative?"); diff --git a/core/java/test/junit/net/i2p/data/DataStructureImplTest.java b/core/java/test/junit/net/i2p/data/DataStructureImplTest.java index b03cac488a..f37dcd8ff4 100644 --- a/core/java/test/junit/net/i2p/data/DataStructureImplTest.java +++ b/core/java/test/junit/net/i2p/data/DataStructureImplTest.java @@ -8,65 +8,75 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.IOException; import java.io.InputStream; 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 */ -public class DataStructureImplTest extends TestCase{ +public class DataStructureImplTest { DataStructure _struct; - - protected void setUp(){ + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Before + public void setUp(){ _struct = new DataStructureImpl(){ private int x = 0; public void writeBytes(OutputStream out) throws IOException, DataFormatException{ if(x++==0) throw new DataFormatException("let it enfold you", new Exception()); else - throw new IOException(); + throw new IOException(); } public void readBytes(InputStream in) throws IOException{ throw new IOException(); } }; } - - public void testNulls() throws Exception{ + + @Test + public void toBase64ReturnsNull() throws Exception{ assertNull(_struct.toBase64()); - - boolean error = false; - try{ - _struct.fromBase64(null); - }catch(DataFormatException dfe){ - error = true; - } - assertTrue(error); - - assertNull(_struct.calculateHash()); - - error = false; - try{ - _struct.fromByteArray(null); - }catch(DataFormatException dfe){ - error = true; - } - assertTrue(error); } - - public void testErrors() throws Exception{ - boolean error = false; - try{ - _struct.fromByteArray(DataHelper.getASCII("water is poison")); - }catch(DataFormatException dfe){ - error = true; - } - assertTrue(error); - - assertNull(_struct.toByteArray()); + + @Test + public void fromBase64ThrowsOnNull() throws Exception{ + exception.expect(DataFormatException.class); + exception.expectMessage("Null data passed in"); + _struct.fromBase64(null); + } + + @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()); } } diff --git a/core/java/test/junit/net/i2p/data/DataTestSuite.java b/core/java/test/junit/net/i2p/data/DataTestSuite.java index a3253916f1..d06e92b0ee 100644 --- a/core/java/test/junit/net/i2p/data/DataTestSuite.java +++ b/core/java/test/junit/net/i2p/data/DataTestSuite.java @@ -1,36 +1,31 @@ package net.i2p.data; -import junit.framework.Test; -import junit.framework.TestSuite; +import org.junit.runners.Suite; +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 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; - } } diff --git a/core/java/test/junit/net/i2p/data/DateTest.java b/core/java/test/junit/net/i2p/data/DateTest.java index b61cd3b39f..48d1a35d99 100644 --- a/core/java/test/junit/net/i2p/data/DateTest.java +++ b/core/java/test/junit/net/i2p/data/DateTest.java @@ -8,35 +8,35 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.Date; -import junit.framework.TestCase; +import org.junit.Test; /** * Test harness for the date structure * * @author jrandom */ -public class DateTest extends TestCase{ - +public class DateTest { + @Test public void testDate() throws Exception{ byte[] temp = null; - + Date orig = new Date(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - + DataHelper.writeDate(baos, orig); temp = baos.toByteArray(); - - + Date d = null; ByteArrayInputStream bais = new ByteArrayInputStream(temp); - + d = DataHelper.readDate(bais); - + assertEquals(orig, d); } - -} \ No newline at end of file +} diff --git a/core/java/test/junit/net/i2p/data/KeyCertificateTest.java b/core/java/test/junit/net/i2p/data/KeyCertificateTest.java index 032b3ef700..3c543da9a8 100644 --- a/core/java/test/junit/net/i2p/data/KeyCertificateTest.java +++ b/core/java/test/junit/net/i2p/data/KeyCertificateTest.java @@ -10,14 +10,16 @@ package net.i2p.data; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; + import net.i2p.crypto.EncType; import net.i2p.crypto.SigType; -import junit.framework.TestCase; + +import org.junit.Test; /** * @author str4d */ -public class KeyCertificateTest extends TestCase { +public class KeyCertificateTest { private static final byte[] P256_PAYLOAD = new byte[] { 0, (byte) (SigType.ECDSA_SHA256_P256.getCode()), 0, (byte) (EncType.EC_P256.getCode()) @@ -29,6 +31,7 @@ public class KeyCertificateTest extends TestCase { 0, 0, 0, 0 }; + @Test public void testFromP256Payload() throws DataFormatException { KeyCertificate cert = new KeyCertificate(P256_PAYLOAD); assertThat(cert.getSigTypeCode(), is(equalTo(SigType.ECDSA_SHA256_P256.getCode()))); @@ -36,10 +39,11 @@ public class KeyCertificateTest extends TestCase { assertThat(cert.getExtraSigningKeyData(), is(nullValue())); } + @Test public void testFromEd25519Payload() throws DataFormatException { KeyCertificate cert = new KeyCertificate(P521_PAYLOAD); assertThat(cert.getSigTypeCode(), is(equalTo(SigType.ECDSA_SHA512_P521.getCode()))); assertThat(cert.getCryptoTypeCode(), is(equalTo(EncType.ELGAMAL_2048.getCode()))); assertThat(cert.getExtraSigningKeyData().length, is(4)); } -} \ No newline at end of file +} diff --git a/core/java/test/junit/net/i2p/data/LeaseSetTest.java b/core/java/test/junit/net/i2p/data/LeaseSetTest.java index 30fb8c11fb..9147131c74 100644 --- a/core/java/test/junit/net/i2p/data/LeaseSetTest.java +++ b/core/java/test/junit/net/i2p/data/LeaseSetTest.java @@ -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 @@ -15,6 +20,10 @@ package net.i2p.data; * @author jrandom */ public class LeaseSetTest extends StructureTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + public DataStructure createDataStructure() throws DataFormatException { LeaseSet leaseSet = new LeaseSet(); leaseSet.setDestination((Destination)(new DestinationTest()).createDataStructure()); @@ -25,50 +34,48 @@ public class LeaseSetTest extends StructureTest { return leaseSet; } public DataStructure createStructureToRead() { return new LeaseSet(); } - - public void testGetLeaseInvalid() { + + @Test + public void failsToGetLeaseWhenEmpty() { // create test subject LeaseSet subj = new LeaseSet(); - - // should contain no leases now.. - try { - assertNull(subj.getLease(0)); - } catch(RuntimeException exc) { - // all good - } - - // this shouldn't work either - try { - assertNull(subj.getLease(-1)); - } catch(RuntimeException exc) { - // all good - } + + // should contain no leases now. + exception.expect(IndexOutOfBoundsException.class); + exception.expectMessage("Index: 0, Size: 0"); + subj.getLease(0); } - + + @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() { // create test subject LeaseSet subj = new LeaseSet(); - + // now add an null lease - try { - subj.addLease(null); - fail("Failed at failing."); - } catch(IllegalArgumentException exc) { - // all good - } + exception.expect(IllegalArgumentException.class); + exception.expectMessage("erm, null lease"); + subj.addLease(null); } - + + @Test public void testAddLeaseInvalid() { // create test subject LeaseSet subj = new LeaseSet(); - + // try to add completely invalid lease(ie. no data) - try { - subj.addLease(new Lease()); - fail("Failed at failing."); - } catch(IllegalArgumentException exc) { - // all good - } + exception.expect(IllegalArgumentException.class); + exception.expectMessage("erm, lease has no gateway"); + subj.addLease(new Lease()); } - } diff --git a/core/java/test/junit/net/i2p/data/LeaseTest.java b/core/java/test/junit/net/i2p/data/LeaseTest.java index bb13c149ef..34e6d8b58b 100644 --- a/core/java/test/junit/net/i2p/data/LeaseTest.java +++ b/core/java/test/junit/net/i2p/data/LeaseTest.java @@ -8,15 +8,25 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayOutputStream; import java.util.Date; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + /** * Test harness for loading / storing Lease objects * * @author jrandom */ public class LeaseTest extends StructureTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + public DataStructure createDataStructure() throws DataFormatException { Lease lease = new Lease(); lease.setEndDate(new Date(1000*60*2)); @@ -24,7 +34,7 @@ public class LeaseTest extends StructureTest { lease.setGateway(new Hash(h)); StructureTest tst = new TunnelIdTest(); lease.setTunnelId((TunnelId)tst.createDataStructure()); - + return lease; } public DataStructure createStructureToRead() { return new Lease(); } @@ -37,54 +47,54 @@ public class LeaseTest extends StructureTest { lease.setGateway(new Hash(h)); StructureTest tst = new TunnelIdTest(); lease.setTunnelId((TunnelId)tst.createDataStructure()); - + lease.getNumSuccess(); lease.getNumFailure(); } */ + @Test public void testExpiration() throws Exception{ Lease lease = new Lease(); assertTrue(lease.isExpired()); - + lease.setEndDate(new Date(1000*60*2)); byte h[] = new byte[Hash.HASH_LENGTH]; lease.setGateway(new Hash(h)); StructureTest tst = new TunnelIdTest(); lease.setTunnelId((TunnelId)tst.createDataStructure()); - + assertTrue(lease.isExpired()); } - - public void testNullWrite() throws Exception{ + + @Test + public void failsWriteWithNullTunnelId() throws Exception{ Lease lease = new Lease(); lease.setEndDate(new Date(1000*60*2)); byte h[] = new byte[Hash.HASH_LENGTH]; lease.setGateway(new Hash(h)); lease.setTunnelId(null); - boolean error = false; - try{ - lease.writeBytes(new ByteArrayOutputStream()); - }catch(DataFormatException dfe){ - error = true; - } - assertTrue(error); - - lease = new Lease(); + + 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)); - h = new byte[Hash.HASH_LENGTH]; + byte h[] = new byte[Hash.HASH_LENGTH]; lease.setGateway(null); StructureTest tst = new TunnelIdTest(); lease.setTunnelId((TunnelId)tst.createDataStructure()); - error = false; - try{ - lease.writeBytes(new ByteArrayOutputStream()); - }catch(DataFormatException dfe){ - error = true; - } - assertTrue(error); + + exception.expect(DataFormatException.class); + exception.expectMessage("Not enough data to write out a Lease"); + lease.writeBytes(new ByteArrayOutputStream()); } - + + @Test public void testNullEquals() throws Exception{ Lease lease = new Lease(); lease.setEndDate(new Date(1000*60*2)); @@ -93,5 +103,4 @@ public class LeaseTest extends StructureTest { lease.setTunnelId(null); assertFalse(lease.equals(null)); } - } diff --git a/core/java/test/junit/net/i2p/data/MappingTest.java b/core/java/test/junit/net/i2p/data/MappingTest.java index 0d2a393e68..fd2d730dfc 100644 --- a/core/java/test/junit/net/i2p/data/MappingTest.java +++ b/core/java/test/junit/net/i2p/data/MappingTest.java @@ -8,38 +8,38 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.Properties; -import junit.framework.TestCase; +import org.junit.Test; /** * Test harness for the date structure * * @author jrandom */ -public class MappingTest extends TestCase{ - +public class MappingTest { + @Test public void testProperties() throws Exception{ byte[] temp = null; - + Properties orig = new Properties(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); orig.setProperty("key1", "val1"); orig.setProperty("key2", "val2"); orig.setProperty("key3", "val3"); - + DataHelper.writeProperties(baos, orig); temp = baos.toByteArray(); - - + Properties p = null; ByteArrayInputStream bais = new ByteArrayInputStream(temp); - + p = DataHelper.readProperties(bais); - + assertEquals(orig, p); } - -} \ No newline at end of file +} diff --git a/core/java/test/junit/net/i2p/data/PrivateKeyTest.java b/core/java/test/junit/net/i2p/data/PrivateKeyTest.java index f2581b3d1a..53a6d89af4 100644 --- a/core/java/test/junit/net/i2p/data/PrivateKeyTest.java +++ b/core/java/test/junit/net/i2p/data/PrivateKeyTest.java @@ -8,15 +8,25 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + /** * Test harness for loading / storing PrivateKey objects * * @author jrandom */ public class PrivateKeyTest extends StructureTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + public DataStructure createDataStructure() throws DataFormatException { PrivateKey privateKey = new PrivateKey(); byte data[] = new byte[PrivateKey.KEYSIZE_BYTES]; @@ -26,69 +36,59 @@ public class PrivateKeyTest extends StructureTest { return privateKey; } public DataStructure createStructureToRead() { return new PrivateKey(); } - + + @Test public void testBase64Constructor() throws Exception{ PrivateKey privateKey = new PrivateKey(); byte data[] = new byte[PrivateKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); privateKey.setData(data); - + PrivateKey key2 = new PrivateKey(privateKey.toBase64()); assertEquals(privateKey, key2); } - + + @Test public void testNullEquals(){ PrivateKey privateKey = new PrivateKey(); byte data[] = new byte[PrivateKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); privateKey.setData(data); - + assertFalse(privateKey.equals(null)); } - + + @Test public void testNullData() throws Exception{ PrivateKey privateKey = new PrivateKey(); privateKey.toString(); - - boolean error = false; - try{ - privateKey.writeBytes(new ByteArrayOutputStream()); - }catch(DataFormatException dfe){ - error = true; - } - assertTrue(error); + + exception.expect(DataFormatException.class); + exception.expectMessage("No data to write out"); + privateKey.writeBytes(new ByteArrayOutputStream()); } - + + @Test public void testShortData() throws Exception{ PrivateKey privateKey = new PrivateKey(); byte data[] = new byte[56]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i); - - boolean error = false; - try{ - privateKey.setData(data); - privateKey.writeBytes(new ByteArrayOutputStream()); - }catch(DataFormatException dfe){ - error = true; - }catch(IllegalArgumentException exc) { - error = true; - } - assertTrue(error); + + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Bad data length: 56; required: " + PrivateKey.KEYSIZE_BYTES); + privateKey.setData(data); } - + + @Test public void testShortRead() throws Exception{ PrivateKey privateKey = new PrivateKey(); 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); } - } diff --git a/core/java/test/junit/net/i2p/data/PublicKeyTest.java b/core/java/test/junit/net/i2p/data/PublicKeyTest.java index 4e404a9555..9115aacc28 100644 --- a/core/java/test/junit/net/i2p/data/PublicKeyTest.java +++ b/core/java/test/junit/net/i2p/data/PublicKeyTest.java @@ -8,15 +8,25 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + /** * Test harness for loading / storing PublicKey objects * * @author jrandom */ public class PublicKeyTest extends StructureTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + public DataStructure createDataStructure() throws DataFormatException { PublicKey publicKey = new PublicKey(); byte data[] = new byte[PublicKey.KEYSIZE_BYTES]; @@ -26,68 +36,60 @@ public class PublicKeyTest extends StructureTest { return publicKey; } public DataStructure createStructureToRead() { return new PublicKey(); } - + + @Test public void testBase64Constructor() throws Exception{ PublicKey publicKey = new PublicKey(); byte data[] = new byte[PublicKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); publicKey.setData(data); - + PublicKey key2 = new PublicKey(publicKey.toBase64()); assertEquals(publicKey, key2); } - + + @Test public void testNullEquals(){ PublicKey publicKey = new PublicKey(); byte data[] = new byte[PublicKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); publicKey.setData(data); - + assertFalse(publicKey.equals(null)); } - + + @Test public void testNullData() throws Exception{ PublicKey publicKey = new PublicKey(); publicKey.toString(); - - boolean error = false; - try{ - publicKey.writeBytes(new ByteArrayOutputStream()); - }catch(DataFormatException dfe){ - error = true; - } - assertTrue(error); + + exception.expect(DataFormatException.class); + exception.expectMessage("No data to write out"); + publicKey.writeBytes(new ByteArrayOutputStream()); } - + + @Test public void testShortData() throws Exception{ PublicKey publicKey = new PublicKey(); byte data[] = new byte[56]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i); - - boolean error = false; - try{ - publicKey.setData(data); - publicKey.writeBytes(new ByteArrayOutputStream()); - }catch(DataFormatException dfe){ - error = true; - }catch(IllegalArgumentException exc) { - error = true; - } - assertTrue(error); + + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Bad data length: 56; required: " + PublicKey.KEYSIZE_BYTES); + publicKey.setData(data); + publicKey.writeBytes(new ByteArrayOutputStream()); } - + + @Test public void testShortRead() throws Exception{ PublicKey publicKey = new PublicKey(); 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 PublicKey, read: 31, required: " + PublicKey.KEYSIZE_BYTES); + publicKey.readBytes(in); } } diff --git a/core/java/test/junit/net/i2p/data/SigningPrivateKeyTest.java b/core/java/test/junit/net/i2p/data/SigningPrivateKeyTest.java index 4ccfc10c93..c40cf725d4 100644 --- a/core/java/test/junit/net/i2p/data/SigningPrivateKeyTest.java +++ b/core/java/test/junit/net/i2p/data/SigningPrivateKeyTest.java @@ -8,15 +8,25 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + /** * Test harness for loading / storing SigningPrivateKey objects * * @author jrandom */ public class SigningPrivateKeyTest extends StructureTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + public DataStructure createDataStructure() throws DataFormatException { SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES]; @@ -26,70 +36,60 @@ public class SigningPrivateKeyTest extends StructureTest { return signingPrivateKey; } public DataStructure createStructureToRead() { return new SigningPrivateKey(); } - + + @Test public void testBase64Constructor() throws Exception{ SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); signingPrivateKey.setData(data); - + SigningPrivateKey key2 = new SigningPrivateKey(signingPrivateKey.toBase64()); assertEquals(signingPrivateKey, key2); } - + + @Test public void testNullEquals(){ SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); signingPrivateKey.setData(data); - + assertFalse(signingPrivateKey.equals(null)); } - + + @Test public void testNullData() throws Exception{ SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); signingPrivateKey.toString(); - - boolean error = false; - try{ - signingPrivateKey.writeBytes(new ByteArrayOutputStream()); - }catch(DataFormatException dfe){ - error = true; - } - assertTrue(error); + + exception.expect(DataFormatException.class); + exception.expectMessage("No data to write out"); + signingPrivateKey.writeBytes(new ByteArrayOutputStream()); } - + + @Test public void testShortData() throws Exception{ SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); byte data[] = new byte[56]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i); - - boolean error = false; - try{ - signingPrivateKey.setData(data); - signingPrivateKey.writeBytes(new ByteArrayOutputStream()); - }catch(DataFormatException dfe){ - error = true; - }catch(IllegalArgumentException exc) { - error = true; - } - assertTrue(error); + + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Bad data length: 56; required: " + SigningPrivateKey.KEYSIZE_BYTES); + signingPrivateKey.setData(data); + signingPrivateKey.writeBytes(new ByteArrayOutputStream()); } - + + @Test public void testShortRead() throws Exception{ SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("short")); - boolean error = false; - try{ - signingPrivateKey.readBytes(in); - }catch(DataFormatException dfe){ - error = true; - } - assertTrue(error); + + exception.expect(DataFormatException.class); + exception.expectMessage("EOF reading SigningPrivateKey, read: 5, required: " + SigningPrivateKey.KEYSIZE_BYTES); + signingPrivateKey.readBytes(in); } - - } diff --git a/core/java/test/junit/net/i2p/data/SigningPublicKeyTest.java b/core/java/test/junit/net/i2p/data/SigningPublicKeyTest.java index 1e5718b42a..6398b4a868 100644 --- a/core/java/test/junit/net/i2p/data/SigningPublicKeyTest.java +++ b/core/java/test/junit/net/i2p/data/SigningPublicKeyTest.java @@ -8,15 +8,25 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + /** * Test harness for loading / storing PublicKey objects * * @author jrandom */ public class SigningPublicKeyTest extends StructureTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + public DataStructure createDataStructure() throws DataFormatException { SigningPublicKey publicKey = new SigningPublicKey(); byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES]; @@ -26,70 +36,60 @@ public class SigningPublicKeyTest extends StructureTest { return publicKey; } public DataStructure createStructureToRead() { return new SigningPublicKey(); } - + + @Test public void testBase64Constructor() throws Exception{ SigningPublicKey publicKey = new SigningPublicKey(); byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); publicKey.setData(data); - + SigningPublicKey key2 = new SigningPublicKey(publicKey.toBase64()); assertEquals(publicKey, key2); } - + + @Test public void testNullEquals(){ SigningPublicKey publicKey = new SigningPublicKey(); byte data[] = new byte[SigningPublicKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); publicKey.setData(data); - + assertFalse(publicKey.equals(null)); } - + + @Test public void testNullData() throws Exception{ SigningPublicKey publicKey = new SigningPublicKey(); publicKey.toString(); - - boolean error = false; - try{ - publicKey.writeBytes(new ByteArrayOutputStream()); - }catch(DataFormatException dfe){ - error = true; - } - assertTrue(error); + + exception.expect(DataFormatException.class); + exception.expectMessage("No data to write out"); + publicKey.writeBytes(new ByteArrayOutputStream()); } - + + @Test public void testShortData() throws Exception{ SigningPublicKey publicKey = new SigningPublicKey(); byte data[] = new byte[56]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i); - - boolean error = false; - try{ - publicKey.setData(data); - publicKey.writeBytes(new ByteArrayOutputStream()); - }catch(DataFormatException dfe){ - error = true; - }catch(IllegalArgumentException exc) { - error = true; - } - - assertTrue(error); + + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Bad data length: 56; required: " + SigningPublicKey.KEYSIZE_BYTES); + publicKey.setData(data); + publicKey.writeBytes(new ByteArrayOutputStream()); } - + + @Test public void testShortRead() throws Exception{ SigningPublicKey publicKey = new SigningPublicKey(); 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); } - } diff --git a/core/java/test/junit/net/i2p/data/SimpleDataStructureTest.java b/core/java/test/junit/net/i2p/data/SimpleDataStructureTest.java index 5e0d1cae0c..d96e44dbb1 100644 --- a/core/java/test/junit/net/i2p/data/SimpleDataStructureTest.java +++ b/core/java/test/junit/net/i2p/data/SimpleDataStructureTest.java @@ -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 * * @author welterde */ -public class SimpleDataStructureTest extends TestCase { - - public void testSetDataImmutable() throws Exception { +public class SimpleDataStructureTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void setDataThrowsOnNullAfterDataSet() 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]); - + // now setting it to null should fail - try { - struct.setData(null); - fail("Should not have allowed us to change this.."); - } 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 - } + exception.expect(RuntimeException.class); + exception.expectMessage("Data already set"); + struct.setData(null); } - + + @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 { // create new test subject TestStruct struct = new TestStruct(); - + // load some data using setData struct.setData(new byte[3]); - + // now try to load via readBytes try { struct.readBytes(null); @@ -59,35 +74,37 @@ public class SimpleDataStructureTest extends TestCase { // all good } } - + + @Test public void testToBase64Safe() throws Exception { // create new test subject TestStruct struct = new TestStruct(); - + // now try to get the Base64.. should not throw an exception, but should not be an empty string either assertNull(struct.toBase64()); } + @Test public void testCalculateHashSafe() throws Exception { // create new test subject TestStruct struct = new TestStruct(); - + // now try to get the hash.. should not throw an exception assertNull(struct.calculateHash()); } - + + @Test public void testHashCodeSafe() throws Exception { // create new test subject TestStruct struct = new TestStruct(); - + // just make sure it doesn't explode in our face struct.hashCode(); } - + public class TestStruct extends SimpleDataStructure { public int length() { return 3; } } - -} \ No newline at end of file +} diff --git a/core/java/test/junit/net/i2p/data/StringTest.java b/core/java/test/junit/net/i2p/data/StringTest.java index f4de303d21..bec9b4f73e 100644 --- a/core/java/test/junit/net/i2p/data/StringTest.java +++ b/core/java/test/junit/net/i2p/data/StringTest.java @@ -8,33 +8,35 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import junit.framework.TestCase; +import org.junit.Test; /** * Test harness for the date structure * * @author jrandom */ -public class StringTest extends TestCase{ - +public class StringTest { + @Test public void testString() throws Exception{ byte[] temp = null; - + ByteArrayOutputStream baos = new ByteArrayOutputStream(); - + DataHelper.writeString(baos, "Hello, I2P"); temp = baos.toByteArray(); - - + + String s = null; ByteArrayInputStream bais = new ByteArrayInputStream(temp); - + s = DataHelper.readString(bais); - + assertEquals(s, "Hello, I2P"); } - -} \ No newline at end of file + +} diff --git a/core/java/test/junit/net/i2p/data/StructureTest.java b/core/java/test/junit/net/i2p/data/StructureTest.java index b8a45a1f9f..211ba2a44b 100644 --- a/core/java/test/junit/net/i2p/data/StructureTest.java +++ b/core/java/test/junit/net/i2p/data/StructureTest.java @@ -8,10 +8,12 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import junit.framework.TestCase; +import org.junit.Test; /** * Utility class for wrapping data structure tests @@ -19,32 +21,33 @@ import junit.framework.TestCase; * @author jrandom */ -public abstract class StructureTest extends TestCase{ - +public abstract class StructureTest { + /** create a populated structure for writing */ public abstract DataStructure createDataStructure() throws DataFormatException; /** create an unpopulated structure for reading */ public abstract DataStructure createStructureToRead(); - + + @Test public void testStructure() throws Exception{ byte[] temp = null; - + DataStructure orig; ByteArrayOutputStream baos = new ByteArrayOutputStream(); - + orig = createDataStructure(); orig.writeBytes(baos); - - + + temp = baos.toByteArray(); - + DataStructure ds; ByteArrayInputStream bais = new ByteArrayInputStream(temp); - + ds = createStructureToRead(); ds.readBytes(bais); - + // I2CP message classes don't implement equals() if (!getClass().getName().startsWith("net.i2p.data.i2cp.")) @@ -56,5 +59,4 @@ public abstract class StructureTest extends TestCase{ byte[] temp2 = baos2.toByteArray(); assert(DataHelper.eq(temp, temp2)); } - } diff --git a/core/java/test/junit/net/i2p/data/UnsignedIntegerTest.java b/core/java/test/junit/net/i2p/data/UnsignedIntegerTest.java index 35a9aabdc4..89a7adb12c 100644 --- a/core/java/test/junit/net/i2p/data/UnsignedIntegerTest.java +++ b/core/java/test/junit/net/i2p/data/UnsignedIntegerTest.java @@ -8,32 +8,33 @@ package net.i2p.data; * */ +import static org.junit.Assert.*; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import junit.framework.TestCase; +import org.junit.Test; /** * Test harness for the date structure * * @author jrandom */ -public class UnsignedIntegerTest extends TestCase{ - +public class UnsignedIntegerTest { + @Test public void testLong() throws Exception{ byte[] temp = null; - + ByteArrayOutputStream baos = new ByteArrayOutputStream(); - + DataHelper.writeLong(baos, 4, 42); temp = baos.toByteArray(); - - + long l; ByteArrayInputStream bais = new ByteArrayInputStream(temp); - + l = DataHelper.readLong(bais, 4); - + assertEquals(42, l); } -} \ No newline at end of file +} diff --git a/core/java/test/junit/net/i2p/data/i2cp/I2CPTestSuite.java b/core/java/test/junit/net/i2p/data/i2cp/I2CPTestSuite.java index dd51940540..f55a625a2e 100644 --- a/core/java/test/junit/net/i2p/data/i2cp/I2CPTestSuite.java +++ b/core/java/test/junit/net/i2p/data/i2cp/I2CPTestSuite.java @@ -1,40 +1,35 @@ package net.i2p.data.i2cp; -import junit.framework.Test; -import junit.framework.TestSuite; +import org.junit.runners.Suite; +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 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; - } }