merged common test code into tls_test_common.h & tls_test_common.c

This commit is contained in:
lv-426
2009-02-02 22:22:56 +00:00
parent b9ec40f2e8
commit f577769d8e
13 changed files with 728 additions and 1754 deletions
+29 -17
View File
@@ -33,70 +33,82 @@ EXTRA_DIST = cert.pem key.pem tls_test_keys.h
TESTS = $(check_PROGRAMS)
# cURL independent tests
# cURL dependent tests
tls_session_time_out_test_SOURCES = \
tls_session_time_out_test.c
tls_session_time_out_test.c \
tls_test_common.c
tls_session_time_out_test_LDADD = \
$(top_builddir)/src/testcurl/libcurl_version_check.a \
$(top_builddir)/src/daemon/libmicrohttpd.la
$(top_builddir)/src/daemon/libmicrohttpd.la \
@LIBCURL@
tls_cipher_change_test_SOURCES = \
tls_cipher_change_test.c
tls_cipher_change_test.c \
tls_test_common.c
tls_cipher_change_test_LDADD = \
$(top_builddir)/src/testcurl/libcurl_version_check.a \
$(top_builddir)/src/daemon/libmicrohttpd.la
$(top_builddir)/src/daemon/libmicrohttpd.la \
@LIBCURL@
tls_alert_test_SOURCES = \
tls_alert_test.c
tls_alert_test.c \
tls_test_common.c
tls_alert_test_LDADD = \
$(top_builddir)/src/testcurl/libcurl_version_check.a \
$(top_builddir)/src/daemon/libmicrohttpd.la
$(top_builddir)/src/daemon/libmicrohttpd.la \
@LIBCURL@
# cURL dependent tests
tls_daemon_options_test_SOURCES = \
tls_daemon_options_test.c
tls_daemon_options_test.c \
tls_test_common.c
tls_daemon_options_test_LDADD = \
$(top_builddir)/src/testcurl/libcurl_version_check.a \
$(top_builddir)/src/daemon/libmicrohttpd.la \
@LIBCURL@
tls_thread_mode_test_SOURCES = \
tls_thread_mode_test.c
tls_thread_mode_test.c \
tls_test_common.c
tls_thread_mode_test_LDADD = \
$(top_builddir)/src/testcurl/libcurl_version_check.a \
$(top_builddir)/src/daemon/libmicrohttpd.la \
@LIBCURL@
tls_multi_thread_mode_test_SOURCES = \
tls_multi_thread_mode_test.c
tls_multi_thread_mode_test.c \
tls_test_common.c
tls_multi_thread_mode_test_LDADD = \
$(top_builddir)/src/testcurl/libcurl_version_check.a \
$(top_builddir)/src/daemon/libmicrohttpd.la \
@LIBCURL@
tls_authentication_test_SOURCES = \
tls_authentication_test.c
tls_authentication_test.c \
tls_test_common.c
tls_authentication_test_LDADD = \
$(top_builddir)/src/testcurl/libcurl_version_check.a \
$(top_builddir)/src/daemon/libmicrohttpd.la \
@LIBCURL@
mhds_session_info_test_SOURCES = \
mhds_session_info_test.c
mhds_session_info_test.c \
tls_test_common.c
mhds_session_info_test_LDADD = \
$(top_builddir)/src/testcurl/libcurl_version_check.a \
$(top_builddir)/src/daemon/libmicrohttpd.la \
@LIBCURL@
mhds_multi_daemon_test_SOURCES = \
mhds_multi_daemon_test.c
mhds_multi_daemon_test.c \
tls_test_common.c
mhds_multi_daemon_test_LDADD = \
$(top_builddir)/src/testcurl/libcurl_version_check.a \
$(top_builddir)/src/daemon/libmicrohttpd.la \
@LIBCURL@
mhds_get_test_SOURCES = \
mhds_get_test.c
mhds_get_test.c \
tls_test_common.c
mhds_get_test_LDADD = \
$(top_builddir)/src/testcurl/libcurl_version_check.a \
$(top_builddir)/src/daemon/libmicrohttpd.la \
+10 -242
View File
@@ -33,214 +33,13 @@
#include "gnutls.h"
#include <curl/curl.h>
#define DEBUG 0
#define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
#define MHD_E_MEM "Error: memory error\n"
#define MHD_E_SERVER_INIT "Error: failed to start server\n"
#define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n"
#define MHD_E_CERT_FILE_CREAT "Error: failed to setup test certificate\n"
#define MHD_E_KEY_FILE_CREAT "Error: failed to setup test certificate\n"
#include "tls_test_keys.h"
const char *test_file_name = "https_test_file";
const char test_file_data[] = "Hello World\n";
#include "tls_test_common.h"
int curl_check_version (const char *req_version, ...);
struct CBC
{
char *buf;
size_t pos;
size_t size;
};
static size_t
copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
{
struct CBC *cbc = ctx;
if (cbc->pos + size * nmemb > cbc->size)
return 0; /* overflow */
memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
cbc->pos += size * nmemb;
return size * nmemb;
}
static int
file_reader (void *cls, size_t pos, char *buf, int max)
{
FILE *file = cls;
fseek (file, pos, SEEK_SET);
return fread (buf, 1, max, file);
}
/* HTTP access handler call back */
static int
http_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size, void **ptr)
{
static int aptr;
struct MHD_Response *response;
int ret;
FILE *file;
struct stat buf;
if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
return MHD_NO; /* unexpected method */
if (&aptr != *ptr)
{
/* do never respond on first call */
*ptr = &aptr;
return MHD_YES;
}
*ptr = NULL; /* reset when done */
file = fopen (url, "r");
if (file == NULL)
{
response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND),
(void *) PAGE_NOT_FOUND,
MHD_NO, MHD_NO);
ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response (response);
}
else
{
stat (url, &buf);
response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
&file_reader, file,
(MHD_ContentReaderFreeCallback)
& fclose);
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
}
return ret;
}
/*
* test HTTPS transfer
* @param test_fd: file to attempt transfering
*/
static int
test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version)
{
CURL *c;
struct CBC cbc;
CURLcode errornum;
char *doc_path;
size_t doc_path_len;
char url[255];
struct stat statb;
stat (test_file_name, &statb);
int len = statb.st_size;
/* used to memcmp local copy & deamon supplied copy */
unsigned char *mem_test_file_local;
/* setup test file path, url */
doc_path_len = PATH_MAX > 4096 ? 4096 : PATH_MAX;
if (NULL == (doc_path = malloc (doc_path_len)))
{
fprintf (stderr, MHD_E_MEM);
return -1;
}
if (getcwd (doc_path, doc_path_len) == NULL)
{
fprintf (stderr, "Error: failed to get working directory. %s\n",
strerror (errno));
free (doc_path);
return -1;
}
if (NULL == (mem_test_file_local = malloc (len)))
{
fprintf (stderr, MHD_E_MEM);
free (doc_path);
return -1;
}
fseek (test_fd, 0, SEEK_SET);
if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len)
{
free (mem_test_file_local);
free (doc_path);
fprintf (stderr, "Error: failed to read test file. %s\n",
strerror (errno));
return -1;
}
if (NULL == (cbc.buf = malloc (len)))
{
free (mem_test_file_local);
free (doc_path);
fprintf (stderr, MHD_E_MEM);
return -1;
}
cbc.size = len;
cbc.pos = 0;
/* construct url - this might use doc_path */
sprintf (url, "%s%s/%s", "https://localhost:42433",
doc_path, test_file_name);
c = curl_easy_init ();
#if DEBUG
curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
#endif
curl_easy_setopt (c, CURLOPT_URL, url);
curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L);
curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L);
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
curl_easy_setopt (c, CURLOPT_FILE, &cbc);
/* TLS options */
curl_easy_setopt (c, CURLOPT_SSLVERSION, proto_version);
curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
/* currently skip any peer authentication */
curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
/* NOTE: use of CONNECTTIMEOUT without also
setting NOSIGNAL results in really weird
crashes on my system! */
curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
if (CURLE_OK != (errornum = curl_easy_perform (c)))
{
fprintf (stderr, "curl_easy_perform failed: `%s'\n",
curl_easy_strerror (errornum));
curl_easy_cleanup (c);
free (mem_test_file_local);
free (doc_path);
free (cbc.buf);
return errornum;
}
curl_easy_cleanup (c);
if (memcmp (cbc.buf, mem_test_file_local, len) != 0)
{
fprintf (stderr, "Error: local file & received file differ.\n");
free (cbc.buf);
free (mem_test_file_local);
free (doc_path);
return -1;
}
free (mem_test_file_local);
free (cbc.buf);
free (doc_path);
return 0;
}
extern const char srv_key_pem[];
extern const char srv_self_signed_cert_pem[];
extern const char srv_signed_cert_pem[];
extern const char srv_signed_key_pem[];
static int
test_cipher_option (FILE * test_fd, char *cipher_suite, int proto_version)
@@ -262,43 +61,12 @@ test_cipher_option (FILE * test_fd, char *cipher_suite, int proto_version)
return -1;
}
ret = test_daemon_get (test_fd, cipher_suite, proto_version);
ret = test_https_transfer (test_fd, cipher_suite, proto_version);
MHD_stop_daemon (d);
return ret;
}
/* setup a temporary transfer test file */
static FILE *
setupTestFile ()
{
FILE *test_fd;
if (NULL == (test_fd = fopen (test_file_name, "w+")))
{
fprintf (stderr, "Error: failed to open `%s': %s\n",
test_file_name, strerror (errno));
return NULL;
}
if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd)
!= strlen (test_file_data))
{
fprintf (stderr, "Error: failed to write `%s. %s'\n",
test_file_name, strerror (errno));
fclose (test_fd);
return NULL;
}
if (fflush (test_fd))
{
fprintf (stderr, "Error: failed to flush test file stream. %s\n",
strerror (errno));
fclose (test_fd);
return NULL;
}
return test_fd;
}
/* perform a HTTP GET request via SSL/TLS */
int
test_secure_get (FILE * test_fd, char *cipher_suite, int proto_version)
@@ -319,7 +87,7 @@ test_secure_get (FILE * test_fd, char *cipher_suite, int proto_version)
return -1;
}
ret = test_daemon_get (test_fd, cipher_suite, proto_version);
ret = test_https_transfer (test_fd, cipher_suite, proto_version);
MHD_stop_daemon (d);
return ret;
@@ -338,7 +106,7 @@ main (int argc, char *const *argv)
return -1;
}
if ((test_fd = setupTestFile ()) == NULL)
if ((test_fd = setup_test_file ()) == NULL)
{
fprintf (stderr, MHD_E_TEST_FILE_CREAT);
return -1;
@@ -358,11 +126,11 @@ main (int argc, char *const *argv)
errorCount +=
test_cipher_option (test_fd, "DES-CBC3-SHA", CURL_SSLVERSION_TLSv1);
print_test_result (errorCount, argv[0]);
curl_global_cleanup ();
fclose (test_fd);
remove (test_file_name);
remove (TEST_FILE_NAME);
return errorCount != 0;
}
+24 -154
View File
@@ -30,93 +30,14 @@
#include <limits.h>
#include <sys/stat.h>
#define DEBUG_CURL_VERBOSE 0
#define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
#define MHD_E_MEM "Error: memory error\n"
#define MHD_E_SERVER_INIT "Error: failed to start server\n"
#define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n"
#include "tls_test_keys.h"
const char *test_file_name = "https_test_file";
const char test_file_data[] = "Hello World\n";
#include "tls_test_common.h"
extern int curl_check_version (const char *req_version, ...);
extern const char srv_key_pem[];
extern const char srv_self_signed_cert_pem[];
struct CBC
{
char *buf;
size_t pos;
size_t size;
};
static size_t
copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
{
struct CBC *cbc = ctx;
if (cbc->pos + size * nmemb > cbc->size)
return 0; /* overflow */
memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
cbc->pos += size * nmemb;
return size * nmemb;
}
static int
file_reader (void *cls, size_t pos, char *buf, int max)
{
FILE *file = cls;
fseek (file, pos, SEEK_SET);
return fread (buf, 1, max, file);
}
/* HTTP access handler call back */
static int
http_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size, void **ptr)
{
static int aptr;
struct MHD_Response *response;
int ret;
FILE *file;
struct stat buf;
if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
return MHD_NO; /* unexpected method */
if (&aptr != *ptr)
{
/* do never respond on first call */
*ptr = &aptr;
return MHD_YES;
}
*ptr = NULL; /* reset when done */
file = fopen (url, "r");
if (file == NULL)
{
response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND),
(void *) PAGE_NOT_FOUND,
MHD_NO, MHD_NO);
ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response (response);
}
else
{
stat (url, &buf);
response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
&file_reader, file,
(MHD_ContentReaderFreeCallback)
& fclose);
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
}
return ret;
}
/*
/* TODO mv to common */
/**
* perform cURL request for file
* @param test_fd: file to attempt transferring
*/
@@ -127,33 +48,16 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version,
CURL *c;
struct CBC cbc;
CURLcode errornum;
char *doc_path;
size_t doc_path_len;
char url[255];
size_t len;
struct stat file_stat;
stat (test_file_name, &file_stat);
stat (TEST_FILE_NAME, &file_stat);
len = file_stat.st_size;
/* used to memcmp local copy & deamon supplied copy */
unsigned char *mem_test_file_local;
/* setup test file path, url */
doc_path_len = PATH_MAX > 4096 ? 4096 : PATH_MAX;
if (NULL == (doc_path = malloc (doc_path_len)))
{
fprintf (stderr, MHD_E_MEM);
return -1;
}
if (getcwd (doc_path, doc_path_len) == NULL)
{
free (doc_path);
fprintf (stderr, "Error: failed to get working directory. %s\n",
strerror (errno));
return -1;
}
mem_test_file_local = malloc (len);
fseek (test_fd, 0, SEEK_SET);
if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len)
@@ -161,7 +65,6 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version,
fprintf (stderr, "Error: failed to read test file. %s\n",
strerror (errno));
free (mem_test_file_local);
free (doc_path);
return -1;
}
@@ -169,19 +72,18 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version,
{
fprintf (stderr, "Error: failed to read test file. %s\n",
strerror (errno));
free (mem_test_file_local);
free (doc_path);
return -1;
}
cbc.size = len;
cbc.pos = 0;
/* construct url */
sprintf (url, "%s:%d%s/%s", "https://localhost", port, doc_path,
test_file_name);
if (gen_test_file_url (url, port))
{
return -1;
}
c = curl_easy_init ();
#if DEBUG_CURL_VERBOSE
#if DEBUG_HTTPS_TEST
curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
#endif
curl_easy_setopt (c, CURLOPT_URL, url);
@@ -210,9 +112,6 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version,
fprintf (stderr, "curl_easy_perform failed: `%s'\n",
curl_easy_strerror (errornum));
curl_easy_cleanup (c);
free (mem_test_file_local);
free (doc_path);
free (cbc.buf);
return errornum;
}
@@ -224,13 +123,11 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version,
fprintf (stderr, "Error: local file & received file differ.\n");
free (mem_test_file_local);
free (cbc.buf);
free (doc_path);
return -1;
}
free (mem_test_file_local);
free (cbc.buf);
free (doc_path);
return 0;
}
@@ -247,7 +144,7 @@ test_concurent_daemon_pair (FILE * test_fd, char *cipher_suite,
struct MHD_Daemon *d1;
struct MHD_Daemon *d2;
d1 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
MHD_USE_DEBUG, 42433,
MHD_USE_DEBUG, DEAMON_TEST_PORT,
NULL, NULL, &http_ahc, NULL,
MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
@@ -260,7 +157,7 @@ test_concurent_daemon_pair (FILE * test_fd, char *cipher_suite,
}
d2 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
MHD_USE_DEBUG, 42434,
MHD_USE_DEBUG, DEAMON_TEST_PORT + 1,
NULL, NULL, &http_ahc, NULL,
MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
@@ -268,50 +165,24 @@ test_concurent_daemon_pair (FILE * test_fd, char *cipher_suite,
if (d2 == NULL)
{
MHD_stop_daemon(d1);
MHD_stop_daemon (d1);
fprintf (stderr, MHD_E_SERVER_INIT);
return -1;
}
ret = test_daemon_get (test_fd, cipher_suite, proto_version, 42433);
ret += test_daemon_get (test_fd, cipher_suite, proto_version, 42434);
ret =
test_daemon_get (test_fd, cipher_suite, proto_version, DEAMON_TEST_PORT);
ret +=
test_daemon_get (test_fd, cipher_suite, proto_version,
DEAMON_TEST_PORT + 1);
MHD_stop_daemon (d2);
ret += test_daemon_get (test_fd, cipher_suite, proto_version, 42433);
ret +=
test_daemon_get (test_fd, cipher_suite, proto_version, DEAMON_TEST_PORT);
MHD_stop_daemon (d1);
return ret;
}
FILE *
setupTestFile ()
{
FILE *test_fd;
if (NULL == (test_fd = fopen (test_file_name, "w+")))
{
fprintf (stderr, "Error: failed to open `%s': %s\n",
test_file_name, strerror (errno));
return NULL;
}
if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd)
!= strlen (test_file_data))
{
fprintf (stderr, "Error: failed to write `%s. %s'\n",
test_file_name, strerror (errno));
fclose (test_fd);
return NULL;
}
if (fflush (test_fd))
{
fprintf (stderr, "Error: failed to flush test file stream. %s\n",
strerror (errno));
fclose (test_fd);
return NULL;
}
return test_fd;
}
int
main (int argc, char *const *argv)
{
@@ -323,7 +194,7 @@ main (int argc, char *const *argv)
return -1;
}
if ((test_fd = setupTestFile ()) == NULL)
if ((test_fd = setup_test_file ()) == NULL)
{
fprintf (stderr, MHD_E_TEST_FILE_CREAT);
return -1;
@@ -340,12 +211,11 @@ main (int argc, char *const *argv)
errorCount +=
test_concurent_daemon_pair (test_fd, "AES256-SHA", CURL_SSLVERSION_SSLv3);
if (errorCount != 0)
fprintf (stderr, "Failed test: %s.\n", __FILE__);
print_test_result (errorCount, "concurent_daemon_pair");
curl_global_cleanup ();
fclose (test_fd);
remove (test_file_name);
remove (TEST_FILE_NAME);
return errorCount != 0;
}
+9 -29
View File
@@ -28,34 +28,14 @@
#include "microhttpd.h"
#include <curl/curl.h>
#define DEBUG_CURL_VERBOSE 0
#define EMPTY_PAGE "<html><head><title>Empty page</title></head><body>Empty page</body></html>"
#include "tls_test_keys.h"
#include "tls_test_common.h"
extern int curl_check_version (const char *req_version, ...);
extern const char srv_key_pem[];
extern const char srv_self_signed_cert_pem[];
struct MHD_Daemon *d;
struct CBC
{
char *buf;
size_t pos;
size_t size;
};
static size_t
copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
{
struct CBC *cbc = ctx;
if (cbc->pos + size * nmemb > cbc->size)
return 0; /* overflow */
memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
cbc->pos += size * nmemb;
return size * nmemb;
}
/*
* HTTP access handler call back
* used to query negotiated security parameters
@@ -103,20 +83,21 @@ query_session_ahc (void *cls, struct MHD_Connection *connection,
static int
test_query_session ()
{
CURL *c;
struct CBC cbc;
CURLcode errornum;
char url[] = "https://localhost:42433/";
char url[256];
if (NULL == (cbc.buf = malloc (sizeof (char) * 255)))
return 16;
cbc.size = 255;
cbc.pos = 0;
gen_test_file_url (url, DEAMON_TEST_PORT);
/* setup test */
d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
MHD_USE_DEBUG, 42433,
MHD_USE_DEBUG, DEAMON_TEST_PORT,
NULL, NULL, &query_session_ahc, NULL,
MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
@@ -126,7 +107,7 @@ test_query_session ()
return 2;
c = curl_easy_init ();
#if DEBUG_CURL_VERBOSE
#if DEBUG_HTTPS_TEST
curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
#endif
curl_easy_setopt (c, CURLOPT_URL, url);
@@ -183,8 +164,7 @@ main (int argc, char *const *argv)
errorCount += test_query_session ();
if (errorCount != 0)
fprintf (stderr, "Failed test: %s.\n", argv[0]);
print_test_result (errorCount, argv[0]);
curl_global_cleanup ();
+15 -83
View File
@@ -30,74 +30,10 @@
#include "gnutls_int.h"
#include "gnutls_datum.h"
#include "gnutls_record.h"
#include "tls_test_keys.h"
#define MHD_E_MEM "Error: memory error\n"
#define MHD_E_SERVER_INIT "Error: failed to start server\n"
#define MHD_E_FAILED_TO_CONNECT "Error: server connection could not be established\n"
const char *ca_cert_file_name = "ca_cert_pem";
const char *test_file_name = "https_test_file";
const char test_file_data[] = "Hello World\n";
struct CBC
{
char *buf;
size_t pos;
size_t size;
};
/* HTTP access handler call back */
static int
http_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size, void **ptr)
{
return 0;
}
static int
setup (MHD_gtls_session_t * session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert, MHD_gtls_cert_credentials_t * xcred)
{
int ret;
const char *err_pos;
MHD__gnutls_certificate_allocate_credentials (xcred);
MHD_gtls_set_datum_m (key, srv_key_pem, strlen (srv_key_pem), &malloc);
MHD_gtls_set_datum_m (cert, srv_self_signed_cert_pem,
strlen (srv_self_signed_cert_pem), &malloc);
MHD__gnutls_certificate_set_x509_key_mem (*xcred, cert, key,
GNUTLS_X509_FMT_PEM);
MHD__gnutls_init (session, GNUTLS_CLIENT);
ret = MHD__gnutls_priority_set_direct (*session, "NORMAL", &err_pos);
if (ret < 0)
{
return -1;
}
MHD__gnutls_credentials_set (*session, MHD_GNUTLS_CRD_CERTIFICATE, xcred);
return 0;
}
static int
teardown (MHD_gtls_session_t session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert, MHD_gtls_cert_credentials_t xcred)
{
MHD_gtls_free_datum_m (key, free);
MHD_gtls_free_datum_m (cert, free);
MHD__gnutls_deinit (session);
MHD__gnutls_certificate_free_credentials (xcred);
return 0;
}
#include "tls_test_common.h"
extern const char srv_key_pem[];
extern const char srv_self_signed_cert_pem[];
/*
* assert server closes connection upon receiving a
@@ -114,14 +50,13 @@ test_alert_close_notify (MHD_gtls_session_t session)
sd = socket (AF_INET, SOCK_STREAM, 0);
if (sd == -1)
{
fprintf(stderr,
"Failed to create socket: %s\n",
strerror(errno));
fprintf (stderr, "Failed to create socket: %s\n", strerror (errno));
return -1;
}
memset (&sa, '\0', sizeof (struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_port = htons (42433);
sa.sin_port = htons (DEAMON_TEST_PORT);
inet_pton (AF_INET, "127.0.0.1", &sa.sin_addr);
MHD__gnutls_transport_set_ptr (session, (MHD_gnutls_transport_ptr_t) sd);
@@ -170,14 +105,12 @@ test_alert_unexpected_message (MHD_gtls_session_t session)
sd = socket (AF_INET, SOCK_STREAM, 0);
if (sd == -1)
{
fprintf(stderr,
"Failed to create socket: %s\n",
strerror(errno));
fprintf (stderr, "Failed to create socket: %s\n", strerror (errno));
return -1;
}
memset (&sa, '\0', sizeof (struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_port = htons (42433);
sa.sin_port = htons (DEAMON_TEST_PORT);
inet_pton (AF_INET, "127.0.0.1", &sa.sin_addr);
MHD__gnutls_transport_set_ptr (session,
@@ -225,8 +158,8 @@ main (int argc, char *const *argv)
MHD_gtls_global_set_log_level (11);
d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
MHD_USE_DEBUG, 42433,
NULL, NULL, &http_ahc, NULL,
MHD_USE_DEBUG, DEAMON_TEST_PORT,
NULL, NULL, &http_dummy_ahc, NULL,
MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
MHD_OPTION_END);
@@ -237,16 +170,15 @@ main (int argc, char *const *argv)
return -1;
}
setup (&session, &key, &cert, &xcred);
setup_session (&session, &key, &cert, &xcred);
errorCount += test_alert_close_notify (session);
teardown (session, &key, &cert, xcred);
teardown_session (session, &key, &cert, xcred);
setup (&session, &key, &cert, &xcred);
setup_session (&session, &key, &cert, &xcred);
errorCount += test_alert_unexpected_message (session);
teardown (session, &key, &cert, xcred);
teardown_session (session, &key, &cert, xcred);
if (errorCount != 0)
fprintf (stderr, "Failed test: %s.\n", argv[0]);
print_test_result (errorCount, argv[0]);
MHD_stop_daemon (d);
MHD__gnutls_global_deinit ();
+23 -156
View File
@@ -30,93 +30,17 @@
#include <limits.h>
#include <sys/stat.h>
#define DEBUG_CURL_VERBOSE 0
#define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
#define MHD_E_MEM "Error: memory error\n"
#define MHD_E_SERVER_INIT "Error: failed to start server\n"
#define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n"
#include "tls_test_keys.h"
#include "tls_test_common.h"
extern int curl_check_version (const char *req_version, ...);
extern const char test_file_data[];
extern const char ca_key_pem[];
extern const char ca_cert_pem[];
extern const char srv_signed_cert_pem[];
extern const char srv_signed_key_pem[];
const int DEBUG_GNUTLS_LOG_LEVEL = 6;
const char *ca_cert_file_name = "ca_cert_pem";
const char *test_file_name = "https_test_file";
const char test_file_data[] = "Hello World\n";
struct CBC
{
char *buf;
size_t pos;
size_t size;
};
static size_t
copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
{
struct CBC *cbc = ctx;
if (cbc->pos + size * nmemb > cbc->size)
return 0; /* overflow */
memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
cbc->pos += size * nmemb;
return size * nmemb;
}
static int
file_reader (void *cls, size_t pos, char *buf, int max)
{
FILE *file = cls;
fseek (file, pos, SEEK_SET);
return fread (buf, 1, max, file);
}
/* HTTP access handler call back */
static int
http_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size, void **ptr)
{
static int aptr;
struct MHD_Response *response;
int ret;
FILE *file;
struct stat buf;
if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
return MHD_NO; /* unexpected method */
if (&aptr != *ptr)
{
/* do never respond on first call */
*ptr = &aptr;
return MHD_YES;
}
*ptr = NULL; /* reset when done */
file = fopen (url, "r");
if (file == NULL)
{
response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND),
(void *) PAGE_NOT_FOUND,
MHD_NO, MHD_NO);
ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response (response);
}
else
{
stat (url, &buf);
response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
&file_reader, file,
(MHD_ContentReaderFreeCallback)
& fclose);
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
}
return ret;
}
/*
* test HTTPS transfer
@@ -128,37 +52,19 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version)
CURL *c;
struct CBC cbc;
CURLcode errornum;
char *doc_path;
size_t doc_path_len;
char url[255];
struct stat statb;
stat (test_file_name, &statb);
stat (TEST_FILE_NAME, &statb);
int len = statb.st_size;
/* used to memcmp local copy & deamon supplied copy */
unsigned char *mem_test_file_local;
/* setup test file path, url */
doc_path_len = PATH_MAX > 4096 ? 4096 : PATH_MAX;
if (NULL == (doc_path = malloc (doc_path_len)))
{
fprintf (stderr, MHD_E_MEM);
return -1;
}
if (getcwd (doc_path, doc_path_len) == NULL)
{
fprintf (stderr, "Error: failed to get working directory. %s\n",
strerror (errno));
free (doc_path);
return -1;
}
if (NULL == (mem_test_file_local = malloc (len)))
{
fprintf (stderr, MHD_E_MEM);
free (doc_path);
return -1;
}
@@ -167,7 +73,6 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version)
{
fprintf (stderr, "Error: failed to read test file. %s\n",
strerror (errno));
free (doc_path);
free (mem_test_file_local);
return -1;
}
@@ -175,7 +80,6 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version)
if (NULL == (cbc.buf = malloc (sizeof (char) * len)))
{
fprintf (stderr, MHD_E_MEM);
free (doc_path);
free (mem_test_file_local);
return -1;
}
@@ -183,11 +87,10 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version)
cbc.pos = 0;
/* construct url - this might use doc_path */
sprintf (url, "%s%s/%s", "https://localhost:42433",
doc_path, test_file_name);
gen_test_file_url (url, DEAMON_TEST_PORT);
c = curl_easy_init ();
#if DEBUG_CURL_VERBOSE
#if DEBUG_HTTPS_TEST
curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
#endif
curl_easy_setopt (c, CURLOPT_URL, url);
@@ -202,6 +105,7 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version)
curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
/* perform peer authentication */
/* TODO merge into send_curl_req */
curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt (c, CURLOPT_CAINFO, ca_cert_file_name);
@@ -218,9 +122,8 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version)
fprintf (stderr, "curl_easy_perform failed: `%s'\n",
curl_easy_strerror (errornum));
curl_easy_cleanup (c);
free (cbc.buf);
free (doc_path);
free (mem_test_file_local);
free (cbc.buf);
return errornum;
}
@@ -231,13 +134,11 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version)
fprintf (stderr, "Error: local file & received file differ.\n");
free (cbc.buf);
free (mem_test_file_local);
free (doc_path);
return -1;
}
free (mem_test_file_local);
free (cbc.buf);
free (doc_path);
return 0;
}
@@ -249,7 +150,7 @@ test_secure_get (FILE * test_fd, char *cipher_suite, int proto_version)
struct MHD_Daemon *d;
d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
MHD_USE_DEBUG, 42433,
MHD_USE_DEBUG, DEAMON_TEST_PORT,
NULL, NULL, &http_ahc, NULL,
MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem,
MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem,
@@ -267,65 +168,34 @@ test_secure_get (FILE * test_fd, char *cipher_suite, int proto_version)
return ret;
}
/* setup a temporary transfer test file */
static FILE *
setupTestFile ()
{
FILE *test_fd;
if (NULL == (test_fd = fopen (test_file_name, "w+")))
{
fprintf (stderr, "Error: failed to open `%s': %s\n",
test_file_name, strerror (errno));
return NULL;
}
if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd)
!= strlen (test_file_data))
{
fprintf (stderr, "Error: failed to write `%s. %s'\n",
test_file_name, strerror (errno));
fclose (test_fd);
return NULL;
}
if (fflush (test_fd))
{
fprintf (stderr, "Error: failed to flush test file stream. %s\n",
strerror (errno));
fclose (test_fd);
return NULL;
}
return test_fd;
}
static FILE *
setup_ca_cert ()
{
FILE *fd;
FILE *cert_fd;
if (NULL == (fd = fopen (ca_cert_file_name, "w+")))
if (NULL == (cert_fd = fopen (ca_cert_file_name, "w+")))
{
fprintf (stderr, "Error: failed to open `%s': %s\n",
ca_cert_file_name, strerror (errno));
return NULL;
}
if (fwrite (ca_cert_pem, sizeof (char), strlen (ca_cert_pem), fd)
if (fwrite (ca_cert_pem, sizeof (char), strlen (ca_cert_pem), cert_fd)
!= strlen (ca_cert_pem))
{
fprintf (stderr, "Error: failed to write `%s. %s'\n",
ca_cert_file_name, strerror (errno));
fclose (fd);
fclose (cert_fd);
return NULL;
}
if (fflush (fd))
if (fflush (cert_fd))
{
fprintf (stderr, "Error: failed to flush ca cert file stream. %s\n",
strerror (errno));
fclose (fd);
fclose (cert_fd);
return NULL;
}
return fd;
return cert_fd;
}
int
@@ -339,14 +209,12 @@ main (int argc, char *const *argv)
return -1;
}
if ((test_fd = setupTestFile ()) == NULL)
if ((test_fd = setup_test_file ()) == NULL || setup_ca_cert () == NULL)
{
fprintf (stderr, MHD_E_TEST_FILE_CREAT);
return -1;
}
setup_ca_cert ();
if (0 != curl_global_init (CURL_GLOBAL_ALL))
{
fprintf (stderr, "Error (code: %u)\n", errorCount);
@@ -357,13 +225,12 @@ main (int argc, char *const *argv)
errorCount +=
test_secure_get (test_fd, "AES256-SHA", CURL_SSLVERSION_TLSv1);
if (errorCount != 0)
fprintf (stderr, "Failed test: %s.\n", argv[0]);
print_test_result (errorCount, argv[0]);
curl_global_cleanup ();
fclose (test_fd);
remove (test_file_name);
remove (TEST_FILE_NAME);
remove (ca_cert_file_name);
return errorCount != 0;
}
+10 -55
View File
@@ -31,10 +31,10 @@
#include "gnutls_int.h"
#include "gnutls_datum.h"
#include "gnutls_record.h"
#include "tls_test_keys.h"
#define MHD_E_SERVER_INIT "Error: failed to start server\n"
#define MHD_E_FAILED_TO_CONNECT "Error: server connection could not be established\n"
#include "tls_test_common.h"
extern const char srv_key_pem[];
extern const char srv_self_signed_cert_pem[];
char *http_get_req = "GET / HTTP/1.1\r\n\r\n";
@@ -58,49 +58,6 @@ rehandshake_ahc (void *cls, struct MHD_Connection *connection,
return 0;
}
static int
setup (MHD_gtls_session_t * session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert, MHD_gtls_cert_credentials_t * xcred)
{
int ret;
const char *err_pos;
MHD__gnutls_certificate_allocate_credentials (xcred);
MHD_gtls_set_datum_m (key, srv_key_pem, strlen (srv_key_pem), &malloc);
MHD_gtls_set_datum_m (cert, srv_self_signed_cert_pem,
strlen (srv_self_signed_cert_pem), &malloc);
MHD__gnutls_certificate_set_x509_key_mem (*xcred, cert, key,
GNUTLS_X509_FMT_PEM);
MHD__gnutls_init (session, GNUTLS_CLIENT);
ret = MHD__gnutls_priority_set_direct (*session, "NORMAL", &err_pos);
if (ret < 0)
{
return -1;
}
MHD__gnutls_credentials_set (*session, MHD_GNUTLS_CRD_CERTIFICATE, xcred);
return 0;
}
static int
teardown (MHD_gtls_session_t session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert, MHD_gtls_cert_credentials_t xcred)
{
MHD_gtls_free_datum_m (key, free);
MHD_gtls_free_datum_m (cert, free);
MHD__gnutls_deinit (session);
MHD__gnutls_certificate_free_credentials (xcred);
return 0;
}
/*
* Cipher change message should only occur while negotiating
* the SSL/TLS handshake.
@@ -118,14 +75,13 @@ test_out_of_context_cipher_change (MHD_gtls_session_t session)
sd = socket (AF_INET, SOCK_STREAM, 0);
if (sd == -1)
{
fprintf(stderr,
"Failed to create socket: %s\n",
strerror(errno));
fprintf (stderr, "Failed to create socket: %s\n", strerror (errno));
return -1;
}
memset (&sa, '\0', sizeof (struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_port = htons (42433);
sa.sin_port = htons (DEAMON_TEST_PORT);
inet_pton (AF_INET, "127.0.0.1", &sa.sin_addr);
MHD__gnutls_transport_set_ptr (session, (MHD_gnutls_transport_ptr_t) sd);
@@ -173,7 +129,7 @@ main (int argc, char *const *argv)
MHD_gtls_global_set_log_level (11);
d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
MHD_USE_DEBUG, 42433,
MHD_USE_DEBUG, DEAMON_TEST_PORT,
NULL, NULL, &rehandshake_ahc, NULL,
MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
@@ -185,12 +141,11 @@ main (int argc, char *const *argv)
return -1;
}
setup (&session, &key, &cert, &xcred);
setup_session (&session, &key, &cert, &xcred);
errorCount += test_out_of_context_cipher_change (session);
teardown (session, &key, &cert, xcred);
teardown_session (session, &key, &cert, xcred);
if (errorCount != 0)
fprintf (stderr, "Failed test: %s.\n", argv[0]);
print_test_result (errorCount, argv[0]);
MHD_stop_daemon (d);
MHD__gnutls_global_deinit ();
+35 -351
View File
@@ -26,360 +26,46 @@
#include "platform.h"
#include "microhttpd.h"
#include <sys/stat.h>
#include <limits.h>
#include "gnutls.h"
#include <curl/curl.h>
#define DEBUG_CURL_VERBOSE 0
#define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
#include "tls_test_common.h"
#define MHD_E_MEM "Error: memory error\n"
#define MHD_E_SERVER_INIT "Error: failed to start server\n"
#define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n"
#define MHD_E_CERT_FILE_CREAT "Error: failed to setup test certificate\n"
#define MHD_E_KEY_FILE_CREAT "Error: failed to setup test certificate\n"
#include "tls_test_keys.h"
const char *test_file_name = "https_test_file";
const char test_file_data[] = "Hello World\n";
extern const char srv_key_pem[];
extern const char srv_self_signed_cert_pem[];
int curl_check_version (const char *req_version, ...);
struct CBC
{
char *buf;
size_t pos;
size_t size;
};
struct https_test_data
{
FILE *test_fd;
char *cipher_suite;
int proto_version;
};
struct CipherDef
{
int options[2];
char *curlname;
};
static size_t
copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
{
struct CBC *cbc = ctx;
if (cbc->pos + size * nmemb > cbc->size)
return 0; /* overflow */
memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
cbc->pos += size * nmemb;
return size * nmemb;
}
static int
file_reader (void *cls, size_t pos, char *buf, int max)
{
FILE *file = cls;
fseek (file, pos, SEEK_SET);
return fread (buf, 1, max, file);
}
/* HTTP access handler call back */
static int
http_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size, void **ptr)
{
static int aptr;
struct MHD_Response *response;
int ret;
FILE *file;
struct stat buf;
if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
return MHD_NO; /* unexpected method */
if (&aptr != *ptr)
{
/* do never respond on first call */
*ptr = &aptr;
return MHD_YES;
}
*ptr = NULL; /* reset when done */
file = fopen (url, "r");
if (file == NULL)
{
response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND),
(void *) PAGE_NOT_FOUND,
MHD_NO, MHD_NO);
ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response (response);
}
else
{
stat (url, &buf);
response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
&file_reader, file,
(MHD_ContentReaderFreeCallback)
& fclose);
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
}
return ret;
}
/**
* test HTTPS transfer
* @param test_fd: file to attempt transfering
*/
static int
test_https_transfer (FILE * test_fd, char *cipher_suite, int proto_version)
{
CURL *c;
CURLcode errornum;
struct CBC cbc;
char *doc_path;
size_t doc_path_len;
char url[255];
struct stat statb;
stat (test_file_name, &statb);
int len = statb.st_size;
/* used to memcmp local copy & deamon supplied copy */
unsigned char *mem_test_file_local;
/* setup test file path, url */
doc_path_len = PATH_MAX > 4096 ? 4096 : PATH_MAX;
if (NULL == (doc_path = malloc (doc_path_len)))
{
fprintf (stderr, MHD_E_MEM);
return -1;
}
if (getcwd (doc_path, doc_path_len) == NULL)
{
fprintf (stderr, "Error: failed to get working directory. %s\n",
strerror (errno));
free (doc_path);
return -1;
}
if (NULL == (mem_test_file_local = malloc (len)))
{
fprintf (stderr, MHD_E_MEM);
free (doc_path);
return -1;
}
fseek (test_fd, 0, SEEK_SET);
if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len)
{
fprintf (stderr, "Error: failed to read test file. %s\n",
strerror (errno));
free (mem_test_file_local);
free (doc_path);
return -1;
}
if (NULL == (cbc.buf = malloc (sizeof (char) * len)))
{
fprintf (stderr, MHD_E_MEM);
free (mem_test_file_local);
free (doc_path);
return -1;
}
cbc.size = len;
cbc.pos = 0;
/* construct url - this might use doc_path */
sprintf (url, "%s%s/%s", "https://localhost:42433",
doc_path, test_file_name);
c = curl_easy_init ();
#if DEBUG_CURL_VERBOSE
curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
#endif
curl_easy_setopt (c, CURLOPT_URL, url);
curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_easy_setopt (c, CURLOPT_TIMEOUT, 60L);
curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 60L);
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
curl_easy_setopt (c, CURLOPT_FILE, &cbc);
/* TLS options */
curl_easy_setopt (c, CURLOPT_SSLVERSION, proto_version);
curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
/* currently skip any peer authentication */
curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
/* NOTE: use of CONNECTTIMEOUT without also
setting NOSIGNAL results in really weird
crashes on my system! */
curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
if (CURLE_OK != (errornum = curl_easy_perform (c)))
{
fprintf (stderr, "curl_easy_perform failed: `%s'\n",
curl_easy_strerror (errornum));
curl_easy_cleanup (c);
free (cbc.buf);
free (mem_test_file_local);
free (doc_path);
return errornum;
}
curl_easy_cleanup (c);
if (memcmp (cbc.buf, mem_test_file_local, len) != 0)
{
fprintf (stderr, "Error: local file & received file differ.\n");
free (cbc.buf);
free (mem_test_file_local);
free (doc_path);
return -1;
}
free (mem_test_file_local);
free (cbc.buf);
free (doc_path);
return 0;
}
static FILE *
setupTestFile ()
{
FILE *test_fd;
if (NULL == (test_fd = fopen (test_file_name, "w+")))
{
fprintf (stderr, "Error: failed to open `%s': %s\n",
test_file_name, strerror (errno));
return NULL;
}
if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd)
!= strlen (test_file_data))
{
fprintf (stderr, "Error: failed to write `%s. %s'\n",
test_file_name, strerror (errno));
fclose (test_fd);
return NULL;
}
if (fflush (test_fd))
{
fprintf (stderr, "Error: failed to flush test file stream. %s\n",
strerror (errno));
fclose (test_fd);
return NULL;
}
return test_fd;
}
static int
setup (struct MHD_Daemon **d, int daemon_flags, va_list arg_list)
{
*d = MHD_start_daemon_va (daemon_flags, 42433,
NULL, NULL, &http_ahc, NULL, arg_list);
if (*d == NULL)
{
fprintf (stderr, MHD_E_SERVER_INIT);
return -1;
}
return 0;
}
static void
teardown (struct MHD_Daemon *d)
{
MHD_stop_daemon (d);
}
/* TODO test_wrap: change sig to (setup_func, test, va_list test_arg) & move to test_util.c */
static int
test_wrap (char *test_name, int
(*test_function) (FILE * test_fd, char *cipher_suite,
int proto_version), FILE * test_fd,
int daemon_flags, char *cipher_suite, int proto_version, ...)
{
int ret;
va_list arg_list;
struct MHD_Daemon *d;
va_start (arg_list, proto_version);
if (setup (&d, daemon_flags, arg_list) != 0)
{
va_end (arg_list);
return -1;
}
fprintf (stdout, "running test: %s ", test_name);
ret = test_function (test_fd, cipher_suite, proto_version);
if (ret == 0)
{
fprintf (stdout, "[pass]\n");
}
else
{
fprintf (stdout, "[fail]\n");
}
teardown (d);
va_end (arg_list);
return ret;
}
/**
* test server refuses to negotiate connections with unsupported protocol versions
*
*/
/* TODO rm test_fd */
static int
test_protocol_version (FILE * test_fd, char *cipher_suite,
int curl_proto_version)
test_unmatching_ssl_version (FILE * test_fd, char *cipher_suite,
int curl_req_ssl_version)
{
CURL *c;
CURLcode errornum;
struct CBC cbc;
if (NULL == (cbc.buf = malloc (sizeof (char) * 256)))
{
fprintf (stderr, "Error: failed to read test file. %s\n",
strerror (errno));
return -1;
}
cbc.size = 256;
cbc.pos = 0;
c = curl_easy_init ();
#if DEBUG_CURL_VERBOSE
curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
#endif
curl_easy_setopt (c, CURLOPT_URL, "https://localhost:42433/");
curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_easy_setopt (c, CURLOPT_TIMEOUT, 3L);
curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 3L);
char url[255];
if (gen_test_file_url (url, DEAMON_TEST_PORT))
{
return -1;
}
/* TLS options */
curl_easy_setopt (c, CURLOPT_SSLVERSION, curl_proto_version);
curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
/* NOTE: use of CONNECTTIMEOUT without also
setting NOSIGNAL results in really weird
crashes on my system! */
curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
/* assert daemon rejected request */
if (CURLE_OK == (errornum = curl_easy_perform (c)))
/* assert daemon *rejected* request */
if (CURLE_OK ==
send_curl_req (url, &cbc, cipher_suite, curl_req_ssl_version))
{
fprintf (stderr, "curl_easy_perform failed: `%s'\n",
curl_easy_strerror (errornum));
curl_easy_cleanup (c);
return -1;
}
@@ -403,7 +89,7 @@ main (int argc, char *const *argv)
return -1;
}
if ((test_fd = setupTestFile ()) == NULL)
if ((test_fd = setup_test_file ()) == NULL)
{
fprintf (stderr, MHD_E_TEST_FILE_CREAT);
return -1;
@@ -411,8 +97,9 @@ main (int argc, char *const *argv)
if (0 != curl_global_init (CURL_GLOBAL_ALL))
{
fprintf (stderr, "Error: %s\n", strerror (errno));
fclose (test_fd);
remove (TEST_FILE_NAME);
fprintf (stderr, "Error: %s\n", strerror (errno));
return -1;
}
@@ -422,12 +109,12 @@ main (int argc, char *const *argv)
MHD_GNUTLS_PROTOCOL_TLS1_0, 0
};
struct CipherDef ciphers[] =
{ {{MHD_GNUTLS_CIPHER_ARCFOUR_128, 0}, "RC4-SHA"},
{{MHD_GNUTLS_CIPHER_3DES_CBC, 0}, "3DES-SHA"},
{{MHD_GNUTLS_CIPHER_AES_128_CBC, 0}, "AES128-SHA"},
{{MHD_GNUTLS_CIPHER_AES_256_CBC, 0}, "AES256-SHA"},
{{0, 0}, NULL}
struct CipherDef ciphers[] = {
{{MHD_GNUTLS_CIPHER_AES_128_CBC, 0}, "AES128-SHA"},
{{MHD_GNUTLS_CIPHER_ARCFOUR_128, 0}, "RC4-SHA"},
{{MHD_GNUTLS_CIPHER_3DES_CBC, 0}, "3DES-SHA"},
{{MHD_GNUTLS_CIPHER_AES_256_CBC, 0}, "AES256-SHA"},
{{0, 0}, NULL}
};
fprintf (stderr, "SHA/TLS tests:\n");
@@ -466,18 +153,15 @@ main (int argc, char *const *argv)
}
errorCount +=
test_wrap ("protocol_version", &test_protocol_version, test_fd,
daemon_flags, "AES256-SHA", CURL_SSLVERSION_TLSv1,
test_wrap ("unmatching SSL version", &test_unmatching_ssl_version,
test_fd, daemon_flags, "AES256-SHA", CURL_SSLVERSION_TLSv1,
MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
MHD_OPTION_PROTOCOL_VERSION, p_ssl3, MHD_OPTION_END);
if (errorCount != 0)
fprintf (stderr, "Failed test: %s.\n", argv[0]);
curl_global_cleanup ();
fclose (test_fd);
remove (test_file_name);
remove (TEST_FILE_NAME);
return errorCount != 0;
}
+14 -316
View File
@@ -35,229 +35,12 @@
#include "gnutls.h"
#include <curl/curl.h>
#define DEBUG_CURL_VERBOSE 0
#define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
#define MHD_E_MEM "Error: memory error\n"
#define MHD_E_SERVER_INIT "Error: failed to start server\n"
#define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n"
#define MHD_E_CERT_FILE_CREAT "Error: failed to setup test certificate\n"
#define MHD_E_KEY_FILE_CREAT "Error: failed to setup test certificate\n"
#include "tls_test_keys.h"
const char *test_file_name = "https_test_file";
const char test_file_data[] = "Hello World\n";
#include "tls_test_common.h"
extern const char srv_key_pem[];
extern const char srv_self_signed_cert_pem[];
int curl_check_version (const char *req_version, ...);
struct CBC
{
char *buf;
size_t pos;
size_t size;
};
struct https_test_data
{
FILE *test_fd;
char *cipher_suite;
int proto_version;
};
struct CipherDef
{
int options[2];
char *curlname;
};
static size_t
copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
{
struct CBC *cbc = ctx;
if (cbc->pos + size * nmemb > cbc->size)
return 0; /* overflow */
memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
cbc->pos += size * nmemb;
return size * nmemb;
}
static int
file_reader (void *cls, size_t pos, char *buf, int max)
{
FILE *file = cls;
fseek (file, pos, SEEK_SET);
return fread (buf, 1, max, file);
}
/* HTTP access handler call back */
static int
http_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size, void **ptr)
{
static int aptr;
struct MHD_Response *response;
int ret;
FILE *file;
struct stat buf;
if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
return MHD_NO; /* unexpected method */
if (&aptr != *ptr)
{
/* do never respond on first call */
*ptr = &aptr;
return MHD_YES;
}
*ptr = NULL; /* reset when done */
file = fopen (url, "r");
if (file == NULL)
{
response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND),
(void *) PAGE_NOT_FOUND,
MHD_NO, MHD_NO);
ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response (response);
}
else
{
stat (url, &buf);
response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
&file_reader, file,
(MHD_ContentReaderFreeCallback)
& fclose);
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
}
return ret;
}
/**
* test HTTPS transfer
* @param test_fd: file to attempt transfering
*/
static int
test_https_transfer (FILE * test_fd, char *cipher_suite, int proto_version)
{
CURL *c;
CURLcode errornum;
struct CBC cbc;
char *doc_path;
size_t doc_path_len;
char url[255];
struct stat statb;
stat (test_file_name, &statb);
int len = statb.st_size;
/* used to memcmp local copy & deamon supplied copy */
unsigned char *mem_test_file_local;
/* setup test file path, url */
doc_path_len = PATH_MAX > 4096 ? 4096 : PATH_MAX;
if (NULL == (doc_path = malloc (doc_path_len)))
{
fprintf (stderr, MHD_E_MEM);
return -1;
}
if (getcwd (doc_path, doc_path_len) == NULL)
{
fprintf (stderr, "Error: failed to get working directory. %s\n",
strerror (errno));
free (doc_path);
return -1;
}
if (NULL == (mem_test_file_local = malloc (len)))
{
fprintf (stderr, MHD_E_MEM);
free (doc_path);
return -1;
}
fseek (test_fd, 0, SEEK_SET);
if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len)
{
fprintf (stderr, "Error: failed to read test file. %s\n",
strerror (errno));
free (doc_path);
free (mem_test_file_local);
return -1;
}
if (NULL == (cbc.buf = malloc (len)))
{
fprintf (stderr, MHD_E_MEM);
free (doc_path);
free (mem_test_file_local);
return -1;
}
cbc.size = len;
cbc.pos = 0;
/* construct url - this might use doc_path */
sprintf (url, "%s%s/%s", "https://localhost:42433",
doc_path, test_file_name);
c = curl_easy_init ();
#if DEBUG_CURL_VERBOSE
curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
#endif
curl_easy_setopt (c, CURLOPT_URL, url);
curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_easy_setopt (c, CURLOPT_TIMEOUT, 15L);
curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
curl_easy_setopt (c, CURLOPT_FILE, &cbc);
/* TLS options */
curl_easy_setopt (c, CURLOPT_SSLVERSION, proto_version);
curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
/* currently skip any peer authentication */
curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
/* NOTE: use of CONNECTTIMEOUT without also
setting NOSIGNAL results in really weird
crashes on my system! */
curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
if (CURLE_OK != (errornum = curl_easy_perform (c)))
{
fprintf (stderr, "curl_easy_perform failed: `%s'\n",
curl_easy_strerror (errornum));
curl_easy_cleanup (c);
free (cbc.buf);
free (mem_test_file_local);
free (doc_path);
return errornum;
}
curl_easy_cleanup (c);
if (memcmp (cbc.buf, mem_test_file_local, len) != 0)
{
fprintf (stderr, "Error: local file & received file differ.\n");
free (cbc.buf);
free (mem_test_file_local);
free (doc_path);
return -1;
}
free (mem_test_file_local);
free (cbc.buf);
free (doc_path);
return 0;
}
/**
* used when spawning multiple threads executing curl server requests
*
@@ -278,92 +61,6 @@ https_transfer_thread_adapter (void *args)
return &nonnull;
}
static FILE *
setupTestFile ()
{
FILE *test_fd;
if (NULL == (test_fd = fopen (test_file_name, "w+")))
{
fprintf (stderr, "Error: failed to open `%s': %s\n",
test_file_name, strerror (errno));
return NULL;
}
if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd)
!= strlen (test_file_data))
{
fprintf (stderr, "Error: failed to write `%s. %s'\n",
test_file_name, strerror (errno));
fclose (test_fd);
return NULL;
}
if (fflush (test_fd))
{
fprintf (stderr, "Error: failed to flush test file stream. %s\n",
strerror (errno));
fclose (test_fd);
return NULL;
}
return test_fd;
}
static int
setup (struct MHD_Daemon **d, int daemon_flags, va_list arg_list)
{
*d = MHD_start_daemon_va (daemon_flags, 42433,
NULL, NULL, &http_ahc, NULL, arg_list);
if (*d == NULL)
{
fprintf (stderr, MHD_E_SERVER_INIT);
return -1;
}
return 0;
}
static void
teardown (struct MHD_Daemon *d)
{
MHD_stop_daemon (d);
}
/* TODO test_wrap: change sig to (setup_func, test, va_list test_arg) & move to test_util.c */
static int
test_wrap (char *test_name, int
(*test_function) (FILE * test_fd, char *cipher_suite,
int proto_version), FILE * test_fd,
int daemon_flags, char *cipher_suite, int proto_version, ...)
{
int ret;
va_list arg_list;
struct MHD_Daemon *d;
va_start (arg_list, proto_version);
if (setup (&d, daemon_flags, arg_list) != 0)
{
va_end (arg_list);
return -1;
}
fprintf (stdout, "running test: %s ", test_name);
ret = test_function (test_fd, cipher_suite, proto_version);
if (ret == 0)
{
fprintf (stdout, "[pass]\n");
}
else
{
fprintf (stdout, "[fail]\n");
}
teardown (d);
va_end (arg_list);
return ret;
}
/**
* Test non-parallel requests.
*
@@ -407,11 +104,10 @@ test_parallel_clients (FILE * test_fd, char *cipher_suite,
for (i = 0; i < client_count; ++i)
{
if (pthread_create (&client_arr[i], NULL,
&https_transfer_thread_adapter,
&client_args) != 0)
&https_transfer_thread_adapter, &client_args) != 0)
{
fprintf (stderr, "Error: failed to spawn test client threads.\n");
return -1;
}
}
@@ -441,7 +137,7 @@ main (int argc, char *const *argv)
if (curl_check_version (MHD_REQ_CURL_VERSION))
return -1;
if ((test_fd = setupTestFile ()) == NULL)
if ((test_fd = setup_test_file ()) == NULL)
{
fprintf (stderr, MHD_E_TEST_FILE_CREAT);
return -1;
@@ -456,14 +152,16 @@ main (int argc, char *const *argv)
errorCount +=
test_wrap ("multi threaded daemon, single client", &test_single_client,
test_fd, MHD_USE_SSL | MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION, "AES256-SHA",
CURL_SSLVERSION_TLSv1, MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
MHD_OPTION_END);
test_fd,
MHD_USE_SSL | MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION,
"AES256-SHA", CURL_SSLVERSION_TLSv1, MHD_OPTION_HTTPS_MEM_KEY,
srv_key_pem, MHD_OPTION_HTTPS_MEM_CERT,
srv_self_signed_cert_pem, MHD_OPTION_END);
errorCount +=
test_wrap ("multi threaded daemon, parallel client",
&test_parallel_clients, test_fd, MHD_USE_SSL | MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION,
&test_parallel_clients, test_fd,
MHD_USE_SSL | MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION,
"AES256-SHA", CURL_SSLVERSION_TLSv1, MHD_OPTION_HTTPS_MEM_KEY,
srv_key_pem, MHD_OPTION_HTTPS_MEM_CERT,
srv_self_signed_cert_pem, MHD_OPTION_END);
@@ -474,7 +172,7 @@ main (int argc, char *const *argv)
curl_global_cleanup ();
fclose (test_fd);
remove (test_file_name);
remove (TEST_FILE_NAME);
return errorCount != 0;
}
+19 -33
View File
@@ -31,33 +31,20 @@
#include "gnutls_int.h"
#include "gnutls_datum.h"
#include "gnutls_record.h"
#include "tls_test_keys.h"
#define MHD_E_MEM "Error: memory error\n"
#define MHD_E_SERVER_INIT "Error: failed to start server\n"
#define MHD_E_FAILED_TO_CONNECT "Error: server connection could not be established\n"
const char *ca_cert_file_name = "ca_cert_pem";
const char *test_file_name = "https_test_file";
const char test_file_data[] = "Hello World\n";
#include "tls_test_common.h"
extern const char srv_key_pem[];
extern const char srv_self_signed_cert_pem[];
static const int TIME_OUT = 3;
char *http_get_req = "GET / HTTP/1.1\r\n\r\n";
/* HTTP access handler call back */
static int
http_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size, void **ptr)
{
return 0;
}
static int
setup (MHD_gtls_session_t * session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert, MHD_gtls_cert_credentials_t * xcred)
setup_timeout_test (MHD_gtls_session_t * session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert,
MHD_gtls_cert_credentials_t * xcred)
{
int ret;
@@ -82,9 +69,10 @@ setup (MHD_gtls_session_t * session,
}
static int
teardown (MHD_gtls_session_t session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert, MHD_gtls_cert_credentials_t xcred)
teardown_timeout_test (MHD_gtls_session_t session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert,
MHD_gtls_cert_credentials_t xcred)
{
MHD_gtls_free_datum_m (key, free);
@@ -105,14 +93,13 @@ test_tls_session_time_out (MHD_gtls_session_t session)
sd = socket (AF_INET, SOCK_STREAM, 0);
if (sd == -1)
{
fprintf(stderr,
"Failed to create socket: %s\n",
strerror(errno));
fprintf (stderr, "Failed to create socket: %s\n", strerror (errno));
return -1;
}
memset (&sa, '\0', sizeof (struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_port = htons (42433);
sa.sin_port = htons (DEAMON_TEST_PORT);
inet_pton (AF_INET, "127.0.0.1", &sa.sin_addr);
MHD__gnutls_transport_set_ptr (session, (MHD_gnutls_transport_ptr_t) sd);
@@ -158,8 +145,8 @@ main (int argc, char *const *argv)
MHD_gtls_global_set_log_level (11);
d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
MHD_USE_DEBUG, 42433,
NULL, NULL, &http_ahc, NULL,
MHD_USE_DEBUG, DEAMON_TEST_PORT,
NULL, NULL, &http_dummy_ahc, NULL,
MHD_OPTION_CONNECTION_TIMEOUT, TIME_OUT,
MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
@@ -171,12 +158,11 @@ main (int argc, char *const *argv)
return -1;
}
setup (&session, &key, &cert, &xcred);
setup_timeout_test (&session, &key, &cert, &xcred);
errorCount += test_tls_session_time_out (session);
teardown (session, &key, &cert, xcred);
teardown_timeout_test (session, &key, &cert, xcred);
if (errorCount != 0)
fprintf (stderr, "Failed test: %s.\n", argv[0]);
print_test_result (errorCount, argv[0]);
MHD_stop_daemon (d);
MHD__gnutls_global_deinit ();
+414
View File
@@ -0,0 +1,414 @@
/*
This file is part of libmicrohttpd
(C) 2007 Christian Grothoff
libmicrohttpd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2, or (at your
option) any later version.
libmicrohttpd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with libmicrohttpd; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file tls_daemon_options_test.c
* @brief Common tls test functions
* @author Sagie Amir
*/
#include "tls_test_common.h"
#include "tls_test_keys.h"
const char test_file_data[] = "Hello World\n";
int curl_check_version (const char *req_version, ...);
void
print_test_result (int test_outcome, char *test_name)
{
if (test_outcome != 0)
fprintf (stderr, "running test: %s [fail]\n", test_name);
else
fprintf (stdout, "running test: %s [pass]\n", test_name);
}
size_t
copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
{
struct CBC *cbc = ctx;
if (cbc->pos + size * nmemb > cbc->size)
return 0; /* overflow */
memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
cbc->pos += size * nmemb;
return size * nmemb;
}
static int
file_reader (void *cls, size_t pos, char *buf, int max)
{
FILE *file = cls;
fseek (file, pos, SEEK_SET);
return fread (buf, 1, max, file);
}
/**
* HTTP access handler call back
*/
int
http_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size, void **ptr)
{
static int aptr;
struct MHD_Response *response;
int ret;
FILE *file;
struct stat buf;
if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
return MHD_NO; /* unexpected method */
if (&aptr != *ptr)
{
/* do never respond on first call */
*ptr = &aptr;
return MHD_YES;
}
*ptr = NULL; /* reset when done */
file = fopen (url, "r");
if (file == NULL)
{
response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND),
(void *) PAGE_NOT_FOUND,
MHD_NO, MHD_NO);
ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response (response);
}
else
{
stat (url, &buf);
response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
&file_reader, file,
(MHD_ContentReaderFreeCallback)
& fclose);
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
}
return ret;
}
/* HTTP access handler call back */
int
http_dummy_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size,
void **ptr)
{
return 0;
}
/**
* send a test http request to the daemon
* @param url
* @param cbc - may be null
* @param cipher_suite
* @param proto_version
* @return
*/
/* TODO have test wrap consider a NULL cbc */
send_curl_req (char *url, struct CBC * cbc, char *cipher_suite,
int proto_version)
{
CURL *c;
CURLcode errornum;
c = curl_easy_init ();
#if DEBUG_HTTPS_TEST
curl_easy_setopt (c, CURLOPT_VERBOSE, CURL_VERBOS_LEVEL);
#endif
curl_easy_setopt (c, CURLOPT_URL, url);
curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_easy_setopt (c, CURLOPT_TIMEOUT, 60L);
curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 60L);
if (cbc != NULL)
{
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
curl_easy_setopt (c, CURLOPT_FILE, cbc);
}
/* TLS options */
curl_easy_setopt (c, CURLOPT_SSLVERSION, proto_version);
curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
/* currently skip any peer authentication */
curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
/* NOTE: use of CONNECTTIMEOUT without also
setting NOSIGNAL results in really weird
crashes on my system! */
curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
if (CURLE_OK != (errornum = curl_easy_perform (c)))
{
fprintf (stderr, "curl_easy_perform failed: `%s'\n",
curl_easy_strerror (errornum));
curl_easy_cleanup (c);
return errornum;
}
curl_easy_cleanup (c);
return CURLE_OK;
}
/**
* compile test file url pointing to the current running directory path
* @param url - char buffer into which the url is compiled
* @return
*/
int
gen_test_file_url (char *url, int port)
{
int ret = 0;
char *doc_path;
size_t doc_path_len;
/* setup test file path, url */
doc_path_len = PATH_MAX > 4096 ? 4096 : PATH_MAX;
if (NULL == (doc_path = malloc (doc_path_len)))
{
fprintf (stderr, MHD_E_MEM);
ret = -1;
}
if (getcwd (doc_path, doc_path_len) == NULL)
{
fprintf (stderr, "Error: failed to get working directory. %s\n",
strerror (errno));
ret = -1;
}
/* construct url - this might use doc_path */
if (sprintf (url, "%s:%d%s/%s", "https://localhost", port,
doc_path, TEST_FILE_NAME) < 0)
ret = -1;
free (doc_path);
return ret;
}
/**
* test HTTPS file transfer
* @param test_fd: file to attempt transferring
*/
int
test_https_transfer (FILE * test_fd, char *cipher_suite, int proto_version)
{
int len, ret = 0;
struct CBC cbc;
char url[255];
struct stat statb;
/* used to memcmp local copy & deamon supplied copy */
unsigned char *mem_test_file_local;
stat (TEST_FILE_NAME, &statb);
len = statb.st_size;
if (NULL == (mem_test_file_local = malloc (len)))
{
fprintf (stderr, MHD_E_MEM);
ret = -1;
goto cleanup;
}
fseek (test_fd, 0, SEEK_SET);
if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len)
{
fprintf (stderr, "Error: failed to read test file. %s\n",
strerror (errno));
ret = -1;
goto cleanup;
}
if (NULL == (cbc.buf = malloc (sizeof (char) * len)))
{
fprintf (stderr, MHD_E_MEM);
ret = -1;
goto cleanup;
}
cbc.size = len;
cbc.pos = 0;
if (gen_test_file_url (url, DEAMON_TEST_PORT))
{
ret = -1;
goto cleanup;
}
if (CURLE_OK != send_curl_req (url, &cbc, cipher_suite, proto_version))
{
ret = -1;
goto cleanup;
}
/* compare test file & daemon responce */
if (memcmp (cbc.buf, mem_test_file_local, len) != 0)
{
fprintf (stderr, "Error: local file & received file differ.\n");
ret = -1;
}
cleanup:
free (mem_test_file_local);
free (cbc.buf);
return ret;
}
/**
* setup a mock test file which is requested from the running daemon
* @return open file descriptor to the test file
*/
FILE *
setup_test_file ()
{
FILE *test_fd;
if (NULL == (test_fd = fopen (TEST_FILE_NAME, "w+")))
{
fprintf (stderr, "Error: failed to open `%s': %s\n",
TEST_FILE_NAME, strerror (errno));
return NULL;
}
if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd)
!= strlen (test_file_data))
{
fprintf (stderr, "Error: failed to write `%s. %s'\n",
TEST_FILE_NAME, strerror (errno));
fclose (test_fd);
return NULL;
}
if (fflush (test_fd))
{
fprintf (stderr, "Error: failed to flush test file stream. %s\n",
strerror (errno));
fclose (test_fd);
return NULL;
}
return test_fd;
}
/**
* setup test case
*
* @param d
* @param daemon_flags
* @param arg_list
* @return
*/
int
setup_testcase (struct MHD_Daemon **d, int daemon_flags, va_list arg_list)
{
*d = MHD_start_daemon_va (daemon_flags, DEAMON_TEST_PORT,
NULL, NULL, &http_ahc, NULL, arg_list);
if (*d == NULL)
{
fprintf (stderr, MHD_E_SERVER_INIT);
return -1;
}
return 0;
}
void
teardown_testcase (struct MHD_Daemon *d)
{
MHD_stop_daemon (d);
}
int
setup_session (MHD_gtls_session_t * session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert, MHD_gtls_cert_credentials_t * xcred)
{
int ret;
const char **err_pos;
MHD__gnutls_certificate_allocate_credentials (xcred);
MHD_gtls_set_datum_m (key, srv_key_pem, strlen (srv_key_pem), &malloc);
MHD_gtls_set_datum_m (cert, srv_self_signed_cert_pem,
strlen (srv_self_signed_cert_pem), &malloc);
MHD__gnutls_certificate_set_x509_key_mem (*xcred, cert, key,
GNUTLS_X509_FMT_PEM);
MHD__gnutls_init (session, GNUTLS_CLIENT);
ret = MHD__gnutls_priority_set_direct (*session, "NORMAL", err_pos);
if (ret < 0)
{
return -1;
}
MHD__gnutls_credentials_set (*session, MHD_GNUTLS_CRD_CERTIFICATE, xcred);
return 0;
}
int
teardown_session (MHD_gtls_session_t session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert,
MHD_gtls_cert_credentials_t xcred)
{
MHD_gtls_free_datum_m (key, free);
MHD_gtls_free_datum_m (cert, free);
MHD__gnutls_deinit (session);
MHD__gnutls_certificate_free_credentials (xcred);
return 0;
}
/* TODO test_wrap: change sig to (setup_func, test, va_list test_arg) */
int
test_wrap (char *test_name, int
(*test_function) (FILE * test_fd, char *cipher_suite,
int proto_version), FILE * test_fd,
int daemon_flags, char *cipher_suite, int proto_version, ...)
{
int ret;
va_list arg_list;
struct MHD_Daemon *d;
va_start (arg_list, proto_version);
if (setup_testcase (&d, daemon_flags, arg_list) != 0)
{
va_end (arg_list);
return -1;
}
fprintf (stdout, "running test: %s ", test_name);
ret = test_function (test_fd, cipher_suite, proto_version);
if (ret == 0)
{
fprintf (stdout, "[pass]\n");
}
else
{
fprintf (stdout, "[fail]\n");
}
teardown_testcase (d);
va_end (arg_list);
return ret;
}
+118
View File
@@ -0,0 +1,118 @@
/*
This file is part of libmicrohttpd
(C) 2007 Christian Grothoff
libmicrohttpd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2, or (at your
option) any later version.
libmicrohttpd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with libmicrohttpd; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#ifndef TLS_TEST_COMMON_H_
#define TLS_TEST_COMMON_H_
#include "platform.h"
#include "microhttpd.h"
#include <curl/curl.h>
#include <sys/stat.h>
#include <limits.h>
#include "gnutls.h"
/* this enables verbos CURL version checking */
#define DEBUG_HTTPS_TEST 1
#define CURL_VERBOS_LEVEL 1
#define DEAMON_TEST_PORT 42433
#define TEST_FILE_NAME "https_test_file"
#define EMPTY_PAGE "<html><head><title>Empty page</title></head><body>Empty page</body></html>"
#define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
#define MHD_E_MEM "Error: memory error\n"
#define MHD_E_SERVER_INIT "Error: failed to start server\n"
#define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n"
#define MHD_E_CERT_FILE_CREAT "Error: failed to setup test certificate\n"
#define MHD_E_KEY_FILE_CREAT "Error: failed to setup test certificate\n"
#define MHD_E_FAILED_TO_CONNECT "Error: server connection could not be established\n"
/* TODO rm if unused */
struct https_test_data
{
FILE *test_fd;
char *cipher_suite;
int proto_version;
};
struct CBC
{
char *buf;
size_t pos;
size_t size;
};
struct CipherDef
{
int options[2];
char *curlname;
};
void print_test_result (int test_outcome, char *test_name);
size_t copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx);
int
http_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size, void **ptr);
int
http_dummy_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size,
void **ptr);
int gen_test_file_url (char *url, int port);
int
send_curl_req (char *url, struct CBC *cbc, char *cipher_suite,
int proto_version);
int
test_https_transfer (FILE * test_fd, char *cipher_suite, int proto_version);
FILE *setup_test_file ();
int
setup_testcase (struct MHD_Daemon **d, int daemon_flags, va_list arg_list);
void teardown_testcase (struct MHD_Daemon *d);
int
setup_session (MHD_gtls_session_t * session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert,
MHD_gtls_cert_credentials_t * xcred);
int
teardown_session (MHD_gtls_session_t session,
MHD_gnutls_datum_t * key,
MHD_gnutls_datum_t * cert,
MHD_gtls_cert_credentials_t xcred);
int
test_wrap (char *test_name, int
(*test_function) (FILE * test_fd, char *cipher_suite,
int proto_version), FILE * test_fd,
int daemon_flags, char *cipher_suite, int proto_version, ...);
#endif /* TLS_TEST_COMMON_H_ */
+8 -318
View File
@@ -35,230 +35,12 @@
#include "gnutls.h"
#include <curl/curl.h>
#define DEBUG_CURL_VERBOSE 0
#define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
#define MHD_E_MEM "Error: memory error\n"
#define MHD_E_SERVER_INIT "Error: failed to start server\n"
#define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n"
#define MHD_E_CERT_FILE_CREAT "Error: failed to setup test certificate\n"
#define MHD_E_KEY_FILE_CREAT "Error: failed to setup test certificate\n"
#include "tls_test_keys.h"
const char *test_file_name = "https_test_file";
const char test_file_data[] = "Hello World\n";
#include "tls_test_common.h"
extern const char srv_key_pem[];
extern const char srv_self_signed_cert_pem[];
int curl_check_version (const char *req_version, ...);
struct CBC
{
char *buf;
size_t pos;
size_t size;
};
struct https_test_data
{
FILE *test_fd;
char *cipher_suite;
int proto_version;
};
struct CipherDef
{
int options[2];
char *curlname;
};
static size_t
copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
{
struct CBC *cbc = ctx;
if (cbc->pos + size * nmemb > cbc->size)
return 0; /* overflow */
memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
cbc->pos += size * nmemb;
return size * nmemb;
}
static int
file_reader (void *cls, size_t pos, char *buf, int max)
{
FILE *file = cls;
fseek (file, pos, SEEK_SET);
return fread (buf, 1, max, file);
}
/* HTTP access handler call back */
static int
http_ahc (void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *upload_data,
const char *version, unsigned int *upload_data_size, void **ptr)
{
static int aptr;
struct MHD_Response *response;
int ret;
FILE *file;
struct stat buf;
if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
return MHD_NO; /* unexpected method */
if (&aptr != *ptr)
{
/* do never respond on first call */
*ptr = &aptr;
return MHD_YES;
}
*ptr = NULL; /* reset when done */
file = fopen (url, "r");
if (file == NULL)
{
response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND),
(void *) PAGE_NOT_FOUND,
MHD_NO, MHD_NO);
ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response (response);
}
else
{
stat (url, &buf);
response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
&file_reader, file,
(MHD_ContentReaderFreeCallback)
& fclose);
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
}
return ret;
}
/**
* test HTTPS transfer
* @param test_fd: file to attempt transfering
*/
static int
test_https_transfer (FILE * test_fd, char *cipher_suite, int proto_version)
{
CURL *c;
CURLcode errornum;
struct CBC cbc;
char *doc_path;
size_t doc_path_len;
char url[255];
struct stat statb;
stat (test_file_name, &statb);
int len = statb.st_size;
/* used to memcmp local copy & deamon supplied copy */
unsigned char *mem_test_file_local;
/* setup test file path, url */
doc_path_len = PATH_MAX > 4096 ? 4096 : PATH_MAX;
if (NULL == (doc_path = malloc (doc_path_len)))
{
fprintf (stderr, MHD_E_MEM);
return -1;
}
if (getcwd (doc_path, doc_path_len) == NULL)
{
fprintf (stderr, "Error: failed to get working directory. %s\n",
strerror (errno));
free (doc_path);
return -1;
}
if (NULL == (mem_test_file_local = malloc (len)))
{
fprintf (stderr, MHD_E_MEM);
free (doc_path);
return -1;
}
fseek (test_fd, 0, SEEK_SET);
if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len)
{
fprintf (stderr, "Error: failed to read test file. %s\n",
strerror (errno));
fclose (test_fd);
free (doc_path);
free (mem_test_file_local);
return -1;
}
if (NULL == (cbc.buf = malloc (len)))
{
free (doc_path);
free (mem_test_file_local);
fprintf (stderr, MHD_E_MEM);
return -1;
}
cbc.size = len;
cbc.pos = 0;
/* construct url - this might use doc_path */
sprintf (url, "%s%s/%s", "https://localhost:42433",
doc_path, test_file_name);
c = curl_easy_init ();
#if DEBUG_CURL_VERBOSE
curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
#endif
curl_easy_setopt (c, CURLOPT_URL, url);
curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_easy_setopt (c, CURLOPT_TIMEOUT, 60L);
curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 60L);
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
curl_easy_setopt (c, CURLOPT_FILE, &cbc);
/* TLS options */
curl_easy_setopt (c, CURLOPT_SSLVERSION, proto_version);
curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
/* currently skip any peer authentication */
curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
/* NOTE: use of CONNECTTIMEOUT without also
setting NOSIGNAL results in really weird
crashes on my system! */
curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
if (CURLE_OK != (errornum = curl_easy_perform (c)))
{
fprintf (stderr, "curl_easy_perform failed: `%s'\n",
curl_easy_strerror (errornum));
curl_easy_cleanup (c);
free (cbc.buf);
free (mem_test_file_local);
free (doc_path);
return errornum;
}
curl_easy_cleanup (c);
if (memcmp (cbc.buf, mem_test_file_local, len) != 0)
{
fprintf (stderr, "Error: local file & received file differ.\n");
free (cbc.buf);
free (mem_test_file_local);
free (doc_path);
return -1;
}
free (mem_test_file_local);
free (cbc.buf);
free (doc_path);
return 0;
}
/**
* used when spawning multiple threads executing curl server requests
*
@@ -279,92 +61,6 @@ https_transfer_thread_adapter (void *args)
return &nonnull;
}
static FILE *
setupTestFile ()
{
FILE *test_fd;
if (NULL == (test_fd = fopen (test_file_name, "w+")))
{
fprintf (stderr, "Error: failed to open `%s': %s\n",
test_file_name, strerror (errno));
return NULL;
}
if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd)
!= strlen (test_file_data))
{
fprintf (stderr, "Error: failed to write `%s. %s'\n",
test_file_name, strerror (errno));
fclose (test_fd);
return NULL;
}
if (fflush (test_fd))
{
fprintf (stderr, "Error: failed to flush test file stream. %s\n",
strerror (errno));
fclose (test_fd);
return NULL;
}
return test_fd;
}
static int
setup (struct MHD_Daemon **d, int daemon_flags, va_list arg_list)
{
*d = MHD_start_daemon_va (daemon_flags, 42433,
NULL, NULL, &http_ahc, NULL, arg_list);
if (*d == NULL)
{
fprintf (stderr, MHD_E_SERVER_INIT);
return -1;
}
return 0;
}
static void
teardown (struct MHD_Daemon *d)
{
MHD_stop_daemon (d);
}
/* TODO test_wrap: change sig to (setup_func, test, va_list test_arg) & move to test_util.c */
static int
test_wrap (char *test_name, int
(*test_function) (FILE * test_fd, char *cipher_suite,
int proto_version), FILE * test_fd,
int daemon_flags, char *cipher_suite, int proto_version, ...)
{
int ret;
va_list arg_list;
struct MHD_Daemon *d;
va_start (arg_list, proto_version);
if (setup (&d, daemon_flags, arg_list) != 0)
{
va_end (arg_list);
return -1;
}
fprintf (stdout, "running test: %s ", test_name);
ret = test_function (test_fd, cipher_suite, proto_version);
if (ret == 0)
{
fprintf (stdout, "[pass]\n");
}
else
{
fprintf (stdout, "[fail]\n");
}
teardown (d);
va_end (arg_list);
return ret;
}
/**
* Test non-parallel requests.
*
@@ -386,7 +82,6 @@ test_single_client (FILE * test_fd, char *cipher_suite,
return 0;
}
/**
* Test parallel request handling.
*
@@ -408,11 +103,9 @@ test_parallel_clients (FILE * test_fd, char *cipher_suite,
for (i = 0; i < client_count; ++i)
{
if (pthread_create (&client_arr[i], NULL,
&https_transfer_thread_adapter,
&client_args) != 0)
&https_transfer_thread_adapter, &client_args) != 0)
{
fprintf (stderr, "Error: failed to spawn test client threads.\n");
return -1;
}
}
@@ -428,7 +121,6 @@ test_parallel_clients (FILE * test_fd, char *cipher_suite,
return 0;
}
int
main (int argc, char *const *argv)
{
@@ -442,7 +134,7 @@ main (int argc, char *const *argv)
if (curl_check_version (MHD_REQ_CURL_VERSION))
return -1;
if ((test_fd = setupTestFile ()) == NULL)
if ((test_fd = setup_test_file ()) == NULL)
{
fprintf (stderr, MHD_E_TEST_FILE_CREAT);
return -1;
@@ -452,6 +144,7 @@ main (int argc, char *const *argv)
{
fprintf (stderr, "Error: %s\n", strerror (errno));
fclose (test_fd);
remove (TEST_FILE_NAME);
return -1;
}
@@ -462,6 +155,7 @@ main (int argc, char *const *argv)
"AES256-SHA", CURL_SSLVERSION_TLSv1, MHD_OPTION_HTTPS_MEM_KEY,
srv_key_pem, MHD_OPTION_HTTPS_MEM_CERT,
srv_self_signed_cert_pem, MHD_OPTION_END);
errorCount +=
test_wrap ("single threaded daemon, parallel clients",
&test_parallel_clients, test_fd,
@@ -470,13 +164,9 @@ main (int argc, char *const *argv)
srv_key_pem, MHD_OPTION_HTTPS_MEM_CERT,
srv_self_signed_cert_pem, MHD_OPTION_END);
if (errorCount != 0)
fprintf (stderr, "Failed test: %s.\n", argv[0]);
curl_global_cleanup ();
fclose (test_fd);
remove (test_file_name);
remove (TEST_FILE_NAME);
return errorCount != 0;
}