Add the ability to drop datagrams in the noisy dgram BIO
authorMatt Caswell <matt@openssl.org>
Thu, 14 Sep 2023 11:24:12 +0000 (12:24 +0100)
committerMatt Caswell <matt@openssl.org>
Fri, 22 Sep 2023 12:56:43 +0000 (13:56 +0100)
Reviewed-by: Tim Hudson <tjh@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22157)

test/helpers/noisydgrambio.c

index 890ff7904c08f4c20f23a29fdb56af6915a00208..f55be836163236634ac4d3619a2d0d7d9a56a268 100644 (file)
@@ -9,6 +9,11 @@
 
 #include <openssl/bio.h>
 #include "quictestlib.h"
+#include "../testutil.h"
+
+struct noisy_dgram_st {
+    size_t this_dgram;
+};
 
 static int noisy_dgram_read(BIO *bio, char *out, int outl)
 {
@@ -69,23 +74,136 @@ static int noisy_dgram_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride,
     return BIO_sendmmsg(next, msg, stride, num_msg, flags, msgs_processed);
 }
 
+static int should_drop(BIO *bio)
+{
+    struct noisy_dgram_st *data = BIO_get_data(bio);
+
+    if (data == NULL)
+        return 0;
+
+    /*
+     * Drop datagram 1 for now.
+     * TODO(QUIC): Provide more control over this behaviour.
+     */
+    if (data->this_dgram == 1)
+        return 1;
+
+    return 0;
+}
+
+/* There isn't a public function to do BIO_ADDR_copy() so we create one */
+static int bio_addr_copy(BIO_ADDR *dst, BIO_ADDR *src)
+{
+    size_t len;
+    void *data = NULL;
+    int res = 0;
+    int family;
+
+    if (src == NULL || dst == NULL)
+        return 0;
+
+    family = BIO_ADDR_family(src);
+    if (family == AF_UNSPEC) {
+        BIO_ADDR_clear(dst);
+        return 1;
+    }
+
+    if (!BIO_ADDR_rawaddress(src, NULL, &len))
+        return 0;
+
+    if (len > 0) {
+        data = OPENSSL_malloc(len);
+        if (!TEST_ptr(data))
+            return 0;
+    }
+
+    if (!BIO_ADDR_rawaddress(src, data, &len))
+        goto err;
+
+    if (!BIO_ADDR_rawmake(src, family, data, len, BIO_ADDR_rawport(src)))
+        goto err;
+
+    res = 1;
+ err:
+    OPENSSL_free(data);
+    return res;
+}
+
 static int noisy_dgram_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride,
                                 size_t num_msg, uint64_t flags,
                                 size_t *msgs_processed)
 {
     BIO *next = BIO_next(bio);
+    size_t i, data_len = 0, drop_cnt = 0;
+    BIO_MSG *src, *dst;
+    struct noisy_dgram_st *data;
 
-    if (next == NULL)
+    if (!TEST_ptr(next))
+        return 0;
+
+    data = BIO_get_data(bio);
+    if (!TEST_ptr(data))
         return 0;
 
     /*
-     * We will introduce noise here. None implemented yet.
+     * For simplicity we assume that all elements in the msg array have the
+     * same data_len. They are not required to by the API, but it would be quite
+     * strange for that not to be the case - and our code that calls
+     * BIO_recvmmsg does do this (which is all that is important for this test
+     * code). We test the invariant here.
      */
-    return BIO_recvmmsg(next, msg, stride, num_msg, flags, msgs_processed);
+    for (i = 0; i < num_msg; i++) {
+        if (i == 0)
+            data_len = msg[i].data_len;
+        else if (!TEST_size_t_eq(msg[i].data_len, data_len))
+            return 0;
+    }
+
+    if (!BIO_recvmmsg(next, msg, stride, num_msg, flags, msgs_processed))
+        return 0;
+
+    /* Drop any messages */
+    for (i = 0, src = msg, dst = msg;
+         i < *msgs_processed;
+         i++, src++, data->this_dgram++) {
+        if (should_drop(bio)) {
+            drop_cnt++;
+            continue;
+        }
+
+        if (src != dst) {
+            /* Copy the src BIO_MSG to the dst BIO_MSG */
+            memcpy(dst->data, src->data, src->data_len);
+            dst->data_len = src->data_len;
+            dst->flags = src->flags;
+            if (src->local != NULL
+                    && !TEST_true(bio_addr_copy(dst->local, src->local)))
+                return 0;
+            if (!TEST_true(bio_addr_copy(dst->peer, src->peer)))
+                return 0;
+        }
+
+        dst++;
+    }
+
+    *msgs_processed -= drop_cnt;
+
+    if (*msgs_processed == 0) {
+        ERR_raise(ERR_LIB_BIO, BIO_R_NON_FATAL);
+        return 0;
+    }
+
+    return 1;
 }
 
 static int noisy_dgram_new(BIO *bio)
 {
+    struct noisy_dgram_st *data = OPENSSL_zalloc(sizeof(*data));
+
+    if (!TEST_ptr(data))
+        return 0;
+
+    BIO_set_data(bio, data);
     BIO_set_init(bio, 1);
 
     return 1;
@@ -93,6 +211,8 @@ static int noisy_dgram_new(BIO *bio)
 
 static int noisy_dgram_free(BIO *bio)
 {
+    OPENSSL_free(BIO_get_data(bio));
+    BIO_set_data(bio, NULL);
     BIO_set_init(bio, 0);
 
     return 1;