xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_ttyname.c (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1 /*	$NetBSD: t_ttyname.c,v 1.4 2017/01/10 15:33:40 christos 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.4 2017/01/10 15:33:40 christos 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 		(void)close(fd);
82 	}
83 }
84 
85 ATF_TC(ttyname_r_err);
86 ATF_TC_HEAD(ttyname_r_err, tc)
87 {
88 	atf_tc_set_md_var(tc, "descr", "Test errors in ttyname_r(3)");
89 }
90 
91 ATF_TC_BODY(ttyname_r_err, tc)
92 {
93 	char sbuf[0];
94 	char *buf;
95 	int fd;
96 	int rv;
97 
98 	buf = malloc(ttymax + 1);
99 
100 	if (buf == NULL)
101 		return;
102 
103 	(void)memset(buf, '\0', ttymax + 1);
104 
105 	if (isatty(STDIN_FILENO) != 0) {
106 
107 		rv = ttyname_r(STDIN_FILENO, sbuf, sizeof(sbuf));
108 		ATF_REQUIRE(rv == ERANGE);
109 	}
110 
111 	rv = ttyname_r(-1, buf, ttymax);
112 	ATF_REQUIRE(rv == EBADF);
113 
114 	fd = open("/etc/passwd", O_RDONLY);
115 
116 	if (fd >= 0) {
117 		rv = ttyname_r(fd, buf, ttymax);
118 		ATF_REQUIRE(rv == ENOTTY);
119 		ATF_REQUIRE(close(fd) == 0);
120 	}
121 
122 	free(buf);
123 }
124 
125 ATF_TC(ttyname_r_stdin);
126 ATF_TC_HEAD(ttyname_r_stdin, tc)
127 {
128 	atf_tc_set_md_var(tc, "descr", "Test ttyname_r(3) with stdin(3)");
129 }
130 
131 ATF_TC_BODY(ttyname_r_stdin, tc)
132 {
133 	const char *str;
134 	char *buf;
135 	int rv;
136 
137 	if (isatty(STDIN_FILENO) == 0)
138 		return;
139 
140 	buf = malloc(ttymax + 1);
141 
142 	if (buf == NULL)
143 		return;
144 
145 	(void)memset(buf, '\0', ttymax + 1);
146 
147 	str = ttyname(STDIN_FILENO);
148 	rv = ttyname_r(STDIN_FILENO, buf, ttymax);
149 
150 	ATF_REQUIRE(rv == 0);
151 	ATF_REQUIRE(str != NULL);
152 
153 	if (strcmp(str, buf) != 0)
154 		atf_tc_fail("ttyname(3) and ttyname_r(3) conflict");
155 
156 	free(buf);
157 }
158 
159 ATF_TC(ttyname_stdin);
160 ATF_TC_HEAD(ttyname_stdin, tc)
161 {
162 	atf_tc_set_md_var(tc, "descr", "Test ttyname(3) with stdin(3)");
163 }
164 
165 ATF_TC_BODY(ttyname_stdin, tc)
166 {
167 
168 	if (isatty(STDIN_FILENO) != 0)
169 		ATF_REQUIRE(ttyname(STDIN_FILENO) != NULL);
170 
171 	(void)close(STDIN_FILENO);
172 
173 	ATF_REQUIRE(isatty(STDIN_FILENO) != 1);
174 	ATF_REQUIRE(ttyname(STDIN_FILENO) == NULL);
175 }
176 
177 ATF_TP_ADD_TCS(tp)
178 {
179 
180 	ttymax = sysconf(_SC_TTY_NAME_MAX);
181 	ATF_REQUIRE(ttymax >= 0);
182 
183 	ATF_TP_ADD_TC(tp, ttyname_err);
184 	ATF_TP_ADD_TC(tp, ttyname_r_err);
185 	ATF_TP_ADD_TC(tp, ttyname_r_stdin);
186 	ATF_TP_ADD_TC(tp, ttyname_stdin);
187 
188 	return atf_no_error();
189 }
190