1108088Stjr/*- 2108088Stjr * Copyright (c) 2002 Tim J. Robbins 3108088Stjr * All rights reserved. 4108088Stjr * 5108088Stjr * Redistribution and use in source and binary forms, with or without 6108088Stjr * modification, are permitted provided that the following conditions 7108088Stjr * are met: 8108088Stjr * 1. Redistributions of source code must retain the above copyright 9108088Stjr * notice, this list of conditions and the following disclaimer. 10108088Stjr * 2. Redistributions in binary form must reproduce the above copyright 11108088Stjr * notice, this list of conditions and the following disclaimer in the 12108088Stjr * documentation and/or other materials provided with the distribution. 13108088Stjr * 14108088Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15108088Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16108088Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17108088Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18108088Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19108088Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20108088Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21108088Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22108088Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23108088Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24108088Stjr * SUCH DAMAGE. 25108088Stjr */ 26108088Stjr 27108088Stjr/* 28108088Stjr * Test program for perror() as specified by IEEE Std. 1003.1-2001 and 29108088Stjr * ISO/IEC 9899:1999. 30108088Stjr */ 31108088Stjr 32108088Stjr#include <sys/cdefs.h> 33108088Stjr__FBSDID("$FreeBSD$"); 34108088Stjr 35108088Stjr#include <err.h> 36108088Stjr#include <errno.h> 37108088Stjr#include <limits.h> 38108088Stjr#include <paths.h> 39108088Stjr#include <stdio.h> 40108088Stjr#include <stdlib.h> 41108088Stjr#include <string.h> 42244038Sjilles#include <unistd.h> 43108088Stjr 44290537Sngie#include <atf-c.h> 45290537Sngie 46108088Stjrstatic char tmpfil[PATH_MAX]; 47108088Stjr 48290537SngieATF_TC_WITHOUT_HEAD(perror_test); 49290537SngieATF_TC_BODY(perror_test, tc) 50108088Stjr{ 51291870Sngie char lbuf[512]; 52108088Stjr int i; 53108088Stjr char *s; 54108088Stjr 55290537Sngie strcpy(tmpfil, "perror.XXXXXXXX"); 56290537Sngie ATF_REQUIRE(mkstemp(tmpfil) >= 0); 57108088Stjr /* Reopen stderr on a file descriptor other than 2. */ 58108088Stjr fclose(stderr); 59108088Stjr for (i = 0; i < 3; i++) 60108088Stjr dup(0); 61290537Sngie ATF_REQUIRE(freopen(tmpfil, "r+", stderr) != NULL); 62108088Stjr 63108088Stjr /* 64108088Stjr * Test that perror() doesn't call strerror() (4.4BSD bug), 65108088Stjr * the two ways of omitting a program name, and the formatting when 66108088Stjr * a program name is specified. 67108088Stjr */ 68108088Stjr s = strerror(ENOENT); 69290537Sngie ATF_REQUIRE_MSG(strcmp(s, "No such file or directory") == 0, 70290537Sngie "message obtained was: %s", s); 71108088Stjr errno = EPERM; 72108088Stjr perror(NULL); 73108088Stjr perror(""); 74290537Sngie perror("perror_test"); 75290537Sngie ATF_REQUIRE_MSG(strcmp(s, "No such file or directory") == 0, 76290537Sngie "message obtained was: %s", s); 77108088Stjr 78108088Stjr /* 79108088Stjr * Read it back to check... 80108088Stjr */ 81108088Stjr rewind(stderr); 82108088Stjr s = fgets(lbuf, sizeof(lbuf), stderr); 83290537Sngie ATF_REQUIRE(s != NULL); 84290537Sngie ATF_REQUIRE_MSG(strcmp(s, "Operation not permitted\n") == 0, 85290537Sngie "message obtained was: %s", s); 86108088Stjr s = fgets(lbuf, sizeof(lbuf), stderr); 87290537Sngie ATF_REQUIRE(s != NULL); 88290537Sngie ATF_REQUIRE_MSG(strcmp(s, "Operation not permitted\n") == 0, 89290537Sngie "message obtained was: %s", s); 90108088Stjr s = fgets(lbuf, sizeof(lbuf), stderr); 91290537Sngie ATF_REQUIRE(s != NULL); 92290537Sngie ATF_REQUIRE_MSG( 93290537Sngie strcmp(s, "perror_test: Operation not permitted\n") == 0, 94290537Sngie "message obtained was: %s", s); 95108088Stjr s = fgets(lbuf, sizeof(lbuf), stderr); 96290537Sngie ATF_REQUIRE(s == NULL); 97108088Stjr fclose(stderr); 98108088Stjr 99108088Stjr} 100108088Stjr 101290537SngieATF_TP_ADD_TCS(tp) 102108088Stjr{ 103108088Stjr 104290537Sngie ATF_TP_ADD_TC(tp, perror_test); 105290537Sngie 106290537Sngie return (atf_no_error()); 107108088Stjr} 108