]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
tests: rewrite 'hello_retry_request' as single process
authorSahil Siddiq <icegambit91@gmail.com>
Sun, 3 Mar 2024 20:16:37 +0000 (01:46 +0530)
committerSahil Siddiq <icegambit91@gmail.com>
Sun, 3 Mar 2024 21:48:09 +0000 (03:18 +0530)
Part of issue #1472.
https://gitlab.com/gnutls/gnutls/-/issues/1472

Signed-off-by: Sahil Siddiq <icegambit91@gmail.com>
tests/tls13/hello_retry_request.c

index 4ce1871c8aae45070b933d6b846c33333b002a13..f407b642348c2ed416c0dfcc553f0712da2a0986 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
-
-#if defined(_WIN32)
-
-int main(void)
-{
-       exit(77);
-}
-
-#else
+#include <stdint.h>
 
 #include <string.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <sys/socket.h>
-#include <sys/wait.h>
-#include <arpa/inet.h>
-#include <unistd.h>
 #include <gnutls/gnutls.h>
-#include <gnutls/dtls.h>
 #include <assert.h>
-#include <signal.h>
 
 #include "cert-common.h"
 #include "utils.h"
 #include "tls13/ext-parse.h"
+#include "eagain-common.h"
 
 /* This program tests whether the version in Hello Retry Request message
  * is the expected */
 
-const char *testname = "";
+const char *testname = "hello entry request";
 
-#define myfail(fmt, ...) fail("%s: " fmt, testname, ##__VA_ARGS__)
+const char *side = "";
 
-static void server_log_func(int level, const char *str)
-{
-       fprintf(stderr, "server|<%d>| %s", level, str);
-}
+#define myfail(fmt, ...) fail("%s: " fmt, testname, ##__VA_ARGS__)
 
-static void client_log_func(int level, const char *str)
+static void tls_log_func(int level, const char *str)
 {
-       fprintf(stderr, "client|<%d>| %s", level, str);
+       fprintf(stderr, "%s|<%d>| %s", side, level, str);
 }
 
 #define HANDSHAKE_SESSION_ID_POS 34
@@ -119,145 +101,78 @@ static int hello_callback(gnutls_session_t session, unsigned int htype,
        return 0;
 }
 
-static void client(int fd)
+void doit(void)
 {
-       int ret;
-       gnutls_certificate_credentials_t x509_cred;
-       gnutls_session_t session;
-       struct ctx_st ctx;
+       int sret, cret;
+       gnutls_certificate_credentials_t scred, ccred;
+       gnutls_session_t server, client;
 
+       struct ctx_st ctx;
        memset(&ctx, 0, sizeof(ctx));
 
-       if (debug) {
-               gnutls_global_set_log_function(client_log_func);
-               gnutls_global_set_log_level(7);
-       }
-
-       assert(gnutls_certificate_allocate_credentials(&x509_cred) >= 0);
-
-       assert(gnutls_init(&session, GNUTLS_CLIENT | GNUTLS_KEY_SHARE_TOP) >=
-              0);
-
-       gnutls_handshake_set_timeout(session, get_timeout());
-       gnutls_session_set_ptr(session, &ctx);
-
-       ret = gnutls_priority_set_direct(
-               session,
-               "NORMAL:-VERS-ALL:+VERS-TLS1.3:-GROUP-ALL:+GROUP-SECP256R1:+GROUP-X25519",
-               NULL);
-       if (ret < 0)
-               myfail("cannot set TLS 1.3 priorities\n");
-
-       gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
-
-       gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_ANY,
-                                          GNUTLS_HOOK_BOTH, hello_callback);
-
-       gnutls_transport_set_int(session, fd);
-
-       do {
-               ret = gnutls_handshake(session);
-       } while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
-
-       assert(ctx.hrr_seen != 0);
+       global_init();
 
-       close(fd);
+       gnutls_global_set_log_function(tls_log_func);
+       if (debug)
+               gnutls_global_set_log_level(9);
 
-       gnutls_deinit(session);
-
-       gnutls_certificate_free_credentials(x509_cred);
-}
-
-static void server(int fd)
-{
-       int ret;
-       gnutls_session_t session;
-       gnutls_certificate_credentials_t x509_cred;
-
-       if (debug) {
-               gnutls_global_set_log_function(server_log_func);
-               gnutls_global_set_log_level(4711);
-       }
-
-       assert(gnutls_certificate_allocate_credentials(&x509_cred) >= 0);
-       assert(gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
+       /* Init server */
+       assert(gnutls_certificate_allocate_credentials(&scred) >= 0);
+       assert(gnutls_certificate_set_x509_key_mem(scred, &server_cert,
                                                   &server_key,
                                                   GNUTLS_X509_FMT_PEM) >= 0);
 
-       gnutls_init(&session, GNUTLS_SERVER);
+       gnutls_init(&server, GNUTLS_SERVER);
 
-       gnutls_handshake_set_timeout(session, get_timeout());
-
-       /* server only supports x25519, client advertises secp256r1 */
        assert(gnutls_priority_set_direct(
-                      session,
+                      server,
                       "NORMAL:-VERS-ALL:+VERS-TLS1.3:-GROUP-ALL:+GROUP-X25519",
                       NULL) >= 0);
 
-       gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
+       gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE, scred);
+       gnutls_transport_set_push_function(server, server_push);
+       gnutls_transport_set_pull_function(server, server_pull);
+       gnutls_transport_set_ptr(server, server);
 
-       gnutls_transport_set_int(session, fd);
+       /* Init client */
+       assert(gnutls_certificate_allocate_credentials(&ccred) >= 0);
 
-       do {
-               ret = gnutls_handshake(session);
-               if (ret == GNUTLS_E_INTERRUPTED) { /* expected */
-                       break;
-               }
-       } while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
+       assert(gnutls_init(&client, GNUTLS_CLIENT | GNUTLS_KEY_SHARE_TOP) >= 0);
 
-       if (ret < 0)
-               myfail("handshake error: %s\n", gnutls_strerror(ret));
+       gnutls_session_set_ptr(client, &ctx);
 
-       if (gnutls_group_get(session) != GNUTLS_GROUP_X25519)
-               myfail("group doesn't match the expected: %s\n",
-                      gnutls_group_get_name(gnutls_group_get(session)));
+       cret = gnutls_priority_set_direct(
+               client,
+               "NORMAL:-VERS-ALL:+VERS-TLS1.3:-GROUP-ALL:+GROUP-SECP256R1:+GROUP-X25519",
+               NULL);
+       if (cret < 0)
+               myfail("cannot set TLS 1.3 priorities\n");
 
-       close(fd);
-       gnutls_deinit(session);
+       gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE, ccred);
+       gnutls_transport_set_push_function(client, client_push);
+       gnutls_transport_set_pull_function(client, client_pull);
+       gnutls_transport_set_ptr(client, client);
 
-       gnutls_certificate_free_credentials(x509_cred);
-}
+       gnutls_handshake_set_hook_function(client, GNUTLS_HANDSHAKE_ANY,
+                                          GNUTLS_HOOK_BOTH, hello_callback);
 
-static void ch_handler(int sig)
-{
-       int status = 0;
-       wait(&status);
-       check_wait_status(status);
-       return;
-}
+       HANDSHAKE(client, server);
 
-void doit(void)
-{
-       int fd[2];
-       int ret;
-       pid_t child;
+       assert(ctx.hrr_seen != 0);
 
-       signal(SIGCHLD, ch_handler);
-       signal(SIGPIPE, SIG_IGN);
+       if (gnutls_group_get(server) != GNUTLS_GROUP_X25519)
+               myfail("group doesn't match the expected: %s\n",
+                      gnutls_group_get_name(gnutls_group_get(server)));
 
-       ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
-       if (ret < 0) {
-               perror("socketpair");
-               exit(1);
-       }
+       gnutls_bye(client, GNUTLS_SHUT_WR);
+       gnutls_bye(server, GNUTLS_SHUT_WR);
 
-       child = fork();
-       if (child < 0) {
-               perror("fork");
-               fail("fork");
-               exit(1);
-       }
+       gnutls_deinit(client);
+       gnutls_deinit(server);
 
-       if (child) {
-               /* parent */
-               close(fd[1]);
-               client(fd[0]);
-               kill(child, SIGTERM);
-       } else {
-               close(fd[0]);
-               server(fd[1]);
-               exit(0);
-       }
-}
+       gnutls_certificate_free_credentials(scred);
+       gnutls_certificate_free_credentials(ccred);
 
-#endif /* _WIN32 */
+       gnutls_global_deinit();
+       reset_buffers();
+}