Path: blob/main/lib/libc/tests/gen/getentropy_test.c
39530 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2018 Conrad Meyer <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <errno.h>30#include <limits.h>31#include <signal.h>32#include <unistd.h>3334#include <atf-c.h>3536ATF_TC_WITHOUT_HEAD(getentropy_count);37ATF_TC_BODY(getentropy_count, tc)38{39char buf[2];40int ret;4142/* getentropy(2) does not modify buf past the requested length */43buf[1] = 0x7C;44ret = getentropy(buf, 1);45ATF_REQUIRE_EQ(ret, 0);46ATF_REQUIRE_EQ(buf[1], 0x7C);47}4849ATF_TC_WITHOUT_HEAD(getentropy_fault);50ATF_TC_BODY(getentropy_fault, tc)51{52int ret;5354ret = getentropy(NULL, 1);55ATF_REQUIRE_EQ(ret, -1);56ATF_REQUIRE_EQ(errno, EFAULT);57}5859ATF_TC_WITHOUT_HEAD(getentropy_sizes);60ATF_TC_BODY(getentropy_sizes, tc)61{62char buf[512];6364ATF_REQUIRE_EQ(getentropy(buf, sizeof(buf)), -1);65ATF_REQUIRE_EQ(errno, EINVAL);66ATF_REQUIRE_EQ(getentropy(buf, GETENTROPY_MAX + 1), -1);67ATF_REQUIRE_EQ(errno, EINVAL);6869/* Smaller sizes always succeed: */70ATF_REQUIRE_EQ(getentropy(buf, GETENTROPY_MAX), 0);71ATF_REQUIRE_EQ(getentropy(buf, GETENTROPY_MAX / 2), 0);72ATF_REQUIRE_EQ(getentropy(buf, 0), 0);73}7475ATF_TP_ADD_TCS(tp)76{7778signal(SIGSYS, SIG_IGN);7980ATF_TP_ADD_TC(tp, getentropy_count);81ATF_TP_ADD_TC(tp, getentropy_fault);82ATF_TP_ADD_TC(tp, getentropy_sizes);83return (atf_no_error());84}858687