From cb5d010c49edff5f935032fee10d525a8dc1d334 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20P=C3=ADsa=C5=99?= Date: Fri, 23 Sep 2011 17:14:08 +0200 Subject: [PATCH] test: Move simulated login tests to basic_authentication --- test/simline/Makefile.am | 6 +- test/simline/basic_authentication.c | 118 +++++++++++++++++++++++++++++++++ test/simline/server.c | 126 +++--------------------------------- test/simline/server.h | 69 ++++++++++++++++++++ test/test-tools.h | 3 +- 5 files changed, 202 insertions(+), 120 deletions(-) create mode 100644 test/simline/basic_authentication.c create mode 100644 test/simline/server.h diff --git a/test/simline/Makefile.am b/test/simline/Makefile.am index ae0f22c..f26a9c3 100644 --- a/test/simline/Makefile.am +++ b/test/simline/Makefile.am @@ -16,16 +16,16 @@ export HOME=@top_builddir@ TESTS = if BUILD_CURL -TESTS += server +TESTS += basic_authentication endif noinst_PROGRAMS = $(TESTS) -common = ../test.c ../test.h ../test-tools.h http.c http.h +common = ../test.c ../test.h ../test-tools.h http.c http.h server.c server.h # Access public API only -server_SOURCES = server.c $(common) +basic_authentication_SOURCES = basic_authentication.c $(common) # Tests that need .gnupg #guess_raw_type.log load_raw.log: prepare_environment.log diff --git a/test/simline/basic_authentication.c b/test/simline/basic_authentication.c new file mode 100644 index 0000000..a71f041 --- /dev/null +++ b/test/simline/basic_authentication.c @@ -0,0 +1,118 @@ +#ifndef _POSIX_SOURCE +#define _POSIX_SOURCE /* For getaddrinfo(3) */ +#endif + +#ifndef _BSD_SOURCE +#define _BSD_SOURCE /* For NI_MAXHOST */ +#endif + +#include "../test.h" +#include "server.h" +#include "isds.h" + +static const char *username = "douglas"; +static const char *password = "42"; + + +static int test_login(const isds_error error, struct isds_ctx *context, + const char *url, const char *username, const char *password, + const struct isds_pki_credentials *pki_credentials, + struct isds_otp *otp) { + isds_error err; + + err = isds_login(context, url, username, password, pki_credentials, otp); + if (error != err) + FAIL_TEST("Wrong return code: expected=%s, returned=%s", + isds_strerror(error), isds_strerror(err)); + + isds_logout(context); + PASS_TEST; +} + +int main(int argc, char **argv) { + int error; + pid_t server_process; + char *server_address = NULL; + struct isds_ctx *context = NULL; + char *url = NULL; + + INIT_TEST("server"); + + if (isds_init()) { + isds_cleanup(); + ABORT_UNIT("isds_init() failed\n"); + } + context = isds_ctx_create(); + if (!context) { + isds_cleanup(); + ABORT_UNIT("isds_ctx_create() failed\n"); + } + + { + const struct arguments_basic_authentication server_arguments = { + .username = username, + .password = password, + .isds_deviations = 1 + }; + error = start_server(&server_process, &server_address, + server_basic_authentication, &server_arguments); + if (error == -1) { + isds_ctx_free(&context); + isds_cleanup(); + ABORT_UNIT(server_error); + } + if (-1 == test_asprintf(&url, "http://%s/", server_address)) { + free(server_address); + stop_server(server_process); + isds_ctx_free(&context); + isds_cleanup(); + ABORT_UNIT("Could not format ISDS URL"); + } + free(server_address); + + TEST("invalid credentials", test_login, IE_NOT_LOGGED_IN, context, + url, "7777777", "nbuusr1", NULL, NULL); + + TEST("valid login", test_login, IE_SUCCESS, context, + url, username, password, NULL, NULL); + + if (-1 == stop_server(server_process)) { + ABORT_UNIT(server_error); + } + + free(url); + url = NULL; + } + + { + error = start_server(&server_process, &server_address, + server_out_of_order, NULL); + if (error == -1) { + isds_ctx_free(&context); + isds_cleanup(); + ABORT_UNIT(server_error); + } + if (-1 == test_asprintf(&url, "http://%s/", server_address)) { + free(server_address); + stop_server(server_process); + isds_ctx_free(&context); + isds_cleanup(); + ABORT_UNIT("Could not format ISDS URL"); + } + free(server_address); + + TEST("log into out-of-order server", test_login, IE_SOAP, context, + url, username, password, NULL, NULL); + + if (-1 == stop_server(server_process)) { + ABORT_UNIT(server_error); + } + + free(url); + url = NULL; + } + + isds_ctx_free(&context); + isds_cleanup(); + SUM_TEST(); +} diff --git a/test/simline/server.c b/test/simline/server.c index fc47341..a83eae3 100644 --- a/test/simline/server.c +++ b/test/simline/server.c @@ -6,11 +6,11 @@ #define _BSD_SOURCE /* For NI_MAXHOST */ #endif -#include "../test.h" +#include "../test-tools.h" #include "http.h" -#include "isds.h" #include +#include #include #include #include @@ -19,13 +19,10 @@ #include #include -static const char *username = "douglas"; -static const char *password = "42"; - -static const char *server_error = NULL; +const char *server_error = NULL; /* Save pointer to static error message if not yet set */ -static void set_server_error(const char *message) { +void set_server_error(const char *message) { if (server_error == NULL) { server_error = message; } @@ -34,7 +31,7 @@ static void set_server_error(const char *message) { /* Creates listening TCP socket on localhost. * Returns the socket descriptor or -1. */ -static int listen_on_socket(void) { +int listen_on_socket(void) { int retval; struct addrinfo hints; struct addrinfo *addresses, *address; @@ -72,7 +69,7 @@ static int listen_on_socket(void) { /* Format socket address as printable string. * @return allocated string or NULL in case of error. */ -static char *socket2address(int socket) { +char *socket2address(int socket) { struct sockaddr_storage storage; socklen_t length = (socklen_t) sizeof(storage); char host[NI_MAXHOST]; @@ -117,7 +114,7 @@ struct arguments_basic_authentication { * @server_socket is listening TCP socket of the server * @server_arguments is pointer to structure: * Never returns. Terminates by exit(). */ -static void server_basic_authentication(int server_socket, +void server_basic_authentication(int server_socket, const void *server_arguments) { int client_socket; const struct arguments_basic_authentication *arguments = @@ -185,7 +182,7 @@ static void server_basic_authentication(int server_socket, * @server_socket is listening TCP socket of the server * @server_arguments is ununsed pointer * Never returns. Terminates by exit(). */ -static void server_out_of_order(int server_socket, +void server_out_of_order(int server_socket, const void *server_arguments) { int client_socket; struct http_request *request = NULL; @@ -228,7 +225,7 @@ static void server_out_of_order(int server_socket, * specification. Otherwise server mimics real ISDS implementation as much * as possible. * @return -1 in case of error. */ -static int start_server(pid_t *server_process, char **server_address, +int start_server(pid_t *server_process, char **server_address, void (*server_implementation)(int, const void *), const void *server_arguments) { int server_socket; @@ -275,7 +272,7 @@ static int start_server(pid_t *server_process, char **server_address, /* Kill the server process. * Return -1 in case of error. */ -static int stop_server(pid_t server_process) { +int stop_server(pid_t server_process) { if (server_process <= 0) { set_server_error("Invalid server PID to kill"); return -1; @@ -291,106 +288,3 @@ static int stop_server(pid_t server_process) { return 0; } - -static int test_login(const isds_error error, struct isds_ctx *context, - const char *url, const char *username, const char *password, - const struct isds_pki_credentials *pki_credentials, - struct isds_otp *otp) { - isds_error err; - - err = isds_login(context, url, username, password, pki_credentials, otp); - if (error != err) - FAIL_TEST("Wrong return code: expected=%s, returned=%s", - isds_strerror(error), isds_strerror(err)); - - isds_logout(context); - PASS_TEST; -} - -int main(int argc, char **argv) { - int error; - pid_t server_process; - char *server_address = NULL; - struct isds_ctx *context = NULL; - char *url = NULL; - - INIT_TEST("server"); - - if (isds_init()) { - isds_cleanup(); - ABORT_UNIT("isds_init() failed\n"); - } - context = isds_ctx_create(); - if (!context) { - isds_cleanup(); - ABORT_UNIT("isds_ctx_create() failed\n"); - } - - { - const struct arguments_basic_authentication server_arguments = { - .username = username, - .password = password, - .isds_deviations = 1 - }; - error = start_server(&server_process, &server_address, - server_basic_authentication, &server_arguments); - if (error == -1) { - isds_ctx_free(&context); - isds_cleanup(); - ABORT_UNIT(server_error); - } - if (-1 == test_asprintf(&url, "http://%s/", server_address)) { - free(server_address); - stop_server(server_process); - isds_ctx_free(&context); - isds_cleanup(); - ABORT_UNIT("Could not format ISDS URL"); - } - free(server_address); - - TEST("invalid credentials", test_login, IE_NOT_LOGGED_IN, context, - url, "7777777", "nbuusr1", NULL, NULL); - - TEST("valid login", test_login, IE_SUCCESS, context, - url, username, password, NULL, NULL); - - if (-1 == stop_server(server_process)) { - ABORT_UNIT(server_error); - } - - free(url); - url = NULL; - } - - { - error = start_server(&server_process, &server_address, - server_out_of_order, NULL); - if (error == -1) { - isds_ctx_free(&context); - isds_cleanup(); - ABORT_UNIT(server_error); - } - if (-1 == test_asprintf(&url, "http://%s/", server_address)) { - free(server_address); - stop_server(server_process); - isds_ctx_free(&context); - isds_cleanup(); - ABORT_UNIT("Could not format ISDS URL"); - } - free(server_address); - - TEST("log into out-of-order server", test_login, IE_SOAP, context, - url, username, password, NULL, NULL); - - if (-1 == stop_server(server_process)) { - ABORT_UNIT(server_error); - } - - free(url); - url = NULL; - } - - isds_ctx_free(&context); - isds_cleanup(); - SUM_TEST(); -} diff --git a/test/simline/server.h b/test/simline/server.h new file mode 100644 index 0000000..d32d0f7 --- /dev/null +++ b/test/simline/server.h @@ -0,0 +1,69 @@ +#ifndef __ISDS_SERVER_H +#define __ISDS_SERVER_H + +extern const char *server_error; + +/* Save pointer to static error message if not yet set */ +void set_server_error(const char *message); + + +/* Creates listening TCP socket on localhost. + * Returns the socket descriptor or -1. */ +int listen_on_socket(void); + + +/* Format socket address as printable string. + * @return allocated string or NULL in case of error. */ +char *socket2address(int socket); + + +struct arguments_basic_authentication { + const char *username; /* Sets required user name server has to require. + Set NULL to disable HTTP authentication. */ + const char *password; /* sets required password server has to require */ + _Bool isds_deviations; /* is flag to set conformance level. If false, + server is compliant to standards (HTTP, SOAP) + if not conflicts with ISDS specification. + Otherwise server mimics real ISDS implementation + as much as possible. */ +}; + +/* Do the server protocol. + * @server_socket is listening TCP socket of the server + * @server_arguments is pointer to structure: + * Never returns. Terminates by exit(). */ +void server_basic_authentication(int server_socket, + const void *server_arguments); + + +/* Implementation of server that is out of order. + * It always sends back SOAP Fault with HTTP error 503. + * @server_socket is listening TCP socket of the server + * @server_arguments is ununsed pointer + * Never returns. Terminates by exit(). */ +void server_out_of_order(int server_socket, + const void *server_arguments); + + +/* Start sever in separate process. + * @server_process is PID of forked server + * @server_address is automatically allocated TCP address of listening server + * @username sets required user name server has to require. Set NULL to + * disable HTTP authentication. + * @password sets required password server has to require + * socket. + * @isds_deviations is flag to set conformance level. If false, server is + * compliant to standards (HTTP, SOAP) if not conflicts with ISDS + * specification. Otherwise server mimics real ISDS implementation as much + * as possible. + * @return -1 in case of error. */ +int start_server(pid_t *server_process, char **server_address, + void (*server_implementation)(int, const void *), + const void *server_arguments); + + +/* Kill the server process. + * Return -1 in case of error. */ +int stop_server(pid_t server_process); + +#endif diff --git a/test/test-tools.h b/test/test-tools.h index 8d2803d..22ee850 100644 --- a/test/test-tools.h +++ b/test/test-tools.h @@ -1,7 +1,8 @@ #ifndef __ISDS_TEST_TOOLS_H #define __ISDS_TEST_TOOLS_H -#include +#include /* va_list */ +#include /* size_t, NULL */ /* Print formated string into automtically reallocated @uffer. * @buffer automatically reallocated buffer. Must be &NULL or preallocated -- 2.11.4.GIT