Home | History | Annotate | Line # | Download | only in kernel
      1 /*	$NetBSD: t_rnd.c,v 1.13 2023/11/24 16:36:23 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __RCSID("$NetBSD: t_rnd.c,v 1.13 2023/11/24 16:36:23 riastradh Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/fcntl.h>
     34 #include <sys/ioctl.h>
     35 #include <sys/rndio.h>
     36 
     37 #include <atf-c.h>
     38 #include <unistd.h>
     39 
     40 #include <rump/rump.h>
     41 #include <rump/rump_syscalls.h>
     42 
     43 #include "h_macros.h"
     44 
     45 ATF_TC(RNDADDDATA);
     46 ATF_TC_HEAD(RNDADDDATA, tc)
     47 {
     48 	atf_tc_set_md_var(tc, "descr",
     49 	    "Checks ioctl(RNDADDDATA) (PR kern/42020)");
     50 }
     51 
     52 /* Adapted from example provided by Juho Salminen in the noted PR. */
     53 ATF_TC_BODY(RNDADDDATA, tc)
     54 {
     55 	rnddata_t rd;
     56 	int fd;
     57 
     58 	rump_init();
     59 	fd = rump_sys_open("/dev/random", O_RDWR, 0);
     60 	if (fd == -1)
     61 		atf_tc_fail_errno("cannot open /dev/random");
     62 
     63 	rd.entropy = 1;
     64 	rd.len = 1;
     65 	if (rump_sys_ioctl(fd, RNDADDDATA, &rd) == -1)
     66 		atf_tc_fail_errno("RNDADDDATA");
     67 }
     68 
     69 ATF_TC(RNDADDDATA2);
     70 ATF_TC_HEAD(RNDADDDATA2, tc)
     71 {
     72 	atf_tc_set_md_var(tc, "descr", "checks ioctl(RNDADDDATA) deals with "
     73 	    "garbage len field");
     74 }
     75 ATF_TC_BODY(RNDADDDATA2, tc)
     76 {
     77 	rnddata_t rd;
     78 	int fd;
     79 
     80 	rump_init();
     81 	fd = rump_sys_open("/dev/random", O_RDWR, 0);
     82 	if (fd == -1)
     83 		atf_tc_fail_errno("cannot open /dev/random");
     84 
     85 	rd.entropy = 1;
     86 	rd.len = -1;
     87 	ATF_REQUIRE_ERRNO(EINVAL, rump_sys_ioctl(fd, RNDADDDATA, &rd) == -1);
     88 }
     89 
     90 ATF_TC(read_random);
     91 ATF_TC_HEAD(read_random, tc)
     92 {
     93 	atf_tc_set_md_var(tc, "descr", "does reading /dev/random return "
     94 	    "within reasonable time");
     95 	atf_tc_set_md_var(tc, "timeout", "10");
     96 }
     97 
     98 ATF_TC_BODY(read_random, tc)
     99 {
    100 	char buf[128];
    101 	int fd;
    102 	unsigned i;
    103 
    104 	rump_init();
    105 	for (i = 0; i < 1000; i++) {
    106 		alarm(2);
    107 		RL(fd = rump_sys_open("/dev/random", RUMP_O_RDONLY));
    108 		RL(rump_sys_read(fd, buf, sizeof(buf)));
    109 		RL(rump_sys_close(fd));
    110 	}
    111 }
    112 
    113 ATF_TP_ADD_TCS(tp)
    114 {
    115 	ATF_TP_ADD_TC(tp, RNDADDDATA);
    116 	ATF_TP_ADD_TC(tp, RNDADDDATA2);
    117 	ATF_TP_ADD_TC(tp, read_random);
    118 
    119 	return atf_no_error();
    120 }
    121