libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 79afda018af6d860548eda29241ac8cbe43a3b58
parent ad200289e96622c68ca345397603b68113d8c39f
Author: lv-426 <oxcafebaby@yahoo.com>
Date:   Tue,  8 Jul 2008 20:51:06 +0000

tests fix

Diffstat:
Msrc/daemon/https/tls/Makefile.am | 1+
Msrc/daemon/https/tls/gnutls_global.c | 6+++++-
Msrc/daemon/https/tls/gnutls_global.h | 2+-
Msrc/examples/https_server_example.c | 89++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
Msrc/testcurl/https/Makefile.am | 3++-
Msrc/testcurl/https/mhds_get_test.c | 24++++++++++++------------
Msrc/testcurl/https/mhds_multi_daemon_test.c | 31+++++++++++++------------------
Msrc/testcurl/https/mhds_session_info_test.c | 13++++++++-----
8 files changed, 94 insertions(+), 75 deletions(-)

diff --git a/src/daemon/https/tls/Makefile.am b/src/daemon/https/tls/Makefile.am @@ -2,6 +2,7 @@ SUBDIRS = . AM_CPPFLAGS = \ -I$(top_srcdir)/src/include \ +-I$(top_srcdir)/src/daemon/ \ -I$(top_srcdir)/src/daemon/https/tls \ -I$(top_srcdir)/src/daemon/https/lgl \ -I$(top_srcdir)/src/daemon/https/x509 \ diff --git a/src/daemon/https/tls/gnutls_global.c b/src/daemon/https/tls/gnutls_global.c @@ -30,6 +30,10 @@ /* this is used in order to make the multi-threaded initialization call to libgcrypt */ #include <pthread.h> #include <gcrypt.h> + +/* used to set the MHD_tls logging function */ +#include "internal.h" + /* TODO fix : needed by GCRY_THREAD_OPTION_PTHREAD_IMPL but missing otherwise */ #define ENOMEM 12 /* Out of memory */ @@ -275,7 +279,7 @@ gnutls_global_init (void) } #ifdef DEBUG - gnutls_global_set_log_function (MHD_tls_log_func()); + gnutls_global_set_log_function(MHD_tls_log_func); #endif /* initialize parser diff --git a/src/daemon/https/tls/gnutls_global.h b/src/daemon/https/tls/gnutls_global.h @@ -23,7 +23,7 @@ */ #ifndef GNUTLS_GLOBAL_H -# define GNUTLS_GLOBAL_H +#define GNUTLS_GLOBAL_H #include <libtasn1.h> diff --git a/src/examples/https_server_example.c b/src/examples/https_server_example.c @@ -19,17 +19,17 @@ /** * @file https_server_example.c * @brief a simple https file server using TLS. - * + * * This example assumes the existence of a private key file named "key.pem" * and a server certificate file named "cert.pem". File path for these should be * provided as command-line arguments. 'certtool' may be used to generate these if - * missing. - * - * Access server with your browser of choice or with curl : - * + * missing. + * + * Access server with your browser of choice or with curl : + * * curl --insecure --tlsv1 --ciphers AES256-SHA <url> - * - * @author LV-426 + * + * @author Sagie Amir */ #include "config.h" @@ -52,39 +52,54 @@ #define CAFILE "ca.pem" #define CRLFILE "crl.pem" -#define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>" +#define EMPTY_PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" /* Test Certificate */ const char cert_pem[] = "-----BEGIN CERTIFICATE-----\n" - "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n" - "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n" - "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n" - "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n" - "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n" - "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n" - "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n" - "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoA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LI2VPO2bNe\n" + "JDMXWuP+8kMC7gEvUnJ7A92Y2yrhu3QI3bjPk8uSpHea19Q77tul1UVBJ5g+zpH3\n" + "OsF5p0MyaVf09GTzcLds5nE/osTdXGUyHJapWReVmPm3Zn6gqYlnzD99z+DPIgIV\n" + "RhZvQx74NQnS6g==\n" "-----END CERTIFICATE-----\n"; const char key_pem[] = "-----BEGIN RSA PRIVATE KEY-----\n" - "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n" - "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n" - "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n" - "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n" - "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n" - "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n" - "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n" - "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n" - "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n" - "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n" - "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n" - "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n" - "sWZGfbnU3ryjvk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1Mlq30N0e3Q61YK9LxMdU1vsluXxNq2rfK1Scb1oOlOOtlbV3zA3VRF\n" + "hV3t1nOA9tFmUrwZi0CUMWJE/zbPAyhwWotKyZkCgYEAh0kFicPdbABdrCglXVae\n" + "SnfSjVwYkVuGd5Ze0WADvjYsVkYBHTvhgRNnRJMg+/vWz3Sf4Ps4rgUbqK8Vc20b\n" + "AU5G6H6tlCvPRGm0ZxrwTWDHTcuKRVs+pJE8C/qWoklE/AAhjluWVoGwUMbPGuiH\n" + "6Gf1bgHF6oj/Sq7rv/VLZ8ECgYBeq7ml05YyLuJutuwa4yzQ/MXfghzv4aVyb0F3\n" + "QCdXR6o2IYgR6jnSewrZKlA9aPqFJrwHNR6sNXlnSmt5Fcf/RWO/qgJQGLUv3+rG\n" + "7kuLTNDR05azSdiZc7J89ID3Bkb+z2YkV+6JUiPq/Ei1+nDBEXb/m+/HqALU/nyj\n" + "P3gXeQKBgBusb8Rbd+KgxSA0hwY6aoRTPRt8LNvXdsB9vRcKKHUFQvxUWiUSS+L9\n" + "/Qu1sJbrUquKOHqksV5wCnWnAKyJNJlhHuBToqQTgKXjuNmVdYSe631saiI7PHyC\n" + "eRJ6DxULPxABytJrYCRrNqmXi5TCiqR2mtfalEMOPxz8rUU8dYyx\n" "-----END RSA PRIVATE KEY-----\n"; static int @@ -124,8 +139,8 @@ http_ahc (void *cls, file = fopen (url, "r"); if (file == NULL) { - response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND), - (void *) PAGE_NOT_FOUND, + response = MHD_create_response_from_data (strlen (EMPTY_PAGE), + (void *) EMPTY_PAGE, MHD_NO, MHD_NO); ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); MHD_destroy_response (response); @@ -165,8 +180,8 @@ main (int argc, char *const *argv) NULL, NULL, &http_ahc, NULL, MHD_OPTION_CONNECTION_TIMEOUT, 256, - MHD_OPTION_HTTPS_KEY_PATH, argv[4], - MHD_OPTION_HTTPS_CERT_PATH, argv[5], + MHD_OPTION_HTTPS_MEM_KEY, key_pem, + MHD_OPTION_HTTPS_MEM_CERT, cert_pem, MHD_OPTION_END); if (TLS_daemon == NULL) diff --git a/src/testcurl/https/Makefile.am b/src/testcurl/https/Makefile.am @@ -8,7 +8,8 @@ AM_CPPFLAGS = \ check_PROGRAMS = \ mhds_get_test \ - mhds_session_info_test + mhds_session_info_test \ + mhds_multi_daemon_test TESTS = $(check_PROGRAMS) diff --git a/src/testcurl/https/mhds_get_test.c b/src/testcurl/https/mhds_get_test.c @@ -20,7 +20,7 @@ /** * @file mhds_get_test.c - * @brief Testcase for libmicrohttpd GET operations + * @brief Testcase for libmicrohttpd HTTPS GET operations * @author Sagie Amir */ @@ -38,9 +38,6 @@ #include <unistd.h> #include <sys/stat.h> -#define BUF_SIZE 1024 -#define MAX_URL_LEN 255 - #define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>" #define MHD_E_SERVER_INIT "Error: failed to start server\n" @@ -97,7 +94,6 @@ const char key_pem[] = "-----END RSA PRIVATE KEY-----\n"; const char *test_file_name = "https_test_file"; - const char test_file_data[] = "Hello World\n"; struct CBC @@ -161,7 +157,7 @@ http_ahc (void *cls, struct MHD_Connection *connection, } else { - stat (&url[1], &buf); + stat (url, &buf); response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */ &file_reader, file, (MHD_ContentReaderFreeCallback) @@ -184,8 +180,11 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version) CURLcode errornum; char *doc_path; char url[255]; - size_t len = fseek (test_fd, 0, SEEK_END); + 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; @@ -194,6 +193,7 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version) doc_path = get_current_dir_name (); mem_test_file_local = malloc (len); + fseek (test_fd, 0, SEEK_SET); if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len) { @@ -218,7 +218,7 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version) doc_path, test_file_name); c = curl_easy_init (); - // curl_easy_setopt (c, CURLOPT_VERBOSE, 1); + /* curl_easy_setopt (c, CURLOPT_VERBOSE, 1); */ 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, 10L); @@ -236,9 +236,9 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version) curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); - // NOTE: use of CONNECTTIMEOUT without also - // setting NOSIGNAL results in really weird - // crashes on my system! + /* 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))) { @@ -283,7 +283,7 @@ test_secure_get (FILE * test_fd, char *cipher_suite, int proto_version) return ret; } -/* test server works with key & certificate files */ +/* test loading of key & certificate files */ int test_file_certificates (FILE * test_fd, char *cipher_suite, int proto_version) { diff --git a/src/testcurl/https/mhds_multi_daemon_test.c b/src/testcurl/https/mhds_multi_daemon_test.c @@ -20,7 +20,7 @@ /** * @file mhds_multi_daemon_test.c - * @brief Testcase for libmicrohttpd GET operations + * @brief Testcase for libmicrohttpd multiple HTTPS daemon scenario * @author Sagie Amir */ @@ -38,15 +38,10 @@ #include <unistd.h> #include <sys/stat.h> -#define BUF_SIZE 1024 -#define MAX_URL_LEN 255 - #define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>" #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" /* Test Certificate */ const char cert_pem[] = @@ -97,7 +92,6 @@ const char key_pem[] = "-----END RSA PRIVATE KEY-----\n"; const char *test_file_name = "https_test_file"; - const char test_file_data[] = "Hello World\n"; struct CBC @@ -139,7 +133,7 @@ http_ahc (void *cls, struct MHD_Connection *connection, FILE *file; struct stat buf; - // TODO never respond on first call + /* TODO never respond on first call */ if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) return MHD_NO; /* unexpected method */ if (&aptr != *ptr) @@ -161,7 +155,7 @@ http_ahc (void *cls, struct MHD_Connection *connection, } else { - stat (&url[1], &buf); + stat (url, &buf); response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */ &file_reader, file, (MHD_ContentReaderFreeCallback) @@ -173,8 +167,8 @@ http_ahc (void *cls, struct MHD_Connection *connection, } /* - * test HTTPS transfer - * @param test_fd: file to attempt transfering + * perform cURL request for file + * @param test_fd: file to attempt transferring */ static int test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version, @@ -185,8 +179,11 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version, CURLcode errornum; char *doc_path; char url[255]; - size_t len = fseek (test_fd, 0, SEEK_END); + size_t len; + struct stat 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; @@ -214,12 +211,12 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version, cbc.size = len; cbc.pos = 0; - /* construct url - this might use doc_path */ + /* construct url */ sprintf (url, "%s:%d%s/%s", "https://localhost", port, doc_path, test_file_name); c = curl_easy_init (); - curl_easy_setopt (c, CURLOPT_VERBOSE, 1); + /* curl_easy_setopt (c, CURLOPT_VERBOSE, 1); */ 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, 10L); @@ -251,6 +248,7 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version, curl_easy_cleanup (c); + /* compare received file and local reference */ if (memcmp (cbc.buf, mem_test_file_local, len) != 0) { fprintf (stderr, "Error: local file & received file differ.\n"); @@ -296,7 +294,7 @@ test_concurent_daemon_pair (FILE * test_fd, char *cipher_suite, return -1; } - ret += test_daemon_get (test_fd, cipher_suite, proto_version, 42433); + ret = test_daemon_get (test_fd, cipher_suite, proto_version, 42433); ret += test_daemon_get (test_fd, cipher_suite, proto_version, 42434); MHD_stop_daemon (d2); @@ -339,8 +337,6 @@ main (int argc, char *const *argv) FILE *test_fd; unsigned int errorCount = 0; - gnutls_global_set_log_level (0); - if ((test_fd = setupTestFile ()) == NULL) { fprintf (stderr, MHD_E_TEST_FILE_CREAT); @@ -363,6 +359,5 @@ main (int argc, char *const *argv) fclose (test_fd); remove (test_file_name); - return errorCount != 0; } diff --git a/src/testcurl/https/mhds_session_info_test.c b/src/testcurl/https/mhds_session_info_test.c @@ -20,7 +20,7 @@ /** * @file mhds_session_info_test.c - * @brief Testcase for libmicrohttpd GET operations + * @brief Testcase for libmicrohttpd HTTPS connection querying operations * @author Sagie Amir */ @@ -36,9 +36,6 @@ #include <fcntl.h> #include <unistd.h> -#define BUF_SIZE 1024 -#define MAX_URL_LEN 255 - #define EMPTY_PAGE "<html><head><title>Empty page</title></head><body>Empty page</body></html>" /* Test Certificate */ @@ -110,7 +107,10 @@ copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) return size * nmemb; } -/* HTTP access handler call back */ +/* + * HTTP access handler call back + * used to query negotiated security parameters + */ static int query_session_ahc (void *cls, struct MHD_Connection *connection, const char *url, const char *method, @@ -156,6 +156,9 @@ query_session_ahc (void *cls, struct MHD_Connection *connection, return ret; } +/* + * negotiate a secure connection with server & query negotiated security parameters + */ static int test_query_session () {