Home | History | Annotate | Line # | Download | only in c063
t_openat.c revision 1.1
      1 /*	$NetBSD: t_openat.c,v 1.1 2012/11/18 17:41:54 manu Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Emmanuel Dreyfus.
      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_openat.c,v 1.1 2012/11/18 17:41:54 manu Exp $");
     33 
     34 #include <atf-c.h>
     35 #include <errno.h>
     36 #include <fcntl.h>
     37 #include <limits.h>
     38 #include <paths.h>
     39 #include <stdio.h>
     40 #include <string.h>
     41 #include <unistd.h>
     42 #include <sys/param.h>
     43 
     44 #define DIR "dir"
     45 #define FILE "dir/openat"
     46 #define BASEFILE "openat"
     47 #define FILEERR "dir/openaterr"
     48 
     49 ATF_TC_WITH_CLEANUP(openat_fd);
     50 ATF_TC_HEAD(openat_fd, tc)
     51 {
     52 	atf_tc_set_md_var(tc, "descr", "See that openat works with fd");
     53 }
     54 
     55 ATF_TC_BODY(openat_fd, tc)
     56 {
     57 	int dfd;
     58 	int fd;
     59 
     60 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
     61 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
     62 	ATF_REQUIRE(close(fd) == 0);
     63 
     64 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
     65 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDONLY, 0)) != -1);
     66 	ATF_REQUIRE(close(dfd) == 0);
     67 	ATF_REQUIRE(close(fd) == 0);
     68 }
     69 
     70 ATF_TC_CLEANUP(openat_fd, tc)
     71 {
     72 	(void)unlink(FILE);
     73 	(void)unlink(FILEERR);
     74 	(void)rmdir(DIR);
     75 }
     76 
     77 ATF_TC_WITH_CLEANUP(openat_fdcwd);
     78 ATF_TC_HEAD(openat_fdcwd, tc)
     79 {
     80 	atf_tc_set_md_var(tc, "descr",
     81 			  "See that openat works with fd as AT_FDCWD");
     82 }
     83 
     84 ATF_TC_BODY(openat_fdcwd, tc)
     85 {
     86 	int fd;
     87 
     88 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
     89 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
     90 	ATF_REQUIRE(close(fd) == 0);
     91 
     92 	ATF_REQUIRE(chdir(DIR) == 0);
     93 	ATF_REQUIRE((fd = openat(AT_FDCWD, BASEFILE, O_RDONLY, 0)) != -1);
     94 	ATF_REQUIRE(close(fd) == 0);
     95 }
     96 
     97 ATF_TC_CLEANUP(openat_fdcwd, tc)
     98 {
     99 	(void)unlink(FILE);
    100 	(void)unlink(FILEERR);
    101 	(void)rmdir(DIR);
    102 }
    103 
    104 ATF_TC_WITH_CLEANUP(openat_fdcwderr);
    105 ATF_TC_HEAD(openat_fdcwderr, tc)
    106 {
    107 	atf_tc_set_md_var(tc, "descr",
    108 		  "See that openat fails with fd as AT_FDCWD and bad path");
    109 }
    110 
    111 ATF_TC_BODY(openat_fdcwderr, tc)
    112 {
    113 	int fd;
    114 
    115 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
    116 	ATF_REQUIRE((fd = openat(AT_FDCWD, FILEERR, O_RDONLY, 0)) == -1);
    117 }
    118 
    119 ATF_TC_CLEANUP(openat_fdcwderr, tc)
    120 {
    121 	(void)unlink(FILE);
    122 	(void)unlink(FILEERR);
    123 	(void)rmdir(DIR);
    124 }
    125 
    126 ATF_TC_WITH_CLEANUP(openat_fderr1);
    127 ATF_TC_HEAD(openat_fderr1, tc)
    128 {
    129 	atf_tc_set_md_var(tc, "descr", "See that openat fail with bad path");
    130 }
    131 
    132 ATF_TC_BODY(openat_fderr1, tc)
    133 {
    134 	int dfd;
    135 	int fd;
    136 
    137 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
    138 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
    139 	ATF_REQUIRE((fd = openat(dfd, FILEERR, O_RDONLY, 0)) == -1);
    140 	ATF_REQUIRE(close(dfd) == 0);
    141 }
    142 
    143 ATF_TC_CLEANUP(openat_fderr1, tc)
    144 {
    145 	(void)unlink(FILE);
    146 	(void)unlink(FILEERR);
    147 	(void)rmdir(DIR);
    148 }
    149 
    150 ATF_TC_WITH_CLEANUP(openat_fderr2);
    151 ATF_TC_HEAD(openat_fderr2, tc)
    152 {
    153 	atf_tc_set_md_var(tc, "descr", "See that openat fails with bad fdat");
    154 }
    155 
    156 ATF_TC_BODY(openat_fderr2, tc)
    157 {
    158 	int dfd;
    159 	int fd;
    160 	char cwd[MAXPATHLEN];
    161 
    162 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
    163 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
    164 	ATF_REQUIRE(close(fd) == 0);
    165 
    166 	ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
    167 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDONLY, 0)) == -1);
    168 	ATF_REQUIRE(close(dfd) == 0);
    169 }
    170 
    171 ATF_TC_CLEANUP(openat_fderr2, tc)
    172 {
    173 	(void)unlink(FILE);
    174 	(void)unlink(FILEERR);
    175 	(void)rmdir(DIR);
    176 }
    177 
    178 ATF_TC_WITH_CLEANUP(openat_fderr3);
    179 ATF_TC_HEAD(openat_fderr3, tc)
    180 {
    181 	atf_tc_set_md_var(tc, "descr", "See that openat fails with fd as -1");
    182 }
    183 
    184 ATF_TC_BODY(openat_fderr3, tc)
    185 {
    186 	int fd;
    187 
    188 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
    189 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
    190 	ATF_REQUIRE(close(fd) == 0);
    191 
    192 	ATF_REQUIRE((fd = openat(-1, FILE, O_RDONLY, 0)) == -1);
    193 }
    194 
    195 ATF_TC_CLEANUP(openat_fderr3, tc)
    196 {
    197 	(void)unlink(FILE);
    198 	(void)unlink(FILEERR);
    199 	(void)rmdir(DIR);
    200 }
    201 
    202 
    203 ATF_TP_ADD_TCS(tp)
    204 {
    205 
    206 	ATF_TP_ADD_TC(tp, openat_fd);
    207 	ATF_TP_ADD_TC(tp, openat_fdcwd);
    208 	ATF_TP_ADD_TC(tp, openat_fdcwderr);
    209 	ATF_TP_ADD_TC(tp, openat_fderr1);
    210 	ATF_TP_ADD_TC(tp, openat_fderr2);
    211 	ATF_TP_ADD_TC(tp, openat_fderr3);
    212 
    213 	return atf_no_error();
    214 }
    215