xref: /minix3/tests/kernel/t_rnd.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: t_rnd.c,v 1.6 2015/04/13 22:24:34 riastradh Exp $	*/
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*
411be35a1SLionel Sambuc  * Copyright (c) 2009 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc  * are met:
1011be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc  *
1611be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1711be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1811be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1911be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2011be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2111be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2211be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2311be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2411be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2511be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2611be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
2711be35a1SLionel Sambuc  */
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc #include <sys/cdefs.h>
30*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_rnd.c,v 1.6 2015/04/13 22:24:34 riastradh Exp $");
3111be35a1SLionel Sambuc 
3211be35a1SLionel Sambuc #include <sys/types.h>
3311be35a1SLionel Sambuc #include <sys/fcntl.h>
3411be35a1SLionel Sambuc #include <sys/ioctl.h>
35*0a6a1f1dSLionel Sambuc #include <sys/rndio.h>
3611be35a1SLionel Sambuc 
3711be35a1SLionel Sambuc #include <atf-c.h>
3811be35a1SLionel Sambuc 
3911be35a1SLionel Sambuc #include <rump/rump.h>
4011be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
4111be35a1SLionel Sambuc 
4211be35a1SLionel Sambuc #include "../h_macros.h"
4311be35a1SLionel Sambuc 
4411be35a1SLionel Sambuc ATF_TC(RNDADDDATA);
ATF_TC_HEAD(RNDADDDATA,tc)4511be35a1SLionel Sambuc ATF_TC_HEAD(RNDADDDATA, tc)
4611be35a1SLionel Sambuc {
4711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
4811be35a1SLionel Sambuc 	    "Checks ioctl(RNDADDDATA) (PR kern/42020)");
4911be35a1SLionel Sambuc }
5011be35a1SLionel Sambuc 
5111be35a1SLionel Sambuc /* Adapted from example provided by Juho Salminen in the noted PR. */
ATF_TC_BODY(RNDADDDATA,tc)5211be35a1SLionel Sambuc ATF_TC_BODY(RNDADDDATA, tc)
5311be35a1SLionel Sambuc {
5411be35a1SLionel Sambuc 	rnddata_t rd;
5511be35a1SLionel Sambuc 	int fd;
5611be35a1SLionel Sambuc 
5711be35a1SLionel Sambuc 	rump_init();
5811be35a1SLionel Sambuc 	fd = rump_sys_open("/dev/random", O_RDWR, 0);
5911be35a1SLionel Sambuc 	if (fd == -1)
6011be35a1SLionel Sambuc 		atf_tc_fail_errno("cannot open /dev/random");
6111be35a1SLionel Sambuc 
6211be35a1SLionel Sambuc 	rd.entropy = 1;
6311be35a1SLionel Sambuc 	rd.len = 1;
6411be35a1SLionel Sambuc 	if (rump_sys_ioctl(fd, RNDADDDATA, &rd) == -1)
6511be35a1SLionel Sambuc 		atf_tc_fail_errno("RNDADDDATA");
6611be35a1SLionel Sambuc }
6711be35a1SLionel Sambuc 
6811be35a1SLionel Sambuc ATF_TC(RNDADDDATA2);
ATF_TC_HEAD(RNDADDDATA2,tc)6911be35a1SLionel Sambuc ATF_TC_HEAD(RNDADDDATA2, tc)
7011be35a1SLionel Sambuc {
7111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "checks ioctl(RNDADDDATA) deals with "
7211be35a1SLionel Sambuc 	    "garbage len field");
7311be35a1SLionel Sambuc }
ATF_TC_BODY(RNDADDDATA2,tc)7411be35a1SLionel Sambuc ATF_TC_BODY(RNDADDDATA2, tc)
7511be35a1SLionel Sambuc {
7611be35a1SLionel Sambuc 	rnddata_t rd;
7711be35a1SLionel Sambuc 	int fd;
7811be35a1SLionel Sambuc 
7911be35a1SLionel Sambuc 	rump_init();
8011be35a1SLionel Sambuc 	fd = rump_sys_open("/dev/random", O_RDWR, 0);
8111be35a1SLionel Sambuc 	if (fd == -1)
8211be35a1SLionel Sambuc 		atf_tc_fail_errno("cannot open /dev/random");
8311be35a1SLionel Sambuc 
8411be35a1SLionel Sambuc 	rd.entropy = 1;
8511be35a1SLionel Sambuc 	rd.len = -1;
8611be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EINVAL, rump_sys_ioctl(fd, RNDADDDATA, &rd) == -1);
8711be35a1SLionel Sambuc }
8811be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)8911be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
9011be35a1SLionel Sambuc {
9111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, RNDADDDATA);
9211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, RNDADDDATA2);
9311be35a1SLionel Sambuc 
9411be35a1SLionel Sambuc 	return atf_no_error();
9511be35a1SLionel Sambuc }
96