diff options
author | Roberto Tyley <roberto.tyley@gmail.com> | 2014-07-26 01:42:38 +0400 |
---|---|---|
committer | Roberto Tyley <roberto.tyley@gmail.com> | 2014-07-26 01:42:38 +0400 |
commit | 62e5eb045063e964d0d99688403423afd804e297 (patch) | |
tree | 317893dbd566d5352d24e00350045c1776a7560a /core/src | |
parent | 7107f91d9199401a19d4518d7c6b0f89e509d378 (diff) |
Stop TLS testcases causing failure on JUnit constructor requirements
If you run the Gradle build, you'd see TlsTestCase & DTLSTestCase cause
a build failure:
```
junit.framework.AssertionFailedError:
Class org.bouncycastle.crypto.tls.test.DTLSTestCase has no public
constructor TestCase(String name) or TestCase()
```
The reason is that both these testcases actually have two-arg constructors
(because they need a bunch of extra configuration data), so don't fit the
normal no-arg or one-arg constructor requirement of classes extending
`TestCase`.
The fix used here is to employ JUnit 'parameterized tests', rather than a
TestSuite:
https://github.com/junit-team/junit/wiki/Parameterized-tests
P.S. You can see also see the Gradle build failure in Travis CI here:
https://travis-ci.org/rtyley/spongycastle/builds/29981411#L1052-L1056
(this is why BC needs https://github.com/bcgit/bc-java/pull/80 !)
Diffstat (limited to 'core/src')
4 files changed, 180 insertions, 188 deletions
diff --git a/core/src/test/java/org/bouncycastle/crypto/tls/test/DTLSTestCase.java b/core/src/test/java/org/bouncycastle/crypto/tls/test/DTLSTestCase.java index 928647c1..65482a77 100644 --- a/core/src/test/java/org/bouncycastle/crypto/tls/test/DTLSTestCase.java +++ b/core/src/test/java/org/bouncycastle/crypto/tls/test/DTLSTestCase.java @@ -1,8 +1,9 @@ package org.bouncycastle.crypto.tls.test; import java.security.SecureRandom; - -import junit.framework.TestCase; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import org.bouncycastle.crypto.tls.DTLSClientProtocol; import org.bouncycastle.crypto.tls.DTLSServerProtocol; @@ -10,9 +11,96 @@ import org.bouncycastle.crypto.tls.DTLSTransport; import org.bouncycastle.crypto.tls.DatagramTransport; import org.bouncycastle.crypto.tls.ProtocolVersion; import org.bouncycastle.util.Arrays; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import static org.junit.Assert.*; -public class DTLSTestCase extends TestCase +@RunWith(Parameterized.class) +public class DTLSTestCase { + // Make the access to constants less verbose + static abstract class C extends TlsTestConfig {} + + @Parameterized.Parameters(name = "{index}: {1}") + public static Collection<Object[]> data() { + List<Object[]> params = new ArrayList<Object[]>(); + addVersionTests(params, ProtocolVersion.DTLSv10); + addVersionTests(params, ProtocolVersion.DTLSv12); + return params; + } + + private static void addVersionTests(List<Object[]> params, ProtocolVersion version) + { + String prefix = version.toString().replaceAll("[ \\.]", "") + "_"; + + /* + * NOTE: Temporarily disabled automatic test runs because of problems getting a clean exit + * of the DTLS server after a fatal alert. As of writing, manual runs show the correct + * alerts being raised + */ + +// { +// TlsTestConfig c = createDTLSTestConfig(version); +// c.clientAuth = C.CLIENT_AUTH_INVALID_VERIFY; +// c.expectServerFatalAlert(AlertDescription.decrypt_error); +// +// testSuite.addTest(new DTLSTestCase(c, prefix + "BadCertificateVerify")); +// } +// +// { +// TlsTestConfig c = createDTLSTestConfig(version); +// c.clientAuth = C.CLIENT_AUTH_INVALID_CERT; +// c.expectServerFatalAlert(AlertDescription.bad_certificate); +// +// testSuite.addTest(new DTLSTestCase(c, prefix + "BadClientCertificate")); +// } +// +// { +// TlsTestConfig c = createDTLSTestConfig(version); +// c.clientAuth = C.CLIENT_AUTH_NONE; +// c.serverCertReq = C.SERVER_CERT_REQ_MANDATORY; +// c.expectServerFatalAlert(AlertDescription.handshake_failure); +// +// testSuite.addTest(new DTLSTestCase(c, prefix + "BadMandatoryCertReqDeclined")); +// } + + { + TlsTestConfig c = createDTLSTestConfig(version); + + params.add(new Object[] { c, prefix + "GoodDefault" }); + } + + { + TlsTestConfig c = createDTLSTestConfig(version); + c.serverCertReq = C.SERVER_CERT_REQ_NONE; + + params.add(new Object[]{ c, prefix + "GoodNoCertReq"}); + } + + { + TlsTestConfig c = createDTLSTestConfig(version); + c.clientAuth = C.CLIENT_AUTH_NONE; + + params.add(new Object[]{ c, prefix + "GoodOptionalCertReqDeclined"}); + } + } + + private static TlsTestConfig createDTLSTestConfig(ProtocolVersion version) + { + TlsTestConfig c = new TlsTestConfig(); + c.clientMinimumVersion = ProtocolVersion.DTLSv10; + /* + * TODO We'd like to just set the offer version to DTLSv12, but there is a known issue with + * overly-restrictive version checks b/w BC DTLS 1.2 client, BC DTLS 1.0 server + */ + c.clientOfferVersion = version; + c.serverMaximumVersion = version; + c.serverMinimumVersion = ProtocolVersion.DTLSv10; + return c; + } + private static void checkDTLSVersion(ProtocolVersion version) { if (version != null && !version.isDTLS()) @@ -31,11 +119,10 @@ public class DTLSTestCase extends TestCase checkDTLSVersion(config.serverMinimumVersion); this.config = config; - - setName(name); } - protected void runTest() throws Throwable + @Test + public void runTest() throws Throwable { SecureRandom secureRandom = new SecureRandom(); diff --git a/core/src/test/java/org/bouncycastle/crypto/tls/test/DTLSTestSuite.java b/core/src/test/java/org/bouncycastle/crypto/tls/test/DTLSTestSuite.java deleted file mode 100644 index bd066f85..00000000 --- a/core/src/test/java/org/bouncycastle/crypto/tls/test/DTLSTestSuite.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.bouncycastle.crypto.tls.test; - -import junit.framework.Test; -import junit.framework.TestSuite; - -import org.bouncycastle.crypto.tls.ProtocolVersion; - -public class DTLSTestSuite extends TestSuite -{ - // Make the access to constants less verbose - static abstract class C extends TlsTestConfig {} - - public static Test suite() - { - DTLSTestSuite testSuite = new DTLSTestSuite(); - - addVersionTests(testSuite, ProtocolVersion.DTLSv10); - addVersionTests(testSuite, ProtocolVersion.DTLSv12); - - return testSuite; - } - - private static void addVersionTests(TestSuite testSuite, ProtocolVersion version) - { - String prefix = version.toString().replaceAll("[ \\.]", "") + "_"; - - /* - * NOTE: Temporarily disabled automatic test runs because of problems getting a clean exit - * of the DTLS server after a fatal alert. As of writing, manual runs show the correct - * alerts being raised - */ - -// { -// TlsTestConfig c = createDTLSTestConfig(version); -// c.clientAuth = C.CLIENT_AUTH_INVALID_VERIFY; -// c.expectServerFatalAlert(AlertDescription.decrypt_error); -// -// testSuite.addTest(new DTLSTestCase(c, prefix + "BadCertificateVerify")); -// } -// -// { -// TlsTestConfig c = createDTLSTestConfig(version); -// c.clientAuth = C.CLIENT_AUTH_INVALID_CERT; -// c.expectServerFatalAlert(AlertDescription.bad_certificate); -// -// testSuite.addTest(new DTLSTestCase(c, prefix + "BadClientCertificate")); -// } -// -// { -// TlsTestConfig c = createDTLSTestConfig(version); -// c.clientAuth = C.CLIENT_AUTH_NONE; -// c.serverCertReq = C.SERVER_CERT_REQ_MANDATORY; -// c.expectServerFatalAlert(AlertDescription.handshake_failure); -// -// testSuite.addTest(new DTLSTestCase(c, prefix + "BadMandatoryCertReqDeclined")); -// } - - { - TlsTestConfig c = createDTLSTestConfig(version); - - testSuite.addTest(new DTLSTestCase(c, prefix + "GoodDefault")); - } - - { - TlsTestConfig c = createDTLSTestConfig(version); - c.serverCertReq = C.SERVER_CERT_REQ_NONE; - - testSuite.addTest(new DTLSTestCase(c, prefix + "GoodNoCertReq")); - } - - { - TlsTestConfig c = createDTLSTestConfig(version); - c.clientAuth = C.CLIENT_AUTH_NONE; - - testSuite.addTest(new DTLSTestCase(c, prefix + "GoodOptionalCertReqDeclined")); - } - } - - private static TlsTestConfig createDTLSTestConfig(ProtocolVersion version) - { - TlsTestConfig c = new TlsTestConfig(); - c.clientMinimumVersion = ProtocolVersion.DTLSv10; - /* - * TODO We'd like to just set the offer version to DTLSv12, but there is a known issue with - * overly-restrictive version checks b/w BC DTLS 1.2 client, BC DTLS 1.0 server - */ - c.clientOfferVersion = version; - c.serverMaximumVersion = version; - c.serverMinimumVersion = ProtocolVersion.DTLSv10; - return c; - } -} diff --git a/core/src/test/java/org/bouncycastle/crypto/tls/test/TlsTestCase.java b/core/src/test/java/org/bouncycastle/crypto/tls/test/TlsTestCase.java index 79ad7844..8e7f13a6 100644 --- a/core/src/test/java/org/bouncycastle/crypto/tls/test/TlsTestCase.java +++ b/core/src/test/java/org/bouncycastle/crypto/tls/test/TlsTestCase.java @@ -4,17 +4,99 @@ import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.security.SecureRandom; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; -import junit.framework.TestCase; - +import org.bouncycastle.crypto.tls.AlertDescription; import org.bouncycastle.crypto.tls.ProtocolVersion; import org.bouncycastle.crypto.tls.TlsClientProtocol; import org.bouncycastle.crypto.tls.TlsServerProtocol; import org.bouncycastle.util.Arrays; import org.bouncycastle.util.io.Streams; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import static org.junit.Assert.*; -public class TlsTestCase extends TestCase +@RunWith(Parameterized.class) +public class TlsTestCase { + + // Make the access to constants less verbose + static abstract class C extends TlsTestConfig {} + + @Parameterized.Parameters(name = "{index}: {1}") + public static Collection<Object[]> data() { + List<Object[]> params = new ArrayList<Object[]>(); + addVersionTests(params, ProtocolVersion.TLSv10); + addVersionTests(params, ProtocolVersion.TLSv11); + addVersionTests(params, ProtocolVersion.TLSv12); + return params; + } + + private static void addVersionTests(List<Object[]> params, ProtocolVersion version) + { + String prefix = version.toString().replaceAll("[ \\.]", "") + "_"; + + { + TlsTestConfig c = createTlsTestConfig(version); + + params.add(new Object[] { c, prefix + "GoodDefault" }); + } + + { + TlsTestConfig c = createTlsTestConfig(version); + c.clientAuth = C.CLIENT_AUTH_INVALID_VERIFY; + c.expectServerFatalAlert(AlertDescription.decrypt_error); + + params.add(new Object[] { c, prefix + "BadCertificateVerify" }); + } + + { + TlsTestConfig c = createTlsTestConfig(version); + c.clientAuth = C.CLIENT_AUTH_INVALID_CERT; + c.expectServerFatalAlert(AlertDescription.bad_certificate); + + params.add(new Object[] { c, prefix + "BadClientCertificate" }); + } + + { + TlsTestConfig c = createTlsTestConfig(version); + c.clientAuth = C.CLIENT_AUTH_NONE; + c.serverCertReq = C.SERVER_CERT_REQ_MANDATORY; + c.expectServerFatalAlert(AlertDescription.handshake_failure); + + params.add(new Object[] { c, prefix + "BadMandatoryCertReqDeclined" }); + } + + { + TlsTestConfig c = createTlsTestConfig(version); + c.serverCertReq = C.SERVER_CERT_REQ_NONE; + + params.add(new Object[] { c, prefix + "GoodNoCertReq" }); + } + + { + TlsTestConfig c = createTlsTestConfig(version); + c.clientAuth = C.CLIENT_AUTH_NONE; + + params.add(new Object[] { c, prefix + "GoodOptionalCertReqDeclined" }); + } + } + + private static TlsTestConfig createTlsTestConfig(ProtocolVersion version) + { + TlsTestConfig c = new TlsTestConfig(); + c.clientMinimumVersion = ProtocolVersion.TLSv10; + c.clientOfferVersion = ProtocolVersion.TLSv12; + c.serverMaximumVersion = version; + c.serverMinimumVersion = ProtocolVersion.TLSv10; + return c; + } + + private static void checkTLSVersion(ProtocolVersion version) { if (version != null && !version.isTLS()) @@ -33,11 +115,10 @@ public class TlsTestCase extends TestCase checkTLSVersion(config.serverMinimumVersion); this.config = config; - - setName(name); } - protected void runTest() throws Throwable + @Test + public void runTest() throws Throwable { SecureRandom secureRandom = new SecureRandom(); diff --git a/core/src/test/java/org/bouncycastle/crypto/tls/test/TlsTestSuite.java b/core/src/test/java/org/bouncycastle/crypto/tls/test/TlsTestSuite.java deleted file mode 100644 index eed9b071..00000000 --- a/core/src/test/java/org/bouncycastle/crypto/tls/test/TlsTestSuite.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.bouncycastle.crypto.tls.test; - -import org.bouncycastle.crypto.tls.AlertDescription; -import org.bouncycastle.crypto.tls.ProtocolVersion; - -import junit.framework.Test; -import junit.framework.TestSuite; - -public class TlsTestSuite extends TestSuite -{ - // Make the access to constants less verbose - static abstract class C extends TlsTestConfig {} - - public static Test suite() - { - TlsTestSuite testSuite = new TlsTestSuite(); - - addVersionTests(testSuite, ProtocolVersion.TLSv10); - addVersionTests(testSuite, ProtocolVersion.TLSv11); - addVersionTests(testSuite, ProtocolVersion.TLSv12); - - return testSuite; - } - - private static void addVersionTests(TestSuite testSuite, ProtocolVersion version) - { - String prefix = version.toString().replaceAll("[ \\.]", "") + "_"; - - { - TlsTestConfig c = createTlsTestConfig(version); - - testSuite.addTest(new TlsTestCase(c, prefix + "GoodDefault")); - } - - { - TlsTestConfig c = createTlsTestConfig(version); - c.clientAuth = C.CLIENT_AUTH_INVALID_VERIFY; - c.expectServerFatalAlert(AlertDescription.decrypt_error); - - testSuite.addTest(new TlsTestCase(c, prefix + "BadCertificateVerify")); - } - - { - TlsTestConfig c = createTlsTestConfig(version); - c.clientAuth = C.CLIENT_AUTH_INVALID_CERT; - c.expectServerFatalAlert(AlertDescription.bad_certificate); - - testSuite.addTest(new TlsTestCase(c, prefix + "BadClientCertificate")); - } - - { - TlsTestConfig c = createTlsTestConfig(version); - c.clientAuth = C.CLIENT_AUTH_NONE; - c.serverCertReq = C.SERVER_CERT_REQ_MANDATORY; - c.expectServerFatalAlert(AlertDescription.handshake_failure); - - testSuite.addTest(new TlsTestCase(c, prefix + "BadMandatoryCertReqDeclined")); - } - - { - TlsTestConfig c = createTlsTestConfig(version); - c.serverCertReq = C.SERVER_CERT_REQ_NONE; - - testSuite.addTest(new TlsTestCase(c, prefix + "GoodNoCertReq")); - } - - { - TlsTestConfig c = createTlsTestConfig(version); - c.clientAuth = C.CLIENT_AUTH_NONE; - - testSuite.addTest(new TlsTestCase(c, prefix + "GoodOptionalCertReqDeclined")); - } - } - - private static TlsTestConfig createTlsTestConfig(ProtocolVersion version) - { - TlsTestConfig c = new TlsTestConfig(); - c.clientMinimumVersion = ProtocolVersion.TLSv10; - c.clientOfferVersion = ProtocolVersion.TLSv12; - c.serverMaximumVersion = version; - c.serverMinimumVersion = ProtocolVersion.TLSv10; - return c; - } -} |