diff options
author | Bjørn Christian Seime <bjorncs@oath.com> | 2018-08-31 17:52:57 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@oath.com> | 2018-09-03 15:35:02 +0200 |
commit | 418c52b59b1ac2f3a188f5de174b1f3497dc78c5 (patch) | |
tree | a0f2e03b835026d3704e3d611865b1296f5a57f0 /jrt/tests | |
parent | 74c39c64b2bcf6f2dab2c48725610fa6699fe2c8 (diff) |
Test SslEngine in EchoTest and SessionTest
Diffstat (limited to 'jrt/tests')
-rw-r--r-- | jrt/tests/com/yahoo/jrt/CryptoUtils.java | 43 | ||||
-rw-r--r-- | jrt/tests/com/yahoo/jrt/EchoTest.java | 8 | ||||
-rw-r--r-- | jrt/tests/com/yahoo/jrt/SessionTest.java | 7 |
3 files changed, 51 insertions, 7 deletions
diff --git a/jrt/tests/com/yahoo/jrt/CryptoUtils.java b/jrt/tests/com/yahoo/jrt/CryptoUtils.java new file mode 100644 index 00000000000..c3128e09bd3 --- /dev/null +++ b/jrt/tests/com/yahoo/jrt/CryptoUtils.java @@ -0,0 +1,43 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.jrt; + +import com.yahoo.security.KeyStoreBuilder; +import com.yahoo.security.KeyUtils; +import com.yahoo.security.SslContextBuilder; +import com.yahoo.security.X509CertificateBuilder; + +import javax.net.ssl.SSLContext; +import javax.security.auth.x500.X500Principal; +import java.security.KeyPair; +import java.security.KeyStore; +import java.security.cert.X509Certificate; +import java.time.Instant; + +import static com.yahoo.security.KeyAlgorithm.RSA; +import static com.yahoo.security.KeyStoreType.PKCS12; +import static com.yahoo.security.SignatureAlgorithm.SHA256_WITH_RSA; +import static com.yahoo.security.X509CertificateBuilder.generateRandomSerialNumber; +import static java.time.Instant.EPOCH; +import static java.time.temporal.ChronoUnit.DAYS; + +/** + * @author bjorncs + */ +class CryptoUtils { + static SSLContext createTestSslContext() { + KeyPair keyPair = KeyUtils.generateKeypair(RSA); + + X509Certificate certificate = X509CertificateBuilder + .fromKeypair(keyPair, new X500Principal("CN=dummy"), EPOCH, Instant.now().plus(1, DAYS), SHA256_WITH_RSA, generateRandomSerialNumber()) + .build(); + + KeyStore trustStore = KeyStoreBuilder.withType(PKCS12) + .withCertificateEntry("self-signed", certificate) + .build(); + + return new SslContextBuilder() + .withTrustStore(trustStore) + .withKeyStore(keyPair.getPrivate(), certificate) + .build(); + } +} diff --git a/jrt/tests/com/yahoo/jrt/EchoTest.java b/jrt/tests/com/yahoo/jrt/EchoTest.java index 0523241354a..c7fcb154766 100644 --- a/jrt/tests/com/yahoo/jrt/EchoTest.java +++ b/jrt/tests/com/yahoo/jrt/EchoTest.java @@ -5,11 +5,11 @@ package com.yahoo.jrt; import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; -import org.junit.runners.Parameterized; - +import static com.yahoo.jrt.CryptoUtils.createTestSslContext; import static org.junit.Assert.assertTrue; @RunWith(Parameterized.class) @@ -22,8 +22,8 @@ public class EchoTest { Values refValues; @Parameter public CryptoEngine crypto; - @Parameters public static Object[] engines() { - return new Object[] { CryptoEngine.createDefault(), new XorCryptoEngine() }; + @Parameters(name = "{0}") public static Object[] engines() { + return new Object[] { CryptoEngine.createDefault(), new XorCryptoEngine(), new SslEngine(createTestSslContext()) }; } @Before diff --git a/jrt/tests/com/yahoo/jrt/SessionTest.java b/jrt/tests/com/yahoo/jrt/SessionTest.java index 2f1a64538de..3c06d3f9b69 100644 --- a/jrt/tests/com/yahoo/jrt/SessionTest.java +++ b/jrt/tests/com/yahoo/jrt/SessionTest.java @@ -5,10 +5,11 @@ package com.yahoo.jrt; import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; -import org.junit.runners.Parameterized; +import static com.yahoo.jrt.CryptoUtils.createTestSslContext; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -17,8 +18,8 @@ import static org.junit.Assert.assertTrue; public class SessionTest implements SessionHandler { @Parameter public CryptoEngine crypto; - @Parameters public static Object[] engines() { - return new Object[] { CryptoEngine.createDefault(), new XorCryptoEngine() }; + @Parameters(name = "{0}") public static Object[] engines() { + return new Object[] { CryptoEngine.createDefault(), new XorCryptoEngine(), new SslEngine(createTestSslContext()) }; } private static class Session { |