Path: blob/main/lib/libc/tests/gen/arc4random_test.c
39500 views
/*-1* Copyright (c) 2011 David Schultz2* Copyright (c) 2024 Robert Clausecker <[email protected]>3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/types.h>28#include <sys/mman.h>29#include <sys/wait.h>30#include <errno.h>31#include <stdio.h>32#include <stdint.h>33#include <stdlib.h>34#include <string.h>35#include <unistd.h>3637#include <atf-c.h>3839/*40* BUFSIZE is the number of bytes of rc4 output to compare. The probability41* that this test fails spuriously is 2**(-BUFSIZE * 8).42*/43#define BUFSIZE 84445/*46* Test whether arc4random_buf() returns the same sequence of bytes in both47* parent and child processes. (Hint: It shouldn't.)48*/49ATF_TC_WITHOUT_HEAD(test_arc4random);50ATF_TC_BODY(test_arc4random, tc)51{52struct shared_page {53char parentbuf[BUFSIZE];54char childbuf[BUFSIZE];55} *page;56pid_t pid;57char c;5859page = mmap(NULL, sizeof(struct shared_page), PROT_READ | PROT_WRITE,60MAP_ANON | MAP_SHARED, -1, 0);61ATF_REQUIRE_MSG(page != MAP_FAILED, "mmap failed; errno=%d", errno);6263arc4random_buf(&c, 1);6465pid = fork();66ATF_REQUIRE(0 <= pid);67if (pid == 0) {68/* child */69arc4random_buf(page->childbuf, BUFSIZE);70exit(0);71} else {72/* parent */73int status;74arc4random_buf(page->parentbuf, BUFSIZE);75wait(&status);76}77ATF_CHECK_MSG(memcmp(page->parentbuf, page->childbuf, BUFSIZE) != 0,78"sequences are the same");79}8081/*82* Test whether arc4random_uniform() returns a number below the given threshold.83* Test with various thresholds.84*/85ATF_TC_WITHOUT_HEAD(test_arc4random_uniform);86ATF_TC_BODY(test_arc4random_uniform, tc)87{88size_t i, j;89static const uint32_t thresholds[] = {901, 2, 3, 4, 5, 10, 100, 1000,91INT32_MAX, (uint32_t)INT32_MAX + 1,92UINT32_MAX - 1000000000, UINT32_MAX - 1000000, UINT32_MAX - 1, 093};9495for (i = 0; thresholds[i] != 0; i++)96for (j = 0; j < 10000; j++)97ATF_CHECK(arc4random_uniform(thresholds[i]) < thresholds[i]);9899/* for a threshold of zero, just check that we get zero every time */100for (j = 0; j < 1000; j++)101ATF_CHECK_EQ(0, arc4random_uniform(0));102}103104ATF_TP_ADD_TCS(tp)105{106107ATF_TP_ADD_TC(tp, test_arc4random);108ATF_TP_ADD_TC(tp, test_arc4random_uniform);109110return (atf_no_error());111}112113114