From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 396A0392C34 for ; Thu, 14 May 2026 17:23:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778779383; cv=none; b=uKxX8M1cHn3r5gyv5RTaTul1y6HsH4C3VqCKdZ5rXJM8dCs6rj6DzOcLW9Oj/pIclR2PJ0QvEPGjQO4vx1Lc8/7JVi0Ak26O9taiykR2CScFHxK7+1//tWuqj1PyDoMlSO0auA7MfvlqnoaDV+EWQuVE8euvKjA+nb0ImG63/vc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778779383; c=relaxed/simple; bh=MVNfRK0cS8Dt06z8NeESFxJysl04Isvyco3jwpSwjh8=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=rthJyxPqtDFV5lMsKoQA2sDDA92eiOclVxXSLMPqr1zOiyqT1nt5dNNADRZgQIUqs/EVsmD54ycsh0Tl3vOaDAT3d1BQ7jRBVvrxQmro2vBGMGzVwSMrrwchm8SRM4LInqoBAOUOjqwJRan1asOAuhKTYnXgHAMpjWan7w+Xdms= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=pLjgY4MT; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="pLjgY4MT" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-48962cd0864so6303055e9.1 for ; Thu, 14 May 2026 10:23:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778779379; x=1779384179; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=AMaxR+q/wtm3s4msj3w1sOUTMessNfD3kYpKQBJanDw=; b=pLjgY4MTLJ//Cft02z8ASl+D/34IQfhhF3d0fC8zsq42DQ2ZsaJU1E6DyRIiqhxPEs qdGLKFDP3IHrnz9Jm/9ZwgB1oPxorg87zcC540lKwO6V7lM0/OVOjhquZSdSpOf6rFlA 7mB6WddKr6D5anR8wiXBaUVcS9lC0qfHFXt2sH3rvDm0gyp9wna18OIHqXk3ZqknqDWK 405F/JDX/SuxWg5RDSiDaf43cOXJgaUORawJ9/QQX2o1i5RN9p+75wZGhdVJj7qHv24Q UDM8vL2wPf8rD6TrBcywzNQLTMp3IR3OhMf+7ZoNUFPC7gn0UAgA/Emaoajqo00QWxJh TCFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778779379; x=1779384179; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=AMaxR+q/wtm3s4msj3w1sOUTMessNfD3kYpKQBJanDw=; b=ny6do69BEWhvxzRs+FdFkkW7nrZ04oTWUB8TZEtFt3brRzMGBgqTjWlLwouJaIYjB2 s7WcDJj/7gG/24aUbHihNcjTlaB0XRF3R8gQ3V6YAHZxn+zFB6SEfxyBVmJThUs+2fLo okgfWKspjZY1rkF7wEvxsGkKYF2uCaBQlrfWs4AbxKKreEfuqczw1rxwGiRcT69f6TRD 1pWjcX++99oxpl5wc6JZWfoW7esrCQgNkF/F4UWFFY6w48TLrU03pe+B3J3fz5Txt14v PnWh2+bO6fCW6A9Qv6uSxcnfge6JsNKXoIaRa77q6iAQ5A2iRfiU5AdDbkuvzeJPkT25 S8NA== X-Forwarded-Encrypted: i=1; AFNElJ8jhzZf2Gn1g478pAEPSwRIKhADpdtcXAiu4IWoqfRKYe00cqYw4+Ms211W6iRaCBttSVTHhhYt8RJ0uYk=@vger.kernel.org X-Gm-Message-State: AOJu0YyvvtpU4SGDdy1Z6HnLMKxIIXVfNXV2zsjFgemsJowFtkN8y9Oe aLQwRM2fnxWiQ7ELm7KvUdbMDsY/VrYV2sPxfNtgdhDeTzIPq4ILfmnW X-Gm-Gg: Acq92OF6iMFBsqJmGxJ/Ex1gYRPLdxnYEJlqpFR4iYiEpUOXsNZXaDtgQDlm2AaPGUv uElXri9Qni/O4wlIDtBTdsCfLI7+hSZ+634t3E2RELpV7wCXahqi7V5ZhJ6CAvVAJwqm2mgd95I 3BDAW5Yql2VG3SckEk23N+5Z2t//PbhZUsuNEUIB4z2K+cS/DGV2IvP8fYiFBZkKyIPCOH3HqAH vKEZyqCG8NRP3MInZhCIePnQLjD7d/qSLdtCeko1GQXB12D6tCt9BLPky7gxXKIDTasyS7ox5t1 9kKviyq18gXF/ycXFEMKst4vATYu6HHybbXpdiNklTqN1oQA1p7QWlpn3qlt+mUTIpX27YnrSOh oo0r30Yk1lGhOrZ62wSs3t5z3tiQlvVMVs0CfLLUG2S7D794q2e1Ygm5CYflYQ8DUKGc4elk21V uh0XmkYDz42vtdLH72FlaWsGI+Ug+LcARfGGQp/B2Xl8xH X-Received: by 2002:a05:600c:310f:b0:48e:7a10:1f5e with SMTP id 5b1f17b1804b1-48fe5fd5b3amr2237065e9.2.1778779379407; Thu, 14 May 2026 10:22:59 -0700 (PDT) Received: from localhost.localdomain ([82.215.118.79]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45da15a6454sm7853932f8f.34.2026.05.14.10.22.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 May 2026 10:22:58 -0700 (PDT) From: Stepan Ionichev To: andriy.shevchenko@linux.intel.com Cc: akpm@linux-foundation.org, gregkh@linuxfoundation.org, brendan.higgins@linux.dev, david@davidgow.net, raemoar63@gmail.com, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, sozdayvek@gmail.com Subject: [PATCH] lib/uuid_kunit: add tests for the four random UUID/GUID generators Date: Thu, 14 May 2026 22:22:41 +0500 Message-Id: <20260514172241.28006-1-sozdayvek@gmail.com> X-Mailer: git-send-email 2.33.0.windows.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Signed-off-by: Stepan Ionichev --- lib/tests/uuid_kunit.c | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/lib/tests/uuid_kunit.c b/lib/tests/uuid_kunit.c index de71b2649..a800a80e6 100644 --- a/lib/tests/uuid_kunit.c +++ b/lib/tests/uuid_kunit.c @@ -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), {}, }; -- 2.43.0