arc4random_test.c revision 302408
1275072Semaste/*-
2275072Semaste * Copyright (c) 2011 David Schultz
3275072Semaste * All rights reserved.
4275072Semaste *
5275072Semaste * Redistribution and use in source and binary forms, with or without
6275072Semaste * modification, are permitted provided that the following conditions
7275072Semaste * are met:
8275072Semaste * 1. Redistributions of source code must retain the above copyright
9275072Semaste *    notice, this list of conditions and the following disclaimer.
10275072Semaste * 2. Redistributions in binary form must reproduce the above copyright
11275072Semaste *    notice, this list of conditions and the following disclaimer in the
12275072Semaste *    documentation and/or other materials provided with the distribution.
13275072Semaste *
14280031Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15280031Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16280031Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17280031Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18280031Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19275072Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20296417Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21275072Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22275072Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23275072Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24275072Semaste * SUCH DAMAGE.
25280031Sdim */
26280031Sdim
27280031Sdim#include <sys/cdefs.h>
28280031Sdim__FBSDID("$FreeBSD: stable/11/lib/libc/tests/gen/arc4random_test.c 290571 2015-11-09 06:16:38Z ngie $");
29280031Sdim
30275072Semaste#include <sys/types.h>
31296417Sdim#include <sys/mman.h>
32275072Semaste#include <sys/wait.h>
33275072Semaste#include <errno.h>
34275072Semaste#include <stdio.h>
35275072Semaste#include <stdlib.h>
36280031Sdim#include <string.h>
37280031Sdim#include <unistd.h>
38280031Sdim
39280031Sdim#include <atf-c.h>
40280031Sdim
41275072Semaste/*
42280031Sdim * BUFSIZE is the number of bytes of rc4 output to compare.  The probability
43296417Sdim * that this test fails spuriously is 2**(-BUFSIZE * 8).
44275072Semaste */
45280031Sdim#define	BUFSIZE		8
46275072Semaste
47275072Semaste/*
48275072Semaste * Test whether arc4random_buf() returns the same sequence of bytes in both
49280031Sdim * parent and child processes.  (Hint: It shouldn't.)
50280031Sdim */
51280031SdimATF_TC_WITHOUT_HEAD(test_arc4random);
52280031SdimATF_TC_BODY(test_arc4random, tc)
53280031Sdim{
54280031Sdim	struct shared_page {
55275072Semaste		char parentbuf[BUFSIZE];
56280031Sdim		char childbuf[BUFSIZE];
57280031Sdim	} *page;
58275072Semaste	pid_t pid;
59280031Sdim	char c;
60280031Sdim
61280031Sdim	page = mmap(NULL, sizeof(struct shared_page), PROT_READ | PROT_WRITE,
62280031Sdim		    MAP_ANON | MAP_SHARED, -1, 0);
63280031Sdim	ATF_REQUIRE_MSG(page != MAP_FAILED, "mmap failed; errno=%d", errno);
64275072Semaste
65275072Semaste	arc4random_buf(&c, 1);
66275072Semaste
67280031Sdim	pid = fork();
68280031Sdim	ATF_REQUIRE(0 <= pid);
69280031Sdim	if (pid == 0) {
70280031Sdim		/* child */
71280031Sdim		arc4random_buf(page->childbuf, BUFSIZE);
72275072Semaste		exit(0);
73280031Sdim	} else {
74296417Sdim		/* parent */
75275072Semaste		int status;
76280031Sdim		arc4random_buf(page->parentbuf, BUFSIZE);
77275072Semaste		wait(&status);
78275072Semaste	}
79275072Semaste	ATF_CHECK_MSG(memcmp(page->parentbuf, page->childbuf, BUFSIZE) != 0,
80280031Sdim	    "sequences are the same");
81280031Sdim}
82280031Sdim
83280031SdimATF_TP_ADD_TCS(tp)
84280031Sdim{
85280031Sdim
86275072Semaste	ATF_TP_ADD_TC(tp, test_arc4random);
87280031Sdim
88280031Sdim	return (atf_no_error());
89275072Semaste}
90280031Sdim