Re: [PATCH] lib/uuid_kunit: add tests for the four random UUID/GUID generators

From: Andy Shevchenko

Date: Fri May 15 2026 - 07:10:20 EST


On Thu, May 14, 2026 at 10:22:41PM +0500, Stepan Ionichev wrote:
> uuid_kunit currently exercises only guid_parse() and uuid_parse() (plus
> their invalid-input paths). The four random generators exported from
> lib/uuid.c -- generate_random_uuid(), generate_random_guid(),
> uuid_gen() and guid_gen() -- have no direct kunit coverage.
>
> Random output cannot be compared against a fixed expected value, but
> RFC 4122 section 4.4 specifies two invariants that any version-4
> random UUID/GUID must satisfy:
>
> - version 4 in the high nibble of the version byte
> (byte 6 in the wire uuid_t layout, byte 7 in the byte-swapped
> guid_t layout);
> - variant DCE 1.1 (binary 10x) in the high bits of byte 8.
>
> Add four test cases that invoke each generator several times and
> verify these bit patterns hold. The same checks catch a regression
> in either the mask/OR sequence in the generators or the layout
> constants. Run the loop a handful of times to cover the small but
> non-zero chance that an unmasked random byte happens to satisfy
> the version/variant pattern by accident on a single call.

Test cases are always good, although here I'm not sure we are so picky about
following this RFC. Whatever, feel free to add
Acked-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>

...

> +static void uuid_test_uuid_gen(struct kunit *test)
> +{
> + unsigned int i;
> + uuid_t u;

> + for (i = 0; i < 8; i++) {

Can be

for (unsigned int i = 0; i < 8; i++) {

> + uuid_gen(&u);
> + KUNIT_EXPECT_EQ(test, u.b[6] & 0xf0, 0x40);
> + KUNIT_EXPECT_EQ(test, u.b[8] & 0xc0, 0x80);
> + }
> +}

Same for the rest.

--
With Best Regards,
Andy Shevchenko