1272343Sngie/* $NetBSD: t_ttyname.c,v 1.3 2011/05/01 18:14:01 jruoho Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Jukka Ruohonen. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie#include <sys/cdefs.h> 32272343Sngie__RCSID("$NetBSD: t_ttyname.c,v 1.3 2011/05/01 18:14:01 jruoho Exp $"); 33272343Sngie 34272343Sngie#include <atf-c.h> 35272343Sngie#include <errno.h> 36272343Sngie#include <fcntl.h> 37272343Sngie#include <stdlib.h> 38272343Sngie#include <string.h> 39272343Sngie#include <unistd.h> 40272343Sngie 41272343Sngiestatic long ttymax = 0; 42272343Sngie 43272343SngieATF_TC(ttyname_err); 44272343SngieATF_TC_HEAD(ttyname_err, tc) 45272343Sngie{ 46272343Sngie atf_tc_set_md_var(tc, "descr", "Test errors in ttyname(3)"); 47272343Sngie} 48272343Sngie 49272343SngieATF_TC_BODY(ttyname_err, tc) 50272343Sngie{ 51272343Sngie int fd; 52272343Sngie 53272343Sngie fd = open("XXX", O_RDONLY); 54272343Sngie 55272343Sngie if (fd < 0) { 56272343Sngie 57272343Sngie errno = 0; 58272343Sngie 59272343Sngie ATF_REQUIRE(isatty(fd) != -1); 60272343Sngie ATF_REQUIRE(errno == EBADF); 61272343Sngie 62272343Sngie errno = 0; 63272343Sngie 64272343Sngie ATF_REQUIRE(ttyname(fd) == NULL); 65272343Sngie ATF_REQUIRE(errno == EBADF); 66272343Sngie } 67272343Sngie 68272343Sngie fd = open("/etc/passwd", O_RDONLY); 69272343Sngie 70272343Sngie if (fd >= 0) { 71272343Sngie 72272343Sngie errno = 0; 73272343Sngie 74272343Sngie ATF_REQUIRE(isatty(fd) != -1); 75272343Sngie ATF_REQUIRE(errno == ENOTTY); 76272343Sngie 77272343Sngie errno = 0; 78272343Sngie 79272343Sngie ATF_REQUIRE(ttyname(fd) == NULL); 80272343Sngie ATF_REQUIRE(errno == ENOTTY); 81272343Sngie } 82272343Sngie} 83272343Sngie 84272343SngieATF_TC(ttyname_r_err); 85272343SngieATF_TC_HEAD(ttyname_r_err, tc) 86272343Sngie{ 87272343Sngie atf_tc_set_md_var(tc, "descr", "Test errors in ttyname_r(3)"); 88272343Sngie} 89272343Sngie 90272343SngieATF_TC_BODY(ttyname_r_err, tc) 91272343Sngie{ 92272343Sngie char sbuf[0]; 93272343Sngie char *buf; 94272343Sngie int fd; 95272343Sngie int rv; 96272343Sngie 97272343Sngie buf = malloc(ttymax + 1); 98272343Sngie 99272343Sngie if (buf == NULL) 100272343Sngie return; 101272343Sngie 102272343Sngie (void)memset(buf, '\0', ttymax + 1); 103272343Sngie 104272343Sngie if (isatty(STDIN_FILENO) != 0) { 105272343Sngie 106272343Sngie rv = ttyname_r(STDIN_FILENO, sbuf, sizeof(sbuf)); 107272343Sngie ATF_REQUIRE(rv == ERANGE); 108272343Sngie } 109272343Sngie 110272343Sngie rv = ttyname_r(-1, buf, ttymax); 111272343Sngie ATF_REQUIRE(rv == EBADF); 112272343Sngie 113272343Sngie fd = open("/etc/passwd", O_RDONLY); 114272343Sngie 115272343Sngie if (fd >= 0) { 116272343Sngie rv = ttyname_r(fd, buf, ttymax); 117272343Sngie ATF_REQUIRE(rv == ENOTTY); 118272343Sngie ATF_REQUIRE(close(fd) == 0); 119272343Sngie } 120272343Sngie 121272343Sngie free(buf); 122272343Sngie} 123272343Sngie 124272343SngieATF_TC(ttyname_r_stdin); 125272343SngieATF_TC_HEAD(ttyname_r_stdin, tc) 126272343Sngie{ 127272343Sngie atf_tc_set_md_var(tc, "descr", "Test ttyname_r(3) with stdin(3)"); 128272343Sngie} 129272343Sngie 130272343SngieATF_TC_BODY(ttyname_r_stdin, tc) 131272343Sngie{ 132272343Sngie const char *str; 133272343Sngie char *buf; 134272343Sngie int rv; 135272343Sngie 136272343Sngie if (isatty(STDIN_FILENO) == 0) 137272343Sngie return; 138272343Sngie 139272343Sngie buf = malloc(ttymax + 1); 140272343Sngie 141272343Sngie if (buf == NULL) 142272343Sngie return; 143272343Sngie 144272343Sngie (void)memset(buf, '\0', ttymax + 1); 145272343Sngie 146272343Sngie str = ttyname(STDIN_FILENO); 147272343Sngie rv = ttyname_r(STDIN_FILENO, buf, ttymax); 148272343Sngie 149272343Sngie ATF_REQUIRE(rv == 0); 150272343Sngie ATF_REQUIRE(str != NULL); 151272343Sngie 152272343Sngie if (strcmp(str, buf) != 0) 153272343Sngie atf_tc_fail("ttyname(3) and ttyname_r(3) conflict"); 154272343Sngie 155272343Sngie free(buf); 156272343Sngie} 157272343Sngie 158272343SngieATF_TC(ttyname_stdin); 159272343SngieATF_TC_HEAD(ttyname_stdin, tc) 160272343Sngie{ 161272343Sngie atf_tc_set_md_var(tc, "descr", "Test ttyname(3) with stdin(3)"); 162272343Sngie} 163272343Sngie 164272343SngieATF_TC_BODY(ttyname_stdin, tc) 165272343Sngie{ 166272343Sngie 167272343Sngie if (isatty(STDIN_FILENO) != 0) 168272343Sngie ATF_REQUIRE(ttyname(STDIN_FILENO) != NULL); 169272343Sngie 170272343Sngie (void)close(STDIN_FILENO); 171272343Sngie 172272343Sngie ATF_REQUIRE(isatty(STDIN_FILENO) != 1); 173272343Sngie ATF_REQUIRE(ttyname(STDIN_FILENO) == NULL); 174272343Sngie} 175272343Sngie 176272343SngieATF_TP_ADD_TCS(tp) 177272343Sngie{ 178272343Sngie 179272343Sngie ttymax = sysconf(_SC_TTY_NAME_MAX); 180272343Sngie ATF_REQUIRE(ttymax >= 0); 181272343Sngie 182272343Sngie ATF_TP_ADD_TC(tp, ttyname_err); 183272343Sngie ATF_TP_ADD_TC(tp, ttyname_r_err); 184272343Sngie ATF_TP_ADD_TC(tp, ttyname_r_stdin); 185272343Sngie ATF_TP_ADD_TC(tp, ttyname_stdin); 186272343Sngie 187272343Sngie return atf_no_error(); 188272343Sngie} 189