xref: /freebsd/lib/libc/tests/stdio/perror_test.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
11ee02192SEnji Cooper /*-
21ee02192SEnji Cooper  * Copyright (c) 2002 Tim J. Robbins
31ee02192SEnji Cooper  * All rights reserved.
41ee02192SEnji Cooper  *
51ee02192SEnji Cooper  * Redistribution and use in source and binary forms, with or without
61ee02192SEnji Cooper  * modification, are permitted provided that the following conditions
71ee02192SEnji Cooper  * are met:
81ee02192SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
91ee02192SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
101ee02192SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
111ee02192SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
121ee02192SEnji Cooper  *    documentation and/or other materials provided with the distribution.
131ee02192SEnji Cooper  *
141ee02192SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151ee02192SEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161ee02192SEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171ee02192SEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181ee02192SEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191ee02192SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201ee02192SEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211ee02192SEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221ee02192SEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231ee02192SEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241ee02192SEnji Cooper  * SUCH DAMAGE.
251ee02192SEnji Cooper  */
261ee02192SEnji Cooper 
271ee02192SEnji Cooper /*
281ee02192SEnji Cooper  * Test program for perror() as specified by IEEE Std. 1003.1-2001 and
291ee02192SEnji Cooper  * ISO/IEC 9899:1999.
301ee02192SEnji Cooper  */
311ee02192SEnji Cooper 
321ee02192SEnji Cooper #include <err.h>
331ee02192SEnji Cooper #include <errno.h>
341ee02192SEnji Cooper #include <limits.h>
351ee02192SEnji Cooper #include <paths.h>
361ee02192SEnji Cooper #include <stdio.h>
371ee02192SEnji Cooper #include <stdlib.h>
381ee02192SEnji Cooper #include <string.h>
391ee02192SEnji Cooper #include <unistd.h>
401ee02192SEnji Cooper 
411ee02192SEnji Cooper #include <atf-c.h>
421ee02192SEnji Cooper 
431ee02192SEnji Cooper static char tmpfil[PATH_MAX];
441ee02192SEnji Cooper 
451ee02192SEnji Cooper ATF_TC_WITHOUT_HEAD(perror_test);
ATF_TC_BODY(perror_test,tc)461ee02192SEnji Cooper ATF_TC_BODY(perror_test, tc)
471ee02192SEnji Cooper {
48*95631a07SBaptiste Daroussin 	char lbuf[512];
491ee02192SEnji Cooper 	int i;
501ee02192SEnji Cooper 	char *s;
511ee02192SEnji Cooper 
521ee02192SEnji Cooper 	strcpy(tmpfil, "perror.XXXXXXXX");
531ee02192SEnji Cooper 	ATF_REQUIRE(mkstemp(tmpfil) >= 0);
541ee02192SEnji Cooper 	/* Reopen stderr on a file descriptor other than 2. */
551ee02192SEnji Cooper 	fclose(stderr);
561ee02192SEnji Cooper 	for (i = 0; i < 3; i++)
571ee02192SEnji Cooper 		dup(0);
581ee02192SEnji Cooper 	ATF_REQUIRE(freopen(tmpfil, "r+", stderr) != NULL);
591ee02192SEnji Cooper 
601ee02192SEnji Cooper 	/*
611ee02192SEnji Cooper 	 * Test that perror() doesn't call strerror() (4.4BSD bug),
621ee02192SEnji Cooper 	 * the two ways of omitting a program name, and the formatting when
631ee02192SEnji Cooper 	 * a program name is specified.
641ee02192SEnji Cooper 	 */
651ee02192SEnji Cooper 	s = strerror(ENOENT);
661ee02192SEnji Cooper 	ATF_REQUIRE_MSG(strcmp(s, "No such file or directory") == 0,
671ee02192SEnji Cooper 	    "message obtained was: %s", s);
681ee02192SEnji Cooper 	errno = EPERM;
691ee02192SEnji Cooper 	perror(NULL);
701ee02192SEnji Cooper 	perror("");
711ee02192SEnji Cooper 	perror("perror_test");
721ee02192SEnji Cooper 	ATF_REQUIRE_MSG(strcmp(s, "No such file or directory") == 0,
731ee02192SEnji Cooper 	    "message obtained was: %s", s);
741ee02192SEnji Cooper 
751ee02192SEnji Cooper 	/*
761ee02192SEnji Cooper 	 * Read it back to check...
771ee02192SEnji Cooper 	 */
781ee02192SEnji Cooper 	rewind(stderr);
791ee02192SEnji Cooper 	s = fgets(lbuf, sizeof(lbuf), stderr);
801ee02192SEnji Cooper 	ATF_REQUIRE(s != NULL);
811ee02192SEnji Cooper 	ATF_REQUIRE_MSG(strcmp(s, "Operation not permitted\n") == 0,
821ee02192SEnji Cooper 	    "message obtained was: %s", s);
831ee02192SEnji Cooper 	s = fgets(lbuf, sizeof(lbuf), stderr);
841ee02192SEnji Cooper 	ATF_REQUIRE(s != NULL);
851ee02192SEnji Cooper 	ATF_REQUIRE_MSG(strcmp(s, "Operation not permitted\n") == 0,
861ee02192SEnji Cooper 	    "message obtained was: %s", s);
871ee02192SEnji Cooper 	s = fgets(lbuf, sizeof(lbuf), stderr);
881ee02192SEnji Cooper 	ATF_REQUIRE(s != NULL);
891ee02192SEnji Cooper 	ATF_REQUIRE_MSG(
901ee02192SEnji Cooper 	    strcmp(s, "perror_test: Operation not permitted\n") == 0,
911ee02192SEnji Cooper 	    "message obtained was: %s", s);
921ee02192SEnji Cooper 	s = fgets(lbuf, sizeof(lbuf), stderr);
931ee02192SEnji Cooper 	ATF_REQUIRE(s == NULL);
941ee02192SEnji Cooper 	fclose(stderr);
951ee02192SEnji Cooper 
961ee02192SEnji Cooper }
971ee02192SEnji Cooper 
ATF_TP_ADD_TCS(tp)981ee02192SEnji Cooper ATF_TP_ADD_TCS(tp)
991ee02192SEnji Cooper {
1001ee02192SEnji Cooper 
1011ee02192SEnji Cooper 	ATF_TP_ADD_TC(tp, perror_test);
1021ee02192SEnji Cooper 
1031ee02192SEnji Cooper 	return (atf_no_error());
1041ee02192SEnji Cooper }
105