summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2023-09-14 12:24:12 +0100
committerMatt Caswell <matt@openssl.org>2023-09-22 13:56:43 +0100
commitc6bb25fab062738d22bea38462d14bd1c7de22e5 (patch)
tree7d1144325ea65de99fda51dbe17e545802e5510c
parent43b94c7fe4a427ad95f7401dd24f42d2ae094dfb (diff)
Add the ability to drop datagrams in the noisy dgram BIO
Reviewed-by: Tim Hudson <tjh@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from https://github.com/openssl/openssl/pull/22157)
-rw-r--r--test/helpers/noisydgrambio.c126
1 files changed, 123 insertions, 3 deletions
diff --git a/test/helpers/noisydgrambio.c b/test/helpers/noisydgrambio.c
index 890ff7904c..f55be83616 100644
--- a/test/helpers/noisydgrambio.c
+++ b/test/helpers/noisydgrambio.c
@@ -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;