xref: /freebsd/contrib/netbsd-tests/lib/libc/c063/t_readlinkat.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*	$NetBSD: t_readlinkat.c,v 1.4 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_readlinkat.c,v 1.4 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/readlinkat"
47 #define BASEFILE "readlinkat"
48 #define LINK "dir/symlink"
49 #define BASELINK "symlink"
50 #define FILEERR "dir/readlinkaterr"
51 
52 ATF_TC(readlinkat_fd);
53 ATF_TC_HEAD(readlinkat_fd, tc)
54 {
55 	atf_tc_set_md_var(tc, "descr", "See that readlinkat works with fd");
56 }
57 ATF_TC_BODY(readlinkat_fd, tc)
58 {
59 	int dfd;
60 	int fd;
61 	ssize_t len;
62 	char buf[MAXPATHLEN];
63 
64 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
65 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
66 	ATF_REQUIRE(close(fd) == 0);
67 	ATF_REQUIRE(symlink(FILE, LINK) == 0);
68 
69 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
70 	len = readlinkat(dfd, BASELINK, buf, sizeof(buf)-1);
71 	ATF_REQUIRE(len != -1);
72 	buf[len] = 0;
73 	ATF_REQUIRE(close(dfd) == 0);
74 
75 	ATF_REQUIRE(strcmp(buf, FILE) == 0);
76 }
77 
78 ATF_TC(readlinkat_fdcwd);
79 ATF_TC_HEAD(readlinkat_fdcwd, tc)
80 {
81 	atf_tc_set_md_var(tc, "descr",
82 			  "See that readlinkat works with fd as AT_FDCWD");
83 }
84 ATF_TC_BODY(readlinkat_fdcwd, tc)
85 {
86 	int fd;
87 	ssize_t len;
88 	char buf[MAXPATHLEN];
89 
90 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
91 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
92 	ATF_REQUIRE(close(fd) == 0);
93 	ATF_REQUIRE(symlink(FILE, LINK) == 0);
94 
95 	len = readlinkat(AT_FDCWD, LINK, buf, sizeof(buf)-1);
96 	ATF_REQUIRE(len != -1);
97 	buf[len] = 0;
98 
99 	ATF_REQUIRE(strcmp(buf, FILE) == 0);
100 }
101 
102 ATF_TC(readlinkat_fdcwderr);
103 ATF_TC_HEAD(readlinkat_fdcwderr, tc)
104 {
105 	atf_tc_set_md_var(tc, "descr",
106 		  "See that readlinkat fails with fd as AT_FDCWD and bad path");
107 }
108 ATF_TC_BODY(readlinkat_fdcwderr, tc)
109 {
110 	char buf[MAXPATHLEN];
111 
112 	ATF_REQUIRE(readlinkat(AT_FDCWD, LINK, buf, sizeof(buf)) == -1);
113 }
114 
115 ATF_TC(readlinkat_fderr1);
116 ATF_TC_HEAD(readlinkat_fderr1, tc)
117 {
118 	atf_tc_set_md_var(tc, "descr", "See that readlinkat fail with bad path");
119 }
120 ATF_TC_BODY(readlinkat_fderr1, tc)
121 {
122 	int dfd;
123 
124 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
125 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
126 	ATF_REQUIRE(readlinkat(dfd, FILEERR, F_OK, 0) == -1);
127 	ATF_REQUIRE(close(dfd) == 0);
128 }
129 
130 ATF_TC(readlinkat_fderr2);
131 ATF_TC_HEAD(readlinkat_fderr2, tc)
132 {
133 	atf_tc_set_md_var(tc, "descr", "See that readlinkat fails with fd as -1");
134 }
135 ATF_TC_BODY(readlinkat_fderr2, tc)
136 {
137 	int fd;
138 	char buf[MAXPATHLEN];
139 
140 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
141 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
142 	ATF_REQUIRE(close(fd) == 0);
143 	ATF_REQUIRE(symlink(FILE, LINK) == 0);
144 
145 	ATF_REQUIRE(readlinkat(-1, LINK, buf, sizeof(buf)) == -1);
146 }
147 
148 ATF_TP_ADD_TCS(tp)
149 {
150 
151 	ATF_TP_ADD_TC(tp, readlinkat_fd);
152 	ATF_TP_ADD_TC(tp, readlinkat_fdcwd);
153 	ATF_TP_ADD_TC(tp, readlinkat_fdcwderr);
154 	ATF_TP_ADD_TC(tp, readlinkat_fderr1);
155 	ATF_TP_ADD_TC(tp, readlinkat_fderr2);
156 
157 	return atf_no_error();
158 }
159