From nobody Mon Dec 02 10:51:06 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Y20vG5bw0z5fYdV; Mon, 02 Dec 2024 10:51:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Y20vG43Fgz4SQP; Mon, 2 Dec 2024 10:51:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733136666; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NjgGl713Pp/23lwV1h2ZRjyBL7q7qrtF+E8FrSXKyHo=; b=ntLb9nHSuV1tDZndgHJMmJnI+TYXmxD7FqkIV6fGg1RPWXA3o2H6Hh0rnlW7B9I5DXV4NQ UrkAVHZAWPGi4X8AEmkNwSbT6ZG3hXSrOZEFvd/e3D7bhbNL0SOkA5xaj9g+83ydYEBlFN eWyE6wU71I2B3WT4LDq0XZlrR6lPksUM6MSvkP8ijvOLOortz8zW6yvwT72Ignid8Jw8X0 RuGrXfDZsJcxU1iBi4Xz4JEfAGZYH6hL5iyGaNrv/aMVj1pfHNCbl8WVQGHG40MhUya9cQ 0ZUI61s3j/4IOJD/c3o8AR3WRg+I1r5fxMDMTBigqKcHHVofgRW47yw7ti2YPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733136666; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NjgGl713Pp/23lwV1h2ZRjyBL7q7qrtF+E8FrSXKyHo=; b=IZJQK+uQtDX8/FuU/uE4x75aecpXQ9dVNjmnr79w4BvxfSqrrr+R0REYN6LX49j2wA8iCC vHrVYhhCm0ZWRbBlgJ1q/ZZRBFc9Oa8OkIX+dDG3a3nBis/r/WyF0vB2G0tmxdXiBkcZcs nsHsOW+7BnSft+6EcjxDRgvs5AlmKu4v/ypJxbDAecPp4AGb7nlzAGOb6841Apg3PpNfoo 4Ee+ty1elKh3muyottUKhSMoeYmVNZP9C7TUZKVdk6nH98Csw26yAzsrdsVVNUoBYTBlsD YiksKHszNjBJdfWXLnxzrHLuA1pySiRvXVfdvw30SmkL8bVJ2XphmvjWAqtTrg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1733136666; a=rsa-sha256; cv=none; b=c62IizAj7r1agQ2Dg8Dc5+kPT5dO1wQp3TJ0a/LxuOzJJCrYDPNaO/36/0xg7yqanewsqu EIWygUjmmnobUmMeyMuAh+FaTz3b70HRm3feZz0/tY9ifws+/RbZLTZ3jNb+4u35RQwQM0 FUEc12SG3UHtx7HflI3uVLRLHhb71ycLzQNXpkjQzN2eGi8RT6Xh8XoIaoyoJF8tbYuF3D st8mw3DdQei0DUfQqq5X7F/iD2gpx/SkZCbXGKI+uef86HJ0jTWJ3VllPetNqtpQw3WTH5 O3Zu5UIAdtYHuZc8qN6UVRXeso8E0TIaUAFbIxCWAJAKn/w6WatDGsKdGungYA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Y20vG3DW4zZTy; Mon, 2 Dec 2024 10:51:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4B2Ap6iQ044007; Mon, 2 Dec 2024 10:51:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4B2Ap6rn044004; Mon, 2 Dec 2024 10:51:06 GMT (envelope-from git) Date: Mon, 2 Dec 2024 10:51:06 GMT Message-Id: <202412021051.4B2Ap6rn044004@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: 1730b5c7916e - main - lib/libc/tests: add unit test for arc4random_uniform() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1730b5c7916ed249ddc97ebe51117899f9323fbc Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=1730b5c7916ed249ddc97ebe51117899f9323fbc commit 1730b5c7916ed249ddc97ebe51117899f9323fbc Author: Robert Clausecker AuthorDate: 2024-11-18 12:46:16 +0000 Commit: Robert Clausecker CommitDate: 2024-12-02 10:41:11 +0000 lib/libc/tests: add unit test for arc4random_uniform() The new unit test validates that the range reduction works correctly. We do not currently validate that there is no bias as that would take too much time and memory for a unit test. Reviewed by: cem Approved by: emaste Differential Revision: https://reviews.freebsd.org/D47659 --- lib/libc/tests/gen/arc4random_test.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/libc/tests/gen/arc4random_test.c b/lib/libc/tests/gen/arc4random_test.c index e96ebdc6c6ca..79139f29f8fa 100644 --- a/lib/libc/tests/gen/arc4random_test.c +++ b/lib/libc/tests/gen/arc4random_test.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 David Schultz + * Copyright (c) 2024 Robert Clausecker * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -77,10 +79,34 @@ ATF_TC_BODY(test_arc4random, tc) "sequences are the same"); } +/* + * Test whether arc4random_uniform() returns a number below the given threshold. + * Test with various thresholds. + */ +ATF_TC_WITHOUT_HEAD(test_arc4random_uniform); +ATF_TC_BODY(test_arc4random_uniform, tc) +{ + size_t i, j; + static const uint32_t thresholds[] = { + 1, 2, 3, 4, 5, 10, 100, 1000, + INT32_MAX, (uint32_t)INT32_MAX + 1, + UINT32_MAX - 1000000000, UINT32_MAX - 1000000, UINT32_MAX - 1, 0 + }; + + for (i = 0; thresholds[i] != 0; i++) + for (j = 0; j < 10000; j++) + ATF_CHECK(arc4random_uniform(thresholds[i]) < thresholds[i]); + + /* for a threshold of zero, just check that we get zero every time */ + for (j = 0; j < 1000; j++) + ATF_CHECK_EQ(0, arc4random_uniform(0)); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, test_arc4random); + ATF_TP_ADD_TC(tp, test_arc4random_uniform); return (atf_no_error()); }