From 7d287a6ba9d5f06e75c0036589526fd90aefa31f Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 26 Mar 2013 22:00:39 -0700 Subject: [PATCH] modified test port number to allow concurrent testing --- .gitignore | 2 + configure.ac | 2 +- cyassl/ctaocrypt/hmac.h | 2 +- cyassl/test.h | 32 ++++++++--- examples/client/client.c | 6 ++- examples/echoclient/echoclient.c | 9 +++- examples/echoserver/echoserver.c | 16 ++++-- examples/server/server.c | 6 ++- tests/suites.c | 40 +++++++++++--- tests/unit.c | 1 + testsuite/testsuite.c | 92 +++++++++++++++++++++++++++----- 11 files changed, 170 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index bf1d9b1e1..6880ec503 100644 --- a/.gitignore +++ b/.gitignore @@ -78,3 +78,5 @@ clang_output_* internal.plist cov-int cyassl.tgz +*.log +*.trs diff --git a/configure.ac b/configure.ac index e65946341..7909b38aa 100644 --- a/configure.ac +++ b/configure.ac @@ -14,7 +14,7 @@ AC_CONFIG_MACRO_DIR([m4]) AC_CANONICAL_TARGET AC_USE_SYSTEM_EXTENSIONS -AM_INIT_AUTOMAKE([1.12 -Wall -Werror -Wno-portability foreign tar-ustar subdir-objects serial-tests]) +AM_INIT_AUTOMAKE([1.12 -Wall -Werror -Wno-portability foreign tar-ustar subdir-objects]) AC_CANONICAL_HOST AC_CANONICAL_BUILD diff --git a/cyassl/ctaocrypt/hmac.h b/cyassl/ctaocrypt/hmac.h index 7b8105a2f..217718ee6 100644 --- a/cyassl/ctaocrypt/hmac.h +++ b/cyassl/ctaocrypt/hmac.h @@ -35,7 +35,7 @@ #include #endif -#ifdef CYASSL_SHA384 +#ifdef CYASSL_SHA512 #include #endif diff --git a/cyassl/test.h b/cyassl/test.h index 4a0f943a1..9e022c350 100644 --- a/cyassl/test.h +++ b/cyassl/test.h @@ -131,6 +131,7 @@ typedef struct tcp_ready { int ready; /* predicate */ + int port; #ifdef _POSIX_THREADS pthread_mutex_t mutex; pthread_cond_t cond; @@ -454,14 +455,14 @@ static INLINE int tcp_select(SOCKET_T socketfd, int to_sec) } -static INLINE void tcp_listen(SOCKET_T* sockfd, int port, int useAnyAddr, +static INLINE void tcp_listen(SOCKET_T* sockfd, int* port, int useAnyAddr, int udp) { SOCKADDR_IN_T addr; /* don't use INADDR_ANY by default, firewall may block, make user switch on */ - build_addr(&addr, (useAnyAddr ? INADDR_ANY : yasslIP), port); + build_addr(&addr, (useAnyAddr ? INADDR_ANY : yasslIP), *port); tcp_socket(sockfd, udp); #ifndef USE_WINDOWS_API @@ -480,6 +481,14 @@ static INLINE void tcp_listen(SOCKET_T* sockfd, int port, int useAnyAddr, if (listen(*sockfd, 5) != 0) err_sys("tcp listen failed"); } + #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) + if (*port == 0) + { + socklen_t len = sizeof(addr); + if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) + *port = ntohs(addr.sin_port); + } + #endif } @@ -504,12 +513,12 @@ static INLINE int udp_read_connect(SOCKET_T sockfd) } static INLINE void udp_accept(SOCKET_T* sockfd, int* clientfd, int useAnyAddr, - func_args* args) + int port, func_args* args) { SOCKADDR_IN_T addr; (void)args; - build_addr(&addr, (useAnyAddr ? INADDR_ANY : yasslIP), yasslPort); + build_addr(&addr, (useAnyAddr ? INADDR_ANY : yasslIP), port); tcp_socket(sockfd, 1); @@ -526,12 +535,22 @@ static INLINE void udp_accept(SOCKET_T* sockfd, int* clientfd, int useAnyAddr, if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) err_sys("tcp bind failed"); + #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) + if (port == 0) + { + socklen_t len = sizeof(addr); + if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) + port = ntohs(addr.sin_port); + } + #endif + #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) /* signal ready to accept data */ { tcp_ready* ready = args->signal; pthread_mutex_lock(&ready->mutex); ready->ready = 1; + ready->port = port; pthread_cond_signal(&ready->cond); pthread_mutex_unlock(&ready->mutex); } @@ -547,11 +566,11 @@ static INLINE void tcp_accept(SOCKET_T* sockfd, int* clientfd, func_args* args, socklen_t client_len = sizeof(client); if (udp) { - udp_accept(sockfd, clientfd, useAnyAddr, args); + udp_accept(sockfd, clientfd, useAnyAddr, port, args); return; } - tcp_listen(sockfd, port, useAnyAddr, udp); + tcp_listen(sockfd, &port, useAnyAddr, udp); #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) /* signal ready to tcp_accept */ @@ -559,6 +578,7 @@ static INLINE void tcp_accept(SOCKET_T* sockfd, int* clientfd, func_args* args, tcp_ready* ready = args->signal; pthread_mutex_lock(&ready->mutex); ready->ready = 1; + ready->port = port; pthread_cond_signal(&ready->cond); pthread_mutex_unlock(&ready->mutex); } diff --git a/examples/client/client.c b/examples/client/client.c index d44f7100b..9234d3937 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -94,7 +94,7 @@ static void Usage(void) " NOTE: All files relative to CyaSSL home dir\n"); printf("-? Help, print this usage\n"); printf("-h Host to connect to, default %s\n", yasslIP); - printf("-p Port to connect on, default %d\n", yasslPort); + printf("-p Port to connect on, not 0, default %d\n", yasslPort); printf("-v SSL version [0-3], SSLv3(0) - TLS1.2(3)), default %d\n", CLIENT_DEFAULT_VERSION); printf("-l Cipher list\n"); @@ -206,6 +206,10 @@ void client_test(void* args) case 'p' : port = atoi(myoptarg); + #if !defined(NO_MAIN_DRIVER) || defined(USE_WINDOWS_API) + if (port == 0) + err_sys("port number cannot be 0"); + #endif break; case 'v' : diff --git a/examples/echoclient/echoclient.c b/examples/echoclient/echoclient.c index 12bbc6a38..6f3258465 100644 --- a/examples/echoclient/echoclient.c +++ b/examples/echoclient/echoclient.c @@ -50,6 +50,7 @@ void echoclient_test(void* args) int sendSz; int argc = 0; char** argv = 0; + int port = yasslPort; ((func_args*)args)->return_code = -1; /* error state */ argc = ((func_args*)args)->argc; @@ -79,6 +80,10 @@ void echoclient_test(void* args) doPSK = 1; #endif +#if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) + port = ((func_args*)args)->signal->port; +#endif + #if defined(CYASSL_DTLS) method = DTLSv1_client_method(); #elif !defined(NO_TLS) @@ -128,12 +133,12 @@ void echoclient_test(void* args) if (doDTLS) { SOCKADDR_IN_T addr; - build_addr(&addr, yasslIP, yasslPort); + build_addr(&addr, yasslIP, port); CyaSSL_dtls_set_peer(ssl, &addr, sizeof(addr)); tcp_socket(&sockfd, 1); } else { - tcp_connect(&sockfd, yasslIP, yasslPort, 0); + tcp_connect(&sockfd, yasslIP, port, 0); } SSL_set_fd(ssl, sockfd); diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index cc4dbd07e..1052fa5d5 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -38,7 +38,7 @@ #endif -static void SignalReady(void* args) +static void SignalReady(void* args, int port) { #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) /* signal ready to tcp_accept */ @@ -46,10 +46,12 @@ static void SignalReady(void* args) tcp_ready* ready = server_args->signal; pthread_mutex_lock(&ready->mutex); ready->ready = 1; + ready->port = port; pthread_cond_signal(&ready->cond); pthread_mutex_unlock(&ready->mutex); #endif (void)args; + (void)port; } @@ -64,6 +66,7 @@ THREAD_RETURN CYASSL_THREAD echoserver_test(void* args) int outCreated = 0; int shutDown = 0; int useAnyAddr = 0; + int port = yasslPort; int argc = ((func_args*)args)->argc; char** argv = ((func_args*)args)->argv; @@ -93,7 +96,10 @@ THREAD_RETURN CYASSL_THREAD echoserver_test(void* args) doPSK = 1; #endif - tcp_listen(&sockfd, yasslPort, useAnyAddr, doDTLS); + #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) + port = 0; + #endif + tcp_listen(&sockfd, &port, useAnyAddr, doDTLS); #if defined(CYASSL_DTLS) method = CyaDTLSv1_server_method(); @@ -176,7 +182,7 @@ THREAD_RETURN CYASSL_THREAD echoserver_test(void* args) #endif } - SignalReady(args); + SignalReady(args, port); while (!shutDown) { CYASSL* ssl = 0; @@ -279,8 +285,8 @@ THREAD_RETURN CYASSL_THREAD echoserver_test(void* args) CyaSSL_free(ssl); CloseSocket(clientfd); #ifdef CYASSL_DTLS - tcp_listen(&sockfd, yasslPort, useAnyAddr, doDTLS); - SignalReady(args); + tcp_listen(&sockfd, &port, useAnyAddr, doDTLS); + SignalReady(args, port); #endif } diff --git a/examples/server/server.c b/examples/server/server.c index 3b06d0687..0c79aff80 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -93,7 +93,7 @@ static void Usage(void) printf("server " LIBCYASSL_VERSION_STRING " NOTE: All files relative to CyaSSL home dir\n"); printf("-? Help, print this usage\n"); - printf("-p Port to listen on, default %d\n", yasslPort); + printf("-p Port to listen on, not 0, default %d\n", yasslPort); printf("-v SSL version [0-3], SSLv3(0) - TLS1.2(3)), default %d\n", SERVER_DEFAULT_VERSION); printf("-l Cipher list\n"); @@ -182,6 +182,10 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args) case 'p' : port = atoi(myoptarg); + #if !defined(NO_MAIN_DRIVER) || defined(USE_WINDOWS_API) + if (port == 0) + err_sys("port number cannot be 0"); + #endif break; case 'v' : diff --git a/tests/suites.c b/tests/suites.c index 3b6307a3d..91e6574f9 100644 --- a/tests/suites.c +++ b/tests/suites.c @@ -43,8 +43,15 @@ CYASSL_CTX* cipherSuiteCtx = NULL; char nonblockFlag[] = "-N"; char noVerifyFlag[] = "-d"; +char portFlag[] = "-p"; char flagSep[] = " "; +char svrPort[] = "0"; +#ifdef USE_WINDOWS_API + #define SNPRINTF _snprintf +#else + #define SNPRINTF snprintf +#endif #ifdef NO_OLD_TLS /* if the protocol version is less than tls 1.2 return 1, else 0 */ @@ -125,6 +132,7 @@ static int execute_test_case(int svr_argc, char** svr_argv, int i; size_t added = 0; static int tests = 1; + char portNumber[6]; commandLine[0] = '\0'; for (i = 0; i < svr_argc; i++) { @@ -156,11 +164,10 @@ static int execute_test_case(int svr_argc, char** svr_argv, if (addNoVerify) { printf("repeating test with client cert request off\n"); added += 4; /* -d plus space plus terminator */ - if (added >= MAX_COMMAND_SZ) + if (added >= MAX_COMMAND_SZ || svr_argc >= MAX_ARGS) printf("server command line too long\n"); else { - svr_argv[svr_argc] = noVerifyFlag; - svr_argc++; + svr_argv[svr_argc++] = noVerifyFlag; svrArgs.argc = svr_argc; strcat(commandLine, noVerifyFlag); strcat(commandLine, flagSep); @@ -169,16 +176,24 @@ static int execute_test_case(int svr_argc, char** svr_argv, if (addNonBlocking) { printf("repeating test with non blocking on\n"); added += 4; /* -N plus terminator */ - if (added >= MAX_COMMAND_SZ) + if (added >= MAX_COMMAND_SZ || svr_argc >= MAX_ARGS) printf("server command line too long\n"); else { - svr_argv[svr_argc] = nonblockFlag; - svr_argc++; + svr_argv[svr_argc++] = nonblockFlag; svrArgs.argc = svr_argc; strcat(commandLine, nonblockFlag); strcat(commandLine, flagSep); } } + /* add port 0 */ + if (svr_argc + 2 > MAX_ARGS) + printf("cannot add the magic port number flag to server\n"); + else + { + svr_argv[svr_argc++] = portFlag; + svr_argv[svr_argc++] = svrPort; + svrArgs.argc = svr_argc; + } printf("trying server command line[%d]: %s\n", tests, commandLine); commandLine[0] = '\0'; @@ -199,7 +214,6 @@ static int execute_test_case(int svr_argc, char** svr_argv, else { cli_argv[cli_argc] = nonblockFlag; cli_argc++; - cliArgs.argc = cli_argc; strcat(commandLine, nonblockFlag); strcat(commandLine, flagSep); } @@ -212,7 +226,17 @@ static int execute_test_case(int svr_argc, char** svr_argv, svrArgs.signal = &ready; start_thread(server_test, &svrArgs, &serverThread); wait_tcp_ready(&svrArgs); - + if (ready.port != 0) + { + if (cli_argc + 2 > MAX_ARGS) + printf("cannot add the magic port number flag to client\n"); + else { + SNPRINTF(portNumber, sizeof(portNumber), "%d", ready.port); + cli_argv[cli_argc++] = portFlag; + cli_argv[cli_argc++] = portNumber; + cliArgs.argc = cli_argc; + } + } /* start client */ client_test(&cliArgs); diff --git a/tests/unit.c b/tests/unit.c index 5cd361bbe..cd7c1e76d 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -94,6 +94,7 @@ void join_thread(THREAD_TYPE thread) void InitTcpReady(tcp_ready* ready) { ready->ready = 0; + ready->port = 0; #ifdef _POSIX_THREADS pthread_mutex_init(&ready->mutex, 0); pthread_cond_init(&ready->cond, 0); diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index cc4b4ace8..44dfa6ff8 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -39,10 +39,18 @@ #include "examples/server/server.h" #include "ctaocrypt/test/test.h" +#ifdef USE_WINDOWS_API + #define SNPRINTF _snprintf +#else + #define SNPRINTF snprintf +#endif + void client_test(void*); void file_test(const char* file, byte* hash); +void simple_test(func_args*); + enum { NUMARGS = 3 }; @@ -54,7 +62,6 @@ char* myoptarg = NULL; int main(int argc, char** argv) { - func_args args; func_args server_args; tcp_ready ready; @@ -68,8 +75,8 @@ int main(int argc, char** argv) StartTCP(); - args.argc = server_args.argc = argc; - args.argv = server_args.argv = argv; + server_args.argc = argc; + server_args.argv = argv; CyaSSL_Init(); #if defined(DEBUG_CYASSL) && !defined(HAVE_VALGRIND) @@ -81,19 +88,15 @@ int main(int argc, char** argv) else if (CurrentDir("build")) /* Xcode->Preferences->Locations->Build */ ChangeDirBack(2); /* Location "Place build product in locations specified by targets", uses build/Debug */ + server_args.signal = &ready; + InitTcpReady(&ready); + /* CTaoCrypt test */ - ctaocrypt_test(&args); - if (args.return_code != 0) return args.return_code; + ctaocrypt_test(&server_args); + if (server_args.return_code != 0) return server_args.return_code; /* Simple CyaSSL client server test */ - InitTcpReady(&ready); - server_args.signal = &ready; - start_thread(server_test, &server_args, &serverThread); - wait_tcp_ready(&server_args); - - client_test(&args); - if (args.return_code != 0) return args.return_code; - join_thread(serverThread); + simple_test(&server_args); if (server_args.return_code != 0) return server_args.return_code; /* Echo input yaSSL client server test */ @@ -119,6 +122,9 @@ int main(int argc, char** argv) strcpy(echo_args.argv[2], "output"); remove("output"); + /* Share the signal, it has the new port number in it. */ + echo_args.signal = server_args.signal; + /* make sure OK */ echoclient_test(&echo_args); if (echo_args.return_code != 0) return echo_args.return_code; @@ -157,6 +163,65 @@ int main(int argc, char** argv) return EXIT_SUCCESS; } +void simple_test(func_args* args) +{ + THREAD_TYPE serverThread; + + func_args svrArgs; + char *svrArgv[NUMARGS]; + char argc0s[32]; + char argc1s[32]; + char argc2s[32]; + + func_args cliArgs; + char *cliArgv[NUMARGS]; + char argc0c[32]; + char argc1c[32]; + char argc2c[32]; + + svrArgv[0] = argc0s; + svrArgv[1] = argc1s; + svrArgv[2] = argc2s; + cliArgv[0] = argc0c; + cliArgv[1] = argc1c; + cliArgv[2] = argc2c; + + svrArgs.argc = 1; + svrArgs.argv = svrArgv; + svrArgs.return_code = 0; + cliArgs.argc = 1; + cliArgs.argv = cliArgv; + cliArgs.return_code = 0; + + strcpy(svrArgs.argv[0], "SimpleServer"); + #ifndef USE_WINDOWS_API + svrArgs.argc = NUMARGS; + strcpy(svrArgs.argv[1], "-p"); + strcpy(svrArgs.argv[2], "0"); + #endif + /* Set the last arg later, when it is known. */ + + args->return_code = 0; + svrArgs.signal = args->signal; + start_thread(server_test, &svrArgs, &serverThread); + wait_tcp_ready(&svrArgs); + + /* Setting the actual port number. */ + strcpy(cliArgs.argv[0], "SimpleClient"); + #ifndef USE_WINDOWS_API + cliArgs.argc = NUMARGS; + strcpy(cliArgs.argv[1], "-p"); + SNPRINTF(cliArgs.argv[2], sizeof(argc2c), "%d", svrArgs.signal->port); + #endif + + client_test(&cliArgs); + if (cliArgs.return_code != 0) { + args->return_code = cliArgs.return_code; + return; + } + join_thread(serverThread); + if (svrArgs.return_code != 0) args->return_code = svrArgs.return_code; +} void wait_tcp_ready(func_args* args) @@ -200,6 +265,7 @@ void join_thread(THREAD_TYPE thread) void InitTcpReady(tcp_ready* ready) { ready->ready = 0; + ready->port = 0; #ifdef _POSIX_THREADS pthread_mutex_init(&ready->mutex, 0); pthread_cond_init(&ready->cond, 0);