From: Albert Esteve <aesteve@redhat.com>
To: qemu-devel@nongnu.org
Cc: kraxel@redhat.com, Albert Esteve <aesteve@redhat.com>,
marcandre.lureau@gmail.com, "Michael S. Tsirkin" <mst@redhat.com>,
cohuck@redhat.com, Fam Zheng <fam@euphon.net>
Subject: [PATCH v3 1/4] uuid: add hash_func and equal_func
Date: Wed, 24 May 2023 11:13:30 +0200 [thread overview]
Message-ID: <20230524091333.201767-2-aesteve@redhat.com> (raw)
In-Reply-To: <20230524091333.201767-1-aesteve@redhat.com>
Add hash and an equal function to uuid module.
Add a couple simple unit tests for new functions,
checking collisions for similar UUIDs in the case
of the hash function, and comparing generated UUIDs
for the equal function.
Signed-off-by: Albert Esteve <aesteve@redhat.com>
---
include/qemu/uuid.h | 2 ++
tests/unit/test-uuid.c | 27 +++++++++++++++++++++++++++
util/uuid.c | 14 ++++++++++++++
3 files changed, 43 insertions(+)
diff --git a/include/qemu/uuid.h b/include/qemu/uuid.h
index dc40ee1fc9..e24a1099e4 100644
--- a/include/qemu/uuid.h
+++ b/include/qemu/uuid.h
@@ -96,4 +96,6 @@ int qemu_uuid_parse(const char *str, QemuUUID *uuid);
QemuUUID qemu_uuid_bswap(QemuUUID uuid);
+uint32_t qemu_uuid_hash(const void *uuid);
+
#endif
diff --git a/tests/unit/test-uuid.c b/tests/unit/test-uuid.c
index c111de5fc1..aedc125ae9 100644
--- a/tests/unit/test-uuid.c
+++ b/tests/unit/test-uuid.c
@@ -171,6 +171,32 @@ static void test_uuid_unparse_strdup(void)
}
}
+static void test_uuid_hash(void)
+{
+ QemuUUID uuid;
+ int i;
+
+ for (i = 0; i < 100; i++) {
+ qemu_uuid_generate(&uuid);
+ /* Obtain the UUID hash */
+ uint32_t hash_a = qemu_uuid_hash(&uuid);
+ int data_idx = g_random_int_range(0, 15);
+ /* Change a single random byte of the UUID */
+ if (uuid.data[data_idx] < 0xFF) {
+ uuid.data[data_idx]++;
+ } else {
+ uuid.data[data_idx]--;
+ }
+ /* Obtain the UUID hash again */
+ uint32_t hash_b = qemu_uuid_hash(&uuid);
+ /*
+ * Both hashes shall be different (avoid collision)
+ * for any change in the UUID fields
+ */
+ g_assert_cmpint(hash_a, !=, hash_b);
+ }
+}
+
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
@@ -179,6 +205,7 @@ int main(int argc, char **argv)
g_test_add_func("/uuid/parse", test_uuid_parse);
g_test_add_func("/uuid/unparse", test_uuid_unparse);
g_test_add_func("/uuid/unparse_strdup", test_uuid_unparse_strdup);
+ g_test_add_func("/uuid/hash", test_uuid_hash);
return g_test_run();
}
diff --git a/util/uuid.c b/util/uuid.c
index b1108dde78..64eaf2e208 100644
--- a/util/uuid.c
+++ b/util/uuid.c
@@ -116,3 +116,17 @@ QemuUUID qemu_uuid_bswap(QemuUUID uuid)
bswap16s(&uuid.fields.time_high_and_version);
return uuid;
}
+
+uint32_t qemu_uuid_hash(const void *uuid)
+{
+ QemuUUID *qid = (QemuUUID *) uuid;
+ uint32_t h = 5381;
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(qid->data); i++) {
+ h = (h << 5) + h + qid->data[i];
+ }
+
+ return h;
+}
+
--
2.40.0
next prev parent reply other threads:[~2023-05-24 9:14 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-24 9:13 [PATCH v3 0/4] Virtio shared dma-buf Albert Esteve
2023-05-24 9:13 ` Albert Esteve [this message]
2023-05-24 9:13 ` [PATCH v3 2/4] virtio-dmabuf: introduce virtio-dmabuf Albert Esteve
2023-05-24 9:13 ` [PATCH v3 3/4] vhost-user: add shared_object msg Albert Esteve
2023-06-22 19:57 ` Michael S. Tsirkin
2023-06-22 20:18 ` Marc-André Lureau
2023-06-23 7:45 ` Albert Esteve
2023-06-23 6:45 ` Michael S. Tsirkin
2023-06-23 7:19 ` Albert Esteve
2023-06-23 7:32 ` Michael S. Tsirkin
2023-06-23 13:46 ` Albert Esteve
2023-06-23 8:14 ` Michael S. Tsirkin
2023-06-23 12:08 ` Albert Esteve
2023-05-24 9:13 ` [PATCH v3 4/4] vhost-user: refactor send_resp code Albert Esteve
2023-06-23 6:48 ` Michael S. Tsirkin
2023-06-21 8:20 ` [PATCH v3 0/4] Virtio shared dma-buf Albert Esteve
2023-06-21 20:25 ` Michael S. Tsirkin
2023-06-22 19:53 ` Michael S. Tsirkin
2023-06-23 6:49 ` Michael S. Tsirkin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230524091333.201767-2-aesteve@redhat.com \
--to=aesteve@redhat.com \
--cc=cohuck@redhat.com \
--cc=fam@euphon.net \
--cc=kraxel@redhat.com \
--cc=marcandre.lureau@gmail.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).