xref: /freebsd/contrib/netbsd-tests/lib/libc/c063/t_openat.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*	$NetBSD: t_openat.c,v 1.3 2017/01/10 15:13:56 christos 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.3 2017/01/10 15:13:56 christos Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <paths.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #define DIR "dir"
46 #define FILE "dir/openat"
47 #define BASEFILE "openat"
48 #define FILEERR "dir/openaterr"
49 
50 ATF_TC(openat_fd);
51 ATF_TC_HEAD(openat_fd, tc)
52 {
53 	atf_tc_set_md_var(tc, "descr", "See that openat works with fd");
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(openat_fdcwd);
71 ATF_TC_HEAD(openat_fdcwd, tc)
72 {
73 	atf_tc_set_md_var(tc, "descr",
74 			  "See that openat works with fd as AT_FDCWD");
75 }
76 ATF_TC_BODY(openat_fdcwd, tc)
77 {
78 	int fd;
79 
80 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
81 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
82 	ATF_REQUIRE(close(fd) == 0);
83 
84 	ATF_REQUIRE(chdir(DIR) == 0);
85 	ATF_REQUIRE((fd = openat(AT_FDCWD, BASEFILE, O_RDONLY, 0)) != -1);
86 	ATF_REQUIRE(close(fd) == 0);
87 }
88 
89 ATF_TC(openat_fdcwderr);
90 ATF_TC_HEAD(openat_fdcwderr, tc)
91 {
92 	atf_tc_set_md_var(tc, "descr",
93 		  "See that openat fails with fd as AT_FDCWD and bad path");
94 }
95 ATF_TC_BODY(openat_fdcwderr, tc)
96 {
97 	int fd;
98 
99 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
100 	ATF_REQUIRE((fd = openat(AT_FDCWD, FILEERR, O_RDONLY, 0)) == -1);
101 }
102 
103 ATF_TC(openat_fderr1);
104 ATF_TC_HEAD(openat_fderr1, tc)
105 {
106 	atf_tc_set_md_var(tc, "descr", "See that openat fail with bad path");
107 }
108 ATF_TC_BODY(openat_fderr1, tc)
109 {
110 	int dfd;
111 	int fd;
112 
113 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
114 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
115 	ATF_REQUIRE((fd = openat(dfd, FILEERR, O_RDONLY, 0)) == -1);
116 	ATF_REQUIRE(close(dfd) == 0);
117 }
118 
119 ATF_TC(openat_fderr2);
120 ATF_TC_HEAD(openat_fderr2, tc)
121 {
122 	atf_tc_set_md_var(tc, "descr", "See that openat fails with bad fdat");
123 }
124 ATF_TC_BODY(openat_fderr2, tc)
125 {
126 	int dfd;
127 	int fd;
128 	char cwd[MAXPATHLEN];
129 
130 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
131 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
132 	ATF_REQUIRE(close(fd) == 0);
133 
134 	ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1);
135 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDONLY, 0)) == -1);
136 	ATF_REQUIRE(close(dfd) == 0);
137 }
138 
139 ATF_TC(openat_fderr3);
140 ATF_TC_HEAD(openat_fderr3, tc)
141 {
142 	atf_tc_set_md_var(tc, "descr", "See that openat fails with fd as -1");
143 }
144 ATF_TC_BODY(openat_fderr3, tc)
145 {
146 	int fd;
147 
148 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
149 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
150 	ATF_REQUIRE(close(fd) == 0);
151 
152 	ATF_REQUIRE((fd = openat(-1, FILE, O_RDONLY, 0)) == -1);
153 }
154 
155 ATF_TP_ADD_TCS(tp)
156 {
157 
158 	ATF_TP_ADD_TC(tp, openat_fd);
159 	ATF_TP_ADD_TC(tp, openat_fdcwd);
160 	ATF_TP_ADD_TC(tp, openat_fdcwderr);
161 	ATF_TP_ADD_TC(tp, openat_fderr1);
162 	ATF_TP_ADD_TC(tp, openat_fderr2);
163 	ATF_TP_ADD_TC(tp, openat_fderr3);
164 
165 	return atf_no_error();
166 }
167