diff mbox series

[v5,11/11] test: lib/uuid: add tests for UUID version/variant bits

Message ID 20240719-b4-dynamic-uuid-v5-11-8a83de3fe3dc@linaro.org
State Superseded
Headers show
Series efi: CapsuleUpdate: support for dynamic UUIDs | expand

Commit Message

Caleb Connolly July 19, 2024, 12:43 p.m. UTC
Add a test to check the version/variant bits of v4 and v5 UUIDs.

Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
---
 test/lib/uuid.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

Comments

Simon Glass July 31, 2024, 2:38 p.m. UTC | #1
Hi Caleb,

On Fri, 19 Jul 2024 at 06:43, Caleb Connolly <caleb.connolly@linaro.org> wrote:
>
> Add a test to check the version/variant bits of v4 and v5 UUIDs.
>
> Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
> ---
>  test/lib/uuid.c | 36 ++++++++++++++++++++++++++++++++++++
>  1 file changed, 36 insertions(+)
>
> diff --git a/test/lib/uuid.c b/test/lib/uuid.c
> index 2c6cfd42ddc3..63d36e120623 100644
> --- a/test/lib/uuid.c
> +++ b/test/lib/uuid.c
> @@ -43,8 +43,44 @@ static int lib_test_uuid_to_le(struct unit_test_state *uts)
>  }
>
>  LIB_TEST(lib_test_uuid_to_le, 0);
>
> +/* Test UUID attribute bits (version, variant) */
> +static int lib_test_uuid_bits(struct unit_test_state *uts)
> +{
> +       unsigned char uuid[16];
> +       efi_guid_t guid;
> +       int i;
> +
> +       /*
> +        * Reduce the chance of a randomly generated UUID disguising
> +        * a regression by testing multiple times.
> +        */
> +       for (i = 0; i < 5; i++) {
> +               /* Test UUID v4 */
> +               gen_rand_uuid((unsigned char *)&uuid);
> +
> +               printf("v4 UUID: %pUb\n", (efi_guid_t *)uuid);
> +
> +               ut_assert((uuid[6] & 0xf0) == 0x40); /* version 4 */

ut_asserteq(0x40, uuid[6] & 0xf0); /* version 4 */

save below

> +               ut_assert((uuid[8] & UUID_VARIANT_MASK) == (UUID_VARIANT << UUID_VARIANT_SHIFT)); /* variant 1 */

check line length

> +
> +               /* Test v5, use the v4 UUID as the namespace */
> +               gen_v5_guid((struct uuid *)uuid,
> +                       &guid, "test", 4, NULL);
> +
> +               printf("v5 GUID: %pUl\n", (efi_guid_t *)uuid);
> +
> +               /* This is a GUID so bits 6 and 7 are swapped (little endian) */
> +               ut_assert((guid.b[7] & 0xf0) == 0x50); /* version 5 */
> +               ut_assert((guid.b[8] & UUID_VARIANT_MASK) == (UUID_VARIANT << UUID_VARIANT_SHIFT)); /* variant 1 */
> +       }
> +
> +       return 0;
> +}
> +
> +LIB_TEST(lib_test_uuid_bits, 0);
> +
>  struct dynamic_uuid_test_data {
>         const char *compatible;
>         const u16 *images[4];
>         const char *expected_uuids[4];
>
> --
> 2.45.2
>

Regards,
Simon
diff mbox series

Patch

diff --git a/test/lib/uuid.c b/test/lib/uuid.c
index 2c6cfd42ddc3..63d36e120623 100644
--- a/test/lib/uuid.c
+++ b/test/lib/uuid.c
@@ -43,8 +43,44 @@  static int lib_test_uuid_to_le(struct unit_test_state *uts)
 }
 
 LIB_TEST(lib_test_uuid_to_le, 0);
 
+/* Test UUID attribute bits (version, variant) */
+static int lib_test_uuid_bits(struct unit_test_state *uts)
+{
+	unsigned char uuid[16];
+	efi_guid_t guid;
+	int i;
+
+	/*
+	 * Reduce the chance of a randomly generated UUID disguising
+	 * a regression by testing multiple times.
+	 */
+	for (i = 0; i < 5; i++) {
+		/* Test UUID v4 */
+		gen_rand_uuid((unsigned char *)&uuid);
+
+		printf("v4 UUID: %pUb\n", (efi_guid_t *)uuid);
+
+		ut_assert((uuid[6] & 0xf0) == 0x40); /* version 4 */
+		ut_assert((uuid[8] & UUID_VARIANT_MASK) == (UUID_VARIANT << UUID_VARIANT_SHIFT)); /* variant 1 */
+
+		/* Test v5, use the v4 UUID as the namespace */
+		gen_v5_guid((struct uuid *)uuid,
+			&guid, "test", 4, NULL);
+
+		printf("v5 GUID: %pUl\n", (efi_guid_t *)uuid);
+
+		/* This is a GUID so bits 6 and 7 are swapped (little endian) */
+		ut_assert((guid.b[7] & 0xf0) == 0x50); /* version 5 */
+		ut_assert((guid.b[8] & UUID_VARIANT_MASK) == (UUID_VARIANT << UUID_VARIANT_SHIFT)); /* variant 1 */
+	}
+
+	return 0;
+}
+
+LIB_TEST(lib_test_uuid_bits, 0);
+
 struct dynamic_uuid_test_data {
 	const char *compatible;
 	const u16 *images[4];
 	const char *expected_uuids[4];