t_ttyname.c revision 278413
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
41static long ttymax = 0;
42
43ATF_TC(ttyname_err);
44ATF_TC_HEAD(ttyname_err, tc)
45{
46	atf_tc_set_md_var(tc, "descr", "Test errors in ttyname(3)");
47}
48
49ATF_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
84ATF_TC(ttyname_r_err);
85ATF_TC_HEAD(ttyname_r_err, tc)
86{
87	atf_tc_set_md_var(tc, "descr", "Test errors in ttyname_r(3)");
88}
89
90ATF_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	rv = ttyname_r(-1, buf, ttymax);
111	ATF_REQUIRE(rv == EBADF);
112
113	fd = open("/etc/passwd", O_RDONLY);
114
115	if (fd >= 0) {
116		rv = ttyname_r(fd, buf, ttymax);
117		ATF_REQUIRE(rv == ENOTTY);
118		ATF_REQUIRE(close(fd) == 0);
119	}
120
121	free(buf);
122}
123
124ATF_TC(ttyname_r_stdin);
125ATF_TC_HEAD(ttyname_r_stdin, tc)
126{
127	atf_tc_set_md_var(tc, "descr", "Test ttyname_r(3) with stdin(3)");
128}
129
130ATF_TC_BODY(ttyname_r_stdin, tc)
131{
132	const char *str;
133	char *buf;
134	int rv;
135
136	if (isatty(STDIN_FILENO) == 0)
137		return;
138
139	buf = malloc(ttymax + 1);
140
141	if (buf == NULL)
142		return;
143
144	(void)memset(buf, '\0', ttymax + 1);
145
146	str = ttyname(STDIN_FILENO);
147	rv = ttyname_r(STDIN_FILENO, buf, ttymax);
148
149	ATF_REQUIRE(rv == 0);
150	ATF_REQUIRE(str != NULL);
151
152	if (strcmp(str, buf) != 0)
153		atf_tc_fail("ttyname(3) and ttyname_r(3) conflict");
154
155	free(buf);
156}
157
158ATF_TC(ttyname_stdin);
159ATF_TC_HEAD(ttyname_stdin, tc)
160{
161	atf_tc_set_md_var(tc, "descr", "Test ttyname(3) with stdin(3)");
162}
163
164ATF_TC_BODY(ttyname_stdin, tc)
165{
166
167	if (isatty(STDIN_FILENO) != 0)
168		ATF_REQUIRE(ttyname(STDIN_FILENO) != NULL);
169
170	(void)close(STDIN_FILENO);
171
172	ATF_REQUIRE(isatty(STDIN_FILENO) != 1);
173	ATF_REQUIRE(ttyname(STDIN_FILENO) == NULL);
174}
175
176ATF_TP_ADD_TCS(tp)
177{
178
179	ttymax = sysconf(_SC_TTY_NAME_MAX);
180	ATF_REQUIRE(ttymax >= 0);
181
182	ATF_TP_ADD_TC(tp, ttyname_err);
183	ATF_TP_ADD_TC(tp, ttyname_r_err);
184	ATF_TP_ADD_TC(tp, ttyname_r_stdin);
185	ATF_TP_ADD_TC(tp, ttyname_stdin);
186
187	return atf_no_error();
188}
189