1272343Sngie/* $NetBSD: t_openat.c,v 1.2 2013/03/17 04:46:06 jmmv Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2012 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Emmanuel Dreyfus. 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_openat.c,v 1.2 2013/03/17 04:46:06 jmmv Exp $"); 33272343Sngie 34272343Sngie#include <atf-c.h> 35272343Sngie#include <errno.h> 36272343Sngie#include <fcntl.h> 37272343Sngie#include <limits.h> 38272343Sngie#include <paths.h> 39272343Sngie#include <stdio.h> 40272343Sngie#include <string.h> 41272343Sngie#include <unistd.h> 42272343Sngie#include <sys/param.h> 43272343Sngie 44272343Sngie#define DIR "dir" 45272343Sngie#define FILE "dir/openat" 46272343Sngie#define BASEFILE "openat" 47272343Sngie#define FILEERR "dir/openaterr" 48272343Sngie 49272343SngieATF_TC(openat_fd); 50272343SngieATF_TC_HEAD(openat_fd, tc) 51272343Sngie{ 52272343Sngie atf_tc_set_md_var(tc, "descr", "See that openat works with fd"); 53272343Sngie} 54272343SngieATF_TC_BODY(openat_fd, tc) 55272343Sngie{ 56272343Sngie int dfd; 57272343Sngie int fd; 58272343Sngie 59272343Sngie ATF_REQUIRE(mkdir(DIR, 0755) == 0); 60272343Sngie ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 61272343Sngie ATF_REQUIRE(close(fd) == 0); 62272343Sngie 63272343Sngie ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 64272343Sngie ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDONLY, 0)) != -1); 65272343Sngie ATF_REQUIRE(close(dfd) == 0); 66272343Sngie ATF_REQUIRE(close(fd) == 0); 67272343Sngie} 68272343Sngie 69272343SngieATF_TC(openat_fdcwd); 70272343SngieATF_TC_HEAD(openat_fdcwd, tc) 71272343Sngie{ 72272343Sngie atf_tc_set_md_var(tc, "descr", 73272343Sngie "See that openat works with fd as AT_FDCWD"); 74272343Sngie} 75272343SngieATF_TC_BODY(openat_fdcwd, tc) 76272343Sngie{ 77272343Sngie int fd; 78272343Sngie 79272343Sngie ATF_REQUIRE(mkdir(DIR, 0755) == 0); 80272343Sngie ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 81272343Sngie ATF_REQUIRE(close(fd) == 0); 82272343Sngie 83272343Sngie ATF_REQUIRE(chdir(DIR) == 0); 84272343Sngie ATF_REQUIRE((fd = openat(AT_FDCWD, BASEFILE, O_RDONLY, 0)) != -1); 85272343Sngie ATF_REQUIRE(close(fd) == 0); 86272343Sngie} 87272343Sngie 88272343SngieATF_TC(openat_fdcwderr); 89272343SngieATF_TC_HEAD(openat_fdcwderr, tc) 90272343Sngie{ 91272343Sngie atf_tc_set_md_var(tc, "descr", 92272343Sngie "See that openat fails with fd as AT_FDCWD and bad path"); 93272343Sngie} 94272343SngieATF_TC_BODY(openat_fdcwderr, tc) 95272343Sngie{ 96272343Sngie int fd; 97272343Sngie 98272343Sngie ATF_REQUIRE(mkdir(DIR, 0755) == 0); 99272343Sngie ATF_REQUIRE((fd = openat(AT_FDCWD, FILEERR, O_RDONLY, 0)) == -1); 100272343Sngie} 101272343Sngie 102272343SngieATF_TC(openat_fderr1); 103272343SngieATF_TC_HEAD(openat_fderr1, tc) 104272343Sngie{ 105272343Sngie atf_tc_set_md_var(tc, "descr", "See that openat fail with bad path"); 106272343Sngie} 107272343SngieATF_TC_BODY(openat_fderr1, tc) 108272343Sngie{ 109272343Sngie int dfd; 110272343Sngie int fd; 111272343Sngie 112272343Sngie ATF_REQUIRE(mkdir(DIR, 0755) == 0); 113272343Sngie ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 114272343Sngie ATF_REQUIRE((fd = openat(dfd, FILEERR, O_RDONLY, 0)) == -1); 115272343Sngie ATF_REQUIRE(close(dfd) == 0); 116272343Sngie} 117272343Sngie 118272343SngieATF_TC(openat_fderr2); 119272343SngieATF_TC_HEAD(openat_fderr2, tc) 120272343Sngie{ 121272343Sngie atf_tc_set_md_var(tc, "descr", "See that openat fails with bad fdat"); 122272343Sngie} 123272343SngieATF_TC_BODY(openat_fderr2, tc) 124272343Sngie{ 125272343Sngie int dfd; 126272343Sngie int fd; 127272343Sngie char cwd[MAXPATHLEN]; 128272343Sngie 129272343Sngie ATF_REQUIRE(mkdir(DIR, 0755) == 0); 130272343Sngie ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 131272343Sngie ATF_REQUIRE(close(fd) == 0); 132272343Sngie 133272343Sngie ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1); 134272343Sngie ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDONLY, 0)) == -1); 135272343Sngie ATF_REQUIRE(close(dfd) == 0); 136272343Sngie} 137272343Sngie 138272343SngieATF_TC(openat_fderr3); 139272343SngieATF_TC_HEAD(openat_fderr3, tc) 140272343Sngie{ 141272343Sngie atf_tc_set_md_var(tc, "descr", "See that openat fails with fd as -1"); 142272343Sngie} 143272343SngieATF_TC_BODY(openat_fderr3, tc) 144272343Sngie{ 145272343Sngie int fd; 146272343Sngie 147272343Sngie ATF_REQUIRE(mkdir(DIR, 0755) == 0); 148272343Sngie ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 149272343Sngie ATF_REQUIRE(close(fd) == 0); 150272343Sngie 151272343Sngie ATF_REQUIRE((fd = openat(-1, FILE, O_RDONLY, 0)) == -1); 152272343Sngie} 153272343Sngie 154272343SngieATF_TP_ADD_TCS(tp) 155272343Sngie{ 156272343Sngie 157272343Sngie ATF_TP_ADD_TC(tp, openat_fd); 158272343Sngie ATF_TP_ADD_TC(tp, openat_fdcwd); 159272343Sngie ATF_TP_ADD_TC(tp, openat_fdcwderr); 160272343Sngie ATF_TP_ADD_TC(tp, openat_fderr1); 161272343Sngie ATF_TP_ADD_TC(tp, openat_fderr2); 162272343Sngie ATF_TP_ADD_TC(tp, openat_fderr3); 163272343Sngie 164272343Sngie return atf_no_error(); 165272343Sngie} 166