diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/mini-x509-rehandshake.c | 172 | ||||
-rw-r--r-- | tests/mini-x509.c | 129 |
2 files changed, 21 insertions, 280 deletions
diff --git a/tests/mini-x509-rehandshake.c b/tests/mini-x509-rehandshake.c index 4004a77952..52cc4c0c75 100644 --- a/tests/mini-x509-rehandshake.c +++ b/tests/mini-x509-rehandshake.c @@ -29,6 +29,8 @@ #include <string.h> #include <errno.h> #include <gnutls/gnutls.h> +#include "utils.h" +#include "eagain-common.h" static void tls_log_func (int level, const char *str) @@ -36,80 +38,6 @@ tls_log_func (int level, const char *str) fprintf (stderr, "|<%d>| %s", level, str); } -static char *to_server; -static size_t to_server_len; - -static char *to_client; -static size_t to_client_len; - -static ssize_t -client_pull (gnutls_transport_ptr_t tr, void *data, size_t len) -{ - if (to_client_len < len) - { - gnutls_transport_set_global_errno (EAGAIN); - return -1; - } - - memcpy (data, to_client, len); - - memmove (to_client, to_client + len, to_client_len - len); - to_client_len -= len; - - return len; -} - -static ssize_t -client_push (gnutls_transport_ptr_t tr, const void *data, size_t len) -{ - size_t newlen = to_server_len + len; - char *tmp; - - tmp = realloc (to_server, newlen); - if (!tmp) - abort (); - to_server = tmp; - - memcpy (to_server + to_server_len, data, len); - to_server_len = newlen; - - return len; -} - -static ssize_t -server_pull (gnutls_transport_ptr_t tr, void *data, size_t len) -{ - if (to_server_len < len) - { - gnutls_transport_set_global_errno (EAGAIN); - return -1; - } - - memcpy (data, to_server, len); - - memmove (to_server, to_server + len, to_server_len - len); - to_server_len -= len; - - return len; -} - -static ssize_t -server_push (gnutls_transport_ptr_t tr, const void *data, size_t len) -{ - size_t newlen = to_client_len + len; - char *tmp; - - tmp = realloc (to_client, newlen); - if (!tmp) - abort (); - to_client = tmp; - - memcpy (to_client + to_client_len, data, len); - to_client_len = newlen; - - return len; -} - static unsigned char server_cert_pem[] = "-----BEGIN CERTIFICATE-----\n" "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n" @@ -151,10 +79,9 @@ const gnutls_datum_t server_key = { server_key_pem, sizeof (server_key_pem) }; -int -main (int argc, char *argv[]) +void +doit (void) { - int debug_level = argc - 1; int exit_code = EXIT_SUCCESS; /* Server stuff. */ gnutls_certificate_credentials_t serverx509cred; @@ -168,7 +95,8 @@ main (int argc, char *argv[]) /* General init. */ gnutls_global_init (); gnutls_global_set_log_function (tls_log_func); - gnutls_global_set_log_level (debug_level); + if (debug) + gnutls_global_set_log_level (2); /* Init server */ gnutls_certificate_allocate_credentials (&serverx509cred); @@ -180,6 +108,7 @@ main (int argc, char *argv[]) gnutls_priority_set_direct (server, "NORMAL", NULL); gnutls_transport_set_push_function (server, server_push); gnutls_transport_set_pull_function (server, server_pull); + gnutls_transport_set_ptr (server, (gnutls_transport_ptr_t)server); /* Init client */ gnutls_certificate_allocate_credentials (&clientx509cred); @@ -188,46 +117,12 @@ main (int argc, char *argv[]) gnutls_priority_set_direct (client, "NORMAL", NULL); gnutls_transport_set_push_function (client, client_push); gnutls_transport_set_pull_function (client, client_pull); + gnutls_transport_set_ptr (client, (gnutls_transport_ptr_t)client); - do - { - static int max_iter = 0; - if (max_iter++ > 10) - abort (); - - if (cret == GNUTLS_E_AGAIN) - { - cret = gnutls_handshake (client); - if (debug_level > 0) - { - tls_log_func (0, "gnutls_handshake (client)...\n"); - tls_log_func (0, gnutls_strerror (cret)); - tls_log_func (0, "\n"); - } - } - - if (sret == GNUTLS_E_AGAIN) - { - sret = gnutls_handshake (server); - if (debug_level > 0) - { - tls_log_func (0, "gnutls_handshake (server)...\n"); - tls_log_func (0, gnutls_strerror (sret)); - tls_log_func (0, "\n"); - } - } - } - while ( - /* Not done: */ - !(cret == GNUTLS_E_SUCCESS && sret == GNUTLS_E_SUCCESS) - /* No error: */ - && (cret == GNUTLS_E_AGAIN || sret == GNUTLS_E_AGAIN)); - - if (cret != GNUTLS_E_SUCCESS && sret != GNUTLS_E_SUCCESS) - exit_code = EXIT_FAILURE; + HANDSHAKE(client, server); sret = gnutls_rehandshake (server); - if (debug_level > 0) + if (debug) { tls_log_func (0, "gnutls_rehandshake (server)...\n"); tls_log_func (0, gnutls_strerror (sret)); @@ -242,45 +137,7 @@ main (int argc, char *argv[]) abort (); } - cret = GNUTLS_E_AGAIN; - sret = GNUTLS_E_AGAIN; - - do - { - static int max_iter = 0; - if (max_iter++ > 10) - abort (); - - if (cret == GNUTLS_E_AGAIN) - { - cret = gnutls_handshake (client); - if (debug_level > 0) - { - tls_log_func (0, "second gnutls_handshake (client)...\n"); - tls_log_func (0, gnutls_strerror (cret)); - tls_log_func (0, "\n"); - } - } - - if (sret == GNUTLS_E_AGAIN) - { - sret = gnutls_handshake (server); - if (debug_level > 0) - { - tls_log_func (0, "second gnutls_handshake (server)...\n"); - tls_log_func (0, gnutls_strerror (sret)); - tls_log_func (0, "\n"); - } - } - } - while ( - /* Not done: */ - !(cret == GNUTLS_E_SUCCESS && sret == GNUTLS_E_SUCCESS) - /* No error: */ - && (cret == GNUTLS_E_AGAIN || sret == GNUTLS_E_AGAIN)); - - if (cret != GNUTLS_E_SUCCESS && sret != GNUTLS_E_SUCCESS) - exit_code = 1; + HANDSHAKE(client, server); gnutls_bye (client, GNUTLS_SHUT_RDWR); gnutls_bye (server, GNUTLS_SHUT_RDWR); @@ -288,20 +145,15 @@ main (int argc, char *argv[]) gnutls_deinit (client); gnutls_deinit (server); - free (to_server); - free (to_client); - gnutls_certificate_free_credentials (serverx509cred); gnutls_global_deinit (); - if (debug_level > 0) + if (debug) { if (exit_code == 0) puts ("Self-test successful"); else puts ("Self-test failed"); } - - return exit_code; } diff --git a/tests/mini-x509.c b/tests/mini-x509.c index 9a1b4e80ba..791eca67ba 100644 --- a/tests/mini-x509.c +++ b/tests/mini-x509.c @@ -29,6 +29,8 @@ #include <string.h> #include <errno.h> #include <gnutls/gnutls.h> +#include "utils.h" +#include "eagain-common.h" static void tls_log_func (int level, const char *str) @@ -36,80 +38,6 @@ tls_log_func (int level, const char *str) fprintf (stderr, "|<%d>| %s", level, str); } -static char *to_server; -static size_t to_server_len; - -static char *to_client; -static size_t to_client_len; - -static ssize_t -client_pull (gnutls_transport_ptr_t tr, void *data, size_t len) -{ - if (to_client_len < len) - { - gnutls_transport_set_global_errno (EAGAIN); - return -1; - } - - memcpy (data, to_client, len); - - memmove (to_client, to_client + len, to_client_len - len); - to_client_len -= len; - - return len; -} - -static ssize_t -client_push (gnutls_transport_ptr_t tr, const void *data, size_t len) -{ - size_t newlen = to_server_len + len; - char *tmp; - - tmp = realloc (to_server, newlen); - if (!tmp) - abort (); - to_server = tmp; - - memcpy (to_server + to_server_len, data, len); - to_server_len = newlen; - - return len; -} - -static ssize_t -server_pull (gnutls_transport_ptr_t tr, void *data, size_t len) -{ - if (to_server_len < len) - { - gnutls_transport_set_global_errno (EAGAIN); - return -1; - } - - memcpy (data, to_server, len); - - memmove (to_server, to_server + len, to_server_len - len); - to_server_len -= len; - - return len; -} - -static ssize_t -server_push (gnutls_transport_ptr_t tr, const void *data, size_t len) -{ - size_t newlen = to_client_len + len; - char *tmp; - - tmp = realloc (to_client, newlen); - if (!tmp) - abort (); - to_client = tmp; - - memcpy (to_client + to_client_len, data, len); - to_client_len = newlen; - - return len; -} - static unsigned char server_cert_pem[] = "-----BEGIN CERTIFICATE-----\n" "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n" @@ -151,10 +79,8 @@ const gnutls_datum_t server_key = { server_key_pem, sizeof (server_key_pem) }; -int -main (int argc, char *argv[]) +void doit(void) { - int debug_level = argc - 1; int exit_code = EXIT_SUCCESS; /* Server stuff. */ gnutls_certificate_credentials_t serverx509cred; @@ -168,7 +94,8 @@ main (int argc, char *argv[]) /* General init. */ gnutls_global_init (); gnutls_global_set_log_function (tls_log_func); - gnutls_global_set_log_level (debug_level); + if (debug) + gnutls_global_set_log_level (2); /* Init server */ gnutls_certificate_allocate_credentials (&serverx509cred); @@ -180,6 +107,7 @@ main (int argc, char *argv[]) gnutls_priority_set_direct (server, "NORMAL", NULL); gnutls_transport_set_push_function (server, server_push); gnutls_transport_set_pull_function (server, server_pull); + gnutls_transport_set_ptr (server, (gnutls_transport_ptr_t)server); /* Init client */ gnutls_certificate_allocate_credentials (&clientx509cred); @@ -188,43 +116,9 @@ main (int argc, char *argv[]) gnutls_priority_set_direct (client, "NORMAL", NULL); gnutls_transport_set_push_function (client, client_push); gnutls_transport_set_pull_function (client, client_pull); + gnutls_transport_set_ptr (client, (gnutls_transport_ptr_t)client); - do - { - static int max_iter = 0; - if (max_iter++ > 10) - abort (); - - if (cret == GNUTLS_E_AGAIN) - { - cret = gnutls_handshake (client); - if (debug_level > 0) - { - tls_log_func (0, "gnutls_handshake (client)...\n"); - tls_log_func (0, gnutls_strerror (cret)); - tls_log_func (0, "\n"); - } - } - - if (sret == GNUTLS_E_AGAIN) - { - sret = gnutls_handshake (server); - if (debug_level > 0) - { - tls_log_func (0, "gnutls_handshake (server)...\n"); - tls_log_func (0, gnutls_strerror (sret)); - tls_log_func (0, "\n"); - } - } - } - while ( - /* Not done: */ - !(cret == GNUTLS_E_SUCCESS && sret == GNUTLS_E_SUCCESS) - /* No error: */ - && (cret == GNUTLS_E_AGAIN || sret == GNUTLS_E_AGAIN)); - - if (cret != GNUTLS_E_SUCCESS && sret != GNUTLS_E_SUCCESS) - exit_code = EXIT_FAILURE; + HANDSHAKE(client, server); gnutls_bye (client, GNUTLS_SHUT_RDWR); gnutls_bye (server, GNUTLS_SHUT_RDWR); @@ -232,21 +126,16 @@ main (int argc, char *argv[]) gnutls_deinit (client); gnutls_deinit (server); - free (to_server); - free (to_client); - gnutls_certificate_free_credentials (serverx509cred); gnutls_certificate_free_credentials (clientx509cred); gnutls_global_deinit (); - if (debug_level > 0) + if (debug > 0) { if (exit_code == 0) puts ("Self-test successful"); else puts ("Self-test failed"); } - - return exit_code; } |