1227522Sdas/*-
2227522Sdas * Copyright (c) 2011 David Schultz
3227522Sdas * All rights reserved.
4227522Sdas *
5227522Sdas * Redistribution and use in source and binary forms, with or without
6227522Sdas * modification, are permitted provided that the following conditions
7227522Sdas * are met:
8227522Sdas * 1. Redistributions of source code must retain the above copyright
9227522Sdas *    notice, this list of conditions and the following disclaimer.
10227522Sdas * 2. Redistributions in binary form must reproduce the above copyright
11227522Sdas *    notice, this list of conditions and the following disclaimer in the
12227522Sdas *    documentation and/or other materials provided with the distribution.
13227522Sdas *
14227522Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15227522Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16227522Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17227522Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18227522Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19227522Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20227522Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21227522Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22227522Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23227522Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24227522Sdas * SUCH DAMAGE.
25227522Sdas */
26227522Sdas
27227522Sdas#include <sys/cdefs.h>
28227522Sdas__FBSDID("$FreeBSD: releng/10.2/lib/libc/tests/gen/arc4random_test.c 277441 2015-01-20 21:42:40Z ngie $");
29227522Sdas
30274581Sngie#include <sys/types.h>
31227522Sdas#include <sys/mman.h>
32227522Sdas#include <sys/wait.h>
33227522Sdas#include <stdio.h>
34227522Sdas#include <stdlib.h>
35227522Sdas#include <string.h>
36227522Sdas#include <unistd.h>
37274581Sngie#include <atf-c.h>
38227522Sdas
39227522Sdas/*
40227522Sdas * BUFSIZE is the number of bytes of rc4 output to compare.  The probability
41227522Sdas * that this test fails spuriously is 2**(-BUFSIZE * 8).
42227522Sdas */
43227522Sdas#define	BUFSIZE		8
44227522Sdas
45227522Sdas/*
46227522Sdas * Test whether arc4random_buf() returns the same sequence of bytes in both
47227522Sdas * parent and child processes.  (Hint: It shouldn't.)
48227522Sdas */
49274581SngieATF_TC_WITHOUT_HEAD(test_arc4random);
50274581SngieATF_TC_BODY(test_arc4random, tc)
51274581Sngie{
52227522Sdas	struct shared_page {
53227522Sdas		char parentbuf[BUFSIZE];
54227522Sdas		char childbuf[BUFSIZE];
55227522Sdas	} *page;
56227522Sdas	pid_t pid;
57227522Sdas	char c;
58227522Sdas
59227522Sdas	printf("1..1\n");
60227522Sdas
61227522Sdas	page = mmap(NULL, sizeof(struct shared_page), PROT_READ | PROT_WRITE,
62227522Sdas		    MAP_ANON | MAP_SHARED, -1, 0);
63227522Sdas	if (page == MAP_FAILED) {
64227522Sdas		printf("fail 1 - mmap\n");
65227522Sdas		exit(1);
66227522Sdas	}
67227522Sdas
68227522Sdas	arc4random_buf(&c, 1);
69227522Sdas
70227522Sdas	pid = fork();
71274581Sngie	ATF_REQUIRE(0 <= pid);
72227522Sdas	if (pid == 0) {
73227522Sdas		/* child */
74227522Sdas		arc4random_buf(page->childbuf, BUFSIZE);
75227522Sdas		exit(0);
76227522Sdas	} else {
77227522Sdas		/* parent */
78227522Sdas		int status;
79227522Sdas		arc4random_buf(page->parentbuf, BUFSIZE);
80227522Sdas		wait(&status);
81227522Sdas	}
82274581Sngie	ATF_CHECK_MSG(memcmp(page->parentbuf, page->childbuf, BUFSIZE) != 0,
83274581Sngie	    "sequences are the same");
84274581Sngie}
85227522Sdas
86274581SngieATF_TP_ADD_TCS(tp)
87274581Sngie{
88274581Sngie
89274581Sngie	ATF_TP_ADD_TC(tp, test_arc4random);
90274581Sngie
91274581Sngie	return (atf_no_error());
92227522Sdas}
93