xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_ttyname.c (revision ff0ba87247820afbdfdc1b307c803f7923d0e4d3)
1 /*	$NetBSD: t_ttyname.c,v 1.3 2011/05/01 18:14:01 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_ttyname.c,v 1.3 2011/05/01 18:14:01 jruoho Exp $");
33 
34 #include <atf-c.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 static long ttymax = 0;
42 
43 ATF_TC(ttyname_err);
44 ATF_TC_HEAD(ttyname_err, tc)
45 {
46 	atf_tc_set_md_var(tc, "descr", "Test errors in ttyname(3)");
47 }
48 
49 ATF_TC_BODY(ttyname_err, tc)
50 {
51 	int fd;
52 
53 	fd = open("XXX", O_RDONLY);
54 
55 	if (fd < 0) {
56 
57 		errno = 0;
58 
59 		ATF_REQUIRE(isatty(fd) != -1);
60 		ATF_REQUIRE(errno == EBADF);
61 
62 		errno = 0;
63 
64 		ATF_REQUIRE(ttyname(fd) == NULL);
65 		ATF_REQUIRE(errno == EBADF);
66 	}
67 
68 	fd = open("/etc/passwd", O_RDONLY);
69 
70 	if (fd >= 0) {
71 
72 		errno = 0;
73 
74 		ATF_REQUIRE(isatty(fd) != -1);
75 		ATF_REQUIRE(errno == ENOTTY);
76 
77 		errno = 0;
78 
79 		ATF_REQUIRE(ttyname(fd) == NULL);
80 		ATF_REQUIRE(errno == ENOTTY);
81 	}
82 }
83 
84 ATF_TC(ttyname_r_err);
85 ATF_TC_HEAD(ttyname_r_err, tc)
86 {
87 	atf_tc_set_md_var(tc, "descr", "Test errors in ttyname_r(3)");
88 }
89 
90 ATF_TC_BODY(ttyname_r_err, tc)
91 {
92 	char sbuf[0];
93 	char *buf;
94 	int fd;
95 	int rv;
96 
97 	buf = malloc(ttymax + 1);
98 
99 	if (buf == NULL)
100 		return;
101 
102 	(void)memset(buf, '\0', ttymax + 1);
103 
104 	if (isatty(STDIN_FILENO) != 0) {
105 
106 		rv = ttyname_r(STDIN_FILENO, sbuf, sizeof(sbuf));
107 		ATF_REQUIRE(rv == ERANGE);
108 	}
109 
110 #ifdef __FreeBSD__
111 	atf_tc_expect_fail("FreeBSD returns ENOTTY instead of EBADF; see bin/191936");
112 #endif
113 	rv = ttyname_r(-1, buf, ttymax);
114 	ATF_REQUIRE(rv == EBADF);
115 
116 	fd = open("/etc/passwd", O_RDONLY);
117 
118 	if (fd >= 0) {
119 		rv = ttyname_r(fd, buf, ttymax);
120 		ATF_REQUIRE(rv == ENOTTY);
121 		ATF_REQUIRE(close(fd) == 0);
122 	}
123 
124 	free(buf);
125 }
126 
127 ATF_TC(ttyname_r_stdin);
128 ATF_TC_HEAD(ttyname_r_stdin, tc)
129 {
130 	atf_tc_set_md_var(tc, "descr", "Test ttyname_r(3) with stdin(3)");
131 }
132 
133 ATF_TC_BODY(ttyname_r_stdin, tc)
134 {
135 	const char *str;
136 	char *buf;
137 	int rv;
138 
139 	if (isatty(STDIN_FILENO) == 0)
140 		return;
141 
142 	buf = malloc(ttymax + 1);
143 
144 	if (buf == NULL)
145 		return;
146 
147 	(void)memset(buf, '\0', ttymax + 1);
148 
149 	str = ttyname(STDIN_FILENO);
150 	rv = ttyname_r(STDIN_FILENO, buf, ttymax);
151 
152 	ATF_REQUIRE(rv == 0);
153 	ATF_REQUIRE(str != NULL);
154 
155 	if (strcmp(str, buf) != 0)
156 		atf_tc_fail("ttyname(3) and ttyname_r(3) conflict");
157 
158 	free(buf);
159 }
160 
161 ATF_TC(ttyname_stdin);
162 ATF_TC_HEAD(ttyname_stdin, tc)
163 {
164 	atf_tc_set_md_var(tc, "descr", "Test ttyname(3) with stdin(3)");
165 }
166 
167 ATF_TC_BODY(ttyname_stdin, tc)
168 {
169 
170 	if (isatty(STDIN_FILENO) != 0)
171 		ATF_REQUIRE(ttyname(STDIN_FILENO) != NULL);
172 
173 	(void)close(STDIN_FILENO);
174 
175 	ATF_REQUIRE(isatty(STDIN_FILENO) != 1);
176 	ATF_REQUIRE(ttyname(STDIN_FILENO) == NULL);
177 }
178 
179 ATF_TP_ADD_TCS(tp)
180 {
181 
182 	ttymax = sysconf(_SC_TTY_NAME_MAX);
183 	ATF_REQUIRE(ttymax >= 0);
184 
185 	ATF_TP_ADD_TC(tp, ttyname_err);
186 	ATF_TP_ADD_TC(tp, ttyname_r_err);
187 	ATF_TP_ADD_TC(tp, ttyname_r_stdin);
188 	ATF_TP_ADD_TC(tp, ttyname_stdin);
189 
190 	return atf_no_error();
191 }
192