Brought API test in line with coding standard.

This commit is contained in:
John Safranek
2011-12-15 16:34:50 -08:00
parent 4b8eba575a
commit e8ce739274

View File

@@ -19,8 +19,6 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/ */
/* XXX I'm just not happy with this. */
#include <stdlib.h> #include <stdlib.h>
#include <cyassl/ssl.h> #include <cyassl/ssl.h>
#include "unit.h" #include "unit.h"
@@ -28,173 +26,112 @@
#define TEST_FAIL (-1) #define TEST_FAIL (-1)
#define TEST_SUCCESS (0) #define TEST_SUCCESS (0)
/* Use a set of negative numbers for error codes */
static int test_CyaSSL_Init(void); static int test_CyaSSL_Init(void);
static int test_CyaSSL_Cleanup(void); static int test_CyaSSL_Cleanup(void);
static int test_CyaSSL_Method_Allocators(void); static int test_CyaSSL_Method_Allocators(void);
static int test_meth(CYASSL_METHOD *meth, const char *name);
static int test_meth2(CYASSL_METHOD *meth, const char *name);
static int test_CyaSSL_CTX_new(CYASSL_METHOD *method); static int test_CyaSSL_CTX_new(CYASSL_METHOD *method);
static int test_CyaSSL_CTX_user_certificate_file(void); static int test_CyaSSL_CTX_use_certificate_file(void);
static int test_cert(CYASSL_CTX *ctx, const char* path, int type, int cond,
const char* name);
static int test_CyaSSL_new(void); static int test_CyaSSL_new(void);
static const char* svrCert = "./certs/server-cert.pem";
static const char* svrKey = "./certs/server-key.pem";
static const char* bogusCert = "/dev/null";
static const char* testingFmt = " %s:";
static const char* resultFmt = " %s\n";
static const char* passed = "passed";
static const char* failed = "failed";
/* List of methods found in echoserver.c that I'm skipping for the moment: /* List of methods found in echoserver.c that I'm skipping for the moment:
* - CyaSSL_Debugging_ON()
* - CyaSSL_CTX_set_session_cache_mode() * - CyaSSL_CTX_set_session_cache_mode()
* - CyaSSL_CTX_use_certificate_file
*/ */
int ApiTest(void) int ApiTest(void)
{ {
if (test_CyaSSL_Init()) return TEST_FAIL; printf(" Begin API Tests\n");
if (test_CyaSSL_Method_Allocators()) return TEST_FAIL; test_CyaSSL_Init();
if (test_CyaSSL_CTX_new(CyaSSLv3_server_method())) return TEST_FAIL; test_CyaSSL_Method_Allocators();
if (test_CyaSSL_CTX_user_certificate_file()) return TEST_FAIL; test_CyaSSL_CTX_new(CyaSSLv23_server_method());
if (test_CyaSSL_new()) return TEST_FAIL; test_CyaSSL_CTX_use_certificate_file();
if (test_CyaSSL_Cleanup()) return TEST_FAIL; test_CyaSSL_new();
test_CyaSSL_Cleanup();
printf(" End API Tests\n");
return TEST_SUCCESS; return TEST_SUCCESS;
} }
int test_CyaSSL_Init(void) int test_CyaSSL_Init(void)
{ {
printf(testingFmt, "CyaSSL_Init()");
int result = CyaSSL_Init(); int result = CyaSSL_Init();
if (result) printf("test_CyaSSL_Init(): failed\n"); printf(resultFmt, result ? failed : passed);
return result; return result;
} }
static int test_CyaSSL_Cleanup(void) static int test_CyaSSL_Cleanup(void)
{ {
printf(testingFmt, "CyaSSL_Cleanup()");
int result = CyaSSL_Cleanup(); int result = CyaSSL_Cleanup();
if (result) printf("test_CyaSSL_Cleanup(): failed\n"); printf(resultFmt, result ? failed : passed);
return result; return result;
} }
int test_meth(CYASSL_METHOD *meth, const char *name)
{
printf(testingFmt, name);
if (meth == NULL)
{
printf(resultFmt, failed);
return TEST_FAIL;
}
free(meth);
printf(resultFmt, passed);
return TEST_SUCCESS;
}
int test_meth2(CYASSL_METHOD *meth, const char *name)
{
printf(testingFmt, name);
if (meth != NULL)
{
free(meth);
printf(resultFmt, failed);
return TEST_FAIL;
}
printf(resultFmt, passed);
return TEST_SUCCESS;
}
int test_CyaSSL_Method_Allocators(void) int test_CyaSSL_Method_Allocators(void)
{ {
CYASSL_METHOD *method = NULL; test_meth(CyaSSLv3_server_method(), "CyaSSLv3_server_method()");
test_meth(CyaSSLv3_client_method(), "CyaSSLv3_client_method()");
method = CyaSSLv3_server_method(); test_meth(CyaTLSv1_server_method(), "CyaTLSv1_server_method()");
if (method == NULL) test_meth(CyaTLSv1_client_method(), "CyaTLSv1_client_method()");
{ test_meth(CyaTLSv1_1_server_method(), "CyaTLSv1_1_server_method()");
printf("test CyaSSLv3_server_method: failed\n"); test_meth(CyaTLSv1_1_client_method(), "CyaTLSv1_1_client_method()");
return TEST_FAIL; test_meth(CyaTLSv1_2_server_method(), "CyaTLSv1_2_server_method()");
} test_meth(CyaTLSv1_2_client_method(), "CyaTLSv1_2_client_method()");
free(method); test_meth(CyaSSLv23_client_method(), "CyaSSLv23_client_method()");
method = NULL;
method = CyaSSLv3_client_method();
if (method == NULL)
{
printf("test CyaSSLv3_client_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
method = CyaTLSv1_server_method();
if (method == NULL)
{
printf("test CyaTLSv1_server_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
method = CyaTLSv1_client_method();
if (method == NULL)
{
printf("test CyaTLSv1_client_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
method = CyaTLSv1_1_server_method();
if (method == NULL)
{
printf("test CyaTLSv1_1_server_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
method = CyaTLSv1_1_client_method();
if (method == NULL)
{
printf("test CyaTLSv1_1_client_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
method = CyaTLSv1_2_server_method();
if (method == NULL)
{
printf("test CyaTLSv1_2_server_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
method = CyaTLSv1_2_client_method();
if (method == NULL)
{
printf("test CyaTLSv1_2_client_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
#ifdef CYASSL_DTLS #ifdef CYASSL_DTLS
method = CyaDTLSv1_client_method(); test_meth(CyaDTLSv1_server_method(), "CyaDTLSv1_server_method()");
if (method == NULL) test_meth(CyaDTLSv1_client_method(), "CyaDTLSv1_client_method()");
{ #endif /* CYASSL_DTLS */
printf("test CyaDTLSv1_client_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
method = CyaDTLSv1_server_method();
if (method == NULL)
{
printf("test CyaDTLSv1_server_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
#endif
method = CyaSSLv23_client_method(); #ifdef OPENSSL_EXTRA
if (method == NULL) test_meth2(CyaSSLv2_server_method(), "CyaSSLv2_server_method()");
{ test_meth2(CyaSSLv2_client_method(), "CyaSSLv2_client_method()");
printf("test CyaSSLv23_client_method: failed\n"); #endif /* OPENSSL_EXTRA */
return TEST_FAIL;
}
free(method);
method = NULL;
method = CyaSSLv2_client_method();
if (method == NULL)
{
printf("test CyaSSLv2_client_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
method = CyaSSLv2_server_method();
if (method == NULL)
{
printf("test CyaSSLv2_server_method: failed\n");
return TEST_FAIL;
}
free(method);
method = NULL;
return TEST_SUCCESS; return TEST_SUCCESS;
} }
@@ -205,104 +142,159 @@ int test_CyaSSL_CTX_new(CYASSL_METHOD *method)
{ {
CYASSL_CTX *ctx = NULL; CYASSL_CTX *ctx = NULL;
printf(testingFmt, "CyaSSL_CTX_new(NULL)");
ctx = CyaSSL_CTX_new(NULL); ctx = CyaSSL_CTX_new(NULL);
if (ctx != NULL) if (ctx != NULL)
{ {
CyaSSL_CTX_free(ctx); CyaSSL_CTX_free(ctx);
printf("test_CyaSSL_CTX_new: passed null to new(), failed\n"); printf(resultFmt, failed);
return TEST_FAIL;
} }
else
printf(resultFmt, passed);
printf(testingFmt, "CyaSSL_CTX_new(method)");
ctx = CyaSSL_CTX_new(method); ctx = CyaSSL_CTX_new(method);
if (ctx == NULL) if (ctx == NULL)
{ {
printf("test_CyaSSL_CTX_new: failed\n"); printf(resultFmt, failed);
return TEST_FAIL; free(method);
/* free the method data. if this was successful, freeing
the CTX frees the method. */
}
else
{
CyaSSL_CTX_free(ctx);
printf(resultFmt, passed);
} }
CyaSSL_CTX_free(ctx);
return TEST_SUCCESS;
} }
else
printf("test_CyaSSL_CTX_new() called without method\n");
printf("test_CyaSSL_CTX_new: failed, no method\n"); return TEST_SUCCESS;
return TEST_FAIL;
} }
int test_CyaSSL_CTX_user_certificate_file(void) int test_cert(CYASSL_CTX *ctx, const char* path, int type, int cond,
const char* name)
{ {
CYASSL_METHOD *method = CyaSSLv2_server_method(); printf(testingFmt, name);
if (method != NULL) int result = CyaSSL_CTX_use_certificate_file(ctx, path, type);
if (result != cond)
{ {
CYASSL_CTX *ctx = CyaSSL_CTX_new(method); printf(resultFmt, failed);
if (ctx != NULL) return TEST_FAIL;
{ }
int result; printf(resultFmt, passed);
return TEST_SUCCESS;
/* setting all parameters to garbage. this should succeed with failure */ }
/* Then set the parameters to legit values but set each item to bogus
and call again. Finish with a successful success. */
result = CyaSSL_CTX_use_certificate_file(NULL, NULL, 9999);
if (result != SSL_FAILURE)
{
printf("test_CyaSSL_CTX_user_certificate_file: should have rejected bad params, failure\n");
return TEST_FAIL;
}
result = CyaSSL_CTX_use_certificate_file(NULL, "../certs/server-cert.pem", SSL_FILETYPE_PEM); int test_CyaSSL_CTX_use_certificate_file(void)
if (result != SSL_FAILURE) {
{ CYASSL_METHOD *method = CyaSSLv23_server_method();
printf("test_CyaSSL_CTX_user_certificate_file: should have rejected NULL CTX, failure\n"); if (method == NULL)
return TEST_FAIL; {
} printf("test_CyaSSL_CTX_use_certificate_file() cannot create method\n");
return TEST_FAIL;
result = CyaSSL_CTX_use_certificate_file(ctx, "/dev/null", SSL_FILETYPE_PEM);
if (result != SSL_FAILURE)
{
printf("test_CyaSSL_CTX_user_certificate_file: should have rejected bad filename, failure\n");
return TEST_FAIL;
}
result = CyaSSL_CTX_use_certificate_file(ctx, "../certs/server-cert.pem", 9999);
if (result != SSL_FAILURE)
{
printf("test_CyaSSL_CTX_user_certificate_file: should have rejected invalid format, failure\n");
return TEST_FAIL;
}
result = CyaSSL_CTX_use_certificate_file(ctx, "../certs/server-cert.pem", SSL_FILETYPE_PEM);
if (result != SSL_SUCCESS)
{
printf("test_CyaSSL_CTX_user_certificate_file: should have accepted known good params, failure\n");
return TEST_FAIL;
}
CyaSSL_CTX_free(ctx);
return TEST_SUCCESS;
}
} }
printf("test_CyaSSL_new: failed, no method\n"); CYASSL_CTX *ctx = CyaSSL_CTX_new(method);
return TEST_FAIL; if (ctx == NULL)
{
printf("test_CyaSSL_CTX_use_certificate_file() cannot create context\n");
free(method);
return TEST_FAIL;
}
/* setting all parameters to garbage. this should succeed with
failure */
/* Then set the parameters to legit values but set each item to
bogus and call again. Finish with a successful success. */
#if 0
/* This test case is known to fail with a segfault */
test_cert(NULL, NULL, 9999, SSL_FAILURE,
"CyaSSL_CTX_use_certificate_file(NULL, NULL, 9999)");
test_cert(NULL, svrCert, SSL_FILETYPE_PEM, SSL_FAILURE,
"CyaSSL_CTX_use_certificate_file(NULL, svrCert, SSL_FILETYPE_PEM)");
#endif
test_cert(ctx, bogusCert, SSL_FILETYPE_PEM, SSL_FAILURE,
"CyaSSL_CTX_use_certificate_file(ctx, bogusCert, SSL_FILETYPE_PEM)");
test_cert(ctx, svrCert, 9999, SSL_FAILURE,
"CyaSSL_CTX_use_certificate_file(ctx, svrCert, 9999)");
test_cert(ctx, svrCert, SSL_FILETYPE_PEM, SSL_SUCCESS,
"CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)");
CyaSSL_CTX_free(ctx);
return TEST_SUCCESS;
} }
int test_CyaSSL_new(void) int test_CyaSSL_new(void)
{ {
CYASSL_CTX *ctx = CyaSSL_CTX_new(CyaSSLv2_server_method()); CYASSL_CTX *ctx = CyaSSL_CTX_new(CyaSSLv23_server_method());
if (ctx != NULL) if (ctx == NULL)
{ {
int result; printf("test_CyaSSL_new() cannot create context\n");
result = CyaSSL_CTX_use_certificate_file(ctx, "../certs/server-cert.pem", SSL_FILETYPE_PEM); return TEST_FAIL;
if (result != SSL_SUCCESS)
{
printf("test_CyaSSL_new(): couldn't prepare test\n");
return TEST_FAIL;
}
CYASSL *ssl;
/* how about using a context without a certificate? */
ssl = CyaSSL_new(NULL);
ssl = CyaSSL_new(ctx);
} }
int result;
result = CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM);
if (result == SSL_FAILURE)
{
printf("test_CyaSSL_new() cannot obtain certificate\n");
CyaSSL_CTX_free(ctx);
return TEST_FAIL;
}
result = CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM);
if (result == SSL_FAILURE)
{
printf("test_CyaSSL_new() cannot obtain key\n");
CyaSSL_CTX_free(ctx);
return TEST_FAIL;
}
CYASSL_CTX *bad_ctx = CyaSSL_CTX_new(CyaSSLv23_server_method());
if (bad_ctx == NULL)
{
printf("test_CyaSSL_new() cannot create bogus context\n");
CyaSSL_CTX_free(ctx);
return TEST_FAIL;
}
CYASSL *ssl;
printf(testingFmt, "CyaSSL_new(NULL)");
ssl = CyaSSL_new(NULL);
if (ssl != NULL)
{
printf(resultFmt, failed);
CyaSSL_free(ssl);
}
else
printf(resultFmt, passed);
printf(testingFmt, "CyaSSL_new(bad_ctx)");
ssl = CyaSSL_new(bad_ctx);
if (ssl != NULL)
{
printf(resultFmt, failed);
CyaSSL_free(ssl);
}
else
printf(resultFmt, passed);
printf(testingFmt, "CyaSSL_new(ctx)");
ssl = CyaSSL_new(ctx);
if (ssl == NULL)
printf(resultFmt, failed);
else
{
printf(resultFmt, passed);
CyaSSL_free(ssl);
}
CyaSSL_CTX_free(bad_ctx);
CyaSSL_CTX_free(ctx);
return TEST_SUCCESS; return TEST_SUCCESS;
} }