perror_test.c revision 291870
159290Sjlemon/*- 272969Sjlemon * Copyright (c) 2002 Tim J. Robbins 359290Sjlemon * All rights reserved. 459290Sjlemon * 559290Sjlemon * Redistribution and use in source and binary forms, with or without 659290Sjlemon * modification, are permitted provided that the following conditions 759290Sjlemon * are met: 859290Sjlemon * 1. Redistributions of source code must retain the above copyright 959290Sjlemon * notice, this list of conditions and the following disclaimer. 1059290Sjlemon * 2. Redistributions in binary form must reproduce the above copyright 1159290Sjlemon * notice, this list of conditions and the following disclaimer in the 1259290Sjlemon * documentation and/or other materials provided with the distribution. 1359290Sjlemon * 1459290Sjlemon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1559290Sjlemon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1659290Sjlemon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1759290Sjlemon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1859290Sjlemon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1959290Sjlemon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2059290Sjlemon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2159290Sjlemon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2259290Sjlemon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2359290Sjlemon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2459290Sjlemon * SUCH DAMAGE. 2559290Sjlemon */ 2699090Sbde 2759290Sjlemon/* 2859290Sjlemon * Test program for perror() as specified by IEEE Std. 1003.1-2001 and 2959290Sjlemon * ISO/IEC 9899:1999. 3059290Sjlemon */ 3159290Sjlemon 3259290Sjlemon#include <sys/cdefs.h> 3359290Sjlemon__FBSDID("$FreeBSD: stable/10/lib/libc/tests/stdio/perror_test.c 291870 2015-12-05 21:49:35Z ngie $"); 3459290Sjlemon 3559290Sjlemon#include <err.h> 3659290Sjlemon#include <errno.h> 3759290Sjlemon#include <limits.h> 3879989Sjlemon#include <paths.h> 3984139Sjlemon#include <stdio.h> 40131562Salfred#include <stdlib.h> 4159290Sjlemon#include <string.h> 42131562Salfred#include <unistd.h> 4359290Sjlemon 44110241Snectar#include <atf-c.h> 45110241Snectar 4672969Sjlemonstatic char tmpfil[PATH_MAX]; 4772969Sjlemon 4872969SjlemonATF_TC_WITHOUT_HEAD(perror_test); 4972969SjlemonATF_TC_BODY(perror_test, tc) 5072969Sjlemon{ 5172969Sjlemon char lbuf[512]; 5272969Sjlemon int i; 5372969Sjlemon char *s; 5459290Sjlemon 5559309Sjlemon strcpy(tmpfil, "perror.XXXXXXXX"); 5659290Sjlemon ATF_REQUIRE(mkstemp(tmpfil) >= 0); 5759290Sjlemon /* Reopen stderr on a file descriptor other than 2. */ 5859290Sjlemon fclose(stderr); 5959309Sjlemon for (i = 0; i < 3; i++) 6059290Sjlemon dup(0); 6159290Sjlemon ATF_REQUIRE(freopen(tmpfil, "r+", stderr) != NULL); 6259290Sjlemon 6359290Sjlemon /* 6459290Sjlemon * Test that perror() doesn't call strerror() (4.4BSD bug), 6559290Sjlemon * the two ways of omitting a program name, and the formatting when 6659290Sjlemon * a program name is specified. 6759290Sjlemon */ 6859290Sjlemon s = strerror(ENOENT); 6959290Sjlemon ATF_REQUIRE_MSG(strcmp(s, "No such file or directory") == 0, 7059290Sjlemon "message obtained was: %s", s); 7159290Sjlemon errno = EPERM; 7259290Sjlemon perror(NULL); 7359290Sjlemon perror(""); 7459290Sjlemon perror("perror_test"); 7559290Sjlemon ATF_REQUIRE_MSG(strcmp(s, "No such file or directory") == 0, 7659290Sjlemon "message obtained was: %s", s); 7759290Sjlemon 7859290Sjlemon /* 7959290Sjlemon * Read it back to check... 8059290Sjlemon */ 8172968Sjlemon rewind(stderr); 8272968Sjlemon s = fgets(lbuf, sizeof(lbuf), stderr); 8372968Sjlemon ATF_REQUIRE(s != NULL); 8472968Sjlemon ATF_REQUIRE_MSG(strcmp(s, "Operation not permitted\n") == 0, 8572968Sjlemon "message obtained was: %s", s); 8659290Sjlemon s = fgets(lbuf, sizeof(lbuf), stderr); 8759290Sjlemon ATF_REQUIRE(s != NULL); 8859290Sjlemon ATF_REQUIRE_MSG(strcmp(s, "Operation not permitted\n") == 0, 8959290Sjlemon "message obtained was: %s", s); 9059290Sjlemon s = fgets(lbuf, sizeof(lbuf), stderr); 9159290Sjlemon ATF_REQUIRE(s != NULL); 9259290Sjlemon ATF_REQUIRE_MSG( 9359290Sjlemon strcmp(s, "perror_test: Operation not permitted\n") == 0, 9472956Sjlemon "message obtained was: %s", s); 9559290Sjlemon s = fgets(lbuf, sizeof(lbuf), stderr); 9659290Sjlemon ATF_REQUIRE(s == NULL); 9759290Sjlemon fclose(stderr); 9859290Sjlemon 9959290Sjlemon} 10059290Sjlemon 10159290SjlemonATF_TP_ADD_TCS(tp) 10259290Sjlemon{ 10359290Sjlemon 10459290Sjlemon ATF_TP_ADD_TC(tp, perror_test); 10559290Sjlemon 10659290Sjlemon return (atf_no_error()); 10759290Sjlemon} 10859290Sjlemon