]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
tests/mini-dtls-fragments: implement a basic DTLS test
authorAlexander Sosedkin <asosedkin@redhat.com>
Fri, 20 Mar 2026 15:09:40 +0000 (16:09 +0100)
committerAlexander Sosedkin <asosedkin@redhat.com>
Wed, 29 Apr 2026 13:35:02 +0000 (15:35 +0200)
Signed-off-by: Alexander Sosedkin <asosedkin@redhat.com>
tests/Makefile.am
tests/mini-dtls-fragments.c [new file with mode: 0644]

index aeeaaf79df6714b76ebd1cbab84fda03604df3d7..586f1952d07b12562c031eed808c3acd6ca4c4dc 100644 (file)
@@ -241,7 +241,8 @@ ctests += mini-record-2 simple gnutls_hmac_fast set_pkcs12_cred cert certuniquei
         x509cert-dntypes id-on-xmppAddr tls13-compat-mode ciphersuite-name \
         x509-upnconstraint xts-key-check cipher-padding pkcs7-verify-double-free \
         fips-rsa-sizes tls12-rehandshake-ticket pathbuf tls-force-ems \
-        psk-importer privkey-derive dh-compute2 ecdh-compute2
+        psk-importer privkey-derive dh-compute2 ecdh-compute2 \
+        mini-dtls-fragments
 
 ctests += tls-channel-binding
 
@@ -513,6 +514,10 @@ pathbuf_CPPFLAGS = $(AM_CPPFLAGS) \
        -I$(top_srcdir)/gl      \
        -I$(top_builddir)/gl
 
+mini_dtls_fragments_CPPFLAGS = $(AM_CPPFLAGS) \
+       -I$(top_srcdir)/gl      \
+       -I$(top_builddir)/gl
+
 if ENABLE_PKCS11
 if !WINDOWS
 ctests += tls13/post-handshake-with-cert-pkcs11 pkcs11/tls-neg-pkcs11-no-key \
diff --git a/tests/mini-dtls-fragments.c b/tests/mini-dtls-fragments.c
new file mode 100644 (file)
index 0000000..ee75fee
--- /dev/null
@@ -0,0 +1,208 @@
+/*
+ * Copyright (C) 2026 Red Hat, Inc.
+ *
+ * Author: Alexander Sosedkin
+ *
+ * This file is part of GnuTLS.
+ *
+ * GnuTLS is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuTLS is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#if defined(_WIN32)
+
+int main(void)
+{
+       exit(77);
+}
+
+#else
+
+#include <assert.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+#include <gnutls/gnutls.h>
+#include <gnutls/dtls.h>
+#include "cert-common.h"
+#include "utils.h"
+
+#include "attribute.h"
+
+static void server_log_func(int level, const char *str)
+{
+       fprintf(stderr, "server|<%d>| %s", level, str);
+}
+
+static void client_log_func(int level, const char *str)
+{
+       fprintf(stderr, "client|<%d>| %s", level, str);
+}
+
+#define QUEUE_SIZE 1024
+#define PACKET_SIZE 2048
+
+typedef struct {
+       uint8_t buf[PACKET_SIZE];
+       size_t len;
+} packet_t;
+typedef struct {
+       packet_t packets[QUEUE_SIZE];
+       size_t head;
+       size_t tail;
+} queue_t;
+
+static queue_t c2s, s2c;
+
+static int queue_put(queue_t *q, const void *buf, size_t len)
+{
+       assert(len <= PACKET_SIZE);
+       memcpy(q->packets[q->tail].buf, buf, len);
+       q->packets[q->tail].len = len;
+       q->tail++;
+       q->tail %= QUEUE_SIZE;
+       assert(q->tail != q->head);
+       return len;
+}
+
+static ssize_t queue_get(queue_t *q, gnutls_session_t s, void *buf, size_t len)
+{
+       if (q->head == q->tail) {
+               gnutls_transport_set_errno(s, EAGAIN);
+               return -1;
+       }
+       size_t n = q->packets[q->head].len;
+       memcpy(buf, q->packets[q->head].buf, n);
+       q->head++;
+       q->head %= QUEUE_SIZE;
+       return n;
+}
+
+static void queue_reset(queue_t *q)
+{
+       q->head = q->tail = 0;
+}
+
+static int pull_timeout(gnutls_transport_ptr_t tr, unsigned ms)
+{
+       return 1;
+}
+
+static ssize_t server_pull(gnutls_transport_ptr_t tr, void *b, size_t l)
+{
+       return queue_get(&c2s, (gnutls_session_t)tr, b, l);
+}
+
+static ssize_t client_pull(gnutls_transport_ptr_t tr, void *b, size_t l)
+{
+       return queue_get(&s2c, (gnutls_session_t)tr, b, l);
+}
+
+static ssize_t server_push(gnutls_transport_ptr_t tr, const void *b, size_t l)
+{
+       return queue_put(&s2c, b, l);
+}
+
+static ssize_t client_push_normal(gnutls_transport_ptr_t tr, const void *b,
+                                 size_t l)
+{
+       return queue_put(&c2s, b, l);
+}
+
+static void test(gnutls_push_func client_push)
+{
+       gnutls_session_t client, server;
+       gnutls_certificate_credentials_t ccred, scred;
+       int cr = 0, sr = 0;
+       bool cdone = false, sdone = false;
+
+       if (debug)
+               gnutls_global_set_log_level(4711);
+
+       gnutls_certificate_allocate_credentials(&scred);
+       gnutls_certificate_set_x509_key_mem(scred, &server_cert, &server_key,
+                                           GNUTLS_X509_FMT_PEM);
+       gnutls_certificate_allocate_credentials(&ccred);
+
+       gnutls_init(&server, GNUTLS_SERVER | GNUTLS_DATAGRAM);
+       gnutls_init(&client, GNUTLS_CLIENT | GNUTLS_DATAGRAM);
+
+       gnutls_priority_set_direct(server, "NORMAL:-VERS-ALL:+VERS-DTLS1.2",
+                                  NULL);
+       gnutls_priority_set_direct(client, "NORMAL:-VERS-ALL:+VERS-DTLS1.2",
+                                  NULL);
+
+       gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE, scred);
+       gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE, ccred);
+
+       gnutls_dtls_set_timeouts(client, get_dtls_retransmit_timeout(),
+                                get_timeout());
+       gnutls_dtls_set_timeouts(server, get_dtls_retransmit_timeout(),
+                                get_timeout());
+
+       gnutls_transport_set_ptr(client, client);
+       gnutls_transport_set_push_function(client, client_push);
+       gnutls_transport_set_pull_function(client, client_pull);
+       gnutls_transport_set_pull_timeout_function(client, pull_timeout);
+
+       gnutls_transport_set_ptr(server, server);
+       gnutls_transport_set_push_function(server, server_push);
+       gnutls_transport_set_pull_function(server, server_pull);
+       gnutls_transport_set_pull_timeout_function(server, pull_timeout);
+
+       while (!cdone || !sdone) {
+               gnutls_global_set_log_function(client_log_func);
+               if (!cdone)
+                       cr = gnutls_handshake(client);
+               if (!cr || gnutls_error_is_fatal(cr))
+                       cdone = true;
+
+               gnutls_global_set_log_function(server_log_func);
+               if (!sdone)
+                       sr = gnutls_handshake(server);
+               if (!sr || gnutls_error_is_fatal(sr))
+                       sdone = true;
+       }
+
+       if (cr)
+               fail("client: %s\n", gnutls_strerror(cr));
+       if (sr)
+               fail("server: %s\n", gnutls_strerror(sr));
+
+       success("OK\n");
+
+       queue_reset(&c2s);
+       queue_reset(&s2c);
+
+       gnutls_deinit(client);
+       gnutls_deinit(server);
+       gnutls_certificate_free_credentials(ccred);
+       gnutls_certificate_free_credentials(scred);
+}
+
+void doit(void)
+{
+       global_init();
+       test(client_push_normal);
+       gnutls_global_deinit();
+}
+
+#endif /* _WIN32 */