1e9ac2743SConrad Meyer /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3e9ac2743SConrad Meyer *
4e9ac2743SConrad Meyer * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org>
5e9ac2743SConrad Meyer * All rights reserved.
6e9ac2743SConrad Meyer *
7e9ac2743SConrad Meyer * Redistribution and use in source and binary forms, with or without
8e9ac2743SConrad Meyer * modification, are permitted provided that the following conditions
9e9ac2743SConrad Meyer * are met:
10e9ac2743SConrad Meyer * 1. Redistributions of source code must retain the above copyright
11e9ac2743SConrad Meyer * notice, this list of conditions and the following disclaimer.
12e9ac2743SConrad Meyer * 2. Redistributions in binary form must reproduce the above copyright
13e9ac2743SConrad Meyer * notice, this list of conditions and the following disclaimer in the
14e9ac2743SConrad Meyer * documentation and/or other materials provided with the distribution.
15e9ac2743SConrad Meyer *
16e9ac2743SConrad Meyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17e9ac2743SConrad Meyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18e9ac2743SConrad Meyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19e9ac2743SConrad Meyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20e9ac2743SConrad Meyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21e9ac2743SConrad Meyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22e9ac2743SConrad Meyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23e9ac2743SConrad Meyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24e9ac2743SConrad Meyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25e9ac2743SConrad Meyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26e9ac2743SConrad Meyer * SUCH DAMAGE.
27e9ac2743SConrad Meyer */
28e9ac2743SConrad Meyer
29e9ac2743SConrad Meyer #include <sys/param.h>
30e9ac2743SConrad Meyer #include <errno.h>
31*c5056a39SMark Johnston #include <limits.h>
3208a7e74cSConrad Meyer #include <signal.h>
33e9ac2743SConrad Meyer #include <unistd.h>
34e9ac2743SConrad Meyer
35e9ac2743SConrad Meyer #include <atf-c.h>
36e9ac2743SConrad Meyer
37e9ac2743SConrad Meyer ATF_TC_WITHOUT_HEAD(getentropy_count);
ATF_TC_BODY(getentropy_count,tc)38e9ac2743SConrad Meyer ATF_TC_BODY(getentropy_count, tc)
39e9ac2743SConrad Meyer {
40e9ac2743SConrad Meyer char buf[2];
41e9ac2743SConrad Meyer int ret;
42e9ac2743SConrad Meyer
43e9ac2743SConrad Meyer /* getentropy(2) does not modify buf past the requested length */
44e9ac2743SConrad Meyer buf[1] = 0x7C;
45e9ac2743SConrad Meyer ret = getentropy(buf, 1);
46e9ac2743SConrad Meyer ATF_REQUIRE_EQ(ret, 0);
47e9ac2743SConrad Meyer ATF_REQUIRE_EQ(buf[1], 0x7C);
48e9ac2743SConrad Meyer }
49e9ac2743SConrad Meyer
50e9ac2743SConrad Meyer ATF_TC_WITHOUT_HEAD(getentropy_fault);
ATF_TC_BODY(getentropy_fault,tc)51e9ac2743SConrad Meyer ATF_TC_BODY(getentropy_fault, tc)
52e9ac2743SConrad Meyer {
53e9ac2743SConrad Meyer int ret;
54e9ac2743SConrad Meyer
55e9ac2743SConrad Meyer ret = getentropy(NULL, 1);
56e9ac2743SConrad Meyer ATF_REQUIRE_EQ(ret, -1);
57e9ac2743SConrad Meyer ATF_REQUIRE_EQ(errno, EFAULT);
58e9ac2743SConrad Meyer }
59e9ac2743SConrad Meyer
60e9ac2743SConrad Meyer ATF_TC_WITHOUT_HEAD(getentropy_sizes);
ATF_TC_BODY(getentropy_sizes,tc)61e9ac2743SConrad Meyer ATF_TC_BODY(getentropy_sizes, tc)
62e9ac2743SConrad Meyer {
63e9ac2743SConrad Meyer char buf[512];
64e9ac2743SConrad Meyer
65e9ac2743SConrad Meyer ATF_REQUIRE_EQ(getentropy(buf, sizeof(buf)), -1);
66*c5056a39SMark Johnston ATF_REQUIRE_EQ(errno, EINVAL);
67*c5056a39SMark Johnston ATF_REQUIRE_EQ(getentropy(buf, GETENTROPY_MAX + 1), -1);
68*c5056a39SMark Johnston ATF_REQUIRE_EQ(errno, EINVAL);
69e9ac2743SConrad Meyer
70e9ac2743SConrad Meyer /* Smaller sizes always succeed: */
71*c5056a39SMark Johnston ATF_REQUIRE_EQ(getentropy(buf, GETENTROPY_MAX), 0);
72*c5056a39SMark Johnston ATF_REQUIRE_EQ(getentropy(buf, GETENTROPY_MAX / 2), 0);
73e9ac2743SConrad Meyer ATF_REQUIRE_EQ(getentropy(buf, 0), 0);
74e9ac2743SConrad Meyer }
75e9ac2743SConrad Meyer
ATF_TP_ADD_TCS(tp)76e9ac2743SConrad Meyer ATF_TP_ADD_TCS(tp)
77e9ac2743SConrad Meyer {
78e9ac2743SConrad Meyer
7908a7e74cSConrad Meyer signal(SIGSYS, SIG_IGN);
8008a7e74cSConrad Meyer
81e9ac2743SConrad Meyer ATF_TP_ADD_TC(tp, getentropy_count);
82e9ac2743SConrad Meyer ATF_TP_ADD_TC(tp, getentropy_fault);
83e9ac2743SConrad Meyer ATF_TP_ADD_TC(tp, getentropy_sizes);
84e9ac2743SConrad Meyer return (atf_no_error());
85e9ac2743SConrad Meyer }
86