1 /* $NetBSD: t_ttyname.c,v 1.3 2011/05/01 18:14:01 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_ttyname.c,v 1.3 2011/05/01 18:14:01 jruoho Exp $");
41 static long ttymax
= 0;
44 ATF_TC_HEAD(ttyname_err
, tc
)
46 atf_tc_set_md_var(tc
, "descr", "Test errors in ttyname(3)");
49 ATF_TC_BODY(ttyname_err
, tc
)
53 fd
= open("XXX", O_RDONLY
);
59 ATF_REQUIRE(isatty(fd
) != -1);
60 ATF_REQUIRE(errno
== EBADF
);
64 ATF_REQUIRE(ttyname(fd
) == NULL
);
65 ATF_REQUIRE(errno
== EBADF
);
68 fd
= open("/etc/passwd", O_RDONLY
);
74 ATF_REQUIRE(isatty(fd
) != -1);
75 ATF_REQUIRE(errno
== ENOTTY
);
79 ATF_REQUIRE(ttyname(fd
) == NULL
);
80 ATF_REQUIRE(errno
== ENOTTY
);
84 ATF_TC(ttyname_r_err
);
85 ATF_TC_HEAD(ttyname_r_err
, tc
)
87 atf_tc_set_md_var(tc
, "descr", "Test errors in ttyname_r(3)");
90 ATF_TC_BODY(ttyname_r_err
, tc
)
97 buf
= malloc(ttymax
+ 1);
102 (void)memset(buf
, '\0', ttymax
+ 1);
104 if (isatty(STDIN_FILENO
) != 0) {
106 rv
= ttyname_r(STDIN_FILENO
, sbuf
, sizeof(sbuf
));
107 ATF_REQUIRE(rv
== ERANGE
);
110 rv
= ttyname_r(-1, buf
, ttymax
);
111 ATF_REQUIRE(rv
== EBADF
);
113 fd
= open("/etc/passwd", O_RDONLY
);
116 rv
= ttyname_r(fd
, buf
, ttymax
);
117 ATF_REQUIRE(rv
== ENOTTY
);
118 ATF_REQUIRE(close(fd
) == 0);
124 ATF_TC(ttyname_r_stdin
);
125 ATF_TC_HEAD(ttyname_r_stdin
, tc
)
127 atf_tc_set_md_var(tc
, "descr", "Test ttyname_r(3) with stdin(3)");
130 ATF_TC_BODY(ttyname_r_stdin
, tc
)
136 if (isatty(STDIN_FILENO
) == 0)
139 buf
= malloc(ttymax
+ 1);
144 (void)memset(buf
, '\0', ttymax
+ 1);
146 str
= ttyname(STDIN_FILENO
);
147 rv
= ttyname_r(STDIN_FILENO
, buf
, ttymax
);
149 ATF_REQUIRE(rv
== 0);
150 ATF_REQUIRE(str
!= NULL
);
152 if (strcmp(str
, buf
) != 0)
153 atf_tc_fail("ttyname(3) and ttyname_r(3) conflict");
158 ATF_TC(ttyname_stdin
);
159 ATF_TC_HEAD(ttyname_stdin
, tc
)
161 atf_tc_set_md_var(tc
, "descr", "Test ttyname(3) with stdin(3)");
164 ATF_TC_BODY(ttyname_stdin
, tc
)
167 if (isatty(STDIN_FILENO
) != 0)
168 ATF_REQUIRE(ttyname(STDIN_FILENO
) != NULL
);
170 (void)close(STDIN_FILENO
);
172 ATF_REQUIRE(isatty(STDIN_FILENO
) != 1);
173 ATF_REQUIRE(ttyname(STDIN_FILENO
) == NULL
);
179 ttymax
= sysconf(_SC_TTY_NAME_MAX
);
180 ATF_REQUIRE(ttymax
>= 0);
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
);
187 return atf_no_error();