lib/uuid_kunit: add tests for the four random UUID/GUID generators
Subsystems (2)
PATCH 1/1 lib/uuid_kunit: add tests for the four random UUID/GUID generators
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.
--- lib/tests/uuid_kunit.c | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+)
lib/tests/uuid_kunit.c +60 -0
@@ -86,11 +86,71 @@ static void uuid_test_uuid_invalid(struct kunit *test) } } +/* +* RFC 4122 section 4.4 says random UUIDs/GUIDs (version 4) must have: +* - version 4 in the high nibble of the version byte, +* - variant DCE 1.1 (binary 10x) in the high bits of byte 8. +* +* The version byte is byte 6 in the "wire" uuid_t layout and byte 7 in +* the byte-swapped guid_t layout. +*/ +static void uuid_test_uuid_gen (struct kunit *test) +{ +unsigned int i; +uuid_t u; + +for (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 ); + } +} + +static void uuid_test_guid_gen (struct kunit *test) +{ +unsigned int i; +guid_t g; + +for (i =0 ; i <8 ; i++) { +guid_gen (&g); +KUNIT_EXPECT_EQ (test, g.b[7 ] &0xf0 ,0x40 ); +KUNIT_EXPECT_EQ (test, g.b[8 ] &0xc0 ,0x80 ); + } +} + +static void uuid_test_generate_random_uuid (struct kunit *test) +{ +unsigned char buf[16 ]; +unsigned int i; + +for (i =0 ; i <8 ; i++) { +generate_random_uuid (buf); +KUNIT_EXPECT_EQ (test, buf[6 ] &0xf0 ,0x40 ); +KUNIT_EXPECT_EQ (test, buf[8 ] &0xc0 ,0x80 ); + } +} + +static void uuid_test_generate_random_guid (struct kunit *test) +{ +unsigned char buf[16 ]; +unsigned int i; + +for (i =0 ; i <8 ; i++) { +generate_random_guid (buf); +KUNIT_EXPECT_EQ (test, buf[7 ] &0xf0 ,0x40 ); +KUNIT_EXPECT_EQ (test, buf[8 ] &0xc0 ,0x80 ); + } +} +static struct kunit_case uuid_test_cases[] = {KUNIT_CASE (uuid_test_guid_valid),KUNIT_CASE (uuid_test_uuid_valid),KUNIT_CASE (uuid_test_guid_invalid),KUNIT_CASE (uuid_test_uuid_invalid), +KUNIT_CASE (uuid_test_uuid_gen), +KUNIT_CASE (uuid_test_guid_gen), +KUNIT_CASE (uuid_test_generate_random_uuid), +KUNIT_CASE (uuid_test_generate_random_guid), {}, };
Signature · 1 line
2.43.0
Quoted 14 lines · Stepan Ionichev
uuid_kunitcurrently exercises onlyguid_parse()anduuid_parse()(plus their invalid-input paths). The four random generators exported fromlib/uuid.c--generate_random_uuid(),generate_random_guid(),uuid_gen()andguid_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_tlayout, byte 7 in the byte-swappedguid_tlayout);
- variant DCE 1.1 (binary 10x) in the high bits of byte 8.
Quoted 6 lines
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
...
lib/tests/uuid_kunit.c static void uuid_test_uuid_invalid(struct kunit *test)
Quoted 6 lines
lib/tests/uuid_kunit.c +5
-0+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++) {lib/tests/uuid_kunit.c static void uuid_test_uuid_invalid(struct kunit *test) · line 107
Quoted 5 lines
lib/tests/uuid_kunit.c +5
-0+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.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 1/1 lib/uuid_kunit: add tests for the four random UUID/GUID generators
In reply to Stepan Ionichevuuid_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.
---
Changes since v1:
- Declare loop counter inside the for(...) header (C99 style) in all four new test cases, per Andy's review.
- Add Andy's Acked-by.
v1: https://lore.kernel.org/lkml/[email protected]/
--- lib/tests/uuid_kunit.c | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
lib/tests/uuid_kunit.c +56 -0
@@ -86,11 +86,67 @@ static void uuid_test_uuid_invalid(struct kunit *test) } } +/* +* RFC 4122 section 4.4 says random UUIDs/GUIDs (version 4) must have: +* - version 4 in the high nibble of the version byte, +* - variant DCE 1.1 (binary 10x) in the high bits of byte 8. +* +* The version byte is byte 6 in the "wire" uuid_t layout and byte 7 in +* the byte-swapped guid_t layout. +*/ +static void uuid_test_uuid_gen (struct kunit *test) +{ +uuid_t u; + +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 ); + } +} + +static void uuid_test_guid_gen (struct kunit *test) +{ +guid_t g; + +for (unsigned int i =0 ; i <8 ; i++) { +guid_gen (&g); +KUNIT_EXPECT_EQ (test, g.b[7 ] &0xf0 ,0x40 ); +KUNIT_EXPECT_EQ (test, g.b[8 ] &0xc0 ,0x80 ); + } +} + +static void uuid_test_generate_random_uuid (struct kunit *test) +{ +unsigned char buf[16 ]; + +for (unsigned int i =0 ; i <8 ; i++) { +generate_random_uuid (buf); +KUNIT_EXPECT_EQ (test, buf[6 ] &0xf0 ,0x40 ); +KUNIT_EXPECT_EQ (test, buf[8 ] &0xc0 ,0x80 ); + } +} + +static void uuid_test_generate_random_guid (struct kunit *test) +{ +unsigned char buf[16 ]; + +for (unsigned int i =0 ; i <8 ; i++) { +generate_random_guid (buf); +KUNIT_EXPECT_EQ (test, buf[7 ] &0xf0 ,0x40 ); +KUNIT_EXPECT_EQ (test, buf[8 ] &0xc0 ,0x80 ); + } +} +static struct kunit_case uuid_test_cases[] = {KUNIT_CASE (uuid_test_guid_valid),KUNIT_CASE (uuid_test_uuid_valid),KUNIT_CASE (uuid_test_guid_invalid),KUNIT_CASE (uuid_test_uuid_invalid), +KUNIT_CASE (uuid_test_uuid_gen), +KUNIT_CASE (uuid_test_guid_gen), +KUNIT_CASE (uuid_test_generate_random_uuid), +KUNIT_CASE (uuid_test_generate_random_guid), {}, };
Signature · 1 line
2.43.0