JNI: refactor WolfSSLSessionTest to use individual Junit Test annotation on methods, better cleanup

pull/191/head
Chris Conlon 2024-04-24 12:18:46 -06:00
parent 76ac7784de
commit 3c94939d3d
1 changed files with 355 additions and 173 deletions

View File

@ -44,78 +44,72 @@ import com.wolfssl.WolfSSLSession;
public class WolfSSLSessionTest {
public final static int TEST_FAIL = -1;
public final static int TEST_SUCCESS = 0;
private final static int TEST_FAIL = -1;
private final static int TEST_SUCCESS = 0;
public static String cliCert = "./examples/certs/client-cert.pem";
public static String cliKey = "./examples/certs/client-key.pem";
public static String caCert = "./examples/certs/ca-cert.pem";
public static String bogusFile = "/dev/null";
private static String cliCert = "./examples/certs/client-cert.pem";
private static String cliKey = "./examples/certs/client-key.pem";
private static String caCert = "./examples/certs/ca-cert.pem";
private static String bogusFile = "/dev/null";
public final static String exampleHost = "www.example.com";
public final static int examplePort = 443;
private final static String exampleHost = "www.example.com";
private final static int examplePort = 443;
WolfSSLContext ctx;
WolfSSLSession ssl;
private static WolfSSLContext ctx = null;
@BeforeClass
public static void loadLibrary() {
public static void loadLibrary()
throws WolfSSLException{
System.out.println("WolfSSLSession Class");
try {
WolfSSL.loadLibrary();
} catch (UnsatisfiedLinkError ule) {
fail("failed to load native JNI library");
}
}
@Test
public void testWolfSSLSession() throws WolfSSLException {
/* Create one WolfSSLContext */
ctx = new WolfSSLContext(WolfSSL.SSLv23_ClientMethod());
System.out.println("WolfSSLSession Class");
/* Set cert/key paths */
cliCert = WolfSSLTestCommon.getPath(cliCert);
cliKey = WolfSSLTestCommon.getPath(cliKey);
caCert = WolfSSLTestCommon.getPath(caCert);
test_WolfSSLSession_new();
test_WolfSSLSession_useCertificateFile();
test_WolfSSLSession_usePrivateKeyFile();
test_WolfSSLSession_useCertificateChainFile();
test_WolfSSLSession_setPskClientCb();
test_WolfSSLSession_setPskServerCb();
test_WolfSSLSession_usePskIdentityHint();
test_WolfSSLSession_getPskIdentityHint();
test_WolfSSLSession_getPskIdentity();
test_WolfSSLSession_useSessionTicket();
test_WolfSSLSession_timeout();
test_WolfSSLSession_status();
test_WolfSSLSession_useSNI();
test_WolfSSLSession_useALPN();
test_WolfSSLSession_freeSSL();
test_WolfSSLSession_UseAfterFree();
test_WolfSSLSession_getSessionID();
test_WolfSSLSession_useSecureRenegotiation();
test_WolfSSLSession_setTls13SecretCb();
}
public void test_WolfSSLSession_new() {
@Test
public void test_WolfSSLSession_new()
throws WolfSSLJNIException {
WolfSSLSession sess = null;
System.out.print("\tWolfSSLSession()");
try {
System.out.print("\tWolfSSLSession()");
ssl = new WolfSSLSession(ctx);
sess = new WolfSSLSession(ctx);
} catch (WolfSSLException we) {
System.out.println("\t... failed");
fail("failed to create WolfSSLSession object");
} finally {
if (sess != null) {
sess.freeSSL();
}
}
System.out.println("\t\t... passed");
}
public void test_WolfSSLSession_useCertificateFile() {
@Test
public void test_WolfSSLSession_useCertificateFile()
throws WolfSSLJNIException, WolfSSLException {
System.out.print("\tuseCertificateFile()");
WolfSSLSession ssl = new WolfSSLSession(ctx);
test_ucf("useCertificateFile", null, null, 9999, WolfSSL.SSL_FAILURE,
"useCertificateFile(null, null, 9999)");
@ -132,13 +126,21 @@ public class WolfSSLSessionTest {
WolfSSL.SSL_SUCCESS,
"useCertificateFile(ssl, cliCert, SSL_FILETYPE_PEM)");
if (ssl != null) {
ssl.freeSSL();
}
System.out.println("\t\t... passed");
}
public void test_WolfSSLSession_useCertificateChainFile() {
@Test
public void test_WolfSSLSession_useCertificateChainFile()
throws WolfSSLJNIException, WolfSSLException {
System.out.print("\tuseCertificateChainFile()");
WolfSSLSession ssl = new WolfSSLSession(ctx);
test_ucf("useCertificateChainFile", null, null, 0,
WolfSSL.SSL_FAILURE,
"useCertificateChainFile(null, null)");
@ -151,11 +153,15 @@ public class WolfSSLSessionTest {
WolfSSL.SSL_SUCCESS,
"useCertificateChainFile(ssl, cliCert)");
if (ssl != null) {
ssl.freeSSL();
}
System.out.println("\t... passed");
}
/* helper for testing WolfSSLSession.useCertificateFile() */
public void test_ucf(String func, WolfSSLSession ssl, String filePath,
private void test_ucf(String func, WolfSSLSession ssl, String filePath,
int type, int cond, String name) {
int result = WolfSSL.SSL_FAILURE;
@ -191,10 +197,14 @@ public class WolfSSLSessionTest {
return;
}
public void test_WolfSSLSession_usePrivateKeyFile() {
@Test
public void test_WolfSSLSession_usePrivateKeyFile()
throws WolfSSLJNIException, WolfSSLException {
System.out.print("\tusePrivateKeyFile()");
WolfSSLSession ssl = new WolfSSLSession(ctx);
test_upkf(null, null, 9999, WolfSSL.SSL_FAILURE,
"usePrivateKeyFile(null, null, 9999)");
@ -208,11 +218,15 @@ public class WolfSSLSessionTest {
test_upkf(ssl, cliKey, WolfSSL.SSL_FILETYPE_PEM, WolfSSL.SSL_SUCCESS,
"usePrivateKeyFile(ssl, cliKey, SSL_FILETYPE_PEM)");
if (ssl != null) {
ssl.freeSSL();
}
System.out.println("\t\t... passed");
}
/* helper for testing WolfSSLSession.usePrivateKeyFile() */
public void test_upkf(WolfSSLSession ssl, String filePath, int type,
private void test_upkf(WolfSSLSession ssl, String filePath, int type,
int cond, String name) {
int result;
@ -259,19 +273,38 @@ public class WolfSSLSessionTest {
}
}
public void test_WolfSSLSession_setPskClientCb() {
@Test
public void test_WolfSSLSession_setPskClientCb()
throws WolfSSLJNIException {
WolfSSLSession ssl = null;
System.out.print("\tsetPskClientCb()");
try {
TestPskClientCb pskClientCb = new TestPskClientCb();
ssl = new WolfSSLSession(ctx);
ssl.setPskClientCb(pskClientCb);
} catch (Exception e) {
if (!e.getMessage().equals("wolfSSL not compiled with PSK " +
if (e.getMessage().equals("wolfSSL not compiled with PSK " +
"support")) {
/* Not compiled in, skip */
System.out.println("\t\t... skipped");
return;
}
else {
System.out.println("\t\t... failed");
fail("Failed setPskClientCb test");
e.printStackTrace();
}
} finally {
if (ssl != null) {
ssl.freeSSL();
}
}
System.out.println("\t\t... passed");
}
@ -295,122 +328,227 @@ public class WolfSSLSessionTest {
}
}
public void test_WolfSSLSession_setPskServerCb() {
@Test
public void test_WolfSSLSession_setPskServerCb()
throws WolfSSLJNIException {
WolfSSLSession ssl = null;
System.out.print("\tsetPskServerCb()");
try {
TestPskServerCb pskServerCb = new TestPskServerCb();
ssl = new WolfSSLSession(ctx);
ssl.setPskServerCb(pskServerCb);
} catch (Exception e) {
if (!e.getMessage().equals("wolfSSL not compiled with PSK " +
if (e.getMessage().equals("wolfSSL not compiled with PSK " +
"support")) {
/* Not compiled in, skip */
System.out.println("\t\t... skipped");
return;
}
else {
System.out.println("\t\t... failed");
fail("Failed setPskServerCb test");
e.printStackTrace();
}
} finally {
if (ssl != null) {
ssl.freeSSL();
}
}
System.out.println("\t\t... passed");
}
public void test_WolfSSLSession_usePskIdentityHint() {
System.out.print("\tusePskIdentityHint()");
@Test
public void test_WolfSSLSession_useGetPskIdentityHint()
throws WolfSSLJNIException, WolfSSLException {
int ret = 0;
String hint = null;
WolfSSLSession ssl = null;
System.out.print("\tuse/getPskIdentityHint()");
ssl = new WolfSSLSession(ctx);
try {
int ret = ssl.usePskIdentityHint("wolfssl hint");
/* Set PSK identity hint */
ret = ssl.usePskIdentityHint("wolfssl hint");
if (ret != WolfSSL.SSL_SUCCESS &&
ret != WolfSSL.NOT_COMPILED_IN) {
System.out.println("\t\t... failed");
System.out.println("\t... failed");
fail("usePskIdentityHint failed");
}
} catch (IllegalStateException e) {
System.out.println("\t\t... failed");
fail("Failed usePskIdentityHint test");
e.printStackTrace();
}
System.out.println("\t\t... passed");
}
public void test_WolfSSLSession_getPskIdentityHint() {
System.out.print("\tgetPskIdentityHint()");
try {
String hint = ssl.getPskIdentityHint();
/* Get PSK identity hint */
hint = ssl.getPskIdentityHint();
if (hint != null && !hint.equals("wolfssl hint")) {
System.out.println("\t\t... failed");
System.out.println("\t... failed");
fail("getPskIdentityHint failed");
}
} catch (IllegalStateException e) {
System.out.println("\t\t... failed");
fail("Failed getPskIdentityHint test");
System.out.println("\t... failed");
e.printStackTrace();
fail("Failed use/getPskIdentityHint test");
} finally {
if (ssl != null) {
ssl.freeSSL();
}
System.out.println("\t\t... passed");
}
public void test_WolfSSLSession_useSessionTicket() {
System.out.println("\t... passed");
}
@Test
public void test_WolfSSLSession_useSessionTicket()
throws WolfSSLJNIException, WolfSSLException {
int ret = 0;
WolfSSLSession ssl = null;
System.out.print("\tuseSessionTicket()");
try {
int ret = ssl.useSessionTicket();
ssl = new WolfSSLSession(ctx);
ret = ssl.useSessionTicket();
if (ret != WolfSSL.SSL_SUCCESS &&
ret != WolfSSL.NOT_COMPILED_IN) {
System.out.println("\t\t... failed");
fail("useSessionTicket failed");
}
} catch (IllegalStateException e) {
System.out.println("\t\t... failed");
e.printStackTrace();
} finally {
if (ssl != null) {
ssl.freeSSL();
}
}
System.out.println("\t\t... passed");
}
public void test_WolfSSLSession_getPskIdentity() {
@Test
public void test_WolfSSLSession_getPskIdentity()
throws WolfSSLJNIException, WolfSSLException {
String identity = null;
WolfSSLSession ssl = null;
System.out.print("\tgetPskIdentity()");
try {
String identity = ssl.getPskIdentity();
ssl = new WolfSSLSession(ctx);
identity = ssl.getPskIdentity();
} catch (IllegalStateException e) {
System.out.println("\t\t... failed");
fail("Failed getPskIdentity test");
e.printStackTrace();
} finally {
if (ssl != null) {
ssl.freeSSL();
}
}
System.out.println("\t\t... passed");
}
public void test_WolfSSLSession_timeout() {
@Test
public void test_WolfSSLSession_timeout()
throws WolfSSLJNIException, WolfSSLException {
WolfSSLSession ssl = null;
System.out.print("\ttimeout()");
ssl = new WolfSSLSession(ctx);
try {
ssl.setTimeout(5);
if (ssl.getTimeout() != 5) {
System.out.println("\t\t\t... failed");
fail("Failed timeout test");
}
} finally {
if (ssl != null) {
ssl.freeSSL();
}
}
System.out.println("\t\t\t... passed");
}
public void test_WolfSSLSession_status() {
@Test
public void test_WolfSSLSession_status()
throws WolfSSLJNIException, WolfSSLException {
WolfSSLSession ssl = null;
System.out.print("\tstatus()");
ssl = new WolfSSLSession(ctx);
try {
if (ssl.handshakeDone() == true) {
System.out.println("\t\t\t... failed");
fail("Failed status test");
}
} finally {
if (ssl != null) {
ssl.freeSSL();
}
}
System.out.println("\t\t\t... passed");
}
public void test_WolfSSLSession_useSNI() {
@Test
public void test_WolfSSLSession_useSNI()
throws WolfSSLJNIException, WolfSSLException {
int ret;
String sniHostName = "www.example.com";
WolfSSLSession ssl = null;
System.out.print("\tuseSNI()");
ssl = new WolfSSLSession(ctx);
try {
ret = ssl.useSNI((byte)0, sniHostName.getBytes());
if (ret == WolfSSL.NOT_COMPILED_IN) {
System.out.println("\t\t\t... skipped");
return;
} else if (ret != WolfSSL.SSL_SUCCESS) {
System.out.println("\t\t\t... failed");
fail("Failed useSNI test");
} else {
System.out.println("\t\t\t... passed");
}
} finally {
if (ssl != null) {
ssl.freeSSL();
}
}
public void test_WolfSSLSession_useALPN() {
System.out.println("\t\t\t... passed");
}
@Test
public void test_WolfSSLSession_useALPN()
throws WolfSSLException, WolfSSLJNIException {
int ret;
String[] alpnProtos = new String[] {
@ -419,9 +557,13 @@ public class WolfSSLSessionTest {
String http11Alpn = "http/1.1";
byte[] alpnProtoBytes = http11Alpn.getBytes();
byte[] alpnProtoBytesPacked = new byte[1 + alpnProtoBytes.length];
WolfSSLSession ssl = null;
System.out.print("\tuseALPN()");
ssl = new WolfSSLSession(ctx);
try {
/* Testing useALPN(String[], int) */
ret = ssl.useALPN(alpnProtos,
WolfSSL.WOLFSSL_ALPN_CONTINUE_ON_MISMATCH);
@ -432,7 +574,8 @@ public class WolfSSLSessionTest {
}
if (ret == WolfSSL.SSL_SUCCESS) {
ret = ssl.useALPN(null, WolfSSL.WOLFSSL_ALPN_CONTINUE_ON_MISMATCH);
ret = ssl.useALPN(null,
WolfSSL.WOLFSSL_ALPN_CONTINUE_ON_MISMATCH);
if (ret < 0) {
/* error expected, null input */
ret = WolfSSL.SSL_SUCCESS;
@ -475,29 +618,48 @@ public class WolfSSLSessionTest {
if (ret == WolfSSL.NOT_COMPILED_IN) {
System.out.println("\t\t\t... skipped");
return;
} else if (ret != WolfSSL.SSL_SUCCESS) {
System.out.println("\t\t\t... failed");
fail("Failed useALPN test");
} else {
System.out.println("\t\t\t... passed");
}
} finally {
if (ssl != null) {
ssl.freeSSL();
}
}
public void test_WolfSSLSession_freeSSL() {
System.out.println("\t\t\t... passed");
}
@Test
public void test_WolfSSLSession_freeSSL()
throws WolfSSLJNIException, WolfSSLException {
WolfSSLSession ssl = null;
System.out.print("\tfreeSSL()");
ssl = new WolfSSLSession(ctx);
try {
ssl.freeSSL();
} catch (WolfSSLJNIException e) {
System.out.println("\t\t\t... failed");
fail("Failed freeSSL test");
e.printStackTrace();
}
System.out.println("\t\t\t... passed");
}
public void test_WolfSSLSession_UseAfterFree() {
@Test
public void test_WolfSSLSession_UseAfterFree()
throws WolfSSLJNIException {
int ret, err;
WolfSSL sslLib = null;
@ -532,14 +694,9 @@ public class WolfSSLSessionTest {
err == WolfSSL.SSL_ERROR_WANT_WRITE));
if (ret != WolfSSL.SSL_SUCCESS) {
ssl.freeSSL();
sslCtx.free();
fail("Failed WolfSSL.connect() to " + exampleHost);
}
ssl.freeSSL();
sslCtx.free();
} catch (UnknownHostException | ConnectException e) {
/* skip if no Internet connection */
System.out.println("\t\t... skipped");
@ -550,14 +707,24 @@ public class WolfSSLSessionTest {
fail("Failed UseAfterFree test");
e.printStackTrace();
return;
} finally {
if (ssl != null) {
ssl.freeSSL();
}
if (sslCtx != null) {
sslCtx.free();
}
}
try {
/* this should fail, use after free */
ret = ssl.connect();
} catch (IllegalStateException ise) {
System.out.println("\t\t... passed");
return;
} catch (SocketTimeoutException | SocketException e) {
System.out.println("\t\t... failed");
fail("Failed UseAfterFree test");
@ -571,7 +738,9 @@ public class WolfSSLSessionTest {
fail("WolfSSLSession was able to be used after freed");
}
public void test_WolfSSLSession_getSessionID() {
@Test
public void test_WolfSSLSession_getSessionID()
throws WolfSSLJNIException {
int ret, err;
WolfSSL sslLib = null;
@ -593,16 +762,12 @@ public class WolfSSLSessionTest {
sessionID = ssl.getSessionID();
if (sessionID == null || sessionID.length != 0) {
/* sessionID array should not be null, but should be empty */
ssl.freeSSL();
sslCtx.free();
fail("Session ID should be empty array before connection");
}
sock = new Socket(exampleHost, examplePort);
ret = ssl.setFd(sock);
if (ret != WolfSSL.SSL_SUCCESS) {
ssl.freeSSL();
sslCtx.free();
fail("Failed to set file descriptor");
}
@ -615,21 +780,15 @@ public class WolfSSLSessionTest {
err == WolfSSL.SSL_ERROR_WANT_WRITE));
if (ret != WolfSSL.SSL_SUCCESS) {
ssl.freeSSL();
sslCtx.free();
fail("Failed WolfSSL.connect() to " + exampleHost);
}
sessionID = ssl.getSessionID();
if (sessionID == null || sessionID.length == 0) {
/* session ID should not be null or zero length */
ssl.freeSSL();
sslCtx.free();
fail("Session ID should not be null or 0 length " +
"after connection");
}
ssl.freeSSL();
sslCtx.free();
} catch (UnknownHostException | ConnectException e) {
/* skip if no Internet connection */
@ -641,12 +800,22 @@ public class WolfSSLSessionTest {
fail("Failed getSessionID test");
e.printStackTrace();
return;
} finally {
if (ssl != null) {
ssl.freeSSL();
}
if (sslCtx != null) {
sslCtx.free();
}
}
System.out.println("\t\t... passed");
}
public void test_WolfSSLSession_useSecureRenegotiation() {
@Test
public void test_WolfSSLSession_useSecureRenegotiation()
throws WolfSSLJNIException {
int ret, err;
WolfSSL sslLib = null;
@ -670,19 +839,22 @@ public class WolfSSLSessionTest {
if (ret != WolfSSL.SSL_SUCCESS && ret != WolfSSL.NOT_COMPILED_IN) {
System.out.println("... failed");
fail("Failed useSecureRenegotiation test");
ssl.freeSSL();
sslCtx.free();
return;
}
ssl.freeSSL();
sslCtx.free();
} catch (Exception e) {
System.out.println("... failed");
fail("Failed useSecureRenegotiation test");
e.printStackTrace();
return;
} finally {
if (ssl != null) {
ssl.freeSSL();
}
if (sslCtx != null) {
sslCtx.free();
}
}
System.out.println("... passed");
@ -697,7 +869,9 @@ public class WolfSSLSessionTest {
}
}
public void test_WolfSSLSession_setTls13SecretCb() {
@Test
public void test_WolfSSLSession_setTls13SecretCb()
throws WolfSSLJNIException {
int ret;
WolfSSL sslLib = null;
@ -732,6 +906,14 @@ public class WolfSSLSessionTest {
e.printStackTrace();
fail("failed setTls13SecretCb() test");
return;
} finally {
if (ssl != null) {
ssl.freeSSL();
}
if (sslCtx != null) {
sslCtx.free();
}
}
System.out.println("\t... passed");