From nobody Mon May 25 08:11:32 2026 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 107F43D34B3 for ; Sat, 16 May 2026 12:09:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778933374; cv=none; b=chk1wUVLvw5AAEX5FMCt6bDGM/aRnzULKd7XaIVS938yDiMFfhxk1D0Imwq+Ce+tTarrixayZif2cg5rneMGuEge6dNY1Ql9zPetUMFeQpiCWAwkpTUX3k2JeIjryg0t98111Qu9+3zkXJiUJo58PtMnEx70cuTwgtMt7SgtK78= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778933374; c=relaxed/simple; bh=WigzUkQhDQXkuTaj/aA+DWPFt4McdPUB35it5BTy8x0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UmWK5wLlC813aG0GMiCJd+PP/wmjGSkJKFZd20kAyGCClBzlSjcGi0V7Lux3bEdY3ZRDLqQT3FdCUbMMeyvVKBcXm71SuBcaPD87boDyePtx4W7Ye1bSuYpkwljW2NIyQ1Tea79iIuhUekIZ91IPQyBAW2OG1ct0KKmEqVZ0FKE= 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=VChew1dk; arc=none smtp.client-ip=209.85.128.54 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="VChew1dk" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-488a8f97f6bso1468115e9.2 for ; Sat, 16 May 2026 05:09:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778933371; x=1779538171; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4xDdmLlH4yGQsstl1746kg3F4bXclg41Alo5RBf4g7w=; b=VChew1dkXAaR3AnXN/0hEI12Jc3bJREgPdoiWhdS/f9X6ljYLdPAaesvYX4I+Z8G3g XvWFZeLuNue2Oj9M6+FhdF3MnopppUV74OwDCxa9V2tot1aI9Y/t3QB9/gCL0RMjZkbO MLe+ZFSqMIUF1DB9yZKUdXdB21h6z0yFaOB/jlS0u6aC5M5nNhp2QBtBuiHe/Zn0ORw6 aAkYpy/RdZ93RYNYB1g9yfElQ1isjaQ4z7BT+1NU2eZQ8yPGg9BrJrOwHMRgnHuaVo9l U7ZF8s8l8uzarqRAjdAvgTFkGjP1igeSMwXJoJqgTZumGu/nzPfO0/Z5Vr/Lu+gXbqLX t6Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778933371; x=1779538171; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=4xDdmLlH4yGQsstl1746kg3F4bXclg41Alo5RBf4g7w=; b=KYmkDW63Fb23XvEsDP+KVcBTT1pEnWlJR/mD9MHHHEu2HmwducCxpL9azNG+VChUBt BJiJDOIWhSTYgoYZoIbuPIHzsFtO5mGdCT4P3AbhYKUkZI1l/N2H08lk5oANlXFoUNfy pxWcy3Z/MGELs+GXm9hyG+ANkjvtpbx0UkZWfqlkZB7Jj3kegl1KkEI5kBwzNCabnitv ZvwqzkOqIgwgU48/lc6XShVLji8nQCsc2043U/thIoKDXPInhUH/zH3LpdOKwwGLz4As UUWbKj+KoGaWW7CRL4/knUBNp45SM85+knzc5xkKLIB1GDMl8h/WccRSNzv5odVocM4E 1Vig== X-Forwarded-Encrypted: i=1; AFNElJ8vPuE4tPR97tDdSIIBZvQT6hYJ8lsY26mVNqSmtfV8oJp6mLPqifB8fPuyfucyY+i8h5dAxFq0ZXaFL04=@vger.kernel.org X-Gm-Message-State: AOJu0Yw0VOUsibiOfQluxAbrbIKuews7in0C94e0lIcFinDFxCXeA30E LNwNzpdL/ifVLWixgCuOK+wTvOiFMOeQ5AAG/qgo7uQN1ZN3+TSePnqNOjt5FanK/cU= X-Gm-Gg: Acq92OHTO/5i5rPKkT5KTUIsxJZ5ZgoxmsbPXUML47ra/QuPJjfgoWQc+FUq9HgB5Kn gBOkQuNoPI7tI5rNwIhX/POX6flVcmSXBoFQsRprutJiZStNZAkZJQTg9OBeQP4iYa02yRRXg1a YKXH9oWWMoOxPNl7sO2aO/37gONWHdGJtWTqoBfoRDTOZcN4PWxlsjSbdaCroNAWdObfr8encJh Z1Xr8kK82Yw6wxwlnBMxP7OtYBhZLY14ZutUPqmL4AMlyfGY+nAeMRj4OVxqt10o6bx+bZsF4zx 5r8IsicxrrIrpSA4XNqE9oJAv8Uz3KbCR48I/XHv1yEtSIc9MmTXQnCmPM1+llyVY7rmMl6itTw gHE+hJAgkGRMHxy3tyOOAhvKo9sbCdxvZVfPIRmPzYtx1MbhYzM6k1QSeRyGNhhNVeL82AghQyG Jc6IeNftrdXHGTkM7fuFeQ+q+/UlDJ4a4m9bPdk9fZ69PACA== X-Received: by 2002:a05:600c:45c3:b0:48a:797f:24f8 with SMTP id 5b1f17b1804b1-48fe59ab0e7mr51093745e9.0.1778933371253; Sat, 16 May 2026 05:09:31 -0700 (PDT) Received: from localhost.localdomain ([185.139.138.68]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45da0a19c2dsm22242665f8f.21.2026.05.16.05.09.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 May 2026 05:09:30 -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 v2] lib/uuid_kunit: add tests for the four random UUID/GUID generators Date: Sat, 16 May 2026 17:09:15 +0500 Message-Id: <20260516120915.40544-1-sozdayvek@gmail.com> X-Mailer: git-send-email 2.33.0.windows.2 In-Reply-To: <20260514172241.28006-1-sozdayvek@gmail.com> References: <20260514172241.28006-1-sozdayvek@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Acked-by: Andy Shevchenko Signed-off-by: Stepan Ionichev --- 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/20260514172241.28006-1-sozdayvek@gmail.com/ --- lib/tests/uuid_kunit.c | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/lib/tests/uuid_kunit.c b/lib/tests/uuid_kunit.c index de71b2649..2ef64fbe6 100644 --- a/lib/tests/uuid_kunit.c +++ b/lib/tests/uuid_kunit.c @@ -86,11 +86,67 @@ static void uuid_test_uuid_invalid(struct kunit *test) } } =20 +/* + * 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 =3D 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 =3D 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 =3D 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 =3D 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[] =3D { 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), {}, }; =20 --=20 2.43.0