Path: blob/main/lib/libc/tests/stdio/perror_test.c
39530 views
/*-1* Copyright (c) 2002 Tim J. Robbins2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Test program for perror() as specified by IEEE Std. 1003.1-2001 and28* ISO/IEC 9899:1999.29*/3031#include <err.h>32#include <errno.h>33#include <limits.h>34#include <paths.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>3940#include <atf-c.h>4142static char tmpfil[PATH_MAX];4344ATF_TC_WITHOUT_HEAD(perror_test);45ATF_TC_BODY(perror_test, tc)46{47char lbuf[512];48int i;49char *s;5051strcpy(tmpfil, "perror.XXXXXXXX");52ATF_REQUIRE(mkstemp(tmpfil) >= 0);53/* Reopen stderr on a file descriptor other than 2. */54fclose(stderr);55for (i = 0; i < 3; i++)56dup(0);57ATF_REQUIRE(freopen(tmpfil, "r+", stderr) != NULL);5859/*60* Test that perror() doesn't call strerror() (4.4BSD bug),61* the two ways of omitting a program name, and the formatting when62* a program name is specified.63*/64s = strerror(ENOENT);65ATF_REQUIRE_MSG(strcmp(s, "No such file or directory") == 0,66"message obtained was: %s", s);67errno = EPERM;68perror(NULL);69perror("");70perror("perror_test");71ATF_REQUIRE_MSG(strcmp(s, "No such file or directory") == 0,72"message obtained was: %s", s);7374/*75* Read it back to check...76*/77rewind(stderr);78s = fgets(lbuf, sizeof(lbuf), stderr);79ATF_REQUIRE(s != NULL);80ATF_REQUIRE_MSG(strcmp(s, "Operation not permitted\n") == 0,81"message obtained was: %s", s);82s = fgets(lbuf, sizeof(lbuf), stderr);83ATF_REQUIRE(s != NULL);84ATF_REQUIRE_MSG(strcmp(s, "Operation not permitted\n") == 0,85"message obtained was: %s", s);86s = fgets(lbuf, sizeof(lbuf), stderr);87ATF_REQUIRE(s != NULL);88ATF_REQUIRE_MSG(89strcmp(s, "perror_test: Operation not permitted\n") == 0,90"message obtained was: %s", s);91s = fgets(lbuf, sizeof(lbuf), stderr);92ATF_REQUIRE(s == NULL);93fclose(stderr);9495}9697ATF_TP_ADD_TCS(tp)98{99100ATF_TP_ADD_TC(tp, perror_test);101102return (atf_no_error());103}104105106